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.
20 "cmd/compile/internal/base"
21 "cmd/compile/internal/importer"
22 "cmd/compile/internal/ir"
23 "cmd/compile/internal/syntax"
24 "cmd/compile/internal/typecheck"
25 "cmd/compile/internal/types"
26 "cmd/compile/internal/types2"
31 // ParseFiles concurrently parses files into *syntax.File structures.
32 // Each declaration in every *syntax.File is converted to a syntax tree
33 // and its root represented by *Node is appended to Target.Decls.
34 // Returns the total count of parsed lines.
35 func ParseFiles(filenames []string) (lines uint) {
36 noders := make([]*noder, 0, len(filenames))
37 // Limit the number of simultaneously open files.
38 sem := make(chan struct{}, runtime.GOMAXPROCS(0)+10)
40 for _, filename := range filenames {
42 basemap: make(map[*syntax.PosBase]*src.PosBase),
43 err: make(chan syntax.Error),
44 trackScopes: base.Flag.Dwarf,
46 noders = append(noders, p)
48 go func(filename string) {
50 defer func() { <-sem }()
52 fbase := syntax.NewFileBase(filename)
54 f, err := os.Open(filename)
56 p.error(syntax.Error{Msg: err.Error()})
61 mode := syntax.CheckBranches
63 mode |= syntax.AllowGenerics
65 p.file, _ = syntax.Parse(fbase, f, p.error, p.pragma, mode) // errors are tracked via p.error
69 // generic noding phase (using new typechecker)
71 // setup and syntax error reporting
72 nodersmap := make(map[string]*noder)
73 var files []*syntax.File
74 for _, p := range noders {
75 for e := range p.err {
76 p.errorAt(e.Pos, "%s", e.Msg)
79 nodersmap[p.file.Pos().RelFilename()] = p
80 files = append(files, p.file)
81 lines += p.file.EOF.Line()
84 if base.SyntaxErrors() != 0 {
89 conf := types2.Config{
90 InferFromConstraints: true,
91 IgnoreBranches: true, // parser already checked via syntax.CheckBranches mode
92 CompilerErrorMessages: true, // use error strings matching existing compiler errors
93 Error: func(err error) {
94 terr := err.(types2.Error)
95 if len(terr.Msg) > 0 && terr.Msg[0] == '\t' {
96 // types2 reports error clarifications via separate
97 // error messages which are indented with a tab.
98 // Ignore them to satisfy tools and tests that expect
99 // only one error in such cases.
100 // TODO(gri) Need to adjust error reporting in types2.
103 p := nodersmap[terr.Pos.RelFilename()]
104 base.ErrorfAt(p.makeXPos(terr.Pos), "%s", terr.Msg)
106 Importer: &gcimports{
107 packages: make(map[string]*types2.Package),
108 lookup: func(path string) (io.ReadCloser, error) {
109 file, ok := findpkg(path)
111 return nil, fmt.Errorf("can't find import: %q", path)
118 Types: make(map[syntax.Expr]types2.TypeAndValue),
119 Defs: make(map[*syntax.Name]types2.Object),
120 Uses: make(map[*syntax.Name]types2.Object),
121 Selections: make(map[*syntax.SelectorExpr]*types2.Selection),
124 conf.Check(base.Ctxt.Pkgpath, files, &info)
131 for _, p := range noders {
132 // errors have already been reported
136 lines += p.file.EOF.Line()
137 p.file = nil // release memory
140 // Always run testdclstack here, even when debug_dclstack is not set, as a sanity measure.
141 types.CheckDclstack()
144 types.LocalPkg.Height = myheight
148 // traditional (non-generic) noding phase
149 for _, p := range noders {
150 for e := range p.err {
151 p.errorAt(e.Pos, "%s", e.Msg)
155 lines += p.file.EOF.Line()
156 p.file = nil // release memory
157 if base.SyntaxErrors() != 0 {
161 // Always run testdclstack here, even when debug_dclstack is not set, as a sanity measure.
162 types.CheckDclstack()
165 for _, p := range noders {
169 types.LocalPkg.Height = myheight
173 // Temporary import helper to get type2-based type-checking going.
174 type gcimports struct {
175 packages map[string]*types2.Package
176 lookup func(path string) (io.ReadCloser, error)
179 func (m *gcimports) Import(path string) (*types2.Package, error) {
180 return m.ImportFrom(path, "" /* no vendoring */, 0)
183 func (m *gcimports) ImportFrom(path, srcDir string, mode types2.ImportMode) (*types2.Package, error) {
185 panic("mode must be 0")
187 return importer.Import(m.packages, path, srcDir, m.lookup)
190 // makeSrcPosBase translates from a *syntax.PosBase to a *src.PosBase.
191 func (p *noder) makeSrcPosBase(b0 *syntax.PosBase) *src.PosBase {
192 // fast path: most likely PosBase hasn't changed
193 if p.basecache.last == b0 {
194 return p.basecache.base
197 b1, ok := p.basemap[b0]
201 b1 = src.NewFileBase(fn, absFilename(fn))
203 // line directive base
207 panic("infinite recursion in makeSrcPosBase")
209 p1 := src.MakePos(p.makeSrcPosBase(p0b), p0.Line(), p0.Col())
210 b1 = src.NewLinePragmaBase(p1, fn, fileh(fn), b0.Line(), b0.Col())
216 p.basecache.last = b0
217 p.basecache.base = b1
222 func (p *noder) makeXPos(pos syntax.Pos) (_ src.XPos) {
223 return base.Ctxt.PosTable.XPos(src.MakePos(p.makeSrcPosBase(pos.Base()), pos.Line(), pos.Col()))
226 func (p *noder) errorAt(pos syntax.Pos, format string, args ...interface{}) {
227 base.ErrorfAt(p.makeXPos(pos), format, args...)
230 // TODO(gri) Can we eliminate fileh in favor of absFilename?
231 func fileh(name string) string {
232 return objabi.AbsFile("", name, base.Flag.TrimPath)
235 func absFilename(name string) string {
236 return objabi.AbsFile(base.Ctxt.Pathname, name, base.Flag.TrimPath)
239 // noder transforms package syntax's AST into a Node tree.
241 basemap map[*syntax.PosBase]*src.PosBase
249 pragcgobuf [][]string
250 err chan syntax.Error
255 // scopeVars is a stack tracking the number of variables declared in the
256 // current function at the moment each open scope was opened.
260 // typeInfo provides access to the type information computed by the new
261 // typechecker. It is only present if -G is set, and all noders point to
262 // the same types.Info. For now this is a local field, if need be we can
264 typeInfo *types2.Info
266 lastCloseScopePos syntax.Pos
269 // For now we provide these basic accessors to get to type and object
270 // information of expression nodes during noding. Eventually we will
271 // attach this information directly to the syntax tree which should
272 // simplify access and make it more efficient as well.
274 // typ returns the type and value information for the given expression.
275 func (p *noder) typ(x syntax.Expr) types2.TypeAndValue {
276 return p.typeInfo.Types[x]
279 // def returns the object for the given name in its declaration.
280 func (p *noder) def(x *syntax.Name) types2.Object {
281 return p.typeInfo.Defs[x]
284 // use returns the object for the given name outside its declaration.
285 func (p *noder) use(x *syntax.Name) types2.Object {
286 return p.typeInfo.Uses[x]
289 // sel returns the selection information for the given selector expression.
290 func (p *noder) sel(x *syntax.SelectorExpr) *types2.Selection {
291 return p.typeInfo.Selections[x]
294 func (p *noder) funcBody(fn *ir.Func, block *syntax.BlockStmt) {
297 typecheck.StartFuncBody(fn)
300 body := p.stmts(block.List)
302 body = []ir.Node{ir.NewBlockStmt(base.Pos, nil)}
306 base.Pos = p.makeXPos(block.Rbrace)
307 fn.Endlineno = base.Pos
310 typecheck.FinishFuncBody()
314 func (p *noder) openScope(pos syntax.Pos) {
318 ir.CurFunc.Parents = append(ir.CurFunc.Parents, p.scope)
319 p.scopeVars = append(p.scopeVars, len(ir.CurFunc.Dcl))
320 p.scope = ir.ScopeID(len(ir.CurFunc.Parents))
326 func (p *noder) closeScope(pos syntax.Pos) {
327 p.lastCloseScopePos = pos
331 scopeVars := p.scopeVars[len(p.scopeVars)-1]
332 p.scopeVars = p.scopeVars[:len(p.scopeVars)-1]
333 if scopeVars == len(ir.CurFunc.Dcl) {
334 // no variables were declared in this scope, so we can retract it.
336 if int(p.scope) != len(ir.CurFunc.Parents) {
337 base.Fatalf("scope tracking inconsistency, no variables declared but scopes were not retracted")
340 p.scope = ir.CurFunc.Parents[p.scope-1]
341 ir.CurFunc.Parents = ir.CurFunc.Parents[:len(ir.CurFunc.Parents)-1]
343 nmarks := len(ir.CurFunc.Marks)
344 ir.CurFunc.Marks[nmarks-1].Scope = p.scope
345 prevScope := ir.ScopeID(0)
347 prevScope = ir.CurFunc.Marks[nmarks-2].Scope
349 if ir.CurFunc.Marks[nmarks-1].Scope == prevScope {
350 ir.CurFunc.Marks = ir.CurFunc.Marks[:nmarks-1]
355 p.scope = ir.CurFunc.Parents[p.scope-1]
361 func (p *noder) markScope(pos syntax.Pos) {
362 xpos := p.makeXPos(pos)
363 if i := len(ir.CurFunc.Marks); i > 0 && ir.CurFunc.Marks[i-1].Pos == xpos {
364 ir.CurFunc.Marks[i-1].Scope = p.scope
366 ir.CurFunc.Marks = append(ir.CurFunc.Marks, ir.Mark{Pos: xpos, Scope: p.scope})
370 // closeAnotherScope is like closeScope, but it reuses the same mark
371 // position as the last closeScope call. This is useful for "for" and
372 // "if" statements, as their implicit blocks always end at the same
373 // position as an explicit block.
374 func (p *noder) closeAnotherScope() {
375 p.closeScope(p.lastCloseScopePos)
378 // linkname records a //go:linkname directive.
379 type linkname struct {
385 func (p *noder) node() {
387 p.importedUnsafe = false
388 p.importedEmbed = false
390 p.setlineno(p.file.PkgName)
391 mkpackage(p.file.PkgName.Value)
393 if pragma, ok := p.file.Pragma.(*pragmas); ok {
394 pragma.Flag &^= ir.GoBuildPragma
395 p.checkUnused(pragma)
398 typecheck.Target.Decls = append(typecheck.Target.Decls, p.decls(p.file.DeclList)...)
400 base.Pos = src.NoXPos
404 func (p *noder) processPragmas() {
405 for _, l := range p.linknames {
406 if !p.importedUnsafe {
407 p.errorAt(l.pos, "//go:linkname only allowed in Go files that import \"unsafe\"")
410 n := ir.AsNode(typecheck.Lookup(l.local).Def)
411 if n == nil || n.Op() != ir.ONAME {
412 // TODO(mdempsky): Change to p.errorAt before Go 1.17 release.
413 // base.WarnfAt(p.makeXPos(l.pos), "//go:linkname must refer to declared function or variable (will be an error in Go 1.17)")
416 if n.Sym().Linkname != "" {
417 p.errorAt(l.pos, "duplicate //go:linkname for %s", l.local)
420 n.Sym().Linkname = l.remote
422 typecheck.Target.CgoPragmas = append(typecheck.Target.CgoPragmas, p.pragcgobuf...)
425 func (p *noder) decls(decls []syntax.Decl) (l []ir.Node) {
428 for _, decl := range decls {
430 switch decl := decl.(type) {
431 case *syntax.ImportDecl:
434 case *syntax.VarDecl:
435 l = append(l, p.varDecl(decl)...)
437 case *syntax.ConstDecl:
438 l = append(l, p.constDecl(decl, &cs)...)
440 case *syntax.TypeDecl:
441 l = append(l, p.typeDecl(decl))
443 case *syntax.FuncDecl:
444 l = append(l, p.funcDecl(decl))
447 panic("unhandled Decl")
454 func (p *noder) importDecl(imp *syntax.ImportDecl) {
455 if imp.Path == nil || imp.Path.Bad {
456 return // avoid follow-on errors if there was a syntax error
459 if pragma, ok := imp.Pragma.(*pragmas); ok {
460 p.checkUnused(pragma)
463 ipkg := importfile(p.basicLit(imp.Path))
465 if base.Errors() == 0 {
466 base.Fatalf("phase error in import")
471 if ipkg == ir.Pkgs.Unsafe {
472 p.importedUnsafe = true
474 if ipkg.Path == "embed" {
475 p.importedEmbed = true
479 typecheck.Target.Imports = append(typecheck.Target.Imports, ipkg)
484 if imp.LocalPkgName != nil {
485 my = p.name(imp.LocalPkgName)
487 my = typecheck.Lookup(ipkg.Name)
490 pack := ir.NewPkgName(p.pos(imp), my, ipkg)
497 base.ErrorfAt(pack.Pos(), "cannot import package as init - init must be a func")
503 typecheck.Redeclared(pack.Pos(), my, "as imported package name")
506 my.Lastlineno = pack.Pos()
507 my.Block = 1 // at top level
510 func (p *noder) varDecl(decl *syntax.VarDecl) []ir.Node {
511 names := p.declNames(ir.ONAME, decl.NameList)
512 typ := p.typeExprOrNil(decl.Type)
513 exprs := p.exprList(decl.Values)
515 if pragma, ok := decl.Pragma.(*pragmas); ok {
516 if len(pragma.Embeds) > 0 {
517 if !p.importedEmbed {
518 // This check can't be done when building the list pragma.Embeds
519 // because that list is created before the noder starts walking over the file,
520 // so at that point it hasn't seen the imports.
521 // We're left to check now, just before applying the //go:embed lines.
522 for _, e := range pragma.Embeds {
523 p.errorAt(e.Pos, "//go:embed only allowed in Go files that import \"embed\"")
526 exprs = varEmbed(p, names, typ, exprs, pragma.Embeds)
530 p.checkUnused(pragma)
534 return typecheck.DeclVars(names, typ, exprs)
537 // constState tracks state between constant specifiers within a
538 // declaration group. This state is kept separate from noder so nested
539 // constant declarations are handled correctly (e.g., issue 15550).
540 type constState struct {
547 func (p *noder) constDecl(decl *syntax.ConstDecl, cs *constState) []ir.Node {
548 if decl.Group == nil || decl.Group != cs.group {
554 if pragma, ok := decl.Pragma.(*pragmas); ok {
555 p.checkUnused(pragma)
558 names := p.declNames(ir.OLITERAL, decl.NameList)
559 typ := p.typeExprOrNil(decl.Type)
562 if decl.Values != nil {
563 values = p.exprList(decl.Values)
564 cs.typ, cs.values = typ, values
567 base.Errorf("const declaration cannot have type without expression")
569 typ, values = cs.typ, cs.values
572 nn := make([]ir.Node, 0, len(names))
573 for i, n := range names {
574 if i >= len(values) {
575 base.Errorf("missing value in const declaration")
579 if decl.Values == nil {
580 v = ir.DeepCopy(n.Pos(), v)
582 typecheck.Declare(n, typecheck.DeclContext)
588 nn = append(nn, ir.NewDecl(p.pos(decl), ir.ODCLCONST, n))
591 if len(values) > len(names) {
592 base.Errorf("extra expression in const declaration")
600 func (p *noder) typeDecl(decl *syntax.TypeDecl) ir.Node {
601 n := p.declName(ir.OTYPE, decl.Name)
602 typecheck.Declare(n, typecheck.DeclContext)
604 // decl.Type may be nil but in that case we got a syntax error during parsing
605 typ := p.typeExprOrNil(decl.Type)
608 n.SetAlias(decl.Alias)
609 if pragma, ok := decl.Pragma.(*pragmas); ok {
611 n.SetPragma(pragma.Flag & typePragmas)
612 pragma.Flag &^= typePragmas
614 p.checkUnused(pragma)
617 nod := ir.NewDecl(p.pos(decl), ir.ODCLTYPE, n)
618 if n.Alias() && !types.AllowsGoVersion(types.LocalPkg, 1, 9) {
619 base.ErrorfAt(nod.Pos(), "type aliases only supported as of -lang=go1.9")
624 func (p *noder) declNames(op ir.Op, names []*syntax.Name) []*ir.Name {
625 nodes := make([]*ir.Name, 0, len(names))
626 for _, name := range names {
627 nodes = append(nodes, p.declName(op, name))
632 func (p *noder) declName(op ir.Op, name *syntax.Name) *ir.Name {
633 return ir.NewDeclNameAt(p.pos(name), op, p.name(name))
636 func (p *noder) funcDecl(fun *syntax.FuncDecl) ir.Node {
637 name := p.name(fun.Name)
638 t := p.signature(fun.Recv, fun.Type)
639 f := ir.NewFunc(p.pos(fun))
642 if name.Name == "init" {
644 if len(t.Params) > 0 || len(t.Results) > 0 {
645 base.ErrorfAt(f.Pos(), "func init must have no arguments and no return values")
647 typecheck.Target.Inits = append(typecheck.Target.Inits, f)
650 if types.LocalPkg.Name == "main" && name.Name == "main" {
651 if len(t.Params) > 0 || len(t.Results) > 0 {
652 base.ErrorfAt(f.Pos(), "func main must have no arguments and no return values")
657 name = ir.BlankNode.Sym() // filled in by typecheckfunc
660 f.Nname = ir.NewFuncNameAt(p.pos(fun.Name), name, f)
664 if pragma, ok := fun.Pragma.(*pragmas); ok {
665 f.Pragma = pragma.Flag & funcPragmas
666 if pragma.Flag&ir.Systemstack != 0 && pragma.Flag&ir.Nosplit != 0 {
667 base.ErrorfAt(f.Pos(), "go:nosplit and go:systemstack cannot be combined")
669 pragma.Flag &^= funcPragmas
670 p.checkUnused(pragma)
674 typecheck.Declare(f.Nname, ir.PFUNC)
677 p.funcBody(f, fun.Body)
680 if f.Pragma&ir.Noescape != 0 {
681 base.ErrorfAt(f.Pos(), "can only use //go:noescape with external func implementations")
684 if base.Flag.Complete || strings.HasPrefix(ir.FuncName(f), "init.") {
685 // Linknamed functions are allowed to have no body. Hopefully
686 // the linkname target has a body. See issue 23311.
688 for _, n := range p.linknames {
689 if ir.FuncName(f) == n.local {
695 base.ErrorfAt(f.Pos(), "missing function body")
703 func (p *noder) signature(recv *syntax.Field, typ *syntax.FuncType) *ir.FuncType {
706 rcvr = p.param(recv, false, false)
708 return ir.NewFuncType(p.pos(typ), rcvr,
709 p.params(typ.ParamList, true),
710 p.params(typ.ResultList, false))
713 func (p *noder) params(params []*syntax.Field, dddOk bool) []*ir.Field {
714 nodes := make([]*ir.Field, 0, len(params))
715 for i, param := range params {
717 nodes = append(nodes, p.param(param, dddOk, i+1 == len(params)))
722 func (p *noder) param(param *syntax.Field, dddOk, final bool) *ir.Field {
724 if param.Name != nil {
725 name = p.name(param.Name)
728 typ := p.typeExpr(param.Type)
729 n := ir.NewField(p.pos(param), name, typ, nil)
731 // rewrite ...T parameter
732 if typ, ok := typ.(*ir.SliceType); ok && typ.DDD {
734 // We mark these as syntax errors to get automatic elimination
735 // of multiple such errors per line (see ErrorfAt in subr.go).
736 base.Errorf("syntax error: cannot use ... in receiver or result parameter list")
738 if param.Name == nil {
739 base.Errorf("syntax error: cannot use ... with non-final parameter")
741 p.errorAt(param.Name.Pos(), "syntax error: cannot use ... with non-final parameter %s", param.Name.Value)
751 func (p *noder) exprList(expr syntax.Expr) []ir.Node {
752 switch expr := expr.(type) {
755 case *syntax.ListExpr:
756 return p.exprs(expr.ElemList)
758 return []ir.Node{p.expr(expr)}
762 func (p *noder) exprs(exprs []syntax.Expr) []ir.Node {
763 nodes := make([]ir.Node, 0, len(exprs))
764 for _, expr := range exprs {
765 nodes = append(nodes, p.expr(expr))
770 func (p *noder) expr(expr syntax.Expr) ir.Node {
772 switch expr := expr.(type) {
773 case nil, *syntax.BadExpr:
776 return p.mkname(expr)
777 case *syntax.BasicLit:
778 n := ir.NewBasicLit(p.pos(expr), p.basicLit(expr))
779 if expr.Kind == syntax.RuneLit {
780 n.SetType(types.UntypedRune)
782 n.SetDiag(expr.Bad) // avoid follow-on errors if there was a syntax error
784 case *syntax.CompositeLit:
785 n := ir.NewCompLitExpr(p.pos(expr), ir.OCOMPLIT, p.typeExpr(expr.Type), nil)
786 l := p.exprs(expr.ElemList)
787 for i, e := range l {
788 l[i] = p.wrapname(expr.ElemList[i], e)
791 base.Pos = p.makeXPos(expr.Rbrace)
793 case *syntax.KeyValueExpr:
794 // use position of expr.Key rather than of expr (which has position of ':')
795 return ir.NewKeyExpr(p.pos(expr.Key), p.expr(expr.Key), p.wrapname(expr.Value, p.expr(expr.Value)))
796 case *syntax.FuncLit:
797 return p.funcLit(expr)
798 case *syntax.ParenExpr:
799 return ir.NewParenExpr(p.pos(expr), p.expr(expr.X))
800 case *syntax.SelectorExpr:
801 // parser.new_dotname
802 obj := p.expr(expr.X)
803 if obj.Op() == ir.OPACK {
804 pack := obj.(*ir.PkgName)
806 return importName(pack.Pkg.Lookup(expr.Sel.Value))
808 n := ir.NewSelectorExpr(base.Pos, ir.OXDOT, obj, p.name(expr.Sel))
809 n.SetPos(p.pos(expr)) // lineno may have been changed by p.expr(expr.X)
811 case *syntax.IndexExpr:
812 return ir.NewIndexExpr(p.pos(expr), p.expr(expr.X), p.expr(expr.Index))
813 case *syntax.SliceExpr:
820 for i, n := range &expr.Index {
825 return ir.NewSliceExpr(p.pos(expr), op, x, index[0], index[1], index[2])
826 case *syntax.AssertExpr:
827 return ir.NewTypeAssertExpr(p.pos(expr), p.expr(expr.X), p.typeExpr(expr.Type))
828 case *syntax.Operation:
829 if expr.Op == syntax.Add && expr.Y != nil {
834 pos, op := p.pos(expr), p.unOp(expr.Op)
837 return typecheck.NodAddrAt(pos, x)
839 return ir.NewStarExpr(pos, x)
841 return ir.NewUnaryExpr(pos, op, x)
844 pos, op, y := p.pos(expr), p.binOp(expr.Op), p.expr(expr.Y)
846 case ir.OANDAND, ir.OOROR:
847 return ir.NewLogicalExpr(pos, op, x, y)
849 return ir.NewBinaryExpr(pos, op, x, y)
850 case *syntax.CallExpr:
851 n := ir.NewCallExpr(p.pos(expr), ir.OCALL, p.expr(expr.Fun), p.exprs(expr.ArgList))
852 n.IsDDD = expr.HasDots
855 case *syntax.ArrayType:
858 len = p.expr(expr.Len)
860 return ir.NewArrayType(p.pos(expr), len, p.typeExpr(expr.Elem))
861 case *syntax.SliceType:
862 return ir.NewSliceType(p.pos(expr), p.typeExpr(expr.Elem))
863 case *syntax.DotsType:
864 t := ir.NewSliceType(p.pos(expr), p.typeExpr(expr.Elem))
867 case *syntax.StructType:
868 return p.structType(expr)
869 case *syntax.InterfaceType:
870 return p.interfaceType(expr)
871 case *syntax.FuncType:
872 return p.signature(nil, expr)
873 case *syntax.MapType:
874 return ir.NewMapType(p.pos(expr),
875 p.typeExpr(expr.Key), p.typeExpr(expr.Value))
876 case *syntax.ChanType:
877 return ir.NewChanType(p.pos(expr),
878 p.typeExpr(expr.Elem), p.chanDir(expr.Dir))
880 case *syntax.TypeSwitchGuard:
883 tag = ir.NewIdent(p.pos(expr.Lhs), p.name(expr.Lhs))
885 base.Errorf("invalid variable name %v in type switch", tag)
888 return ir.NewTypeSwitchGuard(p.pos(expr), tag, p.expr(expr.X))
890 panic("unhandled Expr")
893 // sum efficiently handles very large summation expressions (such as
894 // in issue #16394). In particular, it avoids left recursion and
895 // collapses string literals.
896 func (p *noder) sum(x syntax.Expr) ir.Node {
897 // While we need to handle long sums with asymptotic
898 // efficiency, the vast majority of sums are very small: ~95%
899 // have only 2 or 3 operands, and ~99% of string literals are
900 // never concatenated.
902 adds := make([]*syntax.Operation, 0, 2)
904 add, ok := x.(*syntax.Operation)
905 if !ok || add.Op != syntax.Add || add.Y == nil {
908 adds = append(adds, add)
912 // nstr is the current rightmost string literal in the
913 // summation (if any), and chunks holds its accumulated
916 // Consider the expression x + "a" + "b" + "c" + y. When we
917 // reach the string literal "a", we assign nstr to point to
918 // its corresponding Node and initialize chunks to {"a"}.
919 // Visiting the subsequent string literals "b" and "c", we
920 // simply append their values to chunks. Finally, when we
921 // reach the non-constant operand y, we'll join chunks to form
922 // "abc" and reassign the "a" string literal's value.
924 // N.B., we need to be careful about named string constants
925 // (indicated by Sym != nil) because 1) we can't modify their
926 // value, as doing so would affect other uses of the string
927 // constant, and 2) they may have types, which we need to
928 // handle correctly. For now, we avoid these problems by
929 // treating named string constants the same as non-constant
932 chunks := make([]string, 0, 1)
935 if ir.IsConst(n, constant.String) && n.Sym() == nil {
937 chunks = append(chunks, ir.StringVal(nstr))
940 for i := len(adds) - 1; i >= 0; i-- {
944 if ir.IsConst(r, constant.String) && r.Sym() == nil {
946 // Collapse r into nstr instead of adding to n.
947 chunks = append(chunks, ir.StringVal(r))
952 chunks = append(chunks, ir.StringVal(nstr))
955 nstr.SetVal(constant.MakeString(strings.Join(chunks, "")))
960 n = ir.NewBinaryExpr(p.pos(add), ir.OADD, n, r)
963 nstr.SetVal(constant.MakeString(strings.Join(chunks, "")))
969 func (p *noder) typeExpr(typ syntax.Expr) ir.Ntype {
970 // TODO(mdempsky): Be stricter? typecheck should handle errors anyway.
975 if _, ok := n.(ir.Ntype); !ok {
976 ir.Dump("NOT NTYPE", n)
981 func (p *noder) typeExprOrNil(typ syntax.Expr) ir.Ntype {
983 return p.typeExpr(typ)
988 func (p *noder) chanDir(dir syntax.ChanDir) types.ChanDir {
992 case syntax.SendOnly:
994 case syntax.RecvOnly:
997 panic("unhandled ChanDir")
1000 func (p *noder) structType(expr *syntax.StructType) ir.Node {
1001 l := make([]*ir.Field, 0, len(expr.FieldList))
1002 for i, field := range expr.FieldList {
1005 if field.Name == nil {
1006 n = p.embedded(field.Type)
1008 n = ir.NewField(p.pos(field), p.name(field.Name), p.typeExpr(field.Type), nil)
1010 if i < len(expr.TagList) && expr.TagList[i] != nil {
1011 n.Note = constant.StringVal(p.basicLit(expr.TagList[i]))
1017 return ir.NewStructType(p.pos(expr), l)
1020 func (p *noder) interfaceType(expr *syntax.InterfaceType) ir.Node {
1021 l := make([]*ir.Field, 0, len(expr.MethodList))
1022 for _, method := range expr.MethodList {
1025 if method.Name == nil {
1026 n = ir.NewField(p.pos(method), nil, importName(p.packname(method.Type)).(ir.Ntype), nil)
1028 mname := p.name(method.Name)
1029 if mname.IsBlank() {
1030 base.Errorf("methods must have a unique non-blank name")
1033 sig := p.typeExpr(method.Type).(*ir.FuncType)
1034 sig.Recv = fakeRecv()
1035 n = ir.NewField(p.pos(method), mname, sig, nil)
1040 return ir.NewInterfaceType(p.pos(expr), l)
1043 func (p *noder) packname(expr syntax.Expr) *types.Sym {
1044 switch expr := expr.(type) {
1046 name := p.name(expr)
1047 if n := oldname(name); n.Name() != nil && n.Name().PkgName != nil {
1048 n.Name().PkgName.Used = true
1051 case *syntax.SelectorExpr:
1052 name := p.name(expr.X.(*syntax.Name))
1053 def := ir.AsNode(name.Def)
1055 base.Errorf("undefined: %v", name)
1059 if def.Op() != ir.OPACK {
1060 base.Errorf("%v is not a package", name)
1061 pkg = types.LocalPkg
1063 def := def.(*ir.PkgName)
1067 return pkg.Lookup(expr.Sel.Value)
1069 panic(fmt.Sprintf("unexpected packname: %#v", expr))
1072 func (p *noder) embedded(typ syntax.Expr) *ir.Field {
1073 op, isStar := typ.(*syntax.Operation)
1075 if op.Op != syntax.Mul || op.Y != nil {
1076 panic("unexpected Operation")
1081 sym := p.packname(typ)
1082 n := ir.NewField(p.pos(typ), typecheck.Lookup(sym.Name), importName(sym).(ir.Ntype), nil)
1086 n.Ntype = ir.NewStarExpr(p.pos(op), n.Ntype)
1091 func (p *noder) stmts(stmts []syntax.Stmt) []ir.Node {
1092 return p.stmtsFall(stmts, false)
1095 func (p *noder) stmtsFall(stmts []syntax.Stmt, fallOK bool) []ir.Node {
1097 for i, stmt := range stmts {
1098 s := p.stmtFall(stmt, fallOK && i+1 == len(stmts))
1100 } else if s.Op() == ir.OBLOCK && len(s.(*ir.BlockStmt).List) > 0 {
1101 // Inline non-empty block.
1102 // Empty blocks must be preserved for checkreturn.
1103 nodes = append(nodes, s.(*ir.BlockStmt).List...)
1105 nodes = append(nodes, s)
1111 func (p *noder) stmt(stmt syntax.Stmt) ir.Node {
1112 return p.stmtFall(stmt, false)
1115 func (p *noder) stmtFall(stmt syntax.Stmt, fallOK bool) ir.Node {
1117 switch stmt := stmt.(type) {
1118 case nil, *syntax.EmptyStmt:
1120 case *syntax.LabeledStmt:
1121 return p.labeledStmt(stmt, fallOK)
1122 case *syntax.BlockStmt:
1123 l := p.blockStmt(stmt)
1125 // TODO(mdempsky): Line number?
1126 return ir.NewBlockStmt(base.Pos, nil)
1128 return ir.NewBlockStmt(src.NoXPos, l)
1129 case *syntax.ExprStmt:
1130 return p.wrapname(stmt, p.expr(stmt.X))
1131 case *syntax.SendStmt:
1132 return ir.NewSendStmt(p.pos(stmt), p.expr(stmt.Chan), p.expr(stmt.Value))
1133 case *syntax.DeclStmt:
1134 return ir.NewBlockStmt(src.NoXPos, p.decls(stmt.DeclList))
1135 case *syntax.AssignStmt:
1136 if stmt.Op != 0 && stmt.Op != syntax.Def {
1137 n := ir.NewAssignOpStmt(p.pos(stmt), p.binOp(stmt.Op), p.expr(stmt.Lhs), p.expr(stmt.Rhs))
1138 n.IncDec = stmt.Rhs == syntax.ImplicitOne
1142 rhs := p.exprList(stmt.Rhs)
1143 if list, ok := stmt.Lhs.(*syntax.ListExpr); ok && len(list.ElemList) != 1 || len(rhs) != 1 {
1144 n := ir.NewAssignListStmt(p.pos(stmt), ir.OAS2, nil, nil)
1145 n.Def = stmt.Op == syntax.Def
1146 n.Lhs.Set(p.assignList(stmt.Lhs, n, n.Def))
1151 n := ir.NewAssignStmt(p.pos(stmt), nil, nil)
1152 n.Def = stmt.Op == syntax.Def
1153 n.X = p.assignList(stmt.Lhs, n, n.Def)[0]
1157 case *syntax.BranchStmt:
1162 case syntax.Continue:
1164 case syntax.Fallthrough:
1166 base.Errorf("fallthrough statement out of place")
1172 panic("unhandled BranchStmt")
1175 if stmt.Label != nil {
1176 sym = p.name(stmt.Label)
1178 return ir.NewBranchStmt(p.pos(stmt), op, sym)
1179 case *syntax.CallStmt:
1187 panic("unhandled CallStmt")
1189 return ir.NewGoDeferStmt(p.pos(stmt), op, p.expr(stmt.Call))
1190 case *syntax.ReturnStmt:
1191 n := ir.NewReturnStmt(p.pos(stmt), p.exprList(stmt.Results))
1192 if len(n.Results) == 0 && ir.CurFunc != nil {
1193 for _, ln := range ir.CurFunc.Dcl {
1194 if ln.Class_ == ir.PPARAM {
1197 if ln.Class_ != ir.PPARAMOUT {
1200 if ln.Sym().Def != ln {
1201 base.Errorf("%s is shadowed during return", ln.Sym().Name)
1206 case *syntax.IfStmt:
1207 return p.ifStmt(stmt)
1208 case *syntax.ForStmt:
1209 return p.forStmt(stmt)
1210 case *syntax.SwitchStmt:
1211 return p.switchStmt(stmt)
1212 case *syntax.SelectStmt:
1213 return p.selectStmt(stmt)
1215 panic("unhandled Stmt")
1218 func (p *noder) assignList(expr syntax.Expr, defn ir.Node, colas bool) []ir.Node {
1220 return p.exprList(expr)
1223 var exprs []syntax.Expr
1224 if list, ok := expr.(*syntax.ListExpr); ok {
1225 exprs = list.ElemList
1227 exprs = []syntax.Expr{expr}
1230 res := make([]ir.Node, len(exprs))
1231 seen := make(map[*types.Sym]bool, len(exprs))
1234 for i, expr := range exprs {
1236 res[i] = ir.BlankNode
1238 name, ok := expr.(*syntax.Name)
1240 p.errorAt(expr.Pos(), "non-name %v on left side of :=", p.expr(expr))
1251 p.errorAt(expr.Pos(), "%v repeated on left side of :=", sym)
1257 if sym.Block == types.Block {
1258 res[i] = oldname(sym)
1263 n := typecheck.NewName(sym)
1264 typecheck.Declare(n, typecheck.DeclContext)
1266 defn.PtrInit().Append(ir.NewDecl(base.Pos, ir.ODCL, n))
1271 base.ErrorfAt(defn.Pos(), "no new variables on left side of :=")
1276 func (p *noder) blockStmt(stmt *syntax.BlockStmt) []ir.Node {
1277 p.openScope(stmt.Pos())
1278 nodes := p.stmts(stmt.List)
1279 p.closeScope(stmt.Rbrace)
1283 func (p *noder) ifStmt(stmt *syntax.IfStmt) ir.Node {
1284 p.openScope(stmt.Pos())
1285 init := p.stmt(stmt.Init)
1286 n := ir.NewIfStmt(p.pos(stmt), p.expr(stmt.Cond), p.blockStmt(stmt.Then), nil)
1288 *n.PtrInit() = []ir.Node{init}
1290 if stmt.Else != nil {
1291 e := p.stmt(stmt.Else)
1292 if e.Op() == ir.OBLOCK {
1293 e := e.(*ir.BlockStmt)
1296 n.Else = []ir.Node{e}
1299 p.closeAnotherScope()
1303 func (p *noder) forStmt(stmt *syntax.ForStmt) ir.Node {
1304 p.openScope(stmt.Pos())
1305 if r, ok := stmt.Init.(*syntax.RangeClause); ok {
1306 if stmt.Cond != nil || stmt.Post != nil {
1307 panic("unexpected RangeClause")
1310 n := ir.NewRangeStmt(p.pos(r), nil, nil, p.expr(r.X), nil)
1313 lhs := p.assignList(r.Lhs, n, n.Def)
1319 n.Body.Set(p.blockStmt(stmt.Body))
1320 p.closeAnotherScope()
1324 n := ir.NewForStmt(p.pos(stmt), p.stmt(stmt.Init), p.expr(stmt.Cond), p.stmt(stmt.Post), p.blockStmt(stmt.Body))
1325 p.closeAnotherScope()
1329 func (p *noder) switchStmt(stmt *syntax.SwitchStmt) ir.Node {
1330 p.openScope(stmt.Pos())
1332 init := p.stmt(stmt.Init)
1333 n := ir.NewSwitchStmt(p.pos(stmt), p.expr(stmt.Tag), nil)
1335 *n.PtrInit() = []ir.Node{init}
1338 var tswitch *ir.TypeSwitchGuard
1339 if l := n.Tag; l != nil && l.Op() == ir.OTYPESW {
1340 tswitch = l.(*ir.TypeSwitchGuard)
1342 n.Cases = p.caseClauses(stmt.Body, tswitch, stmt.Rbrace)
1344 p.closeScope(stmt.Rbrace)
1348 func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *ir.TypeSwitchGuard, rbrace syntax.Pos) []*ir.CaseClause {
1349 nodes := make([]*ir.CaseClause, 0, len(clauses))
1350 for i, clause := range clauses {
1353 p.closeScope(clause.Pos())
1355 p.openScope(clause.Pos())
1357 n := ir.NewCaseStmt(p.pos(clause), p.exprList(clause.Cases), nil)
1358 if tswitch != nil && tswitch.Tag != nil {
1359 nn := typecheck.NewName(tswitch.Tag.Sym())
1360 typecheck.Declare(nn, typecheck.DeclContext)
1362 // keep track of the instances for reporting unused
1366 // Trim trailing empty statements. We omit them from
1367 // the Node AST anyway, and it's easier to identify
1368 // out-of-place fallthrough statements without them.
1371 if _, ok := body[len(body)-1].(*syntax.EmptyStmt); !ok {
1374 body = body[:len(body)-1]
1377 n.Body.Set(p.stmtsFall(body, true))
1378 if l := len(n.Body); l > 0 && n.Body[l-1].Op() == ir.OFALL {
1380 base.Errorf("cannot fallthrough in type switch")
1382 if i+1 == len(clauses) {
1383 base.Errorf("cannot fallthrough final case in switch")
1387 nodes = append(nodes, n)
1389 if len(clauses) > 0 {
1390 p.closeScope(rbrace)
1395 func (p *noder) selectStmt(stmt *syntax.SelectStmt) ir.Node {
1396 return ir.NewSelectStmt(p.pos(stmt), p.commClauses(stmt.Body, stmt.Rbrace))
1399 func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []*ir.CommClause {
1400 nodes := make([]*ir.CommClause, len(clauses))
1401 for i, clause := range clauses {
1404 p.closeScope(clause.Pos())
1406 p.openScope(clause.Pos())
1408 nodes[i] = ir.NewCommStmt(p.pos(clause), p.stmt(clause.Comm), p.stmts(clause.Body))
1410 if len(clauses) > 0 {
1411 p.closeScope(rbrace)
1416 func (p *noder) labeledStmt(label *syntax.LabeledStmt, fallOK bool) ir.Node {
1417 sym := p.name(label.Label)
1418 lhs := ir.NewLabelStmt(p.pos(label), sym)
1421 if label.Stmt != nil { // TODO(mdempsky): Should always be present.
1422 ls = p.stmtFall(label.Stmt, fallOK)
1423 // Attach label directly to control statement too.
1427 ls := ls.(*ir.ForStmt)
1430 ls := ls.(*ir.RangeStmt)
1433 ls := ls.(*ir.SwitchStmt)
1436 ls := ls.(*ir.SelectStmt)
1444 if ls.Op() == ir.OBLOCK {
1445 ls := ls.(*ir.BlockStmt)
1446 l = append(l, ls.List...)
1451 return ir.NewBlockStmt(src.NoXPos, l)
1454 var unOps = [...]ir.Op{
1455 syntax.Recv: ir.ORECV,
1456 syntax.Mul: ir.ODEREF,
1457 syntax.And: ir.OADDR,
1459 syntax.Not: ir.ONOT,
1460 syntax.Xor: ir.OBITNOT,
1461 syntax.Add: ir.OPLUS,
1462 syntax.Sub: ir.ONEG,
1465 func (p *noder) unOp(op syntax.Operator) ir.Op {
1466 if uint64(op) >= uint64(len(unOps)) || unOps[op] == 0 {
1467 panic("invalid Operator")
1472 var binOps = [...]ir.Op{
1473 syntax.OrOr: ir.OOROR,
1474 syntax.AndAnd: ir.OANDAND,
1483 syntax.Add: ir.OADD,
1484 syntax.Sub: ir.OSUB,
1486 syntax.Xor: ir.OXOR,
1488 syntax.Mul: ir.OMUL,
1489 syntax.Div: ir.ODIV,
1490 syntax.Rem: ir.OMOD,
1491 syntax.And: ir.OAND,
1492 syntax.AndNot: ir.OANDNOT,
1493 syntax.Shl: ir.OLSH,
1494 syntax.Shr: ir.ORSH,
1497 func (p *noder) binOp(op syntax.Operator) ir.Op {
1498 if uint64(op) >= uint64(len(binOps)) || binOps[op] == 0 {
1499 panic("invalid Operator")
1504 // checkLangCompat reports an error if the representation of a numeric
1505 // literal is not compatible with the current language version.
1506 func checkLangCompat(lit *syntax.BasicLit) {
1508 if len(s) <= 2 || types.AllowsGoVersion(types.LocalPkg, 1, 13) {
1512 if strings.Contains(s, "_") {
1513 base.ErrorfVers("go1.13", "underscores in numeric literals")
1520 if radix == 'b' || radix == 'B' {
1521 base.ErrorfVers("go1.13", "binary literals")
1524 if radix == 'o' || radix == 'O' {
1525 base.ErrorfVers("go1.13", "0o/0O-style octal literals")
1528 if lit.Kind != syntax.IntLit && (radix == 'x' || radix == 'X') {
1529 base.ErrorfVers("go1.13", "hexadecimal floating-point literals")
1533 func (p *noder) basicLit(lit *syntax.BasicLit) constant.Value {
1534 // We don't use the errors of the conversion routines to determine
1535 // if a literal string is valid because the conversion routines may
1536 // accept a wider syntax than the language permits. Rely on lit.Bad
1539 return constant.MakeUnknown()
1543 case syntax.IntLit, syntax.FloatLit, syntax.ImagLit:
1544 checkLangCompat(lit)
1547 v := constant.MakeFromLiteral(lit.Value, tokenForLitKind[lit.Kind], 0)
1548 if v.Kind() == constant.Unknown {
1549 // TODO(mdempsky): Better error message?
1550 p.errorAt(lit.Pos(), "malformed constant: %s", lit.Value)
1553 // go/constant uses big.Rat by default, which is more precise, but
1554 // causes toolstash -cmp and some tests to fail. For now, convert
1555 // to big.Float to match cmd/compile's historical precision.
1556 // TODO(mdempsky): Remove.
1557 if v.Kind() == constant.Float {
1558 v = constant.Make(ir.BigFloat(v))
1564 var tokenForLitKind = [...]token.Token{
1565 syntax.IntLit: token.INT,
1566 syntax.RuneLit: token.CHAR,
1567 syntax.FloatLit: token.FLOAT,
1568 syntax.ImagLit: token.IMAG,
1569 syntax.StringLit: token.STRING,
1572 func (p *noder) name(name *syntax.Name) *types.Sym {
1573 return typecheck.Lookup(name.Value)
1576 func (p *noder) mkname(name *syntax.Name) ir.Node {
1577 // TODO(mdempsky): Set line number?
1578 return mkname(p.name(name))
1581 func (p *noder) wrapname(n syntax.Node, x ir.Node) ir.Node {
1582 // These nodes do not carry line numbers.
1583 // Introduce a wrapper node to give them the correct line.
1585 case ir.OTYPE, ir.OLITERAL:
1590 case ir.ONAME, ir.ONONAME, ir.OPACK:
1591 p := ir.NewParenExpr(p.pos(n), x)
1598 func (p *noder) pos(n syntax.Node) src.XPos {
1599 // TODO(gri): orig.Pos() should always be known - fix package syntax
1601 if pos := n.Pos(); pos.IsKnown() {
1602 xpos = p.makeXPos(pos)
1607 func (p *noder) setlineno(n syntax.Node) {
1613 // error is called concurrently if files are parsed concurrently.
1614 func (p *noder) error(err error) {
1615 p.err <- err.(syntax.Error)
1618 // pragmas that are allowed in the std lib, but don't have
1619 // a syntax.Pragma value (see lex.go) associated with them.
1620 var allowedStdPragmas = map[string]bool{
1621 "go:cgo_export_static": true,
1622 "go:cgo_export_dynamic": true,
1623 "go:cgo_import_static": true,
1624 "go:cgo_import_dynamic": true,
1625 "go:cgo_ldflag": true,
1626 "go:cgo_dynamic_linker": true,
1628 "go:generate": true,
1631 // *pragmas is the value stored in a syntax.pragmas during parsing.
1632 type pragmas struct {
1633 Flag ir.PragmaFlag // collected bits
1634 Pos []pragmaPos // position of each individual flag
1635 Embeds []pragmaEmbed
1638 type pragmaPos struct {
1643 type pragmaEmbed struct {
1648 func (p *noder) checkUnused(pragma *pragmas) {
1649 for _, pos := range pragma.Pos {
1650 if pos.Flag&pragma.Flag != 0 {
1651 p.errorAt(pos.Pos, "misplaced compiler directive")
1654 if len(pragma.Embeds) > 0 {
1655 for _, e := range pragma.Embeds {
1656 p.errorAt(e.Pos, "misplaced go:embed directive")
1661 func (p *noder) checkUnusedDuringParse(pragma *pragmas) {
1662 for _, pos := range pragma.Pos {
1663 if pos.Flag&pragma.Flag != 0 {
1664 p.error(syntax.Error{Pos: pos.Pos, Msg: "misplaced compiler directive"})
1667 if len(pragma.Embeds) > 0 {
1668 for _, e := range pragma.Embeds {
1669 p.error(syntax.Error{Pos: e.Pos, Msg: "misplaced go:embed directive"})
1674 // pragma is called concurrently if files are parsed concurrently.
1675 func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.Pragma) syntax.Pragma {
1676 pragma, _ := old.(*pragmas)
1678 pragma = new(pragmas)
1682 // unused pragma; only called with old != nil.
1683 p.checkUnusedDuringParse(pragma)
1687 if strings.HasPrefix(text, "line ") {
1688 // line directives are handled by syntax package
1689 panic("unreachable")
1693 // directive must be on line by itself
1694 p.error(syntax.Error{Pos: pos, Msg: "misplaced compiler directive"})
1699 case strings.HasPrefix(text, "go:linkname "):
1700 f := strings.Fields(text)
1701 if !(2 <= len(f) && len(f) <= 3) {
1702 p.error(syntax.Error{Pos: pos, Msg: "usage: //go:linkname localname [linkname]"})
1705 // The second argument is optional. If omitted, we use
1706 // the default object symbol name for this and
1707 // linkname only serves to mark this symbol as
1708 // something that may be referenced via the object
1709 // symbol name from another package.
1713 } else if base.Ctxt.Pkgpath != "" {
1714 // Use the default object symbol name if the
1715 // user didn't provide one.
1716 target = objabi.PathToPrefix(base.Ctxt.Pkgpath) + "." + f[1]
1718 p.error(syntax.Error{Pos: pos, Msg: "//go:linkname requires linkname argument or -p compiler flag"})
1721 p.linknames = append(p.linknames, linkname{pos, f[1], target})
1723 case text == "go:embed", strings.HasPrefix(text, "go:embed "):
1724 args, err := parseGoEmbed(text[len("go:embed"):])
1726 p.error(syntax.Error{Pos: pos, Msg: err.Error()})
1729 p.error(syntax.Error{Pos: pos, Msg: "usage: //go:embed pattern..."})
1732 pragma.Embeds = append(pragma.Embeds, pragmaEmbed{pos, args})
1734 case strings.HasPrefix(text, "go:cgo_import_dynamic "):
1735 // This is permitted for general use because Solaris
1736 // code relies on it in golang.org/x/sys/unix and others.
1737 fields := pragmaFields(text)
1738 if len(fields) >= 4 {
1739 lib := strings.Trim(fields[3], `"`)
1740 if lib != "" && !safeArg(lib) && !isCgoGeneratedFile(pos) {
1741 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("invalid library name %q in cgo_import_dynamic directive", lib)})
1743 p.pragcgo(pos, text)
1744 pragma.Flag |= pragmaFlag("go:cgo_import_dynamic")
1748 case strings.HasPrefix(text, "go:cgo_"):
1749 // For security, we disallow //go:cgo_* directives other
1750 // than cgo_import_dynamic outside cgo-generated files.
1751 // Exception: they are allowed in the standard library, for runtime and syscall.
1752 if !isCgoGeneratedFile(pos) && !base.Flag.Std {
1753 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s only allowed in cgo-generated code", text)})
1755 p.pragcgo(pos, text)
1756 fallthrough // because of //go:cgo_unsafe_args
1759 if i := strings.Index(text, " "); i >= 0 {
1762 flag := pragmaFlag(verb)
1763 const runtimePragmas = ir.Systemstack | ir.Nowritebarrier | ir.Nowritebarrierrec | ir.Yeswritebarrierrec
1764 if !base.Flag.CompilingRuntime && flag&runtimePragmas != 0 {
1765 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s only allowed in runtime", verb)})
1767 if flag == 0 && !allowedStdPragmas[verb] && base.Flag.Std {
1768 p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s is not allowed in the standard library", verb)})
1771 pragma.Pos = append(pragma.Pos, pragmaPos{flag, pos})
1777 // isCgoGeneratedFile reports whether pos is in a file
1778 // generated by cgo, which is to say a file with name
1779 // beginning with "_cgo_". Such files are allowed to
1780 // contain cgo directives, and for security reasons
1781 // (primarily misuse of linker flags), other files are not.
1782 // See golang.org/issue/23672.
1783 func isCgoGeneratedFile(pos syntax.Pos) bool {
1784 return strings.HasPrefix(filepath.Base(filepath.Clean(fileh(pos.Base().Filename()))), "_cgo_")
1787 // safeArg reports whether arg is a "safe" command-line argument,
1788 // meaning that when it appears in a command-line, it probably
1789 // doesn't have some special meaning other than its own name.
1790 // This is copied from SafeArg in cmd/go/internal/load/pkg.go.
1791 func safeArg(name string) bool {
1796 return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
1799 func mkname(sym *types.Sym) ir.Node {
1801 if n.Name() != nil && n.Name().PkgName != nil {
1802 n.Name().PkgName.Used = true
1807 // parseGoEmbed parses the text following "//go:embed" to extract the glob patterns.
1808 // It accepts unquoted space-separated patterns as well as double-quoted and back-quoted Go strings.
1809 // go/build/read.go also processes these strings and contains similar logic.
1810 func parseGoEmbed(args string) ([]string, error) {
1812 for args = strings.TrimSpace(args); args != ""; args = strings.TrimSpace(args) {
1818 for j, c := range args {
1819 if unicode.IsSpace(c) {
1828 i := strings.Index(args[1:], "`")
1830 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
1832 path = args[1 : 1+i]
1837 for ; i < len(args); i++ {
1838 if args[i] == '\\' {
1843 q, err := strconv.Unquote(args[:i+1])
1845 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args[:i+1])
1853 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
1858 r, _ := utf8.DecodeRuneInString(args)
1859 if !unicode.IsSpace(r) {
1860 return nil, fmt.Errorf("invalid quoted string in //go:embed: %s", args)
1863 list = append(list, path)
1868 func fakeRecv() *ir.Field {
1869 return ir.NewField(base.Pos, nil, nil, types.FakeRecvType())
1872 func (p *noder) funcLit(expr *syntax.FuncLit) ir.Node {
1873 xtype := p.typeExpr(expr.Type)
1874 ntype := p.typeExpr(expr.Type)
1876 fn := ir.NewFunc(p.pos(expr))
1877 fn.SetIsHiddenClosure(ir.CurFunc != nil)
1878 fn.Nname = ir.NewFuncNameAt(p.pos(expr), ir.BlankNode.Sym(), fn) // filled in by typecheckclosure
1879 fn.Nname.Ntype = xtype
1882 clo := ir.NewClosureExpr(p.pos(expr), fn)
1883 fn.ClosureType = ntype
1886 p.funcBody(fn, expr.Body)
1888 // closure-specific variables are hanging off the
1889 // ordinary ones in the symbol table; see oldname.
1891 // make the list of pointers for the closure call.
1892 for _, v := range fn.ClosureVars {
1893 // Unlink from v1; see comment in syntax.go type Param for these fields.
1895 v1.Name().Innermost = v.Outer
1897 // If the closure usage of v is not dense,
1898 // we need to make it dense; now that we're out
1899 // of the function in which v appeared,
1900 // look up v.Sym in the enclosing function
1901 // and keep it around for use in the compiled code.
1903 // That is, suppose we just finished parsing the innermost
1904 // closure f4 in this code:
1918 // At this point v.Outer is f2's v; there is no f3's v.
1919 // To construct the closure f4 from within f3,
1920 // we need to use f3's v and in this case we need to create f3's v.
1921 // We are now in the context of f3, so calling oldname(v.Sym)
1922 // obtains f3's v, creating it if necessary (as it is in the example).
1924 // capturevars will decide whether to use v directly or &v.
1925 v.Outer = oldname(v.Sym()).(*ir.Name)
1931 // A function named init is a special case.
1932 // It is called by the initialization before main is run.
1933 // To make it unique within a package and also uncallable,
1934 // the name, normally "pkg.init", is altered to "pkg.init.0".
1935 var renameinitgen int
1937 func renameinit() *types.Sym {
1938 s := typecheck.LookupNum("init.", renameinitgen)
1943 // oldname returns the Node that declares symbol s in the current scope.
1944 // If no such Node currently exists, an ONONAME Node is returned instead.
1945 // Automatically creates a new closure variable if the referenced symbol was
1946 // declared in a different (containing) function.
1947 func oldname(s *types.Sym) ir.Node {
1948 if s.Pkg != types.LocalPkg {
1949 return ir.NewIdent(base.Pos, s)
1952 n := ir.AsNode(s.Def)
1954 // Maybe a top-level declaration will come along later to
1955 // define s. resolve will check s.Def again once all input
1956 // source has been processed.
1957 return ir.NewIdent(base.Pos, s)
1960 if ir.CurFunc != nil && n.Op() == ir.ONAME && n.Name().Curfn != nil && n.Name().Curfn != ir.CurFunc {
1961 // Inner func is referring to var in outer func.
1963 // TODO(rsc): If there is an outer variable x and we
1964 // are parsing x := 5 inside the closure, until we get to
1965 // the := it looks like a reference to the outer x so we'll
1966 // make x a closure variable unnecessarily.
1968 c := n.Name().Innermost
1969 if c == nil || c.Curfn != ir.CurFunc {
1970 // Do not have a closure var for the active closure yet; make one.
1971 c = typecheck.NewName(s)
1972 c.Class_ = ir.PAUTOHEAP
1973 c.SetIsClosureVar(true)
1976 // Link into list of active closure variables.
1977 // Popped from list in func funcLit.
1978 c.Outer = n.Name().Innermost
1979 n.Name().Innermost = c
1981 ir.CurFunc.ClosureVars = append(ir.CurFunc.ClosureVars, c)
1984 // return ref to closure var, not original
1991 func varEmbed(p *noder, names []*ir.Name, typ ir.Ntype, exprs []ir.Node, embeds []pragmaEmbed) (newExprs []ir.Node) {
1993 for _, decl := range p.file.DeclList {
1994 imp, ok := decl.(*syntax.ImportDecl)
1996 // imports always come first
1999 path, _ := strconv.Unquote(imp.Path.Value)
2000 if path == "embed" {
2006 pos := embeds[0].Pos
2008 p.errorAt(pos, "invalid go:embed: missing import \"embed\"")
2011 if base.Flag.Cfg.Embed.Patterns == nil {
2012 p.errorAt(pos, "invalid go:embed: build system did not supply embed configuration")
2016 p.errorAt(pos, "go:embed cannot apply to multiple vars")
2020 p.errorAt(pos, "go:embed cannot apply to var with initializer")
2024 // Should not happen, since len(exprs) == 0 now.
2025 p.errorAt(pos, "go:embed cannot apply to var without type")
2028 if typecheck.DeclContext != ir.PEXTERN {
2029 p.errorAt(pos, "go:embed cannot apply to var inside func")
2034 typecheck.Target.Embeds = append(typecheck.Target.Embeds, v)
2035 v.Embed = new([]ir.Embed)
2036 for _, e := range embeds {
2037 *v.Embed = append(*v.Embed, ir.Embed{Pos: p.makeXPos(e.Pos), Patterns: e.Patterns})