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)
| 333 | // Copy implements the ExprHelper interface method by producing a copy of the input Expr value |
| 334 | // with a fresh set of numeric identifiers the Expr and all its descendants. |
| 335 | func (e *exprHelper) Copy(expr ast.Expr) ast.Expr { |
| 336 | offsetRange, _ := e.parserHelper.sourceInfo.GetOffsetRange(expr.ID()) |
| 337 | copyID := e.parserHelper.newID(offsetRange) |
| 338 | switch expr.Kind() { |
| 339 | case ast.LiteralKind: |
| 340 | return e.exprFactory.NewLiteral(copyID, expr.AsLiteral()) |
| 341 | case ast.IdentKind: |
| 342 | return e.exprFactory.NewIdent(copyID, expr.AsIdent()) |
| 343 | case ast.SelectKind: |
| 344 | sel := expr.AsSelect() |
| 345 | op := e.Copy(sel.Operand()) |
| 346 | if sel.IsTestOnly() { |
| 347 | return e.exprFactory.NewPresenceTest(copyID, op, sel.FieldName()) |
| 348 | } |
| 349 | return e.exprFactory.NewSelect(copyID, op, sel.FieldName()) |
| 350 | case ast.CallKind: |
| 351 | call := expr.AsCall() |
| 352 | args := call.Args() |
| 353 | argsCopy := make([]ast.Expr, len(args)) |
| 354 | for i, arg := range args { |
| 355 | argsCopy[i] = e.Copy(arg) |
| 356 | } |
| 357 | if !call.IsMemberFunction() { |
| 358 | return e.exprFactory.NewCall(copyID, call.FunctionName(), argsCopy...) |
| 359 | } |
| 360 | return e.exprFactory.NewMemberCall(copyID, call.FunctionName(), e.Copy(call.Target()), argsCopy...) |
| 361 | case ast.ListKind: |
| 362 | list := expr.AsList() |
| 363 | elems := list.Elements() |
| 364 | elemsCopy := make([]ast.Expr, len(elems)) |
| 365 | for i, elem := range elems { |
| 366 | elemsCopy[i] = e.Copy(elem) |
| 367 | } |
| 368 | return e.exprFactory.NewList(copyID, elemsCopy, list.OptionalIndices()) |
| 369 | case ast.MapKind: |
| 370 | m := expr.AsMap() |
| 371 | entries := m.Entries() |
| 372 | entriesCopy := make([]ast.EntryExpr, len(entries)) |
| 373 | for i, en := range entries { |
| 374 | entry := en.AsMapEntry() |
| 375 | entryID := e.nextMacroID() |
| 376 | entriesCopy[i] = e.exprFactory.NewMapEntry(entryID, |
| 377 | e.Copy(entry.Key()), e.Copy(entry.Value()), entry.IsOptional()) |
| 378 | } |
| 379 | return e.exprFactory.NewMap(copyID, entriesCopy) |
| 380 | case ast.StructKind: |
| 381 | s := expr.AsStruct() |
| 382 | fields := s.Fields() |
| 383 | fieldsCopy := make([]ast.EntryExpr, len(fields)) |
| 384 | for i, f := range fields { |
| 385 | field := f.AsStructField() |
| 386 | fieldID := e.nextMacroID() |
| 387 | fieldsCopy[i] = e.exprFactory.NewStructField(fieldID, |
| 388 | field.Name(), e.Copy(field.Value()), field.IsOptional()) |
| 389 | } |
| 390 | return e.exprFactory.NewStruct(copyID, s.TypeName(), fieldsCopy) |
| 391 | case ast.ComprehensionKind: |
| 392 | compre := expr.AsComprehension() |
nothing calls this directly
no test coverage detected