]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: minor cleanups in predicates.go
authorRobert Griesemer <gri@golang.org>
Wed, 3 Nov 2021 04:31:50 +0000 (21:31 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 4 Nov 2021 02:57:48 +0000 (02:57 +0000)
- reordered some functions for better organization
- renamed single arguments typ to t for consistency
- updated some comments

No functional changes.

Change-Id: I4362ac48044595cdf5c3d9eb7b2f7b94e776d65b
Reviewed-on: https://go-review.googlesource.com/c/go/+/360956
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/predicates.go

index 5a82608671d1c41e97e4a041756822751291d78a..7fbb91eb61256495fc20903937b41a4fb7c417fa 100644 (file)
@@ -6,25 +6,6 @@
 
 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.
@@ -37,6 +18,7 @@ func isComplex(t Type) bool        { return isBasic(t, IsComplex) }
 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.,
@@ -74,36 +56,50 @@ func allBasic(t Type, info BasicInfo) bool {
        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.
@@ -142,15 +138,15 @@ func comparable(T Type, seen map[Type]bool) bool {
        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
 }
@@ -392,9 +388,8 @@ func identicalTParams(x, y []*TypeParam, cmpTags bool, p *ifacePair) bool {
 // 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]
@@ -410,5 +405,5 @@ func Default(typ Type) Type {
                        return Typ[String]
                }
        }
-       return typ
+       return t
 }