1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:24:48 -07:00

cmd/compile: update fmt.go internal documentation

No code changes.

Change-Id: I7a22b3fbd6d727b276c7559f064cb0fdf385c02b
Reviewed-on: https://go-review.googlesource.com/28955
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2016-09-09 22:24:44 -07:00
parent 6537e18f02
commit 36b32911bd

View File

@ -12,24 +12,24 @@ import (
"unicode/utf8" "unicode/utf8"
) )
// TODO(gri) update documentation thoroughly
// A FmtFlag value is a set of flags (or 0). // A FmtFlag value is a set of flags (or 0).
// They control how the Xconv functions format their values. // They control how the Xconv functions format their values.
// See the respective function's documentation for details. // See the respective function's documentation for details.
type FmtFlag int type FmtFlag int
const ( // fmt.Format flag/prec or verb const ( // fmt.Format flag/prec or verb
FmtLeft FmtFlag = 1 << iota // "-" => '-' FmtLeft FmtFlag = 1 << iota // '-'
FmtSharp // "#" => '#' FmtSharp // '#'
FmtSign // "+" => '+' FmtSign // '+'
FmtUnsigned // "u" => ' ' FmtUnsigned // ' ' (historic: u flag)
FmtShort // "h" => verb == 'S' (Short) FmtShort // verb == 'S' (historic: h flag)
FmtLong // "l" => verb == 'L' (Long) FmtLong // verb == 'L' (historic: l flag)
FmtComma // "," => '.' (== hasPrec) FmtComma // '.' (== hasPrec) (historic: , flag)
FmtByte // "hh" => '0' FmtByte // '0' (historic: hh flag)
) )
// fmtFlag computes the (internal) FmtFlag
// value given the fmt.State and format verb.
func fmtFlag(s fmt.State, verb rune) FmtFlag { func fmtFlag(s fmt.State, verb rune) FmtFlag {
var flag FmtFlag var flag FmtFlag
if s.Flag('-') { if s.Flag('-') {
@ -59,45 +59,38 @@ func fmtFlag(s fmt.State, verb rune) FmtFlag {
return flag return flag
} }
// Format conversions:
// TODO(gri) verify these; eliminate those not used anymore
// //
// Format conversions // %v Op Node opcodes
// %L int Line numbers // Flags: #: print Go syntax (automatic unless fmtmode == FDbg)
// //
// %E int etype values (aka 'Kind') // %j *Node Node details
// Flags: 0: suppresses things not relevant until walk
// //
// %O int Node Opcodes // %v *Val Constant values
// Flags: "%#O": print go syntax. (automatic unless fmtmode == FDbg)
// //
// %J Node* Node details // %v *Sym Symbols
// Flags: "%hJ" suppresses things not relevant until walk. // %S unqualified identifier in any mode
// Flags: +,- #: mode (see below)
// 0: in export mode: unqualified identifier if exported, qualified if not
// //
// %V Val* Constant values // %v *Type Types
// %S omit "func" and receiver in function types
// %L definition instead of name.
// Flags: +,- #: mode (see below)
// ' ' (only in -/Sym mode) print type identifiers wit package name instead of prefix.
// //
// %S Sym* Symbols // %v *Node Nodes
// Flags: +,- #: mode (see below) // %S (only in +/debug mode) suppress recursion
// "%hS" unqualified identifier in any mode // %L (only in Error mode) print "foo (type Bar)"
// "%hhS" in export mode: unqualified identifier if exported, qualified if not // Flags: +,- #: mode (see below)
// //
// %T Type* Types // %v Nodes Node lists
// Flags: +,- #: mode (see below) // Flags: those of *Node
// 'l' definition instead of name. // .: separate items with ',' instead of ';'
// 'h' omit "func" and receiver in function types
// 'u' (only in -/Sym mode) print type identifiers wit package name instead of prefix. // *Sym, *Type, and *Node types use the flags below to set the format mode
//
// %N Node* Nodes
// Flags: +,- #: mode (see below)
// 'h' (only in +/debug mode) suppress recursion
// 'l' (only in Error mode) print "foo (type Bar)"
//
// %H Nodes Nodes
// Flags: those of %N
// ',' separate items with ',' instead of ';'
//
// In mparith2.go and mparith3.go:
// %B Mpint* Big integers
// %F Mpflt* Big floats
//
// %S, %T and %N obey use the following flags to set the format mode:
const ( const (
FErr = iota FErr = iota
FDbg FDbg
@ -106,31 +99,33 @@ const (
var fmtmode int = FErr var fmtmode int = FErr
var fmtpkgpfx int // %uT stickyness var fmtpkgpfx int // "% v" stickyness for *Type objects
// The mode flags '+', '-', and '#' are sticky; they persist through
// recursions of *Node, *Type, and *Sym values. The ' ' flag is
// sticky only on *Type recursions and only used in %-/*Sym mode.
// //
// E.g. for %S: %+S %#S %-S print an identifier properly qualified for debug/export/internal mode. // Example: given a *Sym: %+v %#v %-v print an identifier properly qualified for debug/export/internal mode
//
// The mode flags +, - and # are sticky, meaning they persist through
// recursions of %N, %T and %S, but not the h and l flags. The u flag is
// sticky only on %T recursions and only used in %-/Sym mode.
//
// Useful format combinations: // Useful format combinations:
// TODO(gri): verify these
// //
// %+N %+H multiline recursive debug dump of node/nodelist // *Node, Nodes:
// %+hN %+hH non recursive debug dump // %+v multiline recursive debug dump of *Node/Nodes
// %+S non-recursive debug dump
// //
// %#N %#T export format // *Node:
// %#lT type definition instead of name // %#v Go format
// %#hT omit"func" and receiver in function signature // %L "foo (type Bar)" for error messages
// //
// %lN "foo (type Bar)" for error messages // *Type:
// // %#v Go format
// %-T type identifiers // %#L type definition instead of name
// %-hT type identifiers without "func" and arg names in type signatures (methodsym) // %#S omit"func" and receiver in function signature
// %-uT type identifiers with package name instead of prefix (typesym, dcommontype, typehash)
// //
// %-v type identifiers
// %-S type identifiers without "func" and arg names in type signatures (methodsym)
// %- v type identifiers with package name instead of prefix (typesym, dcommontype, typehash)
func setfmode(flags *FmtFlag) (fm int) { func setfmode(flags *FmtFlag) (fm int) {
fm = fmtmode fm = fmtmode
@ -146,8 +141,6 @@ func setfmode(flags *FmtFlag) (fm int) {
return return
} }
// Fmt "%L": Linenumbers
var goopnames = []string{ var goopnames = []string{
OADDR: "&", OADDR: "&",
OADD: "+", OADD: "+",
@ -269,7 +262,7 @@ func (n *Node) Format(s fmt.State, verb rune) {
} }
} }
// Node details // *Node details
func (n *Node) jconv(s fmt.State, flag FmtFlag) { func (n *Node) jconv(s fmt.State, flag FmtFlag) {
c := flag & FmtShort c := flag & FmtShort
@ -387,7 +380,6 @@ func (v Val) Format(s fmt.State, verb rune) {
} }
} }
// Fmt "%V": Values
func (v Val) vconv(s fmt.State, flag FmtFlag) { func (v Val) vconv(s fmt.State, flag FmtFlag) {
switch u := v.U.(type) { switch u := v.U.(type) {
case *Mpint: case *Mpint:
@ -514,7 +506,6 @@ func (et EType) String() string {
return fmt.Sprintf("E-%d", et) return fmt.Sprintf("E-%d", et)
} }
// Fmt "%S": syms
func (s *Sym) symfmt(f fmt.State, flag FmtFlag) { func (s *Sym) symfmt(f fmt.State, flag FmtFlag) {
if s.Pkg != nil && flag&FmtShort == 0 { if s.Pkg != nil && flag&FmtShort == 0 {
switch fmtmode { switch fmtmode {
@ -1593,8 +1584,7 @@ func (s *Sym) String() string {
return fmt.Sprint(s) return fmt.Sprint(s)
} }
// Fmt "%S": syms // "%S" suppresses qualifying with package
// Flags: "%hS" suppresses qualifying with package
func (s *Sym) sconv(f fmt.State, flag FmtFlag) { func (s *Sym) sconv(f fmt.State, flag FmtFlag) {
if flag&FmtLong != 0 { if flag&FmtLong != 0 {
panic("linksymfmt") panic("linksymfmt")
@ -1706,10 +1696,9 @@ func (t *Type) Format(s fmt.State, verb rune) {
} }
} }
// Fmt "%T": types. // "%L" print definition, not name
// Flags: 'l' print definition, not name // "%S" omit 'func' and receiver from function types, short type names
// 'h' omit 'func' and receiver from function types, short type names // "% v" package name, not prefix (FTypeId mode, sticky)
// 'u' package name, not prefix (FTypeId mode, sticky)
func (t *Type) tconv(s fmt.State, flag FmtFlag) { func (t *Type) tconv(s fmt.State, flag FmtFlag) {
if t == nil { if t == nil {
fmt.Fprint(s, "<T>") fmt.Fprint(s, "<T>")
@ -1747,9 +1736,8 @@ func (n *Node) String() string {
return fmt.Sprint(n) return fmt.Sprint(n)
} }
// Fmt '%N': Nodes. // "%L" suffix with "(type %T)" where possible
// Flags: 'l' suffix with "(type %T)" where possible // "%+S" in debug mode, don't recurse, no multiline output
// '+h' in debug mode, don't recurse, no multiline output
func (n *Node) Nconv(s fmt.State, flag FmtFlag) { func (n *Node) Nconv(s fmt.State, flag FmtFlag) {
if n == nil { if n == nil {
fmt.Fprint(s, "<N>") fmt.Fprint(s, "<N>")
@ -1790,8 +1778,7 @@ func (n Nodes) String() string {
return fmt.Sprint(n) return fmt.Sprint(n)
} }
// Fmt '%H': Nodes. // Flags: all those of %N plus '.': separate with comma's instead of semicolons.
// Flags: all those of %N plus ',': separate with comma's instead of semicolons.
func (l Nodes) hconv(s fmt.State, flag FmtFlag) { func (l Nodes) hconv(s fmt.State, flag FmtFlag) {
if l.Len() == 0 && fmtmode == FDbg { if l.Len() == 0 && fmtmode == FDbg {
fmt.Fprint(s, "<nil>") fmt.Fprint(s, "<nil>")