src/jdk.compiler/share/classes/com/sun/tools/javac/main/JavaCompiler.java
author Dusan Balek <dbalek@netbeans.org>
Mon, 31 Jul 2017 11:07:41 +0200
changeset 5955 f54cccaf6e6c
parent 5862 cf7168447ec6
child 5959 28bebf0841dd
permissions -rw-r--r--
Mergin jlahoda's fix of #8182450: javac aborts when generating ct.sym intermittently - Initialize the module system model even in presence of missing/broken module-infos; BadClassFiles should not immediatelly abort compilation anymore, but should be handled as if the classfile did not exist.
duke@0
     1
/*
jlahoda@5675
     2
 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
duke@0
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0
     4
 *
duke@0
     5
 * This code is free software; you can redistribute it and/or modify it
duke@0
     6
 * under the terms of the GNU General Public License version 2 only, as
ohair@961
     7
 * published by the Free Software Foundation.  Oracle designates this
duke@0
     8
 * particular file as subject to the "Classpath" exception as provided
ohair@961
     9
 * by Oracle in the LICENSE file that accompanied this code.
duke@0
    10
 *
duke@0
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
duke@0
    14
 * version 2 for more details (a copy is included in the LICENSE file that
duke@0
    15
 * accompanied this code).
duke@0
    16
 *
duke@0
    17
 * You should have received a copy of the GNU General Public License version
duke@0
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0
    20
 *
ohair@961
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@961
    22
 * or visit www.oracle.com if you need additional information or have any
ohair@961
    23
 * questions.
duke@0
    24
 */
duke@0
    25
duke@0
    26
package com.sun.tools.javac.main;
duke@0
    27
duke@0
    28
import java.io.*;
jjg@4174
    29
import java.util.Collection;
tzezula@780
    30
import java.util.Collections;
jjg@1132
    31
import java.util.HashMap;
duke@0
    32
import java.util.HashSet;
jjg@2050
    33
import java.util.LinkedHashMap;
mcimadamore@273
    34
import java.util.LinkedHashSet;
duke@0
    35
import java.util.Map;
duke@0
    36
import java.util.MissingResourceException;
jjg@1132
    37
import java.util.Queue;
duke@0
    38
import java.util.ResourceBundle;
duke@0
    39
import java.util.Set;
dbalek@5253
    40
import java.util.function.Function;
duke@0
    41
jjg@1132
    42
import javax.annotation.processing.Processor;
jjg@1132
    43
import javax.lang.model.SourceVersion;
alanb@5016
    44
import javax.lang.model.element.ElementVisitor;
jjg@2050
    45
import javax.tools.DiagnosticListener;
duke@0
    46
import javax.tools.JavaFileManager;
duke@0
    47
import javax.tools.JavaFileObject;
sadayapalam@5724
    48
import javax.tools.JavaFileObject.Kind;
jjg@2072
    49
import javax.tools.StandardLocation;
jjg@2072
    50
duke@0
    51
import com.sun.source.util.TaskEvent;
dbalek@2718
    52
import com.sun.tools.javac.api.DuplicateClassChecker;
jjg@2050
    53
import com.sun.tools.javac.api.MultiTaskListener;
duke@0
    54
import com.sun.tools.javac.code.*;
jjg@1202
    55
import com.sun.tools.javac.code.Lint.LintCategory;
jjg@4174
    56
import com.sun.tools.javac.code.Symbol.ClassSymbol;
jjg@4174
    57
import com.sun.tools.javac.code.Symbol.CompletionFailure;
jjg@4174
    58
import com.sun.tools.javac.code.Symbol.PackageSymbol;
jjg@1974
    59
import com.sun.tools.javac.comp.*;
jjg@2704
    60
import com.sun.tools.javac.comp.CompileStates.CompileState;
jjg@1974
    61
import com.sun.tools.javac.file.JavacFileManager;
jjg@1974
    62
import com.sun.tools.javac.jvm.*;
jjg@1974
    63
import com.sun.tools.javac.parser.*;
jlahoda@4550
    64
import com.sun.tools.javac.platform.PlatformDescription;
jjg@1974
    65
import com.sun.tools.javac.processing.*;
duke@0
    66
import com.sun.tools.javac.tree.*;
jjg@4174
    67
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
jjg@4174
    68
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
jjg@4174
    69
import com.sun.tools.javac.tree.JCTree.JCExpression;
jjg@4174
    70
import com.sun.tools.javac.tree.JCTree.JCLambda;
jjg@4174
    71
import com.sun.tools.javac.tree.JCTree.JCMemberReference;
jjg@4174
    72
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
jjg@4174
    73
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
jjg@1974
    74
import com.sun.tools.javac.util.*;
alanb@5016
    75
import com.sun.tools.javac.util.DefinedBy.Api;
alanb@5016
    76
import com.sun.tools.javac.util.JCDiagnostic.Factory;
jlahoda@5832
    77
import com.sun.tools.javac.util.Log.DiagnosticHandler;
jlahoda@5832
    78
import com.sun.tools.javac.util.Log.DiscardDiagnosticHandler;
jjg@1974
    79
import com.sun.tools.javac.util.Log.WriterKind;
jjg@2072
    80
emc@4248
    81
import static com.sun.tools.javac.code.Kinds.Kind.*;
alanb@5016
    82
alanb@5016
    83
import com.sun.tools.javac.code.Symbol.ModuleSymbol;
alanb@5016
    84
import com.sun.tools.javac.resources.CompilerProperties.Errors;
alanb@5016
    85
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
alanb@5016
    86
jjg@2213
    87
import static com.sun.tools.javac.code.TypeTag.CLASS;
jjg@1996
    88
import static com.sun.tools.javac.main.Option.*;
jjg@1060
    89
import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag.*;
tzezula@5426
    90
import java.util.Iterator;
alanb@5016
    91
emc@4248
    92
import static javax.tools.StandardLocation.CLASS_OUTPUT;
duke@0
    93
jlahoda@5832
    94
import com.sun.tools.javac.tree.JCTree.JCModuleDecl;
jlahoda@5832
    95
duke@0
    96
/** This class could be the main entry point for GJC when GJC is used as a
duke@0
    97
 *  component in a larger software system. It provides operations to
duke@0
    98
 *  construct a new compiler, and to run a new compiler on a set of source
duke@0
    99
 *  files.
duke@0
   100
 *
jjg@971
   101
 *  <p><b>This is NOT part of any supported API.
jjg@971
   102
 *  If you write code that depends on this, you do so at your own risk.
duke@0
   103
 *  This code and its internal interfaces are subject to change or
duke@0
   104
 *  deletion without notice.</b>
duke@0
   105
 */
jfranck@3023
   106
