Debian Codegraph @

src/go/types/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 = u.Underlying()
122 }
123 return Identical(x.typ, u)
124 }
125
126 // subsetOf reports whether x βŠ† y.
127 func (x *term) subsetOf(y *term) bool {
128 // easy cases
129 switch {
130 case x == nil:
131 return true // βˆ… βŠ† y == true
132 case y == nil:
133 return false // x βŠ† βˆ… == false since x != βˆ…
134 case y.typ == nil:
135 return true // x βŠ† 𝓀 == true
136 case x.typ == nil:
137 return false // 𝓀 βŠ† y == false since y != 𝓀
138 }
139 // βˆ… βŠ‚ x, y βŠ‚ 𝓀
140
141 if x.disjoint(y) {
142 return false // x βŠ† y == false if x ∩ y == βˆ…
143 }
144 // x.typ == y.typ
145
146 // ~t βŠ† ~t == true
147 // ~t βŠ† T == false
148 // T βŠ† ~t == true
149 // T βŠ† T == true
150 return !x.tilde || y.tilde
151 }
152
153 // disjoint reports whether x ∩ y == βˆ….
154 // x.typ and y.typ must not be nil.
155 func (x *term) disjoint(y *term) bool {
156 if debug && (x.typ == nil || y.typ == nil) {
157 panic("invalid argument(s)")
158 }
159 ux := x.typ
160 if y.tilde {
161 ux = ux.Underlying()
162 }
163 uy := y.typ
164 if x.tilde {
165 uy = uy.Underlying()
166 }
167 return !Identical(ux, uy)
168 }