compiler: support for byte arrays as keys in maps

Этот коммит содержится в:
Konstantin Yegupov 2019-01-31 01:12:57 +10:00 коммит произвёл Ayke van Laethem
родитель f8a1e5f449
коммит 504c82a0e7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED
4 изменённых файлов: 25 добавлений и 0 удалений

Просмотреть файл

@ -120,6 +120,10 @@ func hashmapIsBinaryKey(keyType types.Type) bool {
} }
} }
return true return true
case *types.Array:
return hashmapIsBinaryKey(keyType.Elem())
case *types.Named:
return hashmapIsBinaryKey(keyType.Underlying())
default: default:
return false return false
} }

Просмотреть файл

@ -451,6 +451,13 @@ func (v *MapValue) Value() llvm.Value {
keyBuf[i] = byte(n) keyBuf[i] = byte(n)
n >>= 8 n >>= 8
} }
} else if key.Type().TypeKind() == llvm.ArrayTypeKind &&
key.Type().ElementType().TypeKind() == llvm.IntegerTypeKind &&
key.Type().ElementType().IntTypeWidth() == 8 {
keyBuf = make([]byte, v.Eval.TargetData.TypeAllocSize(key.Type()))
for i := range keyBuf {
keyBuf[i] = byte(llvm.ConstExtractValue(llvmKey, []uint32{uint32(i)}).ZExtValue())
}
} else { } else {
panic("interp: map key type not implemented: " + key.Type().String()) panic("interp: map key type not implemented: " + key.Type().String())
} }

12
testdata/map.go предоставленный
Просмотреть файл

@ -15,6 +15,13 @@ var testmap2 = map[string]int{
"eleven": 11, "eleven": 11,
"twelve": 12, "twelve": 12,
} }
type ArrayKey [4]byte
var testMapArrayKey = map[ArrayKey]int{
ArrayKey([4]byte{1, 2, 3, 4}): 1234,
ArrayKey([4]byte{4, 3, 2, 1}): 4321,
}
var testmapIntInt = map[int]int{1: 1, 2: 4, 3: 9} var testmapIntInt = map[int]int{1: 1, 2: 4, 3: 9}
func main() { func main() {
@ -35,6 +42,11 @@ func main() {
println(testmapIntInt[2]) println(testmapIntInt[2])
testmapIntInt[2] = 42 testmapIntInt[2] = 42
println(testmapIntInt[2]) println(testmapIntInt[2])
arrKey := ArrayKey([4]byte{4, 3, 2, 1})
println(testMapArrayKey[arrKey])
testMapArrayKey[arrKey] = 5555
println(testMapArrayKey[arrKey])
} }
func readMap(m map[string]int, key string) { func readMap(m map[string]int, key string) {

2
testdata/map.txt предоставленный
Просмотреть файл

@ -52,3 +52,5 @@ false true 2
true false 0 true false 0
4 4
42 42
4321
5555