src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java
author jlahoda
Wed, 27 Nov 2019 09:00:01 +0100
changeset 59285 7799a51dbe30
parent 59021 cfc7bb9a5a92
permissions -rw-r--r--
8231826: Implement javac changes for pattern matching for instanceof Reviewed-by: mcimadamore Contributed-by: brian.goetz@oracle.com, gavin.bierman@oracle.com, maurizio.cimadamore@oracle.com, srikanth.adayapalam@oracle.com, vicente.romero@oracle.com, jan.lahoda@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
     2
 * Copyright (c) 1999, 2019, 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.*;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
    29
import java.util.function.Function;
32453
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
    30
import java.util.stream.Collectors;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 57724
diff changeset
    32
import com.sun.source.tree.CaseTree;
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
    33
import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
    34
import com.sun.source.tree.ModuleTree.ModuleKind;
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
    35
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
    36
import com.sun.tools.javac.code.*;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
    37
import com.sun.tools.javac.code.Source.Feature;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
    38
import com.sun.tools.javac.parser.Tokens.*;
10948
063463f6535f 7104201: Refactor DocCommentScanner
mcimadamore
parents: 10815
diff changeset
    39
import com.sun.tools.javac.parser.Tokens.Comment.CommentStyle;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
    40
import com.sun.tools.javac.resources.CompilerProperties.Errors;
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
    41
import com.sun.tools.javac.resources.CompilerProperties.Fragments;
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
    42
import com.sun.tools.javac.resources.CompilerProperties.Warnings;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
import com.sun.tools.javac.tree.*;
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
    44
import com.sun.tools.javac.tree.JCTree.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.util.*;
7076
c96fa26247c8 6994946: option to specify only syntax errors as unrecoverable
jjg
parents: 7074
diff changeset
    46
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
    47
import com.sun.tools.javac.util.JCDiagnostic.Error;
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
    48
import com.sun.tools.javac.util.JCDiagnostic.Fragment;
48920
916690b5edc9 8194892: add compiler support for local-variable syntax for lambda parameters
vromero
parents: 48721
diff changeset
    49
import com.sun.tools.javac.util.List;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
10950
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.*;
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.ASSERT;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
    53
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
    54
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
    55
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
    56
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
    57
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
    58
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
    59
import static com.sun.tools.javac.tree.JCTree.Tag.*;
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
    60
import static com.sun.tools.javac.resources.CompilerProperties.Fragments.ImplicitAndExplicitNotAllowed;
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
    61
import static com.sun.tools.javac.resources.CompilerProperties.Fragments.VarAndExplicitNotAllowed;
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
    62
import static com.sun.tools.javac.resources.CompilerProperties.Fragments.VarAndImplicitNotAllowed;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
/** The parser maps a token sequence into an abstract syntax
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
 *  tree. It operates by recursive descent, with code derived
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 *  systematically from an LL(1) grammar. For efficiency reasons, an
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
 *  operator precedence scheme is used for parsing binary operation
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
 *  expressions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    70
 *  <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
    71
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
 */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
    75