public class JavaCompiler {
duke@0
   107
    /** The context key for the compiler. */
mcimadamore@4596
   108
    public static final Context.Key<JavaCompiler> compilerKey = new Context.Key<>();
duke@0
   109
duke@0
   110
    /** Get the JavaCompiler instance for this context. */
duke@0
   111
    public static JavaCompiler instance(Context context) {
duke@0
   112
        JavaCompiler instance = context.get(compilerKey);
duke@0
   113
        if (instance == null)
duke@0
   114
            instance = new JavaCompiler(context);
duke@0
   115
        return instance;
duke@0
   116
    }
duke@0
   117
duke@0
   118
    /** The current version number as a string.
duke@0
   119
     */
duke@0
   120
    public static String version() {
duke@0
   121
        return version("release");  // mm.nn.oo[-milestone]
duke@0
   122
    }
duke@0
   123
duke@0
   124
    /** The current full version number as a string.
duke@0
   125
     */
duke@0
   126
    public static String fullVersion() {
duke@0
   127
        return version("full"); // mm.mm.oo[-milestone]-build
duke@0
   128
    }
duke@0
   129
duke@0
   130
    private static final String versionRBName = "com.sun.tools.javac.resources.version";
duke@0
   131
    private static ResourceBundle versionRB;
duke@0
   132
duke@0
   133
    private static String version(String key) {
duke@0
   134
        if (versionRB == null) {
duke@0
   135
            try {
duke@0
   136
                versionRB = ResourceBundle.getBundle(versionRBName);
duke@0
   137
            } catch (MissingResourceException e) {
jjg@993
   138
                return Log.getLocalizedString("version.not.available");
duke@0
   139
            }
duke@0
   140
        }
duke@0
   141
        try {
duke@0
   142
            return versionRB.getString(key);
duke@0
   143
        }
duke@0
   144
        catch (MissingResourceException e) {
jjg@993
   145
            return Log.getLocalizedString("version.not.available");
duke@0
   146
        }
duke@0
   147
    }
duke@0
   148
jjg@297
   149
    /**
jjg@297
   150
     * Control how the compiler's latter phases (attr, flow, desugar, generate)
jjg@297
   151
     * are connected. Each individual file is processed by each phase in turn,
jjg@297
   152
     * but with different compile policies, you can control the order in which
jjg@297
   153
     * each class is processed through its next phase.
jjg@297
   154
     *
jjg@297
   155
     * <p>Generally speaking, the compiler will "fail fast" in the face of
jjg@297
   156
     * errors, although not aggressively so. flow, desugar, etc become no-ops
jjg@297
   157
     * once any errors have occurred. No attempt is currently made to determine
jjg@297
   158
     * if it might be safe to process a class through its next phase because
jjg@297
   159
     * it does not depend on any unrelated errors that might have occurred.
jjg@297
   160
     */
jjg@297
   161
    protected static enum CompilePolicy {
jjg@297
   162
        /**
jjg@297
   163
         * Just attribute the parse trees.
duke@0
   164
         */
duke@0
   165
        ATTR_ONLY,
duke@0
   166
jjg@297
   167
        /**
duke@0
   168
         * Just attribute and do flow analysis on the parse trees.
duke@0
   169
         * This should catch most user errors.
duke@0
   170
         */
duke@0
   171
        CHECK_ONLY,
duke@0
   172
jjg@297
   173
        /**
duke@0
   174
         * Attribute everything, then do flow analysis for everything,
duke@0
   175
         * then desugar everything, and only then generate output.
jjg@297
   176
         * This means no output will be generated if there are any
jjg@297
   177
         * errors in any classes.
duke@0
   178
         */
duke@0
   179
        SIMPLE,
duke@0
   180
jjg@297
   181
        /**
jjg@297
   182
         * Groups the classes for each source file together, then process
jjg@297
   183
         * each group in a manner equivalent to the {@code SIMPLE} policy.
jjg@297
   184
         * This means no output will be generated if there are any
jjg@297
   185
         * errors in any of the classes in a source file.
duke@0
   186
         */
duke@0
   187
        BY_FILE,
duke@0
   188
jjg@297
   189
        /**
duke@0
   190
         * Completely process each entry on the todo list in turn.
duke@0
   191
         * -- this is the same for 1.5.
duke@0
   192
         * Means output might be generated for some classes in a compilation unit
duke@0
   193
         * and not others.
duke@0
   194
         */
duke@0
   195
        BY_TODO;
duke@0
   196
duke@0
   197
        static CompilePolicy decode(String option) {
duke@0
   198
            if (option == null)
duke@0
   199
                return DEFAULT_COMPILE_POLICY;
duke@0
   200
            else if (option.equals("attr"))
duke@0
   201
                return ATTR_ONLY;
duke@0
   202
            else if (option.equals("check"))
duke@0
   203
                return CHECK_ONLY;
duke@0
   204
            else if (option.equals("simple"))
duke@0
   205
                return SIMPLE;
duke@0
   206
            else if (option.equals("byfile"))
duke@0
   207
                return BY_FILE;
duke@0
   208
            else if (option.equals("bytodo"))
duke@0
   209
                return BY_TODO;
duke@0
   210
            else
duke@0
   211
                return DEFAULT_COMPILE_POLICY;
duke@0
   212
        }
duke@0
   213
    }
duke@0
   214
vromero@2285
   215
    private static final CompilePolicy DEFAULT_COMPILE_POLICY = CompilePolicy.BY_TODO;
duke@0
   216
jjg@297
   217
    protected static enum ImplicitSourcePolicy {
duke@0
   218
        /** Don't generate or process implicitly read source files. */
duke@0
   219
        NONE,
duke@0
   220
        /** Generate classes for implicitly read source files. */
duke@0
   221
        CLASS,
duke@0
   222
        /** Like CLASS, but generate warnings if annotation processing occurs */
duke@0
   223
        UNSET;
duke@0
   224
duke@0
   225
        static ImplicitSourcePolicy decode(String option) {
duke@0
   226
            if (option == null)
duke@0
   227
                return UNSET;
duke@0
   228
            else if (option.equals("none"))
duke@0
   229
                return NONE;
duke@0
   230
            else if (option.equals("class"))
duke@0
   231
                return CLASS;
duke@0
   232
            else
duke@0
   233
                return UNSET;
duke@0
   234
        }
duke@0
   235
    }
duke@0
   236
duke@0
   237
    /** The log to be used for error reporting.
duke@0
   238
     */
duke@0
   239
    public Log log;
duke@0
   240
jjg@194
   241
    /** Factory for creating diagnostic objects
jjg@194
   242
     */
jjg@194
   243
    JCDiagnostic.Factory diagFactory;
jjg@194
   244
duke@0
   245
    /** The tree factory module.
duke@0
   246
     */
duke@0
   247
    protected TreeMaker make;
duke@0
   248
jjg@4002
   249
    /** The class finder.
jjg@4002
   250
     */
jjg@4002
   251
    protected ClassFinder finder;
jjg@4002
   252
duke@0
   253
    /** The class reader.
duke@0
   254
     */
duke@0
   255
    protected ClassReader reader;
duke@0
   256
duke@0
   257
    /** The class writer.
duke@0
   258
     */
duke@0
   259
    protected ClassWriter writer;
duke@0
   260
jjg@2072
   261
    /** The native header writer.
jjg@2072
   262
     */
jjg@2072
   263
    protected JNIWriter jniWriter;
jjg@2072
   264
duke@0
   265
    /** The module for the symbol table entry phases.
duke@0
   266
     */
duke@0
   267
    protected Enter enter;
duke@0
   268
duke@0
   269
    /** The symbol table.
duke@0
   270
     */
duke@0
   271
    protected Symtab syms;
duke@0
   272
duke@0
   273
    /** The language version.
duke@0
   274
     */
duke@0
   275
    protected Source source;
duke@0
   276
duke@0
   277
    /** The module for code generation.
duke@0
   278
     */
duke@0
   279
    protected Gen gen;
duke@0
   280
duke@0
   281
    /** The name table.
duke@0
   282
     */
jjg@294
   283
    protected Names names;
duke@0
   284
duke@0
   285
    /** The attributor.
duke@0
   286
     */
duke@0
   287
    protected Attr attr;
duke@0
   288
duke@0
   289
    /** The attributor.
duke@0
   290
     */
duke@0
   291
    protected Check chk;
duke@0
   292
duke@0
   293
    /** The flow analyzer.
duke@0
   294
     */
duke@0
   295
    protected Flow flow;
mcimadamore@4596
   296
alanb@5016
   297
    /** The modules visitor
alanb@5016
   298
     */
alanb@5016
   299
    protected Modules modules;
alanb@5016
   300
alanb@5016
   301
    /** The module finder
alanb@5016
   302
     */
alanb@5016
   303
    protected ModuleFinder moduleFinder;
alanb@5016
   304
alanb@5016
   305
    /** The diagnostics factory
alanb@5016
   306
     */
alanb@5016
   307
    protected JCDiagnostic.Factory diags;
alanb@5016
   308
dbalek@42
   309
    
dbalek@42
   310
    /** The error repairer.
dbalek@42
   311
     */
dbalek@62
   312
    public Repair repair;
dbalek@42
   313
    
duke@0
   314
    /** The type eraser.
duke@0
   315
     */
jjg@297
   316
    protected TransTypes transTypes;
duke@0
   317
duke@0
   318
    /** The syntactic sugar desweetener.
duke@0
   319
     */
jjg@297
   320
    protected Lower lower;
duke@0
   321
duke@0
   322
    /** The annotation annotator.
duke@0
   323
     */
duke@0
   324
    protected Annotate annotate;
duke@0
   325
duke@0
   326
    /** Force a completion failure on this name
duke@0
   327
     */
duke@0
   328
    protected final Name completionFailureName;
duke@0
   329
duke@0
   330
    /** Type utilities.
duke@0
   331
     */
duke@0
   332
    protected Types types;
duke@0
   333
duke@0
   334
    /** Access to file objects.
duke@0
   335
     */
duke@0
   336
    protected JavaFileManager fileManager;
duke@0
   337
duke@0
   338
    /** Factory for parsers.
duke@0
   339
     */
jjg@290
   340
    protected ParserFactory parserFactory;
duke@0
   341
jjg@2050
   342
    /** Broadcasting listener for progress events
duke@0
   343
     */
jjg@2050
   344
    protected MultiTaskListener taskListener;
duke@0
   345
duke@0
   346
    /**
jjg@4002
   347
     * SourceCompleter that delegates to the readSourceFile method of this class.
jfranck@3023
   348
     */
jjg@4002
   349
    protected final Symbol.Completer sourceCompleter =
mcimadamore@5585
   350
            sym -> readSourceFile((ClassSymbol) sym);
jfranck@3023
   351
jfranck@3023
   352
    /**
jjg@1381
   353
     * Command line options.
jjg@1381
   354
     */
jjg@1381
   355
    protected Options options;
jjg@1381
   356
jjg@1381
   357
    protected Context context;
jjg@1381
   358
jjg@1381
   359
    /**
duke@0
   360
     * Flag set if any annotation processing occurred.
duke@0
   361
     **/
duke@0
   362
    protected boolean annotationProcessingOccurred;
duke@0
   363
duke@0
   364
    /**
duke@0
   365
     * Flag set if any implicit source files read.
duke@0
   366
     **/
duke@0
   367
    protected boolean implicitSourceFilesRead;
duke@0
   368
jlahoda@5675
   369
    private boolean enterDone;
alanb@5016
   370
vromero@2619
   371
    protected CompileStates compileStates;
vromero@2619
   372
jlahoda@5
   373
    protected Map<JavaFileObject, JCCompilationUnit> notYetEntered;
jlahoda@5
   374
dbalek@752
   375
    public boolean skipAnnotationProcessing = false;
dbalek@752
   376
dbalek@2718
   377
    private final DuplicateClassChecker duplicateClassChecker;
dbalek@791
   378
    public List<JCCompilationUnit> toProcessAnnotations = List.nil();
dbalek@752
   379
duke@0
   380
    /** Construct a new compiler using a shared context.
duke@0
   381
     */
jjg@1377
   382
    public JavaCompiler(Context context) {
duke@0
   383
        this.context = context;
duke@0
   384
        context.put(compilerKey, this);
duke@0
   385
duke@0
   386
        // if fileManager not already set, register the JavacFileManager to be used
duke@0
   387
        if (context.get(JavaFileManager.class) == null)
duke@0
   388
            JavacFileManager.preRegister(context);
duke@0
   389
jjg@294
   390
        names = Names.instance(context);
duke@0
   391
        log = Log.instance(context);
jjg@194
   392
        diagFactory = JCDiagnostic.Factory.instance(context);
jjg@4002
   393
        finder = ClassFinder.instance(context);
duke@0
   394
        reader = ClassReader.instance(context);
duke@0
   395
        make = TreeMaker.instance(context);
duke@0
   396
        writer = ClassWriter.instance(context);
jjg@2072
   397
        jniWriter = JNIWriter.instance(context);
duke@0
   398
        enter = Enter.instance(context);
duke@0
   399
        todo = Todo.instance(context);
duke@0
   400
duke@0
   401
        fileManager = context.get(JavaFileManager.class);
jjg@290
   402
        parserFactory = ParserFactory.instance(context);
vromero@2619
   403
        compileStates = CompileStates.instance(context);
duke@0
   404
duke@0
   405
        try {
duke@0
   406
            // catch completion problems with predefineds
duke@0
   407
            syms = Symtab.instance(context);
duke@0
   408
        } catch (CompletionFailure ex) {
duke@0
   409
            // inlined Check.completionError as it is not initialized yet
jjg@194
   410
            log.error("cant.access", ex.sym, ex.getDetailValue());
duke@0
   411
        }
duke@0
   412
        source = Source.instance(context);
duke@0
   413
        attr = Attr.instance(context);
duke@0
   414
        chk = Check.instance(context);
duke@0
   415
        gen = Gen.instance(context);
duke@0
   416
        flow = Flow.instance(context);
dbalek@42
   417
        repair = Repair.instance(context);
duke@0
   418
        transTypes = TransTypes.instance(context);
duke@0
   419
        lower = Lower.instance(context);
duke@0
   420
        annotate = Annotate.instance(context);
duke@0
   421
        types = Types.instance(context);
jjg@2050
   422
        taskListener = MultiTaskListener.instance(context);
alanb@5016
   423
        modules = Modules.instance(context);
alanb@5016
   424
        moduleFinder = ModuleFinder.instance(context);
alanb@5016
   425
        diags = Factory.instance(context);
dbalek@2718
   426
        duplicateClassChecker = context.get(DuplicateClassChecker.class);
duke@0
   427
jjg@4002
   428
        finder.sourceCompleter = sourceCompleter;
jlahoda@5833
   429
        modules.findPackageInFile = this::findPackageInFile;
jlahoda@5832
   430
        moduleFinder.moduleNameFromSourceReader = this::readModuleName;
duke@0
   431
jjg@1381
   432
        options = Options.instance(context);
duke@0
   433
jjg@1132
   434
        verbose       = options.isSet(VERBOSE);
jjg@1132
   435
        sourceOutput  = options.isSet(PRINTSOURCE); // used to be -s
jjg@1132
   436
        lineDebugInfo = options.isUnset(G_CUSTOM) ||
jjg@1132
   437
                        options.isSet(G_CUSTOM, "lines");
jjg@1132
   438
        genEndPos     = options.isSet(XJCOV) ||
jlahoda@5
   439
                        (context.get(DiagnosticListener.class) != null && options.get("backgroundCompilation") == null);
jjg@1132
   440
        devVerbose    = options.isSet("dev");
jjg@1132
   441
        processPcks   = options.isSet("process.packages");
jjg@1132
   442
        werror        = options.isSet(WERROR);
jlahoda@2388
   443
        keepComments  = options.getBoolean("keepComments");
duke@0
   444
jjg@1132
   445
        verboseCompilePolicy = options.isSet("verboseCompilePolicy");
duke@0
   446
vromero@5388
   447
        if (options.isSet("should-stop.at") &&
vromero@5388
   448
            CompileState.valueOf(options.get("should-stop.at")) == CompileState.ATTR)
duke@0
   449
            compilePolicy = CompilePolicy.ATTR_ONLY;
duke@0
   450
        else
duke@0
   451
            compilePolicy = CompilePolicy.decode(options.get("compilePolicy"));
duke@0
   452
duke@0
   453
        implicitSourcePolicy = ImplicitSourcePolicy.decode(options.get("-implicit"));
duke@0
   454
duke@0
   455
        completionFailureName =
jjg@1132
   456
            options.isSet("failcomplete")
duke@0
   457
            ? names.fromString(options.get("failcomplete"))
duke@0
   458
            : null;
jjg@547
   459
jjg@2179
   460
        shouldStopPolicyIfError =
vromero@5388
   461
            options.isSet("should-stop.at") // backwards compatible
vromero@5388
   462
            ? CompileState.valueOf(options.get("should-stop.at"))
vromero@5388
   463
            : options.isSet("should-stop.ifError")
vromero@5388
   464
            ? CompileState.valueOf(options.get("should-stop.ifError"))
jjg@2179
   465
            : CompileState.INIT;
jjg@2179
   466
        shouldStopPolicyIfNoError =
vromero@5388
   467
            options.isSet("should-stop.ifNoError")
vromero@5388
   468
            ? CompileState.valueOf(options.get("should-stop.ifNoError"))
jjg@2179
   469
            : CompileState.GENERATE;
jjg@2179
   470
vromero@5281
   471
        if (options.isUnset("diags.legacy"))
mcimadamore@578
   472
            log.setDiagnosticFormatter(RichDiagnosticFormatter.instance(context));
jlahoda@4550
   473
jlahoda@4550
   474
        PlatformDescription platformProvider = context.get(PlatformDescription.class);
jlahoda@4550
   475
jlahoda@4550
   476
        if (platformProvider != null)
jlahoda@4550
   477
            closeables = closeables.prepend(platformProvider);
alanb@5016
   478
alanb@5016
   479
        silentFail = new Symbol(ABSENT_TYP, 0, names.empty, Type.noType, syms.rootPackage) {
alanb@5016
   480
            @DefinedBy(Api.LANGUAGE_MODEL)
alanb@5016
   481
            public <R, P> R accept(ElementVisitor<R, P> v, P p) {
alanb@5016
   482
                return v.visitUnknown(this, p);
alanb@5016
   483
            }
alanb@5016
   484
            @Override
alanb@5016
   485
            public boolean exists() {
alanb@5016
   486
                return false;
alanb@5016
   487
            }
alanb@5016
   488
        };
alanb@5016
   489
duke@0
   490
    }
duke@0
   491
duke@0
   492
    /* Switches:
duke@0
   493
     */
duke@0
   494
duke@0
   495
    /** Verbose output.
duke@0
   496
     */
duke@0
   497
    public boolean verbose;
duke@0
   498
duke@0
   499
    /** Emit plain Java source files rather than class files.
duke@0
   500
     */
duke@0
   501
    public boolean sourceOutput;
duke@0
   502
duke@0
   503
duke@0
   504
    /** Generate code with the LineNumberTable attribute for debugging
duke@0
   505
     */
duke@0
   506
    public boolean lineDebugInfo;
duke@0
   507
duke@0
   508
    /** Switch: should we store the ending positions?
duke@0
   509
     */
duke@0
   510
    public boolean genEndPos;
duke@0
   511
duke@0
   512
    /** Switch: should we debug ignored exceptions
duke@0
   513
     */
duke@0
   514
    protected boolean devVerbose;
duke@0
   515
duke@0
   516
    /** Switch: should we (annotation) process packages as well
duke@0
   517
     */
duke@0
   518
    protected boolean processPcks;
duke@0
   519
jjg@397
   520
    /** Switch: treat warnings as errors
jjg@397
   521
     */
jjg@397
   522
    protected boolean werror;
jjg@397
   523
jjg@2704
   524
    /** Switch: is annotation processing requested explicitly via
duke@0
   525
     * CompilationTask.setProcessors?
duke@0
   526
     */
duke@0
   527
    protected boolean explicitAnnotationProcessingRequested = false;
duke@0
   528
duke@0
   529
    /**
duke@0
   530
     * The policy for the order in which to perform the compilation
duke@0
   531
     */
duke@0
   532
    protected CompilePolicy compilePolicy;
duke@0
   533
duke@0
   534
    /**
duke@0
   535
     * The policy for what to do with implicitly read source files
duke@0
   536
     */
duke@0
   537
    protected ImplicitSourcePolicy implicitSourcePolicy;
duke@0
   538
duke@0
   539
    /**
duke@0
   540
     * Report activity related to compilePolicy
duke@0
   541
     */
duke@0
   542
    public boolean verboseCompilePolicy;
duke@0
   543
jjg@547
   544
    /**
jjg@2179
   545
     * Policy of how far to continue compilation after errors have occurred.
jjg@2179
   546
     * Set this to minimum CompileState (INIT) to stop as soon as possible
jjg@2179
   547
     * after errors.
jjg@547
   548
     */
jjg@2179
   549
    public CompileState shouldStopPolicyIfError;
jjg@547
   550
jjg@2179
   551
    /**
jjg@2179
   552
     * Policy of how far to continue compilation when no errors have occurred.
jjg@2179
   553
     * Set this to maximum CompileState (GENERATE) to perform full compilation.
jjg@2179
   554
     * Set this lower to perform partial compilation, such as -proc:only.
jjg@2179
   555
     */
jjg@2179
   556
    public CompileState shouldStopPolicyIfNoError;
jjg@2179
   557
jjg@2372
   558
    /** A queue of all as yet unattributed classes.
duke@0
   559
     */
duke@0
   560
    public Todo todo;
duke@0
   561
jjg@1938
   562
    /** A list of items to be closed when the compilation is complete.
jjg@1938
   563
     */
jjg@1938
   564
    public List<Closeable> closeables = List.nil();
jjg@1938
   565
duke@0
   566
    /** The set of currently compiled inputfiles, needed to ensure
duke@0
   567
     *  we don't accidentally overwrite an input file when -s is set.
duke@0
   568
     *  initialized by `compile'.
duke@0
   569
     */
briangoetz@3808
   570
    protected Set<JavaFileObject> inputFiles = new HashSet<>();
duke@0
   571
alanb@5016
   572
    /** Used by the resolveBinaryNameOrIdent to say that the given type cannot be found, and that
alanb@5016
   573
     *  an error has already been produced about that.
alanb@5016
   574
     */
alanb@5016
   575
    private final Symbol silentFail;
alanb@5016
   576
jjg@547
   577
    protected boolean shouldStop(CompileState cs) {
jjg@2179
   578
        CompileState shouldStopPolicy = (errorCount() > 0 || unrecoverableError())
jjg@2179
   579
            ? shouldStopPolicyIfError
jjg@2179
   580
            : shouldStopPolicyIfNoError;
jjg@2179
   581
        return cs.isAfter(shouldStopPolicy);
jjg@547
   582
    }
jjg@547
   583
duke@0
   584
    /** The number of errors reported so far.
duke@0
   585
     */
duke@0
   586
    public int errorCount() {
jlahoda@3928
   587
        if (werror && log.nerrors == 0 && log.nwarnings > 0) {
jlahoda@3928
   588
            log.error("warnings.and.werror");
jjg@397
   589
        }
jjg@1048
   590
        return log.nerrors;
duke@0
   591
    }
duke@0
   592
jjg@547
   593
    protected final <T> Queue<T> stopIfError(CompileState cs, Queue<T> queue) {
vromero@3810
   594
        return shouldStop(cs) ? new ListBuffer<T>() : queue;
duke@0
   595
    }
duke@0
   596
jjg@547
   597
    protected final <T> List<T> stopIfError(CompileState cs, List<T> list) {
mcimadamore@5586
   598
        return shouldStop(cs) ? List.nil() : list;
duke@0
   599
    }
duke@0
   600
duke@0
   601
    /** The number of warnings reported so far.
duke@0
   602
     */
duke@0
   603
    public int warningCount() {
jlahoda@3928
   604
        return log.nwarnings;
duke@0
   605
    }
duke@0
   606
duke@0
   607
    /** Try to open input stream with given name.
duke@0
   608
     *  Report an error if this fails.
duke@0
   609
     *  @param filename   The file name of the input stream to be opened.
duke@0
   610
     */
duke@0
   611
    public CharSequence readSource(JavaFileObject filename) {
duke@0
   612
        try {
duke@0
   613
            inputFiles.add(filename);
duke@0
   614
            return filename.getCharContent(false);
duke@0
   615
        } catch (IOException e) {
jjg@903
   616
            log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
duke@0
   617
            return null;
duke@0
   618
        }
duke@0
   619
    }
duke@0
   620
duke@0
   621
    /** Parse contents of input stream.
duke@0
   622
     *  @param filename     The name of the file from which input stream comes.
jjg@2197
   623
     *  @param content      The characters to be parsed.
duke@0
   624
     */
duke@0
   625
    protected JCCompilationUnit parse(JavaFileObject filename, CharSequence content) {
duke@0
   626
        long msec = now();
mcimadamore@5586
   627
        JCCompilationUnit tree = make.TopLevel(List.nil());
duke@0
   628
        if (content != null) {
duke@0
   629
            if (verbose) {
jjg@1393
   630
                log.printVerbose("parsing.started", filename);
duke@0
   631
            }
jjg@2050
   632
            if (!taskListener.isEmpty()) {
duke@0
   633
                TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, filename);
duke@0
   634
                taskListener.started(e);
jjg@2421
   635
                keepComments = true;
jjg@2421
   636
                genEndPos = true;
duke@0
   637
            }
sadayapalam@5724
   638
            Parser parser = parserFactory.newParser(content, keepComments(), genEndPos,
dbalek@5754
   639
                                lineDebugInfo, filename != null && filename.isNameCompatible("module-info", Kind.SOURCE));
mcimadamore@5586
   640
            tree = parser.parseCompilationUnit();
duke@0
   641
            if (verbose) {
jjg@1393
   642
                log.printVerbose("parsing.done", Long.toString(elapsed(msec)));
duke@0
   643
            }
duke@0
   644
        }
duke@0
   645
duke@0
   646
        tree.sourcefile = filename;
duke@0
   647
jjg@2050
   648
        if (content != null && !taskListener.isEmpty()) {
duke@0
   649
            TaskEvent e = new TaskEvent(TaskEvent.Kind.PARSE, tree);
duke@0
   650
            taskListener.finished(e);
duke@0
   651
        }
duke@0
   652
duke@0
   653
        return tree;
duke@0
   654
    }
