go/types: more operators supported by contracts

Change-Id: I03493508813d13dd1a16cd675de68170da14b29f
This commit is contained in:
Robert Griesemer 2019-09-13 22:18:19 -07:00
parent cfbeecaabb
commit 1bddb66b7c
2 changed files with 34 additions and 44 deletions

View File

@ -16,40 +16,23 @@ func isNamed(typ Type) bool {
return ok
}
func isBoolean(typ Type) bool {
t, ok := typ.Underlying().(*Basic)
return ok && t.info&IsBoolean != 0
func is(typ Type, what BasicInfo) bool {
switch t := typ.Underlying().(type) {
case *Basic:
return t.info&what != 0
case *TypeParam:
return t.contr.ifaceAt(t.index).is(func(typ Type) bool { return is(typ, what) })
}
return false
}
func isInteger(typ Type) bool {
t, ok := typ.Underlying().(*Basic)
return ok && t.info&IsInteger != 0
}
func isUnsigned(typ Type) bool {
t, ok := typ.Underlying().(*Basic)
return ok && t.info&IsUnsigned != 0
}
func isFloat(typ Type) bool {
t, ok := typ.Underlying().(*Basic)
return ok && t.info&IsFloat != 0
}
func isComplex(typ Type) bool {
t, ok := typ.Underlying().(*Basic)
return ok && t.info&IsComplex != 0
}
func isNumeric(typ Type) bool {
t, ok := typ.Underlying().(*Basic)
return ok && t.info&IsNumeric != 0
}
func isString(typ Type) bool {
t, ok := typ.Underlying().(*Basic)
return ok && t.info&IsString != 0
}
func isBoolean(typ Type) bool { return is(typ, IsBoolean) }
func isInteger(typ Type) bool { return is(typ, IsInteger) }
func isUnsigned(typ Type) bool { return is(typ, IsUnsigned) }
func isFloat(typ Type) bool { return is(typ, IsFloat) }
func isComplex(typ Type) bool { return is(typ, IsComplex) }
func isNumeric(typ Type) bool { return is(typ, IsNumeric) }
func isString(typ Type) bool { return is(typ, IsString) }
func isTyped(typ Type) bool {
t, ok := typ.Underlying().(*Basic)
@ -61,15 +44,7 @@ func isUntyped(typ Type) bool {
return ok && t.info&IsUntyped != 0
}
func isOrdered(typ Type) bool {
switch t := typ.Underlying().(type) {
case *Basic:
return t.info&IsOrdered != 0
case *TypeParam:
return t.contr.ifaceAt(t.index).is(isOrdered)
}
return false
}
func isOrdered(typ Type) bool { return is(typ, IsOrdered) }
func isConstType(typ Type) bool {
t, ok := typ.Underlying().(*Basic)

View File

@ -119,13 +119,28 @@ var _ T2(struct{x int})
// Use of contracts in parameterized functions
contract SignedInteger(T) {
T int8, int16, int32, int64, int
contract Ordered(T) {
T int8, int16, int32, int64, int,
uint8, uint16, uint32, uint64, uint,
float32, float64, string, rune
}
func min(type T SignedInteger)(x, y T) T {
func min(type T Ordered)(x, y T) T {
if x < y {
return x
}
return y
}
contract Integer(T) {
T int8, int16, int32, int64, int,
uint8, uint16, uint32, uint64, uint
}
func sum(type T Integer)(data []T) T {
var s T
for _, x := range data {
s += x
}
return s
}