Skip to content

Commit d571b94

Browse files
committed
primitive semantics for literal numbers
1 parent 052c350 commit d571b94

File tree

6 files changed

+77
-91
lines changed

6 files changed

+77
-91
lines changed

base/org.codehaus.groovy25/src/org/codehaus/groovy/antlr/AntlrParserPlugin.java

+18-20
Original file line numberDiff line numberDiff line change
@@ -1396,28 +1396,28 @@ public static Expression getDefaultValueForPrimitive(ClassNode type) {
13961396
if (type != null) type = type.redirect();
13971397

13981398
if (type == ClassHelper.int_TYPE) {
1399-
return new ConstantExpression(0);
1399+
return new ConstantExpression(0, true);
14001400
}
14011401
if (type == ClassHelper.long_TYPE) {
1402-
return new ConstantExpression(0L);
1402+
return new ConstantExpression(0L, true);
14031403
}
14041404
if (type == ClassHelper.double_TYPE) {
1405-
return new ConstantExpression(0.0);
1405+
return new ConstantExpression(0.0, true);
14061406
}
14071407
if (type == ClassHelper.float_TYPE) {
1408-
return new ConstantExpression(0.0F);
1408+
return new ConstantExpression(0.0F, true);
14091409
}
14101410
if (type == ClassHelper.boolean_TYPE) {
1411-
return ConstantExpression.FALSE;
1412-
}
1413-
if (type == ClassHelper.short_TYPE) {
1414-
return new ConstantExpression((short) 0);
1411+
return ConstantExpression.PRIM_FALSE;
14151412
}
14161413
if (type == ClassHelper.byte_TYPE) {
1417-
return new ConstantExpression((byte) 0);
1414+
return new ConstantExpression((byte) 0, true);
14181415
}
14191416
if (type == ClassHelper.char_TYPE) {
1420-
return new ConstantExpression((char) 0);
1417+
return new ConstantExpression((char) 0, true);
1418+
}
1419+
if (type == ClassHelper.short_TYPE) {
1420+
return new ConstantExpression((short) 0, true);
14211421
}
14221422
return null;
14231423
}
@@ -2585,7 +2585,7 @@ protected Expression variableExpression(AST node) {
25852585
}
25862586