duke@0
   655
    // where
duke@0
   656
        public boolean keepComments = false;
duke@0
   657
        protected boolean keepComments() {
vromero@4979
   658
            return keepComments || sourceOutput;
duke@0
   659
        }
duke@0
   660
duke@0
   661
duke@0
   662
    /** Parse contents of file.
duke@0
   663
     *  @param filename     The name of the file to be parsed.
duke@0
   664
     */
duke@0
   665
    @Deprecated
jjg@1059
   666
    public JCTree.JCCompilationUnit parse(String filename) {
duke@0
   667
        JavacFileManager fm = (JavacFileManager)fileManager;
duke@0
   668
        return parse(fm.getJavaFileObjectsFromStrings(List.of(filename)).iterator().next());
duke@0
   669
    }
duke@0
   670
duke@0
   671
    /** Parse contents of file.
duke@0
   672
     *  @param filename     The name of the file to be parsed.
duke@0
   673
     */
duke@0
   674
    public JCTree.JCCompilationUnit parse(JavaFileObject filename) {
duke@0
   675
        JavaFileObject prev = log.useSource(filename);
duke@0
   676
        try {
duke@0
   677
            JCTree.JCCompilationUnit t = parse(filename, readSource(filename));
duke@0
   678
            if (t.endPositions != null)
duke@0
   679
                log.setEndPosTable(filename, t.endPositions);
duke@0
   680
            return t;
duke@0
   681
        } finally {
duke@0
   682
            log.useSource(prev);
duke@0
   683
        }
duke@0
   684
    }
duke@0
   685
jjg@1421
   686
    /** Resolve an identifier which may be the binary name of a class or
jjg@1421
   687
     * the Java name of a class or package.
jjg@1421
   688
     * @param name      The name to resolve
jjg@1421
   689
     */
jjg@1421
   690
    public Symbol resolveBinaryNameOrIdent(String name) {
alanb@5016
   691
        ModuleSymbol msym;
alanb@5016
   692
        String typeName;
alanb@5016
   693
        int sep = name.indexOf('/');
alanb@5016
   694
        if (sep == -1) {
alanb@5016
   695
            msym = modules.getDefaultModule();
alanb@5016
   696
            typeName = name;
jlahoda@5643
   697
        } else if (source.allowModules()) {
alanb@5016
   698
            Name modName = names.fromString(name.substring(0, sep));
alanb@5016
   699
alanb@5016
   700
            msym = moduleFinder.findModule(modName);
alanb@5016
   701
            typeName = name.substring(sep + 1);
alanb@5016
   702
        } else {
alanb@5016
   703
            log.error(Errors.InvalidModuleSpecifier(name));
alanb@5016
   704
            return silentFail;
alanb@5016
   705
        }
alanb@5016
   706
alanb@5016
   707
        return resolveBinaryNameOrIdent(msym, typeName);
alanb@5016
   708
    }
alanb@5016
   709
alanb@5016
   710
    /** Resolve an identifier which may be the binary name of a class or
alanb@5016
   711
     * the Java name of a class or package.
alanb@5016
   712
     * @param msym      The module in which the search should be performed
alanb@5016
   713
     * @param name      The name to resolve
alanb@5016
   714
     */
alanb@5016
   715
    public Symbol resolveBinaryNameOrIdent(ModuleSymbol msym, String name) {
jjg@1421
   716
        try {
jjg@1421
   717
            Name flatname = names.fromString(name.replace("/", "."));
alanb@5016
   718
            return finder.loadClass(msym, flatname);
jjg@1421
   719
        } catch (CompletionFailure ignore) {
alanb@5016
   720
            return resolveIdent(msym, name);
jjg@1421
   721
        }
jjg@1421
   722
    }
jjg@1421
   723
duke@0
   724
    /** Resolve an identifier.
alanb@5016
   725
     * @param msym      The module in which the search should be performed
duke@0
   726
     * @param name      The identifier to resolve
duke@0
   727
     */
alanb@5016
   728
    public Symbol resolveIdent(ModuleSymbol msym, String name) {
duke@0
   729
        if (name.equals(""))
duke@0
   730
            return syms.errSymbol;
duke@0
   731
        JavaFileObject prev = log.useSource(null);
dbalek@2330
   732
        Log.DiagnosticHandler discardHandler = new Log.DiscardDiagnosticHandler(log);
dbalek@3769
   733
        Log.DeferredDiagnosticHandler deferredHandler = deferredDiagnosticHandler;
dbalek@3769
   734
        deferredDiagnosticHandler = null;
duke@0
   735
        try {
duke@0
   736
            JCExpression tree = null;
duke@0
   737
            for (String s : name.split("\\.", -1)) {
duke@0
   738
                if (!SourceVersion.isIdentifier(s)) // TODO: check for keywords
duke@0
   739
                    return syms.errSymbol;
duke@0
   740
                tree = (tree == null) ? make.Ident(names.fromString(s))
duke@0
   741
                                      : make.Select(tree, names.fromString(s));
duke@0
   742
            }
duke@0
   743
            JCCompilationUnit toplevel =
mcimadamore@5586
   744
                make.TopLevel(List.nil());
alanb@5016
   745
            toplevel.modle = msym;
alanb@5016
   746
            toplevel.packge = msym.unnamedPackage;
dbalek@4426
   747
            Symbol ret = attr.attribIdent(tree, toplevel);
dbalek@4427
   748
            if (!skipAnnotationProcessing && deferredDiagnosticHandler != null && toProcessAnnotations.nonEmpty())
dbalek@4426
   749
                processAnnotations(List.<JCCompilationUnit>nil());
dbalek@4426
   750
            return ret;
duke@0
   751
        } finally {
dbalek@3769
   752
            deferredDiagnosticHandler = deferredHandler;
dbalek@2330
   753
            log.popDiagnosticHandler(discardHandler);
duke@0
   754
            log.useSource(prev);
duke@0
   755
        }
duke@0
   756
    }
duke@0
   757
duke@0
   758
    /** Generate code and emit a class file for a given class
duke@0
   759
     *  @param env    The attribution environment of the outermost class
duke@0
   760
     *                containing this class.
duke@0
   761
     *  @param cdef   The class definition from which code is generated.
duke@0
   762
     */
duke@0
   763
    JavaFileObject genCode(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
duke@0
   764
        try {
duke@0
   765
            if (gen.genClass(env, cdef))
duke@0
   766
                return writer.writeClass(cdef.sym);
duke@0
   767
        } catch (ClassWriter.PoolOverflow ex) {
duke@0
   768
            log.error(cdef.pos(), "limit.pool");
duke@0
   769
        } catch (ClassWriter.StringOverflow ex) {
duke@0
   770
            log.error(cdef.pos(), "limit.string.overflow",
duke@0
   771
                      ex.value.substring(0, 20));
duke@0
   772
        } catch (CompletionFailure ex) {
duke@0
   773
            chk.completionError(cdef.pos(), ex);
duke@0
   774
        }
duke@0
   775
        return null;
duke@0
   776
    }
duke@0
   777
vromero@4979
   778
    /** Emit plain Java source for a class.
vromero@4979
   779
     *  @param env    The attribution environment of the outermost class
vromero@4979
   780
     *                containing this class.
vromero@4979
   781
     *  @param cdef   The class definition to be printed.
vromero@4979
   782
     */
