001 package AST; 002 003 import java.util.HashSet; 004 import java.io.File; 005 import java.util.*; 006 import beaver.*; 007 import java.util.ArrayList; 008 import java.util.zip.*; 009 import java.io.*; 010 import java.io.FileNotFoundException; 011 import java.util.Collection; 012 /** 013 * @ast class 014 * 015 */ 016 public class Frontend extends java.lang.Object { 017 018 protected Program program; 019 020 021 022 protected Frontend() { 023 program = new Program(); 024 program.state().reset(); 025 } 026 027 028 029 public boolean process(String[] args, BytecodeReader reader, JavaParser parser) { 030 program.initBytecodeReader(reader); 031 program.initJavaParser(parser); 032 033 initOptions(); 034 processArgs(args); 035 036 Collection files = program.options().files(); 037 038 if(program.options().hasOption("-version")) { 039 printVersion(); 040 return true; 041 } 042 if(program.options().hasOption("-help") || files.isEmpty()) { 043 printUsage(); 044 return true; 045 } 046 047 try { 048 for(Iterator iter = files.iterator(); iter.hasNext(); ) { 049 String name = (String)iter.next(); 050 if(!new File(name).exists()) 051 System.err.println("WARNING: file \"" + name + "\" does not exist"); 052 program.addSourceFile(name); 053 } 054 055 for(Iterator iter = program.compilationUnitIterator(); iter.hasNext(); ) { 056 CompilationUnit unit = (CompilationUnit)iter.next(); 057 if(unit.fromSource()) { 058 try { 059 Collection errors = unit.parseErrors(); 060 Collection warnings = new LinkedList(); 061 // compute static semantic errors when there are no parse errors 062 // or the recover from parse errors option is specified 063 if(errors.isEmpty() || program.options().hasOption("-recover")) 064 unit.errorCheck(errors, warnings); 065 if(!errors.isEmpty()) { 066 processErrors(errors, unit); 067 return false; 068 } 069 else { 070 if(!warnings.isEmpty()) 071 processWarnings(warnings, unit); 072 processNoErrors(unit); 073 } 074 } catch (Throwable t) { 075 System.err.println("Errors:"); 076 System.err.println("Fatal exception while processing " + 077 unit.pathName() + ":"); 078 t.printStackTrace(System.err); 079 return false; 080 } 081 } 082 } 083 } catch (Throwable t) { 084 System.err.println("Errors:"); 085 System.err.println("Fatal exception:"); 086 t.printStackTrace(System.err); 087 return false; 088 } 089 return true; 090 } 091 092 093 094 protected void initOptions() { 095 Options options = program.options(); 096 options.initOptions(); 097 options.addKeyOption("-version"); 098 options.addKeyOption("-print"); 099 options.addKeyOption("-g"); 100 options.addKeyOption("-g:none"); 101 options.addKeyOption("-g:lines,vars,source"); 102 options.addKeyOption("-nowarn"); 103 options.addKeyOption("-verbose"); 104 options.addKeyOption("-deprecation"); 105 options.addKeyValueOption("-classpath"); 106 options.addKeyValueOption("-cp"); 107 options.addKeyValueOption("-sourcepath"); 108 options.addKeyValueOption("-bootclasspath"); 109 options.addKeyValueOption("-extdirs"); 110 options.addKeyValueOption("-d"); 111 options.addKeyValueOption("-encoding"); 112 options.addKeyValueOption("-source"); 113 options.addKeyValueOption("-target"); 114 options.addKeyOption("-help"); 115 options.addKeyOption("-O"); 116 options.addKeyOption("-J-Xmx128M"); 117 options.addKeyOption("-recover"); 118 } 119 120 121 protected void processArgs(String[] args) { 122 program.options().addOptions(args); 123 } 124 125 126 127 protected void processErrors(Collection errors, CompilationUnit unit) { 128 System.err.println("Errors:"); 129 for(Iterator iter2 = errors.iterator(); iter2.hasNext(); ) { 130 System.err.println(iter2.next()); 131 } 132 } 133 134 135 protected void processWarnings(Collection warnings, CompilationUnit unit) { 136 System.err.println("Warnings:"); 137 for(Iterator iter2 = warnings.iterator(); iter2.hasNext(); ) { 138 System.err.println(iter2.next()); 139 } 140 } 141 142 143 protected void processNoErrors(CompilationUnit unit) { 144 } 145 146 147 148 protected void printUsage() { 149 printLongVersion(); 150 System.out.println( 151 "\n" + name() + "\n\n" + 152 "Usage: java " + name() + " <options> <source files>\n" + 153 " -verbose Output messages about what the compiler is doing\n" + 154 " -classpath <path> Specify where to find user class files\n" + 155 " -sourcepath <path> Specify where to find input source files\n" + 156 " -bootclasspath <path> Override location of bootstrap class files\n" + 157 " -extdirs <dirs> Override location of installed extensions\n" + 158 " -d <directory> Specify where to place generated class files\n" + 159 " -help Print a synopsis of standard options\n" + 160 " -version Print version information\n" 161 ); 162 } 163 164 165 166 protected void printLongVersion() { 167 System.out.println(name() + " " + url() + " Version " + version()); 168 } 169 170 171 172 protected void printVersion() { 173 System.out.println(name() + " " + version()); 174 } 175 176 177 178 protected String name() { 179 return "Java1.4Frontend"; 180 } 181 182 183 protected String url() { 184 return "(http://jastadd.cs.lth.se)"; 185 } 186 187 188 189 protected String version() { 190 return "R20070504"; 191 } 192 193 194 }