]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/compile/internal/types2/predicates.go
go/types, types2: introduce _Alias type node
[gostls13.git] / src / cmd / compile / internal / types2 / predicates.go
index 4f8441467e5c90cfba1fc27af1fd2945362f7a03..9ec7d58d6fbab69a18fbf509e0fff95b867dfecf 100644 (file)
@@ -6,6 +6,9 @@
 
 package types2
 
+// isValid reports whether t is a valid type.
+func isValid(t Type) bool { return _Unalias(t) != Typ[Invalid] }
+
 // 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.
@@ -47,7 +50,7 @@ func allNumericOrString(t Type) bool { return allBasic(t, IsNumeric|IsString) }
 // for all specific types of the type parameter's type set.
 // allBasic(t, info) is an optimized version of isBasic(coreType(t), info).
 func allBasic(t Type, info BasicInfo) bool {
-       if tpar, _ := t.(*TypeParam); tpar != nil {
+       if tpar, _ := _Unalias(t).(*TypeParam); tpar != nil {
                return tpar.is(func(t *term) bool { return t != nil && isBasic(t.typ, info) })
        }
        return isBasic(t, info)
@@ -57,7 +60,7 @@ func allBasic(t Type, info BasicInfo) bool {
 // 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) {
+       switch _Unalias(t).(type) {
        case *Basic, *Named, *TypeParam:
                return true
        }
@@ -68,7 +71,7 @@ func hasName(t Type) bool {
 // This includes all non-defined types, but also basic types.
 // isTypeLit may be called with types that are not fully set up.
 func isTypeLit(t Type) bool {
-       switch t.(type) {
+       switch _Unalias(t).(type) {
        case *Named, *TypeParam:
                return false
        }
@@ -79,8 +82,10 @@ func isTypeLit(t Type) bool {
 // constant or boolean. isTyped may be called with types that
 // are not fully set up.
 func isTyped(t Type) bool {
-       // isTyped is called with types that are not fully
-       // set up. Must not call under()!
+       // Alias or Named types cannot denote untyped types,
+       // thus we don't need to call _Unalias or under
+       // (which would be unsafe to do for types that are
+       // not fully set up).
        b, _ := t.(*Basic)
        return b == nil || b.info&IsUntyped == 0
 }
@@ -103,7 +108,7 @@ func isNonTypeParamInterface(t Type) bool {
 
 // isTypeParam reports whether t is a type parameter.
 func isTypeParam(t Type) bool {
-       _, ok := t.(*TypeParam)
+       _, ok := _Unalias(t).(*TypeParam)
        return ok
 }
 
@@ -112,7 +117,7 @@ func isTypeParam(t Type) bool {
 // use anywhere, but it may report a false negative if the type set has not been
 // computed yet.
 func hasEmptyTypeset(t Type) bool {
-       if tpar, _ := t.(*TypeParam); tpar != nil && tpar.bound != nil {
+       if tpar, _ := _Unalias(t).(*TypeParam); tpar != nil && tpar.bound != nil {
                iface, _ := safeUnderlying(tpar.bound).(*Interface)
                return iface != nil && iface.tset != nil && iface.tset.IsEmpty()
        }
@@ -124,7 +129,7 @@ func hasEmptyTypeset(t Type) bool {
 // 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)
+       named := asNamed(t)
        return named != nil && named.obj != nil && named.inst == nil && named.TypeParams().Len() > 0
 }
 
@@ -218,11 +223,14 @@ type comparer struct {
 
 // For changes to this code the corresponding changes should be made to unifier.nify.
 func (c *comparer) identical(x, y Type, p *ifacePair) bool {
+       x = _Unalias(x)
+       y = _Unalias(y)
+
        if x == y {
                return true
        }
 
-       if c.ignoreInvalids && (x == Typ[Invalid] || y == Typ[Invalid]) {
+       if c.ignoreInvalids && (!isValid(x) || !isValid(y)) {
                return true
        }
 
@@ -435,7 +443,7 @@ func (c *comparer) identical(x, y Type, p *ifacePair) bool {
                // Two named types are identical if their type names originate
                // in the same type declaration; if they are instantiated they
                // must have identical type argument lists.
-               if y, ok := y.(*Named); ok {
+               if y := asNamed(y); y != nil {
                        // check type arguments before origins to match unifier
                        // (for correct source code we need to do all checks so
                        // order doesn't matter)
@@ -449,7 +457,7 @@ func (c *comparer) identical(x, y Type, p *ifacePair) bool {
                                        return false
                                }
                        }
-                       return indenticalOrigin(x, y)
+                       return identicalOrigin(x, y)
                }
 
        case *TypeParam:
@@ -466,7 +474,7 @@ func (c *comparer) identical(x, y Type, p *ifacePair) bool {
 }
 
 // identicalOrigin reports whether x and y originated in the same declaration.
-func indenticalOrigin(x, y *Named) bool {
+func identicalOrigin(x, y *Named) bool {
        // TODO(gri) is this correct?
        return x.Origin().obj == y.Origin().obj
 }
@@ -492,7 +500,7 @@ func identicalInstance(xorig Type, xargs []Type, yorig Type, yargs []Type) bool
 // it returns the incoming type for all other types. The default type
 // for untyped nil is untyped nil.
 func Default(t Type) Type {
-       if t, ok := t.(*Basic); ok {
+       if t, ok := _Unalias(t).(*Basic); ok {
                switch t.kind {
                case UntypedBool:
                        return Typ[Bool]
@@ -510,3 +518,29 @@ func Default(t Type) Type {
        }
        return t
 }
+
+// maxType returns the "largest" type that encompasses both x and y.
+// If x and y are different untyped numeric types, the result is the type of x or y
+// that appears later in this list: integer, rune, floating-point, complex.
+// Otherwise, if x != y, the result is nil.
+func maxType(x, y Type) Type {
+       // We only care about untyped types (for now), so == is good enough.
+       // TODO(gri) investigate generalizing this function to simplify code elsewhere
+       if x == y {
+               return x
+       }
+       if isUntyped(x) && isUntyped(y) && isNumeric(x) && isNumeric(y) {
+               // untyped types are basic types
+               if x.(*Basic).kind > y.(*Basic).kind {
+                       return x
+               }
+               return y
+       }
+       return nil
+}
+
+// clone makes a "flat copy" of *p and returns a pointer to the copy.
+func clone[P *T, T any](p P) P {
+       c := *p
+       return &c
+}