8296322: javac: use methods to manage parser mode flags

Reviewed-by: mcimadamore
This commit is contained in:
Jim Laskey 2023-02-11 00:24:24 +00:00
parent 84e47996e8
commit 98e98e9049

View File

@ -241,19 +241,35 @@ public class JavacParser implements Parser {
* mode = TYPEARG : type argument
* mode |= NOLAMBDA : lambdas are not allowed
*/
protected static final int EXPR = 0x1;
protected static final int TYPE = 0x2;
protected static final int NOPARAMS = 0x4;
protected static final int TYPEARG = 0x8;
protected static final int DIAMOND = 0x10;
protected static final int NOLAMBDA = 0x20;
protected static final int EXPR = 1 << 0;
protected static final int TYPE = 1 << 1;
protected static final int NOPARAMS = 1 << 2;
protected static final int TYPEARG = 1 << 3;
protected static final int DIAMOND = 1 << 4;
protected static final int NOLAMBDA = 1 << 5;
protected void setMode(int mode) {
this.mode = mode;
}
protected void setLastMode(int mode) {
lastmode = mode;
}
protected boolean isMode(int mode) {
return (this.mode & mode) != 0;
}
protected boolean wasTypeMode() {
return (lastmode & TYPE) != 0;
}
protected void selectExprMode() {
mode = (mode & NOLAMBDA) | EXPR;
setMode((mode & NOLAMBDA) | EXPR);
}
protected void selectTypeMode() {
mode = (mode & NOLAMBDA) | TYPE;
setMode((mode & NOLAMBDA) | TYPE);
}
/** The current mode.
@ -477,7 +493,7 @@ public class JavacParser implements Parser {
*/
JCExpression illegal(int pos) {
setErrorEndPos(pos);
if ((mode & EXPR) != 0)
if (isMode(EXPR))
return syntaxError(pos, Errors.IllegalStartOfExpr);
else
return syntaxError(pos, Errors.IllegalStartOfType);
@ -857,10 +873,10 @@ public class JavacParser implements Parser {
protected JCExpression term(int newmode) {
int prevmode = mode;
mode = newmode;
setMode(newmode);
JCExpression t = term();
lastmode = mode;
mode = prevmode;
setLastMode(mode);
setMode(prevmode);
return t;
}
@ -879,7 +895,7 @@ public class JavacParser implements Parser {
*/
JCExpression term() {
JCExpression t = term1();
if ((mode & EXPR) != 0 &&
if (isMode(EXPR) &&
(token.kind == EQ || PLUSEQ.compareTo(token.kind) <= 0 && token.kind.compareTo(GTGTGTEQ) <= 0))
return termRest(t);
else
@ -923,7 +939,7 @@ public class JavacParser implements Parser {
*/
JCExpression term1() {
JCExpression t = term2();
if ((mode & EXPR) != 0 && token.kind == QUES) {
if (isMode(EXPR) && token.kind == QUES) {
selectExprMode();
return term1Rest(t);
} else {
@ -952,7 +968,7 @@ public class JavacParser implements Parser {
*/
JCExpression term2() {
JCExpression t = term3();
if ((mode & EXPR) != 0 && prec(token.kind) >= TreeInfo.orPrec) {
if (isMode(EXPR) && prec(token.kind) >= TreeInfo.orPrec) {
selectExprMode();
return term2Rest(t, TreeInfo.orPrec);
} else {
@ -1177,13 +1193,13 @@ public class JavacParser implements Parser {
List<JCExpression> typeArgs = typeArgumentsOpt(EXPR);
switch (token.kind) {
case QUES:
if ((mode & TYPE) != 0 && (mode & (TYPEARG|NOPARAMS)) == TYPEARG) {
if (isMode(TYPE) && isMode(TYPEARG) && !isMode(NOPARAMS)) {
selectTypeMode();
return typeArgument();
} else
return illegal();
case PLUSPLUS: case SUBSUB: case BANG: case TILDE: case PLUS: case SUB:
if (typeArgs == null && (mode & EXPR) != 0) {
if (typeArgs == null && isMode(EXPR)) {
TokenKind tk = token.kind;
nextToken();
selectExprMode();
@ -1199,7 +1215,7 @@ public class JavacParser implements Parser {
} else return illegal();
break;
case LPAREN:
if (typeArgs == null && (mode & EXPR) != 0) {
if (typeArgs == null && isMode(EXPR)) {
ParensResult pres = analyzeParens();
switch (pres) {
case CAST:
@ -1235,7 +1251,7 @@ public class JavacParser implements Parser {
}
break;
case THIS:
if ((mode & EXPR) != 0) {
if (isMode(EXPR)) {
selectExprMode();
t = to(F.at(pos).Ident(names._this));
nextToken();
@ -1247,7 +1263,7 @@ public class JavacParser implements Parser {
} else return illegal();
break;
case SUPER:
if ((mode & EXPR) != 0) {
if (isMode(EXPR)) {
selectExprMode();
t = to(F.at(pos).Ident(names._super));
t = superSuffix(typeArgs, t);
@ -1257,14 +1273,14 @@ public class JavacParser implements Parser {
case INTLITERAL: case LONGLITERAL: case FLOATLITERAL: case DOUBLELITERAL:
case CHARLITERAL: case STRINGLITERAL:
case TRUE: case FALSE: case NULL:
if (typeArgs == null && (mode & EXPR) != 0) {
if (typeArgs == null && isMode(EXPR)) {
selectExprMode();
t = literal(names.empty);
} else return illegal();
break;
case NEW:
if (typeArgs != null) return illegal();
if ((mode & EXPR) != 0) {
if (isMode(EXPR)) {
selectExprMode();
nextToken();
if (token.kind == LT) typeArgs = typeArguments(false);
@ -1282,7 +1298,7 @@ public class JavacParser implements Parser {
JCExpression expr = term3();
if ((mode & TYPE) == 0) {
if (!isMode(TYPE)) {
// Type annotations on class literals no longer legal
switch (expr.getTag()) {
case REFERENCE: {
@ -1312,7 +1328,7 @@ public class JavacParser implements Parser {
break;
case UNDERSCORE: case IDENTIFIER: case ASSERT: case ENUM:
if (typeArgs != null) return illegal();
if ((mode & EXPR) != 0 && (mode & NOLAMBDA) == 0 && peekToken(ARROW)) {
if (isMode(EXPR) && !isMode(NOLAMBDA) && peekToken(ARROW)) {
t = lambdaExpressionOrStatement(false, false, pos);
} else {
t = toP(F.at(token.pos).Ident(ident()));
@ -1337,7 +1353,7 @@ public class JavacParser implements Parser {
}
t = bracketsSuffix(t);
} else {
if ((mode & EXPR) != 0) {
if (isMode(EXPR)) {
selectExprMode();
JCExpression t1 = term();
if (!annos.isEmpty()) t = illegal(annos.head.pos);
@ -1347,7 +1363,7 @@ public class JavacParser implements Parser {
}
break loop;
case LPAREN:
if ((mode & EXPR) != 0) {
if (isMode(EXPR)) {
selectExprMode();
t = arguments(typeArgs, t);
if (!annos.isEmpty()) t = illegal(annos.head.pos);
@ -1359,11 +1375,11 @@ public class JavacParser implements Parser {
if (token.kind == TokenKind.IDENTIFIER && typeArgs != null) {
return illegal();
}
int oldmode = mode;
mode &= ~NOPARAMS;
int prevmode = mode;
setMode(mode & ~NOPARAMS);
typeArgs = typeArgumentsOpt(EXPR);
mode = oldmode;
if ((mode & EXPR) != 0) {
setMode(prevmode);
if (isMode(EXPR)) {
switch (token.kind) {
case CLASS:
if (typeArgs != null) return illegal();
@ -1396,7 +1412,7 @@ public class JavacParser implements Parser {
}
List<JCAnnotation> tyannos = null;
if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) {
if (isMode(TYPE) && token.kind == MONKEYS_AT) {
tyannos = typeAnnotationsOpt();
}
// typeArgs saved for next loop iteration.
@ -1422,7 +1438,7 @@ public class JavacParser implements Parser {
}
break loop;
case LT:
if ((mode & TYPE) == 0 && isUnboundMemberRef()) {
if (!isMode(TYPE) && isUnboundMemberRef()) {
//this is an unbound method reference whose qualifier
//is a generic type i.e. A<S>::m
int pos1 = token.pos;
@ -1465,7 +1481,7 @@ public class JavacParser implements Parser {
break;
case VOID:
if (typeArgs != null) illegal();
if ((mode & EXPR) != 0) {
if (isMode(EXPR)) {
nextToken();
if (token.kind == DOT) {
JCPrimitiveTypeTree ti = toP(F.at(pos).TypeIdent(TypeTag.VOID));
@ -1509,6 +1525,7 @@ public class JavacParser implements Parser {
syntaxError(pos, Errors.Expected3(CASE, DEFAULT, RBRACE));
}
}
// Not reachable.
default:
return illegal();
}
@ -1572,8 +1589,8 @@ public class JavacParser implements Parser {
if (token.kind == LBRACKET) {
nextToken();
if ((mode & TYPE) != 0) {
int oldmode = mode;
if (isMode(TYPE)) {
int prevmode = mode;
selectTypeMode();
if (token.kind == RBRACKET) {
nextToken();
@ -1588,9 +1605,9 @@ public class JavacParser implements Parser {
}
return t;
}
mode = oldmode;
setMode(prevmode);
}
if ((mode & EXPR) != 0) {
if (isMode(EXPR)) {
selectExprMode();
JCExpression t1 = term();
t = to(F.at(pos1).Indexed(t, t1));
@ -1599,13 +1616,13 @@ public class JavacParser implements Parser {
} else if (token.kind == DOT) {
nextToken();
typeArgs = typeArgumentsOpt(EXPR);
if (token.kind == SUPER && (mode & EXPR) != 0) {
if (token.kind == SUPER && isMode(EXPR)) {
selectExprMode();
t = to(F.at(pos1).Select(t, names._super));
nextToken();
t = arguments(typeArgs, t);
typeArgs = null;
} else if (token.kind == NEW && (mode & EXPR) != 0) {
} else if (token.kind == NEW && isMode(EXPR)) {
if (typeArgs != null) return illegal();
selectExprMode();
int pos2 = token.pos;
@ -1615,7 +1632,7 @@ public class JavacParser implements Parser {
typeArgs = null;
} else {
List<JCAnnotation> tyannos = null;
if ((mode & TYPE) != 0 && token.kind == MONKEYS_AT) {
if (isMode(TYPE) && token.kind == MONKEYS_AT) {
// is the mode check needed?
tyannos = typeAnnotationsOpt();
}
@ -1633,7 +1650,7 @@ public class JavacParser implements Parser {
t = argumentsOpt(typeArgs, typeArgumentsOpt(t));
typeArgs = null;
}
} else if ((mode & EXPR) != 0 && token.kind == COLCOL) {
} else if (isMode(EXPR) && token.kind == COLCOL) {
selectExprMode();
if (typeArgs != null) return illegal();
accept(COLCOL);
@ -1648,7 +1665,7 @@ public class JavacParser implements Parser {
break;
}
}
while ((token.kind == PLUSPLUS || token.kind == SUBSUB) && (mode & EXPR) != 0) {
while ((token.kind == PLUSPLUS || token.kind == SUBSUB) && isMode(EXPR)) {
selectExprMode();
t = to(F.at(token.pos).Unary(
token.kind == PLUSPLUS ? POSTINC : POSTDEC, t));
@ -1790,8 +1807,8 @@ public class JavacParser implements Parser {
return ParensResult.EXPLICIT_LAMBDA;
} else if (peekToken(lookahead, RPAREN, ARROW)) {
// Identifier, ')' '->' -> implicit lambda
return (mode & NOLAMBDA) == 0 ? ParensResult.IMPLICIT_LAMBDA
: ParensResult.PARENS;
return !isMode(NOLAMBDA) ? ParensResult.IMPLICIT_LAMBDA
: ParensResult.PARENS;
} else if (depth == 0 && peekToken(lookahead, COMMA)) {
defaultResult = ParensResult.IMPLICIT_LAMBDA;
}
@ -2040,7 +2057,7 @@ public class JavacParser implements Parser {
/** ArgumentsOpt = [ Arguments ]
*/
JCExpression argumentsOpt(List<JCExpression> typeArgs, JCExpression t) {
if ((mode & EXPR) != 0 && token.kind == LPAREN || typeArgs != null) {
if (isMode(EXPR) && token.kind == LPAREN || typeArgs != null) {
selectExprMode();
return arguments(typeArgs, t);
} else {
@ -2095,8 +2112,8 @@ public class JavacParser implements Parser {
*/
JCExpression typeArgumentsOpt(JCExpression t) {
if (token.kind == LT &&
(mode & TYPE) != 0 &&
(mode & NOPARAMS) == 0) {
isMode(TYPE) &&
!isMode(NOPARAMS)) {
selectTypeMode();
return typeArguments(t, false);
} else {
@ -2109,11 +2126,11 @@ public class JavacParser implements Parser {
List<JCExpression> typeArgumentsOpt(int useMode) {
if (token.kind == LT) {
if ((mode & useMode) == 0 ||
(mode & NOPARAMS) != 0) {
if (!isMode(useMode) ||
isMode(NOPARAMS)) {
illegal();
}
mode = useMode;
setMode(useMode);
return typeArguments(false);
}
return null;
@ -2128,15 +2145,15 @@ public class JavacParser implements Parser {
if (token.kind == LT) {
nextToken();
if (token.kind == GT && diamondAllowed) {
mode |= DIAMOND;
setMode(mode | DIAMOND);
nextToken();
return List.nil();
} else {
ListBuffer<JCExpression> args = new ListBuffer<>();
args.append(((mode & EXPR) == 0) ? typeArgument() : parseType());
args.append(!isMode(EXPR) ? typeArgument() : parseType());
while (token.kind == COMMA) {
nextToken();
args.append(((mode & EXPR) == 0) ? typeArgument() : parseType());
args.append(!isMode(EXPR) ? typeArgument() : parseType());
}
switch (token.kind) {
@ -2257,7 +2274,7 @@ public class JavacParser implements Parser {
* BracketsSuffixType =
*/
JCExpression bracketsSuffix(JCExpression t) {
if ((mode & EXPR) != 0 && token.kind == DOT) {
if (isMode(EXPR) && token.kind == DOT) {
selectExprMode();
int pos = token.pos;
nextToken();
@ -2281,7 +2298,7 @@ public class JavacParser implements Parser {
syntaxError(token.pos, Errors.NoAnnotationsOnDotClass);
t = toP(F.at(pos).Select(t, names._class));
}
} else if ((mode & TYPE) != 0) {
} else if (isMode(TYPE)) {
if (token.kind != COLCOL) {
selectTypeMode();
}
@ -2340,14 +2357,14 @@ public class JavacParser implements Parser {
}
JCExpression t = qualident(true);
int oldmode = mode;
int prevmode = mode;
selectTypeMode();
boolean diamondFound = false;
int lastTypeargsPos = -1;
if (token.kind == LT) {
lastTypeargsPos = token.pos;
t = typeArguments(t, true);
diamondFound = (mode & DIAMOND) != 0;
diamondFound = isMode(DIAMOND);
}
while (token.kind == DOT) {
if (diamondFound) {
@ -2366,10 +2383,10 @@ public class JavacParser implements Parser {
if (token.kind == LT) {
lastTypeargsPos = token.pos;
t = typeArguments(t, true);
diamondFound = (mode & DIAMOND) != 0;
diamondFound = isMode(DIAMOND);
}
}
mode = oldmode;
setMode(prevmode);
if (token.kind == LBRACKET || token.kind == MONKEYS_AT) {
// handle type annotations for non primitive arrays
if (newAnnotations.nonEmpty()) {
@ -2421,9 +2438,9 @@ public class JavacParser implements Parser {
}
if (token.kind == LT) {
int oldmode = mode;
int prevmode = mode;
t = typeArguments(t, true);
mode = oldmode;
setMode(prevmode);
}
return classCreatorRest(newpos, encl, typeArgs, t);
}
@ -2756,7 +2773,7 @@ public class JavacParser implements Parser {
nextToken();
JCStatement stat = parseStatementAsBlock();
return List.of(F.at(pos).Labelled(prevToken.name(), stat));
} else if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.test(token.kind)) {
} else if (wasTypeMode() && LAX_IDENTIFIER.test(token.kind)) {
pos = token.pos;
JCModifiers mods = F.at(Position.NOPOS).Modifiers(0);
F.at(pos);
@ -3318,9 +3335,9 @@ public class JavacParser implements Parser {
return variableDeclarators(optFinal(0), parseType(true), stats, true).toList();
} else {
JCExpression t = term(EXPR | TYPE);
if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.test(token.kind)) {
if (wasTypeMode() && LAX_IDENTIFIER.test(token.kind)) {
return variableDeclarators(modifiersOpt(), t, stats, true).toList();
} else if ((lastmode & TYPE) != 0 && token.kind == COLON) {
} else if (wasTypeMode() && token.kind == COLON) {
log.error(DiagnosticFlag.SYNTAX, pos, Errors.BadInitializer("for-loop"));
return List.of((JCStatement)F.at(pos).VarDef(modifiersOpt(), names.error, t, null));
} else {
@ -3350,8 +3367,8 @@ public class JavacParser implements Parser {
nextToken();
buf.append(annotation(pos, kind));
}
lastmode = mode;
mode = prevmode;
setLastMode(mode);
setMode(prevmode);
List<JCAnnotation> annotations = buf.toList();
return annotations;
@ -3773,7 +3790,7 @@ public class JavacParser implements Parser {
return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false);
}
JCExpression t = term(EXPR | TYPE);
if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.test(token.kind)) {
if (wasTypeMode() && LAX_IDENTIFIER.test(token.kind)) {
JCModifiers mods = F.Modifiers(0);
return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false);
} else {