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