vromero@4979
   783
    JavaFileObject printSource(Env<AttrContext> env, JCClassDecl cdef) throws IOException {
vromero@4979
   784
        JavaFileObject outFile
vromero@4979
   785
           = fileManager.getJavaFileForOutput(CLASS_OUTPUT,
vromero@4979
   786
                                               cdef.sym.flatname.toString(),
vromero@4979
   787
                                               JavaFileObject.Kind.SOURCE,
vromero@4979
   788
                                               null);
vromero@4979
   789
        if (inputFiles.contains(outFile)) {
vromero@4979
   790
            log.error(cdef.pos(), "source.cant.overwrite.input.file", outFile);
vromero@4979
   791
            return null;
vromero@4979
   792
        } else {
vromero@4979
   793
            try (BufferedWriter out = new BufferedWriter(outFile.openWriter())) {
vromero@4979
   794
                new Pretty(out, true).printUnit(env.toplevel, cdef);
vromero@4979
   795
                if (verbose)
vromero@4979
   796
                    log.printVerbose("wrote.file", outFile);
vromero@4979
   797
            }
vromero@4979
   798
            return outFile;
vromero@4979
   799
        }
vromero@4979
   800
    }
vromero@4979
   801
jjg@4002
   802
    /** Compile a source file that has been accessed by the class finder.
duke@0
   803
     *  @param c          The class the source file of which needs to be compiled.
duke@0
   804
     */
jjg@4002
   805
    private void readSourceFile(ClassSymbol c) throws CompletionFailure {
jjg@4002
   806
        readSourceFile(null, c);
jlahoda@3928
   807
    }
jlahoda@3928
   808
jjg@4002
   809
    /** Compile a ClassSymbol from source, optionally using the given compilation unit as
jlahoda@3928
   810
     *  the source tree.
jjg@4002
   811
     *  @param tree the compilation unit in which the given ClassSymbol resides,
jlahoda@3928
   812
     *              or null if should be parsed from source
jlahoda@3928
   813
     *  @param c    the ClassSymbol to complete
jlahoda@3928
   814
     */
jjg@4002
   815
    public void readSourceFile(JCCompilationUnit tree, ClassSymbol c) throws CompletionFailure {
duke@0
   816
        if (completionFailureName == c.fullname) {
duke@0
   817
            throw new CompletionFailure(c, "user-selected completion failure by class name");
duke@0
   818
        }
jlahoda@5832
   819
        JavaFileObject filename = c.classfile;
duke@0
   820
dbalek@5858
   821
        if (tree == null && notYetEntered != null) {
dbalek@5858
   822
            tree = notYetEntered.remove(filename);
dbalek@5858
   823
        }
jlahoda@3928
   824
        if (tree == null) {
dbalek@5862
   825
            JavaFileObject prev = log.useSource(filename);
dbalek@5858
   826
            try {
dbalek@5858
   827
                tree = parse(filename, filename.getCharContent(false));
dbalek@5858
   828
            } catch (IOException e) {
dbalek@5858
   829
                log.error("error.reading.file", filename, JavacFileManager.getMessage(e));
dbalek@5858
   830
                tree = make.TopLevel(List.<JCTree>nil());
dbalek@5858
   831
            } finally {
dbalek@5858
   832
                log.useSource(prev);
jlahoda@5832
   833
            }
duke@0
   834
        }
duke@0
   835
jjg@2050
   836
        if (!taskListener.isEmpty()) {
duke@0
   837
            TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
duke@0
   838
            taskListener.started(e);
duke@0
   839
        }
duke@0
   840
dbalek@5616
   841
        Log.DeferredDiagnosticHandler h = null;
dbalek@3529
   842
        if (!skipAnnotationProcessing && processAnnotations && deferredDiagnosticHandler == null)
dbalek@5616
   843
            deferredDiagnosticHandler = h = new Log.DeferredDiagnosticHandler(log);
dbalek@5616
   844
            
dbalek@3529
   845
alanb@5016
   846
        // Process module declarations.
alanb@5016
   847
        // If module resolution fails, ignore trees, and if trying to
alanb@5016
   848
        // complete a specific symbol, throw CompletionFailure.
alanb@5016
   849
        // Note that if module resolution failed, we may not even
alanb@5016
   850
        // have enough modules available to access java.lang, and
alanb@5016
   851
        // so risk getting FatalError("no.java.lang") from MemberEnter.
jlahoda@5832
   852
        if (!modules.enter(List.of(tree), c)) {
jlahoda@5832
   853
            throw new CompletionFailure(c, diags.fragment("cant.resolve.modules"));
alanb@5016
   854
        }
alanb@5016
   855
jlahoda@5832
   856
        enter.complete(List.of(tree), c);
duke@0
   857
jjg@2050
   858
        if (!taskListener.isEmpty()) {
duke@0
   859
            TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, tree);
duke@0
   860
            taskListener.finished(e);
duke@0
   861
        }
duke@0
   862
jlahoda@5832
   863
        if (enter.getEnv(c) == null) {
duke@0
   864
            boolean isPkgInfo =
duke@0
   865
                tree.sourcefile.isNameCompatible("package-info",
duke@0
   866
                                                 JavaFileObject.Kind.SOURCE);
alanb@5016
   867
            boolean isModuleInfo =
alanb@5016
   868
                tree.sourcefile.isNameCompatible("module-info",
alanb@5016
   869
                                                 JavaFileObject.Kind.SOURCE);
alanb@5016
   870
            if (isModuleInfo) {
alanb@5016
   871
                if (enter.getEnv(tree.modle) == null) {
dbalek@5860
   872
                    if (h != null) {
dbalek@5616
   873
                        log.popDiagnosticHandler(h);
dbalek@5860
   874
                        deferredDiagnosticHandler = null;
dbalek@5860
   875
                    }
alanb@5016
   876
                    JCDiagnostic diag =
alanb@5016
   877
                        diagFactory.fragment("file.does.not.contain.module");
jlahoda@5832
   878
                    throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
alanb@5016
   879
                }
alanb@5016
   880
            } else if (isPkgInfo) {
duke@0
   881
                if (enter.getEnv(tree.packge) == null) {
dbalek@5860
   882
                    if (h != null) {
dbalek@5616
   883
                        log.popDiagnosticHandler(h);
dbalek@5860
   884
                        deferredDiagnosticHandler = null;
dbalek@5860
   885
                    }
jjg@194
   886
                    JCDiagnostic diag =
jjg@194
   887
                        diagFactory.fragment("file.does.not.contain.package",
jlahoda@5832
   888
                                                 c.location());
jlahoda@5832
   889
                    throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
duke@0
   890
                }
duke@0
   891
            } else {
dbalek@5860
   892
                if (h != null) {
dbalek@5616
   893
                    log.popDiagnosticHandler(h);
dbalek@5860
   894
                    deferredDiagnosticHandler = null;
dbalek@5860
   895
                }
jjg@194
   896
                JCDiagnostic diag =
jjg@194
   897
                        diagFactory.fragment("file.doesnt.contain.class",
jlahoda@5832
   898
                                            c.getQualifiedName());
jlahoda@5832
   899
                throw new ClassFinder.BadClassFile(c, filename, diag, diagFactory);
duke@0
   900
            }
duke@0
   901
        }
duke@0
   902
dbalek@5616
   903
        if (!skipAnnotationProcessing && processAnnotations && checkEntered(tree)) {
dbalek@5855
   904
            JCCompilationUnit t = tree;
dbalek@5616
   905
            finder.ap = () -> {
dbalek@5618
   906
                if (annotate.annotationsBlocked() || annotate.isFlushing()) {
dbalek@5855
   907
                    toProcessAnnotations = toProcessAnnotations.prepend(t);
dbalek@5616
   908
                } else {
dbalek@5616
   909
                    skipAnnotationProcessing = true;
dbalek@5616
   910
                    try {
dbalek@5855
   911
                        processAnnotations(List.of(t));
dbalek@5616
   912
                    } finally {
dbalek@5616
   913
                        skipAnnotationProcessing = false;
dbalek@5616
   914
                    }
dbalek@5616
   915
                }
dbalek@5616
   916
            };
dbalek@5616
   917
        }
dbalek@5616
   918
duke@0
   919
        implicitSourceFilesRead = true;
duke@0
   920
    }
dbalek@4765
   921
    
dbalek@4765
   922
    private boolean checkEntered(JCCompilationUnit unit) {
dbalek@4765
   923
        for (JCTree node : unit.defs) {
dbalek@4765
   924
            if (node.hasTag(JCTree.Tag.CLASSDEF)) {
dbalek@4765
   925
                if (((JCClassDecl) node).sym == null) {
dbalek@4765
   926
                    return false;
dbalek@4765
   927
                }
dbalek@4765
   928
            }
dbalek@4765
   929
        }
dbalek@4765
   930
        return true;
dbalek@4765
   931
    }
duke@0
   932
duke@0
   933
    /** Track when the JavaCompiler has been used to compile something. */
duke@0
   934
    private boolean hasBeenUsed = false;
duke@0
   935
    private long start_msec = 0;
duke@0
   936
    public long elapsed_msec = 0;
duke@0
   937
duke@0
   938
    public void compile(List<JavaFileObject> sourceFileObject)
duke@0
   939
        throws Throwable {
jjg@5780
   940
        compile(sourceFileObject, List.nil(), null, List.nil());
duke@0
   941
    }
duke@0
   942
duke@0
   943
    /**
duke@0
   944
     * Main method: compile a list of files, return all compiled classes
duke@0
   945
     *
duke@0
   946
     * @param sourceFileObjects file objects to be compiled
duke@0
   947
     * @param classnames class names to process for annotations
duke@0
   948
     * @param processors user provided annotation processors to bypass
duke@0
   949
     * discovery, {@code null} means that no processors were provided
jjg@5780
   950
     * @param addModules additional root modules to be used during
jjg@5780
   951
     * module resolution.
duke@0
   952
     */
jjg@4174
   953
    public void compile(Collection<JavaFileObject> sourceFileObjects,
jjg@4174
   954
                        Collection<String> classnames,
jjg@5780
   955
                        Iterable<? extends Processor> processors,
jjg@5780
   956
                        Collection<String> addModules)
duke@0
   957
    {
jlahoda@4031
   958
        if (!taskListener.isEmpty()) {
jlahoda@4031
   959
            taskListener.started(new TaskEvent(TaskEvent.Kind.COMPILATION));
jlahoda@4031
   960
        }
jlahoda@4031
   961
duke@0
   962
        if (processors != null && processors.iterator().hasNext())
duke@0
   963
            explicitAnnotationProcessingRequested = true;
duke@0
   964
        // as a JavaCompiler can only be used once, throw an exception if
duke@0
   965
        // it has been used before.
duke@0
   966
        if (hasBeenUsed)
mcimadamore@4596
   967
            checkReusable();
duke@0
   968
        hasBeenUsed = true;
duke@0
   969
jjg@1381
   970
        // forcibly set the equivalent of -Xlint:-options, so that no further
jjg@1381
   971
        // warnings about command line options are generated from this point on
jjg@5307
   972
        options.put(XLINT_CUSTOM.primaryName + "-" + LintCategory.OPTIONS.option, "true");
jjg@5307
   973
        options.remove(XLINT_CUSTOM.primaryName + LintCategory.OPTIONS.option);
jjg@1381
   974
duke@0
   975
        start_msec = now();
jjg@1202
   976
duke@0
   977
        try {
jlahoda@5693
   978
            initProcessAnnotations(processors, sourceFileObjects, classnames);
duke@0
   979
jlahoda@5310
   980
            for (String className : classnames) {
jlahoda@5310
   981
                int sep = className.indexOf('/');
jlahoda@5310
   982
                if (sep != -1) {
jlahoda@5310
   983
                    modules.addExtraAddModules(className.substring(0, sep));
jlahoda@5310
   984
                }
jlahoda@5310
   985
            }
jlahoda@5310
   986
jjg@5780
   987
            for (String moduleName : addModules) {
jjg@5780
   988
                modules.addExtraAddModules(moduleName);
jjg@5780
   989
            }
jjg@5780
   990
duke@0
   991
            // These method calls must be chained to avoid memory leaks
jlahoda@3928
   992
            processAnnotations(
alanb@5016
   993
                enterTrees(
alanb@5016
   994
                        stopIfError(CompileState.PARSE,
alanb@5016
   995
                                initModules(stopIfError(CompileState.PARSE, parseFiles(sourceFileObjects))))
alanb@5016
   996
                ),
alanb@5016
   997
                classnames
alanb@5016
   998
            );
duke@0
   999
alundblad@3868
  1000
            // If it's safe to do so, skip attr / flow / gen for implicit classes
alundblad@3868
  1001
            if (taskListener.isEmpty() &&
alundblad@3868
  1002
                    implicitSourcePolicy == ImplicitSourcePolicy.NONE) {
jlahoda@3928
  1003
                todo.retainFiles(inputFiles);
alundblad@3868
  1004
            }
alundblad@3868
  1005
duke@0
  1006
            switch (compilePolicy) {
duke@0
  1007
            case ATTR_ONLY:
duke@0
  1008
                attribute(todo);
duke@0
  1009
                break;
duke@0
  1010
duke@0
  1011
            case CHECK_ONLY:
duke@0
  1012
                flow(attribute(todo));
duke@0
  1013
                break;
duke@0
  1014
duke@0
  1015
            case SIMPLE:
duke@0
  1016
                generate(desugar(flow(attribute(todo))));
duke@0
  1017
                break;
duke@0
  1018
jjg@297
  1019
            case BY_FILE: {
jjg@297
  1020
                    Queue<Queue<Env<AttrContext>>> q = todo.groupByFile();
jjg@547
  1021
                    while (!q.isEmpty() && !shouldStop(CompileState.ATTR)) {
jjg@297
  1022
                        generate(desugar(flow(attribute(q.remove()))));
jjg@297
  1023
                    }
jjg@297
  1024
                }
duke@0
  1025
                break;
duke@0
  1026
duke@0
  1027
            case BY_TODO:
jjg@297
  1028
                while (!todo.isEmpty())
jjg@297
  1029
                    generate(desugar(flow(attribute(todo.remove()))));
duke@0
  1030
                break;
duke@0
  1031
duke@0
  1032
            default:
jjg@1281
  1033
                Assert.error("unknown compile policy");
duke@0
  1034
            }
duke@0
  1035
        } catch (Abort ex) {
duke@0
  1036
            if (devVerbose)
jjg@1202
  1037
                ex.printStackTrace(System.err);
jlahoda@3928
  1038
        } finally {
jlahoda@3928
  1039
            if (verbose) {
jlahoda@3928
  1040
                elapsed_msec = elapsed(start_msec);
jlahoda@3928
  1041
                log.printVerbose("total", Long.toString(elapsed_msec));
jlahoda@3928
  1042
            }
duke@0
  1043
jlahoda@3928
  1044
            reportDeferredDiagnostics();
duke@0
  1045
jlahoda@3928
  1046
            if (!log.hasDiagnosticListener()) {
jlahoda@3928
  1047
                printCount("error", errorCount());
jlahoda@3928
  1048
                printCount("warn", warningCount());
jlahoda@3928
  1049
            }
jlahoda@4031
  1050
            if (!taskListener.isEmpty()) {
jlahoda@4031
  1051
                taskListener.finished(new TaskEvent(TaskEvent.Kind.COMPILATION));
jlahoda@4031
  1052
            }
jlahoda@3928
  1053
            close();
jlahoda@3928
  1054
            if (procEnvImpl != null)
jlahoda@3928
  1055
                procEnvImpl.close();
duke@0
  1056
        }
duke@0
  1057
    }