public class JavacParser implements Parser {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    /** The number of precedence levels of infix operators.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    private static final int infixPrecedenceLevels = 10;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
43584
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
    81
    /** Is the parser instantiated to parse a module-info file ?
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
    82
     */
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
    83
    private final boolean parseModuleInfo;
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
    84
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /** The scanner used for lexical analysis.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     */
10200
56cc93e7b6ef 7068902: (javac) allow enabling or disabling of String folding
ksrini
parents: 10199
diff changeset
    87
    protected Lexer S;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    /** The factory to be used for abstract syntax tree construction.
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    protected TreeMaker F;
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    /** The log to be used for error diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    private Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    /** The Source language setting. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    private Source source;
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
49580
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
   100
    /** The Preview language setting. */
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
   101
    private Preview preview;
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
   102
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    /** The name table. */
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1258
diff changeset
   104
    private Names names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   106
    /** End position mappings container */
28587
ce5606145ea3 8068488: Facilitate extension of the javac parser -- missing modifier
rfield
parents: 28454
diff changeset
   107
    protected final AbstractEndPosTable endPosTable;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   108
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   109
    // Because of javac's limited lookahead, some contexts are ambiguous in
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   110
    // the presence of type annotations even though they are not ambiguous
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   111
    // in the absence of type annotations.  Consider this code:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   112
    //   void m(String [] m) { }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   113
    //   void m(String ... m) { }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   114
    // After parsing "String", javac calls bracketsOpt which immediately
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   115
    // returns if the next character is not '['.  Similarly, javac can see
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   116
    // 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
   117
    // the presence of type annotations:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   118
    //   void m(String @A [] m) { }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   119
    //   void m(String @A ... m) { }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   120
    // no finite lookahead is enough to determine whether to read array
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   121
    // levels or an ellipsis.  Furthermore, if you call bracketsOpt, then
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   122
    // bracketsOpt first reads all the leading annotations and only then
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   123
    // 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
   124
    // back the extra annotations that it read.  (But, bracketsOpt should
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   125
    // 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
   126
    // -- in most contexts, any such extra annotation is an error.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   127
    //
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   128
    // The following two variables permit type annotations that have
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   129
    // already been read to be stored for later use.  Alternate
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   130
    // implementations are possible but would cause much larger changes to
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   131
    // the parser.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   132
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   133
    /** 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
   134
    private List<JCAnnotation> typeAnnotationsPushedBack = List.nil();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   135
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   136
    /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   137
     * If the parser notices extra annotations, then it either immediately
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   138
     * 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
   139
     * annotations in variable typeAnnotationsPushedBack (if this variable
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   140
     * is true).
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   141
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   142
    private boolean permitTypeAnnotationsPushBack = false;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   143
14450
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   144
    interface ErrorRecoveryAction {
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   145
        JCTree doRecover(JavacParser parser);
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   146
    }
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   147
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   148
    enum BasicErrorRecoveryAction implements ErrorRecoveryAction {
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   149
        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
   150
        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
   151
    }
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
   152
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    /** Construct a parser from a given scanner, tree factory and log.
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
     */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   155
    protected JavacParser(ParserFactory fac,
43584
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   156
                          Lexer S,
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   157
                          boolean keepDocComments,
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   158
                          boolean keepLineMap,
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   159
                          boolean keepEndPositions) {
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   160
        this(fac, S, keepDocComments, keepLineMap, keepEndPositions, false);
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   161
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   162
    }
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   163
    /** Construct a parser from a given scanner, tree factory and log.
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   164
     */
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   165
    protected JavacParser(ParserFactory fac,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                     Lexer S,
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   167
                     boolean keepDocComments,
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   168
                     boolean keepLineMap,
43584
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   169
                     boolean keepEndPositions,
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   170
                     boolean parseModuleInfo) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        this.S = S;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   172
        nextToken(); // prime the pump
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        this.F = fac.F;
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        this.log = fac.log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        this.names = fac.names;
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        this.source = fac.source;
49580
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
   177
        this.preview = fac.preview;
10200
56cc93e7b6ef 7068902: (javac) allow enabling or disabling of String folding
ksrini
parents: 10199
diff changeset
   178
        this.allowStringFolding = fac.options.getBoolean("allowStringFolding", true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        this.keepDocComments = keepDocComments;
43584
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
   180
        this.parseModuleInfo = parseModuleInfo;
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   181
        docComments = newDocCommentTable(keepDocComments, fac);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   182
        this.keepLineMap = keepLineMap;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        this.errorTree = F.Erroneous();
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   184
        endPosTable = newEndPosTable(keepEndPositions);
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
   185
        this.allowYieldStatement = (!preview.isPreview(Feature.SWITCH_EXPRESSION) || preview.isEnabled()) &&
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
   186
                Feature.SWITCH_EXPRESSION.allowedInSource(source);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   189
    protected AbstractEndPosTable newEndPosTable(boolean keepEndPositions) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   190
        return  keepEndPositions
16811
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
   191
                ? new SimpleEndPosTable(this)
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
   192
                : new EmptyEndPosTable(this);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   193
    }
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   194
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   195
    protected DocCommentTable newDocCommentTable(boolean keepDocComments, ParserFactory fac) {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   196
        return keepDocComments ? new LazyDocCommentTable(fac) : null;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   197
    }
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
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
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    /** Switch: should we keep docComments?
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    boolean keepDocComments;
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   207
    /** Switch: should we keep line table?
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   208
     */
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   209
    boolean keepLineMap;
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   210
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   211
    /** Switch: is "this" allowed as an identifier?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   212
     * This is needed to parse receiver types.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   213
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   214
    boolean allowThisIdent;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   215
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
   216
    /** Switch: is yield statement allowed in this source level?
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
   217
     */
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
   218
    boolean allowYieldStatement;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
   219
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   220
    /** 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
   221
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   222
    JCVariableDecl receiverParam;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   223
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
    /** When terms are parsed, the mode determines which is expected:
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
     *     mode = EXPR        : an expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
     *     mode = TYPE        : a type
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
     *     mode = NOPARAMS    : no parameters allowed for type
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     *     mode = TYPEARG     : type argument
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   229
     *     mode |= NOLAMBDA   : lambdas are not allowed
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
     */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   231
    protected static final int EXPR = 0x1;
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   232
    protected static final int TYPE = 0x2;
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   233
    protected static final int NOPARAMS = 0x4;
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   234
    protected static final int TYPEARG = 0x8;
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   235
    protected static final int DIAMOND = 0x10;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   236
    protected static final int NOLAMBDA = 0x20;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   237
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   238
    protected void selectExprMode() {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   239
        mode = (mode & NOLAMBDA) | EXPR;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   240
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   241
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   242
    protected void selectTypeMode() {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   243
        mode = (mode & NOLAMBDA) | TYPE;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   244
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    /** The current mode.
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   248
    protected int mode = 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    /** The mode of the term that was parsed last.
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
     */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   252
    protected int lastmode = 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   254
    /* ---------- token management -------------- */
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   255
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   256
    protected Token token;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   257
14541
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   258
    public Token token() {
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   259
        return token;
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   260
    }
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   261
36f9d11fc9aa 7021614: extend com.sun.source API to support parsing javadoc comments
jjg
parents: 14450
diff changeset
   262
    public void nextToken() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   263
        S.nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   264
        token = S.token();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   265
    }
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   266
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   267
    protected boolean peekToken(Filter<TokenKind> tk) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   268
        return peekToken(0, tk);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   269
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   270
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   271
    protected boolean peekToken(int lookahead, Filter<TokenKind> tk) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   272
        return tk.accepts(S.token(lookahead + 1).kind);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   273
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   274
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   275
    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
   276
        return peekToken(0, tk1, tk2);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   277
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   278
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   279
    protected boolean peekToken(int lookahead, Filter<TokenKind> tk1, Filter<TokenKind> tk2) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   280
        return tk1.accepts(S.token(lookahead + 1).kind) &&
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   281
                tk2.accepts(S.token(lookahead + 2).kind);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   282
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   283
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   284
    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
   285
        return peekToken(0, tk1, tk2, tk3);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   286
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   287
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   288
    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
   289
        return tk1.accepts(S.token(lookahead + 1).kind) &&
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   290
                tk2.accepts(S.token(lookahead + 2).kind) &&
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   291
                tk3.accepts(S.token(lookahead + 3).kind);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   292
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   293
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   294
    @SuppressWarnings("unchecked")
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   295
    protected boolean peekToken(Filter<TokenKind>... kinds) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   296
        return peekToken(0, kinds);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   297
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   298
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   299
    @SuppressWarnings("unchecked")
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   300
    protected boolean peekToken(int lookahead, Filter<TokenKind>... kinds) {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
   301
        for (; lookahead < kinds.length ; lookahead++) {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   302
            if (!kinds[lookahead].accepts(S.token(lookahead + 1).kind)) {
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   303
                return false;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   304
            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   305
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   306
        return true;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   307
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
   308
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   309
    /* ---------- error recovery -------------- */
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    private JCErroneous errorTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    /** Skip forward until a suitable stop token is found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
     */
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
   315
    protected void skip(boolean stopAtImport, boolean stopAtMemberDecl, boolean stopAtIdentifier, boolean stopAtStatement) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
         while (true) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   317
             switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
                case SEMI:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   319
                    nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
                case PUBLIC:
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
                case FINAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
                case ABSTRACT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
                case MONKEYS_AT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
                case EOF:
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
                case CLASS:
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
                case INTERFACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
                case ENUM:
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
                    return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
                case IMPORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
                    if (stopAtImport)
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
                case LBRACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
                case RBRACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
                case PRIVATE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
                case PROTECTED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
                case STATIC:
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                case TRANSIENT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                case NATIVE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                case VOLATILE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
                case SYNCHRONIZED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                case STRICTFP:
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                case LT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                case BYTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                case SHORT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                case CHAR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
                case INT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
                case LONG:
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
                case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
                case DOUBLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
                case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
                    if (stopAtMemberDecl)
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
                    break;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   357
                case UNDERSCORE:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                case IDENTIFIER:
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                   if (stopAtIdentifier)
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
                    break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
                case CASE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
                case DEFAULT:
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
                case IF:
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
                case FOR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
                case WHILE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
                case DO:
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
                case TRY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
                case SWITCH:
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
                case RETURN:
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
                case THROW:
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
                case BREAK:
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
                case CONTINUE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
                case ELSE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
                case FINALLY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
                case CATCH:
25274
a06a738fc6be 8041648: do while loop that misses ending semicolon has wrong end position
jlahoda
parents: 24069
diff changeset
   377
                case THIS:
a06a738fc6be 8041648: do while loop that misses ending semicolon has wrong end position
jlahoda
parents: 24069
diff changeset
   378
                case SUPER:
a06a738fc6be 8041648: do while loop that misses ending semicolon has wrong end position
jlahoda
parents: 24069
diff changeset
   379
                case NEW:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
                    if (stopAtStatement)
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                        return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
                    break;
25274
a06a738fc6be 8041648: do while loop that misses ending semicolon has wrong end position
jlahoda
parents: 24069
diff changeset
   383
                case ASSERT:
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
   384
                    if (stopAtStatement)
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
   385
                        return;
25274
a06a738fc6be 8041648: do while loop that misses ending semicolon has wrong end position
jlahoda
parents: 24069
diff changeset
   386
                    break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   388
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   392
    protected JCErroneous syntaxError(int pos, Error errorKey) {
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   393
        return syntaxError(pos, List.nil(), errorKey);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   396
    protected JCErroneous syntaxError(int pos, List<JCTree> errs, Error errorKey) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
        setErrorEndPos(pos);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   398
        JCErroneous err = F.at(pos).Erroneous(errs);
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   399
        reportSyntaxError(err, errorKey);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   400
        if (errs != null) {
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   401
            JCTree last = errs.last();
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   402
            if (last != null)
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   403
                storeEnd(last, pos);
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   404
        }
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   405
        return toP(err);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
23133
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
   408
    private static final int RECOVERY_THRESHOLD = 50;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
    private int errorPos = Position.NOPOS;
23133
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
   410
    private int count = 0;
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   411
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
    /**
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   413
     * 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
   414
     * unless one was already reported at the same position.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
     */
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   416
    protected void reportSyntaxError(int pos, Error errorKey) {
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   417
        JCDiagnostic.DiagnosticPosition diag = new JCDiagnostic.SimpleDiagnosticPosition(pos);
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   418
        reportSyntaxError(diag, errorKey);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   419
    }
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   420
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   421
    /**
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   422
     * 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
   423
     * 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
   424
     */
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   425
    protected void reportSyntaxError(JCDiagnostic.DiagnosticPosition diagPos, Error errorKey) {
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   426
        int pos = diagPos.getPreferredPosition();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
        if (pos > S.errPos() || pos == Position.NOPOS) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   428
            if (token.kind == EOF) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   429
                log.error(DiagnosticFlag.SYNTAX, diagPos, Errors.PrematureEof);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   430
            } else {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   431
                log.error(DiagnosticFlag.SYNTAX, diagPos, errorKey);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   432
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        S.errPos(pos);
23133
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
   435
        if (token.pos == errorPos) {
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
   436
            //check for a possible infinite loop in parsing:
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
   437
            Assert.check(count++ < RECOVERY_THRESHOLD);
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
   438
        } else {
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
   439
            count = 0;
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
   440
            errorPos = token.pos;
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
   441
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
    /** If next input token matches given token, skip it, otherwise report
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
     *  an error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
     */
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   447
    public void accept(TokenKind tk) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   448
        accept(tk, Errors::Expected);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   449
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   450
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   451
    /** If next input token matches given token, skip it, otherwise report
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   452
     *  an error.
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   453
     */
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   454
    public void accept(TokenKind tk, Function<TokenKind, Error> errorProvider) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   455
        if (token.kind == tk) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   456
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   458
            setErrorEndPos(token.pos);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   459
            reportSyntaxError(S.prevToken().endPos, errorProvider.apply(tk));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   460
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
    /** Report an illegal start of expression/type error at given position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
    JCExpression illegal(int pos) {
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   466
        setErrorEndPos(pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
        if ((mode & EXPR) != 0)
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   468
            return syntaxError(pos, Errors.IllegalStartOfExpr);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
        else
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   470
            return syntaxError(pos, Errors.IllegalStartOfType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
    /** Report an illegal start of expression/type error at current position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
    JCExpression illegal() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   477
        return illegal(token.pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
    /** Diagnose a modifier flag from the set, if any. */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   481
    protected void checkNoMods(long mods) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
        if (mods != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
            long lowestMod = mods & -mods;
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   484
            log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.ModNotAllowedHere(Flags.asFlagSet(lowestMod)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
/* ---------- doc comments --------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   490
    /** A table to store all documentation comments
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
     *  indexed by the tree nodes they refer to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
     *  defined only if option flag keepDocComment is set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   494
    private final DocCommentTable docComments;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
    /** Make an entry into docComments hashtable,
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
     *  provided flag keepDocComments is set and given doc comment is non-null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
     *  @param tree   The tree to be used as index in the hashtable
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
     *  @param dc     The doc comment to associate with the tree, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
     */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   501
    protected void attach(JCTree tree, Comment dc) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
        if (keepDocComments && dc != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
//          System.out.println("doc comment = ");System.out.println(dc);//DEBUG
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
   504
            docComments.putComment(tree, dc);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
/* -------- source positions ------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   510
    protected void setErrorEndPos(int errPos) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   511
        endPosTable.setErrorEndPos(errPos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   514
    protected void storeEnd(JCTree tree, int endpos) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   515
        endPosTable.storeEnd(tree, endpos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   518
    protected <T extends JCTree> T to(T t) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   519
        return endPosTable.to(t);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   520
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   522
    protected <T extends JCTree> T toP(T t) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   523
        return endPosTable.toP(t);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   524
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
    /** Get the start position for a tree node.  The start position is
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
     * defined to be the position of the first character of the first
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
     * token of the node's source text.
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
     * @param tree  The tree node
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
    public int getStartPos(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
        return TreeInfo.getStartPos(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
     * Get the end position for a tree node.  The end position is
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
     * defined to be the position of the last character of the last
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
     * token of the node's source text.  Returns Position.NOPOS if end
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
     * positions are not generated or the position is otherwise not
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
     * found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
     * @param tree  The tree node
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
    public int getEndPos(JCTree tree) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   544
        return endPosTable.getEndPos(tree);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
/* ---------- parsing -------------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
     * Ident = IDENTIFIER
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
     */
43261
d377e97291d8 8138725: Add options for Javadoc generation
jjg
parents: 43030
diff changeset
   554
    public Name ident() {
31939
89d4a8756f98 8081769: Redundant error message on bad usage of 'class' literal
mcimadamore
parents: 29293
diff changeset
   555
        return ident(false);
89d4a8756f98 8081769: Redundant error message on bad usage of 'class' literal
mcimadamore
parents: 29293
diff changeset
   556
    }
89d4a8756f98 8081769: Redundant error message on bad usage of 'class' literal
mcimadamore
parents: 29293
diff changeset
   557
89d4a8756f98 8081769: Redundant error message on bad usage of 'class' literal
mcimadamore
parents: 29293
diff changeset
   558
    protected Name ident(boolean advanceOnErrors) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   559
        if (token.kind == IDENTIFIER) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   560
            Name name = token.name();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   561
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
            return name;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   563
        } else if (token.kind == ASSERT) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   564
            log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.AssertAsIdentifier);
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
   565
            nextToken();
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
   566
            return names.error;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   567
        } else if (token.kind == ENUM) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   568
            log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.EnumAsIdentifier);
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
   569
            nextToken();
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
   570
            return names.error;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   571
        } else if (token.kind == THIS) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   572
            if (allowThisIdent) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   573
                // Make sure we're using a supported source version.
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
   574
                checkSourceLevel(Feature.TYPE_ANNOTATIONS);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   575
                Name name = token.name();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   576
                nextToken();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   577
                return name;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   578
            } else {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   579
                log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.ThisAsIdentifier);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   580
                nextToken();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   581
                return names.error;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   582
            }
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   583
        } else if (token.kind == UNDERSCORE) {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
   584
            if (Feature.UNDERSCORE_IDENTIFIER.allowedInSource(source)) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   585
                log.warning(token.pos, Warnings.UnderscoreAsIdentifier);
27991
8f4b68523da3 8061549: Disallow _ as a one-character identifier
jlahoda
parents: 27989
diff changeset
   586
            } else {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   587
                log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.UnderscoreAsIdentifier);
27991
8f4b68523da3 8061549: Disallow _ as a one-character identifier
jlahoda
parents: 27989
diff changeset
   588
            }
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   589
            Name name = token.name();
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   590
            nextToken();
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   591
            return name;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
            accept(IDENTIFIER);
31939
89d4a8756f98 8081769: Redundant error message on bad usage of 'class' literal
mcimadamore
parents: 29293
diff changeset
   594
            if (advanceOnErrors) {
89d4a8756f98 8081769: Redundant error message on bad usage of 'class' literal
mcimadamore
parents: 29293
diff changeset
   595
                nextToken();
89d4a8756f98 8081769: Redundant error message on bad usage of 'class' literal
mcimadamore
parents: 29293
diff changeset
   596
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
            return names.error;
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
        }
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
   599
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
    /**
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   602
     * Qualident = Ident { DOT [Annotations] Ident }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   604
    public JCExpression qualident(boolean allowAnnos) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   605
        JCExpression t = toP(F.at(token.pos).Ident(ident()));
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   606
        while (token.kind == DOT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   607
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   608
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   609
            List<JCAnnotation> tyannos = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   610
            if (allowAnnos) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   611
                tyannos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   612
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
            t = toP(F.at(pos).Select(t, ident()));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   614
            if (tyannos != null && tyannos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   615
                t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   616
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   621
    JCExpression literal(Name prefix) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   622
        return literal(prefix, token.pos);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   623
    }
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   624
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
     * Literal =
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
     *     INTLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
     *   | LONGLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
     *   | FLOATLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
     *   | DOUBLELITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
     *   | CHARLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
     *   | STRINGLITERAL
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
     *   | TRUE
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
     *   | FALSE
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
     *   | NULL
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
     */
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
   637
    JCExpression literal(Name prefix, int pos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
        JCExpression t = errorTree;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   639
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
        case INTLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
                t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   643
                    TypeTag.INT,
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   644
                    Convert.string2int(strval(prefix), token.radix()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
            } catch (NumberFormatException ex) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   646
                log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.IntNumberTooLarge(strval(prefix)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
        case LONGLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
                t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   652
                    TypeTag.LONG,
37635
9834489579e5 8145468: update java.lang APIs with new deprecations
smarks
parents: 36526
diff changeset
   653
                    Long.valueOf(Convert.string2long(strval(prefix), token.radix())));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
            } catch (NumberFormatException ex) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   655
                log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.IntNumberTooLarge(strval(prefix)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
        case FLOATLITERAL: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   659
            String proper = token.radix() == 16 ?
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   660
                    ("0x"+ token.stringVal()) :
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   661
                    token.stringVal();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
            Float n;
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
                n = Float.valueOf(proper);
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
            } catch (NumberFormatException ex) {
7636
030f141aa32b 6504896: TreeMaker.Literal(Object) does not support Booleans
jjg
parents: 7211
diff changeset
   666
                // error already reported in scanner
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
                n = Float.NaN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
            if (n.floatValue() == 0.0f && !isZero(proper))
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   670
                log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.FpNumberTooSmall);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
            else if (n.floatValue() == Float.POSITIVE_INFINITY)
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   672
                log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.FpNumberTooLarge);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
            else
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   674
                t = F.at(pos).Literal(TypeTag.FLOAT, n);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
        case DOUBLELITERAL: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   678
            String proper = token.radix() == 16 ?
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   679
                    ("0x"+ token.stringVal()) :
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   680
                    token.stringVal();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
            Double n;
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
            try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
                n = Double.valueOf(proper);
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
            } catch (NumberFormatException ex) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
                // error already reported in scanner
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
                n = Double.NaN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
            if (n.doubleValue() == 0.0d && !isZero(proper))
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   689
                log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.FpNumberTooSmall);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
            else if (n.doubleValue() == Double.POSITIVE_INFINITY)
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
   691
                log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.FpNumberTooLarge);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
            else
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   693
                t = F.at(pos).Literal(TypeTag.DOUBLE, n);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
        case CHARLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
            t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   698
                TypeTag.CHAR,
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   699
                token.stringVal().charAt(0) + 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
        case STRINGLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
            t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   703
                TypeTag.CLASS,
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   704
                token.stringVal());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
        case TRUE: case FALSE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
            t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   708
                TypeTag.BOOLEAN,
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   709
                (token.kind == TRUE ? 1 : 0));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
        case NULL:
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
            t = F.at(pos).Literal(
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   713
                TypeTag.BOT,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
                null);
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
        default:
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7636
diff changeset
   717
            Assert.error();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
        if (t == errorTree)
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
            t = F.at(pos).Erroneous();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   721
        storeEnd(t, token.endPos);
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   722
        nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   725
    //where
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
        boolean isZero(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
            char[] cs = s.toCharArray();
3894
e470a29ed0a2 6882235: invalid exponent causes silent javac crash
jjg
parents: 3765
diff changeset
   728
            int base = ((cs.length > 1 && Character.toLowerCase(cs[1]) == 'x') ? 16 : 10);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
            int i = ((base==16) ? 2 : 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
            while (i < cs.length && (cs[i] == '0' || cs[i] == '.')) i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
            return !(i < cs.length && (Character.digit(cs[i], base) > 0));
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
        String strval(Name prefix) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   735
            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
   736
            return prefix.isEmpty() ? s : prefix + s;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
    /** terms can be either expressions or types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
     */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   741
    public JCExpression parseExpression() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
        return term(EXPR);
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   745
    /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   746
     * parses (optional) type annotations followed by a type. If the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   747
     * 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
   748
     * parsing, this method returns a {@link JCAnnotatedType} consisting of
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   749
     * these annotations and the underlying type. Otherwise, it returns the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   750
     * underlying type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   751
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   752
     * <p>
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   753
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   754
     * 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
   755
     * parsing annotations.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   756
     */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
   757
    public JCExpression parseType() {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   758
        return parseType(false);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   759
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   760
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   761
    public JCExpression parseType(boolean allowVar) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   762
        List<JCAnnotation> annotations = typeAnnotationsOpt();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   763
        return parseType(allowVar, annotations);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   764
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   765
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   766
    public JCExpression parseType(boolean allowVar, List<JCAnnotation> annotations) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   767
        JCExpression result = unannotatedType(allowVar);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   768
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   769
        if (annotations.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   770
            result = insertAnnotationsToMostInner(result, annotations, false);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   771
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   772
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   773
        return result;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   774
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   775
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   776
    public JCExpression unannotatedType(boolean allowVar) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   777
        JCExpression result = term(TYPE);
55561
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
   778
        Name restrictedTypeName = restrictedTypeName(result, !allowVar);
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
   779
4c0a7916d3cd 8226522: No compilation error reported when yield is used in incorrect context
jlahoda
parents: 55306
diff changeset
   780
        if (restrictedTypeName != null && (!allowVar || restrictedTypeName != names.var)) {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
   781
            syntaxError(result.pos, Errors.RestrictedTypeNotAllowedHere(restrictedTypeName));
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   782
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   783
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   784
        return result;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   787
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
   788
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
   789
    protected JCExpression term(int newmode) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   790
        int prevmode = mode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   791
        mode = newmode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
        JCExpression t = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
        lastmode = mode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   794
        mode = prevmode;
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
    /**
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
   799
     *  {@literal
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
     *  Expression = Expression1 [ExpressionRest]
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
     *  ExpressionRest = [AssignmentOperator Expression1]
06bc494ca11e Initial load
duke
parents:
diff changeset
   802
     *  AssignmentOperator = "=" | "+=" | "-=" | "*=" | "/=" |
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
     *                       "&=" | "|=" | "^=" |
06bc494ca11e Initial load
duke
parents:
diff changeset
   804
     *                       "%=" | "<<=" | ">>=" | ">>>="
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
     *  Type = Type1
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
     *  TypeNoParams = TypeNoParams1
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
     *  StatementExpression = Expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   808
     *  ConstantExpression = Expression
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
   809
     *  }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   810
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
    JCExpression term() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   812
        JCExpression t = term1();
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
        if ((mode & EXPR) != 0 &&
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   814
            token.kind == EQ || PLUSEQ.compareTo(token.kind) <= 0 && token.kind.compareTo(GTGTGTEQ) <= 0)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
            return termRest(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   816
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   818
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
    JCExpression termRest(JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   821
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
        case EQ: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   823
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   824
            nextToken();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   825
            selectExprMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
            JCExpression t1 = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
            return toP(F.at(pos).Assign(t, t1));
06bc494ca11e Initial load
duke
parents:
diff changeset
   828
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
        case PLUSEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
        case SUBEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
        case STAREQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   832
        case SLASHEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
        case PERCENTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   834
        case AMPEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
        case BAREQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
        case CARETEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
        case LTLTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   838
        case GTGTEQ:
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
        case GTGTGTEQ:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   840
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   841
            TokenKind tk = token.kind;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   842
            nextToken();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   843
            selectExprMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
            JCExpression t1 = term();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   845
            return F.at(pos).Assignop(optag(tk), t, t1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   846
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   848
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   850
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
    /** Expression1   = Expression2 [Expression1Rest]
06bc494ca11e Initial load
duke
parents:
diff changeset
   852
     *  Type1         = Type2
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
     *  TypeNoParams1 = TypeNoParams2
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
    JCExpression term1() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
        JCExpression t = term2();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   857
        if ((mode & EXPR) != 0 && token.kind == QUES) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   858
            selectExprMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
            return term1Rest(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   862
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
    /** Expression1Rest = ["?" Expression ":" Expression1]
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
    JCExpression term1Rest(JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   868
        if (token.kind == QUES) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   869
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   870
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
            JCExpression t1 = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
            accept(COLON);
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
            JCExpression t2 = term1();
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
            return F.at(pos).Conditional(t, t1, t2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
    /** Expression2   = Expression3 [Expression2Rest]
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
     *  Type2         = Type3
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
     *  TypeNoParams2 = TypeNoParams3
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
    JCExpression term2() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
        JCExpression t = term3();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   886
        if ((mode & EXPR) != 0 && prec(token.kind) >= TreeInfo.orPrec) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
   887
            selectExprMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   888
            return term2Rest(t, TreeInfo.orPrec);
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   890
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   892
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
    /*  Expression2Rest = {infixop Expression3}
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
     *                  | Expression3 instanceof Type
59285
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   896
     *                  | Expression3 instanceof Pattern
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
     *  infixop         = "||"
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
     *                  | "&&"
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
     *                  | "|"
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
     *                  | "<" | ">" | "<=" | ">="
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
     *                  | "<<" | ">>" | ">>>"
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
     *                  | "+" | "-"
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
     *                  | "*" | "/" | "%"
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
    JCExpression term2Rest(JCExpression t, int minprec) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
        JCExpression[] odStack = newOdStack();
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
        Token[] opStack = newOpStack();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   911
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   912
        // optimization, was odStack = new Tree[...]; opStack = new Tree[...];
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
        int top = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
        odStack[0] = t;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   915
        int startPos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   916
        Token topOp = Tokens.DUMMY;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   917
        while (prec(token.kind) >= minprec) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
            opStack[top] = topOp;
59285
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   919
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   920
            if (token.kind == INSTANCEOF) {
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   921
                int pos = token.pos;
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   922
                nextToken();
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   923
                JCTree pattern = parseType();
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   924
                if (token.kind == IDENTIFIER) {
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   925
                    checkSourceLevel(token.pos, Feature.PATTERN_MATCHING_IN_INSTANCEOF);
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   926
                    pattern = toP(F.at(token.pos).BindingPattern(ident(), pattern));
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   927
                }
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   928
                odStack[top] = F.at(pos).TypeTest(odStack[top], pattern);
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   929
            } else {
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   930
                topOp = token;
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   931
                nextToken();
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   932
                top++;
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   933
                odStack[top] = term3();
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   934
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
   935
            while (top > 0 && prec(topOp.kind) >= prec(token.kind)) {
59285
7799a51dbe30 8231826: Implement javac changes for pattern matching for instanceof
jlahoda
parents: 59021
diff changeset
   936
                odStack[top - 1] = F.at(topOp.pos).Binary(optag(topOp.kind), odStack[top - 1], odStack[top]);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
                top--;
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
                topOp = opStack[top];
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   940
        }
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 7636
diff changeset
   941
        Assert.check(top == 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   942
        t = odStack[0];
06bc494ca11e Initial load
duke
parents:
diff changeset
   943
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
   944
        if (t.hasTag(JCTree.Tag.PLUS)) {
32453
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   945
            t = foldStrings(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   946
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   947
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
   948
        odStackSupply.add(odStack);
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
   949
        opStackSupply.add(opStack);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   950
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   951
    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
   952
    //where
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   953
        /** If tree is a concatenation of string literals, replace it
06bc494ca11e Initial load
duke
parents:
diff changeset
   954
         *  by a single literal representing the concatenated string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   955
         */
32453
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   956
        protected JCExpression foldStrings(JCExpression tree) {
10200
56cc93e7b6ef 7068902: (javac) allow enabling or disabling of String folding
ksrini
parents: 10199
diff changeset
   957
            if (!allowStringFolding)
33361
1c96344ecd49 8139751: Javac crash with -XDallowStringFolding=false
jlahoda
parents: 32453
diff changeset
   958
                return tree;
32453
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   959
            ListBuffer<JCExpression> opStack = new ListBuffer<>();
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   960
            ListBuffer<JCLiteral> litBuf = new ListBuffer<>();
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   961
            boolean needsFolding = false;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   962
            JCExpression curr = tree;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   963
            while (true) {
32453
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   964
                if (curr.hasTag(JCTree.Tag.PLUS)) {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   965
                    JCBinary op = (JCBinary)curr;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   966
                    needsFolding |= foldIfNeeded(op.rhs, litBuf, opStack, false);
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   967
                    curr = op.lhs;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   968
                } else {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   969
                    needsFolding |= foldIfNeeded(curr, litBuf, opStack, true);
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   970
                    break; //last one!
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   971
                }
32453
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   972
            }
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   973
            if (needsFolding) {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   974
                List<JCExpression> ops = opStack.toList();
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   975
                JCExpression res = ops.head;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   976
                for (JCExpression op : ops.tail) {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   977
                    res = F.at(op.getStartPosition()).Binary(optag(TokenKind.PLUS), res, op);
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   978
                    storeEnd(res, getEndPos(op));
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   979
                }
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   980
                return res;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   981
            } else {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   982
                return tree;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
32453
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   986
        private boolean foldIfNeeded(JCExpression tree, ListBuffer<JCLiteral> litBuf,
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   987
                                                ListBuffer<JCExpression> opStack, boolean last) {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   988
            JCLiteral str = stringLiteral(tree);
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   989
            if (str != null) {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   990
                litBuf.prepend(str);
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   991
                return last && merge(litBuf, opStack);
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   992
            } else {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   993
                boolean res = merge(litBuf, opStack);
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   994
                litBuf.clear();
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   995
                opStack.prepend(tree);
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   996
                return res;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   997
            }
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   998
        }
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
   999
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1000
        boolean merge(ListBuffer<JCLiteral> litBuf, ListBuffer<JCExpression> opStack) {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1001
            if (litBuf.isEmpty()) {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1002
                return false;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1003
            } else if (litBuf.size() == 1) {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1004
                opStack.prepend(litBuf.first());
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1005
                return false;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1006
            } else {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1007
                JCExpression t = F.at(litBuf.first().getStartPosition()).Literal(TypeTag.CLASS,
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1008
                        litBuf.stream().map(lit -> (String)lit.getValue()).collect(Collectors.joining()));
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1009
                storeEnd(t, litBuf.last().getEndPosition(endPosTable));
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1010
                opStack.prepend(t);
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1011
                return true;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1012
            }
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1013
        }
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1014
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1015
        private JCLiteral stringLiteral(JCTree tree) {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1016
            if (tree.hasTag(LITERAL)) {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1017
                JCLiteral lit = (JCLiteral)tree;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1018
                if (lit.typetag == TypeTag.CLASS) {
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1019
                    return lit;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1020
                }
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1021
            }
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1022
            return null;
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1023
        }
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1024
8eebd1f0b8ea 8134007: Improve string folding
mcimadamore
parents: 31941
diff changeset
  1025
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1026
        /** optimization: To save allocating a new operand/operator stack
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
         *  for every binary operation, we use supplys.
06bc494ca11e Initial load
duke
parents:
diff changeset
  1028
         */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  1029
        ArrayList<JCExpression[]> odStackSupply = new ArrayList<>();
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  1030
        ArrayList<Token[]> opStackSupply = new ArrayList<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
        private JCExpression[] newOdStack() {
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1033
            if (odStackSupply.isEmpty())
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1034
                return new JCExpression[infixPrecedenceLevels + 1];
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1035
            return odStackSupply.remove(odStackSupply.size() - 1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1036
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
06bc494ca11e Initial load
duke
parents:
diff changeset
  1038
        private Token[] newOpStack() {
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1039
            if (opStackSupply.isEmpty())
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1040
                return new Token[infixPrecedenceLevels + 1];
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  1041
            return opStackSupply.remove(opStackSupply.size() - 1);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1044
    /**
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1045
     *  Expression3    = PrefixOp Expression3
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1046
     *                 | "(" Expr | TypeNoParams ")" Expression3
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
     *                 | Primary {Selector} {PostfixOp}
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1048
     *
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1049
     *  {@literal
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1050
     *  Primary        = "(" Expression ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
     *                 | Literal
06bc494ca11e Initial load
duke
parents:
diff changeset
  1052
     *                 | [TypeArguments] THIS [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
     *                 | [TypeArguments] SUPER SuperSuffix
06bc494ca11e Initial load
duke
parents:
diff changeset
  1054
     *                 | NEW [TypeArguments] Creator
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1055
     *                 | "(" Arguments ")" "->" ( Expression | Block )
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1056
     *                 | Ident "->" ( Expression | Block )
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1057
     *                 | [Annotations] Ident { "." [Annotations] Ident }
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1058
     *                 | Expression3 MemberReferenceSuffix
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1059
     *                   [ [Annotations] "[" ( "]" BracketsOpt "." CLASS | Expression "]" )
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1060
     *                   | Arguments
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
     *                   | "." ( CLASS | THIS | [TypeArguments] SUPER Arguments | NEW [TypeArguments] InnerCreator )
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
     *                   ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
     *                 | BasicType BracketsOpt "." CLASS
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1064
     *  }
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  1065
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1066
     *  PrefixOp       = "++" | "--" | "!" | "~" | "+" | "-"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
     *  PostfixOp      = "++" | "--"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1068
     *  Type3          = Ident { "." Ident } [TypeArguments] {TypeSelector} BracketsOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
     *                 | BasicType
06bc494ca11e Initial load
duke
parents:
diff changeset
  1070
     *  TypeNoParams3  = Ident { "." Ident } BracketsOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
     *  Selector       = "." [TypeArguments] Ident [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1072
     *                 | "." THIS
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
     *                 | "." [TypeArguments] SUPER SuperSuffix
06bc494ca11e Initial load
duke
parents:
diff changeset
  1074
     *                 | "." NEW [TypeArguments] InnerCreator
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
     *                 | "[" Expression "]"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1076
     *  TypeSelector   = "." Ident [TypeArguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
     *  SuperSuffix    = Arguments | "." Ident [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1078
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
    protected JCExpression term3() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1080
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
        JCExpression t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1082
        List<JCExpression> typeArgs = typeArgumentsOpt(EXPR);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1083
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1084
        case QUES:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
            if ((mode & TYPE) != 0 && (mode & (TYPEARG|NOPARAMS)) == TYPEARG) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1086
                selectTypeMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
                return typeArgument();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1088
            } else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
                return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
        case PLUSPLUS: case SUBSUB: case BANG: case TILDE: case PLUS: case SUB:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1091
            if (typeArgs == null && (mode & EXPR) != 0) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1092
                TokenKind tk = token.kind;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1093
                nextToken();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1094
                selectExprMode();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1095
                if (tk == SUB &&
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1096
                    (token.kind == INTLITERAL || token.kind == LONGLITERAL) &&
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1097
                    token.radix() == 10) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1098
                    selectExprMode();
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  1099
                    t = literal(names.hyphen, pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1100
                } else {
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1101
                    t = term3();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1102
                    return F.at(pos).Unary(unoptag(tk), t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1103
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1104
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1105
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1106
        case LPAREN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1107
            if (typeArgs == null && (mode & EXPR) != 0) {
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1108
                ParensResult pres = analyzeParens();
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1109
                switch (pres) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1110
                    case CAST:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1111
                       accept(LPAREN);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1112
                       selectTypeMode();
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1113
                       int pos1 = pos;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  1114
                       List<JCExpression> targets = List.of(t = parseType());
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1115
                       while (token.kind == AMP) {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  1116
                           checkSourceLevel(Feature.INTERSECTION_TYPES_IN_CAST);
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1117
                           accept(AMP);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  1118
                           targets = targets.prepend(parseType());
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1119
                       }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1120
                       if (targets.length() > 1) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1121
                           t = toP(F.at(pos1).TypeIntersection(targets.reverse()));
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1122
                       }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1123
                       accept(RPAREN);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1124
                       selectExprMode();
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1125
                       JCExpression t1 = term3();
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1126
                       return F.at(pos).TypeCast(t, t1);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1127
                    case IMPLICIT_LAMBDA:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1128
                    case EXPLICIT_LAMBDA:
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1129
                        t = lambdaExpressionOrStatement(true, pres == ParensResult.EXPLICIT_LAMBDA, pos);
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1130
                        break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1131
                    default: //PARENS
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1132
                        accept(LPAREN);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1133
                        selectExprMode();
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1134
                        t = termRest(term1Rest(term2Rest(term3(), TreeInfo.orPrec)));
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1135
                        accept(RPAREN);
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1136
                        t = toP(F.at(pos).Parens(t));
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1137
                        break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1138
                }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1139
            } else {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1140
                return illegal();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1141
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1142
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1143
        case THIS:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1144
            if ((mode & EXPR) != 0) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1145
                selectExprMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1146
                t = to(F.at(pos).Ident(names._this));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1147
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1148
                if (typeArgs == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
  1149
                    t = argumentsOpt(null, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1150
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
  1151
                    t = arguments(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1152
                typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1153
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1154
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1155
        case SUPER:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1156
            if ((mode & EXPR) != 0) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1157
                selectExprMode();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1158
                t = to(F.at(pos).Ident(names._super));
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  1159
                t = superSuffix(typeArgs, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1160
                typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1161
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1162
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1163
        case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: case DOUBLELITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1164
        case CHARLITERAL: case STRINGLITERAL:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1165
        case TRUE: case FALSE: case NULL:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1166
            if (typeArgs == null && (mode & EXPR) != 0) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1167
                selectExprMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1168
                t = literal(names.empty);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1169
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1170
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1171
        case NEW:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1172
            if (typeArgs != null) return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1173
            if ((mode & EXPR) != 0) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1174
                selectExprMode();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1175
                nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1176
                if (token.kind == LT) typeArgs = typeArguments(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1177
                t = creator(pos, typeArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1178
                typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1179
            } else return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1180
            break;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1181
        case MONKEYS_AT:
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1182
            // Only annotated cast types and method references are valid
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1183
            List<JCAnnotation> typeAnnos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1184
            if (typeAnnos.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1185
                // else there would be no '@'
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1186
                throw new AssertionError("Expected type annotations, but found none!");
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1187
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1188
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1189
            JCExpression expr = term3();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1190
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1191
            if ((mode & TYPE) == 0) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1192
                // Type annotations on class literals no longer legal
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1193
                switch (expr.getTag()) {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1194
                case REFERENCE: {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1195
                    JCMemberReference mref = (JCMemberReference) expr;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1196
                    mref.expr = toP(F.at(pos).AnnotatedType(typeAnnos, mref.expr));
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1197
                    t = mref;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1198
                    break;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1199
                }
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1200
                case SELECT: {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1201
                    JCFieldAccess sel = (JCFieldAccess) expr;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1202
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1203
                    if (sel.name != names._class) {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1204
                        return illegal();
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1205
                    } else {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 43584
diff changeset
  1206
                        log.error(token.pos, Errors.NoAnnotationsOnDotClass);
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1207
                        return expr;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1208
                    }
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1209
                }
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1210
                default:
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1211
                    return illegal(typeAnnos.head.pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1212
                }
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1213
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1214
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1215
                // Type annotations targeting a cast
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1216
                t = insertAnnotationsToMostInner(expr, typeAnnos, false);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1217
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1218
            break;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1219
        case UNDERSCORE: case IDENTIFIER: case ASSERT: case ENUM:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1220
            if (typeArgs != null) return illegal();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1221
            if ((mode & EXPR) != 0 && (mode & NOLAMBDA) == 0 && peekToken(ARROW)) {
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1222
                t = lambdaExpressionOrStatement(false, false, pos);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1223
            } else {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1224
                t = toP(F.at(token.pos).Ident(ident()));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1225
                loop: while (true) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1226
                    pos = token.pos;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1227
                    final List<JCAnnotation> annos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1228
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1229
                    // need to report an error later if LBRACKET is for array
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1230
                    // index access rather than array creation level
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1231
                    if (!annos.isEmpty() && token.kind != LBRACKET && token.kind != ELLIPSIS)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1232
                        return illegal(annos.head.pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1233
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1234
                    switch (token.kind) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1235
                    case LBRACKET:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1236
                        nextToken();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1237
                        if (token.kind == RBRACKET) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1238
                            nextToken();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1239
                            t = bracketsOpt(t);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1240
                            t = toP(F.at(pos).TypeArray(t));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1241
                            if (annos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1242
                                t = toP(F.at(pos).AnnotatedType(annos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1243
                            }
28454
63c31d7de8f6 8027888: javac wrongly allows annotations in array-typed class literals
jlahoda
parents: 28147
diff changeset
  1244
                            t = bracketsSuffix(t);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1245
                        } else {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1246
                            if ((mode & EXPR) != 0) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1247
                                selectExprMode();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1248
                                JCExpression t1 = term();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1249
                                if (!annos.isEmpty()) t = illegal(annos.head.pos);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1250
                                t = to(F.at(pos).Indexed(t, t1));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1251
                            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1252
                            accept(RBRACKET);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1253
                        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1254
                        break loop;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1255
                    case LPAREN:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1256
                        if ((mode & EXPR) != 0) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1257
                            selectExprMode();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1258
                            t = arguments(typeArgs, t);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1259
                            if (!annos.isEmpty()) t = illegal(annos.head.pos);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1260
                            typeArgs = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1261
                        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1262
                        break loop;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1263
                    case DOT:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1264
                        nextToken();
47936
3b1ef48862cf 8185983: Javac should reject TypeArguments on field access expression
vromero
parents: 47439
diff changeset
  1265
                        if (token.kind == TokenKind.IDENTIFIER && typeArgs != null) {
3b1ef48862cf 8185983: Javac should reject TypeArguments on field access expression
vromero
parents: 47439
diff changeset
  1266
                            return illegal();
3b1ef48862cf 8185983: Javac should reject TypeArguments on field access expression
vromero
parents: 47439
diff changeset
  1267
                        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1268
                        int oldmode = mode;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1269
                        mode &= ~NOPARAMS;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1270
                        typeArgs = typeArgumentsOpt(EXPR);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1271
                        mode = oldmode;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1272
                        if ((mode & EXPR) != 0) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1273
                            switch (token.kind) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1274
                            case CLASS:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1275
                                if (typeArgs != null) return illegal();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1276
                                selectExprMode();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1277
                                t = to(F.at(pos).Select(t, names._class));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1278
                                nextToken();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1279
                                break loop;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1280
                            case THIS:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1281
                                if (typeArgs != null) return illegal();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1282
                                selectExprMode();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1283
                                t = to(F.at(pos).Select(t, names._this));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1284
                                nextToken();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1285
                                break loop;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1286
                            case SUPER:
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1287
                                selectExprMode();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1288
                                t = to(F.at(pos).Select(t, names._super));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1289
                                t = superSuffix(typeArgs, t);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1290
                                typeArgs = null;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1291
                                break loop;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1292
                            case NEW:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1293
                                if (typeArgs != null) return illegal();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1294
                                selectExprMode();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1295
                                int pos1 = token.pos;
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1296
                                nextToken();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1297
                                if (token.kind == LT) typeArgs = typeArguments(false);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1298
                                t = innerCreator(pos1, typeArgs, t);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1299
                                typeArgs = null;
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
                            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1302
                        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1303
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1304
                        List<JCAnnotation> tyannos = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1305
                        if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1306
                            tyannos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1307
                        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1308
                        // typeArgs saved for next loop iteration.
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1309
                        t = toP(F.at(pos).Select(t, ident()));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1310
                        if (tyannos != null && tyannos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1311
                            t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1312
                        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1313
                        break;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1314
                    case ELLIPSIS:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1315
                        if (this.permitTypeAnnotationsPushBack) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1316
                            this.typeAnnotationsPushedBack = annos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1317
                        } else if (annos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1318
                            // Don't return here -- error recovery attempt
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1319
                            illegal(annos.head.pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1320
                        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1321
                        break loop;
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1322
                    case LT:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1323
                        if ((mode & TYPE) == 0 && isUnboundMemberRef()) {
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1324
                            //this is an unbound method reference whose qualifier
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1325
                            //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
  1326
                            int pos1 = token.pos;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1327
                            accept(LT);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  1328
                            ListBuffer<JCExpression> args = new ListBuffer<>();
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1329
                            args.append(typeArgument());
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1330
                            while (token.kind == COMMA) {
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1331
                                nextToken();
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1332
                                args.append(typeArgument());
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1333
                            }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1334
                            accept(GT);
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1335
                            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
  1336
                            while (token.kind == DOT) {
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1337
                                nextToken();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1338
                                selectTypeMode();
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1339
                                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
  1340
                                t = typeArgumentsOpt(t);
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1341
                            }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1342
                            t = bracketsOpt(t);
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1343
                            if (token.kind != COLCOL) {
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1344
                                //method reference expected here
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1345
                                t = illegal();
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1346
                            }
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1347
                            selectExprMode();
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1348
                            return term3Rest(t, typeArgs);
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1349
                        }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1350
                        break loop;
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1351
                    default:
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1352
                        break loop;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1353
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1354
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1355
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1356
            if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1357
            t = typeArgumentsOpt(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1358
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1359
        case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1360
        case DOUBLE: case BOOLEAN:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1361
            if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1362
            t = bracketsSuffix(bracketsOpt(basicType()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1363
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1364
        case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1365
            if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1366
            if ((mode & EXPR) != 0) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1367
                nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1368
                if (token.kind == DOT) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  1369
                    JCPrimitiveTypeTree ti = toP(F.at(pos).TypeIdent(TypeTag.VOID));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1370
                    t = bracketsSuffix(ti);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1371
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1372
                    return illegal(pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1373
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1374
            } else {
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1375
                // 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
  1376
                // 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
  1377
                // 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
  1378
                JCPrimitiveTypeTree ti = to(F.at(pos).TypeIdent(TypeTag.VOID));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1379
                nextToken();
2723
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1380
                return ti;
b659ca23d5f5 6829189: Java programming with JSR 292 needs language support
jrose
parents: 2212
diff changeset
  1381
                //return illegal();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1382
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1383
            break;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1384
        case SWITCH:
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1385
            checkSourceLevel(Feature.SWITCH_EXPRESSION);
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1386
            allowYieldStatement = true;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1387
            int switchPos = token.pos;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1388
            nextToken();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1389
            JCExpression selector = parExpression();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1390
            accept(LBRACE);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1391
            ListBuffer<JCCase> cases = new ListBuffer<>();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1392
            while (true) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1393
                pos = token.pos;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1394
                switch (token.kind) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1395
                case CASE:
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1396
                case DEFAULT:
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1397
                    cases.appendList(switchExpressionStatementGroup());
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1398
                    break;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1399
                case RBRACE: case EOF:
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1400
                    JCSwitchExpression e = to(F.at(switchPos).SwitchExpression(selector,
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1401
                                                                               cases.toList()));
52635
6938c8ef179a 8212982: Rule cases in switch expression accepted even if complete normally
jlahoda
parents: 52622
diff changeset
  1402
                    e.endpos = token.pos;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1403
                    accept(RBRACE);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1404
                    return e;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1405
                default:
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1406
                    nextToken(); // to ensure progress
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1407
                    syntaxError(pos, Errors.Expected3(CASE, DEFAULT, RBRACE));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1408
                }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1409
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1410
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1411
            return illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1412
        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1413
        return term3Rest(t, typeArgs);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1414
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1415
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1416
    private List<JCCase> switchExpressionStatementGroup() {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1417
        ListBuffer<JCCase> caseExprs = new ListBuffer<>();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1418
        int casePos = token.pos;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1419
        ListBuffer<JCExpression> pats = new ListBuffer<>();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1420
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1421
        if (token.kind == DEFAULT) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1422
            nextToken();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1423
        } else {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1424
            accept(CASE);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1425
            while (true) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1426
                pats.append(term(EXPR | NOLAMBDA));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1427
                if (token.kind != COMMA) break;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1428
                checkSourceLevel(Feature.SWITCH_MULTIPLE_CASE_LABELS);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1429
                nextToken();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1430
            };
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1431
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1432
        List<JCStatement> stats = null;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1433
        JCTree body = null;
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 57724
diff changeset
  1434
        CaseTree.CaseKind kind;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1435
        switch (token.kind) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1436
            case ARROW:
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1437
                checkSourceLevel(Feature.SWITCH_RULE);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1438
                nextToken();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1439
                if (token.kind == TokenKind.THROW || token.kind == TokenKind.LBRACE) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1440
                    stats = List.of(parseStatement());
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1441
                    body = stats.head;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1442
                    kind = JCCase.RULE;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1443
                } else {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1444
                    JCExpression value = parseExpression();
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1445
                    stats = List.of(to(F.at(value).Yield(value)));
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1446
                    body = value;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1447
                    kind = JCCase.RULE;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1448
                    accept(SEMI);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1449
                }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1450
                break;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1451
            default:
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1452
                accept(COLON, tk -> Errors.Expected2(COLON, ARROW));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1453
                stats = blockStatements();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1454
                kind = JCCase.STATEMENT;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1455
                break;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1456
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1457
        caseExprs.append(toP(F.at(casePos).Case(kind, pats.toList(), stats, body)));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1458
        return caseExprs.toList();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1459
    }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1460
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1461
    JCExpression term3Rest(JCExpression t, List<JCExpression> typeArgs) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1462
        if (typeArgs != null) illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1463
        while (true) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1464
            int pos1 = token.pos;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1465
            final List<JCAnnotation> annos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1466
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1467
            if (token.kind == LBRACKET) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1468
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1469
                if ((mode & TYPE) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1470
                    int oldmode = mode;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1471
                    selectTypeMode();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1472
                    if (token.kind == RBRACKET) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1473
                        nextToken();
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  1474
                        t = bracketsOpt(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1475
                        t = toP(F.at(pos1).TypeArray(t));
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1476
                        if (token.kind == COLCOL) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1477
                            selectExprMode();
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1478
                            continue;
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1479
                        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1480
                        if (annos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1481
                            t = toP(F.at(pos1).AnnotatedType(annos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1482
                        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1483
                        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1484
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1485
                    mode = oldmode;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1486
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1487
                if ((mode & EXPR) != 0) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1488
                    selectExprMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1489
                    JCExpression t1 = term();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1490
                    t = to(F.at(pos1).Indexed(t, t1));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1491
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1492
                accept(RBRACKET);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1493
            } else if (token.kind == DOT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1494
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1495
                typeArgs = typeArgumentsOpt(EXPR);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1496
                if (token.kind == SUPER && (mode & EXPR) != 0) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1497
                    selectExprMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1498
                    t = to(F.at(pos1).Select(t, names._super));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1499
                    nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1500
                    t = arguments(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1501
                    typeArgs = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1502
                } else if (token.kind == NEW && (mode & EXPR) != 0) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1503
                    if (typeArgs != null) return illegal();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1504
                    selectExprMode();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1505
                    int pos2 = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1506
                    nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1507
                    if (token.kind == LT) typeArgs = typeArguments(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1508
                    t = innerCreator(pos2, typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1509
                    typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1510
                } else {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1511
                    List<JCAnnotation> tyannos = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1512
                    if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1513
                        // is the mode check needed?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1514
                        tyannos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1515
                    }
31939
89d4a8756f98 8081769: Redundant error message on bad usage of 'class' literal
mcimadamore
parents: 29293
diff changeset
  1516
                    t = toP(F.at(pos1).Select(t, ident(true)));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1517
                    if (tyannos != null && tyannos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1518
                        t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1519
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1520
                    t = argumentsOpt(typeArgs, typeArgumentsOpt(t));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1521
                    typeArgs = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1522
                }
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1523
            } else if ((mode & EXPR) != 0 && token.kind == COLCOL) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1524
                selectExprMode();
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1525
                if (typeArgs != null) return illegal();
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1526
                accept(COLCOL);
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1527
                t = memberReferenceSuffix(pos1, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1528
            } else {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1529
                if (!annos.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1530
                    if (permitTypeAnnotationsPushBack)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1531
                        typeAnnotationsPushedBack = annos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1532
                    else
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1533
                        return illegal(annos.head.pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1534
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1535
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1536
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1537
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1538
        while ((token.kind == PLUSPLUS || token.kind == SUBSUB) && (mode & EXPR) != 0) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1539
            selectExprMode();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1540
            t = to(F.at(token.pos).Unary(
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  1541
                  token.kind == PLUSPLUS ? POSTINC : POSTDEC, t));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1542
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1543
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1544
        return toP(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1545
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1546
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1547
    /**
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1548
     * 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
  1549
     * 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
  1550
     * 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
  1551
     */
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1552
    @SuppressWarnings("fallthrough")
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1553
    boolean isUnboundMemberRef() {
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1554
        int pos = 0, depth = 0;
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1555
        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
  1556
            switch (t.kind) {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1557
                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
  1558
                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
  1559
                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
  1560
                case DOUBLE: case BOOLEAN: case CHAR:
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1561
                case MONKEYS_AT:
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1562
                    break;
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1563
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1564
                case LPAREN:
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1565
                    // skip annotation values
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1566
                    int nesting = 0;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1567
                    for (; ; pos++) {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1568
                        TokenKind tk2 = S.token(pos).kind;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1569
                        switch (tk2) {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1570
                            case EOF:
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1571
                                return false;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1572
                            case LPAREN:
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1573
                                nesting++;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1574
                                break;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1575
                            case RPAREN:
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1576
                                nesting--;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1577
                                if (nesting == 0) {
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1578
                                    continue outer;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1579
                                }
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1580
                                break;
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1581
                        }
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1582
                    }
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15711
diff changeset
  1583
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1584
                case LT:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1585
                    depth++; break;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1586
                case GTGTGT:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1587
                    depth--;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1588
                case GTGT:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1589
                    depth--;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1590
                case GT:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1591
                    depth--;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1592
                    if (depth == 0) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1593
                        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
  1594
                        return
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1595
                            nextKind == TokenKind.DOT ||
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1596
                            nextKind == TokenKind.LBRACKET ||
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1597
                            nextKind == TokenKind.COLCOL;
11322
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1598
                    }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1599
                    break;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1600
                default:
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1601
                    return false;
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1602
            }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1603
        }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1604
    }
6ee85f80967a 7120463: Fix method reference parser support in order to avoid ambiguities
mcimadamore
parents: 11149
diff changeset
  1605
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1606
    /**
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1607
     * 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
  1608
     * 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
  1609
     * 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
  1610
     */
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1611
    @SuppressWarnings("fallthrough")
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1612
    ParensResult analyzeParens() {
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1613
        int depth = 0;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1614
        boolean type = false;
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  1615
        ParensResult defaultResult = ParensResult.PARENS;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1616
        outer: for (int lookahead = 0; ; lookahead++) {
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1617
            TokenKind tk = S.token(lookahead).kind;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1618
            switch (tk) {
17803
d73ddda4575f 8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents: 17578
diff changeset
  1619
                case COMMA:
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1620
                    type = true;
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1621
                case EXTENDS: case SUPER: case DOT: case AMP:
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1622
                    //skip
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1623
                    break;
17803
d73ddda4575f 8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents: 17578
diff changeset
  1624
                case QUES:
d73ddda4575f 8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents: 17578
diff changeset
  1625
                    if (peekToken(lookahead, EXTENDS) ||
d73ddda4575f 8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents: 17578
diff changeset
  1626
                            peekToken(lookahead, SUPER)) {
d73ddda4575f 8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents: 17578
diff changeset
  1627
                        //wildcards
d73ddda4575f 8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents: 17578
diff changeset
  1628
                        type = true;
d73ddda4575f 8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents: 17578
diff changeset
  1629
                    }
d73ddda4575f 8014643: Parser regression in JDK 8 when compiling super.x
mcimadamore
parents: 17578
diff changeset
  1630
                    break;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1631
                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
  1632
                case DOUBLE: case BOOLEAN: case CHAR: case VOID:
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1633
                    if (peekToken(lookahead, RPAREN)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1634
                        //Type, ')' -> cast
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1635
                        return ParensResult.CAST;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1636
                    } else if (peekToken(lookahead, LAX_IDENTIFIER)) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1637
                        //Type, Identifier/'_'/'assert'/'enum' -> explicit lambda
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1638
                        return ParensResult.EXPLICIT_LAMBDA;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1639
                    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1640
                    break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1641
                case LPAREN:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1642
                    if (lookahead != 0) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1643
                        // '(' in a non-starting position -> parens
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1644
                        return ParensResult.PARENS;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1645
                    } else if (peekToken(lookahead, RPAREN)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1646
                        // '(', ')' -> explicit lambda
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1647
                        return ParensResult.EXPLICIT_LAMBDA;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1648
                    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1649
                    break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1650
                case RPAREN:
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1651
                    // if we have seen something that looks like a type,
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1652
                    // then it's a cast expression
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1653
                    if (type) return ParensResult.CAST;
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1654
                    // otherwise, disambiguate cast vs. parenthesized expression
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1655
                    // based on subsequent token.
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1656
                    switch (S.token(lookahead + 1).kind) {
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1657
                        /*case PLUSPLUS: case SUBSUB: */
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1658
                        case BANG: case TILDE:
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1659
                        case LPAREN: case THIS: case SUPER:
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1660
                        case INTLITERAL: case LONGLITERAL: case FLOATLITERAL:
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1661
                        case DOUBLELITERAL: case CHARLITERAL: case STRINGLITERAL:
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1662
                        case TRUE: case FALSE: case NULL:
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1663
                        case NEW: case IDENTIFIER: case ASSERT: case ENUM: case UNDERSCORE:
54900
e9f5e06a0dd7 8224031: Cannot parse switch expressions after type cast
jlahoda
parents: 52635
diff changeset
  1664
                        case SWITCH:
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1665
                        case BYTE: case SHORT: case CHAR: case INT:
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1666
                        case LONG: case FLOAT: case DOUBLE: case BOOLEAN: case VOID:
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1667
                            return ParensResult.CAST;
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1668
                        default:
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  1669
                            return defaultResult;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1670
                    }
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1671
                case UNDERSCORE:
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1672
                case ASSERT:
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1673
                case ENUM:
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1674
                case IDENTIFIER:
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1675
                    if (peekToken(lookahead, LAX_IDENTIFIER)) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1676
                        // Identifier, Identifier/'_'/'assert'/'enum' -> explicit lambda
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1677
                        return ParensResult.EXPLICIT_LAMBDA;
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1678
                    } else if (peekToken(lookahead, RPAREN, ARROW)) {
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1679
                        // Identifier, ')' '->' -> implicit lambda
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1680
                        return (mode & NOLAMBDA) == 0 ? ParensResult.IMPLICIT_LAMBDA
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1681
                                                      : ParensResult.PARENS;
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  1682
                    } else if (depth == 0 && peekToken(lookahead, COMMA)) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  1683
                        defaultResult = ParensResult.IMPLICIT_LAMBDA;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1684
                    }
22004
203318f0799d 8029179: javac produces a compile error for valid boolean expressions
vromero
parents: 20609
diff changeset
  1685
                    type = false;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1686
                    break;
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1687
                case FINAL:
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1688
                case ELLIPSIS:
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1689
                    //those can only appear in explicit lambdas
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1690
                    return ParensResult.EXPLICIT_LAMBDA;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1691
                case MONKEYS_AT:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1692
                    type = true;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1693
                    lookahead += 1; //skip '@'
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1694
                    while (peekToken(lookahead, DOT)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1695
                        lookahead += 2;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1696
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1697
                    if (peekToken(lookahead, LPAREN)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1698
                        lookahead++;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1699
                        //skip annotation values
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1700
                        int nesting = 0;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1701
                        for (; ; lookahead++) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1702
                            TokenKind tk2 = S.token(lookahead).kind;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1703
                            switch (tk2) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1704
                                case EOF:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1705
                                    return ParensResult.PARENS;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1706
                                case LPAREN:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1707
                                    nesting++;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1708
                                    break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1709
                                case RPAREN:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1710
                                    nesting--;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1711
                                    if (nesting == 0) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1712
                                        continue outer;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1713
                                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1714
                                break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1715
                            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1716
                        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1717
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  1718
                    break;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1719
                case LBRACKET:
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1720
                    if (peekToken(lookahead, RBRACKET, LAX_IDENTIFIER)) {
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1721
                        // '[', ']', Identifier/'_'/'assert'/'enum' -> explicit lambda
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1722
                        return ParensResult.EXPLICIT_LAMBDA;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1723
                    } else if (peekToken(lookahead, RBRACKET, RPAREN) ||
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1724
                            peekToken(lookahead, RBRACKET, AMP)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1725
                        // '[', ']', ')' -> cast
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1726
                        // '[', ']', '&' -> cast (intersection type)
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1727
                        return ParensResult.CAST;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1728
                    } else if (peekToken(lookahead, RBRACKET)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1729
                        //consume the ']' and skip
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1730
                        type = true;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1731
                        lookahead++;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1732
                        break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1733
                    } else {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1734
                        return ParensResult.PARENS;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1735
                    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1736
                case LT:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1737
                    depth++; break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1738
                case GTGTGT:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1739
                    depth--;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1740
                case GTGT:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1741
                    depth--;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1742
                case GT:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1743
                    depth--;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1744
                    if (depth == 0) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1745
                        if (peekToken(lookahead, RPAREN) ||
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1746
                                peekToken(lookahead, AMP)) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1747
                            // '>', ')' -> cast
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1748
                            // '>', '&' -> cast
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1749
                            return ParensResult.CAST;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1750
                        } else if (peekToken(lookahead, LAX_IDENTIFIER, COMMA) ||
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1751
                                peekToken(lookahead, LAX_IDENTIFIER, RPAREN, ARROW) ||
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1752
                                peekToken(lookahead, ELLIPSIS)) {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1753
                            // '>', Identifier/'_'/'assert'/'enum', ',' -> explicit lambda
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1754
                            // '>', Identifier/'_'/'assert'/'enum', ')', '->' -> explicit lambda
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1755
                            // '>', '...' -> explicit lambda
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1756
                            return ParensResult.EXPLICIT_LAMBDA;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1757
                        }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1758
                        //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
  1759
                        //(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
  1760
                        type = true;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1761
                        break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1762
                    } else if (depth < 0) {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1763
                        //unbalanced '<', '>' - not a generic type
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1764
                        return ParensResult.PARENS;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1765
                    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1766
                    break;
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1767
                default:
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1768
                    //this includes EOF
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  1769
                    return defaultResult;
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1770
            }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1771
        }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1772
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1773
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1774
    /** Accepts all identifier-like tokens */
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42407
diff changeset
  1775
    protected Filter<TokenKind> LAX_IDENTIFIER = t -> t == IDENTIFIER || t == UNDERSCORE || t == ASSERT || t == ENUM;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1776
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1777
    enum ParensResult {
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1778
        CAST,
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1779
        EXPLICIT_LAMBDA,
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1780
        IMPLICIT_LAMBDA,
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  1781
        PARENS
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1782
    }
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 14722
diff changeset
  1783
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1784
    JCExpression lambdaExpressionOrStatement(boolean hasParens, boolean explicitParams, int pos) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1785
        List<JCVariableDecl> params = explicitParams ?
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  1786
                formalParameters(true) :
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1787
                implicitParameters(hasParens);
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1788
        if (explicitParams) {
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1789
            LambdaClassifier lambdaClassifier = new LambdaClassifier();
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1790
            for (JCVariableDecl param: params) {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1791
                Name restrictedTypeName;
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1792
                if (param.vartype != null &&
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1793
                        (restrictedTypeName = restrictedTypeName(param.vartype, false)) != null &&
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1794
                        param.vartype.hasTag(TYPEARRAY)) {
52016
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1795
                    log.error(DiagnosticFlag.SYNTAX, param.pos,
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1796
                        Feature.VAR_SYNTAX_IMPLICIT_LAMBDAS.allowedInSource(source)
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1797
                            ? Errors.RestrictedTypeNotAllowedArray(restrictedTypeName) : Errors.RestrictedTypeNotAllowedHere(restrictedTypeName));
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1798
                }
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1799
                lambdaClassifier.addParameter(param);
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1800
                if (lambdaClassifier.result() == LambdaParameterKind.ERROR) {
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1801
                    break;
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1802
                }
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1803
            }
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1804
            if (lambdaClassifier.diagFragment != null) {
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1805
                log.error(DiagnosticFlag.SYNTAX, pos, Errors.InvalidLambdaParameterDeclaration(lambdaClassifier.diagFragment));
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1806
            }
49152
e38b6a7f65ee 8199327: nuke var type name after a lambda has been accepted
vromero
parents: 49076
diff changeset
  1807
            for (JCVariableDecl param: params) {
52016
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1808
                if (param.vartype != null
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1809
                        && restrictedTypeName(param.vartype, true) != null) {
52016
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1810
                    checkSourceLevel(param.pos, Feature.VAR_SYNTAX_IMPLICIT_LAMBDAS);
50518
e775444c555e 8204674: Inconsistent lambda parameter span
jlahoda
parents: 49580
diff changeset
  1811
                    param.startPos = TreeInfo.getStartPos(param.vartype);
49152
e38b6a7f65ee 8199327: nuke var type name after a lambda has been accepted
vromero
parents: 49076
diff changeset
  1812
                    param.vartype = null;
e38b6a7f65ee 8199327: nuke var type name after a lambda has been accepted
vromero
parents: 49076
diff changeset
  1813
                }
e38b6a7f65ee 8199327: nuke var type name after a lambda has been accepted
vromero
parents: 49076
diff changeset
  1814
            }
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1815
        }
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1816
        return lambdaExpressionOrStatementRest(params, pos);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1817
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1818
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1819
    enum LambdaParameterKind {
52016
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1820
        VAR(0),
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1821
        EXPLICIT(1),
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1822
        IMPLICIT(2),
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1823
        ERROR(-1);
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1824
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1825
        private final int index;
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1826
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1827
        LambdaParameterKind(int index) {
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1828
            this.index = index;
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1829
        }
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1830
    }
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1831
52016
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1832
    private final static Fragment[][] decisionTable = new Fragment[][] {
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1833
        /*              VAR                              EXPLICIT                         IMPLICIT  */
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1834
        /* VAR      */ {null,                            VarAndExplicitNotAllowed,        VarAndImplicitNotAllowed},
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1835
        /* EXPLICIT */ {VarAndExplicitNotAllowed,        null,                            ImplicitAndExplicitNotAllowed},
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1836
        /* IMPLICIT */ {VarAndImplicitNotAllowed,        ImplicitAndExplicitNotAllowed,   null},
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1837
    };
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1838
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1839
    class LambdaClassifier {
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1840
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1841
        LambdaParameterKind kind;
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1842
        Fragment diagFragment;
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1843
        List<JCVariableDecl> params;
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1844
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1845
        void addParameter(JCVariableDecl param) {
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1846
            if (param.vartype != null && param.name != names.empty) {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1847
                if (restrictedTypeName(param.vartype, false) != null) {
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1848
                    reduce(LambdaParameterKind.VAR);
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1849
                } else {
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1850
                    reduce(LambdaParameterKind.EXPLICIT);
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1851
                }
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1852
            }
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1853
            if (param.vartype == null && param.name != names.empty ||
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1854
                param.vartype != null && param.name == names.empty) {
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1855
                reduce(LambdaParameterKind.IMPLICIT);
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1856
            }
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1857
        }
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1858
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1859
        private void reduce(LambdaParameterKind newKind) {
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1860
            if (kind == null) {
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1861
                kind = newKind;
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1862
            } else if (kind != newKind && kind != LambdaParameterKind.ERROR) {
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1863
                LambdaParameterKind currentKind = kind;
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1864
                kind = LambdaParameterKind.ERROR;
52016
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1865
                boolean varIndex = currentKind.index == LambdaParameterKind.VAR.index ||
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1866
                        newKind.index == LambdaParameterKind.VAR.index;
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1867
                diagFragment = Feature.VAR_SYNTAX_IMPLICIT_LAMBDAS.allowedInSource(source) || !varIndex ?
9ea22a0f9540 8211148: var in implicit lambdas shouldn't be accepted for source < 11
vromero
parents: 51563
diff changeset
  1868
                        decisionTable[currentKind.index][newKind.index] : null;
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1869
            }
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1870
        }
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1871
49076
1d879babed52 8198628: further simplifications to lambda classification at JavacParser
vromero
parents: 48935
diff changeset
  1872
        LambdaParameterKind result() {
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1873
            return kind;
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1874
        }
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1875
    }
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  1876
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1877
    JCExpression lambdaExpressionOrStatementRest(List<JCVariableDecl> args, int pos) {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  1878
        checkSourceLevel(Feature.LAMBDA);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1879
        accept(ARROW);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1880
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1881
        return token.kind == LBRACE ?
43030
a3d5b201d899 8065800: javac, fix diagnostic position for statement-bodied lambdas
cushon
parents: 42828
diff changeset
  1882
            lambdaStatement(args, pos, token.pos) :
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1883
            lambdaExpression(args, pos);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1884
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1885
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1886
    JCExpression lambdaStatement(List<JCVariableDecl> args, int pos, int pos2) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1887
        JCBlock block = block(pos2, 0);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1888
        return toP(F.at(pos).Lambda(args, block));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1889
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1890
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1891
    JCExpression lambdaExpression(List<JCVariableDecl> args, int pos) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1892
        JCTree expr = parseExpression();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1893
        return toP(F.at(pos).Lambda(args, expr));
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1894
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  1895
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1896
    /** SuperSuffix = Arguments | "." [TypeArguments] Ident [Arguments]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1897
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1898
    JCExpression superSuffix(List<JCExpression> typeArgs, JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1899
        nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1900
        if (token.kind == LPAREN || typeArgs != null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1901
            t = arguments(typeArgs, t);
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  1902
        } else if (token.kind == COLCOL) {
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1903
            if (typeArgs != null) return illegal();
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  1904
            t = memberReferenceSuffix(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1905
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1906
            int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1907
            accept(DOT);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1908
            typeArgs = (token.kind == LT) ? typeArguments(false) : null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1909
            t = toP(F.at(pos).Select(t, ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  1910
            t = argumentsOpt(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1911
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1912
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1913
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1914
06bc494ca11e Initial load
duke
parents:
diff changeset
  1915
    /** BasicType = BYTE | SHORT | CHAR | INT | LONG | FLOAT | DOUBLE | BOOLEAN
06bc494ca11e Initial load
duke
parents:
diff changeset
  1916
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1917
    JCPrimitiveTypeTree basicType() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1918
        JCPrimitiveTypeTree t = to(F.at(token.pos).TypeIdent(typetag(token.kind)));
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1919
        nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1920
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1921
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1922
06bc494ca11e Initial load
duke
parents:
diff changeset
  1923
    /** ArgumentsOpt = [ Arguments ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1924
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1925
    JCExpression argumentsOpt(List<JCExpression> typeArgs, JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1926
        if ((mode & EXPR) != 0 && token.kind == LPAREN || typeArgs != null) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1927
            selectExprMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1928
            return arguments(typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1929
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1930
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1931
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1932
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1933
06bc494ca11e Initial load
duke
parents:
diff changeset
  1934
    /** Arguments = "(" [Expression { COMMA Expression }] ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
  1935
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1936
    List<JCExpression> arguments() {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19122
diff changeset
  1937
        ListBuffer<JCExpression> args = new ListBuffer<>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1938
        if (token.kind == LPAREN) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1939
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1940
            if (token.kind != RPAREN) {
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1941
                args.append(parseExpression());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1942
                while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1943
                    nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  1944
                    args.append(parseExpression());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1945
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1946
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1947
            accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1948
        } else {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  1949
            syntaxError(token.pos, Errors.Expected(LPAREN));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1950
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1951
        return args.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1952
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1953
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1954
    JCExpression arguments(List<JCExpression> typeArgs, JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1955
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1956
        List<JCExpression> args = arguments();
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1957
        JCExpression mi = F.at(pos).Apply(typeArgs, t, args);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1958
        if (t.hasTag(IDENT) && isInvalidUnqualifiedMethodIdentifier(((JCIdent) t).pos,
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1959
                                                                    ((JCIdent) t).name)) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1960
            log.error(DiagnosticFlag.SYNTAX, t, Errors.InvalidYield);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1961
            mi = F.Erroneous(List.of(mi));
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1962
        }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1963
        return toP(mi);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1964
    }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1965
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1966
    boolean isInvalidUnqualifiedMethodIdentifier(int pos, Name name) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1967
        if (name == names.yield) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1968
            if (allowYieldStatement) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1969
                return true;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1970
            } else {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1971
                log.warning(pos, Warnings.InvalidYield);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1972
            }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1973
        }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  1974
        return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1975
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1976
06bc494ca11e Initial load
duke
parents:
diff changeset
  1977
    /**  TypeArgumentsOpt = [ TypeArguments ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  1978
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  1979
    JCExpression typeArgumentsOpt(JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1980
        if (token.kind == LT &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1981
            (mode & TYPE) != 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  1982
            (mode & NOPARAMS) == 0) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  1983
            selectTypeMode();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  1984
            return typeArguments(t, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1985
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1986
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1987
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1988
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1989
    List<JCExpression> typeArgumentsOpt() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1990
        return typeArgumentsOpt(TYPE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  1991
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1992
06bc494ca11e Initial load
duke
parents:
diff changeset
  1993
    List<JCExpression> typeArgumentsOpt(int useMode) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  1994
        if (token.kind == LT) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1995
            if ((mode & useMode) == 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
  1996
                (mode & NOPARAMS) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  1997
                illegal();
06bc494ca11e Initial load
duke
parents:
diff changeset
  1998
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1999
            mode = useMode;
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2000
            return typeArguments(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2001
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2002
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2003
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2004
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  2005
    /**
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  2006
     *  {@literal
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  2007
     *  TypeArguments  = "<" TypeArgument {"," TypeArgument} ">"
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  2008
     *  }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2009
     */
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2010
    List<JCExpression> typeArguments(boolean diamondAllowed) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2011
        if (token.kind == LT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2012
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2013
            if (token.kind == GT && diamondAllowed) {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  2014
                checkSourceLevel(Feature.DIAMOND);
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2015
                mode |= DIAMOND;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2016
                nextToken();
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  2017
                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
  2018
            } else {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19122
diff changeset
  2019
                ListBuffer<JCExpression> args = new ListBuffer<>();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2020
                args.append(((mode & EXPR) == 0) ? typeArgument() : parseType());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2021
                while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2022
                    nextToken();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2023
                    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
  2024
                }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2025
                switch (token.kind) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2026
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2027
                case GTGTGTEQ: case GTGTEQ: case GTEQ:
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2028
                case GTGTGT: case GTGT:
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2029
                    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
  2030
                    break;
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2031
                case GT:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2032
                    nextToken();
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2033
                    break;
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2034
                default:
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2035
                    args.append(syntaxError(token.pos, Errors.Expected(GT)));
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2036
                    break;
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2037
                }
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2038
                return args.toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2039
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2040
        } else {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2041
            return List.of(syntaxError(token.pos, Errors.Expected(LT)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2042
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2043
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2044
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  2045
    /**
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  2046
     *  {@literal
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  2047
     *  TypeArgument = Type
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2048
     *               | [Annotations] "?"
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2049
     *               | [Annotations] "?" EXTENDS Type {"&" Type}
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2050
     *               | [Annotations] "?" SUPER Type
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  2051
     *  }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2052
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2053
    JCExpression typeArgument() {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2054
        List<JCAnnotation> annotations = typeAnnotationsOpt();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2055
        if (token.kind != QUES) return parseType(false, annotations);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2056
        int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2057
        nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2058
        JCExpression result;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2059
        if (token.kind == EXTENDS) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2060
            TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.EXTENDS));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2061
            nextToken();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2062
            JCExpression bound = parseType();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2063
            result = F.at(pos).Wildcard(t, bound);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2064
        } else if (token.kind == SUPER) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2065
            TypeBoundKind t = to(F.at(pos).TypeBoundKind(BoundKind.SUPER));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2066
            nextToken();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2067
            JCExpression bound = parseType();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2068
            result = F.at(pos).Wildcard(t, bound);
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2069
        } else if (LAX_IDENTIFIER.accepts(token.kind)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2070
            //error recovery
06bc494ca11e Initial load
duke
parents:
diff changeset
  2071
            TypeBoundKind t = F.at(Position.NOPOS).TypeBoundKind(BoundKind.UNBOUND);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2072
            JCExpression wc = toP(F.at(pos).Wildcard(t, null));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2073
            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
  2074
            JCErroneous err = F.at(pos).Erroneous(List.<JCTree>of(wc, id));
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2075
            reportSyntaxError(err, Errors.Expected3(GT, EXTENDS, SUPER));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2076
            result = err;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2077
        } else {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  2078
            TypeBoundKind t = toP(F.at(pos).TypeBoundKind(BoundKind.UNBOUND));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2079
            result = toP(F.at(pos).Wildcard(t, null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2080
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2081
        if (!annotations.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2082
            result = toP(F.at(annotations.head.pos).AnnotatedType(annotations,result));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2083
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2084
        return result;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2085
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2086
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2087
    JCTypeApply typeArguments(JCExpression t, boolean diamondAllowed) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2088
        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
  2089
        List<JCExpression> args = typeArguments(diamondAllowed);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2090
        return toP(F.at(pos).TypeApply(t, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2091
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2092
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
     * BracketsOpt = { [Annotations] "[" "]" }*
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2095
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2096
     * <p>
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2097
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2098
     * <code>annotations</code> is the list of annotations targeting
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2099
     * the expression <code>t</code>.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2100
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2101
    private JCExpression bracketsOpt(JCExpression t,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2102
            List<JCAnnotation> annotations) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2103
        List<JCAnnotation> nextLevelAnnotations = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2104
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2105
        if (token.kind == LBRACKET) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2106
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2107
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2108
            t = bracketsOptCont(t, pos, nextLevelAnnotations);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2109
        } else if (!nextLevelAnnotations.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2110
            if (permitTypeAnnotationsPushBack) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2111
                this.typeAnnotationsPushedBack = nextLevelAnnotations;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2112
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2113
                return illegal(nextLevelAnnotations.head.pos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2114
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2115
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2116
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2117
        if (!annotations.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2118
            t = toP(F.at(token.pos).AnnotatedType(annotations, t));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2119
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2120
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2122
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2123
    /** BracketsOpt = [ "[" "]" { [Annotations] "[" "]"} ]
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2124
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2125
    private JCExpression bracketsOpt(JCExpression t) {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2126
        return bracketsOpt(t, List.nil());
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2127
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2128
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2129
    private JCExpression bracketsOptCont(JCExpression t, int pos,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2130
            List<JCAnnotation> annotations) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2131
        accept(RBRACKET);
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  2132
        t = bracketsOpt(t);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2133
        t = toP(F.at(pos).TypeArray(t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2134
        if (annotations.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2135
            t = toP(F.at(pos).AnnotatedType(annotations, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2136
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2137
        return t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2139
06bc494ca11e Initial load
duke
parents:
diff changeset
  2140
    /** BracketsSuffixExpr = "." CLASS
06bc494ca11e Initial load
duke
parents:
diff changeset
  2141
     *  BracketsSuffixType =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2142
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2143
    JCExpression bracketsSuffix(JCExpression t) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2144
        if ((mode & EXPR) != 0 && token.kind == DOT) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2145
            selectExprMode();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2146
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2147
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2148
            accept(CLASS);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  2149
            if (token.pos == endPosTable.errorEndPos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2150
                // error recovery
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2151
                Name name;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2152
                if (LAX_IDENTIFIER.accepts(token.kind)) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2153
                    name = token.name();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2154
                    nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2155
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2156
                    name = names.error;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2157
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2158
                t = F.at(pos).Erroneous(List.<JCTree>of(toP(F.at(pos).Select(t, name))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2159
            } else {
28454
63c31d7de8f6 8027888: javac wrongly allows annotations in array-typed class literals
jlahoda
parents: 28147
diff changeset
  2160
                Tag tag = t.getTag();
63c31d7de8f6 8027888: javac wrongly allows annotations in array-typed class literals
jlahoda
parents: 28147
diff changeset
  2161
                // 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
  2162
                // 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
  2163
                // 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
  2164
                if ((tag == TYPEARRAY && TreeInfo.containsTypeAnnotation(t)) || tag == ANNOTATED_TYPE)
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2165
                    syntaxError(token.pos, Errors.NoAnnotationsOnDotClass);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2166
                t = toP(F.at(pos).Select(t, names._class));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2167
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2168
        } else if ((mode & TYPE) != 0) {
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2169
            if (token.kind != COLCOL) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2170
                selectTypeMode();
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2171
            }
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2172
        } else if (token.kind != COLCOL) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2173
            syntaxError(token.pos, Errors.DotClassExpected);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2174
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2175
        return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2176
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2177
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2178
    /**
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2179
     * MemberReferenceSuffix = "::" [TypeArguments] Ident
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2180
     *                       | "::" [TypeArguments] "new"
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2181
     */
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2182
    JCExpression memberReferenceSuffix(JCExpression t) {
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2183
        int pos1 = token.pos;
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
  2184
        accept(COLCOL);
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2185
        return memberReferenceSuffix(pos1, t);
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2186
    }
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2187
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2188
    JCExpression memberReferenceSuffix(int pos1, JCExpression t) {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  2189
        checkSourceLevel(Feature.METHOD_REFERENCES);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2190
        selectExprMode();
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2191
        List<JCExpression> typeArgs = null;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2192
        if (token.kind == LT) {
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2193
            typeArgs = typeArguments(false);
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2194
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2195
        Name refName;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2196
        ReferenceMode refMode;
11144
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2197
        if (token.kind == NEW) {
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2198
            refMode = ReferenceMode.NEW;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2199
            refName = names.init;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2200
            nextToken();
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2201
        } else {
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2202
            refMode = ReferenceMode.INVOKE;
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2203
            refName = ident();
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2204
        }
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2205
        return toP(F.at(t.getStartPosition()).Reference(refMode, refName, t, typeArgs));
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2206
    }
8a4ae514eedf 7115052: Add parser support for method references
mcimadamore
parents: 11143
diff changeset
  2207
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2208
    /** Creator = [Annotations] Qualident [TypeArguments] ( ArrayCreatorRest | ClassCreatorRest )
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2209
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2210
    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
  2211
        List<JCAnnotation> newAnnotations = typeAnnotationsOpt();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2212
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2213
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2214
        case BYTE: case SHORT: case CHAR: case INT: case LONG: case FLOAT:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2215
        case DOUBLE: case BOOLEAN:
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2216
            if (typeArgs == null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2217
                if (newAnnotations.isEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2218
                    return arrayCreatorRest(newpos, basicType());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2219
                } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2220
                    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
  2221
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2222
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2223
            break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2224
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2225
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2226
        JCExpression t = qualident(true);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2227
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2228
        int oldmode = mode;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2229
        selectTypeMode();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2230
        boolean diamondFound = false;
10199
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  2231
        int lastTypeargsPos = -1;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2232
        if (token.kind == LT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2233
            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
  2234
            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
  2235
            diamondFound = (mode & DIAMOND) != 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2236
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2237
        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
  2238
            if (diamondFound) {
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2239
                //cannot select after a diamond
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2240
                illegal();
9073
a29adf86aa0c 7030687: Diamond: compiler accepts erroneous code where diamond is used with non-generic inner class
mcimadamore
parents: 8625
diff changeset
  2241
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2242
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2243
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2244
            List<JCAnnotation> tyannos = typeAnnotationsOpt();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2245
            t = toP(F.at(pos).Select(t, ident()));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2246
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2247
            if (tyannos != null && tyannos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2248
                t = toP(F.at(tyannos.head.pos).AnnotatedType(tyannos, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2249
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2250
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2251
            if (token.kind == LT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2252
                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
  2253
                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
  2254
                diamondFound = (mode & DIAMOND) != 0;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2255
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2256
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2257
        mode = oldmode;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2258
        if (token.kind == LBRACKET || token.kind == MONKEYS_AT) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  2259
            // handle type annotations for non primitive arrays
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  2260
            if (newAnnotations.nonEmpty()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  2261
                t = insertAnnotationsToMostInner(t, newAnnotations, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  2262
            }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  2263
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2264
            JCExpression e = arrayCreatorRest(newpos, t);
10199
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  2265
            if (diamondFound) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2266
                reportSyntaxError(lastTypeargsPos, Errors.CannotCreateArrayWithDiamond);
10199
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  2267
                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
  2268
            }
48bd09ecc88a 7057297: Project Coin: diamond erroneously accepts in array initializer expressions
mcimadamore
parents: 9300
diff changeset
  2269
            else if (typeArgs != null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2270
                int pos = newpos;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2271
                if (!typeArgs.isEmpty() && typeArgs.head.pos != Position.NOPOS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2272
                    // note: this should always happen but we should
06bc494ca11e Initial load
duke
parents:
diff changeset
  2273
                    // not rely on this as the parser is continuously
06bc494ca11e Initial load
duke
parents:
diff changeset
  2274
                    // modified to improve error recovery.
06bc494ca11e Initial load
duke
parents:
diff changeset
  2275
                    pos = typeArgs.head.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2276
                }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2277
                setErrorEndPos(S.prevToken().endPos);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2278
                JCErroneous err = F.at(pos).Erroneous(typeArgs.prepend(e));
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2279
                reportSyntaxError(err, Errors.CannotCreateArrayWithTypeArguments);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2280
                return toP(err);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2281
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2282
            return e;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2283
        } else if (token.kind == LPAREN) {
52278
e11a53698d57 8198945: Invalid RuntimeVisibleTypeAnnotations for annotation on anonymous class type parameter
cushon
parents: 52016
diff changeset
  2284
            // handle type annotations for instantiations and anonymous classes
e11a53698d57 8198945: Invalid RuntimeVisibleTypeAnnotations for annotation on anonymous class type parameter
cushon
parents: 52016
diff changeset
  2285
            if (newAnnotations.nonEmpty()) {
e11a53698d57 8198945: Invalid RuntimeVisibleTypeAnnotations for annotation on anonymous class type parameter
cushon
parents: 52016
diff changeset
  2286
                t = insertAnnotationsToMostInner(t, newAnnotations, false);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2287
            }
52278
e11a53698d57 8198945: Invalid RuntimeVisibleTypeAnnotations for annotation on anonymous class type parameter
cushon
parents: 52016
diff changeset
  2288
            return classCreatorRest(newpos, null, typeArgs, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2289
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2290
            setErrorEndPos(token.pos);
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2291
            reportSyntaxError(token.pos, Errors.Expected2(LPAREN, LBRACKET));
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2292
            t = toP(F.at(newpos).NewClass(null, typeArgs, t, List.nil(), null));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2293
            return toP(F.at(newpos).Erroneous(List.<JCTree>of(t)));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2294
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2295
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2296
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2297
    /** InnerCreator = [Annotations] Ident [TypeArguments] ClassCreatorRest
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2298
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2299
    JCExpression innerCreator(int newpos, List<JCExpression> typeArgs, JCExpression encl) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2300
        List<JCAnnotation> newAnnotations = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2301
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2302
        JCExpression t = toP(F.at(token.pos).Ident(ident()));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2303
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2304
        if (newAnnotations.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2305
            t = toP(F.at(newAnnotations.head.pos).AnnotatedType(newAnnotations, t));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2306
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2307
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2308
        if (token.kind == LT) {
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  2309
            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
  2310
            t = typeArguments(t, true);
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
  2311
            mode = oldmode;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2312
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2313
        return classCreatorRest(newpos, encl, typeArgs, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2314
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2315
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2316
    /** ArrayCreatorRest = [Annotations] "[" ( "]" BracketsOpt ArrayInitializer
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2317
     *                         | Expression "]" {[Annotations]  "[" Expression "]"} BracketsOpt )
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2318
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2319
    JCExpression arrayCreatorRest(int newpos, JCExpression elemtype) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2320
        List<JCAnnotation> annos = typeAnnotationsOpt();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2321
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2322
        accept(LBRACKET);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2323
        if (token.kind == RBRACKET) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2324
            accept(RBRACKET);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2325
            elemtype = bracketsOpt(elemtype, annos);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2326
            if (token.kind == LBRACE) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2327
                JCNewArray na = (JCNewArray)arrayInitializer(newpos, elemtype);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2328
                if (annos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2329
                    // when an array initializer is present then
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2330
                    // the parsed annotations should target the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2331
                    // new array tree
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2332
                    // bracketsOpt inserts the annotation in
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2333
                    // elemtype, and it needs to be corrected
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2334
                    //
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2335
                    JCAnnotatedType annotated = (JCAnnotatedType)elemtype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2336
                    assert annotated.annotations == annos;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2337
                    na.annotations = annotated.annotations;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2338
                    na.elemtype = annotated.underlyingType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2339
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2340
                return na;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2341
            } else {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2342
                JCExpression t = toP(F.at(newpos).NewArray(elemtype, List.nil(), null));
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2343
                return syntaxError(token.pos, List.of(t), Errors.ArrayDimensionMissing);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2344
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2345
        } else {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  2346
            ListBuffer<JCExpression> dims = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2347
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2348
            // maintain array dimension type annotations
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19122
diff changeset
  2349
            ListBuffer<List<JCAnnotation>> dimAnnotations = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2350
            dimAnnotations.append(annos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2351
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2352
            dims.append(parseExpression());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2353
            accept(RBRACKET);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2354
            while (token.kind == LBRACKET
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2355
                    || token.kind == MONKEYS_AT) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2356
                List<JCAnnotation> maybeDimAnnos = typeAnnotationsOpt();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2357
                int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2358
                nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2359
                if (token.kind == RBRACKET) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2360
                    elemtype = bracketsOptCont(elemtype, pos, maybeDimAnnos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2361
                } else {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2362
                    if (token.kind == RBRACKET) { // no dimension
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2363
                        elemtype = bracketsOptCont(elemtype, pos, maybeDimAnnos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2364
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2365
                        dimAnnotations.append(maybeDimAnnos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2366
                        dims.append(parseExpression());
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2367
                        accept(RBRACKET);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2368
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2369
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2370
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2371
45915
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2372
            List<JCExpression> elems = null;
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2373
            int errpos = token.pos;
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2374
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2375
            if (token.kind == LBRACE) {
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2376
                elems = arrayInitializerElements(newpos, elemtype);
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2377
            }
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2378
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2379
            JCNewArray na = toP(F.at(newpos).NewArray(elemtype, dims.toList(), elems));
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2380
            na.dimAnnotations = dimAnnotations.toList();
45915
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2381
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2382
            if (elems != null) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2383
                return syntaxError(errpos, List.of(na), Errors.IllegalArrayCreationBothDimensionAndInitialization);
45915
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2384
            }
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2385
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2386
            return na;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2387
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2388
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2389
06bc494ca11e Initial load
duke
parents:
diff changeset
  2390
    /** ClassCreatorRest = Arguments [ClassBody]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2391
     */
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 2723
diff changeset
  2392
    JCNewClass classCreatorRest(int newpos,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2393
                                  JCExpression encl,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2394
                                  List<JCExpression> typeArgs,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2395
                                  JCExpression t)
06bc494ca11e Initial load
duke
parents:
diff changeset
  2396
    {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2397
        List<JCExpression> args = arguments();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2398
        JCClassDecl body = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2399
        if (token.kind == LBRACE) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2400
            int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2401
            List<JCTree> defs = classOrInterfaceBody(names.empty, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2402
            JCModifiers mods = F.at(Position.NOPOS).Modifiers(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2403
            body = toP(F.at(pos).AnonymousClassDef(mods, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2404
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2405
        return toP(F.at(newpos).NewClass(encl, typeArgs, t, args, body));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2406
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2407
06bc494ca11e Initial load
duke
parents:
diff changeset
  2408
    /** ArrayInitializer = "{" [VariableInitializer {"," VariableInitializer}] [","] "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2409
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2410
    JCExpression arrayInitializer(int newpos, JCExpression t) {
45915
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2411
        List<JCExpression> elems = arrayInitializerElements(newpos, t);
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2412
        return toP(F.at(newpos).NewArray(t, List.nil(), elems));
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2413
    }
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2414
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2415
    List<JCExpression> arrayInitializerElements(int newpos, JCExpression t) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2416
        accept(LBRACE);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  2417
        ListBuffer<JCExpression> elems = new ListBuffer<>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2418
        if (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2419
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2420
        } else if (token.kind != RBRACE) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2421
            elems.append(variableInitializer());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2422
            while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2423
                nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2424
                if (token.kind == RBRACE) break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2425
                elems.append(variableInitializer());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2426
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2427
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2428
        accept(RBRACE);
45915
1afba647cd44 8057647: javac parser needs updates to have better error recovery for error cases of new array creation with dimensions
pmuthuswamy
parents: 45504
diff changeset
  2429
        return elems.toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2430
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2431
06bc494ca11e Initial load
duke
parents:
diff changeset
  2432
    /** VariableInitializer = ArrayInitializer | Expression
06bc494ca11e Initial load
duke
parents:
diff changeset
  2433
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2434
    public JCExpression variableInitializer() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2435
        return token.kind == LBRACE ? arrayInitializer(token.pos, null) : parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2436
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2437
06bc494ca11e Initial load
duke
parents:
diff changeset
  2438
    /** ParExpression = "(" Expression ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2439
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2440
    JCExpression parExpression() {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  2441
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2442
        accept(LPAREN);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2443
        JCExpression t = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2444
        accept(RPAREN);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  2445
        return toP(F.at(pos).Parens(t));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2446
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2447
06bc494ca11e Initial load
duke
parents:
diff changeset
  2448
    /** Block = "{" BlockStatements "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2449
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2450
    JCBlock block(int pos, long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2451
        accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2452
        List<JCStatement> stats = blockStatements();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2453
        JCBlock t = F.at(pos).Block(flags, stats);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2454
        while (token.kind == CASE || token.kind == DEFAULT) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2455
            syntaxError(token.pos, Errors.Orphaned(token.kind));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2456
            switchBlockStatementGroups();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2457
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2458
        // the Block node has a field "endpos" for first char of last token, which is
06bc494ca11e Initial load
duke
parents:
diff changeset
  2459
        // usually but not necessarily the last char of the last token.
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2460
        t.endpos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2461
        accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2462
        return toP(t);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2463
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2464
06bc494ca11e Initial load
duke
parents:
diff changeset
  2465
    public JCBlock block() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2466
        return block(token.pos, 0);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2467
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2468
06bc494ca11e Initial load
duke
parents:
diff changeset
  2469
    /** BlockStatements = { BlockStatement }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2470
     *  BlockStatement  = LocalVariableDeclarationStatement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2471
     *                  | ClassOrInterfaceOrEnumDeclaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  2472
     *                  | [Ident ":"] Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2473
     *  LocalVariableDeclarationStatement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2474
     *                  = { FINAL | '@' Annotation } Type VariableDeclarators ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2475
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2476
    @SuppressWarnings("fallthrough")
06bc494ca11e Initial load
duke
parents:
diff changeset
  2477
    List<JCStatement> blockStatements() {
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2478
        //todo: skip to anchor on error(?)
23133
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
  2479
        int lastErrPos = -1;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  2480
        ListBuffer<JCStatement> stats = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2481
        while (true) {
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2482
            List<JCStatement> stat = blockStatement();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2483
            if (stat.isEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2484
                return stats.toList();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2485
            } else {
23133
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
  2486
                // error recovery
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
  2487
                if (token.pos == lastErrPos)
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
  2488
                    return stats.toList();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2489
                if (token.pos <= endPosTable.errorEndPos) {
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  2490
                    skip(false, true, true, true);
23133
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
  2491
                    lastErrPos = token.pos;
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2492
                }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2493
                stats.addAll(stat);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2494
            }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2495
        }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2496
    }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2497
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2498
    /*
26993
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2499
     * 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
  2500
     * 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
  2501
     * 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
  2502
     * 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
  2503
     * 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
  2504
     */
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2505
    JCStatement parseStatementAsBlock() {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2506
        int pos = token.pos;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2507
        List<JCStatement> stats = blockStatement();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2508
        if (stats.isEmpty()) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2509
            JCErroneous e = syntaxError(pos, Errors.IllegalStartOfStmt);
47439
94943e6674be 8189796: Incorrect end position for missing statement
jlahoda
parents: 47318
diff changeset
  2510
            return toP(F.at(pos).Exec(e));
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2511
        } else {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2512
            JCStatement first = stats.head;
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2513
            Error error = null;
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2514
            switch (first.getTag()) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2515
            case CLASSDEF:
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2516
                error = Errors.ClassNotAllowed;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2517
                break;
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2518
            case VARDEF:
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2519
                error = Errors.VariableNotAllowed;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2520
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2521
            }
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2522
            if (error != null) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2523
                log.error(DiagnosticFlag.SYNTAX, first, error);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2524
                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
  2525
                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
  2526
            }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2527
            return first;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2528
        }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2529
    }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2530
26993
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2531
    /**This method parses a statement appearing inside a block.
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2532
     */
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2533
    @SuppressWarnings("fallthrough")
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2534
    List<JCStatement> blockStatement() {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2535
        //todo: skip to anchor on error(?)
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2536
        int pos = token.pos;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2537
        switch (token.kind) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2538
        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
  2539
            return List.nil();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2540
        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
  2541
        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
  2542
        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
  2543
        case ASSERT:
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2544
            return List.of(parseSimpleStatement());
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2545
        case MONKEYS_AT:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2546
        case FINAL: {
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  2547
            Comment dc = token.comment(CommentStyle.JAVADOC);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2548
            JCModifiers mods = modifiersOpt();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2549
            if (token.kind == INTERFACE ||
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2550
                token.kind == CLASS ||
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  2551
                token.kind == ENUM) {
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2552
                return List.of(classOrInterfaceOrEnumDeclaration(mods, dc));
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2553
            } else {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2554
                JCExpression t = parseType(true);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2555
                return localVariableDeclarations(mods, t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2556
            }
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2557
        }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2558
        case ABSTRACT: case STRICTFP: {
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  2559
            Comment dc = token.comment(CommentStyle.JAVADOC);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2560
            JCModifiers mods = modifiersOpt();
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2561
            return List.of(classOrInterfaceOrEnumDeclaration(mods, dc));
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2562
        }
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2563
        case INTERFACE:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2564
        case CLASS:
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  2565
            Comment dc = token.comment(CommentStyle.JAVADOC);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2566
            return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc));
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2567
        case ENUM:
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2568
            log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.LocalEnum);
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  2569
            dc = token.comment(CommentStyle.JAVADOC);
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  2570
            return List.of(classOrInterfaceOrEnumDeclaration(modifiersOpt(), dc));
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2571
        case IDENTIFIER:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2572
            if (token.name() == names.yield && allowYieldStatement) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2573
                Token next = S.token(1);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2574
                boolean isYieldStatement;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2575
                switch (next.kind) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2576
                    case PLUS: case SUB: case STRINGLITERAL: case CHARLITERAL:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2577
                    case INTLITERAL: case FLOATLITERAL: case DOUBLELITERAL:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2578
                    case NULL: case IDENTIFIER: case TRUE: case FALSE:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2579
                    case NEW: case SWITCH: case THIS: case SUPER:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2580
                        isYieldStatement = true;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2581
                        break;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2582
                    case PLUSPLUS: case SUBSUB:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2583
                        isYieldStatement = S.token(2).kind != SEMI;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2584
                        break;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2585
                    case LPAREN:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2586
                        int lookahead = 2;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2587
                        int balance = 1;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2588
                        boolean hasComma = false;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2589
                        Token l;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2590
                        while ((l = S.token(lookahead)).kind != EOF && balance != 0) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2591
                            switch (l.kind) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2592
                                case LPAREN: balance++; break;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2593
                                case RPAREN: balance--; break;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2594
                                case COMMA: if (balance == 1) hasComma = true; break;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2595
                            }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2596
                            lookahead++;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2597
                        }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2598
                        isYieldStatement = (!hasComma && lookahead != 3) || l.kind == ARROW;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2599
                        break;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2600
                    case SEMI: //error recovery - this is not a valid statement:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2601
                        isYieldStatement = true;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2602
                        break;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2603
                    default:
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2604
                        isYieldStatement = false;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2605
                        break;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2606
                }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2607
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2608
                if (isYieldStatement) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2609
                    nextToken();
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2610
                    JCExpression t = term(EXPR);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2611
                    accept(SEMI);
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2612
                    return List.of(toP(F.at(pos).Yield(t)));
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2613
                }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2614
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2615
                //else intentional fall-through
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2616
            }
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2617
        default:
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2618
            Token prevToken = token;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2619
            JCExpression t = term(EXPR | TYPE);
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2620
            if (token.kind == COLON && t.hasTag(IDENT)) {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2621
                nextToken();
26993
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2622
                JCStatement stat = parseStatementAsBlock();
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2623
                return List.of(F.at(pos).Labelled(prevToken.name(), stat));
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2624
            } 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
  2625
                pos = token.pos;
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2626
                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
  2627
                F.at(pos);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2628
                return localVariableDeclarations(mods, t);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2629
            } else {
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2630
                // 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
  2631
                t = checkExprStat(t);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2632
                accept(SEMI);
25274
a06a738fc6be 8041648: do while loop that misses ending semicolon has wrong end position
jlahoda
parents: 24069
diff changeset
  2633
                JCExpressionStatement expr = toP(F.at(pos).Exec(t));
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2634
                return List.of(expr);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2635
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2636
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2637
    }
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2638
    //where
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2639
        private List<JCStatement> localVariableDeclarations(JCModifiers mods, JCExpression type) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2640
            ListBuffer<JCStatement> stats =
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2641
                    variableDeclarators(mods, type, new ListBuffer<>(), true);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2642
            // A "LocalVariableDeclarationStatement" subsumes the terminating semicolon
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2643
            accept(SEMI);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2644
            storeEnd(stats.last(), S.prevToken().endPos);
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2645
            return stats.toList();
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2646
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2647
06bc494ca11e Initial load
duke
parents:
diff changeset
  2648
    /** Statement =
06bc494ca11e Initial load
duke
parents:
diff changeset
  2649
     *       Block
06bc494ca11e Initial load
duke
parents:
diff changeset
  2650
     *     | IF ParExpression Statement [ELSE Statement]
06bc494ca11e Initial load
duke
parents:
diff changeset
  2651
     *     | FOR "(" ForInitOpt ";" [Expression] ";" ForUpdateOpt ")" Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2652
     *     | FOR "(" FormalParameter : Expression ")" Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2653
     *     | WHILE ParExpression Statement
06bc494ca11e Initial load
duke
parents:
diff changeset
  2654
     *     | DO Statement WHILE ParExpression ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2655
     *     | 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
  2656
     *     | TRY "(" ResourceSpecification ";"opt ")" Block [Catches] [FinallyPart]
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2657
     *     | SWITCH ParExpression "{" SwitchBlockStatementGroups "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2658
     *     | SYNCHRONIZED ParExpression Block
06bc494ca11e Initial load
duke
parents:
diff changeset
  2659
     *     | RETURN [Expression] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2660
     *     | THROW Expression ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2661
     *     | BREAK [Ident] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2662
     *     | CONTINUE [Ident] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2663
     *     | ASSERT Expression [ ":" Expression ] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2664
     *     | ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2665
     */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
  2666
    public JCStatement parseSimpleStatement() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2667
        int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2668
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2669
        case LBRACE:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2670
            return block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2671
        case IF: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2672
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2673
            JCExpression cond = parExpression();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2674
            JCStatement thenpart = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2675
            JCStatement elsepart = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2676
            if (token.kind == ELSE) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2677
                nextToken();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2678
                elsepart = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2679
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2680
            return F.at(pos).If(cond, thenpart, elsepart);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2681
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2682
        case FOR: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2683
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2684
            accept(LPAREN);
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2685
            List<JCStatement> inits = token.kind == SEMI ? List.nil() : forInit();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2686
            if (inits.length() == 1 &&
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  2687
                inits.head.hasTag(VARDEF) &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2688
                ((JCVariableDecl) inits.head).init == null &&
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2689
                token.kind == COLON) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2690
                JCVariableDecl var = (JCVariableDecl)inits.head;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2691
                accept(COLON);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2692
                JCExpression expr = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2693
                accept(RPAREN);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2694
                JCStatement body = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2695
                return F.at(pos).ForeachLoop(var, expr, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2696
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2697
                accept(SEMI);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2698
                JCExpression cond = token.kind == SEMI ? null : parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2699
                accept(SEMI);
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2700
                List<JCExpressionStatement> steps = token.kind == RPAREN ? List.nil() : forUpdate();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2701
                accept(RPAREN);
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2702
                JCStatement body = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2703
                return F.at(pos).ForLoop(inits, cond, steps, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2704
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2705
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2706
        case WHILE: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2707
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2708
            JCExpression cond = parExpression();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2709
            JCStatement body = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2710
            return F.at(pos).WhileLoop(cond, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2711
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2712
        case DO: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2713
            nextToken();
12466
08863ee323df 7156633: (javac) incorrect errors when parsing variable declaration in block statements.
ksrini
parents: 12337
diff changeset
  2714
            JCStatement body = parseStatementAsBlock();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2715
            accept(WHILE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2716
            JCExpression cond = parExpression();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2717
            accept(SEMI);
25274
a06a738fc6be 8041648: do while loop that misses ending semicolon has wrong end position
jlahoda
parents: 24069
diff changeset
  2718
            JCDoWhileLoop t = toP(F.at(pos).DoLoop(body, cond));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2719
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2720
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2721
        case TRY: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2722
            nextToken();
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  2723
            List<JCTree> resources = List.nil();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2724
            if (token.kind == LPAREN) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2725
                nextToken();
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2726
                resources = resources();
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2727
                accept(RPAREN);
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2728
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2729
            JCBlock body = block();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  2730
            ListBuffer<JCCatch> catchers = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2731
            JCBlock finalizer = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2732
            if (token.kind == CATCH || token.kind == FINALLY) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2733
                while (token.kind == CATCH) catchers.append(catchClause());
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2734
                if (token.kind == FINALLY) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2735
                    nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2736
                    finalizer = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2737
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2738
            } else {
40504
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40501
diff changeset
  2739
                if (resources.isEmpty()) {
51047
860a3648c494 8028563: Remove javac support for 6/1.6 source and target values
darcy
parents: 50898
diff changeset
  2740
                    log.error(DiagnosticFlag.SYNTAX, pos, Errors.TryWithoutCatchFinallyOrResourceDecls);
40504
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 40501
diff changeset
  2741
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2742
            }
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  2743
            return F.at(pos).Try(resources, body, catchers.toList(), finalizer);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2744
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2745
        case SWITCH: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2746
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2747
            JCExpression selector = parExpression();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2748
            accept(LBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2749
            List<JCCase> cases = switchBlockStatementGroups();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2750
            JCSwitch t = to(F.at(pos).Switch(selector, cases));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2751
            accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2752
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2753
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2754
        case SYNCHRONIZED: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2755
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2756
            JCExpression lock = parExpression();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2757
            JCBlock body = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2758
            return F.at(pos).Synchronized(lock, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2759
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2760
        case RETURN: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2761
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2762
            JCExpression result = token.kind == SEMI ? null : parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2763
            accept(SEMI);
25274
a06a738fc6be 8041648: do while loop that misses ending semicolon has wrong end position
jlahoda
parents: 24069
diff changeset
  2764
            JCReturn t = toP(F.at(pos).Return(result));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2765
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2766
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2767
        case THROW: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2768
            nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2769
            JCExpression exc = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2770
            accept(SEMI);
25274
a06a738fc6be 8041648: do while loop that misses ending semicolon has wrong end position
jlahoda
parents: 24069
diff changeset
  2771
            JCThrow t = toP(F.at(pos).Throw(exc));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2772
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2773
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2774
        case BREAK: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2775
            nextToken();
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2776
            Name label = LAX_IDENTIFIER.accepts(token.kind) ? ident() : null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2777
            accept(SEMI);
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  2778
            JCBreak t = toP(F.at(pos).Break(label));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2779
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2780
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2781
        case CONTINUE: {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2782
            nextToken();
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2783
            Name label = LAX_IDENTIFIER.accepts(token.kind) ? ident() : null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2784
            accept(SEMI);
25274
a06a738fc6be 8041648: do while loop that misses ending semicolon has wrong end position
jlahoda
parents: 24069
diff changeset
  2785
            JCContinue t =  toP(F.at(pos).Continue(label));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2786
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2787
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2788
        case SEMI:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2789
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2790
            return toP(F.at(pos).Skip());
06bc494ca11e Initial load
duke
parents:
diff changeset
  2791
        case ELSE:
14450
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2792
            int elsePos = token.pos;
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2793
            nextToken();
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2794
            return doRecover(elsePos, BasicErrorRecoveryAction.BLOCK_STMT, Errors.ElseWithoutIf);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2795
        case FINALLY:
14450
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2796
            int finallyPos = token.pos;
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2797
            nextToken();
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2798
            return doRecover(finallyPos, BasicErrorRecoveryAction.BLOCK_STMT, Errors.FinallyWithoutTry);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2799
        case CATCH:
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2800
            return doRecover(token.pos, BasicErrorRecoveryAction.CATCH_CLAUSE, Errors.CatchWithoutTry);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2801
        case ASSERT: {
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  2802
            nextToken();
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  2803
            JCExpression assertion = parseExpression();
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  2804
            JCExpression message = null;
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  2805
            if (token.kind == COLON) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2806
                nextToken();
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  2807
                message = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2808
            }
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  2809
            accept(SEMI);
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  2810
            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
  2811
            return t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2812
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2813
        default:
26993
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2814
            Assert.error();
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2815
            return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2816
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2817
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2818
26993
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2819
    @Override
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2820
    public JCStatement parseStatement() {
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2821
        return parseStatementAsBlock();
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2822
    }
513b2cae81c3 8057652: Request to improve error messages for labeled declarations
jlahoda
parents: 25874
diff changeset
  2823
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2824
    private JCStatement doRecover(int startPos, ErrorRecoveryAction action, Error errorKey) {
14450
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2825
        int errPos = S.errPos();
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2826
        JCTree stm = action.doRecover(this);
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2827
        S.errPos(errPos);
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2828
        return toP(F.Exec(syntaxError(startPos, List.of(stm), errorKey)));
14450
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2829
    }
7a62c5b13d6e 8000484: Bad error recovery when 'catch' without 'try' is found
vromero
parents: 14359
diff changeset
  2830
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2831
    /** CatchClause     = CATCH "(" FormalParameter ")" Block
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2832
     * TODO: the "FormalParameter" is not correct, it uses the special "catchTypes" rule below.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2833
     */
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  2834
    protected JCCatch catchClause() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2835
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2836
        accept(CATCH);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2837
        accept(LPAREN);
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2838
        JCModifiers mods = optFinal(Flags.PARAMETER);
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2839
        List<JCExpression> catchTypes = catchTypes();
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2840
        JCExpression paramType = catchTypes.size() > 1 ?
9300
c2de4dd9853b 7033809: Rename "disjunctive" to "union" in javax.lang.model
darcy
parents: 9073
diff changeset
  2841
                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
  2842
                catchTypes.head;
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2843
        JCVariableDecl formal = variableDeclaratorId(mods, paramType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2844
        accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2845
        JCBlock body = block();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2846
        return F.at(pos).Catch(formal, body);
06bc494ca11e Initial load
duke
parents:
diff changeset
  2847
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2848
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2849
    List<JCExpression> catchTypes() {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19122
diff changeset
  2850
        ListBuffer<JCExpression> catchTypes = new ListBuffer<>();
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2851
        catchTypes.add(parseType());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2852
        while (token.kind == BAR) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2853
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2854
            // Instead of qualident this is now parseType.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2855
            // But would that allow too much, e.g. arrays or generics?
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2856
            catchTypes.add(parseType());
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2857
        }
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2858
        return catchTypes.toList();
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2859
    }
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
  2860
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2861
    /** SwitchBlockStatementGroups = { SwitchBlockStatementGroup }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2862
     *  SwitchBlockStatementGroup = SwitchLabel BlockStatements
06bc494ca11e Initial load
duke
parents:
diff changeset
  2863
     *  SwitchLabel = CASE ConstantExpression ":" | DEFAULT ":"
06bc494ca11e Initial load
duke
parents:
diff changeset
  2864
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2865
    List<JCCase> switchBlockStatementGroups() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  2866
        ListBuffer<JCCase> cases = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2867
        while (true) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2868
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2869
            switch (token.kind) {
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2870
            case CASE:
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2871
            case DEFAULT:
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2872
                cases.appendList(switchBlockStatementGroup());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2873
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2874
            case RBRACE: case EOF:
06bc494ca11e Initial load
duke
parents:
diff changeset
  2875
                return cases.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  2876
            default:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2877
                nextToken(); // to ensure progress
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2878
                syntaxError(pos, Errors.Expected3(CASE, DEFAULT, RBRACE));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2879
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2880
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2881
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2882
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2883
    protected List<JCCase> switchBlockStatementGroup() {
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2884
        int pos = token.pos;
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2885
        List<JCStatement> stats;
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2886
        JCCase c;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2887
        ListBuffer<JCCase> cases = new ListBuffer<JCCase>();
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2888
        switch (token.kind) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2889
        case CASE: {
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2890
            nextToken();
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2891
            ListBuffer<JCExpression> pats = new ListBuffer<>();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2892
            while (true) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2893
                pats.append(term(EXPR | NOLAMBDA));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2894
                if (token.kind != COMMA) break;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2895
                nextToken();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2896
                checkSourceLevel(Feature.SWITCH_MULTIPLE_CASE_LABELS);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2897
            };
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 57724
diff changeset
  2898
            CaseTree.CaseKind caseKind;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2899
            JCTree body = null;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2900
            if (token.kind == ARROW) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2901
                checkSourceLevel(Feature.SWITCH_RULE);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2902
                accept(ARROW);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2903
                caseKind = JCCase.RULE;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2904
                JCStatement statement = parseStatementAsBlock();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2905
                if (!statement.hasTag(EXEC) && !statement.hasTag(BLOCK) && !statement.hasTag(Tag.THROW)) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2906
                    log.error(statement.pos(), Errors.SwitchCaseUnexpectedStatement);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2907
                }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2908
                stats = List.of(statement);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2909
                body = stats.head;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2910
            } else {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2911
                accept(COLON, tk -> Errors.Expected2(COLON, ARROW));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2912
                caseKind = JCCase.STATEMENT;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2913
                stats = blockStatements();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2914
            }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2915
            c = F.at(pos).Case(caseKind, pats.toList(), stats, body);
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2916
            if (stats.isEmpty())
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2917
                storeEnd(c, S.prevToken().endPos);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2918
            return cases.append(c).toList();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2919
        }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2920
        case DEFAULT: {
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2921
            nextToken();
58713
ad69fd32778e 8226585: Improve javac messages for using a preview API
jlahoda
parents: 57724
diff changeset
  2922
            CaseTree.CaseKind caseKind;
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2923
            JCTree body = null;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2924
            if (token.kind == ARROW) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2925
                checkSourceLevel(Feature.SWITCH_RULE);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2926
                accept(ARROW);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2927
                caseKind = JCCase.RULE;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2928
                JCStatement statement = parseStatementAsBlock();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2929
                if (!statement.hasTag(EXEC) && !statement.hasTag(BLOCK) && !statement.hasTag(Tag.THROW)) {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2930
                    log.error(statement.pos(), Errors.SwitchCaseUnexpectedStatement);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2931
                }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2932
                stats = List.of(statement);
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2933
                body = stats.head;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2934
            } else {
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2935
                accept(COLON, tk -> Errors.Expected2(COLON, ARROW));
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2936
                caseKind = JCCase.STATEMENT;
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2937
                stats = blockStatements();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2938
            }
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2939
            c = F.at(pos).Case(caseKind, List.nil(), stats, body);
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2940
            if (stats.isEmpty())
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2941
                storeEnd(c, S.prevToken().endPos);
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2942
            return cases.append(c).toList();
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  2943
        }
14056
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2944
        }
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2945
        throw new AssertionError("should not reach here");
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2946
    }
0ea78d6e0b7b 7198582: (java) Minor refactor of JavacParser
ksrini
parents: 13844
diff changeset
  2947
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2948
    /** MoreStatementExpressions = { COMMA StatementExpression }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2949
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2950
    <T extends ListBuffer<? super JCExpressionStatement>> T moreStatementExpressions(int pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2951
                                                                    JCExpression first,
06bc494ca11e Initial load
duke
parents:
diff changeset
  2952
                                                                    T stats) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2953
        // This Exec is a "StatementExpression"; it subsumes no terminating token
06bc494ca11e Initial load
duke
parents:
diff changeset
  2954
        stats.append(toP(F.at(pos).Exec(checkExprStat(first))));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2955
        while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2956
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2957
            pos = token.pos;
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2958
            JCExpression t = parseExpression();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2959
            // This Exec is a "StatementExpression"; it subsumes no terminating token
06bc494ca11e Initial load
duke
parents:
diff changeset
  2960
            stats.append(toP(F.at(pos).Exec(checkExprStat(t))));
06bc494ca11e Initial load
duke
parents:
diff changeset
  2961
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2962
        return stats;
06bc494ca11e Initial load
duke
parents:
diff changeset
  2963
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2964
06bc494ca11e Initial load
duke
parents:
diff changeset
  2965
    /** ForInit = StatementExpression MoreStatementExpressions
06bc494ca11e Initial load
duke
parents:
diff changeset
  2966
     *           |  { FINAL | '@' Annotation } Type VariableDeclarators
06bc494ca11e Initial load
duke
parents:
diff changeset
  2967
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2968
    List<JCStatement> forInit() {
20249
93f8eae31092 6386236: Please rename com.sun.tools.javac.util.ListBuffer.lb()
alundblad
parents: 19122
diff changeset
  2969
        ListBuffer<JCStatement> stats = new ListBuffer<>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2970
        int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2971
        if (token.kind == FINAL || token.kind == MONKEYS_AT) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2972
            return variableDeclarators(optFinal(0), parseType(true), stats, true).toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2973
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  2974
            JCExpression t = term(EXPR | TYPE);
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  2975
            if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  2976
                return variableDeclarators(modifiersOpt(), t, stats, true).toList();
12715
139b8354de6a 7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents: 12466
diff changeset
  2977
            } else if ((lastmode & TYPE) != 0 && token.kind == COLON) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  2978
                log.error(DiagnosticFlag.SYNTAX, pos, Errors.BadInitializer("for-loop"));
50868
addda6247cb0 8205913: Inconsistent source code model
jlahoda
parents: 50675
diff changeset
  2979
                return List.of((JCStatement)F.at(pos).VarDef(modifiersOpt(), names.error, t, null));
12715
139b8354de6a 7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents: 12466
diff changeset
  2980
            } else {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2981
                return moreStatementExpressions(pos, t, stats).toList();
12715
139b8354de6a 7159445: (javac) emits inaccurate diagnostics for enhanced for-loops
ksrini
parents: 12466
diff changeset
  2982
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2983
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2984
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2985
06bc494ca11e Initial load
duke
parents:
diff changeset
  2986
    /** ForUpdate = StatementExpression MoreStatementExpressions
06bc494ca11e Initial load
duke
parents:
diff changeset
  2987
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  2988
    List<JCExpressionStatement> forUpdate() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2989
        return moreStatementExpressions(token.pos,
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  2990
                                        parseExpression(),
22165
ec53c8946fc2 8030807: langtools should still build using jdk 7
vromero
parents: 22163
diff changeset
  2991
                                        new ListBuffer<JCExpressionStatement>()).toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2992
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  2993
06bc494ca11e Initial load
duke
parents:
diff changeset
  2994
    /** AnnotationsOpt = { '@' Annotation }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2995
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  2996
     * @param kind Whether to parse an ANNOTATION or TYPE_ANNOTATION
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  2997
     */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
  2998
    protected List<JCAnnotation> annotationsOpt(Tag kind) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  2999
        if (token.kind != MONKEYS_AT) return List.nil(); // optimization
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  3000
        ListBuffer<JCAnnotation> buf = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3001
        int prevmode = mode;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3002
        while (token.kind == MONKEYS_AT) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3003
            int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3004
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3005
            buf.append(annotation(pos, kind));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3006
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3007
        lastmode = mode;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3008
        mode = prevmode;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3009
        List<JCAnnotation> annotations = buf.toList();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3010
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3011
        return annotations;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3012
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3013
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3014
    List<JCAnnotation> typeAnnotationsOpt() {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3015
        List<JCAnnotation> annotations = annotationsOpt(Tag.TYPE_ANNOTATION);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3016
        return annotations;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3017
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3018
06bc494ca11e Initial load
duke
parents:
diff changeset
  3019
    /** ModifiersOpt = { Modifier }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3020
     *  Modifier = PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT | FINAL
06bc494ca11e Initial load
duke
parents:
diff changeset
  3021
     *           | NATIVE | SYNCHRONIZED | TRANSIENT | VOLATILE | "@"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3022
     *           | "@" Annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
  3023
     */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
  3024
    protected JCModifiers modifiersOpt() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3025
        return modifiersOpt(null);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3026
    }
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3027
    protected JCModifiers modifiersOpt(JCModifiers partial) {
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3028
        long flags;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  3029
        ListBuffer<JCAnnotation> annotations = new ListBuffer<>();
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3030
        int pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3031
        if (partial == null) {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3032
            flags = 0;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3033
            pos = token.pos;
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3034
        } else {
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3035
            flags = partial.flags;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3036
            annotations.appendList(partial.annotations);
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3037
            pos = partial.pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3038
        }
10948
063463f6535f 7104201: Refactor DocCommentScanner
mcimadamore
parents: 10815
diff changeset
  3039
        if (token.deprecatedFlag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3040
            flags |= Flags.DEPRECATED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3041
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3042
        int lastPos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3043
    loop:
06bc494ca11e Initial load
duke
parents:
diff changeset
  3044
        while (true) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3045
            long flag;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3046
            switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3047
            case PRIVATE     : flag = Flags.PRIVATE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3048
            case PROTECTED   : flag = Flags.PROTECTED; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3049
            case PUBLIC      : flag = Flags.PUBLIC; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3050
            case STATIC      : flag = Flags.STATIC; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3051
            case TRANSIENT   : flag = Flags.TRANSIENT; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3052
            case FINAL       : flag = Flags.FINAL; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3053
            case ABSTRACT    : flag = Flags.ABSTRACT; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3054
            case NATIVE      : flag = Flags.NATIVE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3055
            case VOLATILE    : flag = Flags.VOLATILE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3056
            case SYNCHRONIZED: flag = Flags.SYNCHRONIZED; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3057
            case STRICTFP    : flag = Flags.STRICTFP; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3058
            case MONKEYS_AT  : flag = Flags.ANNOTATION; break;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  3059
            case DEFAULT     : checkSourceLevel(Feature.DEFAULT_METHODS); flag = Flags.DEFAULT; break;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3060
            case ERROR       : flag = 0; nextToken(); break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3061
            default: break loop;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3062
            }
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  3063
            if ((flags & flag) != 0) log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.RepeatedModifier);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3064
            lastPos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3065
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3066
            if (flag == Flags.ANNOTATION) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3067
                if (token.kind != INTERFACE) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3068
                    JCAnnotation ann = annotation(lastPos, Tag.ANNOTATION);
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3069
                    // 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
  3070
                    if (flags == 0 && annotations.isEmpty())
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3071
                        pos = ann.pos;
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3072
                    annotations.append(ann);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3073
                    flag = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3074
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3075
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3076
            flags |= flag;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3077
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3078
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3079
        case ENUM: flags |= Flags.ENUM; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3080
        case INTERFACE: flags |= Flags.INTERFACE; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3081
        default: break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3082
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3083
06bc494ca11e Initial load
duke
parents:
diff changeset
  3084
        /* A modifiers tree with no modifier tokens or annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
  3085
         * has no text position. */
6152
111b884a19a7 6972327: JCTree.pos incorrect for annotations without modifiers and package
jjg
parents: 6148
diff changeset
  3086
        if ((flags & (Flags.ModifierFlags | Flags.ANNOTATION)) == 0 && annotations.isEmpty())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3087
            pos = Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3088
06bc494ca11e Initial load
duke
parents:
diff changeset
  3089
        JCModifiers mods = F.at(pos).Modifiers(flags, annotations.toList());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3090
        if (pos != Position.NOPOS)
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3091
            storeEnd(mods, S.prevToken().endPos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3092
        return mods;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3093
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3094
06bc494ca11e Initial load
duke
parents:
diff changeset
  3095
    /** Annotation              = "@" Qualident [ "(" AnnotationFieldValues ")" ]
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3096
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3097
     * @param pos position of "@" token
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3098
     * @param kind Whether to parse an ANNOTATION or TYPE_ANNOTATION
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3099
     */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3100
    JCAnnotation annotation(int pos, Tag kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3101
        // accept(AT); // AT consumed by caller
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3102
        if (kind == Tag.TYPE_ANNOTATION) {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  3103
            checkSourceLevel(Feature.TYPE_ANNOTATIONS);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3104
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3105
        JCTree ident = qualident(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3106
        List<JCExpression> fieldValues = annotationFieldValuesOpt();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3107
        JCAnnotation ann;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3108
        if (kind == Tag.ANNOTATION) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3109
            ann = F.at(pos).Annotation(ident, fieldValues);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3110
        } else if (kind == Tag.TYPE_ANNOTATION) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3111
            ann = F.at(pos).TypeAnnotation(ident, fieldValues);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3112
        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3113
            throw new AssertionError("Unhandled annotation kind: " + kind);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3114
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3115
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3116
        storeEnd(ann, S.prevToken().endPos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3117
        return ann;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3119
06bc494ca11e Initial load
duke
parents:
diff changeset
  3120
    List<JCExpression> annotationFieldValuesOpt() {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3121
        return (token.kind == LPAREN) ? annotationFieldValues() : List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3123
06bc494ca11e Initial load
duke
parents:
diff changeset
  3124
    /** AnnotationFieldValues   = "(" [ AnnotationFieldValue { "," AnnotationFieldValue } ] ")" */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3125
    List<JCExpression> annotationFieldValues() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3126
        accept(LPAREN);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  3127
        ListBuffer<JCExpression> buf = new ListBuffer<>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3128
        if (token.kind != RPAREN) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3129
            buf.append(annotationFieldValue());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3130
            while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3131
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3132
                buf.append(annotationFieldValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3133
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3134
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3135
        accept(RPAREN);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3136
        return buf.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3137
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3138
06bc494ca11e Initial load
duke
parents:
diff changeset
  3139
    /** AnnotationFieldValue    = AnnotationValue
06bc494ca11e Initial load
duke
parents:
diff changeset
  3140
     *                          | Identifier "=" AnnotationValue
06bc494ca11e Initial load
duke
parents:
diff changeset
  3141
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3142
    JCExpression annotationFieldValue() {
16296
22c10640a521 8007427: Annotation element as '_' gives compiler error instead of a warning
mcimadamore
parents: 15718
diff changeset
  3143
        if (LAX_IDENTIFIER.accepts(token.kind)) {
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  3144
            selectExprMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3145
            JCExpression t1 = term1();
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3146
            if (t1.hasTag(IDENT) && token.kind == EQ) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3147
                int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3148
                accept(EQ);
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3149
                JCExpression v = annotationValue();
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3150
                return toP(F.at(pos).Assign(t1, v));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3151
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3152
                return t1;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3153
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3154
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3155
        return annotationValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3156
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3157
06bc494ca11e Initial load
duke
parents:
diff changeset
  3158
    /* AnnotationValue          = ConditionalExpression
06bc494ca11e Initial load
duke
parents:
diff changeset
  3159
     *                          | Annotation
3997
35250de1d4ea 6337964: should ignore last comma in annotation array
darcy
parents: 3894
diff changeset
  3160
     *                          | "{" [ AnnotationValue { "," AnnotationValue } ] [","] "}"
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3161
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3162
    JCExpression annotationValue() {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3163
        int pos;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3164
        switch (token.kind) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3165
        case MONKEYS_AT:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3166
            pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3167
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3168
            return annotation(pos, Tag.ANNOTATION);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3169
        case LBRACE:
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3170
            pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3171
            accept(LBRACE);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  3172
            ListBuffer<JCExpression> buf = new ListBuffer<>();
18645
4623b7161e9f 8012722: Single comma in array initializer should parse
emc
parents: 17803
diff changeset
  3173
            if (token.kind == COMMA) {
4623b7161e9f 8012722: Single comma in array initializer should parse
emc
parents: 17803
diff changeset
  3174
                nextToken();
4623b7161e9f 8012722: Single comma in array initializer should parse
emc
parents: 17803
diff changeset
  3175
            } else if (token.kind != RBRACE) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3176
                buf.append(annotationValue());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3177
                while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3178
                    nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3179
                    if (token.kind == RBRACE) break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3180
                    buf.append(annotationValue());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3181
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3182
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3183
            accept(RBRACE);
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3184
            return toP(F.at(pos).NewArray(null, List.nil(), buf.toList()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3185
        default:
51563
de411d537aae 8206986: Compiler support for Switch Expressions (Preview)
jlahoda
parents: 51047
diff changeset
  3186
            selectExprMode();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3187
            return term1();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3188
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3189
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3190
06bc494ca11e Initial load
duke
parents:
diff changeset
  3191
    /** VariableDeclarators = VariableDeclarator { "," VariableDeclarator }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3192
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3193
    public <T extends ListBuffer<? super JCVariableDecl>> T variableDeclarators(JCModifiers mods,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3194
                                                                         JCExpression type,
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3195
                                                                         T vdefs,
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3196
                                                                         boolean localDecl)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3197
    {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3198
        return variableDeclaratorsRest(token.pos, mods, type, ident(), false, null, vdefs, localDecl);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3199
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3200
06bc494ca11e Initial load
duke
parents:
diff changeset
  3201
    /** VariableDeclaratorsRest = VariableDeclaratorRest { "," VariableDeclarator }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3202
     *  ConstantDeclaratorsRest = ConstantDeclaratorRest { "," ConstantDeclarator }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3203
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3204
     *  @param reqInit  Is an initializer always required?
06bc494ca11e Initial load
duke
parents:
diff changeset
  3205
     *  @param dc       The documentation comment for the variable declarations, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3206
     */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
  3207
    protected <T extends ListBuffer<? super JCVariableDecl>> T variableDeclaratorsRest(int pos,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3208
                                                                     JCModifiers mods,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3209
                                                                     JCExpression type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3210
                                                                     Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3211
                                                                     boolean reqInit,
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3212
                                                                     Comment dc,
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3213
                                                                     T vdefs,
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3214
                                                                     boolean localDecl)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3215
    {
50675
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3216
        JCVariableDecl head = variableDeclaratorRest(pos, mods, type, name, reqInit, dc, localDecl, false);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3217
        vdefs.append(head);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3218
        while (token.kind == COMMA) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3219
            // 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
  3220
            storeEnd((JCTree)vdefs.last(), token.endPos);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3221
            nextToken();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3222
            vdefs.append(variableDeclarator(mods, type, reqInit, dc, localDecl));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3223
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3224
        return vdefs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3225
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3226
06bc494ca11e Initial load
duke
parents:
diff changeset
  3227
    /** VariableDeclarator = Ident VariableDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  3228
     *  ConstantDeclarator = Ident ConstantDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  3229
     */
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3230
    JCVariableDecl variableDeclarator(JCModifiers mods, JCExpression type, boolean reqInit, Comment dc, boolean localDecl) {
50675
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3231
        return variableDeclaratorRest(token.pos, mods, type, ident(), reqInit, dc, localDecl, true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3232
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3233
06bc494ca11e Initial load
duke
parents:
diff changeset
  3234
    /** VariableDeclaratorRest = BracketsOpt ["=" VariableInitializer]
06bc494ca11e Initial load
duke
parents:
diff changeset
  3235
     *  ConstantDeclaratorRest = BracketsOpt "=" VariableInitializer
06bc494ca11e Initial load
duke
parents:
diff changeset
  3236
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
  3237
     *  @param reqInit  Is an initializer always required?
06bc494ca11e Initial load
duke
parents:
diff changeset
  3238
     *  @param dc       The documentation comment for the variable declarations, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3239
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3240
    JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression type, Name name,
50675
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3241
                                  boolean reqInit, Comment dc, boolean localDecl, boolean compound) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3242
        type = bracketsOpt(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3243
        JCExpression init = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3244
        if (token.kind == EQ) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3245
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3246
            init = variableInitializer();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3247
        }
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  3248
        else if (reqInit) syntaxError(token.pos, Errors.Expected(EQ));
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3249
        JCTree elemType = TreeInfo.innermostType(type, true);
47318
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47268
diff changeset
  3250
        int startPos = Position.NOPOS;
50675
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3251
        if (elemType.hasTag(IDENT)) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3252
            Name typeName = ((JCIdent)elemType).name;
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3253
            if (isRestrictedTypeName(typeName, pos, !compound && localDecl)) {
52622
7a8af2f1f0c5 8210742: compound var declaration type is not uniform for all variables
sdama
parents: 52278
diff changeset
  3254
                if (type.hasTag(TYPEARRAY) && !compound) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3255
                    //error - 'var' and arrays
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3256
                    reportSyntaxError(pos, Errors.RestrictedTypeNotAllowedArray(typeName));
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3257
                } else {
52622
7a8af2f1f0c5 8210742: compound var declaration type is not uniform for all variables
sdama
parents: 52278
diff changeset
  3258
                    if(compound)
7a8af2f1f0c5 8210742: compound var declaration type is not uniform for all variables
sdama
parents: 52278
diff changeset
  3259
                        //error - 'var' in compound local var decl
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3260
                        reportSyntaxError(pos, Errors.RestrictedTypeNotAllowedCompound(typeName));
47318
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47268
diff changeset
  3261
                    startPos = TreeInfo.getStartPos(mods);
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47268
diff changeset
  3262
                    if (startPos == Position.NOPOS)
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47268
diff changeset
  3263
                        startPos = TreeInfo.getStartPos(type);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3264
                    //implicit type
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3265
                    type = null;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3266
                }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3267
            }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3268
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3269
        JCVariableDecl result =
06bc494ca11e Initial load
duke
parents:
diff changeset
  3270
            toP(F.at(pos).VarDef(mods, name, type, init));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3271
        attach(result, dc);
47318
423f5e46016e 8188225: AST could be improved in presence of var types.
jlahoda
parents: 47268
diff changeset
  3272
        result.startPos = startPos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3273
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3274
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3275
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3276
    Name restrictedTypeName(JCExpression e, boolean shouldWarn) {
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3277
        switch (e.getTag()) {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3278
            case IDENT:
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3279
                return isRestrictedTypeName(((JCIdent)e).name, e.pos, shouldWarn) ? ((JCIdent)e).name : null;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3280
            case TYPEARRAY:
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3281
                return restrictedTypeName(((JCArrayTypeTree)e).elemtype, shouldWarn);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3282
            default:
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3283
                return null;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3284
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3285
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3286
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3287
    boolean isRestrictedTypeName(Name name, int pos, boolean shouldWarn) {
50675
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3288
        if (name == names.var) {
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3289
            if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source)) {
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3290
                return true;
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3291
            } else if (shouldWarn) {
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3292
                log.warning(pos, Warnings.RestrictedTypeNotAllowed(name, Source.JDK10));
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3293
            }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3294
        }
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3295
        if (name == names.yield) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3296
            if (allowYieldStatement) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3297
                return true;
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3298
            } else if (shouldWarn) {
59021
cfc7bb9a5a92 8232684: Make switch expressions final
jlahoda
parents: 58713
diff changeset
  3299
                log.warning(pos, Warnings.RestrictedTypeNotAllowed(name, Source.JDK14));
50675
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3300
            }
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3301
        }
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3302
        return false;
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3303
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3304
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3305
    /** VariableDeclaratorId = Ident BracketsOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  3306
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3307
    JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type) {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3308
        return variableDeclaratorId(mods, type, false);
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3309
    }
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3310
    //where
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3311
    JCVariableDecl variableDeclaratorId(JCModifiers mods, JCExpression type, boolean lambdaParameter) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3312
        int pos = token.pos;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3313
        Name name;
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3314
        if (lambdaParameter && token.kind == UNDERSCORE) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 43584
diff changeset
  3315
            log.error(pos, Errors.UnderscoreAsIdentifierInLambda);
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3316
            name = token.name();
20609
dce669916b62 8023679: Improve error message for '_' used as a lambda parameter name
vromero
parents: 20249
diff changeset
  3317
            nextToken();
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3318
        } else {
49543
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3319
            if (allowThisIdent ||
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3320
                !lambdaParameter ||
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3321
                LAX_IDENTIFIER.accepts(token.kind) ||
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3322
                mods.flags != Flags.PARAMETER ||
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3323
                mods.annotations.nonEmpty()) {
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  3324
                JCExpression pn = qualident(false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  3325
                if (pn.hasTag(Tag.IDENT) && ((JCIdent)pn).name != names._this) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  3326
                    name = ((JCIdent)pn).name;
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  3327
                } else {
49543
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3328
                    if (allowThisIdent) {
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3329
                        if ((mods.flags & Flags.VARARGS) != 0) {
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3330
                            log.error(token.pos, Errors.VarargsAndReceiver);
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3331
                        }
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3332
                        if (token.kind == LBRACKET) {
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3333
                            log.error(token.pos, Errors.ArrayAndReceiver);
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3334
                        }
50568
0f807f558017 8203813: javac accepts an illegal name as a receiver parameter name
bsrbnd
parents: 50518
diff changeset
  3335
                        if (pn.hasTag(Tag.SELECT) && ((JCFieldAccess)pn).name != names._this) {
0f807f558017 8203813: javac accepts an illegal name as a receiver parameter name
bsrbnd
parents: 50518
diff changeset
  3336
                            log.error(token.pos, Errors.WrongReceiver);
0f807f558017 8203813: javac accepts an illegal name as a receiver parameter name
bsrbnd
parents: 50518
diff changeset
  3337
                        }
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  3338
                    }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  3339
                    return toP(F.at(pos).ReceiverVarDef(mods, pn, type));
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  3340
                }
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  3341
            } else {
49543
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3342
                /** if it is a lambda parameter and the token kind is not an identifier,
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3343
                 *  and there are no modifiers or annotations, then this means that the compiler
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3344
                 *  supposed the lambda to be explicit but it can contain a mix of implicit,
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3345
                 *  var or explicit parameters. So we assign the error name to the parameter name
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3346
                 *  instead of issuing an error and analyze the lambda parameters as a whole at
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3347
                 *  a higher level.
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3348
                 */
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  3349
                name = names.empty;
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  3350
            }
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3351
        }
8047
c7f08cdb5c3c 6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents: 8033
diff changeset
  3352
        if ((mods.flags & Flags.VARARGS) != 0 &&
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3353
                token.kind == LBRACKET) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 43584
diff changeset
  3354
            log.error(token.pos, Errors.VarargsAndOldArraySyntax);
8047
c7f08cdb5c3c 6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents: 8033
diff changeset
  3355
        }
c7f08cdb5c3c 6569633: Varargs: parser error when varargs element type is an array
mcimadamore
parents: 8033
diff changeset
  3356
        type = bracketsOpt(type);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3357
        return toP(F.at(pos).VarDef(mods, name, type, null));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3358
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3359
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3360
    /** Resources = Resource { ";" Resources }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3361
     */
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3362
    List<JCTree> resources() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  3363
        ListBuffer<JCTree> defs = new ListBuffer<>();
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3364
        defs.append(resource());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3365
        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
  3366
            // 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
  3367
            storeEnd(defs.last(), token.endPos);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3368
            int semiColonPos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3369
            nextToken();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3370
            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
  3371
                                       // after last resource
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  3372
                break;
8f18e1622660 7013420: Project Coin: remove general expression support from try-with-resources statement
darcy
parents: 8047
diff changeset
  3373
            }
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3374
            defs.append(resource());
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3375
        }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3376
        return defs.toList();
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3377
    }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3378
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 26993
diff changeset
  3379
    /** 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
  3380
     *           | Expression
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3381
     */
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3382
    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
  3383
        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
  3384
        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
  3385
            JCModifiers mods = optFinal(Flags.FINAL);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3386
            JCExpression t = parseType(true);
50675
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3387
            return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false);
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 26993
diff changeset
  3388
        }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 26993
diff changeset
  3389
        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
  3390
        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
  3391
            JCModifiers mods = toP(F.at(startPos).Modifiers(Flags.FINAL));
50675
273183fd8246 8195293: Issue more comprehensive warnings for use of \"var\" in earlier source versions
mcimadamore
parents: 50568
diff changeset
  3392
            return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false);
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 26993
diff changeset
  3393
        } else {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  3394
            checkSourceLevel(Feature.EFFECTIVELY_FINAL_VARIABLES_IN_TRY_WITH_RESOURCES);
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 26993
diff changeset
  3395
            if (!t.hasTag(IDENT) && !t.hasTag(SELECT)) {
45504
ea7475564d07 8170326: Inconsistencies between code, compiler.properties and comments
jlahoda
parents: 43584
diff changeset
  3396
                log.error(t.pos(), Errors.TryWithResourcesExprNeedsVar);
27844
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 26993
diff changeset
  3397
            }
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 26993
diff changeset
  3398
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 26993
diff changeset
  3399
            return t;
8b5d79870a2f 7196163: Project Coin: Allow effectively final variables to be used as resources in try-with-resources
jlahoda
parents: 26993
diff changeset
  3400
        }
6148
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3401
    }
3a8158299c51 6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents: 6146
diff changeset
  3402
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3403
    /** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration}
06bc494ca11e Initial load
duke
parents:
diff changeset
  3404
     */
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3405
    public JCTree.JCCompilationUnit parseCompilationUnit() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3406
        Token firstToken = token;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3407
        JCModifiers mods = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3408
        boolean consumedToplevelDoc = false;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3409
        boolean seenImport = false;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3410
        boolean seenPackage = false;
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 24062
diff changeset
  3411
        ListBuffer<JCTree> defs = new ListBuffer<>();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3412
        if (token.kind == MONKEYS_AT)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3413
            mods = modifiersOpt();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3414
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3415
        if (token.kind == PACKAGE) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3416
            int packagePos = token.pos;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3417
            List<JCAnnotation> annotations = List.nil();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3418
            seenPackage = true;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3419
            if (mods != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3420
                checkNoMods(mods.flags);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3421
                annotations = mods.annotations;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3422
                mods = null;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3423
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3424
            nextToken();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3425
            JCExpression pid = qualident(false);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3426
            accept(SEMI);
47234
1b1618daf237 8184739: Incorrect end position of PackageTree
jlahoda
parents: 47216
diff changeset
  3427
            JCPackageDecl pd = toP(F.at(packagePos).PackageDecl(annotations, pid));
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3428
            attach(pd, firstToken.comment(CommentStyle.JAVADOC));
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 24062
diff changeset
  3429
            consumedToplevelDoc = true;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3430
            defs.append(pd);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3431
        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3432
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3433
        boolean checkForImports = true;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3434
        boolean firstTypeDecl = true;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3435
        while (token.kind != EOF) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3436
            if (token.pos <= endPosTable.errorEndPos) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3437
                // error recovery
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  3438
                skip(checkForImports, false, false, false);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3439
                if (token.kind == EOF)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3440
                    break;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3441
            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3442
            if (checkForImports && mods == null && token.kind == IMPORT) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3443
                seenImport = true;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3444
                defs.append(importDeclaration());
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3445
            } else {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3446
                Comment docComment = token.comment(CommentStyle.JAVADOC);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3447
                if (firstTypeDecl && !seenImport && !seenPackage) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3448
                    docComment = firstToken.comment(CommentStyle.JAVADOC);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3449
                    consumedToplevelDoc = true;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3450
                }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3451
                if (mods != null || token.kind != SEMI)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3452
                    mods = modifiersOpt(mods);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3453
                if (firstTypeDecl && token.kind == IDENTIFIER) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3454
                    ModuleKind kind = ModuleKind.STRONG;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3455
                    if (token.name() == names.open) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3456
                        kind = ModuleKind.OPEN;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3457
                        nextToken();
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3458
                    }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3459
                    if (token.kind == IDENTIFIER && token.name() == names.module) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3460
                        if (mods != null) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3461
                            checkNoMods(mods.flags & ~Flags.DEPRECATED);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3462
                        }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3463
                        defs.append(moduleDecl(mods, kind, docComment));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3464
                        consumedToplevelDoc = true;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3465
                        break;
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3466
                    } else if (kind != ModuleKind.STRONG) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  3467
                        reportSyntaxError(token.pos, Errors.ExpectedModule);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3468
                    }
6716
71df48777dd1 6877202: Elements.getDocComment() is not getting JavaDocComments
jjg
parents: 6585
diff changeset
  3469
                }
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3470
                JCTree def = typeDeclaration(mods, docComment);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3471
                if (def instanceof JCExpressionStatement)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3472
                    def = ((JCExpressionStatement)def).expr;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3473
                defs.append(def);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3474
                if (def instanceof JCClassDecl)
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3475
                    checkForImports = false;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3476
                mods = null;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3477
                firstTypeDecl = false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3478
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3479
        }
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 24062
diff changeset
  3480
        JCTree.JCCompilationUnit toplevel = F.at(firstToken.pos).TopLevel(defs.toList());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3481
        if (!consumedToplevelDoc)
10948
063463f6535f 7104201: Refactor DocCommentScanner
mcimadamore
parents: 10815
diff changeset
  3482
            attach(toplevel, firstToken.comment(CommentStyle.JAVADOC));
14803
88347e495d34 8004504: ListBuffer could reuse List.nil() as the sentinel element
jlahoda
parents: 14725
diff changeset
  3483
        if (defs.isEmpty())
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3484
            storeEnd(toplevel, S.prevToken().endPos);
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3485
        if (keepDocComments)
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3486
            toplevel.docComments = docComments;
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3487
        if (keepLineMap)
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3488
            toplevel.lineMap = S.getLineMap();
16811
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  3489
        this.endPosTable.setParser(null); // remove reference to parser
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3490
        toplevel.endPositions = this.endPosTable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3491
        return toplevel;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3492
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3493
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3494
    JCModuleDecl moduleDecl(JCModifiers mods, ModuleKind kind, Comment dc) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3495
        int pos = token.pos;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  3496
        checkSourceLevel(Feature.MODULES);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3497
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3498
        nextToken();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3499
        JCExpression name = qualident(false);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3500
        List<JCDirective> directives = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3501
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3502
        accept(LBRACE);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3503
        directives = moduleDirectiveList();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3504
        accept(RBRACE);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3505
        accept(EOF);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3506
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3507
        JCModuleDecl result = toP(F.at(pos).ModuleDef(mods, kind, name, directives));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3508
        attach(result, dc);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3509
        return result;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3510
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3511
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3512
    List<JCDirective> moduleDirectiveList() {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3513
        ListBuffer<JCDirective> defs = new ListBuffer<>();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3514
        while (token.kind == IDENTIFIER) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3515
            int pos = token.pos;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3516
            if (token.name() == names.requires) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3517
                nextToken();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3518
                boolean isTransitive = false;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3519
                boolean isStaticPhase = false;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3520
            loop:
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3521
                while (true) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3522
                    switch (token.kind) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3523
                        case IDENTIFIER:
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3524
                            if (token.name() == names.transitive && !isTransitive) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3525
                                Token t1 = S.token(1);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3526
                                if (t1.kind == SEMI || t1.kind == DOT) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3527
                                    break loop;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3528
                                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3529
                                isTransitive = true;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3530
                                break;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3531
                            } else {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3532
                                break loop;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3533
                            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3534
                        case STATIC:
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3535
                            if (isStaticPhase) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  3536
                                log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.RepeatedModifier);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3537
                            }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3538
                            isStaticPhase = true;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3539
                            break;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3540
                        default:
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3541
                            break loop;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3542
                    }
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3543
                    nextToken();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3544
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3545
                JCExpression moduleName = qualident(false);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3546
                accept(SEMI);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3547
                defs.append(toP(F.at(pos).Requires(isTransitive, isStaticPhase, moduleName)));
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3548
            } else if (token.name() == names.exports || token.name() == names.opens) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3549
                boolean exports = token.name() == names.exports;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3550
                nextToken();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3551
                JCExpression pkgName = qualident(false);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3552
                List<JCExpression> moduleNames = null;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3553
                if (token.kind == IDENTIFIER && token.name() == names.to) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3554
                    nextToken();
38916
d118cb2cffab 8158123: NPE when the annotations is used in export-to of module-info
shinyafox
parents: 37635
diff changeset
  3555
                    moduleNames = qualidentList(false);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3556
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3557
                accept(SEMI);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3558
                JCDirective d;
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3559
                if (exports) {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3560
                    d = F.at(pos).Exports(pkgName, moduleNames);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3561
                } else {
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3562
                    d = F.at(pos).Opens(pkgName, moduleNames);
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3563
                }
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3564
                defs.append(toP(d));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3565
            } else if (token.name() == names.provides) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3566
                nextToken();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3567
                JCExpression serviceName = qualident(false);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3568
                if (token.kind == IDENTIFIER && token.name() == names.with) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3569
                    nextToken();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3570
                    List<JCExpression> implNames = qualidentList(false);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3571
                    accept(SEMI);
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 40504
diff changeset
  3572
                    defs.append(toP(F.at(pos).Provides(serviceName, implNames)));
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3573
                } else {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  3574
                    log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.ExpectedStr("'" + names.with + "'"));
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  3575
                    skip(false, false, false, false);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3576
                }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3577
            } else if (token.name() == names.uses) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3578
                nextToken();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3579
                JCExpression service = qualident(false);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3580
                accept(SEMI);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3581
                defs.append(toP(F.at(pos).Uses(service)));
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3582
            } else {
40501
8455b59aa04a 8157519: Error messages when compiling a malformed module-info.java confusing
sadayapalam
parents: 38916
diff changeset
  3583
                setErrorEndPos(pos);
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  3584
                reportSyntaxError(pos, Errors.InvalidModuleDirective);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3585
                break;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3586
            }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3587
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3588
        return defs.toList();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3589
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 33361
diff changeset
  3590
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3591
    /** ImportDeclaration = IMPORT [ STATIC ] Ident { "." Ident } [ "." "*" ] ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3592
     */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
  3593
    protected JCTree importDeclaration() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3594
        int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3595
        nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3596
        boolean importStatic = false;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3597
        if (token.kind == STATIC) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3598
            importStatic = true;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3599
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3600
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3601
        JCExpression pid = toP(F.at(token.pos).Ident(ident()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3602
        do {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3603
            int pos1 = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3604
            accept(DOT);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3605
            if (token.kind == STAR) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3606
                pid = to(F.at(pos1).Select(pid, names.asterisk));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3607
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3608
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3609
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3610
                pid = toP(F.at(pos1).Select(pid, ident()));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3611
            }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3612
        } while (token.kind == DOT);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3613
        accept(SEMI);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3614
        return toP(F.at(pos).Import(pid, importStatic));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3615
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3616
06bc494ca11e Initial load
duke
parents:
diff changeset
  3617
    /** TypeDeclaration = ClassOrInterfaceOrEnumDeclaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3618
     *                  | ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3619
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3620
    JCTree typeDeclaration(JCModifiers mods, Comment docComment) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3621
        int pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3622
        if (mods == null && token.kind == SEMI) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3623
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3624
            return toP(F.at(pos).Skip());
06bc494ca11e Initial load
duke
parents:
diff changeset
  3625
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3626
            return classOrInterfaceOrEnumDeclaration(modifiersOpt(mods), docComment);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3627
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3628
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3629
06bc494ca11e Initial load
duke
parents:
diff changeset
  3630
    /** ClassOrInterfaceOrEnumDeclaration = ModifiersOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  3631
     *           (ClassDeclaration | InterfaceDeclaration | EnumDeclaration)
06bc494ca11e Initial load
duke
parents:
diff changeset
  3632
     *  @param mods     Any modifiers starting the class or interface declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3633
     *  @param dc       The documentation comment for the class, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3634
     */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
  3635
    protected JCStatement classOrInterfaceOrEnumDeclaration(JCModifiers mods, Comment dc) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3636
        if (token.kind == CLASS) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3637
            return classDeclaration(mods, dc);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3638
        } else if (token.kind == INTERFACE) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3639
            return interfaceDeclaration(mods, dc);
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  3640
        } else if (token.kind == ENUM) {
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  3641
            return enumDeclaration(mods, dc);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3642
        } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3643
            int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3644
            List<JCTree> errs;
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  3645
            if (LAX_IDENTIFIER.accepts(token.kind)) {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3646
                errs = List.of(mods, toP(F.at(pos).Ident(ident())));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3647
                setErrorEndPos(token.pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3648
            } else {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3649
                errs = List.of(mods);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3650
            }
43584
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
  3651
            final JCErroneous erroneousTree;
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
  3652
            if (parseModuleInfo) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  3653
                erroneousTree = syntaxError(pos, errs, Errors.ExpectedModuleOrOpen);
43584
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
  3654
            } else {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  3655
                erroneousTree = syntaxError(pos, errs, Errors.Expected3(CLASS, INTERFACE, ENUM));
43584
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
  3656
            }
