typeterm.go raw
1 // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
2 // Source: ../../cmd/compile/internal/types2/typeterm.go
3
4 // Copyright 2021 The Go Authors. All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
7
8 package types
9
10 // A term describes elementary type sets:
11 //
12 // β
: (*term)(nil) == β
// set of no types (empty set)
13 // π€: &term{} == π€ // set of all types (π€niverse)
14 // T: &term{false, T} == {T} // set of type T
15 // ~t: &term{true, t} == {t' | under(t') == t} // set of types with underlying type t
16 type term struct {
17 tilde bool // valid if typ != nil
18 typ Type
19 }
20
21 func (x *term) String() string {
22 switch {
23 case x == nil:
24 return "β
"
25 case x.typ == nil:
26 return "π€"
27 case x.tilde:
28 return "~" + x.typ.String()
29 default:
30 return x.typ.String()
31 }
32 }
33
34 // equal reports whether x and y represent the same type set.
35 func (x *term) equal(y *term) bool {
36 // easy cases
37 switch {
38 case x == nil || y == nil:
39 return x == y
40 case x.typ == nil || y.typ == nil:
41 return x.typ == y.typ
42 }
43 // β
β x, y β π€
44
45 return x.tilde == y.tilde && Identical(x.typ, y.typ)
46 }
47
48 // union returns the union x βͺ y: zero, one, or two non-nil terms.
49 func (x *term) union(y *term) (_, _ *term) {
50 // easy cases
51 switch {
52 case x == nil && y == nil:
53 return nil, nil // β
βͺ β
== β
54 case x == nil:
55 return y, nil // β
βͺ y == y
56 case y == nil:
57 return x, nil // x βͺ β
== x
58 case x.typ == nil:
59 return x, nil // π€ βͺ y == π€
60 case y.typ == nil:
61 return y, nil // x βͺ π€ == π€
62 }
63 // β
β x, y β π€
64
65 if x.disjoint(y) {
66 return x, y // x βͺ y == (x, y) if x β© y == β
67 }
68 // x.typ == y.typ
69
70 // ~t βͺ ~t == ~t
71 // ~t βͺ T == ~t
72 // T βͺ ~t == ~t
73 // T βͺ T == T
74 if x.tilde || !y.tilde {
75 return x, nil
76 }
77 return y, nil
78 }
79
80 // intersect returns the intersection x β© y.
81 func (x *term) intersect(y *term) *term {
82 // easy cases
83 switch {
84 case x == nil || y == nil:
85 return nil // β
β© y == β
and β© β
== β
86 case x.typ == nil:
87 return y // π€ β© y == y
88 case y.typ == nil:
89 return x // x β© π€ == x
90 }
91 // β
β x, y β π€
92
93 if x.disjoint(y) {
94 return nil // x β© y == β
if x β© y == β
95 }
96 // x.typ == y.typ
97
98 // ~t β© ~t == ~t
99 // ~t β© T == T
100 // T β© ~t == T
101 // T β© T == T
102 if !x.tilde || y.tilde {
103 return x
104 }
105 return y
106 }
107
108 // includes reports whether t β x.
109 func (x *term) includes(t Type) bool {
110 // easy cases
111 switch {
112 case x == nil:
113 return false // t β β
== false
114 case x.typ == nil:
115 return true // t β π€ == true
116 }
117 // β
β x β π€
118
119 u := t
120 if x.tilde {
121 u = under(u)
122 }
123 if Identical(x.typ, u) {
124 return true
125 }
126 // Moxie: []byte satisfies ~string (string=[]byte unification).
127 if isString(x.typ) && isByteSlice(t) {
128 return true
129 }
130 if isByteSlice(x.typ) && isString(t) {
131 return true
132 }
133 return false
134 }
135
136 // subsetOf reports whether x β y.
137 func (x *term) subsetOf(y *term) bool {
138 // easy cases
139 switch {
140 case x == nil:
141 return true // β
β y == true
142 case y == nil:
143 return false // x β β
== false since x != β
144 case y.typ == nil:
145 return true // x β π€ == true
146 case x.typ == nil:
147 return false // π€ β y == false since y != π€
148 }
149 // β
β x, y β π€
150
151 if x.disjoint(y) {
152 return false // x β y == false if x β© y == β
153 }
154 // x.typ == y.typ
155
156 // ~t β ~t == true
157 // ~t β T == false
158 // T β ~t == true
159 // T β T == true
160 return !x.tilde || y.tilde
161 }
162
163 // disjoint reports whether x β© y == β
.
164 // x.typ and y.typ must not be nil.
165 func (x *term) disjoint(y *term) bool {
166 if debug && (x.typ == nil || y.typ == nil) {
167 panic("invalid argument(s)")
168 }
169 ux := x.typ
170 if y.tilde {
171 ux = under(ux)
172 }
173 uy := y.typ
174 if x.tilde {
175 uy = under(uy)
176 }
177 return !Identical(ux, uy)
178 }
179