github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gotools/go/ssa/mode.go (about)

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