63e67712246b 8166420: Confusing error message when reading bad module declaration
sadayapalam
parents: 43261
diff changeset
  3657
            return toP(F.Exec(erroneousTree));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3658
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3659
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3660
06bc494ca11e Initial load
duke
parents:
diff changeset
  3661
    /** ClassDeclaration = CLASS Ident TypeParametersOpt [EXTENDS Type]
06bc494ca11e Initial load
duke
parents:
diff changeset
  3662
     *                     [IMPLEMENTS TypeList] ClassBody
06bc494ca11e Initial load
duke
parents:
diff changeset
  3663
     *  @param mods    The modifiers starting the class declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3664
     *  @param dc       The documentation comment for the class, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3665
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3666
    protected JCClassDecl classDeclaration(JCModifiers mods, Comment dc) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3667
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3668
        accept(CLASS);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3669
        Name name = typeName();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3670
06bc494ca11e Initial load
duke
parents:
diff changeset
  3671
        List<JCTypeParameter> typarams = typeParametersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3672
8625
6b51ef804d49 6639645: Modeling type implementing missing interfaces
jjg
parents: 8234
diff changeset
  3673
        JCExpression extending = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3674
        if (token.kind == EXTENDS) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3675
            nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3676
            extending = parseType();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3677
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3678
        List<JCExpression> implementing = List.nil();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3679
        if (token.kind == IMPLEMENTS) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3680
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3681
            implementing = typeList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3682
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3683
        List<JCTree> defs = classOrInterfaceBody(name, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3684
        JCClassDecl result = toP(F.at(pos).ClassDef(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3685
            mods, name, typarams, extending, implementing, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3686
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3687
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3688
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3689
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3690
    Name typeName() {
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3691
        int pos = token.pos;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3692
        Name name = ident();
55306
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3693
        if (isRestrictedTypeName(name, pos, true)) {
ea43db53de91 8223305: Compiler support for Switch Expressions
jlahoda
parents: 54900
diff changeset
  3694
            reportSyntaxError(pos, Errors.RestrictedTypeNotAllowed(name, name == names.var ? Source.JDK10 : Source.JDK13));
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3695
        }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3696
        return name;
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3697
    }
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3698
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3699
    /** InterfaceDeclaration = INTERFACE Ident TypeParametersOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  3700
     *                         [EXTENDS TypeList] InterfaceBody
06bc494ca11e Initial load
duke
parents:
diff changeset
  3701
     *  @param mods    The modifiers starting the interface declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3702
     *  @param dc       The documentation comment for the interface, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3703
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3704
    protected JCClassDecl interfaceDeclaration(JCModifiers mods, Comment dc) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3705
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3706
        accept(INTERFACE);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3707
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3708
        Name name = typeName();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3709
06bc494ca11e Initial load
duke
parents:
diff changeset
  3710
        List<JCTypeParameter> typarams = typeParametersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3711
06bc494ca11e Initial load
duke
parents:
diff changeset
  3712
        List<JCExpression> extending = List.nil();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3713
        if (token.kind == EXTENDS) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3714
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3715
            extending = typeList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3716
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3717
        List<JCTree> defs = classOrInterfaceBody(name, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3718
        JCClassDecl result = toP(F.at(pos).ClassDef(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3719
            mods, name, typarams, null, extending, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3720
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3721
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3722
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3723
06bc494ca11e Initial load
duke
parents:
diff changeset
  3724
    /** EnumDeclaration = ENUM Ident [IMPLEMENTS TypeList] EnumBody
06bc494ca11e Initial load
duke
parents:
diff changeset
  3725
     *  @param mods    The modifiers starting the enum declaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3726
     *  @param dc       The documentation comment for the enum, or null.
06bc494ca11e Initial load
duke
parents:
diff changeset
  3727
     */
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3728
    protected JCClassDecl enumDeclaration(JCModifiers mods, Comment dc) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3729
        int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3730
        accept(ENUM);
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3731
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3732
        Name name = typeName();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3733
