langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java
author mcimadamore
Wed, 27 Jul 2011 19:01:33 +0100
changeset 10199 48bd09ecc88a
parent 9300 c2de4dd9853b
child 10200 56cc93e7b6ef
permissions -rw-r--r--
7057297: Project Coin: diamond erroneously accepts in array initializer expressions Summary: Diamond in array initializer expressions should be rejected Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7636
diff changeset
     2
 * Copyright (c) 1999, 2011, 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: 5492
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: 5492
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: 5492
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
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.parser;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.tree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.*;
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
    33
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import static com.sun.tools.javac.util.ListBuffer.lb;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import static com.sun.tools.javac.parser.Token.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
/** The parser maps a token sequence into an abstract syntax
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *  tree. It operates by recursive descent, with code derived
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *  systematically from an LL(1) grammar. For efficiency reasons, an
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *  operator precedence scheme is used for parsing binary operation
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *  expressions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    47
 *  <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
    48
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
    52
public class JavacParser implements Parser {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    /** The number of precedence levels of infix operators.
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    private static final int infixPrecedenceLevels = 10;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    /** The scanner used for lexical analysis.
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private Lexer S;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /** The factory to be used for abstract syntax tree construction.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    protected TreeMaker F;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    /** The log to be used for error diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    private Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    /** The keyword table. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    private Keywords keywords;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    /** The Source language setting. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    private Source source;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    /** The name table. */
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1258
diff changeset
    77
    private Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    /** Construct a parser from a given scanner, tree factory and log.
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
    81
    protected JavacParser(ParserFactory fac,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
                     Lexer S,
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
    83
                     boolean keepDocComments,
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
    84
                     boolean keepLineMap) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        this.S = S;
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        S.nextToken(); // prime the pump
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        this.F = fac.F;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        this.log = fac.log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        this.names = fac.names;
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        this.keywords = fac.keywords;
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        this.source = fac.source;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        this.allowGenerics = source.allowGenerics();
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        this.allowVarargs = source.allowVarargs();
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        this.allowAsserts = source.allowAsserts();
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        this.allowEnums = source.allowEnums();
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        this.allowForeach = source.allowForeach();
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        this.allowStaticImport = source.allowStaticImport();
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        this.allowAnnotations = source.allowAnnotations();
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
    99
        this.allowTWR = source.allowTryWithResources();
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   100
        this.allowDiamond = source.allowDiamond();
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   101
        this.allowMulticatch = source.allowMulticatch();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        this.keepDocComments = keepDocComments;
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   103
        if (keepDocComments)
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   104
            docComments = new HashMap<JCTree,String>();
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   105
        this.keepLineMap = keepLineMap;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        this.errorTree = F.Erroneous();
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    /** Switch: Should generics be recognized?
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    boolean allowGenerics;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   113
    /** Switch: Should diamond operator be recognized?
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   114
     */
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   115
    boolean allowDiamond;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   116
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   117
    /** Switch: Should multicatch clause be accepted?
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   118
     */
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   119
    boolean allowMulticatch;
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   120
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    /** Switch: Should varargs be recognized?
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    boolean allowVarargs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    /** Switch: should we recognize assert statements, or just give a warning?
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    boolean allowAsserts;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    /** Switch: should we recognize enums, or just give a warning?
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    boolean allowEnums;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    /** Switch: should we recognize foreach?
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    boolean allowForeach;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    /** Switch: should we recognize foreach?
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    boolean allowStaticImport;
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    /** Switch: should we recognize annotations?
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    boolean allowAnnotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
8224
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
   145
    /** Switch: should we recognize try-with-resources?
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
   146
     */
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
   147
    boolean allowTWR;
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
   148
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    /** Switch: should we keep docComments?
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    boolean keepDocComments;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   153
    /** Switch: should we keep line table?
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   154
     */
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   155
    boolean keepLineMap;
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   156
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    /** When terms are parsed, the mode determines which is expected:
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     *     mode = EXPR        : an expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     *     mode = TYPE        : a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     *     mode = NOPARAMS    : no parameters allowed for type
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     *     mode = TYPEARG     : type argument
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     */
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   163
    static final int EXPR = 0x1;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   164
    static final int TYPE = 0x2;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   165
    static final int NOPARAMS = 0x4;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   166
    static final int TYPEARG = 0x8;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   167
    static final int DIAMOND = 0x10;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    /** The current mode.
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    private int mode = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    /** The mode of the term that was parsed last.
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    private int lastmode = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
/* ---------- error recovery -------------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    private JCErroneous errorTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    /** Skip forward until a suitable stop token is found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    private void skip(boolean stopAtImport, boolean stopAtMemberDecl, boolean stopAtIdentifier, boolean stopAtStatement) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
         while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
             switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                case SEMI:
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                    S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
                case PUBLIC:
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                case FINAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
                case ABSTRACT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                case MONKEYS_AT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                case EOF:
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                case INTERFACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                case IMPORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                    if (stopAtImport)
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                case LBRACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                case RBRACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
                case PRIVATE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                case PROTECTED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
                case STATIC:
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                case TRANSIENT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                case NATIVE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                case VOLATILE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                case SYNCHRONIZED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                case STRICTFP:
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                case LT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
                case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
                case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
                case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
                case INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
                case LONG:
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
                case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
                case DOUBLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
                case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
                case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
                    if (stopAtMemberDecl)
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
                case IDENTIFIER:
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
                   if (stopAtIdentifier)
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
                case CASE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
                case DEFAULT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
                case IF:
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
                case FOR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                case WHILE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
                case DO:
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
                case TRY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                case SWITCH:
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                case RETURN:
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
                case THROW:
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
                case BREAK:
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
                case CONTINUE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
                case ELSE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
                case FINALLY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                case CATCH:
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
                    if (stopAtStatement)
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   252
    private JCErroneous syntaxError(int pos, String key, Token... args) {
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   253
        return syntaxError(pos, null, key, args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   256
    private JCErroneous syntaxError(int pos, List<JCTree> errs, String key, Token... args) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        setErrorEndPos(pos);
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   258
        reportSyntaxError(pos, key, (Object[])args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        return toP(F.at(pos).Erroneous(errs));
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    private int errorPos = Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     * Report a syntax error at given position using the given
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     * argument unless one was already reported at the same position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     */
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   267
    private void reportSyntaxError(int pos, String key, Object... args) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        if (pos > S.errPos() || pos == Position.NOPOS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            if (S.token() == EOF)
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   270
                error(pos, "premature.eof");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            else
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   272
                error(pos, key, args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
        S.errPos(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        if (S.pos() == errorPos)
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            S.nextToken(); // guarantee progress
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
        errorPos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    /** Generate a syntax error at current position unless one was already
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
     *  reported at the same position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    private JCErroneous syntaxError(String key) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        return syntaxError(S.pos(), key);
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    /** Generate a syntax error at current position unless one was
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     *  already reported at the same position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     */
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   291
    private JCErroneous syntaxError(String key, Token arg) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        return syntaxError(S.pos(), key, arg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    /** If next input token matches given token, skip it, otherwise report
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
     *  an error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
    public void accept(Token token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
        if (S.token() == token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            setErrorEndPos(S.pos());
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   303
            reportSyntaxError(S.prevEndPos(), "expected", token);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
    /** Report an illegal start of expression/type error at given position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
    JCExpression illegal(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
        setErrorEndPos(S.pos());
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
        if ((mode & EXPR) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            return syntaxError(pos, "illegal.start.of.expr");
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            return syntaxError(pos, "illegal.start.of.type");
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
    /** Report an illegal start of expression/type error at current position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
    JCExpression illegal() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        return illegal(S.pos());
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
    /** Diagnose a modifier flag from the set, if any. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
    void checkNoMods(long mods) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        if (mods != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
            long lowestMod = mods & -mods;
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   328
            error(S.pos(), "mod.not.allowed.here",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   329
                      Flags.asFlagSet(lowestMod));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
/* ---------- doc comments --------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
    /** A hashtable to store all documentation comments
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
     *  indexed by the tree nodes they refer to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
     *  defined only if option flag keepDocComment is set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
    Map<JCTree, String> docComments;
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
    /** Make an entry into docComments hashtable,
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
     *  provided flag keepDocComments is set and given doc comment is non-null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
     *  @param tree   The tree to be used as index in the hashtable
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
     *  @param dc     The doc comment to associate with the tree, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
    void attach(JCTree tree, String dc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        if (keepDocComments && dc != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
//          System.out.println("doc comment = ");System.out.println(dc);//DEBUG
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
            docComments.put(tree, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
/* -------- source positions ------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
    private int errorEndPos = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
    private void setErrorEndPos(int errPos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        if (errPos > errorEndPos)
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
            errorEndPos = errPos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    protected int getErrorEndPos() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
        return errorEndPos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
     * Store ending position for a tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
     * @param tree   The tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
     * @param endpos The ending position to associate with the tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
    protected void storeEnd(JCTree tree, int endpos) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
     * Store ending position for a tree.  The ending position should
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
     * be the ending position of the current token.
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
     * @param t The tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
    protected <T extends JCTree> T to(T t) { return t; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
     * Store ending position for a tree.  The ending position should
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
     * be greater of the ending position of the previous token and errorEndPos.
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
     * @param t The tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
    protected <T extends JCTree> T toP(T t) { return t; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
    /** Get the start position for a tree node.  The start position is
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
     * defined to be the position of the first character of the first
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
     * token of the node's source text.
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
     * @param tree  The tree node
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
    public int getStartPos(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        return TreeInfo.getStartPos(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
     * Get the end position for a tree node.  The end position is
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
     * defined to be the position of the last character of the last
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
     * token of the node's source text.  Returns Position.NOPOS if end
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
     * positions are not generated or the position is otherwise not
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
     * found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
     * @param tree  The tree node
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
    public int getEndPos(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        return Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
/* ---------- parsing -------------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
     * Ident = IDENTIFIER
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
    Name ident() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
        if (S.token() == IDENTIFIER) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
            Name name = S.name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
            return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        } else if (S.token() == ASSERT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
            if (allowAsserts) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   422
                error(S.pos(), "assert.as.identifier");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
                return names.error;
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
            } else {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   426
                warning(S.pos(), "assert.as.identifier");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
                Name name = S.name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
        } else if (S.token() == ENUM) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
            if (allowEnums) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   433
                error(S.pos(), "enum.as.identifier");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
                return names.error;
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            } else {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   437
                warning(S.pos(), "enum.as.identifier");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
                Name name = S.name();
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
                return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
            accept(IDENTIFIER);
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
            return names.error;
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   447
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
     * Qualident = Ident { DOT Ident }
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
    public JCExpression qualident() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        JCExpression t = toP(F.at(S.pos()).Ident(ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
        while (S.token() == DOT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   456
            t = toP(F.at(pos).Select(t, ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
     * Literal =
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
     *     INTLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
     *   | LONGLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
     *   | FLOATLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
     *   | DOUBLELITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
     *   | CHARLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
     *   | STRINGLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
     *   | TRUE
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
     *   | FALSE
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
     *   | NULL
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
    JCExpression literal(Name prefix) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
        JCExpression t = errorTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
        switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
        case INTLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
                t = F.at(pos).Literal(
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
                    TypeTags.INT,
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
                    Convert.string2int(strval(prefix), S.radix()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
            } catch (NumberFormatException ex) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   483
                error(S.pos(), "int.number.too.large", strval(prefix));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
        case LONGLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
                t = F.at(pos).Literal(
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
                    TypeTags.LONG,
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
                    new Long(Convert.string2long(strval(prefix), S.radix())));
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
            } catch (NumberFormatException ex) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   492
                error(S.pos(), "int.number.too.large", strval(prefix));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
        case FLOATLITERAL: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
            String proper = (S.radix() == 16 ? ("0x"+ S.stringVal()) : S.stringVal());
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            Float n;
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
                n = Float.valueOf(proper);
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
            } catch (NumberFormatException ex) {
7636
030f141aa32b 6504896: TreeMaker.Literal(Object) does not support Booleans
jjg
parents: 7211
diff changeset
   501
                // error already reported in scanner
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
                n = Float.NaN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
            if (n.floatValue() == 0.0f && !isZero(proper))
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   505
                error(S.pos(), "fp.number.too.small");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
            else if (n.floatValue() == Float.POSITIVE_INFINITY)
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   507
                error(S.pos(), "fp.number.too.large");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
                t = F.at(pos).Literal(TypeTags.FLOAT, n);
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
        case DOUBLELITERAL: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
            String proper = (S.radix() == 16 ? ("0x"+ S.stringVal()) : S.stringVal());
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
            Double n;
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
                n = Double.valueOf(proper);
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
            } catch (NumberFormatException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
                // error already reported in scanner
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
                n = Double.NaN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
            if (n.doubleValue() == 0.0d && !isZero(proper))
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   522
                error(S.pos(), "fp.number.too.small");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
            else if (n.doubleValue() == Double.POSITIVE_INFINITY)
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
   524
                error(S.pos(), "fp.number.too.large");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
                t = F.at(pos).Literal(TypeTags.DOUBLE, n);
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
        case CHARLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
            t = F.at(pos).Literal(
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
                TypeTags.CHAR,
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
                S.stringVal().charAt(0) + 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
        case STRINGLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
            t = F.at(pos).Literal(
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
                TypeTags.CLASS,
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
                S.stringVal());
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
        case TRUE: case FALSE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
            t = F.at(pos).Literal(
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
                TypeTags.BOOLEAN,
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
                (S.token() == TRUE ? 1 : 0));
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        case NULL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
            t = F.at(pos).Literal(
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
                TypeTags.BOT,
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
                null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
        default:
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7636
diff changeset
   550
            Assert.error();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
        if (t == errorTree)
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
            t = F.at(pos).Erroneous();
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
        storeEnd(t, S.endPos());
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
        S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
//where
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
        boolean isZero(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
            char[] cs = s.toCharArray();
3894
e470a29ed0a2 6882235: invalid exponent causes silent javac crash
jjg
parents: 3765
diff changeset
   561
            int base = ((cs.length > 1 && Character.toLowerCase(cs[1]) == 'x') ? 16 : 10);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
            int i = ((base==16) ? 2 : 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
            while (i < cs.length && (cs[i] == '0' || cs[i] == '.')) i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
            return !(i < cs.length && (Character.digit(cs[i], base) > 0));
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
        String strval(Name prefix) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
            String s = S.stringVal();
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1258
diff changeset
   569
            return prefix.isEmpty() ? s : prefix + s;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
    /** terms can be either expressions or types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
     */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   574
    public JCExpression parseExpression() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
        return term(EXPR);
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   578
    public JCExpression parseType() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
        return term(TYPE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
    JCExpression term(int newmode) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
        int prevmode = mode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
        mode = newmode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
        JCExpression t = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
        lastmode = mode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
        mode = prevmode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
     *  Expression = Expression1 [ExpressionRest]
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
     *  ExpressionRest = [AssignmentOperator Expression1]
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
     *  AssignmentOperator = "=" | "+=" | "-=" | "*=" | "/=" |
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
     *                       "&=" | "|=" | "^=" |
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
     *                       "%=" | "<<=" | ">>=" | ">>>="
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
     *  Type = Type1
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
     *  TypeNoParams = TypeNoParams1
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
     *  StatementExpression = Expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
     *  ConstantExpression = Expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
    JCExpression term() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
        JCExpression t = term1();
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
        if ((mode & EXPR) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
            S.token() == EQ || PLUSEQ.compareTo(S.token()) <= 0 && S.token().compareTo(GTGTGTEQ) <= 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
            return termRest(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
    JCExpression termRest(JCExpression t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
        switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
        case EQ: {
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
            JCExpression t1 = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
            return toP(F.at(pos).Assign(t, t1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
        case PLUSEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
        case SUBEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
        case STAREQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
        case SLASHEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
        case PERCENTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
        case AMPEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
        case BAREQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        case CARETEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        case LTLTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
        case GTGTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
        case GTGTGTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
            Token token = S.token();
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
            JCExpression t1 = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
            return F.at(pos).Assignop(optag(token), t, t1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
    /** Expression1   = Expression2 [Expression1Rest]
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
     *  Type1         = Type2
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
     *  TypeNoParams1 = TypeNoParams2
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
    JCExpression term1() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
        JCExpression t = term2();
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
        if ((mode & EXPR) != 0 && S.token() == QUES) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
            return term1Rest(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
    /** Expression1Rest = ["?" Expression ":" Expression1]
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
    JCExpression term1Rest(JCExpression t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
        if (S.token() == QUES) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
            JCExpression t1 = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
            accept(COLON);
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
            JCExpression t2 = term1();
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
            return F.at(pos).Conditional(t, t1, t2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
    /** Expression2   = Expression3 [Expression2Rest]
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
     *  Type2         = Type3
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
     *  TypeNoParams2 = TypeNoParams3
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
    JCExpression term2() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
        JCExpression t = term3();
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
        if ((mode & EXPR) != 0 && prec(S.token()) >= TreeInfo.orPrec) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
            return term2Rest(t, TreeInfo.orPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
    /*  Expression2Rest = {infixop Expression3}
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
     *                  | Expression3 instanceof Type
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
     *  infixop         = "||"
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
     *                  | "&&"
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
     *                  | "|"
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
     *                  | "^"
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
     *                  | "&"
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
     *                  | "==" | "!="
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
     *                  | "<" | ">" | "<=" | ">="
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
     *                  | "<<" | ">>" | ">>>"
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
     *                  | "+" | "-"
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
     *                  | "*" | "/" | "%"
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
    JCExpression term2Rest(JCExpression t, int minprec) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
        List<JCExpression[]> savedOd = odStackSupply.elems;
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
        JCExpression[] odStack = newOdStack();
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
        List<Token[]> savedOp = opStackSupply.elems;
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
        Token[] opStack = newOpStack();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   703
        List<int[]> savedPos = posStackSupply.elems;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   704
        int[] posStack = newPosStack();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
        // optimization, was odStack = new Tree[...]; opStack = new Tree[...];
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
        int top = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
        odStack[0] = t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
        int startPos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
        Token topOp = ERROR;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   710
        int topOpPos = Position.NOPOS;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
        while (prec(S.token()) >= minprec) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   712
            posStack[top] = topOpPos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
            opStack[top] = topOp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
            top++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
            topOp = S.token();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   716
            topOpPos = S.pos();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
            S.nextToken();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   718
            odStack[top] = (topOp == INSTANCEOF) ? parseType() : term3();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
            while (top > 0 && prec(topOp) >= prec(S.token())) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   720
                odStack[top-1] = makeOp(topOpPos, topOp, odStack[top-1],
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
                                        odStack[top]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
                top--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
                topOp = opStack[top];
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   724
                topOpPos = posStack[top];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
        }
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7636
diff changeset
   727
        Assert.check(top == 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
        t = odStack[0];
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
        if (t.getTag() == JCTree.PLUS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
            StringBuffer buf = foldStrings(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
            if (buf != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
                t = toP(F.at(startPos).Literal(TypeTags.CLASS, buf.toString()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
        odStackSupply.elems = savedOd; // optimization
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
        opStackSupply.elems = savedOp; // optimization
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   739
        posStackSupply.elems = savedPos; // optimization
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
//where
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
        /** Construct a binary or type test node.
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
        private JCExpression makeOp(int pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
                                    Token topOp,
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
                                    JCExpression od1,
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
                                    JCExpression od2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
            if (topOp == INSTANCEOF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
                return F.at(pos).TypeTest(od1, od2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
                return F.at(pos).Binary(optag(topOp), od1, od2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
        /** If tree is a concatenation of string literals, replace it
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
         *  by a single literal representing the concatenated string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   758
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
        protected StringBuffer foldStrings(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
            List<String> buf = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
            while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
                if (tree.getTag() == JCTree.LITERAL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
                    JCLiteral lit = (JCLiteral) tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
                    if (lit.typetag == TypeTags.CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
                        StringBuffer sbuf =
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
                            new StringBuffer((String)lit.value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
                        while (buf.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
                            sbuf.append(buf.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
                            buf = buf.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
                        return sbuf;
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
                } else if (tree.getTag() == JCTree.PLUS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
                    JCBinary op = (JCBinary)tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
                    if (op.rhs.getTag() == JCTree.LITERAL) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
                        JCLiteral lit = (JCLiteral) op.rhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
                        if (lit.typetag == TypeTags.CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
                            buf = buf.prepend((String) lit.value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
                            tree = op.lhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
                            continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
        /** optimization: To save allocating a new operand/operator stack
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
         *  for every binary operation, we use supplys.
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
        ListBuffer<JCExpression[]> odStackSupply = new ListBuffer<JCExpression[]>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
        ListBuffer<Token[]> opStackSupply = new ListBuffer<Token[]>();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   793
        ListBuffer<int[]> posStackSupply = new ListBuffer<int[]>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
        private JCExpression[] newOdStack() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
            if (odStackSupply.elems == odStackSupply.last)
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
                odStackSupply.append(new JCExpression[infixPrecedenceLevels + 1]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
            JCExpression[] odStack = odStackSupply.elems.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
            odStackSupply.elems = odStackSupply.elems.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
            return odStack;
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
        private Token[] newOpStack() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
            if (opStackSupply.elems == opStackSupply.last)
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
                opStackSupply.append(new Token[infixPrecedenceLevels + 1]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
            Token[] opStack = opStackSupply.elems.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
            opStackSupply.elems = opStackSupply.elems.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
            return opStack;
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   811
        private int[] newPosStack() {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   812
            if (posStackSupply.elems == posStackSupply.last)
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   813
                posStackSupply.append(new int[infixPrecedenceLevels + 1]);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   814
            int[] posStack = posStackSupply.elems.head;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   815
            posStackSupply.elems = posStackSupply.elems.tail;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   816
            return posStack;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   817
        }
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   818
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
    /** Expression3    = PrefixOp Expression3
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
     *                 | "(" Expr | TypeNoParams ")" Expression3
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
     *                 | Primary {Selector} {PostfixOp}
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
     *  Primary        = "(" Expression ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
     *                 | Literal
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
     *                 | [TypeArguments] THIS [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
     *                 | [TypeArguments] SUPER SuperSuffix
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
     *                 | NEW [TypeArguments] Creator
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
   827
     *                 | Ident { "." Ident }
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
   828
     *                   [ "[" ( "]" BracketsOpt "." CLASS | Expression "]" )
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
     *                   | Arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
     *                   | "." ( CLASS | THIS | [TypeArguments] SUPER Arguments | NEW [TypeArguments] InnerCreator )
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
     *                   ]
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
     *                 | BasicType BracketsOpt "." CLASS
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
     *  PrefixOp       = "++" | "--" | "!" | "~" | "+" | "-"
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
     *  PostfixOp      = "++" | "--"
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
     *  Type3          = Ident { "." Ident } [TypeArguments] {TypeSelector} BracketsOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
     *                 | BasicType
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
     *  TypeNoParams3  = Ident { "." Ident } BracketsOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
     *  Selector       = "." [TypeArguments] Ident [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
     *                 | "." THIS
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
     *                 | "." [TypeArguments] SUPER SuperSuffix
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
     *                 | "." NEW [TypeArguments] InnerCreator
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
     *                 | "[" Expression "]"
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
     *  TypeSelector   = "." Ident [TypeArguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
     *  SuperSuffix    = Arguments | "." Ident [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
    protected JCExpression term3() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
        JCExpression t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
        List<JCExpression> typeArgs = typeArgumentsOpt(EXPR);
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
        switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
        case QUES:
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
            if ((mode & TYPE) != 0 && (mode & (TYPEARG|NOPARAMS)) == TYPEARG) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
                mode = TYPE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
                return typeArgument();
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
            } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
                return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
        case PLUSPLUS: case SUBSUB: case BANG: case TILDE: case PLUS: case SUB:
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
            if (typeArgs == null && (mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
                Token token = S.token();
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
                mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
                if (token == SUB &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
                    (S.token() == INTLITERAL || S.token() == LONGLITERAL) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
                    S.radix() == 10) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
                    mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
                    t = literal(names.hyphen);
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
                    t = term3();
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
                    return F.at(pos).Unary(unoptag(token), t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
        case LPAREN:
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
            if (typeArgs == null && (mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
                mode = EXPR | TYPE | NOPARAMS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
                t = term3();
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
                if ((mode & TYPE) != 0 && S.token() == LT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
                    // Could be a cast to a parameterized type
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
                    int op = JCTree.LT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
                    int pos1 = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
                    S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
                    mode &= (EXPR | TYPE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
                    mode |= TYPEARG;
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
                    JCExpression t1 = term3();
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
                    if ((mode & TYPE) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
                        (S.token() == COMMA || S.token() == GT)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
                        mode = TYPE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
                        ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
                        args.append(t1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
                        while (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
                            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
                            args.append(typeArgument());
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
                        accept(GT);
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   896
                        t = toP(F.at(pos1).TypeApply(t, args.toList()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
                        checkGenerics();
1790
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1260
diff changeset
   898
                        while (S.token() == DOT) {
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1260
diff changeset
   899
                            S.nextToken();
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1260
diff changeset
   900
                            mode = TYPE;
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1260
diff changeset
   901
                            t = toP(F.at(S.pos()).Select(t, ident()));
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1260
diff changeset
   902
                            t = typeArgumentsOpt(t);
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1260
diff changeset
   903
                        }
1867
0a3af3ae0d8e 6795580: parser confused by square brackets in qualified generic cast
mcimadamore
parents: 1790
diff changeset
   904
                        t = bracketsOpt(toP(t));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
                    } else if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
                        mode = EXPR;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   907
                        JCExpression e = term2Rest(t1, TreeInfo.shiftPrec);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   908
                        t = F.at(pos1).Binary(op, t, e);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
                        t = termRest(term1Rest(term2Rest(t, TreeInfo.orPrec)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
                        accept(GT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
                    }
1790
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1260
diff changeset
   913
                }
7182011ee8a6 6665356: Cast not allowed when both qualifying type and inner class are parameterized
mcimadamore
parents: 1260
diff changeset
   914
                else {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
                    t = termRest(term1Rest(term2Rest(t, TreeInfo.orPrec)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
                accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
                lastmode = mode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
                mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
                if ((lastmode & EXPR) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
                    JCExpression t1 = term3();
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
                    return F.at(pos).TypeCast(t, t1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
                } else if ((lastmode & TYPE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
                    switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
                    /*case PLUSPLUS: case SUBSUB: */
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
                    case BANG: case TILDE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
                    case LPAREN: case THIS: case SUPER:
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
                    case INTLITERAL: case LONGLITERAL: case FLOATLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
                    case DOUBLELITERAL: case CHARLITERAL: case STRINGLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
                    case TRUE: case FALSE: case NULL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
                    case NEW: case IDENTIFIER: case ASSERT: case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
                    case BYTE: case SHORT: case CHAR: case INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
                    case LONG: case FLOAT: case DOUBLE: case BOOLEAN: case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
                        JCExpression t1 = term3();
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
                        return F.at(pos).TypeCast(t, t1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
            t = toP(F.at(pos).Parens(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
        case THIS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
            if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
                mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
                t = to(F.at(pos).Ident(names._this));
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
                if (typeArgs == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
                    t = argumentsOpt(null, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   949
                    t = arguments(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
                typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
   952
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
        case SUPER:
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
            if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
                mode = EXPR;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   956
                t = to(F.at(pos).Ident(names._super));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
   957
                t = superSuffix(typeArgs, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
                typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   959
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
   960
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
        case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: case DOUBLELITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
        case CHARLITERAL: case STRINGLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
        case TRUE: case FALSE: case NULL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
            if (typeArgs == null && (mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
                mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
                t = literal(names.empty);
06bc494ca11e Initial load
duke
parents:
diff changeset
   967
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
        case NEW:
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
            if (typeArgs != null) return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
            if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
                mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
                S.nextToken();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
   974
                if (S.token() == LT) typeArgs = typeArguments(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
                t = creator(pos, typeArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
                typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
        case IDENTIFIER: case ASSERT: case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
   980
            if (typeArgs != null) return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
   981
            t = toP(F.at(S.pos()).Ident(ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   982
            loop: while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
                pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
                switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
                case LBRACKET:
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
                    S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
   987
                    if (S.token() == RBRACKET) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
                        S.nextToken();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
   989
                        t = bracketsOpt(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
                        t = toP(F.at(pos).TypeArray(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
                        t = bracketsSuffix(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
                        if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
                            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
                            JCExpression t1 = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
   996
                            t = to(F.at(pos).Indexed(t, t1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   998
                        accept(RBRACKET);
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1000
                    break loop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
                case LPAREN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
                    if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
                        mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
                        t = arguments(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
                        typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
                    break loop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
                case DOT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
                    S.nextToken();
510
8d0b2c7d60a1 6481655: Parser confused by combination of parens and explicit type args
mcimadamore
parents: 10
diff changeset
  1010
                    int oldmode = mode;
8d0b2c7d60a1 6481655: Parser confused by combination of parens and explicit type args
mcimadamore
parents: 10
diff changeset
  1011
                    mode &= ~NOPARAMS;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
                    typeArgs = typeArgumentsOpt(EXPR);
510
8d0b2c7d60a1 6481655: Parser confused by combination of parens and explicit type args
mcimadamore
parents: 10
diff changeset
  1013
                    mode = oldmode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1014
                    if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
                        switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
                        case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
                            if (typeArgs != null) return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
                            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
                            t = to(F.at(pos).Select(t, names._class));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1020
                            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
                            break loop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
                        case THIS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
                            if (typeArgs != null) return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
                            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
                            t = to(F.at(pos).Select(t, names._this));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
                            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
                            break loop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
                        case SUPER:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
                            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1030
                            t = to(F.at(pos).Select(t, names._super));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
                            t = superSuffix(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
                            typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
                            break loop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1034
                        case NEW:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
                            if (typeArgs != null) return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
                            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
                            int pos1 = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
                            S.nextToken();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1039
                            if (S.token() == LT) typeArgs = typeArguments(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1040
                            t = innerCreator(pos1, typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
                            typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
                            break loop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1044
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
                    // typeArgs saved for next loop iteration.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
                    t = toP(F.at(pos).Select(t, ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
                default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
                    break loop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
            if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
            t = typeArgumentsOpt(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
        case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
        case DOUBLE: case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
            if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
            t = bracketsSuffix(bracketsOpt(basicType()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
        case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
            if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
            if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
                if (S.token() == DOT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
                    JCPrimitiveTypeTree ti = toP(F.at(pos).TypeIdent(TypeTags.VOID));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
                    t = bracketsSuffix(ti);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
                    return illegal(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
            } else {
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1071
                // Support the corner case of myMethodHandle.<void>invoke() by passing
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1072
                // a void type (like other primitive types) to the next phase.
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1073
                // The error will be reported in Attr.attribTypes or Attr.visitApply.
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1074
                JCPrimitiveTypeTree ti = to(F.at(pos).TypeIdent(TypeTags.VOID));
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1075
                S.nextToken();
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1076
                return ti;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1077
                //return illegal();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1080
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
            return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
        if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
            int pos1 = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
            if (S.token() == LBRACKET) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
                if ((mode & TYPE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
                    int oldmode = mode;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
                    mode = TYPE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
                    if (S.token() == RBRACKET) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
                        S.nextToken();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1093
                        t = bracketsOpt(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1094
                        t = toP(F.at(pos1).TypeArray(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1095
                        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
                    mode = oldmode;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
                if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
                    mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1101
                    JCExpression t1 = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1102
                    t = to(F.at(pos1).Indexed(t, t1));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
                accept(RBRACKET);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
            } else if (S.token() == DOT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
                typeArgs = typeArgumentsOpt(EXPR);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1108
                if (S.token() == SUPER && (mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1109
                    mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1110
                    t = to(F.at(pos1).Select(t, names._super));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1111
                    S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1112
                    t = arguments(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1113
                    typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1114
                } else if (S.token() == NEW && (mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1115
                    if (typeArgs != null) return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1116
                    mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1117
                    int pos2 = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1118
                    S.nextToken();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1119
                    if (S.token() == LT) typeArgs = typeArguments(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1120
                    t = innerCreator(pos2, typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1121
                    typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1122
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
                    t = toP(F.at(pos1).Select(t, ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1124
                    t = argumentsOpt(typeArgs, typeArgumentsOpt(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1125
                    typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1126
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
        while ((S.token() == PLUSPLUS || S.token() == SUBSUB) && (mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1132
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
            t = to(F.at(S.pos()).Unary(
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
                  S.token() == PLUSPLUS ? JCTree.POSTINC : JCTree.POSTDEC, t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
        return toP(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
    /** SuperSuffix = Arguments | "." [TypeArguments] Ident [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
    JCExpression superSuffix(List<JCExpression> typeArgs, JCExpression t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
        S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
        if (S.token() == LPAREN || typeArgs != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
            t = arguments(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
            accept(DOT);
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1149
            typeArgs = (S.token() == LT) ? typeArguments(false) : null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
            t = toP(F.at(pos).Select(t, ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
            t = argumentsOpt(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
    /** BasicType = BYTE | SHORT | CHAR | INT | LONG | FLOAT | DOUBLE | BOOLEAN
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
    JCPrimitiveTypeTree basicType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
        JCPrimitiveTypeTree t = to(F.at(S.pos()).TypeIdent(typetag(S.token())));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
        S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
    /** ArgumentsOpt = [ Arguments ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
    JCExpression argumentsOpt(List<JCExpression> typeArgs, JCExpression t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1167
        if ((mode & EXPR) != 0 && S.token() == LPAREN || typeArgs != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
            return arguments(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1174
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
    /** Arguments = "(" [Expression { COMMA Expression }] ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1176
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
    List<JCExpression> arguments() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
        ListBuffer<JCExpression> args = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
        if (S.token() == LPAREN) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1181
            if (S.token() != RPAREN) {
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1182
                args.append(parseExpression());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1183
                while (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1184
                    S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1185
                    args.append(parseExpression());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1186
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1187
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1188
            accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1189
        } else {
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1190
            syntaxError(S.pos(), "expected", LPAREN);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1191
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1192
        return args.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1193
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1194
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
    JCMethodInvocation arguments(List<JCExpression> typeArgs, JCExpression t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1196
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1197
        List<JCExpression> args = arguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1198
        return toP(F.at(pos).Apply(typeArgs, t, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1199
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1200
06bc494ca11e Initial load
duke
parents:
diff changeset
  1201
    /**  TypeArgumentsOpt = [ TypeArguments ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1202
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1203
    JCExpression typeArgumentsOpt(JCExpression t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1204
        if (S.token() == LT &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1205
            (mode & TYPE) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1206
            (mode & NOPARAMS) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1207
            mode = TYPE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1208
            checkGenerics();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1209
            return typeArguments(t, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1210
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1211
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1212
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1213
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1214
    List<JCExpression> typeArgumentsOpt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1215
        return typeArgumentsOpt(TYPE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1216
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1217
06bc494ca11e Initial load
duke
parents:
diff changeset
  1218
    List<JCExpression> typeArgumentsOpt(int useMode) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1219
        if (S.token() == LT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
            checkGenerics();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1221
            if ((mode & useMode) == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1222
                (mode & NOPARAMS) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1223
                illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1224
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1225
            mode = useMode;
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1226
            return typeArguments(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1227
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1228
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1229
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1230
06bc494ca11e Initial load
duke
parents:
diff changeset
  1231
    /**  TypeArguments  = "<" TypeArgument {"," TypeArgument} ">"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1232
     */
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1233
    List<JCExpression> typeArguments(boolean diamondAllowed) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1234
        if (S.token() == LT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1235
            S.nextToken();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1236
            if (S.token() == GT && diamondAllowed) {
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  1237
                checkDiamond();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1238
                mode |= DIAMOND;
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  1239
                S.nextToken();
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  1240
                return List.nil();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1241
            } else {
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1242
                ListBuffer<JCExpression> args = ListBuffer.lb();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1243
                args.append(((mode & EXPR) == 0) ? typeArgument() : parseType());
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1244
                while (S.token() == COMMA) {
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1245
                    S.nextToken();
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1246
                    args.append(((mode & EXPR) == 0) ? typeArgument() : parseType());
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1247
                }
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1248
                switch (S.token()) {
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1249
                case GTGTGTEQ:
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1250
                    S.token(GTGTEQ);
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1251
                    break;
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1252
                case GTGTEQ:
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1253
                    S.token(GTEQ);
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1254
                    break;
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1255
                case GTEQ:
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1256
                    S.token(EQ);
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1257
                    break;
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1258
                case GTGTGT:
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1259
                    S.token(GTGT);
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1260
                    break;
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1261
                case GTGT:
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1262
                    S.token(GT);
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1263
                    break;
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1264
                default:
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1265
                    accept(GT);
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1266
                    break;
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1267
                }
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1268
                return args.toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1269
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1270
        } else {
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1271
            syntaxError(S.pos(), "expected", LT);
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1272
            return List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1273
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1274
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1275
06bc494ca11e Initial load
duke
parents:
diff changeset
  1276
    /** TypeArgument = Type
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1277
     *               | "?"
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1278
     *               | "?" EXTENDS Type {"&" Type}
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1279
     *               | "?" SUPER Type
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1280
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1281
    JCExpression typeArgument() {
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1282
        if (S.token() != QUES) return parseType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1283
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1284
        S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1285
        if (S.token() == EXTENDS) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1286
            TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.EXTENDS));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1287
            S.nextToken();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1288
            JCExpression bound = parseType();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1289
            return F.at(pos).Wildcard(t, bound);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1290
        } else if (S.token() == SUPER) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1291
            TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.SUPER));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1292
            S.nextToken();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1293
            JCExpression bound = parseType();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1294
            return F.at(pos).Wildcard(t, bound);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1295
        } else if (S.token() == IDENTIFIER) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1296
            //error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  1297
            reportSyntaxError(S.prevEndPos(), "expected3",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1298
                    GT, EXTENDS, SUPER);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1299
            TypeBoundKind t = F.at(Position.NOPOS).TypeBoundKind(BoundKind.UNBOUND);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1300
            JCExpression wc = toP(F.at(pos).Wildcard(t, null));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1301
            JCIdent id = toP(F.at(S.pos()).Ident(ident()));
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1302
            return F.at(pos).Erroneous(List.<JCTree>of(wc, id));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1303
        } else {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1304
            TypeBoundKind t = toP(F.at(pos).TypeBoundKind(BoundKind.UNBOUND));
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1305
            return toP(F.at(pos).Wildcard(t, null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1306
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1307
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1308
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1309
    JCTypeApply typeArguments(JCExpression t, boolean diamondAllowed) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1310
        int pos = S.pos();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1311
        List<JCExpression> args = typeArguments(diamondAllowed);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1312
        return toP(F.at(pos).TypeApply(t, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1313
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1314
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1315
    /** BracketsOpt = {"[" "]"}
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1316
     */
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1317
    private JCExpression bracketsOpt(JCExpression t) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1318
        if (S.token() == LBRACKET) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1319
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1320
            S.nextToken();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1321
            t = bracketsOptCont(t, pos);
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1322
            F.at(pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1323
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1324
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1325
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1326
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1327
    private JCArrayTypeTree bracketsOptCont(JCExpression t, int pos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1328
        accept(RBRACKET);
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1329
        t = bracketsOpt(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1330
        return toP(F.at(pos).TypeArray(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1331
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1332
06bc494ca11e Initial load
duke
parents:
diff changeset
  1333
    /** BracketsSuffixExpr = "." CLASS
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
     *  BracketsSuffixType =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
    JCExpression bracketsSuffix(JCExpression t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
        if ((mode & EXPR) != 0 && S.token() == DOT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
            accept(CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1342
            if (S.pos() == errorEndPos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
                // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
                Name name = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
                if (S.token() == IDENTIFIER) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
                    name = S.name();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
                    S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1348
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1349
                    name = names.error;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1350
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
                t = F.at(pos).Erroneous(List.<JCTree>of(toP(F.at(pos).Select(t, name))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
                t = toP(F.at(pos).Select(t, names._class));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
        } else if ((mode & TYPE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
            mode = TYPE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
            syntaxError(S.pos(), "dot.class.expected");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1363
    /** Creator = Qualident [TypeArguments] ( ArrayCreatorRest | ClassCreatorRest )
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
    JCExpression creator(int newpos, List<JCExpression> typeArgs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
        switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
        case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1368
        case DOUBLE: case BOOLEAN:
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1369
            if (typeArgs == null)
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1370
                return arrayCreatorRest(newpos, basicType());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
        JCExpression t = qualident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1375
        int oldmode = mode;
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1376
        mode = TYPE;
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1377
        boolean diamondFound = false;
10199
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  1378
        int lastTypeargsPos = -1;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
        if (S.token() == LT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
            checkGenerics();
10199
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  1381
            lastTypeargsPos = S.pos();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1382
            t = typeArguments(t, true);
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1383
            diamondFound = (mode & DIAMOND) != 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1384
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
        while (S.token() == DOT) {
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1386
            if (diamondFound) {
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1387
                //cannot select after a diamond
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1388
                illegal(S.pos());
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1389
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1390
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1391
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1392
            t = toP(F.at(pos).Select(t, ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
            if (S.token() == LT) {
10199
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  1394
                lastTypeargsPos = S.pos();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
                checkGenerics();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1396
                t = typeArguments(t, true);
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1397
                diamondFound = (mode & DIAMOND) != 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
        mode = oldmode;
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1401
        if (S.token() == LBRACKET) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
            JCExpression e = arrayCreatorRest(newpos, t);
10199
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  1403
            if (diamondFound) {
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  1404
                reportSyntaxError(lastTypeargsPos, "cannot.create.array.with.diamond");
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  1405
                return toP(F.at(newpos).Erroneous(List.of(e)));
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  1406
            }
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  1407
            else if (typeArgs != null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
                int pos = newpos;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1409
                if (!typeArgs.isEmpty() && typeArgs.head.pos != Position.NOPOS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
                    // note: this should always happen but we should
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
                    // not rely on this as the parser is continuously
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
                    // modified to improve error recovery.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
                    pos = typeArgs.head.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1415
                setErrorEndPos(S.prevEndPos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1416
                reportSyntaxError(pos, "cannot.create.array.with.type.arguments");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1417
                return toP(F.at(newpos).Erroneous(typeArgs.prepend(e)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
            return e;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
        } else if (S.token() == LPAREN) {
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1421
            return classCreatorRest(newpos, null, typeArgs, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1422
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1423
            reportSyntaxError(S.pos(), "expected2",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1424
                               LPAREN, LBRACKET);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1425
            t = toP(F.at(newpos).NewClass(null, typeArgs, t, List.<JCExpression>nil(), null));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
            return toP(F.at(newpos).Erroneous(List.<JCTree>of(t)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1427
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1428
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1429
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
    /** InnerCreator = Ident [TypeArguments] ClassCreatorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
    JCExpression innerCreator(int newpos, List<JCExpression> typeArgs, JCExpression encl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1433
        JCExpression t = toP(F.at(S.pos()).Ident(ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1434
        if (S.token() == LT) {
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  1435
            int oldmode = mode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1436
            checkGenerics();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1437
            t = typeArguments(t, true);
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  1438
            mode = oldmode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1439
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1440
        return classCreatorRest(newpos, encl, typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1441
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1442
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1443
    /** ArrayCreatorRest = "[" ( "]" BracketsOpt ArrayInitializer
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1444
     *                         | Expression "]" {"[" Expression "]"} BracketsOpt )
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
    JCExpression arrayCreatorRest(int newpos, JCExpression elemtype) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1447
        accept(LBRACKET);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1448
        if (S.token() == RBRACKET) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
            accept(RBRACKET);
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1450
            elemtype = bracketsOpt(elemtype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1451
            if (S.token() == LBRACE) {
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1452
                return arrayInitializer(newpos, elemtype);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
                return syntaxError(S.pos(), "array.dimension.missing");
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1457
            ListBuffer<JCExpression> dims = new ListBuffer<JCExpression>();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1458
            dims.append(parseExpression());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1459
            accept(RBRACKET);
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1460
            while (S.token() == LBRACKET) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1461
                int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
                if (S.token() == RBRACKET) {
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1464
                    elemtype = bracketsOptCont(elemtype, pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1465
                } else {
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1466
                    dims.append(parseExpression());
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1467
                    accept(RBRACKET);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1468
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
            }
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1470
            return toP(F.at(newpos).NewArray(elemtype, dims.toList(), null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1471
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1472
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1473
06bc494ca11e Initial load
duke
parents:
diff changeset
  1474
    /** ClassCreatorRest = Arguments [ClassBody]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
     */
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 2723
diff changeset
  1476
    JCNewClass classCreatorRest(int newpos,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1477
                                  JCExpression encl,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1478
                                  List<JCExpression> typeArgs,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1479
                                  JCExpression t)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1480
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1481
        List<JCExpression> args = arguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1482
        JCClassDecl body = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
        if (S.token() == LBRACE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
            List<JCTree> defs = classOrInterfaceBody(names.empty, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
            JCModifiers mods = F.at(Position.NOPOS).Modifiers(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
            body = toP(F.at(pos).AnonymousClassDef(mods, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1488
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
        return toP(F.at(newpos).NewClass(encl, typeArgs, t, args, body));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
    /** ArrayInitializer = "{" [VariableInitializer {"," VariableInitializer}] [","] "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1493
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1494
    JCExpression arrayInitializer(int newpos, JCExpression t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
        accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1496
        ListBuffer<JCExpression> elems = new ListBuffer<JCExpression>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1497
        if (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1499
        } else if (S.token() != RBRACE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1500
            elems.append(variableInitializer());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
            while (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1502
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
                if (S.token() == RBRACE) break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1504
                elems.append(variableInitializer());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1505
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1506
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1507
        accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
        return toP(F.at(newpos).NewArray(t, List.<JCExpression>nil(), elems.toList()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1509
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
06bc494ca11e Initial load
duke
parents:
diff changeset
  1511
    /** VariableInitializer = ArrayInitializer | Expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  1512
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1513
    public JCExpression variableInitializer() {
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1514
        return S.token() == LBRACE ? arrayInitializer(S.pos(), null) : parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1515
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1516
06bc494ca11e Initial load
duke
parents:
diff changeset
  1517
    /** ParExpression = "(" Expression ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1518
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1519
    JCExpression parExpression() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
        accept(LPAREN);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1521
        JCExpression t = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
        accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1523
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1524
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1525
06bc494ca11e Initial load
duke
parents:
diff changeset
  1526
    /** Block = "{" BlockStatements "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1527
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
    JCBlock block(int pos, long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1529
        accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1530
        List<JCStatement> stats = blockStatements();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1531
        JCBlock t = F.at(pos).Block(flags, stats);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1532
        while (S.token() == CASE || S.token() == DEFAULT) {
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1533
            syntaxError("orphaned", S.token());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1534
            switchBlockStatementGroups();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1535
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1536
        // the Block node has a field "endpos" for first char of last token, which is
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
        // usually but not necessarily the last char of the last token.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1538
        t.endpos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1539
        accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1540
        return toP(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1541
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1542
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
    public JCBlock block() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
        return block(S.pos(), 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
06bc494ca11e Initial load
duke
parents:
diff changeset
  1547
    /** BlockStatements = { BlockStatement }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1548
     *  BlockStatement  = LocalVariableDeclarationStatement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1549
     *                  | ClassOrInterfaceOrEnumDeclaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  1550
     *                  | [Ident ":"] Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1551
     *  LocalVariableDeclarationStatement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1552
     *                  = { FINAL | '@' Annotation } Type VariableDeclarators ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1553
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1554
    @SuppressWarnings("fallthrough")
06bc494ca11e Initial load
duke
parents:
diff changeset
  1555
    List<JCStatement> blockStatements() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1556
//todo: skip to anchor on error(?)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1557
        int lastErrPos = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1558
        ListBuffer<JCStatement> stats = new ListBuffer<JCStatement>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1559
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1560
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1561
            switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1562
            case RBRACE: case CASE: case DEFAULT: case EOF:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1563
                return stats.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1564
            case LBRACE: case IF: case FOR: case WHILE: case DO: case TRY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1565
            case SWITCH: case SYNCHRONIZED: case RETURN: case THROW: case BREAK:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1566
            case CONTINUE: case SEMI: case ELSE: case FINALLY: case CATCH:
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1567
                stats.append(parseStatement());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1568
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1569
            case MONKEYS_AT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1570
            case FINAL: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1571
                String dc = S.docComment();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1572
                JCModifiers mods = modifiersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1573
                if (S.token() == INTERFACE ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1574
                    S.token() == CLASS ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1575
                    allowEnums && S.token() == ENUM) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1576
                    stats.append(classOrInterfaceOrEnumDeclaration(mods, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1577
                } else {
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1578
                    JCExpression t = parseType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1579
                    stats.appendList(variableDeclarators(mods, t,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1580
                                                         new ListBuffer<JCStatement>()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1581
                    // A "LocalVariableDeclarationStatement" subsumes the terminating semicolon
06bc494ca11e Initial load
duke
parents:
diff changeset
  1582
                    storeEnd(stats.elems.last(), S.endPos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1583
                    accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1584
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1585
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1586
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1587
            case ABSTRACT: case STRICTFP: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1588
                String dc = S.docComment();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1589
                JCModifiers mods = modifiersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1590
                stats.append(classOrInterfaceOrEnumDeclaration(mods, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1591
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1592
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1593
            case INTERFACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1594
            case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1595
                stats.append(classOrInterfaceOrEnumDeclaration(modifiersOpt(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1596
                                                               S.docComment()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1597
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1598
            case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1599
            case ASSERT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1600
                if (allowEnums && S.token() == ENUM) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  1601
                    error(S.pos(), "local.enum");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1602
                    stats.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1603
                        append(classOrInterfaceOrEnumDeclaration(modifiersOpt(),
06bc494ca11e Initial load
duke
parents:
diff changeset
  1604
                                                                 S.docComment()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1605
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1606
                } else if (allowAsserts && S.token() == ASSERT) {
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1607
                    stats.append(parseStatement());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1608
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1609
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1610
                /* fall through to default */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1611
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1612
                Name name = S.name();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1613
                JCExpression t = term(EXPR | TYPE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1614
                if (S.token() == COLON && t.getTag() == JCTree.IDENT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1615
                    S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1616
                    JCStatement stat = parseStatement();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1617
                    stats.append(F.at(pos).Labelled(name, stat));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1618
                } else if ((lastmode & TYPE) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1619
                           (S.token() == IDENTIFIER ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1620
                            S.token() == ASSERT ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1621
                            S.token() == ENUM)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1622
                    pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1623
                    JCModifiers mods = F.at(Position.NOPOS).Modifiers(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1624
                    F.at(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1625
                    stats.appendList(variableDeclarators(mods, t,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1626
                                                         new ListBuffer<JCStatement>()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1627
                    // A "LocalVariableDeclarationStatement" subsumes the terminating semicolon
06bc494ca11e Initial load
duke
parents:
diff changeset
  1628
                    storeEnd(stats.elems.last(), S.endPos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1629
                    accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1630
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1631
                    // This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon
06bc494ca11e Initial load
duke
parents:
diff changeset
  1632
                    stats.append(to(F.at(pos).Exec(checkExprStat(t))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1633
                    accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1634
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1635
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1636
06bc494ca11e Initial load
duke
parents:
diff changeset
  1637
            // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  1638
            if (S.pos() == lastErrPos)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1639
                return stats.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1640
            if (S.pos() <= errorEndPos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1641
                skip(false, true, true, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1642
                lastErrPos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1643
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1644
06bc494ca11e Initial load
duke
parents:
diff changeset
  1645
            // ensure no dangling /** @deprecated */ active
06bc494ca11e Initial load
duke
parents:
diff changeset
  1646
            S.resetDeprecatedFlag();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1647
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1648
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1649
06bc494ca11e Initial load
duke
parents:
diff changeset
  1650
    /** Statement =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1651
     *       Block
06bc494ca11e Initial load
duke
parents:
diff changeset
  1652
     *     | IF ParExpression Statement [ELSE Statement]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1653
     *     | FOR "(" ForInitOpt ";" [Expression] ";" ForUpdateOpt ")" Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1654
     *     | FOR "(" FormalParameter : Expression ")" Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1655
     *     | WHILE ParExpression Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1656
     *     | DO Statement WHILE ParExpression ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1657
     *     | TRY Block ( Catches | [Catches] FinallyPart )
8234
5050975e2df0 7014734: Project Coin: Allow optional trailing semicolon to terminate resources list in try-with-resources
darcy
parents: 8224
diff changeset
  1658
     *     | TRY "(" ResourceSpecification ";"opt ")" Block [Catches] [FinallyPart]
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1659
     *     | SWITCH ParExpression "{" SwitchBlockStatementGroups "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1660
     *     | SYNCHRONIZED ParExpression Block
06bc494ca11e Initial load
duke
parents:
diff changeset
  1661
     *     | RETURN [Expression] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1662
     *     | THROW Expression ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1663
     *     | BREAK [Ident] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1664
     *     | CONTINUE [Ident] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1665
     *     | ASSERT Expression [ ":" Expression ] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1666
     *     | ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1667
     *     | ExpressionStatement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1668
     *     | Ident ":" Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  1669
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1670
    @SuppressWarnings("fallthrough")
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1671
    public JCStatement parseStatement() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1672
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1673
        switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1674
        case LBRACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1675
            return block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1676
        case IF: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1677
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1678
            JCExpression cond = parExpression();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1679
            JCStatement thenpart = parseStatement();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1680
            JCStatement elsepart = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1681
            if (S.token() == ELSE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1682
                S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1683
                elsepart = parseStatement();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1684
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1685
            return F.at(pos).If(cond, thenpart, elsepart);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1686
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1687
        case FOR: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1688
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1689
            accept(LPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
            List<JCStatement> inits = S.token() == SEMI ? List.<JCStatement>nil() : forInit();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
            if (inits.length() == 1 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1692
                inits.head.getTag() == JCTree.VARDEF &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1693
                ((JCVariableDecl) inits.head).init == null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1694
                S.token() == COLON) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
                checkForeach();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1696
                JCVariableDecl var = (JCVariableDecl)inits.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1697
                accept(COLON);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1698
                JCExpression expr = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1699
                accept(RPAREN);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1700
                JCStatement body = parseStatement();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1701
                return F.at(pos).ForeachLoop(var, expr, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1702
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1703
                accept(SEMI);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1704
                JCExpression cond = S.token() == SEMI ? null : parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1705
                accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1706
                List<JCExpressionStatement> steps = S.token() == RPAREN ? List.<JCExpressionStatement>nil() : forUpdate();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1707
                accept(RPAREN);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1708
                JCStatement body = parseStatement();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1709
                return F.at(pos).ForLoop(inits, cond, steps, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1712
        case WHILE: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1713
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1714
            JCExpression cond = parExpression();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1715
            JCStatement body = parseStatement();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
            return F.at(pos).WhileLoop(cond, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
        case DO: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
            S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1720
            JCStatement body = parseStatement();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
            accept(WHILE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1722
            JCExpression cond = parExpression();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1723
            JCDoWhileLoop t = to(F.at(pos).DoLoop(body, cond));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1724
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1725
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1727
        case TRY: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1728
            S.nextToken();
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  1729
            List<JCTree> resources = List.<JCTree>nil();
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  1730
            if (S.token() == LPAREN) {
7211
163fe60f63de 6970016: Clean up ARM/try-with-resources implementation
mcimadamore
parents: 7076
diff changeset
  1731
                checkTryWithResources();
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  1732
                S.nextToken();
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  1733
                resources = resources();
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  1734
                accept(RPAREN);
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  1735
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1736
            JCBlock body = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1737
            ListBuffer<JCCatch> catchers = new ListBuffer<JCCatch>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1738
            JCBlock finalizer = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1739
            if (S.token() == CATCH || S.token() == FINALLY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1740
                while (S.token() == CATCH) catchers.append(catchClause());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1741
                if (S.token() == FINALLY) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1742
                    S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1743
                    finalizer = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1744
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1745
            } else {
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  1746
                if (allowTWR) {
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  1747
                    if (resources.isEmpty())
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  1748
                        error(pos, "try.without.catch.finally.or.resource.decls");
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  1749
                } else
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  1750
                    error(pos, "try.without.catch.or.finally");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1751
            }
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  1752
            return F.at(pos).Try(resources, body, catchers.toList(), finalizer);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1753
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1754
        case SWITCH: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1755
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1756
            JCExpression selector = parExpression();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1757
            accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1758
            List<JCCase> cases = switchBlockStatementGroups();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1759
            JCSwitch t = to(F.at(pos).Switch(selector, cases));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1760
            accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1761
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1762
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1763
        case SYNCHRONIZED: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1764
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1765
            JCExpression lock = parExpression();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1766
            JCBlock body = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1767
            return F.at(pos).Synchronized(lock, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1768
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1769
        case RETURN: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1770
            S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1771
            JCExpression result = S.token() == SEMI ? null : parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1772
            JCReturn t = to(F.at(pos).Return(result));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1773
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1774
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1775
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1776
        case THROW: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1777
            S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1778
            JCExpression exc = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1779
            JCThrow t = to(F.at(pos).Throw(exc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1780
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1781
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1782
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1783
        case BREAK: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1784
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1785
            Name label = (S.token() == IDENTIFIER || S.token() == ASSERT || S.token() == ENUM) ? ident() : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1786
            JCBreak t = to(F.at(pos).Break(label));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1787
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1788
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1789
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1790
        case CONTINUE: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1791
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1792
            Name label = (S.token() == IDENTIFIER || S.token() == ASSERT || S.token() == ENUM) ? ident() : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1793
            JCContinue t =  to(F.at(pos).Continue(label));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1794
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1795
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1796
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1797
        case SEMI:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1798
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1799
            return toP(F.at(pos).Skip());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1800
        case ELSE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1801
            return toP(F.Exec(syntaxError("else.without.if")));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1802
        case FINALLY:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1803
            return toP(F.Exec(syntaxError("finally.without.try")));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1804
        case CATCH:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1805
            return toP(F.Exec(syntaxError("catch.without.try")));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1806
        case ASSERT: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1807
            if (allowAsserts && S.token() == ASSERT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1808
                S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1809
                JCExpression assertion = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1810
                JCExpression message = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1811
                if (S.token() == COLON) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1812
                    S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1813
                    message = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1814
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1815
                JCAssert t = to(F.at(pos).Assert(assertion, message));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1816
                accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1817
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1818
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1819
            /* else fall through to default case */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1820
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1821
        case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1822
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1823
            Name name = S.name();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1824
            JCExpression expr = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1825
            if (S.token() == COLON && expr.getTag() == JCTree.IDENT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1826
                S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1827
                JCStatement stat = parseStatement();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1828
                return F.at(pos).Labelled(name, stat);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1829
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1830
                // This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon
06bc494ca11e Initial load
duke
parents:
diff changeset
  1831
                JCExpressionStatement stat = to(F.at(pos).Exec(checkExprStat(expr)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1832
                accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1833
                return stat;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1834
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1835
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1836
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1837
06bc494ca11e Initial load
duke
parents:
diff changeset
  1838
    /** CatchClause     = CATCH "(" FormalParameter ")" Block
06bc494ca11e Initial load
duke
parents:
diff changeset
  1839
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1840
    JCCatch catchClause() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1841
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1842
        accept(CATCH);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1843
        accept(LPAREN);
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1844
        JCModifiers mods = optFinal(Flags.PARAMETER);
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1845
        List<JCExpression> catchTypes = catchTypes();
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1846
        JCExpression paramType = catchTypes.size() > 1 ?
9300
c2de4dd9853b 7033809: Rename "disjunctive" to "union" in javax.lang.model
darcy
parents: 9073
diff changeset
  1847
                toP(F.at(catchTypes.head.getStartPosition()).TypeUnion(catchTypes)) :
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1848
                catchTypes.head;
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1849
        JCVariableDecl formal = variableDeclaratorId(mods, paramType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1850
        accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1851
        JCBlock body = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1852
        return F.at(pos).Catch(formal, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1853
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1854
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1855
    List<JCExpression> catchTypes() {
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1856
        ListBuffer<JCExpression> catchTypes = ListBuffer.lb();
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1857
        catchTypes.add(parseType());
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1858
        while (S.token() == BAR) {
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1859
            checkMulticatch();
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1860
            S.nextToken();
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1861
            catchTypes.add(qualident());
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1862
        }
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1863
        return catchTypes.toList();
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1864
    }
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  1865
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1866
    /** SwitchBlockStatementGroups = { SwitchBlockStatementGroup }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1867
     *  SwitchBlockStatementGroup = SwitchLabel BlockStatements
06bc494ca11e Initial load
duke
parents:
diff changeset
  1868
     *  SwitchLabel = CASE ConstantExpression ":" | DEFAULT ":"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1869
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1870
    List<JCCase> switchBlockStatementGroups() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1871
        ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1872
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1873
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1874
            switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1875
            case CASE: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1876
                S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1877
                JCExpression pat = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1878
                accept(COLON);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1879
                List<JCStatement> stats = blockStatements();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1880
                JCCase c = F.at(pos).Case(pat, stats);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1881
                if (stats.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1882
                    storeEnd(c, S.prevEndPos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1883
                cases.append(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1884
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1885
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1886
            case DEFAULT: {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1887
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1888
                accept(COLON);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1889
                List<JCStatement> stats = blockStatements();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1890
                JCCase c = F.at(pos).Case(null, stats);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1891
                if (stats.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  1892
                    storeEnd(c, S.prevEndPos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  1893
                cases.append(c);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1894
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1895
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1896
            case RBRACE: case EOF:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
                return cases.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1898
            default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1899
                S.nextToken(); // to ensure progress
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
                syntaxError(pos, "expected3",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  1901
                    CASE, DEFAULT, RBRACE);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1902
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1903
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1904
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1905
06bc494ca11e Initial load
duke
parents:
diff changeset
  1906
    /** MoreStatementExpressions = { COMMA StatementExpression }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1907
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1908
    <T extends ListBuffer<? super JCExpressionStatement>> T moreStatementExpressions(int pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1909
                                                                    JCExpression first,
06bc494ca11e Initial load
duke
parents:
diff changeset
  1910
                                                                    T stats) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1911
        // This Exec is a "StatementExpression"; it subsumes no terminating token
06bc494ca11e Initial load
duke
parents:
diff changeset
  1912
        stats.append(toP(F.at(pos).Exec(checkExprStat(first))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1913
        while (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1914
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1915
            pos = S.pos();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1916
            JCExpression t = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1917
            // This Exec is a "StatementExpression"; it subsumes no terminating token
06bc494ca11e Initial load
duke
parents:
diff changeset
  1918
            stats.append(toP(F.at(pos).Exec(checkExprStat(t))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1919
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1920
        return stats;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1921
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1922
06bc494ca11e Initial load
duke
parents:
diff changeset
  1923
    /** ForInit = StatementExpression MoreStatementExpressions
06bc494ca11e Initial load
duke
parents:
diff changeset
  1924
     *           |  { FINAL | '@' Annotation } Type VariableDeclarators
06bc494ca11e Initial load
duke
parents:
diff changeset
  1925
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1926
    List<JCStatement> forInit() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1927
        ListBuffer<JCStatement> stats = lb();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1928
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1929
        if (S.token() == FINAL || S.token() == MONKEYS_AT) {
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1930
            return variableDeclarators(optFinal(0), parseType(), stats).toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1931
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1932
            JCExpression t = term(EXPR | TYPE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
            if ((lastmode & TYPE) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1934
                (S.token() == IDENTIFIER || S.token() == ASSERT || S.token() == ENUM))
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
                return variableDeclarators(modifiersOpt(), t, stats).toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1936
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1937
                return moreStatementExpressions(pos, t, stats).toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1938
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1939
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1940
06bc494ca11e Initial load
duke
parents:
diff changeset
  1941
    /** ForUpdate = StatementExpression MoreStatementExpressions
06bc494ca11e Initial load
duke
parents:
diff changeset
  1942
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1943
    List<JCExpressionStatement> forUpdate() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1944
        return moreStatementExpressions(S.pos(),
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1945
                                        parseExpression(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1946
                                        new ListBuffer<JCExpressionStatement>()).toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1947
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1948
06bc494ca11e Initial load
duke
parents:
diff changeset
  1949
    /** AnnotationsOpt = { '@' Annotation }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1950
     */
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1951
    List<JCAnnotation> annotationsOpt() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1952
        if (S.token() != MONKEYS_AT) return List.nil(); // optimization
06bc494ca11e Initial load
duke
parents:
diff changeset
  1953
        ListBuffer<JCAnnotation> buf = new ListBuffer<JCAnnotation>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1954
        while (S.token() == MONKEYS_AT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1955
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1956
            S.nextToken();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1957
            buf.append(annotation(pos));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1958
        }
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1959
        return buf.toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1960
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1961
06bc494ca11e Initial load
duke
parents:
diff changeset
  1962
    /** ModifiersOpt = { Modifier }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1963
     *  Modifier = PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL
06bc494ca11e Initial load
duke
parents:
diff changeset
  1964
     *           | NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE | "@"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1965
     *           | "@" Annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
  1966
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1967
    JCModifiers modifiersOpt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1968
        return modifiersOpt(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1969
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1970
    JCModifiers modifiersOpt(JCModifiers partial) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1971
        long flags;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1972
        ListBuffer<JCAnnotation> annotations = new ListBuffer<JCAnnotation>();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1973
        int pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1974
        if (partial == null) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1975
            flags = 0;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1976
            pos = S.pos();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1977
        } else {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1978
            flags = partial.flags;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1979
            annotations.appendList(partial.annotations);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1980
            pos = partial.pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1981
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1982
        if (S.deprecatedFlag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1983
            flags |= Flags.DEPRECATED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1984
            S.resetDeprecatedFlag();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1985
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1986
        int lastPos = Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1987
    loop:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1988
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
            long flag;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1990
            switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
            case PRIVATE     : flag = Flags.PRIVATE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
            case PROTECTED   : flag = Flags.PROTECTED; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
            case PUBLIC      : flag = Flags.PUBLIC; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1994
            case STATIC      : flag = Flags.STATIC; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1995
            case TRANSIENT   : flag = Flags.TRANSIENT; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
            case FINAL       : flag = Flags.FINAL; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1997
            case ABSTRACT    : flag = Flags.ABSTRACT; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
            case NATIVE      : flag = Flags.NATIVE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1999
            case VOLATILE    : flag = Flags.VOLATILE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2000
            case SYNCHRONIZED: flag = Flags.SYNCHRONIZED; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2001
            case STRICTFP    : flag = Flags.STRICTFP; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2002
            case MONKEYS_AT  : flag = Flags.ANNOTATION; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2003
            default: break loop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2004
            }
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2005
            if ((flags & flag) != 0) error(S.pos(), "repeated.modifier");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2006
            lastPos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2007
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2008
            if (flag == Flags.ANNOTATION) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2009
                checkAnnotations();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2010
                if (S.token() != INTERFACE) {
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2011
                    JCAnnotation ann = annotation(lastPos);
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2012
                    // if first modifier is an annotation, set pos to annotation's.
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2013
                    if (flags == 0 && annotations.isEmpty())
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2014
                        pos = ann.pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2015
                    annotations.append(ann);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2016
                    lastPos = ann.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2017
                    flag = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2018
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2019
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2020
            flags |= flag;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2021
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2022
        switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2023
        case ENUM: flags |= Flags.ENUM; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2024
        case INTERFACE: flags |= Flags.INTERFACE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2025
        default: break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2026
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2027
06bc494ca11e Initial load
duke
parents:
diff changeset
  2028
        /* A modifiers tree with no modifier tokens or annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
  2029
         * has no text position. */
6152
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents: 6148
diff changeset
  2030
        if ((flags & (Flags.ModifierFlags | Flags.ANNOTATION)) == 0 && annotations.isEmpty())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2031
            pos = Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2032
06bc494ca11e Initial load
duke
parents:
diff changeset
  2033
        JCModifiers mods = F.at(pos).Modifiers(flags, annotations.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2034
        if (pos != Position.NOPOS)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2035
            storeEnd(mods, S.prevEndPos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2036
        return mods;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2037
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2038
06bc494ca11e Initial load
duke
parents:
diff changeset
  2039
    /** Annotation              = "@" Qualident [ "(" AnnotationFieldValues ")" ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2040
     * @param pos position of "@" token
06bc494ca11e Initial load
duke
parents:
diff changeset
  2041
     */
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2042
    JCAnnotation annotation(int pos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2043
        // accept(AT); // AT consumed by caller
06bc494ca11e Initial load
duke
parents:
diff changeset
  2044
        checkAnnotations();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2045
        JCTree ident = qualident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2046
        List<JCExpression> fieldValues = annotationFieldValuesOpt();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2047
        JCAnnotation ann = F.at(pos).Annotation(ident, fieldValues);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2048
        storeEnd(ann, S.prevEndPos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2049
        return ann;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2050
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2051
06bc494ca11e Initial load
duke
parents:
diff changeset
  2052
    List<JCExpression> annotationFieldValuesOpt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2053
        return (S.token() == LPAREN) ? annotationFieldValues() : List.<JCExpression>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2054
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2055
06bc494ca11e Initial load
duke
parents:
diff changeset
  2056
    /** AnnotationFieldValues   = "(" [ AnnotationFieldValue { "," AnnotationFieldValue } ] ")" */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2057
    List<JCExpression> annotationFieldValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2058
        accept(LPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2059
        ListBuffer<JCExpression> buf = new ListBuffer<JCExpression>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2060
        if (S.token() != RPAREN) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2061
            buf.append(annotationFieldValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2062
            while (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2063
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2064
                buf.append(annotationFieldValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2065
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2066
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2067
        accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2068
        return buf.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2069
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2070
06bc494ca11e Initial load
duke
parents:
diff changeset
  2071
    /** AnnotationFieldValue    = AnnotationValue
06bc494ca11e Initial load
duke
parents:
diff changeset
  2072
     *                          | Identifier "=" AnnotationValue
06bc494ca11e Initial load
duke
parents:
diff changeset
  2073
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2074
    JCExpression annotationFieldValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2075
        if (S.token() == IDENTIFIER) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2076
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
            JCExpression t1 = term1();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2078
            if (t1.getTag() == JCTree.IDENT && S.token() == EQ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2079
                int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2080
                accept(EQ);
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2081
                JCExpression v = annotationValue();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2082
                return toP(F.at(pos).Assign(t1, v));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2083
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2084
                return t1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2085
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2086
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2087
        return annotationValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2088
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2089
06bc494ca11e Initial load
duke
parents:
diff changeset
  2090
    /* AnnotationValue          = ConditionalExpression
06bc494ca11e Initial load
duke
parents:
diff changeset
  2091
     *                          | Annotation
3997
35250de1d4ea 6337964: should ignore last comma in annotation array
darcy
parents: 3894
diff changeset
  2092
     *                          | "{" [ AnnotationValue { "," AnnotationValue } ] [","] "}"
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2093
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2094
    JCExpression annotationValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2095
        int pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2096
        switch (S.token()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2097
        case MONKEYS_AT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2098
            pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2099
            S.nextToken();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2100
            return annotation(pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2101
        case LBRACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2102
            pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2103
            accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2104
            ListBuffer<JCExpression> buf = new ListBuffer<JCExpression>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2105
            if (S.token() != RBRACE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2106
                buf.append(annotationValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2107
                while (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2108
                    S.nextToken();
3997
35250de1d4ea 6337964: should ignore last comma in annotation array
darcy
parents: 3894
diff changeset
  2109
                    if (S.token() == RBRACE) break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2110
                    buf.append(annotationValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2111
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2112
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2113
            accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2114
            return toP(F.at(pos).NewArray(null, List.<JCExpression>nil(), buf.toList()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2115
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2116
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2117
            return term1();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2118
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2119
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2120
06bc494ca11e Initial load
duke
parents:
diff changeset
  2121
    /** VariableDeclarators = VariableDeclarator { "," VariableDeclarator }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2122
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2123
    public <T extends ListBuffer<? super JCVariableDecl>> T variableDeclarators(JCModifiers mods,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2124
                                                                         JCExpression type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2125
                                                                         T vdefs)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2127
        return variableDeclaratorsRest(S.pos(), mods, type, ident(), false, null, vdefs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2128
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2129
06bc494ca11e Initial load
duke
parents:
diff changeset
  2130
    /** VariableDeclaratorsRest = VariableDeclaratorRest { "," VariableDeclarator }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2131
     *  ConstantDeclaratorsRest = ConstantDeclaratorRest { "," ConstantDeclarator }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2132
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
     *  @param reqInit  Is an initializer always required?
06bc494ca11e Initial load
duke
parents:
diff changeset
  2134
     *  @param dc       The documentation comment for the variable declarations, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2135
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2136
    <T extends ListBuffer<? super JCVariableDecl>> T variableDeclaratorsRest(int pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2137
                                                                     JCModifiers mods,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2138
                                                                     JCExpression type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2139
                                                                     Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2140
                                                                     boolean reqInit,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2141
                                                                     String dc,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2142
                                                                     T vdefs)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2143
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2144
        vdefs.append(variableDeclaratorRest(pos, mods, type, name, reqInit, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2145
        while (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2146
            // All but last of multiple declarators subsume a comma
06bc494ca11e Initial load
duke
parents:
diff changeset
  2147
            storeEnd((JCTree)vdefs.elems.last(), S.endPos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2148
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2149
            vdefs.append(variableDeclarator(mods, type, reqInit, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2151
        return vdefs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2152
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2153
06bc494ca11e Initial load
duke
parents:
diff changeset
  2154
    /** VariableDeclarator = Ident VariableDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  2155
     *  ConstantDeclarator = Ident ConstantDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
    JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean reqInit, String dc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2158
        return variableDeclaratorRest(S.pos(), mods, type, ident(), reqInit, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2160
06bc494ca11e Initial load
duke
parents:
diff changeset
  2161
    /** VariableDeclaratorRest = BracketsOpt ["=" VariableInitializer]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2162
     *  ConstantDeclaratorRest = BracketsOpt "=" VariableInitializer
06bc494ca11e Initial load
duke
parents:
diff changeset
  2163
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2164
     *  @param reqInit  Is an initializer always required?
06bc494ca11e Initial load
duke
parents:
diff changeset
  2165
     *  @param dc       The documentation comment for the variable declarations, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2166
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2167
    JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2168
                                  boolean reqInit, String dc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2169
        type = bracketsOpt(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2170
        JCExpression init = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2171
        if (S.token() == EQ) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2172
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2173
            init = variableInitializer();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2174
        }
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2175
        else if (reqInit) syntaxError(S.pos(), "expected", EQ);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2176
        JCVariableDecl result =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2177
            toP(F.at(pos).VarDef(mods, name, type, init));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2178
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2179
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2180
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2181
06bc494ca11e Initial load
duke
parents:
diff changeset
  2182
    /** VariableDeclaratorId = Ident BracketsOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  2183
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2184
    JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2185
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2186
        Name name = ident();
8047
c7f08cdb5c3c 6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents: 8033
diff changeset
  2187
        if ((mods.flags & Flags.VARARGS) != 0 &&
c7f08cdb5c3c 6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents: 8033
diff changeset
  2188
                S.token() == LBRACKET) {
c7f08cdb5c3c 6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents: 8033
diff changeset
  2189
            log.error(S.pos(), "varargs.and.old.array.syntax");
c7f08cdb5c3c 6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents: 8033
diff changeset
  2190
        }
c7f08cdb5c3c 6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents: 8033
diff changeset
  2191
        type = bracketsOpt(type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2192
        return toP(F.at(pos).VarDef(mods, name, type, null));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2193
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2194
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2195
    /** Resources = Resource { ";" Resources }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2196
     */
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2197
    List<JCTree> resources() {
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2198
        ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2199
        defs.append(resource());
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2200
        while (S.token() == SEMI) {
8234
5050975e2df0 7014734: Project Coin: Allow optional trailing semicolon to terminate resources list in try-with-resources
darcy
parents: 8224
diff changeset
  2201
            // All but last of multiple declarators must subsume a semicolon
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2202
            storeEnd(defs.elems.last(), S.endPos());
8224
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  2203
            int semiColonPos = S.pos();
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2204
            S.nextToken();
8234
5050975e2df0 7014734: Project Coin: Allow optional trailing semicolon to terminate resources list in try-with-resources
darcy
parents: 8224
diff changeset
  2205
            if (S.token() == RPAREN) { // Optional trailing semicolon
8224
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  2206
                                       // after last resource
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  2207
                break;
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  2208
            }
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2209
            defs.append(resource());
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2210
        }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2211
        return defs.toList();
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2212
    }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2213
8224
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  2214
    /** Resource = VariableModifiersOpt Type VariableDeclaratorId = Expression
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2215
     */
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2216
    JCTree resource() {
8224
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  2217
        return variableDeclaratorRest(S.pos(), optFinal(Flags.FINAL),
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  2218
                                      parseType(), ident(), true, null);
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2219
    }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2220
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2221
    /** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration}
06bc494ca11e Initial load
duke
parents:
diff changeset
  2222
     */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2223
    public JCTree.JCCompilationUnit parseCompilationUnit() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2224
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2225
        JCExpression pid = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2226
        String dc = S.docComment();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2227
        JCModifiers mods = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2228
        List<JCAnnotation> packageAnnotations = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2229
        if (S.token() == MONKEYS_AT)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2230
            mods = modifiersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2231
06bc494ca11e Initial load
duke
parents:
diff changeset
  2232
        if (S.token() == PACKAGE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2233
            if (mods != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2234
                checkNoMods(mods.flags);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2235
                packageAnnotations = mods.annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2236
                mods = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2237
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2238
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2239
            pid = qualident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2240
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2241
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2242
        ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2243
        boolean checkForImports = true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2244
        while (S.token() != EOF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2245
            if (S.pos() <= errorEndPos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2246
                // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  2247
                skip(checkForImports, false, false, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2248
                if (S.token() == EOF)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2249
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2250
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2251
            if (checkForImports && mods == null && S.token() == IMPORT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2252
                defs.append(importDeclaration());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2253
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2254
                JCTree def = typeDeclaration(mods);
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6585
diff changeset
  2255
                if (keepDocComments && dc != null && docComments.get(def) == dc) {
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6585
diff changeset
  2256
                    // If the first type declaration has consumed the first doc
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6585
diff changeset
  2257
                    // comment, then don't use it for the top level comment as well.
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6585
diff changeset
  2258
                    dc = null;
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6585
diff changeset
  2259
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2260
                if (def instanceof JCExpressionStatement)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2261
                    def = ((JCExpressionStatement)def).expr;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2262
                defs.append(def);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2263
                if (def instanceof JCClassDecl)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2264
                    checkForImports = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2265
                mods = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2266
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2267
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2268
        JCTree.JCCompilationUnit toplevel = F.at(pos).TopLevel(packageAnnotations, pid, defs.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2269
        attach(toplevel, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2270
        if (defs.elems.isEmpty())
06bc494ca11e Initial load
duke
parents:
diff changeset
  2271
            storeEnd(toplevel, S.prevEndPos());
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2272
        if (keepDocComments)
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2273
            toplevel.docComments = docComments;
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2274
        if (keepLineMap)
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2275
            toplevel.lineMap = S.getLineMap();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2276
        return toplevel;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2277
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2278
06bc494ca11e Initial load
duke
parents:
diff changeset
  2279
    /** ImportDeclaration = IMPORT [ STATIC ] Ident { "." Ident } [ "." "*" ] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2280
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2281
    JCTree importDeclaration() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2282
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2283
        S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2284
        boolean importStatic = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2285
        if (S.token() == STATIC) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2286
            checkStaticImports();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2287
            importStatic = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2288
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2289
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2290
        JCExpression pid = toP(F.at(S.pos()).Ident(ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2291
        do {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2292
            int pos1 = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2293
            accept(DOT);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2294
            if (S.token() == STAR) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2295
                pid = to(F.at(pos1).Select(pid, names.asterisk));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2296
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2297
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2298
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2299
                pid = toP(F.at(pos1).Select(pid, ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2300
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2301
        } while (S.token() == DOT);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2302
        accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2303
        return toP(F.at(pos).Import(pid, importStatic));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2304
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2305
06bc494ca11e Initial load
duke
parents:
diff changeset
  2306
    /** TypeDeclaration = ClassOrInterfaceOrEnumDeclaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  2307
     *                  | ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2308
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2309
    JCTree typeDeclaration(JCModifiers mods) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2310
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2311
        if (mods == null && S.token() == SEMI) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2312
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2313
            return toP(F.at(pos).Skip());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2314
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2315
            String dc = S.docComment();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2316
            return classOrInterfaceOrEnumDeclaration(modifiersOpt(mods), dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2317
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2318
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2319
06bc494ca11e Initial load
duke
parents:
diff changeset
  2320
    /** ClassOrInterfaceOrEnumDeclaration = ModifiersOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  2321
     *           (ClassDeclaration | InterfaceDeclaration | EnumDeclaration)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2322
     *  @param mods     Any modifiers starting the class or interface declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  2323
     *  @param dc       The documentation comment for the class, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2324
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2325
    JCStatement classOrInterfaceOrEnumDeclaration(JCModifiers mods, String dc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2326
        if (S.token() == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2327
            return classDeclaration(mods, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2328
        } else if (S.token() == INTERFACE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2329
            return interfaceDeclaration(mods, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2330
        } else if (allowEnums) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2331
            if (S.token() == ENUM) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2332
                return enumDeclaration(mods, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2333
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2334
                int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2335
                List<JCTree> errs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2336
                if (S.token() == IDENTIFIER) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2337
                    errs = List.<JCTree>of(mods, toP(F.at(pos).Ident(ident())));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2338
                    setErrorEndPos(S.pos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2339
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2340
                    errs = List.<JCTree>of(mods);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2341
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2342
                return toP(F.Exec(syntaxError(pos, errs, "expected3",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2343
                                              CLASS, INTERFACE, ENUM)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2344
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2345
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2346
            if (S.token() == ENUM) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2347
                error(S.pos(), "enums.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2348
                allowEnums = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2349
                return enumDeclaration(mods, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2350
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2351
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2352
            List<JCTree> errs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2353
            if (S.token() == IDENTIFIER) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2354
                errs = List.<JCTree>of(mods, toP(F.at(pos).Ident(ident())));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2355
                setErrorEndPos(S.pos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2356
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2357
                errs = List.<JCTree>of(mods);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2358
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2359
            return toP(F.Exec(syntaxError(pos, errs, "expected2",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2360
                                          CLASS, INTERFACE)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2361
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2362
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2363
06bc494ca11e Initial load
duke
parents:
diff changeset
  2364
    /** ClassDeclaration = CLASS Ident TypeParametersOpt [EXTENDS Type]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2365
     *                     [IMPLEMENTS TypeList] ClassBody
06bc494ca11e Initial load
duke
parents:
diff changeset
  2366
     *  @param mods    The modifiers starting the class declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  2367
     *  @param dc       The documentation comment for the class, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2368
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2369
    JCClassDecl classDeclaration(JCModifiers mods, String dc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2370
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2371
        accept(CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2372
        Name name = ident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2373
06bc494ca11e Initial load
duke
parents:
diff changeset
  2374
        List<JCTypeParameter> typarams = typeParametersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2375
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8234
diff changeset
  2376
        JCExpression extending = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2377
        if (S.token() == EXTENDS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2378
            S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2379
            extending = parseType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2380
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2381
        List<JCExpression> implementing = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2382
        if (S.token() == IMPLEMENTS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2383
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2384
            implementing = typeList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2385
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2386
        List<JCTree> defs = classOrInterfaceBody(name, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2387
        JCClassDecl result = toP(F.at(pos).ClassDef(
06bc494ca11e Initial load
duke
parents:
diff changeset
  2388
            mods, name, typarams, extending, implementing, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2389
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2390
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2391
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2392
06bc494ca11e Initial load
duke
parents:
diff changeset
  2393
    /** InterfaceDeclaration = INTERFACE Ident TypeParametersOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  2394
     *                         [EXTENDS TypeList] InterfaceBody
06bc494ca11e Initial load
duke
parents:
diff changeset
  2395
     *  @param mods    The modifiers starting the interface declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  2396
     *  @param dc       The documentation comment for the interface, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2397
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2398
    JCClassDecl interfaceDeclaration(JCModifiers mods, String dc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2399
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2400
        accept(INTERFACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2401
        Name name = ident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2402
06bc494ca11e Initial load
duke
parents:
diff changeset
  2403
        List<JCTypeParameter> typarams = typeParametersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2404
06bc494ca11e Initial load
duke
parents:
diff changeset
  2405
        List<JCExpression> extending = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2406
        if (S.token() == EXTENDS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2407
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2408
            extending = typeList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2409
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2410
        List<JCTree> defs = classOrInterfaceBody(name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2411
        JCClassDecl result = toP(F.at(pos).ClassDef(
06bc494ca11e Initial load
duke
parents:
diff changeset
  2412
            mods, name, typarams, null, extending, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2413
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2414
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2415
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2416
06bc494ca11e Initial load
duke
parents:
diff changeset
  2417
    /** EnumDeclaration = ENUM Ident [IMPLEMENTS TypeList] EnumBody
06bc494ca11e Initial load
duke
parents:
diff changeset
  2418
     *  @param mods    The modifiers starting the enum declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  2419
     *  @param dc       The documentation comment for the enum, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2420
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2421
    JCClassDecl enumDeclaration(JCModifiers mods, String dc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2422
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2423
        accept(ENUM);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2424
        Name name = ident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2425
06bc494ca11e Initial load
duke
parents:
diff changeset
  2426
        List<JCExpression> implementing = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2427
        if (S.token() == IMPLEMENTS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2428
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2429
            implementing = typeList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2430
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2431
06bc494ca11e Initial load
duke
parents:
diff changeset
  2432
        List<JCTree> defs = enumBody(name);
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2433
        mods.flags |= Flags.ENUM;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2434
        JCClassDecl result = toP(F.at(pos).
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2435
            ClassDef(mods, name, List.<JCTypeParameter>nil(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2436
                null, implementing, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2437
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2438
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2439
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2440
06bc494ca11e Initial load
duke
parents:
diff changeset
  2441
    /** EnumBody = "{" { EnumeratorDeclarationList } [","]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2442
     *                  [ ";" {ClassBodyDeclaration} ] "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2443
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2444
    List<JCTree> enumBody(Name enumName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2445
        accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2446
        ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2447
        if (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2448
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2449
        } else if (S.token() != RBRACE && S.token() != SEMI) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2450
            defs.append(enumeratorDeclaration(enumName));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2451
            while (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2452
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2453
                if (S.token() == RBRACE || S.token() == SEMI) break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2454
                defs.append(enumeratorDeclaration(enumName));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2455
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2456
            if (S.token() != SEMI && S.token() != RBRACE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2457
                defs.append(syntaxError(S.pos(), "expected3",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2458
                                COMMA, RBRACE, SEMI));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2459
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2460
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2461
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2462
        if (S.token() == SEMI) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2463
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2464
            while (S.token() != RBRACE && S.token() != EOF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2465
                defs.appendList(classOrInterfaceBodyDeclaration(enumName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2466
                                                                false));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2467
                if (S.pos() <= errorEndPos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2468
                    // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  2469
                   skip(false, true, true, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2470
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2471
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2472
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2473
        accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2474
        return defs.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2475
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2476
06bc494ca11e Initial load
duke
parents:
diff changeset
  2477
    /** EnumeratorDeclaration = AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2478
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2479
    JCTree enumeratorDeclaration(Name enumName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2480
        String dc = S.docComment();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2481
        int flags = Flags.PUBLIC|Flags.STATIC|Flags.FINAL|Flags.ENUM;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2482
        if (S.deprecatedFlag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2483
            flags |= Flags.DEPRECATED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2484
            S.resetDeprecatedFlag();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2485
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2486
        int pos = S.pos();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2487
        List<JCAnnotation> annotations = annotationsOpt();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2488
        JCModifiers mods = F.at(annotations.isEmpty() ? Position.NOPOS : pos).Modifiers(flags, annotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2489
        List<JCExpression> typeArgs = typeArgumentsOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2490
        int identPos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2491
        Name name = ident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2492
        int createPos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2493
        List<JCExpression> args = (S.token() == LPAREN)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2494
            ? arguments() : List.<JCExpression>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2495
        JCClassDecl body = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2496
        if (S.token() == LBRACE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2497
            JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM | Flags.STATIC);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2498
            List<JCTree> defs = classOrInterfaceBody(names.empty, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2499
            body = toP(F.at(identPos).AnonymousClassDef(mods1, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2500
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2501
        if (args.isEmpty() && body == null)
4704
5206047418c2 6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents: 3997
diff changeset
  2502
            createPos = identPos;
5206047418c2 6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents: 3997
diff changeset
  2503
        JCIdent ident = F.at(identPos).Ident(enumName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2504
        JCNewClass create = F.at(createPos).NewClass(null, typeArgs, ident, args, body);
4704
5206047418c2 6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents: 3997
diff changeset
  2505
        if (createPos != identPos)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2506
            storeEnd(create, S.prevEndPos());
4704
5206047418c2 6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents: 3997
diff changeset
  2507
        ident = F.at(identPos).Ident(enumName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2508
        JCTree result = toP(F.at(pos).VarDef(mods, name, ident, create));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2509
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2510
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2511
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2512
06bc494ca11e Initial load
duke
parents:
diff changeset
  2513
    /** TypeList = Type {"," Type}
06bc494ca11e Initial load
duke
parents:
diff changeset
  2514
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2515
    List<JCExpression> typeList() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2516
        ListBuffer<JCExpression> ts = new ListBuffer<JCExpression>();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2517
        ts.append(parseType());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2518
        while (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2519
            S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2520
            ts.append(parseType());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2521
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2522
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2523
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2524
06bc494ca11e Initial load
duke
parents:
diff changeset
  2525
    /** ClassBody     = "{" {ClassBodyDeclaration} "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2526
     *  InterfaceBody = "{" {InterfaceBodyDeclaration} "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2527
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2528
    List<JCTree> classOrInterfaceBody(Name className, boolean isInterface) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2529
        accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2530
        if (S.pos() <= errorEndPos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2531
            // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  2532
            skip(false, true, false, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2533
            if (S.token() == LBRACE)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2534
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2535
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2536
        ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2537
        while (S.token() != RBRACE && S.token() != EOF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2538
            defs.appendList(classOrInterfaceBodyDeclaration(className, isInterface));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2539
            if (S.pos() <= errorEndPos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2540
               // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  2541
               skip(false, true, true, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2542
           }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2543
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2544
        accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2545
        return defs.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2546
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2547
06bc494ca11e Initial load
duke
parents:
diff changeset
  2548
    /** ClassBodyDeclaration =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2549
     *      ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2550
     *    | [STATIC] Block
06bc494ca11e Initial load
duke
parents:
diff changeset
  2551
     *    | ModifiersOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  2552
     *      ( Type Ident
06bc494ca11e Initial load
duke
parents:
diff changeset
  2553
     *        ( VariableDeclaratorsRest ";" | MethodDeclaratorRest )
06bc494ca11e Initial load
duke
parents:
diff changeset
  2554
     *      | VOID Ident MethodDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  2555
     *      | TypeParameters (Type | VOID) Ident MethodDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  2556
     *      | Ident ConstructorDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  2557
     *      | TypeParameters Ident ConstructorDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  2558
     *      | ClassOrInterfaceOrEnumDeclaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  2559
     *      )
06bc494ca11e Initial load
duke
parents:
diff changeset
  2560
     *  InterfaceBodyDeclaration =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2561
     *      ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2562
     *    | ModifiersOpt Type Ident
06bc494ca11e Initial load
duke
parents:
diff changeset
  2563
     *      ( ConstantDeclaratorsRest | InterfaceMethodDeclaratorRest ";" )
06bc494ca11e Initial load
duke
parents:
diff changeset
  2564
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2565
    List<JCTree> classOrInterfaceBodyDeclaration(Name className, boolean isInterface) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2566
        if (S.token() == SEMI) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2567
            S.nextToken();
6585
189f53daed52 6921495: spurious semicolons in class def cause empty NOPOS blocks
jjg
parents: 6152
diff changeset
  2568
            return List.<JCTree>nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2569
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2570
            String dc = S.docComment();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2571
            int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2572
            JCModifiers mods = modifiersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2573
            if (S.token() == CLASS ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2574
                S.token() == INTERFACE ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  2575
                allowEnums && S.token() == ENUM) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2576
                return List.<JCTree>of(classOrInterfaceOrEnumDeclaration(mods, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2577
            } else if (S.token() == LBRACE && !isInterface &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2578
                       (mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  2579
                       mods.annotations.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2580
                return List.<JCTree>of(block(pos, mods.flags));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2581
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2582
                pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2583
                List<JCTypeParameter> typarams = typeParametersOpt();
8033
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  2584
                // if there are type parameters but no modifiers, save the start
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  2585
                // position of the method in the modifiers.
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  2586
                if (typarams.nonEmpty() && mods.pos == Position.NOPOS) {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  2587
                    mods.pos = pos;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  2588
                    storeEnd(mods, pos);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  2589
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2590
                Name name = S.name();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2591
                pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2592
                JCExpression type;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2593
                boolean isVoid = S.token() == VOID;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2594
                if (isVoid) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2595
                    type = to(F.at(pos).TypeIdent(TypeTags.VOID));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2596
                    S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2597
                } else {
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2598
                    type = parseType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2599
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2600
                if (S.token() == LPAREN && !isInterface && type.getTag() == JCTree.IDENT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2601
                    if (isInterface || name != className)
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2602
                        error(pos, "invalid.meth.decl.ret.type.req");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2603
                    return List.of(methodDeclaratorRest(
06bc494ca11e Initial load
duke
parents:
diff changeset
  2604
                        pos, mods, null, names.init, typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2605
                        isInterface, true, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2606
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2607
                    pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2608
                    name = ident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2609
                    if (S.token() == LPAREN) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2610
                        return List.of(methodDeclaratorRest(
06bc494ca11e Initial load
duke
parents:
diff changeset
  2611
                            pos, mods, type, name, typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2612
                            isInterface, isVoid, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2613
                    } else if (!isVoid && typarams.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2614
                        List<JCTree> defs =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2615
                            variableDeclaratorsRest(pos, mods, type, name, isInterface, dc,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2616
                                                    new ListBuffer<JCTree>()).toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2617
                        storeEnd(defs.last(), S.endPos());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2618
                        accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2619
                        return defs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2620
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2621
                        pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2622
                        List<JCTree> err = isVoid
06bc494ca11e Initial load
duke
parents:
diff changeset
  2623
                            ? List.<JCTree>of(toP(F.at(pos).MethodDef(mods, name, type, typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2624
                                List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null)))
06bc494ca11e Initial load
duke
parents:
diff changeset
  2625
                            : null;
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2626
                        return List.<JCTree>of(syntaxError(S.pos(), err, "expected", LPAREN));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2627
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2628
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2629
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2630
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2631
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2632
06bc494ca11e Initial load
duke
parents:
diff changeset
  2633
    /** MethodDeclaratorRest =
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2634
     *      FormalParameters BracketsOpt [Throws TypeList] ( MethodBody | [DEFAULT AnnotationValue] ";")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2635
     *  VoidMethodDeclaratorRest =
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2636
     *      FormalParameters [Throws TypeList] ( MethodBody | ";")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2637
     *  InterfaceMethodDeclaratorRest =
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2638
     *      FormalParameters BracketsOpt [THROWS TypeList] ";"
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2639
     *  VoidInterfaceMethodDeclaratorRest =
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2640
     *      FormalParameters [THROWS TypeList] ";"
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2641
     *  ConstructorDeclaratorRest =
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2642
     *      "(" FormalParameterListOpt ")" [THROWS TypeList] MethodBody
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2643
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2644
    JCTree methodDeclaratorRest(int pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2645
                              JCModifiers mods,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2646
                              JCExpression type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2647
                              Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2648
                              List<JCTypeParameter> typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2649
                              boolean isInterface, boolean isVoid,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2650
                              String dc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2651
        List<JCVariableDecl> params = formalParameters();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2652
        if (!isVoid) type = bracketsOpt(type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2653
        List<JCExpression> thrown = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2654
        if (S.token() == THROWS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2655
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2656
            thrown = qualidentList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2657
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2658
        JCBlock body = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2659
        JCExpression defaultValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2660
        if (S.token() == LBRACE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2661
            body = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2662
            defaultValue = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2663
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2664
            if (S.token() == DEFAULT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2665
                accept(DEFAULT);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2666
                defaultValue = annotationValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2667
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2668
                defaultValue = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2669
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2670
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2671
            if (S.pos() <= errorEndPos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2672
                // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  2673
                skip(false, true, false, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2674
                if (S.token() == LBRACE) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2675
                    body = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2676
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2677
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2678
        }
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2679
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2680
        JCMethodDecl result =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2681
            toP(F.at(pos).MethodDef(mods, name, type, typarams,
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2682
                                    params, thrown,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2683
                                    body, defaultValue));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2684
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2685
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2686
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2687
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2688
    /** QualidentList = Qualident {"," Qualident}
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2689
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2690
    List<JCExpression> qualidentList() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2691
        ListBuffer<JCExpression> ts = new ListBuffer<JCExpression>();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2692
        ts.append(qualident());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2693
        while (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2694
            S.nextToken();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2695
            ts.append(qualident());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2696
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2697
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2698
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2699
06bc494ca11e Initial load
duke
parents:
diff changeset
  2700
    /** TypeParametersOpt = ["<" TypeParameter {"," TypeParameter} ">"]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2701
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2702
    List<JCTypeParameter> typeParametersOpt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2703
        if (S.token() == LT) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2704
            checkGenerics();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2705
            ListBuffer<JCTypeParameter> typarams = new ListBuffer<JCTypeParameter>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2706
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2707
            typarams.append(typeParameter());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2708
            while (S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2709
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2710
                typarams.append(typeParameter());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2711
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2712
            accept(GT);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2713
            return typarams.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2714
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2715
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2716
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2717
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2718
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2719
    /** TypeParameter = TypeVariable [TypeParameterBound]
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2720
     *  TypeParameterBound = EXTENDS Type {"&" Type}
06bc494ca11e Initial load
duke
parents:
diff changeset
  2721
     *  TypeVariable = Ident
06bc494ca11e Initial load
duke
parents:
diff changeset
  2722
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2723
    JCTypeParameter typeParameter() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2724
        int pos = S.pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2725
        Name name = ident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2726
        ListBuffer<JCExpression> bounds = new ListBuffer<JCExpression>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2727
        if (S.token() == EXTENDS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2728
            S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2729
            bounds.append(parseType());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2730
            while (S.token() == AMP) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2731
                S.nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2732
                bounds.append(parseType());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2733
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2734
        }
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2735
        return toP(F.at(pos).TypeParameter(name, bounds.toList()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2736
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2737
06bc494ca11e Initial load
duke
parents:
diff changeset
  2738
    /** FormalParameters = "(" [ FormalParameterList ] ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2739
     *  FormalParameterList = [ FormalParameterListNovarargs , ] LastFormalParameter
06bc494ca11e Initial load
duke
parents:
diff changeset
  2740
     *  FormalParameterListNovarargs = [ FormalParameterListNovarargs , ] FormalParameter
06bc494ca11e Initial load
duke
parents:
diff changeset
  2741
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2742
    List<JCVariableDecl> formalParameters() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2743
        ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2744
        JCVariableDecl lastParam = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2745
        accept(LPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2746
        if (S.token() != RPAREN) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2747
            params.append(lastParam = formalParameter());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2748
            while ((lastParam.mods.flags & Flags.VARARGS) == 0 && S.token() == COMMA) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2749
                S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2750
                params.append(lastParam = formalParameter());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2751
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2752
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2753
        accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2754
        return params.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2755
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2756
06bc494ca11e Initial load
duke
parents:
diff changeset
  2757
    JCModifiers optFinal(long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2758
        JCModifiers mods = modifiersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2759
        checkNoMods(mods.flags & ~(Flags.FINAL | Flags.DEPRECATED));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2760
        mods.flags |= flags;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2761
        return mods;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2762
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2763
06bc494ca11e Initial load
duke
parents:
diff changeset
  2764
    /** FormalParameter = { FINAL | '@' Annotation } Type VariableDeclaratorId
06bc494ca11e Initial load
duke
parents:
diff changeset
  2765
     *  LastFormalParameter = { FINAL | '@' Annotation } Type '...' Ident | FormalParameter
06bc494ca11e Initial load
duke
parents:
diff changeset
  2766
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2767
    JCVariableDecl formalParameter() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2768
        JCModifiers mods = optFinal(Flags.PARAMETER);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2769
        JCExpression type = parseType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2770
        if (S.token() == ELLIPSIS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2771
            checkVarargs();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2772
            mods.flags |= Flags.VARARGS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2773
            type = to(F.at(S.pos()).TypeArray(type));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2774
            S.nextToken();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2775
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2776
        return variableDeclaratorId(mods, type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2777
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2778
06bc494ca11e Initial load
duke
parents:
diff changeset
  2779
/* ---------- auxiliary methods -------------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2780
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2781
    void error(int pos, String key, Object ... args) {
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2782
        log.error(DiagnosticFlag.SYNTAX, pos, key, args);
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2783
    }
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2784
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2785
    void warning(int pos, String key, Object ... args) {
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2786
        log.warning(pos, key, args);
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2787
    }
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2788
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2789
    /** Check that given tree is a legal expression statement.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2790
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2791
    protected JCExpression checkExprStat(JCExpression t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2792
        switch(t.getTag()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2793
        case JCTree.PREINC: case JCTree.PREDEC:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2794
        case JCTree.POSTINC: case JCTree.POSTDEC:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2795
        case JCTree.ASSIGN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2796
        case JCTree.BITOR_ASG: case JCTree.BITXOR_ASG: case JCTree.BITAND_ASG:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2797
        case JCTree.SL_ASG: case JCTree.SR_ASG: case JCTree.USR_ASG:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2798
        case JCTree.PLUS_ASG: case JCTree.MINUS_ASG:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2799
        case JCTree.MUL_ASG: case JCTree.DIV_ASG: case JCTree.MOD_ASG:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2800
        case JCTree.APPLY: case JCTree.NEWCLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2801
        case JCTree.ERRONEOUS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2802
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2803
        default:
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2804
            error(t.pos, "not.stmt");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2805
            return F.at(t.pos).Erroneous(List.<JCTree>of(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2806
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2807
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2808
06bc494ca11e Initial load
duke
parents:
diff changeset
  2809
    /** Return precedence of operator represented by token,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2810
     *  -1 if token is not a binary operator. @see TreeInfo.opPrec
06bc494ca11e Initial load
duke
parents:
diff changeset
  2811
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2812
    static int prec(Token token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2813
        int oc = optag(token);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2814
        return (oc >= 0) ? TreeInfo.opPrec(oc) : -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2815
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2816
5013
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2817
    /**
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2818
     * Return the lesser of two positions, making allowance for either one
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2819
     * being unset.
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2820
     */
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2821
    static int earlier(int pos1, int pos2) {
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2822
        if (pos1 == Position.NOPOS)
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2823
            return pos2;
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2824
        if (pos2 == Position.NOPOS)
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2825
            return pos1;
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2826
        return (pos1 < pos2 ? pos1 : pos2);
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2827
    }
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  2828
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2829
    /** Return operation tag of binary operator represented by token,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2830
     *  -1 if token is not a binary operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2831
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2832
    static int optag(Token token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2833
        switch (token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2834
        case BARBAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2835
            return JCTree.OR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2836
        case AMPAMP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2837
            return JCTree.AND;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2838
        case BAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2839
            return JCTree.BITOR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2840
        case BAREQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2841
            return JCTree.BITOR_ASG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2842
        case CARET:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2843
            return JCTree.BITXOR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2844
        case CARETEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2845
            return JCTree.BITXOR_ASG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2846
        case AMP:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2847
            return JCTree.BITAND;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2848
        case AMPEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2849
            return JCTree.BITAND_ASG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2850
        case EQEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2851
            return JCTree.EQ;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2852
        case BANGEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2853
            return JCTree.NE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2854
        case LT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2855
            return JCTree.LT;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2856
        case GT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2857
            return JCTree.GT;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2858
        case LTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2859
            return JCTree.LE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2860
        case GTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2861
            return JCTree.GE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2862
        case LTLT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2863
            return JCTree.SL;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2864
        case LTLTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2865
            return JCTree.SL_ASG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2866
        case GTGT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2867
            return JCTree.SR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2868
        case GTGTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2869
            return JCTree.SR_ASG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2870
        case GTGTGT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2871
            return JCTree.USR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2872
        case GTGTGTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2873
            return JCTree.USR_ASG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2874
        case PLUS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2875
            return JCTree.PLUS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2876
        case PLUSEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2877
            return JCTree.PLUS_ASG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2878
        case SUB:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2879
            return JCTree.MINUS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2880
        case SUBEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2881
            return JCTree.MINUS_ASG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2882
        case STAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2883
            return JCTree.MUL;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2884
        case STAREQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2885
            return JCTree.MUL_ASG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2886
        case SLASH:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2887
            return JCTree.DIV;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2888
        case SLASHEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2889
            return JCTree.DIV_ASG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2890
        case PERCENT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2891
            return JCTree.MOD;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2892
        case PERCENTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2893
            return JCTree.MOD_ASG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2894
        case INSTANCEOF:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2895
            return JCTree.TYPETEST;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2896
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2897
            return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2898
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2899
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2900
06bc494ca11e Initial load
duke
parents:
diff changeset
  2901
    /** Return operation tag of unary operator represented by token,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2902
     *  -1 if token is not a binary operator.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2903
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2904
    static int unoptag(Token token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2905
        switch (token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2906
        case PLUS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2907
            return JCTree.POS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2908
        case SUB:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2909
            return JCTree.NEG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2910
        case BANG:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2911
            return JCTree.NOT;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2912
        case TILDE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2913
            return JCTree.COMPL;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2914
        case PLUSPLUS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2915
            return JCTree.PREINC;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2916
        case SUBSUB:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2917
            return JCTree.PREDEC;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2918
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2919
            return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2920
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2921
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2922
06bc494ca11e Initial load
duke
parents:
diff changeset
  2923
    /** Return type tag of basic type represented by token,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2924
     *  -1 if token is not a basic type identifier.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2925
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2926
    static int typetag(Token token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2927
        switch (token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2928
        case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2929
            return TypeTags.BYTE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2930
        case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2931
            return TypeTags.CHAR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2932
        case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2933
            return TypeTags.SHORT;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2934
        case INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2935
            return TypeTags.INT;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2936
        case LONG:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2937
            return TypeTags.LONG;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2938
        case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2939
            return TypeTags.FLOAT;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2940
        case DOUBLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2941
            return TypeTags.DOUBLE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2942
        case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2943
            return TypeTags.BOOLEAN;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2944
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2945
            return -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2946
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2947
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2948
06bc494ca11e Initial load
duke
parents:
diff changeset
  2949
    void checkGenerics() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2950
        if (!allowGenerics) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2951
            error(S.pos(), "generics.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2952
            allowGenerics = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2953
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2954
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2955
    void checkVarargs() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2956
        if (!allowVarargs) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2957
            error(S.pos(), "varargs.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2958
            allowVarargs = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2959
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2960
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2961
    void checkForeach() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2962
        if (!allowForeach) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2963
            error(S.pos(), "foreach.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2964
            allowForeach = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2965
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2966
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2967
    void checkStaticImports() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2968
        if (!allowStaticImport) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2969
            error(S.pos(), "static.import.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2970
            allowStaticImport = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2971
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2972
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2973
    void checkAnnotations() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2974
        if (!allowAnnotations) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2975
            error(S.pos(), "annotations.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2976
            allowAnnotations = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2977
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2978
    }
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  2979
    void checkDiamond() {
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  2980
        if (!allowDiamond) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2981
            error(S.pos(), "diamond.not.supported.in.source", source.name);
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  2982
            allowDiamond = true;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  2983
        }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  2984
    }
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2985
    void checkMulticatch() {
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2986
        if (!allowMulticatch) {
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2987
            error(S.pos(), "multicatch.not.supported.in.source", source.name);
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2988
            allowMulticatch = true;
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2989
        }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2990
    }
7211
163fe60f63de 6970016: Clean up ARM/try-with-resources implementation
mcimadamore
parents: 7076
diff changeset
  2991
    void checkTryWithResources() {
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2992
        if (!allowTWR) {
7211
163fe60f63de 6970016: Clean up ARM/try-with-resources implementation
mcimadamore
parents: 7076
diff changeset
  2993
            error(S.pos(), "try.with.resources.not.supported.in.source", source.name);
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2994
            allowTWR = true;
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2995
        }
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2996
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2997
}