Skip to content

Commit 19ee2ef

Browse files
committed
cmd/compile: introduce gc.Node.copy method
When making a shallow copy of a node, various methods were used, including calling nod(OXXX, nil, nil) and then overwriting it, or "n1 := *n" and then using &n1. Add a copy method instead, simplifying all of those and making them consistent. Passes toolstash -cmp on std cmd. Change-Id: I3f3fc88bad708edc712bf6d87214cda4ddc43b01 Reviewed-on: https://go-review.googlesource.com/72710 Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 321bd8c commit 19ee2ef

File tree

9 files changed

+57
-66
lines changed

9 files changed

+57
-66
lines changed

src/cmd/compile/internal/gc/const.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ func convlit1(n *Node, t *types.Type, explicit bool, reuse canReuseNode) *Node {
223223
if n.Op == OLITERAL && !reuse {
224224
// Can't always set n.Type directly on OLITERAL nodes.
225225
// See discussion on CL 20813.
226-
nn := *n
227-
n = &nn
226+
n = n.copy()
228227
reuse = true
229228
}
230229

@@ -1333,8 +1332,7 @@ func defaultlitreuse(n *Node, t *types.Type, reuse canReuseNode) *Node {
13331332
}
13341333

13351334
if n.Op == OLITERAL && !reuse {
1336-
nn := *n
1337-
n = &nn
1335+
n = n.copy()
13381336
reuse = true
13391337
}
13401338

src/cmd/compile/internal/gc/dcl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,11 @@ func funcargs(nt *Node) {
466466
// So the two cases must be distinguished.
467467
// We do not record a pointer to the original node (n->orig).
468468
// Having multiple names causes too much confusion in later passes.
469-
nn := *n.Left
470-
nn.Orig = &nn
469+
nn := n.Left.copy()
470+
nn.Orig = nn
471471
nn.Sym = lookupN("~b", gen)
472472
gen++
473-
n.Left = &nn
473+
n.Left = nn
474474
}
475475

476476
n.Left.Name.Param.Ntype = n.Right

src/cmd/compile/internal/gc/inl.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ func inlcopy(n *Node) *Node {
392392
return n
393393
}
394394

395-
m := *n
395+
m := n.copy()
396396
if m.Func != nil {
397397
m.Func.Inl.Set(nil)
398398
}
@@ -403,7 +403,7 @@ func inlcopy(n *Node) *Node {
403403
m.Ninit.Set(inlcopylist(n.Ninit.Slice()))
404404
m.Nbody.Set(inlcopylist(n.Nbody.Slice()))
405405

406-
return &m
406+
return m
407407
}
408408

409409
// Inlcalls/nodelist/node walks fn's statements and expressions and substitutes any
@@ -1192,8 +1192,7 @@ func (subst *inlsubst) node(n *Node) *Node {
11921192
return m
11931193

11941194
case OGOTO, OLABEL:
1195-
m := nod(OXXX, nil, nil)
1196-
*m = *n
1195+
m := n.copy()
11971196
m.Pos = subst.updatedPos(m.Pos)
11981197
m.Ninit.Set(nil)
11991198
p := fmt.Sprintf("%s·%d", n.Left.Sym.Name, inlgen)
@@ -1202,8 +1201,7 @@ func (subst *inlsubst) node(n *Node) *Node {
12021201
return m
12031202
}
12041203

1205-
m := nod(OXXX, nil, nil)
1206-
*m = *n
1204+
m := n.copy()
12071205
m.Pos = subst.updatedPos(m.Pos)
12081206
m.Ninit.Set(nil)
12091207

src/cmd/compile/internal/gc/order.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ func (o *Order) cheapExpr(n *Node) *Node {
109109
if l == n.Left {
110110
return n
111111
}
112-
a := *n
113-
a.Orig = &a
112+
a := n.copy()
113+
a.Orig = a
114114
a.Left = l
115-
return typecheck(&a, Erv)
115+
return typecheck(a, Erv)
116116
}
117117

118118
return o.copyExpr(n, n.Type, false)
@@ -135,20 +135,20 @@ func (o *Order) safeExpr(n *Node) *Node {
135135
if l == n.Left {
136136
return n
137137
}
138-
a := *n
139-
a.Orig = &a
138+
a := n.copy()
139+
a.Orig = a
140140
a.Left = l
141-
return typecheck(&a, Erv)
141+
return typecheck(a, Erv)
142142

143143
case ODOTPTR, OIND:
144144
l := o.cheapExpr(n.Left)
145145
if l == n.Left {
146146
return n
147147
}
148-
a := *n
149-
a.Orig = &a
148+
a := n.copy()
149+
a.Orig = a
150150
a.Left = l
151-
return typecheck(&a, Erv)
151+
return typecheck(a, Erv)
152152

153153
case OINDEX, OINDEXMAP:
154154
var l *Node
@@ -161,11 +161,11 @@ func (o *Order) safeExpr(n *Node) *Node {
161161
if l == n.Left && r == n.Right {
162162
return n
163163
}
164-
a := *n
165-
a.Orig = &a
164+
a := n.copy()
165+
a.Orig = a
166166
a.Left = l
167167
a.Right = r
168-
return typecheck(&a, Erv)
168+
return typecheck(a, Erv)
169169

170170
default:
171171
Fatalf("ordersafeexpr %v", n.Op)

src/cmd/compile/internal/gc/racewalk.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,17 @@ func instrument(fn *Node) {
6767
// nodpc is the PC of the caller as extracted by
6868
// getcallerpc. We use -widthptr(FP) for x86.
6969
// BUG: this will not work on arm.
70-
nodpc := *nodfp
70+
nodpc := nodfp.copy()
7171
nodpc.Type = types.Types[TUINTPTR]
7272
nodpc.Xoffset = int64(-Widthptr)
7373
savedLineno := lineno
7474
lineno = src.NoXPos
75-
nd := mkcall("racefuncenter", nil, nil, &nodpc)
75+
nd := mkcall("racefuncenter", nil, nil, nodpc)
7676

7777
fn.Func.Enter.Prepend(nd)
7878
nd = mkcall("racefuncexit", nil, nil)
7979
fn.Func.Exit.Append(nd)
80-
fn.Func.Dcl = append(fn.Func.Dcl, &nodpc)
80+
fn.Func.Dcl = append(fn.Func.Dcl, nodpc)
8181
lineno = savedLineno
8282
}
8383

src/cmd/compile/internal/gc/sinit.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -328,35 +328,32 @@ func staticcopy(l *Node, r *Node, out *[]*Node) bool {
328328
// copy slice
329329
a := inittemps[r]
330330

331-
n := *l
331+
n := l.copy()
332332
n.Xoffset = l.Xoffset + int64(array_array)
333-
gdata(&n, nod(OADDR, a, nil), Widthptr)
333+
gdata(n, nod(OADDR, a, nil), Widthptr)
334334
n.Xoffset = l.Xoffset + int64(array_nel)
335-
gdata(&n, r.Right, Widthptr)
335+
gdata(n, r.Right, Widthptr)
336336
n.Xoffset = l.Xoffset + int64(array_cap)
337-
gdata(&n, r.Right, Widthptr)
337+
gdata(n, r.Right, Widthptr)
338338
return true
339339

340340
case OARRAYLIT, OSTRUCTLIT:
341341
p := initplans[r]
342342

343-
n := *l
343+
n := l.copy()
344344
for i := range p.E {
345345
e := &p.E[i]
346346
n.Xoffset = l.Xoffset + e.Xoffset
347347
n.Type = e.Expr.Type
348348
if e.Expr.Op == OLITERAL {
349-
gdata(&n, e.Expr, int(n.Type.Width))
349+
gdata(n, e.Expr, int(n.Type.Width))
350350
} else {
351-
ll := nod(OXXX, nil, nil)
352-
*ll = n
351+
ll := n.copy()
353352
ll.Orig = ll // completely separate copy
354353
if !staticassign(ll, e.Expr, out) {
355354
// Requires computation, but we're
356355
// copying someone else's computation.
357-
rr := nod(OXXX, nil, nil)
358-
359-
*rr = *orig
356+
rr := orig.copy()
360357
rr.Orig = rr // completely separate copy
361358
rr.Type = ll.Type
362359
rr.Xoffset += e.Xoffset
@@ -429,13 +426,13 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
429426
ta := types.NewArray(r.Type.Elem(), bound)
430427
a := staticname(ta)
431428
inittemps[r] = a
432-
n := *l
429+
n := l.copy()
433430
n.Xoffset = l.Xoffset + int64(array_array)
434-
gdata(&n, nod(OADDR, a, nil), Widthptr)
431+
gdata(n, nod(OADDR, a, nil), Widthptr)
435432
n.Xoffset = l.Xoffset + int64(array_nel)
436-
gdata(&n, r.Right, Widthptr)
433+
gdata(n, r.Right, Widthptr)
437434
n.Xoffset = l.Xoffset + int64(array_cap)
438-
gdata(&n, r.Right, Widthptr)
435+
gdata(n, r.Right, Widthptr)
439436

440437
// Fall through to init underlying array.
441438
l = a
@@ -445,17 +442,16 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
445442
initplan(r)
446443

447444
p := initplans[r]
448-
n := *l
445+
n := l.copy()
449446
for i := range p.E {
450447
e := &p.E[i]
451448
n.Xoffset = l.Xoffset + e.Xoffset
452449
n.Type = e.Expr.Type
453450
if e.Expr.Op == OLITERAL {
454-
gdata(&n, e.Expr, int(n.Type.Width))
451+
gdata(n, e.Expr, int(n.Type.Width))
455452
} else {
456453
setlineno(e.Expr)
457-
a := nod(OXXX, nil, nil)
458-
*a = n
454+
a := n.copy()
459455
a.Orig = a // completely separate copy
460456
if !staticassign(a, e.Expr, out) {
461457
*out = append(*out, nod(OAS, a, e.Expr))
@@ -522,11 +518,10 @@ func staticassign(l *Node, r *Node, out *[]*Node) bool {
522518
// Copy val directly into n.
523519
n.Type = val.Type
524520
setlineno(val)
525-
a := nod(OXXX, nil, nil)
526-
*a = n
527-
a.Orig = a
528-
if !staticassign(a, val, out) {
529-
*out = append(*out, nod(OAS, a, val))
521+
a := n
522+
a.Orig = &a
523+
if !staticassign(&a, val, out) {
524+
*out = append(*out, nod(OAS, &a, val))
530525
}
531526
} else {
532527
// Construct temp to hold val, write pointer to temp into n.

src/cmd/compile/internal/gc/subr.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ func nodSym(op Op, left *Node, sym *types.Sym) *Node {
364364
return n
365365
}
366366

367+
func (n *Node) copy() *Node {
368+
n2 := *n
369+
return &n2
370+
}
371+
367372
// methcmp sorts methods by name with exported methods first,
368373
// and then non-exported methods by their package path.
369374
type methcmp []*types.Field
@@ -439,8 +444,8 @@ func treecopy(n *Node, pos src.XPos) *Node {
439444

440445
switch n.Op {
441446
default:
442-
m := *n
443-
m.Orig = &m
447+
m := n.copy()
448+
m.Orig = m
444449
m.Left = treecopy(n.Left, pos)
445450
m.Right = treecopy(n.Right, pos)
446451
m.List.Set(listtreecopy(n.List.Slice(), pos))
@@ -451,7 +456,7 @@ func treecopy(n *Node, pos src.XPos) *Node {
451456
Dump("treecopy", n)
452457
Fatalf("treecopy Name")
453458
}
454-
return &m
459+
return m
455460

456461
case OPACK:
457462
// OPACK nodes are never valid in const value declarations,
@@ -1252,8 +1257,7 @@ func safeexpr(n *Node, init *Nodes) *Node {
12521257
if l == n.Left {
12531258
return n
12541259
}
1255-
r := nod(OXXX, nil, nil)
1256-
*r = *n
1260+
r := n.copy()
12571261
r.Left = l
12581262
r = typecheck(r, Erv)
12591263
r = walkexpr(r, init)
@@ -1264,8 +1268,7 @@ func safeexpr(n *Node, init *Nodes) *Node {
12641268
if l == n.Left {
12651269
return n
12661270
}
1267-
a := nod(OXXX, nil, nil)
1268-
*a = *n
1271+
a := n.copy()
12691272
a.Left = l
12701273
a = walkexpr(a, init)
12711274
return a
@@ -1276,8 +1279,7 @@ func safeexpr(n *Node, init *Nodes) *Node {
12761279
if l == n.Left && r == n.Right {
12771280
return n
12781281
}
1279-
a := nod(OXXX, nil, nil)
1280-
*a = *n
1282+
a := n.copy()
12811283
a.Left = l
12821284
a.Right = r
12831285
a = walkexpr(a, init)

src/cmd/compile/internal/gc/typecheck.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2873,9 +2873,7 @@ func typecheckcomplit(n *Node) *Node {
28732873
}
28742874

28752875
// Save original node (including n.Right)
2876-
norig := nod(n.Op, nil, nil)
2877-
2878-
*norig = *n
2876+
norig := n.copy()
28792877

28802878
setlineno(n.Right)
28812879
n.Right = typecheck(n.Right, Etype|Ecomplit)

src/cmd/compile/internal/gc/walk.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,7 +3894,7 @@ func wrapCall(n *Node, init *Nodes) *Node {
38943894
// The result of substArgTypes MUST be assigned back to old, e.g.
38953895
// n.Left = substArgTypes(n.Left, t1, t2)
38963896
func substArgTypes(old *Node, types_ ...*types.Type) *Node {
3897-
n := *old // make shallow copy
3897+
n := old.copy() // make shallow copy
38983898

38993899
for _, t := range types_ {
39003900
dowidth(t)
@@ -3903,5 +3903,5 @@ func substArgTypes(old *Node, types_ ...*types.Type) *Node {
39033903
if len(types_) > 0 {
39043904
Fatalf("substArgTypes: too many argument types")
39053905
}
3906-
return &n
3906+
return n
39073907
}

0 commit comments

Comments
 (0)