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

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

Issue 1316763002: Clean up default parameter computation in parser (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Address review comments Created 5 years, 4 months 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
« no previous file with comments | « runtime/vm/parser.h ('k') | no next file » | 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) 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 872 matching lines...) Expand 10 before | Expand all | Expand 10 after
883 INC_STAT(isolate, num_functions_compiled, 1); 883 INC_STAT(isolate, num_functions_compiled, 1);
884 VMTagScope tagScope(thread, VMTag::kCompileParseFunctionTagId, 884 VMTagScope tagScope(thread, VMTag::kCompileParseFunctionTagId,
885 FLAG_profile_vm); 885 FLAG_profile_vm);
886 886
887 ASSERT(thread->long_jump_base()->IsSafeToJump()); 887 ASSERT(thread->long_jump_base()->IsSafeToJump());
888 ASSERT(parsed_function != NULL); 888 ASSERT(parsed_function != NULL);
889 const Function& func = parsed_function->function(); 889 const Function& func = parsed_function->function();
890 const Script& script = Script::Handle(zone, func.script()); 890 const Script& script = Script::Handle(zone, func.script());
891 Parser parser(script, parsed_function, func.token_pos()); 891 Parser parser(script, parsed_function, func.token_pos());
892 SequenceNode* node_sequence = NULL; 892 SequenceNode* node_sequence = NULL;
893 ZoneGrowableArray<const Instance*>* default_parameter_values =
894 new ZoneGrowableArray<const Instance*>(zone, 2);
895 switch (func.kind()) { 893 switch (func.kind()) {
896 case RawFunction::kClosureFunction: 894 case RawFunction::kClosureFunction:
897 if (func.IsImplicitClosureFunction()) { 895 if (func.IsImplicitClosureFunction()) {
898 node_sequence = 896 node_sequence =
899 parser.ParseImplicitClosure(func, default_parameter_values); 897 parser.ParseImplicitClosure(func);
900 break; 898 break;
901 } 899 }
902 if (func.IsConstructorClosureFunction()) { 900 if (func.IsConstructorClosureFunction()) {
903 node_sequence = 901 node_sequence =
904 parser.ParseConstructorClosure(func, default_parameter_values); 902 parser.ParseConstructorClosure(func);
905 break; 903 break;
906 } 904 }
907 // Fall-through: Handle non-implicit closures. 905 // Fall-through: Handle non-implicit closures.
908 case RawFunction::kRegularFunction: 906 case RawFunction::kRegularFunction:
909 case RawFunction::kGetterFunction: 907 case RawFunction::kGetterFunction:
910 case RawFunction::kSetterFunction: 908 case RawFunction::kSetterFunction:
911 case RawFunction::kConstructor: 909 case RawFunction::kConstructor:
912 // The call to a redirecting factory is redirected. 910 // The call to a redirecting factory is redirected.
913 ASSERT(!func.IsRedirectingFactory()); 911 ASSERT(!func.IsRedirectingFactory());
914 if (!func.IsImplicitConstructor()) { 912 if (!func.IsImplicitConstructor()) {
915 parser.SkipFunctionPreamble(); 913 parser.SkipFunctionPreamble();
916 } 914 }
917 node_sequence = parser.ParseFunc(func, default_parameter_values); 915 node_sequence = parser.ParseFunc(func);
918 break; 916 break;
919 case RawFunction::kImplicitGetter: 917 case RawFunction::kImplicitGetter:
920 ASSERT(!func.is_static()); 918 ASSERT(!func.is_static());
921 node_sequence = parser.ParseInstanceGetter(func); 919 node_sequence = parser.ParseInstanceGetter(func);
922 break; 920 break;
923 case RawFunction::kImplicitSetter: 921 case RawFunction::kImplicitSetter:
924 ASSERT(!func.is_static()); 922 ASSERT(!func.is_static());
925 node_sequence = parser.ParseInstanceSetter(func); 923 node_sequence = parser.ParseInstanceSetter(func);
926 break; 924 break;
927 case RawFunction::kImplicitStaticFinalGetter: 925 case RawFunction::kImplicitStaticFinalGetter:
928 node_sequence = parser.ParseStaticFinalGetter(func); 926 node_sequence = parser.ParseStaticFinalGetter(func);
929 INC_STAT(isolate, num_implicit_final_getters, 1); 927 INC_STAT(isolate, num_implicit_final_getters, 1);
930 break; 928 break;
931 case RawFunction::kMethodExtractor: 929 case RawFunction::kMethodExtractor:
932 node_sequence = parser.ParseMethodExtractor(func); 930 node_sequence = parser.ParseMethodExtractor(func);
933 break; 931 break;
934 case RawFunction::kNoSuchMethodDispatcher: 932 case RawFunction::kNoSuchMethodDispatcher:
935 node_sequence = 933 node_sequence =
936 parser.ParseNoSuchMethodDispatcher(func, default_parameter_values); 934 parser.ParseNoSuchMethodDispatcher(func);
937 break; 935 break;
938 case RawFunction::kInvokeFieldDispatcher: 936 case RawFunction::kInvokeFieldDispatcher:
939 node_sequence = 937 node_sequence =
940 parser.ParseInvokeFieldDispatcher(func, default_parameter_values); 938 parser.ParseInvokeFieldDispatcher(func);
941 break; 939 break;
942 case RawFunction::kIrregexpFunction: 940 case RawFunction::kIrregexpFunction:
943 UNREACHABLE(); // Irregexp functions have their own parser. 941 UNREACHABLE(); // Irregexp functions have their own parser.
944 default: 942 default:
945 UNREACHABLE(); 943 UNREACHABLE();
946 } 944 }
947 945
948 if (parsed_function->has_expression_temp_var()) { 946 if (parsed_function->has_expression_temp_var()) {
949 node_sequence->scope()->AddVariable(parsed_function->expression_temp_var()); 947 node_sequence->scope()->AddVariable(parsed_function->expression_temp_var());
950 } 948 }
(...skipping 16 matching lines...) Expand all
967 instantiator = parser.LookupTypeArgumentsParameter(node_sequence->scope(), 965 instantiator = parser.LookupTypeArgumentsParameter(node_sequence->scope(),
968 kTestOnly); 966 kTestOnly);
969 } else { 967 } else {
970 instantiator = parser.LookupReceiver(node_sequence->scope(), kTestOnly); 968 instantiator = parser.LookupReceiver(node_sequence->scope(), kTestOnly);
971 } 969 }
972 if (!parser.current_function().IsLocalFunction() || 970 if (!parser.current_function().IsLocalFunction() ||
973 ((instantiator != NULL) && instantiator->is_captured())) { 971 ((instantiator != NULL) && instantiator->is_captured())) {
974 parsed_function->set_instantiator(instantiator); 972 parsed_function->set_instantiator(instantiator);
975 } 973 }
976 } 974 }
977
978 parsed_function->set_default_parameter_values(default_parameter_values);
979 } 975 }
980 976
981 977
982 RawObject* Parser::ParseMetadata(const Class& cls, intptr_t token_pos) { 978 RawObject* Parser::ParseMetadata(const Class& cls, intptr_t token_pos) {
983 Thread* thread = Thread::Current(); 979 Thread* thread = Thread::Current();
984 Isolate* isolate = thread->isolate(); 980 Isolate* isolate = thread->isolate();
985 StackZone stack_zone(thread); 981 StackZone stack_zone(thread);
986 Zone* zone = stack_zone.GetZone(); 982 Zone* zone = stack_zone.GetZone();
987 LongJumpScope jump; 983 LongJumpScope jump;
988 if (setjmp(*jump.Set()) == 0) { 984 if (setjmp(*jump.Set()) == 0) {
(...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after
1327 1323
1328 EnsureExpressionTemp(); 1324 EnsureExpressionTemp();
1329 StoreInstanceFieldNode* store_field = 1325 StoreInstanceFieldNode* store_field =
1330 new StoreInstanceFieldNode(ident_pos, receiver, field, value); 1326 new StoreInstanceFieldNode(ident_pos, receiver, field, value);
1331 current_block_->statements->Add(store_field); 1327 current_block_->statements->Add(store_field);
1332 current_block_->statements->Add(new ReturnNode(Scanner::kNoSourcePos)); 1328 current_block_->statements->Add(new ReturnNode(Scanner::kNoSourcePos));
1333 return CloseBlock(); 1329 return CloseBlock();
1334 } 1330 }
1335 1331
1336 1332
1337 SequenceNode* Parser::ParseConstructorClosure( 1333 SequenceNode* Parser::ParseConstructorClosure(const Function& func) {
1338 const Function& func,
1339 ZoneGrowableArray<const Instance*>* default_values) {
1340 TRACE_PARSER("ParseConstructorClosure"); 1334 TRACE_PARSER("ParseConstructorClosure");
1341 const intptr_t token_pos = func.token_pos(); 1335 const intptr_t token_pos = func.token_pos();
1342 1336
1343 Function& constructor = Function::ZoneHandle(Z); 1337 Function& constructor = Function::ZoneHandle(Z);
1344 TypeArguments& type_args = TypeArguments::ZoneHandle(Z); 1338 TypeArguments& type_args = TypeArguments::ZoneHandle(Z);
1345 ParseConstructorClosurization(&constructor, &type_args); 1339 ParseConstructorClosurization(&constructor, &type_args);
1346 ASSERT(!constructor.IsNull()); 1340 ASSERT(!constructor.IsNull());
1347 1341
1348 ParamList params; 1342 ParamList params;
1349 // The first parameter of the closure function is the implicit closure 1343 // The first parameter of the closure function is the implicit closure
1350 // argument. 1344 // argument.
1351 params.AddFinalParameter(token_pos, 1345 params.AddFinalParameter(token_pos,
1352 &Symbols::ClosureParameter(), 1346 &Symbols::ClosureParameter(),
1353 &Type::ZoneHandle(Z, Type::DynamicType())); 1347 &Type::ZoneHandle(Z, Type::DynamicType()));
1354 bool params_ok = ParseFormalParameters(constructor, &params); 1348 bool params_ok = ParseFormalParameters(constructor, &params);
1355 USE(params_ok); 1349 USE(params_ok);
1356 ASSERT(params_ok); 1350 ASSERT(params_ok);
1357 // Per language spec, the type of the closure parameters is dynamic. 1351 // Per language spec, the type of the closure parameters is dynamic.
1358 // Replace the types parsed from the constructor. 1352 // Replace the types parsed from the constructor.
1359 params.EraseParameterTypes(); 1353 params.EraseParameterTypes();
1360 1354
1361 SetupDefaultsForOptionalParams(params, default_values); 1355 SetupDefaultsForOptionalParams(params);
1362 ASSERT(func.num_fixed_parameters() == params.num_fixed_parameters); 1356 ASSERT(func.num_fixed_parameters() == params.num_fixed_parameters);
1363 ASSERT(func.NumOptionalParameters() == params.num_optional_parameters); 1357 ASSERT(func.NumOptionalParameters() == params.num_optional_parameters);
1364 1358
1365 OpenFunctionBlock(func); 1359 OpenFunctionBlock(func);
1366 LocalScope* scope = current_block_->scope; 1360 LocalScope* scope = current_block_->scope;
1367 AddFormalParamsToScope(&params, scope); 1361 AddFormalParamsToScope(&params, scope);
1368 1362
1369 ArgumentListNode* ctor_args = new ArgumentListNode(token_pos); 1363 ArgumentListNode* ctor_args = new ArgumentListNode(token_pos);
1370 // Skip implicit closure parameter at 0. 1364 // Skip implicit closure parameter at 0.
1371 for (intptr_t i = 1; i < func.NumParameters(); i++) { 1365 for (intptr_t i = 1; i < func.NumParameters(); i++) {
(...skipping 11 matching lines...) Expand all
1383 } 1377 }
1384 1378
1385 AstNode* new_object = 1379 AstNode* new_object =
1386 CreateConstructorCallNode(token_pos, type_args, constructor, ctor_args); 1380 CreateConstructorCallNode(token_pos, type_args, constructor, ctor_args);
1387 ReturnNode* return_node = new ReturnNode(token_pos, new_object); 1381 ReturnNode* return_node = new ReturnNode(token_pos, new_object);
1388 current_block_->statements->Add(return_node); 1382 current_block_->statements->Add(return_node);
1389 return CloseBlock(); 1383 return CloseBlock();
1390 } 1384 }
1391 1385
1392 1386
1393 SequenceNode* Parser::ParseImplicitClosure( 1387 SequenceNode* Parser::ParseImplicitClosure(const Function& func) {
1394 const Function& func, ZoneGrowableArray<const Instance*>* default_values) {
1395 TRACE_PARSER("ParseImplicitClosure"); 1388 TRACE_PARSER("ParseImplicitClosure");
1396 intptr_t token_pos = func.token_pos(); 1389 intptr_t token_pos = func.token_pos();
1397 1390
1398 OpenFunctionBlock(func); 1391 OpenFunctionBlock(func);
1399 1392
1400 ParamList params; 1393 ParamList params;
1401 params.AddFinalParameter( 1394 params.AddFinalParameter(
1402 token_pos, 1395 token_pos,
1403 &Symbols::ClosureParameter(), 1396 &Symbols::ClosureParameter(),
1404 &Type::ZoneHandle(Type::DynamicType())); 1397 &Type::ZoneHandle(Type::DynamicType()));
1405 1398
1406 const Function& parent = Function::ZoneHandle(func.parent_function()); 1399 const Function& parent = Function::ZoneHandle(func.parent_function());
1407 if (parent.IsImplicitSetterFunction()) { 1400 if (parent.IsImplicitSetterFunction()) {
1408 const intptr_t ident_pos = func.token_pos(); 1401 const intptr_t ident_pos = func.token_pos();
1409 ASSERT(IsIdentifier()); 1402 ASSERT(IsIdentifier());
1410 const String& field_name = *CurrentLiteral(); 1403 const String& field_name = *CurrentLiteral();
1411 const Class& field_class = Class::ZoneHandle(Z, parent.Owner()); 1404 const Class& field_class = Class::ZoneHandle(Z, parent.Owner());
1412 const Field& field = 1405 const Field& field =
1413 Field::ZoneHandle(Z, field_class.LookupInstanceField(field_name)); 1406 Field::ZoneHandle(Z, field_class.LookupInstanceField(field_name));
1414 const AbstractType& field_type = AbstractType::ZoneHandle(Z, field.type()); 1407 const AbstractType& field_type = AbstractType::ZoneHandle(Z, field.type());
1415 params.AddFinalParameter(ident_pos, 1408 params.AddFinalParameter(ident_pos,
1416 &Symbols::Value(), 1409 &Symbols::Value(),
1417 &field_type); 1410 &field_type);
1418 ASSERT(func.num_fixed_parameters() == 2); // closure, value. 1411 ASSERT(func.num_fixed_parameters() == 2); // closure, value.
1419 } else if (!parent.IsGetterFunction() && !parent.IsImplicitGetterFunction()) { 1412 } else if (!parent.IsGetterFunction() && !parent.IsImplicitGetterFunction()) {
1420 const bool allow_explicit_default_values = true; 1413 const bool allow_explicit_default_values = true;
1421 SkipFunctionPreamble(); 1414 SkipFunctionPreamble();
1422 ParseFormalParameterList(allow_explicit_default_values, false, &params); 1415 ParseFormalParameterList(allow_explicit_default_values, false, &params);
1423 SetupDefaultsForOptionalParams(params, default_values); 1416 SetupDefaultsForOptionalParams(params);
1424 } 1417 }
1425 1418
1426 // Populate function scope with the formal parameters. 1419 // Populate function scope with the formal parameters.
1427 LocalScope* scope = current_block_->scope; 1420 LocalScope* scope = current_block_->scope;
1428 AddFormalParamsToScope(&params, scope); 1421 AddFormalParamsToScope(&params, scope);
1429 1422
1430 ArgumentListNode* func_args = new ArgumentListNode(token_pos); 1423 ArgumentListNode* func_args = new ArgumentListNode(token_pos);
1431 if (!func.is_static()) { 1424 if (!func.is_static()) {
1432 func_args->Add(LoadReceiver(token_pos)); 1425 func_args->Add(LoadReceiver(token_pos));
1433 } 1426 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1478 Function::ZoneHandle(Z, func.extracted_method_closure()), 1471 Function::ZoneHandle(Z, func.extracted_method_closure()),
1479 load_receiver, 1472 load_receiver,
1480 NULL); 1473 NULL);
1481 1474
1482 ReturnNode* return_node = new ReturnNode(Scanner::kNoSourcePos, closure); 1475 ReturnNode* return_node = new ReturnNode(Scanner::kNoSourcePos, closure);
1483 current_block_->statements->Add(return_node); 1476 current_block_->statements->Add(return_node);
1484 return CloseBlock(); 1477 return CloseBlock();
1485 } 1478 }
1486 1479
1487 1480
1488 void Parser::BuildDispatcherScope( 1481 void Parser::BuildDispatcherScope(const Function& func,
1489 const Function& func, 1482 const ArgumentsDescriptor& desc) {
1490 const ArgumentsDescriptor& desc,
1491 ZoneGrowableArray<const Instance*>* default_values) {
1492 ParamList params; 1483 ParamList params;
1493 // Receiver first. 1484 // Receiver first.
1494 intptr_t token_pos = func.token_pos(); 1485 intptr_t token_pos = func.token_pos();
1495 params.AddReceiver(ReceiverType(current_class()), token_pos); 1486 params.AddReceiver(ReceiverType(current_class()), token_pos);
1496 // Remaining positional parameters. 1487 // Remaining positional parameters.
1497 intptr_t i = 1; 1488 intptr_t i = 1;
1498 for (; i < desc.PositionalCount(); ++i) { 1489 for (; i < desc.PositionalCount(); ++i) {
1499 ParamDesc p; 1490 ParamDesc p;
1500 char name[64]; 1491 char name[64];
1501 OS::SNPrint(name, 64, ":p%" Pd, i); 1492 OS::SNPrint(name, 64, ":p%" Pd, i);
(...skipping 10 matching lines...) Expand all
1512 intptr_t index = i - desc.PositionalCount(); 1503 intptr_t index = i - desc.PositionalCount();
1513 p.name = &String::ZoneHandle(Z, desc.NameAt(index)); 1504 p.name = &String::ZoneHandle(Z, desc.NameAt(index));
1514 p.type = &Type::ZoneHandle(Z, Type::DynamicType()); 1505 p.type = &Type::ZoneHandle(Z, Type::DynamicType());
1515 p.default_value = &Object::null_instance(); 1506 p.default_value = &Object::null_instance();
1516 params.parameters->Add(p); 1507 params.parameters->Add(p);
1517 params.num_optional_parameters++; 1508 params.num_optional_parameters++;
1518 params.has_optional_named_parameters = true; 1509 params.has_optional_named_parameters = true;
1519 } 1510 }
1520 ASSERT(desc.NamedCount() == params.num_optional_parameters); 1511 ASSERT(desc.NamedCount() == params.num_optional_parameters);
1521 1512
1522 SetupDefaultsForOptionalParams(params, default_values); 1513 SetupDefaultsForOptionalParams(params);
1523 1514
1524 // Build local scope for function and populate with the formal parameters. 1515 // Build local scope for function and populate with the formal parameters.
1525 OpenFunctionBlock(func); 1516 OpenFunctionBlock(func);
1526 AddFormalParamsToScope(&params, current_block_->scope); 1517 AddFormalParamsToScope(&params, current_block_->scope);
1527 } 1518 }
1528 1519
1529 SequenceNode* Parser::ParseNoSuchMethodDispatcher( 1520
1530 const Function& func, 1521 SequenceNode* Parser::ParseNoSuchMethodDispatcher(const Function& func) {
1531 ZoneGrowableArray<const Instance*>* default_values) {
1532 TRACE_PARSER("ParseNoSuchMethodDispatcher"); 1522 TRACE_PARSER("ParseNoSuchMethodDispatcher");
1533 ASSERT(FLAG_lazy_dispatchers); 1523 ASSERT(FLAG_lazy_dispatchers);
1534
1535 ASSERT(func.IsNoSuchMethodDispatcher()); 1524 ASSERT(func.IsNoSuchMethodDispatcher());
1536 intptr_t token_pos = func.token_pos(); 1525 intptr_t token_pos = func.token_pos();
1537 ASSERT(func.token_pos() == 0); 1526 ASSERT(func.token_pos() == 0);
1538 ASSERT(current_class().raw() == func.Owner()); 1527 ASSERT(current_class().raw() == func.Owner());
1539 1528
1540 ArgumentsDescriptor desc(Array::Handle(Z, func.saved_args_desc())); 1529 ArgumentsDescriptor desc(Array::Handle(Z, func.saved_args_desc()));
1541 ASSERT(desc.Count() > 0); 1530 ASSERT(desc.Count() > 0);
1542 1531
1543 // Set up scope for this function. 1532 // Set up scope for this function.
1544 BuildDispatcherScope(func, desc, default_values); 1533 BuildDispatcherScope(func, desc);
1545 1534
1546 // Receiver is local 0. 1535 // Receiver is local 0.
1547 LocalScope* scope = current_block_->scope; 1536 LocalScope* scope = current_block_->scope;
1548 ArgumentListNode* func_args = new ArgumentListNode(token_pos); 1537 ArgumentListNode* func_args = new ArgumentListNode(token_pos);
1549 for (intptr_t i = 0; i < desc.Count(); ++i) { 1538 for (intptr_t i = 0; i < desc.Count(); ++i) {
1550 func_args->Add(new LoadLocalNode(token_pos, scope->VariableAt(i))); 1539 func_args->Add(new LoadLocalNode(token_pos, scope->VariableAt(i)));
1551 } 1540 }
1552 1541
1553 if (desc.NamedCount() > 0) { 1542 if (desc.NamedCount() > 0) {
1554 const Array& arg_names = 1543 const Array& arg_names =
(...skipping 23 matching lines...) Expand all
1578 } 1567 }
1579 StaticCallNode* call = 1568 StaticCallNode* call =
1580 new StaticCallNode(token_pos, no_such_method, arguments); 1569 new StaticCallNode(token_pos, no_such_method, arguments);
1581 1570
1582 ReturnNode* return_node = new ReturnNode(token_pos, call); 1571 ReturnNode* return_node = new ReturnNode(token_pos, call);
1583 current_block_->statements->Add(return_node); 1572 current_block_->statements->Add(return_node);
1584 return CloseBlock(); 1573 return CloseBlock();
1585 } 1574 }
1586 1575
1587 1576
1588 SequenceNode* Parser::ParseInvokeFieldDispatcher( 1577 SequenceNode* Parser::ParseInvokeFieldDispatcher(const Function& func) {
1589 const Function& func,
1590 ZoneGrowableArray<const Instance*>* default_values) {
1591 TRACE_PARSER("ParseInvokeFieldDispatcher"); 1578 TRACE_PARSER("ParseInvokeFieldDispatcher");
1592 ASSERT(FLAG_lazy_dispatchers); 1579 ASSERT(FLAG_lazy_dispatchers);
1593
1594 ASSERT(func.IsInvokeFieldDispatcher()); 1580 ASSERT(func.IsInvokeFieldDispatcher());
1595 intptr_t token_pos = func.token_pos(); 1581 intptr_t token_pos = func.token_pos();
1596 ASSERT(func.token_pos() == 0); 1582 ASSERT(func.token_pos() == 0);
1597 ASSERT(current_class().raw() == func.Owner()); 1583 ASSERT(current_class().raw() == func.Owner());
1598 1584
1599 const Array& args_desc = Array::Handle(Z, func.saved_args_desc()); 1585 const Array& args_desc = Array::Handle(Z, func.saved_args_desc());
1600 ArgumentsDescriptor desc(args_desc); 1586 ArgumentsDescriptor desc(args_desc);
1601 ASSERT(desc.Count() > 0); 1587 ASSERT(desc.Count() > 0);
1602 1588
1603 // Set up scope for this function. 1589 // Set up scope for this function.
1604 BuildDispatcherScope(func, desc, default_values); 1590 BuildDispatcherScope(func, desc);
1605 1591
1606 // Receiver is local 0. 1592 // Receiver is local 0.
1607 LocalScope* scope = current_block_->scope; 1593 LocalScope* scope = current_block_->scope;
1608 ArgumentListNode* no_args = new ArgumentListNode(token_pos); 1594 ArgumentListNode* no_args = new ArgumentListNode(token_pos);
1609 LoadLocalNode* receiver = new LoadLocalNode(token_pos, scope->VariableAt(0)); 1595 LoadLocalNode* receiver = new LoadLocalNode(token_pos, scope->VariableAt(0));
1610 1596
1611 const Class& function_impl = Class::Handle(Type::Handle( 1597 const Class& function_impl = Class::Handle(Type::Handle(
1612 Isolate::Current()->object_store()->function_impl_type()).type_class()); 1598 Isolate::Current()->object_store()->function_impl_type()).type_class());
1613 1599
1614 const Class& owner = Class::Handle(Z, func.Owner()); 1600 const Class& owner = Class::Handle(Z, func.Owner());
(...skipping 1289 matching lines...) Expand 10 before | Expand all | Expand 10 after
2904 } 2890 }
2905 } 2891 }
2906 ASSERT(!unregister_pending_function_); 2892 ASSERT(!unregister_pending_function_);
2907 pending_functions.Add(current_function()); 2893 pending_functions.Add(current_function());
2908 unregister_pending_function_ = true; 2894 unregister_pending_function_ = true;
2909 } 2895 }
2910 2896
2911 2897
2912 // Parser is at the opening parenthesis of the formal parameter declaration 2898 // Parser is at the opening parenthesis of the formal parameter declaration
2913 // of function. Parse the formal parameters, initializers and code. 2899 // of function. Parse the formal parameters, initializers and code.
2914 SequenceNode* Parser::ParseConstructor( 2900 SequenceNode* Parser::ParseConstructor(const Function& func) {
2915 const Function& func,
2916 ZoneGrowableArray<const Instance*>* default_parameter_values) {
2917 TRACE_PARSER("ParseConstructor"); 2901 TRACE_PARSER("ParseConstructor");
2918 ASSERT(func.IsGenerativeConstructor()); 2902 ASSERT(func.IsGenerativeConstructor());
2919 ASSERT(!func.IsFactory()); 2903 ASSERT(!func.IsFactory());
2920 ASSERT(!func.is_static()); 2904 ASSERT(!func.is_static());
2921 ASSERT(!func.IsLocalFunction()); 2905 ASSERT(!func.IsLocalFunction());
2922 const Class& cls = Class::Handle(Z, func.Owner()); 2906 const Class& cls = Class::Handle(Z, func.Owner());
2923 ASSERT(!cls.IsNull()); 2907 ASSERT(!cls.IsNull());
2924 2908
2925 CheckRecursiveInvocation(); 2909 CheckRecursiveInvocation();
2926 2910
(...skipping 21 matching lines...) Expand all
2948 params.AddFinalParameter( 2932 params.AddFinalParameter(
2949 TokenPos(), 2933 TokenPos(),
2950 &Symbols::PhaseParameter(), 2934 &Symbols::PhaseParameter(),
2951 &Type::ZoneHandle(Z, Type::SmiType())); 2935 &Type::ZoneHandle(Z, Type::SmiType()));
2952 2936
2953 if (func.is_const()) { 2937 if (func.is_const()) {
2954 params.SetImplicitlyFinal(); 2938 params.SetImplicitlyFinal();
2955 } 2939 }
2956 ParseFormalParameterList(allow_explicit_default_values, false, &params); 2940 ParseFormalParameterList(allow_explicit_default_values, false, &params);
2957 2941
2958 SetupDefaultsForOptionalParams(params, default_parameter_values); 2942 SetupDefaultsForOptionalParams(params);
2959 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); 2943 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved());
2960 ASSERT(func.NumParameters() == params.parameters->length()); 2944 ASSERT(func.NumParameters() == params.parameters->length());
2961 2945
2962 // Now populate function scope with the formal parameters. 2946 // Now populate function scope with the formal parameters.
2963 AddFormalParamsToScope(&params, current_block_->scope); 2947 AddFormalParamsToScope(&params, current_block_->scope);
2964 2948
2965 const bool is_redirecting_constructor = 2949 const bool is_redirecting_constructor =
2966 (CurrentToken() == Token::kCOLON) && 2950 (CurrentToken() == Token::kCOLON) &&
2967 ((LookaheadToken(1) == Token::kTHIS) && 2951 ((LookaheadToken(1) == Token::kTHIS) &&
2968 ((LookaheadToken(2) == Token::kLPAREN) || 2952 ((LookaheadToken(2) == Token::kLPAREN) ||
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
3214 } 3198 }
3215 current_block_->statements->Add(new ReturnNode(func.end_token_pos())); 3199 current_block_->statements->Add(new ReturnNode(func.end_token_pos()));
3216 SequenceNode* statements = CloseBlock(); 3200 SequenceNode* statements = CloseBlock();
3217 return statements; 3201 return statements;
3218 } 3202 }
3219 3203
3220 3204
3221 // Parser is at the opening parenthesis of the formal parameter 3205 // Parser is at the opening parenthesis of the formal parameter
3222 // declaration of the function or constructor. 3206 // declaration of the function or constructor.
3223 // Parse the formal parameters and code. 3207 // Parse the formal parameters and code.
3224 SequenceNode* Parser::ParseFunc( 3208 SequenceNode* Parser::ParseFunc(const Function& func) {
3225 const Function& func,
3226 ZoneGrowableArray<const Instance*>* default_parameter_values) {
3227 TRACE_PARSER("ParseFunc"); 3209 TRACE_PARSER("ParseFunc");
3228 Function& saved_innermost_function = 3210 Function& saved_innermost_function =
3229 Function::Handle(Z, innermost_function().raw()); 3211 Function::Handle(Z, innermost_function().raw());
3230 innermost_function_ = func.raw(); 3212 innermost_function_ = func.raw();
3231 3213
3232 // Save current try index. Try index starts at zero for each function. 3214 // Save current try index. Try index starts at zero for each function.
3233 intptr_t saved_try_index = last_used_try_index_; 3215 intptr_t saved_try_index = last_used_try_index_;
3234 last_used_try_index_ = 0; 3216 last_used_try_index_ = 0;
3235 3217
3236 // In case of nested async functions we also need to save the scope where 3218 // In case of nested async functions we also need to save the scope where
3237 // temporaries are added. 3219 // temporaries are added.
3238 LocalScope* saved_async_temp_scope = async_temp_scope_; 3220 LocalScope* saved_async_temp_scope = async_temp_scope_;
3239 3221
3240 if (func.IsGenerativeConstructor()) { 3222 if (func.IsGenerativeConstructor()) {
3241 SequenceNode* statements = ParseConstructor(func, default_parameter_values); 3223 SequenceNode* statements = ParseConstructor(func);
3242 innermost_function_ = saved_innermost_function.raw(); 3224 innermost_function_ = saved_innermost_function.raw();
3243 last_used_try_index_ = saved_try_index; 3225 last_used_try_index_ = saved_try_index;
3244 return statements; 3226 return statements;
3245 } 3227 }
3246 3228
3247 ASSERT(!func.IsGenerativeConstructor()); 3229 ASSERT(!func.IsGenerativeConstructor());
3248 OpenFunctionBlock(func); // Build local scope for function. 3230 OpenFunctionBlock(func); // Build local scope for function.
3249 3231
3250 ParamList params; 3232 ParamList params;
3251 // An instance closure function may capture and access the receiver, but via 3233 // An instance closure function may capture and access the receiver, but via
(...skipping 23 matching lines...) Expand all
3275 func.IsGetterFunction() || 3257 func.IsGetterFunction() ||
3276 (func.is_generated_body() && 3258 (func.is_generated_body() &&
3277 Function::Handle(func.parent_function()).IsGetterFunction())); 3259 Function::Handle(func.parent_function()).IsGetterFunction()));
3278 const bool allow_explicit_default_values = true; 3260 const bool allow_explicit_default_values = true;
3279 if (func.IsGetterFunction()) { 3261 if (func.IsGetterFunction()) {
3280 // Populate function scope with the formal parameters. Since in this case 3262 // Populate function scope with the formal parameters. Since in this case
3281 // we are compiling a getter this will at most populate the receiver. 3263 // we are compiling a getter this will at most populate the receiver.
3282 AddFormalParamsToScope(&params, current_block_->scope); 3264 AddFormalParamsToScope(&params, current_block_->scope);
3283 } else if (func.IsAsyncClosure()) { 3265 } else if (func.IsAsyncClosure()) {
3284 AddAsyncClosureParameters(&params); 3266 AddAsyncClosureParameters(&params);
3285 SetupDefaultsForOptionalParams(params, default_parameter_values); 3267 SetupDefaultsForOptionalParams(params);
3286 AddFormalParamsToScope(&params, current_block_->scope); 3268 AddFormalParamsToScope(&params, current_block_->scope);
3287 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); 3269 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved());
3288 ASSERT(func.NumParameters() == params.parameters->length()); 3270 ASSERT(func.NumParameters() == params.parameters->length());
3289 if (!Function::Handle(func.parent_function()).IsGetterFunction()) { 3271 if (!Function::Handle(func.parent_function()).IsGetterFunction()) {
3290 // Parse and discard any formal parameters. They are accessed as 3272 // Parse and discard any formal parameters. They are accessed as
3291 // context variables. 3273 // context variables.
3292 ParamList discarded_params; 3274 ParamList discarded_params;
3293 ParseFormalParameterList(allow_explicit_default_values, 3275 ParseFormalParameterList(allow_explicit_default_values,
3294 false, 3276 false,
3295 &discarded_params); 3277 &discarded_params);
3296 } 3278 }
3297 } else if (func.IsSyncGenClosure()) { 3279 } else if (func.IsSyncGenClosure()) {
3298 AddSyncGenClosureParameters(&params); 3280 AddSyncGenClosureParameters(&params);
3299 SetupDefaultsForOptionalParams(params, default_parameter_values); 3281 SetupDefaultsForOptionalParams(params);
3300 AddFormalParamsToScope(&params, current_block_->scope); 3282 AddFormalParamsToScope(&params, current_block_->scope);
3301 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); 3283 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved());
3302 if (!Function::Handle(func.parent_function()).IsGetterFunction()) { 3284 if (!Function::Handle(func.parent_function()).IsGetterFunction()) {
3303 // Parse and discard any formal parameters. They are accessed as 3285 // Parse and discard any formal parameters. They are accessed as
3304 // context variables. 3286 // context variables.
3305 ParamList discarded_params; 3287 ParamList discarded_params;
3306 ParseFormalParameterList(allow_explicit_default_values, 3288 ParseFormalParameterList(allow_explicit_default_values,
3307 false, 3289 false,
3308 &discarded_params); 3290 &discarded_params);
3309 } 3291 }
3310 } else if (func.IsAsyncGenClosure()) { 3292 } else if (func.IsAsyncGenClosure()) {
3311 AddAsyncGenClosureParameters(&params); 3293 AddAsyncGenClosureParameters(&params);
3312 SetupDefaultsForOptionalParams(params, default_parameter_values); 3294 SetupDefaultsForOptionalParams(params);
3313 AddFormalParamsToScope(&params, current_block_->scope); 3295 AddFormalParamsToScope(&params, current_block_->scope);
3314 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); 3296 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved());
3315 ASSERT(func.NumParameters() == params.parameters->length()); 3297 ASSERT(func.NumParameters() == params.parameters->length());
3316 if (!Function::Handle(func.parent_function()).IsGetterFunction()) { 3298 if (!Function::Handle(func.parent_function()).IsGetterFunction()) {
3317 // Parse and discard any formal parameters. They are accessed as 3299 // Parse and discard any formal parameters. They are accessed as
3318 // context variables. 3300 // context variables.
3319 ParamList discarded_params; 3301 ParamList discarded_params;
3320 ParseFormalParameterList(allow_explicit_default_values, 3302 ParseFormalParameterList(allow_explicit_default_values,
3321 false, 3303 false,
3322 &discarded_params); 3304 &discarded_params);
3323 } 3305 }
3324 } else { 3306 } else {
3325 ParseFormalParameterList(allow_explicit_default_values, false, &params); 3307 ParseFormalParameterList(allow_explicit_default_values, false, &params);
3326 3308
3327 // The number of parameters and their type are not yet set in local 3309 // The number of parameters and their type are not yet set in local
3328 // functions, since they are not 'top-level' parsed. 3310 // functions, since they are not 'top-level' parsed.
3329 if (func.IsLocalFunction()) { 3311 if (func.IsLocalFunction()) {
3330 AddFormalParamsToFunction(&params, func); 3312 AddFormalParamsToFunction(&params, func);
3331 } 3313 }
3332 SetupDefaultsForOptionalParams(params, default_parameter_values); 3314 SetupDefaultsForOptionalParams(params);
3333 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved()); 3315 ASSERT(AbstractType::Handle(Z, func.result_type()).IsResolved());
3334 ASSERT(func.NumParameters() == params.parameters->length()); 3316 ASSERT(func.NumParameters() == params.parameters->length());
3335 3317
3336 // Check whether the function has any field initializer formal parameters, 3318 // Check whether the function has any field initializer formal parameters,
3337 // which are not allowed in non-constructor functions. 3319 // which are not allowed in non-constructor functions.
3338 if (params.has_field_initializer) { 3320 if (params.has_field_initializer) {
3339 for (int i = 0; i < params.parameters->length(); i++) { 3321 for (int i = 0; i < params.parameters->length(); i++) {
3340 ParamDesc& param = (*params.parameters)[i]; 3322 ParamDesc& param = (*params.parameters)[i];
3341 if (param.is_field_initializer) { 3323 if (param.is_field_initializer) {
3342 ReportError(param.name_pos, 3324 ReportError(param.name_pos,
(...skipping 3881 matching lines...) Expand 10 before | Expand all | Expand 10 after
7224 ASSERT(new_body->scope() != NULL); 7206 ASSERT(new_body->scope() != NULL);
7225 new_body->scope()->LookupVariable(Symbols::AwaitJumpVar(), false); 7207 new_body->scope()->LookupVariable(Symbols::AwaitJumpVar(), false);
7226 new_body->scope()->LookupVariable(Symbols::AwaitContextVar(), false); 7208 new_body->scope()->LookupVariable(Symbols::AwaitContextVar(), false);
7227 new_body->scope()->LookupVariable(Symbols::AsyncCompleter(), false); 7209 new_body->scope()->LookupVariable(Symbols::AsyncCompleter(), false);
7228 new_body->scope()->RecursivelyCaptureAllVariables(); 7210 new_body->scope()->RecursivelyCaptureAllVariables();
7229 return new_body; 7211 return new_body;
7230 } 7212 }
7231 7213
7232 7214
7233 // Set up default values for all optional parameters to the function. 7215 // Set up default values for all optional parameters to the function.
7234 void Parser::SetupDefaultsForOptionalParams( 7216 void Parser::SetupDefaultsForOptionalParams(const ParamList& params) {
7235 const ParamList& params, 7217 if ((current_function().raw() == innermost_function().raw()) &&
7236 ZoneGrowableArray<const Instance*>* default_values) { 7218 (params.num_optional_parameters > 0)) {
7237 if (params.num_optional_parameters > 0) { 7219 ZoneGrowableArray<const Instance*>* default_values =
7220 new ZoneGrowableArray<const Instance*>(zone(),
7221 params.num_optional_parameters);
7238 // Build array of default parameter values. 7222 // Build array of default parameter values.
7239 const ZoneGrowableArray<ParamDesc>& parameters = *params.parameters; 7223 const ZoneGrowableArray<ParamDesc>& parameters = *params.parameters;
7240 const int first_opt_param_offset = params.num_fixed_parameters; 7224 const int first_opt_param_offset = params.num_fixed_parameters;
7241 for (int i = 0; i < params.num_optional_parameters; i++) { 7225 for (int i = 0; i < params.num_optional_parameters; i++) {
7242 const Instance* default_value = 7226 const Instance* default_value =
7243 parameters[i + first_opt_param_offset].default_value; 7227 parameters[i + first_opt_param_offset].default_value;
7244 default_values->Add(default_value); 7228 default_values->Add(default_value);
7245 } 7229 }
7230 parsed_function()->set_default_parameter_values(default_values);
7246 } 7231 }
7247 } 7232 }
7248 7233
7249 7234
7250 // Populate the parameter type array and parameter name array of the function 7235 // Populate the parameter type array and parameter name array of the function
7251 // with the formal parameter types and names. 7236 // with the formal parameter types and names.
7252 void Parser::AddFormalParamsToFunction(const ParamList* params, 7237 void Parser::AddFormalParamsToFunction(const ParamList* params,
7253 const Function& func) { 7238 const Function& func) {
7254 ASSERT((params != NULL) && (params->parameters != NULL)); 7239 ASSERT((params != NULL) && (params->parameters != NULL));
7255 ASSERT((params->num_optional_parameters > 0) == 7240 ASSERT((params->num_optional_parameters > 0) ==
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
7671 } else { 7656 } else {
7672 ReportError(function_pos, 7657 ReportError(function_pos,
7673 "'%s' from outer scope has already been used, " 7658 "'%s' from outer scope has already been used, "
7674 "cannot redefine", 7659 "cannot redefine",
7675 function_variable->name().ToCString()); 7660 function_variable->name().ToCString());
7676 } 7661 }
7677 } 7662 }
7678 } 7663 }
7679 7664
7680 // Parse the local function. 7665 // Parse the local function.
7681 ZoneGrowableArray<const Instance*>* default_parameter_values = 7666 SequenceNode* statements = Parser::ParseFunc(function);
7682 new ZoneGrowableArray<const Instance*>(Z, 2);
7683 SequenceNode* statements = Parser::ParseFunc(function,
7684 default_parameter_values);
7685 7667
7686 // Now that the local function has formal parameters, lookup the signature 7668 // Now that the local function has formal parameters, lookup the signature
7687 // class in the current library (but not in its imports) and only create a new 7669 // class in the current library (but not in its imports) and only create a new
7688 // canonical signature class if it does not exist yet. 7670 // canonical signature class if it does not exist yet.
7689 const String& signature = String::Handle(Z, function.Signature()); 7671 const String& signature = String::Handle(Z, function.Signature());
7690 Class& signature_class = Class::ZoneHandle(Z); 7672 Class& signature_class = Class::ZoneHandle(Z);
7691 if (!is_new_closure) { 7673 if (!is_new_closure) {
7692 signature_class = function.signature_class(); 7674 signature_class = function.signature_class();
7693 } 7675 }
7694 if (signature_class.IsNull()) { 7676 if (signature_class.IsNull()) {
(...skipping 6485 matching lines...) Expand 10 before | Expand all | Expand 10 after
14180 void Parser::SkipQualIdent() { 14162 void Parser::SkipQualIdent() {
14181 ASSERT(IsIdentifier()); 14163 ASSERT(IsIdentifier());
14182 ConsumeToken(); 14164 ConsumeToken();
14183 if (CurrentToken() == Token::kPERIOD) { 14165 if (CurrentToken() == Token::kPERIOD) {
14184 ConsumeToken(); // Consume the kPERIOD token. 14166 ConsumeToken(); // Consume the kPERIOD token.
14185 ExpectIdentifier("identifier expected after '.'"); 14167 ExpectIdentifier("identifier expected after '.'");
14186 } 14168 }
14187 } 14169 }
14188 14170
14189 } // namespace dart 14171 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/parser.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698