github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/go/types/predicates.go (about) 1 // Copyright 2012 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 // This file implements commonly used type predicates. 6 7 package types 8 9 import "sort" 10 11 func isNamed(typ Type) bool { 12 if _, ok := typ.(*Basic); ok { 13 return ok 14 } 15 _, ok := typ.(*Named) 16 return ok 17 } 18 19 func isBoolean(typ Type) bool { 20 t, ok := typ.Underlying().(*Basic) 21 return ok && t.info&IsBoolean != 0 22 } 23 24 func isInteger(typ Type) bool { 25 t, ok := typ.Underlying().(*Basic) 26 return ok && t.info&IsInteger != 0 27 } 28 29 func isUnsigned(typ Type) bool { 30 t, ok := typ.Underlying().(*Basic) 31 return ok && t.info&IsUnsigned != 0 32 } 33 34 func isFloat(typ Type) bool { 35 t, ok := typ.Underlying().(*Basic) 36 return ok && t.info&IsFloat != 0 37 } 38 39 func isComplex(typ Type) bool { 40 t, ok := typ.Underlying().(*Basic) 41 return ok && t.info&IsComplex != 0 42 } 43 44 func isNumeric(typ Type) bool { 45 t, ok := typ.Underlying().(*Basic) 46 return ok && t.info&IsNumeric != 0 47 } 48 49 func isString(typ Type) bool { 50 t, ok := typ.Underlying().(*Basic) 51 return ok && t.info&IsString != 0 52 } 53 54 func isTyped(typ Type) bool { 55 t, ok := typ.Underlying().(*Basic) 56 return !ok || t.info&IsUntyped == 0 57 } 58 59 func isUntyped(typ Type) bool { 60 t, ok := typ.Underlying().(*Basic) 61 return ok && t.info&IsUntyped != 0 62 } 63 64 func isOrdered(typ Type) bool { 65 t, ok := typ.Underlying().(*Basic) 66 return ok && t.info&IsOrdered != 0 67 } 68 69 func isConstType(typ Type) bool { 70 t, ok := typ.Underlying().(*Basic) 71 return ok && t.info&IsConstType != 0 72 } 73 74 // IsInterface reports whether typ is an interface type. 75 func IsInterface(typ Type) bool { 76 _, ok := typ.Underlying().(*Interface) 77 return ok 78 } 79 80 // Comparable reports whether values of type T are comparable. 81 func Comparable(T Type) bool { 82 switch t := T.Underlying().(type) { 83 case *Basic: 84 // assume invalid types to be comparable 85 // to avoid follow-up errors 86 return t.kind != UntypedNil 87 case *Pointer, *Interface, *Chan: 88 return true 89 case *Struct: 90 for _, f := range t.fields { 91 if !Comparable(f.typ) { 92 return false 93 } 94 } 95 return true 96 case *Array: 97 return Comparable(t.elem) 98 } 99 return false 100 } 101 102 // hasNil reports whether a type includes the nil value. 103 func hasNil(typ Type) bool { 104 switch t := typ.Underlying().(type) { 105 case *Basic: 106 return t.kind == UnsafePointer 107 case *Slice, *Pointer, *Signature, *Interface, *Map, *Chan: 108 return true 109 } 110 return false 111 } 112 113 // Identical reports whether x and y are identical types. 114 // Receivers of Signature types are ignored. 115 func Identical(x, y Type) bool { 116 return identical(x, y, true, nil) 117 } 118 119 // IdenticalIgnoreTags reports whether x and y are identical types if tags are ignored. 120 // Receivers of Signature types are ignored. 121 func IdenticalIgnoreTags(x, y Type) bool { 122 return identical(x, y, false, nil) 123 } 124 125 // An ifacePair is a node in a stack of interface type pairs compared for identity. 126 type ifacePair struct { 127 x, y *Interface 128 prev *ifacePair 129 } 130 131 func (p *ifacePair) identical(q *ifacePair) bool { 132 return p.x == q.x && p.y == q.y || p.x == q.y && p.y == q.x 133 } 134 135 func identical(x, y Type, cmpTags bool, p *ifacePair) bool { 136 if x == y { 137 return true 138 } 139 140 switch x := x.(type) { 141 case *Basic: 142 // Basic types are singletons except for the rune and byte 143 // aliases, thus we cannot solely rely on the x == y check 144 // above. See also comment in TypeName.IsAlias. 145 if y, ok := y.(*Basic); ok { 146 return x.kind == y.kind 147 } 148 149 case *Array: 150 // Two array types are identical if they have identical element types 151 // and the same array length. 152 if y, ok := y.(*Array); ok { 153 return x.len == y.len && identical(x.elem, y.elem, cmpTags, p) 154 } 155 156 case *Slice: 157 // Two slice types are identical if they have identical element types. 158 if y, ok := y.(*Slice); ok { 159 return identical(x.elem, y.elem, cmpTags, p) 160 } 161 162 case *Struct: 163 // Two struct types are identical if they have the same sequence of fields, 164 // and if corresponding fields have the same names, and identical types, 165 // and identical tags. Two anonymous fields are considered to have the same 166 // name. Lower-case field names from different packages are always different. 167 if y, ok := y.(*Struct); ok { 168 if x.NumFields() == y.NumFields() { 169 for i, f := range x.fields { 170 g := y.fields[i] 171 if f.anonymous != g.anonymous || 172 cmpTags && x.Tag(i) != y.Tag(i) || 173 !f.sameId(g.pkg, g.name) || 174 !identical(f.typ, g.typ, cmpTags, p) { 175 return false 176 } 177 } 178 return true 179 } 180 } 181 182 case *Pointer: 183 // Two pointer types are identical if they have identical base types. 184 if y, ok := y.(*Pointer); ok { 185 return identical(x.base, y.base, cmpTags, p) 186 } 187 188 case *Tuple: 189 // Two tuples types are identical if they have the same number of elements 190 // and corresponding elements have identical types. 191 if y, ok := y.(*Tuple); ok { 192 if x.Len() == y.Len() { 193 if x != nil { 194 for i, v := range x.vars { 195 w := y.vars[i] 196 if !identical(v.typ, w.typ, cmpTags, p) { 197 return false 198 } 199 } 200 } 201 return true 202 } 203 } 204 205 case *Signature: 206 // Two function types are identical if they have the same number of parameters 207 // and result values, corresponding parameter and result types are identical, 208 // and either both functions are variadic or neither is. Parameter and result 209 // names are not required to match. 210 if y, ok := y.(*Signature); ok { 211 return x.variadic == y.variadic && 212 identical(x.params, y.params, cmpTags, p) && 213 identical(x.results, y.results, cmpTags, p) 214 } 215 216 case *Interface: 217 // Two interface types are identical if they have the same set of methods with 218 // the same names and identical function types. Lower-case method names from 219 // different packages are always different. The order of the methods is irrelevant. 220 if y, ok := y.(*Interface); ok { 221 a := x.allMethods 222 b := y.allMethods 223 if len(a) == len(b) { 224 // Interface types are the only types where cycles can occur 225 // that are not "terminated" via named types; and such cycles 226 // can only be created via method parameter types that are 227 // anonymous interfaces (directly or indirectly) embedding 228 // the current interface. Example: 229 // 230 // type T interface { 231 // m() interface{T} 232 // } 233 // 234 // If two such (differently named) interfaces are compared, 235 // endless recursion occurs if the cycle is not detected. 236 // 237 // If x and y were compared before, they must be equal 238 // (if they were not, the recursion would have stopped); 239 // search the ifacePair stack for the same pair. 240 // 241 // This is a quadratic algorithm, but in practice these stacks 242 // are extremely short (bounded by the nesting depth of interface 243 // type declarations that recur via parameter types, an extremely 244 // rare occurrence). An alternative implementation might use a 245 // "visited" map, but that is probably less efficient overall. 246 q := &ifacePair{x, y, p} 247 for p != nil { 248 if p.identical(q) { 249 return true // same pair was compared before 250 } 251 p = p.prev 252 } 253 if debug { 254 assert(sort.IsSorted(byUniqueMethodName(a))) 255 assert(sort.IsSorted(byUniqueMethodName(b))) 256 } 257 for i, f := range a { 258 g := b[i] 259 if f.Id() != g.Id() || !identical(f.typ, g.typ, cmpTags, q) { 260 return false 261 } 262 } 263 return true 264 } 265 } 266 267 case *Map: 268 // Two map types are identical if they have identical key and value types. 269 if y, ok := y.(*Map); ok { 270 return identical(x.key, y.key, cmpTags, p) && identical(x.elem, y.elem, cmpTags, p) 271 } 272 273 case *Chan: 274 // Two channel types are identical if they have identical value types 275 // and the same direction. 276 if y, ok := y.(*Chan); ok { 277 return x.dir == y.dir && identical(x.elem, y.elem, cmpTags, p) 278 } 279 280 case *Named: 281 // Two named types are identical if their type names originate 282 // in the same type declaration. 283 if y, ok := y.(*Named); ok { 284 return x.obj == y.obj 285 } 286 287 case nil: 288 289 default: 290 unreachable() 291 } 292 293 return false 294 } 295 296 // Default returns the default "typed" type for an "untyped" type; 297 // it returns the incoming type for all other types. The default type 298 // for untyped nil is untyped nil. 299 // 300 func Default(typ Type) Type { 301 if t, ok := typ.(*Basic); ok { 302 switch t.kind { 303 case UntypedBool: 304 return Typ[Bool] 305 case UntypedInt: 306 return Typ[Int] 307 case UntypedRune: 308 return universeRune // use 'rune' name 309 case UntypedFloat: 310 return Typ[Float64] 311 case UntypedComplex: 312 return Typ[Complex128] 313 case UntypedString: 314 return Typ[String] 315 } 316 } 317 return typ 318 }