Source file src/cmd/compile/internal/ir/copy.go

     1  // Copyright 2020 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ir
     6  
     7  import (
     8  	"cmd/internal/src"
     9  )
    10  
    11  // Copy returns a shallow copy of n.
    12  func Copy(n Node) Node {
    13  	return n.copy()
    14  }
    15  
    16  // DeepCopy returns a “deep” copy of n, with its entire structure copied
    17  // (except for shared nodes like ONAME, ONONAME, OLITERAL, and OTYPE).
    18  // If pos.IsKnown(), it sets the source position of newly allocated Nodes to pos.
    19  func DeepCopy(pos src.XPos, n Node) Node {
    20  	var edit func(Node) Node
    21  	edit = func(x Node) Node {
    22  		switch x.Op() {
    23  		case ONAME, ONONAME, OLITERAL, ONIL, OTYPE:
    24  			return x
    25  		}
    26  		x = Copy(x)
    27  		if pos.IsKnown() {
    28  			x.SetPos(pos)
    29  		}
    30  		EditChildren(x, edit)
    31  		return x
    32  	}
    33  	return edit(n)
    34  }
    35  
    36  // DeepCopyList returns a list of deep copies (using DeepCopy) of the nodes in list.
    37  func DeepCopyList(pos src.XPos, list []Node) []Node {
    38  	var out []Node
    39  	for _, n := range list {
    40  		out = append(out, DeepCopy(pos, n))
    41  	}
    42  	return out
    43  }
    44  

View as plain text