Debian Codegraph @

typeparams/termlist.go

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