cuelang.org/go@v0.10.1/internal/golangorgx/tools/gcimporter/bimport.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 contains the remaining vestiges of 6 // $GOROOT/src/go/internal/gcimporter/bimport.go. 7 8 package gcimporter 9 10 import ( 11 "fmt" 12 "go/token" 13 "go/types" 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]*fileInfo 27 } 28 29 type fileInfo struct { 30 file *token.File 31 lastline int 32 } 33 34 const maxlines = 64 * 1024 35 36 func (s *fakeFileSet) pos(file string, line, column int) token.Pos { 37 // TODO(mdempsky): Make use of column. 38 39 // Since we don't know the set of needed file positions, we reserve maxlines 40 // positions per file. We delay calling token.File.SetLines until all 41 // positions have been calculated (by way of fakeFileSet.setLines), so that 42 // we can avoid setting unnecessary lines. See also golang/go#46586. 43 f := s.files[file] 44 if f == nil { 45 f = &fileInfo{file: s.fset.AddFile(file, -1, maxlines)} 46 s.files[file] = f 47 } 48 if line > maxlines { 49 line = 1 50 } 51 if line > f.lastline { 52 f.lastline = line 53 } 54 55 // Return a fake position assuming that f.file consists only of newlines. 56 return token.Pos(f.file.Base() + line - 1) 57 } 58 59 func (s *fakeFileSet) setLines() { 60 fakeLinesOnce.Do(func() { 61 fakeLines = make([]int, maxlines) 62 for i := range fakeLines { 63 fakeLines[i] = i 64 } 65 }) 66 for _, f := range s.files { 67 f.file.SetLines(fakeLines[:f.lastline]) 68 } 69 } 70 71 var ( 72 fakeLines []int 73 fakeLinesOnce sync.Once 74 ) 75 76 func chanDir(d int) types.ChanDir { 77 // tag values must match the constants in cmd/compile/internal/gc/go.go 78 switch d { 79 case 1 /* Crecv */ : 80 return types.RecvOnly 81 case 2 /* Csend */ : 82 return types.SendOnly 83 case 3 /* Cboth */ : 84 return types.SendRecv 85 default: 86 errorf("unexpected channel dir %d", d) 87 return 0 88 } 89 } 90 91 var predeclOnce sync.Once 92 var predecl []types.Type // initialized lazily 93 94 func predeclared() []types.Type { 95 predeclOnce.Do(func() { 96 // initialize lazily to be sure that all 97 // elements have been initialized before 98 predecl = []types.Type{ // basic types 99 types.Typ[types.Bool], 100 types.Typ[types.Int], 101 types.Typ[types.Int8], 102 types.Typ[types.Int16], 103 types.Typ[types.Int32], 104 types.Typ[types.Int64], 105 types.Typ[types.Uint], 106 types.Typ[types.Uint8], 107 types.Typ[types.Uint16], 108 types.Typ[types.Uint32], 109 types.Typ[types.Uint64], 110 types.Typ[types.Uintptr], 111 types.Typ[types.Float32], 112 types.Typ[types.Float64], 113 types.Typ[types.Complex64], 114 types.Typ[types.Complex128], 115 types.Typ[types.String], 116 117 // basic type aliases 118 types.Universe.Lookup("byte").Type(), 119 types.Universe.Lookup("rune").Type(), 120 121 // error 122 types.Universe.Lookup("error").Type(), 123 124 // untyped types 125 types.Typ[types.UntypedBool], 126 types.Typ[types.UntypedInt], 127 types.Typ[types.UntypedRune], 128 types.Typ[types.UntypedFloat], 129 types.Typ[types.UntypedComplex], 130 types.Typ[types.UntypedString], 131 types.Typ[types.UntypedNil], 132 133 // package unsafe 134 types.Typ[types.UnsafePointer], 135 136 // invalid type 137 types.Typ[types.Invalid], // only appears in packages with errors 138 139 // used internally by gc; never used by this package or in .a files 140 anyType{}, 141 } 142 predecl = append(predecl, additionalPredeclared()...) 143 }) 144 return predecl 145 } 146 147 type anyType struct{} 148 149 func (t anyType) Underlying() types.Type { return t } 150 func (t anyType) String() string { return "any" }