1
2
3
4
5 package noder
6
7 import (
8 "errors"
9 "fmt"
10 "internal/buildcfg"
11 "os"
12 "path/filepath"
13 "runtime"
14 "strconv"
15 "strings"
16 "unicode"
17 "unicode/utf8"
18
19 "cmd/compile/internal/base"
20 "cmd/compile/internal/ir"
21 "cmd/compile/internal/syntax"
22 "cmd/compile/internal/typecheck"
23 "cmd/compile/internal/types"
24 "cmd/internal/objabi"
25 )
26
27 func LoadPackage(filenames []string) {
28 base.Timer.Start("fe", "parse")
29
30
31 sem := make(chan struct{}, runtime.GOMAXPROCS(0)+10)
32
33 noders := make([]*noder, len(filenames))
34 for i := range noders {
35 p := noder{
36 err: make(chan syntax.Error),
37 }
38 noders[i] = &p
39 }
40
41
42 go func() {
43 for i, filename := range filenames {
44 filename := filename
45 p := noders[i]
46 sem <- struct{}{}
47 go func() {
48 defer func() { <-sem }()
49 defer close(p.err)
50 fbase := syntax.NewFileBase(filename)
51
52 f, err := os.Open(filename)
53 if err != nil {
54 p.error(syntax.Error{Msg: err.Error()})
55 return
56 }
57 defer f.Close()
58
59 p.file, _ = syntax.Parse(fbase, f, p.error, p.pragma, syntax.CheckBranches)
60 }()
61 }
62 }()
63
64 var lines uint
65 var m posMap
66 for _, p := range noders {
67 for e := range p.err {
68 base.ErrorfAt(m.makeXPos(e.Pos), 0, "%s", e.Msg)
69 }
70 if p.file == nil {
71 base.ErrorExit()
72 }
73 lines += p.file.EOF.Line()
74 }
75 base.Timer.AddEvent(int64(lines), "lines")
76
77 unified(m, noders)
78 }
79
80
81
82
83
84
85
86
87 func trimFilename(b *syntax.PosBase) string {
88 filename := b.Filename()
89 if !b.Trimmed() {
90 dir := ""
91 if b.IsFileBase() {
92 dir = base.Ctxt.Pathname
93 }
94 filename = objabi.AbsFile(dir, filename, base.Flag.TrimPath)
95 }
96 return filename
97 }
98
99
100 type noder struct {
101 file *syntax.File
102 linknames []linkname
103 pragcgobuf [][]string
104 err chan syntax.Error
105 }
106
107
108 type linkname struct {
109 pos syntax.Pos
110 local string
111 remote string
112 }
113
114 var unOps = [...]ir.Op{
115 syntax.Recv: ir.ORECV,
116 syntax.Mul: ir.ODEREF,
117 syntax.And: ir.OADDR,
118
119 syntax.Not: ir.ONOT,
120 syntax.Xor: ir.OBITNOT,
121 syntax.Add: ir.OPLUS,
122 syntax.Sub: ir.ONEG,
123 }
124
125 var binOps = [...]ir.Op{
126 syntax.OrOr: ir.OOROR,
127 syntax.AndAnd: ir.OANDAND,
128
129 syntax.Eql: ir.OEQ,
130 syntax.Neq: ir.ONE,
131 syntax.Lss: ir.OLT,
132 syntax.Leq: ir.OLE,
133 syntax.Gtr: ir.OGT,
134 syntax.Geq: ir.OGE,
135
136 syntax.Add: ir.OADD,
137 syntax.Sub: ir.OSUB,
138 syntax.Or: ir.OOR,
139 syntax.Xor: ir.OXOR,
140
141 syntax.Mul: ir.OMUL,
142 syntax.Div: ir.ODIV,
143 syntax.Rem: ir.OMOD,
144 syntax.And: ir.OAND,
145 syntax.AndNot: ir.OANDNOT,
146 syntax.Shl: ir.OLSH,
147 syntax.Shr: ir.ORSH,
148 }
149
150
151 func (p *noder) error(err error) {
152 p.err <- err.(syntax.Error)
153 }
154
155
156
157 var allowedStdPragmas = map[string]bool{
158 "go:cgo_export_static": true,
159 "go:cgo_export_dynamic": true,
160 "go:cgo_import_static": true,
161 "go:cgo_import_dynamic": true,
162 "go:cgo_ldflag": true,
163 "go:cgo_dynamic_linker": true,
164 "go:embed": true,
165 "go:fix": true,
166 "go:generate": true,
167 }
168
169
170 type pragmas struct {
171 Flag ir.PragmaFlag
172 Pos []pragmaPos
173 Embeds []pragmaEmbed
174 WasmImport *WasmImport
175 WasmExport *WasmExport
176 }
177
178
179 type WasmImport struct {
180 Pos syntax.Pos
181 Module string
182 Name string
183 }
184
185
186 type WasmExport struct {
187 Pos syntax.Pos
188 Name string
189 }
190
191 type pragmaPos struct {
192 Flag ir.PragmaFlag
193 Pos syntax.Pos
194 }
195
196 type pragmaEmbed struct {
197 Pos syntax.Pos
198 Patterns []string
199 }
200
201 func (p *noder) checkUnusedDuringParse(pragma *pragmas) {
202 for _, pos := range pragma.Pos {
203 if pos.Flag&pragma.Flag != 0 {
204 p.error(syntax.Error{Pos: pos.Pos, Msg: "misplaced compiler directive"})
205 }
206 }
207 if len(pragma.Embeds) > 0 {
208 for _, e := range pragma.Embeds {
209 p.error(syntax.Error{Pos: e.Pos, Msg: "misplaced go:embed directive"})
210 }
211 }
212 if pragma.WasmImport != nil {
213 p.error(syntax.Error{Pos: pragma.WasmImport.Pos, Msg: "misplaced go:wasmimport directive"})
214 }
215 if pragma.WasmExport != nil {
216 p.error(syntax.Error{Pos: pragma.WasmExport.Pos, Msg: "misplaced go:wasmexport directive"})
217 }
218 }
219
220
221 func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.Pragma) syntax.Pragma {
222 pragma, _ := old.(*pragmas)
223 if pragma == nil {
224 pragma = new(pragmas)
225 }
226
227 if text == "" {
228
229 p.checkUnusedDuringParse(pragma)
230 return nil
231 }
232
233 if strings.HasPrefix(text, "line ") {
234
235 panic("unreachable")
236 }
237
238 if !blankLine {
239
240 p.error(syntax.Error{Pos: pos, Msg: "misplaced compiler directive"})
241 return pragma
242 }
243
244 switch {
245 case strings.HasPrefix(text, "go:wasmimport "):
246 f := strings.Fields(text)
247 if len(f) != 3 {
248 p.error(syntax.Error{Pos: pos, Msg: "usage: //go:wasmimport importmodule importname"})
249 break
250 }
251
252 if buildcfg.GOARCH == "wasm" {
253
254 pragma.WasmImport = &WasmImport{
255 Pos: pos,
256 Module: f[1],
257 Name: f[2],
258 }
259 }
260
261 case strings.HasPrefix(text, "go:wasmexport "):
262 f := strings.Fields(text)
263 if len(f) != 2 {
264
265 p.error(syntax.Error{Pos: pos, Msg: "usage: //go:wasmexport exportname"})
266 break
267 }
268
269 if buildcfg.GOARCH == "wasm" {
270
271 pragma.WasmExport = &WasmExport{
272 Pos: pos,
273 Name: f[1],
274 }
275 }
276
277 case strings.HasPrefix(text, "go:linkname "):
278 f := strings.Fields(text)
279 if !(2 <= len(f) && len(f) <= 3) {
280 p.error(syntax.Error{Pos: pos, Msg: "usage: //go:linkname localname [linkname]"})
281 break
282 }
283
284
285
286
287
288 var target string
289 if len(f) == 3 {
290 target = f[2]
291 } else if base.Ctxt.Pkgpath != "" {
292
293
294 target = objabi.PathToPrefix(base.Ctxt.Pkgpath) + "." + f[1]
295 } else {
296 panic("missing pkgpath")
297 }
298 p.linknames = append(p.linknames, linkname{pos, f[1], target})
299
300 case text == "go:embed", strings.HasPrefix(text, "go:embed "):
301 args, err := parseGoEmbed(text[len("go:embed"):])
302 if err != nil {
303 p.error(syntax.Error{Pos: pos, Msg: err.Error()})
304 }
305 if len(args) == 0 {
306 p.error(syntax.Error{Pos: pos, Msg: "usage: //go:embed pattern..."})
307 break
308 }
309 pragma.Embeds = append(pragma.Embeds, pragmaEmbed{pos, args})
310
311 case strings.HasPrefix(text, "go:cgo_import_dynamic "):
312
313
314 fields := pragmaFields(text)
315 if len(fields) >= 4 {
316 lib := strings.Trim(fields[3], `"`)
317 if lib != "" && !safeArg(lib) && !isCgoGeneratedFile(pos) {
318 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("invalid library name %q in cgo_import_dynamic directive", lib)})
319 }
320 p.pragcgo(pos, text)
321 pragma.Flag |= pragmaFlag("go:cgo_import_dynamic")
322 break
323 }
324 fallthrough
325 case strings.HasPrefix(text, "go:cgo_"):
326
327
328
329 if !isCgoGeneratedFile(pos) && !base.Flag.Std {
330 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s only allowed in cgo-generated code", text)})
331 }
332 p.pragcgo(pos, text)
333 fallthrough
334 default:
335 verb := text
336 if i := strings.Index(text, " "); i >= 0 {
337 verb = verb[:i]
338 }
339 flag := pragmaFlag(verb)
340 const runtimePragmas = ir.Systemstack | ir.Nowritebarrier | ir.Nowritebarrierrec | ir.Yeswritebarrierrec
341 if !base.Flag.CompilingRuntime && flag&runtimePragmas != 0 {
342 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s only allowed in runtime", verb)})
343 }
344 if flag == ir.UintptrKeepAlive && !base.Flag.Std {
345 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s is only allowed in the standard library", verb)})
346 }
347 if flag == 0 && !allowedStdPragmas[verb] && base.Flag.Std {
348 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s is not allowed in the standard library", verb)})
349 }
350 pragma.Flag |= flag
351 pragma.Pos = append(pragma.Pos, pragmaPos{flag, pos})
352 }
353
354 return pragma
355 }
356
357
358
359
360
361
362
363
364
365
366
367 func isCgoGeneratedFile(pos syntax.Pos) bool {
368
369
370 return strings.HasPrefix(filepath.Base(trimFilename(pos.Base().Pos().Base())), "_cgo_")
371 }
372
373
374
375
376
377 func safeArg(name string) bool {
378 if name == "" {
379 return false
380 }
381 c := name[0]
382 return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
383 }
384
385
386
387
388 func parseGoEmbed(args string) ([]string, error) {
389 var list []string
390 for args = strings.TrimSpace(args); args != ""; args = strings.TrimSpace(args) {
391 var path string
392 Switch:
393 switch args[0] {
394 default:
395 i := len(args)
396 for j, c := range args {
397 if unicode.IsSpace(c) {
398 i = j
399 break
400 }
401 }
402 path = args[:i]
403 args = args[i:]
404
405 case '`':
406 i := strings.Index(args[1:], "`")
407 if i < 0 {
408 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
409 }
410 path = args[1 : 1+i]
411 args = args[1+i+1:]
412
413 case '"':
414 i := 1
415 for ; i < len(args); i++ {
416 if args[i] == '\\' {
417 i++
418 continue
419 }
420 if args[i] == '"' {
421 q, err := strconv.Unquote(args[:i+1])
422 if err != nil {
423 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args[:i+1])
424 }
425 path = q
426 args = args[i+1:]
427 break Switch
428 }
429 }
430 if i >= len(args) {
431 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
432 }
433 }
434
435 if args != "" {
436 r, _ := utf8.DecodeRuneInString(args)
437 if !unicode.IsSpace(r) {
438 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
439 }
440 }
441 list = append(list, path)
442 }
443 return list, nil
444 }
445
446
447
448
449
450 var renameinitgen int
451
452 func Renameinit() *types.Sym {
453 s := typecheck.LookupNum("init.", renameinitgen)
454 renameinitgen++
455 return s
456 }
457
458 func checkEmbed(decl *syntax.VarDecl, haveEmbed, withinFunc bool) error {
459 switch {
460 case !haveEmbed:
461 return errors.New("go:embed only allowed in Go files that import \"embed\"")
462 case len(decl.NameList) > 1:
463 return errors.New("go:embed cannot apply to multiple vars")
464 case decl.Values != nil:
465 return errors.New("go:embed cannot apply to var with initializer")
466 case decl.Type == nil:
467
468 return errors.New("go:embed cannot apply to var without type")
469 case withinFunc:
470 return errors.New("go:embed cannot apply to var inside func")
471 case !types.AllowsGoVersion(1, 16):
472 return fmt.Errorf("go:embed requires go1.16 or later (-lang was set to %s; check go.mod)", base.Flag.Lang)
473
474 default:
475 return nil
476 }
477 }
478
View as plain text