duke@0
  1058
mcimadamore@4596
  1059
    protected void checkReusable() {
mcimadamore@4596
  1060
        throw new AssertionError("attempt to reuse JavaCompiler");
mcimadamore@4596
  1061
    }
mcimadamore@4596
  1062
ohrstrom@2301
  1063
    /**
ohrstrom@2301
  1064
     * The list of classes explicitly supplied on the command line for compilation.
ohrstrom@2301
  1065
     * Not always populated.
ohrstrom@2301
  1066
     */
duke@0
  1067
    private List<JCClassDecl> rootClasses;
duke@0
  1068
duke@0
  1069
    /**
duke@0
  1070
     * Parses a list of files.
duke@0
  1071
     */
jjg@1059
  1072
   public List<JCCompilationUnit> parseFiles(Iterable<JavaFileObject> fileObjects) {
jjg@547
  1073
       if (shouldStop(CompileState.PARSE))
duke@0
  1074
           return List.nil();
duke@0
  1075
duke@0
  1076
        //parse all files
alundblad@3063
  1077
        ListBuffer<JCCompilationUnit> trees = new ListBuffer<>();
briangoetz@3808
  1078
        Set<JavaFileObject> filesSoFar = new HashSet<>();
sundar@1085
  1079
        for (JavaFileObject fileObject : fileObjects) {
sundar@1085
  1080
            if (!filesSoFar.contains(fileObject)) {
sundar@1085
  1081
                filesSoFar.add(fileObject);
sundar@1085
  1082
                trees.append(parse(fileObject));
sundar@1085
  1083
            }
sundar@1085
  1084
        }
duke@0
  1085
        return trees.toList();
duke@0
  1086
    }
duke@0
  1087
duke@0
  1088
    /**
jjg@2179
  1089
     * Enter the symbols found in a list of parse trees if the compilation
jjg@2179
  1090
     * is expected to proceed beyond anno processing into attr.
jjg@2179
  1091
     * As a side-effect, this puts elements on the "todo" list.
jjg@2179
  1092
     * Also stores a list of all top level classes in rootClasses.
jjg@2179
  1093
     */
jjg@2179
  1094
    public List<JCCompilationUnit> enterTreesIfNeeded(List<JCCompilationUnit> roots) {
jjg@2179
  1095
       if (shouldStop(CompileState.ATTR))
jjg@2179
  1096
           return List.nil();
alanb@5016
  1097
        return enterTrees(initModules(roots));
alanb@5016
  1098
    }
alanb@5016
  1099
alanb@5016
  1100
    public List<JCCompilationUnit> initModules(List<JCCompilationUnit> roots) {
jlahoda@5310
  1101
        modules.initModules(roots);
alanb@5016
  1102
        if (roots.isEmpty()) {
jlahoda@5675
  1103
            enterDone();
alanb@5016
  1104
        }
alanb@5016
  1105
        return roots;
jjg@2179
  1106
    }
jjg@2179
  1107
jjg@2179
  1108
    /**
duke@0
  1109
     * Enter the symbols found in a list of parse trees.
duke@0
  1110
     * As a side-effect, this puts elements on the "todo" list.
duke@0
  1111
     * Also stores a list of all top level classes in rootClasses.
duke@0
  1112
     */
duke@0
  1113
    public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) {
duke@0
  1114
        //enter symbols for all files
jjg@2050
  1115
        if (!taskListener.isEmpty()) {
duke@0
  1116
            for (JCCompilationUnit unit: roots) {
duke@0
  1117
                TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit);
duke@0
  1118
                taskListener.started(e);
duke@0
  1119
            }
duke@0
  1120
        }
duke@0
  1121
duke@0
  1122
        enter.main(roots);
duke@0
  1123
jlahoda@5675
  1124
        enterDone();
alanb@5016
  1125
jjg@2050
  1126
        if (!taskListener.isEmpty()) {
duke@0
  1127
            for (JCCompilationUnit unit: roots) {
duke@0
  1128
                TaskEvent e = new TaskEvent(TaskEvent.Kind.ENTER, unit);
duke@0
  1129
                taskListener.finished(e);
duke@0
  1130
            }
duke@0
  1131
        }
duke@0
  1132
ohrstrom@2301
  1133
        // If generating source, or if tracking public apis,
ohrstrom@2301
  1134
        // then remember the classes declared in
ohrstrom@2301
  1135
        // the original compilation units listed on the command line.
vromero@4979
  1136
        if (sourceOutput) {
alundblad@3063
  1137
            ListBuffer<JCClassDecl> cdefs = new ListBuffer<>();
duke@0
  1138
            for (JCCompilationUnit unit : roots) {
duke@0
  1139
                for (List<JCTree> defs = unit.defs;
dbalek@791
  1140
                        defs.nonEmpty();
dbalek@791
  1141
                        defs = defs.tail) {
dbalek@791
  1142
                    if (defs.head instanceof JCClassDecl) {
dbalek@791
  1143
                        cdefs.append((JCClassDecl) defs.head);
dbalek@791
  1144
                    }
duke@0
  1145
                }
duke@0
  1146
            }
duke@0
  1147
            rootClasses = cdefs.toList();
duke@0
  1148
        }
jjg@1050
  1149
jjg@1050
  1150
        // Ensure the input files have been recorded. Although this is normally
jjg@1050
  1151
        // done by readSource, it may not have been done if the trees were read
jjg@1050
  1152
        // in a prior round of annotation processing, and the trees have been
jjg@1050
  1153
        // cleaned and are being reused.
jjg@1050
  1154
        for (JCCompilationUnit unit : roots) {
jjg@1050
  1155
            inputFiles.add(unit.sourcefile);
jjg@1050
  1156
        }
jjg@1050
  1157
duke@0
  1158
        return roots;
duke@0
  1159
    }
duke@0
  1160
jlahoda@5
  1161
    public void initNotYetEntered(Map<JavaFileObject, JCCompilationUnit> notYetEntered) {
jlahoda@5
  1162
        this.notYetEntered = notYetEntered;
jlahoda@5
  1163
    }
jlahoda@5
  1164
duke@0
  1165
    /**
duke@0
  1166
     * Set to true to enable skeleton annotation processing code.
duke@0
  1167
     * Currently, we assume this variable will be replaced more
duke@0
  1168
     * advanced logic to figure out if annotation processing is
duke@0
  1169
     * needed.
duke@0
  1170
     */
dbalek@2381
  1171
    public boolean processAnnotations = false;
duke@0
  1172
dbalek@2381
  1173
    public Log.DeferredDiagnosticHandler deferredDiagnosticHandler;
jjg@2249
  1174
duke@0
  1175
    /**
duke@0
  1176
     * Object to handle annotation processing.
duke@0
  1177
     */
jjg@662
  1178
    private JavacProcessingEnvironment procEnvImpl = null;
duke@0
  1179
duke@0
  1180
    /**
duke@0
  1181
     * Check if we should process annotations.
duke@0
  1182
     * If so, and if no scanner is yet registered, then set up the DocCommentScanner
duke@0
  1183
     * to catch doc comments, and set keepComments so the parser records them in
duke@0
  1184
     * the compilation unit.
duke@0
  1185
     *
duke@0
  1186
     * @param processors user provided annotation processors to bypass
duke@0
  1187
     * discovery, {@code null} means that no processors were provided
duke@0
  1188
     */
jlahoda@5693
  1189
    public void initProcessAnnotations(Iterable<? extends Processor> processors,
jlahoda@5693
  1190
                                       Collection<? extends JavaFileObject> initialFiles,
jlahoda@5693
  1191
                                       Collection<String> initialClassNames) {
duke@0
  1192
        // Process annotations if processing is not disabled and there
duke@0
  1193
        // is at least one Processor available.
jjg@1132
  1194
        if (options.isSet(PROC, "none")) {
duke@0
  1195
            processAnnotations = false;
duke@0
  1196
        } else if (procEnvImpl == null) {
jjg@2261
  1197
            procEnvImpl = JavacProcessingEnvironment.instance(context);
jjg@2261
  1198
            procEnvImpl.setProcessors(processors);
duke@0
  1199
            processAnnotations = procEnvImpl.atLeastOneProcessor();
duke@0
  1200
duke@0
  1201
            if (processAnnotations) {
vromero@5238
  1202
                options.put("parameters", "parameters");
duke@0
  1203
                reader.saveParameterNames = true;
duke@0
  1204
                keepComments = true;
jjg@1127
  1205
                genEndPos = true;
jjg@2050
  1206
                if (!taskListener.isEmpty())
duke@0
  1207
                    taskListener.started(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING));
jjg@2249
  1208
                deferredDiagnosticHandler = new Log.DeferredDiagnosticHandler(log);
jlahoda@5693
  1209
                procEnvImpl.getFiler().setInitialState(initialFiles, initialClassNames);
duke@0
  1210
            } else { // free resources
duke@0
  1211
                procEnvImpl.close();
duke@0
  1212
            }
duke@0
  1213
        }
duke@0
  1214
    }
duke@0
  1215
duke@0
  1216
    // TODO: called by JavacTaskImpl
jlahoda@3928
  1217
    public void processAnnotations(List<JCCompilationUnit> roots) {
mcimadamore@5586
  1218
        processAnnotations(roots, List.nil());
duke@0
  1219
    }
duke@0
  1220
duke@0
  1221
    /**
jjg@2050
  1222
     * Process any annotations found in the specified compilation units.
duke@0
  1223
     * @param roots a list of compilation units
duke@0
  1224
     */
jjg@1060
  1225
    // Implementation note: when this method is called, log.deferredDiagnostics
jjg@1060
  1226
    // will have been set true by initProcessAnnotations, meaning that any diagnostics
jjg@1060
  1227
    // that are reported will go into the log.deferredDiagnostics queue.
jjg@1060
  1228
    // By the time this method exits, log.deferDiagnostics must be set back to false,
jjg@1060
  1229
    // and all deferredDiagnostics must have been handled: i.e. either reported
jjg@1060
  1230
    // or determined to be transient, and therefore suppressed.
