Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/compile/internal/importer/support.go

Documentation: cmd/compile/internal/importer

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

View as plain text