Chromium Code Reviews| 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 5901 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 6830 new_body->scope()->LookupVariable(Symbols::Controller(), false); | 6808 new_body->scope()->LookupVariable(Symbols::Controller(), false); |
| 6831 new_body->scope()->LookupVariable(Symbols::AsyncOperationParam(), false); | 6809 new_body->scope()->LookupVariable(Symbols::AsyncOperationParam(), false); |
| 6832 new_body->scope()->LookupVariable(Symbols::AsyncOperationErrorParam(), false); | 6810 new_body->scope()->LookupVariable(Symbols::AsyncOperationErrorParam(), false); |
| 6833 new_body->scope()->LookupVariable( | 6811 new_body->scope()->LookupVariable( |
| 6834 Symbols::AsyncOperationStackTraceParam(), false); | 6812 Symbols::AsyncOperationStackTraceParam(), false); |
| 6835 new_body->scope()->RecursivelyCaptureAllVariables(); | 6813 new_body->scope()->RecursivelyCaptureAllVariables(); |
| 6836 return new_body; | 6814 return new_body; |
| 6837 } | 6815 } |
| 6838 | 6816 |
| 6839 | 6817 |
| 6818 // This is called for the compiled function's outermost node sequence. | |
| 6819 // Ensure that the node sequence S containing the function's code ends | |
| 6820 // with a return statement. S may be a child sequence of seq. | |
|
regis
2015/03/31 18:44:41
The comment does not explain the logic implemented
| |
| 6821 void Parser::EnsureHasReturnNode(SequenceNode* seq) { | |
| 6822 // Find innermost child sequence that ends at the same token position. | |
| 6823 while ((seq->length() == 1) && (seq->NodeAt(0)->IsSequenceNode())) { | |
| 6824 SequenceNode* child = seq->NodeAt(0)->AsSequenceNode(); | |
| 6825 if ((seq->scope() != NULL) && | |
| 6826 (child->scope() != NULL) && | |
| 6827 (seq->scope()->end_token_pos() == child->scope()->end_token_pos())) { | |
| 6828 seq = child; | |
| 6829 } else { | |
| 6830 break; | |
| 6831 } | |
| 6832 } | |
| 6833 // Add a return node if necessary. | |
| 6834 if ((seq->length() == 0) || | |
| 6835 !seq->NodeAt(seq->length() - 1)->IsReturnNode()) { | |
| 6836 // The implicit return value of synchronous generator closures is false, | |
| 6837 // to indicate that there are no more elements in the iterable. | |
| 6838 // In other cases the implicit return value is null. | |
| 6839 ASSERT(current_function().raw() == innermost_function().raw()); | |
| 6840 const Function& func = current_function(); | |
| 6841 AstNode* return_value = func.IsSyncGenClosure() | |
| 6842 ? new LiteralNode(func.end_token_pos(), Bool::False()) | |
| 6843 : new LiteralNode(func.end_token_pos(), Instance::ZoneHandle()); | |
| 6844 seq->Add(new ReturnNode(func.end_token_pos(), return_value)); | |
| 6845 } | |
| 6846 } | |
| 6847 | |
| 6848 | |
| 6840 SequenceNode* Parser::CloseBlock() { | 6849 SequenceNode* Parser::CloseBlock() { |
| 6841 SequenceNode* statements = current_block_->statements; | 6850 SequenceNode* statements = current_block_->statements; |
| 6842 if (current_block_->scope != NULL) { | 6851 if (current_block_->scope != NULL) { |
| 6843 // Record the begin and end token index of the scope. | 6852 // Record the begin and end token index of the scope. |
| 6844 ASSERT(statements != NULL); | 6853 ASSERT(statements != NULL); |
| 6845 current_block_->scope->set_begin_token_pos(statements->token_pos()); | 6854 current_block_->scope->set_begin_token_pos(statements->token_pos()); |
| 6846 current_block_->scope->set_end_token_pos(TokenPos()); | 6855 current_block_->scope->set_end_token_pos(TokenPos()); |
| 6847 } | 6856 } |
| 6848 current_block_ = current_block_->parent; | 6857 current_block_ = current_block_->parent; |
| 6858 if (current_block_ == NULL) { | |
| 6859 ASSERT(statements != NULL); | |
| 6860 EnsureHasReturnNode(statements); | |
|
regis
2015/03/31 18:44:41
This does not feel right to me. Closing a block sh
| |
| 6861 } | |
| 6849 return statements; | 6862 return statements; |
| 6850 } | 6863 } |
| 6851 | 6864 |
| 6852 | 6865 |
| 6853 SequenceNode* Parser::CloseAsyncFunction(const Function& closure, | 6866 SequenceNode* Parser::CloseAsyncFunction(const Function& closure, |
| 6854 SequenceNode* closure_body) { | 6867 SequenceNode* closure_body) { |
| 6855 TRACE_PARSER("CloseAsyncFunction"); | 6868 TRACE_PARSER("CloseAsyncFunction"); |
| 6856 ASSERT(!closure.IsNull()); | 6869 ASSERT(!closure.IsNull()); |
| 6857 ASSERT(closure_body != NULL); | 6870 ASSERT(closure_body != NULL); |
| 6858 | 6871 |
| (...skipping 6554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 13413 void Parser::SkipQualIdent() { | 13426 void Parser::SkipQualIdent() { |
| 13414 ASSERT(IsIdentifier()); | 13427 ASSERT(IsIdentifier()); |
| 13415 ConsumeToken(); | 13428 ConsumeToken(); |
| 13416 if (CurrentToken() == Token::kPERIOD) { | 13429 if (CurrentToken() == Token::kPERIOD) { |
| 13417 ConsumeToken(); // Consume the kPERIOD token. | 13430 ConsumeToken(); // Consume the kPERIOD token. |
| 13418 ExpectIdentifier("identifier expected after '.'"); | 13431 ExpectIdentifier("identifier expected after '.'"); |
| 13419 } | 13432 } |
| 13420 } | 13433 } |
| 13421 | 13434 |
| 13422 } // namespace dart | 13435 } // namespace dart |
| OLD | NEW |