1
2
3
4
5 package types2
6
7 import (
8 "cmd/compile/internal/syntax"
9 . "internal/types/errors"
10 "strconv"
11 )
12
13
14
15
16
17 type Struct struct {
18 fields []*Var
19 tags []string
20 }
21
22
23
24
25
26 func NewStruct(fields []*Var, tags []string) *Struct {
27 var fset objset
28 for _, f := range fields {
29 if f.name != "_" && fset.insert(f) != nil {
30 panic("multiple fields with the same name")
31 }
32 }
33 if len(tags) > len(fields) {
34 panic("more tags than fields")
35 }
36 s := &Struct{fields: fields, tags: tags}
37 s.markComplete()
38 return s
39 }
40
41
42 func (s *Struct) NumFields() int { return len(s.fields) }
43
44
45 func (s *Struct) Field(i int) *Var { return s.fields[i] }
46
47
48 func (s *Struct) Tag(i int) string {
49 if i < len(s.tags) {
50 return s.tags[i]
51 }
52 return ""
53 }
54
55 func (s *Struct) Underlying() Type { return s }
56 func (s *Struct) String() string { return TypeString(s, nil) }
57
58
59
60
61 func (s *Struct) markComplete() {
62 if s.fields == nil {
63 s.fields = make([]*Var, 0)
64 }
65 }
66
67 func (check *Checker) structType(styp *Struct, e *syntax.StructType) {
68 if e.FieldList == nil {
69 styp.markComplete()
70 return
71 }
72
73
74 var fields []*Var
75 var tags []string
76
77
78 var fset objset
79
80
81 var typ Type
82 var tag string
83 add := func(ident *syntax.Name, embedded bool) {
84 if tag != "" && tags == nil {
85 tags = make([]string, len(fields))
86 }
87 if tags != nil {
88 tags = append(tags, tag)
89 }
90
91 pos := ident.Pos()
92 name := ident.Value
93 fld := NewField(pos, check.pkg, name, typ, embedded)
94
95 if name == "_" || check.declareInSet(&fset, pos, fld) {
96 fields = append(fields, fld)
97 check.recordDef(ident, fld)
98 }
99 }
100
101
102
103
104
105 addInvalid := func(ident *syntax.Name) {
106 typ = Typ[Invalid]
107 tag = ""
108 add(ident, true)
109 }
110
111 var prev syntax.Expr
112 for i, f := range e.FieldList {
113
114
115 if i == 0 || f.Type != prev {
116 typ = check.varType(f.Type)
117 prev = f.Type
118 }
119 tag = ""
120 if i < len(e.TagList) {
121 tag = check.tag(e.TagList[i])
122 }
123 if f.Name != nil {
124
125 add(f.Name, false)
126 } else {
127
128
129
130
131 pos := syntax.StartPos(f.Type)
132 name := embeddedFieldIdent(f.Type)
133 if name == nil {
134 check.errorf(pos, InvalidSyntaxTree, "invalid embedded field type %s", f.Type)
135 name = syntax.NewName(pos, "_")
136 addInvalid(name)
137 continue
138 }
139 add(name, true)
140
141
142
143
144
145 embeddedTyp := typ
146 embeddedPos := pos
147 check.later(func() {
148 t, isPtr := deref(embeddedTyp)
149 switch u := under(t).(type) {
150 case *Basic:
151 if !isValid(t) {
152
153 return
154 }
155
156 if u.kind == UnsafePointer {
157 check.error(embeddedPos, InvalidPtrEmbed, "embedded field type cannot be unsafe.Pointer")
158 }
159 case *Pointer:
160 check.error(embeddedPos, InvalidPtrEmbed, "embedded field type cannot be a pointer")
161 case *Interface:
162 if isTypeParam(t) {
163
164
165
166 check.error(embeddedPos, MisplacedTypeParam, "embedded field type cannot be a (pointer to a) type parameter")
167 break
168 }
169 if isPtr {
170 check.error(embeddedPos, InvalidPtrEmbed, "embedded field type cannot be a pointer to an interface")
171 }
172 }
173 }).describef(embeddedPos, "check embedded type %s", embeddedTyp)
174 }
175 }
176
177 styp.fields = fields
178 styp.tags = tags
179 styp.markComplete()
180 }
181
182 func embeddedFieldIdent(e syntax.Expr) *syntax.Name {
183 switch e := e.(type) {
184 case *syntax.Name:
185 return e
186 case *syntax.Operation:
187 if base := ptrBase(e); base != nil {
188
189 if op, _ := base.(*syntax.Operation); op == nil || ptrBase(op) == nil {
190 return embeddedFieldIdent(e.X)
191 }
192 }
193 case *syntax.SelectorExpr:
194 return e.Sel
195 case *syntax.IndexExpr:
196 return embeddedFieldIdent(e.X)
197 }
198 return nil
199 }
200
201 func (check *Checker) declareInSet(oset *objset, pos syntax.Pos, obj Object) bool {
202 if alt := oset.insert(obj); alt != nil {
203 err := check.newError(DuplicateDecl)
204 err.addf(pos, "%s redeclared", obj.Name())
205 err.addAltDecl(alt)
206 err.report()
207 return false
208 }
209 return true
210 }
211
212 func (check *Checker) tag(t *syntax.BasicLit) string {
213
214 if t != nil && !t.Bad {
215 if t.Kind == syntax.StringLit {
216 if val, err := strconv.Unquote(t.Value); err == nil {
217 return val
218 }
219 }
220 check.errorf(t, InvalidSyntaxTree, "incorrect tag syntax: %q", t.Value)
221 }
222 return ""
223 }
224
225 func ptrBase(x *syntax.Operation) syntax.Expr {
226 if x.Op == syntax.Mul && x.Y == nil {
227 return x.X
228 }
229 return nil
230 }
231
View as plain text