package types2
-// hasName reports whether typ has a name. This includes
-// predeclared types, defined types, and type parameters.
-// hasName may be called with types that are not fully set up.
-func hasName(typ Type) bool {
- switch typ.(type) {
- case *Basic, *Named, *TypeParam:
- return true
- }
- return false
-}
-
-// isGeneric reports whether a type is a generic, uninstantiated type (generic
-// signatures are not included).
-func isGeneric(typ Type) bool {
- // A parameterized type is only instantiated if it doesn't have an instantiation already.
- named, _ := typ.(*Named)
- return named != nil && named.obj != nil && named.targs == nil && named.TypeParams() != nil
-}
-
// The isX predicates below report whether t is an X.
// If t is a type parameter the result is false; i.e.,
// these predicates don't look inside a type parameter.
func isNumeric(t Type) bool { return isBasic(t, IsNumeric) }
func isString(t Type) bool { return isBasic(t, IsString) }
func isIntegerOrFloat(t Type) bool { return isBasic(t, IsInteger|IsFloat) }
+func isConstType(t Type) bool { return isBasic(t, IsConstType) }
// isBasic reports whether under(t) is a basic type with the specified info.
// If t is a type parameter the result is false; i.e.,
return false
}
-// isTyped reports whether typ is typed; i.e., not an untyped
+// hasName reports whether t has a name. This includes
+// predeclared types, defined types, and type parameters.
+// hasName may be called with types that are not fully set up.
+func hasName(t Type) bool {
+ switch t.(type) {
+ case *Basic, *Named, *TypeParam:
+ return true
+ }
+ return false
+}
+
+// isTyped reports whether t is typed; i.e., not an untyped
// constant or boolean. isTyped may be called with types that
// are not fully set up.
-func isTyped(typ Type) bool {
+func isTyped(t Type) bool {
// isTyped is called with types that are not fully
// set up. Must not call asBasic()!
- t, _ := typ.(*Basic)
- return t == nil || t.info&IsUntyped == 0
+ b, _ := t.(*Basic)
+ return b == nil || b.info&IsUntyped == 0
}
-// isUntyped(typ) is the same as !isTyped(typ).
-func isUntyped(typ Type) bool {
- return !isTyped(typ)
+// isUntyped(t) is the same as !isTyped(t).
+func isUntyped(t Type) bool {
+ return !isTyped(t)
}
-func isConstType(typ Type) bool {
- // Type parameters are never const types.
- t := asBasic(typ)
- return t != nil && t.info&IsConstType != 0
+// IsInterface reports whether t is an interface type.
+func IsInterface(t Type) bool {
+ return asInterface(t) != nil
}
-// IsInterface reports whether typ is an interface type.
-func IsInterface(typ Type) bool {
- return asInterface(typ) != nil
+// isTypeParam reports whether t is a type parameter.
+func isTypeParam(t Type) bool {
+ _, ok := under(t).(*TypeParam)
+ return ok
}
-// isTypeParam reports whether typ is a type parameter.
-func isTypeParam(typ Type) bool {
- _, ok := under(typ).(*TypeParam)
- return ok
+// isGeneric reports whether a type is a generic, uninstantiated type
+// (generic signatures are not included).
+// TODO(gri) should we include signatures or assert that they are not present?
+func isGeneric(t Type) bool {
+ // A parameterized type is only generic if it doesn't have an instantiation already.
+ named, _ := t.(*Named)
+ return named != nil && named.obj != nil && named.targs == nil && named.TypeParams() != nil
}
// Comparable reports whether values of type T are comparable.
return false
}
-// hasNil reports whether a type includes the nil value.
-func hasNil(typ Type) bool {
- switch t := under(typ).(type) {
+// hasNil reports whether type t includes the nil value.
+func hasNil(t Type) bool {
+ switch u := under(t).(type) {
case *Basic:
- return t.kind == UnsafePointer
+ return u.kind == UnsafePointer
case *Slice, *Pointer, *Signature, *Interface, *Map, *Chan:
return true
case *TypeParam:
- return t.underIs(hasNil)
+ return u.underIs(hasNil)
}
return false
}
// Default returns the default "typed" type for an "untyped" type;
// it returns the incoming type for all other types. The default type
// for untyped nil is untyped nil.
-//
-func Default(typ Type) Type {
- if t, ok := typ.(*Basic); ok {
+func Default(t Type) Type {
+ if t, ok := t.(*Basic); ok {
switch t.kind {
case UntypedBool:
return Typ[Bool]
return Typ[String]
}
}
- return typ
+ return t
}