| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/parser.h" | 5 #include "vm/parser.h" |
| 6 | 6 |
| 7 #include "lib/invocation_mirror.h" | 7 #include "lib/invocation_mirror.h" |
| 8 #include "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 763 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 774 ASSERT(!func.is_static()); | 774 ASSERT(!func.is_static()); |
| 775 node_sequence = parser.ParseInstanceGetter(func); | 775 node_sequence = parser.ParseInstanceGetter(func); |
| 776 break; | 776 break; |
| 777 case RawFunction::kImplicitSetter: | 777 case RawFunction::kImplicitSetter: |
| 778 ASSERT(!func.is_static()); | 778 ASSERT(!func.is_static()); |
| 779 node_sequence = parser.ParseInstanceSetter(func); | 779 node_sequence = parser.ParseInstanceSetter(func); |
| 780 break; | 780 break; |
| 781 case RawFunction::kImplicitStaticFinalGetter: | 781 case RawFunction::kImplicitStaticFinalGetter: |
| 782 node_sequence = parser.ParseStaticFinalGetter(func); | 782 node_sequence = parser.ParseStaticFinalGetter(func); |
| 783 break; | 783 break; |
| 784 case RawFunction::kStaticInitializer: |
| 785 node_sequence = parser.ParseStaticInitializer(func); |
| 786 break; |
| 784 case RawFunction::kMethodExtractor: | 787 case RawFunction::kMethodExtractor: |
| 785 node_sequence = parser.ParseMethodExtractor(func); | 788 node_sequence = parser.ParseMethodExtractor(func); |
| 786 break; | 789 break; |
| 787 case RawFunction::kNoSuchMethodDispatcher: | 790 case RawFunction::kNoSuchMethodDispatcher: |
| 788 node_sequence = | 791 node_sequence = |
| 789 parser.ParseNoSuchMethodDispatcher(func, default_parameter_values); | 792 parser.ParseNoSuchMethodDispatcher(func, default_parameter_values); |
| 790 break; | 793 break; |
| 791 case RawFunction::kInvokeFieldDispatcher: | 794 case RawFunction::kInvokeFieldDispatcher: |
| 792 node_sequence = | 795 node_sequence = |
| 793 parser.ParseInvokeFieldDispatcher(func, default_parameter_values); | 796 parser.ParseInvokeFieldDispatcher(func, default_parameter_values); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 900 // Build local scope for function and populate with the formal parameters. | 903 // Build local scope for function and populate with the formal parameters. |
| 901 OpenFunctionBlock(func); | 904 OpenFunctionBlock(func); |
| 902 AddFormalParamsToScope(¶ms, current_block_->scope); | 905 AddFormalParamsToScope(¶ms, current_block_->scope); |
| 903 | 906 |
| 904 intptr_t ident_pos = TokenPos(); | 907 intptr_t ident_pos = TokenPos(); |
| 905 const String& field_name = *ExpectIdentifier("field name expected"); | 908 const String& field_name = *ExpectIdentifier("field name expected"); |
| 906 const Class& field_class = Class::Handle(func.Owner()); | 909 const Class& field_class = Class::Handle(func.Owner()); |
| 907 const Field& field = | 910 const Field& field = |
| 908 Field::ZoneHandle(field_class.LookupStaticField(field_name)); | 911 Field::ZoneHandle(field_class.LookupStaticField(field_name)); |
| 909 | 912 |
| 910 if (!field.is_const() && | 913 // Static final fields must have an initializer. |
| 911 (field.value() != Object::transition_sentinel().raw()) && | |
| 912 (field.value() != Object::sentinel().raw())) { | |
| 913 // The field has already been initialized at compile time (this can | |
| 914 // happen, e.g., if we are recompiling for optimization). There is no | |
| 915 // need to check for initialization and compile the potentially very | |
| 916 // large initialization code. By skipping this code, the deoptimization | |
| 917 // ids will not line up with the original code, but this is safe because | |
| 918 // LoadStaticField does not deoptimize. | |
| 919 LoadStaticFieldNode* load_node = new LoadStaticFieldNode(ident_pos, field); | |
| 920 ReturnNode* return_node = new ReturnNode(ident_pos, load_node); | |
| 921 current_block_->statements->Add(return_node); | |
| 922 return CloseBlock(); | |
| 923 } | |
| 924 | |
| 925 // Static const fields must have an initializer. | |
| 926 ExpectToken(Token::kASSIGN); | 914 ExpectToken(Token::kASSIGN); |
| 927 | 915 |
| 928 // We don't want to use ParseConstExpr() here because we don't want | |
| 929 // the constant folding code to create, compile and execute a code | |
| 930 // fragment to evaluate the expression. Instead, we just make sure | |
| 931 // the static const field initializer is a constant expression and | |
| 932 // leave the evaluation to the getter function. | |
| 933 const intptr_t expr_pos = TokenPos(); | 916 const intptr_t expr_pos = TokenPos(); |
| 934 AstNode* expr = ParseExpr(kAllowConst, kConsumeCascades); | |
| 935 | |
| 936 if (field.is_const()) { | 917 if (field.is_const()) { |
| 918 // We don't want to use ParseConstExpr() here because we don't want |
| 919 // the constant folding code to create, compile and execute a code |
| 920 // fragment to evaluate the expression. Instead, we just make sure |
| 921 // the static const field initializer is a constant expression and |
| 922 // leave the evaluation to the getter function. |
| 923 AstNode* expr = ParseExpr(kAllowConst, kConsumeCascades); |
| 937 // This getter will only be called once at compile time. | 924 // This getter will only be called once at compile time. |
| 938 if (expr->EvalConstExpr() == NULL) { | 925 if (expr->EvalConstExpr() == NULL) { |
| 939 ErrorMsg(expr_pos, "initializer is not a valid compile-time constant"); | 926 ErrorMsg(expr_pos, "initializer is not a valid compile-time constant"); |
| 940 } | 927 } |
| 941 ReturnNode* return_node = new ReturnNode(ident_pos, expr); | 928 ReturnNode* return_node = new ReturnNode(ident_pos, expr); |
| 942 current_block_->statements->Add(return_node); | 929 current_block_->statements->Add(return_node); |
| 943 } else { | 930 } else { |
| 944 // This getter may be called each time the static field is accessed. | 931 // This getter may be called each time the static field is accessed. |
| 945 // The following generated code lazily initializes the field: | 932 // The following generated code lazily initializes the field: |
| 946 // if (field.value === transition_sentinel) { | 933 // if (field.value === transition_sentinel) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 988 SequenceNode* initialize_field = new SequenceNode(ident_pos, NULL); | 975 SequenceNode* initialize_field = new SequenceNode(ident_pos, NULL); |
| 989 initialize_field->Add( | 976 initialize_field->Add( |
| 990 new StoreStaticFieldNode( | 977 new StoreStaticFieldNode( |
| 991 ident_pos, | 978 ident_pos, |
| 992 field, | 979 field, |
| 993 new LiteralNode(ident_pos, Object::transition_sentinel()))); | 980 new LiteralNode(ident_pos, Object::transition_sentinel()))); |
| 994 // TODO(hausner): If evaluation of the field value throws an exception, | 981 // TODO(hausner): If evaluation of the field value throws an exception, |
| 995 // we leave the field value as 'transition_sentinel', which is wrong. | 982 // we leave the field value as 'transition_sentinel', which is wrong. |
| 996 // A second reference to the field later throws a circular dependency | 983 // A second reference to the field later throws a circular dependency |
| 997 // exception. The field should instead be set to null after an exception. | 984 // exception. The field should instead be set to null after an exception. |
| 998 initialize_field->Add(new StoreStaticFieldNode(ident_pos, field, expr)); | 985 const String& init_name = |
| 986 String::Handle(Symbols::New(String::Handle( |
| 987 String::Concat(Symbols::InitPrefix(), String::Handle(field.name()))))); |
| 988 const Function& init_function = Function::ZoneHandle( |
| 989 field_class.LookupStaticFunction(init_name)); |
| 990 ASSERT(!init_function.IsNull()); |
| 991 ArgumentListNode* arguments = new ArgumentListNode(expr_pos); |
| 992 StaticCallNode* init_call = |
| 993 new StaticCallNode(expr_pos, init_function, arguments); |
| 994 |
| 995 initialize_field->Add(new StoreStaticFieldNode(ident_pos, |
| 996 field, |
| 997 init_call)); |
| 999 AstNode* uninitialized_check = | 998 AstNode* uninitialized_check = |
| 1000 new IfNode(ident_pos, compare_uninitialized, initialize_field, NULL); | 999 new IfNode(ident_pos, compare_uninitialized, initialize_field, NULL); |
| 1001 current_block_->statements->Add(uninitialized_check); | 1000 current_block_->statements->Add(uninitialized_check); |
| 1002 | 1001 |
| 1003 // Generate code returning the field value. | 1002 // Generate code returning the field value. |
| 1004 ReturnNode* return_node = | 1003 ReturnNode* return_node = |
| 1005 new ReturnNode(ident_pos, | 1004 new ReturnNode(ident_pos, |
| 1006 new LoadStaticFieldNode(ident_pos, field)); | 1005 new LoadStaticFieldNode(ident_pos, field)); |
| 1007 current_block_->statements->Add(return_node); | 1006 current_block_->statements->Add(return_node); |
| 1008 } | 1007 } |
| 1009 return CloseBlock(); | 1008 return CloseBlock(); |
| 1010 } | 1009 } |
| 1011 | 1010 |
| 1012 | 1011 |
| 1012 SequenceNode* Parser::ParseStaticInitializer(const Function& func) { |
| 1013 TRACE_PARSER("ParseStaticInitializer"); |
| 1014 ParamList params; |
| 1015 ASSERT(func.num_fixed_parameters() == 0); // static. |
| 1016 ASSERT(!func.HasOptionalParameters()); |
| 1017 ASSERT(AbstractType::Handle(func.result_type()).IsResolved()); |
| 1018 |
| 1019 // Build local scope for function and populate with the formal parameters. |
| 1020 OpenFunctionBlock(func); |
| 1021 AddFormalParamsToScope(¶ms, current_block_->scope); |
| 1022 |
| 1023 intptr_t expr_pos = func.token_pos(); |
| 1024 AstNode* expr = ParseExpr(kAllowConst, kConsumeCascades); |
| 1025 ReturnNode* return_node = new ReturnNode(expr_pos, expr); |
| 1026 current_block_->statements->Add(return_node); |
| 1027 return CloseBlock(); |
| 1028 } |
| 1029 |
| 1030 |
| 1013 // Create AstNodes for an implicit instance getter method: | 1031 // Create AstNodes for an implicit instance getter method: |
| 1014 // LoadLocalNode 0 ('this'); | 1032 // LoadLocalNode 0 ('this'); |
| 1015 // LoadInstanceFieldNode (field_name); | 1033 // LoadInstanceFieldNode (field_name); |
| 1016 // ReturnNode (field's value); | 1034 // ReturnNode (field's value); |
| 1017 SequenceNode* Parser::ParseInstanceGetter(const Function& func) { | 1035 SequenceNode* Parser::ParseInstanceGetter(const Function& func) { |
| 1018 TRACE_PARSER("ParseInstanceGetter"); | 1036 TRACE_PARSER("ParseInstanceGetter"); |
| 1019 ParamList params; | 1037 ParamList params; |
| 1020 // func.token_pos() points to the name of the field. | 1038 // func.token_pos() points to the name of the field. |
| 1021 intptr_t ident_pos = func.token_pos(); | 1039 intptr_t ident_pos = func.token_pos(); |
| 1022 ASSERT(current_class().raw() == func.Owner()); | 1040 ASSERT(current_class().raw() == func.Owner()); |
| (...skipping 2223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3246 if (field->has_factory) { | 3264 if (field->has_factory) { |
| 3247 ErrorMsg("keyword 'factory' not allowed in field declaration"); | 3265 ErrorMsg("keyword 'factory' not allowed in field declaration"); |
| 3248 } | 3266 } |
| 3249 if (!field->has_static && field->has_const) { | 3267 if (!field->has_static && field->has_const) { |
| 3250 ErrorMsg(field->name_pos, "instance field may not be 'const'"); | 3268 ErrorMsg(field->name_pos, "instance field may not be 'const'"); |
| 3251 } | 3269 } |
| 3252 Function& getter = Function::Handle(); | 3270 Function& getter = Function::Handle(); |
| 3253 Function& setter = Function::Handle(); | 3271 Function& setter = Function::Handle(); |
| 3254 Field& class_field = Field::ZoneHandle(); | 3272 Field& class_field = Field::ZoneHandle(); |
| 3255 Instance& init_value = Instance::Handle(); | 3273 Instance& init_value = Instance::Handle(); |
| 3274 intptr_t expr_pos = -1; |
| 3256 while (true) { | 3275 while (true) { |
| 3257 bool has_initializer = CurrentToken() == Token::kASSIGN; | 3276 bool has_initializer = CurrentToken() == Token::kASSIGN; |
| 3258 bool has_simple_literal = false; | 3277 bool has_simple_literal = false; |
| 3259 if (has_initializer) { | 3278 if (has_initializer) { |
| 3260 ConsumeToken(); | 3279 ConsumeToken(); |
| 3261 init_value = Object::sentinel().raw(); | 3280 init_value = Object::sentinel().raw(); |
| 3262 // For static const fields and static final non-const fields, the | 3281 // For static const fields and static final non-const fields, the |
| 3263 // initialization expression will be parsed through the | 3282 // initialization expression will be parsed through the |
| 3264 // kImplicitStaticFinalGetter method invocation/compilation. | 3283 // kImplicitStaticFinalGetter method invocation/compilation. |
| 3265 // For instance fields, the expression is parsed when a constructor | 3284 // For instance fields, the expression is parsed when a constructor |
| 3266 // is compiled. | 3285 // is compiled. |
| 3267 // For static const fields and static final non-const fields with very | 3286 // For static const fields and static final non-const fields with very |
| 3268 // simple initializer expressions (e.g. a literal number or string), we | 3287 // simple initializer expressions (e.g. a literal number or string), we |
| 3269 // optimize away the kImplicitStaticFinalGetter and initialize the field | 3288 // optimize away the kImplicitStaticFinalGetter and initialize the field |
| 3270 // here. However, the class finalizer will check the value type for | 3289 // here. However, the class finalizer will check the value type for |
| 3271 // assignability once the declared field type can be resolved. If the | 3290 // assignability once the declared field type can be resolved. If the |
| 3272 // value is not assignable (assuming checked mode and disregarding actual | 3291 // value is not assignable (assuming checked mode and disregarding actual |
| 3273 // mode), the field value is reset and a kImplicitStaticFinalGetter is | 3292 // mode), the field value is reset and a kImplicitStaticFinalGetter is |
| 3274 // created at finalization time. | 3293 // created at finalization time. |
| 3275 | 3294 |
| 3276 if (field->has_static && (field->has_const || field->has_final) && | 3295 if (field->has_static && (field->has_const || field->has_final) && |
| 3277 (LookaheadToken(1) == Token::kSEMICOLON)) { | 3296 (LookaheadToken(1) == Token::kSEMICOLON)) { |
| 3278 has_simple_literal = IsSimpleLiteral(*field->type, &init_value); | 3297 has_simple_literal = IsSimpleLiteral(*field->type, &init_value); |
| 3279 } | 3298 } |
| 3299 expr_pos = TokenPos(); |
| 3280 SkipExpr(); | 3300 SkipExpr(); |
| 3281 } else { | 3301 } else { |
| 3282 // Static const and static final fields must have an initializer. | 3302 // Static const and static final fields must have an initializer. |
| 3283 // Static const fields are implicitly final. | 3303 // Static const fields are implicitly final. |
| 3284 if (field->has_static && field->has_final) { | 3304 if (field->has_static && field->has_final) { |
| 3285 ErrorMsg(field->name_pos, | 3305 ErrorMsg(field->name_pos, |
| 3286 "static %s field '%s' must have an initializer expression", | 3306 "static %s field '%s' must have an initializer expression", |
| 3287 field->has_const ? "const" : "final", | 3307 field->has_const ? "const" : "final", |
| 3288 field->name->ToCString()); | 3308 field->name->ToCString()); |
| 3289 } | 3309 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 3313 getter = Function::New(getter_name, | 3333 getter = Function::New(getter_name, |
| 3314 RawFunction::kImplicitStaticFinalGetter, | 3334 RawFunction::kImplicitStaticFinalGetter, |
| 3315 field->has_static, | 3335 field->has_static, |
| 3316 field->has_const, | 3336 field->has_const, |
| 3317 /* is_abstract = */ false, | 3337 /* is_abstract = */ false, |
| 3318 /* is_external = */ false, | 3338 /* is_external = */ false, |
| 3319 current_class(), | 3339 current_class(), |
| 3320 field->name_pos); | 3340 field->name_pos); |
| 3321 getter.set_result_type(*field->type); | 3341 getter.set_result_type(*field->type); |
| 3322 members->AddFunction(getter); | 3342 members->AddFunction(getter); |
| 3343 |
| 3344 // Create initializer function. |
| 3345 const Function& init_function = Function::ZoneHandle( |
| 3346 Function::NewStaticInitializer(*field->name, |
| 3347 *field->type, |
| 3348 current_class(), |
| 3349 expr_pos)); |
| 3350 members->AddFunction(init_function); |
| 3323 } | 3351 } |
| 3324 } | 3352 } |
| 3325 | 3353 |
| 3326 // For instance fields, we create implicit getter and setter methods. | 3354 // For instance fields, we create implicit getter and setter methods. |
| 3327 if (!field->has_static) { | 3355 if (!field->has_static) { |
| 3328 String& getter_name = String::Handle(Field::GetterSymbol(*field->name)); | 3356 String& getter_name = String::Handle(Field::GetterSymbol(*field->name)); |
| 3329 getter = Function::New(getter_name, RawFunction::kImplicitGetter, | 3357 getter = Function::New(getter_name, RawFunction::kImplicitGetter, |
| 3330 field->has_static, | 3358 field->has_static, |
| 3331 field->has_final, | 3359 field->has_final, |
| 3332 /* is_abstract = */ false, | 3360 /* is_abstract = */ false, |
| (...skipping 1089 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4422 if (metadata_pos >= 0) { | 4450 if (metadata_pos >= 0) { |
| 4423 library_.AddFieldMetadata(field, metadata_pos); | 4451 library_.AddFieldMetadata(field, metadata_pos); |
| 4424 } | 4452 } |
| 4425 if (CurrentToken() == Token::kASSIGN) { | 4453 if (CurrentToken() == Token::kASSIGN) { |
| 4426 ConsumeToken(); | 4454 ConsumeToken(); |
| 4427 Instance& field_value = Instance::Handle(Object::sentinel().raw()); | 4455 Instance& field_value = Instance::Handle(Object::sentinel().raw()); |
| 4428 bool has_simple_literal = false; | 4456 bool has_simple_literal = false; |
| 4429 if ((is_const || is_final) && (LookaheadToken(1) == Token::kSEMICOLON)) { | 4457 if ((is_const || is_final) && (LookaheadToken(1) == Token::kSEMICOLON)) { |
| 4430 has_simple_literal = IsSimpleLiteral(type, &field_value); | 4458 has_simple_literal = IsSimpleLiteral(type, &field_value); |
| 4431 } | 4459 } |
| 4460 const intptr_t expr_pos = TokenPos(); |
| 4432 SkipExpr(); | 4461 SkipExpr(); |
| 4433 field.set_value(field_value); | 4462 field.set_value(field_value); |
| 4434 if (!has_simple_literal) { | 4463 if (!has_simple_literal) { |
| 4435 // Create a static final getter. | 4464 // Create a static final getter. |
| 4436 String& getter_name = String::Handle(Field::GetterSymbol(var_name)); | 4465 String& getter_name = String::Handle(Field::GetterSymbol(var_name)); |
| 4437 getter = Function::New(getter_name, | 4466 getter = Function::New(getter_name, |
| 4438 RawFunction::kImplicitStaticFinalGetter, | 4467 RawFunction::kImplicitStaticFinalGetter, |
| 4439 is_static, | 4468 is_static, |
| 4440 is_const, | 4469 is_const, |
| 4441 /* is_abstract = */ false, | 4470 /* is_abstract = */ false, |
| 4442 /* is_external = */ false, | 4471 /* is_external = */ false, |
| 4443 current_class(), | 4472 current_class(), |
| 4444 name_pos); | 4473 name_pos); |
| 4445 getter.set_result_type(type); | 4474 getter.set_result_type(type); |
| 4446 top_level->functions.Add(getter); | 4475 top_level->functions.Add(getter); |
| 4476 |
| 4477 // Create initializer function. |
| 4478 const Function& init_function = Function::ZoneHandle( |
| 4479 Function::NewStaticInitializer(var_name, |
| 4480 type, |
| 4481 current_class(), |
| 4482 expr_pos)); |
| 4483 top_level->functions.Add(init_function); |
| 4447 } | 4484 } |
| 4448 } else if (is_final) { | 4485 } else if (is_final) { |
| 4449 ErrorMsg(name_pos, "missing initializer for final or const variable"); | 4486 ErrorMsg(name_pos, "missing initializer for final or const variable"); |
| 4450 } | 4487 } |
| 4451 | 4488 |
| 4452 if (CurrentToken() == Token::kCOMMA) { | 4489 if (CurrentToken() == Token::kCOMMA) { |
| 4453 ConsumeToken(); | 4490 ConsumeToken(); |
| 4454 } else if (CurrentToken() == Token::kSEMICOLON) { | 4491 } else if (CurrentToken() == Token::kSEMICOLON) { |
| 4455 ConsumeToken(); | 4492 ConsumeToken(); |
| 4456 break; | 4493 break; |
| (...skipping 6137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 10594 void Parser::SkipQualIdent() { | 10631 void Parser::SkipQualIdent() { |
| 10595 ASSERT(IsIdentifier()); | 10632 ASSERT(IsIdentifier()); |
| 10596 ConsumeToken(); | 10633 ConsumeToken(); |
| 10597 if (CurrentToken() == Token::kPERIOD) { | 10634 if (CurrentToken() == Token::kPERIOD) { |
| 10598 ConsumeToken(); // Consume the kPERIOD token. | 10635 ConsumeToken(); // Consume the kPERIOD token. |
| 10599 ExpectIdentifier("identifier expected after '.'"); | 10636 ExpectIdentifier("identifier expected after '.'"); |
| 10600 } | 10637 } |
| 10601 } | 10638 } |
| 10602 | 10639 |
| 10603 } // namespace dart | 10640 } // namespace dart |
| OLD | NEW |