langtools/src/share/classes/com/sun/tools/javac/parser/JavacParser.java
author vromero
Fri, 08 Feb 2013 09:15:27 +0000
changeset 15711 a975ad2bfe35
parent 15385 ee1eebe7e210
child 15718 8e54c8e43d38
permissions -rw-r--r--
8005931: javac doesn't set ACC_STRICT for classes with package access Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
     2
 * Copyright (c) 1999, 2013, 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
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
    30
import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
    31
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
    32
import com.sun.tools.javac.code.*;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
    33
import com.sun.tools.javac.parser.Tokens.*;
10948
063463f6535f 7104201: Refactor DocCommentScanner
mcimadamore
parents: 10815
diff changeset
    34
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.tree.*;
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
    36
import com.sun.tools.javac.tree.JCTree.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.util.*;
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
    38
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
    39
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
    42
import static com.sun.tools.javac.parser.Tokens.TokenKind.*;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
    43
import static com.sun.tools.javac.parser.Tokens.TokenKind.ASSERT;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
    44
import static com.sun.tools.javac.parser.Tokens.TokenKind.CASE;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
    45
import static com.sun.tools.javac.parser.Tokens.TokenKind.CATCH;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
    46
import static com.sun.tools.javac.parser.Tokens.TokenKind.EQ;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
    47
import static com.sun.tools.javac.parser.Tokens.TokenKind.GT;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
    48
import static com.sun.tools.javac.parser.Tokens.TokenKind.IMPORT;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
    49
import static com.sun.tools.javac.parser.Tokens.TokenKind.LT;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
    50
import static com.sun.tools.javac.tree.JCTree.Tag.*;
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
    51
import static com.sun.tools.javac.util.ListBuffer.lb;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
/** The parser maps a token sequence into an abstract syntax
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *  tree. It operates by recursive descent, with code derived
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *  systematically from an LL(1) grammar. For efficiency reasons, an
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 *  operator precedence scheme is used for parsing binary operation
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 *  expressions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    59
 *  <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
    60
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
    64
public class JavacParser implements Parser {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    /** The number of precedence levels of infix operators.
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    private static final int infixPrecedenceLevels = 10;
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    /** The scanner used for lexical analysis.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     */
10200
56cc93e7b6ef 7068902: (javac) allow enabling or disabling of String folding
ksrini
parents: 10199
diff changeset
    72
    protected Lexer S;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    /** The factory to be used for abstract syntax tree construction.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    protected TreeMaker F;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    /** The log to be used for error diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    private Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    /** The Source language setting. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    private Source source;
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /** The name table. */
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1258
diff changeset
    86
    private Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
    88
    /** End position mappings container */
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
    89
    private final AbstractEndPosTable endPosTable;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
    90
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
    91
    // Because of javac's limited lookahead, some contexts are ambiguous in
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
    92
    // the presence of type annotations even though they are not ambiguous
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
    93
    // in the absence of type annotations.  Consider this code:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
    94
    //   void m(String [] m) { }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
    95
    //   void m(String ... m) { }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
    96
    // After parsing "String", javac calls bracketsOpt which immediately
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
    97
    // returns if the next character is not '['.  Similarly, javac can see
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
    98
    // if the next token is ... and in that case parse an ellipsis.  But in
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
    99
    // the presence of type annotations:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   100
    //   void m(String @A [] m) { }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   101
    //   void m(String @A ... m) { }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   102
    // no finite lookahead is enough to determine whether to read array
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   103
    // levels or an ellipsis.  Furthermore, if you call bracketsOpt, then
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   104
    // bracketsOpt first reads all the leading annotations and only then
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   105
    // discovers that it needs to fail.  bracketsOpt needs a way to push
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   106
    // back the extra annotations that it read.  (But, bracketsOpt should
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   107
    // not *always* be allowed to push back extra annotations that it finds
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   108
    // -- in most contexts, any such extra annotation is an error.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   109
    //
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   110
    // The following two variables permit type annotations that have
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   111
    // already been read to be stored for later use.  Alternate
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   112
    // implementations are possible but would cause much larger changes to
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   113
    // the parser.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   114
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   115
    /** Type annotations that have already been read but have not yet been used. **/
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   116
    private List<JCAnnotation> typeAnnotationsPushedBack = List.nil();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   117
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   118
    /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   119
     * If the parser notices extra annotations, then it either immediately
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   120
     * issues an error (if this variable is false) or places the extra
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   121
     * annotations in variable typeAnnotationsPushedBack (if this variable
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   122
     * is true).
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   123
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   124
    private boolean permitTypeAnnotationsPushBack = false;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   125
14450
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   126
    interface ErrorRecoveryAction {
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   127
        JCTree doRecover(JavacParser parser);
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   128
    }
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   129
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   130
    enum BasicErrorRecoveryAction implements ErrorRecoveryAction {
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   131
        BLOCK_STMT {public JCTree doRecover(JavacParser parser) { return parser.parseStatementAsBlock(); }},
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   132
        CATCH_CLAUSE {public JCTree doRecover(JavacParser parser) { return parser.catchClause(); }}
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   133
    }
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   134
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    /** Construct a parser from a given scanner, tree factory and log.
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   137
    protected JavacParser(ParserFactory fac,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
                     Lexer S,
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   139
                     boolean keepDocComments,
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   140
                     boolean keepLineMap,
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   141
                     boolean keepEndPositions) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        this.S = S;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   143
        nextToken(); // prime the pump
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        this.F = fac.F;
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        this.log = fac.log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        this.names = fac.names;
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        this.source = fac.source;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        this.allowGenerics = source.allowGenerics();
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        this.allowVarargs = source.allowVarargs();
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        this.allowAsserts = source.allowAsserts();
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        this.allowEnums = source.allowEnums();
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        this.allowForeach = source.allowForeach();
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        this.allowStaticImport = source.allowStaticImport();
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        this.allowAnnotations = source.allowAnnotations();
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
   155
        this.allowTWR = source.allowTryWithResources();
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   156
        this.allowDiamond = source.allowDiamond();
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   157
        this.allowMulticatch = source.allowMulticatch();
10200
56cc93e7b6ef 7068902: (javac) allow enabling or disabling of String folding
ksrini
parents: 10199
diff changeset
   158
        this.allowStringFolding = fac.options.getBoolean("allowStringFolding", true);
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   159
        this.allowLambda = source.allowLambda();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   160
        this.allowMethodReferences = source.allowMethodReferences();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents: 14541
diff changeset
   161
        this.allowDefaultMethods = source.allowDefaultMethods();
15377
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
   162
        this.allowStaticInterfaceMethods = source.allowStaticInterfaceMethods();
15375
d2529dc91d77 8006566: Remove transient lambda-related guards from JavacParser
mcimadamore
parents: 15367
diff changeset
   163
        this.allowIntersectionTypesInCast = source.allowIntersectionTypesInCast();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   164
        this.allowTypeAnnotations = source.allowTypeAnnotations();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        this.keepDocComments = keepDocComments;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   166
        docComments = newDocCommentTable(keepDocComments, fac);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   167
        this.keepLineMap = keepLineMap;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        this.errorTree = F.Erroneous();
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   169
        endPosTable = newEndPosTable(keepEndPositions);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   172
    protected AbstractEndPosTable newEndPosTable(boolean keepEndPositions) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   173
        return  keepEndPositions
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   174
                ? new SimpleEndPosTable()
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   175
                : new EmptyEndPosTable();
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   176
    }
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   177
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   178
    protected DocCommentTable newDocCommentTable(boolean keepDocComments, ParserFactory fac) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   179
        return keepDocComments ? new LazyDocCommentTable(fac) : null;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   180
    }
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   181
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    /** Switch: Should generics be recognized?
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    boolean allowGenerics;
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   186
    /** Switch: Should diamond operator be recognized?
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   187
     */
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   188
    boolean allowDiamond;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   189
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   190
    /** Switch: Should multicatch clause be accepted?
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   191
     */
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   192
    boolean allowMulticatch;
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   193
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    /** Switch: Should varargs be recognized?
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    boolean allowVarargs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    /** Switch: should we recognize assert statements, or just give a warning?
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    boolean allowAsserts;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    /** Switch: should we recognize enums, or just give a warning?
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    boolean allowEnums;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    /** Switch: should we recognize foreach?
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    boolean allowForeach;
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    /** Switch: should we recognize foreach?
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    boolean allowStaticImport;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    /** Switch: should we recognize annotations?
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    boolean allowAnnotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
8224
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
   218
    /** Switch: should we recognize try-with-resources?
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
   219
     */
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
   220
    boolean allowTWR;
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
   221
10200
56cc93e7b6ef 7068902: (javac) allow enabling or disabling of String folding
ksrini
parents: 10199
diff changeset
   222
    /** Switch: should we fold strings?
56cc93e7b6ef 7068902: (javac) allow enabling or disabling of String folding
ksrini
parents: 10199
diff changeset
   223
     */
56cc93e7b6ef 7068902: (javac) allow enabling or disabling of String folding
ksrini
parents: 10199
diff changeset
   224
    boolean allowStringFolding;
56cc93e7b6ef 7068902: (javac) allow enabling or disabling of String folding
ksrini
parents: 10199
diff changeset
   225
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   226
    /** Switch: should we recognize lambda expressions?
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   227
     */
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   228
    boolean allowLambda;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   229
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
   230
    /** Switch: should we allow method/constructor references?
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
   231
     */
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
   232
    boolean allowMethodReferences;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
   233
14267
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14263
diff changeset
   234
    /** Switch: should we allow default methods in interfaces?
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14263
diff changeset
   235
     */
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14263
diff changeset
   236
    boolean allowDefaultMethods;
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14263
diff changeset
   237
