evaluateStruct converts a struct expression to an SSA value.
(expr *expression.Expression)
| 10 | |
| 11 | // evaluateStruct converts a struct expression to an SSA value. |
| 12 | func (f *Function) evaluateStruct(expr *expression.Expression) (ssa.Value, error) { |
| 13 | if expr.Children[0].Token.Kind == token.Call && expr.Children[0].Children[0].Token.Kind == token.New { |
| 14 | return f.evaluateNewStruct(expr) |
| 15 | } |
| 16 | |
| 17 | typ, err := f.Env.TypeFromToken(expr.Children[0].Token, f.File) |
| 18 | |
| 19 | if err != nil { |
| 20 | return nil, err |
| 21 | } |
| 22 | |
| 23 | structType, isStructType := typ.(*types.Struct) |
| 24 | |
| 25 | if !isStructType { |
| 26 | return nil, errors.New(&NotDataStruct{TypeName: typ.Name()}, f.File, expr.Children[0].Source()) |
| 27 | } |
| 28 | |
| 29 | structure := &ssa.Struct{ |
| 30 | Typ: structType, |
| 31 | Arguments: make(ssa.Arguments, len(structType.Fields)), |
| 32 | Source: expr.Source(), |
| 33 | } |
| 34 | |
| 35 | for _, definition := range expr.Children[1:] { |
| 36 | if isTrailing(definition, expr.Children) { |
| 37 | continue |
| 38 | } |
| 39 | |
| 40 | field, rightValue, err := f.extractField(structType, definition) |
| 41 | |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | structure.Arguments[field.Index] = rightValue |
| 47 | } |
| 48 | |
| 49 | return structure, nil |
| 50 | } |
no test coverage detected