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.
11 // hasName reports whether typ has a name. This includes
12 // predeclared types, defined types, and type parameters.
13 // hasName may be called with types that are not fully set up.
14 func hasName(typ Type) bool {
16 case *Basic, *Named, *TypeParam:
22 // isGeneric reports whether a type is a generic, uninstantiated type (generic
23 // signatures are not included).
24 func isGeneric(typ Type) bool {
25 // A parameterized type is only instantiated if it doesn't have an instantiation already.
26 named, _ := typ.(*Named)
27 return named != nil && named.obj != nil && named.targs == nil && named.TypeParams() != nil
30 // The is_X predicates below report whether t is an X.
31 // If t is a type parameter the result is false; i.e.,
32 // these predicates don't look inside a type parameter.
34 func is_Boolean(t Type) bool { return isBasic(t, IsBoolean) }
35 func is_Integer(t Type) bool { return isBasic(t, IsInteger) }
36 func is_Unsigned(t Type) bool { return isBasic(t, IsUnsigned) }
37 func is_Float(t Type) bool { return isBasic(t, IsFloat) }
38 func is_Complex(t Type) bool { return isBasic(t, IsComplex) }
39 func is_Numeric(t Type) bool { return isBasic(t, IsNumeric) }
40 func is_String(t Type) bool { return isBasic(t, IsString) }
41 func is_IntegerOrFloat(t Type) bool { return isBasic(t, IsInteger|IsFloat) }
43 // isBasic reports whether under(t) is a basic type with the specified info.
44 // If t is a type parameter the result is false; i.e.,
45 // isBasic does not look inside a type parameter.
46 func isBasic(t Type, info BasicInfo) bool {
47 u, _ := under(t).(*Basic)
48 return u != nil && u.info&info != 0
51 // The allX predicates below report whether t is an X.
52 // If t is a type parameter the result is true if is_X is true
53 // for all specified types of the type parameter's type set.
54 // allX is an optimized version of is_X(structure(t)) (which
55 // is the same as underIs(t, is_X)).
57 func allBoolean(typ Type) bool { return allBasic(typ, IsBoolean) }
58 func allInteger(typ Type) bool { return allBasic(typ, IsInteger) }
59 func allUnsigned(typ Type) bool { return allBasic(typ, IsUnsigned) }
60 func allNumeric(typ Type) bool { return allBasic(typ, IsNumeric) }
61 func allString(typ Type) bool { return allBasic(typ, IsString) }
62 func allOrdered(typ Type) bool { return allBasic(typ, IsOrdered) }
63 func allNumericOrString(typ Type) bool { return allBasic(typ, IsNumeric|IsString) }
65 // allBasic reports whether under(t) is a basic type with the specified info.
66 // If t is a type parameter, the result is true if isBasic(t, info) is true
67 // for all specific types of the type parameter's type set.
68 // allBasic(t, info) is an optimized version of isBasic(structure(t), info).
69 func allBasic(t Type, info BasicInfo) bool {
70 switch u := under(t).(type) {
72 return u.info&info != 0
74 return u.is(func(t *term) bool { return t != nil && isBasic(t.typ, info) })
79 // isTyped reports whether typ is typed; i.e., not an untyped
80 // constant or boolean. isTyped may be called with types that
81 // are not fully set up.
82 func isTyped(typ Type) bool {
83 // isTyped is called with types that are not fully
84 // set up. Must not call asBasic()!
86 return t == nil || t.info&IsUntyped == 0
89 // isUntyped(typ) is the same as !isTyped(typ).
90 func isUntyped(typ Type) bool {
94 func isConstType(typ Type) bool {
95 // Type parameters are never const types.
97 return t != nil && t.info&IsConstType != 0
100 // IsInterface reports whether typ is an interface type.
101 func IsInterface(typ Type) bool {
102 return asInterface(typ) != nil
105 // isTypeParam reports whether typ is a type parameter.
106 func isTypeParam(typ Type) bool {
107 _, ok := under(typ).(*TypeParam)
111 // Comparable reports whether values of type T are comparable.
112 func Comparable(T Type) bool {
113 return comparable(T, nil)
116 func comparable(T Type, seen map[Type]bool) bool {
121 seen = make(map[Type]bool)
125 switch t := under(T).(type) {
127 // assume invalid types to be comparable
128 // to avoid follow-up errors
129 return t.kind != UntypedNil
130 case *Pointer, *Interface, *Chan:
133 for _, f := range t.fields {
134 if !comparable(f.typ, seen) {
140 return comparable(t.elem, seen)
142 return t.iface().IsComparable()
147 // hasNil reports whether a type includes the nil value.
148 func hasNil(typ Type) bool {
149 switch t := under(typ).(type) {
151 return t.kind == UnsafePointer
152 case *Slice, *Pointer, *Signature, *Interface, *Map, *Chan:
155 return t.underIs(hasNil)
160 // An ifacePair is a node in a stack of interface type pairs compared for identity.
161 type ifacePair struct {
166 func (p *ifacePair) identical(q *ifacePair) bool {
167 return p.x == q.x && p.y == q.y || p.x == q.y && p.y == q.x
170 // For changes to this code the corresponding changes should be made to unifier.nify.
171 func identical(x, y Type, cmpTags bool, p *ifacePair) bool {
176 switch x := x.(type) {
178 // Basic types are singletons except for the rune and byte
179 // aliases, thus we cannot solely rely on the x == y check
180 // above. See also comment in TypeName.IsAlias.
181 if y, ok := y.(*Basic); ok {
182 return x.kind == y.kind
186 // Two array types are identical if they have identical element types
187 // and the same array length.
188 if y, ok := y.(*Array); ok {
189 // If one or both array lengths are unknown (< 0) due to some error,
190 // assume they are the same to avoid spurious follow-on errors.
191 return (x.len < 0 || y.len < 0 || x.len == y.len) && identical(x.elem, y.elem, cmpTags, p)
195 // Two slice types are identical if they have identical element types.
196 if y, ok := y.(*Slice); ok {
197 return identical(x.elem, y.elem, cmpTags, p)
201 // Two struct types are identical if they have the same sequence of fields,
202 // and if corresponding fields have the same names, and identical types,
203 // and identical tags. Two embedded fields are considered to have the same
204 // name. Lower-case field names from different packages are always different.
205 if y, ok := y.(*Struct); ok {
206 if x.NumFields() == y.NumFields() {
207 for i, f := range x.fields {
209 if f.embedded != g.embedded ||
210 cmpTags && x.Tag(i) != y.Tag(i) ||
211 !f.sameId(g.pkg, g.name) ||
212 !identical(f.typ, g.typ, cmpTags, p) {
221 // Two pointer types are identical if they have identical base types.
222 if y, ok := y.(*Pointer); ok {
223 return identical(x.base, y.base, cmpTags, p)
227 // Two tuples types are identical if they have the same number of elements
228 // and corresponding elements have identical types.
229 if y, ok := y.(*Tuple); ok {
230 if x.Len() == y.Len() {
232 for i, v := range x.vars {
234 if !identical(v.typ, w.typ, cmpTags, p) {
244 // Two function types are identical if they have the same number of parameters
245 // and result values, corresponding parameter and result types are identical,
246 // and either both functions are variadic or neither is. Parameter and result
247 // names are not required to match.
248 // Generic functions must also have matching type parameter lists, but for the
250 if y, ok := y.(*Signature); ok {
251 return x.variadic == y.variadic &&
252 identicalTParams(x.TypeParams().list(), y.TypeParams().list(), cmpTags, p) &&
253 identical(x.params, y.params, cmpTags, p) &&
254 identical(x.results, y.results, cmpTags, p)
258 if y, _ := y.(*Union); y != nil {
259 xset := computeUnionTypeSet(nil, token.NoPos, x)
260 yset := computeUnionTypeSet(nil, token.NoPos, y)
261 return xset.terms.equal(yset.terms)
265 // Two interface types are identical if they describe the same type sets.
266 // With the existing implementation restriction, this simplifies to:
268 // Two interface types are identical if they have the same set of methods with
269 // the same names and identical function types, and if any type restrictions
270 // are the same. Lower-case method names from different packages are always
271 // different. The order of the methods is irrelevant.
272 if y, ok := y.(*Interface); ok {
275 if !xset.terms.equal(yset.terms) {
280 if len(a) == len(b) {
281 // Interface types are the only types where cycles can occur
282 // that are not "terminated" via named types; and such cycles
283 // can only be created via method parameter types that are
284 // anonymous interfaces (directly or indirectly) embedding
285 // the current interface. Example:
287 // type T interface {
291 // If two such (differently named) interfaces are compared,
292 // endless recursion occurs if the cycle is not detected.
294 // If x and y were compared before, they must be equal
295 // (if they were not, the recursion would have stopped);
296 // search the ifacePair stack for the same pair.
298 // This is a quadratic algorithm, but in practice these stacks
299 // are extremely short (bounded by the nesting depth of interface
300 // type declarations that recur via parameter types, an extremely
301 // rare occurrence). An alternative implementation might use a
302 // "visited" map, but that is probably less efficient overall.
303 q := &ifacePair{x, y, p}
306 return true // same pair was compared before
311 assertSortedMethods(a)
312 assertSortedMethods(b)
314 for i, f := range a {
316 if f.Id() != g.Id() || !identical(f.typ, g.typ, cmpTags, q) {
325 // Two map types are identical if they have identical key and value types.
326 if y, ok := y.(*Map); ok {
327 return identical(x.key, y.key, cmpTags, p) && identical(x.elem, y.elem, cmpTags, p)
331 // Two channel types are identical if they have identical value types
332 // and the same direction.
333 if y, ok := y.(*Chan); ok {
334 return x.dir == y.dir && identical(x.elem, y.elem, cmpTags, p)
338 // Two named types are identical if their type names originate
339 // in the same type declaration.
340 if y, ok := y.(*Named); ok {
341 xargs := x.TypeArgs().list()
342 yargs := y.TypeArgs().list()
344 if len(xargs) != len(yargs) {
349 // Instances are identical if their original type and type arguments
351 if !Identical(x.orig, y.orig) {
354 for i, xa := range xargs {
355 if !Identical(xa, yargs[i]) {
362 // TODO(gri) Why is x == y not sufficient? And if it is,
363 // we can just return false here because x == y
364 // is caught in the very beginning of this function.
365 return x.obj == y.obj
369 // nothing to do (x and y being equal is caught in the very beginning of this function)
372 // avoid a crash in case of nil type
381 func identicalTParams(x, y []*TypeParam, cmpTags bool, p *ifacePair) bool {
382 if len(x) != len(y) {
385 for i, x := range x {
387 if !identical(x.bound, y.bound, cmpTags, p) {
394 // Default returns the default "typed" type for an "untyped" type;
395 // it returns the incoming type for all other types. The default type
396 // for untyped nil is untyped nil.
398 func Default(typ Type) Type {
399 if t, ok := typ.(*Basic); ok {
406 return universeRune // use 'rune' name
410 return Typ[Complex128]