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