Copy implements the ExprHelper interface method by producing a copy of the input Expr value with a fresh set of numeric identifiers the Expr and all its descendants.
(expr ast.Expr)
| 374 | // Copy implements the ExprHelper interface method by producing a copy of the input Expr value |
| 375 | // with a fresh set of numeric identifiers the Expr and all its descendants. |
| 376 | func (e *exprHelper) Copy(expr ast.Expr) ast.Expr { |
| 377 | offsetRange, _ := e.parserHelper.sourceInfo.GetOffsetRange(expr.ID()) |
| 378 | copyID := e.parserHelper.newID(offsetRange) |
| 379 | switch expr.Kind() { |
| 380 | case ast.LiteralKind: |
| 381 | return e.exprFactory.NewLiteral(copyID, expr.AsLiteral()) |
| 382 | case ast.IdentKind: |
| 383 | return e.exprFactory.NewIdent(copyID, expr.AsIdent()) |
| 384 | case ast.SelectKind: |
| 385 | sel := expr.AsSelect() |
| 386 | op := e.Copy(sel.Operand()) |
| 387 | if sel.IsTestOnly() { |
| 388 | return e.exprFactory.NewPresenceTest(copyID, op, sel.FieldName()) |
| 389 | } |
| 390 | return e.exprFactory.NewSelect(copyID, op, sel.FieldName()) |
| 391 | case ast.CallKind: |
| 392 | call := expr.AsCall() |
| 393 | args := call.Args() |
| 394 | argsCopy := make([]ast.Expr, len(args)) |
| 395 | for i, arg := range args { |
| 396 | argsCopy[i] = e.Copy(arg) |
| 397 | } |
| 398 | if !call.IsMemberFunction() { |
| 399 | return e.exprFactory.NewCall(copyID, call.FunctionName(), argsCopy...) |
| 400 | } |
| 401 | return e.exprFactory.NewMemberCall(copyID, call.FunctionName(), e.Copy(call.Target()), argsCopy...) |
| 402 | case ast.ListKind: |
| 403 | list := expr.AsList() |
| 404 | elems := list.Elements() |
| 405 | elemsCopy := make([]ast.Expr, len(elems)) |
| 406 | for i, elem := range elems { |
| 407 | elemsCopy[i] = e.Copy(elem) |
| 408 | } |
| 409 | return e.exprFactory.NewList(copyID, elemsCopy, list.OptionalIndices()) |
| 410 | case ast.MapKind: |
| 411 | m := expr.AsMap() |
| 412 | entries := m.Entries() |
| 413 | entriesCopy := make([]ast.EntryExpr, len(entries)) |
| 414 | for i, en := range entries { |
| 415 | entry := en.AsMapEntry() |
| 416 | entryID := e.nextMacroID() |
| 417 | entriesCopy[i] = e.exprFactory.NewMapEntry(entryID, |
| 418 | e.Copy(entry.Key()), e.Copy(entry.Value()), entry.IsOptional()) |
| 419 | } |
| 420 | return e.exprFactory.NewMap(copyID, entriesCopy) |
| 421 | case ast.StructKind: |
| 422 | s := expr.AsStruct() |
| 423 | fields := s.Fields() |
| 424 | fieldsCopy := make([]ast.EntryExpr, len(fields)) |
| 425 | for i, f := range fields { |
| 426 | field := f.AsStructField() |
| 427 | fieldID := e.nextMacroID() |
| 428 | fieldsCopy[i] = e.exprFactory.NewStructField(fieldID, |
| 429 | field.Name(), e.Copy(field.Value()), field.IsOptional()) |
| 430 | } |
| 431 | return e.exprFactory.NewStruct(copyID, s.TypeName(), fieldsCopy) |
| 432 | case ast.ComprehensionKind: |
| 433 | compre := expr.AsComprehension() |
nothing calls this directly
no test coverage detected