github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/compile/internal/ir/reassignment.go (about)

     1  // Copyright 2023 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  // A ReassignOracle efficiently answers queries about whether local
     8  // variables are reassigned. This helper works by looking for function
     9  // params and short variable declarations (e.g.
    10  // https://go.dev/ref/spec#Short_variable_declarations) that are
    11  // neither address taken nor subsequently re-assigned. It is intended
    12  // to operate much like "ir.StaticValue" and "ir.Reassigned", but in a
    13  // way that does just a single walk of the containing function (as
    14  // opposed to a new walk on every call).
    15  type ReassignOracle struct {
    16  	fn *Func
    17  	// maps candidate name to its defining assignment (or for
    18  	// for params, defining func).
    19  	singleDef map[*Name]Node
    20  }
    21  
    22  // Init initializes the oracle based on the IR in function fn, laying
    23  // the groundwork for future calls to the StaticValue and Reassigned
    24  // methods. If the fn's IR is subsequently modified, Init must be
    25  // called again.
    26  func (ro *ReassignOracle) Init(fn *Func)
    27  
    28  // StaticValue method has the same semantics as the ir package function
    29  // of the same name; see comments on [StaticValue].
    30  func (ro *ReassignOracle) StaticValue(n Node) Node
    31  
    32  // Reassigned method has the same semantics as the ir package function
    33  // of the same name; see comments on [Reassigned] for more info.
    34  func (ro *ReassignOracle) Reassigned(n *Name) bool