Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: runtime/vm/parser.cc

Issue 8682025: Allow optional initializing formals again (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/language/language.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 "vm/bigint_operations.h" 7 #include "vm/bigint_operations.h"
8 #include "vm/class_finalizer.h" 8 #include "vm/class_finalizer.h"
9 #include "vm/compiler.h" 9 #include "vm/compiler.h"
10 #include "vm/compiler_stats.h" 10 #include "vm/compiler_stats.h"
(...skipping 820 matching lines...) Expand 10 before | Expand all | Expand 10 after
831 parameter.is_field_initializer = true; 831 parameter.is_field_initializer = true;
832 } 832 }
833 // At this point, we must see an identifier for the parameter name. 833 // At this point, we must see an identifier for the parameter name.
834 if (CurrentToken() != Token::kIDENT) { 834 if (CurrentToken() != Token::kIDENT) {
835 ErrorMsg("parameter name expected"); 835 ErrorMsg("parameter name expected");
836 } 836 }
837 parameter.name = CurrentLiteral(); 837 parameter.name = CurrentLiteral();
838 parameter.name_pos = token_index_; 838 parameter.name_pos = token_index_;
839 ConsumeToken(); 839 ConsumeToken();
840 if (parameter.is_field_initializer) { 840 if (parameter.is_field_initializer) {
841 if (params->has_named_optional_parameters) {
842 ErrorMsg(parameter.name_pos,
843 "initializing formal parameter cannot be optional");
844 }
845 params->has_field_initializer = true; 841 params->has_field_initializer = true;
846 } 842 }
847 843
848 if (CurrentToken() == Token::kLPAREN) { 844 if (CurrentToken() == Token::kLPAREN) {
849 // This parameter is probably a closure. If we saw the keyword 'var' 845 // This parameter is probably a closure. If we saw the keyword 'var'
850 // or 'final', a closure is not legal here and we ignore the 846 // or 'final', a closure is not legal here and we ignore the
851 // opening parens. 847 // opening parens.
852 if (!var_seen && !parameter.is_final) { 848 if (!var_seen && !parameter.is_final) {
853 // The parsed parameter type is actually the function result type. 849 // The parsed parameter type is actually the function result type.
854 const Type& result_type = Type::Handle(parameter.type->raw()); 850 const Type& result_type = Type::Handle(parameter.type->raw());
(...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after
1595 AstNode* instance = new LoadLocalNode(field->token_index(), *receiver); 1591 AstNode* instance = new LoadLocalNode(field->token_index(), *receiver);
1596 AstNode* field_init = 1592 AstNode* field_init =
1597 new StoreInstanceFieldNode(field->token_index(), 1593 new StoreInstanceFieldNode(field->token_index(),
1598 instance, 1594 instance,
1599 *field, 1595 *field,
1600 initializers[i].expr); 1596 initializers[i].expr);
1601 current_block_->statements->Add(field_init); 1597 current_block_->statements->Add(field_init);
1602 } 1598 }
1603 1599
1604 // Turn formal field parameters into field initializers or report error 1600 // Turn formal field parameters into field initializers or report error
1605 // if the function is not a constructor 1601 // if the function is not a constructor.
1606 if (params.has_field_initializer) { 1602 if (params.has_field_initializer) {
1607 for (int i = 0; i < params.parameters->length(); i++) { 1603 for (int i = 0; i < params.parameters->length(); i++) {
1608 ParamDesc& param = (*params.parameters)[i]; 1604 ParamDesc& param = (*params.parameters)[i];
1609 if (param.is_field_initializer) { 1605 if (param.is_field_initializer) {
1610 const String& field_name = *param.name; 1606 const String& field_name = *param.name;
1611 Field& field = Field::ZoneHandle(cls.LookupInstanceField(field_name)); 1607 Field& field = Field::ZoneHandle(cls.LookupInstanceField(field_name));
1612 if (field.IsNull()) { 1608 if (field.IsNull()) {
1613 ErrorMsg(param.name_pos, 1609 ErrorMsg(param.name_pos,
1614 "unresolved reference to instance field '%s'", 1610 "unresolved reference to instance field '%s'",
1615 field_name.ToCString()); 1611 field_name.ToCString());
1616 } 1612 }
1617 const String& mangled_name =
1618 String::ZoneHandle(MangledInitParamName(field_name));
1619 AstNode* instance = new LoadLocalNode(param.name_pos, *receiver); 1613 AstNode* instance = new LoadLocalNode(param.name_pos, *receiver);
1620 LocalVariable* p = 1614 LocalVariable* p =
1621 current_block_->scope->LookupVariable(mangled_name, false); 1615 current_block_->scope->LookupVariable(*param.name, false);
1622 ASSERT(p != NULL); 1616 ASSERT(p != NULL);
1617 // Initializing formals cannot be used in the explicit initializer
1618 // list, nor can they be used in the constructor body.
1619 // Thus, make the parameter invisible.
1620 p->set_invisible(true);
1623 AstNode* value = new LoadLocalNode(param.name_pos, *p); 1621 AstNode* value = new LoadLocalNode(param.name_pos, *p);
1624 AstNode* initializer = new StoreInstanceFieldNode( 1622 AstNode* initializer = new StoreInstanceFieldNode(
1625 param.name_pos, instance, field, value); 1623 param.name_pos, instance, field, value);
1626 current_block_->statements->Add(initializer); 1624 current_block_->statements->Add(initializer);
1627 } 1625 }
1628 } 1626 }
1629 } 1627 }
1630 1628
1631 // Now parse the explicit initializer list or constructor redirection. 1629 // Now parse the explicit initializer list or constructor redirection.
1632 ParseInitializers(cls, receiver); 1630 ParseInitializers(cls, receiver);
(...skipping 1752 matching lines...) Expand 10 before | Expand all | Expand 10 after
3385 // Populate local scope with the formal parameters. 3383 // Populate local scope with the formal parameters.
3386 void Parser::AddFormalParamsToScope(const ParamList* params, 3384 void Parser::AddFormalParamsToScope(const ParamList* params,
3387 LocalScope* scope) { 3385 LocalScope* scope) {
3388 ASSERT((params != NULL) && (params->parameters != NULL)); 3386 ASSERT((params != NULL) && (params->parameters != NULL));
3389 ASSERT(scope != NULL); 3387 ASSERT(scope != NULL);
3390 const int num_parameters = params->parameters->length(); 3388 const int num_parameters = params->parameters->length();
3391 for (int i = 0; i < num_parameters; i++) { 3389 for (int i = 0; i < num_parameters; i++) {
3392 ParamDesc& param_desc = (*params->parameters)[i]; 3390 ParamDesc& param_desc = (*params->parameters)[i];
3393 ASSERT(!is_top_level_ || param_desc.type->IsResolved()); 3391 ASSERT(!is_top_level_ || param_desc.type->IsResolved());
3394 const String* name = param_desc.name; 3392 const String* name = param_desc.name;
3395 if (param_desc.is_field_initializer) {
3396 name = &String::ZoneHandle(MangledInitParamName(*name));
3397 }
3398 LocalVariable* parameter = new LocalVariable( 3393 LocalVariable* parameter = new LocalVariable(
3399 param_desc.name_pos, *name, *param_desc.type); 3394 param_desc.name_pos, *name, *param_desc.type);
3400 if (!scope->AddVariable(parameter)) { 3395 if (!scope->AddVariable(parameter)) {
3401 ErrorMsg(param_desc.name_pos, 3396 ErrorMsg(param_desc.name_pos,
3402 "name '%s' already exists in scope", 3397 "name '%s' already exists in scope",
3403 param_desc.name->ToCString()); 3398 param_desc.name->ToCString());
3404 } 3399 }
3405 if (param_desc.is_final) { 3400 if (param_desc.is_final) {
3406 parameter->set_is_final(); 3401 parameter->set_is_final();
3407 } 3402 }
(...skipping 4126 matching lines...) Expand 10 before | Expand all | Expand 10 after
7534 } 7529 }
7535 7530
7536 7531
7537 void Parser::SkipNestedExpr() { 7532 void Parser::SkipNestedExpr() {
7538 const bool saved_mode = SetAllowFunctionLiterals(true); 7533 const bool saved_mode = SetAllowFunctionLiterals(true);
7539 SkipExpr(); 7534 SkipExpr();
7540 SetAllowFunctionLiterals(saved_mode); 7535 SetAllowFunctionLiterals(saved_mode);
7541 } 7536 }
7542 7537
7543 } // namespace dart 7538 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | tests/language/language.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698