github.com/ltltlt/go-source-code@v0.0.0-20190830023027-95be009773aa/cmd/compile/internal/types/utils.go (about)

     1  // Copyright 2017 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 types
     6  
     7  import (
     8  	"cmd/internal/obj"
     9  	"fmt"
    10  )
    11  
    12  const BADWIDTH = -1000000000
    13  
    14  // Initialized by frontend. Exists only here.
    15  var Tptr EType // either TPTR32 or TPTR64
    16  
    17  // The following variables must be initialized early by the frontend.
    18  // They are here to break import cycles.
    19  // TODO(gri) eliminate these dependencies.
    20  var (
    21  	Widthptr    int
    22  	Dowidth     func(*Type)
    23  	Fatalf      func(string, ...interface{})
    24  	Sconv       func(*Sym, int, int) string       // orig: func sconv(s *Sym, flag FmtFlag, mode fmtMode) string
    25  	Tconv       func(*Type, int, int, int) string // orig: func tconv(t *Type, flag FmtFlag, mode fmtMode, depth int) string
    26  	FormatSym   func(*Sym, fmt.State, rune, int)  // orig: func symFormat(sym *Sym, s fmt.State, verb rune, mode fmtMode)
    27  	FormatType  func(*Type, fmt.State, rune, int) // orig: func typeFormat(t *Type, s fmt.State, verb rune, mode fmtMode)
    28  	TypeLinkSym func(*Type) *obj.LSym
    29  	Ctxt        *obj.Link
    30  
    31  	FmtLeft     int
    32  	FmtUnsigned int
    33  	FErr        int
    34  )
    35  
    36  func (s *Sym) String() string {
    37  	return Sconv(s, 0, FErr)
    38  }
    39  
    40  func (sym *Sym) Format(s fmt.State, verb rune) {
    41  	FormatSym(sym, s, verb, FErr)
    42  }
    43  
    44  func (t *Type) String() string {
    45  	// This is an external entry point, so we pass depth 0 to tconv.
    46  	// The implementation of tconv (including typefmt and fldconv)
    47  	// must take care not to use a type in a formatting string
    48  	// to avoid resetting the recursion counter.
    49  	return Tconv(t, 0, FErr, 0)
    50  }
    51  
    52  // ShortString generates a short description of t.
    53  // It is used in autogenerated method names, reflection,
    54  // and itab names.
    55  func (t *Type) ShortString() string {
    56  	return Tconv(t, FmtLeft, FErr, 0)
    57  }
    58  
    59  // LongString generates a complete description of t.
    60  // It is useful for reflection,
    61  // or when a unique fingerprint or hash of a type is required.
    62  func (t *Type) LongString() string {
    63  	return Tconv(t, FmtLeft|FmtUnsigned, FErr, 0)
    64  }
    65  
    66  func (t *Type) Format(s fmt.State, verb rune) {
    67  	FormatType(t, s, verb, FErr)
    68  }
    69  
    70  type bitset8 uint8
    71  
    72  func (f *bitset8) set(mask uint8, b bool) {
    73  	if b {
    74  		*(*uint8)(f) |= mask
    75  	} else {
    76  		*(*uint8)(f) &^= mask
    77  	}
    78  }