06bc494ca11e Initial load
duke
parents:
diff changeset
  3734
        List<JCExpression> implementing = List.nil();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3735
        if (token.kind == IMPLEMENTS) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3736
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3737
            implementing = typeList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3738
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3739
06bc494ca11e Initial load
duke
parents:
diff changeset
  3740
        List<JCTree> defs = enumBody(name);
4870
a132763160d7 6919889: assorted position errors in compiler syntax trees
jjg
parents: 4704
diff changeset
  3741
        mods.flags |= Flags.ENUM;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3742
        JCClassDecl result = toP(F.at(pos).
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3743
            ClassDef(mods, name, List.nil(),
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  3744
                     null, implementing, defs));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3745
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3746
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3747
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3748
06bc494ca11e Initial load
duke
parents:
diff changeset
  3749
    /** EnumBody = "{" { EnumeratorDeclarationList } [","]
06bc494ca11e Initial load
duke
parents:
diff changeset
  3750
     *                  [ ";" {ClassBodyDeclaration} ] "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3751
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3752
    List<JCTree> enumBody(Name enumName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3753
        accept(LBRACE);
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  3754
        ListBuffer<JCTree> defs = new ListBuffer<>();
57724
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3755
        boolean wasSemi = false;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3756
        boolean hasStructuralErrors = false;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3757
        boolean wasError = false;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3758
        if (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3759
            nextToken();
57724
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3760
            if (token.kind == SEMI) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3761
                wasSemi = true;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3762
                nextToken();
57724
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3763
            } else if (token.kind != RBRACE) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3764
                reportSyntaxError(S.prevToken().endPos,
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3765
                                  Errors.Expected2(RBRACE, SEMI));
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3766
                wasError = true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3767
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3768
        }
57724
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3769
        while (token.kind != RBRACE && token.kind != EOF) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3770
            if (token.kind == SEMI) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3771
                accept(SEMI);
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3772
                wasSemi = true;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3773
                if (token.kind == RBRACE || token.kind == EOF) break;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3774
            }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3775
            EnumeratorEstimate memberType = estimateEnumeratorOrMember(enumName);
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3776
            if (memberType == EnumeratorEstimate.UNKNOWN) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3777
                memberType = wasSemi ? EnumeratorEstimate.MEMBER
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3778
                                     : EnumeratorEstimate.ENUMERATOR;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3779
            }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3780
            if (memberType == EnumeratorEstimate.ENUMERATOR) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3781
                wasError = false;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3782
                if (wasSemi && !hasStructuralErrors) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3783
                    reportSyntaxError(token.pos, Errors.EnumConstantNotExpected);
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3784
                    hasStructuralErrors = true;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3785
                }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3786
                defs.append(enumeratorDeclaration(enumName));
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3787
                if (token.pos <= endPosTable.errorEndPos) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3788
                    // error recovery
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3789
                   skip(false, true, true, false);
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3790
                } else {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3791
                    if (token.kind != RBRACE && token.kind != SEMI && token.kind != EOF) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3792
                        if (token.kind == COMMA) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3793
                            nextToken();
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3794
                        } else {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3795
                            setErrorEndPos(token.pos);
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3796
                            reportSyntaxError(S.prevToken().endPos,
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3797
                                              Errors.Expected3(COMMA, RBRACE, SEMI));
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3798
                            wasError = true;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3799
                        }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3800
                    }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3801
                }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3802
            } else {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3803
                if (!wasSemi && !hasStructuralErrors && !wasError) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3804
                    reportSyntaxError(token.pos, Errors.EnumConstantExpected);
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3805
                    hasStructuralErrors = true;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3806
                }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3807
                wasError = false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3808
                defs.appendList(classOrInterfaceBodyDeclaration(enumName,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3809
                                                                false));
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3810
                if (token.pos <= endPosTable.errorEndPos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3811
                    // error recovery
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  3812
                   skip(false, true, true, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3813
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3814
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3815
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3816
        accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3817
        return defs.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3818
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3819
57724
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3820
    private EnumeratorEstimate estimateEnumeratorOrMember(Name enumName) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3821
        if (token.kind == TokenKind.IDENTIFIER && token.name() != enumName) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3822
            Token next = S.token(1);
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3823
            switch (next.kind) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3824
                case LPAREN: case LBRACE: case COMMA: case SEMI:
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3825
                    return EnumeratorEstimate.ENUMERATOR;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3826
            }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3827
        }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3828
        switch (token.kind) {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3829
            case IDENTIFIER: case MONKEYS_AT: case LT:
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3830
                return EnumeratorEstimate.UNKNOWN;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3831
            default:
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3832
                return EnumeratorEstimate.MEMBER;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3833
        }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3834
    }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3835
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3836
    private enum EnumeratorEstimate {
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3837
        ENUMERATOR,
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3838
        MEMBER,
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3839
        UNKNOWN;
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3840
    }
447d48371b41 8228647: Broken enum produce inconvenient errors and AST
jlahoda
parents: 55561
diff changeset
  3841
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3842
    /** EnumeratorDeclaration = AnnotationsOpt [TypeArguments] IDENTIFIER [ Arguments ] [ "{" ClassBody "}" ]
06bc494ca11e Initial load
duke
parents:
diff changeset
  3843
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3844
    JCTree enumeratorDeclaration(Name enumName) {
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3845
        Comment dc = token.comment(CommentStyle.JAVADOC);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3846
        int flags = Flags.PUBLIC|Flags.STATIC|Flags.FINAL|Flags.ENUM;
10948
063463f6535f 7104201: Refactor DocCommentScanner
mcimadamore
parents: 10815
diff changeset
  3847
        if (token.deprecatedFlag()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3848
            flags |= Flags.DEPRECATED;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3849
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3850
        int pos = token.pos;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3851
        List<JCAnnotation> annotations = annotationsOpt(Tag.ANNOTATION);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3852
        JCModifiers mods = F.at(annotations.isEmpty() ? Position.NOPOS : pos).Modifiers(flags, annotations);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3853
        List<JCExpression> typeArgs = typeArgumentsOpt();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3854
        int identPos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3855
        Name name = ident();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3856
        int createPos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3857
        List<JCExpression> args = (token.kind == LPAREN)
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3858
            ? arguments() : List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3859
        JCClassDecl body = null;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3860
        if (token.kind == LBRACE) {
23803
98c4ebaddefc 8034044: Class.getModifiers() returns "static" for anonymous classes
igerasim
parents: 23800
diff changeset
  3861
            JCModifiers mods1 = F.at(Position.NOPOS).Modifiers(Flags.ENUM);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3862
            List<JCTree> defs = classOrInterfaceBody(names.empty, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3863
            body = toP(F.at(identPos).AnonymousClassDef(mods1, defs));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3864
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3865
        if (args.isEmpty() && body == null)
4704
5206047418c2 6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents: 3997
diff changeset
  3866
            createPos = identPos;
5206047418c2 6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents: 3997
diff changeset
  3867
        JCIdent ident = F.at(identPos).Ident(enumName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3868
        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
  3869
        if (createPos != identPos)
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3870
            storeEnd(create, S.prevToken().endPos);
4704
5206047418c2 6472751: SourcePositions.getStartPos returns incorrect value for enum constants
jjg
parents: 3997
diff changeset
  3871
        ident = F.at(identPos).Ident(enumName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3872
        JCTree result = toP(F.at(pos).VarDef(mods, name, ident, create));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3873
        attach(result, dc);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3874
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
  3875
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3876
06bc494ca11e Initial load
duke
parents:
diff changeset
  3877
    /** TypeList = Type {"," Type}
06bc494ca11e Initial load
duke
parents:
diff changeset
  3878
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3879
    List<JCExpression> typeList() {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  3880
        ListBuffer<JCExpression> ts = new ListBuffer<>();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3881
        ts.append(parseType());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3882
        while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3883
            nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  3884
            ts.append(parseType());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3885
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3886
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3887
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3888
06bc494ca11e Initial load
duke
parents:
diff changeset
  3889
    /** ClassBody     = "{" {ClassBodyDeclaration} "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3890
     *  InterfaceBody = "{" {InterfaceBodyDeclaration} "}"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3891
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  3892
    List<JCTree> classOrInterfaceBody(Name className, boolean isInterface) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  3893
        accept(LBRACE);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3894
        if (token.pos <= endPosTable.errorEndPos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3895
            // error recovery
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  3896
            skip(false, true, false, false);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3897
            if (token.kind == LBRACE)
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3898
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3899
        }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  3900
        ListBuffer<JCTree> defs = new ListBuffer<>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3901
        while (token.kind != RBRACE && token.kind != EOF) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3902
            defs.appendList(classOrInterfaceBodyDeclaration(className, isInterface));
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  3903
            if (token.pos <= endPosTable.errorEndPos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3904
               // error recovery
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  3905
               skip(false, true, true, false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3906
           }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3907
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3908
        accept(RBRACE);
06bc494ca11e Initial load
duke
parents:
diff changeset
  3909
        return defs.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  3910
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  3911
06bc494ca11e Initial load
duke
parents:
diff changeset
  3912
    /** ClassBodyDeclaration =
06bc494ca11e Initial load
duke
parents:
diff changeset
  3913
     *      ";"
06bc494ca11e Initial load
duke
parents:
diff changeset
  3914
     *    | [STATIC] Block
06bc494ca11e Initial load
duke
parents:
diff changeset
  3915
     *    | ModifiersOpt
06bc494ca11e Initial load
duke
parents:
diff changeset
  3916
     *      ( Type Ident
06bc494ca11e Initial load
duke
parents:
diff changeset
  3917
     *        ( VariableDeclaratorsRest ";" | MethodDeclaratorRest )
24062
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3918
     *      | VOID Ident VoidMethodDeclaratorRest
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3919
     *      | TypeParameters [Annotations]
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3920
     *        ( Type Ident MethodDeclaratorRest
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3921
     *        | VOID Ident VoidMethodDeclaratorRest
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3922
     *        )
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3923
     *      | Ident ConstructorDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  3924
     *      | TypeParameters Ident ConstructorDeclaratorRest
06bc494ca11e Initial load
duke
parents:
diff changeset
  3925
     *      | ClassOrInterfaceOrEnumDeclaration
06bc494ca11e Initial load
duke
parents:
diff changeset
  3926
     *      )
06bc494ca11e Initial load
duke
parents:
diff changeset
  3927
     *  InterfaceBodyDeclaration =
06bc494ca11e Initial load
duke
parents:
diff changeset
  3928
     *      ";"
24062
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3929
     *    | ModifiersOpt
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3930
     *      ( Type Ident
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3931
     *        ( ConstantDeclaratorsRest ";" | MethodDeclaratorRest )
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3932
     *      | VOID Ident MethodDeclaratorRest
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3933
     *      | TypeParameters [Annotations]
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3934
     *        ( Type Ident MethodDeclaratorRest
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3935
     *        | VOID Ident VoidMethodDeclaratorRest
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3936
     *        )
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3937
     *      | ClassOrInterfaceOrEnumDeclaration
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3938
     *      )
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3939
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3940
     */
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  3941
    protected List<JCTree> classOrInterfaceBodyDeclaration(Name className, boolean isInterface) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3942
        if (token.kind == SEMI) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3943
            nextToken();
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3944
            return List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3945
        } else {
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  3946
            Comment dc = token.comment(CommentStyle.JAVADOC);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3947
            int pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3948
            JCModifiers mods = modifiersOpt();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3949
            if (token.kind == CLASS ||
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3950
                token.kind == INTERFACE ||
25445
603f0c93d5c9 8011044: Remove support for 1.5 and earlier source and target options
ntoda
parents: 25274
diff changeset
  3951
                token.kind == ENUM) {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3952
                return List.of(classOrInterfaceOrEnumDeclaration(mods, dc));
23133
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
  3953
            } else if (token.kind == LBRACE &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3954
                       (mods.flags & Flags.StandardFlags & ~Flags.STATIC) == 0 &&
06bc494ca11e Initial load
duke
parents:
diff changeset
  3955
                       mods.annotations.isEmpty()) {
23133
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
  3956
                if (isInterface) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  3957
                    log.error(DiagnosticFlag.SYNTAX, token.pos, Errors.InitializerNotAllowed);
23133
a58476887b3d 8031383: Error recovery in JavacParser could be improved
jlahoda
parents: 23131
diff changeset
  3958
                }
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  3959
                return List.of(block(pos, mods.flags));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3960
            } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3961
                pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3962
                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
  3963
                // 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
  3964
                // 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
  3965
                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
  3966
                    mods.pos = pos;
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  3967
                    storeEnd(mods, pos);
