Skip to content

Commit

Permalink
Reland "[VM/runtime] Refactor the representation of type parameters i…
Browse files Browse the repository at this point in the history
…n the VM."

This is a reland of 8a21ab1

Original change's description:
> [VM/runtime] Refactor the representation of type parameters in the VM.
>
> This introduces a new VM internal class 'TypeParameters' representing the declaration of a list of type parameters, either in a class or function.
> The reference to (or use of) a type parameter is still represented by the existing 'TypeParameter' class.
>
> Fixes #43901
> Fixes #45763
>
> TEST=existing ones and a regression test
>
> Change-Id: I1fde808bf753cc1cb829f2c4383c1836651cee80
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/189942
> Commit-Queue: Régis Crelier <regis@google.com>
> Reviewed-by: Alexander Markov <alexmarkov@google.com>

This fixes #45911

TEST=existing ones and a regression test

Change-Id: I709d38b1df3d73fe3c9796d5aca3cbbdcf77fd38
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/198380
Commit-Queue: Régis Crelier <regis@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
  • Loading branch information
crelier authored and commit-bot@chromium.org committed May 5, 2021
1 parent df3bb63 commit 1f55b7c
Show file tree
Hide file tree
Showing 65 changed files with 1,743 additions and 1,521 deletions.
15 changes: 7 additions & 8 deletions runtime/lib/mirrors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,26 +191,25 @@ static InstancePtr CreateTypeVariableMirror(const TypeParameter& param,
const Instance& owner_mirror) {
const Array& args = Array::Handle(Array::New(3));
args.SetAt(0, param);
args.SetAt(1, String::Handle(param.name()));
args.SetAt(1, String::Handle(param.UserVisibleName()));
args.SetAt(2, owner_mirror);
return CreateMirror(Symbols::_TypeVariableMirror(), args);
}

// We create a list in native code and let Dart code create the type mirror
// object and the ordered map.
static InstancePtr CreateTypeVariableList(const Class& cls) {
const TypeArguments& args = TypeArguments::Handle(cls.type_parameters());
if (args.IsNull()) {
const intptr_t num_type_params = cls.NumTypeParameters();
if (num_type_params == 0) {
return Object::empty_array().ptr();
}
const Array& result = Array::Handle(Array::New(args.Length() * 2));
const Array& result = Array::Handle(Array::New(num_type_params * 2));
TypeParameter& type = TypeParameter::Handle();
String& name = String::Handle();
for (intptr_t i = 0; i < args.Length(); i++) {
type ^= args.TypeAt(i);
ASSERT(type.IsTypeParameter());
for (intptr_t i = 0; i < num_type_params; i++) {
type = cls.TypeParameterAt(i, Nullability::kLegacy);
ASSERT(type.IsFinalized());
name = type.name();
name = type.UserVisibleName();
result.SetAt(2 * i, name);
result.SetAt(2 * i + 1, type);
}
Expand Down
21 changes: 10 additions & 11 deletions runtime/lib/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -449,18 +449,19 @@ DEFINE_NATIVE_ENTRY(Internal_boundsCheckForPartialInstantiation, 0, 2) {
const Closure& closure =
Closure::CheckedHandle(zone, arguments->NativeArgAt(0));
const Function& target = Function::Handle(zone, closure.function());
const TypeArguments& bounds =
TypeArguments::Handle(zone, target.type_parameters());

// Either the bounds are all-dynamic or the function is not generic.
if (bounds.IsNull()) return Object::null();
const TypeParameters& type_params =
TypeParameters::Handle(zone, target.type_parameters());
if (type_params.IsNull() || type_params.AllDynamicBounds()) {
// The function is not generic or the bounds are all dynamic.
return Object::null();
}

const TypeArguments& type_args_to_check =
TypeArguments::CheckedHandle(zone, arguments->NativeArgAt(1));

// This should be guaranteed by the front-end.
ASSERT(type_args_to_check.IsNull() ||
bounds.Length() <= type_args_to_check.Length());
type_params.Length() <= type_args_to_check.Length());

// The bounds on the closure may need instantiation.
const TypeArguments& instantiator_type_args =
Expand All @@ -470,10 +471,8 @@ DEFINE_NATIVE_ENTRY(Internal_boundsCheckForPartialInstantiation, 0, 2) {

AbstractType& supertype = AbstractType::Handle(zone);
AbstractType& subtype = AbstractType::Handle(zone);
TypeParameter& parameter = TypeParameter::Handle(zone);
for (intptr_t i = 0; i < bounds.Length(); ++i) {
parameter ^= bounds.TypeAt(i);
supertype = parameter.bound();
for (intptr_t i = 0; i < type_params.Length(); ++i) {
supertype = type_params.BoundAt(i);
subtype = type_args_to_check.IsNull() ? Object::dynamic_type().ptr()
: type_args_to_check.TypeAt(i);

Expand All @@ -492,7 +491,7 @@ DEFINE_NATIVE_ENTRY(Internal_boundsCheckForPartialInstantiation, 0, 2) {
ASSERT(caller_frame != NULL);
location = caller_frame->GetTokenPos();
}
String& parameter_name = String::Handle(zone, parameter.Name());
const auto& parameter_name = String::Handle(zone, type_params.NameAt(i));
Exceptions::CreateAndThrowTypeError(location, subtype, supertype,
parameter_name);
UNREACHABLE();
Expand Down
5 changes: 0 additions & 5 deletions runtime/vm/canonical_tables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ bool MetadataMapTraits::IsMatch(const Object& a, const Object& b) {
const Object& owner_b = Object::Handle(Field::Cast(b).Owner());
return IsMatch(owner_a, owner_b);
} else if (a.IsTypeParameter() && b.IsTypeParameter()) {
const String& name_a = String::Handle(TypeParameter::Cast(a).name());
const String& name_b = String::Handle(TypeParameter::Cast(b).name());
if (!name_a.Equals(name_b)) {
return false;
}
if (TypeParameter::Cast(a).index() != TypeParameter::Cast(b).index() ||
TypeParameter::Cast(a).base() != TypeParameter::Cast(b).base()) {
return false;
Expand Down
Loading

0 comments on commit 1f55b7c

Please sign in to comment.