...

Source file src/cmd/compile/internal/types2/format.go

Documentation: cmd/compile/internal/types2

     1  // Copyright 2024 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  // This file implements (error and trace) message formatting support.
     6  
     7  package types2
     8  
     9  import (
    10  	"bytes"
    11  	"cmd/compile/internal/syntax"
    12  	"fmt"
    13  	"strconv"
    14  	"strings"
    15  )
    16  
    17  func sprintf(qf Qualifier, tpSubscripts bool, format string, args ...any) string {
    18  	for i, arg := range args {
    19  		switch a := arg.(type) {
    20  		case nil:
    21  			arg = "<nil>"
    22  		case operand:
    23  			panic("got operand instead of *operand")
    24  		case *operand:
    25  			arg = operandString(a, qf)
    26  		case syntax.Pos:
    27  			arg = a.String()
    28  		case syntax.Expr:
    29  			arg = ExprString(a)
    30  		case []syntax.Expr:
    31  			var buf strings.Builder
    32  			buf.WriteByte('[')
    33  			for i, x := range a {
    34  				if i > 0 {
    35  					buf.WriteString(", ")
    36  				}
    37  				buf.WriteString(ExprString(x))
    38  			}
    39  			buf.WriteByte(']')
    40  			arg = buf.String()
    41  		case Object:
    42  			arg = ObjectString(a, qf)
    43  		case Type:
    44  			var buf bytes.Buffer
    45  			w := newTypeWriter(&buf, qf)
    46  			w.tpSubscripts = tpSubscripts
    47  			w.typ(a)
    48  			arg = buf.String()
    49  		case []Type:
    50  			var buf bytes.Buffer
    51  			w := newTypeWriter(&buf, qf)
    52  			w.tpSubscripts = tpSubscripts
    53  			buf.WriteByte('[')
    54  			for i, x := range a {
    55  				if i > 0 {
    56  					buf.WriteString(", ")
    57  				}
    58  				w.typ(x)
    59  			}
    60  			buf.WriteByte(']')
    61  			arg = buf.String()
    62  		case []*TypeParam:
    63  			var buf bytes.Buffer
    64  			w := newTypeWriter(&buf, qf)
    65  			w.tpSubscripts = tpSubscripts
    66  			buf.WriteByte('[')
    67  			for i, x := range a {
    68  				if i > 0 {
    69  					buf.WriteString(", ")
    70  				}
    71  				w.typ(x)
    72  			}
    73  			buf.WriteByte(']')
    74  			arg = buf.String()
    75  		}
    76  		args[i] = arg
    77  	}
    78  	return fmt.Sprintf(format, args...)
    79  }
    80  
    81  // check may be nil.
    82  func (check *Checker) sprintf(format string, args ...any) string {
    83  	var qf Qualifier
    84  	if check != nil {
    85  		qf = check.qualifier
    86  	}
    87  	return sprintf(qf, false, format, args...)
    88  }
    89  
    90  func (check *Checker) trace(pos syntax.Pos, format string, args ...any) {
    91  	fmt.Printf("%s:\t%s%s\n",
    92  		pos,
    93  		strings.Repeat(".  ", check.indent),
    94  		sprintf(check.qualifier, true, format, args...),
    95  	)
    96  }
    97  
    98  // dump is only needed for debugging
    99  func (check *Checker) dump(format string, args ...any) {
   100  	fmt.Println(sprintf(check.qualifier, true, format, args...))
   101  }
   102  
   103  func (check *Checker) qualifier(pkg *Package) string {
   104  	// Qualify the package unless it's the package being type-checked.
   105  	if pkg != check.pkg {
   106  		if check.pkgPathMap == nil {
   107  			check.pkgPathMap = make(map[string]map[string]bool)
   108  			check.seenPkgMap = make(map[*Package]bool)
   109  			check.markImports(check.pkg)
   110  		}
   111  		// If the same package name was used by multiple packages, display the full path.
   112  		if len(check.pkgPathMap[pkg.name]) > 1 {
   113  			return strconv.Quote(pkg.path)
   114  		}
   115  		return pkg.name
   116  	}
   117  	return ""
   118  }
   119  
   120  // markImports recursively walks pkg and its imports, to record unique import
   121  // paths in pkgPathMap.
   122  func (check *Checker) markImports(pkg *Package) {
   123  	if check.seenPkgMap[pkg] {
   124  		return
   125  	}
   126  	check.seenPkgMap[pkg] = true
   127  
   128  	forName, ok := check.pkgPathMap[pkg.name]
   129  	if !ok {
   130  		forName = make(map[string]bool)
   131  		check.pkgPathMap[pkg.name] = forName
   132  	}
   133  	forName[pkg.path] = true
   134  
   135  	for _, imp := range pkg.imports {
   136  		check.markImports(imp)
   137  	}
   138  }
   139  
   140  // stripAnnotations removes internal (type) annotations from s.
   141  func stripAnnotations(s string) string {
   142  	var buf strings.Builder
   143  	for _, r := range s {
   144  		// strip #'s and subscript digits
   145  		if r < '₀' || '₀'+10 <= r { // '₀' == U+2080
   146  			buf.WriteRune(r)
   147  		}
   148  	}
   149  	if buf.Len() < len(s) {
   150  		return buf.String()
   151  	}
   152  	return s
   153  }
   154  

View as plain text