github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/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.
   114  func Identical(x, y Type) bool {
   115  	return identical(x, y, nil)
   116  }
   117  
   118  // An ifacePair is a node in a stack of interface type pairs compared for identity.
   119  type ifacePair struct {
   120  	x, y *Interface
   121  	prev *ifacePair
   122  }
   123  
   124  func (p *ifacePair) identical(q *ifacePair) bool {
   125  	return p.x == q.x && p.y == q.y || p.x == q.y && p.y == q.x
   126  }
   127  
   128  func identical(x, y Type, p *ifacePair) bool {
   129  	if x == y {
   130  		return true
   131  	}
   132  
   133  	switch x := x.(type) {
   134  	case *Basic:
   135  		// Basic types are singletons except for the rune and byte
   136  		// aliases, thus we cannot solely rely on the x == y check
   137  		// above.
   138  		if y, ok := y.(*Basic); ok {
   139  			return x.kind == y.kind
   140  		}
   141  
   142  	case *Array:
   143  		// Two array types are identical if they have identical element types
   144  		// and the same array length.
   145  		if y, ok := y.(*Array); ok {
   146  			return x.len == y.len && identical(x.elem, y.elem, p)
   147  		}
   148  
   149  	case *Slice:
   150  		// Two slice types are identical if they have identical element types.
   151  		if y, ok := y.(*Slice); ok {
   152  			return identical(x.elem, y.elem, p)
   153  		}
   154  
   155  	case *Struct:
   156  		// Two struct types are identical if they have the same sequence of fields,
   157  		// and if corresponding fields have the same names, and identical types,
   158  		// and identical tags. Two anonymous fields are considered to have the same
   159  		// name. Lower-case field names from different packages are always different.
   160  		if y, ok := y.(*Struct); ok {
   161  			if x.NumFields() == y.NumFields() {
   162  				for i, f := range x.fields {
   163  					g := y.fields[i]
   164  					if f.anonymous != g.anonymous ||
   165  						x.Tag(i) != y.Tag(i) ||
   166  						!f.sameId(g.pkg, g.name) ||
   167  						!identical(f.typ, g.typ, p) {
   168  						return false
   169  					}
   170  				}
   171  				return true
   172  			}
   173  		}
   174  
   175  	case *Pointer:
   176  		// Two pointer types are identical if they have identical base types.
   177  		if y, ok := y.(*Pointer); ok {
   178  			return identical(x.base, y.base, p)
   179  		}
   180  
   181  	case *Tuple:
   182  		// Two tuples types are identical if they have the same number of elements
   183  		// and corresponding elements have identical types.
   184  		if y, ok := y.(*Tuple); ok {
   185  			if x.Len() == y.Len() {
   186  				if x != nil {
   187  					for i, v := range x.vars {
   188  						w := y.vars[i]
   189  						if !identical(v.typ, w.typ, p) {
   190  							return false
   191  						}
   192  					}
   193  				}
   194  				return true
   195  			}
   196  		}
   197  
   198  	case *Signature:
   199  		// Two function types are identical if they have the same number of parameters
   200  		// and result values, corresponding parameter and result types are identical,
   201  		// and either both functions are variadic or neither is. Parameter and result
   202  		// names are not required to match.
   203  		if y, ok := y.(*Signature); ok {
   204  			return x.variadic == y.variadic &&
   205  				identical(x.params, y.params, p) &&
   206  				identical(x.results, y.results, p)
   207  		}
   208  
   209  	case *Interface:
   210  		// Two interface types are identical if they have the same set of methods with
   211  		// the same names and identical function types. Lower-case method names from
   212  		// different packages are always different. The order of the methods is irrelevant.
   213  		if y, ok := y.(*Interface); ok {
   214  			a := x.allMethods
   215  			b := y.allMethods
   216  			if len(a) == len(b) {
   217  				// Interface types are the only types where cycles can occur
   218  				// that are not "terminated" via named types; and such cycles
   219  				// can only be created via method parameter types that are
   220  				// anonymous interfaces (directly or indirectly) embedding
   221  				// the current interface. Example:
   222  				//
   223  				//    type T interface {
   224  				//        m() interface{T}
   225  				//    }
   226  				//
   227  				// If two such (differently named) interfaces are compared,
   228  				// endless recursion occurs if the cycle is not detected.
   229  				//
   230  				// If x and y were compared before, they must be equal
   231  				// (if they were not, the recursion would have stopped);
   232  				// search the ifacePair stack for the same pair.
   233  				//
   234  				// This is a quadratic algorithm, but in practice these stacks
   235  				// are extremely short (bounded by the nesting depth of interface
   236  				// type declarations that recur via parameter types, an extremely
   237  				// rare occurrence). An alternative implementation might use a
   238  				// "visited" map, but that is probably less efficient overall.
   239  				q := &ifacePair{x, y, p}
   240  				for p != nil {
   241  					if p.identical(q) {
   242  						return true // same pair was compared before
   243  					}
   244  					p = p.prev
   245  				}
   246  				if debug {
   247  					assert(sort.IsSorted(byUniqueMethodName(a)))
   248  					assert(sort.IsSorted(byUniqueMethodName(b)))
   249  				}
   250  				for i, f := range a {
   251  					g := b[i]
   252  					if f.Id() != g.Id() || !identical(f.typ, g.typ, q) {
   253  						return false
   254  					}
   255  				}
   256  				return true
   257  			}
   258  		}
   259  
   260  	case *Map:
   261  		// Two map types are identical if they have identical key and value types.
   262  		if y, ok := y.(*Map); ok {
   263  			return identical(x.key, y.key, p) && identical(x.elem, y.elem, p)
   264  		}
   265  
   266  	case *Chan:
   267  		// Two channel types are identical if they have identical value types
   268  		// and the same direction.
   269  		if y, ok := y.(*Chan); ok {
   270  			return x.dir == y.dir && identical(x.elem, y.elem, p)
   271  		}
   272  
   273  	case *Named:
   274  		// Two named types are identical if their type names originate
   275  		// in the same type declaration.
   276  		if y, ok := y.(*Named); ok {
   277  			return x.obj == y.obj
   278  		}
   279  
   280  	default:
   281  		unreachable()
   282  	}
   283  
   284  	return false
   285  }
   286  
   287  // defaultType returns the default "typed" type for an "untyped" type;
   288  // it returns the incoming type for all other types. The default type
   289  // for untyped nil is untyped nil.
   290  //
   291  func defaultType(typ Type) Type {
   292  	if t, ok := typ.(*Basic); ok {
   293  		switch t.kind {
   294  		case UntypedBool:
   295  			return Typ[Bool]
   296  		case UntypedInt:
   297  			return Typ[Int]
   298  		case UntypedRune:
   299  			return universeRune // use 'rune' name
   300  		case UntypedFloat:
   301  			return Typ[Float64]
   302  		case UntypedComplex:
   303  			return Typ[Complex128]
   304  		case UntypedString:
   305  			return Typ[String]
   306  		}
   307  	}
   308  	return typ
   309  }