|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 | + |
| 5 | +part of masks; |
| 6 | + |
| 7 | +/// A [SetTypeMask] is a [TypeMask] for a specific allocation site of a set |
| 8 | +/// (currently only the internal Set class) that will get specialized once the |
| 9 | +/// [TypeGraphInferrer] phase finds an element type for it. |
| 10 | +class SetTypeMask extends AllocationTypeMask { |
| 11 | + /// Tag used for identifying serialized [SetTypeMask] objects in a debugging |
| 12 | + /// data stream. |
| 13 | + static const String tag = 'set-type-mask'; |
| 14 | + |
| 15 | + final TypeMask forwardTo; |
| 16 | + |
| 17 | + // The [Node] where this type mask was created. |
| 18 | + final ir.TreeNode allocationNode; |
| 19 | + |
| 20 | + // The [Entity] where this type mask was created. |
| 21 | + final MemberEntity allocationElement; |
| 22 | + |
| 23 | + // The element type of this set. |
| 24 | + final TypeMask elementType; |
| 25 | + |
| 26 | + SetTypeMask(this.forwardTo, this.allocationNode, this.allocationElement, |
| 27 | + this.elementType); |
| 28 | + |
| 29 | + /// Deserializes a [SetTypeMask] object from [source]. |
| 30 | + factory SetTypeMask.readFromDataSource( |
| 31 | + DataSource source, JClosedWorld closedWorld) { |
| 32 | + source.begin(tag); |
| 33 | + TypeMask forwardTo = new TypeMask.readFromDataSource(source, closedWorld); |
| 34 | + ir.TreeNode allocationNode = source.readTreeNodeOrNull(); |
| 35 | + MemberEntity allocationElement = source.readMemberOrNull(); |
| 36 | + TypeMask elementType = new TypeMask.readFromDataSource(source, closedWorld); |
| 37 | + source.end(tag); |
| 38 | + return new SetTypeMask( |
| 39 | + forwardTo, allocationNode, allocationElement, elementType); |
| 40 | + } |
| 41 | + |
| 42 | + /// Serializes this [SetTypeMask] to [sink]. |
| 43 | + @override |
| 44 | + void writeToDataSink(DataSink sink) { |
| 45 | + sink.writeEnum(TypeMaskKind.set); |
| 46 | + sink.begin(tag); |
| 47 | + forwardTo.writeToDataSink(sink); |
| 48 | + sink.writeTreeNodeOrNull(allocationNode); |
| 49 | + sink.writeMemberOrNull(allocationElement); |
| 50 | + elementType.writeToDataSink(sink); |
| 51 | + sink.end(tag); |
| 52 | + } |
| 53 | + |
| 54 | + @override |
| 55 | + TypeMask nullable() => isNullable |
| 56 | + ? this |
| 57 | + : new SetTypeMask( |
| 58 | + forwardTo.nullable(), allocationNode, allocationElement, elementType); |
| 59 | + |
| 60 | + @override |
| 61 | + TypeMask nonNullable() => isNullable |
| 62 | + ? new SetTypeMask(forwardTo.nonNullable(), allocationNode, |
| 63 | + allocationElement, elementType) |
| 64 | + : this; |
| 65 | + |
| 66 | + @override |
| 67 | + bool get isSet => true; |
| 68 | + |
| 69 | + @override |
| 70 | + bool get isExact => true; |
| 71 | + |
| 72 | + @override |
| 73 | + bool equalsDisregardNull(other) { |
| 74 | + if (other is! SetTypeMask) return false; |
| 75 | + return super.equalsDisregardNull(other) && |
| 76 | + allocationNode == other.allocationNode && |
| 77 | + elementType == other.elementType; |
| 78 | + } |
| 79 | + |
| 80 | + @override |
| 81 | + TypeMask intersection(TypeMask other, JClosedWorld closedWorld) { |
| 82 | + TypeMask forwardIntersection = forwardTo.intersection(other, closedWorld); |
| 83 | + if (forwardIntersection.isEmptyOrNull) return forwardIntersection; |
| 84 | + return forwardIntersection.isNullable ? nullable() : nonNullable(); |
| 85 | + } |
| 86 | + |
| 87 | + @override |
| 88 | + TypeMask union(dynamic other, JClosedWorld closedWorld) { |
| 89 | + if (this == other) { |
| 90 | + return this; |
| 91 | + } else if (equalsDisregardNull(other)) { |
| 92 | + return other.isNullable ? other : this; |
| 93 | + } else if (other.isEmptyOrNull) { |
| 94 | + return other.isNullable ? this.nullable() : this; |
| 95 | + } else if (other.isSet && |
| 96 | + elementType != null && |
| 97 | + other.elementType != null) { |
| 98 | + TypeMask newElementType = |
| 99 | + elementType.union(other.elementType, closedWorld); |
| 100 | + TypeMask newForwardTo = forwardTo.union(other.forwardTo, closedWorld); |
| 101 | + return new SetTypeMask(newForwardTo, null, null, newElementType); |
| 102 | + } else { |
| 103 | + return forwardTo.union(other, closedWorld); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @override |
| 108 | + bool operator ==(other) => super == other; |
| 109 | + |
| 110 | + @override |
| 111 | + int get hashCode => |
| 112 | + computeHashCode(allocationNode, isNullable, elementType, forwardTo); |
| 113 | + |
| 114 | + @override |
| 115 | + String toString() => 'Set($forwardTo, element: $elementType)'; |
| 116 | +} |
0 commit comments