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