25872587
protected ConstantExpression literalExpression(AST node, Object value) {
2588-
ConstantExpression constantExpression = new ConstantExpression(value, value instanceof Boolean);
2588+
ConstantExpression constantExpression = new ConstantExpression(value, true);
25892589
configureAST(constantExpression, node);
25902590
return constantExpression;
25912591
}
@@ -3347,7 +3347,7 @@ protected Expression unaryMinusExpression(AST unaryMinusExpr) {
33473347
case NUM_DOUBLE:
33483348
case NUM_FLOAT:
33493349
case NUM_BIG_DECIMAL:
3350-
ConstantExpression constantExpression = new ConstantExpression(Numbers.parseDecimal("-" + text));
3350+
ConstantExpression constantExpression = new ConstantExpression(Numbers.parseDecimal("-" + text), true);
33513351
configureAST(constantExpression, unaryMinusExpr);
33523352
// GRECLIPSE add
33533353
setSourceEnd(constantExpression, expression(node));
@@ -3357,7 +3357,7 @@ protected Expression unaryMinusExpression(AST unaryMinusExpr) {
33573357
case NUM_BIG_INT:
33583358
case NUM_INT:
33593359
case NUM_LONG:
3360-
ConstantExpression constantLongExpression = new ConstantExpression(Numbers.parseInteger("-" + text));
3360+
ConstantExpression constantLongExpression = new ConstantExpression(Numbers.parseInteger("-" + text), true);
33613361
configureAST(constantLongExpression, unaryMinusExpr);
33623362
// GRECLIPSE add
33633363
setSourceEnd(constantLongExpression, expression(node));
@@ -3401,17 +3401,15 @@ protected Expression unaryPlusExpression(AST unaryPlusExpr) {
34013401
protected ConstantExpression decimalExpression(AST node) {
34023402
String text = node.getText();
34033403
Object number = Numbers.parseDecimal(text);
3404-
ConstantExpression constantExpression = new ConstantExpression(number,
3405-
number instanceof Double || number instanceof Float);
3404+
ConstantExpression constantExpression = new ConstantExpression(number, true);
34063405
configureAST(constantExpression, node);
34073406
return constantExpression;
34083407
}
34093408

34103409
protected ConstantExpression integerExpression(AST node) {
34113410
String text = node.getText();
3412-
Object number = Numbers.parseInteger(node, text);
3413-
boolean keepPrimitive = number instanceof Integer || number instanceof Long;
3414-
ConstantExpression constantExpression = new ConstantExpression(number, keepPrimitive);
3411+
Object number = Numbers.parseInteger(text);
3412+
ConstantExpression constantExpression = new ConstantExpression(number, true);
34153413
configureAST(constantExpression, node);
34163414
return constantExpression;
34173415
}
@@ -3430,7 +3428,7 @@ protected Expression gstring(AST gstringNode) {
34303428
switch (type) {
34313429

34323430
case STRING_LITERAL:
3433-
if (isPrevString) assertNodeType(IDENT, node); // parser bug
3431+
if (isPrevString) assertNodeType(IDENT, node); // parser bug
34343432
isPrevString = true;
34353433
text = node.getText();
34363434
ConstantExpression constantExpression = new ConstantExpression(text);
@@ -3440,7 +3438,7 @@ protected Expression gstring(AST gstringNode) {
34403438
break;
34413439

34423440
default: {
3443-
if (!isPrevString) assertNodeType(IDENT, node); // parser bug
3441+
if (!isPrevString) assertNodeType(IDENT, node); // parser bug
34443442
isPrevString = false;
34453443
Expression expression = expression(node);
34463444
values.add(expression);

base/org.codehaus.groovy30/src/org/apache/groovy/parser/antlr4/AstBuilder.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -2124,7 +2124,7 @@ private DeclarationListStatement createFieldDeclarationListStatement(final Varia
21242124

21252125
if (classNode.isInterface()) {
21262126
if (!asBoolean(initialValue)) {
2127-
initialValue = !asBoolean(defaultValue) ? null : new ConstantExpression(defaultValue);
2127+
initialValue = !asBoolean(defaultValue) ? null : new ConstantExpression(defaultValue, true);
21282128
}
21292129

21302130
modifiers |= Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
@@ -2970,9 +2970,8 @@ public Expression visitEnhancedArgumentListElement(final EnhancedArgumentListEle
29702970
public ConstantExpression visitStringLiteral(final StringLiteralContext ctx) {
29712971
String text = parseStringLiteral(ctx.StringLiteral().getText());
29722972

2973-
ConstantExpression constantExpression = new ConstantExpression(text, true);
2974-
constantExpression.putNodeMetaData(IS_STRING, true);
2975-
2973+
ConstantExpression constantExpression = new ConstantExpression(text);
2974+
constantExpression.putNodeMetaData(IS_STRING, Boolean.TRUE);
29762975
return configureAST(constantExpression, ctx);
29772976
}
29782977

@@ -3144,7 +3143,7 @@ public Expression visitUnaryAddExprAlt(final UnaryAddExprAltContext ctx) {
31443143
String integerLiteralText = constantExpression.getNodeMetaData(INTEGER_LITERAL_TEXT);
31453144
if (null != integerLiteralText) {
31463145

3147-
ConstantExpression result = new ConstantExpression(Numbers.parseInteger(SUB_STR + integerLiteralText));
3146+
ConstantExpression result = new ConstantExpression(Numbers.parseInteger(SUB_STR + integerLiteralText), true);
31483147

31493148
this.numberFormatError = null; // reset the numberFormatError
31503149

@@ -3153,7 +3152,7 @@ public Expression visitUnaryAddExprAlt(final UnaryAddExprAltContext ctx) {
31533152

31543153
String floatingPointLiteralText = constantExpression.getNodeMetaData(FLOATING_POINT_LITERAL_TEXT);
31553154
if (null != floatingPointLiteralText) {
3156-
ConstantExpression result = new ConstantExpression(Numbers.parseDecimal(SUB_STR + floatingPointLiteralText));
3155+
ConstantExpression result = new ConstantExpression(Numbers.parseDecimal(SUB_STR + floatingPointLiteralText), true);
31573156

31583157
this.numberFormatError = null; // reset the numberFormatError
31593158

@@ -3826,10 +3825,9 @@ public ConstantExpression visitIntegerLiteralAlt(final IntegerLiteralAltContext
38263825
this.numberFormatError = tuple(ctx, e);
38273826
}
38283827

3829-
ConstantExpression constantExpression = new ConstantExpression(num, !text.startsWith(SUB_STR));
3830-
constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
3828+
ConstantExpression constantExpression = new ConstantExpression(num, true);
38313829
constantExpression.putNodeMetaData(INTEGER_LITERAL_TEXT, text);
3832-
3830+
constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
38333831
return configureAST(constantExpression, ctx);
38343832
}
38353833

@@ -3844,10 +3842,9 @@ public ConstantExpression visitFloatingPointLiteralAlt(final FloatingPointLitera
38443842
this.numberFormatError = tuple(ctx, e);
38453843
}
38463844

3847-
ConstantExpression constantExpression = new ConstantExpression(num, !text.startsWith(SUB_STR));
3848-
constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
3845+
ConstantExpression constantExpression = new ConstantExpression(num, true);
38493846
constantExpression.putNodeMetaData(FLOATING_POINT_LITERAL_TEXT, text);
3850-
3847+
constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
38513848
return configureAST(constantExpression, ctx);
38523849
}
38533850

base/org.codehaus.groovy30/src/org/codehaus/groovy/antlr/AntlrParserPlugin.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,7 @@ protected Statement forStatement(AST forNode) {
18871887
AST variableNode = inNode.getFirstChild();
18881888
AST collectionNode = variableNode.getNextSibling();
18891889

1890-
ClassNode type = ClassHelper.OBJECT_TYPE;
1890+
ClassNode type = ClassHelper.DYNAMIC_TYPE;
18911891
if (isType(VARIABLE_DEF, variableNode)) {
18921892
AST node = variableNode.getFirstChild();
18931893
// skip the final modifier if it's present
@@ -2652,7 +2652,7 @@ protected Expression variableExpression(AST node) {
26522652
}
26532653

26542654
protected ConstantExpression literalExpression(AST node, Object value) {
2655-
ConstantExpression constantExpression = new ConstantExpression(value, value instanceof Boolean);
2655+
ConstantExpression constantExpression = new ConstantExpression(value, true);
26562656
configureAST(constantExpression, node);
26572657
return constantExpression;
26582658
}
@@ -3414,7 +3414,7 @@ protected Expression unaryMinusExpression(AST unaryMinusExpr) {
34143414
case NUM_DOUBLE:
34153415
case NUM_FLOAT:
34163416
case NUM_BIG_DECIMAL:
3417-
ConstantExpression constantExpression = new ConstantExpression(Numbers.parseDecimal("-" + text));
3417+
ConstantExpression constantExpression = new ConstantExpression(Numbers.parseDecimal("-" + text), true);
34183418
configureAST(constantExpression, unaryMinusExpr);
34193419
// GRECLIPSE add
34203420
setSourceEnd(constantExpression, expression(node));
@@ -3424,7 +3424,7 @@ protected Expression unaryMinusExpression(AST unaryMinusExpr) {
34243424
case NUM_BIG_INT:
34253425
case NUM_INT:
34263426
case NUM_LONG:
3427-
ConstantExpression constantLongExpression = new ConstantExpression(Numbers.parseInteger("-" + text));
3427+
ConstantExpression constantLongExpression = new ConstantExpression(Numbers.parseInteger("-" + text), true);
34283428
configureAST(constantLongExpression, unaryMinusExpr);
34293429
// GRECLIPSE add
34303430
setSourceEnd(constantLongExpression, expression(node));
@@ -3468,17 +3468,15 @@ protected Expression unaryPlusExpression(AST unaryPlusExpr) {
34683468
protected ConstantExpression decimalExpression(AST node) {
34693469
String text = node.getText();
34703470
Object number = Numbers.parseDecimal(text);
3471-
ConstantExpression constantExpression = new ConstantExpression(number,
3472-
number instanceof Double || number instanceof Float);
3471+
ConstantExpression constantExpression = new ConstantExpression(number, true);
34733472
configureAST(constantExpression, node);
34743473
return constantExpression;
34753474
}
34763475

34773476
protected ConstantExpression integerExpression(AST node) {
34783477
String text = node.getText();
3479-
Object number = Numbers.parseInteger(node, text);
3480-
boolean keepPrimitive = number instanceof Integer || number instanceof Long;
3481-
ConstantExpression constantExpression = new ConstantExpression(number, keepPrimitive);
3478+
Object number = Numbers.parseInteger(text);
3479+
ConstantExpression constantExpression = new ConstantExpression(number, true);
34823480
configureAST(constantExpression, node);
34833481
return constantExpression;
34843482
}
@@ -3497,7 +3495,7 @@ protected Expression gstring(AST gstringNode) {
34973495
switch (type) {
34983496

34993497
case STRING_LITERAL:
3500-
if (isPrevString) assertNodeType(IDENT, node); // parser bug
3498+
if (isPrevString) assertNodeType(IDENT, node); // parser bug
35013499
isPrevString = true;
35023500
text = node.getText();
35033501
ConstantExpression constantExpression = new ConstantExpression(text);
@@ -3507,7 +3505,7 @@ protected Expression gstring(AST gstringNode) {
35073505
break;
35083506

35093507
default: {
3510-
if (!isPrevString) assertNodeType(IDENT, node); // parser bug
3508+
if (!isPrevString) assertNodeType(IDENT, node); // parser bug
35113509
isPrevString = false;
35123510
Expression expression = expression(node);
35133511
values.add(expression);

base/org.codehaus.groovy30/src/org/codehaus/groovy/vmplugin/v8/Java8.java

-6
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,6 @@ private void setAnnotationMetaData(Annotation[] annotations, AnnotatedNode an) {
311311
}
312312
}
313313

314-
@Deprecated
315-
private void configureAnnotationFromDefinition(AnnotationNode definition, AnnotationNode root) {
316-
VMPlugin plugin = VMPluginFactory.getPlugin();
317-
plugin.configureAnnotationNodeFromDefinition(definition, root);
318-
}
319-
320314
public void configureAnnotationNodeFromDefinition(AnnotationNode definition, AnnotationNode root) {
321315
ClassNode type = definition.getClassNode();
322316
final String typeName = type.getName();

base/org.codehaus.groovy40/src/org/apache/groovy/parser/antlr4/AstBuilder.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -2379,7 +2379,7 @@ private DeclarationListStatement createFieldDeclarationListStatement(final Varia
23792379

23802380
if (classNode.isInterface()) {
23812381
if (!asBoolean(initialValue)) {
2382-
initialValue = !asBoolean(defaultValue) ? null : new ConstantExpression(defaultValue);
2382+
initialValue = !asBoolean(defaultValue) ? null : new ConstantExpression(defaultValue, true);
23832383
}
23842384

23852385
modifiers |= Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
@@ -3265,9 +3265,8 @@ public Expression visitEnhancedArgumentListElement(final EnhancedArgumentListEle
32653265
public ConstantExpression visitStringLiteral(final StringLiteralContext ctx) {
32663266
String text = parseStringLiteral(ctx.StringLiteral().getText());
32673267

3268-
ConstantExpression constantExpression = new ConstantExpression(text, true);
3269-
constantExpression.putNodeMetaData(IS_STRING, true);
3270-
3268+
ConstantExpression constantExpression = new ConstantExpression(text);
3269+
constantExpression.putNodeMetaData(IS_STRING, Boolean.TRUE);
32713270
return configureAST(constantExpression, ctx);
32723271
}
32733272

@@ -3437,7 +3436,7 @@ public Expression visitUnaryAddExprAlt(final UnaryAddExprAltContext ctx) {
34373436
String integerLiteralText = constantExpression.getNodeMetaData(INTEGER_LITERAL_TEXT);
34383437
if (null != integerLiteralText) {
34393438

3440-
ConstantExpression result = new ConstantExpression(Numbers.parseInteger(SUB_STR + integerLiteralText));
3439+
ConstantExpression result = new ConstantExpression(Numbers.parseInteger(SUB_STR + integerLiteralText), true);
34413440

34423441
this.numberFormatError = null; // reset the numberFormatError
34433442

@@ -3446,7 +3445,7 @@ public Expression visitUnaryAddExprAlt(final UnaryAddExprAltContext ctx) {
34463445

34473446
String floatingPointLiteralText = constantExpression.getNodeMetaData(FLOATING_POINT_LITERAL_TEXT);
34483447
if (null != floatingPointLiteralText) {
3449-
ConstantExpression result = new ConstantExpression(Numbers.parseDecimal(SUB_STR + floatingPointLiteralText));
3448+
ConstantExpression result = new ConstantExpression(Numbers.parseDecimal(SUB_STR + floatingPointLiteralText), true);
34503449

34513450
this.numberFormatError = null; // reset the numberFormatError
34523451

@@ -4110,10 +4109,9 @@ public ConstantExpression visitIntegerLiteralAlt(final IntegerLiteralAltContext
41104109
this.numberFormatError = tuple(ctx, e);
41114110
}
41124111

4113-
ConstantExpression constantExpression = new ConstantExpression(num, !text.startsWith(SUB_STR));
4114-
constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
4112+
ConstantExpression constantExpression = new ConstantExpression(num, true);
41154113
constantExpression.putNodeMetaData(INTEGER_LITERAL_TEXT, text);
4116-
4114+
constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
41174115
return configureAST(constantExpression, ctx);
41184116
}
41194117

@@ -4128,10 +4126,9 @@ public ConstantExpression visitFloatingPointLiteralAlt(final FloatingPointLitera
41284126
this.numberFormatError = tuple(ctx, e);
41294127
}
41304128

4131-
ConstantExpression constantExpression = new ConstantExpression(num, !text.startsWith(SUB_STR));
4132-
constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
4129+
ConstantExpression constantExpression = new ConstantExpression(num, true);
41334130
constantExpression.putNodeMetaData(FLOATING_POINT_LITERAL_TEXT, text);
4134-
4131+
constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
41354132
return configureAST(constantExpression, ctx);
41364133
}
41374134

0 commit comments

Comments
 (0)