github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/compile/importer/support.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  // This file implements support functionality for iimport.go.
     6  
     7  package importer
     8  
     9  import (
    10  	"fmt"
    11  	"go/token"
    12  	"sync"
    13  
    14  	"github.com/go-asm/go/cmd/compile/base"
    15  	"github.com/go-asm/go/cmd/compile/types2"
    16  	"github.com/go-asm/go/pkgbits"
    17  )
    18  
    19  func assert(p bool) {
    20  	base.Assert(p)
    21  }
    22  
    23  func errorf(format string, args ...interface{}) {
    24  	panic(fmt.Sprintf(format, args...))
    25  }
    26  
    27  const deltaNewFile = -64 // see github.com/go-asm/go/cmd/compile/gc/bexport.go
    28  
    29  // Synthesize a token.Pos
    30  type fakeFileSet struct {
    31  	fset  *token.FileSet
    32  	files map[string]*token.File
    33  }
    34  
    35  func (s *fakeFileSet) pos(file string, line, column int) token.Pos {
    36  	// TODO(mdempsky): Make use of column.
    37  
    38  	// Since we don't know the set of needed file positions, we
    39  	// reserve maxlines positions per file.
    40  	const maxlines = 64 * 1024
    41  	f := s.files[file]
    42  	if f == nil {
    43  		f = s.fset.AddFile(file, -1, maxlines)
    44  		s.files[file] = f
    45  		// Allocate the fake linebreak indices on first use.
    46  		// TODO(adonovan): opt: save ~512KB using a more complex scheme?
    47  		fakeLinesOnce.Do(func() {
    48  			fakeLines = make([]int, maxlines)
    49  			for i := range fakeLines {
    50  				fakeLines[i] = i
    51  			}
    52  		})
    53  		f.SetLines(fakeLines)
    54  	}
    55  
    56  	if line > maxlines {
    57  		line = 1
    58  	}
    59  
    60  	// Treat the file as if it contained only newlines
    61  	// and column=1: use the line number as the offset.
    62  	return f.Pos(line - 1)
    63  }
    64  
    65  var (
    66  	fakeLines     []int
    67  	fakeLinesOnce sync.Once
    68  )
    69  
    70  func chanDir(d int) types2.ChanDir {
    71  	// tag values must match the constants in github.com/go-asm/go/cmd/compile/gc/go.go
    72  	switch d {
    73  	case 1 /* Crecv */ :
    74  		return types2.RecvOnly
    75  	case 2 /* Csend */ :
    76  		return types2.SendOnly
    77  	case 3 /* Cboth */ :
    78  		return types2.SendRecv
    79  	default:
    80  		errorf("unexpected channel dir %d", d)
    81  		return 0
    82  	}
    83  }
    84  
    85  var predeclared = []types2.Type{
    86  	// basic types
    87  	types2.Typ[types2.Bool],
    88  	types2.Typ[types2.Int],
    89  	types2.Typ[types2.Int8],
    90  	types2.Typ[types2.Int16],
    91  	types2.Typ[types2.Int32],
    92  	types2.Typ[types2.Int64],
    93  	types2.Typ[types2.Uint],
    94  	types2.Typ[types2.Uint8],
    95  	types2.Typ[types2.Uint16],
    96  	types2.Typ[types2.Uint32],
    97  	types2.Typ[types2.Uint64],
    98  	types2.Typ[types2.Uintptr],
    99  	types2.Typ[types2.Float32],
   100  	types2.Typ[types2.Float64],
   101  	types2.Typ[types2.Complex64],
   102  	types2.Typ[types2.Complex128],
   103  	types2.Typ[types2.String],
   104  
   105  	// basic type aliases
   106  	types2.Universe.Lookup("byte").Type(),
   107  	types2.Universe.Lookup("rune").Type(),
   108  
   109  	// error
   110  	types2.Universe.Lookup("error").Type(),
   111  
   112  	// untyped types
   113  	types2.Typ[types2.UntypedBool],
   114  	types2.Typ[types2.UntypedInt],
   115  	types2.Typ[types2.UntypedRune],
   116  	types2.Typ[types2.UntypedFloat],
   117  	types2.Typ[types2.UntypedComplex],
   118  	types2.Typ[types2.UntypedString],
   119  	types2.Typ[types2.UntypedNil],
   120  
   121  	// package unsafe
   122  	types2.Typ[types2.UnsafePointer],
   123  
   124  	// invalid type
   125  	types2.Typ[types2.Invalid], // only appears in packages with errors
   126  
   127  	// used internally by gc; never used by this package or in .a files
   128  	// not to be confused with the universe any
   129  	anyType{},
   130  
   131  	// comparable
   132  	types2.Universe.Lookup("comparable").Type(),
   133  
   134  	// any
   135  	types2.Universe.Lookup("any").Type(),
   136  }
   137  
   138  type anyType struct{}
   139  
   140  func (t anyType) Underlying() types2.Type { return t }
   141  func (t anyType) String() string          { return "any" }
   142  
   143  // See github.com/go-asm/go/cmd/compile/noder.derivedInfo.
   144  type derivedInfo struct {
   145  	idx    pkgbits.Index
   146  	needed bool
   147  }
   148  
   149  // See github.com/go-asm/go/cmd/compile/noder.typeInfo.
   150  type typeInfo struct {
   151  	idx     pkgbits.Index
   152  	derived bool
   153  }