Tue, 08 Nov 2011 11:51:05 -0800
6921494: provide way to print javac tree tag values
Reviewed-by: jjg, mcimadamore
Contributed-by: vicenterz@yahoo.es
duke@1 | 1 | /* |
jjg@816 | 2 | * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.main; |
duke@1 | 27 | |
duke@1 | 28 | import java.io.*; |
jjg@700 | 29 | import java.util.HashMap; |
duke@1 | 30 | import java.util.HashSet; |
mcimadamore@95 | 31 | import java.util.LinkedHashSet; |
duke@1 | 32 | import java.util.LinkedHashMap; |
duke@1 | 33 | import java.util.Map; |
duke@1 | 34 | import java.util.MissingResourceException; |
jjg@700 | 35 | import java.util.Queue; |
duke@1 | 36 | import java.util.ResourceBundle; |
duke@1 | 37 | import java.util.Set; |
duke@1 | 38 | import java.util.logging.Handler; |
duke@1 | 39 | import java.util.logging.Level; |
duke@1 | 40 | import java.util.logging.Logger; |
duke@1 | 41 | |
jjg@700 | 42 | import javax.annotation.processing.Processor; |
jjg@700 | 43 | import javax.lang.model.SourceVersion; |
duke@1 | 44 | import javax.tools.JavaFileManager; |
duke@1 | 45 | import javax.tools.JavaFileObject; |
duke@1 | 46 | import javax.tools.DiagnosticListener; |
duke@1 | 47 | |
duke@1 | 48 | import com.sun.source.util.TaskEvent; |
duke@1 | 49 | import com.sun.source.util.TaskListener; |
duke@1 | 50 | |
jjg@700 | 51 | import com.sun.tools.javac.file.JavacFileManager; |
duke@1 | 52 | import com.sun.tools.javac.util.*; |
duke@1 | 53 | import com.sun.tools.javac.code.*; |
jjg@757 | 54 | import com.sun.tools.javac.code.Lint.LintCategory; |
jjg@700 | 55 | import com.sun.tools.javac.code.Symbol.*; |
duke@1 | 56 | import com.sun.tools.javac.tree.*; |
jjg@700 | 57 | import com.sun.tools.javac.tree.JCTree.*; |
duke@1 | 58 | import com.sun.tools.javac.parser.*; |
duke@1 | 59 | import com.sun.tools.javac.comp.*; |
duke@1 | 60 | import com.sun.tools.javac.jvm.*; |
duke@1 | 61 | import com.sun.tools.javac.processing.*; |
duke@1 | 62 | |
duke@1 | 63 | import static javax.tools.StandardLocation.CLASS_OUTPUT; |
jjg@700 | 64 | import static com.sun.tools.javac.main.OptionName.*; |
jjg@664 | 65 | import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*; |
duke@1 | 66 | import static com.sun.tools.javac.util.ListBuffer.lb; |
duke@1 | 67 | |
duke@1 | 68 | |
duke@1 | 69 | /** This class could be the main entry point for GJC when GJC is used as a |
duke@1 | 70 | * component in a larger software system. It provides operations to |
duke@1 | 71 | * construct a new compiler, and to run a new compiler on a set of source |
duke@1 | 72 | * files. |
duke@1 | 73 | * |
jjg@581 | 74 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 75 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 76 | * This code and its internal interfaces are subject to change or |
duke@1 | 77 | * deletion without notice.</b> |
duke@1 | 78 | */ |
duke@1 | 79 | public class JavaCompiler implements ClassReader.SourceCompleter { |
duke@1 | 80 | /** The context key for the compiler. */ |
duke@1 | 81 | protected static final Context.Key<JavaCompiler> compilerKey = |
duke@1 | 82 | new Context.Key<JavaCompiler>(); |
duke@1 | 83 | |
duke@1 | 84 | /** Get the JavaCompiler instance for this context. */ |
duke@1 | 85 | public static JavaCompiler instance(Context context) { |
duke@1 | 86 | JavaCompiler instance = context.get(compilerKey); |
duke@1 | 87 | if (instance == null) |
duke@1 | 88 | instance = new JavaCompiler(context); |
duke@1 | 89 | return instance; |
duke@1 | 90 | } |
duke@1 | 91 | |
duke@1 | 92 | /** The current version number as a string. |
duke@1 | 93 | */ |
duke@1 | 94 | public static String version() { |
duke@1 | 95 | return version("release"); // mm.nn.oo[-milestone] |
duke@1 | 96 | } |
duke@1 | 97 | |
duke@1 | 98 | /** The current full version number as a string. |
duke@1 | 99 | */ |
duke@1 | 100 | public static String fullVersion() { |
duke@1 | 101 | return version("full"); // mm.mm.oo[-milestone]-build |
duke@1 | 102 | } |
duke@1 | 103 | |
duke@1 | 104 | private static final String versionRBName = "com.sun.tools.javac.resources.version"; |
duke@1 | 105 | private static ResourceBundle versionRB; |
duke@1 | 106 | |
duke@1 | 107 | private static String version(String key) { |
duke@1 | 108 | if (versionRB == null) { |
duke@1 | 109 | try { |
duke@1 | 110 | versionRB = ResourceBundle.getBundle(versionRBName); |
duke@1 | 111 | } catch (MissingResourceException e) { |
jjg@597 | 112 | return Log.getLocalizedString("version.not.available"); |
duke@1 | 113 | } |
duke@1 | 114 | } |
duke@1 | 115 | try { |
duke@1 | 116 | return versionRB.getString(key); |
duke@1 | 117 | } |
duke@1 | 118 | catch (MissingResourceException e) { |
jjg@597 | 119 | return Log.getLocalizedString("version.not.available"); |
duke@1 | 120 | } |
duke@1 | 121 | } |
duke@1 | 122 | |
jjg@119 | 123 | /** |
jjg@119 | 124 | * Control how the compiler's latter phases (attr, flow, desugar, generate) |
jjg@119 | 125 | * are connected. Each individual file is processed by each phase in turn, |
jjg@119 | 126 | * but with different compile policies, you can control the order in which |
jjg@119 | 127 | * each class is processed through its next phase. |
jjg@119 | 128 | * |
jjg@119 | 129 | * <p>Generally speaking, the compiler will "fail fast" in the face of |
jjg@119 | 130 | * errors, although not aggressively so. flow, desugar, etc become no-ops |
jjg@119 | 131 | * once any errors have occurred. No attempt is currently made to determine |
jjg@119 | 132 | * if it might be safe to process a class through its next phase because |
jjg@119 | 133 | * it does not depend on any unrelated errors that might have occurred. |
jjg@119 | 134 | */ |
jjg@119 | 135 | protected static enum CompilePolicy { |
jjg@119 | 136 | /** |
jjg@119 | 137 | * Just attribute the parse trees. |
duke@1 | 138 | */ |
duke@1 | 139 | ATTR_ONLY, |
duke@1 | 140 | |
jjg@119 | 141 | /** |
duke@1 | 142 | * Just attribute and do flow analysis on the parse trees. |
duke@1 | 143 | * This should catch most user errors. |
duke@1 | 144 | */ |
duke@1 | 145 | CHECK_ONLY, |
duke@1 | 146 | |
jjg@119 | 147 | /** |
duke@1 | 148 | * Attribute everything, then do flow analysis for everything, |
duke@1 | 149 | * then desugar everything, and only then generate output. |
jjg@119 | 150 | * This means no output will be generated if there are any |
jjg@119 | 151 | * errors in any classes. |
duke@1 | 152 | */ |
duke@1 | 153 | SIMPLE, |
duke@1 | 154 | |
jjg@119 | 155 | /** |
jjg@119 | 156 | * Groups the classes for each source file together, then process |
jjg@119 | 157 | * each group in a manner equivalent to the {@code SIMPLE} policy. |
jjg@119 | 158 | * This means no output will be generated if there are any |
jjg@119 | 159 | * errors in any of the classes in a source file. |
duke@1 | 160 | */ |
duke@1 | 161 | BY_FILE, |
duke@1 | 162 | |
jjg@119 | 163 | /** |
duke@1 | 164 | * Completely process each entry on the todo list in turn. |
duke@1 | 165 | * -- this is the same for 1.5. |
duke@1 | 166 | * Means output might be generated for some classes in a compilation unit |
duke@1 | 167 | * and not others. |
duke@1 | 168 | */ |
duke@1 | 169 | BY_TODO; |
duke@1 | 170 | |
duke@1 | 171 | static CompilePolicy decode(String option) { |
duke@1 | 172 | if (option == null) |
duke@1 | 173 | return DEFAULT_COMPILE_POLICY; |
duke@1 | 174 | else if (option.equals("attr")) |
duke@1 | 175 | return ATTR_ONLY; |
duke@1 | 176 | else if (option.equals("check")) |
duke@1 | 177 | return CHECK_ONLY; |
duke@1 | 178 | else if (option.equals("simple")) |
duke@1 | 179 | return SIMPLE; |
duke@1 | 180 | else if (option.equals("byfile")) |
duke@1 | 181 | return BY_FILE; |
duke@1 | 182 | else if (option.equals("bytodo")) |
duke@1 | 183 | return BY_TODO; |
duke@1 | 184 | else |
duke@1 | 185 | return DEFAULT_COMPILE_POLICY; |
duke@1 | 186 | } |
duke@1 | 187 | } |
duke@1 | 188 | |
duke@1 | 189 | private static CompilePolicy DEFAULT_COMPILE_POLICY = CompilePolicy.BY_TODO; |
duke@1 | 190 | |
jjg@119 | 191 | protected static enum ImplicitSourcePolicy { |
duke@1 | 192 | /** Don't generate or process implicitly read source files. */ |
duke@1 | 193 | NONE, |
duke@1 | 194 | /** Generate classes for implicitly read source files. */ |
duke@1 | 195 | CLASS, |
duke@1 | 196 | /** Like CLASS, but generate warnings if annotation processing occurs */ |
duke@1 | 197 | UNSET; |
duke@1 | 198 | |
duke@1 | 199 | static ImplicitSourcePolicy decode(String option) { |
duke@1 | 200 | if (option == null) |
duke@1 | 201 | return UNSET; |
duke@1 | 202 | else if (option.equals("none")) |
duke@1 | 203 | return NONE; |
duke@1 | 204 | else if (option.equals("class")) |
duke@1 | 205 | return CLASS; |
duke@1 | 206 | else |
duke@1 | 207 | return UNSET; |
duke@1 | 208 | } |
duke@1 | 209 | } |
duke@1 | 210 | |
duke@1 | 211 | /** The log to be used for error reporting. |
duke@1 | 212 | */ |
duke@1 | 213 | public Log log; |
duke@1 | 214 | |
jjg@12 | 215 | /** Factory for creating diagnostic objects |
jjg@12 | 216 | */ |
jjg@12 | 217 | JCDiagnostic.Factory diagFactory; |
jjg@12 | 218 | |
duke@1 | 219 | /** The tree factory module. |
duke@1 | 220 | */ |
duke@1 | 221 | protected TreeMaker make; |
duke@1 | 222 | |
duke@1 | 223 | /** The class reader. |
duke@1 | 224 | */ |
duke@1 | 225 | protected ClassReader reader; |
duke@1 | 226 | |
duke@1 | 227 | /** The class writer. |
duke@1 | 228 | */ |
duke@1 | 229 | protected ClassWriter writer; |
duke@1 | 230 | |
duke@1 | 231 | /** The module for the symbol table entry phases. |
duke@1 | 232 | */ |
duke@1 | 233 | protected Enter enter; |
duke@1 | 234 | |
duke@1 | 235 | /** The symbol table. |
duke@1 | 236 | */ |
duke@1 | 237 | protected Symtab syms; |
duke@1 | 238 | |
duke@1 | 239 | /** The language version. |
duke@1 | 240 | */ |
duke@1 | 241 | protected Source source; |
duke@1 | 242 | |
duke@1 | 243 | /** The module for code generation. |
duke@1 | 244 | */ |
duke@1 | 245 | protected Gen gen; |
duke@1 | 246 | |
duke@1 | 247 | /** The name table. |
duke@1 | 248 | */ |
jjg@113 | 249 | protected Names names; |
duke@1 | 250 | |
duke@1 | 251 | /** The attributor. |
duke@1 | 252 | */ |
duke@1 | 253 | protected Attr attr; |
duke@1 | 254 | |
duke@1 | 255 | /** The attributor. |
duke@1 | 256 | */ |
duke@1 | 257 | protected Check chk; |
duke@1 | 258 | |
duke@1 | 259 | /** The flow analyzer. |
duke@1 | 260 | */ |
duke@1 | 261 | protected Flow flow; |
duke@1 | 262 | |
duke@1 | 263 | /** The type eraser. |
duke@1 | 264 | */ |
jjg@119 | 265 | protected TransTypes transTypes; |
duke@1 | 266 | |
duke@1 | 267 | /** The syntactic sugar desweetener. |
duke@1 | 268 | */ |
jjg@119 | 269 | protected Lower lower; |
duke@1 | 270 | |
duke@1 | 271 | /** The annotation annotator. |
duke@1 | 272 | */ |
duke@1 | 273 | protected Annotate annotate; |
duke@1 | 274 | |
duke@1 | 275 | /** Force a completion failure on this name |
duke@1 | 276 | */ |
duke@1 | 277 | protected final Name completionFailureName; |
duke@1 | 278 | |
duke@1 | 279 | /** Type utilities. |
duke@1 | 280 | */ |
duke@1 | 281 | protected Types types; |
duke@1 | 282 | |
duke@1 | 283 | /** Access to file objects. |
duke@1 | 284 | */ |
duke@1 | 285 | protected JavaFileManager fileManager; |
duke@1 | 286 | |
duke@1 | 287 | /** Factory for parsers. |
duke@1 | 288 | */ |
jjg@111 | 289 | protected ParserFactory parserFactory; |
duke@1 | 290 | |
duke@1 | 291 | /** Optional listener for progress events |
duke@1 | 292 | */ |
duke@1 | 293 | protected TaskListener taskListener; |
duke@1 | 294 | |
duke@1 | 295 | /** |
duke@1 | 296 | * Annotation processing may require and provide a new instance |
duke@1 | 297 | * of the compiler to be used for the analyze and generate phases. |
duke@1 | 298 | */ |
duke@1 | 299 | protected JavaCompiler delegateCompiler; |
duke@1 | 300 | |
duke@1 | 301 | /** |
jjg@897 | 302 | * Command line options. |
jjg@897 | 303 | */ |
jjg@897 | 304 | protected Options options; |
jjg@897 | 305 | |
jjg@897 | 306 | protected Context context; |
jjg@897 | 307 | |
jjg@897 | 308 | /** |
duke@1 | 309 | * Flag set if any annotation processing occurred. |
duke@1 | 310 | **/ |
duke@1 | 311 | protected boolean annotationProcessingOccurred; |
duke@1 | 312 | |
duke@1 | 313 | /** |
duke@1 | 314 | * Flag set if any implicit source files read. |
duke@1 | 315 | **/ |
duke@1 | 316 | protected boolean implicitSourceFilesRead; |
duke@1 | 317 | |
duke@1 | 318 | /** Construct a new compiler using a shared context. |
duke@1 | 319 | */ |
jjg@893 | 320 | public JavaCompiler(Context context) { |
duke@1 | 321 | this.context = context; |
duke@1 | 322 | context.put(compilerKey, this); |
duke@1 | 323 | |
duke@1 | 324 | // if fileManager not already set, register the JavacFileManager to be used |
duke@1 | 325 | if (context.get(JavaFileManager.class) == null) |
duke@1 | 326 | JavacFileManager.preRegister(context); |
duke@1 | 327 | |
jjg@113 | 328 | names = Names.instance(context); |
duke@1 | 329 | log = Log.instance(context); |
jjg@12 | 330 | diagFactory = JCDiagnostic.Factory.instance(context); |
duke@1 | 331 | reader = ClassReader.instance(context); |
duke@1 | 332 | make = TreeMaker.instance(context); |
duke@1 | 333 | writer = ClassWriter.instance(context); |
duke@1 | 334 | enter = Enter.instance(context); |
duke@1 | 335 | todo = Todo.instance(context); |
duke@1 | 336 | |
duke@1 | 337 | fileManager = context.get(JavaFileManager.class); |
jjg@111 | 338 | parserFactory = ParserFactory.instance(context); |
duke@1 | 339 | |
duke@1 | 340 | try { |
duke@1 | 341 | // catch completion problems with predefineds |
duke@1 | 342 | syms = Symtab.instance(context); |
duke@1 | 343 | } catch (CompletionFailure ex) { |
duke@1 | 344 | // inlined Check.completionError as it is not initialized yet |
jjg@12 | 345 | log.error("cant.access", ex.sym, ex.getDetailValue()); |
duke@1 | 346 | if (ex instanceof ClassReader.BadClassFile) |
duke@1 | 347 | throw new Abort(); |
duke@1 | 348 | } |
duke@1 | 349 | source = Source.instance(context); |
duke@1 | 350 | attr = Attr.instance(context); |
duke@1 | 351 | chk = Check.instance(context); |
duke@1 | 352 | gen = Gen.instance(context); |
duke@1 | 353 | flow = Flow.instance(context); |
duke@1 | 354 | transTypes = TransTypes.instance(context); |
duke@1 | 355 | lower = Lower.instance(context); |
duke@1 | 356 | annotate = Annotate.instance(context); |
duke@1 | 357 | types = Types.instance(context); |
duke@1 | 358 | taskListener = context.get(TaskListener.class); |
duke@1 | 359 | |
duke@1 | 360 | reader.sourceCompleter = this; |
duke@1 | 361 | |
jjg@897 | 362 | options = Options.instance(context); |
duke@1 | 363 | |
jjg@700 | 364 | verbose = options.isSet(VERBOSE); |
jjg@700 | 365 | sourceOutput = options.isSet(PRINTSOURCE); // used to be -s |
jjg@700 | 366 | stubOutput = options.isSet("-stubs"); |
jjg@700 | 367 | relax = options.isSet("-relax"); |
jjg@700 | 368 | printFlat = options.isSet("-printflat"); |
jjg@700 | 369 | attrParseOnly = options.isSet("-attrparseonly"); |
jjg@700 | 370 | encoding = options.get(ENCODING); |
jjg@700 | 371 | lineDebugInfo = options.isUnset(G_CUSTOM) || |
jjg@700 | 372 | options.isSet(G_CUSTOM, "lines"); |
jjg@700 | 373 | genEndPos = options.isSet(XJCOV) || |
duke@1 | 374 | context.get(DiagnosticListener.class) != null; |
jjg@700 | 375 | devVerbose = options.isSet("dev"); |
jjg@700 | 376 | processPcks = options.isSet("process.packages"); |
jjg@700 | 377 | werror = options.isSet(WERROR); |
duke@1 | 378 | |
jjg@757 | 379 | if (source.compareTo(Source.DEFAULT) < 0) { |
jjg@757 | 380 | if (options.isUnset(XLINT_CUSTOM, "-" + LintCategory.OPTIONS.option)) { |
jjg@757 | 381 | if (fileManager instanceof BaseFileManager) { |
jjg@757 | 382 | if (((BaseFileManager) fileManager).isDefaultBootClassPath()) |
jjg@757 | 383 | log.warning(LintCategory.OPTIONS, "source.no.bootclasspath", source.name); |
jjg@757 | 384 | } |
jjg@757 | 385 | } |
jjg@757 | 386 | } |
jjg@757 | 387 | |
jjg@700 | 388 | verboseCompilePolicy = options.isSet("verboseCompilePolicy"); |
duke@1 | 389 | |
duke@1 | 390 | if (attrParseOnly) |
duke@1 | 391 | compilePolicy = CompilePolicy.ATTR_ONLY; |
duke@1 | 392 | else |
duke@1 | 393 | compilePolicy = CompilePolicy.decode(options.get("compilePolicy")); |
duke@1 | 394 | |
duke@1 | 395 | implicitSourcePolicy = ImplicitSourcePolicy.decode(options.get("-implicit")); |
duke@1 | 396 | |
duke@1 | 397 | completionFailureName = |
jjg@700 | 398 | options.isSet("failcomplete") |
duke@1 | 399 | ? names.fromString(options.get("failcomplete")) |
duke@1 | 400 | : null; |
jjg@257 | 401 | |
jjg@257 | 402 | shouldStopPolicy = |
jjg@700 | 403 | options.isSet("shouldStopPolicy") |
jjg@257 | 404 | ? CompileState.valueOf(options.get("shouldStopPolicy")) |
jjg@257 | 405 | : null; |
jjg@700 | 406 | if (options.isUnset("oldDiags")) |
mcimadamore@288 | 407 | log.setDiagnosticFormatter(RichDiagnosticFormatter.instance(context)); |
duke@1 | 408 | } |
duke@1 | 409 | |
duke@1 | 410 | /* Switches: |
duke@1 | 411 | */ |
duke@1 | 412 | |
duke@1 | 413 | /** Verbose output. |
duke@1 | 414 | */ |
duke@1 | 415 | public boolean verbose; |
duke@1 | 416 | |
duke@1 | 417 | /** Emit plain Java source files rather than class files. |
duke@1 | 418 | */ |
duke@1 | 419 | public boolean sourceOutput; |
duke@1 | 420 | |
duke@1 | 421 | /** Emit stub source files rather than class files. |
duke@1 | 422 | */ |
duke@1 | 423 | public boolean stubOutput; |
duke@1 | 424 | |
duke@1 | 425 | /** Generate attributed parse tree only. |
duke@1 | 426 | */ |
duke@1 | 427 | public boolean attrParseOnly; |
duke@1 | 428 | |
duke@1 | 429 | /** Switch: relax some constraints for producing the jsr14 prototype. |
duke@1 | 430 | */ |
duke@1 | 431 | boolean relax; |
duke@1 | 432 | |
duke@1 | 433 | /** Debug switch: Emit Java sources after inner class flattening. |
duke@1 | 434 | */ |
duke@1 | 435 | public boolean printFlat; |
duke@1 | 436 | |
duke@1 | 437 | /** The encoding to be used for source input. |
duke@1 | 438 | */ |
duke@1 | 439 | public String encoding; |
duke@1 | 440 | |
duke@1 | 441 | /** Generate code with the LineNumberTable attribute for debugging |
duke@1 | 442 | */ |
duke@1 | 443 | public boolean lineDebugInfo; |
duke@1 | 444 | |
duke@1 | 445 | /** Switch: should we store the ending positions? |
duke@1 | 446 | */ |
duke@1 | 447 | public boolean genEndPos; |
duke@1 | 448 | |
duke@1 | 449 | /** Switch: should we debug ignored exceptions |
duke@1 | 450 | */ |
duke@1 | 451 | protected boolean devVerbose; |
duke@1 | 452 | |
duke@1 | 453 | /** Switch: should we (annotation) process packages as well |
duke@1 | 454 | */ |
duke@1 | 455 | protected boolean processPcks; |
duke@1 | 456 | |
jjg@215 | 457 | /** Switch: treat warnings as errors |
jjg@215 | 458 | */ |
jjg@215 | 459 | protected boolean werror; |
jjg@215 | 460 | |
duke@1 | 461 | /** Switch: is annotation processing requested explitly via |
duke@1 | 462 | * CompilationTask.setProcessors? |
duke@1 | 463 | */ |
duke@1 | 464 | protected boolean explicitAnnotationProcessingRequested = false; |
duke@1 | 465 | |
duke@1 | 466 | /** |
duke@1 | 467 | * The policy for the order in which to perform the compilation |
duke@1 | 468 | */ |
duke@1 | 469 | protected CompilePolicy compilePolicy; |
duke@1 | 470 | |
duke@1 | 471 | /** |
duke@1 | 472 | * The policy for what to do with implicitly read source files |
duke@1 | 473 | */ |
duke@1 | 474 | protected ImplicitSourcePolicy implicitSourcePolicy; |
duke@1 | 475 | |
duke@1 | 476 | /** |
duke@1 | 477 | * Report activity related to compilePolicy |
duke@1 | 478 | */ |
duke@1 | 479 | public boolean verboseCompilePolicy; |
duke@1 | 480 | |
jjg@257 | 481 | /** |
jjg@257 | 482 | * Policy of how far to continue processing. null means until first |
jjg@257 | 483 | * error. |
jjg@257 | 484 | */ |
jjg@257 | 485 | public CompileState shouldStopPolicy; |
jjg@257 | 486 | |
duke@1 | 487 | /** A queue of all as yet unattributed classes. |
duke@1 | 488 | */ |
duke@1 | 489 | public Todo todo; |
duke@1 | 490 | |
jjg@1096 | 491 | /** A list of items to be closed when the compilation is complete. |
jjg@1096 | 492 | */ |
jjg@1096 | 493 | public List<Closeable> closeables = List.nil(); |
jjg@1096 | 494 | |
jjg@257 | 495 | /** Ordered list of compiler phases for each compilation unit. */ |
jjg@310 | 496 | public enum CompileState { |
jjg@257 | 497 | PARSE(1), |
jjg@257 | 498 | ENTER(2), |
jjg@257 | 499 | PROCESS(3), |
jjg@257 | 500 | ATTR(4), |
jjg@257 | 501 | FLOW(5), |
jjg@257 | 502 | TRANSTYPES(6), |
jjg@257 | 503 | LOWER(7), |
jjg@257 | 504 | GENERATE(8); |
jjg@77 | 505 | CompileState(int value) { |
jjg@77 | 506 | this.value = value; |
jjg@77 | 507 | } |
jjg@77 | 508 | boolean isDone(CompileState other) { |
jjg@77 | 509 | return value >= other.value; |
jjg@77 | 510 | } |
jjg@77 | 511 | private int value; |
jjg@77 | 512 | }; |
jjg@257 | 513 | /** Partial map to record which compiler phases have been executed |
jjg@257 | 514 | * for each compilation unit. Used for ATTR and FLOW phases. |
jjg@257 | 515 | */ |
jjg@77 | 516 | protected class CompileStates extends HashMap<Env<AttrContext>,CompileState> { |
jjg@198 | 517 | private static final long serialVersionUID = 1812267524140424433L; |
jjg@77 | 518 | boolean isDone(Env<AttrContext> env, CompileState cs) { |
jjg@77 | 519 | CompileState ecs = get(env); |
jjg@77 | 520 | return ecs != null && ecs.isDone(cs); |
jjg@77 | 521 | } |
jjg@77 | 522 | } |
jjg@77 | 523 | private CompileStates compileStates = new CompileStates(); |
duke@1 | 524 | |
duke@1 | 525 | /** The set of currently compiled inputfiles, needed to ensure |
duke@1 | 526 | * we don't accidentally overwrite an input file when -s is set. |
duke@1 | 527 | * initialized by `compile'. |
duke@1 | 528 | */ |
duke@1 | 529 | protected Set<JavaFileObject> inputFiles = new HashSet<JavaFileObject>(); |
duke@1 | 530 | |
jjg@257 | 531 | protected boolean shouldStop(CompileState cs) { |
jjg@257 | 532 | if (shouldStopPolicy == null) |
jjg@711 | 533 | return (errorCount() > 0 || unrecoverableError()); |
jjg@257 | 534 | else |
jjg@257 | 535 | return cs.ordinal() > shouldStopPolicy.ordinal(); |
jjg@257 | 536 | } |
jjg@257 | 537 | |
duke@1 | 538 | /** The number of errors reported so far. |
duke@1 | 539 | */ |
duke@1 | 540 | public int errorCount() { |
duke@1 | 541 | if (delegateCompiler != null && delegateCompiler != this) |
duke@1 | 542 | return delegateCompiler.errorCount(); |
jjg@215 | 543 | else { |
jjg@215 | 544 | if (werror && log.nerrors == 0 && log.nwarnings > 0) { |
jjg@215 | 545 | log.error("warnings.and.werror"); |
jjg@215 | 546 | } |
jjg@215 | 547 | } |
jjg@642 | 548 | return log.nerrors; |
duke@1 | 549 | } |
duke@1 | 550 | |
jjg@257 | 551 | protected final <T> Queue<T> stopIfError(CompileState cs, Queue<T> queue) { |
jjg@257 | 552 | return shouldStop(cs) ? ListBuffer.<T>lb() : queue; |
duke@1 | 553 | } |
duke@1 | 554 | |
jjg@257 | 555 | protected final <T> List<T> stopIfError(CompileState cs, List<T> list) { |
jjg@257 | 556 | return shouldStop(cs) ? List.<T>nil() : list; |
duke@1 | 557 | } |
duke@1 | 558 | |
duke@1 | 559 | /** The number of warnings reported so far. |
duke@1 | 560 | */ |
duke@1 | 561 | public int warningCount() { |
duke@1 | 562 | if (delegateCompiler != null && delegateCompiler != this) |
duke@1 | 563 | return delegateCompiler.warningCount(); |
duke@1 | 564 | else |
duke@1 | 565 | return log.nwarnings; |
duke@1 | 566 | } |
duke@1 | 567 | |
duke@1 | 568 | /** Try to open input stream with given name. |
duke@1 | 569 | * Report an error if this fails. |
duke@1 | 570 | * @param filename The file name of the input stream to be opened. |
duke@1 | 571 | */ |
duke@1 | 572 | public CharSequence readSource(JavaFileObject filename) { |
duke@1 | 573 | try { |
duke@1 | 574 | inputFiles.add(filename); |
duke@1 | 575 | return filename.getCharContent(false); |
duke@1 | 576 | } catch (IOException e) { |
jjg@510 | 577 | log.error("error.reading.file", filename, JavacFileManager.getMessage(e)); |
duke@1 | 578 | return null; |
duke@1 | 579 | } |
duke@1 | 580 | } |
duke@1 | 581 | |
duke@1 | 582 | /** Parse contents of input stream. |
duke@1 | 583 | * @param filename The name of the file from which input stream comes. |
duke@1 | 584 | * @param input The input stream to be parsed. |
duke@1 | 585 | */ |
duke@1 | 586 | protected JCCompilationUnit parse(JavaFileObject filename, CharSequence content) { |
duke@1 | 587 | long msec = now(); |
duke@1 | 588 | JCCompilationUnit tree = make.TopLevel(List.<JCTree.JCAnnotation>nil(), |
duke@1 | 589 | null, List.<JCTree>nil()); |
duke@1 | 590 | if (content != null) { |
duke@1 | 591 | if (verbose) { |
jjg@909 | 592 | log.printVerbose("parsing.started", filename); |
duke@1 | 593 | } |
duke@1 | 594 | if (taskListener != null) { |
duke@1 | 595 | TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, filename); |
duke@1 | 596 | taskListener.started(e); |
duke@1 | 597 | } |
jjg@111 | 598 | Parser parser = parserFactory.newParser(content, keepComments(), genEndPos, lineDebugInfo); |
jjg@111 | 599 | tree = parser.parseCompilationUnit(); |
duke@1 | 600 | if (verbose) { |
jjg@909 | 601 | log.printVerbose("parsing.done", Long.toString(elapsed(msec))); |
duke@1 | 602 | } |
duke@1 | 603 | } |
duke@1 | 604 | |
duke@1 | 605 | tree.sourcefile = filename; |
duke@1 | 606 | |
duke@1 | 607 | if (content != null && taskListener != null) { |
duke@1 | 608 | TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, tree); |
duke@1 | 609 | taskListener.finished(e); |
duke@1 | 610 | } |
duke@1 | 611 | |
duke@1 | 612 | return tree; |
duke@1 | 613 | } |
duke@1 | 614 | // where |
duke@1 | 615 | public boolean keepComments = false; |
duke@1 | 616 | protected boolean keepComments() { |
duke@1 | 617 | return keepComments || sourceOutput || stubOutput; |
duke@1 | 618 | } |
duke@1 | 619 | |
duke@1 | 620 | |
duke@1 | 621 | /** Parse contents of file. |
duke@1 | 622 | * @param filename The name of the file to be parsed. |
duke@1 | 623 | */ |
duke@1 | 624 | @Deprecated |
jjg@663 | 625 | public JCTree.JCCompilationUnit parse(String filename) { |
duke@1 | 626 | JavacFileManager fm = (JavacFileManager)fileManager; |
duke@1 | 627 | return parse(fm.getJavaFileObjectsFromStrings(List.of(filename)).iterator().next()); |
duke@1 | 628 | } |
duke@1 | 629 | |
duke@1 | 630 | /** Parse contents of file. |
duke@1 | 631 | * @param filename The name of the file to be parsed. |
duke@1 | 632 | */ |
duke@1 | 633 | public JCTree.JCCompilationUnit parse(JavaFileObject filename) { |
duke@1 | 634 | JavaFileObject prev = log.useSource(filename); |
duke@1 | 635 | try { |
duke@1 | 636 | JCTree.JCCompilationUnit t = parse(filename, readSource(filename)); |
duke@1 | 637 | if (t.endPositions != null) |
duke@1 | 638 | log.setEndPosTable(filename, t.endPositions); |
duke@1 | 639 | return t; |
duke@1 | 640 | } finally { |
duke@1 | 641 | log.useSource(prev); |
duke@1 | 642 | } |
duke@1 | 643 | } |
duke@1 | 644 | |
jjg@937 | 645 | /** Resolve an identifier which may be the binary name of a class or |
jjg@937 | 646 | * the Java name of a class or package. |
jjg@937 | 647 | * @param name The name to resolve |
jjg@937 | 648 | */ |
jjg@937 | 649 | public Symbol resolveBinaryNameOrIdent(String name) { |
jjg@937 | 650 | try { |
jjg@937 | 651 | Name flatname = names.fromString(name.replace("/", ".")); |
jjg@937 | 652 | return reader.loadClass(flatname); |
jjg@937 | 653 | } catch (CompletionFailure ignore) { |
jjg@937 | 654 | return resolveIdent(name); |
jjg@937 | 655 | } |
jjg@937 | 656 | } |
jjg@937 | 657 | |
duke@1 | 658 | /** Resolve an identifier. |
duke@1 | 659 | * @param name The identifier to resolve |
duke@1 | 660 | */ |
duke@1 | 661 | public Symbol resolveIdent(String name) { |
duke@1 | 662 | if (name.equals("")) |
duke@1 | 663 | return syms.errSymbol; |
duke@1 | 664 | JavaFileObject prev = log.useSource(null); |
duke@1 | 665 | try { |
duke@1 | 666 | JCExpression tree = null; |
duke@1 | 667 | for (String s : name.split("\\.", -1)) { |
duke@1 | 668 | if (!SourceVersion.isIdentifier(s)) // TODO: check for keywords |
duke@1 | 669 | return syms.errSymbol; |
duke@1 | 670 | tree = (tree == null) ? make.Ident(names.fromString(s)) |
duke@1 | 671 | : make.Select(tree, names.fromString(s)); |
duke@1 | 672 | } |
duke@1 | 673 | JCCompilationUnit toplevel = |
duke@1 | 674 | make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil()); |
duke@1 | 675 | toplevel.packge = syms.unnamedPackage; |
duke@1 | 676 | return attr.attribIdent(tree, toplevel); |
duke@1 | 677 | } finally { |
duke@1 | 678 | log.useSource(prev); |
duke@1 | 679 | } |
duke@1 | 680 | } |
duke@1 | 681 | |
duke@1 | 682 | /** Emit plain Java source for a class. |
duke@1 | 683 | * @param env The attribution environment of the outermost class |
duke@1 | 684 | * containing this class. |
duke@1 | 685 | * @param cdef The class definition to be printed. |
duke@1 | 686 | */ |
duke@1 | 687 | JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException { |
duke@1 | 688 | JavaFileObject outFile |
duke@1 | 689 | = fileManager.getJavaFileForOutput(CLASS_OUTPUT, |
duke@1 | 690 | cdef.sym.flatname.toString(), |
duke@1 | 691 | JavaFileObject.Kind.SOURCE, |
duke@1 | 692 | null); |
duke@1 | 693 | if (inputFiles.contains(outFile)) { |
duke@1 | 694 | log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile); |
duke@1 | 695 | return null; |
duke@1 | 696 | } else { |
duke@1 | 697 | BufferedWriter out = new BufferedWriter(outFile.openWriter()); |
duke@1 | 698 | try { |
duke@1 | 699 | new Pretty(out, true).printUnit(env.toplevel, cdef); |
duke@1 | 700 | if (verbose) |
jjg@909 | 701 | log.printVerbose("wrote.file", outFile); |
duke@1 | 702 | } finally { |
duke@1 | 703 | out.close(); |
duke@1 | 704 | } |
duke@1 | 705 | return outFile; |
duke@1 | 706 | } |
duke@1 | 707 | } |
duke@1 | 708 | |
duke@1 | 709 | /** Generate code and emit a class file for a given class |
duke@1 | 710 | * @param env The attribution environment of the outermost class |
duke@1 | 711 | * containing this class. |
duke@1 | 712 | * @param cdef The class definition from which code is generated. |
duke@1 | 713 | */ |
duke@1 | 714 | JavaFileObject genCode(Env<AttrContext> env, JCClassDecl cdef) throws IOException { |
duke@1 | 715 | try { |
jjg@257 | 716 | if (gen.genClass(env, cdef) && (errorCount() == 0)) |
duke@1 | 717 | return writer.writeClass(cdef.sym); |
duke@1 | 718 | } catch (ClassWriter.PoolOverflow ex) { |
duke@1 | 719 | log.error(cdef.pos(), "limit.pool"); |
duke@1 | 720 | } catch (ClassWriter.StringOverflow ex) { |
duke@1 | 721 | log.error(cdef.pos(), "limit.string.overflow", |
duke@1 | 722 | ex.value.substring(0, 20)); |
duke@1 | 723 | } catch (CompletionFailure ex) { |
duke@1 | 724 | chk.completionError(cdef.pos(), ex); |
duke@1 | 725 | } |
duke@1 | 726 | return null; |
duke@1 | 727 | } |
duke@1 | 728 | |
duke@1 | 729 | /** Complete compiling a source file that has been accessed |
duke@1 | 730 | * by the class file reader. |
duke@1 | 731 | * @param c The class the source file of which needs to be compiled. |
duke@1 | 732 | * @param filename The name of the source file. |
duke@1 | 733 | * @param f An input stream that reads the source file. |
duke@1 | 734 | */ |
duke@1 | 735 | public void complete(ClassSymbol c) throws CompletionFailure { |
duke@1 | 736 | // System.err.println("completing " + c);//DEBUG |
duke@1 | 737 | if (completionFailureName == c.fullname) { |
duke@1 | 738 | throw new CompletionFailure(c, "user-selected completion failure by class name"); |
duke@1 | 739 | } |
duke@1 | 740 | JCCompilationUnit tree; |
duke@1 | 741 | JavaFileObject filename = c.classfile; |
duke@1 | 742 | JavaFileObject prev = log.useSource(filename); |
duke@1 | 743 | |
duke@1 | 744 | try { |
duke@1 | 745 | tree = parse(filename, filename.getCharContent(false)); |
duke@1 | 746 | } catch (IOException e) { |
jjg@510 | 747 | log.error("error.reading.file", filename, JavacFileManager.getMessage(e)); |
duke@1 | 748 | tree = make.TopLevel(List.<JCTree.JCAnnotation>nil(), null, List.<JCTree>nil()); |
duke@1 | 749 | } finally { |
duke@1 | 750 | log.useSource(prev); |
duke@1 | 751 | } |
duke@1 | 752 | |
duke@1 | 753 | if (taskListener != null) { |
duke@1 | 754 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree); |
duke@1 | 755 | taskListener.started(e); |
duke@1 | 756 | } |
duke@1 | 757 | |
duke@1 | 758 | enter.complete(List.of(tree), c); |
duke@1 | 759 | |
duke@1 | 760 | if (taskListener != null) { |
duke@1 | 761 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree); |
duke@1 | 762 | taskListener.finished(e); |
duke@1 | 763 | } |
duke@1 | 764 | |
duke@1 | 765 | if (enter.getEnv(c) == null) { |
duke@1 | 766 | boolean isPkgInfo = |
duke@1 | 767 | tree.sourcefile.isNameCompatible("package-info", |
duke@1 | 768 | JavaFileObject.Kind.SOURCE); |
duke@1 | 769 | if (isPkgInfo) { |
duke@1 | 770 | if (enter.getEnv(tree.packge) == null) { |
jjg@12 | 771 | JCDiagnostic diag = |
jjg@12 | 772 | diagFactory.fragment("file.does.not.contain.package", |
duke@1 | 773 | c.location()); |
jjg@12 | 774 | throw reader.new BadClassFile(c, filename, diag); |
duke@1 | 775 | } |
duke@1 | 776 | } else { |
jjg@12 | 777 | JCDiagnostic diag = |
jjg@12 | 778 | diagFactory.fragment("file.doesnt.contain.class", |
jjg@12 | 779 | c.getQualifiedName()); |
jjg@12 | 780 | throw reader.new BadClassFile(c, filename, diag); |
duke@1 | 781 | } |
duke@1 | 782 | } |
duke@1 | 783 | |
duke@1 | 784 | implicitSourceFilesRead = true; |
duke@1 | 785 | } |
duke@1 | 786 | |
duke@1 | 787 | /** Track when the JavaCompiler has been used to compile something. */ |
duke@1 | 788 | private boolean hasBeenUsed = false; |
duke@1 | 789 | private long start_msec = 0; |
duke@1 | 790 | public long elapsed_msec = 0; |
duke@1 | 791 | |
duke@1 | 792 | public void compile(List<JavaFileObject> sourceFileObject) |
duke@1 | 793 | throws Throwable { |
duke@1 | 794 | compile(sourceFileObject, List.<String>nil(), null); |
duke@1 | 795 | } |
duke@1 | 796 | |
duke@1 | 797 | /** |
duke@1 | 798 | * Main method: compile a list of files, return all compiled classes |
duke@1 | 799 | * |
duke@1 | 800 | * @param sourceFileObjects file objects to be compiled |
duke@1 | 801 | * @param classnames class names to process for annotations |
duke@1 | 802 | * @param processors user provided annotation processors to bypass |
duke@1 | 803 | * discovery, {@code null} means that no processors were provided |
duke@1 | 804 | */ |
duke@1 | 805 | public void compile(List<JavaFileObject> sourceFileObjects, |
duke@1 | 806 | List<String> classnames, |
duke@1 | 807 | Iterable<? extends Processor> processors) |
duke@1 | 808 | { |
duke@1 | 809 | if (processors != null && processors.iterator().hasNext()) |
duke@1 | 810 | explicitAnnotationProcessingRequested = true; |
duke@1 | 811 | // as a JavaCompiler can only be used once, throw an exception if |
duke@1 | 812 | // it has been used before. |
duke@1 | 813 | if (hasBeenUsed) |
duke@1 | 814 | throw new AssertionError("attempt to reuse JavaCompiler"); |
duke@1 | 815 | hasBeenUsed = true; |
duke@1 | 816 | |
jjg@897 | 817 | // forcibly set the equivalent of -Xlint:-options, so that no further |
jjg@897 | 818 | // warnings about command line options are generated from this point on |
jjg@897 | 819 | options.put(XLINT_CUSTOM + "-" + LintCategory.OPTIONS.option, "true"); |
jjg@897 | 820 | options.remove(XLINT_CUSTOM + LintCategory.OPTIONS.option); |
jjg@897 | 821 | |
duke@1 | 822 | start_msec = now(); |
jjg@757 | 823 | |
duke@1 | 824 | try { |
duke@1 | 825 | initProcessAnnotations(processors); |
duke@1 | 826 | |
duke@1 | 827 | // These method calls must be chained to avoid memory leaks |
jjg@257 | 828 | delegateCompiler = |
jjg@257 | 829 | processAnnotations( |
jjg@257 | 830 | enterTrees(stopIfError(CompileState.PARSE, parseFiles(sourceFileObjects))), |
jjg@257 | 831 | classnames); |
duke@1 | 832 | |
duke@1 | 833 | delegateCompiler.compile2(); |
duke@1 | 834 | delegateCompiler.close(); |
duke@1 | 835 | elapsed_msec = delegateCompiler.elapsed_msec; |
duke@1 | 836 | } catch (Abort ex) { |
duke@1 | 837 | if (devVerbose) |
jjg@757 | 838 | ex.printStackTrace(System.err); |
jjg@372 | 839 | } finally { |
jjg@372 | 840 | if (procEnvImpl != null) |
jjg@372 | 841 | procEnvImpl.close(); |
duke@1 | 842 | } |
duke@1 | 843 | } |
duke@1 | 844 | |
duke@1 | 845 | /** |
duke@1 | 846 | * The phases following annotation processing: attribution, |
duke@1 | 847 | * desugar, and finally code generation. |
duke@1 | 848 | */ |
duke@1 | 849 | private void compile2() { |
duke@1 | 850 | try { |
duke@1 | 851 | switch (compilePolicy) { |
duke@1 | 852 | case ATTR_ONLY: |
duke@1 | 853 | attribute(todo); |
duke@1 | 854 | break; |
duke@1 | 855 | |
duke@1 | 856 | case CHECK_ONLY: |
duke@1 | 857 | flow(attribute(todo)); |
duke@1 | 858 | break; |
duke@1 | 859 | |
duke@1 | 860 | case SIMPLE: |
duke@1 | 861 | generate(desugar(flow(attribute(todo)))); |
duke@1 | 862 | break; |
duke@1 | 863 | |
jjg@119 | 864 | case BY_FILE: { |
jjg@119 | 865 | Queue<Queue<Env<AttrContext>>> q = todo.groupByFile(); |
jjg@257 | 866 | while (!q.isEmpty() && !shouldStop(CompileState.ATTR)) { |
jjg@119 | 867 | generate(desugar(flow(attribute(q.remove())))); |
jjg@119 | 868 | } |
jjg@119 | 869 | } |
duke@1 | 870 | break; |
duke@1 | 871 | |
duke@1 | 872 | case BY_TODO: |
jjg@119 | 873 | while (!todo.isEmpty()) |
jjg@119 | 874 | generate(desugar(flow(attribute(todo.remove())))); |
duke@1 | 875 | break; |
duke@1 | 876 | |
duke@1 | 877 | default: |
jjg@816 | 878 | Assert.error("unknown compile policy"); |
duke@1 | 879 | } |
duke@1 | 880 | } catch (Abort ex) { |
duke@1 | 881 | if (devVerbose) |
jjg@757 | 882 | ex.printStackTrace(System.err); |
duke@1 | 883 | } |
duke@1 | 884 | |
duke@1 | 885 | if (verbose) { |
jjg@70 | 886 | elapsed_msec = elapsed(start_msec); |
jjg@909 | 887 | log.printVerbose("total", Long.toString(elapsed_msec)); |
duke@1 | 888 | } |
duke@1 | 889 | |
duke@1 | 890 | reportDeferredDiagnostics(); |
duke@1 | 891 | |
duke@1 | 892 | if (!log.hasDiagnosticListener()) { |
duke@1 | 893 | printCount("error", errorCount()); |
duke@1 | 894 | printCount("warn", warningCount()); |
duke@1 | 895 | } |
duke@1 | 896 | } |
duke@1 | 897 | |
duke@1 | 898 | private List<JCClassDecl> rootClasses; |
duke@1 | 899 | |
duke@1 | 900 | /** |
duke@1 | 901 | * Parses a list of files. |
duke@1 | 902 | */ |
jjg@663 | 903 | public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) { |
jjg@257 | 904 | if (shouldStop(CompileState.PARSE)) |
duke@1 | 905 | return List.nil(); |
duke@1 | 906 | |
duke@1 | 907 | //parse all files |
duke@1 | 908 | ListBuffer<JCCompilationUnit> trees = lb(); |
sundar@678 | 909 | Set<JavaFileObject> filesSoFar = new HashSet<JavaFileObject>(); |
sundar@678 | 910 | for (JavaFileObject fileObject : fileObjects) { |
sundar@678 | 911 | if (!filesSoFar.contains(fileObject)) { |
sundar@678 | 912 | filesSoFar.add(fileObject); |
sundar@678 | 913 | trees.append(parse(fileObject)); |
sundar@678 | 914 | } |
sundar@678 | 915 | } |
duke@1 | 916 | return trees.toList(); |
duke@1 | 917 | } |
duke@1 | 918 | |
duke@1 | 919 | /** |
duke@1 | 920 | * Enter the symbols found in a list of parse trees. |
duke@1 | 921 | * As a side-effect, this puts elements on the "todo" list. |
duke@1 | 922 | * Also stores a list of all top level classes in rootClasses. |
duke@1 | 923 | */ |
duke@1 | 924 | public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) { |
duke@1 | 925 | //enter symbols for all files |
duke@1 | 926 | if (taskListener != null) { |
duke@1 | 927 | for (JCCompilationUnit unit: roots) { |
duke@1 | 928 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit); |
duke@1 | 929 | taskListener.started(e); |
duke@1 | 930 | } |
duke@1 | 931 | } |
duke@1 | 932 | |
duke@1 | 933 | enter.main(roots); |
duke@1 | 934 | |
duke@1 | 935 | if (taskListener != null) { |
duke@1 | 936 | for (JCCompilationUnit unit: roots) { |
duke@1 | 937 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit); |
duke@1 | 938 | taskListener.finished(e); |
duke@1 | 939 | } |
duke@1 | 940 | } |
duke@1 | 941 | |
duke@1 | 942 | //If generating source, remember the classes declared in |
duke@1 | 943 | //the original compilation units listed on the command line. |
duke@1 | 944 | if (sourceOutput || stubOutput) { |
duke@1 | 945 | ListBuffer<JCClassDecl> cdefs = lb(); |
duke@1 | 946 | for (JCCompilationUnit unit : roots) { |
duke@1 | 947 | for (List<JCTree> defs = unit.defs; |
duke@1 | 948 | defs.nonEmpty(); |
duke@1 | 949 | defs = defs.tail) { |
duke@1 | 950 | if (defs.head instanceof JCClassDecl) |
duke@1 | 951 | cdefs.append((JCClassDecl)defs.head); |
duke@1 | 952 | } |
duke@1 | 953 | } |
duke@1 | 954 | rootClasses = cdefs.toList(); |
duke@1 | 955 | } |
jjg@654 | 956 | |
jjg@654 | 957 | // Ensure the input files have been recorded. Although this is normally |
jjg@654 | 958 | // done by readSource, it may not have been done if the trees were read |
jjg@654 | 959 | // in a prior round of annotation processing, and the trees have been |
jjg@654 | 960 | // cleaned and are being reused. |
jjg@654 | 961 | for (JCCompilationUnit unit : roots) { |
jjg@654 | 962 | inputFiles.add(unit.sourcefile); |
jjg@654 | 963 | } |
jjg@654 | 964 | |
duke@1 | 965 | return roots; |
duke@1 | 966 | } |
duke@1 | 967 | |
duke@1 | 968 | /** |
duke@1 | 969 | * Set to true to enable skeleton annotation processing code. |
duke@1 | 970 | * Currently, we assume this variable will be replaced more |
duke@1 | 971 | * advanced logic to figure out if annotation processing is |
duke@1 | 972 | * needed. |
duke@1 | 973 | */ |
duke@1 | 974 | boolean processAnnotations = false; |
duke@1 | 975 | |
duke@1 | 976 | /** |
duke@1 | 977 | * Object to handle annotation processing. |
duke@1 | 978 | */ |
jjg@372 | 979 | private JavacProcessingEnvironment procEnvImpl = null; |
duke@1 | 980 | |
duke@1 | 981 | /** |
duke@1 | 982 | * Check if we should process annotations. |
duke@1 | 983 | * If so, and if no scanner is yet registered, then set up the DocCommentScanner |
duke@1 | 984 | * to catch doc comments, and set keepComments so the parser records them in |
duke@1 | 985 | * the compilation unit. |
duke@1 | 986 | * |
duke@1 | 987 | * @param processors user provided annotation processors to bypass |
duke@1 | 988 | * discovery, {@code null} means that no processors were provided |
duke@1 | 989 | */ |
jjg@663 | 990 | public void initProcessAnnotations(Iterable<? extends Processor> processors) { |
duke@1 | 991 | // Process annotations if processing is not disabled and there |
duke@1 | 992 | // is at least one Processor available. |
jjg@700 | 993 | if (options.isSet(PROC, "none")) { |
duke@1 | 994 | processAnnotations = false; |
duke@1 | 995 | } else if (procEnvImpl == null) { |
duke@1 | 996 | procEnvImpl = new JavacProcessingEnvironment(context, processors); |
duke@1 | 997 | processAnnotations = procEnvImpl.atLeastOneProcessor(); |
duke@1 | 998 | |
duke@1 | 999 | if (processAnnotations) { |
duke@1 | 1000 | options.put("save-parameter-names", "save-parameter-names"); |
duke@1 | 1001 | reader.saveParameterNames = true; |
duke@1 | 1002 | keepComments = true; |
jjg@695 | 1003 | genEndPos = true; |
duke@1 | 1004 | if (taskListener != null) |
duke@1 | 1005 | taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING)); |
jjg@664 | 1006 | log.deferDiagnostics = true; |
duke@1 | 1007 | } else { // free resources |
duke@1 | 1008 | procEnvImpl.close(); |
duke@1 | 1009 | } |
duke@1 | 1010 | } |
duke@1 | 1011 | } |
duke@1 | 1012 | |
duke@1 | 1013 | // TODO: called by JavacTaskImpl |
jjg@663 | 1014 | public JavaCompiler processAnnotations(List<JCCompilationUnit> roots) { |
duke@1 | 1015 | return processAnnotations(roots, List.<String>nil()); |
duke@1 | 1016 | } |
duke@1 | 1017 | |
duke@1 | 1018 | /** |
duke@1 | 1019 | * Process any anotations found in the specifed compilation units. |
duke@1 | 1020 | * @param roots a list of compilation units |
duke@1 | 1021 | * @return an instance of the compiler in which to complete the compilation |
duke@1 | 1022 | */ |
jjg@664 | 1023 | // Implementation note: when this method is called, log.deferredDiagnostics |
jjg@664 | 1024 | // will have been set true by initProcessAnnotations, meaning that any diagnostics |
jjg@664 | 1025 | // that are reported will go into the log.deferredDiagnostics queue. |
jjg@664 | 1026 | // By the time this method exits, log.deferDiagnostics must be set back to false, |
jjg@664 | 1027 | // and all deferredDiagnostics must have been handled: i.e. either reported |
jjg@664 | 1028 | // or determined to be transient, and therefore suppressed. |
duke@1 | 1029 | public JavaCompiler processAnnotations(List<JCCompilationUnit> roots, |
jjg@663 | 1030 | List<String> classnames) { |
jjg@257 | 1031 | if (shouldStop(CompileState.PROCESS)) { |
jjg@642 | 1032 | // Errors were encountered. |
jjg@664 | 1033 | // Unless all the errors are resolve errors, the errors were parse errors |
jjg@642 | 1034 | // or other errors during enter which cannot be fixed by running |
jjg@642 | 1035 | // any annotation processors. |
jjg@664 | 1036 | if (unrecoverableError()) { |
jjg@664 | 1037 | log.reportDeferredDiagnostics(); |
duke@1 | 1038 | return this; |
jjg@664 | 1039 | } |
duke@1 | 1040 | } |
duke@1 | 1041 | |
duke@1 | 1042 | // ASSERT: processAnnotations and procEnvImpl should have been set up by |
duke@1 | 1043 | // by initProcessAnnotations |
duke@1 | 1044 | |
duke@1 | 1045 | // NOTE: The !classnames.isEmpty() checks should be refactored to Main. |
duke@1 | 1046 | |
duke@1 | 1047 | if (!processAnnotations) { |
duke@1 | 1048 | // If there are no annotation processors present, and |
duke@1 | 1049 | // annotation processing is to occur with compilation, |
duke@1 | 1050 | // emit a warning. |
jjg@700 | 1051 | if (options.isSet(PROC, "only")) { |
duke@1 | 1052 | log.warning("proc.proc-only.requested.no.procs"); |
duke@1 | 1053 | todo.clear(); |
duke@1 | 1054 | } |
duke@1 | 1055 | // If not processing annotations, classnames must be empty |
duke@1 | 1056 | if (!classnames.isEmpty()) { |
duke@1 | 1057 | log.error("proc.no.explicit.annotation.processing.requested", |
duke@1 | 1058 | classnames); |
duke@1 | 1059 | } |
jjg@664 | 1060 | log.reportDeferredDiagnostics(); |
duke@1 | 1061 | return this; // continue regular compilation |
duke@1 | 1062 | } |
duke@1 | 1063 | |
duke@1 | 1064 | try { |
duke@1 | 1065 | List<ClassSymbol> classSymbols = List.nil(); |
duke@1 | 1066 | List<PackageSymbol> pckSymbols = List.nil(); |
duke@1 | 1067 | if (!classnames.isEmpty()) { |
duke@1 | 1068 | // Check for explicit request for annotation |
duke@1 | 1069 | // processing |
duke@1 | 1070 | if (!explicitAnnotationProcessingRequested()) { |
duke@1 | 1071 | log.error("proc.no.explicit.annotation.processing.requested", |
duke@1 | 1072 | classnames); |
jjg@664 | 1073 | log.reportDeferredDiagnostics(); |
duke@1 | 1074 | return this; // TODO: Will this halt compilation? |
duke@1 | 1075 | } else { |
duke@1 | 1076 | boolean errors = false; |
duke@1 | 1077 | for (String nameStr : classnames) { |
jjg@937 | 1078 | Symbol sym = resolveBinaryNameOrIdent(nameStr); |
duke@1 | 1079 | if (sym == null || (sym.kind == Kinds.PCK && !processPcks)) { |
duke@1 | 1080 | log.error("proc.cant.find.class", nameStr); |
duke@1 | 1081 | errors = true; |
duke@1 | 1082 | continue; |
duke@1 | 1083 | } |
duke@1 | 1084 | try { |
duke@1 | 1085 | if (sym.kind == Kinds.PCK) |
duke@1 | 1086 | sym.complete(); |
duke@1 | 1087 | if (sym.exists()) { |
duke@1 | 1088 | if (sym.kind == Kinds.PCK) |
duke@1 | 1089 | pckSymbols = pckSymbols.prepend((PackageSymbol)sym); |
duke@1 | 1090 | else |
duke@1 | 1091 | classSymbols = classSymbols.prepend((ClassSymbol)sym); |
duke@1 | 1092 | continue; |
duke@1 | 1093 | } |
jjg@816 | 1094 | Assert.check(sym.kind == Kinds.PCK); |
duke@1 | 1095 | log.warning("proc.package.does.not.exist", nameStr); |
duke@1 | 1096 | pckSymbols = pckSymbols.prepend((PackageSymbol)sym); |
duke@1 | 1097 | } catch (CompletionFailure e) { |
duke@1 | 1098 | log.error("proc.cant.find.class", nameStr); |
duke@1 | 1099 | errors = true; |
duke@1 | 1100 | continue; |
duke@1 | 1101 | } |
duke@1 | 1102 | } |
jjg@664 | 1103 | if (errors) { |
jjg@664 | 1104 | log.reportDeferredDiagnostics(); |
duke@1 | 1105 | return this; |
jjg@664 | 1106 | } |
duke@1 | 1107 | } |
duke@1 | 1108 | } |
jjg@372 | 1109 | try { |
jjg@372 | 1110 | JavaCompiler c = procEnvImpl.doProcessing(context, roots, classSymbols, pckSymbols); |
jjg@372 | 1111 | if (c != this) |
jjg@372 | 1112 | annotationProcessingOccurred = c.annotationProcessingOccurred = true; |
jjg@664 | 1113 | // doProcessing will have handled deferred diagnostics |
jjg@816 | 1114 | Assert.check(c.log.deferDiagnostics == false |
jjg@816 | 1115 | && c.log.deferredDiagnostics.size() == 0); |
jjg@372 | 1116 | return c; |
jjg@372 | 1117 | } finally { |
jjg@372 | 1118 | procEnvImpl.close(); |
jjg@372 | 1119 | } |
duke@1 | 1120 | } catch (CompletionFailure ex) { |
jjg@12 | 1121 | log.error("cant.access", ex.sym, ex.getDetailValue()); |
jjg@664 | 1122 | log.reportDeferredDiagnostics(); |
duke@1 | 1123 | return this; |
jjg@664 | 1124 | } |
jjg@664 | 1125 | } |
duke@1 | 1126 | |
jjg@664 | 1127 | private boolean unrecoverableError() { |
jjg@664 | 1128 | for (JCDiagnostic d: log.deferredDiagnostics) { |
jjg@726 | 1129 | if (d.getKind() == JCDiagnostic.Kind.ERROR && !d.isFlagSet(RECOVERABLE)) |
jjg@664 | 1130 | return true; |
duke@1 | 1131 | } |
jjg@664 | 1132 | return false; |
duke@1 | 1133 | } |
duke@1 | 1134 | |
duke@1 | 1135 | boolean explicitAnnotationProcessingRequested() { |
duke@1 | 1136 | return |
duke@1 | 1137 | explicitAnnotationProcessingRequested || |
jjg@902 | 1138 | explicitAnnotationProcessingRequested(options); |
jjg@902 | 1139 | } |
jjg@902 | 1140 | |
jjg@902 | 1141 | static boolean explicitAnnotationProcessingRequested(Options options) { |
jjg@902 | 1142 | return |
jjg@700 | 1143 | options.isSet(PROCESSOR) || |
jjg@700 | 1144 | options.isSet(PROCESSORPATH) || |
jjg@700 | 1145 | options.isSet(PROC, "only") || |
jjg@700 | 1146 | options.isSet(XPRINT); |
duke@1 | 1147 | } |
duke@1 | 1148 | |
duke@1 | 1149 | /** |
duke@1 | 1150 | * Attribute a list of parse trees, such as found on the "todo" list. |
duke@1 | 1151 | * Note that attributing classes may cause additional files to be |
duke@1 | 1152 | * parsed and entered via the SourceCompleter. |
duke@1 | 1153 | * Attribution of the entries in the list does not stop if any errors occur. |
duke@1 | 1154 | * @returns a list of environments for attributd classes. |
duke@1 | 1155 | */ |
jjg@70 | 1156 | public Queue<Env<AttrContext>> attribute(Queue<Env<AttrContext>> envs) { |
duke@1 | 1157 | ListBuffer<Env<AttrContext>> results = lb(); |
jjg@70 | 1158 | while (!envs.isEmpty()) |
jjg@70 | 1159 | results.append(attribute(envs.remove())); |
jjg@257 | 1160 | return stopIfError(CompileState.ATTR, results); |
duke@1 | 1161 | } |
duke@1 | 1162 | |
duke@1 | 1163 | /** |
duke@1 | 1164 | * Attribute a parse tree. |
duke@1 | 1165 | * @returns the attributed parse tree |
duke@1 | 1166 | */ |
duke@1 | 1167 | public Env<AttrContext> attribute(Env<AttrContext> env) { |
jjg@77 | 1168 | if (compileStates.isDone(env, CompileState.ATTR)) |
jjg@77 | 1169 | return env; |
jjg@77 | 1170 | |
duke@1 | 1171 | if (verboseCompilePolicy) |
jjg@604 | 1172 | printNote("[attribute " + env.enclClass.sym + "]"); |
duke@1 | 1173 | if (verbose) |
jjg@909 | 1174 | log.printVerbose("checking.attribution", env.enclClass.sym); |
duke@1 | 1175 | |
duke@1 | 1176 | if (taskListener != null) { |
duke@1 | 1177 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym); |
duke@1 | 1178 | taskListener.started(e); |
duke@1 | 1179 | } |
duke@1 | 1180 | |
duke@1 | 1181 | JavaFileObject prev = log.useSource( |
duke@1 | 1182 | env.enclClass.sym.sourcefile != null ? |
duke@1 | 1183 | env.enclClass.sym.sourcefile : |
duke@1 | 1184 | env.toplevel.sourcefile); |
duke@1 | 1185 | try { |
jjg@931 | 1186 | attr.attrib(env); |
mcimadamore@676 | 1187 | if (errorCount() > 0 && !shouldStop(CompileState.ATTR)) { |
mcimadamore@676 | 1188 | //if in fail-over mode, ensure that AST expression nodes |
mcimadamore@676 | 1189 | //are correctly initialized (e.g. they have a type/symbol) |
mcimadamore@676 | 1190 | attr.postAttr(env); |
mcimadamore@676 | 1191 | } |
jjg@77 | 1192 | compileStates.put(env, CompileState.ATTR); |
duke@1 | 1193 | } |
duke@1 | 1194 | finally { |
duke@1 | 1195 | log.useSource(prev); |
duke@1 | 1196 | } |
duke@1 | 1197 | |
duke@1 | 1198 | return env; |
duke@1 | 1199 | } |
duke@1 | 1200 | |
duke@1 | 1201 | /** |
duke@1 | 1202 | * Perform dataflow checks on attributed parse trees. |
duke@1 | 1203 | * These include checks for definite assignment and unreachable statements. |
duke@1 | 1204 | * If any errors occur, an empty list will be returned. |
duke@1 | 1205 | * @returns the list of attributed parse trees |
duke@1 | 1206 | */ |
jjg@70 | 1207 | public Queue<Env<AttrContext>> flow(Queue<Env<AttrContext>> envs) { |
duke@1 | 1208 | ListBuffer<Env<AttrContext>> results = lb(); |
jjg@70 | 1209 | for (Env<AttrContext> env: envs) { |
jjg@70 | 1210 | flow(env, results); |
duke@1 | 1211 | } |
jjg@257 | 1212 | return stopIfError(CompileState.FLOW, results); |
duke@1 | 1213 | } |
duke@1 | 1214 | |
duke@1 | 1215 | /** |
duke@1 | 1216 | * Perform dataflow checks on an attributed parse tree. |
duke@1 | 1217 | */ |
jjg@70 | 1218 | public Queue<Env<AttrContext>> flow(Env<AttrContext> env) { |
duke@1 | 1219 | ListBuffer<Env<AttrContext>> results = lb(); |
duke@1 | 1220 | flow(env, results); |
jjg@257 | 1221 | return stopIfError(CompileState.FLOW, results); |
duke@1 | 1222 | } |
duke@1 | 1223 | |
duke@1 | 1224 | /** |
duke@1 | 1225 | * Perform dataflow checks on an attributed parse tree. |
duke@1 | 1226 | */ |
jjg@119 | 1227 | protected void flow(Env<AttrContext> env, Queue<Env<AttrContext>> results) { |
duke@1 | 1228 | try { |
jjg@257 | 1229 | if (shouldStop(CompileState.FLOW)) |
duke@1 | 1230 | return; |
duke@1 | 1231 | |
jjg@77 | 1232 | if (relax || compileStates.isDone(env, CompileState.FLOW)) { |
jjg@119 | 1233 | results.add(env); |
duke@1 | 1234 | return; |
duke@1 | 1235 | } |
duke@1 | 1236 | |
duke@1 | 1237 | if (verboseCompilePolicy) |
jjg@257 | 1238 | printNote("[flow " + env.enclClass.sym + "]"); |
duke@1 | 1239 | JavaFileObject prev = log.useSource( |
duke@1 | 1240 | env.enclClass.sym.sourcefile != null ? |
duke@1 | 1241 | env.enclClass.sym.sourcefile : |
duke@1 | 1242 | env.toplevel.sourcefile); |
duke@1 | 1243 | try { |
duke@1 | 1244 | make.at(Position.FIRSTPOS); |
duke@1 | 1245 | TreeMaker localMake = make.forToplevel(env.toplevel); |
mcimadamore@617 | 1246 | flow.analyzeTree(env, localMake); |
jjg@77 | 1247 | compileStates.put(env, CompileState.FLOW); |
duke@1 | 1248 | |
jjg@257 | 1249 | if (shouldStop(CompileState.FLOW)) |
duke@1 | 1250 | return; |
duke@1 | 1251 | |
jjg@119 | 1252 | results.add(env); |
duke@1 | 1253 | } |
duke@1 | 1254 | finally { |
duke@1 | 1255 | log.useSource(prev); |
duke@1 | 1256 | } |
duke@1 | 1257 | } |
duke@1 | 1258 | finally { |
duke@1 | 1259 | if (taskListener != null) { |
duke@1 | 1260 | TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym); |
duke@1 | 1261 | taskListener.finished(e); |
duke@1 | 1262 | } |
duke@1 | 1263 | } |
duke@1 | 1264 | } |
duke@1 | 1265 | |
duke@1 | 1266 | /** |
duke@1 | 1267 | * Prepare attributed parse trees, in conjunction with their attribution contexts, |
duke@1 | 1268 | * for source or code generation. |
duke@1 | 1269 | * If any errors occur, an empty list will be returned. |
duke@1 | 1270 | * @returns a list containing the classes to be generated |
duke@1 | 1271 | */ |
jjg@70 | 1272 | public Queue<Pair<Env<AttrContext>, JCClassDecl>> desugar(Queue<Env<AttrContext>> envs) { |
duke@1 | 1273 | ListBuffer<Pair<Env<AttrContext>, JCClassDecl>> results = lb(); |
jjg@70 | 1274 | for (Env<AttrContext> env: envs) |
jjg@70 | 1275 | desugar(env, results); |
jjg@257 | 1276 | return stopIfError(CompileState.FLOW, results); |
duke@1 | 1277 | } |
duke@1 | 1278 | |
mcimadamore@359 | 1279 | HashMap<Env<AttrContext>, Queue<Pair<Env<AttrContext>, JCClassDecl>>> desugaredEnvs = |
mcimadamore@359 | 1280 | new HashMap<Env<AttrContext>, Queue<Pair<Env<AttrContext>, JCClassDecl>>>(); |
mcimadamore@359 | 1281 | |
duke@1 | 1282 | /** |
duke@1 | 1283 | * Prepare attributed parse trees, in conjunction with their attribution contexts, |
duke@1 | 1284 | * for source or code generation. If the file was not listed on the command line, |
duke@1 | 1285 | * the current implicitSourcePolicy is taken into account. |
duke@1 | 1286 | * The preparation stops as soon as an error is found. |
duke@1 | 1287 | */ |
jjg@77 | 1288 | protected void desugar(final Env<AttrContext> env, Queue<Pair<Env<AttrContext>, JCClassDecl>> results) { |
jjg@257 | 1289 | if (shouldStop(CompileState.TRANSTYPES)) |
duke@1 | 1290 | return; |
duke@1 | 1291 | |
duke@1 | 1292 | if (implicitSourcePolicy == ImplicitSourcePolicy.NONE |
duke@1 | 1293 | && !inputFiles.contains(env.toplevel.sourcefile)) { |
duke@1 | 1294 | return; |
duke@1 | 1295 | } |
duke@1 | 1296 | |
mcimadamore@359 | 1297 | if (compileStates.isDone(env, CompileState.LOWER)) { |
mcimadamore@359 | 1298 | results.addAll(desugaredEnvs.get(env)); |
mcimadamore@359 | 1299 | return; |
mcimadamore@359 | 1300 | } |
mcimadamore@359 | 1301 | |
jjg@77 | 1302 | /** |
mcimadamore@359 | 1303 | * Ensure that superclasses of C are desugared before C itself. This is |
mcimadamore@359 | 1304 | * required for two reasons: (i) as erasure (TransTypes) destroys |
mcimadamore@359 | 1305 | * information needed in flow analysis and (ii) as some checks carried |
mcimadamore@359 | 1306 | * out during lowering require that all synthetic fields/methods have |
mcimadamore@359 | 1307 | * already been added to C and its superclasses. |
jjg@77 | 1308 | */ |
jjg@77 | 1309 | class ScanNested extends TreeScanner { |
mcimadamore@95 | 1310 | Set<Env<AttrContext>> dependencies = new LinkedHashSet<Env<AttrContext>>(); |
jjg@257 | 1311 | @Override |
jjg@77 | 1312 | public void visitClassDef(JCClassDecl node) { |
jjg@77 | 1313 | Type st = types.supertype(node.sym.type); |
jjg@77 | 1314 | if (st.tag == TypeTags.CLASS) { |
jjg@77 | 1315 | ClassSymbol c = st.tsym.outermostClass(); |
jjg@77 | 1316 | Env<AttrContext> stEnv = enter.getEnv(c); |
mcimadamore@95 | 1317 | if (stEnv != null && env != stEnv) { |
mcimadamore@95 | 1318 | if (dependencies.add(stEnv)) |
mcimadamore@95 | 1319 | scan(stEnv.tree); |
mcimadamore@95 | 1320 | } |
jjg@77 | 1321 | } |
jjg@77 | 1322 | super.visitClassDef(node); |
jjg@77 | 1323 | } |
duke@1 | 1324 | } |
jjg@77 | 1325 | ScanNested scanner = new ScanNested(); |
jjg@77 | 1326 | scanner.scan(env.tree); |
jjg@77 | 1327 | for (Env<AttrContext> dep: scanner.dependencies) { |
mcimadamore@359 | 1328 | if (!compileStates.isDone(dep, CompileState.FLOW)) |
mcimadamore@359 | 1329 | desugaredEnvs.put(dep, desugar(flow(attribute(dep)))); |
jjg@77 | 1330 | } |
duke@1 | 1331 | |
mcimadamore@95 | 1332 | //We need to check for error another time as more classes might |
mcimadamore@95 | 1333 | //have been attributed and analyzed at this stage |
jjg@257 | 1334 | if (shouldStop(CompileState.TRANSTYPES)) |
mcimadamore@95 | 1335 | return; |
mcimadamore@95 | 1336 | |
duke@1 | 1337 | if (verboseCompilePolicy) |
jjg@257 | 1338 | printNote("[desugar " + env.enclClass.sym + "]"); |
duke@1 | 1339 | |
duke@1 | 1340 | JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? |
duke@1 | 1341 | env.enclClass.sym.sourcefile : |
duke@1 | 1342 | env.toplevel.sourcefile); |
duke@1 | 1343 | try { |
duke@1 | 1344 | //save tree prior to rewriting |
duke@1 | 1345 | JCTree untranslated = env.tree; |
duke@1 | 1346 | |
duke@1 | 1347 | make.at(Position.FIRSTPOS); |
duke@1 | 1348 | TreeMaker localMake = make.forToplevel(env.toplevel); |
duke@1 | 1349 | |
duke@1 | 1350 | if (env.tree instanceof JCCompilationUnit) { |
duke@1 | 1351 | if (!(stubOutput || sourceOutput || printFlat)) { |
jjg@257 | 1352 | if (shouldStop(CompileState.LOWER)) |
jjg@257 | 1353 | return; |
duke@1 | 1354 | List<JCTree> pdef = lower.translateTopLevelClass(env, env.tree, localMake); |
duke@1 | 1355 | if (pdef.head != null) { |
jjg@816 | 1356 | Assert.check(pdef.tail.isEmpty()); |
jjg@70 | 1357 | results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, (JCClassDecl)pdef.head)); |
duke@1 | 1358 | } |
duke@1 | 1359 | } |
duke@1 | 1360 | return; |
duke@1 | 1361 | } |
duke@1 | 1362 | |
duke@1 | 1363 | if (stubOutput) { |
duke@1 | 1364 | //emit stub Java source file, only for compilation |
duke@1 | 1365 | //units enumerated explicitly on the command line |
duke@1 | 1366 | JCClassDecl cdef = (JCClassDecl)env.tree; |
duke@1 | 1367 | if (untranslated instanceof JCClassDecl && |
duke@1 | 1368 | rootClasses.contains((JCClassDecl)untranslated) && |
duke@1 | 1369 | ((cdef.mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || |
duke@1 | 1370 | cdef.sym.packge().getQualifiedName() == names.java_lang)) { |
jjg@70 | 1371 | results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, removeMethodBodies(cdef))); |
duke@1 | 1372 | } |
duke@1 | 1373 | return; |
duke@1 | 1374 | } |
duke@1 | 1375 | |
jjg@257 | 1376 | if (shouldStop(CompileState.TRANSTYPES)) |
jjg@257 | 1377 | return; |
jjg@257 | 1378 | |
duke@1 | 1379 | env.tree = transTypes.translateTopLevelClass(env.tree, localMake); |
mcimadamore@359 | 1380 | compileStates.put(env, CompileState.TRANSTYPES); |
duke@1 | 1381 | |
jjg@257 | 1382 | if (shouldStop(CompileState.LOWER)) |
duke@1 | 1383 | return; |
duke@1 | 1384 | |
duke@1 | 1385 | if (sourceOutput) { |
duke@1 | 1386 | //emit standard Java source file, only for compilation |
duke@1 | 1387 | //units enumerated explicitly on the command line |
duke@1 | 1388 | JCClassDecl cdef = (JCClassDecl)env.tree; |
duke@1 | 1389 | if (untranslated instanceof JCClassDecl && |
duke@1 | 1390 | rootClasses.contains((JCClassDecl)untranslated)) { |
jjg@70 | 1391 | results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, cdef)); |
duke@1 | 1392 | } |
duke@1 | 1393 | return; |
duke@1 | 1394 | } |
duke@1 | 1395 | |
duke@1 | 1396 | //translate out inner classes |
duke@1 | 1397 | List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake); |
mcimadamore@359 | 1398 | compileStates.put(env, CompileState.LOWER); |
duke@1 | 1399 | |
jjg@257 | 1400 | if (shouldStop(CompileState.LOWER)) |
duke@1 | 1401 | return; |
duke@1 | 1402 | |
duke@1 | 1403 | //generate code for each class |
duke@1 | 1404 | for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) { |
duke@1 | 1405 | JCClassDecl cdef = (JCClassDecl)l.head; |
jjg@70 | 1406 | results.add(new Pair<Env<AttrContext>, JCClassDecl>(env, cdef)); |
duke@1 | 1407 | } |
duke@1 | 1408 | } |
duke@1 | 1409 | finally { |
duke@1 | 1410 | log.useSource(prev); |
duke@1 | 1411 | } |
duke@1 | 1412 | |
duke@1 | 1413 | } |
duke@1 | 1414 | |
duke@1 | 1415 | /** Generates the source or class file for a list of classes. |
duke@1 | 1416 | * The decision to generate a source file or a class file is |
duke@1 | 1417 | * based upon the compiler's options. |
duke@1 | 1418 | * Generation stops if an error occurs while writing files. |
duke@1 | 1419 | */ |
jjg@70 | 1420 | public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) { |
jjg@70 | 1421 | generate(queue, null); |
duke@1 | 1422 | } |
duke@1 | 1423 | |
jjg@119 | 1424 | public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) { |
jjg@257 | 1425 | if (shouldStop(CompileState.GENERATE)) |
jjg@257 | 1426 | return; |
jjg@257 | 1427 | |
duke@1 | 1428 | boolean usePrintSource = (stubOutput || sourceOutput || printFlat); |
duke@1 | 1429 | |
jjg@70 | 1430 | for (Pair<Env<AttrContext>, JCClassDecl> x: queue) { |
duke@1 | 1431 | Env<AttrContext> env = x.fst; |
duke@1 | 1432 | JCClassDecl cdef = x.snd; |
duke@1 | 1433 | |
duke@1 | 1434 | if (verboseCompilePolicy) { |
jjg@257 | 1435 | printNote("[generate " |
duke@1 | 1436 | + (usePrintSource ? " source" : "code") |
jjg@119 | 1437 | + " " + cdef.sym + "]"); |
duke@1 | 1438 | } |
duke@1 | 1439 | |
duke@1 | 1440 | if (taskListener != null) { |
duke@1 | 1441 | TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym); |
duke@1 | 1442 | taskListener.started(e); |
duke@1 | 1443 | } |
duke@1 | 1444 | |
duke@1 | 1445 | JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ? |
duke@1 | 1446 | env.enclClass.sym.sourcefile : |
duke@1 | 1447 | env.toplevel.sourcefile); |
duke@1 | 1448 | try { |
duke@1 | 1449 | JavaFileObject file; |
duke@1 | 1450 | if (usePrintSource) |
duke@1 | 1451 | file = printSource(env, cdef); |
duke@1 | 1452 | else |
duke@1 | 1453 | file = genCode(env, cdef); |
duke@1 | 1454 | if (results != null && file != null) |
jjg@119 | 1455 | results.add(file); |
duke@1 | 1456 | } catch (IOException ex) { |
duke@1 | 1457 | log.error(cdef.pos(), "class.cant.write", |
duke@1 | 1458 | cdef.sym, ex.getMessage()); |
duke@1 | 1459 | return; |
duke@1 | 1460 | } finally { |
duke@1 | 1461 | log.useSource(prev); |
duke@1 | 1462 | } |
duke@1 | 1463 | |
duke@1 | 1464 | if (taskListener != null) { |
duke@1 | 1465 | TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym); |
duke@1 | 1466 | taskListener.finished(e); |
duke@1 | 1467 | } |
duke@1 | 1468 | } |
duke@1 | 1469 | } |
duke@1 | 1470 | |
duke@1 | 1471 | // where |
jjg@70 | 1472 | Map<JCCompilationUnit, Queue<Env<AttrContext>>> groupByFile(Queue<Env<AttrContext>> envs) { |
duke@1 | 1473 | // use a LinkedHashMap to preserve the order of the original list as much as possible |
jjg@70 | 1474 | Map<JCCompilationUnit, Queue<Env<AttrContext>>> map = new LinkedHashMap<JCCompilationUnit, Queue<Env<AttrContext>>>(); |
jjg@70 | 1475 | for (Env<AttrContext> env: envs) { |
jjg@70 | 1476 | Queue<Env<AttrContext>> sublist = map.get(env.toplevel); |
jjg@70 | 1477 | if (sublist == null) { |
jjg@70 | 1478 | sublist = new ListBuffer<Env<AttrContext>>(); |
jjg@70 | 1479 | map.put(env.toplevel, sublist); |
duke@1 | 1480 | } |
jjg@70 | 1481 | sublist.add(env); |
duke@1 | 1482 | } |
duke@1 | 1483 | return map; |
duke@1 | 1484 | } |
duke@1 | 1485 | |
duke@1 | 1486 | JCClassDecl removeMethodBodies(JCClassDecl cdef) { |
duke@1 | 1487 | final boolean isInterface = (cdef.mods.flags & Flags.INTERFACE) != 0; |
duke@1 | 1488 | class MethodBodyRemover extends TreeTranslator { |
jjg@257 | 1489 | @Override |
duke@1 | 1490 | public void visitMethodDef(JCMethodDecl tree) { |
duke@1 | 1491 | tree.mods.flags &= ~Flags.SYNCHRONIZED; |
duke@1 | 1492 | for (JCVariableDecl vd : tree.params) |
duke@1 | 1493 | vd.mods.flags &= ~Flags.FINAL; |
duke@1 | 1494 | tree.body = null; |
duke@1 | 1495 | super.visitMethodDef(tree); |
duke@1 | 1496 | } |
jjg@257 | 1497 | @Override |
duke@1 | 1498 | public void visitVarDef(JCVariableDecl tree) { |
duke@1 | 1499 | if (tree.init != null && tree.init.type.constValue() == null) |
duke@1 | 1500 | tree.init = null; |
duke@1 | 1501 | super.visitVarDef(tree); |
duke@1 | 1502 | } |
jjg@257 | 1503 | @Override |
duke@1 | 1504 | public void visitClassDef(JCClassDecl tree) { |
duke@1 | 1505 | ListBuffer<JCTree> newdefs = lb(); |
duke@1 | 1506 | for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) { |
duke@1 | 1507 | JCTree t = it.head; |
duke@1 | 1508 | switch (t.getTag()) { |
jjg@1127 | 1509 | case CLASSDEF: |
duke@1 | 1510 | if (isInterface || |
duke@1 | 1511 | (((JCClassDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || |
duke@1 | 1512 | (((JCClassDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCClassDecl) t).sym.packge().getQualifiedName() == names.java_lang) |
duke@1 | 1513 | newdefs.append(t); |
duke@1 | 1514 | break; |
jjg@1127 | 1515 | case METHODDEF: |
duke@1 | 1516 | if (isInterface || |
duke@1 | 1517 | (((JCMethodDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || |
duke@1 | 1518 | ((JCMethodDecl) t).sym.name == names.init || |
duke@1 | 1519 | (((JCMethodDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCMethodDecl) t).sym.packge().getQualifiedName() == names.java_lang) |
duke@1 | 1520 | newdefs.append(t); |
duke@1 | 1521 | break; |
jjg@1127 | 1522 | case VARDEF: |
duke@1 | 1523 | if (isInterface || (((JCVariableDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 || |
duke@1 | 1524 | (((JCVariableDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCVariableDecl) t).sym.packge().getQualifiedName() == names.java_lang) |
duke@1 | 1525 | newdefs.append(t); |
duke@1 | 1526 | break; |
duke@1 | 1527 | default: |
duke@1 | 1528 | break; |
duke@1 | 1529 | } |
duke@1 | 1530 | } |
duke@1 | 1531 | tree.defs = newdefs.toList(); |
duke@1 | 1532 | super.visitClassDef(tree); |
duke@1 | 1533 | } |
duke@1 | 1534 | } |
duke@1 | 1535 | MethodBodyRemover r = new MethodBodyRemover(); |
duke@1 | 1536 | return r.translate(cdef); |
duke@1 | 1537 | } |
duke@1 | 1538 | |
duke@1 | 1539 | public void reportDeferredDiagnostics() { |
jjg@903 | 1540 | if (errorCount() == 0 |
jjg@903 | 1541 | && annotationProcessingOccurred |
duke@1 | 1542 | && implicitSourceFilesRead |
duke@1 | 1543 | && implicitSourcePolicy == ImplicitSourcePolicy.UNSET) { |
duke@1 | 1544 | if (explicitAnnotationProcessingRequested()) |
duke@1 | 1545 | log.warning("proc.use.implicit"); |
duke@1 | 1546 | else |
duke@1 | 1547 | log.warning("proc.use.proc.or.implicit"); |
duke@1 | 1548 | } |
duke@1 | 1549 | chk.reportDeferredDiagnostics(); |
duke@1 | 1550 | } |
duke@1 | 1551 | |
duke@1 | 1552 | /** Close the compiler, flushing the logs |
duke@1 | 1553 | */ |
duke@1 | 1554 | public void close() { |
duke@1 | 1555 | close(true); |
duke@1 | 1556 | } |
duke@1 | 1557 | |
jjg@113 | 1558 | public void close(boolean disposeNames) { |
duke@1 | 1559 | rootClasses = null; |
duke@1 | 1560 | reader = null; |
duke@1 | 1561 | make = null; |
duke@1 | 1562 | writer = null; |
duke@1 | 1563 | enter = null; |
duke@1 | 1564 | if (todo != null) |
duke@1 | 1565 | todo.clear(); |
duke@1 | 1566 | todo = null; |
duke@1 | 1567 | parserFactory = null; |
duke@1 | 1568 | syms = null; |
duke@1 | 1569 | source = null; |
duke@1 | 1570 | attr = null; |
duke@1 | 1571 | chk = null; |
duke@1 | 1572 | gen = null; |
duke@1 | 1573 | flow = null; |
duke@1 | 1574 | transTypes = null; |
duke@1 | 1575 | lower = null; |
duke@1 | 1576 | annotate = null; |
duke@1 | 1577 | types = null; |
duke@1 | 1578 | |
duke@1 | 1579 | log.flush(); |
duke@1 | 1580 | try { |
duke@1 | 1581 | fileManager.flush(); |
duke@1 | 1582 | } catch (IOException e) { |
duke@1 | 1583 | throw new Abort(e); |
duke@1 | 1584 | } finally { |
duke@1 | 1585 | if (names != null && disposeNames) |
duke@1 | 1586 | names.dispose(); |
duke@1 | 1587 | names = null; |
jjg@1096 | 1588 | |
jjg@1096 | 1589 | for (Closeable c: closeables) { |
jjg@1096 | 1590 | try { |
jjg@1096 | 1591 | c.close(); |
jjg@1096 | 1592 | } catch (IOException e) { |
jjg@1096 | 1593 | // When javac uses JDK 7 as a baseline, this code would be |
jjg@1096 | 1594 | // better written to set any/all exceptions from all the |
jjg@1096 | 1595 | // Closeables as suppressed exceptions on the FatalError |
jjg@1096 | 1596 | // that is thrown. |
jjg@1096 | 1597 | JCDiagnostic msg = diagFactory.fragment("fatal.err.cant.close"); |
jjg@1096 | 1598 | throw new FatalError(msg, e); |
jjg@1096 | 1599 | } |
jjg@1096 | 1600 | } |
duke@1 | 1601 | } |
duke@1 | 1602 | } |
duke@1 | 1603 | |
jjg@257 | 1604 | protected void printNote(String lines) { |
jjg@257 | 1605 | Log.printLines(log.noticeWriter, lines); |
jjg@257 | 1606 | } |
jjg@257 | 1607 | |
duke@1 | 1608 | /** Print numbers of errors and warnings. |
duke@1 | 1609 | */ |
duke@1 | 1610 | protected void printCount(String kind, int count) { |
duke@1 | 1611 | if (count != 0) { |
jjg@604 | 1612 | String key; |
duke@1 | 1613 | if (count == 1) |
jjg@604 | 1614 | key = "count." + kind; |
duke@1 | 1615 | else |
jjg@604 | 1616 | key = "count." + kind + ".plural"; |
jjg@604 | 1617 | log.printErrLines(key, String.valueOf(count)); |
duke@1 | 1618 | log.errWriter.flush(); |
duke@1 | 1619 | } |
duke@1 | 1620 | } |
duke@1 | 1621 | |
duke@1 | 1622 | private static long now() { |
duke@1 | 1623 | return System.currentTimeMillis(); |
duke@1 | 1624 | } |
duke@1 | 1625 | |
duke@1 | 1626 | private static long elapsed(long then) { |
duke@1 | 1627 | return now() - then; |
duke@1 | 1628 | } |
duke@1 | 1629 | |
duke@1 | 1630 | public void initRound(JavaCompiler prev) { |
jjg@695 | 1631 | genEndPos = prev.genEndPos; |
duke@1 | 1632 | keepComments = prev.keepComments; |
duke@1 | 1633 | start_msec = prev.start_msec; |
duke@1 | 1634 | hasBeenUsed = true; |
jjg@1096 | 1635 | closeables = prev.closeables; |
jjg@1096 | 1636 | prev.closeables = List.nil(); |
duke@1 | 1637 | } |
duke@1 | 1638 | |
duke@1 | 1639 | public static void enableLogging() { |
duke@1 | 1640 | Logger logger = Logger.getLogger(com.sun.tools.javac.Main.class.getPackage().getName()); |
duke@1 | 1641 | logger.setLevel(Level.ALL); |
duke@1 | 1642 | for (Handler h : logger.getParent().getHandlers()) { |
duke@1 | 1643 | h.setLevel(Level.ALL); |
duke@1 | 1644 | } |
duke@1 | 1645 | |
duke@1 | 1646 | } |
duke@1 | 1647 | } |