2010-12-03 05:34:57 +01:00
|
|
|
/* go-reflect-map.c -- map reflection support for Go.
|
|
|
|
|
|
|
|
Copyright 2009, 2010 The Go Authors. All rights reserved.
|
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file. */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "go-alloc.h"
|
2011-05-20 02:18:15 +02:00
|
|
|
#include "go-panic.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "go-type.h"
|
|
|
|
#include "map.h"
|
|
|
|
|
|
|
|
/* This file implements support for reflection on maps. These
|
|
|
|
functions are called from reflect/value.go. */
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
struct mapaccess_ret
|
|
|
|
{
|
|
|
|
uintptr_t val;
|
|
|
|
_Bool pres;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern struct mapaccess_ret mapaccess (uintptr_t, uintptr_t)
|
2010-12-03 05:34:57 +01:00
|
|
|
asm ("libgo_reflect.reflect.mapaccess");
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
struct mapaccess_ret
|
|
|
|
mapaccess (uintptr_t m, uintptr_t key_i)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
struct __go_map *map = (struct __go_map *) m;
|
2011-05-20 02:18:15 +02:00
|
|
|
void *key;
|
|
|
|
const struct __go_type_descriptor *key_descriptor;
|
2010-12-03 05:34:57 +01:00
|
|
|
void *p;
|
|
|
|
const struct __go_type_descriptor *val_descriptor;
|
2011-05-20 02:18:15 +02:00
|
|
|
struct mapaccess_ret ret;
|
|
|
|
void *val;
|
|
|
|
void *pv;
|
|
|
|
|
|
|
|
if (map == NULL)
|
|
|
|
__go_panic_msg ("lookup in nil map");
|
|
|
|
|
|
|
|
key_descriptor = map->__descriptor->__map_descriptor->__key_type;
|
|
|
|
if (__go_is_pointer_type (key_descriptor))
|
|
|
|
key = &key_i;
|
|
|
|
else
|
|
|
|
key = (void *) key_i;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
p = __go_map_index (map, key, 0);
|
2011-05-20 02:18:15 +02:00
|
|
|
|
|
|
|
val_descriptor = map->__descriptor->__map_descriptor->__val_type;
|
|
|
|
if (__go_is_pointer_type (val_descriptor))
|
|
|
|
{
|
|
|
|
val = NULL;
|
|
|
|
pv = &val;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
val = __go_alloc (val_descriptor->__size);
|
|
|
|
pv = val;
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
if (p == NULL)
|
2011-05-20 02:18:15 +02:00
|
|
|
ret.pres = 0;
|
2010-12-03 05:34:57 +01:00
|
|
|
else
|
|
|
|
{
|
2011-05-20 02:18:15 +02:00
|
|
|
__builtin_memcpy (pv, p, val_descriptor->__size);
|
|
|
|
ret.pres = 1;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
|
|
|
|
ret.val = (uintptr_t) val;
|
|
|
|
return ret;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
extern void mapassign (uintptr_t, uintptr_t, uintptr_t, _Bool)
|
2010-12-03 05:34:57 +01:00
|
|
|
asm ("libgo_reflect.reflect.mapassign");
|
|
|
|
|
|
|
|
void
|
2011-05-20 02:18:15 +02:00
|
|
|
mapassign (uintptr_t m, uintptr_t key_i, uintptr_t val_i, _Bool pres)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
struct __go_map *map = (struct __go_map *) m;
|
2011-05-20 02:18:15 +02:00
|
|
|
const struct __go_type_descriptor *key_descriptor;
|
|
|
|
void *key;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
if (map == NULL)
|
|
|
|
__go_panic_msg ("lookup in nil map");
|
|
|
|
|
|
|
|
key_descriptor = map->__descriptor->__map_descriptor->__key_type;
|
|
|
|
if (__go_is_pointer_type (key_descriptor))
|
|
|
|
key = &key_i;
|
|
|
|
else
|
|
|
|
key = (void *) key_i;
|
|
|
|
|
|
|
|
if (!pres)
|
2010-12-03 05:34:57 +01:00
|
|
|
__go_map_delete (map, key);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
const struct __go_type_descriptor *val_descriptor;
|
2011-05-20 02:18:15 +02:00
|
|
|
void *pv;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
p = __go_map_index (map, key, 1);
|
2011-05-20 02:18:15 +02:00
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
val_descriptor = map->__descriptor->__map_descriptor->__val_type;
|
2011-05-20 02:18:15 +02:00
|
|
|
if (__go_is_pointer_type (val_descriptor))
|
|
|
|
pv = &val_i;
|
|
|
|
else
|
|
|
|
pv = (void *) val_i;
|
|
|
|
__builtin_memcpy (p, pv, val_descriptor->__size);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
extern int32_t maplen (uintptr_t)
|
2010-12-03 05:34:57 +01:00
|
|
|
asm ("libgo_reflect.reflect.maplen");
|
|
|
|
|
|
|
|
int32_t
|
2011-05-20 02:18:15 +02:00
|
|
|
maplen (uintptr_t m)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
struct __go_map *map = (struct __go_map *) m;
|
2011-05-20 02:18:15 +02:00
|
|
|
|
|
|
|
if (map == NULL)
|
|
|
|
return 0;
|
2010-12-03 05:34:57 +01:00
|
|
|
return (int32_t) map->__element_count;
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
extern unsigned char *mapiterinit (uintptr_t)
|
2010-12-03 05:34:57 +01:00
|
|
|
asm ("libgo_reflect.reflect.mapiterinit");
|
|
|
|
|
|
|
|
unsigned char *
|
2011-05-20 02:18:15 +02:00
|
|
|
mapiterinit (uintptr_t m)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
struct __go_hash_iter *it;
|
|
|
|
|
|
|
|
it = __go_alloc (sizeof (struct __go_hash_iter));
|
|
|
|
__go_mapiterinit ((struct __go_map *) m, it);
|
|
|
|
return (unsigned char *) it;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void mapiternext (unsigned char *)
|
|
|
|
asm ("libgo_reflect.reflect.mapiternext");
|
|
|
|
|
|
|
|
void
|
|
|
|
mapiternext (unsigned char *it)
|
|
|
|
{
|
|
|
|
__go_mapiternext ((struct __go_hash_iter *) it);
|
|
|
|
}
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
struct mapiterkey_ret
|
|
|
|
{
|
|
|
|
uintptr_t key;
|
|
|
|
_Bool ok;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern struct mapiterkey_ret mapiterkey (unsigned char *)
|
2010-12-03 05:34:57 +01:00
|
|
|
asm ("libgo_reflect.reflect.mapiterkey");
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
struct mapiterkey_ret
|
|
|
|
mapiterkey (unsigned char *ita)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
struct __go_hash_iter *it = (struct __go_hash_iter *) ita;
|
2011-05-20 02:18:15 +02:00
|
|
|
struct mapiterkey_ret ret;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
if (it->entry == NULL)
|
2011-05-20 02:18:15 +02:00
|
|
|
{
|
|
|
|
ret.key = 0;
|
|
|
|
ret.ok = 0;
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
else
|
|
|
|
{
|
2011-05-20 02:18:15 +02:00
|
|
|
const struct __go_type_descriptor *key_descriptor;
|
|
|
|
void *key;
|
|
|
|
void *pk;
|
|
|
|
|
|
|
|
key_descriptor = it->map->__descriptor->__map_descriptor->__key_type;
|
|
|
|
if (__go_is_pointer_type (key_descriptor))
|
|
|
|
{
|
|
|
|
key = NULL;
|
|
|
|
pk = &key;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
key = __go_alloc (key_descriptor->__size);
|
|
|
|
pk = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
__go_mapiter1 (it, pk);
|
|
|
|
|
|
|
|
ret.key = (uintptr_t) key;
|
|
|
|
ret.ok = 1;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
|
|
|
|
return ret;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make a new map. We have to build our own map descriptor. */
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
extern uintptr_t makemap (const struct __go_map_type *)
|
2010-12-03 05:34:57 +01:00
|
|
|
asm ("libgo_reflect.reflect.makemap");
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
uintptr_t
|
2010-12-03 05:34:57 +01:00
|
|
|
makemap (const struct __go_map_type *t)
|
|
|
|
{
|
|
|
|
struct __go_map_descriptor *md;
|
|
|
|
unsigned int o;
|
|
|
|
const struct __go_type_descriptor *kt;
|
|
|
|
const struct __go_type_descriptor *vt;
|
2011-05-20 02:18:15 +02:00
|
|
|
struct __go_map* map;
|
|
|
|
void *ret;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
/* FIXME: Reference count. */
|
|
|
|
md = (struct __go_map_descriptor *) __go_alloc (sizeof (*md));
|
|
|
|
md->__map_descriptor = t;
|
|
|
|
o = sizeof (void *);
|
|
|
|
kt = t->__key_type;
|
|
|
|
o = (o + kt->__field_align - 1) & ~ (kt->__field_align - 1);
|
|
|
|
md->__key_offset = o;
|
|
|
|
o += kt->__size;
|
|
|
|
vt = t->__val_type;
|
|
|
|
o = (o + vt->__field_align - 1) & ~ (vt->__field_align - 1);
|
|
|
|
md->__val_offset = o;
|
|
|
|
o += vt->__size;
|
|
|
|
o = (o + sizeof (void *) - 1) & ~ (sizeof (void *) - 1);
|
|
|
|
o = (o + kt->__field_align - 1) & ~ (kt->__field_align - 1);
|
|
|
|
o = (o + vt->__field_align - 1) & ~ (vt->__field_align - 1);
|
|
|
|
md->__entry_size = o;
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
map = __go_new_map (md, 0);
|
|
|
|
|
|
|
|
ret = __go_alloc (sizeof (void *));
|
|
|
|
__builtin_memcpy (ret, &map, sizeof (void *));
|
|
|
|
return (uintptr_t) ret;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|