Handle const list and map literals.

R=karlklose@google.com

Review-Url: https://codereview.chromium.org/2802693004 .
This commit is contained in:
Peter von der Ahé 2017-04-06 14:51:33 +02:00
parent 588857cae3
commit 32e2d2af84
6 changed files with 49 additions and 23 deletions

View file

@ -96,6 +96,11 @@ class AstBuilder extends ScopeListener {
_handleInstanceCreation(token);
}
@override
void endConstLiteral(Token token) {
debugEvent("endConstLiteral");
}
void _handleInstanceCreation(Token token) {
MethodInvocation arguments = pop();
ConstructorName constructorName = pop();

View file

@ -747,7 +747,7 @@ class BodyBuilder extends ScopeListener<JumpTarget> implements BuilderHelper {
if (!isQualified && isInstanceContext) {
assert(builder == null);
if (constantExpressionRequired) {
addCompileTimeError(charOffset, "Not a constant expression.");
return new UnresolvedAccessor(this, n, charOffset);
}
return new ThisPropertyAccessor(this, charOffset, n, null, null);
} else if (isDartLibrary &&
@ -1136,7 +1136,7 @@ class BodyBuilder extends ScopeListener<JumpTarget> implements BuilderHelper {
typeArgument = typeArguments.first;
if (typeArguments.length > 1) {
typeArgument = const DynamicType();
warning(
warningNotError(
"Too many type arguments on List literal.", beginToken.charOffset);
}
}
@ -1178,7 +1178,7 @@ class BodyBuilder extends ScopeListener<JumpTarget> implements BuilderHelper {
if (typeArguments.length != 2) {
keyType = const DynamicType();
valueType = const DynamicType();
warning(
warningNotError(
"Map literal requires two type arguments.", beginToken.charOffset);
} else {
keyType = typeArguments[0];
@ -1246,7 +1246,8 @@ class BodyBuilder extends ScopeListener<JumpTarget> implements BuilderHelper {
ProblemBuilder problem = builder;
addCompileTimeError(charOffset, problem.message);
} else {
warning("Not a type: '${builder.fullNameForErrors}'.", charOffset);
warningNotError(
"Not a type: '${builder.fullNameForErrors}'.", charOffset);
}
// TODO(ahe): Create an error somehow.
return const DynamicType();
@ -1290,7 +1291,8 @@ class BodyBuilder extends ScopeListener<JumpTarget> implements BuilderHelper {
name = name.name;
}
if (name is FastaAccessor) {
warning("'${beginToken.lexeme}' isn't a type.", beginToken.charOffset);
warningNotError(
"'${beginToken.lexeme}' isn't a type.", beginToken.charOffset);
push(const DynamicType());
} else if (name is TypeVariableBuilder) {
if (constantExpressionRequired) {
@ -1751,6 +1753,21 @@ class BodyBuilder extends ScopeListener<JumpTarget> implements BuilderHelper {
constantExpressionRequired = true;
}
@override
void beginConstLiteral(Token token) {
debugEvent("beginConstLiteral");
super.push(constantExpressionRequired);
constantExpressionRequired = true;
}
@override
void endConstLiteral(Token token) {
debugEvent("endConstLiteral");
var literal = pop();
constantExpressionRequired = pop();
push(literal);
}
@override
void endNewExpression(Token token) {
debugEvent("NewExpression");
@ -2423,6 +2440,10 @@ class BodyBuilder extends ScopeListener<JumpTarget> implements BuilderHelper {
}
}
void warningNotError(String message, [int charOffset = -1]) {
super.warning(message, charOffset);
}
Expression evaluateArgumentsBefore(
Arguments arguments, Expression expression) {
if (arguments == null) return expression;

View file

@ -104,6 +104,12 @@ class Listener {
logEvent("CompilationUnit");
}
void beginConstLiteral(Token token) {}
void endConstLiteral(Token token) {
logEvent("ConstLiteral");
}
void beginConstructorReference(Token start) {}
void endConstructorReference(

View file

@ -3093,15 +3093,24 @@ class Parser {
token = _injectGenericCommentTypeList(token);
final String value = token.stringValue;
if ((identical(value, '[')) || (identical(value, '[]'))) {
listener.beginConstLiteral(token);
listener.handleNoTypeArguments(token);
return parseLiteralListSuffix(token, constKeyword);
token = parseLiteralListSuffix(token, constKeyword);
listener.endConstLiteral(token);
return token;
}
if (identical(value, '{')) {
listener.beginConstLiteral(token);
listener.handleNoTypeArguments(token);
return parseLiteralMapSuffix(token, constKeyword);
token = parseLiteralMapSuffix(token, constKeyword);
listener.endConstLiteral(token);
return token;
}
if (identical(value, '<')) {
return parseLiteralListOrMapOrFunction(token, constKeyword);
listener.beginConstLiteral(token);
token = parseLiteralListOrMapOrFunction(token, constKeyword);
listener.endConstLiteral(token);
return token;
}
listener.beginConstExpression(constKeyword);
token = parseConstructorReference(token);

View file

@ -2,7 +2,6 @@
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
# Disable tests globally for kernel.
[ $compiler == dartk || $compiler == dartkp ]
Language/Classes/Constructors/Constant_Constructors/initializer_not_a_constant_t03: MissingCompileTimeError
Language/Classes/Constructors/Constant_Constructors/non_final_instance_variable_t02: MissingCompileTimeError
@ -111,7 +110,6 @@ Language/Expressions/Constants/no_other_constant_expressions_t15: Crash
Language/Expressions/Constants/no_other_constant_expressions_t17: Crash
Language/Expressions/Constants/static_constant_t06: MissingCompileTimeError
Language/Expressions/Constants/static_constant_t07: MissingCompileTimeError
Language/Expressions/Constants/static_method_t02: Crash
Language/Expressions/Constants/string_length_t03: MissingCompileTimeError
Language/Expressions/Constants/ternary_operator_t02: MissingCompileTimeError
Language/Expressions/Constants/top_level_function_t01: Crash
@ -133,13 +131,7 @@ Language/Expressions/Instance_Creation/Const/deferred_type_t02: MissingCompileTi
Language/Expressions/Instance_Creation/New/evaluation_t06: RuntimeError
Language/Expressions/Instance_Creation/New/evaluation_t19: RuntimeError # Kernel Issue 28335 (deferred libraries)
Language/Expressions/Instance_Creation/New/evaluation_t20: RuntimeError # Kernel Issue 28335 (deferred libraries)
Language/Expressions/Lists/constant_list_t01: MissingCompileTimeError
Language/Expressions/Lists/constant_list_t02: Crash
Language/Expressions/Maps/constant_map_key_value_t01: Crash
Language/Expressions/Maps/constant_map_key_value_t03: MissingCompileTimeError
Language/Expressions/Maps/constant_map_t01: Crash
Language/Expressions/Maps/constant_map_t02: MissingCompileTimeError
Language/Expressions/Maps/constant_map_type_t01: MissingCompileTimeError
Language/Expressions/Maps/key_value_equals_operator_t01: MissingCompileTimeError
Language/Expressions/Method_Invocation/Ordinary_Invocation/evaluation_t07: RuntimeError # Dartk Issue 28562
Language/Expressions/Method_Invocation/Ordinary_Invocation/method_lookup_failed_t17: RuntimeError
@ -385,7 +377,6 @@ LibTest/isolate/Isolate/spawnUri_A01_t06: Skip
[ ($compiler == dartk || $compiler == dartkp) && $mode == debug ]
Language/Classes/Instance_Variables/constant_t01: Crash
Language/Expressions/Lists/constant_list_t01: Crash
Language/Libraries_and_Scripts/Scripts/top_level_main_t05: Crash
Language/Statements/Switch/syntax_t16: Crash
Language/Statements/Switch/syntax_t17: Crash

View file

@ -3,7 +3,6 @@
# BSD-style license that can be found in the LICENSE file.
[ $compiler == dartk || $compiler == dartkp ]
bad_initializer1_negative_test: Crash
body_less_constructor_wrong_arg_negative_test: Crash
built_in_identifier_test/01: CompileTimeError
call_nonexistent_static_test/01: CompileTimeError
@ -58,8 +57,6 @@ const_switch2_test/01: MissingCompileTimeError
const_syntax_test/01: MissingCompileTimeError
const_syntax_test/02: MissingCompileTimeError
const_syntax_test/05: MissingCompileTimeError
const_types_test/06: CompileTimeError
const_types_test/13: CompileTimeError
const_types_test/23: MissingCompileTimeError
const_types_test/24: MissingCompileTimeError
const_types_test/25: MissingCompileTimeError
@ -279,14 +276,12 @@ library_ambiguous_test/00: MissingRuntimeError
library_ambiguous_test/04: MissingRuntimeError
library_env_test/has_html_support: RuntimeError
library_env_test/has_no_io_support: RuntimeError
list_literal2_negative_test: Fail
list_literal4_test: RuntimeError
main_not_a_function_test/01: Crash
malformed2_test/00: RuntimeError
malformed_inheritance_test/09: MissingCompileTimeError
malformed_inheritance_test/10: MissingCompileTimeError
malformed_test/none: RuntimeError
map_literal2_negative_test: Fail
map_literal3_test: RuntimeError
map_literal6_test: RuntimeError
metadata_test: CompileTimeError
@ -443,7 +438,6 @@ vm/type_vm_test: RuntimeError
# dartk: JIT & AOT failures (debug)
[ ($compiler == dartk || $compiler == dartkp) && $mode == debug ]
const_instance_field_test/01: Crash
list_literal2_negative_test: Crash
switch1_negative_test: Crash
type_parameter_test/04: Crash
type_parameter_test/05: Crash