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