| 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 5583 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5594 } | 5594 } |
| 5595 | 5595 |
| 5596 const char* Function::KindToCString(RawFunction::Kind kind) { | 5596 const char* Function::KindToCString(RawFunction::Kind kind) { |
| 5597 switch (kind) { | 5597 switch (kind) { |
| 5598 case RawFunction::kRegularFunction: | 5598 case RawFunction::kRegularFunction: |
| 5599 return "RegularFunction"; | 5599 return "RegularFunction"; |
| 5600 break; | 5600 break; |
| 5601 case RawFunction::kClosureFunction: | 5601 case RawFunction::kClosureFunction: |
| 5602 return "ClosureFunction"; | 5602 return "ClosureFunction"; |
| 5603 break; | 5603 break; |
| 5604 case RawFunction::kImplicitClosureFunction: | |
| 5605 return "ImplicitClosureFunction"; | |
| 5606 break; | |
| 5607 case RawFunction::kSignatureFunction: | 5604 case RawFunction::kSignatureFunction: |
| 5608 return "SignatureFunction"; | 5605 return "SignatureFunction"; |
| 5609 break; | 5606 break; |
| 5610 case RawFunction::kGetterFunction: | 5607 case RawFunction::kGetterFunction: |
| 5611 return "GetterFunction"; | 5608 return "GetterFunction"; |
| 5612 break; | 5609 break; |
| 5613 case RawFunction::kSetterFunction: | 5610 case RawFunction::kSetterFunction: |
| 5614 return "SetterFunction"; | 5611 return "SetterFunction"; |
| 5615 break; | 5612 break; |
| 5616 case RawFunction::kConstructor: | 5613 case RawFunction::kConstructor: |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5983 return is_inlinable() && !is_external() && !is_generated_body() && | 5980 return is_inlinable() && !is_external() && !is_generated_body() && |
| 5984 !thread->isolate()->debugger()->HasBreakpoint(*this, thread->zone()); | 5981 !thread->isolate()->debugger()->HasBreakpoint(*this, thread->zone()); |
| 5985 #endif | 5982 #endif |
| 5986 } | 5983 } |
| 5987 | 5984 |
| 5988 intptr_t Function::NumParameters() const { | 5985 intptr_t Function::NumParameters() const { |
| 5989 return num_fixed_parameters() + NumOptionalParameters(); | 5986 return num_fixed_parameters() + NumOptionalParameters(); |
| 5990 } | 5987 } |
| 5991 | 5988 |
| 5992 intptr_t Function::NumImplicitParameters() const { | 5989 intptr_t Function::NumImplicitParameters() const { |
| 5993 const RawFunction::Kind k = kind(); | 5990 if (kind() == RawFunction::kConstructor) { |
| 5994 if (k == RawFunction::kConstructor) { | |
| 5995 // Type arguments for factory; instance for generative constructor. | 5991 // Type arguments for factory; instance for generative constructor. |
| 5996 return 1; | 5992 return 1; |
| 5997 } | 5993 } |
| 5998 if ((k == RawFunction::kClosureFunction) || | 5994 if ((kind() == RawFunction::kClosureFunction) || |
| 5999 (k == RawFunction::kImplicitClosureFunction) || | 5995 (kind() == RawFunction::kSignatureFunction)) { |
| 6000 (k == RawFunction::kSignatureFunction)) { | |
| 6001 return 1; // Closure object. | 5996 return 1; // Closure object. |
| 6002 } | 5997 } |
| 6003 if (!is_static()) { | 5998 if (!is_static()) { |
| 6004 // Closure functions defined inside instance (i.e. non-static) functions are | 5999 // Closure functions defined inside instance (i.e. non-static) functions are |
| 6005 // marked as non-static, but they do not have a receiver. | 6000 // marked as non-static, but they do not have a receiver. |
| 6006 // Closures are handled above. | 6001 // Closures are handled above. |
| 6007 ASSERT((k != RawFunction::kClosureFunction) && | 6002 ASSERT((kind() != RawFunction::kClosureFunction) && |
| 6008 (k != RawFunction::kImplicitClosureFunction) && | 6003 (kind() != RawFunction::kSignatureFunction)); |
| 6009 (k != RawFunction::kSignatureFunction)); | |
| 6010 return 1; // Receiver. | 6004 return 1; // Receiver. |
| 6011 } | 6005 } |
| 6012 return 0; // No implicit parameters. | 6006 return 0; // No implicit parameters. |
| 6013 } | 6007 } |
| 6014 | 6008 |
| 6015 bool Function::AreValidArgumentCounts(intptr_t num_type_arguments, | 6009 bool Function::AreValidArgumentCounts(intptr_t num_type_arguments, |
| 6016 intptr_t num_arguments, | 6010 intptr_t num_arguments, |
| 6017 intptr_t num_named_arguments, | 6011 intptr_t num_named_arguments, |
| 6018 String* error_message) const { | 6012 String* error_message) const { |
| 6019 if ((num_type_arguments != 0) && | 6013 if ((num_type_arguments != 0) && |
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6529 return true; | 6523 return true; |
| 6530 } | 6524 } |
| 6531 | 6525 |
| 6532 // The compiler generates an implicit constructor if a class definition | 6526 // The compiler generates an implicit constructor if a class definition |
| 6533 // does not contain an explicit constructor or factory. The implicit | 6527 // does not contain an explicit constructor or factory. The implicit |
| 6534 // constructor has the same token position as the owner class. | 6528 // constructor has the same token position as the owner class. |
| 6535 bool Function::IsImplicitConstructor() const { | 6529 bool Function::IsImplicitConstructor() const { |
| 6536 return IsGenerativeConstructor() && (token_pos() == end_token_pos()); | 6530 return IsGenerativeConstructor() && (token_pos() == end_token_pos()); |
| 6537 } | 6531 } |
| 6538 | 6532 |
| 6533 bool Function::IsImplicitClosureFunction() const { |
| 6534 if (!IsClosureFunction()) { |
| 6535 return false; |
| 6536 } |
| 6537 const Function& parent = Function::Handle(parent_function()); |
| 6538 return (parent.implicit_closure_function() == raw()); |
| 6539 } |
| 6540 |
| 6539 bool Function::IsImplicitStaticClosureFunction(RawFunction* func) { | 6541 bool Function::IsImplicitStaticClosureFunction(RawFunction* func) { |
| 6540 NoSafepointScope no_safepoint; | 6542 NoSafepointScope no_safepoint; |
| 6541 uint32_t kind_tag = func->ptr()->kind_tag_; | 6543 uint32_t kind_tag = func->ptr()->kind_tag_; |
| 6542 return (KindBits::decode(kind_tag) == | 6544 if (KindBits::decode(kind_tag) != RawFunction::kClosureFunction) { |
| 6543 RawFunction::kImplicitClosureFunction) && | 6545 return false; |
| 6544 StaticBit::decode(kind_tag); | 6546 } |
| 6547 if (!StaticBit::decode(kind_tag)) { |
| 6548 return false; |
| 6549 } |
| 6550 RawClosureData* data = reinterpret_cast<RawClosureData*>(func->ptr()->data_); |
| 6551 RawFunction* parent_function = data->ptr()->parent_function_; |
| 6552 return (parent_function->ptr()->data_ == reinterpret_cast<RawObject*>(func)); |
| 6545 } | 6553 } |
| 6546 | 6554 |
| 6547 bool Function::IsConstructorClosureFunction() const { | 6555 bool Function::IsConstructorClosureFunction() const { |
| 6548 return IsClosureFunction() && | 6556 return IsClosureFunction() && |
| 6549 String::Handle(name()).StartsWith(Symbols::ConstructorClosurePrefix()); | 6557 String::Handle(name()).StartsWith(Symbols::ConstructorClosurePrefix()); |
| 6550 } | 6558 } |
| 6551 | 6559 |
| 6552 RawFunction* Function::New(Heap::Space space) { | 6560 RawFunction* Function::New(Heap::Space space) { |
| 6553 ASSERT(Object::function_class() != Class::null()); | 6561 ASSERT(Object::function_class() != Class::null()); |
| 6554 RawObject* raw = | 6562 RawObject* raw = |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6598 NOT_IN_PRECOMPILED(result.set_optimized_instruction_count(0)); | 6606 NOT_IN_PRECOMPILED(result.set_optimized_instruction_count(0)); |
| 6599 NOT_IN_PRECOMPILED(result.set_optimized_call_site_count(0)); | 6607 NOT_IN_PRECOMPILED(result.set_optimized_call_site_count(0)); |
| 6600 result.set_kernel_offset(0); | 6608 result.set_kernel_offset(0); |
| 6601 result.set_is_optimizable(is_native ? false : true); | 6609 result.set_is_optimizable(is_native ? false : true); |
| 6602 result.set_is_inlinable(true); | 6610 result.set_is_inlinable(true); |
| 6603 result.set_allows_hoisting_check_class(true); | 6611 result.set_allows_hoisting_check_class(true); |
| 6604 result.set_allows_bounds_check_generalization(true); | 6612 result.set_allows_bounds_check_generalization(true); |
| 6605 result.SetInstructionsSafe( | 6613 result.SetInstructionsSafe( |
| 6606 Code::Handle(StubCode::LazyCompile_entry()->code())); | 6614 Code::Handle(StubCode::LazyCompile_entry()->code())); |
| 6607 if (kind == RawFunction::kClosureFunction || | 6615 if (kind == RawFunction::kClosureFunction || |
| 6608 kind == RawFunction::kImplicitClosureFunction || | |
| 6609 kind == RawFunction::kConvertedClosureFunction) { | 6616 kind == RawFunction::kConvertedClosureFunction) { |
| 6610 ASSERT(space == Heap::kOld); | 6617 ASSERT(space == Heap::kOld); |
| 6611 const ClosureData& data = ClosureData::Handle(ClosureData::New()); | 6618 const ClosureData& data = ClosureData::Handle(ClosureData::New()); |
| 6612 result.set_data(data); | 6619 result.set_data(data); |
| 6613 } else if (kind == RawFunction::kSignatureFunction) { | 6620 } else if (kind == RawFunction::kSignatureFunction) { |
| 6614 const SignatureData& data = | 6621 const SignatureData& data = |
| 6615 SignatureData::Handle(SignatureData::New(space)); | 6622 SignatureData::Handle(SignatureData::New(space)); |
| 6616 result.set_data(data); | 6623 result.set_data(data); |
| 6617 } else { | 6624 } else { |
| 6618 // Functions other than signature functions have no reason to be allocated | 6625 // Functions other than signature functions have no reason to be allocated |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6664 clone.set_parameter_types(array); | 6671 clone.set_parameter_types(array); |
| 6665 for (intptr_t i = 0; i < num_params; i++) { | 6672 for (intptr_t i = 0; i < num_params; i++) { |
| 6666 type = clone.ParameterTypeAt(i); | 6673 type = clone.ParameterTypeAt(i); |
| 6667 type ^= type.CloneUninstantiated(new_owner); | 6674 type ^= type.CloneUninstantiated(new_owner); |
| 6668 clone.SetParameterTypeAt(i, type); | 6675 clone.SetParameterTypeAt(i, type); |
| 6669 } | 6676 } |
| 6670 } | 6677 } |
| 6671 return clone.raw(); | 6678 return clone.raw(); |
| 6672 } | 6679 } |
| 6673 | 6680 |
| 6674 RawFunction* Function::NewClosureFunctionWithKind(RawFunction::Kind kind, | 6681 RawFunction* Function::NewClosureFunction(const String& name, |
| 6675 const String& name, | 6682 const Function& parent, |
| 6676 const Function& parent, | 6683 TokenPosition token_pos) { |
| 6677 TokenPosition token_pos) { | |
| 6678 ASSERT((kind == RawFunction::kClosureFunction) || | |
| 6679 (kind == RawFunction::kImplicitClosureFunction) || | |
| 6680 (kind == RawFunction::kConvertedClosureFunction)); | |
| 6681 ASSERT(!parent.IsNull()); | 6684 ASSERT(!parent.IsNull()); |
| 6682 // Only static top-level functions are allowed to be converted right now. | |
| 6683 ASSERT((kind != RawFunction::kConvertedClosureFunction) || | |
| 6684 parent.is_static()); | |
| 6685 // Use the owner defining the parent function and not the class containing it. | 6685 // Use the owner defining the parent function and not the class containing it. |
| 6686 const Object& parent_owner = Object::Handle(parent.raw_ptr()->owner_); | 6686 const Object& parent_owner = Object::Handle(parent.raw_ptr()->owner_); |
| 6687 ASSERT(!parent_owner.IsNull()); | 6687 ASSERT(!parent_owner.IsNull()); |
| 6688 const Function& result = Function::Handle( | 6688 const Function& result = Function::Handle( |
| 6689 Function::New(name, kind, | 6689 Function::New(name, RawFunction::kClosureFunction, |
| 6690 /* is_static = */ parent.is_static(), | 6690 /* is_static = */ parent.is_static(), |
| 6691 /* is_const = */ false, | 6691 /* is_const = */ false, |
| 6692 /* is_abstract = */ false, | 6692 /* is_abstract = */ false, |
| 6693 /* is_external = */ false, | 6693 /* is_external = */ false, |
| 6694 /* is_native = */ false, parent_owner, token_pos)); | 6694 /* is_native = */ false, parent_owner, token_pos)); |
| 6695 result.set_parent_function(parent); | 6695 result.set_parent_function(parent); |
| 6696 return result.raw(); | 6696 return result.raw(); |
| 6697 } | 6697 } |
| 6698 | 6698 |
| 6699 RawFunction* Function::NewClosureFunction(const String& name, | |
| 6700 const Function& parent, | |
| 6701 TokenPosition token_pos) { | |
| 6702 return NewClosureFunctionWithKind(RawFunction::kClosureFunction, name, parent, | |
| 6703 token_pos); | |
| 6704 } | |
| 6705 | |
| 6706 RawFunction* Function::NewImplicitClosureFunction(const String& name, | |
| 6707 const Function& parent, | |
| 6708 TokenPosition token_pos) { | |
| 6709 return NewClosureFunctionWithKind(RawFunction::kImplicitClosureFunction, name, | |
| 6710 parent, token_pos); | |
| 6711 } | |
| 6712 | |
| 6713 RawFunction* Function::NewConvertedClosureFunction(const String& name, | 6699 RawFunction* Function::NewConvertedClosureFunction(const String& name, |
| 6714 const Function& parent, | 6700 const Function& parent, |
| 6715 TokenPosition token_pos) { | 6701 TokenPosition token_pos) { |
| 6716 return NewClosureFunctionWithKind(RawFunction::kConvertedClosureFunction, | 6702 ASSERT(!parent.IsNull()); |
| 6717 name, parent, token_pos); | 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(); |
| 6718 } | 6717 } |
| 6719 | 6718 |
| 6720 RawFunction* Function::NewSignatureFunction(const Object& owner, | 6719 RawFunction* Function::NewSignatureFunction(const Object& owner, |
| 6721 const Function& parent, | 6720 const Function& parent, |
| 6722 TokenPosition token_pos, | 6721 TokenPosition token_pos, |
| 6723 Heap::Space space) { | 6722 Heap::Space space) { |
| 6724 const Function& result = Function::Handle(Function::New( | 6723 const Function& result = Function::Handle(Function::New( |
| 6725 Symbols::AnonymousSignature(), RawFunction::kSignatureFunction, | 6724 Symbols::AnonymousSignature(), RawFunction::kSignatureFunction, |
| 6726 /* is_static = */ false, | 6725 /* is_static = */ false, |
| 6727 /* is_const = */ false, | 6726 /* is_const = */ false, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6761 // Return the existing implicit closure function if any. | 6760 // Return the existing implicit closure function if any. |
| 6762 if (implicit_closure_function() != Function::null()) { | 6761 if (implicit_closure_function() != Function::null()) { |
| 6763 return implicit_closure_function(); | 6762 return implicit_closure_function(); |
| 6764 } | 6763 } |
| 6765 ASSERT(!IsSignatureFunction() && !IsClosureFunction()); | 6764 ASSERT(!IsSignatureFunction() && !IsClosureFunction()); |
| 6766 Thread* thread = Thread::Current(); | 6765 Thread* thread = Thread::Current(); |
| 6767 Zone* zone = thread->zone(); | 6766 Zone* zone = thread->zone(); |
| 6768 // Create closure function. | 6767 // Create closure function. |
| 6769 const String& closure_name = String::Handle(zone, name()); | 6768 const String& closure_name = String::Handle(zone, name()); |
| 6770 const Function& closure_function = Function::Handle( | 6769 const Function& closure_function = Function::Handle( |
| 6771 zone, NewImplicitClosureFunction(closure_name, *this, token_pos())); | 6770 zone, NewClosureFunction(closure_name, *this, token_pos())); |
| 6772 | 6771 |
| 6773 // Set closure function's context scope. | 6772 // Set closure function's context scope. |
| 6774 if (is_static()) { | 6773 if (is_static()) { |
| 6775 closure_function.set_context_scope(Object::empty_context_scope()); | 6774 closure_function.set_context_scope(Object::empty_context_scope()); |
| 6776 } else { | 6775 } else { |
| 6777 const ContextScope& context_scope = ContextScope::Handle( | 6776 const ContextScope& context_scope = ContextScope::Handle( |
| 6778 zone, LocalScope::CreateImplicitClosureScope(*this)); | 6777 zone, LocalScope::CreateImplicitClosureScope(*this)); |
| 6779 closure_function.set_context_scope(context_scope); | 6778 closure_function.set_context_scope(context_scope); |
| 6780 } | 6779 } |
| 6781 | 6780 |
| (...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7476 if (IsNull()) { | 7475 if (IsNull()) { |
| 7477 return "Function: null"; | 7476 return "Function: null"; |
| 7478 } | 7477 } |
| 7479 const char* static_str = is_static() ? " static" : ""; | 7478 const char* static_str = is_static() ? " static" : ""; |
| 7480 const char* abstract_str = is_abstract() ? " abstract" : ""; | 7479 const char* abstract_str = is_abstract() ? " abstract" : ""; |
| 7481 const char* kind_str = NULL; | 7480 const char* kind_str = NULL; |
| 7482 const char* const_str = is_const() ? " const" : ""; | 7481 const char* const_str = is_const() ? " const" : ""; |
| 7483 switch (kind()) { | 7482 switch (kind()) { |
| 7484 case RawFunction::kRegularFunction: | 7483 case RawFunction::kRegularFunction: |
| 7485 case RawFunction::kClosureFunction: | 7484 case RawFunction::kClosureFunction: |
| 7486 case RawFunction::kImplicitClosureFunction: | |
| 7487 case RawFunction::kConvertedClosureFunction: | 7485 case RawFunction::kConvertedClosureFunction: |
| 7488 case RawFunction::kGetterFunction: | 7486 case RawFunction::kGetterFunction: |
| 7489 case RawFunction::kSetterFunction: | 7487 case RawFunction::kSetterFunction: |
| 7490 kind_str = ""; | 7488 kind_str = ""; |
| 7491 break; | 7489 break; |
| 7492 case RawFunction::kSignatureFunction: | 7490 case RawFunction::kSignatureFunction: |
| 7493 kind_str = " signature"; | 7491 kind_str = " signature"; |
| 7494 break; | 7492 break; |
| 7495 case RawFunction::kConstructor: | 7493 case RawFunction::kConstructor: |
| 7496 kind_str = is_static() ? " factory" : " constructor"; | 7494 kind_str = is_static() ? " factory" : " constructor"; |
| (...skipping 15029 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 22526 } | 22524 } |
| 22527 return UserTag::null(); | 22525 return UserTag::null(); |
| 22528 } | 22526 } |
| 22529 | 22527 |
| 22530 const char* UserTag::ToCString() const { | 22528 const char* UserTag::ToCString() const { |
| 22531 const String& tag_label = String::Handle(label()); | 22529 const String& tag_label = String::Handle(label()); |
| 22532 return tag_label.ToCString(); | 22530 return tag_label.ToCString(); |
| 22533 } | 22531 } |
| 22534 | 22532 |
| 22535 } // namespace dart | 22533 } // namespace dart |
| OLD | NEW |