compiler: implement interface assertions

This is a lot harder than 'regular' type assertions as the actual
methods need to be checked.
This commit is contained in:
Ayke van Laethem
2018-09-06 20:18:18 +02:00
parent 30ac6ec281
commit 43b8c24226
5 changed files with 290 additions and 59 deletions
+58 -9
View File
@@ -10,10 +10,10 @@ package runtime
// signatures as interned strings at compile time.
//
// The typecode is a small number unique for the Go type. All typecodes <
// firstInterfaceNum do not have any methods and typecodes >= firstInterfaceNum
// all have at least one method. This means that methodSetRanges does not need
// to contain types without methods and is thus indexed starting at a typecode
// with number firstInterfaceNum.
// firstTypeWithMethods do not have any methods and typecodes >=
// firstTypeWithMethods all have at least one method. This means that
// methodSetRanges does not need to contain types without methods and is thus
// indexed starting at a typecode with number firstTypeWithMethods.
//
// To further conserve some space, the methodSetRange (as the name indicates)
// doesn't contain a list of methods and function pointers directly, but instead
@@ -36,10 +36,13 @@ type methodSetRange struct {
// which is a dummy value, but will be bigger after the compiler has filled them
// in.
var (
firstInterfaceNum uint16 // the lowest typecode that has at least one method
methodSetRanges [0]methodSetRange // indexes into methodSetSignatures and methodSetFunctions
methodSetSignatures [0]uint16 // uniqued method ID
methodSetFunctions [0]*uint8 // function pointer of method
firstTypeWithMethods uint16 // the lowest typecode that has at least one method
methodSetRanges [0]methodSetRange // indexes into methodSetSignatures and methodSetFunctions
methodSetSignatures [0]uint16 // uniqued method ID
methodSetFunctions [0]*uint8 // function pointer of method
interfaceIndex [0]uint16 // mapping from interface ID to an index in interfaceMethods
interfaceLengths [0]uint8 // mapping from interface ID to the number of methods it has
interfaceMethods [0]uint16 // the method an interface implements (list of method IDs)
)
// Get the function pointer for the method on the interface.
@@ -50,7 +53,7 @@ func interfaceMethod(itf _interface, method uint16) *uint8 {
// in the list of signatures. The compiler will only emit
// runtime.interfaceMethod calls when the method actually exists on this
// interface (proven by the typechecker).
i := methodSetRanges[itf.typecode-firstInterfaceNum].index
i := methodSetRanges[itf.typecode-firstTypeWithMethods].index
for {
if methodSetSignatures[i] == method {
return methodSetFunctions[i]
@@ -72,3 +75,49 @@ func interfaceEqual(x, y _interface) bool {
// TODO: depends on reflection.
panic("unimplemented: interface equality")
}
// Return true iff the type implements all methods needed by the interface. This
// means the type satisfies the interface.
// This is a compiler intrinsic.
//go:nobounds
func interfaceImplements(typecode, interfaceNum uint16) bool {
// method set indexes of the concrete type
methodSet := methodSetRanges[typecode-firstTypeWithMethods]
methodIndex := methodSet.index
methodIndexEnd := methodSet.index + methodSet.length
// method set indexes of the interface
itfIndex := interfaceIndex[interfaceNum]
itfIndexEnd := itfIndex + uint16(interfaceLengths[interfaceNum])
// Iterate over all methods of the interface:
for itfIndex < itfIndexEnd {
methodId := interfaceMethods[itfIndex]
if methodIndex >= methodIndexEnd {
// Reached the end of the list of methods, so interface doesn't
// implement this type.
return false
}
if methodId == methodSetSignatures[methodIndex] {
// Found a matching method, continue to the next method.
itfIndex++
methodIndex++
continue
} else if methodId > methodSetSignatures[methodIndex] {
// The method didn't match, but method ID of the concrete type was
// lower than that of the interface, so probably it has a method the
// interface doesn't implement.
// Move on to the next method of the concrete type.
methodIndex++
continue
} else {
// The concrete type is missing a method. This means the type assert
// fails.
return false
}
}
// Found a method for each expected method in the interface. This type
// assert is successful.
return true
}