Source file src/go/types/initorder.go

     1  // Copyright 2014 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 types
     6  
     7  import (
     8  	"container/heap"
     9  	"fmt"
    10  	"sort"
    11  )
    12  
    13  // initOrder computes the Info.InitOrder for package variables.
    14  func (check *Checker) initOrder() {
    15  	// An InitOrder may already have been computed if a package is
    16  	// built from several calls to (*Checker).Files. Clear it.
    17  	check.Info.InitOrder = check.Info.InitOrder[:0]
    18  
    19  	// Compute the object dependency graph and initialize
    20  	// a priority queue with the list of graph nodes.
    21  	pq := nodeQueue(dependencyGraph(check.objMap))
    22  	heap.Init(&pq)
    23  
    24  	const debug = false
    25  	if debug {
    26  		fmt.Printf("Computing initialization order for %s\n\n", check.pkg)
    27  		fmt.Println("Object dependency graph:")
    28  		for obj, d := range check.objMap {
    29  			// only print objects that may appear in the dependency graph
    30  			if obj, _ := obj.(dependency); obj != nil {
    31  				if len(d.deps) > 0 {
    32  					fmt.Printf("\t%s depends on\n", obj.Name())
    33  					for dep := range d.deps {
    34  						fmt.Printf("\t\t%s\n", dep.Name())
    35  					}
    36  				} else {
    37  					fmt.Printf("\t%s has no dependencies\n", obj.Name())
    38  				}
    39  			}
    40  		}
    41  		fmt.Println()
    42  
    43  		fmt.Println("Transposed object dependency graph (functions eliminated):")
    44  		for _, n := range pq {
    45  			fmt.Printf("\t%s depends on %d nodes\n", n.obj.Name(), n.ndeps)
    46  			for p := range n.pred {
    47  				fmt.Printf("\t\t%s is dependent\n", p.obj.Name())
    48  			}
    49  		}
    50  		fmt.Println()
    51  
    52  		fmt.Println("Processing nodes:")
    53  	}
    54  
    55  	// Determine initialization order by removing the highest priority node
    56  	// (the one with the fewest dependencies) and its edges from the graph,
    57  	// repeatedly, until there are no nodes left.
    58  	// In a valid Go program, those nodes always have zero dependencies (after
    59  	// removing all incoming dependencies), otherwise there are initialization
    60  	// cycles.
    61  	emitted := make(map[*declInfo]bool)
    62  	for len(pq) > 0 {
    63  		// get the next node
    64  		n := heap.Pop(&pq).(*graphNode)
    65  
    66  		if debug {
    67  			fmt.Printf("\t%s (src pos %d) depends on %d nodes now\n",
    68  				n.obj.Name(), n.obj.order(), n.ndeps)
    69  		}
    70  
    71  		// if n still depends on other nodes, we have a cycle
    72  		if n.ndeps > 0 {
    73  			cycle := findPath(check.objMap, n.obj, n.obj, make(map[Object]bool))
    74  			// If n.obj is not part of the cycle (e.g., n.obj->b->c->d->c),
    75  			// cycle will be nil. Don't report anything in that case since
    76  			// the cycle is reported when the algorithm gets to an object
    77  			// in the cycle.
    78  			// Furthermore, once an object in the cycle is encountered,
    79  			// the cycle will be broken (dependency count will be reduced
    80  			// below), and so the remaining nodes in the cycle don't trigger
    81  			// another error (unless they are part of multiple cycles).
    82  			if cycle != nil {
    83  				check.reportCycle(cycle)
    84  			}
    85  			// Ok to continue, but the variable initialization order
    86  			// will be incorrect at this point since it assumes no
    87  			// cycle errors.
    88  		}
    89  
    90  		// reduce dependency count of all dependent nodes
    91  		// and update priority queue
    92  		for p := range n.pred {
    93  			p.ndeps--
    94  			heap.Fix(&pq, p.index)
    95  		}
    96  
    97  		// record the init order for variables with initializers only
    98  		v, _ := n.obj.(*Var)
    99  		info := check.objMap[v]
   100  		if v == nil || !info.hasInitializer() {
   101  			continue
   102  		}
   103  
   104  		// n:1 variable declarations such as: a, b = f()
   105  		// introduce a node for each lhs variable (here: a, b);
   106  		// but they all have the same initializer - emit only
   107  		// one, for the first variable seen
   108  		if emitted[info] {
   109  			continue // initializer already emitted, if any
   110  		}
   111  		emitted[info] = true
   112  
   113  		infoLhs := info.lhs // possibly nil (see declInfo.lhs field comment)
   114  		if infoLhs == nil {
   115  			infoLhs = []*Var{v}
   116  		}
   117  		init := &Initializer{infoLhs, info.init}
   118  		check.Info.InitOrder = append(check.Info.InitOrder, init)
   119  	}
   120  
   121  	if debug {
   122  		fmt.Println()
   123  		fmt.Println("Initialization order:")
   124  		for _, init := range check.Info.InitOrder {
   125  			fmt.Printf("\t%s\n", init)
   126  		}
   127  		fmt.Println()
   128  	}
   129  }
   130  
   131  // findPath returns the (reversed) list of objects []Object{to, ... from}
   132  // such that there is a path of object dependencies from 'from' to 'to'.
   133  // If there is no such path, the result is nil.
   134  func findPath(objMap map[Object]*declInfo, from, to Object, seen map[Object]bool) []Object {
   135  	if seen[from] {
   136  		return nil
   137  	}
   138  	seen[from] = true
   139  
   140  	for d := range objMap[from].deps {
   141  		if d == to {
   142  			return []Object{d}
   143  		}
   144  		if P := findPath(objMap, d, to, seen); P != nil {
   145  			return append(P, d)
   146  		}
   147  	}
   148  
   149  	return nil
   150  }
   151  
   152  // reportCycle reports an error for the given cycle.
   153  func (check *Checker) reportCycle(cycle []Object) {
   154  	obj := cycle[0]
   155  	check.errorf(obj, _InvalidInitCycle, "initialization cycle for %s", obj.Name())
   156  	// subtle loop: print cycle[i] for i = 0, n-1, n-2, ... 1 for len(cycle) = n
   157  	for i := len(cycle) - 1; i >= 0; i-- {
   158  		check.errorf(obj, _InvalidInitCycle, "\t%s refers to", obj.Name()) // secondary error, \t indented
   159  		obj = cycle[i]
   160  	}
   161  	// print cycle[0] again to close the cycle
   162  	check.errorf(obj, _InvalidInitCycle, "\t%s", obj.Name())
   163  }
   164  
   165  // ----------------------------------------------------------------------------
   166  // Object dependency graph
   167  
   168  // A dependency is an object that may be a dependency in an initialization
   169  // expression. Only constants, variables, and functions can be dependencies.
   170  // Constants are here because constant expression cycles are reported during
   171  // initialization order computation.
   172  type dependency interface {
   173  	Object
   174  	isDependency()
   175  }
   176  
   177  // A graphNode represents a node in the object dependency graph.
   178  // Each node p in n.pred represents an edge p->n, and each node
   179  // s in n.succ represents an edge n->s; with a->b indicating that
   180  // a depends on b.
   181  type graphNode struct {
   182  	obj        dependency // object represented by this node
   183  	pred, succ nodeSet    // consumers and dependencies of this node (lazily initialized)
   184  	index      int        // node index in graph slice/priority queue
   185  	ndeps      int        // number of outstanding dependencies before this object can be initialized
   186  }
   187  
   188  // cost returns the cost of removing this node, which involves copying each
   189  // predecessor to each successor (and vice-versa).
   190  func (n *graphNode) cost() int {
   191  	return len(n.pred) * len(n.succ)
   192  }
   193  
   194  type nodeSet map[*graphNode]bool
   195  
   196  func (s *nodeSet) add(p *graphNode) {
   197  	if *s == nil {
   198  		*s = make(nodeSet)
   199  	}
   200  	(*s)[p] = true
   201  }
   202  
   203  // dependencyGraph computes the object dependency graph from the given objMap,
   204  // with any function nodes removed. The resulting graph contains only constants
   205  // and variables.
   206  func dependencyGraph(objMap map[Object]*declInfo) []*graphNode {
   207  	// M is the dependency (Object) -> graphNode mapping
   208  	M := make(map[dependency]*graphNode)
   209  	for obj := range objMap {
   210  		// only consider nodes that may be an initialization dependency
   211  		if obj, _ := obj.(dependency); obj != nil {
   212  			M[obj] = &graphNode{obj: obj}
   213  		}
   214  	}
   215  
   216  	// compute edges for graph M
   217  	// (We need to include all nodes, even isolated ones, because they still need
   218  	// to be scheduled for initialization in correct order relative to other nodes.)
   219  	for obj, n := range M {
   220  		// for each dependency obj -> d (= deps[i]), create graph edges n->s and s->n
   221  		for d := range objMap[obj].deps {
   222  			// only consider nodes that may be an initialization dependency
   223  			if d, _ := d.(dependency); d != nil {
   224  				d := M[d]
   225  				n.succ.add(d)
   226  				d.pred.add(n)
   227  			}
   228  		}
   229  	}
   230  
   231  	var G, funcG []*graphNode // separate non-functions and functions
   232  	for _, n := range M {
   233  		if _, ok := n.obj.(*Func); ok {
   234  			funcG = append(funcG, n)
   235  		} else {
   236  			G = append(G, n)
   237  		}
   238  	}
   239  
   240  	// remove function nodes and collect remaining graph nodes in G
   241  	// (Mutually recursive functions may introduce cycles among themselves
   242  	// which are permitted. Yet such cycles may incorrectly inflate the dependency
   243  	// count for variables which in turn may not get scheduled for initialization
   244  	// in correct order.)
   245  	//
   246  	// Note that because we recursively copy predecessors and successors
   247  	// throughout the function graph, the cost of removing a function at
   248  	// position X is proportional to cost * (len(funcG)-X). Therefore, we should
   249  	// remove high-cost functions last.
   250  	sort.Slice(funcG, func(i, j int) bool {
   251  		return funcG[i].cost() < funcG[j].cost()
   252  	})
   253  	for _, n := range funcG {
   254  		// connect each predecessor p of n with each successor s
   255  		// and drop the function node (don't collect it in G)
   256  		for p := range n.pred {
   257  			// ignore self-cycles
   258  			if p != n {
   259  				// Each successor s of n becomes a successor of p, and
   260  				// each predecessor p of n becomes a predecessor of s.
   261  				for s := range n.succ {
   262  					// ignore self-cycles
   263  					if s != n {
   264  						p.succ.add(s)
   265  						s.pred.add(p)
   266  					}
   267  				}
   268  				delete(p.succ, n) // remove edge to n
   269  			}
   270  		}
   271  		for s := range n.succ {
   272  			delete(s.pred, n) // remove edge to n
   273  		}
   274  	}
   275  
   276  	// fill in index and ndeps fields
   277  	for i, n := range G {
   278  		n.index = i
   279  		n.ndeps = len(n.succ)
   280  	}
   281  
   282  	return G
   283  }
   284  
   285  // ----------------------------------------------------------------------------
   286  // Priority queue
   287  
   288  // nodeQueue implements the container/heap interface;
   289  // a nodeQueue may be used as a priority queue.
   290  type nodeQueue []*graphNode
   291  
   292  func (a nodeQueue) Len() int { return len(a) }
   293  
   294  func (a nodeQueue) Swap(i, j int) {
   295  	x, y := a[i], a[j]
   296  	a[i], a[j] = y, x
   297  	x.index, y.index = j, i
   298  }
   299  
   300  func (a nodeQueue) Less(i, j int) bool {
   301  	x, y := a[i], a[j]
   302  	// nodes are prioritized by number of incoming dependencies (1st key)
   303  	// and source order (2nd key)
   304  	return x.ndeps < y.ndeps || x.ndeps == y.ndeps && x.obj.order() < y.obj.order()
   305  }
   306  
   307  func (a *nodeQueue) Push(x any) {
   308  	panic("unreachable")
   309  }
   310  
   311  func (a *nodeQueue) Pop() any {
   312  	n := len(*a)
   313  	x := (*a)[n-1]
   314  	x.index = -1 // for safety
   315  	*a = (*a)[:n-1]
   316  	return x
   317  }
   318  

View as plain text