15377
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
   238
    /** Switch: should we allow static methods in interfaces?
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
   239
     */
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
   240
    boolean allowStaticInterfaceMethods;
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
   241
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   242
    /** Switch: should we allow intersection types in cast?
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   243
     */
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   244
    boolean allowIntersectionTypesInCast;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   245
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    /** Switch: should we keep docComments?
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    boolean keepDocComments;
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   250
    /** Switch: should we keep line table?
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   251
     */
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   252
    boolean keepLineMap;
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   253
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   254
    /** Switch: should we recognize type annotations?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   255
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   256
    boolean allowTypeAnnotations;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   257
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   258
    /** Switch: is "this" allowed as an identifier?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   259
     * This is needed to parse receiver types.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   260
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   261
    boolean allowThisIdent;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   262
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   263
    /** The type of the method receiver, as specified by a first "this" parameter.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   264
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   265
    JCVariableDecl receiverParam;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   266
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   267
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    /** When terms are parsed, the mode determines which is expected:
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
     *     mode = EXPR        : an expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
     *     mode = TYPE        : a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     *     mode = NOPARAMS    : no parameters allowed for type
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     *     mode = TYPEARG     : type argument
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     */
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   274
    static final int EXPR = 0x1;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   275
    static final int TYPE = 0x2;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   276
    static final int NOPARAMS = 0x4;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   277
    static final int TYPEARG = 0x8;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   278
    static final int DIAMOND = 0x10;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
    /** The current mode.
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
    private int mode = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    /** The mode of the term that was parsed last.
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    private int lastmode = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   288
    /* ---------- token management -------------- */
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   289
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   290
    protected Token token;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   291
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   292
    public Token token() {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   293
        return token;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   294
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   295
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   296
    public void nextToken() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   297
        S.nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   298
        token = S.token();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   299
    }
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   300
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   301
    protected boolean peekToken(Filter<TokenKind> tk) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   302
        return peekToken(0, tk);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   303
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   304
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   305
    protected boolean peekToken(int lookahead, Filter<TokenKind> tk) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   306
        return tk.accepts(S.token(lookahead + 1).kind);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   307
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   308
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   309
    protected boolean peekToken(Filter<TokenKind> tk1, Filter<TokenKind> tk2) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   310
        return peekToken(0, tk1, tk2);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   311
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   312
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   313
    protected boolean peekToken(int lookahead, Filter<TokenKind> tk1, Filter<TokenKind> tk2) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   314
        return tk1.accepts(S.token(lookahead + 1).kind) &&
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   315
                tk2.accepts(S.token(lookahead + 2).kind);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   316
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   317
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   318
    protected boolean peekToken(Filter<TokenKind> tk1, Filter<TokenKind> tk2, Filter<TokenKind> tk3) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   319
        return peekToken(0, tk1, tk2, tk3);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   320
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   321
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   322
    protected boolean peekToken(int lookahead, Filter<TokenKind> tk1, Filter<TokenKind> tk2, Filter<TokenKind> tk3) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   323
        return tk1.accepts(S.token(lookahead + 1).kind) &&
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   324
                tk2.accepts(S.token(lookahead + 2).kind) &&
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   325
                tk3.accepts(S.token(lookahead + 3).kind);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   326
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   327
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   328
    @SuppressWarnings("unchecked")
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   329
    protected boolean peekToken(Filter<TokenKind>... kinds) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   330
        return peekToken(0, kinds);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   331
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   332
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   333
    @SuppressWarnings("unchecked")
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   334
    protected boolean peekToken(int lookahead, Filter<TokenKind>... kinds) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   335
        for (; lookahead < kinds.length ; lookahead++) {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   336
            if (!kinds[lookahead].accepts(S.token(lookahead + 1).kind)) {
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   337
                return false;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   338
            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   339
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   340
        return true;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   341
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   342
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   343
    /* ---------- error recovery -------------- */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
    private JCErroneous errorTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
    /** Skip forward until a suitable stop token is found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
    private void skip(boolean stopAtImport, boolean stopAtMemberDecl, boolean stopAtIdentifier, boolean stopAtStatement) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
         while (true) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   351
             switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                case SEMI:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   353
                    nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                case PUBLIC:
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                case FINAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                case ABSTRACT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                case MONKEYS_AT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                case EOF:
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                case INTERFACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
                case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
                case IMPORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                    if (stopAtImport)
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                case LBRACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                case RBRACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                case PRIVATE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                case PROTECTED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                case STATIC:
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                case TRANSIENT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                case NATIVE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                case VOLATILE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                case SYNCHRONIZED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
                case STRICTFP:
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
                case LT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
                case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                case INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
                case LONG:
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
                case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
                case DOUBLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
                case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
                case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
                    if (stopAtMemberDecl)
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                    break;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   391
                case UNDERSCORE:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
                case IDENTIFIER:
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
                   if (stopAtIdentifier)
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                case CASE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                case DEFAULT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
                case IF:
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
                case FOR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
                case WHILE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
                case DO:
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
                case TRY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
                case SWITCH:
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
                case RETURN:
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
                case THROW:
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
                case BREAK:
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
                case CONTINUE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
                case ELSE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
                case FINALLY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
                case CATCH:
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
                    if (stopAtStatement)
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   415
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   419
    private JCErroneous syntaxError(int pos, String key, TokenKind... args) {
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   420
        return syntaxError(pos, List.<JCTree>nil(), key, args);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   423
    private JCErroneous syntaxError(int pos, List<JCTree> errs, String key, TokenKind... args) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        setErrorEndPos(pos);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   425
        JCErroneous err = F.at(pos).Erroneous(errs);
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   426
        reportSyntaxError(err, key, (Object[])args);
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   427
        if (errs != null) {
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   428
            JCTree last = errs.last();
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   429
            if (last != null)
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   430
                storeEnd(last, pos);
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   431
        }
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   432
        return toP(err);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
    private int errorPos = Position.NOPOS;
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   436
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    /**
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   438
     * Report a syntax using the given the position parameter and arguments,
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   439
     * unless one was already reported at the same position.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
     */
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   441
    private void reportSyntaxError(int pos, String key, Object... args) {
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   442
        JCDiagnostic.DiagnosticPosition diag = new JCDiagnostic.SimpleDiagnosticPosition(pos);
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   443
        reportSyntaxError(diag, key, args);
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   444
    }
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   445
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   446
    /**
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   447
     * Report a syntax error using the given DiagnosticPosition object and
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   448
     * arguments, unless one was already reported at the same position.
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   449
     */
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   450
    private void reportSyntaxError(JCDiagnostic.DiagnosticPosition diagPos, String key, Object... args) {
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   451
        int pos = diagPos.getPreferredPosition();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
        if (pos > S.errPos() || pos == Position.NOPOS) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   453
            if (token.kind == EOF) {
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   454
                error(diagPos, "premature.eof");
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   455
            } else {
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   456
                error(diagPos, key, args);
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   457
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   458
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
        S.errPos(pos);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   460
        if (token.pos == errorPos)
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   461
            nextToken(); // guarantee progress
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   462
        errorPos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
    /** Generate a syntax error at current position unless one was already
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
     *  reported at the same position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
    private JCErroneous syntaxError(String key) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   470
        return syntaxError(token.pos, key);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
    /** Generate a syntax error at current position unless one was
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
     *  already reported at the same position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
     */
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   476
    private JCErroneous syntaxError(String key, TokenKind arg) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   477
        return syntaxError(token.pos, key, arg);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
    /** If next input token matches given token, skip it, otherwise report
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
     *  an error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
     */
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   483
    public void accept(TokenKind tk) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   484
        if (token.kind == tk) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   485
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   487
            setErrorEndPos(token.pos);
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   488
            reportSyntaxError(S.prevToken().endPos, "expected", tk);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
    /** Report an illegal start of expression/type error at given position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
    JCExpression illegal(int pos) {
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   495
        setErrorEndPos(pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
        if ((mode & EXPR) != 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            return syntaxError(pos, "illegal.start.of.expr");
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
            return syntaxError(pos, "illegal.start.of.type");
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
    /** Report an illegal start of expression/type error at current position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
    JCExpression illegal() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   506
        return illegal(token.pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
    /** Diagnose a modifier flag from the set, if any. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
    void checkNoMods(long mods) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
        if (mods != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
            long lowestMod = mods & -mods;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   513
            error(token.pos, "mod.not.allowed.here",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
   514
                      Flags.asFlagSet(lowestMod));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
/* ---------- doc comments --------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   520
    /** A table to store all documentation comments
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
     *  indexed by the tree nodes they refer to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
     *  defined only if option flag keepDocComment is set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   524
    private final DocCommentTable docComments;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
    /** Make an entry into docComments hashtable,
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
     *  provided flag keepDocComments is set and given doc comment is non-null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
     *  @param tree   The tree to be used as index in the hashtable
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
     *  @param dc     The doc comment to associate with the tree, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   531
    void attach(JCTree tree, Comment dc) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
        if (keepDocComments && dc != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
//          System.out.println("doc comment = ");System.out.println(dc);//DEBUG
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   534
            docComments.putComment(tree, dc);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
/* -------- source positions ------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
    private void setErrorEndPos(int errPos) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   541
        endPosTable.setErrorEndPos(errPos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   544
    private void storeEnd(JCTree tree, int endpos) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   545
        endPosTable.storeEnd(tree, endpos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   548
    private <T extends JCTree> T to(T t) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   549
        return endPosTable.to(t);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   550
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   552
    private <T extends JCTree> T toP(T t) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   553
        return endPosTable.toP(t);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   554
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
    /** Get the start position for a tree node.  The start position is
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
     * defined to be the position of the first character of the first
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
     * token of the node's source text.
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
     * @param tree  The tree node
06bc494ca11e Initial load
duke
parents:
diff changeset
   560
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
    public int getStartPos(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
        return TreeInfo.getStartPos(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
     * Get the end position for a tree node.  The end position is
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
     * defined to be the position of the last character of the last
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
     * token of the node's source text.  Returns Position.NOPOS if end
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
     * positions are not generated or the position is otherwise not
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
     * found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
     * @param tree  The tree node
06bc494ca11e Initial load
duke
parents:
diff changeset
   572
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
    public int getEndPos(JCTree tree) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   574
        return endPosTable.getEndPos(tree);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
/* ---------- parsing -------------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
     * Ident = IDENTIFIER
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
    Name ident() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   585
        if (token.kind == IDENTIFIER) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   586
            Name name = token.name();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   587
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
            return name;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   589
        } else if (token.kind == ASSERT) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
            if (allowAsserts) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   591
                error(token.pos, "assert.as.identifier");
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   592
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
                return names.error;
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
            } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   595
                warning(token.pos, "assert.as.identifier");
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   596
                Name name = token.name();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   597
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
                return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   600
        } else if (token.kind == ENUM) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
            if (allowEnums) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   602
                error(token.pos, "enum.as.identifier");
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   603
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
                return names.error;
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
            } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   606
                warning(token.pos, "enum.as.identifier");
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   607
                Name name = token.name();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   608
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
                return name;
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   611
        } else if (token.kind == THIS) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   612
            if (allowThisIdent) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   613
                // Make sure we're using a supported source version.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   614
                checkTypeAnnotations();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   615
                Name name = token.name();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   616
                nextToken();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   617
                return name;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   618
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   619
                error(token.pos, "this.as.identifier");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   620
                nextToken();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   621
                return names.error;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   622
            }
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   623
        } else if (token.kind == UNDERSCORE) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   624
            warning(token.pos, "underscore.as.identifier");
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   625
            Name name = token.name();
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   626
            nextToken();
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   627
            return name;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
            accept(IDENTIFIER);
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
            return names.error;
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
        }
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   632
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
    /**
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   635
     * Qualident = Ident { DOT [Annotations] Ident }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   637
    public JCExpression qualident(boolean allowAnnos) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   638
        JCExpression t = toP(F.at(token.pos).Ident(ident()));
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   639
        while (token.kind == DOT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   640
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   641
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   642
            List<JCAnnotation> tyannos = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   643
            if (allowAnnos) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   644
                tyannos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   645
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
            t = toP(F.at(pos).Select(t, ident()));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   647
            if (tyannos != null && tyannos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   648
                t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   649
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   654
    JCExpression literal(Name prefix) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   655
        return literal(prefix, token.pos);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   656
    }
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   657
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
     * Literal =
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
     *     INTLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
     *   | LONGLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
     *   | FLOATLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
     *   | DOUBLELITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
     *   | CHARLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
     *   | STRINGLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
     *   | TRUE
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
     *   | FALSE
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
     *   | NULL
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
     */
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   670
    JCExpression literal(Name prefix, int pos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
        JCExpression t = errorTree;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   672
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
        case INTLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
                t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   676
                    TypeTag.INT,
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   677
                    Convert.string2int(strval(prefix), token.radix()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
            } catch (NumberFormatException ex) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   679
                error(token.pos, "int.number.too.large", strval(prefix));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
        case LONGLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
                t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   685
                    TypeTag.LONG,
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   686
                    new Long(Convert.string2long(strval(prefix), token.radix())));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
            } catch (NumberFormatException ex) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   688
                error(token.pos, "int.number.too.large", strval(prefix));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
        case FLOATLITERAL: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   692
            String proper = token.radix() == 16 ?
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   693
                    ("0x"+ token.stringVal()) :
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   694
                    token.stringVal();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
            Float n;
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
                n = Float.valueOf(proper);
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
            } catch (NumberFormatException ex) {
7636
030f141aa32b 6504896: TreeMaker.Literal(Object) does not support Booleans
jjg
parents: 7211
diff changeset
   699
                // error already reported in scanner
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
                n = Float.NaN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
            if (n.floatValue() == 0.0f && !isZero(proper))
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   703
                error(token.pos, "fp.number.too.small");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
            else if (n.floatValue() == Float.POSITIVE_INFINITY)
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   705
                error(token.pos, "fp.number.too.large");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
            else
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   707
                t = F.at(pos).Literal(TypeTag.FLOAT, n);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
        case DOUBLELITERAL: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   711
            String proper = token.radix() == 16 ?
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   712
                    ("0x"+ token.stringVal()) :
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   713
                    token.stringVal();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
            Double n;
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
                n = Double.valueOf(proper);
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
            } catch (NumberFormatException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
                // error already reported in scanner
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
                n = Double.NaN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
            if (n.doubleValue() == 0.0d && !isZero(proper))
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   722
                error(token.pos, "fp.number.too.small");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
            else if (n.doubleValue() == Double.POSITIVE_INFINITY)
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   724
                error(token.pos, "fp.number.too.large");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
            else
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   726
                t = F.at(pos).Literal(TypeTag.DOUBLE, n);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
        case CHARLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
            t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   731
                TypeTag.CHAR,
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   732
                token.stringVal().charAt(0) + 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
        case STRINGLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
            t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   736
                TypeTag.CLASS,
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   737
                token.stringVal());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
        case TRUE: case FALSE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
            t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   741
                TypeTag.BOOLEAN,
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   742
                (token.kind == TRUE ? 1 : 0));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
        case NULL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
            t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   746
                TypeTag.BOT,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
                null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
        default:
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7636
diff changeset
   750
            Assert.error();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
        if (t == errorTree)
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
            t = F.at(pos).Erroneous();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   754
        storeEnd(t, token.endPos);
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   755
        nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   758
    //where
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
        boolean isZero(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   760
            char[] cs = s.toCharArray();
3894
e470a29ed0a2 6882235: invalid exponent causes silent javac crash
jjg
parents: 3765
diff changeset
   761
            int base = ((cs.length > 1 && Character.toLowerCase(cs[1]) == 'x') ? 16 : 10);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   762
            int i = ((base==16) ? 2 : 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
            while (i < cs.length && (cs[i] == '0' || cs[i] == '.')) i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
            return !(i < cs.length && (Character.digit(cs[i], base) > 0));
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
        String strval(Name prefix) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   768
            String s = token.stringVal();
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1258
diff changeset
   769
            return prefix.isEmpty() ? s : prefix + s;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
    /** terms can be either expressions or types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
     */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   774
    public JCExpression parseExpression() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   775
        return term(EXPR);
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   777
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   778
    /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   779
     * parses (optional) type annotations followed by a type. If the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   780
     * annotations are present before the type and are not consumed during array
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   781
     * parsing, this method returns a {@link JCAnnotatedType} consisting of
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   782
     * these annotations and the underlying type. Otherwise, it returns the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   783
     * underlying type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   784
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   785
     * <p>
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   786
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   787
     * Note that this method sets {@code mode} to {@code TYPE} first, before
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   788
     * parsing annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   789
     */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   790
    public JCExpression parseType() {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   791
        List<JCAnnotation> annotations = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   792
        return parseType(annotations);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   793
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   794
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   795
    public JCExpression parseType(List<JCAnnotation> annotations) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   796
        JCExpression result = unannotatedType();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   797
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   798
        if (annotations.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   799
            result = insertAnnotationsToMostInner(result, annotations, false);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   800
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   801
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   802
        return result;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   803
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   804
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   805
    public JCExpression unannotatedType() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
        return term(TYPE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
    JCExpression term(int newmode) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
        int prevmode = mode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
        mode = newmode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
        JCExpression t = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
        lastmode = mode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   814
        mode = prevmode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
    /**
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
   819
     *  {@literal
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
     *  Expression = Expression1 [ExpressionRest]
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
     *  ExpressionRest = [AssignmentOperator Expression1]
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
     *  AssignmentOperator = "=" | "+=" | "-=" | "*=" | "/=" |
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
     *                       "&=" | "|=" | "^=" |
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
     *                       "%=" | "<<=" | ">>=" | ">>>="
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
     *  Type = Type1
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
     *  TypeNoParams = TypeNoParams1
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
     *  StatementExpression = Expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
     *  ConstantExpression = Expression
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
   829
     *  }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
    JCExpression term() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
        JCExpression t = term1();
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
        if ((mode & EXPR) != 0 &&
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   834
            token.kind == EQ || PLUSEQ.compareTo(token.kind) <= 0 && token.kind.compareTo(GTGTGTEQ) <= 0)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
            return termRest(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
    JCExpression termRest(JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   841
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
        case EQ: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   843
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   844
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
            JCExpression t1 = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
            return toP(F.at(pos).Assign(t, t1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
        case PLUSEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
        case SUBEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
        case STAREQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
        case SLASHEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
        case PERCENTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
        case AMPEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
        case BAREQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
        case CARETEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
        case LTLTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
        case GTGTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
        case GTGTGTEQ:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   860
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   861
            TokenKind tk = token.kind;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   862
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
            JCExpression t1 = term();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   865
            return F.at(pos).Assignop(optag(tk), t, t1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
    /** Expression1   = Expression2 [Expression1Rest]
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
     *  Type1         = Type2
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
     *  TypeNoParams1 = TypeNoParams2
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
    JCExpression term1() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
        JCExpression t = term2();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   877
        if ((mode & EXPR) != 0 && token.kind == QUES) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
            return term1Rest(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
    /** Expression1Rest = ["?" Expression ":" Expression1]
06bc494ca11e Initial load
duke
parents:
diff changeset
   886
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
    JCExpression term1Rest(JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   888
        if (token.kind == QUES) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   889
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   890
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
            JCExpression t1 = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
            accept(COLON);
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
            JCExpression t2 = term1();
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
            return F.at(pos).Conditional(t, t1, t2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
    /** Expression2   = Expression3 [Expression2Rest]
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
     *  Type2         = Type3
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
     *  TypeNoParams2 = TypeNoParams3
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
    JCExpression term2() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
        JCExpression t = term3();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   906
        if ((mode & EXPR) != 0 && prec(token.kind) >= TreeInfo.orPrec) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
            return term2Rest(t, TreeInfo.orPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
    /*  Expression2Rest = {infixop Expression3}
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
     *                  | Expression3 instanceof Type
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
     *  infixop         = "||"
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
     *                  | "&&"
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
     *                  | "|"
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
     *                  | "^"
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
     *                  | "&"
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
     *                  | "==" | "!="
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
     *                  | "<" | ">" | "<=" | ">="
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
     *                  | "<<" | ">>" | ">>>"
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
     *                  | "+" | "-"
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
     *                  | "*" | "/" | "%"
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
    JCExpression term2Rest(JCExpression t, int minprec) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
        JCExpression[] odStack = newOdStack();
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
        Token[] opStack = newOpStack();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   930
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
        // optimization, was odStack = new Tree[...]; opStack = new Tree[...];
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
        int top = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
        odStack[0] = t;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   934
        int startPos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   935
        Token topOp = Tokens.DUMMY;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   936
        while (prec(token.kind) >= minprec) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
            opStack[top] = topOp;
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
            top++;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   939
            topOp = token;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   940
            nextToken();
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
   941
            odStack[top] = (topOp.kind == INSTANCEOF) ? parseType() : term3();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   942
            while (top > 0 && prec(topOp.kind) >= prec(token.kind)) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   943
                odStack[top-1] = makeOp(topOp.pos, topOp.kind, odStack[top-1],
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   944
                                        odStack[top]);
06bc494ca11e Initial load
duke
parents:
diff changeset
   945
                top--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
                topOp = opStack[top];
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   948
        }
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7636
diff changeset
   949
        Assert.check(top == 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
        t = odStack[0];
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
   952
        if (t.hasTag(JCTree.Tag.PLUS)) {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14062
diff changeset
   953
            StringBuilder buf = foldStrings(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
            if (buf != null) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   955
                t = toP(F.at(startPos).Literal(TypeTag.CLASS, buf.toString()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   956
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   957
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   958
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
   959
        odStackSupply.add(odStack);
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
   960
        opStackSupply.add(opStack);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   961
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   962
    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   963
    //where
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   964
        /** Construct a binary or type test node.
06bc494ca11e Initial load
duke
parents:
diff changeset
   965
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   966
        private JCExpression makeOp(int pos,
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   967
                                    TokenKind topOp,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   968
                                    JCExpression od1,
06bc494ca11e Initial load
duke
parents:
diff changeset
   969
                                    JCExpression od2)
06bc494ca11e Initial load
duke
parents:
diff changeset
   970
        {
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
            if (topOp == INSTANCEOF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   972
                return F.at(pos).TypeTest(od1, od2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   973
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   974
                return F.at(pos).Binary(optag(topOp), od1, od2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   975
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   976
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   977
        /** If tree is a concatenation of string literals, replace it
06bc494ca11e Initial load
duke
parents:
diff changeset
   978
         *  by a single literal representing the concatenated string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   979
         */
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14062
diff changeset
   980
        protected StringBuilder foldStrings(JCTree tree) {
10200
56cc93e7b6ef 7068902: (javac) allow enabling or disabling of String folding
ksrini
parents: 10199
diff changeset
   981
            if (!allowStringFolding)
56cc93e7b6ef 7068902: (javac) allow enabling or disabling of String folding
ksrini
parents: 10199
diff changeset
   982
                return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
            List<String> buf = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
            while (true) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
   985
                if (tree.hasTag(LITERAL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
                    JCLiteral lit = (JCLiteral) tree;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   987
                    if (lit.typetag == TypeTag.CLASS) {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14062
diff changeset
   988
                        StringBuilder sbuf =
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14062
diff changeset
   989
                            new StringBuilder((String)lit.value);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   990
                        while (buf.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
                            sbuf.append(buf.head);
06bc494ca11e Initial load
duke
parents:
diff changeset
   992
                            buf = buf.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
                        return sbuf;
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
                    }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
   996
                } else if (tree.hasTag(JCTree.Tag.PLUS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
                    JCBinary op = (JCBinary)tree;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
   998
                    if (op.rhs.hasTag(LITERAL)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
                        JCLiteral lit = (JCLiteral) op.rhs;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1000
                        if (lit.typetag == TypeTag.CLASS) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
                            buf = buf.prepend((String) lit.value);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1002
                            tree = op.lhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
                            continue;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1004
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1006
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
                return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
        /** optimization: To save allocating a new operand/operator stack
06bc494ca11e Initial load
duke
parents:
diff changeset
  1012
         *  for every binary operation, we use supplys.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
         */
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1014
        ArrayList<JCExpression[]> odStackSupply = new ArrayList<JCExpression[]>();
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1015
        ArrayList<Token[]> opStackSupply = new ArrayList<Token[]>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1016
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
        private JCExpression[] newOdStack() {
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1018
            if (odStackSupply.isEmpty())
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1019
                return new JCExpression[infixPrecedenceLevels + 1];
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1020
            return odStackSupply.remove(odStackSupply.size() - 1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1022
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
        private Token[] newOpStack() {
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1024
            if (opStackSupply.isEmpty())
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1025
                return new Token[infixPrecedenceLevels + 1];
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1026
            return opStackSupply.remove(opStackSupply.size() - 1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1029
    /**
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1030
     *  Expression3    = PrefixOp Expression3
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
     *                 | "(" Expr | TypeNoParams ")" Expression3
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
     *                 | Primary {Selector} {PostfixOp}
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1033
     *
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1034
     *  {@literal
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
     *  Primary        = "(" Expression ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
     *                 | Literal
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
     *                 | [TypeArguments] THIS [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
     *                 | [TypeArguments] SUPER SuperSuffix
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
     *                 | NEW [TypeArguments] Creator
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1040
     *                 | "(" Arguments ")" "->" ( Expression | Block )
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1041
     *                 | Ident "->" ( Expression | Block )
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1042
     *                 | [Annotations] Ident { "." [Annotations] Ident }
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1043
     *                 | Expression3 MemberReferenceSuffix
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1044
     *                   [ [Annotations] "[" ( "]" BracketsOpt "." CLASS | Expression "]" )
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
     *                   | Arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
     *                   | "." ( CLASS | THIS | [TypeArguments] SUPER Arguments | NEW [TypeArguments] InnerCreator )
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
     *                   ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
     *                 | BasicType BracketsOpt "." CLASS
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1049
     *  }
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1050
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
     *  PrefixOp       = "++" | "--" | "!" | "~" | "+" | "-"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
     *  PostfixOp      = "++" | "--"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
     *  Type3          = Ident { "." Ident } [TypeArguments] {TypeSelector} BracketsOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
     *                 | BasicType
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
     *  TypeNoParams3  = Ident { "." Ident } BracketsOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
     *  Selector       = "." [TypeArguments] Ident [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
     *                 | "." THIS
06bc494ca11e Initial load
duke
parents:
diff changeset
  1058
     *                 | "." [TypeArguments] SUPER SuperSuffix
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
     *                 | "." NEW [TypeArguments] InnerCreator
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
     *                 | "[" Expression "]"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
     *  TypeSelector   = "." Ident [TypeArguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
     *  SuperSuffix    = Arguments | "." Ident [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1064
    protected JCExpression term3() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1065
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
        JCExpression t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
        List<JCExpression> typeArgs = typeArgumentsOpt(EXPR);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1068
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
        case QUES:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
            if ((mode & TYPE) != 0 && (mode & (TYPEARG|NOPARAMS)) == TYPEARG) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
                mode = TYPE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
                return typeArgument();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
            } else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
                return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
        case PLUSPLUS: case SUBSUB: case BANG: case TILDE: case PLUS: case SUB:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
            if (typeArgs == null && (mode & EXPR) != 0) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1077
                TokenKind tk = token.kind;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1078
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
                mode = EXPR;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1080
                if (tk == SUB &&
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1081
                    (token.kind == INTLITERAL || token.kind == LONGLITERAL) &&
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1082
                    token.radix() == 10) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
                    mode = EXPR;
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  1084
                    t = literal(names.hyphen, pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
                } else {
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1086
                    t = term3();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1087
                    return F.at(pos).Unary(unoptag(tk), t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
        case LPAREN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1092
            if (typeArgs == null && (mode & EXPR) != 0) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1093
                ParensResult pres = analyzeParens();
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1094
                switch (pres) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1095
                    case CAST:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1096
                       accept(LPAREN);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1097
                       mode = TYPE;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1098
                       int pos1 = pos;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1099
                       List<JCExpression> targets = List.of(t = term3());
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1100
                       while (token.kind == AMP) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1101
                           checkIntersectionTypesInCast();
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1102
                           accept(AMP);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1103
                           targets = targets.prepend(term3());
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1104
                       }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1105
                       if (targets.length() > 1) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1106
                           t = toP(F.at(pos1).TypeIntersection(targets.reverse()));
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1107
                       }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1108
                       accept(RPAREN);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1109
                       mode = EXPR;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1110
                       JCExpression t1 = term3();
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1111
                       return F.at(pos).TypeCast(t, t1);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1112
                    case IMPLICIT_LAMBDA:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1113
                    case EXPLICIT_LAMBDA:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1114
                        t = lambdaExpressionOrStatement(true, pres == ParensResult.EXPLICIT_LAMBDA, pos);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1115
                        break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1116
                    default: //PARENS
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1117
                        accept(LPAREN);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1118
                        mode = EXPR;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1119
                        t = termRest(term1Rest(term2Rest(term3(), TreeInfo.orPrec)));
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1120
                        accept(RPAREN);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1121
                        t = toP(F.at(pos).Parens(t));
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1122
                        break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1123
                }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1124
            } else {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1125
                return illegal();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1126
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1127
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1128
        case THIS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1129
            if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1130
                mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1131
                t = to(F.at(pos).Ident(names._this));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1132
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1133
                if (typeArgs == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1134
                    t = argumentsOpt(null, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1135
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1136
                    t = arguments(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1137
                typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1139
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1140
        case SUPER:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1141
            if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
                mode = EXPR;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1143
                t = to(F.at(pos).Ident(names._super));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1144
                t = superSuffix(typeArgs, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1145
                typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1147
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
        case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: case DOUBLELITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
        case CHARLITERAL: case STRINGLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
        case TRUE: case FALSE: case NULL:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
            if (typeArgs == null && (mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
                mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
                t = literal(names.empty);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
        case NEW:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1157
            if (typeArgs != null) return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1158
            if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1159
                mode = EXPR;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1160
                nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1161
                if (token.kind == LT) typeArgs = typeArguments(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
                t = creator(pos, typeArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
                typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
            break;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1166
        case MONKEYS_AT:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1167
            // Only annotated cast types are valid
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1168
            List<JCAnnotation> typeAnnos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1169
            if (typeAnnos.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1170
                // else there would be no '@'
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1171
                throw new AssertionError("Expected type annotations, but found none!");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1172
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1173
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1174
            JCExpression expr = term3();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1175
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1176
            if ((mode & TYPE) == 0) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1177
                // Type annotations on class literals no longer legal
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1178
                if (!expr.hasTag(Tag.SELECT)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1179
                    return illegal(typeAnnos.head.pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1180
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1181
                JCFieldAccess sel = (JCFieldAccess)expr;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1182
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1183
                if (sel.name != names._class) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1184
                    return illegal();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1185
                } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1186
                    log.error(token.pos, "no.annotations.on.dot.class");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1187
                    return expr;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1188
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1189
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1190
                // Type annotations targeting a cast
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1191
                t = insertAnnotationsToMostInner(expr, typeAnnos, false);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1192
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1193
            break;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1194
        case UNDERSCORE: case IDENTIFIER: case ASSERT: case ENUM:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1195
            if (typeArgs != null) return illegal();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1196
            if ((mode & EXPR) != 0 && peekToken(ARROW)) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1197
                t = lambdaExpressionOrStatement(false, false, pos);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1198
            } else {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1199
                t = toP(F.at(token.pos).Ident(ident()));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1200
                loop: while (true) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1201
                    pos = token.pos;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1202
                    final List<JCAnnotation> annos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1203
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1204
                    // need to report an error later if LBRACKET is for array
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1205
                    // index access rather than array creation level
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1206
                    if (!annos.isEmpty() && token.kind != LBRACKET && token.kind != ELLIPSIS)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1207
                        return illegal(annos.head.pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1208
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1209
                    switch (token.kind) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1210
                    case LBRACKET:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1211
                        nextToken();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1212
                        if (token.kind == RBRACKET) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1213
                            nextToken();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1214
                            t = bracketsOpt(t);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1215
                            t = toP(F.at(pos).TypeArray(t));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1216
                            if (annos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1217
                                t = toP(F.at(pos).AnnotatedType(annos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1218
                            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1219
                            // .class is only allowed if there were no annotations
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1220
                            JCExpression nt = bracketsSuffix(t);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1221
                            if (nt != t && (annos.nonEmpty() || TreeInfo.containsTypeAnnotation(t))) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1222
                                // t and nt are different if bracketsSuffix parsed a .class.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1223
                                // The check for nonEmpty covers the case when the whole array is annotated.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1224
                                // Helper method isAnnotated looks for annos deeply within t.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1225
                                syntaxError("no.annotations.on.dot.class");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1226
                            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1227
                            t = nt;
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1228
                        } else {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1229
                            if ((mode & EXPR) != 0) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1230
                                mode = EXPR;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1231
                                JCExpression t1 = term();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1232
                                if (!annos.isEmpty()) t = illegal(annos.head.pos);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1233
                                t = to(F.at(pos).Indexed(t, t1));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1234
                            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1235
                            accept(RBRACKET);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1236
                        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1237
                        break loop;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1238
                    case LPAREN:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1239
                        if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1240
                            mode = EXPR;
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1241
                            t = arguments(typeArgs, t);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1242
                            if (!annos.isEmpty()) t = illegal(annos.head.pos);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1243
                            typeArgs = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1244
                        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1245
                        break loop;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1246
                    case DOT:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1247
                        nextToken();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1248
                        int oldmode = mode;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1249
                        mode &= ~NOPARAMS;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1250
                        typeArgs = typeArgumentsOpt(EXPR);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1251
                        mode = oldmode;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1252
                        if ((mode & EXPR) != 0) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1253
                            switch (token.kind) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1254
                            case CLASS:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1255
                                if (typeArgs != null) return illegal();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1256
                                mode = EXPR;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1257
                                t = to(F.at(pos).Select(t, names._class));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1258
                                nextToken();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1259
                                break loop;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1260
                            case THIS:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1261
                                if (typeArgs != null) return illegal();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1262
                                mode = EXPR;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1263
                                t = to(F.at(pos).Select(t, names._this));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1264
                                nextToken();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1265
                                break loop;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1266
                            case SUPER:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1267
                                mode = EXPR;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1268
                                t = to(F.at(pos).Select(t, names._super));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1269
                                t = superSuffix(typeArgs, t);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1270
                                typeArgs = null;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1271
                                break loop;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1272
                            case NEW:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1273
                                if (typeArgs != null) return illegal();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1274
                                mode = EXPR;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1275
                                int pos1 = token.pos;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1276
                                nextToken();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1277
                                if (token.kind == LT) typeArgs = typeArguments(false);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1278
                                t = innerCreator(pos1, typeArgs, t);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1279
                                typeArgs = null;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1280
                                break loop;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1281
                            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1282
                        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1283
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1284
                        List<JCAnnotation> tyannos = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1285
                        if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1286
                            tyannos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1287
                        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1288
                        // typeArgs saved for next loop iteration.
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1289
                        t = toP(F.at(pos).Select(t, ident()));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1290
                        if (tyannos != null && tyannos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1291
                            t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1292
                        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1293
                        break;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1294
                    case ELLIPSIS:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1295
                        if (this.permitTypeAnnotationsPushBack) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1296
                            this.typeAnnotationsPushedBack = annos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1297
                        } else if (annos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1298
                            // Don't return here -- error recovery attempt
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1299
                            illegal(annos.head.pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1300
                        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1301
                        break loop;
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1302
                    case LT:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1303
                        if ((mode & TYPE) == 0 && isUnboundMemberRef()) {
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1304
                            //this is an unbound method reference whose qualifier
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1305
                            //is a generic type i.e. A<S>::m
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1306
                            int pos1 = token.pos;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1307
                            accept(LT);
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1308
                            ListBuffer<JCExpression> args = new ListBuffer<JCExpression>();
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1309
                            args.append(typeArgument());
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1310
                            while (token.kind == COMMA) {
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1311
                                nextToken();
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1312
                                args.append(typeArgument());
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1313
                            }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1314
                            accept(GT);
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1315
                            t = toP(F.at(pos1).TypeApply(t, args.toList()));
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1316
                            checkGenerics();
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1317
                            while (token.kind == DOT) {
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1318
                                nextToken();
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1319
                                mode = TYPE;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1320
                                t = toP(F.at(token.pos).Select(t, ident()));
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1321
                                t = typeArgumentsOpt(t);
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1322
                            }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1323
                            t = bracketsOpt(t);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1324
                            if (token.kind != COLCOL) {
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1325
                                //method reference expected here
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1326
                                t = illegal();
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1327
                            }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1328
                            mode = EXPR;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1329
                            return term3Rest(t, typeArgs);
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1330
                        }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1331
                        break loop;
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1332
                    default:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1333
                        break loop;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1334
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1335
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1336
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1337
            if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1338
            t = typeArgumentsOpt(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1339
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1340
        case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1341
        case DOUBLE: case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1342
            if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1343
            t = bracketsSuffix(bracketsOpt(basicType()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1344
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1345
        case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1346
            if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1347
            if ((mode & EXPR) != 0) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1348
                nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1349
                if (token.kind == DOT) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1350
                    JCPrimitiveTypeTree ti = toP(F.at(pos).TypeIdent(TypeTag.VOID));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1351
                    t = bracketsSuffix(ti);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1352
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
                    return illegal(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
            } else {
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1356
                // 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
  1357
                // 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
  1358
                // The error will be reported in Attr.attribTypes or Attr.visitApply.
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1359
                JCPrimitiveTypeTree ti = to(F.at(pos).TypeIdent(TypeTag.VOID));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1360
                nextToken();
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1361
                return ti;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1362
                //return illegal();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
            return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1367
        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1368
        return term3Rest(t, typeArgs);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1369
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1370
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1371
    JCExpression term3Rest(JCExpression t, List<JCExpression> typeArgs) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
        if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
        while (true) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1374
            int pos1 = token.pos;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1375
            final List<JCAnnotation> annos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1376
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1377
            if (token.kind == LBRACKET) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1378
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1379
                if ((mode & TYPE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1380
                    int oldmode = mode;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1381
                    mode = TYPE;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1382
                    if (token.kind == RBRACKET) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1383
                        nextToken();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1384
                        t = bracketsOpt(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1385
                        t = toP(F.at(pos1).TypeArray(t));
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1386
                        if (token.kind == COLCOL) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1387
                            mode = EXPR;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1388
                            continue;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1389
                        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1390
                        if (annos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1391
                            t = toP(F.at(pos1).AnnotatedType(annos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1392
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1393
                        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1394
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1395
                    mode = oldmode;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1396
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1397
                if ((mode & EXPR) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1398
                    mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1399
                    JCExpression t1 = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1400
                    t = to(F.at(pos1).Indexed(t, t1));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1401
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1402
                accept(RBRACKET);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1403
            } else if (token.kind == DOT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1404
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1405
                typeArgs = typeArgumentsOpt(EXPR);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1406
                if (token.kind == SUPER && (mode & EXPR) != 0) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1407
                    mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1408
                    t = to(F.at(pos1).Select(t, names._super));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1409
                    nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
                    t = arguments(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
                    typeArgs = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1412
                } else if (token.kind == NEW && (mode & EXPR) != 0) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1413
                    if (typeArgs != null) return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1414
                    mode = EXPR;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1415
                    int pos2 = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1416
                    nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1417
                    if (token.kind == LT) typeArgs = typeArguments(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1418
                    t = innerCreator(pos2, typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1419
                    typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1420
                } else {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1421
                    List<JCAnnotation> tyannos = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1422
                    if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1423
                        // is the mode check needed?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1424
                        tyannos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1425
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1426
                    t = toP(F.at(pos1).Select(t, ident()));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1427
                    if (tyannos != null && tyannos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1428
                        t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1429
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1430
                    t = argumentsOpt(typeArgs, typeArgumentsOpt(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1431
                    typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1432
                }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1433
            } else if ((mode & EXPR) != 0 && token.kind == COLCOL) {
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1434
                mode = EXPR;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1435
                if (typeArgs != null) return illegal();
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1436
                accept(COLCOL);
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1437
                t = memberReferenceSuffix(pos1, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1438
            } else {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1439
                if (!annos.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1440
                    if (permitTypeAnnotationsPushBack)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1441
                        typeAnnotationsPushedBack = annos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1442
                    else
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1443
                        return illegal(annos.head.pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1444
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1445
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1446
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1447
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1448
        while ((token.kind == PLUSPLUS || token.kind == SUBSUB) && (mode & EXPR) != 0) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1449
            mode = EXPR;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1450
            t = to(F.at(token.pos).Unary(
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  1451
                  token.kind == PLUSPLUS ? POSTINC : POSTDEC, t));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1452
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1453
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1454
        return toP(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1455
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1456
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1457
    /**
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1458
     * If we see an identifier followed by a '&lt;' it could be an unbound
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1459
     * method reference or a binary expression. To disambiguate, look for a
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1460
     * matching '&gt;' and see if the subsequent terminal is either '.' or '#'.
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1461
     */
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1462
    @SuppressWarnings("fallthrough")
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1463
    boolean isUnboundMemberRef() {
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1464
        int pos = 0, depth = 0;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1465
        for (Token t = S.token(pos) ; ; t = S.token(++pos)) {
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1466
            switch (t.kind) {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1467
                case IDENTIFIER: case UNDERSCORE: case QUES: case EXTENDS: case SUPER:
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1468
                case DOT: case RBRACKET: case LBRACKET: case COMMA:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1469
                case BYTE: case SHORT: case INT: case LONG: case FLOAT:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1470
                case DOUBLE: case BOOLEAN: case CHAR:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1471
                    break;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1472
                case LT:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1473
                    depth++; break;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1474
                case GTGTGT:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1475
                    depth--;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1476
                case GTGT:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1477
                    depth--;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1478
                case GT:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1479
                    depth--;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1480
                    if (depth == 0) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1481
                        TokenKind nextKind = S.token(pos + 1).kind;
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1482
                        return
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1483
                            nextKind == TokenKind.DOT ||
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1484
                            nextKind == TokenKind.LBRACKET ||
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1485
                            nextKind == TokenKind.COLCOL;
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1486
                    }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1487
                    break;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1488
                default:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1489
                    return false;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1490
            }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1491
        }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1492
    }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1493
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1494
    /**
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1495
     * If we see an identifier followed by a '&lt;' it could be an unbound
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1496
     * method reference or a binary expression. To disambiguate, look for a
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1497
     * matching '&gt;' and see if the subsequent terminal is either '.' or '#'.
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1498
     */
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1499
    @SuppressWarnings("fallthrough")
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1500
    ParensResult analyzeParens() {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1501
        int depth = 0;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1502
        boolean type = false;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1503
        outer: for (int lookahead = 0 ; ; lookahead++) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1504
            TokenKind tk = S.token(lookahead).kind;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1505
            switch (tk) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1506
                case EXTENDS: case SUPER: case COMMA:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1507
                    type = true;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1508
                case QUES: case DOT: case AMP:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1509
                    //skip
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1510
                    break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1511
                case BYTE: case SHORT: case INT: case LONG: case FLOAT:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1512
                case DOUBLE: case BOOLEAN: case CHAR:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1513
                    if (peekToken(lookahead, RPAREN)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1514
                        //Type, ')' -> cast
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1515
                        return ParensResult.CAST;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1516
                    } else if (peekToken(lookahead, LAX_IDENTIFIER)) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1517
                        //Type, Identifier/'_'/'assert'/'enum' -> explicit lambda
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1518
                        return ParensResult.EXPLICIT_LAMBDA;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1519
                    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1520
                    break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1521
                case LPAREN:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1522
                    if (lookahead != 0) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1523
                        // '(' in a non-starting position -> parens
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1524
                        return ParensResult.PARENS;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1525
                    } else if (peekToken(lookahead, RPAREN)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1526
                        // '(', ')' -> explicit lambda
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1527
                        return ParensResult.EXPLICIT_LAMBDA;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1528
                    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1529
                    break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1530
                case RPAREN:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1531
                    // if we have seen something that looks like a type,
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1532
                    // then it's a cast expression
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1533
                    if (type) return ParensResult.CAST;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1534
                    // otherwise, disambiguate cast vs. parenthesized expression
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1535
                    // based on subsequent token.
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1536
                    switch (S.token(lookahead + 1).kind) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1537
                        /*case PLUSPLUS: case SUBSUB: */
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1538
                        case BANG: case TILDE:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1539
                        case LPAREN: case THIS: case SUPER:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1540
                        case INTLITERAL: case LONGLITERAL: case FLOATLITERAL:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1541
                        case DOUBLELITERAL: case CHARLITERAL: case STRINGLITERAL:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1542
                        case TRUE: case FALSE: case NULL:
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1543
                        case NEW: case IDENTIFIER: case ASSERT: case ENUM: case UNDERSCORE:
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1544
                        case BYTE: case SHORT: case CHAR: case INT:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1545
                        case LONG: case FLOAT: case DOUBLE: case BOOLEAN: case VOID:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1546
                            return ParensResult.CAST;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1547
                        default:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1548
                            return ParensResult.PARENS;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1549
                    }
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1550
                case UNDERSCORE:
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1551
                case ASSERT:
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1552
                case ENUM:
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1553
                case IDENTIFIER:
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1554
                    if (peekToken(lookahead, LAX_IDENTIFIER)) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1555
                        // Identifier, Identifier/'_'/'assert'/'enum' -> explicit lambda
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1556
                        return ParensResult.EXPLICIT_LAMBDA;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1557
                    } else if (peekToken(lookahead, RPAREN, ARROW)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1558
                        // Identifier, ')' '->' -> implicit lambda
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1559
                        return ParensResult.IMPLICIT_LAMBDA;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1560
                    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1561
                    break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1562
                case FINAL:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1563
                case ELLIPSIS:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1564
                    //those can only appear in explicit lambdas
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1565
                    return ParensResult.EXPLICIT_LAMBDA;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1566
                case MONKEYS_AT:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1567
                    type = true;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1568
                    lookahead += 1; //skip '@'
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1569
                    while (peekToken(lookahead, DOT)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1570
                        lookahead += 2;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1571
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1572
                    if (peekToken(lookahead, LPAREN)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1573
                        lookahead++;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1574
                        //skip annotation values
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1575
                        int nesting = 0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1576
                        for (; ; lookahead++) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1577
                            TokenKind tk2 = S.token(lookahead).kind;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1578
                            switch (tk2) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1579
                                case EOF:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1580
                                    return ParensResult.PARENS;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1581
                                case LPAREN:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1582
                                    nesting++;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1583
                                    break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1584
                                case RPAREN:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1585
                                    nesting--;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1586
                                    if (nesting == 0) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1587
                                        continue outer;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1588
                                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1589
                                break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1590
                            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1591
                        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1592
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1593
                    break;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1594
                case LBRACKET:
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1595
                    if (peekToken(lookahead, RBRACKET, LAX_IDENTIFIER)) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1596
                        // '[', ']', Identifier/'_'/'assert'/'enum' -> explicit lambda
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1597
                        return ParensResult.EXPLICIT_LAMBDA;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1598
                    } else if (peekToken(lookahead, RBRACKET, RPAREN) ||
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1599
                            peekToken(lookahead, RBRACKET, AMP)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1600
                        // '[', ']', ')' -> cast
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1601
                        // '[', ']', '&' -> cast (intersection type)
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1602
                        return ParensResult.CAST;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1603
                    } else if (peekToken(lookahead, RBRACKET)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1604
                        //consume the ']' and skip
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1605
                        type = true;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1606
                        lookahead++;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1607
                        break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1608
                    } else {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1609
                        return ParensResult.PARENS;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1610
                    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1611
                case LT:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1612
                    depth++; break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1613
                case GTGTGT:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1614
                    depth--;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1615
                case GTGT:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1616
                    depth--;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1617
                case GT:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1618
                    depth--;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1619
                    if (depth == 0) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1620
                        if (peekToken(lookahead, RPAREN) ||
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1621
                                peekToken(lookahead, AMP)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1622
                            // '>', ')' -> cast
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1623
                            // '>', '&' -> cast
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1624
                            return ParensResult.CAST;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1625
                        } else if (peekToken(lookahead, LAX_IDENTIFIER, COMMA) ||
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1626
                                peekToken(lookahead, LAX_IDENTIFIER, RPAREN, ARROW) ||
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1627
                                peekToken(lookahead, ELLIPSIS)) {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1628
                            // '>', Identifier/'_'/'assert'/'enum', ',' -> explicit lambda
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1629
                            // '>', Identifier/'_'/'assert'/'enum', ')', '->' -> explicit lambda
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1630
                            // '>', '...' -> explicit lambda
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1631
                            return ParensResult.EXPLICIT_LAMBDA;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1632
                        }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1633
                        //it looks a type, but could still be (i) a cast to generic type,
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1634
                        //(ii) an unbound method reference or (iii) an explicit lambda
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1635
                        type = true;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1636
                        break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1637
                    } else if (depth < 0) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1638
                        //unbalanced '<', '>' - not a generic type
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1639
                        return ParensResult.PARENS;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1640
                    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1641
                    break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1642
                default:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1643
                    //this includes EOF
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1644
                    return ParensResult.PARENS;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1645
            }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1646
        }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1647
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1648
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1649
    /** Accepts all identifier-like tokens */
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1650
    Filter<TokenKind> LAX_IDENTIFIER = new Filter<TokenKind>() {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1651
        public boolean accepts(TokenKind t) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1652
            return t == IDENTIFIER || t == UNDERSCORE || t == ASSERT || t == ENUM;
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1653
        }
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1654
    };
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1655
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1656
    enum ParensResult {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1657
        CAST,
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1658
        EXPLICIT_LAMBDA,
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1659
        IMPLICIT_LAMBDA,
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1660
        PARENS;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1661
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1662
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1663
    JCExpression lambdaExpressionOrStatement(boolean hasParens, boolean explicitParams, int pos) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1664
        List<JCVariableDecl> params = explicitParams ?
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1665
                formalParameters(true) :
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1666
                implicitParameters(hasParens);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1667
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1668
        return lambdaExpressionOrStatementRest(params, pos);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1669
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1670
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1671
    JCExpression lambdaExpressionOrStatementRest(List<JCVariableDecl> args, int pos) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1672
        checkLambda();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1673
        accept(ARROW);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1674
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1675
        return token.kind == LBRACE ?
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1676
            lambdaStatement(args, pos, pos) :
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1677
            lambdaExpression(args, pos);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1678
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1679
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1680
    JCExpression lambdaStatement(List<JCVariableDecl> args, int pos, int pos2) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1681
        JCBlock block = block(pos2, 0);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1682
        return toP(F.at(pos).Lambda(args, block));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1683
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1684
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1685
    JCExpression lambdaExpression(List<JCVariableDecl> args, int pos) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1686
        JCTree expr = parseExpression();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1687
        return toP(F.at(pos).Lambda(args, expr));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1688
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1689
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1690
    /** SuperSuffix = Arguments | "." [TypeArguments] Ident [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1691
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1692
    JCExpression superSuffix(List<JCExpression> typeArgs, JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1693
        nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1694
        if (token.kind == LPAREN || typeArgs != null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1695
            t = arguments(typeArgs, t);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1696
        } else if (token.kind == COLCOL) {
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1697
            if (typeArgs != null) return illegal();
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1698
            t = memberReferenceSuffix(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1699
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1700
            int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1701
            accept(DOT);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1702
            typeArgs = (token.kind == LT) ? typeArguments(false) : null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1703
            t = toP(F.at(pos).Select(t, ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1704
            t = argumentsOpt(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1705
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1706
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1707
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1708
06bc494ca11e Initial load
duke
parents:
diff changeset
  1709
    /** BasicType = BYTE | SHORT | CHAR | INT | LONG | FLOAT | DOUBLE | BOOLEAN
06bc494ca11e Initial load
duke
parents:
diff changeset
  1710
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1711
    JCPrimitiveTypeTree basicType() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1712
        JCPrimitiveTypeTree t = to(F.at(token.pos).TypeIdent(typetag(token.kind)));
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1713
        nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1714
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1715
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1716
06bc494ca11e Initial load
duke
parents:
diff changeset
  1717
    /** ArgumentsOpt = [ Arguments ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1718
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1719
    JCExpression argumentsOpt(List<JCExpression> typeArgs, JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1720
        if ((mode & EXPR) != 0 && token.kind == LPAREN || typeArgs != null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1721
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1722
            return arguments(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1723
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1724
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1725
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1726
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1727
06bc494ca11e Initial load
duke
parents:
diff changeset
  1728
    /** Arguments = "(" [Expression { COMMA Expression }] ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1729
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1730
    List<JCExpression> arguments() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1731
        ListBuffer<JCExpression> args = lb();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1732
        if (token.kind == LPAREN) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1733
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1734
            if (token.kind != RPAREN) {
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1735
                args.append(parseExpression());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1736
                while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1737
                    nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1738
                    args.append(parseExpression());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1739
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1740
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1741
            accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1742
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1743
            syntaxError(token.pos, "expected", LPAREN);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1744
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1745
        return args.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1746
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1747
06bc494ca11e Initial load
duke
parents:
diff changeset
  1748
    JCMethodInvocation arguments(List<JCExpression> typeArgs, JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1749
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1750
        List<JCExpression> args = arguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1751
        return toP(F.at(pos).Apply(typeArgs, t, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1752
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1753
06bc494ca11e Initial load
duke
parents:
diff changeset
  1754
    /**  TypeArgumentsOpt = [ TypeArguments ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1755
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1756
    JCExpression typeArgumentsOpt(JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1757
        if (token.kind == LT &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1758
            (mode & TYPE) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1759
            (mode & NOPARAMS) == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1760
            mode = TYPE;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1761
            checkGenerics();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1762
            return typeArguments(t, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1763
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1764
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1765
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1766
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1767
    List<JCExpression> typeArgumentsOpt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1768
        return typeArgumentsOpt(TYPE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1769
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1770
06bc494ca11e Initial load
duke
parents:
diff changeset
  1771
    List<JCExpression> typeArgumentsOpt(int useMode) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1772
        if (token.kind == LT) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1773
            checkGenerics();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1774
            if ((mode & useMode) == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1775
                (mode & NOPARAMS) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1776
                illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1777
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1778
            mode = useMode;
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1779
            return typeArguments(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1780
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1781
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1782
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1783
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1784
    /**
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1785
     *  {@literal
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1786
     *  TypeArguments  = "<" TypeArgument {"," TypeArgument} ">"
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1787
     *  }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1788
     */
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1789
    List<JCExpression> typeArguments(boolean diamondAllowed) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1790
        if (token.kind == LT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1791
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1792
            if (token.kind == GT && diamondAllowed) {
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  1793
                checkDiamond();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1794
                mode |= DIAMOND;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1795
                nextToken();
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  1796
                return List.nil();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1797
            } else {
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1798
                ListBuffer<JCExpression> args = ListBuffer.lb();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1799
                args.append(((mode & EXPR) == 0) ? typeArgument() : parseType());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1800
                while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1801
                    nextToken();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1802
                    args.append(((mode & EXPR) == 0) ? typeArgument() : parseType());
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1803
                }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1804
                switch (token.kind) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1805
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1806
                case GTGTGTEQ: case GTGTEQ: case GTEQ:
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1807
                case GTGTGT: case GTGT:
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1808
                    token = S.split();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1809
                    break;
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  1810
                case GT:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1811
                    nextToken();
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  1812
                    break;
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1813
                default:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1814
                    args.append(syntaxError(token.pos, "expected", GT));
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1815
                    break;
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1816
                }
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1817
                return args.toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1818
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1819
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1820
            return List.<JCExpression>of(syntaxError(token.pos, "expected", LT));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1821
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1822
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1823
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1824
    /**
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1825
     *  {@literal
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1826
     *  TypeArgument = Type
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1827
     *               | [Annotations] "?"
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1828
     *               | [Annotations] "?" EXTENDS Type {"&" Type}
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1829
     *               | [Annotations] "?" SUPER Type
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1830
     *  }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1831
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1832
    JCExpression typeArgument() {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1833
        List<JCAnnotation> annotations = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1834
        if (token.kind != QUES) return parseType(annotations);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1835
        int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1836
        nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1837
        JCExpression result;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1838
        if (token.kind == EXTENDS) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1839
            TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.EXTENDS));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1840
            nextToken();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1841
            JCExpression bound = parseType();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1842
            result = F.at(pos).Wildcard(t, bound);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1843
        } else if (token.kind == SUPER) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1844
            TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.SUPER));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1845
            nextToken();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1846
            JCExpression bound = parseType();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1847
            result = F.at(pos).Wildcard(t, bound);
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1848
        } else if (LAX_IDENTIFIER.accepts(token.kind)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1849
            //error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  1850
            TypeBoundKind t = F.at(Position.NOPOS).TypeBoundKind(BoundKind.UNBOUND);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1851
            JCExpression wc = toP(F.at(pos).Wildcard(t, null));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1852
            JCIdent id = toP(F.at(token.pos).Ident(ident()));
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  1853
            JCErroneous err = F.at(pos).Erroneous(List.<JCTree>of(wc, id));
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  1854
            reportSyntaxError(err, "expected3", GT, EXTENDS, SUPER);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1855
            result = err;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1856
        } else {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1857
            TypeBoundKind t = toP(F.at(pos).TypeBoundKind(BoundKind.UNBOUND));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1858
            result = toP(F.at(pos).Wildcard(t, null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1859
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1860
        if (!annotations.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1861
            result = toP(F.at(annotations.head.pos).AnnotatedType(annotations,result));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1862
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1863
        return result;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1864
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1865
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1866
    JCTypeApply typeArguments(JCExpression t, boolean diamondAllowed) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1867
        int pos = token.pos;
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1868
        List<JCExpression> args = typeArguments(diamondAllowed);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1869
        return toP(F.at(pos).TypeApply(t, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1870
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1871
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1872
    /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1873
     * BracketsOpt = { [Annotations] "[" "]" }*
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1874
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1875
     * <p>
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1876
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1877
     * <code>annotations</code> is the list of annotations targeting
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1878
     * the expression <code>t</code>.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1879
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1880
    private JCExpression bracketsOpt(JCExpression t,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1881
            List<JCAnnotation> annotations) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1882
        List<JCAnnotation> nextLevelAnnotations = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1883
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1884
        if (token.kind == LBRACKET) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1885
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1886
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1887
            t = bracketsOptCont(t, pos, nextLevelAnnotations);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1888
        } else if (!nextLevelAnnotations.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1889
            if (permitTypeAnnotationsPushBack) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1890
                this.typeAnnotationsPushedBack = nextLevelAnnotations;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1891
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1892
                return illegal(nextLevelAnnotations.head.pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1893
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1894
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1895
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1896
        if (!annotations.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1897
            t = toP(F.at(token.pos).AnnotatedType(annotations, t));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1898
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1899
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1900
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1902
    /** BracketsOpt = [ "[" "]" { [Annotations] "[" "]"} ]
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1903
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1904
    private JCExpression bracketsOpt(JCExpression t) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1905
        return bracketsOpt(t, List.<JCAnnotation>nil());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1906
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1907
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1908
    private JCExpression bracketsOptCont(JCExpression t, int pos,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1909
            List<JCAnnotation> annotations) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1910
        accept(RBRACKET);
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1911
        t = bracketsOpt(t);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1912
        t = toP(F.at(pos).TypeArray(t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1913
        if (annotations.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1914
            t = toP(F.at(pos).AnnotatedType(annotations, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1915
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1916
        return t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1917
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1918
06bc494ca11e Initial load
duke
parents:
diff changeset
  1919
    /** BracketsSuffixExpr = "." CLASS
06bc494ca11e Initial load
duke
parents:
diff changeset
  1920
     *  BracketsSuffixType =
06bc494ca11e Initial load
duke
parents:
diff changeset
  1921
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1922
    JCExpression bracketsSuffix(JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1923
        if ((mode & EXPR) != 0 && token.kind == DOT) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1924
            mode = EXPR;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1925
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1926
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1927
            accept(CLASS);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  1928
            if (token.pos == endPosTable.errorEndPos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1929
                // error recovery
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1930
                Name name;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1931
                if (LAX_IDENTIFIER.accepts(token.kind)) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1932
                    name = token.name();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1933
                    nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1934
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
                    name = names.error;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1936
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1937
                t = F.at(pos).Erroneous(List.<JCTree>of(toP(F.at(pos).Select(t, name))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1938
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1939
                t = toP(F.at(pos).Select(t, names._class));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1940
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1941
        } else if ((mode & TYPE) != 0) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1942
            if (token.kind != COLCOL) {
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1943
                mode = TYPE;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1944
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1945
        } else if (token.kind != COLCOL) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1946
            syntaxError(token.pos, "dot.class.expected");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1947
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1948
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1949
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1950
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1951
    /**
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1952
     * MemberReferenceSuffix = "::" [TypeArguments] Ident
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1953
     *                       | "::" [TypeArguments] "new"
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1954
     */
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1955
    JCExpression memberReferenceSuffix(JCExpression t) {
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1956
        int pos1 = token.pos;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1957
        accept(COLCOL);
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1958
        return memberReferenceSuffix(pos1, t);
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1959
    }
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1960
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1961
    JCExpression memberReferenceSuffix(int pos1, JCExpression t) {
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1962
        checkMethodReferences();
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1963
        mode = EXPR;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1964
        List<JCExpression> typeArgs = null;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1965
        if (token.kind == LT) {
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1966
            typeArgs = typeArguments(false);
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1967
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1968
        Name refName;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1969
        ReferenceMode refMode;
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1970
        if (token.kind == NEW) {
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1971
            refMode = ReferenceMode.NEW;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1972
            refName = names.init;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1973
            nextToken();
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1974
        } else {
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1975
            refMode = ReferenceMode.INVOKE;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1976
            refName = ident();
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1977
        }
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1978
        return toP(F.at(t.getStartPosition()).Reference(refMode, refName, t, typeArgs));
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1979
    }
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1980
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1981
    /** Creator = [Annotations] Qualident [TypeArguments] ( ArrayCreatorRest | ClassCreatorRest )
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1982
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1983
    JCExpression creator(int newpos, List<JCExpression> typeArgs) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1984
        List<JCAnnotation> newAnnotations = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1985
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1986
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1987
        case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1988
        case DOUBLE: case BOOLEAN:
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1989
            if (typeArgs == null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1990
                if (newAnnotations.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1991
                    return arrayCreatorRest(newpos, basicType());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1992
                } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1993
                    return arrayCreatorRest(newpos, toP(F.at(newAnnotations.head.pos).AnnotatedType(newAnnotations, basicType())));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1994
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1995
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1997
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1999
        JCExpression t = qualident(true);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2000
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2001
        // handle type annotations for non primitive arrays
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2002
        if (newAnnotations.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2003
            t = insertAnnotationsToMostInner(t, newAnnotations, false);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2004
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2005
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2006
        int oldmode = mode;
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2007
        mode = TYPE;
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2008
        boolean diamondFound = false;
10199
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  2009
        int lastTypeargsPos = -1;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2010
        if (token.kind == LT) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2011
            checkGenerics();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2012
            lastTypeargsPos = token.pos;
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2013
            t = typeArguments(t, true);
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2014
            diamondFound = (mode & DIAMOND) != 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2015
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2016
        while (token.kind == DOT) {
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2017
            if (diamondFound) {
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2018
                //cannot select after a diamond
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2019
                illegal();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2020
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2021
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2022
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2023
            List<JCAnnotation> tyannos = typeAnnotationsOpt();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2024
            t = toP(F.at(pos).Select(t, ident()));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2025
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2026
            if (tyannos != null && tyannos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2027
                t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2028
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2029
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2030
            if (token.kind == LT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2031
                lastTypeargsPos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2032
                checkGenerics();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2033
                t = typeArguments(t, true);
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2034
                diamondFound = (mode & DIAMOND) != 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2035
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2036
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2037
        mode = oldmode;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2038
        if (token.kind == LBRACKET || token.kind == MONKEYS_AT) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2039
            JCExpression e = arrayCreatorRest(newpos, t);
10199
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  2040
            if (diamondFound) {
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  2041
                reportSyntaxError(lastTypeargsPos, "cannot.create.array.with.diamond");
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  2042
                return toP(F.at(newpos).Erroneous(List.of(e)));
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  2043
            }
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  2044
            else if (typeArgs != null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2045
                int pos = newpos;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2046
                if (!typeArgs.isEmpty() && typeArgs.head.pos != Position.NOPOS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2047
                    // note: this should always happen but we should
06bc494ca11e Initial load
duke
parents:
diff changeset
  2048
                    // not rely on this as the parser is continuously
06bc494ca11e Initial load
duke
parents:
diff changeset
  2049
                    // modified to improve error recovery.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2050
                    pos = typeArgs.head.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2051
                }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2052
                setErrorEndPos(S.prevToken().endPos);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2053
                JCErroneous err = F.at(pos).Erroneous(typeArgs.prepend(e));
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2054
                reportSyntaxError(err, "cannot.create.array.with.type.arguments");
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2055
                return toP(err);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2056
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2057
            return e;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2058
        } else if (token.kind == LPAREN) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2059
            JCNewClass newClass = classCreatorRest(newpos, null, typeArgs, t);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2060
            if (newClass.def != null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2061
                assert newClass.def.mods.annotations.isEmpty();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2062
                if (newAnnotations.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2063
                    newClass.def.mods.pos = earlier(newClass.def.mods.pos, newAnnotations.head.pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2064
                    newClass.def.mods.annotations = List.convert(JCAnnotation.class, newAnnotations);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2065
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2066
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2067
            return newClass;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2068
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2069
            setErrorEndPos(token.pos);
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2070
            reportSyntaxError(token.pos, "expected2", LPAREN, LBRACKET);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2071
            t = toP(F.at(newpos).NewClass(null, typeArgs, t, List.<JCExpression>nil(), null));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2072
            return toP(F.at(newpos).Erroneous(List.<JCTree>of(t)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2073
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2074
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2075
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2076
    /** InnerCreator = [Annotations] Ident [TypeArguments] ClassCreatorRest
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2078
    JCExpression innerCreator(int newpos, List<JCExpression> typeArgs, JCExpression encl) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2079
        List<JCAnnotation> newAnnotations = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2080
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2081
        JCExpression t = toP(F.at(token.pos).Ident(ident()));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2082
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2083
        if (newAnnotations.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2084
            t = toP(F.at(newAnnotations.head.pos).AnnotatedType(newAnnotations, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2085
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2086
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2087
        if (token.kind == LT) {
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  2088
            int oldmode = mode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2089
            checkGenerics();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2090
            t = typeArguments(t, true);
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  2091
            mode = oldmode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2092
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2093
        return classCreatorRest(newpos, encl, typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2094
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2095
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2096
    /** ArrayCreatorRest = [Annotations] "[" ( "]" BracketsOpt ArrayInitializer
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2097
     *                         | Expression "]" {[Annotations]  "[" Expression "]"} BracketsOpt )
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2098
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2099
    JCExpression arrayCreatorRest(int newpos, JCExpression elemtype) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2100
        List<JCAnnotation> annos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2101
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2102
        accept(LBRACKET);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2103
        if (token.kind == RBRACKET) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2104
            accept(RBRACKET);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2105
            elemtype = bracketsOpt(elemtype, annos);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2106
            if (token.kind == LBRACE) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2107
                JCNewArray na = (JCNewArray)arrayInitializer(newpos, elemtype);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2108
                if (annos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2109
                    // when an array initializer is present then
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2110
                    // the parsed annotations should target the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2111
                    // new array tree
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2112
                    // bracketsOpt inserts the annotation in
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2113
                    // elemtype, and it needs to be corrected
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2114
                    //
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2115
                    JCAnnotatedType annotated = (JCAnnotatedType)elemtype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2116
                    assert annotated.annotations == annos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2117
                    na.annotations = annotated.annotations;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2118
                    na.elemtype = annotated.underlyingType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2119
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2120
                return na;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2121
            } else {
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2122
                JCExpression t = toP(F.at(newpos).NewArray(elemtype, List.<JCExpression>nil(), null));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2123
                return syntaxError(token.pos, List.<JCTree>of(t), "array.dimension.missing");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2124
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2125
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2126
            ListBuffer<JCExpression> dims = new ListBuffer<JCExpression>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2127
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2128
            // maintain array dimension type annotations
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2129
            ListBuffer<List<JCAnnotation>> dimAnnotations = ListBuffer.lb();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2130
            dimAnnotations.append(annos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2131
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2132
            dims.append(parseExpression());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2133
            accept(RBRACKET);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2134
            while (token.kind == LBRACKET
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2135
                    || token.kind == MONKEYS_AT) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2136
                List<JCAnnotation> maybeDimAnnos = typeAnnotationsOpt();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2137
                int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2138
                nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2139
                if (token.kind == RBRACKET) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2140
                    elemtype = bracketsOptCont(elemtype, pos, maybeDimAnnos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2141
                } else {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2142
                    if (token.kind == RBRACKET) { // no dimension
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2143
                        elemtype = bracketsOptCont(elemtype, pos, maybeDimAnnos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2144
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2145
                        dimAnnotations.append(maybeDimAnnos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2146
                        dims.append(parseExpression());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2147
                        accept(RBRACKET);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2148
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2149
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2151
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2152
            JCNewArray na = toP(F.at(newpos).NewArray(elemtype, dims.toList(), null));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2153
            na.dimAnnotations = dimAnnotations.toList();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2154
            return na;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2155
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
06bc494ca11e Initial load
duke
parents:
diff changeset
  2158
    /** ClassCreatorRest = Arguments [ClassBody]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
     */
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 2723
diff changeset
  2160
    JCNewClass classCreatorRest(int newpos,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2161
                                  JCExpression encl,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2162
                                  List<JCExpression> typeArgs,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2163
                                  JCExpression t)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2164
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2165
        List<JCExpression> args = arguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2166
        JCClassDecl body = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2167
        if (token.kind == LBRACE) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2168
            int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2169
            List<JCTree> defs = classOrInterfaceBody(names.empty, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2170
            JCModifiers mods = F.at(Position.NOPOS).Modifiers(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2171
            body = toP(F.at(pos).AnonymousClassDef(mods, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2172
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2173
        return toP(F.at(newpos).NewClass(encl, typeArgs, t, args, body));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2175
06bc494ca11e Initial load
duke
parents:
diff changeset
  2176
    /** ArrayInitializer = "{" [VariableInitializer {"," VariableInitializer}] [","] "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2177
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2178
    JCExpression arrayInitializer(int newpos, JCExpression t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2179
        accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2180
        ListBuffer<JCExpression> elems = new ListBuffer<JCExpression>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2181
        if (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2182
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2183
        } else if (token.kind != RBRACE) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2184
            elems.append(variableInitializer());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2185
            while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2186
                nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2187
                if (token.kind == RBRACE) break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2188
                elems.append(variableInitializer());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2189
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2190
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2191
        accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2192
        return toP(F.at(newpos).NewArray(t, List.<JCExpression>nil(), elems.toList()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2193
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2194
06bc494ca11e Initial load
duke
parents:
diff changeset
  2195
    /** VariableInitializer = ArrayInitializer | Expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  2196
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2197
    public JCExpression variableInitializer() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2198
        return token.kind == LBRACE ? arrayInitializer(token.pos, null) : parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2199
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2200
06bc494ca11e Initial load
duke
parents:
diff changeset
  2201
    /** ParExpression = "(" Expression ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2202
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2203
    JCExpression parExpression() {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  2204
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2205
        accept(LPAREN);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2206
        JCExpression t = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2207
        accept(RPAREN);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  2208
        return toP(F.at(pos).Parens(t));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2209
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
06bc494ca11e Initial load
duke
parents:
diff changeset
  2211
    /** Block = "{" BlockStatements "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2212
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2213
    JCBlock block(int pos, long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2214
        accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2215
        List<JCStatement> stats = blockStatements();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2216
        JCBlock t = F.at(pos).Block(flags, stats);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2217
        while (token.kind == CASE || token.kind == DEFAULT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2218
            syntaxError("orphaned", token.kind);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2219
            switchBlockStatementGroups();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2220
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2221
        // the Block node has a field "endpos" for first char of last token, which is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2222
        // usually but not necessarily the last char of the last token.
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2223
        t.endpos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2224
        accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2225
        return toP(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2226
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2227
06bc494ca11e Initial load
duke
parents:
diff changeset
  2228
    public JCBlock block() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2229
        return block(token.pos, 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2230
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2231
06bc494ca11e Initial load
duke
parents:
diff changeset
  2232
    /** BlockStatements = { BlockStatement }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2233
     *  BlockStatement  = LocalVariableDeclarationStatement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2234
     *                  | ClassOrInterfaceOrEnumDeclaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  2235
     *                  | [Ident ":"] Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2236
     *  LocalVariableDeclarationStatement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2237
     *                  = { FINAL | '@' Annotation } Type VariableDeclarators ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2238
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2239
    @SuppressWarnings("fallthrough")
06bc494ca11e Initial load
duke
parents:
diff changeset
  2240
    List<JCStatement> blockStatements() {
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2241
        //todo: skip to anchor on error(?)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2242
        ListBuffer<JCStatement> stats = new ListBuffer<JCStatement>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2243
        while (true) {
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2244
            List<JCStatement> stat = blockStatement();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2245
            if (stat.isEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2246
                return stats.toList();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2247
            } else {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2248
                if (token.pos <= endPosTable.errorEndPos) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2249
                    skip(false, true, true, true);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2250
                }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2251
                stats.addAll(stat);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2252
            }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2253
        }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2254
    }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2255
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2256
    /*
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2257
     * This method parses a statement treating it as a block, relaxing the
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2258
     * JLS restrictions, allows us to parse more faulty code, doing so
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2259
     * enables us to provide better and accurate diagnostics to the user.
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2260
     */
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2261
    JCStatement parseStatementAsBlock() {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2262
        int pos = token.pos;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2263
        List<JCStatement> stats = blockStatement();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2264
        if (stats.isEmpty()) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2265
            JCErroneous e = F.at(pos).Erroneous();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2266
            error(e, "illegal.start.of.stmt");
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2267
            return F.at(pos).Exec(e);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2268
        } else {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2269
            JCStatement first = stats.head;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2270
            String error = null;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2271
            switch (first.getTag()) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2272
            case CLASSDEF:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2273
                error = "class.not.allowed";
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2274
                break;
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2275
            case VARDEF:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2276
                error = "variable.not.allowed";
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2277
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2278
            }
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2279
            if (error != null) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2280
                error(first, error);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2281
                List<JCBlock> blist = List.of(F.at(first.pos).Block(0, stats));
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2282
                return toP(F.at(pos).Exec(F.at(first.pos).Erroneous(blist)));
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2283
            }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2284
            return first;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2285
        }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2286
    }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2287
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2288
    @SuppressWarnings("fallthrough")
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2289
    List<JCStatement> blockStatement() {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2290
        //todo: skip to anchor on error(?)
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2291
        int pos = token.pos;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2292
        switch (token.kind) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2293
        case RBRACE: case CASE: case DEFAULT: case EOF:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2294
            return List.nil();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2295
        case LBRACE: case IF: case FOR: case WHILE: case DO: case TRY:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2296
        case SWITCH: case SYNCHRONIZED: case RETURN: case THROW: case BREAK:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2297
        case CONTINUE: case SEMI: case ELSE: case FINALLY: case CATCH:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2298
            return List.of(parseStatement());
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2299
        case MONKEYS_AT:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2300
        case FINAL: {
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  2301
            Comment dc = token.comment(CommentStyle.JAVADOC);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2302
            JCModifiers mods = modifiersOpt();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2303
            if (token.kind == INTERFACE ||
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2304
                token.kind == CLASS ||
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2305
                allowEnums && token.kind == ENUM) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2306
                return List.of(classOrInterfaceOrEnumDeclaration(mods, dc));
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2307
            } else {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2308
                JCExpression t = parseType();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2309
                ListBuffer<JCStatement> stats =
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2310
                        variableDeclarators(mods, t, new ListBuffer<JCStatement>());
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2311
                // A "LocalVariableDeclarationStatement" subsumes the terminating semicolon
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  2312
                storeEnd(stats.last(), token.endPos);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2313
                accept(SEMI);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2314
                return stats.toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2315
            }
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2316
        }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2317
        case ABSTRACT: case STRICTFP: {
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  2318
            Comment dc = token.comment(CommentStyle.JAVADOC);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2319
            JCModifiers mods = modifiersOpt();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2320
            return List.of(classOrInterfaceOrEnumDeclaration(mods, dc));
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2321
        }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2322
        case INTERFACE:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2323
        case CLASS:
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  2324
            Comment dc = token.comment(CommentStyle.JAVADOC);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2325
            return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc));
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2326
        case ENUM:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2327
        case ASSERT:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2328
            if (allowEnums && token.kind == ENUM) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2329
                error(token.pos, "local.enum");
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2330
                dc = token.comment(CommentStyle.JAVADOC);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2331
                return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc));
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2332
            } else if (allowAsserts && token.kind == ASSERT) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2333
                return List.of(parseStatement());
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2334
            }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2335
            /* fall through to default */
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2336
        default:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2337
            Token prevToken = token;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2338
            JCExpression t = term(EXPR | TYPE);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2339
            if (token.kind == COLON && t.hasTag(IDENT)) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2340
                nextToken();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2341
                JCStatement stat = parseStatement();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2342
                return List.<JCStatement>of(F.at(pos).Labelled(prevToken.name(), stat));
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2343
            } else if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2344
                pos = token.pos;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2345
                JCModifiers mods = F.at(Position.NOPOS).Modifiers(0);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2346
                F.at(pos);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2347
                ListBuffer<JCStatement> stats =
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2348
                        variableDeclarators(mods, t, new ListBuffer<JCStatement>());
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2349
                // A "LocalVariableDeclarationStatement" subsumes the terminating semicolon
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  2350
                storeEnd(stats.last(), token.endPos);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2351
                accept(SEMI);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2352
                return stats.toList();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2353
            } else {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2354
                // This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2355
                JCExpressionStatement expr = to(F.at(pos).Exec(checkExprStat(t)));
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2356
                accept(SEMI);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2357
                return List.<JCStatement>of(expr);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2358
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2359
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2360
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2361
06bc494ca11e Initial load
duke
parents:
diff changeset
  2362
    /** Statement =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2363
     *       Block
06bc494ca11e Initial load
duke
parents:
diff changeset
  2364
     *     | IF ParExpression Statement [ELSE Statement]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2365
     *     | FOR "(" ForInitOpt ";" [Expression] ";" ForUpdateOpt ")" Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2366
     *     | FOR "(" FormalParameter : Expression ")" Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2367
     *     | WHILE ParExpression Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2368
     *     | DO Statement WHILE ParExpression ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2369
     *     | TRY Block ( Catches | [Catches] FinallyPart )
8234
5050975e2df0 7014734: Project Coin: Allow optional trailing semicolon to terminate resources list in try-with-resources
darcy
parents: 8224
diff changeset
  2370
     *     | TRY "(" ResourceSpecification ";"opt ")" Block [Catches] [FinallyPart]
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2371
     *     | SWITCH ParExpression "{" SwitchBlockStatementGroups "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2372
     *     | SYNCHRONIZED ParExpression Block
06bc494ca11e Initial load
duke
parents:
diff changeset
  2373
     *     | RETURN [Expression] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2374
     *     | THROW Expression ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2375
     *     | BREAK [Ident] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2376
     *     | CONTINUE [Ident] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2377
     *     | ASSERT Expression [ ":" Expression ] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2378
     *     | ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2379
     *     | ExpressionStatement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2380
     *     | Ident ":" Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2381
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2382
    @SuppressWarnings("fallthrough")
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2383
    public JCStatement parseStatement() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2384
        int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2385
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2386
        case LBRACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2387
            return block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2388
        case IF: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2389
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2390
            JCExpression cond = parExpression();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2391
            JCStatement thenpart = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2392
            JCStatement elsepart = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2393
            if (token.kind == ELSE) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2394
                nextToken();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2395
                elsepart = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2396
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2397
            return F.at(pos).If(cond, thenpart, elsepart);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2398
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2399
        case FOR: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2400
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2401
            accept(LPAREN);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2402
            List<JCStatement> inits = token.kind == SEMI ? List.<JCStatement>nil() : forInit();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2403
            if (inits.length() == 1 &&
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  2404
                inits.head.hasTag(VARDEF) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2405
                ((JCVariableDecl) inits.head).init == null &&
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2406
                token.kind == COLON) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2407
                checkForeach();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2408
                JCVariableDecl var = (JCVariableDecl)inits.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2409
                accept(COLON);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2410
                JCExpression expr = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2411
                accept(RPAREN);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2412
                JCStatement body = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2413
                return F.at(pos).ForeachLoop(var, expr, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2414
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2415
                accept(SEMI);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2416
                JCExpression cond = token.kind == SEMI ? null : parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2417
                accept(SEMI);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2418
                List<JCExpressionStatement> steps = token.kind == RPAREN ? List.<JCExpressionStatement>nil() : forUpdate();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2419
                accept(RPAREN);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2420
                JCStatement body = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2421
                return F.at(pos).ForLoop(inits, cond, steps, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2422
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2423
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2424
        case WHILE: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2425
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2426
            JCExpression cond = parExpression();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2427
            JCStatement body = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2428
            return F.at(pos).WhileLoop(cond, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2429
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2430
        case DO: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2431
            nextToken();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2432
            JCStatement body = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2433
            accept(WHILE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2434
            JCExpression cond = parExpression();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2435
            JCDoWhileLoop t = to(F.at(pos).DoLoop(body, cond));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2436
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2437
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2438
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2439
        case TRY: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2440
            nextToken();
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2441
            List<JCTree> resources = List.<JCTree>nil();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2442
            if (token.kind == LPAREN) {
7211
163fe60f63de 6970016: Clean up ARM/try-with-resources implementation
mcimadamore
parents: 7076
diff changeset
  2443
                checkTryWithResources();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2444
                nextToken();
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2445
                resources = resources();
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2446
                accept(RPAREN);
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2447
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2448
            JCBlock body = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2449
            ListBuffer<JCCatch> catchers = new ListBuffer<JCCatch>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2450
            JCBlock finalizer = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2451
            if (token.kind == CATCH || token.kind == FINALLY) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2452
                while (token.kind == CATCH) catchers.append(catchClause());
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2453
                if (token.kind == FINALLY) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2454
                    nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2455
                    finalizer = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2456
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2457
            } else {
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2458
                if (allowTWR) {
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2459
                    if (resources.isEmpty())
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2460
                        error(pos, "try.without.catch.finally.or.resource.decls");
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2461
                } else
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  2462
                    error(pos, "try.without.catch.or.finally");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2463
            }
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2464
            return F.at(pos).Try(resources, body, catchers.toList(), finalizer);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2465
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2466
        case SWITCH: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2467
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2468
            JCExpression selector = parExpression();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2469
            accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2470
            List<JCCase> cases = switchBlockStatementGroups();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2471
            JCSwitch t = to(F.at(pos).Switch(selector, cases));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2472
            accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2473
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2474
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2475
        case SYNCHRONIZED: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2476
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2477
            JCExpression lock = parExpression();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2478
            JCBlock body = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2479
            return F.at(pos).Synchronized(lock, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2480
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2481
        case RETURN: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2482
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2483
            JCExpression result = token.kind == SEMI ? null : parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2484
            JCReturn t = to(F.at(pos).Return(result));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2485
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2486
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2487
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2488
        case THROW: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2489
            nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2490
            JCExpression exc = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2491
            JCThrow t = to(F.at(pos).Throw(exc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2492
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2493
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2494
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2495
        case BREAK: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2496
            nextToken();
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2497
            Name label = LAX_IDENTIFIER.accepts(token.kind) ? ident() : null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2498
            JCBreak t = to(F.at(pos).Break(label));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2499
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2500
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2501
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2502
        case CONTINUE: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2503
            nextToken();
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2504
            Name label = LAX_IDENTIFIER.accepts(token.kind) ? ident() : null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2505
            JCContinue t =  to(F.at(pos).Continue(label));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2506
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2507
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2508
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2509
        case SEMI:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2510
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2511
            return toP(F.at(pos).Skip());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2512
        case ELSE:
14450
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2513
            int elsePos = token.pos;
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2514
            nextToken();
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2515
            return doRecover(elsePos, BasicErrorRecoveryAction.BLOCK_STMT, "else.without.if");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2516
        case FINALLY:
14450
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2517
            int finallyPos = token.pos;
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2518
            nextToken();
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2519
            return doRecover(finallyPos, BasicErrorRecoveryAction.BLOCK_STMT, "finally.without.try");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2520
        case CATCH:
14450
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2521
            return doRecover(token.pos, BasicErrorRecoveryAction.CATCH_CLAUSE, "catch.without.try");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2522
        case ASSERT: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2523
            if (allowAsserts && token.kind == ASSERT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2524
                nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2525
                JCExpression assertion = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2526
                JCExpression message = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2527
                if (token.kind == COLON) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2528
                    nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2529
                    message = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2530
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2531
                JCAssert t = to(F.at(pos).Assert(assertion, message));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2532
                accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2533
                return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2534
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2535
            /* else fall through to default case */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2536
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2537
        case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2538
        default:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2539
            Token prevToken = token;
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2540
            JCExpression expr = parseExpression();
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  2541
            if (token.kind == COLON && expr.hasTag(IDENT)) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2542
                nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2543
                JCStatement stat = parseStatement();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2544
                return F.at(pos).Labelled(prevToken.name(), stat);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2545
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2546
                // This Exec is an "ExpressionStatement"; it subsumes the terminating semicolon
06bc494ca11e Initial load
duke
parents:
diff changeset
  2547
                JCExpressionStatement stat = to(F.at(pos).Exec(checkExprStat(expr)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2548
                accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2549
                return stat;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2550
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2551
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2552
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2553
14450
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2554
    private JCStatement doRecover(int startPos, ErrorRecoveryAction action, String key) {
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2555
        int errPos = S.errPos();
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2556
        JCTree stm = action.doRecover(this);
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2557
        S.errPos(errPos);
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2558
        return toP(F.Exec(syntaxError(startPos, List.<JCTree>of(stm), key)));
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2559
    }
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2560
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2561
    /** CatchClause     = CATCH "(" FormalParameter ")" Block
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2562
     * TODO: the "FormalParameter" is not correct, it uses the special "catchTypes" rule below.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2563
     */
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2564
    protected JCCatch catchClause() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2565
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2566
        accept(CATCH);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2567
        accept(LPAREN);
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2568
        JCModifiers mods = optFinal(Flags.PARAMETER);
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2569
        List<JCExpression> catchTypes = catchTypes();
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2570
        JCExpression paramType = catchTypes.size() > 1 ?
9300
c2de4dd9853b 7033809: Rename "disjunctive" to "union" in javax.lang.model
darcy
parents: 9073
diff changeset
  2571
                toP(F.at(catchTypes.head.getStartPosition()).TypeUnion(catchTypes)) :
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2572
                catchTypes.head;
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2573
        JCVariableDecl formal = variableDeclaratorId(mods, paramType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2574
        accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2575
        JCBlock body = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2576
        return F.at(pos).Catch(formal, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2577
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2578
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2579
    List<JCExpression> catchTypes() {
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2580
        ListBuffer<JCExpression> catchTypes = ListBuffer.lb();
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2581
        catchTypes.add(parseType());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2582
        while (token.kind == BAR) {
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2583
            checkMulticatch();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2584
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2585
            // Instead of qualident this is now parseType.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2586
            // But would that allow too much, e.g. arrays or generics?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2587
            catchTypes.add(parseType());
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2588
        }
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2589
        return catchTypes.toList();
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2590
    }
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2591
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2592
    /** SwitchBlockStatementGroups = { SwitchBlockStatementGroup }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2593
     *  SwitchBlockStatementGroup = SwitchLabel BlockStatements
06bc494ca11e Initial load
duke
parents:
diff changeset
  2594
     *  SwitchLabel = CASE ConstantExpression ":" | DEFAULT ":"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2595
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2596
    List<JCCase> switchBlockStatementGroups() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2597
        ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2598
        while (true) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2599
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2600
            switch (token.kind) {
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2601
            case CASE:
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2602
            case DEFAULT:
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2603
                cases.append(switchBlockStatementGroup());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2604
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2605
            case RBRACE: case EOF:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2606
                return cases.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2607
            default:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2608
                nextToken(); // to ensure progress
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2609
                syntaxError(pos, "expected3",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  2610
                    CASE, DEFAULT, RBRACE);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2611
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2612
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2613
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2614
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2615
    protected JCCase switchBlockStatementGroup() {
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2616
        int pos = token.pos;
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2617
        List<JCStatement> stats;
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2618
        JCCase c;
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2619
        switch (token.kind) {
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2620
        case CASE:
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2621
            nextToken();
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2622
            JCExpression pat = parseExpression();
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2623
            accept(COLON);
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2624
            stats = blockStatements();
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2625
            c = F.at(pos).Case(pat, stats);
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2626
            if (stats.isEmpty())
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2627
                storeEnd(c, S.prevToken().endPos);
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2628
            return c;
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2629
        case DEFAULT:
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2630
            nextToken();
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2631
            accept(COLON);
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2632
            stats = blockStatements();
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2633
            c = F.at(pos).Case(null, stats);
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2634
            if (stats.isEmpty())
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2635
                storeEnd(c, S.prevToken().endPos);
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2636
            return c;
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2637
        }
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2638
        throw new AssertionError("should not reach here");
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2639
    }
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2640
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2641
    /** MoreStatementExpressions = { COMMA StatementExpression }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2642
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2643
    <T extends ListBuffer<? super JCExpressionStatement>> T moreStatementExpressions(int pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2644
                                                                    JCExpression first,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2645
                                                                    T stats) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2646
        // This Exec is a "StatementExpression"; it subsumes no terminating token
06bc494ca11e Initial load
duke
parents:
diff changeset
  2647
        stats.append(toP(F.at(pos).Exec(checkExprStat(first))));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2648
        while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2649
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2650
            pos = token.pos;
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2651
            JCExpression t = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2652
            // This Exec is a "StatementExpression"; it subsumes no terminating token
06bc494ca11e Initial load
duke
parents:
diff changeset
  2653
            stats.append(toP(F.at(pos).Exec(checkExprStat(t))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2654
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2655
        return stats;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2656
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2657
06bc494ca11e Initial load
duke
parents:
diff changeset
  2658
    /** ForInit = StatementExpression MoreStatementExpressions
06bc494ca11e Initial load
duke
parents:
diff changeset
  2659
     *           |  { FINAL | '@' Annotation } Type VariableDeclarators
06bc494ca11e Initial load
duke
parents:
diff changeset
  2660
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2661
    List<JCStatement> forInit() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2662
        ListBuffer<JCStatement> stats = lb();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2663
        int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2664
        if (token.kind == FINAL || token.kind == MONKEYS_AT) {
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2665
            return variableDeclarators(optFinal(0), parseType(), stats).toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2666
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2667
            JCExpression t = term(EXPR | TYPE);
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2668
            if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2669
                return variableDeclarators(modifiersOpt(), t, stats).toList();
12715
139b8354de6a 7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents: 12466
diff changeset
  2670
            } else if ((lastmode & TYPE) != 0 && token.kind == COLON) {
139b8354de6a 7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents: 12466
diff changeset
  2671
                error(pos, "bad.initializer", "for-loop");
139b8354de6a 7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents: 12466
diff changeset
  2672
                return List.of((JCStatement)F.at(pos).VarDef(null, null, t, null));
139b8354de6a 7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents: 12466
diff changeset
  2673
            } else {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2674
                return moreStatementExpressions(pos, t, stats).toList();
12715
139b8354de6a 7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents: 12466
diff changeset
  2675
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2676
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2677
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2678
06bc494ca11e Initial load
duke
parents:
diff changeset
  2679
    /** ForUpdate = StatementExpression MoreStatementExpressions
06bc494ca11e Initial load
duke
parents:
diff changeset
  2680
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2681
    List<JCExpressionStatement> forUpdate() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2682
        return moreStatementExpressions(token.pos,
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2683
                                        parseExpression(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2684
                                        new ListBuffer<JCExpressionStatement>()).toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2685
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2686
06bc494ca11e Initial load
duke
parents:
diff changeset
  2687
    /** AnnotationsOpt = { '@' Annotation }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2688
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2689
     * @param kind Whether to parse an ANNOTATION or TYPE_ANNOTATION
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2690
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2691
    List<JCAnnotation> annotationsOpt(Tag kind) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2692
        if (token.kind != MONKEYS_AT) return List.nil(); // optimization
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2693
        ListBuffer<JCAnnotation> buf = new ListBuffer<JCAnnotation>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2694
        int prevmode = mode;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2695
        while (token.kind == MONKEYS_AT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2696
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2697
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2698
            buf.append(annotation(pos, kind));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2699
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2700
        lastmode = mode;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2701
        mode = prevmode;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2702
        List<JCAnnotation> annotations = buf.toList();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2703
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2704
        return annotations;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2705
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2706
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2707
    List<JCAnnotation> typeAnnotationsOpt() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2708
        List<JCAnnotation> annotations = annotationsOpt(Tag.TYPE_ANNOTATION);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2709
        return annotations;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2710
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2711
06bc494ca11e Initial load
duke
parents:
diff changeset
  2712
    /** ModifiersOpt = { Modifier }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2713
     *  Modifier = PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL
06bc494ca11e Initial load
duke
parents:
diff changeset
  2714
     *           | NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE | "@"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2715
     *           | "@" Annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
  2716
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2717
    JCModifiers modifiersOpt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2718
        return modifiersOpt(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2719
    }
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2720
    protected JCModifiers modifiersOpt(JCModifiers partial) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2721
        long flags;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2722
        ListBuffer<JCAnnotation> annotations = new ListBuffer<JCAnnotation>();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2723
        int pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2724
        if (partial == null) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2725
            flags = 0;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2726
            pos = token.pos;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2727
        } else {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2728
            flags = partial.flags;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2729
            annotations.appendList(partial.annotations);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2730
            pos = partial.pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2731
        }
10948
063463f6535f 7104201: Refactor DocCommentScanner
mcimadamore
parents: 10815
diff changeset
  2732
        if (token.deprecatedFlag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2733
            flags |= Flags.DEPRECATED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2734
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2735
        int lastPos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2736
    loop:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2737
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2738
            long flag;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2739
            switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2740
            case PRIVATE     : flag = Flags.PRIVATE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2741
            case PROTECTED   : flag = Flags.PROTECTED; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2742
            case PUBLIC      : flag = Flags.PUBLIC; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2743
            case STATIC      : flag = Flags.STATIC; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2744
            case TRANSIENT   : flag = Flags.TRANSIENT; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2745
            case FINAL       : flag = Flags.FINAL; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2746
            case ABSTRACT    : flag = Flags.ABSTRACT; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2747
            case NATIVE      : flag = Flags.NATIVE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2748
            case VOLATILE    : flag = Flags.VOLATILE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2749
            case SYNCHRONIZED: flag = Flags.SYNCHRONIZED; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2750
            case STRICTFP    : flag = Flags.STRICTFP; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2751
            case MONKEYS_AT  : flag = Flags.ANNOTATION; break;
14267
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14263
diff changeset
  2752
            case DEFAULT     : checkDefaultMethods(); flag = Flags.DEFAULT; break;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2753
            case ERROR       : flag = 0; nextToken(); break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2754
            default: break loop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2755
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2756
            if ((flags & flag) != 0) error(token.pos, "repeated.modifier");
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2757
            lastPos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2758
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2759
            if (flag == Flags.ANNOTATION) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2760
                checkAnnotations();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2761
                if (token.kind != INTERFACE) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2762
                    JCAnnotation ann = annotation(lastPos, Tag.ANNOTATION);
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2763
                    // 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
  2764
                    if (flags == 0 && annotations.isEmpty())
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2765
                        pos = ann.pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2766
                    annotations.append(ann);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2767
                    flag = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2768
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2769
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2770
            flags |= flag;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2771
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2772
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2773
        case ENUM: flags |= Flags.ENUM; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2774
        case INTERFACE: flags |= Flags.INTERFACE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2775
        default: break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2776
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2777
06bc494ca11e Initial load
duke
parents:
diff changeset
  2778
        /* A modifiers tree with no modifier tokens or annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
  2779
         * has no text position. */
6152
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents: 6148
diff changeset
  2780
        if ((flags & (Flags.ModifierFlags | Flags.ANNOTATION)) == 0 && annotations.isEmpty())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2781
            pos = Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2782
06bc494ca11e Initial load
duke
parents:
diff changeset
  2783
        JCModifiers mods = F.at(pos).Modifiers(flags, annotations.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2784
        if (pos != Position.NOPOS)
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2785
            storeEnd(mods, S.prevToken().endPos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2786
        return mods;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2787
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2788
06bc494ca11e Initial load
duke
parents:
diff changeset
  2789
    /** Annotation              = "@" Qualident [ "(" AnnotationFieldValues ")" ]
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2790
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2791
     * @param pos position of "@" token
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2792
     * @param kind Whether to parse an ANNOTATION or TYPE_ANNOTATION
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2793
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2794
    JCAnnotation annotation(int pos, Tag kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2795
        // accept(AT); // AT consumed by caller
06bc494ca11e Initial load
duke
parents:
diff changeset
  2796
        checkAnnotations();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2797
        if (kind == Tag.TYPE_ANNOTATION) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2798
            checkTypeAnnotations();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2799
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2800
        JCTree ident = qualident(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2801
        List<JCExpression> fieldValues = annotationFieldValuesOpt();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2802
        JCAnnotation ann;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2803
        if (kind == Tag.ANNOTATION) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2804
            ann = F.at(pos).Annotation(ident, fieldValues);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2805
        } else if (kind == Tag.TYPE_ANNOTATION) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2806
            ann = F.at(pos).TypeAnnotation(ident, fieldValues);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2807
        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2808
            throw new AssertionError("Unhandled annotation kind: " + kind);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2809
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2810
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2811
        storeEnd(ann, S.prevToken().endPos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2812
        return ann;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2813
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2814
06bc494ca11e Initial load
duke
parents:
diff changeset
  2815
    List<JCExpression> annotationFieldValuesOpt() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2816
        return (token.kind == LPAREN) ? annotationFieldValues() : List.<JCExpression>nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2817
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2818
06bc494ca11e Initial load
duke
parents:
diff changeset
  2819
    /** AnnotationFieldValues   = "(" [ AnnotationFieldValue { "," AnnotationFieldValue } ] ")" */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2820
    List<JCExpression> annotationFieldValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2821
        accept(LPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2822
        ListBuffer<JCExpression> buf = new ListBuffer<JCExpression>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2823
        if (token.kind != RPAREN) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2824
            buf.append(annotationFieldValue());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2825
            while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2826
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2827
                buf.append(annotationFieldValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2828
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2829
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2830
        accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2831
        return buf.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2832
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2833
06bc494ca11e Initial load
duke
parents:
diff changeset
  2834
    /** AnnotationFieldValue    = AnnotationValue
06bc494ca11e Initial load
duke
parents:
diff changeset
  2835
     *                          | Identifier "=" AnnotationValue
06bc494ca11e Initial load
duke
parents:
diff changeset
  2836
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2837
    JCExpression annotationFieldValue() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2838
        if (token.kind == IDENTIFIER) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2839
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2840
            JCExpression t1 = term1();
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  2841
            if (t1.hasTag(IDENT) && token.kind == EQ) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2842
                int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2843
                accept(EQ);
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2844
                JCExpression v = annotationValue();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2845
                return toP(F.at(pos).Assign(t1, v));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2846
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2847
                return t1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2848
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2849
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2850
        return annotationValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2851
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2852
06bc494ca11e Initial load
duke
parents:
diff changeset
  2853
    /* AnnotationValue          = ConditionalExpression
06bc494ca11e Initial load
duke
parents:
diff changeset
  2854
     *                          | Annotation
3997
35250de1d4ea 6337964: should ignore last comma in annotation array
darcy
parents: 3894
diff changeset
  2855
     *                          | "{" [ AnnotationValue { "," AnnotationValue } ] [","] "}"
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2856
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2857
    JCExpression annotationValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2858
        int pos;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2859
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2860
        case MONKEYS_AT:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2861
            pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2862
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2863
            return annotation(pos, Tag.ANNOTATION);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2864
        case LBRACE:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2865
            pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2866
            accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2867
            ListBuffer<JCExpression> buf = new ListBuffer<JCExpression>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2868
            if (token.kind != RBRACE) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2869
                buf.append(annotationValue());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2870
                while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2871
                    nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2872
                    if (token.kind == RBRACE) break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2873
                    buf.append(annotationValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2874
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2875
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2876
            accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2877
            return toP(F.at(pos).NewArray(null, List.<JCExpression>nil(), buf.toList()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2878
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2879
            mode = EXPR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2880
            return term1();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2881
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2882
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2883
06bc494ca11e Initial load
duke
parents:
diff changeset
  2884
    /** VariableDeclarators = VariableDeclarator { "," VariableDeclarator }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2885
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2886
    public <T extends ListBuffer<? super JCVariableDecl>> T variableDeclarators(JCModifiers mods,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2887
                                                                         JCExpression type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2888
                                                                         T vdefs)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2889
    {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2890
        return variableDeclaratorsRest(token.pos, mods, type, ident(), false, null, vdefs);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2891
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2892
06bc494ca11e Initial load
duke
parents:
diff changeset
  2893
    /** VariableDeclaratorsRest = VariableDeclaratorRest { "," VariableDeclarator }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2894
     *  ConstantDeclaratorsRest = ConstantDeclaratorRest { "," ConstantDeclarator }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2895
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2896
     *  @param reqInit  Is an initializer always required?
06bc494ca11e Initial load
duke
parents:
diff changeset
  2897
     *  @param dc       The documentation comment for the variable declarations, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2898
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2899
    <T extends ListBuffer<? super JCVariableDecl>> T variableDeclaratorsRest(int pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2900
                                                                     JCModifiers mods,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2901
                                                                     JCExpression type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2902
                                                                     Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2903
                                                                     boolean reqInit,
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  2904
                                                                     Comment dc,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2905
                                                                     T vdefs)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2906
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2907
        vdefs.append(variableDeclaratorRest(pos, mods, type, name, reqInit, dc));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2908
        while (token.kind == COMMA) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2909
            // All but last of multiple declarators subsume a comma
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  2910
            storeEnd((JCTree)vdefs.last(), token.endPos);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2911
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2912
            vdefs.append(variableDeclarator(mods, type, reqInit, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2913
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2914
        return vdefs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2915
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2916
06bc494ca11e Initial load
duke
parents:
diff changeset
  2917
    /** VariableDeclarator = Ident VariableDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  2918
     *  ConstantDeclarator = Ident ConstantDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  2919
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  2920
    JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean reqInit, Comment dc) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2921
        return variableDeclaratorRest(token.pos, mods, type, ident(), reqInit, dc);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2922
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2923
06bc494ca11e Initial load
duke
parents:
diff changeset
  2924
    /** VariableDeclaratorRest = BracketsOpt ["=" VariableInitializer]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2925
     *  ConstantDeclaratorRest = BracketsOpt "=" VariableInitializer
06bc494ca11e Initial load
duke
parents:
diff changeset
  2926
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  2927
     *  @param reqInit  Is an initializer always required?
06bc494ca11e Initial load
duke
parents:
diff changeset
  2928
     *  @param dc       The documentation comment for the variable declarations, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2929
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2930
    JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name,
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  2931
                                  boolean reqInit, Comment dc) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2932
        type = bracketsOpt(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2933
        JCExpression init = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2934
        if (token.kind == EQ) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2935
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2936
            init = variableInitializer();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2937
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2938
        else if (reqInit) syntaxError(token.pos, "expected", EQ);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2939
        JCVariableDecl result =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2940
            toP(F.at(pos).VarDef(mods, name, type, init));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2941
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2942
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2943
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2944
06bc494ca11e Initial load
duke
parents:
diff changeset
  2945
    /** VariableDeclaratorId = Ident BracketsOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  2946
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2947
    JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type) {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2948
        return variableDeclaratorId(mods, type, false);
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2949
    }
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2950
    //where
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2951
    JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type, boolean lambdaParameter) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2952
        int pos = token.pos;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2953
        Name name;
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2954
        if (lambdaParameter && token.kind == UNDERSCORE) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2955
            syntaxError(pos, "expected", IDENTIFIER);
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2956
            name = token.name();
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2957
        } else {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2958
            name = ident();
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2959
        }
8047
c7f08cdb5c3c 6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents: 8033
diff changeset
  2960
        if ((mods.flags & Flags.VARARGS) != 0 &&
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2961
                token.kind == LBRACKET) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2962
            log.error(token.pos, "varargs.and.old.array.syntax");
8047
c7f08cdb5c3c 6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents: 8033
diff changeset
  2963
        }
c7f08cdb5c3c 6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents: 8033
diff changeset
  2964
        type = bracketsOpt(type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2965
        return toP(F.at(pos).VarDef(mods, name, type, null));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2966
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2967
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2968
    /** Resources = Resource { ";" Resources }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2969
     */
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2970
    List<JCTree> resources() {
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2971
        ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2972
        defs.append(resource());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2973
        while (token.kind == SEMI) {
8234
5050975e2df0 7014734: Project Coin: Allow optional trailing semicolon to terminate resources list in try-with-resources
darcy
parents: 8224
diff changeset
  2974
            // All but last of multiple declarators must subsume a semicolon
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  2975
            storeEnd(defs.last(), token.endPos);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2976
            int semiColonPos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2977
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2978
            if (token.kind == RPAREN) { // Optional trailing semicolon
8224
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  2979
                                       // after last resource
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  2980
                break;
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  2981
            }
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2982
            defs.append(resource());
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2983
        }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2984
        return defs.toList();
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2985
    }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2986
8224
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  2987
    /** Resource = VariableModifiersOpt Type VariableDeclaratorId = Expression
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2988
     */
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2989
    protected JCTree resource() {
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2990
        JCModifiers optFinal = optFinal(Flags.FINAL);
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2991
        JCExpression type = parseType();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2992
        int pos = token.pos;
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2993
        Name ident = ident();
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2994
        return variableDeclaratorRest(pos, optFinal, type, ident, true, null);
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2995
    }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2996
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2997
    /** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration}
06bc494ca11e Initial load
duke
parents:
diff changeset
  2998
     */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2999
    public JCTree.JCCompilationUnit parseCompilationUnit() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3000
        Token firstToken = token;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3001
        JCExpression pid = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3002
        JCModifiers mods = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3003
        boolean consumedToplevelDoc = false;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3004
        boolean seenImport = false;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3005
        boolean seenPackage = false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3006
        List<JCAnnotation> packageAnnotations = List.nil();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3007
        if (token.kind == MONKEYS_AT)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3008
            mods = modifiersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3009
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3010
        if (token.kind == PACKAGE) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3011
            seenPackage = true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3012
            if (mods != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3013
                checkNoMods(mods.flags);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3014
                packageAnnotations = mods.annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3015
                mods = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3016
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3017
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3018
            pid = qualident(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3019
            accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3020
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3021
        ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3022
        boolean checkForImports = true;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3023
        boolean firstTypeDecl = true;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3024
        while (token.kind != EOF) {
15711
a975ad2bfe35 8005931: javac doesn't set ACC_STRICT for classes with package access
vromero
parents: 15385
diff changeset
  3025
            if (token.pos > 0 && token.pos <= endPosTable.errorEndPos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3026
                // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  3027
                skip(checkForImports, false, false, false);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3028
                if (token.kind == EOF)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3029
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3030
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3031
            if (checkForImports && mods == null && token.kind == IMPORT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3032
                seenImport = true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3033
                defs.append(importDeclaration());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3034
            } else {
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3035
                Comment docComment = token.comment(CommentStyle.JAVADOC);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3036
                if (firstTypeDecl && !seenImport && !seenPackage) {
10948
063463f6535f 7104201: Refactor DocCommentScanner
mcimadamore
parents: 10815
diff changeset
  3037
                    docComment = firstToken.comment(CommentStyle.JAVADOC);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3038
                    consumedToplevelDoc = true;
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6585
diff changeset
  3039
                }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3040
                JCTree def = typeDeclaration(mods, docComment);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3041
                if (def instanceof JCExpressionStatement)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3042
                    def = ((JCExpressionStatement)def).expr;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3043
                defs.append(def);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3044
                if (def instanceof JCClassDecl)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3045
                    checkForImports = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3046
                mods = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3047
                firstTypeDecl = false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3048
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3049
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3050
        JCTree.JCCompilationUnit toplevel = F.at(firstToken.pos).TopLevel(packageAnnotations, pid, defs.toList());
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3051
        if (!consumedToplevelDoc)
10948
063463f6535f 7104201: Refactor DocCommentScanner
mcimadamore
parents: 10815
diff changeset
  3052
            attach(toplevel, firstToken.comment(CommentStyle.JAVADOC));
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  3053
        if (defs.isEmpty())
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3054
            storeEnd(toplevel, S.prevToken().endPos);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3055
        if (keepDocComments)
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3056
            toplevel.docComments = docComments;
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3057
        if (keepLineMap)
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3058
            toplevel.lineMap = S.getLineMap();
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3059
        toplevel.endPositions = this.endPosTable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3060
        return toplevel;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3061
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3062
06bc494ca11e Initial load
duke
parents:
diff changeset
  3063
    /** ImportDeclaration = IMPORT [ STATIC ] Ident { "." Ident } [ "." "*" ] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3064
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3065
    JCTree importDeclaration() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3066
        int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3067
        nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3068
        boolean importStatic = false;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3069
        if (token.kind == STATIC) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3070
            checkStaticImports();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3071
            importStatic = true;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3072
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3073
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3074
        JCExpression pid = toP(F.at(token.pos).Ident(ident()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3075
        do {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3076
            int pos1 = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3077
            accept(DOT);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3078
            if (token.kind == STAR) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3079
                pid = to(F.at(pos1).Select(pid, names.asterisk));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3080
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3081
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3082
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3083
                pid = toP(F.at(pos1).Select(pid, ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3084
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3085
        } while (token.kind == DOT);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3086
        accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3087
        return toP(F.at(pos).Import(pid, importStatic));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3088
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3089
06bc494ca11e Initial load
duke
parents:
diff changeset
  3090
    /** TypeDeclaration = ClassOrInterfaceOrEnumDeclaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3091
     *                  | ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3092
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3093
    JCTree typeDeclaration(JCModifiers mods, Comment docComment) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3094
        int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3095
        if (mods == null && token.kind == SEMI) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3096
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3097
            return toP(F.at(pos).Skip());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3098
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3099
            return classOrInterfaceOrEnumDeclaration(modifiersOpt(mods), docComment);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3101
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3102
06bc494ca11e Initial load
duke
parents:
diff changeset
  3103
    /** ClassOrInterfaceOrEnumDeclaration = ModifiersOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  3104
     *           (ClassDeclaration | InterfaceDeclaration | EnumDeclaration)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3105
     *  @param mods     Any modifiers starting the class or interface declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3106
     *  @param dc       The documentation comment for the class, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3107
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3108
    JCStatement classOrInterfaceOrEnumDeclaration(JCModifiers mods, Comment dc) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3109
        if (token.kind == CLASS) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3110
            return classDeclaration(mods, dc);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3111
        } else if (token.kind == INTERFACE) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3112
            return interfaceDeclaration(mods, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3113
        } else if (allowEnums) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3114
            if (token.kind == ENUM) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3115
                return enumDeclaration(mods, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3116
            } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3117
                int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3118
                List<JCTree> errs;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3119
                if (LAX_IDENTIFIER.accepts(token.kind)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3120
                    errs = List.<JCTree>of(mods, toP(F.at(pos).Ident(ident())));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3121
                    setErrorEndPos(token.pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3122
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3123
                    errs = List.<JCTree>of(mods);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3124
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3125
                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
  3126
                                              CLASS, INTERFACE, ENUM)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3127
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3128
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3129
            if (token.kind == ENUM) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3130
                error(token.pos, "enums.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3131
                allowEnums = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3132
                return enumDeclaration(mods, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3133
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3134
            int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3135
            List<JCTree> errs;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3136
            if (LAX_IDENTIFIER.accepts(token.kind)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3137
                errs = List.<JCTree>of(mods, toP(F.at(pos).Ident(ident())));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3138
                setErrorEndPos(token.pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3139
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3140
                errs = List.<JCTree>of(mods);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3141
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3142
            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
  3143
                                          CLASS, INTERFACE)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3144
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3145
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3146
06bc494ca11e Initial load
duke
parents:
diff changeset
  3147
    /** ClassDeclaration = CLASS Ident TypeParametersOpt [EXTENDS Type]
06bc494ca11e Initial load
duke
parents:
diff changeset
  3148
     *                     [IMPLEMENTS TypeList] ClassBody
06bc494ca11e Initial load
duke
parents:
diff changeset
  3149
     *  @param mods    The modifiers starting the class declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3150
     *  @param dc       The documentation comment for the class, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3151
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3152
    protected JCClassDecl classDeclaration(JCModifiers mods, Comment dc) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3153
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3154
        accept(CLASS);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3155
        Name name = ident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3156
06bc494ca11e Initial load
duke
parents:
diff changeset
  3157
        List<JCTypeParameter> typarams = typeParametersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3158
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8234
diff changeset
  3159
        JCExpression extending = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3160
        if (token.kind == EXTENDS) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3161
            nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3162
            extending = parseType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3163
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3164
        List<JCExpression> implementing = List.nil();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3165
        if (token.kind == IMPLEMENTS) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3166
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3167
            implementing = typeList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3168
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3169
        List<JCTree> defs = classOrInterfaceBody(name, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3170
        JCClassDecl result = toP(F.at(pos).ClassDef(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3171
            mods, name, typarams, extending, implementing, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3172
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3173
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3174
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3175
06bc494ca11e Initial load
duke
parents:
diff changeset
  3176
    /** InterfaceDeclaration = INTERFACE Ident TypeParametersOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  3177
     *                         [EXTENDS TypeList] InterfaceBody
06bc494ca11e Initial load
duke
parents:
diff changeset
  3178
     *  @param mods    The modifiers starting the interface declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3179
     *  @param dc       The documentation comment for the interface, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3180
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3181
    protected JCClassDecl interfaceDeclaration(JCModifiers mods, Comment dc) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3182
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3183
        accept(INTERFACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3184
        Name name = ident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3185
06bc494ca11e Initial load
duke
parents:
diff changeset
  3186
        List<JCTypeParameter> typarams = typeParametersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3187
06bc494ca11e Initial load
duke
parents:
diff changeset
  3188
        List<JCExpression> extending = List.nil();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3189
        if (token.kind == EXTENDS) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3190
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3191
            extending = typeList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3192
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3193
        List<JCTree> defs = classOrInterfaceBody(name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3194
        JCClassDecl result = toP(F.at(pos).ClassDef(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3195
            mods, name, typarams, null, extending, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3196
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3197
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3198
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3199
06bc494ca11e Initial load
duke
parents:
diff changeset
  3200
    /** EnumDeclaration = ENUM Ident [IMPLEMENTS TypeList] EnumBody
06bc494ca11e Initial load
duke
parents:
diff changeset
  3201
     *  @param mods    The modifiers starting the enum declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3202
     *  @param dc       The documentation comment for the enum, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3203
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3204
    protected JCClassDecl enumDeclaration(JCModifiers mods, Comment dc) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3205
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3206
        accept(ENUM);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3207
        Name name = ident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3208
06bc494ca11e Initial load
duke
parents:
diff changeset
  3209
        List<JCExpression> implementing = List.nil();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3210
        if (token.kind == IMPLEMENTS) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3211
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3212
            implementing = typeList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3213
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3214
06bc494ca11e Initial load
duke
parents:
diff changeset
  3215
        List<JCTree> defs = enumBody(name);
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3216
        mods.flags |= Flags.ENUM;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3217
        JCClassDecl result = toP(F.at(pos).
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3218
            ClassDef(mods, name, List.<JCTypeParameter>nil(),
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3219
                null, implementing, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3220
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3221
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3222
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3223
06bc494ca11e Initial load
duke
parents:
diff changeset
  3224
    /** EnumBody = "{" { EnumeratorDeclarationList } [","]
06bc494ca11e Initial load
duke
parents:
diff changeset
  3225
     *                  [ ";" {ClassBodyDeclaration} ] "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3226
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3227
    List<JCTree> enumBody(Name enumName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3228
        accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3229
        ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3230
        if (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3231
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3232
        } else if (token.kind != RBRACE && token.kind != SEMI) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3233
            defs.append(enumeratorDeclaration(enumName));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3234
            while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3235
                nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3236
                if (token.kind == RBRACE || token.kind == SEMI) break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3237
                defs.append(enumeratorDeclaration(enumName));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3238
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3239
            if (token.kind != SEMI && token.kind != RBRACE) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3240
                defs.append(syntaxError(token.pos, "expected3",
939
38e24969c7e9 6717241: some diagnostic argument is prematurely converted into a String object
mcimadamore
parents: 735
diff changeset
  3241
                                COMMA, RBRACE, SEMI));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3242
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3243
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3244
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3245
        if (token.kind == SEMI) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3246
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3247
            while (token.kind != RBRACE && token.kind != EOF) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3248
                defs.appendList(classOrInterfaceBodyDeclaration(enumName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3249
                                                                false));
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3250
                if (token.pos <= endPosTable.errorEndPos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3251
                    // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  3252
                   skip(false, true, true, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3253
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3254
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3255
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3256
        accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3257
        return defs.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3258
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3259
06bc494ca11e Initial load
duke
parents:
diff changeset
  3260
    /** EnumeratorDeclaration = AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  3261
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3262
    JCTree enumeratorDeclaration(Name enumName) {
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3263
        Comment dc = token.comment(CommentStyle.JAVADOC);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3264
        int flags = Flags.PUBLIC|Flags.STATIC|Flags.FINAL|Flags.ENUM;
10948
063463f6535f 7104201: Refactor DocCommentScanner
mcimadamore
parents: 10815
diff changeset
  3265
        if (token.deprecatedFlag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3266
            flags |= Flags.DEPRECATED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3267
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3268
        int pos = token.pos;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3269
        List<JCAnnotation> annotations = annotationsOpt(Tag.ANNOTATION);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3270
        JCModifiers mods = F.at(annotations.isEmpty() ? Position.NOPOS : pos).Modifiers(flags, annotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3271
        List<JCExpression> typeArgs = typeArgumentsOpt();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3272
        int identPos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3273
        Name name = ident();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3274
        int createPos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3275
        List<JCExpression> args = (token.kind == LPAREN)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3276
            ? arguments() : List.<JCExpression>nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3277
        JCClassDecl body = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3278
        if (token.kind == LBRACE) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3279
            JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM | Flags.STATIC);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3280
            List<JCTree> defs = classOrInterfaceBody(names.empty, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3281
            body = toP(F.at(identPos).AnonymousClassDef(mods1, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3282
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3283
        if (args.isEmpty() && body == null)
4704
5206047418c2 6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents: 3997
diff changeset
  3284
            createPos = identPos;
5206047418c2 6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents: 3997
diff changeset
  3285
        JCIdent ident = F.at(identPos).Ident(enumName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3286
        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
  3287
        if (createPos != identPos)
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3288
            storeEnd(create, S.prevToken().endPos);
4704
5206047418c2 6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents: 3997
diff changeset
  3289
        ident = F.at(identPos).Ident(enumName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3290
        JCTree result = toP(F.at(pos).VarDef(mods, name, ident, create));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3291
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3292
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3293
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3294
06bc494ca11e Initial load
duke
parents:
diff changeset
  3295
    /** TypeList = Type {"," Type}
06bc494ca11e Initial load
duke
parents:
diff changeset
  3296
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3297
    List<JCExpression> typeList() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3298
        ListBuffer<JCExpression> ts = new ListBuffer<JCExpression>();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3299
        ts.append(parseType());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3300
        while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3301
            nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3302
            ts.append(parseType());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3303
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3304
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3305
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3306
06bc494ca11e Initial load
duke
parents:
diff changeset
  3307
    /** ClassBody     = "{" {ClassBodyDeclaration} "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3308
     *  InterfaceBody = "{" {InterfaceBodyDeclaration} "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3309
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3310
    List<JCTree> classOrInterfaceBody(Name className, boolean isInterface) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3311
        accept(LBRACE);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3312
        if (token.pos <= endPosTable.errorEndPos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3313
            // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  3314
            skip(false, true, false, false);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3315
            if (token.kind == LBRACE)
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3316
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3317
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3318
        ListBuffer<JCTree> defs = new ListBuffer<JCTree>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3319
        while (token.kind != RBRACE && token.kind != EOF) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3320
            defs.appendList(classOrInterfaceBodyDeclaration(className, isInterface));
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3321
            if (token.pos <= endPosTable.errorEndPos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3322
               // error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  3323
               skip(false, true, true, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3324
           }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3325
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3326
        accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3327
        return defs.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3328
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3329
06bc494ca11e Initial load
duke
parents:
diff changeset
  3330
    /** ClassBodyDeclaration =
06bc494ca11e Initial load
duke
parents:
diff changeset
  3331
     *      ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3332
     *    | [STATIC] Block
06bc494ca11e Initial load
duke
parents:
diff changeset
  3333
     *    | ModifiersOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  3334
     *      ( Type Ident
06bc494ca11e Initial load
duke
parents:
diff changeset
  3335
     *        ( VariableDeclaratorsRest ";" | MethodDeclaratorRest )
06bc494ca11e Initial load
duke
parents:
diff changeset
  3336
     *      | VOID Ident MethodDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  3337
     *      | TypeParameters (Type | VOID) Ident MethodDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  3338
     *      | Ident ConstructorDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  3339
     *      | TypeParameters Ident ConstructorDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  3340
     *      | ClassOrInterfaceOrEnumDeclaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3341
     *      )
06bc494ca11e Initial load
duke
parents:
diff changeset
  3342
     *  InterfaceBodyDeclaration =
06bc494ca11e Initial load
duke
parents:
diff changeset
  3343
     *      ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3344
     *    | ModifiersOpt Type Ident
06bc494ca11e Initial load
duke
parents:
diff changeset
  3345
     *      ( ConstantDeclaratorsRest | InterfaceMethodDeclaratorRest ";" )
06bc494ca11e Initial load
duke
parents:
diff changeset
  3346
     */
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3347
    protected List<JCTree> classOrInterfaceBodyDeclaration(Name className, boolean isInterface) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3348
        if (token.kind == SEMI) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3349
            nextToken();
6585
189f53daed52 6921495: spurious semicolons in class def cause empty NOPOS blocks
jjg
parents: 6152
diff changeset
  3350
            return List.<JCTree>nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3351
        } else {
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3352
            Comment dc = token.comment(CommentStyle.JAVADOC);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3353
            int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3354
            JCModifiers mods = modifiersOpt();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3355
            if (token.kind == CLASS ||
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3356
                token.kind == INTERFACE ||
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3357
                allowEnums && token.kind == ENUM) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3358
                return List.<JCTree>of(classOrInterfaceOrEnumDeclaration(mods, dc));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3359
            } else if (token.kind == LBRACE && !isInterface &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3360
                       (mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3361
                       mods.annotations.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3362
                return List.<JCTree>of(block(pos, mods.flags));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3363
            } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3364
                pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3365
                List<JCTypeParameter> typarams = typeParametersOpt();
8033
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  3366
                // if there are type parameters but no modifiers, save the start
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  3367
                // position of the method in the modifiers.
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  3368
                if (typarams.nonEmpty() && mods.pos == Position.NOPOS) {
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  3369
                    mods.pos = pos;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  3370
                    storeEnd(mods, pos);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  3371
                }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3372
                List<JCAnnotation> annosAfterParams = annotationsOpt(Tag.ANNOTATION);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3373
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3374
                Token tk = token;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3375
                pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3376
                JCExpression type;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3377
                boolean isVoid = token.kind == VOID;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3378
                if (isVoid) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3379
                    if (annosAfterParams.nonEmpty())
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3380
                        illegal(annosAfterParams.head.pos);
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3381
                    type = to(F.at(pos).TypeIdent(TypeTag.VOID));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3382
                    nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3383
                } else {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3384
                    if (annosAfterParams.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3385
                        mods.annotations = mods.annotations.appendList(annosAfterParams);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3386
                        if (mods.pos == Position.NOPOS)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3387
                            mods.pos = mods.annotations.head.pos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3388
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3389
                    // method returns types are un-annotated types
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3390
                    type = unannotatedType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3391
                }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3392
                if (token.kind == LPAREN && !isInterface && type.hasTag(IDENT)) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3393
                    if (isInterface || tk.name() != className)
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  3394
                        error(pos, "invalid.meth.decl.ret.type.req");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3395
                    return List.of(methodDeclaratorRest(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3396
                        pos, mods, null, names.init, typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3397
                        isInterface, true, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3398
                } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3399
                    pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3400
                    Name name = ident();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3401
                    if (token.kind == LPAREN) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3402
                        return List.of(methodDeclaratorRest(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3403
                            pos, mods, type, name, typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3404
                            isInterface, isVoid, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3405
                    } else if (!isVoid && typarams.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3406
                        List<JCTree> defs =
06bc494ca11e Initial load
duke
parents:
diff changeset
  3407
                            variableDeclaratorsRest(pos, mods, type, name, isInterface, dc,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3408
                                                    new ListBuffer<JCTree>()).toList();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3409
                        storeEnd(defs.last(), token.endPos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3410
                        accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3411
                        return defs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3412
                    } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3413
                        pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3414
                        List<JCTree> err = isVoid
06bc494ca11e Initial load
duke
parents:
diff changeset
  3415
                            ? List.<JCTree>of(toP(F.at(pos).MethodDef(mods, name, type, typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3416
                                List.<JCVariableDecl>nil(), List.<JCExpression>nil(), null, null)))
06bc494ca11e Initial load
duke
parents:
diff changeset
  3417
                            : null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3418
                        return List.<JCTree>of(syntaxError(token.pos, err, "expected", LPAREN));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3419
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3420
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3421
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3422
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3423
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3424
06bc494ca11e Initial load
duke
parents:
diff changeset
  3425
    /** MethodDeclaratorRest =
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  3426
     *      FormalParameters BracketsOpt [Throws TypeList] ( MethodBody | [DEFAULT AnnotationValue] ";")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3427
     *  VoidMethodDeclaratorRest =
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  3428
     *      FormalParameters [Throws TypeList] ( MethodBody | ";")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3429
     *  InterfaceMethodDeclaratorRest =
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  3430
     *      FormalParameters BracketsOpt [THROWS TypeList] ";"
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3431
     *  VoidInterfaceMethodDeclaratorRest =
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  3432
     *      FormalParameters [THROWS TypeList] ";"
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3433
     *  ConstructorDeclaratorRest =
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  3434
     *      "(" FormalParameterListOpt ")" [THROWS TypeList] MethodBody
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3435
     */
11147
20770e90d993 7119032: (javac) increase visibility of JavacParser methods to improve subtyping
ksrini
parents: 11144
diff changeset
  3436
    protected JCTree methodDeclaratorRest(int pos,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3437
                              JCModifiers mods,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3438
                              JCExpression type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3439
                              Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3440
                              List<JCTypeParameter> typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3441
                              boolean isInterface, boolean isVoid,
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3442
                              Comment dc) {
15377
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
  3443
        if (isInterface && (mods.flags & Flags.STATIC) != 0) {
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
  3444
            checkStaticInterfaceMethods();
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
  3445
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3446
        JCVariableDecl prevReceiverParam = this.receiverParam;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3447
        try {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3448
            this.receiverParam = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3449
            // Parsing formalParameters sets the receiverParam, if present
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3450
            List<JCVariableDecl> params = formalParameters();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3451
            if (!isVoid) type = bracketsOpt(type);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3452
            List<JCExpression> thrown = List.nil();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3453
            if (token.kind == THROWS) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3454
                nextToken();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3455
                thrown = qualidentList();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3456
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3457
            JCBlock body = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3458
            JCExpression defaultValue;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3459
            if (token.kind == LBRACE) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3460
                body = block();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3461
                defaultValue = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3462
            } else {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3463
                if (token.kind == DEFAULT) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3464
                    accept(DEFAULT);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3465
                    defaultValue = annotationValue();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3466
                } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3467
                    defaultValue = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3468
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3469
                accept(SEMI);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3470
                if (token.pos <= endPosTable.errorEndPos) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3471
                    // error recovery
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3472
                    skip(false, true, false, false);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3473
                    if (token.kind == LBRACE) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3474
                        body = block();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3475
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3476
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3477
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3478
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3479
            JCMethodDecl result =
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3480
                    toP(F.at(pos).MethodDef(mods, name, type, typarams,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3481
                                            receiverParam, params, thrown,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3482
                                            body, defaultValue));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3483
            attach(result, dc);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3484
            return result;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3485
        } finally {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3486
            this.receiverParam = prevReceiverParam;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3487
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3488
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3489
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3490
    /** QualidentList = [Annotations] Qualident {"," [Annotations] Qualident}
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3491
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3492
    List<JCExpression> qualidentList() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3493
        ListBuffer<JCExpression> ts = new ListBuffer<JCExpression>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3494
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3495
        List<JCAnnotation> typeAnnos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3496
        if (!typeAnnos.isEmpty())
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3497
            ts.append(toP(F.at(typeAnnos.head.pos).AnnotatedType(typeAnnos, qualident(true))));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3498
        else
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3499
            ts.append(qualident(true));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3500
        while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3501
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3502
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3503
            typeAnnos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3504
            if (!typeAnnos.isEmpty())
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3505
                ts.append(toP(F.at(typeAnnos.head.pos).AnnotatedType(typeAnnos, qualident(true))));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3506
            else
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3507
                ts.append(qualident(true));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3508
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3509
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3510
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3511
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  3512
    /**
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  3513
     *  {@literal
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  3514
     *  TypeParametersOpt = ["<" TypeParameter {"," TypeParameter} ">"]
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  3515
     *  }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3516
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3517
    List<JCTypeParameter> typeParametersOpt() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3518
        if (token.kind == LT) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3519
            checkGenerics();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3520
            ListBuffer<JCTypeParameter> typarams = new ListBuffer<JCTypeParameter>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3521
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3522
            typarams.append(typeParameter());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3523
            while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3524
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3525
                typarams.append(typeParameter());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3526
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3527
            accept(GT);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3528
            return typarams.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3529
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3530
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3531
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3532
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3533
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  3534
    /**
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  3535
     *  {@literal
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3536
     *  TypeParameter = [Annotations] TypeVariable [TypeParameterBound]
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3537
     *  TypeParameterBound = EXTENDS Type {"&" Type}
06bc494ca11e Initial load
duke
parents:
diff changeset
  3538
     *  TypeVariable = Ident
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  3539
     *  }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3540
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3541
    JCTypeParameter typeParameter() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3542
        int pos = token.pos;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3543
        List<JCAnnotation> annos = typeAnnotationsOpt();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3544
        Name name = ident();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3545
        ListBuffer<JCExpression> bounds = new ListBuffer<JCExpression>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3546
        if (token.kind == EXTENDS) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3547
            nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3548
            bounds.append(parseType());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3549
            while (token.kind == AMP) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3550
                nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3551
                bounds.append(parseType());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3552
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3553
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3554
        return toP(F.at(pos).TypeParameter(name, bounds.toList(), annos));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3555
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3556
06bc494ca11e Initial load
duke
parents:
diff changeset
  3557
    /** FormalParameters = "(" [ FormalParameterList ] ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3558
     *  FormalParameterList = [ FormalParameterListNovarargs , ] LastFormalParameter
06bc494ca11e Initial load
duke
parents:
diff changeset
  3559
     *  FormalParameterListNovarargs = [ FormalParameterListNovarargs , ] FormalParameter
06bc494ca11e Initial load
duke
parents:
diff changeset
  3560
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3561
    List<JCVariableDecl> formalParameters() {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3562
        return formalParameters(false);
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3563
    }
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3564
    List<JCVariableDecl> formalParameters(boolean lambdaParameters) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3565
        ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3566
        JCVariableDecl lastParam;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3567
        accept(LPAREN);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3568
        if (token.kind != RPAREN) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3569
            this.allowThisIdent = true;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3570
            lastParam = formalParameter(lambdaParameters);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3571
            if (lastParam.name.contentEquals(TokenKind.THIS.name)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3572
                this.receiverParam = lastParam;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3573
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3574
                params.append(lastParam);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3575
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3576
            this.allowThisIdent = false;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3577
            while ((lastParam.mods.flags & Flags.VARARGS) == 0 && token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3578
                nextToken();
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3579
                params.append(lastParam = formalParameter(lambdaParameters));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3580
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3581
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3582
        accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3583
        return params.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3584
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3585
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3586
    List<JCVariableDecl> implicitParameters(boolean hasParens) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3587
        if (hasParens) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3588
            accept(LPAREN);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3589
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3590
        ListBuffer<JCVariableDecl> params = new ListBuffer<JCVariableDecl>();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3591
        if (token.kind != RPAREN && token.kind != ARROW) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3592
            params.append(implicitParameter());
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3593
            while (token.kind == COMMA) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3594
                nextToken();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3595
                params.append(implicitParameter());
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3596
            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3597
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3598
        if (hasParens) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3599
            accept(RPAREN);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3600
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3601
        return params.toList();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3602
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3603
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3604
    JCModifiers optFinal(long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3605
        JCModifiers mods = modifiersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3606
        checkNoMods(mods.flags & ~(Flags.FINAL | Flags.DEPRECATED));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3607
        mods.flags |= flags;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3608
        return mods;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3609
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3610
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3611
    /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3612
     * Inserts the annotations (and possibly a new array level)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3613
     * to the left-most type in an array or nested type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3614
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3615
     * When parsing a type like {@code @B Outer.Inner @A []}, the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3616
     * {@code @A} annotation should target the array itself, while
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3617
     * {@code @B} targets the nested type {@code Outer}.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3618
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3619
     * Currently the parser parses the annotation first, then
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3620
     * the array, and then inserts the annotation to the left-most
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3621
     * nested type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3622
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3623
     * When {@code createNewLevel} is true, then a new array
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3624
     * level is inserted as the most inner type, and have the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3625
     * annotations target it.  This is useful in the case of
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3626
     * varargs, e.g. {@code String @A [] @B ...}, as the parser
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3627
     * first parses the type {@code String @A []} then inserts
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3628
     * a new array level with {@code @B} annotation.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3629
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3630
    private JCExpression insertAnnotationsToMostInner(
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3631
            JCExpression type, List<JCAnnotation> annos,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3632
            boolean createNewLevel) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3633
        int origEndPos = getEndPos(type);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3634
        JCExpression mostInnerType = type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3635
        JCArrayTypeTree mostInnerArrayType = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3636
        while (TreeInfo.typeIn(mostInnerType).hasTag(TYPEARRAY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3637
            mostInnerArrayType = (JCArrayTypeTree) TreeInfo.typeIn(mostInnerType);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3638
            mostInnerType = mostInnerArrayType.elemtype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3639
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3640
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3641
        if (createNewLevel) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3642
            mostInnerType = to(F.at(token.pos).TypeArray(mostInnerType));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3643
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3644
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3645
        JCExpression mostInnerTypeToReturn = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3646
        if (annos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3647
            JCExpression lastToModify = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3648
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3649
            while (TreeInfo.typeIn(mostInnerType).hasTag(SELECT) ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3650
                    TreeInfo.typeIn(mostInnerType).hasTag(TYPEAPPLY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3651
                while (TreeInfo.typeIn(mostInnerType).hasTag(SELECT)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3652
                    lastToModify = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3653
                    mostInnerType = ((JCFieldAccess) TreeInfo.typeIn(mostInnerType)).getExpression();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3654
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3655
                while (TreeInfo.typeIn(mostInnerType).hasTag(TYPEAPPLY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3656
                    lastToModify = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3657
                    mostInnerType = ((JCTypeApply) TreeInfo.typeIn(mostInnerType)).clazz;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3658
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3659
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3660
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3661
            mostInnerType = F.at(annos.head.pos).AnnotatedType(annos, mostInnerType);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3662
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3663
            if (TreeInfo.typeIn(lastToModify).hasTag(TYPEAPPLY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3664
                ((JCTypeApply) TreeInfo.typeIn(lastToModify)).clazz = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3665
            } else if (TreeInfo.typeIn(lastToModify).hasTag(SELECT)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3666
                ((JCFieldAccess) TreeInfo.typeIn(lastToModify)).selected = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3667
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3668
                // We never saw a SELECT or TYPEAPPLY, return the annotated type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3669
                mostInnerTypeToReturn = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3670
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3671
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3672
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3673
        if (mostInnerArrayType == null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3674
            return mostInnerTypeToReturn;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3675
        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3676
            mostInnerArrayType.elemtype = mostInnerTypeToReturn;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3677
            storeEnd(type, origEndPos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3678
            return type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3679
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3680
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3681
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3682
    /** FormalParameter = { FINAL | '@' Annotation } Type VariableDeclaratorId
06bc494ca11e Initial load
duke
parents:
diff changeset
  3683
     *  LastFormalParameter = { FINAL | '@' Annotation } Type '...' Ident | FormalParameter
06bc494ca11e Initial load
duke
parents:
diff changeset
  3684
     */
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3685
    protected JCVariableDecl formalParameter() {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3686
        return formalParameter(false);
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3687
    }
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3688
    protected JCVariableDecl formalParameter(boolean lambdaParameter) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3689
        JCModifiers mods = optFinal(Flags.PARAMETER);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3690
        // need to distinguish between vararg annos and array annos
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3691
        // look at typeAnnotationsPushedBack comment
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3692
        this.permitTypeAnnotationsPushBack = true;
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3693
        JCExpression type = parseType();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3694
        this.permitTypeAnnotationsPushBack = false;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3695
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3696
        if (token.kind == ELLIPSIS) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3697
            List<JCAnnotation> varargsAnnos = typeAnnotationsPushedBack;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3698
            typeAnnotationsPushedBack = List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3699
            checkVarargs();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3700
            mods.flags |= Flags.VARARGS;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3701
            // insert var arg type annotations
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3702
            type = insertAnnotationsToMostInner(type, varargsAnnos, true);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3703
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3704
        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3705
            // if not a var arg, then typeAnnotationsPushedBack should be null
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3706
            if (typeAnnotationsPushedBack.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3707
                reportSyntaxError(typeAnnotationsPushedBack.head.pos,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3708
                        "illegal.start.of.type");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3709
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3710
            typeAnnotationsPushedBack = List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3711
        }
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3712
        return variableDeclaratorId(mods, type, lambdaParameter);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3713
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3714
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3715
    protected JCVariableDecl implicitParameter() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3716
        JCModifiers mods = F.at(token.pos).Modifiers(Flags.PARAMETER);
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3717
        return variableDeclaratorId(mods, null, true);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3718
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3719
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3720
/* ---------- auxiliary methods -------------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3721
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  3722
    void error(int pos, String key, Object ... args) {
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  3723
        log.error(DiagnosticFlag.SYNTAX, pos, key, args);
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  3724
    }
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  3725
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3726
    void error(DiagnosticPosition pos, String key, Object ... args) {
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3727
        log.error(DiagnosticFlag.SYNTAX, pos, key, args);
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3728
    }
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3729
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  3730
    void warning(int pos, String key, Object ... args) {
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  3731
        log.warning(pos, key, args);
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  3732
    }
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
  3733
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3734
    /** Check that given tree is a legal expression statement.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3735
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3736
    protected JCExpression checkExprStat(JCExpression t) {
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  3737
        if (!TreeInfo.isExpressionStatement(t)) {
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3738
            JCExpression ret = F.at(t.pos).Erroneous(List.<JCTree>of(t));
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3739
            error(ret, "not.stmt");
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3740
            return ret;
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  3741
        } else {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  3742
            return t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3743
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3744
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3745
06bc494ca11e Initial load
duke
parents:
diff changeset
  3746
    /** Return precedence of operator represented by token,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3747
     *  -1 if token is not a binary operator. @see TreeInfo.opPrec
06bc494ca11e Initial load
duke
parents:
diff changeset
  3748
     */
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3749
    static int prec(TokenKind token) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3750
        JCTree.Tag oc = optag(token);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3751
        return (oc != NO_TAG) ? TreeInfo.opPrec(oc) : -1;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3752
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3753
5013
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3754
    /**
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3755
     * Return the lesser of two positions, making allowance for either one
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3756
     * being unset.
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3757
     */
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3758
    static int earlier(int pos1, int pos2) {
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3759
        if (pos1 == Position.NOPOS)
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3760
            return pos2;
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3761
        if (pos2 == Position.NOPOS)
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3762
            return pos1;
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3763
        return (pos1 < pos2 ? pos1 : pos2);
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3764
    }
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  3765
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3766
    /** Return operation tag of binary operator represented by token,
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3767
     *  No_TAG if token is not a binary operator.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3768
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3769
    static JCTree.Tag optag(TokenKind token) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3770
        switch (token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3771
        case BARBAR:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3772
            return OR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3773
        case AMPAMP:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3774
            return AND;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3775
        case BAR:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3776
            return BITOR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3777
        case BAREQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3778
            return BITOR_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3779
        case CARET:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3780
            return BITXOR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3781
        case CARETEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3782
            return BITXOR_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3783
        case AMP:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3784
            return BITAND;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3785
        case AMPEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3786
            return BITAND_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3787
        case EQEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3788
            return JCTree.Tag.EQ;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3789
        case BANGEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3790
            return NE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3791
        case LT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3792
            return JCTree.Tag.LT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3793
        case GT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3794
            return JCTree.Tag.GT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3795
        case LTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3796
            return LE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3797
        case GTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3798
            return GE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3799
        case LTLT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3800
            return SL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3801
        case LTLTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3802
            return SL_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3803
        case GTGT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3804
            return SR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3805
        case GTGTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3806
            return SR_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3807
        case GTGTGT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3808
            return USR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3809
        case GTGTGTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3810
            return USR_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3811
        case PLUS:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3812
            return JCTree.Tag.PLUS;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3813
        case PLUSEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3814
            return PLUS_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3815
        case SUB:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3816
            return MINUS;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3817
        case SUBEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3818
            return MINUS_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3819
        case STAR:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3820
            return MUL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3821
        case STAREQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3822
            return MUL_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3823
        case SLASH:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3824
            return DIV;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3825
        case SLASHEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3826
            return DIV_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3827
        case PERCENT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3828
            return MOD;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3829
        case PERCENTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3830
            return MOD_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3831
        case INSTANCEOF:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3832
            return TYPETEST;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3833
        default:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3834
            return NO_TAG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3835
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3836
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3837
06bc494ca11e Initial load
duke
parents:
diff changeset
  3838
    /** Return operation tag of unary operator represented by token,
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3839
     *  No_TAG if token is not a binary operator.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3840
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3841
    static JCTree.Tag unoptag(TokenKind token) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3842
        switch (token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3843
        case PLUS:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3844
            return POS;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3845
        case SUB:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3846
            return NEG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3847
        case BANG:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3848
            return NOT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3849
        case TILDE:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3850
            return COMPL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3851
        case PLUSPLUS:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3852
            return PREINC;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3853
        case SUBSUB:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3854
            return PREDEC;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3855
        default:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3856
            return NO_TAG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3857
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3858
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3859
06bc494ca11e Initial load
duke
parents:
diff changeset
  3860
    /** Return type tag of basic type represented by token,
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3861
     *  NONE if token is not a basic type identifier.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3862
     */
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3863
    static TypeTag typetag(TokenKind token) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3864
        switch (token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3865
        case BYTE:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3866
            return TypeTag.BYTE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3867
        case CHAR:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3868
            return TypeTag.CHAR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3869
        case SHORT:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3870
            return TypeTag.SHORT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3871
        case INT:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3872
            return TypeTag.INT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3873
        case LONG:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3874
            return TypeTag.LONG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3875
        case FLOAT:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3876
            return TypeTag.FLOAT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3877
        case DOUBLE:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3878
            return TypeTag.DOUBLE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3879
        case BOOLEAN:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3880
            return TypeTag.BOOLEAN;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3881
        default:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3882
            return TypeTag.NONE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3883
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3884
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3885
06bc494ca11e Initial load
duke
parents:
diff changeset
  3886
    void checkGenerics() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3887
        if (!allowGenerics) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3888
            error(token.pos, "generics.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3889
            allowGenerics = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3890
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3891
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3892
    void checkVarargs() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3893
        if (!allowVarargs) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3894
            error(token.pos, "varargs.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3895
            allowVarargs = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3896
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3897
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3898
    void checkForeach() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3899
        if (!allowForeach) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3900
            error(token.pos, "foreach.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3901
            allowForeach = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3902
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3903
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3904
    void checkStaticImports() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3905
        if (!allowStaticImport) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3906
            error(token.pos, "static.import.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3907
            allowStaticImport = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3908
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3909
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3910
    void checkAnnotations() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3911
        if (!allowAnnotations) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3912
            error(token.pos, "annotations.not.supported.in.source", source.name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3913
            allowAnnotations = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3914
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3915
    }
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  3916
    void checkDiamond() {
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  3917
        if (!allowDiamond) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3918
            error(token.pos, "diamond.not.supported.in.source", source.name);
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  3919
            allowDiamond = true;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  3920
        }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  3921
    }
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  3922
    void checkMulticatch() {
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  3923
        if (!allowMulticatch) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3924
            error(token.pos, "multicatch.not.supported.in.source", source.name);
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  3925
            allowMulticatch = true;
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3926
        }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3927
    }
7211
163fe60f63de 6970016: Clean up ARM/try-with-resources implementation
mcimadamore
parents: 7076
diff changeset
  3928
    void checkTryWithResources() {
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3929
        if (!allowTWR) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3930
            error(token.pos, "try.with.resources.not.supported.in.source", source.name);
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3931
            allowTWR = true;
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3932
        }
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  3933
    }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3934
    void checkLambda() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3935
        if (!allowLambda) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3936
            log.error(token.pos, "lambda.not.supported.in.source", source.name);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3937
            allowLambda = true;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3938
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  3939
    }
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  3940
    void checkMethodReferences() {
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  3941
        if (!allowMethodReferences) {
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  3942
            log.error(token.pos, "method.references.not.supported.in.source", source.name);
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  3943
            allowMethodReferences = true;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  3944
        }
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  3945
    }
14267
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14263
diff changeset
  3946
    void checkDefaultMethods() {
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14263
diff changeset
  3947
        if (!allowDefaultMethods) {
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14263
diff changeset
  3948
            log.error(token.pos, "default.methods.not.supported.in.source", source.name);
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14263
diff changeset
  3949
            allowDefaultMethods = true;
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14263
diff changeset
  3950
        }
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14263
diff changeset
  3951
    }
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  3952
    void checkIntersectionTypesInCast() {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  3953
        if (!allowIntersectionTypesInCast) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  3954
            log.error(token.pos, "intersection.types.in.cast.not.supported.in.source", source.name);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  3955
            allowIntersectionTypesInCast = true;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  3956
        }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  3957
    }
15377
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
  3958
    void checkStaticInterfaceMethods() {
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
  3959
        if (!allowStaticInterfaceMethods) {
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
  3960
            log.error(token.pos, "static.intf.methods.not.supported.in.source", source.name);
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
  3961
            allowStaticInterfaceMethods = true;
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
  3962
        }
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
  3963
    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3964
    void checkTypeAnnotations() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3965
        if (!allowTypeAnnotations) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3966
            log.error(token.pos, "type.annotations.not.supported.in.source", source.name);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3967
            allowTypeAnnotations = true;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3968
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3969
    }
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3970
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3971
    /*
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3972
     * a functional source tree and end position mappings
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3973
     */
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3974
    protected class SimpleEndPosTable extends AbstractEndPosTable {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3975
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3976
        private final Map<JCTree, Integer> endPosMap;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3977
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3978
        SimpleEndPosTable() {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3979
            endPosMap = new HashMap<JCTree, Integer>();
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3980
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3981
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3982
        protected void storeEnd(JCTree tree, int endpos) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3983
            endPosMap.put(tree, errorEndPos > endpos ? errorEndPos : endpos);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3984
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3985
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3986
        protected <T extends JCTree> T to(T t) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3987
            storeEnd(t, token.endPos);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3988
            return t;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3989
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3990
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3991
        protected <T extends JCTree> T toP(T t) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3992
            storeEnd(t, S.prevToken().endPos);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3993
            return t;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3994
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3995
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3996
        public int getEndPos(JCTree tree) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3997
            Integer value = endPosMap.get(tree);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3998
            return (value == null) ? Position.NOPOS : value;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3999
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4000
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4001
        public int replaceTree(JCTree oldTree, JCTree newTree) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4002
            Integer pos = endPosMap.remove(oldTree);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4003
            if (pos != null) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4004
                endPosMap.put(newTree, pos);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4005
                return pos;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4006
            }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4007
            return Position.NOPOS;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4008
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4009
    }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4010
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4011
    /*
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4012
     * a default skeletal implementation without any mapping overhead.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4013
     */
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4014
    protected class EmptyEndPosTable extends AbstractEndPosTable {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4015
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4016
        protected void storeEnd(JCTree tree, int endpos) { /* empty */ }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4017
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4018
        protected <T extends JCTree> T to(T t) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4019
            return t;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4020
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4021
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4022
        protected <T extends JCTree> T toP(T t) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4023
            return t;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4024
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4025
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4026
        public int getEndPos(JCTree tree) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4027
            return Position.NOPOS;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4028
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4029
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4030
        public int replaceTree(JCTree oldTree, JCTree newTree) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4031
            return Position.NOPOS;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4032
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4033
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4034
    }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4035
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4036
    protected abstract class AbstractEndPosTable implements EndPosTable {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4037
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4038
        /**
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4039
         * Store the last error position.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4040
         */
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4041
        protected int errorEndPos;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4042
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4043
        /**
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4044
         * Store ending position for a tree, the value of which is the greater
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4045
         * of last error position and the given ending position.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4046
         * @param tree   The tree.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4047
         * @param endpos The ending position to associate with the tree.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4048
         */
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4049
        protected abstract void storeEnd(JCTree tree, int endpos);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4050
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4051
        /**
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4052
         * Store current token's ending position for a tree, the value of which
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4053
         * will be the greater of last error position and the ending position of
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4054
         * the current token.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4055
         * @param t The tree.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4056
         */
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4057
        protected abstract <T extends JCTree> T to(T t);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4058
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4059
        /**
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4060
         * Store current token's ending position for a tree, the value of which
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4061
         * will be the greater of last error position and the ending position of
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4062
         * the previous token.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4063
         * @param t The tree.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4064
         */
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4065
        protected abstract <T extends JCTree> T toP(T t);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4066
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4067
        /**
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4068
         * Set the error position during the parsing phases, the value of which
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4069
         * will be set only if it is greater than the last stored error position.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4070
         * @param errPos The error position
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4071
         */
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4072
        protected void setErrorEndPos(int errPos) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4073
            if (errPos > errorEndPos) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4074
                errorEndPos = errPos;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4075
            }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4076
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4077
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4078
}