(e Expr)
| 234 | } |
| 235 | |
| 236 | func (fac *baseExprFactory) CopyExpr(e Expr) Expr { |
| 237 | // unwrap navigable expressions to avoid unnecessary allocations during copying. |
| 238 | if nav, ok := e.(*navigableExprImpl); ok { |
| 239 | e = nav.Expr |
| 240 | } |
| 241 | switch e.Kind() { |
| 242 | case CallKind: |
| 243 | c := e.AsCall() |
| 244 | argsCopy := make([]Expr, len(c.Args())) |
| 245 | for i, arg := range c.Args() { |
| 246 | argsCopy[i] = fac.CopyExpr(arg) |
| 247 | } |
| 248 | if !c.IsMemberFunction() { |
| 249 | return fac.NewCall(e.ID(), c.FunctionName(), argsCopy...) |
| 250 | } |
| 251 | return fac.NewMemberCall(e.ID(), c.FunctionName(), fac.CopyExpr(c.Target()), argsCopy...) |
| 252 | case ComprehensionKind: |
| 253 | compre := e.AsComprehension() |
| 254 | return fac.NewComprehensionTwoVar(e.ID(), |
| 255 | fac.CopyExpr(compre.IterRange()), |
| 256 | compre.IterVar(), |
| 257 | compre.IterVar2(), |
| 258 | compre.AccuVar(), |
| 259 | fac.CopyExpr(compre.AccuInit()), |
| 260 | fac.CopyExpr(compre.LoopCondition()), |
| 261 | fac.CopyExpr(compre.LoopStep()), |
| 262 | fac.CopyExpr(compre.Result())) |
| 263 | case IdentKind: |
| 264 | return fac.NewIdent(e.ID(), e.AsIdent()) |
| 265 | case ListKind: |
| 266 | l := e.AsList() |
| 267 | elemsCopy := make([]Expr, l.Size()) |
| 268 | for i, elem := range l.Elements() { |
| 269 | elemsCopy[i] = fac.CopyExpr(elem) |
| 270 | } |
| 271 | return fac.NewList(e.ID(), elemsCopy, l.OptionalIndices()) |
| 272 | case LiteralKind: |
| 273 | return fac.NewLiteral(e.ID(), e.AsLiteral()) |
| 274 | case MapKind: |
| 275 | m := e.AsMap() |
| 276 | entriesCopy := make([]EntryExpr, m.Size()) |
| 277 | for i, entry := range m.Entries() { |
| 278 | entriesCopy[i] = fac.CopyEntryExpr(entry) |
| 279 | } |
| 280 | return fac.NewMap(e.ID(), entriesCopy) |
| 281 | case SelectKind: |
| 282 | s := e.AsSelect() |
| 283 | if s.IsTestOnly() { |
| 284 | return fac.NewPresenceTest(e.ID(), fac.CopyExpr(s.Operand()), s.FieldName()) |
| 285 | } |
| 286 | return fac.NewSelect(e.ID(), fac.CopyExpr(s.Operand()), s.FieldName()) |
| 287 | case StructKind: |
| 288 | s := e.AsStruct() |
| 289 | fieldsCopy := make([]EntryExpr, len(s.Fields())) |
| 290 | for i, field := range s.Fields() { |
| 291 | fieldsCopy[i] = fac.CopyEntryExpr(field) |
| 292 | } |
| 293 | return fac.NewStruct(e.ID(), s.TypeName(), fieldsCopy) |
no test coverage detected