1// Copyright 2012 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
5package reflect
6
7import "unsafe"
8
9// MakeRO returns a copy of v with the read-only flag set.
10func MakeRO(v Value) Value {
11	v.flag |= flagStickyRO
12	return v
13}
14
15// IsRO reports whether v's read-only flag is set.
16func IsRO(v Value) bool {
17	return v.flag&flagStickyRO != 0
18}
19
20var CallGC = &callGC
21
22const PtrSize = ptrSize
23
24func FuncLayout(t Type, rcvr Type) (frametype Type, argSize, retOffset uintptr, stack []byte, gc []byte, ptrs bool) {
25	var ft *rtype
26	var s *bitVector
27	if rcvr != nil {
28		ft, argSize, retOffset, s, _ = funcLayout(t.(*rtype), rcvr.(*rtype))
29	} else {
30		ft, argSize, retOffset, s, _ = funcLayout(t.(*rtype), nil)
31	}
32	frametype = ft
33	for i := uint32(0); i < s.n; i++ {
34		stack = append(stack, s.data[i/8]>>(i%8)&1)
35	}
36	if ft.kind&kindGCProg != 0 {
37		panic("can't handle gc programs")
38	}
39	gcdata := (*[1000]byte)(unsafe.Pointer(ft.gcdata))
40	for i := uintptr(0); i < ft.ptrdata/ptrSize; i++ {
41		gc = append(gc, gcdata[i/8]>>(i%8)&1)
42	}
43	ptrs = ft.kind&kindNoPointers == 0
44	return
45}
46
47func TypeLinks() []string {
48	var r []string
49	sections, offset := typelinks()
50	for i, offs := range offset {
51		rodata := sections[i]
52		for _, off := range offs {
53			typ := (*rtype)(resolveTypeOff(unsafe.Pointer(rodata), off))
54			r = append(r, typ.String())
55		}
56	}
57	return r
58}
59
60var GCBits = gcbits
61
62func gcbits(interface{}) []byte // provided by runtime
63
64func MapBucketOf(x, y Type) Type {
65	return bucketOf(x.(*rtype), y.(*rtype))
66}
67
68func CachedBucketOf(m Type) Type {
69	t := m.(*rtype)
70	if Kind(t.kind&kindMask) != Map {
71		panic("not map")
72	}
73	tt := (*mapType)(unsafe.Pointer(t))
74	return tt.bucket
75}
76
77type EmbedWithUnexpMeth struct{}
78
79func (EmbedWithUnexpMeth) f() {}
80
81type pinUnexpMeth interface {
82	f()
83}
84
85var pinUnexpMethI = pinUnexpMeth(EmbedWithUnexpMeth{})
86
87func FirstMethodNameBytes(t Type) *byte {
88	_ = pinUnexpMethI
89
90	ut := t.uncommon()
91	if ut == nil {
92		panic("type has no methods")
93	}
94	m := ut.methods()[0]
95	mname := t.(*rtype).nameOff(m.name)
96	if *mname.data(0)&(1<<2) == 0 {
97		panic("method name does not have pkgPath *string")
98	}
99	return mname.bytes
100}
101
102type OtherPkgFields struct {
103	OtherExported   int
104	otherUnexported int
105}
106
107func IsExported(t Type) bool {
108	typ := t.(*rtype)
109	n := typ.nameOff(typ.str)
110	return n.isExported()
111}
112
113func ResolveReflectName(s string) {
114	resolveReflectName(newName(s, "", "", false))
115}
116
117type Buffer struct {
118	buf []byte
119}
120