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 | 6 |
7 #include "lib/invocation_mirror.h" | 7 #include "lib/invocation_mirror.h" |
8 #include "platform/utils.h" | 8 #include "platform/utils.h" |
9 #include "vm/ast_transformer.h" | 9 #include "vm/ast_transformer.h" |
10 #include "vm/bootstrap.h" | 10 #include "vm/bootstrap.h" |
(...skipping 746 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
757 struct TopLevel { | 757 struct TopLevel { |
758 TopLevel() : | 758 TopLevel() : |
759 fields(GrowableObjectArray::Handle(GrowableObjectArray::New())), | 759 fields(GrowableObjectArray::Handle(GrowableObjectArray::New())), |
760 functions(GrowableObjectArray::Handle(GrowableObjectArray::New())) { } | 760 functions(GrowableObjectArray::Handle(GrowableObjectArray::New())) { } |
761 | 761 |
762 GrowableObjectArray& fields; | 762 GrowableObjectArray& fields; |
763 GrowableObjectArray& functions; | 763 GrowableObjectArray& functions; |
764 }; | 764 }; |
765 | 765 |
766 | 766 |
767 static bool HasReturnNode(SequenceNode* seq) { | |
768 if (seq->length() == 0) { | |
769 return false; | |
770 } else if ((seq->length()) == 1 && | |
771 (seq->NodeAt(seq->length() - 1)->IsSequenceNode())) { | |
772 return HasReturnNode(seq->NodeAt(seq->length() - 1)->AsSequenceNode()); | |
773 } else { | |
774 return seq->NodeAt(seq->length() - 1)->IsReturnNode(); | |
775 } | |
776 } | |
777 | |
778 | |
779 void Parser::ParseClass(const Class& cls) { | 767 void Parser::ParseClass(const Class& cls) { |
780 if (!cls.is_synthesized_class()) { | 768 if (!cls.is_synthesized_class()) { |
781 Isolate* isolate = Isolate::Current(); | 769 Isolate* isolate = Isolate::Current(); |
782 TimerScope timer(FLAG_compiler_stats, &CompilerStats::parser_timer); | 770 TimerScope timer(FLAG_compiler_stats, &CompilerStats::parser_timer); |
783 ASSERT(isolate->long_jump_base()->IsSafeToJump()); | 771 ASSERT(isolate->long_jump_base()->IsSafeToJump()); |
784 const Script& script = Script::Handle(isolate, cls.script()); | 772 const Script& script = Script::Handle(isolate, cls.script()); |
785 const Library& lib = Library::Handle(isolate, cls.library()); | 773 const Library& lib = Library::Handle(isolate, cls.library()); |
786 Parser parser(script, lib, cls.token_pos()); | 774 Parser parser(script, lib, cls.token_pos()); |
787 parser.ParseClassDefinition(cls); | 775 parser.ParseClassDefinition(cls); |
788 } else if (cls.is_enum_class()) { | 776 } else if (cls.is_enum_class()) { |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 case RawFunction::kInvokeFieldDispatcher: | 887 case RawFunction::kInvokeFieldDispatcher: |
900 node_sequence = | 888 node_sequence = |
901 parser.ParseInvokeFieldDispatcher(func, &default_parameter_values); | 889 parser.ParseInvokeFieldDispatcher(func, &default_parameter_values); |
902 break; | 890 break; |
903 case RawFunction::kIrregexpFunction: | 891 case RawFunction::kIrregexpFunction: |
904 UNREACHABLE(); // Irregexp functions have their own parser. | 892 UNREACHABLE(); // Irregexp functions have their own parser. |
905 default: | 893 default: |
906 UNREACHABLE(); | 894 UNREACHABLE(); |
907 } | 895 } |
908 | 896 |
909 if (!HasReturnNode(node_sequence)) { | |
910 // Add implicit return node. The implicit return value of synchronous | |
911 // generator closures is false, to indicate that there are no more | |
912 // elements in the iterable. In other cases the implicit return value | |
913 // is null. | |
914 AstNode* return_value = func.IsSyncGenClosure() | |
915 ? new LiteralNode(func.end_token_pos(), Bool::False()) | |
916 : new LiteralNode(func.end_token_pos(), Instance::ZoneHandle()); | |
917 node_sequence->Add(new ReturnNode(func.end_token_pos(), return_value)); | |
918 } | |
919 if (parsed_function->has_expression_temp_var()) { | 897 if (parsed_function->has_expression_temp_var()) { |
920 node_sequence->scope()->AddVariable(parsed_function->expression_temp_var()); | 898 node_sequence->scope()->AddVariable(parsed_function->expression_temp_var()); |
921 } | 899 } |
922 node_sequence->scope()->AddVariable( | 900 node_sequence->scope()->AddVariable( |
923 parsed_function->current_context_var()); | 901 parsed_function->current_context_var()); |
924 if (parsed_function->has_finally_return_temp_var()) { | 902 if (parsed_function->has_finally_return_temp_var()) { |
925 node_sequence->scope()->AddVariable( | 903 node_sequence->scope()->AddVariable( |
926 parsed_function->finally_return_temp_var()); | 904 parsed_function->finally_return_temp_var()); |
927 } | 905 } |
928 parsed_function->SetNodeSequence(node_sequence); | 906 parsed_function->SetNodeSequence(node_sequence); |
(...skipping 2410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3339 body = CloseSyncGenFunction(generated_body_closure, body); | 3317 body = CloseSyncGenFunction(generated_body_closure, body); |
3340 generated_body_closure.set_end_token_pos(end_token_pos); | 3318 generated_body_closure.set_end_token_pos(end_token_pos); |
3341 } else if (func.IsSyncGenClosure()) { | 3319 } else if (func.IsSyncGenClosure()) { |
3342 body->scope()->RecursivelyCaptureAllVariables(); | 3320 body->scope()->RecursivelyCaptureAllVariables(); |
3343 } else if (func.IsAsyncGenerator()) { | 3321 } else if (func.IsAsyncGenerator()) { |
3344 body = CloseAsyncGeneratorFunction(generated_body_closure, body); | 3322 body = CloseAsyncGeneratorFunction(generated_body_closure, body); |
3345 generated_body_closure.set_end_token_pos(end_token_pos); | 3323 generated_body_closure.set_end_token_pos(end_token_pos); |
3346 } else if (func.IsAsyncGenClosure()) { | 3324 } else if (func.IsAsyncGenClosure()) { |
3347 body = CloseAsyncGeneratorClosure(body); | 3325 body = CloseAsyncGeneratorClosure(body); |
3348 } | 3326 } |
| 3327 EnsureHasReturnStatement(body, end_token_pos); |
3349 current_block_->statements->Add(body); | 3328 current_block_->statements->Add(body); |
3350 innermost_function_ = saved_innermost_function.raw(); | 3329 innermost_function_ = saved_innermost_function.raw(); |
3351 last_used_try_index_ = saved_try_index; | 3330 last_used_try_index_ = saved_try_index; |
3352 async_temp_scope_ = saved_async_temp_scope; | 3331 async_temp_scope_ = saved_async_temp_scope; |
3353 return CloseBlock(); | 3332 return CloseBlock(); |
3354 } | 3333 } |
3355 | 3334 |
3356 | 3335 |
3357 void Parser::AddEqualityNullCheck() { | 3336 void Parser::AddEqualityNullCheck() { |
3358 AstNode* argument = | 3337 AstNode* argument = |
(...skipping 3471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6830 new_body->scope()->LookupVariable(Symbols::Controller(), false); | 6809 new_body->scope()->LookupVariable(Symbols::Controller(), false); |
6831 new_body->scope()->LookupVariable(Symbols::AsyncOperationParam(), false); | 6810 new_body->scope()->LookupVariable(Symbols::AsyncOperationParam(), false); |
6832 new_body->scope()->LookupVariable(Symbols::AsyncOperationErrorParam(), false); | 6811 new_body->scope()->LookupVariable(Symbols::AsyncOperationErrorParam(), false); |
6833 new_body->scope()->LookupVariable( | 6812 new_body->scope()->LookupVariable( |
6834 Symbols::AsyncOperationStackTraceParam(), false); | 6813 Symbols::AsyncOperationStackTraceParam(), false); |
6835 new_body->scope()->RecursivelyCaptureAllVariables(); | 6814 new_body->scope()->RecursivelyCaptureAllVariables(); |
6836 return new_body; | 6815 return new_body; |
6837 } | 6816 } |
6838 | 6817 |
6839 | 6818 |
| 6819 // Add a return node to the sequence if necessary. |
| 6820 void Parser::EnsureHasReturnStatement(SequenceNode* seq, intptr_t return_pos) { |
| 6821 if ((seq->length() == 0) || |
| 6822 !seq->NodeAt(seq->length() - 1)->IsReturnNode()) { |
| 6823 const Function& func = innermost_function(); |
| 6824 // The implicit return value of synchronous generator closures is false, |
| 6825 // to indicate that there are no more elements in the iterable. |
| 6826 // In other cases the implicit return value is null. |
| 6827 AstNode* return_value = func.IsSyncGenClosure() |
| 6828 ? new LiteralNode(return_pos, Bool::False()) |
| 6829 : new LiteralNode(return_pos, Instance::ZoneHandle()); |
| 6830 seq->Add(new ReturnNode(return_pos, return_value)); |
| 6831 } |
| 6832 } |
| 6833 |
| 6834 |
6840 SequenceNode* Parser::CloseBlock() { | 6835 SequenceNode* Parser::CloseBlock() { |
6841 SequenceNode* statements = current_block_->statements; | 6836 SequenceNode* statements = current_block_->statements; |
6842 if (current_block_->scope != NULL) { | 6837 if (current_block_->scope != NULL) { |
6843 // Record the begin and end token index of the scope. | 6838 // Record the begin and end token index of the scope. |
6844 ASSERT(statements != NULL); | 6839 ASSERT(statements != NULL); |
6845 current_block_->scope->set_begin_token_pos(statements->token_pos()); | 6840 current_block_->scope->set_begin_token_pos(statements->token_pos()); |
6846 current_block_->scope->set_end_token_pos(TokenPos()); | 6841 current_block_->scope->set_end_token_pos(TokenPos()); |
6847 } | 6842 } |
6848 current_block_ = current_block_->parent; | 6843 current_block_ = current_block_->parent; |
6849 return statements; | 6844 return statements; |
(...skipping 6563 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
13413 void Parser::SkipQualIdent() { | 13408 void Parser::SkipQualIdent() { |
13414 ASSERT(IsIdentifier()); | 13409 ASSERT(IsIdentifier()); |
13415 ConsumeToken(); | 13410 ConsumeToken(); |
13416 if (CurrentToken() == Token::kPERIOD) { | 13411 if (CurrentToken() == Token::kPERIOD) { |
13417 ConsumeToken(); // Consume the kPERIOD token. | 13412 ConsumeToken(); // Consume the kPERIOD token. |
13418 ExpectIdentifier("identifier expected after '.'"); | 13413 ExpectIdentifier("identifier expected after '.'"); |
13419 } | 13414 } |
13420 } | 13415 } |
13421 | 13416 |
13422 } // namespace dart | 13417 } // namespace dart |
OLD | NEW |