github.com/AndrienkoAleksandr/go@v0.0.19/src/go/types/typeterm.go (about)

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