@@ -15,25 +15,40 @@ func (check *Checker) initOrder() {
15
15
// built from several calls to (*Checker).Files. Clear it.
16
16
check .Info .InitOrder = check .Info .InitOrder [:0 ]
17
17
18
- // compute the object dependency graph and
19
- // initialize a priority queue with the list
20
- // of graph nodes
18
+ // Compute the transposed object dependency graph and initialize
19
+ // a priority queue with the list of graph nodes.
21
20
pq := nodeQueue (dependencyGraph (check .objMap ))
22
21
heap .Init (& pq )
23
22
24
23
const debug = false
25
24
if debug {
26
- fmt .Printf ("package %s: object dependency graph\n " , check .pkg .Name ())
25
+ fmt .Printf ("Computing initialization order for %s\n \n " , check .pkg )
26
+ fmt .Println ("Object dependency graph:" )
27
+ for obj , d := range check .objMap {
28
+ if len (d .deps ) > 0 {
29
+ fmt .Printf ("\t %s depends on\n " , obj .Name ())
30
+ for dep := range d .deps {
31
+ fmt .Printf ("\t \t %s\n " , dep .Name ())
32
+ }
33
+ } else {
34
+ fmt .Printf ("\t %s has no dependencies\n " , obj .Name ())
35
+ }
36
+ }
37
+ fmt .Println ()
38
+
39
+ fmt .Println ("Transposed object dependency graph:" )
27
40
for _ , n := range pq {
28
- for _ , o := range n .out {
29
- fmt .Printf ("\t %s -> %s\n " , n .obj .Name (), o .obj .Name ())
41
+ fmt .Printf ("\t %s depends on %d nodes\n " , n .obj .Name (), n .in )
42
+ for _ , out := range n .out {
43
+ fmt .Printf ("\t \t %s is dependent\n " , out .obj .Name ())
30
44
}
31
45
}
32
46
fmt .Println ()
33
- fmt .Printf ("package %s: initialization order\n " , check .pkg .Name ())
47
+
48
+ fmt .Println ("Processing nodes:" )
34
49
}
35
50
36
- // determine initialization order by removing the highest priority node
51
+ // Determine initialization order by removing the highest priority node
37
52
// (the one with the fewest dependencies) and its edges from the graph,
38
53
// repeatedly, until there are no nodes left.
39
54
// In a valid Go program, those nodes always have zero dependencies (after
@@ -45,6 +60,11 @@ func (check *Checker) initOrder() {
45
60
// get the next node
46
61
n := heap .Pop (& pq ).(* objNode )
47
62
63
+ if debug {
64
+ fmt .Printf ("\t %s (src pos %d) depends on %d nodes now\n " ,
65
+ n .obj .Name (), n .obj .order (), n .in )
66
+ }
67
+
48
68
// if n still depends on other nodes, we have a cycle
49
69
if n .in > 0 {
50
70
mark ++ // mark nodes using a different value each time
@@ -86,14 +106,15 @@ func (check *Checker) initOrder() {
86
106
}
87
107
init := & Initializer {infoLhs , info .init }
88
108
check .Info .InitOrder = append (check .Info .InitOrder , init )
89
-
90
- if debug {
91
- fmt .Printf ("\t %s\n " , init )
92
- }
93
109
}
94
110
95
111
if debug {
96
112
fmt .Println ()
113
+ fmt .Println ("Initialization order:" )
114
+ for _ , init := range check .Info .InitOrder {
115
+ fmt .Printf ("\t %s\n " , init )
116
+ }
117
+ fmt .Println ()
97
118
}
98
119
}
99
120
0 commit comments