Source file
src/go/types/index.go
1
2
3
4
5
6
7 package types
8
9 import (
10 "go/ast"
11 "go/constant"
12 "go/token"
13 . "internal/types/errors"
14 )
15
16
17
18
19 func (check *Checker) indexExpr(x *operand, e *indexedExpr) (isFuncInst bool) {
20 check.exprOrType(x, e.x, true)
21
22
23 switch x.mode {
24 case invalid:
25 check.use(e.indices...)
26 return false
27
28 case typexpr:
29
30 x.mode = invalid
31
32 x.typ = check.varType(e.orig)
33 if isValid(x.typ) {
34 x.mode = typexpr
35 }
36 return false
37
38 case value:
39 if sig, _ := under(x.typ).(*Signature); sig != nil && sig.TypeParams().Len() > 0 {
40
41 return true
42 }
43 }
44
45
46 check.nonGeneric(nil, x)
47 if x.mode == invalid {
48 return false
49 }
50
51
52 valid := false
53 length := int64(-1)
54 switch typ := under(x.typ).(type) {
55 case *Basic:
56 if isString(typ) {
57 valid = true
58 if x.mode == constant_ {
59 length = int64(len(constant.StringVal(x.val)))
60 }
61
62
63
64 x.mode = value
65 x.typ = universeByte
66 }
67
68 case *Array:
69 valid = true
70 length = typ.len
71 if x.mode != variable {
72 x.mode = value
73 }
74 x.typ = typ.elem
75
76 case *Pointer:
77 if typ, _ := under(typ.base).(*Array); typ != nil {
78 valid = true
79 length = typ.len
80 x.mode = variable
81 x.typ = typ.elem
82 }
83
84 case *Slice:
85 valid = true
86 x.mode = variable
87 x.typ = typ.elem
88
89 case *Map:
90 index := check.singleIndex(e)
91 if index == nil {
92 x.mode = invalid
93 return false
94 }
95 var key operand
96 check.expr(nil, &key, index)
97 check.assignment(&key, typ.key, "map index")
98
99 x.mode = mapindex
100 x.typ = typ.elem
101 x.expr = e.orig
102 return false
103
104 case *Interface:
105 if !isTypeParam(x.typ) {
106 break
107 }
108
109 var key, elem Type
110 mode := variable
111
112 if underIs(x.typ, func(u Type) bool {
113 l := int64(-1)
114 var k, e Type
115 switch t := u.(type) {
116 case *Basic:
117 if isString(t) {
118 e = universeByte
119 mode = value
120 }
121 case *Array:
122 l = t.len
123 e = t.elem
124 if x.mode != variable {
125 mode = value
126 }
127 case *Pointer:
128 if t, _ := under(t.base).(*Array); t != nil {
129 l = t.len
130 e = t.elem
131 }
132 case *Slice:
133 e = t.elem
134 case *Map:
135 k = t.key
136 e = t.elem
137 }
138 if e == nil {
139 return false
140 }
141 if elem == nil {
142
143 length = l
144 key, elem = k, e
145 return true
146 }
147
148
149 if !Identical(key, k) {
150 return false
151 }
152
153 if !Identical(elem, e) {
154 return false
155 }
156
157 if l >= 0 && l < length {
158 length = l
159 }
160 return true
161 }) {
162
163 if key != nil {
164 index := check.singleIndex(e)
165 if index == nil {
166 x.mode = invalid
167 return false
168 }
169 var k operand
170 check.expr(nil, &k, index)
171 check.assignment(&k, key, "map index")
172
173 x.mode = mapindex
174 x.typ = elem
175 x.expr = e.orig
176 return false
177 }
178
179
180 valid = true
181 x.mode = mode
182 x.typ = elem
183 }
184 }
185
186 if !valid {
187
188 check.errorf(x, NonIndexableOperand, "cannot index %s", x)
189 check.use(e.indices...)
190 x.mode = invalid
191 return false
192 }
193
194 index := check.singleIndex(e)
195 if index == nil {
196 x.mode = invalid
197 return false
198 }
199
200
201
202
203 if x.typ == nil {
204 x.typ = Typ[Invalid]
205 }
206
207 check.index(index, length)
208 return false
209 }
210
211 func (check *Checker) sliceExpr(x *operand, e *ast.SliceExpr) {
212 check.expr(nil, x, e.X)
213 if x.mode == invalid {
214 check.use(e.Low, e.High, e.Max)
215 return
216 }
217
218
219 var ct, cu Type
220 var hasString bool
221 typeset(x.typ, func(t, u Type) bool {
222 if u == nil {
223 check.errorf(x, NonSliceableOperand, "cannot slice %s: no specific type in %s", x, x.typ)
224 cu = nil
225 return false
226 }
227
228
229 if isString(u) {
230 u = NewSlice(universeByte)
231 hasString = true
232 }
233
234
235 if cu == nil {
236 ct, cu = t, u
237 return true
238 }
239
240
241 if !Identical(cu, u) {
242 check.errorf(x, NonSliceableOperand, "cannot slice %s: %s and %s have different underlying types", x, ct, t)
243 cu = nil
244 return false
245 }
246
247 return true
248 })
249 if hasString {
250
251
252 cu = Typ[String]
253 if !isTypeParam(x.typ) {
254 cu = under(x.typ)
255 }
256 }
257
258 valid := false
259 length := int64(-1)
260 switch u := cu.(type) {
261 case nil:
262
263 x.mode = invalid
264 return
265
266 case *Basic:
267 if isString(u) {
268 if e.Slice3 {
269 at := e.Max
270 if at == nil {
271 at = e
272 }
273 check.error(at, InvalidSliceExpr, invalidOp+"3-index slice of string")
274 x.mode = invalid
275 return
276 }
277 valid = true
278 if x.mode == constant_ {
279 length = int64(len(constant.StringVal(x.val)))
280 }
281
282
283 if isUntyped(x.typ) {
284 x.typ = Typ[String]
285 }
286 }
287
288 case *Array:
289 valid = true
290 length = u.len
291 if x.mode != variable {
292 check.errorf(x, NonSliceableOperand, "cannot slice unaddressable value %s", x)
293 x.mode = invalid
294 return
295 }
296 x.typ = &Slice{elem: u.elem}
297
298 case *Pointer:
299 if u, _ := under(u.base).(*Array); u != nil {
300 valid = true
301 length = u.len
302 x.typ = &Slice{elem: u.elem}
303 }
304
305 case *Slice:
306 valid = true
307
308 }
309
310 if !valid {
311 check.errorf(x, NonSliceableOperand, "cannot slice %s", x)
312 x.mode = invalid
313 return
314 }
315
316 x.mode = value
317
318
319 if e.Slice3 && (e.High == nil || e.Max == nil) {
320 check.error(inNode(e, e.Rbrack), InvalidSyntaxTree, "2nd and 3rd index required in 3-index slice")
321 x.mode = invalid
322 return
323 }
324
325
326 var ind [3]int64
327 for i, expr := range []ast.Expr{e.Low, e.High, e.Max} {
328 x := int64(-1)
329 switch {
330 case expr != nil:
331
332
333
334 max := int64(-1)
335 if length >= 0 {
336 max = length + 1
337 }
338 if _, v := check.index(expr, max); v >= 0 {
339 x = v
340 }
341 case i == 0:
342
343 x = 0
344 case length >= 0:
345
346 x = length
347 }
348 ind[i] = x
349 }
350
351
352
353 L:
354 for i, x := range ind[:len(ind)-1] {
355 if x > 0 {
356 for j, y := range ind[i+1:] {
357 if y >= 0 && y < x {
358
359
360
361 at := []ast.Expr{e.Low, e.High, e.Max}[i+1+j]
362 check.errorf(at, SwappedSliceIndices, "invalid slice indices: %d < %d", y, x)
363 break L
364 }
365 }
366 }
367 }
368 }
369
370
371
372
373 func (check *Checker) singleIndex(expr *indexedExpr) ast.Expr {
374 if len(expr.indices) == 0 {
375 check.errorf(expr.orig, InvalidSyntaxTree, "index expression %v with 0 indices", expr)
376 return nil
377 }
378 if len(expr.indices) > 1 {
379
380 check.error(expr.indices[1], InvalidIndex, invalidOp+"more than one index")
381 }
382 return expr.indices[0]
383 }
384
385
386
387
388
389 func (check *Checker) index(index ast.Expr, max int64) (typ Type, val int64) {
390 typ = Typ[Invalid]
391 val = -1
392
393 var x operand
394 check.expr(nil, &x, index)
395 if !check.isValidIndex(&x, InvalidIndex, "index", false) {
396 return
397 }
398
399 if x.mode != constant_ {
400 return x.typ, -1
401 }
402
403 if x.val.Kind() == constant.Unknown {
404 return
405 }
406
407 v, ok := constant.Int64Val(x.val)
408 assert(ok)
409 if max >= 0 && v >= max {
410 check.errorf(&x, InvalidIndex, invalidArg+"index %s out of bounds [0:%d]", x.val.String(), max)
411 return
412 }
413
414
415 return x.typ, v
416 }
417
418 func (check *Checker) isValidIndex(x *operand, code Code, what string, allowNegative bool) bool {
419 if x.mode == invalid {
420 return false
421 }
422
423
424 check.convertUntyped(x, Typ[Int])
425 if x.mode == invalid {
426 return false
427 }
428
429
430 if !allInteger(x.typ) {
431 check.errorf(x, code, invalidArg+"%s %s must be integer", what, x)
432 return false
433 }
434
435 if x.mode == constant_ {
436
437 if !allowNegative && constant.Sign(x.val) < 0 {
438 check.errorf(x, code, invalidArg+"%s %s must not be negative", what, x)
439 return false
440 }
441
442
443 if !representableConst(x.val, check, Typ[Int], &x.val) {
444 check.errorf(x, code, invalidArg+"%s %s overflows int", what, x)
445 return false
446 }
447 }
448
449 return true
450 }
451
452
453
454
455
456
457
458
459
460
461 type indexedExpr struct {
462 orig ast.Expr
463 x ast.Expr
464 lbrack token.Pos
465 indices []ast.Expr
466 rbrack token.Pos
467 }
468
469 func (x *indexedExpr) Pos() token.Pos {
470 return x.orig.Pos()
471 }
472
473 func unpackIndexedExpr(n ast.Node) *indexedExpr {
474 switch e := n.(type) {
475 case *ast.IndexExpr:
476 return &indexedExpr{
477 orig: e,
478 x: e.X,
479 lbrack: e.Lbrack,
480 indices: []ast.Expr{e.Index},
481 rbrack: e.Rbrack,
482 }
483 case *ast.IndexListExpr:
484 return &indexedExpr{
485 orig: e,
486 x: e.X,
487 lbrack: e.Lbrack,
488 indices: e.Indices,
489 rbrack: e.Rbrack,
490 }
491 }
492 return nil
493 }
494
View as plain text