jlahoda@3928
  1231
    public void processAnnotations(List<JCCompilationUnit> roots,
jjg@4174
  1232
                                   Collection<String> classnames) {
dbalek@4426
  1233
        List<JCCompilationUnit> currentRoots = toProcessAnnotations.prependList(roots);
dbalek@4426
  1234
        toProcessAnnotations = List.nil();
jjg@547
  1235
        if (shouldStop(CompileState.PROCESS)) {
jjg@1048
  1236
            // Errors were encountered.
jjg@1060
  1237
            // Unless all the errors are resolve errors, the errors were parse errors
jjg@1048
  1238
            // or other errors during enter which cannot be fixed by running
jjg@1048
  1239
            // any annotation processors.
jjg@1060
  1240
            if (unrecoverableError()) {
jjg@2249
  1241
                deferredDiagnosticHandler.reportDeferredDiagnostics();
jjg@2249
  1242
                log.popDiagnosticHandler(deferredDiagnosticHandler);
dbalek@3529
  1243
                deferredDiagnosticHandler = null;
jlahoda@3928
  1244
                return ;
jjg@1060
  1245
            }
duke@0
  1246
        }
duke@0
  1247
duke@0
  1248
        // ASSERT: processAnnotations and procEnvImpl should have been set up by
duke@0
  1249
        // by initProcessAnnotations
duke@0
  1250
duke@0
  1251
        // NOTE: The !classnames.isEmpty() checks should be refactored to Main.
duke@0
  1252
duke@0
  1253
        if (!processAnnotations) {
duke@0
  1254
            // If there are no annotation processors present, and
duke@0
  1255
            // annotation processing is to occur with compilation,
duke@0
  1256
            // emit a warning.
jjg@1132
  1257
            if (options.isSet(PROC, "only")) {
duke@0
  1258
                log.warning("proc.proc-only.requested.no.procs");
duke@0
  1259
                todo.clear();
duke@0
  1260
            }
duke@0
  1261
            // If not processing annotations, classnames must be empty
duke@0
  1262
            if (!classnames.isEmpty()) {
duke@0
  1263
                log.error("proc.no.explicit.annotation.processing.requested",
duke@0
  1264
                          classnames);
duke@0
  1265
            }
jjg@2249
  1266
            Assert.checkNull(deferredDiagnosticHandler);
jlahoda@3928
  1267
            return ; // continue regular compilation
duke@0
  1268
        }
duke@0
  1269
jjg@2249
  1270
        Assert.checkNonNull(deferredDiagnosticHandler);
jjg@2249
  1271
duke@0
  1272
        try {
duke@0
  1273
            List<ClassSymbol> classSymbols = List.nil();
duke@0
  1274
            List<PackageSymbol> pckSymbols = List.nil();
duke@0
  1275
            if (!classnames.isEmpty()) {
duke@0
  1276
                 // Check for explicit request for annotation
duke@0
  1277
                 // processing
duke@0
  1278
                if (!explicitAnnotationProcessingRequested()) {
duke@0
  1279
                    log.error("proc.no.explicit.annotation.processing.requested",
duke@0
  1280
                              classnames);
jjg@2249
  1281
                    deferredDiagnosticHandler.reportDeferredDiagnostics();
jjg@2249
  1282
                    log.popDiagnosticHandler(deferredDiagnosticHandler);
dbalek@3529
  1283
                    deferredDiagnosticHandler = null;
jlahoda@3928
  1284
                    return ; // TODO: Will this halt compilation?
duke@0
  1285
                } else {
duke@0
  1286
                    boolean errors = false;
duke@0
  1287
                    for (String nameStr : classnames) {
jjg@1421
  1288
                        Symbol sym = resolveBinaryNameOrIdent(nameStr);
jjh@2042
  1289
                        if (sym == null ||
emc@4248
  1290
                            (sym.kind == PCK && !processPcks) ||
emc@4248
  1291
                            sym.kind == ABSENT_TYP) {
alanb@5016
  1292
                            if (sym != silentFail)
alanb@5016
  1293
                                log.error(Errors.ProcCantFindClass(nameStr));
duke@0
  1294
                            errors = true;
duke@0
  1295
                            continue;
duke@0
  1296
                        }
duke@0
  1297
                        try {
emc@4248
  1298
                            if (sym.kind == PCK)
duke@0
  1299
                                sym.complete();
duke@0
  1300
                            if (sym.exists()) {
emc@4248
  1301
                                if (sym.kind == PCK)
duke@0
  1302
                                    pckSymbols = pckSymbols.prepend((PackageSymbol)sym);
duke@0
  1303
                                else
duke@0
  1304
                                    classSymbols = classSymbols.prepend((ClassSymbol)sym);
duke@0
  1305
                                continue;
duke@0
  1306
                            }
emc@4248
  1307
                            Assert.check(sym.kind == PCK);
alanb@5016
  1308
                            log.warning(Warnings.ProcPackageDoesNotExist(nameStr));
duke@0
  1309
                            pckSymbols = pckSymbols.prepend((PackageSymbol)sym);
duke@0
  1310
                        } catch (CompletionFailure e) {
alanb@5016
  1311
                            log.error(Errors.ProcCantFindClass(nameStr));
duke@0
  1312
                            errors = true;
duke@0
  1313
                            continue;
duke@0
  1314
                        }
duke@0
  1315
                    }
jjg@1060
  1316
                    if (errors) {
jjg@2249
  1317
                        deferredDiagnosticHandler.reportDeferredDiagnostics();
jjg@2249
  1318
                        log.popDiagnosticHandler(deferredDiagnosticHandler);
dbalek@3529
  1319
                        deferredDiagnosticHandler = null;
jlahoda@3928
  1320
                        return ;
jjg@1060
  1321
                    }
duke@0
  1322
                }
duke@0
  1323
            }
dbalek@4426
  1324
            final boolean hasOrigin = currentRoots.nonEmpty();
dbalek@1771
  1325
            if (hasOrigin) {
dbalek@4426
  1326
                fileManager.handleOption("apt-origin", Collections.singleton(currentRoots.head.getSourceFile().toUri().toString()).iterator());    //NOI18N
dbalek@1771
  1327
            }
jjg@662
  1328
            try {
jlahoda@3928
  1329
                annotationProcessingOccurred =
dbalek@4426
  1330
                        procEnvImpl.doProcessing(currentRoots,
jlahoda@3928
  1331
                                                 classSymbols,
jlahoda@3928
  1332
                                                 pckSymbols,
jlahoda@3928
  1333
                                                 deferredDiagnosticHandler);
jjg@1060
  1334
                // doProcessing will have handled deferred diagnostics
jjg@662
  1335
            } finally {
jlahoda@1259
  1336
                if (hasOrigin) {
dbalek@1771
  1337
                    fileManager.handleOption("apt-origin", Collections.singletonList("").iterator());   //NOI18N
jlahoda@1259
  1338
                }
jjg@662
  1339
            }
duke@0
  1340
        } catch (CompletionFailure ex) {
jjg@194
  1341
            log.error("cant.access", ex.sym, ex.getDetailValue());
jlahoda@3928
  1342
            if (deferredDiagnosticHandler != null) {
jlahoda@3928
  1343
                deferredDiagnosticHandler.reportDeferredDiagnostics();
jlahoda@3928
  1344
                log.popDiagnosticHandler(deferredDiagnosticHandler);
dbalek@4427
  1345
                deferredDiagnosticHandler = null;
jlahoda@3928
  1346
            }
jjg@1060
  1347
        }
jjg@1060
  1348
    }
duke@0
  1349
jjg@1060
  1350
    private boolean unrecoverableError() {
jjg@2249
  1351
        if (deferredDiagnosticHandler != null) {
jjg@2249
  1352
            for (JCDiagnostic d: deferredDiagnosticHandler.getDiagnostics()) {
jjg@2249
  1353
                if (d.getKind() == JCDiagnostic.Kind.ERROR && !d.isFlagSet(RECOVERABLE))
jjg@2249
  1354
                    return true;
jjg@2249
  1355
            }
duke@0
  1356
        }
jjg@1060
  1357
        return false;
duke@0
  1358
    }
duke@0
  1359
duke@0
  1360
    boolean explicitAnnotationProcessingRequested() {
duke@0
  1361
        return
duke@0
  1362
            explicitAnnotationProcessingRequested ||
jjg@1386
  1363
            explicitAnnotationProcessingRequested(options);
jjg@1386
  1364
    }
jjg@1386
  1365
jjg@1386
  1366
    static boolean explicitAnnotationProcessingRequested(Options options) {
jjg@1386
  1367
        return
jjg@1132
  1368
            options.isSet(PROCESSOR) ||
jjg@5307
  1369
            options.isSet(PROCESSOR_PATH) ||
jjg@5307
  1370
            options.isSet(PROCESSOR_MODULE_PATH) ||
jjg@1132
  1371
            options.isSet(PROC, "only") ||
jjg@1132
  1372
            options.isSet(XPRINT);
duke@0
  1373
    }
duke@0
  1374
jlahoda@3928
  1375
    public void setDeferredDiagnosticHandler(Log.DeferredDiagnosticHandler deferredDiagnosticHandler) {
jlahoda@3928
  1376
        this.deferredDiagnosticHandler = deferredDiagnosticHandler;
jlahoda@3928
  1377
    }
jlahoda@3928
  1378
duke@0
  1379
    /**
duke@0
  1380
     * Attribute a list of parse trees, such as found on the "todo" list.
duke@0
  1381
     * Note that attributing classes may cause additional files to be
duke@0
  1382
     * parsed and entered via the SourceCompleter.
duke@0
  1383
     * Attribution of the entries in the list does not stop if any errors occur.
avstepan@4558
  1384
     * @return a list of environments for attribute classes.
duke@0
  1385
     */
jjg@251
  1386
    public Queue<Env<AttrContext>> attribute(Queue<Env<AttrContext>> envs) {
alundblad@3063
  1387
        ListBuffer<Env<AttrContext>> results = new ListBuffer<>();
jjg@251
  1388
        while (!envs.isEmpty())
jjg@251
  1389
            results.append(attribute(envs.remove()));
jjg@547
  1390
        return stopIfError(CompileState.ATTR, results);
duke@0
  1391
    }
duke@0
  1392
duke@0
  1393
    /**
duke@0
  1394
     * Attribute a parse tree.
avstepan@4558
  1395
     * @return the attributed parse tree
duke@0
  1396
     */
duke@0
  1397
    public Env<AttrContext> attribute(Env<AttrContext> env) {
jjg@258
  1398
        if (compileStates.isDone(env, CompileState.ATTR))
jjg@258
  1399
            return env;
jjg@258
  1400
duke@0
  1401
        if (verboseCompilePolicy)
jjg@995
  1402
            printNote("[attribute " + env.enclClass.sym + "]");
duke@0
  1403
        if (verbose)
jjg@1393
  1404
            log.printVerbose("checking.attribution", env.enclClass.sym);
duke@0
  1405
jjg@2050
  1406
        if (!taskListener.isEmpty()) {
duke@0
  1407
            TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
duke@0
  1408
            taskListener.started(e);
duke@0
  1409
        }
duke@0
  1410
duke@0
  1411
        JavaFileObject prev = log.useSource(
duke@0
  1412
                                  env.enclClass.sym.sourcefile != null ?
duke@0
  1413
                                  env.enclClass.sym.sourcefile :
duke@0
  1414
                                  env.toplevel.sourcefile);
duke@0
  1415
        try {
jjg@1415
  1416
            attr.attrib(env);
dbalek@2737
  1417
            if (!shouldStop(CompileState.ATTR)) {
mcimadamore@1079
  1418
                //if in fail-over mode, ensure that AST expression nodes
mcimadamore@1079
  1419
                //are correctly initialized (e.g. they have a type/symbol)
mcimadamore@2191
  1420
                attr.postAttr(env.tree);
mcimadamore@1079
  1421
            }
jjg@258
  1422
            compileStates.put(env, CompileState.ATTR);
duke@0
  1423
        }
duke@0
  1424
        finally {
duke@0
  1425
            log.useSource(prev);
duke@0
  1426
        }
duke@0
  1427
duke@0
  1428
        return env;
duke@0
  1429
    }
duke@0
  1430
duke@0
  1431
    /**
duke@0
  1432
     * Perform dataflow checks on attributed parse trees.
duke@0
  1433
     * These include checks for definite assignment and unreachable statements.
duke@0
  1434
     * If any errors occur, an empty list will be returned.
avstepan@4558
  1435
     * @return the list of attributed parse trees
duke@0
  1436
     */
jjg@251
  1437
    public Queue<Env<AttrContext>> flow(Queue<Env<AttrContext>> envs) {
alundblad@3063
  1438
        ListBuffer<Env<AttrContext>> results = new ListBuffer<>();
jjg@251
  1439
        for (Env<AttrContext> env: envs) {
jjg@251
  1440
            flow(env, results);
duke@0
  1441
        }
jjg@547
  1442
        return stopIfError(CompileState.FLOW, results);
duke@0
  1443
    }
duke@0
  1444
duke@0
  1445
    /**
duke@0
  1446
     * Perform dataflow checks on an attributed parse tree.
duke@0
  1447
     */
jjg@251
  1448
    public Queue<Env<AttrContext>> flow(Env<AttrContext> env) {
alundblad@3063
  1449
        ListBuffer<Env<AttrContext>> results = new ListBuffer<>();
duke@0
  1450
        flow(env, results);
jjg@547
  1451
        return stopIfError(CompileState.FLOW, results);
duke@0
  1452
    }
duke@0
  1453
duke@0
  1454
    /**
duke@0
  1455
     * Perform dataflow checks on an attributed parse tree.
duke@0
  1456
     */
jjg@297
  1457
    protected void flow(Env<AttrContext> env, Queue<Env<AttrContext>> results) {
jlahoda@3984
  1458
        if (compileStates.isDone(env, CompileState.FLOW)) {
jlahoda@3984
  1459
            results.add(env);
jlahoda@3984
  1460
            return;
jlahoda@3984
  1461
        }
jlahoda@3984
  1462
duke@0
  1463
        try {
jjg@547
  1464
            if (shouldStop(CompileState.FLOW))
duke@0
  1465
                return;
duke@0
  1466
duke@0
  1467
            if (verboseCompilePolicy)
jjg@547
  1468
                printNote("[flow " + env.enclClass.sym + "]");
duke@0
  1469
            JavaFileObject prev = log.useSource(
duke@0
  1470
                                                env.enclClass.sym.sourcefile != null ?
duke@0
  1471
                                                env.enclClass.sym.sourcefile :
duke@0
  1472
                                                env.toplevel.sourcefile);
duke@0
  1473
            try {
duke@0
  1474
                make.at(Position.FIRSTPOS);
duke@0
  1475
                TreeMaker localMake = make.forToplevel(env.toplevel);
mcimadamore@1015
  1476
                flow.analyzeTree(env, localMake);
jjg@258
  1477
                compileStates.put(env, CompileState.FLOW);
duke@0
  1478
jjg@547
  1479
                if (shouldStop(CompileState.FLOW))
duke@0
  1480
                    return;
duke@0
  1481
jjg@297
  1482
                results.add(env);
duke@0
  1483
            }
duke@0
  1484
            finally {
duke@0
  1485
                log.useSource(prev);
duke@0
  1486
            }
duke@0
  1487
        }
duke@0
  1488
        finally {
jjg@2050
  1489
            if (!taskListener.isEmpty()) {
duke@0
  1490
                TaskEvent e = new TaskEvent(TaskEvent.Kind.ANALYZE, env.toplevel, env.enclClass.sym);
duke@0
  1491
                taskListener.finished(e);
duke@0
  1492
            }
duke@0
  1493
        }
duke@0
  1494
    }
duke@0
  1495
dbalek@72
  1496
    public boolean doRepair = true; // Allows for switching off repair. For test purposes only.
