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

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

View as plain text