1 /* go-reflect-map.c -- map reflection support for Go.
3 Copyright 2009, 2010 The Go Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file. */
15 /* This file implements support for reflection on maps. These
16 functions are called from reflect/value.go. */
24 extern struct mapaccess_ret
mapaccess (uintptr_t, uintptr_t)
25 asm ("libgo_reflect.reflect.mapaccess");
28 mapaccess (uintptr_t m
, uintptr_t key_i
)
30 struct __go_map
*map
= (struct __go_map
*) m
;
32 const struct __go_type_descriptor
*key_descriptor
;
34 const struct __go_type_descriptor
*val_descriptor
;
35 struct mapaccess_ret ret
;
40 __go_panic_msg ("lookup in nil map");
42 key_descriptor
= map
->__descriptor
->__map_descriptor
->__key_type
;
43 if (__go_is_pointer_type (key_descriptor
))
48 p
= __go_map_index (map
, key
, 0);
50 val_descriptor
= map
->__descriptor
->__map_descriptor
->__val_type
;
51 if (__go_is_pointer_type (val_descriptor
))
58 val
= __go_alloc (val_descriptor
->__size
);
66 __builtin_memcpy (pv
, p
, val_descriptor
->__size
);
70 ret
.val
= (uintptr_t) val
;
74 extern void mapassign (uintptr_t, uintptr_t, uintptr_t, _Bool
)
75 asm ("libgo_reflect.reflect.mapassign");
78 mapassign (uintptr_t m
, uintptr_t key_i
, uintptr_t val_i
, _Bool pres
)
80 struct __go_map
*map
= (struct __go_map
*) m
;
81 const struct __go_type_descriptor
*key_descriptor
;
85 __go_panic_msg ("lookup in nil map");
87 key_descriptor
= map
->__descriptor
->__map_descriptor
->__key_type
;
88 if (__go_is_pointer_type (key_descriptor
))
94 __go_map_delete (map
, key
);
98 const struct __go_type_descriptor
*val_descriptor
;
101 p
= __go_map_index (map
, key
, 1);
103 val_descriptor
= map
->__descriptor
->__map_descriptor
->__val_type
;
104 if (__go_is_pointer_type (val_descriptor
))
108 __builtin_memcpy (p
, pv
, val_descriptor
->__size
);
112 extern int32_t maplen (uintptr_t)
113 asm ("libgo_reflect.reflect.maplen");
118 struct __go_map
*map
= (struct __go_map
*) m
;
122 return (int32_t) map
->__element_count
;
125 extern unsigned char *mapiterinit (uintptr_t)
126 asm ("libgo_reflect.reflect.mapiterinit");
129 mapiterinit (uintptr_t m
)
131 struct __go_hash_iter
*it
;
133 it
= __go_alloc (sizeof (struct __go_hash_iter
));
134 __go_mapiterinit ((struct __go_map
*) m
, it
);
135 return (unsigned char *) it
;
138 extern void mapiternext (unsigned char *)
139 asm ("libgo_reflect.reflect.mapiternext");
142 mapiternext (unsigned char *it
)
144 __go_mapiternext ((struct __go_hash_iter
*) it
);
147 struct mapiterkey_ret
153 extern struct mapiterkey_ret
mapiterkey (unsigned char *)
154 asm ("libgo_reflect.reflect.mapiterkey");
156 struct mapiterkey_ret
157 mapiterkey (unsigned char *ita
)
159 struct __go_hash_iter
*it
= (struct __go_hash_iter
*) ita
;
160 struct mapiterkey_ret ret
;
162 if (it
->entry
== NULL
)
169 const struct __go_type_descriptor
*key_descriptor
;
173 key_descriptor
= it
->map
->__descriptor
->__map_descriptor
->__key_type
;
174 if (__go_is_pointer_type (key_descriptor
))
181 key
= __go_alloc (key_descriptor
->__size
);
185 __go_mapiter1 (it
, pk
);
187 ret
.key
= (uintptr_t) key
;
194 /* Make a new map. We have to build our own map descriptor. */
196 extern uintptr_t makemap (const struct __go_map_type
*)
197 asm ("libgo_reflect.reflect.makemap");
200 makemap (const struct __go_map_type
*t
)
202 struct __go_map_descriptor
*md
;
204 const struct __go_type_descriptor
*kt
;
205 const struct __go_type_descriptor
*vt
;
206 struct __go_map
* map
;
209 /* FIXME: Reference count. */
210 md
= (struct __go_map_descriptor
*) __go_alloc (sizeof (*md
));
211 md
->__map_descriptor
= t
;
214 o
= (o
+ kt
->__field_align
- 1) & ~ (kt
->__field_align
- 1);
215 md
->__key_offset
= o
;
218 o
= (o
+ vt
->__field_align
- 1) & ~ (vt
->__field_align
- 1);
219 md
->__val_offset
= o
;
221 o
= (o
+ sizeof (void *) - 1) & ~ (sizeof (void *) - 1);
222 o
= (o
+ kt
->__field_align
- 1) & ~ (kt
->__field_align
- 1);
223 o
= (o
+ vt
->__field_align
- 1) & ~ (vt
->__field_align
- 1);
224 md
->__entry_size
= o
;
226 map
= __go_new_map (md
, 0);
228 ret
= __go_alloc (sizeof (void *));
229 __builtin_memcpy (ret
, &map
, sizeof (void *));
230 return (uintptr_t) ret
;