summaryrefslogtreecommitdiffstats
path: root/src/cmd/compile/internal/ir/mknode.go
blob: 5a0aaadf16fa66ed864cf1903b8688620293d6e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build ignore
// +build ignore

package main

import (
	"bytes"
	"fmt"
	"go/format"
	"go/types"
	"io/ioutil"
	"log"
	"reflect"
	"sort"
	"strings"

	"golang.org/x/tools/go/packages"
)

var irPkg *types.Package
var buf bytes.Buffer

func main() {
	cfg := &packages.Config{
		Mode: packages.NeedSyntax | packages.NeedTypes,
	}
	pkgs, err := packages.Load(cfg, "cmd/compile/internal/ir")
	if err != nil {
		log.Fatal(err)
	}
	irPkg = pkgs[0].Types

	fmt.Fprintln(&buf, "// Code generated by mknode.go. DO NOT EDIT.")
	fmt.Fprintln(&buf)
	fmt.Fprintln(&buf, "package ir")
	fmt.Fprintln(&buf)
	fmt.Fprintln(&buf, `import "fmt"`)

	scope := irPkg.Scope()
	for _, name := range scope.Names() {
		if strings.HasPrefix(name, "mini") {
			continue
		}

		obj, ok := scope.Lookup(name).(*types.TypeName)
		if !ok {
			continue
		}
		typ := obj.Type().(*types.Named)
		if !implementsNode(types.NewPointer(typ)) {
			continue
		}

		fmt.Fprintf(&buf, "\n")
		fmt.Fprintf(&buf, "func (n *%s) Format(s fmt.State, verb rune) { fmtNode(n, s, verb) }\n", name)

		switch name {
		case "Name", "Func":
			// Too specialized to automate.
			continue
		}

		forNodeFields(typ,
			"func (n *%[1]s) copy() Node { c := *n\n",
			"",
			"c.%[1]s = copy%[2]s(c.%[1]s)",
			"return &c }\n")

		forNodeFields(typ,
			"func (n *%[1]s) doChildren(do func(Node) bool) bool {\n",
			"if n.%[1]s != nil && do(n.%[1]s) { return true }",
			"if do%[2]s(n.%[1]s, do) { return true }",
			"return false }\n")

		forNodeFields(typ,
			"func (n *%[1]s) editChildren(edit func(Node) Node) {\n",
			"if n.%[1]s != nil { n.%[1]s = edit(n.%[1]s).(%[2]s) }",
			"edit%[2]s(n.%[1]s, edit)",
			"}\n")
	}

	makeHelpers()

	out, err := format.Source(buf.Bytes())
	if err != nil {
		// write out mangled source so we can see the bug.
		out = buf.Bytes()
	}

	err = ioutil.WriteFile("node_gen.go", out, 0666)
	if err != nil {
		log.Fatal(err)
	}
}

// needHelper maps needed slice helpers from their base name to their
// respective slice-element type.
var needHelper = map[string]string{}

func makeHelpers() {
	var names []string
	for name := range needHelper {
		names = append(names, name)
	}
	sort.Strings(names)

	for _, name := range names {
		fmt.Fprintf(&buf, sliceHelperTmpl, name, needHelper[name])
	}
}

const sliceHelperTmpl = `
func copy%[1]s(list []%[2]s) []%[2]s {
	if list == nil {
		return nil
	}
	c := make([]%[2]s, len(list))
	copy(c, list)
	return c
}
func do%[1]s(list []%[2]s, do func(Node) bool) bool {
	for _, x := range list {
		if x != nil && do(x) {
			return true
		}
	}
	return false
}
func edit%[1]s(list []%[2]s, edit func(Node) Node) {
	for i, x := range list {
		if x != nil {
			list[i] = edit(x).(%[2]s)
		}
	}
}
`

func forNodeFields(named *types.Named, prologue, singleTmpl, sliceTmpl, epilogue string) {
	fmt.Fprintf(&buf, prologue, named.Obj().Name())

	anyField(named.Underlying().(*types.Struct), func(f *types.Var) bool {
		if f.Embedded() {
			return false
		}
		name, typ := f.Name(), f.Type()

		slice, _ := typ.Underlying().(*types.Slice)
		if slice != nil {
			typ = slice.Elem()
		}

		tmpl, what := singleTmpl, types.TypeString(typ, types.RelativeTo(irPkg))
		if implementsNode(typ) {
			if slice != nil {
				helper := strings.TrimPrefix(what, "*") + "s"
				needHelper[helper] = what
				tmpl, what = sliceTmpl, helper
			}
		} else if what == "*Field" {
			// Special case for *Field.
			tmpl = sliceTmpl
			if slice != nil {
				what = "Fields"
			} else {
				what = "Field"
			}
		} else {
			return false
		}

		if tmpl == "" {
			return false
		}

		// Allow template to not use all arguments without
		// upsetting fmt.Printf.
		s := fmt.Sprintf(tmpl+"\x00 %[1]s %[2]s", name, what)
		fmt.Fprintln(&buf, s[:strings.LastIndex(s, "\x00")])
		return false
	})

	fmt.Fprintf(&buf, epilogue)
}

func implementsNode(typ types.Type) bool {
	if _, ok := typ.Underlying().(*types.Interface); ok {
		// TODO(mdempsky): Check the interface implements Node.
		// Worst case, node_gen.go will fail to compile if we're wrong.
		return true
	}

	if ptr, ok := typ.(*types.Pointer); ok {
		if str, ok := ptr.Elem().Underlying().(*types.Struct); ok {
			return anyField(str, func(f *types.Var) bool {
				return f.Embedded() && f.Name() == "miniNode"
			})
		}
	}

	return false
}

func anyField(typ *types.Struct, pred func(f *types.Var) bool) bool {
	for i, n := 0, typ.NumFields(); i < n; i++ {
		if value, ok := reflect.StructTag(typ.Tag(i)).Lookup("mknode"); ok {
			if value != "-" {
				panic(fmt.Sprintf("unexpected tag value: %q", value))
			}
			continue
		}

		f := typ.Field(i)
		if pred(f) {
			return true
		}
		if f.Embedded() {
			if typ, ok := f.Type().Underlying().(*types.Struct); ok {
				if anyField(typ, pred) {
					return true
				}
			}
		}
	}
	return false
}