dbalek@72
  1497
duke@0
  1498
    /**
duke@0
  1499
     * Prepare attributed parse trees, in conjunction with their attribution contexts,
duke@0
  1500
     * for source or code generation.
duke@0
  1501
     * If any errors occur, an empty list will be returned.
avstepan@4558
  1502
     * @return a list containing the classes to be generated
duke@0
  1503
     */
jjg@251
  1504
    public Queue<Pair<Env<AttrContext>, JCClassDecl>> desugar(Queue<Env<AttrContext>> envs) {
alundblad@3063
  1505
        ListBuffer<Pair<Env<AttrContext>, JCClassDecl>> results = new ListBuffer<>();
jjg@251
  1506
        for (Env<AttrContext> env: envs)
jjg@251
  1507
            desugar(env, results);
jjg@547
  1508
        return stopIfError(CompileState.FLOW, results);
duke@0
  1509
    }
duke@0
  1510
briangoetz@3808
  1511
    HashMap<Env<AttrContext>, Queue<Pair<Env<AttrContext>, JCClassDecl>>> desugaredEnvs = new HashMap<>();
mcimadamore@649
  1512
duke@0
  1513
    /**
duke@0
  1514
     * Prepare attributed parse trees, in conjunction with their attribution contexts,
duke@0
  1515
     * for source or code generation. If the file was not listed on the command line,
duke@0
  1516
     * the current implicitSourcePolicy is taken into account.
duke@0
  1517
     * The preparation stops as soon as an error is found.
duke@0
  1518
     */
jjg@258
  1519
    protected void desugar(final Env<AttrContext> env, Queue<Pair<Env<AttrContext>, JCClassDecl>> results) {
jjg@547
  1520
        if (shouldStop(CompileState.TRANSTYPES))
duke@0
  1521
            return;
duke@0
  1522
duke@0
  1523
        if (implicitSourcePolicy == ImplicitSourcePolicy.NONE
duke@0
  1524
                && !inputFiles.contains(env.toplevel.sourcefile)) {
duke@0
  1525
            return;
duke@0
  1526
        }
duke@0
  1527
jlahoda@5751
  1528
        if (!modules.multiModuleMode && env.toplevel.modle != modules.getDefaultModule()) {
jlahoda@5751
  1529
            //can only generate classfiles for a single module:
jlahoda@5751
  1530
            return;
jlahoda@5751
  1531
        }
jlahoda@5751
  1532
dbalek@4426
  1533
        if (duplicateClassChecker != null && env.tree.hasTag(JCTree.Tag.CLASSDEF) && duplicateClassChecker.check(((JCClassDecl)env.tree).sym.fullname,
dbalek@2729
  1534
                env.enclClass.sym.sourcefile != null
dbalek@2729
  1535
                ? env.enclClass.sym.sourcefile
dbalek@2729
  1536
                : env.toplevel.sourcefile)) {
dbalek@2729
  1537
            return;
dbalek@2729
  1538
        }
dbalek@2729
  1539
mcimadamore@649
  1540
        if (compileStates.isDone(env, CompileState.LOWER)) {
mcimadamore@649
  1541
            results.addAll(desugaredEnvs.get(env));
mcimadamore@649
  1542
            return;
mcimadamore@649
  1543
        }
mcimadamore@649
  1544
jjg@258
  1545
        /**
mcimadamore@649
  1546
         * Ensure that superclasses of C are desugared before C itself. This is
mcimadamore@649
  1547
         * required for two reasons: (i) as erasure (TransTypes) destroys
mcimadamore@649
  1548
         * information needed in flow analysis and (ii) as some checks carried
mcimadamore@649
  1549
         * out during lowering require that all synthetic fields/methods have
mcimadamore@649
  1550
         * already been added to C and its superclasses.
jjg@258
  1551
         */
jjg@258
  1552
        class ScanNested extends TreeScanner {
briangoetz@3808
  1553
            Set<Env<AttrContext>> dependencies = new LinkedHashSet<>();
ksrini@3190
  1554
            protected boolean hasLambdas;
jjg@547
  1555
            @Override
jjg@258
  1556
            public void visitClassDef(JCClassDecl node) {
dbalek@735
  1557
                if (node.sym != null) {
dbalek@735
  1558
                    Type st = types.supertype(node.sym.type);
dbalek@2633
  1559
                    boolean envForSuperTypeFound = false;
dbalek@2633
  1560
                    while (!envForSuperTypeFound && st != null && st.hasTag(CLASS)) {
dbalek@735
  1561
                        ClassSymbol c = st.tsym.outermostClass();
dbalek@735
  1562
                        Env<AttrContext> stEnv = enter.getEnv(c);
dbalek@735
  1563
                        if (stEnv != null && env != stEnv) {
dbalek@2633
  1564
                            if (dependencies.add(stEnv)) {
dbalek@3201
  1565
                                boolean prevHasLambdas = hasLambdas;
dbalek@3201
  1566
                                try {
dbalek@3201
  1567
                                    scan(stEnv.tree);
dbalek@3201
  1568
                                } finally {
dbalek@3201
  1569
                                    /*
dbalek@3201
  1570
                                     * ignore any updates to hasLambdas made during
dbalek@3201
  1571
                                     * the nested scan, this ensures an initalized
dbalek@3201
  1572
                                     * LambdaToMethod is available only to those
dbalek@3201
  1573
                                     * classes that contain lambdas
dbalek@3201
  1574
                                     */
dbalek@3201
  1575
                                    hasLambdas = prevHasLambdas;
dbalek@3201
  1576
                                }
ksrini@3190
  1577
                            }
dbalek@2633
  1578
                            envForSuperTypeFound = true;
vromero@2619
  1579
                        }
dbalek@2633
  1580
                        st = types.supertype(st);
mcimadamore@273
  1581
                    }
jjg@258
  1582
                }
jjg@258
  1583
                super.visitClassDef(node);
jjg@258
  1584
            }
ksrini@3190
  1585
            @Override
ksrini@3190
  1586
            public void visitLambda(JCLambda tree) {
ksrini@3190
  1587
                hasLambdas = true;
ksrini@3190
  1588
                super.visitLambda(tree);
ksrini@3190
  1589
            }
ksrini@3190
  1590
            @Override
ksrini@3190
  1591
            public void visitReference(JCMemberReference tree) {
ksrini@3190
  1592
                hasLambdas = true;
ksrini@3190
  1593
                super.visitReference(tree);
ksrini@3190
  1594
            }
duke@0
  1595
        }
jjg@258
  1596
        ScanNested scanner = new ScanNested();
jjg@258
  1597
        scanner.scan(env.tree);
jjg@258
  1598
        for (Env<AttrContext> dep: scanner.dependencies) {
mcimadamore@649
  1599
        if (!compileStates.isDone(dep, CompileState.FLOW))
mcimadamore@649
  1600
            desugaredEnvs.put(dep, desugar(flow(attribute(dep))));
jjg@258
  1601
        }
duke@0
  1602
mcimadamore@273
  1603
        //We need to check for error another time as more classes might
mcimadamore@273
  1604
        //have been attributed and analyzed at this stage
jjg@547
  1605
        if (shouldStop(CompileState.TRANSTYPES))
mcimadamore@273
  1606
            return;
mcimadamore@273
  1607
duke@0
  1608
        if (verboseCompilePolicy)
jjg@547
  1609
            printNote("[desugar " + env.enclClass.sym + "]");
duke@0
  1610
duke@0
  1611
        JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
duke@0
  1612
                                  env.enclClass.sym.sourcefile :
duke@0
  1613
                                  env.toplevel.sourcefile);
duke@0
  1614
        try {
duke@0
  1615
            //save tree prior to rewriting
duke@0
  1616
            JCTree untranslated = env.tree;
duke@0
  1617
duke@0
  1618
            make.at(Position.FIRSTPOS);
duke@0
  1619
            TreeMaker localMake = make.forToplevel(env.toplevel);
duke@0
  1620
alanb@5016
  1621
            if (env.tree.hasTag(JCTree.Tag.PACKAGEDEF) || env.tree.hasTag(JCTree.Tag.MODULEDEF)) {
vromero@4979
  1622
                if (!(sourceOutput)) {
jjg@547
  1623
                    if (shouldStop(CompileState.LOWER))
jjg@547
  1624
                        return;
alanb@5016
  1625
                    List<JCTree> def = lower.translateTopLevelClass(env, env.tree, localMake);
alanb@5016
  1626
                    if (def.head != null) {
alanb@5016
  1627
                        Assert.check(def.tail.isEmpty());
alanb@5016
  1628
                        results.add(new Pair<>(env, (JCClassDecl)def.head));
duke@0
  1629
                    }
duke@0
  1630
                }
duke@0
  1631
                return;
duke@0
  1632
            }
duke@0
  1633
dbalek@72
  1634
            if (doRepair)
dbalek@72
  1635
                env.tree = repair.translateTopLevelClass(env, env.tree, localMake);
duke@0
  1636
jjg@547
  1637
            if (shouldStop(CompileState.TRANSTYPES))
jjg@547
  1638
                return;
jjg@547
  1639
duke@0
  1640
            env.tree = transTypes.translateTopLevelClass(env.tree, localMake);
mcimadamore@649
  1641
            compileStates.put(env, CompileState.TRANSTYPES);
duke@0
  1642
ksrini@3190
  1643
            if (source.allowLambda() && scanner.hasLambdas) {
vromero@2809
  1644
                if (shouldStop(CompileState.UNLAMBDA))
vromero@2809
  1645
                    return;
rfield@2219
  1646
ksrini@3190
  1647
                env.tree = LambdaToMethod.instance(context).translateTopLevelClass(env, env.tree, localMake);
vromero@2809
  1648
                compileStates.put(env, CompileState.UNLAMBDA);
vromero@2809
  1649
            }
rfield@2219
  1650
jjg@547
  1651
            if (shouldStop(CompileState.LOWER))
duke@0
  1652
                return;
duke@0
  1653
duke@0
  1654
            if (sourceOutput) {
duke@0
  1655
                //emit standard Java source file, only for compilation
duke@0
  1656
                //units enumerated explicitly on the command line
duke@0
  1657
                JCClassDecl cdef = (JCClassDecl)env.tree;
duke@0
  1658
                if (untranslated instanceof JCClassDecl &&
duke@0
  1659
                    rootClasses.contains((JCClassDecl)untranslated)) {
briangoetz@3808
  1660
                    results.add(new Pair<>(env, cdef));
duke@0
  1661
                }
duke@0
  1662
                return;
duke@0
  1663
            }
duke@0
  1664
duke@0
  1665
            //translate out inner classes
duke@0
  1666
            List<JCTree> cdefs = lower.translateTopLevelClass(env, env.tree, localMake);
mcimadamore@649
  1667
            compileStates.put(env, CompileState.LOWER);
duke@0
  1668
jjg@547
  1669
            if (shouldStop(CompileState.LOWER))
duke@0
  1670
                return;
duke@0
  1671
duke@0
  1672
            //generate code for each class
duke@0
  1673
            for (List<JCTree> l = cdefs; l.nonEmpty(); l = l.tail) {
duke@0
  1674
                JCClassDecl cdef = (JCClassDecl)l.head;
briangoetz@3808
  1675
                results.add(new Pair<>(env, cdef));
duke@0
  1676
            }
duke@0
  1677
        }
duke@0
  1678
        finally {
duke@0
  1679
            log.useSource(prev);
duke@0
  1680
        }
duke@0
  1681
duke@0
  1682
    }
duke@0
  1683
duke@0
  1684
    /** Generates the source or class file for a list of classes.
duke@0
  1685
     * The decision to generate a source file or a class file is
duke@0
  1686
     * based upon the compiler's options.
duke@0
  1687
     * Generation stops if an error occurs while writing files.
duke@0
  1688
     */
jjg@251
  1689
    public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue) {
jjg@251
  1690
        generate(queue, null);
duke@0
  1691
    }
duke@0
  1692
jjg@297
  1693
    public void generate(Queue<Pair<Env<AttrContext>, JCClassDecl>> queue, Queue<JavaFileObject> results) {
jjg@547
  1694
        if (shouldStop(CompileState.GENERATE))
jjg@547
  1695
            return;
jjg@547
  1696
jjg@251
  1697
        for (Pair<Env<AttrContext>, JCClassDecl> x: queue) {
duke@0
  1698
            Env<AttrContext> env = x.fst;
duke@0
  1699
            JCClassDecl cdef = x.snd;
duke@0
  1700
duke@0
  1701
            if (verboseCompilePolicy) {
vromero@4979
  1702
                printNote("[generate " + (sourceOutput ? " source" : "code") + " " + cdef.sym + "]");
duke@0
  1703
            }
duke@0
  1704
jjg@2050
  1705
            if (!taskListener.isEmpty()) {
duke@0
  1706
                TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
duke@0
  1707
                taskListener.started(e);
duke@0
  1708
            }
duke@0
  1709
duke@0
  1710
            JavaFileObject prev = log.useSource(env.enclClass.sym.sourcefile != null ?
duke@0
  1711
                                      env.enclClass.sym.sourcefile :
duke@0
  1712
                                      env.toplevel.sourcefile);
duke@0
  1713
            try {
dbalek@2718
  1714
                JavaFileObject file = null;
vromero@4979
  1715
                if (sourceOutput) {
duke@0
  1716
                    file = printSource(env, cdef);
vromero@4979
  1717
                } else {
dbalek@3089
  1718
                    try {
dbalek@3089
  1719
                        writeHeader(cdef);
dbalek@3089
  1720
                    } catch (LinkageError ex) {
dbalek@3089
  1721
                        log.error(cdef.pos(), "class.cant.write",
dbalek@3089
  1722
                                  cdef.sym, ex.getMessage());
jjg@2072
  1723
                    }
duke@0
  1724
                    file = genCode(env, cdef);
jjg@2072
  1725
                }
duke@0
  1726
                if (results != null && file != null)
jjg@297
  1727
                    results.add(file);
duke@0
  1728
            } catch (IOException ex) {
duke@0
  1729
                log.error(cdef.pos(), "class.cant.write",
duke@0
  1730
                          cdef.sym, ex.getMessage());
duke@0
  1731
                return;
duke@0
  1732
            } finally {
duke@0
  1733
                log.useSource(prev);
duke@0
  1734
            }
duke@0
  1735
jjg@2050
  1736
            if (!taskListener.isEmpty()) {
duke@0
  1737
                TaskEvent e = new TaskEvent(TaskEvent.Kind.GENERATE, env.toplevel, cdef.sym);
duke@0
  1738
                taskListener.finished(e);
duke@0
  1739
            }
duke@0
  1740
        }
duke@0
  1741
    }
