OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "vm/parser.h" | 5 #include "vm/parser.h" |
6 #include "vm/flags.h" | 6 #include "vm/flags.h" |
7 | 7 |
8 #ifndef DART_PRECOMPILED_RUNTIME | 8 #ifndef DART_PRECOMPILED_RUNTIME |
9 | 9 |
10 #include "lib/invocation_mirror.h" | 10 #include "lib/invocation_mirror.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 // committed to the current version. | 48 // committed to the current version. |
49 DEFINE_FLAG(bool, conditional_directives, true, | 49 DEFINE_FLAG(bool, conditional_directives, true, |
50 "Enable conditional directives"); | 50 "Enable conditional directives"); |
51 DEFINE_FLAG(bool, initializing_formal_access, false, | 51 DEFINE_FLAG(bool, initializing_formal_access, false, |
52 "Make initializing formal parameters visible in initializer list."); | 52 "Make initializing formal parameters visible in initializer list."); |
53 DEFINE_FLAG(bool, warn_super, false, | 53 DEFINE_FLAG(bool, warn_super, false, |
54 "Warning if super initializer not last in initializer list."); | 54 "Warning if super initializer not last in initializer list."); |
55 DEFINE_FLAG(bool, warn_patch, false, "Warn on old-style patch syntax."); | 55 DEFINE_FLAG(bool, warn_patch, false, "Warn on old-style patch syntax."); |
56 DEFINE_FLAG(bool, await_is_keyword, false, | 56 DEFINE_FLAG(bool, await_is_keyword, false, |
57 "await and yield are treated as proper keywords in synchronous code."); | 57 "await and yield are treated as proper keywords in synchronous code."); |
| 58 DEFINE_FLAG(bool, assert_initializer, false, |
| 59 "Allow asserts in initializer lists."); |
58 | 60 |
59 DECLARE_FLAG(bool, profile_vm); | 61 DECLARE_FLAG(bool, profile_vm); |
60 DECLARE_FLAG(bool, trace_service); | 62 DECLARE_FLAG(bool, trace_service); |
61 | 63 |
62 // Quick access to the current thread, isolate and zone. | 64 // Quick access to the current thread, isolate and zone. |
63 #define T (thread()) | 65 #define T (thread()) |
64 #define I (isolate()) | 66 #define I (isolate()) |
65 #define Z (zone()) | 67 #define Z (zone()) |
66 | 68 |
67 // Quick synthetic token position. | 69 // Quick synthetic token position. |
(...skipping 2593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2661 } | 2663 } |
2662 return new StaticCallNode(supercall_pos, super_ctor, arguments); | 2664 return new StaticCallNode(supercall_pos, super_ctor, arguments); |
2663 } | 2665 } |
2664 | 2666 |
2665 | 2667 |
2666 AstNode* Parser::ParseInitializer(const Class& cls, | 2668 AstNode* Parser::ParseInitializer(const Class& cls, |
2667 LocalVariable* receiver, | 2669 LocalVariable* receiver, |
2668 GrowableArray<Field*>* initialized_fields) { | 2670 GrowableArray<Field*>* initialized_fields) { |
2669 TRACE_PARSER("ParseInitializer"); | 2671 TRACE_PARSER("ParseInitializer"); |
2670 const TokenPosition field_pos = TokenPos(); | 2672 const TokenPosition field_pos = TokenPos(); |
| 2673 if (FLAG_assert_initializer && CurrentToken() == Token::kASSERT) { |
| 2674 return ParseAssertStatement(current_function().is_const()); |
| 2675 } |
2671 if (CurrentToken() == Token::kTHIS) { | 2676 if (CurrentToken() == Token::kTHIS) { |
2672 ConsumeToken(); | 2677 ConsumeToken(); |
2673 ExpectToken(Token::kPERIOD); | 2678 ExpectToken(Token::kPERIOD); |
2674 } | 2679 } |
2675 const String& field_name = *ExpectIdentifier("field name expected"); | 2680 const String& field_name = *ExpectIdentifier("field name expected"); |
2676 ExpectToken(Token::kASSIGN); | 2681 ExpectToken(Token::kASSIGN); |
2677 | 2682 |
2678 const bool saved_mode = SetAllowFunctionLiterals(false); | 2683 const bool saved_mode = SetAllowFunctionLiterals(false); |
2679 // "this" must not be accessible in initializer expressions. | 2684 // "this" must not be accessible in initializer expressions. |
2680 receiver->set_invisible(true); | 2685 receiver->set_invisible(true); |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2950 ReportError("duplicate call to super constructor"); | 2955 ReportError("duplicate call to super constructor"); |
2951 } | 2956 } |
2952 super_init_call = ParseSuperInitializer(cls, receiver); | 2957 super_init_call = ParseSuperInitializer(cls, receiver); |
2953 super_init_index = current_block_->statements->length(); | 2958 super_init_index = current_block_->statements->length(); |
2954 current_block_->statements->Add(super_init_call); | 2959 current_block_->statements->Add(super_init_call); |
2955 super_init_is_last = true; | 2960 super_init_is_last = true; |
2956 } else { | 2961 } else { |
2957 AstNode* init_statement = | 2962 AstNode* init_statement = |
2958 ParseInitializer(cls, receiver, initialized_fields); | 2963 ParseInitializer(cls, receiver, initialized_fields); |
2959 super_init_is_last = false; | 2964 super_init_is_last = false; |
2960 current_block_->statements->Add(init_statement); | 2965 if (init_statement != NULL) { |
| 2966 current_block_->statements->Add(init_statement); |
| 2967 } |
2961 } | 2968 } |
2962 } while (CurrentToken() == Token::kCOMMA); | 2969 } while (CurrentToken() == Token::kCOMMA); |
2963 } | 2970 } |
2964 if (super_init_call == NULL) { | 2971 if (super_init_call == NULL) { |
2965 // Generate implicit super() if we haven't seen an explicit super call | 2972 // Generate implicit super() if we haven't seen an explicit super call |
2966 // or constructor redirection. | 2973 // or constructor redirection. |
2967 super_init_call = | 2974 super_init_call = |
2968 GenerateSuperConstructorCall(cls, TokenPos(), receiver, NULL); | 2975 GenerateSuperConstructorCall(cls, TokenPos(), receiver, NULL); |
2969 if (super_init_call != NULL) { | 2976 if (super_init_call != NULL) { |
2970 super_init_index = current_block_->statements->length(); | 2977 super_init_index = current_block_->statements->length(); |
(...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3652 do { | 3659 do { |
3653 ConsumeToken(); // Colon or comma. | 3660 ConsumeToken(); // Colon or comma. |
3654 if (CurrentToken() == Token::kSUPER) { | 3661 if (CurrentToken() == Token::kSUPER) { |
3655 ConsumeToken(); | 3662 ConsumeToken(); |
3656 if (CurrentToken() == Token::kPERIOD) { | 3663 if (CurrentToken() == Token::kPERIOD) { |
3657 ConsumeToken(); | 3664 ConsumeToken(); |
3658 ExpectIdentifier("identifier expected"); | 3665 ExpectIdentifier("identifier expected"); |
3659 } | 3666 } |
3660 CheckToken(Token::kLPAREN); | 3667 CheckToken(Token::kLPAREN); |
3661 SkipToMatchingParenthesis(); | 3668 SkipToMatchingParenthesis(); |
| 3669 } else if (FLAG_assert_initializer && (CurrentToken() == Token::kASSERT)) { |
| 3670 ConsumeToken(); |
| 3671 CheckToken(Token::kLPAREN); |
| 3672 SkipToMatchingParenthesis(); |
3662 } else { | 3673 } else { |
3663 SkipIf(Token::kTHIS); | 3674 SkipIf(Token::kTHIS); |
3664 SkipIf(Token::kPERIOD); | 3675 SkipIf(Token::kPERIOD); |
3665 ExpectIdentifier("identifier expected"); | 3676 ExpectIdentifier("identifier expected"); |
3666 ExpectToken(Token::kASSIGN); | 3677 ExpectToken(Token::kASSIGN); |
3667 SetAllowFunctionLiterals(false); | 3678 SetAllowFunctionLiterals(false); |
3668 SkipExpr(); | 3679 SkipExpr(); |
3669 SetAllowFunctionLiterals(true); | 3680 SetAllowFunctionLiterals(true); |
3670 } | 3681 } |
3671 } while (CurrentToken() == Token::kCOMMA); | 3682 } while (CurrentToken() == Token::kCOMMA); |
(...skipping 5609 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9281 const Function& func = Function::ZoneHandle(Z, | 9292 const Function& func = Function::ZoneHandle(Z, |
9282 Resolver::ResolveStatic(cls, | 9293 Resolver::ResolveStatic(cls, |
9283 func_name, | 9294 func_name, |
9284 arguments->length(), | 9295 arguments->length(), |
9285 arguments->names())); | 9296 arguments->names())); |
9286 ASSERT(!func.IsNull()); | 9297 ASSERT(!func.IsNull()); |
9287 return new(Z) StaticCallNode(arguments->token_pos(), func, arguments); | 9298 return new(Z) StaticCallNode(arguments->token_pos(), func, arguments); |
9288 } | 9299 } |
9289 | 9300 |
9290 | 9301 |
9291 AstNode* Parser::ParseAssertStatement() { | 9302 AstNode* Parser::ParseAssertStatement(bool is_const) { |
9292 TRACE_PARSER("ParseAssertStatement"); | 9303 TRACE_PARSER("ParseAssertStatement"); |
9293 ConsumeToken(); // Consume assert keyword. | 9304 ConsumeToken(); // Consume assert keyword. |
9294 ExpectToken(Token::kLPAREN); | 9305 ExpectToken(Token::kLPAREN); |
9295 const TokenPosition condition_pos = TokenPos(); | 9306 const TokenPosition condition_pos = TokenPos(); |
9296 if (!I->asserts()) { | 9307 if (!I->asserts()) { |
9297 SkipExpr(); | 9308 SkipExpr(); |
9298 ExpectToken(Token::kRPAREN); | 9309 ExpectToken(Token::kRPAREN); |
9299 return NULL; | 9310 return NULL; |
9300 } | 9311 } |
9301 AstNode* condition = ParseAwaitableExpr(kAllowConst, kConsumeCascades, NULL); | 9312 AstNode* condition = ParseAwaitableExpr(kAllowConst, kConsumeCascades, NULL); |
| 9313 if (is_const && !condition->IsPotentiallyConst()) { |
| 9314 ReportError(condition_pos, |
| 9315 "initializer assert expression must be compile time constant."); |
| 9316 } |
9302 const TokenPosition condition_end = TokenPos(); | 9317 const TokenPosition condition_end = TokenPos(); |
9303 ExpectToken(Token::kRPAREN); | 9318 ExpectToken(Token::kRPAREN); |
9304 | 9319 |
9305 ArgumentListNode* arguments = new(Z) ArgumentListNode(condition_pos); | 9320 ArgumentListNode* arguments = new(Z) ArgumentListNode(condition_pos); |
9306 arguments->Add(condition); | 9321 arguments->Add(condition); |
9307 arguments->Add(new(Z) LiteralNode(condition_pos, | 9322 arguments->Add(new(Z) LiteralNode(condition_pos, |
9308 Integer::ZoneHandle(Z, Integer::New(condition_pos.value(), Heap::kOld)))); | 9323 Integer::ZoneHandle(Z, Integer::New(condition_pos.value(), Heap::kOld)))); |
9309 arguments->Add(new(Z) LiteralNode(condition_end, | 9324 arguments->Add(new(Z) LiteralNode(condition_end, |
9310 Integer::ZoneHandle(Z, Integer::New(condition_end.value(), Heap::kOld)))); | 9325 Integer::ZoneHandle(Z, Integer::New(condition_end.value(), Heap::kOld)))); |
9311 return MakeStaticCall(Symbols::AssertionError(), | 9326 AstNode* assert_throw = MakeStaticCall(Symbols::AssertionError(), |
9312 Library::PrivateCoreLibName(Symbols::CheckAssertion()), | 9327 Library::PrivateCoreLibName(is_const ? Symbols::CheckConstAssertion() |
9313 arguments); | 9328 : Symbols::CheckAssertion()), |
| 9329 arguments); |
| 9330 |
| 9331 return assert_throw; |
9314 } | 9332 } |
9315 | 9333 |
9316 | 9334 |
9317 // Populate local scope of the catch block with the catch parameters. | 9335 // Populate local scope of the catch block with the catch parameters. |
9318 void Parser::AddCatchParamsToScope(CatchParamDesc* exception_param, | 9336 void Parser::AddCatchParamsToScope(CatchParamDesc* exception_param, |
9319 CatchParamDesc* stack_trace_param, | 9337 CatchParamDesc* stack_trace_param, |
9320 LocalScope* scope) { | 9338 LocalScope* scope) { |
9321 if (exception_param->name != NULL) { | 9339 if (exception_param->name != NULL) { |
9322 LocalVariable* var = new(Z) LocalVariable( | 9340 LocalVariable* var = new(Z) LocalVariable( |
9323 exception_param->token_pos, | 9341 exception_param->token_pos, |
(...skipping 5417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
14741 const ArgumentListNode& function_args, | 14759 const ArgumentListNode& function_args, |
14742 const LocalVariable* temp_for_last_arg, | 14760 const LocalVariable* temp_for_last_arg, |
14743 bool is_super_invocation) { | 14761 bool is_super_invocation) { |
14744 UNREACHABLE(); | 14762 UNREACHABLE(); |
14745 return NULL; | 14763 return NULL; |
14746 } | 14764 } |
14747 | 14765 |
14748 } // namespace dart | 14766 } // namespace dart |
14749 | 14767 |
14750 #endif // DART_PRECOMPILED_RUNTIME | 14768 #endif // DART_PRECOMPILED_RUNTIME |
OLD | NEW |