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