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.
5 // This file implements commonly used type predicates.
9 // The isX predicates below report whether t is an X.
10 // If t is a type parameter the result is false; i.e.,
11 // these predicates don't look inside a type parameter.
13 func isBoolean(t Type) bool { return isBasic(t, IsBoolean) }
14 func isInteger(t Type) bool { return isBasic(t, IsInteger) }
15 func isUnsigned(t Type) bool { return isBasic(t, IsUnsigned) }
16 func isFloat(t Type) bool { return isBasic(t, IsFloat) }
17 func isComplex(t Type) bool { return isBasic(t, IsComplex) }
18 func isNumeric(t Type) bool { return isBasic(t, IsNumeric) }
19 func isString(t Type) bool { return isBasic(t, IsString) }
20 func isIntegerOrFloat(t Type) bool { return isBasic(t, IsInteger|IsFloat) }
21 func isConstType(t Type) bool { return isBasic(t, IsConstType) }
23 // isBasic reports whether under(t) is a basic type with the specified info.
24 // If t is a type parameter the result is false; i.e.,
25 // isBasic does not look inside a type parameter.
26 func isBasic(t Type, info BasicInfo) bool {
27 u, _ := under(t).(*Basic)
28 return u != nil && u.info&info != 0
31 // The allX predicates below report whether t is an X.
32 // If t is a type parameter the result is true if isX is true
33 // for all specified types of the type parameter's type set.
34 // allX is an optimized version of isX(structure(t)) (which
35 // is the same as underIs(t, isX)).
37 func allBoolean(t Type) bool { return allBasic(t, IsBoolean) }
38 func allInteger(t Type) bool { return allBasic(t, IsInteger) }
39 func allUnsigned(t Type) bool { return allBasic(t, IsUnsigned) }
40 func allNumeric(t Type) bool { return allBasic(t, IsNumeric) }
41 func allString(t Type) bool { return allBasic(t, IsString) }
42 func allOrdered(t Type) bool { return allBasic(t, IsOrdered) }
43 func allNumericOrString(t Type) bool { return allBasic(t, IsNumeric|IsString) }
45 // allBasic reports whether under(t) is a basic type with the specified info.
46 // If t is a type parameter, the result is true if isBasic(t, info) is true
47 // for all specific types of the type parameter's type set.
48 // allBasic(t, info) is an optimized version of isBasic(structure(t), info).
49 func allBasic(t Type, info BasicInfo) bool {
50 switch u := under(t).(type) {
52 return u.info&info != 0
54 return u.is(func(t *term) bool { return t != nil && isBasic(t.typ, info) })
59 // hasName reports whether t has a name. This includes
60 // predeclared types, defined types, and type parameters.
61 // hasName may be called with types that are not fully set up.
62 func hasName(t Type) bool {
64 case *Basic, *Named, *TypeParam:
70 // isTyped reports whether t is typed; i.e., not an untyped
71 // constant or boolean. isTyped may be called with types that
72 // are not fully set up.
73 func isTyped(t Type) bool {
74 // isTyped is called with types that are not fully
75 // set up. Must not call under()!
77 return b == nil || b.info&IsUntyped == 0
80 // isUntyped(t) is the same as !isTyped(t).
81 func isUntyped(t Type) bool {
85 // IsInterface reports whether t is an interface type.
86 func IsInterface(t Type) bool {
87 _, ok := under(t).(*Interface)
91 // isTypeParam reports whether t is a type parameter.
92 func isTypeParam(t Type) bool {
93 _, ok := under(t).(*TypeParam)
97 // isGeneric reports whether a type is a generic, uninstantiated type
98 // (generic signatures are not included).
99 // TODO(gri) should we include signatures or assert that they are not present?
100 func isGeneric(t Type) bool {
101 // A parameterized type is only generic if it doesn't have an instantiation already.
102 named, _ := t.(*Named)
103 return named != nil && named.obj != nil && named.targs == nil && named.TypeParams() != nil
106 // Comparable reports whether values of type T are comparable.
107 func Comparable(T Type) bool {
108 return comparable(T, nil)
111 func comparable(T Type, seen map[Type]bool) bool {
116 seen = make(map[Type]bool)
120 switch t := under(T).(type) {
122 // assume invalid types to be comparable
123 // to avoid follow-up errors
124 return t.kind != UntypedNil
125 case *Pointer, *Interface, *Chan:
128 for _, f := range t.fields {
129 if !comparable(f.typ, seen) {
135 return comparable(t.elem, seen)
137 return t.iface().IsComparable()
142 // hasNil reports whether type t includes the nil value.
143 func hasNil(t Type) bool {
144 switch u := under(t).(type) {
146 return u.kind == UnsafePointer
147 case *Slice, *Pointer, *Signature, *Interface, *Map, *Chan:
150 return u.underIs(hasNil)
155 // An ifacePair is a node in a stack of interface type pairs compared for identity.
156 type ifacePair struct {
161 func (p *ifacePair) identical(q *ifacePair) bool {
162 return p.x == q.x && p.y == q.y || p.x == q.y && p.y == q.x
165 // For changes to this code the corresponding changes should be made to unifier.nify.
166 func identical(x, y Type, cmpTags bool, p *ifacePair) bool {
171 switch x := x.(type) {
173 // Basic types are singletons except for the rune and byte
174 // aliases, thus we cannot solely rely on the x == y check
175 // above. See also comment in TypeName.IsAlias.
176 if y, ok := y.(*Basic); ok {
177 return x.kind == y.kind
181 // Two array types are identical if they have identical element types
182 // and the same array length.
183 if y, ok := y.(*Array); ok {
184 // If one or both array lengths are unknown (< 0) due to some error,
185 // assume they are the same to avoid spurious follow-on errors.
186 return (x.len < 0 || y.len < 0 || x.len == y.len) && identical(x.elem, y.elem, cmpTags, p)
190 // Two slice types are identical if they have identical element types.
191 if y, ok := y.(*Slice); ok {
192 return identical(x.elem, y.elem, cmpTags, p)
196 // Two struct types are identical if they have the same sequence of fields,
197 // and if corresponding fields have the same names, and identical types,
198 // and identical tags. Two embedded fields are considered to have the same
199 // name. Lower-case field names from different packages are always different.
200 if y, ok := y.(*Struct); ok {
201 if x.NumFields() == y.NumFields() {
202 for i, f := range x.fields {
204 if f.embedded != g.embedded ||
205 cmpTags && x.Tag(i) != y.Tag(i) ||
206 !f.sameId(g.pkg, g.name) ||
207 !identical(f.typ, g.typ, cmpTags, p) {
216 // Two pointer types are identical if they have identical base types.
217 if y, ok := y.(*Pointer); ok {
218 return identical(x.base, y.base, cmpTags, p)
222 // Two tuples types are identical if they have the same number of elements
223 // and corresponding elements have identical types.
224 if y, ok := y.(*Tuple); ok {
225 if x.Len() == y.Len() {
227 for i, v := range x.vars {
229 if !identical(v.typ, w.typ, cmpTags, p) {
239 // Two function types are identical if they have the same number of parameters
240 // and result values, corresponding parameter and result types are identical,
241 // and either both functions are variadic or neither is. Parameter and result
242 // names are not required to match.
243 // Generic functions must also have matching type parameter lists, but for the
245 if y, ok := y.(*Signature); ok {
246 return x.variadic == y.variadic &&
247 identicalTParams(x.TypeParams().list(), y.TypeParams().list(), cmpTags, p) &&
248 identical(x.params, y.params, cmpTags, p) &&
249 identical(x.results, y.results, cmpTags, p)
253 if y, _ := y.(*Union); y != nil {
254 xset := computeUnionTypeSet(nil, nopos, x)
255 yset := computeUnionTypeSet(nil, nopos, y)
256 return xset.terms.equal(yset.terms)
260 // Two interface types are identical if they describe the same type sets.
261 // With the existing implementation restriction, this simplifies to:
263 // Two interface types are identical if they have the same set of methods with
264 // the same names and identical function types, and if any type restrictions
265 // are the same. Lower-case method names from different packages are always
266 // different. The order of the methods is irrelevant.
267 if y, ok := y.(*Interface); ok {
270 if !xset.terms.equal(yset.terms) {
275 if len(a) == len(b) {
276 // Interface types are the only types where cycles can occur
277 // that are not "terminated" via named types; and such cycles
278 // can only be created via method parameter types that are
279 // anonymous interfaces (directly or indirectly) embedding
280 // the current interface. Example:
282 // type T interface {
286 // If two such (differently named) interfaces are compared,
287 // endless recursion occurs if the cycle is not detected.
289 // If x and y were compared before, they must be equal
290 // (if they were not, the recursion would have stopped);
291 // search the ifacePair stack for the same pair.
293 // This is a quadratic algorithm, but in practice these stacks
294 // are extremely short (bounded by the nesting depth of interface
295 // type declarations that recur via parameter types, an extremely
296 // rare occurrence). An alternative implementation might use a
297 // "visited" map, but that is probably less efficient overall.
298 q := &ifacePair{x, y, p}
301 return true // same pair was compared before
306 assertSortedMethods(a)
307 assertSortedMethods(b)
309 for i, f := range a {
311 if f.Id() != g.Id() || !identical(f.typ, g.typ, cmpTags, q) {
320 // Two map types are identical if they have identical key and value types.
321 if y, ok := y.(*Map); ok {
322 return identical(x.key, y.key, cmpTags, p) && identical(x.elem, y.elem, cmpTags, p)
326 // Two channel types are identical if they have identical value types
327 // and the same direction.
328 if y, ok := y.(*Chan); ok {
329 return x.dir == y.dir && identical(x.elem, y.elem, cmpTags, p)
333 // Two named types are identical if their type names originate
334 // in the same type declaration.
335 if y, ok := y.(*Named); ok {
336 xargs := x.TypeArgs().list()
337 yargs := y.TypeArgs().list()
339 if len(xargs) != len(yargs) {
344 // Instances are identical if their original type and type arguments
346 if !Identical(x.orig, y.orig) {
349 for i, xa := range xargs {
350 if !Identical(xa, yargs[i]) {
357 // TODO(gri) Why is x == y not sufficient? And if it is,
358 // we can just return false here because x == y
359 // is caught in the very beginning of this function.
360 return x.obj == y.obj
364 // nothing to do (x and y being equal is caught in the very beginning of this function)
367 // avoid a crash in case of nil type
376 func identicalTParams(x, y []*TypeParam, cmpTags bool, p *ifacePair) bool {
377 if len(x) != len(y) {
380 for i, x := range x {
382 if !identical(x.bound, y.bound, cmpTags, p) {
389 // Default returns the default "typed" type for an "untyped" type;
390 // it returns the incoming type for all other types. The default type
391 // for untyped nil is untyped nil.
392 func Default(t Type) Type {
393 if t, ok := t.(*Basic); ok {
400 return universeRune // use 'rune' name
404 return Typ[Complex128]