typeterm.go raw

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