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>
|
|
|
|
|
2011-11-30 01:21:52 +01:00
|
|
|
#include "runtime.h"
|
2010-12-03 05:34:57 +01:00
|
|
|
#include "go-alloc.h"
|
2011-09-16 17:47:21 +02:00
|
|
|
#include "go-assert.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. */
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
extern void *mapaccess (struct __go_map_type *, void *, void *)
|
2013-01-24 20:44:23 +01:00
|
|
|
__asm__ (GOSYM_PREFIX "reflect.mapaccess");
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
void *
|
|
|
|
mapaccess (struct __go_map_type *mt, void *m, void *key)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
struct __go_map *map = (struct __go_map *) m;
|
2011-05-20 02:18:15 +02:00
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
__go_assert ((mt->__common.__code & GO_CODE_MASK) == GO_MAP);
|
2011-09-16 17:47:21 +02:00
|
|
|
if (map == NULL)
|
2014-06-05 01:15:33 +02:00
|
|
|
return NULL;
|
2011-09-16 17:47:21 +02:00
|
|
|
else
|
2014-06-05 01:15:33 +02:00
|
|
|
return __go_map_index (map, key, 0);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
extern void mapassign (struct __go_map_type *, void *, void *, void *)
|
2013-01-24 20:44:23 +01:00
|
|
|
__asm__ (GOSYM_PREFIX "reflect.mapassign");
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
void
|
2014-06-05 01:15:33 +02:00
|
|
|
mapassign (struct __go_map_type *mt, void *m, void *key, void *val)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
struct __go_map *map = (struct __go_map *) m;
|
2014-06-05 01:15:33 +02:00
|
|
|
void *p;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
__go_assert ((mt->__common.__code & GO_CODE_MASK) == GO_MAP);
|
2011-05-20 02:18:15 +02:00
|
|
|
if (map == NULL)
|
2011-11-30 01:21:52 +01:00
|
|
|
runtime_panicstring ("assignment to entry in nil map");
|
2014-06-05 01:15:33 +02:00
|
|
|
p = __go_map_index (map, key, 1);
|
|
|
|
__builtin_memcpy (p, val, mt->__val_type->__size);
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
extern void mapdelete (struct __go_map_type *, void *, void *)
|
|
|
|
__asm__ (GOSYM_PREFIX "reflect.mapdelete");
|
2011-05-20 02:18:15 +02:00
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
void
|
|
|
|
mapdelete (struct __go_map_type *mt, void *m, void *key)
|
|
|
|
{
|
|
|
|
struct __go_map *map = (struct __go_map *) m;
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
__go_assert ((mt->__common.__code & GO_CODE_MASK) == GO_MAP);
|
2014-06-05 01:15:33 +02:00
|
|
|
if (map == NULL)
|
|
|
|
return;
|
|
|
|
__go_map_delete (map, key);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
extern int32_t maplen (void *) __asm__ (GOSYM_PREFIX "reflect.maplen");
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
int32_t
|
2014-06-05 01:15:33 +02:00
|
|
|
maplen (void *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;
|
|
|
|
}
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
extern unsigned char *mapiterinit (struct __go_map_type *, void *)
|
2013-01-24 20:44:23 +01:00
|
|
|
__asm__ (GOSYM_PREFIX "reflect.mapiterinit");
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
unsigned char *
|
2014-06-05 01:15:33 +02:00
|
|
|
mapiterinit (struct __go_map_type *mt, void *m)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
struct __go_hash_iter *it;
|
|
|
|
|
2015-01-15 01:27:56 +01:00
|
|
|
__go_assert ((mt->__common.__code & GO_CODE_MASK) == GO_MAP);
|
2010-12-03 05:34:57 +01:00
|
|
|
it = __go_alloc (sizeof (struct __go_hash_iter));
|
|
|
|
__go_mapiterinit ((struct __go_map *) m, it);
|
|
|
|
return (unsigned char *) it;
|
|
|
|
}
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
extern void mapiternext (void *) __asm__ (GOSYM_PREFIX "reflect.mapiternext");
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
void
|
2014-06-05 01:15:33 +02:00
|
|
|
mapiternext (void *it)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
__go_mapiternext ((struct __go_hash_iter *) it);
|
|
|
|
}
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
extern void *mapiterkey (void *) __asm__ (GOSYM_PREFIX "reflect.mapiterkey");
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
void *
|
|
|
|
mapiterkey (void *ita)
|
2010-12-03 05:34:57 +01:00
|
|
|
{
|
|
|
|
struct __go_hash_iter *it = (struct __go_hash_iter *) ita;
|
2014-06-05 01:15:33 +02:00
|
|
|
const struct __go_type_descriptor *key_descriptor;
|
|
|
|
void *key;
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
if (it->entry == NULL)
|
2014-06-05 01:15:33 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
key_descriptor = it->map->__descriptor->__map_descriptor->__key_type;
|
|
|
|
key = __go_alloc (key_descriptor->__size);
|
|
|
|
__go_mapiter1 (it, key);
|
|
|
|
return key;
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make a new map. We have to build our own map descriptor. */
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
extern struct __go_map *makemap (const struct __go_map_type *)
|
2013-01-24 20:44:23 +01:00
|
|
|
__asm__ (GOSYM_PREFIX "reflect.makemap");
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
struct __go_map *
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-06-05 01:15:33 +02:00
|
|
|
return __go_new_map (md, 0);
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2013-07-16 08:54:42 +02:00
|
|
|
|
|
|
|
extern _Bool ismapkey (const struct __go_type_descriptor *)
|
|
|
|
__asm__ (GOSYM_PREFIX "reflect.ismapkey");
|
|
|
|
|
|
|
|
_Bool
|
|
|
|
ismapkey (const struct __go_type_descriptor *typ)
|
|
|
|
{
|
|
|
|
return typ != NULL && typ->__hashfn != __go_type_hash_error;
|
|
|
|
}
|