evaluateArray converts a array indexing expression to an SSA value.
(expr *expression.Expression)
| 10 | |
| 11 | // evaluateArray converts a array indexing expression to an SSA value. |
| 12 | func (f *Function) evaluateArray(expr *expression.Expression) (ssa.Value, error) { |
| 13 | address := expr.Children[0] |
| 14 | addressValue, err := f.evaluateRight(address) |
| 15 | |
| 16 | if err != nil { |
| 17 | return nil, err |
| 18 | } |
| 19 | |
| 20 | addressValue, addressType, length, err := f.decomposeSlice(addressValue) |
| 21 | |
| 22 | if err != nil { |
| 23 | return nil, err |
| 24 | } |
| 25 | |
| 26 | pointer, isPointer := types.Unwrap(addressType).(*types.Pointer) |
| 27 | |
| 28 | if !isPointer { |
| 29 | return nil, errors.New(&TypeNotIndexable{TypeName: addressType.Name()}, f.File, address.Source()) |
| 30 | } |
| 31 | |
| 32 | var indexValue ssa.Value |
| 33 | |
| 34 | if len(expr.Children) > 1 { |
| 35 | index := expr.Children[1] |
| 36 | |
| 37 | if index.Token.Kind == token.Range { |
| 38 | return f.evaluateSlice(expr, index, addressValue, length) |
| 39 | } else { |
| 40 | indexValue, err = f.evaluateRight(index) |
| 41 | |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | if !types.Is(indexValue.Type(), types.AnyInt) { |
| 47 | return nil, errors.New(&TypeMismatch{Encountered: indexValue.Type().Name(), Expected: types.AnyInt.Name()}, f.File, index.Source()) |
| 48 | } |
| 49 | } |
| 50 | } else { |
| 51 | indexValue = f.Append(&ssa.Int{Int: 0}) |
| 52 | } |
| 53 | |
| 54 | memory := &ssa.Memory{ |
| 55 | Address: addressValue, |
| 56 | Index: indexValue, |
| 57 | Scale: true, |
| 58 | Typ: pointer.To, |
| 59 | Source: expr.Source(), |
| 60 | } |
| 61 | |
| 62 | return memory, nil |
| 63 | } |
no test coverage detected