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

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

Issue 2643523002: Implement generic function type syntax in the VM (fixes #27966). (Closed)
Patch Set: refactor parsing of formal parameter per review request Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/vm/class_finalizer.cc ('k') | runtime/vm/parser.h » ('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/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 5139 matching lines...) Expand 10 before | Expand all | Expand 10 after
5150 for (intptr_t i = 0; i < num_types; i++) { 5150 for (intptr_t i = 0; i < num_types; i++) {
5151 type = TypeAt(i); 5151 type = TypeAt(i);
5152 pieces.SetAt(i, String::Handle(zone, type.EnumerateURIs())); 5152 pieces.SetAt(i, String::Handle(zone, type.EnumerateURIs()));
5153 } 5153 }
5154 return String::ConcatAll(pieces); 5154 return String::ConcatAll(pieces);
5155 } 5155 }
5156 5156
5157 5157
5158 const char* TypeArguments::ToCString() const { 5158 const char* TypeArguments::ToCString() const {
5159 if (IsNull()) { 5159 if (IsNull()) {
5160 return "NULL TypeArguments"; 5160 return "TypeArguments: null";
5161 } 5161 }
5162 Zone* zone = Thread::Current()->zone(); 5162 Zone* zone = Thread::Current()->zone();
5163 const char* prev_cstr = OS::SCreate(zone, "TypeArguments: (%" Pd ")", 5163 const char* prev_cstr = OS::SCreate(zone, "TypeArguments: (%" Pd ")",
5164 Smi::Value(raw_ptr()->hash_)); 5164 Smi::Value(raw_ptr()->hash_));
5165 for (int i = 0; i < Length(); i++) { 5165 for (int i = 0; i < Length(); i++) {
5166 const AbstractType& type_at = AbstractType::Handle(zone, TypeAt(i)); 5166 const AbstractType& type_at = AbstractType::Handle(zone, TypeAt(i));
5167 const char* type_cstr = type_at.IsNull() ? "null" : type_at.ToCString(); 5167 const char* type_cstr = type_at.IsNull() ? "null" : type_at.ToCString();
5168 char* chars = OS::SCreate(zone, "%s [%s]", prev_cstr, type_cstr); 5168 char* chars = OS::SCreate(zone, "%s [%s]", prev_cstr, type_cstr);
5169 prev_cstr = chars; 5169 prev_cstr = chars;
5170 } 5170 }
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
5551 // argument vector. Therefore, we only need to set the type arguments 5551 // argument vector. Therefore, we only need to set the type arguments
5552 // matching the type parameters here. 5552 // matching the type parameters here.
5553 // In case of a function type alias, the function owner is the alias class, 5553 // In case of a function type alias, the function owner is the alias class,
5554 // i.e. the typedef. The signature type is therefore parameterized according 5554 // i.e. the typedef. The signature type is therefore parameterized according
5555 // to the alias class declaration, even if the function type is not generic. 5555 // to the alias class declaration, even if the function type is not generic.
5556 // Otherwise, if the function is static or if its signature type is 5556 // Otherwise, if the function is static or if its signature type is
5557 // non-generic, i.e. it does not depend on any type parameter of the owner 5557 // non-generic, i.e. it does not depend on any type parameter of the owner
5558 // class, then the signature type is not parameterized, although the owner 5558 // class, then the signature type is not parameterized, although the owner
5559 // class may be. In this case, the scope class of the function type is reset 5559 // class may be. In this case, the scope class of the function type is reset
5560 // to _Closure class as well as the owner of the signature function. 5560 // to _Closure class as well as the owner of the signature function.
5561 // With the introduction of generic functions, we may reach here before the
5562 // function type parameters have been resolved. Therefore, we cannot yet
5563 // check whether the function type has an instantiated signature.
5564 // We will do it later when resolving the type.
5561 Class& scope_class = Class::Handle(Owner()); 5565 Class& scope_class = Class::Handle(Owner());
5562 if (!scope_class.IsTypedefClass() && 5566 if (!scope_class.IsTypedefClass() &&
5563 (is_static() || !scope_class.IsGeneric() || 5567 (is_static() || !scope_class.IsGeneric())) {
5564 HasInstantiatedSignature())) {
5565 scope_class = Isolate::Current()->object_store()->closure_class(); 5568 scope_class = Isolate::Current()->object_store()->closure_class();
5566 if (IsSignatureFunction()) { 5569 if (IsSignatureFunction()) {
5567 set_owner(scope_class); 5570 set_owner(scope_class);
5568 set_token_pos(TokenPosition::kNoSource); 5571 set_token_pos(TokenPosition::kNoSource);
5569 } 5572 }
5570 } 5573 }
5574 // TODO(regis): With generic functions, this type is not only parameterized
5575 // with the type parameters of the scope class, but also with those of all
5576 // enclosing generic functions, which may not even have been parsed at this
5577 // point. What actually matters is that a signature type can be expressed in
5578 // a right-hand side type test by name. This is only possible with a typedef
5579 // and the free variables are only the type parameters of the typedef.
5571 const TypeArguments& signature_type_arguments = 5580 const TypeArguments& signature_type_arguments =
5572 TypeArguments::Handle(scope_class.type_parameters()); 5581 TypeArguments::Handle(scope_class.type_parameters());
5573 // Return the still unfinalized signature type. 5582 // Return the still unfinalized signature type.
5574 type = Type::New(scope_class, signature_type_arguments, token_pos()); 5583 type = Type::New(scope_class, signature_type_arguments, token_pos());
5575 type.set_signature(*this); 5584 type.set_signature(*this);
5576 SetSignatureType(type); 5585 SetSignatureType(type);
5577 } 5586 }
5578 return type.raw(); 5587 return type.raw();
5579 } 5588 }
5580 5589
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
5804 const Array& pair = Array::Handle(Array::New(2, Heap::kOld)); 5813 const Array& pair = Array::Handle(Array::New(2, Heap::kOld));
5805 pair.SetAt(0, value); 5814 pair.SetAt(0, value);
5806 // pair[1] will be the implicit closure function if needed. 5815 // pair[1] will be the implicit closure function if needed.
5807 set_data(pair); 5816 set_data(pair);
5808 } 5817 }
5809 5818
5810 5819
5811 void Function::set_result_type(const AbstractType& value) const { 5820 void Function::set_result_type(const AbstractType& value) const {
5812 ASSERT(!value.IsNull()); 5821 ASSERT(!value.IsNull());
5813 StorePointer(&raw_ptr()->result_type_, value.raw()); 5822 StorePointer(&raw_ptr()->result_type_, value.raw());
5823 if (value.IsFunctionType() && !value.IsResolved()) {
5824 // The unresolved function result type may refer to this
5825 // function's type parameters. Change its parent function.
5826 const Function& result_signature_function =
5827 Function::Handle(Type::Cast(value).signature());
5828 result_signature_function.set_parent_function(*this);
5829 }
5814 } 5830 }
5815 5831
5816 5832
5817 RawAbstractType* Function::ParameterTypeAt(intptr_t index) const { 5833 RawAbstractType* Function::ParameterTypeAt(intptr_t index) const {
5818 const Array& parameter_types = Array::Handle(raw_ptr()->parameter_types_); 5834 const Array& parameter_types = Array::Handle(raw_ptr()->parameter_types_);
5819 return AbstractType::RawCast(parameter_types.At(index)); 5835 return AbstractType::RawCast(parameter_types.At(index));
5820 } 5836 }
5821 5837
5822 5838
5823 void Function::SetParameterTypeAt(intptr_t index, 5839 void Function::SetParameterTypeAt(intptr_t index,
(...skipping 1395 matching lines...) Expand 10 before | Expand all | Expand 10 after
7219 " expecting 0x%08x found 0x%08x\n", 7235 " expecting 0x%08x found 0x%08x\n",
7220 ToFullyQualifiedCString(), fp, SourceFingerprint()); 7236 ToFullyQualifiedCString(), fp, SourceFingerprint());
7221 return false; 7237 return false;
7222 } 7238 }
7223 } 7239 }
7224 return true; 7240 return true;
7225 } 7241 }
7226 7242
7227 7243
7228 const char* Function::ToCString() const { 7244 const char* Function::ToCString() const {
7245 if (IsNull()) {
7246 return "Function: null";
7247 }
7229 const char* static_str = is_static() ? " static" : ""; 7248 const char* static_str = is_static() ? " static" : "";
7230 const char* abstract_str = is_abstract() ? " abstract" : ""; 7249 const char* abstract_str = is_abstract() ? " abstract" : "";
7231 const char* kind_str = NULL; 7250 const char* kind_str = NULL;
7232 const char* const_str = is_const() ? " const" : ""; 7251 const char* const_str = is_const() ? " const" : "";
7233 switch (kind()) { 7252 switch (kind()) {
7234 case RawFunction::kRegularFunction: 7253 case RawFunction::kRegularFunction:
7235 case RawFunction::kClosureFunction: 7254 case RawFunction::kClosureFunction:
7236 case RawFunction::kGetterFunction: 7255 case RawFunction::kGetterFunction:
7237 case RawFunction::kSetterFunction: 7256 case RawFunction::kSetterFunction:
7238 kind_str = ""; 7257 kind_str = "";
(...skipping 5145 matching lines...) Expand 10 before | Expand all | Expand 10 after
12384 " begin=%-3d end=%-3d name=%s\n", 12403 " begin=%-3d end=%-3d name=%s\n",
12385 i, LocalVarDescriptors::KindToCString(kind), info.scope_id, index, 12404 i, LocalVarDescriptors::KindToCString(kind), info.scope_id, index,
12386 static_cast<int>(info.begin_pos.Pos()), 12405 static_cast<int>(info.begin_pos.Pos()),
12387 static_cast<int>(info.end_pos.Pos()), var_name.ToCString()); 12406 static_cast<int>(info.end_pos.Pos()), var_name.ToCString());
12388 } 12407 }
12389 } 12408 }
12390 12409
12391 12410
12392 const char* LocalVarDescriptors::ToCString() const { 12411 const char* LocalVarDescriptors::ToCString() const {
12393 if (IsNull()) { 12412 if (IsNull()) {
12394 return "LocalVarDescriptors(NULL)"; 12413 return "LocalVarDescriptors: null";
12395 } 12414 }
12396 if (Length() == 0) { 12415 if (Length() == 0) {
12397 return "empty LocalVarDescriptors"; 12416 return "empty LocalVarDescriptors";
12398 } 12417 }
12399 intptr_t len = 1; // Trailing '\0'. 12418 intptr_t len = 1; // Trailing '\0'.
12400 String& var_name = String::Handle(); 12419 String& var_name = String::Handle();
12401 for (intptr_t i = 0; i < Length(); i++) { 12420 for (intptr_t i = 0; i < Length(); i++) {
12402 RawLocalVarDescriptors::VarInfo info; 12421 RawLocalVarDescriptors::VarInfo info;
12403 var_name = GetName(i); 12422 var_name = GetName(i);
12404 GetInfo(i, &info); 12423 GetInfo(i, &info);
(...skipping 2394 matching lines...) Expand 10 before | Expand all | Expand 10 after
14799 NoSafepointScope no_safepoint; 14818 NoSafepointScope no_safepoint;
14800 result ^= raw; 14819 result ^= raw;
14801 result.set_num_variables(num_variables); 14820 result.set_num_variables(num_variables);
14802 } 14821 }
14803 return result.raw(); 14822 return result.raw();
14804 } 14823 }
14805 14824
14806 14825
14807 const char* Context::ToCString() const { 14826 const char* Context::ToCString() const {
14808 if (IsNull()) { 14827 if (IsNull()) {
14809 return "Context (Null)"; 14828 return "Context: null";
14810 } 14829 }
14811 Zone* zone = Thread::Current()->zone(); 14830 Zone* zone = Thread::Current()->zone();
14812 const Context& parent_ctx = Context::Handle(parent()); 14831 const Context& parent_ctx = Context::Handle(parent());
14813 if (parent_ctx.IsNull()) { 14832 if (parent_ctx.IsNull()) {
14814 return zone->PrintToString("Context num_variables: %" Pd "", 14833 return zone->PrintToString("Context num_variables: %" Pd "",
14815 num_variables()); 14834 num_variables());
14816 } else { 14835 } else {
14817 const char* parent_str = parent_ctx.ToCString(); 14836 const char* parent_str = parent_ctx.ToCString();
14818 return zone->PrintToString("Context num_variables: %" Pd " parent:{ %s }", 14837 return zone->PrintToString("Context num_variables: %" Pd " parent:{ %s }",
14819 num_variables(), parent_str); 14838 num_variables(), parent_str);
(...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after
15212 *test_result ^= data.At(data_pos + kTestResult); 15231 *test_result ^= data.At(data_pos + kTestResult);
15213 } 15232 }
15214 15233
15215 15234
15216 const char* SubtypeTestCache::ToCString() const { 15235 const char* SubtypeTestCache::ToCString() const {
15217 return "SubtypeTestCache"; 15236 return "SubtypeTestCache";
15218 } 15237 }
15219 15238
15220 15239
15221 const char* Error::ToErrorCString() const { 15240 const char* Error::ToErrorCString() const {
15241 if (IsNull()) {
15242 return "Error: null";
15243 }
15222 UNREACHABLE(); 15244 UNREACHABLE();
15223 return "Internal Error"; 15245 return "Error";
15224 } 15246 }
15225 15247
15226 15248
15227 const char* Error::ToCString() const { 15249 const char* Error::ToCString() const {
15250 if (IsNull()) {
15251 return "Error: null";
15252 }
15228 // Error is an abstract class. We should never reach here. 15253 // Error is an abstract class. We should never reach here.
15229 UNREACHABLE(); 15254 UNREACHABLE();
15230 return "Error"; 15255 return "Error";
15231 } 15256 }
15232 15257
15233 15258
15234 RawApiError* ApiError::New() { 15259 RawApiError* ApiError::New() {
15235 ASSERT(Object::api_error_class() != Class::null()); 15260 ASSERT(Object::api_error_class() != Class::null());
15236 RawObject* raw = Object::Allocate(ApiError::kClassId, 15261 RawObject* raw = Object::Allocate(ApiError::kClassId,
15237 ApiError::InstanceSize(), Heap::kOld); 15262 ApiError::InstanceSize(), Heap::kOld);
(...skipping 1475 matching lines...) Expand 10 before | Expand all | Expand 10 after
16713 16738
16714 16739
16715 intptr_t AbstractType::Hash() const { 16740 intptr_t AbstractType::Hash() const {
16716 // AbstractType is an abstract class. 16741 // AbstractType is an abstract class.
16717 UNREACHABLE(); 16742 UNREACHABLE();
16718 return 0; 16743 return 0;
16719 } 16744 }
16720 16745
16721 16746
16722 const char* AbstractType::ToCString() const { 16747 const char* AbstractType::ToCString() const {
16748 if (IsNull()) {
16749 return "AbstractType: null";
16750 }
16723 // AbstractType is an abstract class. 16751 // AbstractType is an abstract class.
16724 UNREACHABLE(); 16752 UNREACHABLE();
16725 return "AbstractType"; 16753 return "AbstractType";
16726 } 16754 }
16727 16755
16728 16756
16729 RawType* Type::NullType() { 16757 RawType* Type::NullType() {
16730 return Isolate::Current()->object_store()->null_type(); 16758 return Isolate::Current()->object_store()->null_type();
16731 } 16759 }
16732 16760
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
16972 intptr_t num_type_args = args.Length(); 17000 intptr_t num_type_args = args.Length();
16973 intptr_t len = num_type_args; // Check the full vector of type args. 17001 intptr_t len = num_type_args; // Check the full vector of type args.
16974 ASSERT(num_type_args > 0); 17002 ASSERT(num_type_args > 0);
16975 // This type is not instantiated if it refers to type parameters. 17003 // This type is not instantiated if it refers to type parameters.
16976 // Although this type may still be unresolved, the type parameters it may 17004 // Although this type may still be unresolved, the type parameters it may
16977 // refer to are resolved by definition. We can therefore return the correct 17005 // refer to are resolved by definition. We can therefore return the correct
16978 // result even for an unresolved type. We just need to look at all type 17006 // result even for an unresolved type. We just need to look at all type
16979 // arguments and not just at the type parameters. 17007 // arguments and not just at the type parameters.
16980 if (HasResolvedTypeClass()) { 17008 if (HasResolvedTypeClass()) {
16981 const Class& cls = Class::Handle(type_class()); 17009 const Class& cls = Class::Handle(type_class());
16982 len = cls.NumTypeArguments();
16983 ASSERT(num_type_args >= len); // The vector may be longer than necessary.
16984 num_type_args = len;
16985 len = cls.NumTypeParameters(); // Check the type parameters only. 17010 len = cls.NumTypeParameters(); // Check the type parameters only.
16986 } 17011 }
16987 return (len == 0) || args.IsSubvectorInstantiated(num_type_args - len, len); 17012 return (len == 0) || args.IsSubvectorInstantiated(num_type_args - len, len);
16988 } 17013 }
16989 17014
16990 17015
16991 RawAbstractType* Type::InstantiateFrom( 17016 RawAbstractType* Type::InstantiateFrom(
16992 const TypeArguments& instantiator_type_arguments, 17017 const TypeArguments& instantiator_type_arguments,
16993 Error* bound_error, 17018 Error* bound_error,
16994 TrailPtr instantiation_trail, 17019 TrailPtr instantiation_trail,
(...skipping 628 matching lines...) Expand 10 before | Expand all | Expand 10 after
17623 17648
17624 17649
17625 void Type::set_type_state(int8_t state) const { 17650 void Type::set_type_state(int8_t state) const {
17626 ASSERT((state >= RawType::kAllocated) && 17651 ASSERT((state >= RawType::kAllocated) &&
17627 (state <= RawType::kFinalizedUninstantiated)); 17652 (state <= RawType::kFinalizedUninstantiated));
17628 StoreNonPointer(&raw_ptr()->type_state_, state); 17653 StoreNonPointer(&raw_ptr()->type_state_, state);
17629 } 17654 }
17630 17655
17631 17656
17632 const char* Type::ToCString() const { 17657 const char* Type::ToCString() const {
17658 if (IsNull()) {
17659 return "Type: null";
17660 }
17633 Zone* zone = Thread::Current()->zone(); 17661 Zone* zone = Thread::Current()->zone();
17634 const char* unresolved = IsResolved() ? "" : "Unresolved "; 17662 const char* unresolved = IsResolved() ? "" : "Unresolved ";
17635 const TypeArguments& type_args = TypeArguments::Handle(zone, arguments()); 17663 const TypeArguments& type_args = TypeArguments::Handle(zone, arguments());
17636 const char* args_cstr = type_args.IsNull() ? "null" : type_args.ToCString(); 17664 const char* args_cstr = type_args.IsNull() ? "null" : type_args.ToCString();
17637 Class& cls = Class::Handle(zone); 17665 Class& cls = Class::Handle(zone);
17638 const char* class_name; 17666 const char* class_name;
17639 if (HasResolvedTypeClass()) { 17667 if (HasResolvedTypeClass()) {
17640 cls = type_class(); 17668 cls = type_class();
17641 class_name = String::Handle(zone, cls.Name()).ToCString(); 17669 class_name = String::Handle(zone, cls.Name()).ToCString();
17642 } else { 17670 } else {
(...skipping 4266 matching lines...) Expand 10 before | Expand all | Expand 10 after
21909 result ^= raw; 21937 result ^= raw;
21910 result.SetLength(0); 21938 result.SetLength(0);
21911 result.SetData(array); 21939 result.SetData(array);
21912 } 21940 }
21913 return result.raw(); 21941 return result.raw();
21914 } 21942 }
21915 21943
21916 21944
21917 const char* GrowableObjectArray::ToCString() const { 21945 const char* GrowableObjectArray::ToCString() const {
21918 if (IsNull()) { 21946 if (IsNull()) {
21919 return "_GrowableList NULL"; 21947 return "_GrowableList: null";
21920 } 21948 }
21921 return OS::SCreate(Thread::Current()->zone(), 21949 return OS::SCreate(Thread::Current()->zone(),
21922 "Instance(length:%" Pd ") of '_GrowableList'", Length()); 21950 "Instance(length:%" Pd ") of '_GrowableList'", Length());
21923 } 21951 }
21924 21952
21925 21953
21926 // Equivalent to Dart's operator "==" and hashCode. 21954 // Equivalent to Dart's operator "==" and hashCode.
21927 class DefaultHashTraits { 21955 class DefaultHashTraits {
21928 public: 21956 public:
21929 static const char* Name() { return "DefaultHashTraits"; } 21957 static const char* Name() { return "DefaultHashTraits"; }
(...skipping 1087 matching lines...) Expand 10 before | Expand all | Expand 10 after
23017 return UserTag::null(); 23045 return UserTag::null();
23018 } 23046 }
23019 23047
23020 23048
23021 const char* UserTag::ToCString() const { 23049 const char* UserTag::ToCString() const {
23022 const String& tag_label = String::Handle(label()); 23050 const String& tag_label = String::Handle(label());
23023 return tag_label.ToCString(); 23051 return tag_label.ToCString();
23024 } 23052 }
23025 23053
23026 } // namespace dart 23054 } // namespace dart
OLDNEW
« no previous file with comments | « runtime/vm/class_finalizer.cc ('k') | runtime/vm/parser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698