Source file src/cmd/compile/internal/types/utils.go

Documentation: cmd/compile/internal/types

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

View as plain text