KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > frontend > Compiler


1 package polyglot.frontend;
2
3 import java.io.FileNotFoundException JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.util.*;
6
7 import polyglot.types.reflect.ClassFileLoader;
8 import polyglot.main.Report;
9 import polyglot.util.*;
10
11 /**
12  * This is the main entry point for the compiler. It contains a work list that
13  * contains entries for all classes that must be compiled (or otherwise worked
14  * on).
15  */

16 public class Compiler
17 {
18     /** The extension info */
19     private ExtensionInfo extensionInfo;
20
21     /** A list of all extension infos active in this compiler. */
22     private List allExtensions;
23
24     /** The error queue handles outputting error messages. */
25     private ErrorQueue eq;
26
27     /**
28      * Class file loader. There should be only one of these so we can cache
29      * across type systems.
30      */

31     private ClassFileLoader loader;
32
33     /**
34      * The output files generated by the compiler. This is used to to call the
35      * post-compiler (e.g., javac).
36      */

37     private Collection outputFiles = new HashSet();
38
39     /**
40      * Initialize the compiler.
41      *
42      * @param extensionInfo the <code>ExtensionInfo</code> this compiler is for.
43      */

44     public Compiler(ExtensionInfo extensionInfo) {
45         this(extensionInfo, new StdErrorQueue(System.err,
46                                               extensionInfo.getOptions().error_count,
47                                               extensionInfo.compilerName()));
48     }
49         
50     /**
51      * Initialize the compiler.
52      *
53      * @param extensionInfo the <code>ExtensionInfo</code> this compiler is for.
54      */

55     public Compiler(ExtensionInfo extensionInfo, ErrorQueue eq) {
56         this.extensionInfo = extensionInfo;
57         this.eq = eq;
58         this.allExtensions = new ArrayList(2);
59
60         loader = new ClassFileLoader();
61
62         // This must be done last.
63
extensionInfo.initCompiler(this);
64     }
65
66     /** Return a set of output filenames resulting from a compilation. */
67     public Collection outputFiles() {
68         return outputFiles;
69     }
70
71     /**
72      * Compile all the files listed in the set of strings <code>source</code>.
73      * Return true on success. The method <code>outputFiles</code> can be
74      * used to obtain the output of the compilation. This is the main entry
75      * point for the compiler, called from main().
76      */

77     public boolean compile(Collection sources) {
78     boolean okay = false;
79
80     try {
81         try {
82                 SourceLoader source_loader = sourceExtension().sourceLoader();
83
84         for (Iterator i = sources.iterator(); i.hasNext(); ) {
85             String JavaDoc sourceName = (String JavaDoc) i.next();
86             FileSource source = source_loader.fileSource(sourceName);
87
88                     // mark this source as being explicitly specified
89
// by the user.
90
source.setUserSpecified(true);
91
92             sourceExtension().addJob(source);
93         }
94
95         okay = sourceExtension().runToCompletion();
96         }
97         catch (FileNotFoundException JavaDoc e) {
98         eq.enqueue(ErrorInfo.IO_ERROR,
99             "Cannot find source file \"" + e.getMessage() + "\".");
100         }
101         catch (IOException JavaDoc e) {
102         eq.enqueue(ErrorInfo.IO_ERROR, e.getMessage());
103         }
104         catch (InternalCompilerError e) {
105         // Report it like other errors, but rethrow to get the stack trace.
106
try {
107             eq.enqueue(ErrorInfo.INTERNAL_ERROR, e.message(), e.position());
108         }
109         catch (ErrorLimitError e2) {
110         }
111         eq.flush();
112         throw e;
113         }
114         catch (RuntimeException JavaDoc e) {
115         // Flush the error queue, then rethrow to get the stack trace.
116
eq.flush();
117         throw e;
118         }
119     }
120     catch (ErrorLimitError e) {
121     }
122
123     eq.flush();
124
125         for (Iterator i = allExtensions.iterator(); i.hasNext(); ) {
126             ExtensionInfo ext = (ExtensionInfo) i.next();
127             ext.getStats().report();
128         }
129
130     return okay;
131     }
132
133     /** Get the compiler's class file loader. */
134     public ClassFileLoader loader() {
135         return this.loader;
136     }
137
138     /** Should fully qualified class names be used in the output? */
139     public boolean useFullyQualifiedNames() {
140         return extensionInfo.getOptions().fully_qualified_names;
141     }
142
143     /** Return a list of all languages extensions active in the compiler. */
144     public void addExtension(ExtensionInfo ext) {
145         allExtensions.add(ext);
146     }
147
148     /** Return a list of all languages extensions active in the compiler. */
149     public List allExtensions() {
150         return allExtensions;
151     }
152
153     /** Get information about the language extension being compiled. */
154     public ExtensionInfo sourceExtension() {
155     return extensionInfo;
156     }
157
158     /** Maximum number of characters on each line of output */
159     public int outputWidth() {
160         return extensionInfo.getOptions().output_width;
161     }
162
163     /** Should class info be serialized into the output? */
164     public boolean serializeClassInfo() {
165     return extensionInfo.getOptions().serialize_type_info;
166     }
167
168     /** Get the compiler's error queue. */
169     public ErrorQueue errorQueue() {
170     return eq;
171     }
172
173     static {
174       // FIXME: if we get an io error (due to too many files open, for example)
175
// it will throw an exception. but, we won't be able to do anything with
176
// it since the exception handlers will want to load
177
// polyglot.util.CodeWriter and polyglot.util.ErrorInfo to print and
178
// enqueue the error; but the classes must be in memory since the io
179
// can't open any files; thus, we force the classloader to load the class
180
// file.
181
try {
182     ClassLoader JavaDoc loader = Compiler JavaDoc.class.getClassLoader();
183     // loader.loadClass("polyglot.util.CodeWriter");
184
// loader.loadClass("polyglot.util.ErrorInfo");
185
loader.loadClass("polyglot.util.StdErrorQueue");
186       }
187       catch (ClassNotFoundException JavaDoc e) {
188     throw new InternalCompilerError(e.getMessage());
189       }
190     }
191 }
192
Popular Tags