1// Copyright 2015 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
5package ssa
6
7// This file defines the BuilderMode type and its command-line flag.
8
9import (
10	"bytes"
11	"fmt"
12)
13
14// BuilderMode is a bitmask of options for diagnostics and checking.
15//
16// *BuilderMode satisfies the flag.Value interface.  Example:
17//
18// 	var mode = ssa.BuilderMode(0)
19// 	func init() { flag.Var(&mode, "build", ssa.BuilderModeDoc) }
20//
21type BuilderMode uint
22
23const (
24	PrintPackages        BuilderMode = 1 << iota // Print package inventory to stdout
25	PrintFunctions                               // Print function SSA code to stdout
26	LogSource                                    // Log source locations as SSA builder progresses
27	SanityCheckFunctions                         // Perform sanity checking of function bodies
28	NaiveForm                                    // Build naïve SSA form: don't replace local loads/stores with registers
29	BuildSerially                                // Build packages serially, not in parallel.
30	GlobalDebug                                  // Enable debug info for all packages
31	BareInits                                    // Build init functions without guards or calls to dependent inits
32)
33
34const BuilderModeDoc = `Options controlling the SSA builder.
35The value is a sequence of zero or more of these letters:
36C	perform sanity [C]hecking of the SSA form.
37D	include [D]ebug info for every function.
38P	print [P]ackage inventory.
39F	print [F]unction SSA code.
40S	log [S]ource locations as SSA builder progresses.
41L	build distinct packages seria[L]ly instead of in parallel.
42N	build [N]aive SSA form: don't replace local loads/stores with registers.
43I	build bare [I]nit functions: no init guards or calls to dependent inits.
44`
45
46func (m BuilderMode) String() string {
47	var buf bytes.Buffer
48	if m&GlobalDebug != 0 {
49		buf.WriteByte('D')
50	}
51	if m&PrintPackages != 0 {
52		buf.WriteByte('P')
53	}
54	if m&PrintFunctions != 0 {
55		buf.WriteByte('F')
56	}
57	if m&LogSource != 0 {
58		buf.WriteByte('S')
59	}
60	if m&SanityCheckFunctions != 0 {
61		buf.WriteByte('C')
62	}
63	if m&NaiveForm != 0 {
64		buf.WriteByte('N')
65	}
66	if m&BuildSerially != 0 {
67		buf.WriteByte('L')
68	}
69	return buf.String()
70}
71
72// Set parses the flag characters in s and updates *m.
73func (m *BuilderMode) Set(s string) error {
74	var mode BuilderMode
75	for _, c := range s {
76		switch c {
77		case 'D':
78			mode |= GlobalDebug
79		case 'P':
80			mode |= PrintPackages
81		case 'F':
82			mode |= PrintFunctions
83		case 'S':
84			mode |= LogSource | BuildSerially
85		case 'C':
86			mode |= SanityCheckFunctions
87		case 'N':
88			mode |= NaiveForm
89		case 'L':
90			mode |= BuildSerially
91		default:
92			return fmt.Errorf("unknown BuilderMode option: %q", c)
93		}
94	}
95	*m = mode
96	return nil
97}
98
99// Get returns m.
100func (m BuilderMode) Get() interface{} { return m }
101