blob: 63752d84de4f67a8509c7320a7ac4163724ed814 [file] [log] [blame]
Rob Pikefac3dfe2008-10-22 11:02:56 -07001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Reflection library.
6// Handling values.
7
8package reflect
9
10import (
11 "reflect";
Rob Pike50d06952008-12-09 15:41:21 -080012 "unsafe";
Rob Pikefac3dfe2008-10-22 11:02:56 -070013)
14
Rob Pikeed2ac9b2009-01-16 12:48:07 -080015export type Addr unsafe.pointer
Rob Pikea45f9472008-11-04 22:54:11 -080016
Rob Pikeed2ac9b2009-01-16 12:48:07 -080017func equalType(a, b Type) bool {
Russ Coxd0e30cd2008-12-10 15:55:59 -080018 return a.String() == b.String()
19}
20
Rob Pikea45f9472008-11-04 22:54:11 -080021export type Value interface {
22 Kind() int;
23 Type() Type;
Rob Pikeb1d37b72008-11-12 14:19:39 -080024 Addr() Addr;
Russ Cox387df5e2008-11-24 14:51:33 -080025 Interface() interface {};
Rob Pikea45f9472008-11-04 22:54:11 -080026}
27
Russ Coxb54133d2009-01-15 16:16:42 -080028// commonValue fields and functionality for all values
Rob Pikea45f9472008-11-04 22:54:11 -080029
Russ Coxb54133d2009-01-15 16:16:42 -080030type commonValue struct {
Rob Pikea45f9472008-11-04 22:54:11 -080031 kind int;
32 typ Type;
33 addr Addr;
34}
35
Russ Coxb54133d2009-01-15 16:16:42 -080036func (c *commonValue) Kind() int {
Rob Pikea45f9472008-11-04 22:54:11 -080037 return c.kind
38}
39
Russ Coxb54133d2009-01-15 16:16:42 -080040func (c *commonValue) Type() Type {
Rob Pikea45f9472008-11-04 22:54:11 -080041 return c.typ
42}
43
Russ Coxb54133d2009-01-15 16:16:42 -080044func (c *commonValue) Addr() Addr {
Rob Pikeb1d37b72008-11-12 14:19:39 -080045 return c.addr
46}
47
Russ Coxb54133d2009-01-15 16:16:42 -080048func (c *commonValue) Interface() interface {} {
Russ Cox484ba932009-01-09 00:17:46 -080049 var i interface {};
50 if c.typ.Size() > 8 { // TODO(rsc): how do we know it is 8?
51 i = sys.unreflect(c.addr.(uintptr).(uint64), c.typ.String(), true);
52 } else {
53 if uintptr(c.addr) == 0 {
54 panicln("reflect: address 0 for", c.typ.String());
55 }
56 i = sys.unreflect(uint64(uintptr(*c.addr.(*Addr))), c.typ.String(), false);
Russ Coxba882f92008-12-19 03:06:19 -080057 }
Russ Cox484ba932009-01-09 00:17:46 -080058 return i;
Rob Pikea45f9472008-11-04 22:54:11 -080059}
60
Rob Pikeed2ac9b2009-01-16 12:48:07 -080061func newValueAddr(typ Type, addr Addr) Value
Rob Pikea45f9472008-11-04 22:54:11 -080062
Rob Pikeed2ac9b2009-01-16 12:48:07 -080063type creatorFn *(typ Type, addr Addr) Value
Rob Pikea45f9472008-11-04 22:54:11 -080064
65
Rob Pike178e37e2008-11-02 12:32:14 -080066// -- Missing
67
68export type MissingValue interface {
69 Kind() int;
70 Type() Type;
Rob Pikeac09eb42008-12-11 12:59:49 -080071 Addr() Addr;
Rob Pike178e37e2008-11-02 12:32:14 -080072}
73
Rob Pikeed2ac9b2009-01-16 12:48:07 -080074type missingValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -080075 commonValue
Rob Pike178e37e2008-11-02 12:32:14 -080076}
77
Rob Pikeed2ac9b2009-01-16 12:48:07 -080078func missingCreator(typ Type, addr Addr) Value {
79 return &missingValueStruct{ commonValue{MissingKind, typ, addr} }
Rob Pike178e37e2008-11-02 12:32:14 -080080}
81
Rob Pike282493b2008-10-29 15:31:02 -070082// -- Int
83
84export type IntValue interface {
85 Kind() int;
86 Get() int;
Russ Coxe4f4ab02008-11-13 13:42:59 -080087 Set(int);
Rob Pike282493b2008-10-29 15:31:02 -070088 Type() Type;
89}
90
Rob Pikeed2ac9b2009-01-16 12:48:07 -080091type intValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -080092 commonValue
Rob Pike282493b2008-10-29 15:31:02 -070093}
94
Rob Pikeed2ac9b2009-01-16 12:48:07 -080095func intCreator(typ Type, addr Addr) Value {
96 return &intValueStruct{ commonValue{IntKind, typ, addr} }
Rob Pike282493b2008-10-29 15:31:02 -070097}
98
Rob Pikeed2ac9b2009-01-16 12:48:07 -080099func (v *intValueStruct) Get() int {
Rob Pike50d06952008-12-09 15:41:21 -0800100 return *v.addr.(*int)
Rob Pike282493b2008-10-29 15:31:02 -0700101}
102
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800103func (v *intValueStruct) Set(i int) {
Rob Pike50d06952008-12-09 15:41:21 -0800104 *v.addr.(*int) = i
Rob Pike282493b2008-10-29 15:31:02 -0700105}
106
Rob Pikefac3dfe2008-10-22 11:02:56 -0700107// -- Int8
108
109export type Int8Value interface {
110 Kind() int;
111 Get() int8;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800112 Set(int8);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700113 Type() Type;
114}
115
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800116type int8ValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800117 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700118}
119
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800120func int8Creator(typ Type, addr Addr) Value {
121 return &int8ValueStruct{ commonValue{Int8Kind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700122}
123
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800124func (v *int8ValueStruct) Get() int8 {
Rob Pike50d06952008-12-09 15:41:21 -0800125 return *v.addr.(*int8)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700126}
127
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800128func (v *int8ValueStruct) Set(i int8) {
Rob Pike50d06952008-12-09 15:41:21 -0800129 *v.addr.(*int8) = i
Rob Pikefac3dfe2008-10-22 11:02:56 -0700130}
131
Rob Pikefac3dfe2008-10-22 11:02:56 -0700132// -- Int16
133
134export type Int16Value interface {
135 Kind() int;
136 Get() int16;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800137 Set(int16);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700138 Type() Type;
139}
140
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800141type int16ValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800142 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700143}
144
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800145func int16Creator(typ Type, addr Addr) Value {
146 return &int16ValueStruct{ commonValue{Int16Kind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700147}
148
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800149func (v *int16ValueStruct) Get() int16 {
Rob Pike50d06952008-12-09 15:41:21 -0800150 return *v.addr.(*int16)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700151}
152
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800153func (v *int16ValueStruct) Set(i int16) {
Rob Pike50d06952008-12-09 15:41:21 -0800154 *v.addr.(*int16) = i
Rob Pikefac3dfe2008-10-22 11:02:56 -0700155}
156
Rob Pikefac3dfe2008-10-22 11:02:56 -0700157// -- Int32
158
159export type Int32Value interface {
160 Kind() int;
161 Get() int32;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800162 Set(int32);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700163 Type() Type;
164}
165
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800166type int32ValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800167 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700168}
169
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800170func int32Creator(typ Type, addr Addr) Value {
171 return &int32ValueStruct{ commonValue{Int32Kind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700172}
173
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800174func (v *int32ValueStruct) Get() int32 {
Rob Pike50d06952008-12-09 15:41:21 -0800175 return *v.addr.(*int32)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700176}
177
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800178func (v *int32ValueStruct) Set(i int32) {
Rob Pike50d06952008-12-09 15:41:21 -0800179 *v.addr.(*int32) = i
Rob Pikefac3dfe2008-10-22 11:02:56 -0700180}
181
Rob Pikefac3dfe2008-10-22 11:02:56 -0700182// -- Int64
183
184export type Int64Value interface {
185 Kind() int;
186 Get() int64;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800187 Set(int64);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700188 Type() Type;
189}
190
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800191type int64ValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800192 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700193}
194
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800195func int64Creator(typ Type, addr Addr) Value {
196 return &int64ValueStruct{ commonValue{Int64Kind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700197}
198
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800199func (v *int64ValueStruct) Get() int64 {
Rob Pike50d06952008-12-09 15:41:21 -0800200 return *v.addr.(*int64)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700201}
202
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800203func (v *int64ValueStruct) Set(i int64) {
Rob Pike50d06952008-12-09 15:41:21 -0800204 *v.addr.(*int64) = i
Rob Pikefac3dfe2008-10-22 11:02:56 -0700205}
206
Rob Pike282493b2008-10-29 15:31:02 -0700207// -- Uint
208
209export type UintValue interface {
210 Kind() int;
211 Get() uint;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800212 Set(uint);
Rob Pike282493b2008-10-29 15:31:02 -0700213 Type() Type;
214}
215
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800216type uintValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800217 commonValue
Rob Pike282493b2008-10-29 15:31:02 -0700218}
219
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800220func uintCreator(typ Type, addr Addr) Value {
221 return &uintValueStruct{ commonValue{UintKind, typ, addr} }
Rob Pike282493b2008-10-29 15:31:02 -0700222}
223
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800224func (v *uintValueStruct) Get() uint {
Rob Pike50d06952008-12-09 15:41:21 -0800225 return *v.addr.(*uint)
Rob Pike282493b2008-10-29 15:31:02 -0700226}
227
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800228func (v *uintValueStruct) Set(i uint) {
Rob Pike50d06952008-12-09 15:41:21 -0800229 *v.addr.(*uint) = i
Rob Pike282493b2008-10-29 15:31:02 -0700230}
231
Rob Pikefac3dfe2008-10-22 11:02:56 -0700232// -- Uint8
233
234export type Uint8Value interface {
235 Kind() int;
236 Get() uint8;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800237 Set(uint8);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700238 Type() Type;
239}
240
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800241type uint8ValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800242 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700243}
244
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800245func uint8Creator(typ Type, addr Addr) Value {
246 return &uint8ValueStruct{ commonValue{Uint8Kind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700247}
248
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800249func (v *uint8ValueStruct) Get() uint8 {
Rob Pike50d06952008-12-09 15:41:21 -0800250 return *v.addr.(*uint8)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700251}
252
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800253func (v *uint8ValueStruct) Set(i uint8) {
Rob Pike50d06952008-12-09 15:41:21 -0800254 *v.addr.(*uint8) = i
Rob Pikefac3dfe2008-10-22 11:02:56 -0700255}
256
Rob Pikefac3dfe2008-10-22 11:02:56 -0700257// -- Uint16
258
259export type Uint16Value interface {
260 Kind() int;
261 Get() uint16;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800262 Set(uint16);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700263 Type() Type;
264}
265
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800266type uint16ValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800267 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700268}
269
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800270func uint16Creator(typ Type, addr Addr) Value {
271 return &uint16ValueStruct{ commonValue{Uint16Kind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700272}
273
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800274func (v *uint16ValueStruct) Get() uint16 {
Rob Pike50d06952008-12-09 15:41:21 -0800275 return *v.addr.(*uint16)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700276}
277
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800278func (v *uint16ValueStruct) Set(i uint16) {
Rob Pike50d06952008-12-09 15:41:21 -0800279 *v.addr.(*uint16) = i
Rob Pikefac3dfe2008-10-22 11:02:56 -0700280}
281
Rob Pikefac3dfe2008-10-22 11:02:56 -0700282// -- Uint32
283
284export type Uint32Value interface {
285 Kind() int;
286 Get() uint32;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800287 Set(uint32);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700288 Type() Type;
289}
290
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800291type uint32ValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800292 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700293}
294
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800295func uint32Creator(typ Type, addr Addr) Value {
296 return &uint32ValueStruct{ commonValue{Uint32Kind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700297}
298
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800299func (v *uint32ValueStruct) Get() uint32 {
Rob Pike50d06952008-12-09 15:41:21 -0800300 return *v.addr.(*uint32)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700301}
302
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800303func (v *uint32ValueStruct) Set(i uint32) {
Rob Pike50d06952008-12-09 15:41:21 -0800304 *v.addr.(*uint32) = i
Rob Pikefac3dfe2008-10-22 11:02:56 -0700305}
306
Rob Pikefac3dfe2008-10-22 11:02:56 -0700307// -- Uint64
308
309export type Uint64Value interface {
310 Kind() int;
311 Get() uint64;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800312 Set(uint64);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700313 Type() Type;
314}
315
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800316type uint64ValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800317 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700318}
319
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800320func uint64Creator(typ Type, addr Addr) Value {
321 return &uint64ValueStruct{ commonValue{Uint64Kind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700322}
323
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800324func (v *uint64ValueStruct) Get() uint64 {
Rob Pike50d06952008-12-09 15:41:21 -0800325 return *v.addr.(*uint64)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700326}
327
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800328func (v *uint64ValueStruct) Set(i uint64) {
Rob Pike50d06952008-12-09 15:41:21 -0800329 *v.addr.(*uint64) = i
Rob Pikefac3dfe2008-10-22 11:02:56 -0700330}
331
Rob Pike9ba97ca2008-12-11 14:41:12 -0800332// -- Uintptr
333
334export type UintptrValue interface {
335 Kind() int;
336 Get() uintptr;
337 Set(uintptr);
338 Type() Type;
339}
340
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800341type uintptrValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800342 commonValue
Rob Pike9ba97ca2008-12-11 14:41:12 -0800343}
344
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800345func uintptrCreator(typ Type, addr Addr) Value {
346 return &uintptrValueStruct{ commonValue{UintptrKind, typ, addr} }
Rob Pike9ba97ca2008-12-11 14:41:12 -0800347}
348
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800349func (v *uintptrValueStruct) Get() uintptr {
Rob Pike9ba97ca2008-12-11 14:41:12 -0800350 return *v.addr.(*uintptr)
351}
352
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800353func (v *uintptrValueStruct) Set(i uintptr) {
Rob Pike9ba97ca2008-12-11 14:41:12 -0800354 *v.addr.(*uintptr) = i
355}
356
Rob Pike282493b2008-10-29 15:31:02 -0700357// -- Float
358
359export type FloatValue interface {
360 Kind() int;
361 Get() float;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800362 Set(float);
Rob Pike282493b2008-10-29 15:31:02 -0700363 Type() Type;
364}
365
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800366type floatValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800367 commonValue
Rob Pike282493b2008-10-29 15:31:02 -0700368}
369
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800370func floatCreator(typ Type, addr Addr) Value {
371 return &floatValueStruct{ commonValue{FloatKind, typ, addr} }
Rob Pike282493b2008-10-29 15:31:02 -0700372}
373
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800374func (v *floatValueStruct) Get() float {
Rob Pike50d06952008-12-09 15:41:21 -0800375 return *v.addr.(*float)
Rob Pike282493b2008-10-29 15:31:02 -0700376}
377
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800378func (v *floatValueStruct) Set(f float) {
Rob Pike50d06952008-12-09 15:41:21 -0800379 *v.addr.(*float) = f
Rob Pike282493b2008-10-29 15:31:02 -0700380}
381
Rob Pikefac3dfe2008-10-22 11:02:56 -0700382// -- Float32
383
384export type Float32Value interface {
385 Kind() int;
386 Get() float32;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800387 Set(float32);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700388 Type() Type;
389}
390
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800391type float32ValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800392 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700393}
394
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800395func float32Creator(typ Type, addr Addr) Value {
396 return &float32ValueStruct{ commonValue{Float32Kind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700397}
398
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800399func (v *float32ValueStruct) Get() float32 {
Rob Pike50d06952008-12-09 15:41:21 -0800400 return *v.addr.(*float32)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700401}
402
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800403func (v *float32ValueStruct) Set(f float32) {
Rob Pike50d06952008-12-09 15:41:21 -0800404 *v.addr.(*float32) = f
Rob Pikefac3dfe2008-10-22 11:02:56 -0700405}
406
Rob Pikefac3dfe2008-10-22 11:02:56 -0700407// -- Float64
408
409export type Float64Value interface {
410 Kind() int;
411 Get() float64;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800412 Set(float64);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700413 Type() Type;
414}
415
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800416type float64ValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800417 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700418}
419
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800420func float64Creator(typ Type, addr Addr) Value {
421 return &float64ValueStruct{ commonValue{Float64Kind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700422}
423
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800424func (v *float64ValueStruct) Get() float64 {
Rob Pike50d06952008-12-09 15:41:21 -0800425 return *v.addr.(*float64)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700426}
427
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800428func (v *float64ValueStruct) Set(f float64) {
Rob Pike50d06952008-12-09 15:41:21 -0800429 *v.addr.(*float64) = f
Rob Pikefac3dfe2008-10-22 11:02:56 -0700430}
431
Rob Pikefac3dfe2008-10-22 11:02:56 -0700432// -- Float80
433
434export type Float80Value interface {
435 Kind() int;
436 Get() float80;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800437 Set(float80);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700438 Type() Type;
439}
440
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800441type float80ValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800442 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700443}
444
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800445func float80Creator(typ Type, addr Addr) Value {
446 return &float80ValueStruct{ commonValue{Float80Kind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700447}
448
449/*
450BUG: can't gen code for float80s
451func (v *Float80ValueStruct) Get() float80 {
Rob Pike50d06952008-12-09 15:41:21 -0800452 return *v.addr.(*float80)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700453}
454
Russ Coxe4f4ab02008-11-13 13:42:59 -0800455func (v *Float80ValueStruct) Set(f float80) {
Rob Pike50d06952008-12-09 15:41:21 -0800456 *v.addr.(*float80) = f
Rob Pikefac3dfe2008-10-22 11:02:56 -0700457}
458*/
459
Rob Pikefac3dfe2008-10-22 11:02:56 -0700460// -- String
461
462export type StringValue interface {
463 Kind() int;
464 Get() string;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800465 Set(string);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700466 Type() Type;
467}
468
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800469type stringValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800470 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700471}
472
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800473func stringCreator(typ Type, addr Addr) Value {
474 return &stringValueStruct{ commonValue{StringKind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700475}
476
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800477func (v *stringValueStruct) Get() string {
Rob Pike50d06952008-12-09 15:41:21 -0800478 return *v.addr.(*string)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700479}
480
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800481func (v *stringValueStruct) Set(s string) {
Rob Pike50d06952008-12-09 15:41:21 -0800482 *v.addr.(*string) = s
Rob Pikefac3dfe2008-10-22 11:02:56 -0700483}
484
Ian Lance Taylor16fd3562008-10-31 16:34:47 -0700485// -- Bool
486
487export type BoolValue interface {
488 Kind() int;
489 Get() bool;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800490 Set(bool);
Ian Lance Taylor16fd3562008-10-31 16:34:47 -0700491 Type() Type;
492}
493
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800494type boolValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800495 commonValue
Ian Lance Taylor16fd3562008-10-31 16:34:47 -0700496}
497
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800498func boolCreator(typ Type, addr Addr) Value {
499 return &boolValueStruct{ commonValue{BoolKind, typ, addr} }
Ian Lance Taylor16fd3562008-10-31 16:34:47 -0700500}
501
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800502func (v *boolValueStruct) Get() bool {
Rob Pike50d06952008-12-09 15:41:21 -0800503 return *v.addr.(*bool)
Ian Lance Taylor16fd3562008-10-31 16:34:47 -0700504}
505
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800506func (v *boolValueStruct) Set(b bool) {
Rob Pike50d06952008-12-09 15:41:21 -0800507 *v.addr.(*bool) = b
Ian Lance Taylor16fd3562008-10-31 16:34:47 -0700508}
509
Rob Pikefac3dfe2008-10-22 11:02:56 -0700510// -- Pointer
511
512export type PtrValue interface {
513 Kind() int;
Rob Pikefac3dfe2008-10-22 11:02:56 -0700514 Type() Type;
Rob Pike418b97c2008-10-24 16:33:29 -0700515 Sub() Value;
516 Get() Addr;
Rob Pikeb1d37b72008-11-12 14:19:39 -0800517 SetSub(Value);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700518}
519
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800520type ptrValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800521 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700522}
523
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800524func (v *ptrValueStruct) Get() Addr {
Rob Pike50d06952008-12-09 15:41:21 -0800525 return *v.addr.(*Addr)
Rob Pikefac3dfe2008-10-22 11:02:56 -0700526}
527
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800528func (v *ptrValueStruct) Sub() Value {
529 return newValueAddr(v.typ.(PtrType).Sub(), v.Get());
Rob Pikefac3dfe2008-10-22 11:02:56 -0700530}
531
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800532func (v *ptrValueStruct) SetSub(subv Value) {
Russ Coxd0e30cd2008-12-10 15:55:59 -0800533 a := v.typ.(PtrType).Sub();
534 b := subv.Type();
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800535 if !equalType(a, b) {
Russ Coxd0e30cd2008-12-10 15:55:59 -0800536 panicln("reflect: incompatible types in PtrValue.SetSub:",
537 a.String(), b.String());
Rob Pike419e1e02008-11-12 19:05:05 -0800538 }
Rob Pike50d06952008-12-09 15:41:21 -0800539 *v.addr.(*Addr) = subv.Addr();
Rob Pikeb1d37b72008-11-12 14:19:39 -0800540}
541
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800542func ptrCreator(typ Type, addr Addr) Value {
543 return &ptrValueStruct{ commonValue{PtrKind, typ, addr} };
Rob Pikefac3dfe2008-10-22 11:02:56 -0700544}
545
Rob Pikec4af3e72008-10-26 08:28:33 -0700546// -- Array
Rob Pikefac3dfe2008-10-22 11:02:56 -0700547
548export type ArrayValue interface {
549 Kind() int;
550 Type() Type;
551 Open() bool;
Rob Pike554d0aa2008-11-05 10:17:38 -0800552 Len() int;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800553 Cap() int;
Rob Pike554d0aa2008-11-05 10:17:38 -0800554 Elem(i int) Value;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800555 SetLen(len int);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700556}
Rob Pikec4af3e72008-10-26 08:28:33 -0700557
Rob Pike34b88732008-10-22 16:48:17 -0700558/*
559 Run-time representation of open arrays looks like this:
560 struct Array {
561 byte* array; // actual data
562 uint32 nel; // number of elements
Russ Coxe4f4ab02008-11-13 13:42:59 -0800563 uint32 cap;
Rob Pike34b88732008-10-22 16:48:17 -0700564 };
565*/
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800566type runtimeArray struct {
Russ Coxe4f4ab02008-11-13 13:42:59 -0800567 data Addr;
568 len uint32;
569 cap uint32;
570}
571
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800572type openArrayValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800573 commonValue;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800574 elemtype Type;
575 elemsize int;
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800576 array *runtimeArray;
Russ Coxe4f4ab02008-11-13 13:42:59 -0800577}
Rob Pikefac3dfe2008-10-22 11:02:56 -0700578
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800579func (v *openArrayValueStruct) Open() bool {
Rob Pikefac3dfe2008-10-22 11:02:56 -0700580 return true
581}
582
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800583func (v *openArrayValueStruct) Len() int {
Russ Coxe4f4ab02008-11-13 13:42:59 -0800584 return int(v.array.len);
585}
586
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800587func (v *openArrayValueStruct) Cap() int {
Russ Coxe4f4ab02008-11-13 13:42:59 -0800588 return int(v.array.cap);
589}
590
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800591func (v *openArrayValueStruct) SetLen(len int) {
Russ Coxe4f4ab02008-11-13 13:42:59 -0800592 if len > v.Cap() {
593 panicln("reflect: OpenArrayValueStruct.SetLen", len, v.Cap());
594 }
595 v.array.len = uint32(len);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700596}
597
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800598func (v *openArrayValueStruct) Elem(i int) Value {
Rob Pike50d06952008-12-09 15:41:21 -0800599 data_uint := uintptr(v.array.data) + uintptr(i * v.elemsize);
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800600 return newValueAddr(v.elemtype, Addr(data_uint));
Rob Pikefac3dfe2008-10-22 11:02:56 -0700601}
602
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800603type fixedArrayValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800604 commonValue;
Rob Pikefac3dfe2008-10-22 11:02:56 -0700605 elemtype Type;
Rob Pike554d0aa2008-11-05 10:17:38 -0800606 elemsize int;
607 len int;
Rob Pikefac3dfe2008-10-22 11:02:56 -0700608}
609
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800610func (v *fixedArrayValueStruct) Open() bool {
Rob Pikefac3dfe2008-10-22 11:02:56 -0700611 return false
612}
613
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800614func (v *fixedArrayValueStruct) Len() int {
Rob Pikefac3dfe2008-10-22 11:02:56 -0700615 return v.len
616}
617
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800618func (v *fixedArrayValueStruct) Cap() int {
Russ Coxe4f4ab02008-11-13 13:42:59 -0800619 return v.len
620}
621
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800622func (v *fixedArrayValueStruct) SetLen(len int) {
Russ Coxe4f4ab02008-11-13 13:42:59 -0800623}
624
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800625func (v *fixedArrayValueStruct) Elem(i int) Value {
Rob Pike50d06952008-12-09 15:41:21 -0800626 data_uint := uintptr(v.addr) + uintptr(i * v.elemsize);
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800627 return newValueAddr(v.elemtype, Addr(data_uint));
Rob Pikefac3dfe2008-10-22 11:02:56 -0700628 return nil
629}
630
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800631func arrayCreator(typ Type, addr Addr) Value {
Rob Pikefac3dfe2008-10-22 11:02:56 -0700632 arraytype := typ.(ArrayType);
633 if arraytype.Open() {
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800634 v := new(openArrayValueStruct);
Rob Pikec4af3e72008-10-26 08:28:33 -0700635 v.kind = ArrayKind;
Rob Pike34b88732008-10-22 16:48:17 -0700636 v.addr = addr;
Rob Pikefac3dfe2008-10-22 11:02:56 -0700637 v.typ = typ;
Rob Pike34b88732008-10-22 16:48:17 -0700638 v.elemtype = arraytype.Elem();
639 v.elemsize = v.elemtype.Size();
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800640 v.array = addr.(*runtimeArray);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700641 return v;
642 }
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800643 v := new(fixedArrayValueStruct);
Rob Pikec4af3e72008-10-26 08:28:33 -0700644 v.kind = ArrayKind;
Rob Pike34b88732008-10-22 16:48:17 -0700645 v.addr = addr;
Rob Pikefac3dfe2008-10-22 11:02:56 -0700646 v.typ = typ;
647 v.elemtype = arraytype.Elem();
Rob Pike34b88732008-10-22 16:48:17 -0700648 v.elemsize = v.elemtype.Size();
649 v.len = arraytype.Len();
Rob Pikefac3dfe2008-10-22 11:02:56 -0700650 return v;
651}
652
653// -- Map TODO: finish and test
654
655export type MapValue interface {
656 Kind() int;
657 Type() Type;
658 Len() int;
659 Elem(key Value) Value;
660}
661
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800662type mapValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800663 commonValue
Russ Coxde137272008-10-23 12:41:06 -0700664}
665
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800666func mapCreator(typ Type, addr Addr) Value {
667 return &mapValueStruct{ commonValue{MapKind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700668}
669
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800670func (v *mapValueStruct) Len() int {
Russ Coxde137272008-10-23 12:41:06 -0700671 return 0 // TODO: probably want this to be dynamic
Rob Pikefac3dfe2008-10-22 11:02:56 -0700672}
673
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800674func (v *mapValueStruct) Elem(key Value) Value {
Rob Pikefac3dfe2008-10-22 11:02:56 -0700675 panic("map value element");
676 return nil
677}
678
Rob Pikefac3dfe2008-10-22 11:02:56 -0700679// -- Chan
680
681export type ChanValue interface {
682 Kind() int;
683 Type() Type;
684}
685
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800686type chanValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800687 commonValue
Russ Coxde137272008-10-23 12:41:06 -0700688}
689
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800690func chanCreator(typ Type, addr Addr) Value {
691 return &chanValueStruct{ commonValue{ChanKind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700692}
693
Rob Pikefac3dfe2008-10-22 11:02:56 -0700694// -- Struct
695
696export type StructValue interface {
697 Kind() int;
698 Type() Type;
699 Len() int;
700 Field(i int) Value;
701}
702
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800703type structValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800704 commonValue;
Russ Coxd47d8882008-12-18 22:37:22 -0800705 field []Value;
Rob Pikefac3dfe2008-10-22 11:02:56 -0700706}
707
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800708func (v *structValueStruct) Len() int {
Rob Pikefac3dfe2008-10-22 11:02:56 -0700709 return len(v.field)
710}
711
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800712func (v *structValueStruct) Field(i int) Value {
Rob Pikefac3dfe2008-10-22 11:02:56 -0700713 return v.field[i]
714}
715
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800716func structCreator(typ Type, addr Addr) Value {
Rob Pikefac3dfe2008-10-22 11:02:56 -0700717 t := typ.(StructType);
Rob Pikefac3dfe2008-10-22 11:02:56 -0700718 nfield := t.Len();
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800719 v := &structValueStruct{ commonValue{StructKind, typ, addr}, make([]Value, nfield) };
Rob Pikefac3dfe2008-10-22 11:02:56 -0700720 for i := 0; i < nfield; i++ {
Rob Pike12a34352008-10-30 17:29:53 -0700721 name, ftype, str, offset := t.Field(i);
Rob Pike50d06952008-12-09 15:41:21 -0800722 addr_uint := uintptr(addr) + uintptr(offset);
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800723 v.field[i] = newValueAddr(ftype, Addr(addr_uint));
Rob Pikefac3dfe2008-10-22 11:02:56 -0700724 }
725 v.typ = typ;
726 return v;
727}
728
729// -- Interface
730
731export type InterfaceValue interface {
732 Kind() int;
733 Type() Type;
Russ Cox387df5e2008-11-24 14:51:33 -0800734 Get() interface {};
Rob Pikefac3dfe2008-10-22 11:02:56 -0700735}
736
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800737type interfaceValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800738 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700739}
740
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800741func (v *interfaceValueStruct) Get() interface{} {
Rob Pike50d06952008-12-09 15:41:21 -0800742 return *v.addr.(*interface{})
Russ Cox387df5e2008-11-24 14:51:33 -0800743}
744
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800745func interfaceCreator(typ Type, addr Addr) Value {
746 return &interfaceValueStruct{ commonValue{InterfaceKind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700747}
748
Rob Pikefac3dfe2008-10-22 11:02:56 -0700749// -- Func
750
751export type FuncValue interface {
752 Kind() int;
753 Type() Type;
754}
755
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800756type funcValueStruct struct {
Russ Coxb54133d2009-01-15 16:16:42 -0800757 commonValue
Rob Pikefac3dfe2008-10-22 11:02:56 -0700758}
759
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800760func funcCreator(typ Type, addr Addr) Value {
761 return &funcValueStruct{ commonValue{FuncKind, typ, addr} }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700762}
763
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800764var creator = map[int] creatorFn {
765 MissingKind : &missingCreator,
766 IntKind : &intCreator,
767 Int8Kind : &int8Creator,
768 Int16Kind : &int16Creator,
769 Int32Kind : &int32Creator,
770 Int64Kind : &int64Creator,
771 UintKind : &uintCreator,
772 Uint8Kind : &uint8Creator,
773 Uint16Kind : &uint16Creator,
774 Uint32Kind : &uint32Creator,
775 Uint64Kind : &uint64Creator,
776 UintptrKind : &uintptrCreator,
777 FloatKind : &floatCreator,
778 Float32Kind : &float32Creator,
779 Float64Kind : &float64Creator,
780 Float80Kind : &float80Creator,
781 StringKind : &stringCreator,
782 BoolKind : &boolCreator,
783 PtrKind : &ptrCreator,
784 ArrayKind : &arrayCreator,
785 MapKind : &mapCreator,
786 ChanKind : &chanCreator,
787 StructKind : &structCreator,
788 InterfaceKind : &interfaceCreator,
789 FuncKind : &funcCreator,
Rob Pikefac3dfe2008-10-22 11:02:56 -0700790}
791
Russ Cox484ba932009-01-09 00:17:46 -0800792var typecache = make(map[string] Type);
Rob Pike9ba97ca2008-12-11 14:41:12 -0800793
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800794func newValueAddr(typ Type, addr Addr) Value {
Rob Pikefac3dfe2008-10-22 11:02:56 -0700795 c, ok := creator[typ.Kind()];
796 if !ok {
797 panicln("no creator for type" , typ.Kind());
798 }
799 return c(typ, addr);
800}
801
Rob Pikefac3dfe2008-10-22 11:02:56 -0700802export func NewInitValue(typ Type) Value {
Rob Pike34b88732008-10-22 16:48:17 -0700803 // Some values cannot be made this way.
804 switch typ.Kind() {
Russ Coxba882f92008-12-19 03:06:19 -0800805 case FuncKind: // must be pointers, at least for now (TODO?)
Rob Pike34b88732008-10-22 16:48:17 -0700806 return nil;
807 case ArrayKind:
808 if typ.(ArrayType).Open() {
809 return nil
810 }
811 }
Rob Pikefac3dfe2008-10-22 11:02:56 -0700812 size := typ.Size();
813 if size == 0 {
814 size = 1;
815 }
Russ Cox55645042009-01-06 15:19:02 -0800816 data := make([]uint8, size);
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800817 return newValueAddr(typ, Addr(&data[0]));
Rob Pikefac3dfe2008-10-22 11:02:56 -0700818}
819
Russ Coxe4f4ab02008-11-13 13:42:59 -0800820/*
821 Run-time representation of open arrays looks like this:
822 struct Array {
823 byte* array; // actual data
824 uint32 nel; // number of elements
825 uint32 cap; // allocated number of elements
826 };
827*/
828export func NewOpenArrayValue(typ ArrayType, len, cap int) ArrayValue {
829 if !typ.Open() {
830 return nil
831 }
832
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800833 array := new(runtimeArray);
Russ Coxe4f4ab02008-11-13 13:42:59 -0800834 size := typ.Elem().Size() * cap;
835 if size == 0 {
836 size = 1;
837 }
Russ Cox55645042009-01-06 15:19:02 -0800838 data := make([]uint8, size);
Rob Pike50d06952008-12-09 15:41:21 -0800839 array.data = Addr(&data[0]);
Russ Coxe4f4ab02008-11-13 13:42:59 -0800840 array.len = uint32(len);
841 array.cap = uint32(cap);
842
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800843 return newValueAddr(typ, Addr(array));
Russ Coxe4f4ab02008-11-13 13:42:59 -0800844}
845
Russ Coxd0e30cd2008-12-10 15:55:59 -0800846export func CopyArray(dst ArrayValue, src ArrayValue, n int) {
847 if n == 0 {
848 return
849 }
850 dt := dst.Type().(ArrayType).Elem();
851 st := src.Type().(ArrayType).Elem();
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800852 if !equalType(dt, st) {
Russ Coxd0e30cd2008-12-10 15:55:59 -0800853 panicln("reflect: incompatible types in CopyArray:",
854 dt.String(), st.String());
855 }
856 if n < 0 || n > dst.Len() || n > src.Len() {
857 panicln("reflect: CopyArray: invalid count", n);
858 }
859 dstp := uintptr(dst.Elem(0).Addr());
860 srcp := uintptr(src.Elem(0).Addr());
861 end := uintptr(n)*uintptr(dt.Size());
Russ Coxd47d8882008-12-18 22:37:22 -0800862 if end % 8 == 0 {
Russ Coxd0e30cd2008-12-10 15:55:59 -0800863 for i := uintptr(0); i < end; i += 8{
864 di := Addr(dstp + i);
865 si := Addr(srcp + i);
866 *di.(*uint64) = *si.(*uint64);
867 }
868 } else {
869 for i := uintptr(0); i < end; i++ {
870 di := Addr(dstp + i);
871 si := Addr(srcp + i);
872 *di.(*byte) = *si.(*byte);
873 }
874 }
875}
876
877
Russ Cox387df5e2008-11-24 14:51:33 -0800878export func NewValue(e interface {}) Value {
Russ Cox484ba932009-01-09 00:17:46 -0800879 value, typestring, indir := sys.reflect(e);
880 typ, ok := typecache[typestring];
Rob Pike842e1a92008-11-10 14:53:40 -0800881 if !ok {
Russ Cox484ba932009-01-09 00:17:46 -0800882 typ = ParseTypeString("", typestring);
883 typecache[typestring] = typ;
Rob Pike842e1a92008-11-10 14:53:40 -0800884 }
Russ Cox484ba932009-01-09 00:17:46 -0800885
886 if indir {
887 // Content of interface is a pointer.
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800888 return newValueAddr(typ, value.(uintptr).(Addr));
Russ Cox484ba932009-01-09 00:17:46 -0800889 }
890
891 // Content of interface is a value;
892 // need a permanent copy to take its address.
Russ Cox55645042009-01-06 15:19:02 -0800893 ap := new(uint64);
Rob Pike34b88732008-10-22 16:48:17 -0700894 *ap = value;
Rob Pikeed2ac9b2009-01-16 12:48:07 -0800895 return newValueAddr(typ, ap.(Addr));
Rob Pikefac3dfe2008-10-22 11:02:56 -0700896}