1 // Copyright 2016 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.
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"
27 func LoadPackage(filenames []string) {
28 base.Timer.Start("fe", "parse")
30 // Limit the number of simultaneously open files.
31 sem := make(chan struct{}, runtime.GOMAXPROCS(0)+10)
33 noders := make([]*noder, len(filenames))
34 for i := range noders {
36 err: make(chan syntax.Error),
41 // Move the entire syntax processing logic into a separate goroutine to avoid blocking on the "sem".
43 for i, filename := range filenames {
48 defer func() { <-sem }()
50 fbase := syntax.NewFileBase(filename)
52 f, err := os.Open(filename)
54 p.error(syntax.Error{Msg: err.Error()})
59 p.file, _ = syntax.Parse(fbase, f, p.error, p.pragma, syntax.CheckBranches) // errors are tracked via p.error
66 for _, p := range noders {
67 for e := range p.err {
68 base.ErrorfAt(m.makeXPos(e.Pos), "%s", e.Msg)
73 lines += p.file.EOF.Line()
75 base.Timer.AddEvent(int64(lines), "lines")
80 // trimFilename returns the "trimmed" filename of b, which is the
81 // absolute filename after applying -trimpath processing. This
82 // filename form is suitable for use in object files and export data.
84 // If b's filename has already been trimmed (i.e., because it was read
85 // in from an imported package's export data), then the filename is
86 // returned unchanged.
87 func trimFilename(b *syntax.PosBase) string {
88 filename := b.Filename()
92 dir = base.Ctxt.Pathname
94 filename = objabi.AbsFile(dir, filename, base.Flag.TrimPath)
99 // noder transforms package syntax's AST into a Node tree.
103 pragcgobuf [][]string
104 err chan syntax.Error
107 // linkname records a //go:linkname directive.
108 type linkname struct {
114 var unOps = [...]ir.Op{
115 syntax.Recv: ir.ORECV,
116 syntax.Mul: ir.ODEREF,
117 syntax.And: ir.OADDR,
120 syntax.Xor: ir.OBITNOT,
121 syntax.Add: ir.OPLUS,
125 var binOps = [...]ir.Op{
126 syntax.OrOr: ir.OOROR,
127 syntax.AndAnd: ir.OANDAND,
145 syntax.AndNot: ir.OANDNOT,
150 // error is called concurrently if files are parsed concurrently.
151 func (p *noder) error(err error) {
152 p.err <- err.(syntax.Error)
155 // pragmas that are allowed in the std lib, but don't have
156 // a syntax.Pragma value (see lex.go) associated with them.
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,
168 // *pragmas is the value stored in a syntax.pragmas during parsing.
169 type pragmas struct {
170 Flag ir.PragmaFlag // collected bits
171 Pos []pragmaPos // position of each individual flag
173 WasmImport *WasmImport
176 // WasmImport stores metadata associated with the //go:wasmimport pragma
177 type WasmImport struct {
183 type pragmaPos struct {
188 type pragmaEmbed struct {
193 func (p *noder) checkUnusedDuringParse(pragma *pragmas) {
194 for _, pos := range pragma.Pos {
195 if pos.Flag&pragma.Flag != 0 {
196 p.error(syntax.Error{Pos: pos.Pos, Msg: "misplaced compiler directive"})
199 if len(pragma.Embeds) > 0 {
200 for _, e := range pragma.Embeds {
201 p.error(syntax.Error{Pos: e.Pos, Msg: "misplaced go:embed directive"})
204 if pragma.WasmImport != nil {
205 p.error(syntax.Error{Pos: pragma.WasmImport.Pos, Msg: "misplaced go:wasmimport directive"})
209 // pragma is called concurrently if files are parsed concurrently.
210 func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.Pragma) syntax.Pragma {
211 pragma, _ := old.(*pragmas)
213 pragma = new(pragmas)
217 // unused pragma; only called with old != nil.
218 p.checkUnusedDuringParse(pragma)
222 if strings.HasPrefix(text, "line ") {
223 // line directives are handled by syntax package
228 // directive must be on line by itself
229 p.error(syntax.Error{Pos: pos, Msg: "misplaced compiler directive"})
234 case strings.HasPrefix(text, "go:wasmimport "):
235 f := strings.Fields(text)
237 p.error(syntax.Error{Pos: pos, Msg: "usage: //go:wasmimport importmodule importname"})
240 if !base.Flag.CompilingRuntime && base.Ctxt.Pkgpath != "syscall/js" && base.Ctxt.Pkgpath != "syscall/js_test" {
241 p.error(syntax.Error{Pos: pos, Msg: "//go:wasmimport directive cannot be used outside of runtime or syscall/js"})
245 if buildcfg.GOARCH == "wasm" {
246 // Only actually use them if we're compiling to WASM though.
247 pragma.WasmImport = &WasmImport{
253 case strings.HasPrefix(text, "go:linkname "):
254 f := strings.Fields(text)
255 if !(2 <= len(f) && len(f) <= 3) {
256 p.error(syntax.Error{Pos: pos, Msg: "usage: //go:linkname localname [linkname]"})
259 // The second argument is optional. If omitted, we use
260 // the default object symbol name for this and
261 // linkname only serves to mark this symbol as
262 // something that may be referenced via the object
263 // symbol name from another package.
267 } else if base.Ctxt.Pkgpath != "" {
268 // Use the default object symbol name if the
269 // user didn't provide one.
270 target = objabi.PathToPrefix(base.Ctxt.Pkgpath) + "." + f[1]
272 p.error(syntax.Error{Pos: pos, Msg: "//go:linkname requires linkname argument or -p compiler flag"})
275 p.linknames = append(p.linknames, linkname{pos, f[1], target})
277 case text == "go:embed", strings.HasPrefix(text, "go:embed "):
278 args, err := parseGoEmbed(text[len("go:embed"):])
280 p.error(syntax.Error{Pos: pos, Msg: err.Error()})
283 p.error(syntax.Error{Pos: pos, Msg: "usage: //go:embed pattern..."})
286 pragma.Embeds = append(pragma.Embeds, pragmaEmbed{pos, args})
288 case strings.HasPrefix(text, "go:cgo_import_dynamic "):
289 // This is permitted for general use because Solaris
290 // code relies on it in golang.org/x/sys/unix and others.
291 fields := pragmaFields(text)
292 if len(fields) >= 4 {
293 lib := strings.Trim(fields[3], `"`)
294 if lib != "" && !safeArg(lib) && !isCgoGeneratedFile(pos) {
295 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("invalid library name %q in cgo_import_dynamic directive", lib)})
298 pragma.Flag |= pragmaFlag("go:cgo_import_dynamic")
302 case strings.HasPrefix(text, "go:cgo_"):
303 // For security, we disallow //go:cgo_* directives other
304 // than cgo_import_dynamic outside cgo-generated files.
305 // Exception: they are allowed in the standard library, for runtime and syscall.
306 if !isCgoGeneratedFile(pos) && !base.Flag.Std {
307 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s only allowed in cgo-generated code", text)})
310 fallthrough // because of //go:cgo_unsafe_args
313 if i := strings.Index(text, " "); i >= 0 {
316 flag := pragmaFlag(verb)
317 const runtimePragmas = ir.Systemstack | ir.Nowritebarrier | ir.Nowritebarrierrec | ir.Yeswritebarrierrec
318 if !base.Flag.CompilingRuntime && flag&runtimePragmas != 0 {
319 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s only allowed in runtime", verb)})
321 if flag == ir.UintptrKeepAlive && !base.Flag.Std {
322 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s is only allowed in the standard library", verb)})
324 if flag == 0 && !allowedStdPragmas[verb] && base.Flag.Std {
325 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s is not allowed in the standard library", verb)})
328 pragma.Pos = append(pragma.Pos, pragmaPos{flag, pos})
334 // isCgoGeneratedFile reports whether pos is in a file
335 // generated by cgo, which is to say a file with name
336 // beginning with "_cgo_". Such files are allowed to
337 // contain cgo directives, and for security reasons
338 // (primarily misuse of linker flags), other files are not.
339 // See golang.org/issue/23672.
340 func isCgoGeneratedFile(pos syntax.Pos) bool {
341 return strings.HasPrefix(filepath.Base(trimFilename(pos.Base())), "_cgo_")
344 // safeArg reports whether arg is a "safe" command-line argument,
345 // meaning that when it appears in a command-line, it probably
346 // doesn't have some special meaning other than its own name.
347 // This is copied from SafeArg in cmd/go/internal/load/pkg.go.
348 func safeArg(name string) bool {
353 return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
356 // parseGoEmbed parses the text following "//go:embed" to extract the glob patterns.
357 // It accepts unquoted space-separated patterns as well as double-quoted and back-quoted Go strings.
358 // go/build/read.go also processes these strings and contains similar logic.
359 func parseGoEmbed(args string) ([]string, error) {
361 for args = strings.TrimSpace(args); args != ""; args = strings.TrimSpace(args) {
367 for j, c := range args {
368 if unicode.IsSpace(c) {
377 i := strings.Index(args[1:], "`")
379 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
386 for ; i < len(args); i++ {
392 q, err := strconv.Unquote(args[:i+1])
394 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args[:i+1])
402 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
407 r, _ := utf8.DecodeRuneInString(args)
408 if !unicode.IsSpace(r) {
409 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
412 list = append(list, path)
417 // A function named init is a special case.
418 // It is called by the initialization before main is run.
419 // To make it unique within a package and also uncallable,
420 // the name, normally "pkg.init", is altered to "pkg.init.0".
421 var renameinitgen int
423 func Renameinit() *types.Sym {
424 s := typecheck.LookupNum("init.", renameinitgen)
429 func checkEmbed(decl *syntax.VarDecl, haveEmbed, withinFunc bool) error {
432 return errors.New("go:embed only allowed in Go files that import \"embed\"")
433 case len(decl.NameList) > 1:
434 return errors.New("go:embed cannot apply to multiple vars")
435 case decl.Values != nil:
436 return errors.New("go:embed cannot apply to var with initializer")
437 case decl.Type == nil:
438 // Should not happen, since Values == nil now.
439 return errors.New("go:embed cannot apply to var without type")
441 return errors.New("go:embed cannot apply to var inside func")
442 case !types.AllowsGoVersion(1, 16):
443 return fmt.Errorf("go:embed requires go1.16 or later (-lang was set to %s; check go.mod)", base.Flag.Lang)