65 import com.sun.tools.javac.util.Log.WriterKind; |
65 import com.sun.tools.javac.util.Log.WriterKind; |
66 |
66 |
67 import static com.sun.tools.javac.code.TypeTag.CLASS; |
67 import static com.sun.tools.javac.code.TypeTag.CLASS; |
68 import static com.sun.tools.javac.main.Option.*; |
68 import static com.sun.tools.javac.main.Option.*; |
69 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*; |
69 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*; |
70 import static com.sun.tools.javac.util.ListBuffer.lb; |
|
71 |
70 |
72 |
71 |
73 /** This class could be the main entry point for GJC when GJC is used as a |
72 /** This class could be the main entry point for GJC when GJC is used as a |
74 * component in a larger software system. It provides operations to |
73 * component in a larger software system. It provides operations to |
75 * construct a new compiler, and to run a new compiler on a set of source |
74 * construct a new compiler, and to run a new compiler on a set of source |
584 } |
583 } |
585 return log.nerrors; |
584 return log.nerrors; |
586 } |
585 } |
587 |
586 |
588 protected final <T> Queue<T> stopIfError(CompileState cs, Queue<T> queue) { |
587 protected final <T> Queue<T> stopIfError(CompileState cs, Queue<T> queue) { |
589 return shouldStop(cs) ? ListBuffer.<T>lb() : queue; |
588 return shouldStop(cs) ? new ListBuffer<T>() : queue; |
590 } |
589 } |
591 |
590 |
592 protected final <T> List<T> stopIfError(CompileState cs, List<T> list) { |
591 protected final <T> List<T> stopIfError(CompileState cs, List<T> list) { |
593 return shouldStop(cs) ? List.<T>nil() : list; |
592 return shouldStop(cs) ? List.<T>nil() : list; |
594 } |
593 } |
950 public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) { |
949 public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) { |
951 if (shouldStop(CompileState.PARSE)) |
950 if (shouldStop(CompileState.PARSE)) |
952 return List.nil(); |
951 return List.nil(); |
953 |
952 |
954 //parse all files |
953 //parse all files |
955 ListBuffer<JCCompilationUnit> trees = lb(); |
954 ListBuffer<JCCompilationUnit> trees = new ListBuffer<>(); |
956 Set<JavaFileObject> filesSoFar = new HashSet<JavaFileObject>(); |
955 Set<JavaFileObject> filesSoFar = new HashSet<JavaFileObject>(); |
957 for (JavaFileObject fileObject : fileObjects) { |
956 for (JavaFileObject fileObject : fileObjects) { |
958 if (!filesSoFar.contains(fileObject)) { |
957 if (!filesSoFar.contains(fileObject)) { |
959 filesSoFar.add(fileObject); |
958 filesSoFar.add(fileObject); |
960 trees.append(parse(fileObject)); |
959 trees.append(parse(fileObject)); |
1000 |
999 |
1001 // If generating source, or if tracking public apis, |
1000 // If generating source, or if tracking public apis, |
1002 // then remember the classes declared in |
1001 // then remember the classes declared in |
1003 // the original compilation units listed on the command line. |
1002 // the original compilation units listed on the command line. |
1004 if (needRootClasses || sourceOutput || stubOutput) { |
1003 if (needRootClasses || sourceOutput || stubOutput) { |
1005 ListBuffer<JCClassDecl> cdefs = lb(); |
1004 ListBuffer<JCClassDecl> cdefs = new ListBuffer<>(); |
1006 for (JCCompilationUnit unit : roots) { |
1005 for (JCCompilationUnit unit : roots) { |
1007 for (List<JCTree> defs = unit.defs; |
1006 for (List<JCTree> defs = unit.defs; |
1008 defs.nonEmpty(); |
1007 defs.nonEmpty(); |
1009 defs = defs.tail) { |
1008 defs = defs.tail) { |
1010 if (defs.head instanceof JCClassDecl) |
1009 if (defs.head instanceof JCClassDecl) |
1224 * parsed and entered via the SourceCompleter. |
1223 * parsed and entered via the SourceCompleter. |
1225 * Attribution of the entries in the list does not stop if any errors occur. |
1224 * Attribution of the entries in the list does not stop if any errors occur. |
1226 * @returns a list of environments for attributd classes. |
1225 * @returns a list of environments for attributd classes. |
1227 */ |
1226 */ |
1228 public Queue<Env<AttrContext>> attribute(Queue<Env<AttrContext>> envs) { |
1227 public Queue<Env<AttrContext>> attribute(Queue<Env<AttrContext>> envs) { |
1229 ListBuffer<Env<AttrContext>> results = lb(); |
1228 ListBuffer<Env<AttrContext>> results = new ListBuffer<>(); |
1230 while (!envs.isEmpty()) |
1229 while (!envs.isEmpty()) |
1231 results.append(attribute(envs.remove())); |
1230 results.append(attribute(envs.remove())); |
1232 return stopIfError(CompileState.ATTR, results); |
1231 return stopIfError(CompileState.ATTR, results); |
1233 } |
1232 } |
1234 |
1233 |
1289 * These include checks for definite assignment and unreachable statements. |
1288 * These include checks for definite assignment and unreachable statements. |
1290 * If any errors occur, an empty list will be returned. |
1289 * If any errors occur, an empty list will be returned. |
1291 * @returns the list of attributed parse trees |
1290 * @returns the list of attributed parse trees |
1292 */ |
1291 */ |
1293 public Queue<Env<AttrContext>> flow(Queue<Env<AttrContext>> envs) { |
1292 public Queue<Env<AttrContext>> flow(Queue<Env<AttrContext>> envs) { |
1294 ListBuffer<Env<AttrContext>> results = lb(); |
1293 ListBuffer<Env<AttrContext>> results = new ListBuffer<>(); |
1295 for (Env<AttrContext> env: envs) { |
1294 for (Env<AttrContext> env: envs) { |
1296 flow(env, results); |
1295 flow(env, results); |
1297 } |
1296 } |
1298 return stopIfError(CompileState.FLOW, results); |
1297 return stopIfError(CompileState.FLOW, results); |
1299 } |
1298 } |
1300 |
1299 |
1301 /** |
1300 /** |
1302 * Perform dataflow checks on an attributed parse tree. |
1301 * Perform dataflow checks on an attributed parse tree. |
1303 */ |
1302 */ |
1304 public Queue<Env<AttrContext>> flow(Env<AttrContext> env) { |
1303 public Queue<Env<AttrContext>> flow(Env<AttrContext> env) { |
1305 ListBuffer<Env<AttrContext>> results = lb(); |
1304 ListBuffer<Env<AttrContext>> results = new ListBuffer<>(); |
1306 flow(env, results); |
1305 flow(env, results); |
1307 return stopIfError(CompileState.FLOW, results); |
1306 return stopIfError(CompileState.FLOW, results); |
1308 } |
1307 } |
1309 |
1308 |
1310 /** |
1309 /** |
1354 * for source or code generation. |
1353 * for source or code generation. |
1355 * If any errors occur, an empty list will be returned. |
1354 * If any errors occur, an empty list will be returned. |
1356 * @returns a list containing the classes to be generated |
1355 * @returns a list containing the classes to be generated |
1357 */ |
1356 */ |
1358 public Queue<Pair<Env<AttrContext>, JCClassDecl>> desugar(Queue<Env<AttrContext>> envs) { |
1357 public Queue<Pair<Env<AttrContext>, JCClassDecl>> desugar(Queue<Env<AttrContext>> envs) { |
1359 ListBuffer<Pair<Env<AttrContext>, JCClassDecl>> results = lb(); |
1358 ListBuffer<Pair<Env<AttrContext>, JCClassDecl>> results = new ListBuffer<>(); |
1360 for (Env<AttrContext> env: envs) |
1359 for (Env<AttrContext> env: envs) |
1361 desugar(env, results); |
1360 desugar(env, results); |
1362 return stopIfError(CompileState.FLOW, results); |
1361 return stopIfError(CompileState.FLOW, results); |
1363 } |
1362 } |
1364 |
1363 |
1603 tree.init = null; |
1602 tree.init = null; |
1604 super.visitVarDef(tree); |
1603 super.visitVarDef(tree); |
1605 } |
1604 } |
1606 @Override |
1605 @Override |
1607 public void visitClassDef(JCClassDecl tree) { |
1606 public void visitClassDef(JCClassDecl tree) { |
1608 ListBuffer<JCTree> newdefs = lb(); |
1607 ListBuffer<JCTree> newdefs = new ListBuffer<>(); |
1609 for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) { |
1608 for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) { |
1610 JCTree t = it.head; |
1609 JCTree t = it.head; |
1611 switch (t.getTag()) { |
1610 switch (t.getTag()) { |
1612 case CLASSDEF: |
1611 case CLASSDEF: |
1613 if (isInterface || |
1612 if (isInterface || |