github.com/gotranspile/cxgo@v0.3.7/types/helpers.go (about)

     1  package types
     2  
     3  func Unwrap(t Type) Type {
     4  	for {
     5  		named, ok := t.(Named)
     6  		if !ok {
     7  			return t
     8  		}
     9  		t = named.Underlying()
    10  	}
    11  }
    12  
    13  func UnwrapPtr(t Type) PtrType {
    14  	for {
    15  		switch tt := t.(type) {
    16  		case PtrType:
    17  			return tt
    18  		case Named:
    19  			t = tt.Underlying()
    20  		default:
    21  			return nil
    22  		}
    23  	}
    24  }
    25  
    26  func IsFuncPtr(t Type) (*FuncType, bool) {
    27  	t = Unwrap(t)
    28  	ptr, ok := t.(PtrType)
    29  	if !ok {
    30  		return nil, false
    31  	}
    32  	fnc, ok := ptr.Elem().(*FuncType)
    33  	return fnc, ok
    34  }
    35  
    36  func IsUnsafePtr(t Type) bool {
    37  	return t.Kind().IsUnsafePtr()
    38  }
    39  
    40  func IsUnsafePtr2(t Type) bool {
    41  	if p, ok := t.(PtrType); ok && IsUnsafePtr(p.Elem()) {
    42  		return true
    43  	}
    44  	return false
    45  }
    46  
    47  func IsPtr(t Type) bool {
    48  	return t.Kind().IsPtr()
    49  }
    50  
    51  func IsInt(t Type) bool {
    52  	return t.Kind().IsInt()
    53  }
    54  
    55  func sameInt(x, y IntType) bool {
    56  	if x.untyped && y.untyped {
    57  		return x.signed == y.signed
    58  	} else if x.untyped {
    59  		if x.size > y.size {
    60  			return false
    61  		}
    62  		return x.size < y.size || x.signed == y.signed
    63  	} else if y.untyped {
    64  		if y.size > x.size {
    65  			return false
    66  		}
    67  		return y.size < x.size || x.signed == y.signed
    68  	}
    69  	return x.Sizeof() == y.Sizeof() && x.Signed() == y.Signed()
    70  }
    71  
    72  func Same(x, y Type) bool {
    73  	if (x == nil && y != nil) || (x != nil && y == nil) {
    74  		return false
    75  	} else if _, ok := x.(*unkType); ok {
    76  		return false
    77  	} else if _, ok := y.(*unkType); ok {
    78  		return false
    79  	} else if x == y {
    80  		return true
    81  	}
    82  	switch x := x.(type) {
    83  	case IntType:
    84  		y, ok := y.(IntType)
    85  		if !ok {
    86  			return false
    87  		}
    88  		return sameInt(x, y)
    89  	case PtrType:
    90  		y, ok := y.(PtrType)
    91  		return ok && Same(x.Elem(), y.Elem())
    92  	case ArrayType:
    93  		y, ok := y.(ArrayType)
    94  		return ok && x.size == y.size && x.slice == y.slice && Same(x.elem, y.elem)
    95  	case *namedType:
    96  		y, ok := y.(*namedType)
    97  		return ok && x.name == y.name && Same(x.typ, y.typ)
    98  	case *FuncType:
    99  		y, ok := y.(*FuncType)
   100  		if !ok || !Same(x.Return(), y.Return()) {
   101  			return false
   102  		}
   103  		xargs, yargs := x.Args(), y.Args()
   104  		if len(xargs) != len(yargs) {
   105  			return false
   106  		}
   107  		for i := range xargs {
   108  			if !Same(xargs[i].Type(), yargs[i].Type()) {
   109  				return false
   110  			}
   111  		}
   112  		return true
   113  	}
   114  	return x == y
   115  }