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/object.h" | 5 #include "vm/object.h" |
6 | 6 |
7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
8 #include "platform/assert.h" | 8 #include "platform/assert.h" |
9 #include "vm/assembler.h" | 9 #include "vm/assembler.h" |
10 #include "vm/become.h" | 10 #include "vm/become.h" |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 Bool* Object::bool_true_ = NULL; | 128 Bool* Object::bool_true_ = NULL; |
129 Bool* Object::bool_false_ = NULL; | 129 Bool* Object::bool_false_ = NULL; |
130 Smi* Object::smi_illegal_cid_ = NULL; | 130 Smi* Object::smi_illegal_cid_ = NULL; |
131 LanguageError* Object::snapshot_writer_error_ = NULL; | 131 LanguageError* Object::snapshot_writer_error_ = NULL; |
132 LanguageError* Object::branch_offset_error_ = NULL; | 132 LanguageError* Object::branch_offset_error_ = NULL; |
133 LanguageError* Object::speculative_inlining_error_ = NULL; | 133 LanguageError* Object::speculative_inlining_error_ = NULL; |
134 LanguageError* Object::background_compilation_error_ = NULL; | 134 LanguageError* Object::background_compilation_error_ = NULL; |
135 Array* Object::vm_isolate_snapshot_object_table_ = NULL; | 135 Array* Object::vm_isolate_snapshot_object_table_ = NULL; |
136 Type* Object::dynamic_type_ = NULL; | 136 Type* Object::dynamic_type_ = NULL; |
137 Type* Object::void_type_ = NULL; | 137 Type* Object::void_type_ = NULL; |
| 138 Type* Object::vector_type_ = NULL; |
138 | 139 |
139 RawObject* Object::null_ = reinterpret_cast<RawObject*>(RAW_NULL); | 140 RawObject* Object::null_ = reinterpret_cast<RawObject*>(RAW_NULL); |
140 RawClass* Object::class_class_ = reinterpret_cast<RawClass*>(RAW_NULL); | 141 RawClass* Object::class_class_ = reinterpret_cast<RawClass*>(RAW_NULL); |
141 RawClass* Object::dynamic_class_ = reinterpret_cast<RawClass*>(RAW_NULL); | 142 RawClass* Object::dynamic_class_ = reinterpret_cast<RawClass*>(RAW_NULL); |
| 143 RawClass* Object::vector_class_ = reinterpret_cast<RawClass*>(RAW_NULL); |
142 RawClass* Object::void_class_ = reinterpret_cast<RawClass*>(RAW_NULL); | 144 RawClass* Object::void_class_ = reinterpret_cast<RawClass*>(RAW_NULL); |
143 RawClass* Object::unresolved_class_class_ = | 145 RawClass* Object::unresolved_class_class_ = |
144 reinterpret_cast<RawClass*>(RAW_NULL); | 146 reinterpret_cast<RawClass*>(RAW_NULL); |
145 RawClass* Object::type_arguments_class_ = reinterpret_cast<RawClass*>(RAW_NULL); | 147 RawClass* Object::type_arguments_class_ = reinterpret_cast<RawClass*>(RAW_NULL); |
146 RawClass* Object::patch_class_class_ = reinterpret_cast<RawClass*>(RAW_NULL); | 148 RawClass* Object::patch_class_class_ = reinterpret_cast<RawClass*>(RAW_NULL); |
147 RawClass* Object::function_class_ = reinterpret_cast<RawClass*>(RAW_NULL); | 149 RawClass* Object::function_class_ = reinterpret_cast<RawClass*>(RAW_NULL); |
148 RawClass* Object::closure_data_class_ = reinterpret_cast<RawClass*>(RAW_NULL); | 150 RawClass* Object::closure_data_class_ = reinterpret_cast<RawClass*>(RAW_NULL); |
149 RawClass* Object::signature_data_class_ = reinterpret_cast<RawClass*>(RAW_NULL); | 151 RawClass* Object::signature_data_class_ = reinterpret_cast<RawClass*>(RAW_NULL); |
150 RawClass* Object::redirection_data_class_ = | 152 RawClass* Object::redirection_data_class_ = |
151 reinterpret_cast<RawClass*>(RAW_NULL); | 153 reinterpret_cast<RawClass*>(RAW_NULL); |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 bool_true_ = Bool::ReadOnlyHandle(); | 522 bool_true_ = Bool::ReadOnlyHandle(); |
521 bool_false_ = Bool::ReadOnlyHandle(); | 523 bool_false_ = Bool::ReadOnlyHandle(); |
522 smi_illegal_cid_ = Smi::ReadOnlyHandle(); | 524 smi_illegal_cid_ = Smi::ReadOnlyHandle(); |
523 snapshot_writer_error_ = LanguageError::ReadOnlyHandle(); | 525 snapshot_writer_error_ = LanguageError::ReadOnlyHandle(); |
524 branch_offset_error_ = LanguageError::ReadOnlyHandle(); | 526 branch_offset_error_ = LanguageError::ReadOnlyHandle(); |
525 speculative_inlining_error_ = LanguageError::ReadOnlyHandle(); | 527 speculative_inlining_error_ = LanguageError::ReadOnlyHandle(); |
526 background_compilation_error_ = LanguageError::ReadOnlyHandle(); | 528 background_compilation_error_ = LanguageError::ReadOnlyHandle(); |
527 vm_isolate_snapshot_object_table_ = Array::ReadOnlyHandle(); | 529 vm_isolate_snapshot_object_table_ = Array::ReadOnlyHandle(); |
528 dynamic_type_ = Type::ReadOnlyHandle(); | 530 dynamic_type_ = Type::ReadOnlyHandle(); |
529 void_type_ = Type::ReadOnlyHandle(); | 531 void_type_ = Type::ReadOnlyHandle(); |
| 532 vector_type_ = Type::ReadOnlyHandle(); |
530 | 533 |
531 *null_object_ = Object::null(); | 534 *null_object_ = Object::null(); |
532 *null_array_ = Array::null(); | 535 *null_array_ = Array::null(); |
533 *null_string_ = String::null(); | 536 *null_string_ = String::null(); |
534 *null_instance_ = Instance::null(); | 537 *null_instance_ = Instance::null(); |
535 *null_type_arguments_ = TypeArguments::null(); | 538 *null_type_arguments_ = TypeArguments::null(); |
536 | 539 |
537 // Initialize the empty and zero array handles to null_ in order to be able to | 540 // Initialize the empty and zero array handles to null_ in order to be able to |
538 // check if the empty and zero arrays were allocated (RAW_NULL is not | 541 // check if the empty and zero arrays were allocated (RAW_NULL is not |
539 // available). | 542 // available). |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
879 dynamic_class_ = cls.raw(); | 882 dynamic_class_ = cls.raw(); |
880 | 883 |
881 cls = Class::New<Instance>(kVoidCid); | 884 cls = Class::New<Instance>(kVoidCid); |
882 cls.set_num_type_arguments(0); | 885 cls.set_num_type_arguments(0); |
883 cls.set_num_own_type_arguments(0); | 886 cls.set_num_own_type_arguments(0); |
884 cls.set_is_finalized(); | 887 cls.set_is_finalized(); |
885 cls.set_is_type_finalized(); | 888 cls.set_is_type_finalized(); |
886 cls.set_is_cycle_free(); | 889 cls.set_is_cycle_free(); |
887 void_class_ = cls.raw(); | 890 void_class_ = cls.raw(); |
888 | 891 |
| 892 cls = Class::New<Instance>(kVectorCid); |
| 893 cls.set_num_type_arguments(0); |
| 894 cls.set_num_own_type_arguments(0); |
| 895 cls.set_is_finalized(); |
| 896 cls.set_is_type_finalized(); |
| 897 cls.set_is_cycle_free(); |
| 898 vector_class_ = cls.raw(); |
| 899 |
889 cls = Class::New<Type>(); | 900 cls = Class::New<Type>(); |
890 cls.set_is_finalized(); | 901 cls.set_is_finalized(); |
891 cls.set_is_type_finalized(); | 902 cls.set_is_type_finalized(); |
892 cls.set_is_cycle_free(); | 903 cls.set_is_cycle_free(); |
893 | 904 |
894 cls = dynamic_class_; | 905 cls = dynamic_class_; |
895 *dynamic_type_ = Type::NewNonParameterizedType(cls); | 906 *dynamic_type_ = Type::NewNonParameterizedType(cls); |
896 | 907 |
897 cls = void_class_; | 908 cls = void_class_; |
898 *void_type_ = Type::NewNonParameterizedType(cls); | 909 *void_type_ = Type::NewNonParameterizedType(cls); |
899 | 910 |
| 911 cls = vector_class_; |
| 912 *vector_type_ = Type::NewNonParameterizedType(cls); |
| 913 |
900 // Allocate and initialize singleton true and false boolean objects. | 914 // Allocate and initialize singleton true and false boolean objects. |
901 cls = Class::New<Bool>(); | 915 cls = Class::New<Bool>(); |
902 isolate->object_store()->set_bool_class(cls); | 916 isolate->object_store()->set_bool_class(cls); |
903 *bool_true_ = Bool::New(true); | 917 *bool_true_ = Bool::New(true); |
904 *bool_false_ = Bool::New(false); | 918 *bool_false_ = Bool::New(false); |
905 | 919 |
906 *smi_illegal_cid_ = Smi::New(kIllegalCid); | 920 *smi_illegal_cid_ = Smi::New(kIllegalCid); |
907 | 921 |
908 String& error_str = String::Handle(); | 922 String& error_str = String::Handle(); |
909 error_str = String::New("SnapshotWriter Error", Heap::kOld); | 923 error_str = String::New("SnapshotWriter Error", Heap::kOld); |
(...skipping 4394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5304 #if defined(DART_PRECOMPILED_RUNTIME) | 5318 #if defined(DART_PRECOMPILED_RUNTIME) |
5305 UNREACHABLE(); | 5319 UNREACHABLE(); |
5306 #else | 5320 #else |
5307 ASSERT(Thread::Current()->IsMutatorThread()); | 5321 ASSERT(Thread::Current()->IsMutatorThread()); |
5308 ASSERT(value.IsNull() || !value.is_optimized()); | 5322 ASSERT(value.IsNull() || !value.is_optimized()); |
5309 StorePointer(&raw_ptr()->unoptimized_code_, value.raw()); | 5323 StorePointer(&raw_ptr()->unoptimized_code_, value.raw()); |
5310 #endif | 5324 #endif |
5311 } | 5325 } |
5312 | 5326 |
5313 RawContextScope* Function::context_scope() const { | 5327 RawContextScope* Function::context_scope() const { |
5314 if (IsClosureFunction()) { | 5328 if (IsClosureFunction() || IsConvertedClosureFunction()) { |
5315 const Object& obj = Object::Handle(raw_ptr()->data_); | 5329 const Object& obj = Object::Handle(raw_ptr()->data_); |
5316 ASSERT(!obj.IsNull()); | 5330 ASSERT(!obj.IsNull()); |
5317 return ClosureData::Cast(obj).context_scope(); | 5331 return ClosureData::Cast(obj).context_scope(); |
5318 } | 5332 } |
5319 return ContextScope::null(); | 5333 return ContextScope::null(); |
5320 } | 5334 } |
5321 | 5335 |
5322 void Function::set_context_scope(const ContextScope& value) const { | 5336 void Function::set_context_scope(const ContextScope& value) const { |
5323 if (IsClosureFunction()) { | 5337 if (IsClosureFunction() || IsConvertedClosureFunction()) { |
5324 const Object& obj = Object::Handle(raw_ptr()->data_); | 5338 const Object& obj = Object::Handle(raw_ptr()->data_); |
5325 ASSERT(!obj.IsNull()); | 5339 ASSERT(!obj.IsNull()); |
5326 ClosureData::Cast(obj).set_context_scope(value); | 5340 ClosureData::Cast(obj).set_context_scope(value); |
5327 return; | 5341 return; |
5328 } | 5342 } |
5329 UNREACHABLE(); | 5343 UNREACHABLE(); |
5330 } | 5344 } |
5331 | 5345 |
5332 RawInstance* Function::implicit_static_closure() const { | 5346 RawInstance* Function::implicit_static_closure() const { |
5333 if (IsImplicitStaticClosureFunction()) { | 5347 if (IsImplicitStaticClosureFunction()) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5403 field ^= fields.At(i); | 5417 field ^= fields.At(i); |
5404 ASSERT(!field.IsNull()); | 5418 ASSERT(!field.IsNull()); |
5405 if (field.token_pos() == token_pos()) { | 5419 if (field.token_pos() == token_pos()) { |
5406 return field.raw(); | 5420 return field.raw(); |
5407 } | 5421 } |
5408 } | 5422 } |
5409 return Field::null(); | 5423 return Field::null(); |
5410 } | 5424 } |
5411 | 5425 |
5412 RawFunction* Function::parent_function() const { | 5426 RawFunction* Function::parent_function() const { |
5413 if (IsClosureFunction() || IsSignatureFunction()) { | 5427 if (IsClosureFunction() || IsConvertedClosureFunction() || |
| 5428 IsSignatureFunction()) { |
5414 const Object& obj = Object::Handle(raw_ptr()->data_); | 5429 const Object& obj = Object::Handle(raw_ptr()->data_); |
5415 ASSERT(!obj.IsNull()); | 5430 ASSERT(!obj.IsNull()); |
5416 if (IsClosureFunction()) { | 5431 if (IsClosureFunction() || IsConvertedClosureFunction()) { |
5417 return ClosureData::Cast(obj).parent_function(); | 5432 return ClosureData::Cast(obj).parent_function(); |
5418 } else { | 5433 } else { |
5419 return SignatureData::Cast(obj).parent_function(); | 5434 return SignatureData::Cast(obj).parent_function(); |
5420 } | 5435 } |
5421 } | 5436 } |
5422 return Function::null(); | 5437 return Function::null(); |
5423 } | 5438 } |
5424 | 5439 |
5425 void Function::set_parent_function(const Function& value) const { | 5440 void Function::set_parent_function(const Function& value) const { |
5426 const Object& obj = Object::Handle(raw_ptr()->data_); | 5441 const Object& obj = Object::Handle(raw_ptr()->data_); |
5427 ASSERT(!obj.IsNull()); | 5442 ASSERT(!obj.IsNull()); |
5428 if (IsClosureFunction()) { | 5443 if (IsClosureFunction() || IsConvertedClosureFunction()) { |
5429 ClosureData::Cast(obj).set_parent_function(value); | 5444 ClosureData::Cast(obj).set_parent_function(value); |
5430 } else { | 5445 } else { |
5431 ASSERT(IsSignatureFunction()); | 5446 ASSERT(IsSignatureFunction()); |
5432 SignatureData::Cast(obj).set_parent_function(value); | 5447 SignatureData::Cast(obj).set_parent_function(value); |
5433 } | 5448 } |
5434 } | 5449 } |
5435 | 5450 |
5436 bool Function::HasGenericParent() const { | 5451 bool Function::HasGenericParent() const { |
5437 if (IsImplicitClosureFunction()) { | 5452 if (IsImplicitClosureFunction()) { |
5438 // The parent function of an implicit closure function is not the enclosing | 5453 // The parent function of an implicit closure function is not the enclosing |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5473 const Object& obj = Object::Handle(raw_ptr()->data_); | 5488 const Object& obj = Object::Handle(raw_ptr()->data_); |
5474 ASSERT(obj.IsArray()); | 5489 ASSERT(obj.IsArray()); |
5475 ASSERT((Array::Cast(obj).At(1) == Object::null()) || value.IsNull()); | 5490 ASSERT((Array::Cast(obj).At(1) == Object::null()) || value.IsNull()); |
5476 Array::Cast(obj).SetAt(1, value); | 5491 Array::Cast(obj).SetAt(1, value); |
5477 } else { | 5492 } else { |
5478 ASSERT((raw_ptr()->data_ == Object::null()) || value.IsNull()); | 5493 ASSERT((raw_ptr()->data_ == Object::null()) || value.IsNull()); |
5479 set_data(value); | 5494 set_data(value); |
5480 } | 5495 } |
5481 } | 5496 } |
5482 | 5497 |
| 5498 RawFunction* Function::converted_closure_function() const { |
| 5499 if (IsClosureFunction() || IsSignatureFunction() || IsFactory()) { |
| 5500 return Function::null(); |
| 5501 } |
| 5502 const Object& obj = Object::Handle(raw_ptr()->data_); |
| 5503 ASSERT(obj.IsNull() || obj.IsFunction()); |
| 5504 if (obj.IsFunction()) { |
| 5505 return Function::Cast(obj).raw(); |
| 5506 } |
| 5507 return Function::null(); |
| 5508 } |
| 5509 |
| 5510 void Function::set_converted_closure_function(const Function& value) const { |
| 5511 ASSERT(!IsClosureFunction() && !IsSignatureFunction() && !is_native()); |
| 5512 ASSERT((raw_ptr()->data_ == Object::null()) || value.IsNull()); |
| 5513 set_data(value); |
| 5514 } |
| 5515 |
5483 RawType* Function::ExistingSignatureType() const { | 5516 RawType* Function::ExistingSignatureType() const { |
5484 const Object& obj = Object::Handle(raw_ptr()->data_); | 5517 const Object& obj = Object::Handle(raw_ptr()->data_); |
5485 ASSERT(!obj.IsNull()); | 5518 ASSERT(!obj.IsNull()); |
5486 if (IsSignatureFunction()) { | 5519 if (IsSignatureFunction()) { |
5487 return SignatureData::Cast(obj).signature_type(); | 5520 return SignatureData::Cast(obj).signature_type(); |
5488 } else { | 5521 } else { |
5489 ASSERT(IsClosureFunction()); | 5522 ASSERT(IsClosureFunction() || IsConvertedClosureFunction()); |
5490 return ClosureData::Cast(obj).signature_type(); | 5523 return ClosureData::Cast(obj).signature_type(); |
5491 } | 5524 } |
5492 } | 5525 } |
5493 | 5526 |
5494 RawType* Function::SignatureType() const { | 5527 RawType* Function::SignatureType() const { |
5495 Type& type = Type::Handle(ExistingSignatureType()); | 5528 Type& type = Type::Handle(ExistingSignatureType()); |
5496 if (type.IsNull()) { | 5529 if (type.IsNull()) { |
5497 // The function type of this function is not yet cached and needs to be | 5530 // The function type of this function is not yet cached and needs to be |
5498 // constructed and cached here. | 5531 // constructed and cached here. |
5499 // A function type is type parameterized in the same way as the owner class | 5532 // A function type is type parameterized in the same way as the owner class |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5532 return type.raw(); | 5565 return type.raw(); |
5533 } | 5566 } |
5534 | 5567 |
5535 void Function::SetSignatureType(const Type& value) const { | 5568 void Function::SetSignatureType(const Type& value) const { |
5536 const Object& obj = Object::Handle(raw_ptr()->data_); | 5569 const Object& obj = Object::Handle(raw_ptr()->data_); |
5537 ASSERT(!obj.IsNull()); | 5570 ASSERT(!obj.IsNull()); |
5538 if (IsSignatureFunction()) { | 5571 if (IsSignatureFunction()) { |
5539 SignatureData::Cast(obj).set_signature_type(value); | 5572 SignatureData::Cast(obj).set_signature_type(value); |
5540 ASSERT(!value.IsCanonical() || (value.signature() == this->raw())); | 5573 ASSERT(!value.IsCanonical() || (value.signature() == this->raw())); |
5541 } else { | 5574 } else { |
5542 ASSERT(IsClosureFunction()); | 5575 ASSERT(IsClosureFunction() || IsConvertedClosureFunction()); |
5543 ClosureData::Cast(obj).set_signature_type(value); | 5576 ClosureData::Cast(obj).set_signature_type(value); |
5544 } | 5577 } |
5545 } | 5578 } |
5546 | 5579 |
5547 bool Function::IsRedirectingFactory() const { | 5580 bool Function::IsRedirectingFactory() const { |
5548 if (!IsFactory() || !is_redirecting()) { | 5581 if (!IsFactory() || !is_redirecting()) { |
5549 return false; | 5582 return false; |
5550 } | 5583 } |
5551 ASSERT(!IsClosureFunction()); // A factory cannot also be a closure. | 5584 ASSERT(!IsClosureFunction()); // A factory cannot also be a closure. |
5552 return true; | 5585 return true; |
(...skipping 1019 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6572 NOT_IN_PRECOMPILED(result.set_deoptimization_counter(0)); | 6605 NOT_IN_PRECOMPILED(result.set_deoptimization_counter(0)); |
6573 NOT_IN_PRECOMPILED(result.set_optimized_instruction_count(0)); | 6606 NOT_IN_PRECOMPILED(result.set_optimized_instruction_count(0)); |
6574 NOT_IN_PRECOMPILED(result.set_optimized_call_site_count(0)); | 6607 NOT_IN_PRECOMPILED(result.set_optimized_call_site_count(0)); |
6575 result.set_kernel_offset(0); | 6608 result.set_kernel_offset(0); |
6576 result.set_is_optimizable(is_native ? false : true); | 6609 result.set_is_optimizable(is_native ? false : true); |
6577 result.set_is_inlinable(true); | 6610 result.set_is_inlinable(true); |
6578 result.set_allows_hoisting_check_class(true); | 6611 result.set_allows_hoisting_check_class(true); |
6579 result.set_allows_bounds_check_generalization(true); | 6612 result.set_allows_bounds_check_generalization(true); |
6580 result.SetInstructionsSafe( | 6613 result.SetInstructionsSafe( |
6581 Code::Handle(StubCode::LazyCompile_entry()->code())); | 6614 Code::Handle(StubCode::LazyCompile_entry()->code())); |
6582 if (kind == RawFunction::kClosureFunction) { | 6615 if (kind == RawFunction::kClosureFunction || |
| 6616 kind == RawFunction::kConvertedClosureFunction) { |
6583 ASSERT(space == Heap::kOld); | 6617 ASSERT(space == Heap::kOld); |
6584 const ClosureData& data = ClosureData::Handle(ClosureData::New()); | 6618 const ClosureData& data = ClosureData::Handle(ClosureData::New()); |
6585 result.set_data(data); | 6619 result.set_data(data); |
6586 } else if (kind == RawFunction::kSignatureFunction) { | 6620 } else if (kind == RawFunction::kSignatureFunction) { |
6587 const SignatureData& data = | 6621 const SignatureData& data = |
6588 SignatureData::Handle(SignatureData::New(space)); | 6622 SignatureData::Handle(SignatureData::New(space)); |
6589 result.set_data(data); | 6623 result.set_data(data); |
6590 } else { | 6624 } else { |
6591 // Functions other than signature functions have no reason to be allocated | 6625 // Functions other than signature functions have no reason to be allocated |
6592 // in new space. | 6626 // in new space. |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6655 Function::New(name, RawFunction::kClosureFunction, | 6689 Function::New(name, RawFunction::kClosureFunction, |
6656 /* is_static = */ parent.is_static(), | 6690 /* is_static = */ parent.is_static(), |
6657 /* is_const = */ false, | 6691 /* is_const = */ false, |
6658 /* is_abstract = */ false, | 6692 /* is_abstract = */ false, |
6659 /* is_external = */ false, | 6693 /* is_external = */ false, |
6660 /* is_native = */ false, parent_owner, token_pos)); | 6694 /* is_native = */ false, parent_owner, token_pos)); |
6661 result.set_parent_function(parent); | 6695 result.set_parent_function(parent); |
6662 return result.raw(); | 6696 return result.raw(); |
6663 } | 6697 } |
6664 | 6698 |
| 6699 RawFunction* Function::NewConvertedClosureFunction(const String& name, |
| 6700 const Function& parent, |
| 6701 TokenPosition token_pos) { |
| 6702 ASSERT(!parent.IsNull()); |
| 6703 // Only static top-level functions are allowed to be converted right now. |
| 6704 ASSERT(parent.is_static()); |
| 6705 // Use the owner defining the parent function and not the class containing it. |
| 6706 const Object& parent_owner = Object::Handle(parent.raw_ptr()->owner_); |
| 6707 ASSERT(!parent_owner.IsNull()); |
| 6708 const Function& result = Function::Handle( |
| 6709 Function::New(name, RawFunction::kConvertedClosureFunction, |
| 6710 /* is_static = */ true, |
| 6711 /* is_const = */ false, |
| 6712 /* is_abstract = */ false, |
| 6713 /* is_external = */ false, |
| 6714 /* is_native = */ false, parent_owner, token_pos)); |
| 6715 result.set_parent_function(parent); |
| 6716 return result.raw(); |
| 6717 } |
| 6718 |
6665 RawFunction* Function::NewSignatureFunction(const Object& owner, | 6719 RawFunction* Function::NewSignatureFunction(const Object& owner, |
6666 const Function& parent, | 6720 const Function& parent, |
6667 TokenPosition token_pos, | 6721 TokenPosition token_pos, |
6668 Heap::Space space) { | 6722 Heap::Space space) { |
6669 const Function& result = Function::Handle(Function::New( | 6723 const Function& result = Function::Handle(Function::New( |
6670 Symbols::AnonymousSignature(), RawFunction::kSignatureFunction, | 6724 Symbols::AnonymousSignature(), RawFunction::kSignatureFunction, |
6671 /* is_static = */ false, | 6725 /* is_static = */ false, |
6672 /* is_const = */ false, | 6726 /* is_const = */ false, |
6673 /* is_abstract = */ false, | 6727 /* is_abstract = */ false, |
6674 /* is_external = */ false, | 6728 /* is_external = */ false, |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6780 | 6834 |
6781 void Function::DropUncompiledImplicitClosureFunction() const { | 6835 void Function::DropUncompiledImplicitClosureFunction() const { |
6782 if (implicit_closure_function() != Function::null()) { | 6836 if (implicit_closure_function() != Function::null()) { |
6783 const Function& func = Function::Handle(implicit_closure_function()); | 6837 const Function& func = Function::Handle(implicit_closure_function()); |
6784 if (!func.HasCode()) { | 6838 if (!func.HasCode()) { |
6785 set_implicit_closure_function(Function::Handle()); | 6839 set_implicit_closure_function(Function::Handle()); |
6786 } | 6840 } |
6787 } | 6841 } |
6788 } | 6842 } |
6789 | 6843 |
| 6844 // Converted closure functions represent a pair of a top-level function and a |
| 6845 // vector of captured variables. When being invoked, converted closure |
| 6846 // functions get the vector as the first argument, and the arguments supplied at |
| 6847 // the invocation are passed as the remaining arguments to that function. |
| 6848 // |
| 6849 // Consider the following example in Kernel: |
| 6850 // |
| 6851 // static method foo(core::int start) → core::Function { |
| 6852 // core::int i = 0; |
| 6853 // return () → dynamic => start.+( |
| 6854 // let final dynamic #t1 = i in |
| 6855 // let final dynamic #t2 = i = #t1.+(1) in |
| 6856 // #t1 |
| 6857 // ); |
| 6858 // } |
| 6859 // |
| 6860 // The method [foo] above produces a closure as a result. The closure captures |
| 6861 // two variables: function parameter [start] and local variable [i]. Here is |
| 6862 // how this code would look like after closure conversion: |
| 6863 // |
| 6864 // static method foo(core::int start) → core::Function { |
| 6865 // final Vector #context = MakeVector(3); |
| 6866 // #context[1] = start; |
| 6867 // #context[2] = 0; |
| 6868 // return MakeClosure<() → dynamic>(com::closure#foo#function, #context); |
| 6869 // } |
| 6870 // static method closure#foo#function(Vector #contextParameter) → dynamic { |
| 6871 // return (#contextParameter[1]).+( |
| 6872 // let final dynamic #t1 = #contextParameter[2] in |
| 6873 // let final dynamic #t2 = #contextParameter[2] = #t1.+(1) in |
| 6874 // #t1 |
| 6875 // ); |
| 6876 // } |
| 6877 // |
| 6878 // Converted closure functions are used in VM Closure instances that represent |
| 6879 // the results of evaluation of [MakeClosure] primitive operations. |
| 6880 // |
| 6881 // Internally, converted closure functins are represented with the same Closure |
| 6882 // class as implicit closure functions (that are used for dealing with |
| 6883 // tear-offs). The Closure class instances have two fields, one for the |
| 6884 // function, and one for the captured context. Implicit closure functions have |
| 6885 // pre-defined shape of the context: it's a single variable that is used as |
| 6886 // 'this'. Converted closure functions use the context field to store the |
| 6887 // vector of captured variables that will be supplied as the first argument |
| 6888 // during invocation. |
| 6889 // |
| 6890 // The top-level functions used in converted closure functions are generated |
| 6891 // during a front-end transformation in Kernel. Those functions have some |
| 6892 // common properties: |
| 6893 // * they expect the captured context to be passed as the first argument, |
| 6894 // * the first argument should be of [Vector] type (index-based storage), |
| 6895 // * they retrieve the captured variables from [Vector] explicitly, so they |
| 6896 // don't need the variables from the context to be put into their current |
| 6897 // scope. |
| 6898 // |
| 6899 // During closure-conversion pass in Kernel, the contexts are generated |
| 6900 // explicitly and are represented as [Vector]s. Then they are paired together |
| 6901 // with the top-level functions to form a closure. When the closure, created |
| 6902 // this way, is invoked, it should receive the [Vector] as the first argument, |
| 6903 // and take the rest of the arguments from the invocation. |
| 6904 // |
| 6905 // Converted cosure functions in VM follow same discipline as implicit closure |
| 6906 // functions, because they are similar in many ways. For further deatils, please |
| 6907 // refer to the following methods: |
| 6908 // -> Function::ConvertedClosureFunction |
| 6909 // -> StreamingFlowGraphBuilder::BuildGraphOfConvertedClosureFunction |
| 6910 // -> StreamingFlowGraphBuilder::BuildGraph (small change that calls |
| 6911 // BuildGraphOfConvertedClosureFunction) |
| 6912 // -> StreamingFlowGraphBuilder::BuildClosureCreation (converted closure |
| 6913 // functions are created here) |
| 6914 // |
| 6915 // Function::ConvertedClosureFunction method follows the logic of |
| 6916 // Function::ImplicitClosureFunction method. |
| 6917 // |
| 6918 // TODO(29181): Adjust the method to correctly pass type parameters after they |
| 6919 // are enabled in closure conversion. |
| 6920 RawFunction* Function::ConvertedClosureFunction() const { |
| 6921 // Return the existing converted closure function if any. |
| 6922 if (converted_closure_function() != Function::null()) { |
| 6923 return converted_closure_function(); |
| 6924 } |
| 6925 ASSERT(!IsSignatureFunction() && !IsClosureFunction()); |
| 6926 Thread* thread = Thread::Current(); |
| 6927 Zone* zone = thread->zone(); |
| 6928 // Create closure function. |
| 6929 const String& closure_name = String::Handle(zone, name()); |
| 6930 const Function& closure_function = Function::Handle( |
| 6931 zone, NewConvertedClosureFunction(closure_name, *this, token_pos())); |
| 6932 |
| 6933 // Currently only static top-level functions are allowed to be converted. |
| 6934 ASSERT(is_static()); |
| 6935 closure_function.set_context_scope(Object::empty_context_scope()); |
| 6936 |
| 6937 // Set closure function's type parameters. |
| 6938 closure_function.set_type_parameters( |
| 6939 TypeArguments::Handle(zone, type_parameters())); |
| 6940 |
| 6941 // Set closure function's result type to this result type. |
| 6942 closure_function.set_result_type(AbstractType::Handle(zone, result_type())); |
| 6943 |
| 6944 // Set closure function's end token to this end token. |
| 6945 closure_function.set_end_token_pos(end_token_pos()); |
| 6946 |
| 6947 // The closurized method stub just calls into the original method and should |
| 6948 // therefore be skipped by the debugger and in stack traces. |
| 6949 closure_function.set_is_debuggable(false); |
| 6950 closure_function.set_is_visible(false); |
| 6951 |
| 6952 // Set closure function's formal parameters to this formal parameters, |
| 6953 // removing the first parameter over which the currying is done, and adding |
| 6954 // the closure class instance as the first parameter. So, the overall number |
| 6955 // of fixed parameters doesn't change. |
| 6956 const int num_fixed_params = num_fixed_parameters(); |
| 6957 const int num_opt_params = NumOptionalParameters(); |
| 6958 const bool has_opt_pos_params = HasOptionalPositionalParameters(); |
| 6959 const int num_params = num_fixed_params + num_opt_params; |
| 6960 |
| 6961 closure_function.set_num_fixed_parameters(num_fixed_params); |
| 6962 closure_function.SetNumOptionalParameters(num_opt_params, has_opt_pos_params); |
| 6963 closure_function.set_parameter_types( |
| 6964 Array::Handle(zone, Array::New(num_params, Heap::kOld))); |
| 6965 closure_function.set_parameter_names( |
| 6966 Array::Handle(zone, Array::New(num_params, Heap::kOld))); |
| 6967 |
| 6968 AbstractType& param_type = AbstractType::Handle(zone); |
| 6969 String& param_name = String::Handle(zone); |
| 6970 |
| 6971 // Add implicit closure object as the first parameter. |
| 6972 param_type = Type::DynamicType(); |
| 6973 closure_function.SetParameterTypeAt(0, param_type); |
| 6974 closure_function.SetParameterNameAt(0, Symbols::ClosureParameter()); |
| 6975 |
| 6976 // All the parameters, but the first one, are the same for the top-level |
| 6977 // function being converted, and the method of the closure class that is being |
| 6978 // generated. |
| 6979 for (int i = 1; i < num_params; i++) { |
| 6980 param_type = ParameterTypeAt(i); |
| 6981 closure_function.SetParameterTypeAt(i, param_type); |
| 6982 param_name = ParameterNameAt(i); |
| 6983 closure_function.SetParameterNameAt(i, param_name); |
| 6984 } |
| 6985 closure_function.set_kernel_offset(kernel_offset()); |
| 6986 |
| 6987 const Type& signature_type = |
| 6988 Type::Handle(zone, closure_function.SignatureType()); |
| 6989 if (!signature_type.IsFinalized()) { |
| 6990 ClassFinalizer::FinalizeType(Class::Handle(zone, Owner()), signature_type); |
| 6991 } |
| 6992 |
| 6993 set_converted_closure_function(closure_function); |
| 6994 |
| 6995 return closure_function.raw(); |
| 6996 } |
| 6997 |
| 6998 void Function::DropUncompiledConvertedClosureFunction() const { |
| 6999 if (converted_closure_function() != Function::null()) { |
| 7000 const Function& func = Function::Handle(converted_closure_function()); |
| 7001 if (!func.HasCode()) { |
| 7002 set_converted_closure_function(Function::Handle()); |
| 7003 } |
| 7004 } |
| 7005 } |
| 7006 |
6790 RawString* Function::UserVisibleFormalParameters() const { | 7007 RawString* Function::UserVisibleFormalParameters() const { |
6791 Thread* thread = Thread::Current(); | 7008 Thread* thread = Thread::Current(); |
6792 Zone* zone = thread->zone(); | 7009 Zone* zone = thread->zone(); |
6793 // Typically 3, 5,.. elements in 'pieces', e.g.: | 7010 // Typically 3, 5,.. elements in 'pieces', e.g.: |
6794 // '_LoadRequest', CommaSpace, '_LoadError'. | 7011 // '_LoadRequest', CommaSpace, '_LoadError'. |
6795 GrowableHandlePtrArray<const String> pieces(zone, 5); | 7012 GrowableHandlePtrArray<const String> pieces(zone, 5); |
6796 BuildSignatureParameters(thread, zone, kUserVisibleName, &pieces); | 7013 BuildSignatureParameters(thread, zone, kUserVisibleName, &pieces); |
6797 return Symbols::FromConcatAll(thread, pieces); | 7014 return Symbols::FromConcatAll(thread, pieces); |
6798 } | 7015 } |
6799 | 7016 |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7258 if (IsNull()) { | 7475 if (IsNull()) { |
7259 return "Function: null"; | 7476 return "Function: null"; |
7260 } | 7477 } |
7261 const char* static_str = is_static() ? " static" : ""; | 7478 const char* static_str = is_static() ? " static" : ""; |
7262 const char* abstract_str = is_abstract() ? " abstract" : ""; | 7479 const char* abstract_str = is_abstract() ? " abstract" : ""; |
7263 const char* kind_str = NULL; | 7480 const char* kind_str = NULL; |
7264 const char* const_str = is_const() ? " const" : ""; | 7481 const char* const_str = is_const() ? " const" : ""; |
7265 switch (kind()) { | 7482 switch (kind()) { |
7266 case RawFunction::kRegularFunction: | 7483 case RawFunction::kRegularFunction: |
7267 case RawFunction::kClosureFunction: | 7484 case RawFunction::kClosureFunction: |
| 7485 case RawFunction::kConvertedClosureFunction: |
7268 case RawFunction::kGetterFunction: | 7486 case RawFunction::kGetterFunction: |
7269 case RawFunction::kSetterFunction: | 7487 case RawFunction::kSetterFunction: |
7270 kind_str = ""; | 7488 kind_str = ""; |
7271 break; | 7489 break; |
7272 case RawFunction::kSignatureFunction: | 7490 case RawFunction::kSignatureFunction: |
7273 kind_str = " signature"; | 7491 kind_str = " signature"; |
7274 break; | 7492 break; |
7275 case RawFunction::kConstructor: | 7493 case RawFunction::kConstructor: |
7276 kind_str = is_static() ? " factory" : " constructor"; | 7494 kind_str = is_static() ? " factory" : " constructor"; |
7277 break; | 7495 break; |
(...skipping 7833 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
15111 instantiated_other.IsDartFunctionType()) { | 15329 instantiated_other.IsDartFunctionType()) { |
15112 return true; | 15330 return true; |
15113 } | 15331 } |
15114 } | 15332 } |
15115 if (!instantiated_other.IsFunctionType()) { | 15333 if (!instantiated_other.IsFunctionType()) { |
15116 return false; | 15334 return false; |
15117 } | 15335 } |
15118 Function& other_signature = | 15336 Function& other_signature = |
15119 Function::Handle(zone, Type::Cast(instantiated_other).signature()); | 15337 Function::Handle(zone, Type::Cast(instantiated_other).signature()); |
15120 Function& sig_fun = Function::Handle(zone, Closure::Cast(*this).function()); | 15338 Function& sig_fun = Function::Handle(zone, Closure::Cast(*this).function()); |
| 15339 if (sig_fun.IsConvertedClosureFunction()) { |
| 15340 const String& closure_name = String::Handle(zone, sig_fun.name()); |
| 15341 const Function& new_sig_fun = Function::Handle( |
| 15342 zone, |
| 15343 Function::NewConvertedClosureFunction( |
| 15344 closure_name, Function::Handle(zone, sig_fun.parent_function()), |
| 15345 TokenPosition::kNoSource)); |
| 15346 |
| 15347 new_sig_fun.set_type_parameters( |
| 15348 TypeArguments::Handle(zone, sig_fun.type_parameters())); |
| 15349 new_sig_fun.set_result_type( |
| 15350 AbstractType::Handle(zone, sig_fun.result_type())); |
| 15351 new_sig_fun.set_end_token_pos(TokenPosition::kNoSource); |
| 15352 |
| 15353 new_sig_fun.set_is_debuggable(false); |
| 15354 new_sig_fun.set_is_visible(false); |
| 15355 |
| 15356 // The converted closed top-level function type should have its first |
| 15357 // required optional parameter, i.e. context, removed. |
| 15358 const int num_fixed_params = sig_fun.num_fixed_parameters() - 1; |
| 15359 const int num_opt_params = sig_fun.NumOptionalParameters(); |
| 15360 const bool has_opt_pos_params = sig_fun.HasOptionalPositionalParameters(); |
| 15361 const int num_params = num_fixed_params + num_opt_params; |
| 15362 new_sig_fun.set_num_fixed_parameters(num_fixed_params); |
| 15363 new_sig_fun.SetNumOptionalParameters(num_opt_params, has_opt_pos_params); |
| 15364 new_sig_fun.set_parameter_types( |
| 15365 Array::Handle(zone, Array::New(num_params, Heap::kOld))); |
| 15366 new_sig_fun.set_parameter_names( |
| 15367 Array::Handle(zone, Array::New(num_params, Heap::kOld))); |
| 15368 AbstractType& param_type = AbstractType::Handle(zone); |
| 15369 String& param_name = String::Handle(zone); |
| 15370 for (int i = 0; i < num_params; i++) { |
| 15371 param_type = sig_fun.ParameterTypeAt(i + 1); |
| 15372 new_sig_fun.SetParameterTypeAt(i, param_type); |
| 15373 param_name = sig_fun.ParameterNameAt(i + 1); |
| 15374 new_sig_fun.SetParameterNameAt(i, param_name); |
| 15375 } |
| 15376 |
| 15377 sig_fun = new_sig_fun.raw(); |
| 15378 } |
15121 if (!sig_fun.HasInstantiatedSignature()) { | 15379 if (!sig_fun.HasInstantiatedSignature()) { |
15122 const TypeArguments& instantiator_type_arguments = TypeArguments::Handle( | 15380 const TypeArguments& instantiator_type_arguments = TypeArguments::Handle( |
15123 zone, Closure::Cast(*this).instantiator_type_arguments()); | 15381 zone, Closure::Cast(*this).instantiator_type_arguments()); |
15124 const TypeArguments& function_type_arguments = TypeArguments::Handle( | 15382 const TypeArguments& function_type_arguments = TypeArguments::Handle( |
15125 zone, Closure::Cast(*this).function_type_arguments()); | 15383 zone, Closure::Cast(*this).function_type_arguments()); |
15126 sig_fun = sig_fun.InstantiateSignatureFrom( | 15384 sig_fun = sig_fun.InstantiateSignatureFrom( |
15127 instantiator_type_arguments, function_type_arguments, Heap::kOld); | 15385 instantiator_type_arguments, function_type_arguments, Heap::kOld); |
15128 } | 15386 } |
15129 return sig_fun.IsSubtypeOf(other_signature, bound_error, NULL, Heap::kOld); | 15387 return sig_fun.IsSubtypeOf(other_signature, bound_error, NULL, Heap::kOld); |
15130 } | 15388 } |
(...skipping 7135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
22266 } | 22524 } |
22267 return UserTag::null(); | 22525 return UserTag::null(); |
22268 } | 22526 } |
22269 | 22527 |
22270 const char* UserTag::ToCString() const { | 22528 const char* UserTag::ToCString() const { |
22271 const String& tag_label = String::Handle(label()); | 22529 const String& tag_label = String::Handle(label()); |
22272 return tag_label.ToCString(); | 22530 return tag_label.ToCString(); |
22273 } | 22531 } |
22274 | 22532 |
22275 } // namespace dart | 22533 } // namespace dart |
OLD | NEW |