1 package polyglot.frontend; 2 3 import java.io.FileNotFoundException ; 4 import java.io.IOException ; 5 import java.util.*; 6 7 import polyglot.types.reflect.ClassFileLoader; 8 import polyglot.main.Report; 9 import polyglot.util.*; 10 11 16 public class Compiler 17 { 18 19 private ExtensionInfo extensionInfo; 20 21 22 private List allExtensions; 23 24 25 private ErrorQueue eq; 26 27 31 private ClassFileLoader loader; 32 33 37 private Collection outputFiles = new HashSet(); 38 39 44 public Compiler(ExtensionInfo extensionInfo) { 45 this(extensionInfo, new StdErrorQueue(System.err, 46 extensionInfo.getOptions().error_count, 47 extensionInfo.compilerName())); 48 } 49 50 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 extensionInfo.initCompiler(this); 64 } 65 66 67 public Collection outputFiles() { 68 return outputFiles; 69 } 70 71 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 sourceName = (String ) i.next(); 86 FileSource source = source_loader.fileSource(sourceName); 87 88 source.setUserSpecified(true); 91 92 sourceExtension().addJob(source); 93 } 94 95 okay = sourceExtension().runToCompletion(); 96 } 97 catch (FileNotFoundException e) { 98 eq.enqueue(ErrorInfo.IO_ERROR, 99 "Cannot find source file \"" + e.getMessage() + "\"."); 100 } 101 catch (IOException e) { 102 eq.enqueue(ErrorInfo.IO_ERROR, e.getMessage()); 103 } 104 catch (InternalCompilerError e) { 105 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 e) { 115 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 134 public ClassFileLoader loader() { 135 return this.loader; 136 } 137 138 139 public boolean useFullyQualifiedNames() { 140 return extensionInfo.getOptions().fully_qualified_names; 141 } 142 143 144 public void addExtension(ExtensionInfo ext) { 145 allExtensions.add(ext); 146 } 147 148 149 public List allExtensions() { 150 return allExtensions; 151 } 152 153 154 public ExtensionInfo sourceExtension() { 155 return extensionInfo; 156 } 157 158 159 public int outputWidth() { 160 return extensionInfo.getOptions().output_width; 161 } 162 163 164 public boolean serializeClassInfo() { 165 return extensionInfo.getOptions().serialize_type_info; 166 } 167 168 169 public ErrorQueue errorQueue() { 170 return eq; 171 } 172 173 static { 174 try { 182 ClassLoader loader = Compiler .class.getClassLoader(); 183 loader.loadClass("polyglot.util.StdErrorQueue"); 186 } 187 catch (ClassNotFoundException e) { 188 throw new InternalCompilerError(e.getMessage()); 189 } 190 } 191 } 192 | Popular Tags |