13d9c18b8fa1 6993305: starting position of a method without modifiers and with type parameters is incorrect
jjg
parents: 8032
diff changeset
  3968
                }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3969
                List<JCAnnotation> annosAfterParams = annotationsOpt(Tag.ANNOTATION);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3970
24062
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3971
                if (annosAfterParams.nonEmpty()) {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  3972
                    checkSourceLevel(annosAfterParams.head.pos, Feature.ANNOTATIONS_AFTER_TYPE_PARAMS);
24062
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3973
                    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
  3974
                    if (mods.pos == Position.NOPOS)
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3975
                        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
  3976
                }
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3977
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3978
                Token tk = token;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3979
                pos = token.pos;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3980
                JCExpression type;
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3981
                boolean isVoid = token.kind == VOID;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3982
                if (isVoid) {
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  3983
                    type = to(F.at(pos).TypeIdent(TypeTag.VOID));
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3984
                    nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3985
                } else {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  3986
                    // method returns types are un-annotated types
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  3987
                    type = unannotatedType(false);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3988
                }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  3989
                if (token.kind == LPAREN && !isInterface && type.hasTag(IDENT)) {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3990
                    if (isInterface || tk.name() != className)
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  3991
                        log.error(DiagnosticFlag.SYNTAX, pos, Errors.InvalidMethDeclRetTypeReq);
24062
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3992
                    else if (annosAfterParams.nonEmpty())
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  3993
                        illegal(annosAfterParams.head.pos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  3994
                    return List.of(methodDeclaratorRest(
06bc494ca11e Initial load
duke
parents:
diff changeset
  3995
                        pos, mods, null, names.init, typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  3996
                        isInterface, true, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  3997
                } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3998
                    pos = token.pos;
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  3999
                    Name name = ident();
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4000
                    if (token.kind == LPAREN) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4001
                        return List.of(methodDeclaratorRest(
06bc494ca11e Initial load
duke
parents:
diff changeset
  4002
                            pos, mods, type, name, typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4003
                            isInterface, isVoid, dc));
06bc494ca11e Initial load
duke
parents:
diff changeset
  4004
                    } else if (!isVoid && typarams.isEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4005
                        List<JCTree> defs =
06bc494ca11e Initial load
duke
parents:
diff changeset
  4006
                            variableDeclaratorsRest(pos, mods, type, name, isInterface, dc,
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  4007
                                                    new ListBuffer<JCTree>(), false).toList();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4008
                        accept(SEMI);
25274
a06a738fc6be 8041648: do while loop that misses ending semicolon has wrong end position
jlahoda
parents: 24069
diff changeset
  4009
                        storeEnd(defs.last(), S.prevToken().endPos);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4010
                        return defs;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4011
                    } else {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4012
                        pos = token.pos;
50898
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  4013
                        List<JCTree> err;
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  4014
                        if (isVoid || typarams.nonEmpty()) {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  4015
                            JCMethodDecl m =
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  4016
                                    toP(F.at(pos).MethodDef(mods, name, type, typarams,
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  4017
                                                            List.nil(), List.nil(), null, null));
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  4018
                            attach(m, dc);
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  4019
                            err = List.of(m);
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  4020
                        } else {
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  4021
                            err = List.nil();
12133a6e2613 8205418: Assorted improvements to source code model
jlahoda
parents: 50868
diff changeset
  4022
                        }
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  4023
                        return List.of(syntaxError(token.pos, err, Errors.Expected(LPAREN)));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4024
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4025
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4026
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4027
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4028
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4029
06bc494ca11e Initial load
duke
parents:
diff changeset
  4030
    /** MethodDeclaratorRest =
24062
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  4031
     *      FormalParameters BracketsOpt [THROWS TypeList] ( MethodBody | [DEFAULT AnnotationValue] ";")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4032
     *  VoidMethodDeclaratorRest =
24062
09047fd2a43e 8038788: javac behaves incorrectly for annotations after method type parameters in some cases
jlahoda
parents: 23803
diff changeset
  4033
     *      FormalParameters [THROWS TypeList] ( MethodBody | ";")
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4034
     *  ConstructorDeclaratorRest =
7072
4863847e93a5 6987760: remove 308 support from JDK7
jjg
parents: 6716
diff changeset
  4035
     *      "(" FormalParameterListOpt ")" [THROWS TypeList] MethodBody
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4036
     */
11147
20770e90d993 7119032: (javac) increase visibility of JavacParser methods to improve subtyping
ksrini
parents: 11144
diff changeset
  4037
    protected JCTree methodDeclaratorRest(int pos,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4038
                              JCModifiers mods,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4039
                              JCExpression type,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4040
                              Name name,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4041
                              List<JCTypeParameter> typarams,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4042
                              boolean isInterface, boolean isVoid,
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12715
diff changeset
  4043
                              Comment dc) {
29293
1583c6dd6df7 8071453: Allow interface methods to be private
sadayapalam
parents: 28587
diff changeset
  4044
        if (isInterface) {
1583c6dd6df7 8071453: Allow interface methods to be private
sadayapalam
parents: 28587
diff changeset
  4045
            if ((mods.flags & Flags.STATIC) != 0) {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  4046
                checkSourceLevel(Feature.STATIC_INTERFACE_METHODS);
29293
1583c6dd6df7 8071453: Allow interface methods to be private
sadayapalam
parents: 28587
diff changeset
  4047
            }
1583c6dd6df7 8071453: Allow interface methods to be private
sadayapalam
parents: 28587
diff changeset
  4048
            if ((mods.flags & Flags.PRIVATE) != 0) {
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  4049
                checkSourceLevel(Feature.PRIVATE_INTERFACE_METHODS);
29293
1583c6dd6df7 8071453: Allow interface methods to be private
sadayapalam
parents: 28587
diff changeset
  4050
            }
15377
515846bb6637 8005166: Add support for static interface methods
mcimadamore
parents: 15375
diff changeset
  4051
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4052
        JCVariableDecl prevReceiverParam = this.receiverParam;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4053
        try {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4054
            this.receiverParam = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4055
            // Parsing formalParameters sets the receiverParam, if present
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4056
            List<JCVariableDecl> params = formalParameters();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4057
            if (!isVoid) type = bracketsOpt(type);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4058
            List<JCExpression> thrown = List.nil();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4059
            if (token.kind == THROWS) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4060
                nextToken();
38916
d118cb2cffab 8158123: NPE when the annotations is used in export-to of module-info
shinyafox
parents: 37635
diff changeset
  4061
                thrown = qualidentList(true);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4062
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4063
            JCBlock body = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4064
            JCExpression defaultValue;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4065
            if (token.kind == LBRACE) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4066
                body = block();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4067
                defaultValue = null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4068
            } else {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4069
                if (token.kind == DEFAULT) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4070
                    accept(DEFAULT);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4071
                    defaultValue = annotationValue();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4072
                } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4073
                    defaultValue = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4074
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4075
                accept(SEMI);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4076
                if (token.pos <= endPosTable.errorEndPos) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4077
                    // error recovery
48926
02404e27d356 8198479: JDK build is broken by 8194892
vromero
parents: 48920
diff changeset
  4078
                    skip(false, true, false, false);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4079
                    if (token.kind == LBRACE) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4080
                        body = block();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4081
                    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4082
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4083
            }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4084
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4085
            JCMethodDecl result =
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4086
                    toP(F.at(pos).MethodDef(mods, name, type, typarams,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4087
                                            receiverParam, params, thrown,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4088
                                            body, defaultValue));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4089
            attach(result, dc);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4090
            return result;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4091
        } finally {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4092
            this.receiverParam = prevReceiverParam;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4093
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4094
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4095
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4096
    /** QualidentList = [Annotations] Qualident {"," [Annotations] Qualident}
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4097
     */
38916
d118cb2cffab 8158123: NPE when the annotations is used in export-to of module-info
shinyafox
parents: 37635
diff changeset
  4098
    List<JCExpression> qualidentList(boolean allowAnnos) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  4099
        ListBuffer<JCExpression> ts = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4100
38916
d118cb2cffab 8158123: NPE when the annotations is used in export-to of module-info
shinyafox
parents: 37635
diff changeset
  4101
        List<JCAnnotation> typeAnnos = allowAnnos ? typeAnnotationsOpt() : List.nil();
d118cb2cffab 8158123: NPE when the annotations is used in export-to of module-info
shinyafox
parents: 37635
diff changeset
  4102
        JCExpression qi = qualident(allowAnnos);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4103
        if (!typeAnnos.isEmpty()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4104
            JCExpression at = insertAnnotationsToMostInner(qi, typeAnnos, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4105
            ts.append(at);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4106
        } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4107
            ts.append(qi);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4108
        }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4109
        while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4110
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4111
38916
d118cb2cffab 8158123: NPE when the annotations is used in export-to of module-info
shinyafox
parents: 37635
diff changeset
  4112
            typeAnnos = allowAnnos ? typeAnnotationsOpt() : List.nil();
d118cb2cffab 8158123: NPE when the annotations is used in export-to of module-info
shinyafox
parents: 37635
diff changeset
  4113
            qi = qualident(allowAnnos);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4114
            if (!typeAnnos.isEmpty()) {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4115
                JCExpression at = insertAnnotationsToMostInner(qi, typeAnnos, false);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4116
                ts.append(at);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4117
            } else {
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4118
                ts.append(qi);
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4119
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4120
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4121
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4123
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  4124
    /**
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  4125
     *  {@literal
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  4126
     *  TypeParametersOpt = ["<" TypeParameter {"," TypeParameter} ">"]
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  4127
     *  }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4128
     */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
  4129
    protected List<JCTypeParameter> typeParametersOpt() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4130
        if (token.kind == LT) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  4131
            ListBuffer<JCTypeParameter> typarams = new ListBuffer<>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4132
            nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4133
            typarams.append(typeParameter());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4134
            while (token.kind == COMMA) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4135
                nextToken();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4136
                typarams.append(typeParameter());
06bc494ca11e Initial load
duke
parents:
diff changeset
  4137
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4138
            accept(GT);
06bc494ca11e Initial load
duke
parents:
diff changeset
  4139
            return typarams.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4140
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4141
            return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4142
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4144
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  4145
    /**
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  4146
     *  {@literal
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4147
     *  TypeParameter = [Annotations] TypeVariable [TypeParameterBound]
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4148
     *  TypeParameterBound = EXTENDS Type {"&" Type}
06bc494ca11e Initial load
duke
parents:
diff changeset
  4149
     *  TypeVariable = Ident
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 13439
diff changeset
  4150
     *  }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4151
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4152
    JCTypeParameter typeParameter() {
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4153
        int pos = token.pos;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4154
        List<JCAnnotation> annos = typeAnnotationsOpt();
47268
48ec75306997 8177466: Add compiler support for local variable type-inference
mcimadamore
parents: 47234
diff changeset
  4155
        Name name = typeName();
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  4156
        ListBuffer<JCExpression> bounds = new ListBuffer<>();
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4157
        if (token.kind == EXTENDS) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4158
            nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  4159
            bounds.append(parseType());
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4160
            while (token.kind == AMP) {
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4161
                nextToken();
1258
1cf37d8837d1 6724118: change JavaCompiler to not use Scanner directly
jjg
parents: 939
diff changeset
  4162
                bounds.append(parseType());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4163
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4164
        }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4165
        return toP(F.at(pos).TypeParameter(name, bounds.toList(), annos));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4166
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4167
06bc494ca11e Initial load
duke
parents:
diff changeset
  4168
    /** FormalParameters = "(" [ FormalParameterList ] ")"
06bc494ca11e Initial load
duke
parents:
diff changeset
  4169
     *  FormalParameterList = [ FormalParameterListNovarargs , ] LastFormalParameter
06bc494ca11e Initial load
duke
parents:
diff changeset
  4170
     *  FormalParameterListNovarargs = [ FormalParameterListNovarargs , ] FormalParameter
06bc494ca11e Initial load
duke
parents:
diff changeset
  4171
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4172
    List<JCVariableDecl> formalParameters() {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  4173
        return formalParameters(false);
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  4174
    }
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  4175
    List<JCVariableDecl> formalParameters(boolean lambdaParameters) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  4176
        ListBuffer<JCVariableDecl> params = new ListBuffer<>();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4177
        JCVariableDecl lastParam;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4178
        accept(LPAREN);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4179
        if (token.kind != RPAREN) {
49543
2d5d75263e77 8199744: Incorrect compiler message for ReceiverParameter in inner class constructor
vromero
parents: 49152
diff changeset
  4180
            this.allowThisIdent = !lambdaParameters;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4181
            lastParam = formalParameter(lambdaParameters);
17578
46ac954e4a84 8013852: update reference impl for type-annotations
jjg
parents: 16811
diff changeset
  4182
            if (lastParam.nameexpr != null) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4183
                this.receiverParam = lastParam;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4184
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4185
                params.append(lastParam);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4186
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4187
            this.allowThisIdent = false;
22701
67811024e8b4 8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents: 22165
diff changeset
  4188
            while (token.kind == COMMA) {
67811024e8b4 8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents: 22165
diff changeset
  4189
                if ((lastParam.mods.flags & Flags.VARARGS) != 0) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  4190
                    log.error(DiagnosticFlag.SYNTAX, lastParam, Errors.VarargsMustBeLast);
22701
67811024e8b4 8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents: 22165
diff changeset
  4191
                }
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4192
                nextToken();
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  4193
                params.append(lastParam = formalParameter(lambdaParameters));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4194
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4195
        }
22701
67811024e8b4 8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents: 22165
diff changeset
  4196
        if (token.kind == RPAREN) {
67811024e8b4 8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents: 22165
diff changeset
  4197
            nextToken();
67811024e8b4 8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents: 22165
diff changeset
  4198
        } else {
67811024e8b4 8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents: 22165
diff changeset
  4199
            setErrorEndPos(token.pos);
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  4200
            reportSyntaxError(S.prevToken().endPos, Errors.Expected3(COMMA, RPAREN, LBRACKET));
22701
67811024e8b4 8030091: Request to update error messages from javac for negative varargs test cases
jlahoda
parents: 22165
diff changeset
  4201
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4202
        return params.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4203
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4204
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4205
    List<JCVariableDecl> implicitParameters(boolean hasParens) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4206
        if (hasParens) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4207
            accept(LPAREN);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4208
        }
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22155
diff changeset
  4209
        ListBuffer<JCVariableDecl> params = new ListBuffer<>();
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4210
        if (token.kind != RPAREN && token.kind != ARROW) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4211
            params.append(implicitParameter());
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4212
            while (token.kind == COMMA) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4213
                nextToken();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4214
                params.append(implicitParameter());
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4215
            }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4216
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4217
        if (hasParens) {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4218
            accept(RPAREN);
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4219
        }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4220
        return params.toList();
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4221
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4222
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4223
    JCModifiers optFinal(long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4224
        JCModifiers mods = modifiersOpt();
06bc494ca11e Initial load
duke
parents:
diff changeset
  4225
        checkNoMods(mods.flags & ~(Flags.FINAL | Flags.DEPRECATED));
06bc494ca11e Initial load
duke
parents:
diff changeset
  4226
        mods.flags |= flags;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4227
        return mods;
06bc494ca11e Initial load
duke
parents:
diff changeset
  4228
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4229
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4230
    /**
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4231
     * Inserts the annotations (and possibly a new array level)
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4232
     * to the left-most type in an array or nested type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4233
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4234
     * When parsing a type like {@code @B Outer.Inner @A []}, the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4235
     * {@code @A} annotation should target the array itself, while
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4236
     * {@code @B} targets the nested type {@code Outer}.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4237
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4238
     * Currently the parser parses the annotation first, then
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4239
     * the array, and then inserts the annotation to the left-most
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4240
     * nested type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4241
     *
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4242
     * When {@code createNewLevel} is true, then a new array
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4243
     * level is inserted as the most inner type, and have the
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4244
     * annotations target it.  This is useful in the case of
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4245
     * varargs, e.g. {@code String @A [] @B ...}, as the parser
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4246
     * first parses the type {@code String @A []} then inserts
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4247
     * a new array level with {@code @B} annotation.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4248
     */
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4249
    private JCExpression insertAnnotationsToMostInner(
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4250
            JCExpression type, List<JCAnnotation> annos,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4251
            boolean createNewLevel) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4252
        int origEndPos = getEndPos(type);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4253
        JCExpression mostInnerType = type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4254
        JCArrayTypeTree mostInnerArrayType = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4255
        while (TreeInfo.typeIn(mostInnerType).hasTag(TYPEARRAY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4256
            mostInnerArrayType = (JCArrayTypeTree) TreeInfo.typeIn(mostInnerType);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4257
            mostInnerType = mostInnerArrayType.elemtype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4258
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4259
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4260
        if (createNewLevel) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4261
            mostInnerType = to(F.at(token.pos).TypeArray(mostInnerType));
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4262
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4263
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4264
        JCExpression mostInnerTypeToReturn = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4265
        if (annos.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4266
            JCExpression lastToModify = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4267
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4268
            while (TreeInfo.typeIn(mostInnerType).hasTag(SELECT) ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4269
                    TreeInfo.typeIn(mostInnerType).hasTag(TYPEAPPLY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4270
                while (TreeInfo.typeIn(mostInnerType).hasTag(SELECT)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4271
                    lastToModify = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4272
                    mostInnerType = ((JCFieldAccess) TreeInfo.typeIn(mostInnerType)).getExpression();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4273
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4274
                while (TreeInfo.typeIn(mostInnerType).hasTag(TYPEAPPLY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4275
                    lastToModify = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4276
                    mostInnerType = ((JCTypeApply) TreeInfo.typeIn(mostInnerType)).clazz;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4277
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4278
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4279
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4280
            mostInnerType = F.at(annos.head.pos).AnnotatedType(annos, mostInnerType);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4281
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4282
            if (TreeInfo.typeIn(lastToModify).hasTag(TYPEAPPLY)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4283
                ((JCTypeApply) TreeInfo.typeIn(lastToModify)).clazz = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4284
            } else if (TreeInfo.typeIn(lastToModify).hasTag(SELECT)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4285
                ((JCFieldAccess) TreeInfo.typeIn(lastToModify)).selected = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4286
            } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4287
                // We never saw a SELECT or TYPEAPPLY, return the annotated type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4288
                mostInnerTypeToReturn = mostInnerType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4289
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4290
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4291
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4292
        if (mostInnerArrayType == null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4293
            return mostInnerTypeToReturn;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4294
        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4295
            mostInnerArrayType.elemtype = mostInnerTypeToReturn;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4296
            storeEnd(type, origEndPos);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4297
            return type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4298
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4299
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4300
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4301
    /** FormalParameter = { FINAL | '@' Annotation } Type VariableDeclaratorId
06bc494ca11e Initial load
duke
parents:
diff changeset
  4302
     *  LastFormalParameter = { FINAL | '@' Annotation } Type '...' Ident | FormalParameter
06bc494ca11e Initial load
duke
parents:
diff changeset
  4303
     */
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  4304
    protected JCVariableDecl formalParameter() {
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  4305
        return formalParameter(false);
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  4306
    }
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  4307
    protected JCVariableDecl formalParameter(boolean lambdaParameter) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4308
        JCModifiers mods = optFinal(Flags.PARAMETER);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4309
        // need to distinguish between vararg annos and array annos
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4310
        // look at typeAnnotationsPushedBack comment
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4311
        this.permitTypeAnnotationsPushBack = true;
48935
03ae177c26b0 8198512: compiler support for local-variable syntax for lambda parameters
vromero
parents: 48926
diff changeset
  4312
        JCExpression type = parseType(lambdaParameter);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4313
        this.permitTypeAnnotationsPushBack = false;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4314
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4315
        if (token.kind == ELLIPSIS) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4316
            List<JCAnnotation> varargsAnnos = typeAnnotationsPushedBack;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4317
            typeAnnotationsPushedBack = List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4318
            mods.flags |= Flags.VARARGS;
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4319
            // insert var arg type annotations
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4320
            type = insertAnnotationsToMostInner(type, varargsAnnos, true);
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4321
            nextToken();
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4322
        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4323
            // if not a var arg, then typeAnnotationsPushedBack should be null
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4324
            if (typeAnnotationsPushedBack.nonEmpty()) {
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  4325
                reportSyntaxError(typeAnnotationsPushedBack.head.pos, Errors.IllegalStartOfType);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4326
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15377
diff changeset
  4327
            typeAnnotationsPushedBack = List.nil();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4328
        }
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  4329
        return variableDeclaratorId(mods, type, lambdaParameter);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4330
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4331
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4332
    protected JCVariableDecl implicitParameter() {
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4333
        JCModifiers mods = F.at(token.pos).Modifiers(Flags.PARAMETER);
15367
31b57f2b8d0b 8005852: Treatment of '_' as identifier
mcimadamore
parents: 14803
diff changeset
  4334
        return variableDeclaratorId(mods, null, true);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4335
    }
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4336
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4337
/* ---------- auxiliary methods -------------- */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4338
    /** Check that given tree is a legal expression statement.
06bc494ca11e Initial load
duke
parents:
diff changeset
  4339
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
  4340
    protected JCExpression checkExprStat(JCExpression t) {
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  4341
        if (!TreeInfo.isExpressionStatement(t)) {
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  4342
            JCExpression ret = F.at(t.pos).Erroneous(List.<JCTree>of(t));
48721
ef3557eb4306 8196403: remove the remaining use of string keys for errors and warnings in the compiler
vromero
parents: 48551
diff changeset
  4343
            log.error(DiagnosticFlag.SYNTAX, ret, Errors.NotStmt);
10455
3d070be0fff8 7073631: (javac) javac parser improvements for error position reporting
ksrini
parents: 10200
diff changeset
  4344
            return ret;
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  4345
        } else {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14547
diff changeset
  4346
            return t;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4347
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4348
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4349
06bc494ca11e Initial load
duke
parents:
diff changeset
  4350
    /** Return precedence of operator represented by token,
06bc494ca11e Initial load
duke
parents:
diff changeset
  4351
     *  -1 if token is not a binary operator. @see TreeInfo.opPrec
06bc494ca11e Initial load
duke
parents:
diff changeset
  4352
     */
10815
a719aa5f1631 7096014: Javac tokens should retain state
mcimadamore
parents: 10455
diff changeset
  4353
    static int prec(TokenKind token) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4354
        JCTree.Tag oc = optag(token);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4355
        return (oc != NO_TAG) ? TreeInfo.opPrec(oc) : -1;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4356
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4357
5013
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4358
    /**
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4359
     * Return the lesser of two positions, making allowance for either one
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4360
     * being unset.
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4361
     */
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4362
    static int earlier(int pos1, int pos2) {
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4363
        if (pos1 == Position.NOPOS)
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4364
            return pos2;
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4365
        if (pos2 == Position.NOPOS)
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4366
            return pos1;
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4367
        return (pos1 < pos2 ? pos1 : pos2);
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4368
    }
e942b2e52479 6931927: position issues with synthesized anonymous class
jjg
parents: 4870
diff changeset
  4369
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4370
    /** 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
  4371
     *  No_TAG if token is not a binary operator.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4372
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4373
    static JCTree.Tag optag(TokenKind token) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4374
        switch (token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4375
        case BARBAR:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4376
            return OR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4377
        case AMPAMP:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4378
            return AND;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4379
        case BAR:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4380
            return BITOR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4381
        case BAREQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4382
            return BITOR_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4383
        case CARET:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4384
            return BITXOR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4385
        case CARETEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4386
            return BITXOR_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4387
        case AMP:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4388
            return BITAND;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4389
        case AMPEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4390
            return BITAND_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4391
        case EQEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4392
            return JCTree.Tag.EQ;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4393
        case BANGEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4394
            return NE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4395
        case LT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4396
            return JCTree.Tag.LT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4397
        case GT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4398
            return JCTree.Tag.GT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4399
        case LTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4400
            return LE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4401
        case GTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4402
            return GE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4403
        case LTLT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4404
            return SL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4405
        case LTLTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4406
            return SL_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4407
        case GTGT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4408
            return SR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4409
        case GTGTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4410
            return SR_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4411
        case GTGTGT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4412
            return USR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4413
        case GTGTGTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4414
            return USR_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4415
        case PLUS:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4416
            return JCTree.Tag.PLUS;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4417
        case PLUSEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4418
            return PLUS_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4419
        case SUB:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4420
            return MINUS;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4421
        case SUBEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4422
            return MINUS_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4423
        case STAR:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4424
            return MUL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4425
        case STAREQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4426
            return MUL_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4427
        case SLASH:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4428
            return DIV;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4429
        case SLASHEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4430
            return DIV_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4431
        case PERCENT:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4432
            return MOD;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4433
        case PERCENTEQ:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4434
            return MOD_ASG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4435
        case INSTANCEOF:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4436
            return TYPETEST;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4437
        default:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4438
            return NO_TAG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4439
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4440
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4441
06bc494ca11e Initial load
duke
parents:
diff changeset
  4442
    /** 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
  4443
     *  No_TAG if token is not a binary operator.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4444
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4445
    static JCTree.Tag unoptag(TokenKind token) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4446
        switch (token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4447
        case PLUS:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4448
            return POS;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4449
        case SUB:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4450
            return NEG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4451
        case BANG:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4452
            return NOT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4453
        case TILDE:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4454
            return COMPL;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4455
        case PLUSPLUS:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4456
            return PREINC;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4457
        case SUBSUB:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4458
            return PREDEC;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4459
        default:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 10948
diff changeset
  4460
            return NO_TAG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4461
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4462
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4463
06bc494ca11e Initial load
duke
parents:
diff changeset
  4464
    /** 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
  4465
     *  NONE if token is not a basic type identifier.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4466
     */
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4467
    static TypeTag typetag(TokenKind token) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4468
        switch (token) {
06bc494ca11e Initial load
duke
parents:
diff changeset
  4469
        case BYTE:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4470
            return TypeTag.BYTE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4471
        case CHAR:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4472
            return TypeTag.CHAR;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4473
        case SHORT:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4474
            return TypeTag.SHORT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4475
        case INT:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4476
            return TypeTag.INT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4477
        case LONG:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4478
            return TypeTag.LONG;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4479
        case FLOAT:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4480
            return TypeTag.FLOAT;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4481
        case DOUBLE:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4482
            return TypeTag.DOUBLE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4483
        case BOOLEAN:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4484
            return TypeTag.BOOLEAN;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4485
        default:
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
  4486
            return TypeTag.NONE;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4487
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4488
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
  4489
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  4490
    void checkSourceLevel(Feature feature) {
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  4491
        checkSourceLevel(token.pos, feature);
11143
9dbe313bfb74 7115050: Add parser support for lambda expressions
mcimadamore
parents: 11055
diff changeset
  4492
    }
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  4493
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  4494
    protected void checkSourceLevel(int pos, Feature feature) {
49580
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
  4495
        if (preview.isPreview(feature) && !preview.isEnabled()) {
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
  4496
            //preview feature without --preview flag, error
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
  4497
            log.error(DiagnosticFlag.SOURCE_LEVEL, pos, preview.disabledError(feature));
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
  4498
        } else if (!feature.allowedInSource(source)) {
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
  4499
            //incompatible source level, error
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47936
diff changeset
  4500
            log.error(DiagnosticFlag.SOURCE_LEVEL, pos, feature.error(source.name));
49580
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
  4501
        } else if (preview.isPreview(feature)) {
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
  4502
            //use of preview feature, warn
62b908c9f0e6 8199194: Add javac support for preview features
mcimadamore
parents: 49543
diff changeset
  4503
            preview.warnPreview(pos, feature);
23800
f7ffcfe938f2 8035890: jdk8 javac -source 7 compiles test case it should not
jlahoda
parents: 23133
diff changeset
  4504
        }
f7ffcfe938f2 8035890: jdk8 javac -source 7 compiles test case it should not
jlahoda
parents: 23133
diff changeset
  4505
    }
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4506
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4507
    /*
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4508
     * a functional source tree and end position mappings
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4509
     */
16811
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4510
    protected static class SimpleEndPosTable extends AbstractEndPosTable {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4511
23131
90aee246c017 8033287: Reduce the size of the endPosTable
jjg
parents: 22701
diff changeset
  4512
        private final IntHashTable endPosMap;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4513
16811
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4514
        SimpleEndPosTable(JavacParser parser) {
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4515
            super(parser);
23131
90aee246c017 8033287: Reduce the size of the endPosTable
jjg
parents: 22701
diff changeset
  4516
            endPosMap = new IntHashTable();
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4517
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4518
19122
1841f2fa76de 8017216: javac doesn't fill in end position for some errors of type not found
ksrini
parents: 18645
diff changeset
  4519
        public void storeEnd(JCTree tree, int endpos) {
23131
90aee246c017 8033287: Reduce the size of the endPosTable
jjg
parents: 22701
diff changeset
  4520
            endPosMap.putAtIndex(tree, errorEndPos > endpos ? errorEndPos : endpos,
90aee246c017 8033287: Reduce the size of the endPosTable
jjg
parents: 22701
diff changeset
  4521
                                 endPosMap.lookup(tree));
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4522
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4523
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4524
        protected <T extends JCTree> T to(T t) {
16811
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4525
            storeEnd(t, parser.token.endPos);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4526
            return t;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4527
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4528
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4529
        protected <T extends JCTree> T toP(T t) {
16811
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4530
            storeEnd(t, parser.S.prevToken().endPos);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4531
            return t;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4532
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4533
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4534
        public int getEndPos(JCTree tree) {
23131
90aee246c017 8033287: Reduce the size of the endPosTable
jjg
parents: 22701
diff changeset
  4535
            int value = endPosMap.getFromIndex(endPosMap.lookup(tree));
90aee246c017 8033287: Reduce the size of the endPosTable
jjg
parents: 22701
diff changeset
  4536
            // As long as Position.NOPOS==-1, this just returns value.
90aee246c017 8033287: Reduce the size of the endPosTable
jjg
parents: 22701
diff changeset
  4537
            return (value == -1) ? Position.NOPOS : value;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4538
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4539
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4540
        public int replaceTree(JCTree oldTree, JCTree newTree) {
23131
90aee246c017 8033287: Reduce the size of the endPosTable
jjg
parents: 22701
diff changeset
  4541
            int pos = endPosMap.remove(oldTree);
90aee246c017 8033287: Reduce the size of the endPosTable
jjg
parents: 22701
diff changeset
  4542
            if (pos != -1) {
90aee246c017 8033287: Reduce the size of the endPosTable
jjg
parents: 22701
diff changeset
  4543
                storeEnd(newTree, pos);
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4544
                return pos;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4545
            }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4546
            return Position.NOPOS;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4547
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4548
    }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4549
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4550
    /*
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4551
     * a default skeletal implementation without any mapping overhead.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4552
     */
16811
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4553
    protected static class EmptyEndPosTable extends AbstractEndPosTable {
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4554
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4555
        EmptyEndPosTable(JavacParser parser) {
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4556
            super(parser);
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4557
        }
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4558
19122
1841f2fa76de 8017216: javac doesn't fill in end position for some errors of type not found
ksrini
parents: 18645
diff changeset
  4559
        public void storeEnd(JCTree tree, int endpos) { /* empty */ }
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4560
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4561
        protected <T extends JCTree> T to(T t) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4562
            return t;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4563
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4564
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4565
        protected <T extends JCTree> T toP(T t) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4566
            return t;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4567
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4568
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4569
        public int getEndPos(JCTree tree) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4570
            return Position.NOPOS;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4571
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4572
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4573
        public int replaceTree(JCTree oldTree, JCTree newTree) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4574
            return Position.NOPOS;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4575
        }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4576
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4577
    }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4578
16811
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4579
    protected static abstract class AbstractEndPosTable implements EndPosTable {
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4580
        /**
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4581
         * The current parser.
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4582
         */
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4583
        protected JavacParser parser;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4584
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4585
        /**
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4586
         * Store the last error position.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4587
         */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
  4588
        public int errorEndPos = Position.NOPOS;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4589
16811
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4590
        public AbstractEndPosTable(JavacParser parser) {
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4591
            this.parser = parser;
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4592
        }
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4593
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4594
        /**
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4595
         * 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
  4596
         * 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
  4597
         * the current token.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4598
         * @param t The tree.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4599
         */
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4600
        protected abstract <T extends JCTree> T to(T t);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4601
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4602
        /**
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4603
         * 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
  4604
         * 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
  4605
         * the previous token.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4606
         * @param t The tree.
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4607
         */
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4608
        protected abstract <T extends JCTree> T toP(T t);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4609
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4610
        /**
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4611
         * Set the error position during the parsing phases, the value of which
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4612
         * 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
  4613
         * @param errPos The error position
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4614
         */
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
  4615
        public void setErrorEndPos(int errPos) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4616
            if (errPos > errorEndPos) {
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4617
                errorEndPos = errPos;
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4618
            }
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4619
        }
16811
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4620
28147
72e7fdc79b78 8067384: Facilitate extension of the javac parser
rfield
parents: 27991
diff changeset
  4621
        public void setParser(JavacParser parser) {
16811
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4622
            this.parser = parser;
fca5473a5823 8011677: EndPosTables should avoid hidden references to Parser
jjg
parents: 16296
diff changeset
  4623
        }
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
  4624
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  4625
}