golang.org/x/tools@v0.21.1-0.20240520172518-788d39e776b1/internal/typeparams/typeterm.go (about)

     1  // Copyright 2021 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  // Code generated by copytermlist.go DO NOT EDIT.
     6  
     7  package typeparams
     8  
     9  import "go/types"
    10  
    11  // A term describes elementary type sets:
    12  //
    13  //	 βˆ…:  (*term)(nil)     == βˆ…                      // set of no types (empty set)
    14  //	 𝓀:  &term{}          == 𝓀                      // set of all types (𝓀niverse)
    15  //	 T:  &term{false, T}  == {T}                    // set of type T
    16  //	~t:  &term{true, t}   == {t' | under(t') == t}  // set of types with underlying type t
    17  type term struct {
    18  	tilde bool // valid if typ != nil
    19  	typ   types.Type
    20  }
    21  
    22  func (x *term) String() string {
    23  	switch {
    24  	case x == nil:
    25  		return "βˆ…"
    26  	case x.typ == nil:
    27  		return "𝓀"
    28  	case x.tilde:
    29  		return "~" + x.typ.String()
    30  	default:
    31  		return x.typ.String()
    32  	}
    33  }
    34  
    35  // equal reports whether x and y represent the same type set.
    36  func (x *term) equal(y *term) bool {
    37  	// easy cases
    38  	switch {
    39  	case x == nil || y == nil:
    40  		return x == y
    41  	case x.typ == nil || y.typ == nil:
    42  		return x.typ == y.typ
    43  	}
    44  	// βˆ… βŠ‚ x, y βŠ‚ 𝓀
    45  
    46  	return x.tilde == y.tilde && types.Identical(x.typ, y.typ)
    47  }
    48  
    49  // union returns the union x βˆͺ y: zero, one, or two non-nil terms.
    50  func (x *term) union(y *term) (_, _ *term) {
    51  	// easy cases
    52  	switch {
    53  	case x == nil && y == nil:
    54  		return nil, nil // βˆ… βˆͺ βˆ… == βˆ…
    55  	case x == nil:
    56  		return y, nil // βˆ… βˆͺ y == y
    57  	case y == nil:
    58  		return x, nil // x βˆͺ βˆ… == x
    59  	case x.typ == nil:
    60  		return x, nil // 𝓀 βˆͺ y == 𝓀
    61  	case y.typ == nil:
    62  		return y, nil // x βˆͺ 𝓀 == 𝓀
    63  	}
    64  	// βˆ… βŠ‚ x, y βŠ‚ 𝓀
    65  
    66  	if x.disjoint(y) {
    67  		return x, y // x βˆͺ y == (x, y) if x ∩ y == βˆ…
    68  	}
    69  	// x.typ == y.typ
    70  
    71  	// ~t βˆͺ ~t == ~t
    72  	// ~t βˆͺ  T == ~t
    73  	//  T βˆͺ ~t == ~t
    74  	//  T βˆͺ  T ==  T
    75  	if x.tilde || !y.tilde {
    76  		return x, nil
    77  	}
    78  	return y, nil
    79  }
    80  
    81  // intersect returns the intersection x ∩ y.
    82  func (x *term) intersect(y *term) *term {
    83  	// easy cases
    84  	switch {
    85  	case x == nil || y == nil:
    86  		return nil // βˆ… ∩ y == βˆ… and ∩ βˆ… == βˆ…
    87  	case x.typ == nil:
    88  		return y // 𝓀 ∩ y == y
    89  	case y.typ == nil:
    90  		return x // x ∩ 𝓀 == x
    91  	}
    92  	// βˆ… βŠ‚ x, y βŠ‚ 𝓀
    93  
    94  	if x.disjoint(y) {
    95  		return nil // x ∩ y == βˆ… if x ∩ y == βˆ…
    96  	}
    97  	// x.typ == y.typ
    98  
    99  	// ~t ∩ ~t == ~t
   100  	// ~t ∩  T ==  T
   101  	//  T ∩ ~t ==  T
   102  	//  T ∩  T ==  T
   103  	if !x.tilde || y.tilde {
   104  		return x
   105  	}
   106  	return y
   107  }
   108  
   109  // includes reports whether t ∈ x.
   110  func (x *term) includes(t types.Type) bool {
   111  	// easy cases
   112  	switch {
   113  	case x == nil:
   114  		return false // t ∈ βˆ… == false
   115  	case x.typ == nil:
   116  		return true // t ∈ 𝓀 == true
   117  	}
   118  	// βˆ… βŠ‚ x βŠ‚ 𝓀
   119  
   120  	u := t
   121  	if x.tilde {
   122  		u = under(u)
   123  	}
   124  	return types.Identical(x.typ, u)
   125  }
   126  
   127  // subsetOf reports whether x βŠ† y.
   128  func (x *term) subsetOf(y *term) bool {
   129  	// easy cases
   130  	switch {
   131  	case x == nil:
   132  		return true // βˆ… βŠ† y == true
   133  	case y == nil:
   134  		return false // x βŠ† βˆ… == false since x != βˆ…
   135  	case y.typ == nil:
   136  		return true // x βŠ† 𝓀 == true
   137  	case x.typ == nil:
   138  		return false // 𝓀 βŠ† y == false since y != 𝓀
   139  	}
   140  	// βˆ… βŠ‚ x, y βŠ‚ 𝓀
   141  
   142  	if x.disjoint(y) {
   143  		return false // x βŠ† y == false if x ∩ y == βˆ…
   144  	}
   145  	// x.typ == y.typ
   146  
   147  	// ~t βŠ† ~t == true
   148  	// ~t βŠ† T == false
   149  	//  T βŠ† ~t == true
   150  	//  T βŠ†  T == true
   151  	return !x.tilde || y.tilde
   152  }
   153  
   154  // disjoint reports whether x ∩ y == βˆ….
   155  // x.typ and y.typ must not be nil.
   156  func (x *term) disjoint(y *term) bool {
   157  	if debug && (x.typ == nil || y.typ == nil) {
   158  		panic("invalid argument(s)")
   159  	}
   160  	ux := x.typ
   161  	if y.tilde {
   162  		ux = under(ux)
   163  	}
   164  	uy := y.typ
   165  	if x.tilde {
   166  		uy = under(uy)
   167  	}
   168  	return !types.Identical(ux, uy)
   169  }