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

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

Issue 22685007: Implement updated method overriding rules in the vm. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 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 | Annotate | Revision Log
« no previous file with comments | « runtime/vm/parser.h ('k') | tests/co19/co19-runtime.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) 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 "vm/bigint_operations.h" 8 #include "vm/bigint_operations.h"
9 #include "vm/bootstrap.h" 9 #include "vm/bootstrap.h"
10 #include "vm/class_finalizer.h" 10 #include "vm/class_finalizer.h"
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
449 const AbstractType* type) { 449 const AbstractType* type) {
450 this->num_fixed_parameters++; 450 this->num_fixed_parameters++;
451 ParamDesc param; 451 ParamDesc param;
452 param.name_pos = name_pos; 452 param.name_pos = name_pos;
453 param.name = name; 453 param.name = name;
454 param.is_final = true; 454 param.is_final = true;
455 param.type = type; 455 param.type = type;
456 this->parameters->Add(param); 456 this->parameters->Add(param);
457 } 457 }
458 458
459 void AddReceiver(const Type* receiver_type, intptr_t token_pos) { 459 void AddReceiver(const AbstractType* receiver_type, intptr_t token_pos) {
460 ASSERT(this->parameters->is_empty()); 460 ASSERT(this->parameters->is_empty());
461 AddFinalParameter(token_pos, &Symbols::This(), receiver_type); 461 AddFinalParameter(token_pos, &Symbols::This(), receiver_type);
462 } 462 }
463 463
464 void SetImplicitlyFinal() { 464 void SetImplicitlyFinal() {
465 implicitly_final = true; 465 implicitly_final = true;
466 } 466 }
467 467
468 int num_fixed_parameters; 468 int num_fixed_parameters;
469 int num_optional_parameters; 469 int num_optional_parameters;
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after
1009 // Create AstNodes for an implicit instance getter method: 1009 // Create AstNodes for an implicit instance getter method:
1010 // LoadLocalNode 0 ('this'); 1010 // LoadLocalNode 0 ('this');
1011 // LoadInstanceFieldNode (field_name); 1011 // LoadInstanceFieldNode (field_name);
1012 // ReturnNode (field's value); 1012 // ReturnNode (field's value);
1013 SequenceNode* Parser::ParseInstanceGetter(const Function& func) { 1013 SequenceNode* Parser::ParseInstanceGetter(const Function& func) {
1014 TRACE_PARSER("ParseInstanceGetter"); 1014 TRACE_PARSER("ParseInstanceGetter");
1015 ParamList params; 1015 ParamList params;
1016 // func.token_pos() points to the name of the field. 1016 // func.token_pos() points to the name of the field.
1017 intptr_t ident_pos = func.token_pos(); 1017 intptr_t ident_pos = func.token_pos();
1018 ASSERT(current_class().raw() == func.Owner()); 1018 ASSERT(current_class().raw() == func.Owner());
1019 params.AddReceiver(ReceiverType(), ident_pos); 1019 params.AddReceiver(ReceiverType(current_class()), ident_pos);
1020 ASSERT(func.num_fixed_parameters() == 1); // receiver. 1020 ASSERT(func.num_fixed_parameters() == 1); // receiver.
1021 ASSERT(!func.HasOptionalParameters()); 1021 ASSERT(!func.HasOptionalParameters());
1022 ASSERT(AbstractType::Handle(func.result_type()).IsResolved()); 1022 ASSERT(AbstractType::Handle(func.result_type()).IsResolved());
1023 1023
1024 // Build local scope for function and populate with the formal parameters. 1024 // Build local scope for function and populate with the formal parameters.
1025 OpenFunctionBlock(func); 1025 OpenFunctionBlock(func);
1026 AddFormalParamsToScope(&params, current_block_->scope); 1026 AddFormalParamsToScope(&params, current_block_->scope);
1027 1027
1028 // Receiver is local 0. 1028 // Receiver is local 0.
1029 LocalVariable* receiver = current_block_->scope->VariableAt(0); 1029 LocalVariable* receiver = current_block_->scope->VariableAt(0);
(...skipping 23 matching lines...) Expand all
1053 // func.token_pos() points to the name of the field. 1053 // func.token_pos() points to the name of the field.
1054 intptr_t ident_pos = func.token_pos(); 1054 intptr_t ident_pos = func.token_pos();
1055 const String& field_name = *CurrentLiteral(); 1055 const String& field_name = *CurrentLiteral();
1056 const Class& field_class = Class::ZoneHandle(func.Owner()); 1056 const Class& field_class = Class::ZoneHandle(func.Owner());
1057 const Field& field = 1057 const Field& field =
1058 Field::ZoneHandle(field_class.LookupInstanceField(field_name)); 1058 Field::ZoneHandle(field_class.LookupInstanceField(field_name));
1059 const AbstractType& field_type = AbstractType::ZoneHandle(field.type()); 1059 const AbstractType& field_type = AbstractType::ZoneHandle(field.type());
1060 1060
1061 ParamList params; 1061 ParamList params;
1062 ASSERT(current_class().raw() == func.Owner()); 1062 ASSERT(current_class().raw() == func.Owner());
1063 params.AddReceiver(ReceiverType(), ident_pos); 1063 params.AddReceiver(ReceiverType(current_class()), ident_pos);
1064 params.AddFinalParameter(ident_pos, 1064 params.AddFinalParameter(ident_pos,
1065 &Symbols::Value(), 1065 &Symbols::Value(),
1066 &field_type); 1066 &field_type);
1067 ASSERT(func.num_fixed_parameters() == 2); // receiver, value. 1067 ASSERT(func.num_fixed_parameters() == 2); // receiver, value.
1068 ASSERT(!func.HasOptionalParameters()); 1068 ASSERT(!func.HasOptionalParameters());
1069 ASSERT(AbstractType::Handle(func.result_type()).IsVoidType()); 1069 ASSERT(AbstractType::Handle(func.result_type()).IsVoidType());
1070 1070
1071 // Build local scope for function and populate with the formal parameters. 1071 // Build local scope for function and populate with the formal parameters.
1072 OpenFunctionBlock(func); 1072 OpenFunctionBlock(func);
1073 AddFormalParamsToScope(&params, current_block_->scope); 1073 AddFormalParamsToScope(&params, current_block_->scope);
(...skipping 12 matching lines...) Expand all
1086 } 1086 }
1087 1087
1088 1088
1089 SequenceNode* Parser::ParseMethodExtractor(const Function& func) { 1089 SequenceNode* Parser::ParseMethodExtractor(const Function& func) {
1090 TRACE_PARSER("ParseMethodExtractor"); 1090 TRACE_PARSER("ParseMethodExtractor");
1091 ParamList params; 1091 ParamList params;
1092 1092
1093 const intptr_t ident_pos = func.token_pos(); 1093 const intptr_t ident_pos = func.token_pos();
1094 ASSERT(func.token_pos() == 0); 1094 ASSERT(func.token_pos() == 0);
1095 ASSERT(current_class().raw() == func.Owner()); 1095 ASSERT(current_class().raw() == func.Owner());
1096 params.AddReceiver(ReceiverType(), ident_pos); 1096 params.AddReceiver(ReceiverType(current_class()), ident_pos);
1097 ASSERT(func.num_fixed_parameters() == 1); // Receiver. 1097 ASSERT(func.num_fixed_parameters() == 1); // Receiver.
1098 ASSERT(!func.HasOptionalParameters()); 1098 ASSERT(!func.HasOptionalParameters());
1099 1099
1100 // Build local scope for function and populate with the formal parameters. 1100 // Build local scope for function and populate with the formal parameters.
1101 OpenFunctionBlock(func); 1101 OpenFunctionBlock(func);
1102 AddFormalParamsToScope(&params, current_block_->scope); 1102 AddFormalParamsToScope(&params, current_block_->scope);
1103 1103
1104 // Receiver is local 0. 1104 // Receiver is local 0.
1105 LocalVariable* receiver = current_block_->scope->VariableAt(0); 1105 LocalVariable* receiver = current_block_->scope->VariableAt(0);
1106 LoadLocalNode* load_receiver = new LoadLocalNode(ident_pos, receiver); 1106 LoadLocalNode* load_receiver = new LoadLocalNode(ident_pos, receiver);
1107 1107
1108 ClosureNode* closure = new ClosureNode( 1108 ClosureNode* closure = new ClosureNode(
1109 ident_pos, 1109 ident_pos,
1110 Function::ZoneHandle(func.extracted_method_closure()), 1110 Function::ZoneHandle(func.extracted_method_closure()),
1111 load_receiver, 1111 load_receiver,
1112 NULL); 1112 NULL);
1113 1113
1114 ReturnNode* return_node = new ReturnNode(ident_pos, closure); 1114 ReturnNode* return_node = new ReturnNode(ident_pos, closure);
1115 current_block_->statements->Add(return_node); 1115 current_block_->statements->Add(return_node);
1116 return CloseBlock(); 1116 return CloseBlock();
1117 } 1117 }
1118 1118
1119 1119
1120 void Parser::BuildDispatcherScope(const Function& func, 1120 void Parser::BuildDispatcherScope(const Function& func,
1121 const ArgumentsDescriptor& desc, 1121 const ArgumentsDescriptor& desc,
1122 Array& default_values) { 1122 Array& default_values) {
1123 ParamList params; 1123 ParamList params;
1124 // Receiver first. 1124 // Receiver first.
1125 intptr_t token_pos = func.token_pos(); 1125 intptr_t token_pos = func.token_pos();
1126 params.AddReceiver(ReceiverType(), token_pos); 1126 params.AddReceiver(ReceiverType(current_class()), token_pos);
1127 // Remaining positional parameters. 1127 // Remaining positional parameters.
1128 intptr_t i = 1; 1128 intptr_t i = 1;
1129 for (; i < desc.PositionalCount(); ++i) { 1129 for (; i < desc.PositionalCount(); ++i) {
1130 ParamDesc p; 1130 ParamDesc p;
1131 char name[64]; 1131 char name[64];
1132 OS::SNPrint(name, 64, ":p%"Pd, i); 1132 OS::SNPrint(name, 64, ":p%"Pd, i);
1133 p.name = &String::ZoneHandle(Symbols::New(name)); 1133 p.name = &String::ZoneHandle(Symbols::New(name));
1134 p.type = &Type::ZoneHandle(Type::DynamicType()); 1134 p.type = &Type::ZoneHandle(Type::DynamicType());
1135 params.parameters->Add(p); 1135 params.parameters->Add(p);
1136 params.num_fixed_parameters++; 1136 params.num_fixed_parameters++;
(...skipping 1139 matching lines...) Expand 10 before | Expand all | Expand 10 after
2276 ctor_name.ToCString(), 2276 ctor_name.ToCString(),
2277 error_message.ToCString()); 2277 error_message.ToCString());
2278 } 2278 }
2279 current_block_->statements->Add( 2279 current_block_->statements->Add(
2280 new StaticCallNode(call_pos, redirect_ctor, arguments)); 2280 new StaticCallNode(call_pos, redirect_ctor, arguments));
2281 } 2281 }
2282 2282
2283 2283
2284 SequenceNode* Parser::MakeImplicitConstructor(const Function& func) { 2284 SequenceNode* Parser::MakeImplicitConstructor(const Function& func) {
2285 ASSERT(func.IsConstructor()); 2285 ASSERT(func.IsConstructor());
2286 ASSERT(func.Owner() == current_class().raw());
2286 const intptr_t ctor_pos = TokenPos(); 2287 const intptr_t ctor_pos = TokenPos();
2287 OpenFunctionBlock(func); 2288 OpenFunctionBlock(func);
2288 const Class& cls = Class::Handle(func.Owner());
2289 2289
2290 LocalVariable* receiver = new LocalVariable( 2290 LocalVariable* receiver = new LocalVariable(
2291 ctor_pos, 2291 ctor_pos, Symbols::This(), *ReceiverType(current_class()));
2292 Symbols::This(),
2293 Type::ZoneHandle(Type::DynamicType()));
2294 current_block_->scope->AddVariable(receiver); 2292 current_block_->scope->AddVariable(receiver);
2295 2293
2296 LocalVariable* phase_parameter = new LocalVariable( 2294 LocalVariable* phase_parameter = new LocalVariable(
2297 ctor_pos, 2295 ctor_pos, Symbols::PhaseParameter(), Type::ZoneHandle(Type::SmiType()));
2298 Symbols::PhaseParameter(),
2299 Type::ZoneHandle(Type::SmiType()));
2300 current_block_->scope->AddVariable(phase_parameter); 2296 current_block_->scope->AddVariable(phase_parameter);
2301 2297
2302 // Parse expressions of instance fields that have an explicit 2298 // Parse expressions of instance fields that have an explicit
2303 // initializer expression. 2299 // initializer expression.
2304 // The receiver must not be visible to field initializer expressions. 2300 // The receiver must not be visible to field initializer expressions.
2305 receiver->set_invisible(true); 2301 receiver->set_invisible(true);
2306 GrowableArray<Field*> initialized_fields; 2302 GrowableArray<Field*> initialized_fields;
2307 ParseInitializedInstanceFields(cls, receiver, &initialized_fields); 2303 ParseInitializedInstanceFields(
2304 current_class(), receiver, &initialized_fields);
2308 receiver->set_invisible(false); 2305 receiver->set_invisible(false);
2309 2306
2310 // If the class of this implicit constructor is a mixin application class, 2307 // If the class of this implicit constructor is a mixin application class,
2311 // it is a forwarding constructor of the mixin. The forwarding 2308 // it is a forwarding constructor of the mixin. The forwarding
2312 // constructor initializes the instance fields that have initializer 2309 // constructor initializes the instance fields that have initializer
2313 // expressions and then calls the respective super constructor with 2310 // expressions and then calls the respective super constructor with
2314 // the same name and number of parameters. 2311 // the same name and number of parameters.
2315 ArgumentListNode* forwarding_args = NULL; 2312 ArgumentListNode* forwarding_args = NULL;
2316 if (cls.mixin() != Type::null()) { 2313 if (current_class().mixin() != Type::null()) {
2317 // At this point we don't support forwarding constructors 2314 // At this point we don't support forwarding constructors
2318 // that have optional parameters because we don't know the default 2315 // that have optional parameters because we don't know the default
2319 // values of the optional parameters. We would have to compile the super 2316 // values of the optional parameters. We would have to compile the super
2320 // constructor to get the default values. Also, the spec is not clear 2317 // constructor to get the default values. Also, the spec is not clear
2321 // whether optional parameters are even allowed in this situation. 2318 // whether optional parameters are even allowed in this situation.
2322 // TODO(hausner): Remove this limitation if the language spec indeed 2319 // TODO(hausner): Remove this limitation if the language spec indeed
2323 // allows optional parameters. 2320 // allows optional parameters.
2324 if (func.HasOptionalParameters()) { 2321 if (func.HasOptionalParameters()) {
2325 ErrorMsg(ctor_pos, 2322 ErrorMsg(ctor_pos,
2326 "forwarding constructors must not have optional parameters"); 2323 "forwarding constructors must not have optional parameters");
2327 } 2324 }
2328 2325
2329 // Prepare user-defined arguments to be forwarded to super call. 2326 // Prepare user-defined arguments to be forwarded to super call.
2330 // The first user-defined argument is at position 2. 2327 // The first user-defined argument is at position 2.
2331 forwarding_args = new ArgumentListNode(ctor_pos); 2328 forwarding_args = new ArgumentListNode(ctor_pos);
2332 for (int i = 2; i < func.NumParameters(); i++) { 2329 for (int i = 2; i < func.NumParameters(); i++) {
2333 LocalVariable* param = new LocalVariable( 2330 LocalVariable* param = new LocalVariable(
2334 ctor_pos, 2331 ctor_pos,
2335 String::ZoneHandle(func.ParameterNameAt(i)), 2332 String::ZoneHandle(func.ParameterNameAt(i)),
2336 Type::ZoneHandle(Type::DynamicType())); 2333 Type::ZoneHandle(Type::DynamicType()));
2337 current_block_->scope->AddVariable(param); 2334 current_block_->scope->AddVariable(param);
2338 forwarding_args->Add(new LoadLocalNode(ctor_pos, param)); 2335 forwarding_args->Add(new LoadLocalNode(ctor_pos, param));
2339 } 2336 }
2340 } 2337 }
2341 2338
2342 GenerateSuperConstructorCall(cls, receiver, forwarding_args); 2339 GenerateSuperConstructorCall(current_class(), receiver, forwarding_args);
2343 CheckConstFieldsInitialized(cls); 2340 CheckConstFieldsInitialized(current_class());
2344 2341
2345 // Empty constructor body. 2342 // Empty constructor body.
2346 SequenceNode* statements = CloseBlock(); 2343 SequenceNode* statements = CloseBlock();
2347 return statements; 2344 return statements;
2348 } 2345 }
2349 2346
2350 2347
2351 // Helper function to make the first num_variables variables in the 2348 // Helper function to make the first num_variables variables in the
2352 // given scope visible/invisible. 2349 // given scope visible/invisible.
2353 static void SetInvisible(LocalScope* scope, int num_variables, bool invisible) { 2350 static void SetInvisible(LocalScope* scope, int num_variables, bool invisible) {
(...skipping 27 matching lines...) Expand all
2381 } 2378 }
2382 2379
2383 OpenFunctionBlock(func); 2380 OpenFunctionBlock(func);
2384 ParamList params; 2381 ParamList params;
2385 const bool allow_explicit_default_values = true; 2382 const bool allow_explicit_default_values = true;
2386 ASSERT(CurrentToken() == Token::kLPAREN); 2383 ASSERT(CurrentToken() == Token::kLPAREN);
2387 2384
2388 // Add implicit receiver parameter which is passed the allocated 2385 // Add implicit receiver parameter which is passed the allocated
2389 // but uninitialized instance to construct. 2386 // but uninitialized instance to construct.
2390 ASSERT(current_class().raw() == func.Owner()); 2387 ASSERT(current_class().raw() == func.Owner());
2391 params.AddReceiver(ReceiverType(), func.token_pos()); 2388 params.AddReceiver(ReceiverType(current_class()), func.token_pos());
2392 2389
2393 // Add implicit parameter for construction phase. 2390 // Add implicit parameter for construction phase.
2394 params.AddFinalParameter( 2391 params.AddFinalParameter(
2395 TokenPos(), 2392 TokenPos(),
2396 &Symbols::PhaseParameter(), 2393 &Symbols::PhaseParameter(),
2397 &Type::ZoneHandle(Type::SmiType())); 2394 &Type::ZoneHandle(Type::SmiType()));
2398 2395
2399 if (func.is_const()) { 2396 if (func.is_const()) {
2400 params.SetImplicitlyFinal(); 2397 params.SetImplicitlyFinal();
2401 } 2398 }
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
2641 if (func.IsClosureFunction()) { 2638 if (func.IsClosureFunction()) {
2642 // The first parameter of a closure function is the closure object. 2639 // The first parameter of a closure function is the closure object.
2643 ASSERT(!func.is_const()); // Closure functions cannot be const. 2640 ASSERT(!func.is_const()); // Closure functions cannot be const.
2644 params.AddFinalParameter( 2641 params.AddFinalParameter(
2645 TokenPos(), 2642 TokenPos(),
2646 &Symbols::ClosureParameter(), 2643 &Symbols::ClosureParameter(),
2647 &Type::ZoneHandle(Type::DynamicType())); 2644 &Type::ZoneHandle(Type::DynamicType()));
2648 } else if (!func.is_static()) { 2645 } else if (!func.is_static()) {
2649 // Static functions do not have a receiver. 2646 // Static functions do not have a receiver.
2650 ASSERT(current_class().raw() == func.Owner()); 2647 ASSERT(current_class().raw() == func.Owner());
2651 params.AddReceiver(ReceiverType(), func.token_pos()); 2648 params.AddReceiver(ReceiverType(current_class()), func.token_pos());
2652 } else if (func.IsFactory()) { 2649 } else if (func.IsFactory()) {
2653 // The first parameter of a factory is the AbstractTypeArguments vector of 2650 // The first parameter of a factory is the AbstractTypeArguments vector of
2654 // the type of the instance to be allocated. 2651 // the type of the instance to be allocated.
2655 params.AddFinalParameter( 2652 params.AddFinalParameter(
2656 TokenPos(), 2653 TokenPos(),
2657 &Symbols::TypeArgumentsParameter(), 2654 &Symbols::TypeArgumentsParameter(),
2658 &Type::ZoneHandle(Type::DynamicType())); 2655 &Type::ZoneHandle(Type::DynamicType()));
2659 } 2656 }
2660 ASSERT((CurrentToken() == Token::kLPAREN) || func.IsGetterFunction()); 2657 ASSERT((CurrentToken() == Token::kLPAREN) || func.IsGetterFunction());
2661 const bool allow_explicit_default_values = true; 2658 const bool allow_explicit_default_values = true;
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after
2863 2860
2864 // Parse the formal parameters. 2861 // Parse the formal parameters.
2865 const bool are_implicitly_final = method->has_const; 2862 const bool are_implicitly_final = method->has_const;
2866 const bool allow_explicit_default_values = true; 2863 const bool allow_explicit_default_values = true;
2867 const intptr_t formal_param_pos = TokenPos(); 2864 const intptr_t formal_param_pos = TokenPos();
2868 method->params.Clear(); 2865 method->params.Clear();
2869 // Static functions do not have a receiver. 2866 // Static functions do not have a receiver.
2870 // The first parameter of a factory is the AbstractTypeArguments vector of 2867 // The first parameter of a factory is the AbstractTypeArguments vector of
2871 // the type of the instance to be allocated. 2868 // the type of the instance to be allocated.
2872 if (!method->has_static || method->IsConstructor()) { 2869 if (!method->has_static || method->IsConstructor()) {
2873 method->params.AddReceiver(ReceiverType(), formal_param_pos); 2870 method->params.AddReceiver(ReceiverType(current_class()), formal_param_pos);
2874 } else if (method->IsFactory()) { 2871 } else if (method->IsFactory()) {
2875 method->params.AddFinalParameter( 2872 method->params.AddFinalParameter(
2876 formal_param_pos, 2873 formal_param_pos,
2877 &Symbols::TypeArgumentsParameter(), 2874 &Symbols::TypeArgumentsParameter(),
2878 &Type::ZoneHandle(Type::DynamicType())); 2875 &Type::ZoneHandle(Type::DynamicType()));
2879 } 2876 }
2880 // Constructors have an implicit parameter for the construction phase. 2877 // Constructors have an implicit parameter for the construction phase.
2881 if (method->IsConstructor()) { 2878 if (method->IsConstructor()) {
2882 method->params.AddFinalParameter( 2879 method->params.AddFinalParameter(
2883 TokenPos(), 2880 TokenPos(),
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after
3214 String& getter_name = String::Handle(Field::GetterSymbol(*field->name)); 3211 String& getter_name = String::Handle(Field::GetterSymbol(*field->name));
3215 getter = Function::New(getter_name, RawFunction::kImplicitGetter, 3212 getter = Function::New(getter_name, RawFunction::kImplicitGetter,
3216 field->has_static, 3213 field->has_static,
3217 field->has_final, 3214 field->has_final,
3218 /* is_abstract = */ false, 3215 /* is_abstract = */ false,
3219 /* is_external = */ false, 3216 /* is_external = */ false,
3220 current_class(), 3217 current_class(),
3221 field->name_pos); 3218 field->name_pos);
3222 ParamList params; 3219 ParamList params;
3223 ASSERT(current_class().raw() == getter.Owner()); 3220 ASSERT(current_class().raw() == getter.Owner());
3224 params.AddReceiver(ReceiverType(), field->name_pos); 3221 params.AddReceiver(ReceiverType(current_class()), field->name_pos);
3225 getter.set_result_type(*field->type); 3222 getter.set_result_type(*field->type);
3226 AddFormalParamsToFunction(&params, getter); 3223 AddFormalParamsToFunction(&params, getter);
3227 members->AddFunction(getter); 3224 members->AddFunction(getter);
3228 if (!field->has_final) { 3225 if (!field->has_final) {
3229 // Build a setter accessor for non-const fields. 3226 // Build a setter accessor for non-const fields.
3230 String& setter_name = String::Handle(Field::SetterSymbol(*field->name)); 3227 String& setter_name = String::Handle(Field::SetterSymbol(*field->name));
3231 setter = Function::New(setter_name, RawFunction::kImplicitSetter, 3228 setter = Function::New(setter_name, RawFunction::kImplicitSetter,
3232 field->has_static, 3229 field->has_static,
3233 field->has_final, 3230 field->has_final,
3234 /* is_abstract = */ false, 3231 /* is_abstract = */ false,
3235 /* is_external = */ false, 3232 /* is_external = */ false,
3236 current_class(), 3233 current_class(),
3237 field->name_pos); 3234 field->name_pos);
3238 ParamList params; 3235 ParamList params;
3239 ASSERT(current_class().raw() == setter.Owner()); 3236 ASSERT(current_class().raw() == setter.Owner());
3240 params.AddReceiver(ReceiverType(), field->name_pos); 3237 params.AddReceiver(ReceiverType(current_class()), field->name_pos);
3241 params.AddFinalParameter(TokenPos(), 3238 params.AddFinalParameter(TokenPos(),
3242 &Symbols::Value(), 3239 &Symbols::Value(),
3243 field->type); 3240 field->type);
3244 setter.set_result_type(Type::Handle(Type::VoidType())); 3241 setter.set_result_type(Type::Handle(Type::VoidType()));
3245 AddFormalParamsToFunction(&params, setter); 3242 AddFormalParamsToFunction(&params, setter);
3246 members->AddFunction(setter); 3243 members->AddFunction(setter);
3247 } 3244 }
3248 } 3245 }
3249 3246
3250 if (CurrentToken() != Token::kCOMMA) { 3247 if (CurrentToken() != Token::kCOMMA) {
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
3361 member.name_pos = TokenPos(); 3358 member.name_pos = TokenPos();
3362 member.name = CurrentLiteral(); // Unqualified identifier. 3359 member.name = CurrentLiteral(); // Unqualified identifier.
3363 ConsumeToken(); 3360 ConsumeToken();
3364 if (member.has_factory) { 3361 if (member.has_factory) {
3365 // The factory name may be qualified, but the first identifier must match 3362 // The factory name may be qualified, but the first identifier must match
3366 // the name of the immediately enclosing class. 3363 // the name of the immediately enclosing class.
3367 if (!member.name->Equals(members->class_name())) { 3364 if (!member.name->Equals(members->class_name())) {
3368 ErrorMsg(member.name_pos, "factory name must be '%s'", 3365 ErrorMsg(member.name_pos, "factory name must be '%s'",
3369 members->class_name().ToCString()); 3366 members->class_name().ToCString());
3370 } 3367 }
3371 // Do not bypass class resolution by using current_class() directly, since
3372 // it may be a patch class.
3373 const Object& result_type_class = Object::Handle(
3374 UnresolvedClass::New(LibraryPrefix::Handle(),
3375 *member.name,
3376 member.name_pos));
3377 // The type arguments of the result type are the type parameters of the
3378 // current class. Note that in the case of a patch class, they are copied
3379 // from the class being patched.
3380 member.type = &Type::ZoneHandle(Type::New(
3381 result_type_class,
3382 TypeArguments::Handle(current_class().type_parameters()),
3383 member.name_pos));
3384 } else if (member.has_static) { 3368 } else if (member.has_static) {
3385 ErrorMsg(member.name_pos, "constructor cannot be static"); 3369 ErrorMsg(member.name_pos, "constructor cannot be static");
3386 } 3370 }
3371 if (member.type != NULL) {
3372 ErrorMsg(member.name_pos, "constructor must not specify return type");
3373 }
3374 // Do not bypass class resolution by using current_class() directly, since
3375 // it may be a patch class.
3376 const Object& result_type_class = Object::Handle(
3377 UnresolvedClass::New(LibraryPrefix::Handle(),
3378 *member.name,
3379 member.name_pos));
3380 // The type arguments of the result type are the type parameters of the
3381 // current class. Note that in the case of a patch class, they are copied
3382 // from the class being patched.
3383 member.type = &Type::ZoneHandle(Type::New(
3384 result_type_class,
3385 TypeArguments::Handle(current_class().type_parameters()),
3386 member.name_pos));
3387
3387 // We must be dealing with a constructor or named constructor. 3388 // We must be dealing with a constructor or named constructor.
3388 member.kind = RawFunction::kConstructor; 3389 member.kind = RawFunction::kConstructor;
3389 *member.name = String::Concat(*member.name, Symbols::Dot()); 3390 *member.name = String::Concat(*member.name, Symbols::Dot());
3390 if (CurrentToken() == Token::kPERIOD) { 3391 if (CurrentToken() == Token::kPERIOD) {
3391 // Named constructor. 3392 // Named constructor.
3392 ConsumeToken(); 3393 ConsumeToken();
3393 member.constructor_name = ExpectIdentifier("identifier expected"); 3394 member.constructor_name = ExpectIdentifier("identifier expected");
3394 *member.name = String::Concat(*member.name, *member.constructor_name); 3395 *member.name = String::Concat(*member.name, *member.constructor_name);
3395 } 3396 }
3396 // Ensure that names are symbols. 3397 // Ensure that names are symbols.
3397 *member.name = Symbols::New(*member.name); 3398 *member.name = Symbols::New(*member.name);
3398 if (member.type == NULL) { 3399
3399 ASSERT(!member.has_factory);
3400 // The body of the constructor cannot modify the type arguments of the
3401 // constructed instance, which is passed in as a hidden parameter.
3402 // Therefore, there is no need to set the result type to be checked.
3403 member.type = &Type::ZoneHandle(Type::DynamicType());
3404 } else {
3405 // The type can only be already set in the factory case.
3406 if (!member.has_factory) {
3407 ErrorMsg(member.name_pos, "constructor must not specify return type");
3408 }
3409 }
3410 if (CurrentToken() != Token::kLPAREN) { 3400 if (CurrentToken() != Token::kLPAREN) {
3411 ErrorMsg("left parenthesis expected"); 3401 ErrorMsg("left parenthesis expected");
3412 } 3402 }
3413 } else if ((CurrentToken() == Token::kGET) && !member.has_var && 3403 } else if ((CurrentToken() == Token::kGET) && !member.has_var &&
3414 (LookaheadToken(1) != Token::kLPAREN) && 3404 (LookaheadToken(1) != Token::kLPAREN) &&
3415 (LookaheadToken(1) != Token::kASSIGN) && 3405 (LookaheadToken(1) != Token::kASSIGN) &&
3416 (LookaheadToken(1) != Token::kCOMMA) && 3406 (LookaheadToken(1) != Token::kCOMMA) &&
3417 (LookaheadToken(1) != Token::kSEMICOLON)) { 3407 (LookaheadToken(1) != Token::kSEMICOLON)) {
3418 ConsumeToken(); 3408 ConsumeToken();
3419 member.kind = RawFunction::kGetterFunction; 3409 member.kind = RawFunction::kGetterFunction;
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
3683 if (need_implicit_constructor) { 3673 if (need_implicit_constructor) {
3684 AddImplicitConstructor(cls); 3674 AddImplicitConstructor(cls);
3685 } 3675 }
3686 3676
3687 if (cls.is_patch()) { 3677 if (cls.is_patch()) {
3688 // Apply the changes to the patched class looked up above. 3678 // Apply the changes to the patched class looked up above.
3689 Object& obj = Object::Handle(library_.LookupLocalObject(class_name)); 3679 Object& obj = Object::Handle(library_.LookupLocalObject(class_name));
3690 // The patched class must not be finalized yet. 3680 // The patched class must not be finalized yet.
3691 const Class& orig_class = Class::Cast(obj); 3681 const Class& orig_class = Class::Cast(obj);
3692 ASSERT(!orig_class.is_finalized()); 3682 ASSERT(!orig_class.is_finalized());
3693 const char* err_msg = orig_class.ApplyPatch(cls); 3683 Error& error = Error::Handle();
3694 if (err_msg != NULL) { 3684 if (!orig_class.ApplyPatch(cls, &error)) {
3695 ErrorMsg(class_pos, "applying patch failed with '%s'", err_msg); 3685 AppendErrorMsg(error, class_pos, "applying patch failed");
3696 } 3686 }
3697 } 3687 }
3698 } 3688 }
3699 3689
3700 3690
3701 // Add an implicit constructor to the given class. 3691 // Add an implicit constructor to the given class.
3702 void Parser::AddImplicitConstructor(const Class& cls) { 3692 void Parser::AddImplicitConstructor(const Class& cls) {
3703 // The implicit constructor is unnamed, has no explicit parameter. 3693 // The implicit constructor is unnamed, has no explicit parameter.
3704 String& ctor_name = String::ZoneHandle(cls.Name()); 3694 String& ctor_name = String::ZoneHandle(cls.Name());
3705 ctor_name = String::Concat(ctor_name, Symbols::Dot()); 3695 ctor_name = String::Concat(ctor_name, Symbols::Dot());
3706 ctor_name = Symbols::New(ctor_name); 3696 ctor_name = Symbols::New(ctor_name);
3707 // To indicate that this is an implicit constructor, we set the 3697 // To indicate that this is an implicit constructor, we set the
3708 // token position and end token position of the function 3698 // token position and end token position of the function
3709 // to the token position of the class. 3699 // to the token position of the class.
3710 Function& ctor = Function::Handle( 3700 Function& ctor = Function::Handle(
3711 Function::New(ctor_name, 3701 Function::New(ctor_name,
3712 RawFunction::kConstructor, 3702 RawFunction::kConstructor,
3713 /* is_static = */ false, 3703 /* is_static = */ false,
3714 /* is_const = */ false, 3704 /* is_const = */ false,
3715 /* is_abstract = */ false, 3705 /* is_abstract = */ false,
3716 /* is_external = */ false, 3706 /* is_external = */ false,
3717 cls, 3707 cls,
3718 cls.token_pos())); 3708 cls.token_pos()));
3719 ctor.set_end_token_pos(ctor.token_pos()); 3709 ctor.set_end_token_pos(ctor.token_pos());
3720 3710
3721 ParamList params; 3711 ParamList params;
3722 // Add implicit 'this' parameter. We don't care about the specific type 3712 // Add implicit 'this' parameter.
3723 // and just specify dynamic. 3713 const AbstractType* receiver_type = ReceiverType(cls);
3724 const Type& receiver_type = Type::Handle(Type::DynamicType()); 3714 params.AddReceiver(receiver_type, cls.token_pos());
3725 params.AddReceiver(&receiver_type, cls.token_pos());
3726 // Add implicit parameter for construction phase. 3715 // Add implicit parameter for construction phase.
3727 params.AddFinalParameter(cls.token_pos(), 3716 params.AddFinalParameter(cls.token_pos(),
3728 &Symbols::PhaseParameter(), 3717 &Symbols::PhaseParameter(),
3729 &Type::ZoneHandle(Type::SmiType())); 3718 &Type::ZoneHandle(Type::SmiType()));
3730 3719
3731 AddFormalParamsToFunction(&params, ctor); 3720 AddFormalParamsToFunction(&params, ctor);
3732 // The body of the constructor cannot modify the type of the constructed 3721 // The body of the constructor cannot modify the type of the constructed
3733 // instance, which is passed in as the receiver. 3722 // instance, which is passed in as the receiver.
3734 ctor.set_result_type(receiver_type); 3723 ctor.set_result_type(*receiver_type);
3735 cls.AddFunction(ctor); 3724 cls.AddFunction(ctor);
3736 } 3725 }
3737 3726
3738 3727
3739 // Check for cycles in constructor redirection. Also check whether a 3728 // Check for cycles in constructor redirection. Also check whether a
3740 // named constructor collides with the name of another class member. 3729 // named constructor collides with the name of another class member.
3741 void Parser::CheckConstructors(ClassDesc* class_desc) { 3730 void Parser::CheckConstructors(ClassDesc* class_desc) {
3742 // Check for cycles in constructor redirection. 3731 // Check for cycles in constructor redirection.
3743 const GrowableArray<MemberDesc>& members = class_desc->members(); 3732 const GrowableArray<MemberDesc>& members = class_desc->members();
3744 for (int i = 0; i < members.length(); i++) { 3733 for (int i = 0; i < members.length(); i++) {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
3815 if (CurrentToken() != Token::kWITH) { 3804 if (CurrentToken() != Token::kWITH) {
3816 ErrorMsg("mixin application 'with Type' expected"); 3805 ErrorMsg("mixin application 'with Type' expected");
3817 } 3806 }
3818 type = ParseMixins(type); 3807 type = ParseMixins(type);
3819 3808
3820 // TODO(hausner): treat the mixin application as an alias, not as a base 3809 // TODO(hausner): treat the mixin application as an alias, not as a base
3821 // class whose super class is the mixin application! 3810 // class whose super class is the mixin application!
3822 mixin_application.set_super_type(type); 3811 mixin_application.set_super_type(type);
3823 mixin_application.set_is_synthesized_class(); 3812 mixin_application.set_is_synthesized_class();
3824 3813
3825 AddImplicitConstructor(mixin_application); 3814 // This mixin application typedef needs an implicit constructor, but it is
3815 // too early to call 'AddImplicitConstructor(mixin_application)' here,
3816 // because this class should be lazily compiled.
3826 if (CurrentToken() == Token::kIMPLEMENTS) { 3817 if (CurrentToken() == Token::kIMPLEMENTS) {
3827 ParseInterfaceList(mixin_application); 3818 ParseInterfaceList(mixin_application);
3828 } 3819 }
3829 ExpectSemicolon(); 3820 ExpectSemicolon();
3830 pending_classes.Add(mixin_application, Heap::kOld); 3821 pending_classes.Add(mixin_application, Heap::kOld);
3831 } 3822 }
3832 3823
3833 3824
3834 // Look ahead to detect if we are seeing ident [ TypeParameters ] "(". 3825 // Look ahead to detect if we are seeing ident [ TypeParameters ] "(".
3835 // We need this lookahead to distinguish between the optional return type 3826 // We need this lookahead to distinguish between the optional return type
(...skipping 1117 matching lines...) Expand 10 before | Expand all | Expand 10 after
4953 // Populate the parameter type array and parameter name array of the function 4944 // Populate the parameter type array and parameter name array of the function
4954 // with the formal parameter types and names. 4945 // with the formal parameter types and names.
4955 void Parser::AddFormalParamsToFunction(const ParamList* params, 4946 void Parser::AddFormalParamsToFunction(const ParamList* params,
4956 const Function& func) { 4947 const Function& func) {
4957 ASSERT((params != NULL) && (params->parameters != NULL)); 4948 ASSERT((params != NULL) && (params->parameters != NULL));
4958 ASSERT((params->num_optional_parameters > 0) == 4949 ASSERT((params->num_optional_parameters > 0) ==
4959 (params->has_optional_positional_parameters || 4950 (params->has_optional_positional_parameters ||
4960 params->has_optional_named_parameters)); 4951 params->has_optional_named_parameters));
4961 if (!Utils::IsInt(16, params->num_fixed_parameters) || 4952 if (!Utils::IsInt(16, params->num_fixed_parameters) ||
4962 !Utils::IsInt(16, params->num_optional_parameters)) { 4953 !Utils::IsInt(16, params->num_optional_parameters)) {
4963 ErrorMsg(func.token_pos(), "too many formal parameters"); 4954 const Script& script = Script::Handle(Class::Handle(func.Owner()).script());
4955 const Error& error = Error::Handle(FormatErrorMsg(
4956 script, func.token_pos(), "Error", "too many formal parameters"));
4957 ErrorMsg(error);
4964 } 4958 }
4965 func.set_num_fixed_parameters(params->num_fixed_parameters); 4959 func.set_num_fixed_parameters(params->num_fixed_parameters);
4966 func.SetNumOptionalParameters(params->num_optional_parameters, 4960 func.SetNumOptionalParameters(params->num_optional_parameters,
4967 params->has_optional_positional_parameters); 4961 params->has_optional_positional_parameters);
4968 const int num_parameters = params->parameters->length(); 4962 const int num_parameters = params->parameters->length();
4969 ASSERT(num_parameters == func.NumParameters()); 4963 ASSERT(num_parameters == func.NumParameters());
4970 func.set_parameter_types(Array::Handle(Array::New(num_parameters, 4964 func.set_parameter_types(Array::Handle(Array::New(num_parameters,
4971 Heap::kOld))); 4965 Heap::kOld)));
4972 func.set_parameter_names(Array::Handle(Array::New(num_parameters, 4966 func.set_parameter_names(Array::Handle(Array::New(num_parameters,
4973 Heap::kOld))); 4967 Heap::kOld)));
4974 for (int i = 0; i < num_parameters; i++) { 4968 for (int i = 0; i < num_parameters; i++) {
4975 ParamDesc& param_desc = (*params->parameters)[i]; 4969 ParamDesc& param_desc = (*params->parameters)[i];
4976 ASSERT(is_top_level_ || param_desc.type->IsResolved());
4977 func.SetParameterTypeAt(i, *param_desc.type); 4970 func.SetParameterTypeAt(i, *param_desc.type);
4978 func.SetParameterNameAt(i, *param_desc.name); 4971 func.SetParameterNameAt(i, *param_desc.name);
4979 } 4972 }
4980 } 4973 }
4981 4974
4982 4975
4983 // Populate local scope with the formal parameters. 4976 // Populate local scope with the formal parameters.
4984 void Parser::AddFormalParamsToScope(const ParamList* params, 4977 void Parser::AddFormalParamsToScope(const ParamList* params,
4985 LocalScope* scope) { 4978 LocalScope* scope) {
4986 ASSERT((params != NULL) && (params->parameters != NULL)); 4979 ASSERT((params != NULL) && (params->parameters != NULL));
(...skipping 1946 matching lines...) Expand 10 before | Expand all | Expand 10 after
6933 va_start(args, format); 6926 va_start(args, format);
6934 const Error& error = Error::Handle( 6927 const Error& error = Error::Handle(
6935 FormatError(script_, TokenPos(), "Error", format, args)); 6928 FormatError(script_, TokenPos(), "Error", format, args));
6936 va_end(args); 6929 va_end(args);
6937 isolate()->long_jump_base()->Jump(1, error); 6930 isolate()->long_jump_base()->Jump(1, error);
6938 UNREACHABLE(); 6931 UNREACHABLE();
6939 } 6932 }
6940 6933
6941 6934
6942 void Parser::ErrorMsg(const Error& error) { 6935 void Parser::ErrorMsg(const Error& error) {
6943 isolate()->long_jump_base()->Jump(1, error); 6936 Isolate::Current()->long_jump_base()->Jump(1, error);
6944 UNREACHABLE(); 6937 UNREACHABLE();
6945 } 6938 }
6946 6939
6947 6940
6948 void Parser::AppendErrorMsg( 6941 void Parser::AppendErrorMsg(
6949 const Error& prev_error, intptr_t token_pos, const char* format, ...) { 6942 const Error& prev_error, intptr_t token_pos, const char* format, ...) {
6950 va_list args; 6943 va_list args;
6951 va_start(args, format); 6944 va_start(args, format);
6952 const Error& error = Error::Handle(FormatErrorWithAppend( 6945 const Error& error = Error::Handle(FormatErrorWithAppend(
6953 prev_error, script_, token_pos, "Error", format, args)); 6946 prev_error, script_, token_pos, "Error", format, args));
(...skipping 1392 matching lines...) Expand 10 before | Expand all | Expand 10 after
8346 if (is_top_level_) { 8339 if (is_top_level_) {
8347 return (current_member_ != NULL) && 8340 return (current_member_ != NULL) &&
8348 current_member_->has_static && !current_member_->has_factory; 8341 current_member_->has_static && !current_member_->has_factory;
8349 } 8342 }
8350 ASSERT(!current_function().IsNull()); 8343 ASSERT(!current_function().IsNull());
8351 return 8344 return
8352 current_function().is_static() && !current_function().IsInFactoryScope(); 8345 current_function().is_static() && !current_function().IsInFactoryScope();
8353 } 8346 }
8354 8347
8355 8348
8356 const Type* Parser::ReceiverType() const { 8349 const AbstractType* Parser::ReceiverType(const Class& cls) {
8357 ASSERT(!current_class().IsNull()); 8350 ASSERT(!cls.IsNull());
8358 TypeArguments& type_arguments = TypeArguments::Handle(); 8351 TypeArguments& type_arguments = TypeArguments::Handle();
8359 if (current_class().NumTypeParameters() > 0) { 8352 if (cls.NumTypeParameters() > 0) {
8360 type_arguments = current_class().type_parameters(); 8353 type_arguments = cls.type_parameters();
8361 } 8354 }
8362 Type& type = Type::ZoneHandle( 8355 AbstractType& type = AbstractType::ZoneHandle(
8363 Type::New(current_class(), type_arguments, current_class().token_pos())); 8356 Type::New(cls, type_arguments, cls.token_pos()));
8364 if (!is_top_level_ || current_class().is_type_finalized()) { 8357 if (cls.is_type_finalized()) {
8365 type ^= ClassFinalizer::FinalizeType( 8358 type ^= ClassFinalizer::FinalizeType(
8366 current_class(), type, ClassFinalizer::kCanonicalizeWellFormed); 8359 cls, type, ClassFinalizer::kCanonicalizeWellFormed);
8360 // Note that the receiver type may now be a malbounded type.
8367 } 8361 }
8368 return &type; 8362 return &type;
8369 } 8363 }
8370 8364
8371 8365
8372 bool Parser::IsInstantiatorRequired() const { 8366 bool Parser::IsInstantiatorRequired() const {
8373 ASSERT(!current_function().IsNull()); 8367 ASSERT(!current_function().IsNull());
8374 if (current_function().is_static() && 8368 if (current_function().is_static() &&
8375 !current_function().IsInFactoryScope()) { 8369 !current_function().IsInFactoryScope()) {
8376 return false; 8370 return false;
(...skipping 1917 matching lines...) Expand 10 before | Expand all | Expand 10 after
10294 void Parser::SkipQualIdent() { 10288 void Parser::SkipQualIdent() {
10295 ASSERT(IsIdentifier()); 10289 ASSERT(IsIdentifier());
10296 ConsumeToken(); 10290 ConsumeToken();
10297 if (CurrentToken() == Token::kPERIOD) { 10291 if (CurrentToken() == Token::kPERIOD) {
10298 ConsumeToken(); // Consume the kPERIOD token. 10292 ConsumeToken(); // Consume the kPERIOD token.
10299 ExpectIdentifier("identifier expected after '.'"); 10293 ExpectIdentifier("identifier expected after '.'");
10300 } 10294 }
10301 } 10295 }
10302 10296
10303 } // namespace dart 10297 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/parser.h ('k') | tests/co19/co19-runtime.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698