Debian Codegraph @

internal/typeparams/termlist.go

raw symbols SCIP
1 // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT.
2 // Source: ../../cmd/compile/internal/types2/termlist.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 // Code generated by copytermlist.go DO NOT EDIT.
9
10 package typeparams
11
12 import (
13 "go/types"
14 "strings"
15 )
16
17 // A termlist represents the type set represented by the union
18 // t1 ∪ y2 ∪ ... tn of the type sets of the terms t1 to tn.
19 // A termlist is in normal form if all terms are disjoint.
20 // termlist operations don't require the operands to be in
21 // normal form.
22 type termlist []*term
23
24 // allTermlist represents the set of all types.
25 // It is in normal form.
26 var allTermlist = termlist{new(term)}
27
28 // termSep is the separator used between individual terms.
29 const termSep = " | "
30
31 // String prints the termlist exactly (without normalization).
32 func (xl termlist) String() string {
33 if len(xl) == 0 {
34 return "∅"
35 }
36 var buf strings.Builder
37 for i, x := range xl {
38 if i > 0 {
39 buf.WriteString(termSep)
40 }
41 buf.WriteString(x.String())
42 }
43 return buf.String()
44 }
45
46 // isEmpty reports whether the termlist xl represents the empty set of types.
47 func (xl termlist) isEmpty() bool {
48 // If there's a non-nil term, the entire list is not empty.
49 // If the termlist is in normal form, this requires at most
50 // one iteration.
51 for _, x := range xl {
52 if x != nil {
53 return false
54 }
55 }
56 return true
57 }
58
59 // isAll reports whether the termlist xl represents the set of all types.
60 func (xl termlist) isAll() bool {
61 // If there's a 𝓤 term, the entire list is 𝓤.
62 // If the termlist is in normal form, this requires at most
63 // one iteration.
64 for _, x := range xl {
65 if x != nil && x.typ == nil {
66 return true
67 }
68 }
69 return false
70 }
71
72 // norm returns the normal form of xl.
73 func (xl termlist) norm() termlist {
74 // Quadratic algorithm, but good enough for now.
75 // TODO(gri) fix asymptotic performance
76 used := make([]bool, len(xl))
77 var rl termlist
78 for i, xi := range xl {
79 if xi == nil || used[i] {
80 continue
81 }
82 for j := i + 1; j < len(xl); j++ {
83 xj := xl[j]
84 if xj == nil || used[j] {
85 continue
86 }
87 if u1, u2 := xi.union(xj); u2 == nil {
88 // If we encounter a 𝓤 term, the entire list is 𝓤.
89 // Exit early.
90 // (Note that this is not just an optimization;
91 // if we continue, we may end up with a 𝓤 term
92 // and other terms and the result would not be
93 // in normal form.)
94 if u1.typ == nil {
95 return allTermlist
96 }
97 xi = u1
98 used[j] = true // xj is now unioned into xi - ignore it in future iterations
99 }
100 }
101 rl = append(rl, xi)
102 }
103 return rl
104 }
105
106 // union returns the union xl ∪ yl.
107 func (xl termlist) union(yl termlist) termlist {
108 return append(xl, yl...).norm()
109 }
110
111 // intersect returns the intersection xl ∩ yl.
112 func (xl termlist) intersect(yl termlist) termlist {
113 if xl.isEmpty() || yl.isEmpty() {
114 return nil
115 }
116
117 // Quadratic algorithm, but good enough for now.
118 // TODO(gri) fix asymptotic performance
119 var rl termlist
120 for _, x := range xl {
121 for _, y := range yl {
122 if r := x.intersect(y); r != nil {
123 rl = append(rl, r)
124 }
125 }
126 }
127 return rl.norm()
128 }
129
130 // equal reports whether xl and yl represent the same type set.
131 func (xl termlist) equal(yl termlist) bool {
132 // TODO(gri) this should be more efficient
133 return xl.subsetOf(yl) && yl.subsetOf(xl)
134 }
135
136 // includes reports whether t ∈ xl.
137 func (xl termlist) includes(t types.Type) bool {
138 for _, x := range xl {
139 if x.includes(t) {
140 return true
141 }
142 }
143 return false
144 }
145
146 // supersetOf reports whether y ⊆ xl.
147 func (xl termlist) supersetOf(y *term) bool {
148 for _, x := range xl {
149 if y.subsetOf(x) {
150 return true
151 }
152 }
153 return false
154 }
155
156 // subsetOf reports whether xl ⊆ yl.
157 func (xl termlist) subsetOf(yl termlist) bool {
158 if yl.isEmpty() {
159 return xl.isEmpty()
160 }
161
162 // each term x of xl must be a subset of yl
163 for _, x := range xl {
164 if !yl.supersetOf(x) {
165 return false // x is not a subset yl
166 }
167 }
168 return true
169 }