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