dbalek@3089
  1742
    
dbalek@3089
  1743
    private void writeHeader(JCClassDecl cdef) throws IOException {
dbalek@3089
  1744
        if (fileManager.hasLocation(StandardLocation.NATIVE_HEADER_OUTPUT)
dbalek@3089
  1745
                && jniWriter.needsHeader(cdef.sym)) {
dbalek@3089
  1746
            jniWriter.write(cdef.sym);
dbalek@3089
  1747
        }
dbalek@3089
  1748
    }
duke@0
  1749
duke@0
  1750
        // where
jjg@251
  1751
        Map<JCCompilationUnit, Queue<Env<AttrContext>>> groupByFile(Queue<Env<AttrContext>> envs) {
duke@0
  1752
            // use a LinkedHashMap to preserve the order of the original list as much as possible
briangoetz@3808
  1753
            Map<JCCompilationUnit, Queue<Env<AttrContext>>> map = new LinkedHashMap<>();
jjg@251
  1754
            for (Env<AttrContext> env: envs) {
jjg@251
  1755
                Queue<Env<AttrContext>> sublist = map.get(env.toplevel);
jjg@251
  1756
                if (sublist == null) {
briangoetz@3808
  1757
                    sublist = new ListBuffer<>();
jjg@251
  1758
                    map.put(env.toplevel, sublist);
duke@0
  1759
                }
jjg@251
  1760
                sublist.add(env);
duke@0
  1761
            }
duke@0
  1762
            return map;
duke@0
  1763
        }
duke@0
  1764
duke@0
  1765
        JCClassDecl removeMethodBodies(JCClassDecl cdef) {
duke@0
  1766
            final boolean isInterface = (cdef.mods.flags & Flags.INTERFACE) != 0;
duke@0
  1767
            class MethodBodyRemover extends TreeTranslator {
jjg@547
  1768
                @Override
duke@0
  1769
                public void visitMethodDef(JCMethodDecl tree) {
duke@0
  1770
                    tree.mods.flags &= ~Flags.SYNCHRONIZED;
duke@0
  1771
                    for (JCVariableDecl vd : tree.params)
duke@0
  1772
                        vd.mods.flags &= ~Flags.FINAL;
duke@0
  1773
                    tree.body = null;
duke@0
  1774
                    super.visitMethodDef(tree);
duke@0
  1775
                }
jjg@547
  1776
                @Override
duke@0
  1777
                public void visitVarDef(JCVariableDecl tree) {
duke@0
  1778
                    if (tree.init != null && tree.init.type.constValue() == null)
duke@0
  1779
                        tree.init = null;
duke@0
  1780
                    super.visitVarDef(tree);
duke@0
  1781
                }
jjg@547
  1782
                @Override
duke@0
  1783
                public void visitClassDef(JCClassDecl tree) {
alundblad@3063
  1784
                    ListBuffer<JCTree> newdefs = new ListBuffer<>();
duke@0
  1785
                    for (List<JCTree> it = tree.defs; it.tail != null; it = it.tail) {
duke@0
  1786
                        JCTree t = it.head;
duke@0
  1787
                        switch (t.getTag()) {
jjg@1972
  1788
                        case CLASSDEF:
duke@0
  1789
                            if (isInterface ||
duke@0
  1790
                                (((JCClassDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
duke@0
  1791
                                (((JCClassDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCClassDecl) t).sym.packge().getQualifiedName() == names.java_lang)
duke@0
  1792
                                newdefs.append(t);
duke@0
  1793
                            break;
jjg@1972
  1794
                        case METHODDEF:
duke@0
  1795
                            if (isInterface ||
duke@0
  1796
                                (((JCMethodDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
duke@0
  1797
                                ((JCMethodDecl) t).sym.name == names.init ||
duke@0
  1798
                                (((JCMethodDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCMethodDecl) t).sym.packge().getQualifiedName() == names.java_lang)
duke@0
  1799
                                newdefs.append(t);
duke@0
  1800
                            break;
jjg@1972
  1801
                        case VARDEF:
duke@0
  1802
                            if (isInterface || (((JCVariableDecl) t).mods.flags & (Flags.PROTECTED|Flags.PUBLIC)) != 0 ||
duke@0
  1803
                                (((JCVariableDecl) t).mods.flags & (Flags.PRIVATE)) == 0 && ((JCVariableDecl) t).sym.packge().getQualifiedName() == names.java_lang)
duke@0
  1804
                                newdefs.append(t);
duke@0
  1805
                            break;
duke@0
  1806
                        default:
duke@0
  1807
                            break;
duke@0
  1808
                        }
duke@0
  1809
                    }
duke@0
  1810
                    tree.defs = newdefs.toList();
duke@0
  1811
                    super.visitClassDef(tree);
duke@0
  1812
                }
duke@0
  1813
            }
duke@0
  1814
            MethodBodyRemover r = new MethodBodyRemover();
duke@0
  1815
            return r.translate(cdef);
duke@0
  1816
        }
duke@0
  1817
duke@0
  1818
    public void reportDeferredDiagnostics() {
jjg@1387
  1819
        if (errorCount() == 0
jjg@1387
  1820
                && annotationProcessingOccurred
duke@0
  1821
                && implicitSourceFilesRead
duke@0
  1822
                && implicitSourcePolicy == ImplicitSourcePolicy.UNSET) {
duke@0
  1823
            if (explicitAnnotationProcessingRequested())
duke@0
  1824
                log.warning("proc.use.implicit");
duke@0
  1825
            else
duke@0
  1826
                log.warning("proc.use.proc.or.implicit");
duke@0
  1827
        }
duke@0
  1828
        chk.reportDeferredDiagnostics();
mcimadamore@2708
  1829
        if (log.compressedOutput) {
mcimadamore@2708
  1830
            log.mandatoryNote(null, "compressed.diags");
mcimadamore@2708
  1831
        }
duke@0
  1832
    }
duke@0
  1833
jlahoda@5675
  1834
    public void enterDone() {
jlahoda@5675
  1835
        enterDone = true;
jlahoda@5675
  1836
        annotate.enterDone();
jlahoda@5675
  1837
    }
jlahoda@5675
  1838
alanb@5016
  1839
    public boolean isEnterDone() {
alanb@5016
  1840
        return enterDone;
alanb@5016
  1841
    }
alanb@5016
  1842
jlahoda@5832
  1843
    private Name readModuleName(JavaFileObject fo) {
jlahoda@5832
  1844
        return parseAndGetName(fo, t -> {
jlahoda@5832
  1845
            JCModuleDecl md = t.getModuleDecl();
jlahoda@5832
  1846
jlahoda@5832
  1847
            return md != null ? TreeInfo.fullName(md.getName()) : null;
jlahoda@5832
  1848
        });
jlahoda@5832
  1849
    }
jlahoda@5832
  1850
jlahoda@5833
  1851
    private Name findPackageInFile(JavaFileObject fo) {
jlahoda@5833
  1852
        return parseAndGetName(fo, t -> t.getPackage() != null ?
jlahoda@5833
  1853
                                        TreeInfo.fullName(t.getPackage().getPackageName()) : null);
jlahoda@5833
  1854
    }
jlahoda@5833
  1855
jlahoda@5832
  1856
    private Name parseAndGetName(JavaFileObject fo,
jlahoda@5832
  1857
                                 Function<JCTree.JCCompilationUnit, Name> tree2Name) {
jlahoda@5832
  1858
        DiagnosticHandler dh = new DiscardDiagnosticHandler(log);
jlahoda@5832
  1859
        try {
jlahoda@5832
  1860
            JCTree.JCCompilationUnit t = parse(fo, fo.getCharContent(false));
jlahoda@5832
  1861
            return tree2Name.apply(t);
jlahoda@5832
  1862
        } catch (IOException e) {
jlahoda@5832
  1863
            return null;
jlahoda@5832
  1864
        } finally {
jlahoda@5832
  1865
            log.popDiagnosticHandler(dh);
jlahoda@5832
  1866
        }
jlahoda@5832
  1867
    }
jlahoda@5832
  1868
dbalek@5828
  1869
    public void resetEnterDone() {
dbalek@5828
  1870
        enterDone = false;
dbalek@5828
  1871
    }
dbalek@5828
  1872
duke@0
  1873
    /** Close the compiler, flushing the logs
duke@0
  1874
     */
duke@0
  1875
    public void close() {
duke@0
  1876
        rootClasses = null;
jjg@4002
  1877
        finder = null;
duke@0
  1878
        reader = null;
duke@0
  1879
        make = null;
duke@0
  1880
        writer = null;
duke@0
  1881
        enter = null;
duke@0
  1882
        if (todo != null)
duke@0
  1883
            todo.clear();
duke@0
  1884
        todo = null;
duke@0
  1885
        parserFactory = null;
duke@0
  1886
        syms = null;
duke@0
  1887
        source = null;
duke@0
  1888
        attr = null;
duke@0
  1889
        chk = null;
duke@0
  1890
        gen = null;
duke@0
  1891
        flow = null;
duke@0
  1892
        transTypes = null;
duke@0
  1893
        lower = null;
duke@0
  1894
        annotate = null;
duke@0
  1895
        types = null;
duke@0
  1896
duke@0
  1897
        log.flush();
duke@0
  1898
        try {
duke@0
  1899
            fileManager.flush();
dbalek@731
  1900
            if (procEnvImpl != null)
dbalek@1771
  1901
                procEnvImpl.close();
dbalek@731
  1902
            procEnvImpl = null;
duke@0
  1903
        } catch (IOException e) {
duke@0
  1904
            throw new Abort(e);
duke@0
  1905
        } finally {
jlahoda@3928
  1906
            if (names != null)
duke@0
  1907
                names.dispose();
duke@0
  1908
            names = null;
jjg@1938
  1909
jjg@1938
  1910
            for (Closeable c: closeables) {
jjg@1938
  1911
                try {
jjg@1938
  1912
                    c.close();
jjg@1938
  1913
                } catch (IOException e) {
jjg@1938
  1914
                    // When javac uses JDK 7 as a baseline, this code would be
jjg@1938
  1915
                    // better written to set any/all exceptions from all the
jjg@1938
  1916
                    // Closeables as suppressed exceptions on the FatalError
jjg@1938
  1917
                    // that is thrown.
jjg@1938
  1918
                    JCDiagnostic msg = diagFactory.fragment("fatal.err.cant.close");
jjg@1938
  1919
                    throw new FatalError(msg, e);
jjg@1938
  1920
                }
jjg@1938
  1921
            }
jjg@2677
  1922
            closeables = List.nil();
duke@0
  1923
        }
duke@0
  1924
    }
duke@0
  1925
jjg@547
  1926
    protected void printNote(String lines) {
jjg@1974
  1927
        log.printRawLines(Log.WriterKind.NOTICE, lines);
jjg@547
  1928
    }
jjg@547
  1929
duke@0
  1930
    /** Print numbers of errors and warnings.
duke@0
  1931
     */
jjg@2296
  1932
    public void printCount(String kind, int count) {
duke@0
  1933
        if (count != 0) {
jjg@995
  1934
            String key;
duke@0
  1935
            if (count == 1)
jjg@995
  1936
                key = "count." + kind;
duke@0
  1937
            else
jjg@995
  1938
                key = "count." + kind + ".plural";
jjg@1974
  1939
            log.printLines(WriterKind.ERROR, key, String.valueOf(count));
jjg@1973
  1940
            log.flush(Log.WriterKind.ERROR);
duke@0
  1941
        }
duke@0
  1942
    }
duke@0
  1943
duke@0
  1944
    private static long now() {
duke@0
  1945
        return System.currentTimeMillis();
duke@0
  1946
    }
duke@0
  1947
duke@0
  1948
    private static long elapsed(long then) {
duke@0
  1949
        return now() - then;
duke@0
  1950
    }
duke@0
  1951
tzezula@5426
  1952
    public void newRound(final Set<? extends JCCompilationUnit> treesToClean) {
tzezula@5426
  1953
        for (JCCompilationUnit treeToClean : treesToClean) {
tzezula@5426
  1954
            if (treeToClean.sourcefile != null) {
tzezula@5426
  1955
                inputFiles.remove(treeToClean.sourcefile);
tzezula@5426
  1956
            }
tzezula@5426
  1957
        }
tzezula@5426
  1958
        for (Iterator<Env<AttrContext>> it = todo.iterator(); it.hasNext();) {
tzezula@5426
  1959
            final Env<AttrContext> env = it.next();
tzezula@5426
  1960
            if (treesToClean.contains(env.toplevel)) {
tzezula@5426
  1961
                it.remove();
tzezula@5426
  1962
            }
tzezula@5426
  1963
        }
duke@0
  1964
    }
duke@0
  1965
}