github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/cmd/compile/internal/gc/parser.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 package gc 6 7 // The recursive-descent parser is built around a slighty modified grammar 8 // of Go to accomodate for the constraints imposed by strict one token look- 9 // ahead, and for better error handling. Subsequent checks of the constructed 10 // syntax tree restrict the language accepted by the compiler to proper Go. 11 // 12 // Semicolons are inserted by the lexer. The parser uses one-token look-ahead 13 // to handle optional commas and semicolons before a closing ) or } . 14 15 import ( 16 "fmt" 17 "strconv" 18 "strings" 19 ) 20 21 const trace = false // if set, parse tracing can be enabled with -x 22 23 // TODO(gri) Once we handle imports w/o redirecting the underlying 24 // source of the lexer we can get rid of these. They are here for 25 // compatibility with the existing yacc-based parser setup (issue 13242). 26 var thenewparser parser // the parser in use 27 var savedstate []parser // saved parser state, used during import 28 29 func push_parser() { 30 // Indentation (for tracing) must be preserved across parsers 31 // since we are changing the lexer source (and parser state) 32 // under foot, in the middle of productions. This won't be 33 // needed anymore once we fix issue 13242, but neither will 34 // be the push/pop_parser functionality. 35 // (Instead we could just use a global variable indent, but 36 // but eventually indent should be parser-specific anyway.) 37 indent := thenewparser.indent 38 savedstate = append(savedstate, thenewparser) 39 thenewparser = parser{indent: indent} // preserve indentation 40 thenewparser.next() 41 } 42 43 func pop_parser() { 44 indent := thenewparser.indent 45 n := len(savedstate) - 1 46 thenewparser = savedstate[n] 47 thenewparser.indent = indent // preserve indentation 48 savedstate = savedstate[:n] 49 } 50 51 // parse_file sets up a new parser and parses a single Go source file. 52 func parse_file() { 53 thenewparser = parser{} 54 thenewparser.loadsys() 55 thenewparser.next() 56 thenewparser.file() 57 } 58 59 // loadsys loads the definitions for the low-level runtime functions, 60 // so that the compiler can generate calls to them, 61 // but does not make the name "runtime" visible as a package. 62 func (p *parser) loadsys() { 63 if trace && Debug['x'] != 0 { 64 defer p.trace("loadsys")() 65 } 66 67 importpkg = Runtimepkg 68 69 if Debug['A'] != 0 { 70 cannedimports("runtime.Builtin", "package runtime\n\n$$\n\n") 71 } else { 72 cannedimports("runtime.Builtin", runtimeimport) 73 } 74 curio.importsafe = true 75 76 p.import_package() 77 p.import_there() 78 79 importpkg = nil 80 } 81 82 type parser struct { 83 tok int32 // next token (one-token look-ahead) 84 op Op // valid if tok == LASOP 85 val Val // valid if tok == LLITERAL 86 sym_ *Sym // valid if tok == LNAME 87 fnest int // function nesting level (for error handling) 88 xnest int // expression nesting level (for complit ambiguity resolution) 89 yy yySymType // for temporary use by next 90 indent []byte // tracing support 91 } 92 93 func (p *parser) next() { 94 p.tok = yylex(&p.yy) 95 p.op = p.yy.op 96 p.val = p.yy.val 97 p.sym_ = p.yy.sym 98 } 99 100 func (p *parser) got(tok int32) bool { 101 if p.tok == tok { 102 p.next() 103 return true 104 } 105 return false 106 } 107 108 func (p *parser) want(tok int32) { 109 if !p.got(tok) { 110 p.syntax_error("expecting " + tokstring(tok)) 111 p.advance() 112 } 113 } 114 115 // ---------------------------------------------------------------------------- 116 // Syntax error handling 117 118 func (p *parser) syntax_error(msg string) { 119 if trace && Debug['x'] != 0 { 120 defer p.trace("syntax_error (" + msg + ")")() 121 } 122 123 if p.tok == EOF && nerrors > 0 { 124 return // avoid meaningless follow-up errors 125 } 126 127 // add punctuation etc. as needed to msg 128 switch { 129 case msg == "": 130 // nothing to do 131 case strings.HasPrefix(msg, "in"), strings.HasPrefix(msg, "at"), strings.HasPrefix(msg, "after"): 132 msg = " " + msg 133 case strings.HasPrefix(msg, "expecting"): 134 msg = ", " + msg 135 default: 136 // plain error - we don't care about current token 137 Yyerror("syntax error: %s", msg) 138 return 139 } 140 141 // determine token string 142 var tok string 143 switch p.tok { 144 case LLITERAL: 145 tok = litbuf 146 case LNAME: 147 if p.sym_ != nil && p.sym_.Name != "" { 148 tok = p.sym_.Name 149 } else { 150 tok = "name" 151 } 152 case LASOP: 153 tok = goopnames[p.op] + "=" 154 default: 155 tok = tokstring(p.tok) 156 } 157 158 Yyerror("syntax error: unexpected %s", tok+msg) 159 } 160 161 // Like syntax_error, but reports error at given line rather than current lexer line. 162 func (p *parser) syntax_error_at(lineno int32, msg string) { 163 defer func(lineno int32) { 164 lexlineno = lineno 165 }(lexlineno) 166 lexlineno = lineno 167 p.syntax_error(msg) 168 } 169 170 // The stoplist contains keywords that start a statement. 171 // They are good synchronization points in case of syntax 172 // errors and (usually) shouldn't be skipped over. 173 var stoplist = map[int32]bool{ 174 LBREAK: true, 175 LCONST: true, 176 LCONTINUE: true, 177 LDEFER: true, 178 LFALL: true, 179 LFOR: true, 180 LFUNC: true, 181 LGO: true, 182 LGOTO: true, 183 LIF: true, 184 LRETURN: true, 185 LSELECT: true, 186 LSWITCH: true, 187 LTYPE: true, 188 LVAR: true, 189 } 190 191 // Advance consumes tokens until it finds a token of the stop- or followlist. 192 // The stoplist is only considered if we are inside a function (p.fnest > 0). 193 // The followlist is the list of valid tokens that can follow a production; 194 // if it is empty, exactly one token is consumed to ensure progress. 195 func (p *parser) advance(followlist ...int32) { 196 if len(followlist) == 0 { 197 p.next() 198 return 199 } 200 for p.tok != EOF { 201 if p.fnest > 0 && stoplist[p.tok] { 202 return 203 } 204 for _, follow := range followlist { 205 if p.tok == follow { 206 return 207 } 208 } 209 p.next() 210 } 211 } 212 213 func tokstring(tok int32) string { 214 switch tok { 215 case EOF: 216 return "EOF" 217 case ',': 218 return "comma" 219 case ';': 220 return "semicolon or newline" 221 } 222 if 0 <= tok && tok < 128 { 223 // get invisibles properly backslashed 224 s := strconv.QuoteRune(tok) 225 if n := len(s); n > 0 && s[0] == '\'' && s[n-1] == '\'' { 226 s = s[1 : n-1] 227 } 228 return s 229 } 230 if s := tokstrings[tok]; s != "" { 231 return s 232 } 233 // catchall 234 return fmt.Sprintf("tok-%v", tok) 235 } 236 237 var tokstrings = map[int32]string{ 238 LLITERAL: "LLITERAL", 239 LASOP: "op=", 240 LCOLAS: ":=", 241 LBREAK: "break", 242 LCASE: "case", 243 LCHAN: "chan", 244 LCONST: "const", 245 LCONTINUE: "continue", 246 LDDD: "...", 247 LDEFAULT: "default", 248 LDEFER: "defer", 249 LELSE: "else", 250 LFALL: "fallthrough", 251 LFOR: "for", 252 LFUNC: "func", 253 LGO: "go", 254 LGOTO: "goto", 255 LIF: "if", 256 LIMPORT: "import", 257 LINTERFACE: "interface", 258 LMAP: "map", 259 LNAME: "LNAME", 260 LPACKAGE: "package", 261 LRANGE: "range", 262 LRETURN: "return", 263 LSELECT: "select", 264 LSTRUCT: "struct", 265 LSWITCH: "switch", 266 LTYPE: "type", 267 LVAR: "var", 268 LANDAND: "&&", 269 LANDNOT: "&^", 270 LCOMM: "<-", 271 LDEC: "--", 272 LEQ: "==", 273 LGE: ">=", 274 LGT: ">", 275 LIGNORE: "LIGNORE", // we should never see this one 276 LINC: "++", 277 LLE: "<=", 278 LLSH: "<<", 279 LLT: "<", 280 LNE: "!=", 281 LOROR: "||", 282 LRSH: ">>", 283 } 284 285 // usage: defer p.trace(msg)() 286 func (p *parser) trace(msg string) func() { 287 fmt.Printf("%5d: %s%s (\n", lineno, p.indent, msg) 288 const tab = ". " 289 p.indent = append(p.indent, tab...) 290 return func() { 291 p.indent = p.indent[:len(p.indent)-len(tab)] 292 if x := recover(); x != nil { 293 panic(x) // skip print_trace 294 } 295 fmt.Printf("%5d: %s)\n", lineno, p.indent) 296 } 297 } 298 299 // ---------------------------------------------------------------------------- 300 // Parsing package files 301 // 302 // Parse methods are annotated with matching Go productions as appropriate. 303 // The annotations are intended as guidelines only since a single Go grammar 304 // rule may be covered by multiple parse methods and vice versa. 305 306 // SourceFile = PackageClause ";" { ImportDecl ";" } { TopLevelDecl ";" } . 307 func (p *parser) file() { 308 if trace && Debug['x'] != 0 { 309 defer p.trace("file")() 310 } 311 312 p.package_() 313 p.want(';') 314 315 for p.tok == LIMPORT { 316 p.import_() 317 p.want(';') 318 } 319 320 xtop = concat(xtop, p.xdcl_list()) 321 322 p.want(EOF) 323 } 324 325 // PackageClause = "package" PackageName . 326 // PackageName = identifier . 327 func (p *parser) package_() { 328 if trace && Debug['x'] != 0 { 329 defer p.trace("package_")() 330 } 331 332 if p.got(LPACKAGE) { 333 mkpackage(p.sym().Name) 334 } else { 335 prevlineno = lineno // see issue #13267 336 p.syntax_error("package statement must be first") 337 errorexit() 338 } 339 } 340 341 // ImportDecl = "import" ( ImportSpec | "(" { ImportSpec ";" } ")" ) . 342 func (p *parser) import_() { 343 if trace && Debug['x'] != 0 { 344 defer p.trace("import_")() 345 } 346 347 p.want(LIMPORT) 348 if p.got('(') { 349 for p.tok != EOF && p.tok != ')' { 350 p.import_stmt() 351 if !p.osemi(')') { 352 break 353 } 354 } 355 p.want(')') 356 } else { 357 p.import_stmt() 358 } 359 } 360 361 func (p *parser) import_stmt() { 362 if trace && Debug['x'] != 0 { 363 defer p.trace("import_stmt")() 364 } 365 366 line := int32(p.import_here()) 367 if p.tok == LPACKAGE { 368 p.import_package() 369 p.import_there() 370 371 ipkg := importpkg 372 my := importmyname 373 importpkg = nil 374 importmyname = nil 375 376 if my == nil { 377 my = Lookup(ipkg.Name) 378 } 379 380 pack := Nod(OPACK, nil, nil) 381 pack.Sym = my 382 pack.Name.Pkg = ipkg 383 pack.Lineno = line 384 385 if strings.HasPrefix(my.Name, ".") { 386 importdot(ipkg, pack) 387 return 388 } 389 if my.Name == "init" { 390 lineno = line 391 Yyerror("cannot import package as init - init must be a func") 392 return 393 } 394 if my.Name == "_" { 395 return 396 } 397 if my.Def != nil { 398 lineno = line 399 redeclare(my, "as imported package name") 400 } 401 my.Def = pack 402 my.Lastlineno = line 403 my.Block = 1 // at top level 404 405 return 406 } 407 408 p.import_there() 409 // When an invalid import path is passed to importfile, 410 // it calls Yyerror and then sets up a fake import with 411 // no package statement. This allows us to test more 412 // than one invalid import statement in a single file. 413 if nerrors == 0 { 414 Fatalf("phase error in import") 415 } 416 } 417 418 // ImportSpec = [ "." | PackageName ] ImportPath . 419 // ImportPath = string_lit . 420 // 421 // import_here switches the underlying lexed source to the export data 422 // of the imported package. 423 func (p *parser) import_here() int { 424 if trace && Debug['x'] != 0 { 425 defer p.trace("import_here")() 426 } 427 428 importmyname = nil 429 switch p.tok { 430 case LNAME, '@', '?': 431 // import with given name 432 importmyname = p.sym() 433 434 case '.': 435 // import into my name space 436 importmyname = Lookup(".") 437 p.next() 438 } 439 440 var path Val 441 if p.tok == LLITERAL { 442 path = p.val 443 p.next() 444 } else { 445 p.syntax_error("missing import path; require quoted string") 446 p.advance(';', ')') 447 } 448 449 line := parserline() 450 importfile(&path, line) 451 return line 452 } 453 454 // import_package parses the header of an imported package as exported 455 // in textual format from another package. 456 func (p *parser) import_package() { 457 if trace && Debug['x'] != 0 { 458 defer p.trace("import_package")() 459 } 460 461 p.want(LPACKAGE) 462 var name string 463 if p.tok == LNAME { 464 name = p.sym_.Name 465 p.next() 466 } else { 467 p.import_error() 468 } 469 470 if p.tok == LNAME { 471 if p.sym_.Name == "safe" { 472 curio.importsafe = true 473 } 474 p.next() 475 } 476 p.want(';') 477 478 if importpkg.Name == "" { 479 importpkg.Name = name 480 numImport[name]++ 481 } else if importpkg.Name != name { 482 Yyerror("conflicting names %s and %s for package %q", importpkg.Name, name, importpkg.Path) 483 } 484 if incannedimport == 0 { 485 importpkg.Direct = true 486 } 487 importpkg.Safe = curio.importsafe 488 489 if safemode != 0 && !curio.importsafe { 490 Yyerror("cannot import unsafe package %q", importpkg.Path) 491 } 492 } 493 494 // import_there parses the imported package definitions and then switches 495 // the underlying lexed source back to the importing package. 496 func (p *parser) import_there() { 497 if trace && Debug['x'] != 0 { 498 defer p.trace("import_there")() 499 } 500 501 defercheckwidth() 502 503 p.hidden_import_list() 504 p.want('$') 505 // don't read past 2nd '$' 506 if p.tok != '$' { 507 p.import_error() 508 } 509 510 resumecheckwidth() 511 unimportfile() 512 } 513 514 // Declaration = ConstDecl | TypeDecl | VarDecl . 515 // ConstDecl = "const" ( ConstSpec | "(" { ConstSpec ";" } ")" ) . 516 // TypeDecl = "type" ( TypeSpec | "(" { TypeSpec ";" } ")" ) . 517 // VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) . 518 func (p *parser) common_dcl() *NodeList { 519 if trace && Debug['x'] != 0 { 520 defer p.trace("common_dcl")() 521 } 522 523 var dcl func() *NodeList 524 switch p.tok { 525 case LVAR: 526 dcl = p.vardcl 527 528 case LCONST: 529 iota_ = 0 530 dcl = p.constdcl 531 532 case LTYPE: 533 dcl = p.typedcl 534 535 default: 536 panic("unreachable") 537 } 538 539 p.next() 540 var l *NodeList 541 if p.got('(') { 542 for p.tok != EOF && p.tok != ')' { 543 l = concat(l, dcl()) 544 if !p.osemi(')') { 545 break 546 } 547 } 548 p.want(')') 549 } else { 550 l = dcl() 551 } 552 553 iota_ = -100000 554 lastconst = nil 555 556 return l 557 } 558 559 // VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) . 560 func (p *parser) vardcl() *NodeList { 561 if trace && Debug['x'] != 0 { 562 defer p.trace("vardcl")() 563 } 564 565 names := p.dcl_name_list() 566 var typ *Node 567 var exprs *NodeList 568 if p.got('=') { 569 exprs = p.expr_list() 570 } else { 571 typ = p.ntype() 572 if p.got('=') { 573 exprs = p.expr_list() 574 } 575 } 576 577 return variter(names, typ, exprs) 578 } 579 580 // ConstSpec = IdentifierList [ [ Type ] "=" ExpressionList ] . 581 func (p *parser) constdcl() *NodeList { 582 if trace && Debug['x'] != 0 { 583 defer p.trace("constdcl")() 584 } 585 586 names := p.dcl_name_list() 587 var typ *Node 588 var exprs *NodeList 589 if p.tok != EOF && p.tok != ';' && p.tok != ')' { 590 typ = p.try_ntype() 591 if p.got('=') { 592 exprs = p.expr_list() 593 } 594 } 595 596 return constiter(names, typ, exprs) 597 } 598 599 // TypeSpec = identifier Type . 600 func (p *parser) typedcl() *NodeList { 601 if trace && Debug['x'] != 0 { 602 defer p.trace("typedcl")() 603 } 604 605 name := typedcl0(p.sym()) 606 607 typ := p.try_ntype() 608 // handle case where type is missing 609 if typ == nil { 610 p.syntax_error("in type declaration") 611 p.advance(';', ')') 612 } 613 614 return list1(typedcl1(name, typ, true)) 615 } 616 617 // SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl . 618 // 619 // simple_stmt may return missing_stmt if labelOk is set. 620 func (p *parser) simple_stmt(labelOk, rangeOk bool) *Node { 621 if trace && Debug['x'] != 0 { 622 defer p.trace("simple_stmt")() 623 } 624 625 if rangeOk && p.got(LRANGE) { 626 // LRANGE expr 627 r := Nod(ORANGE, nil, p.expr()) 628 r.Etype = 0 // := flag 629 return r 630 } 631 632 lhs := p.expr_list() 633 634 if count(lhs) == 1 && p.tok != '=' && p.tok != LCOLAS && p.tok != LRANGE { 635 // expr 636 lhs := lhs.N 637 switch p.tok { 638 case LASOP: 639 // expr LASOP expr 640 op := p.op 641 p.next() 642 rhs := p.expr() 643 644 stmt := Nod(OASOP, lhs, rhs) 645 stmt.Etype = EType(op) // rathole to pass opcode 646 return stmt 647 648 case LINC: 649 // expr LINC 650 p.next() 651 652 stmt := Nod(OASOP, lhs, Nodintconst(1)) 653 stmt.Implicit = true 654 stmt.Etype = EType(OADD) 655 return stmt 656 657 case LDEC: 658 // expr LDEC 659 p.next() 660 661 stmt := Nod(OASOP, lhs, Nodintconst(1)) 662 stmt.Implicit = true 663 stmt.Etype = EType(OSUB) 664 return stmt 665 666 case ':': 667 // labelname ':' stmt 668 if labelOk { 669 // If we have a labelname, it was parsed by operand 670 // (calling p.name()) and given an ONAME, ONONAME, OTYPE, or OPACK node. 671 switch lhs.Op { 672 case ONAME, ONONAME, OTYPE, OPACK: 673 lhs = newname(lhs.Sym) 674 default: 675 p.syntax_error("expecting semicolon or newline or }") 676 // we already progressed, no need to advance 677 } 678 lhs := Nod(OLABEL, lhs, nil) 679 lhs.Sym = dclstack // context, for goto restrictions 680 p.next() // consume ':' after making label node for correct lineno 681 return p.labeled_stmt(lhs) 682 } 683 fallthrough 684 685 default: 686 // expr 687 // Since a bare name used as an expression is an error, 688 // introduce a wrapper node where necessary to give the 689 // correct line. 690 return wrapname(lhs) 691 } 692 } 693 694 // expr_list 695 switch p.tok { 696 case '=': 697 p.next() 698 if rangeOk && p.got(LRANGE) { 699 // expr_list '=' LRANGE expr 700 r := Nod(ORANGE, nil, p.expr()) 701 r.List = lhs 702 r.Etype = 0 // := flag 703 return r 704 } 705 706 // expr_list '=' expr_list 707 rhs := p.expr_list() 708 709 if lhs.Next == nil && rhs.Next == nil { 710 // simple 711 return Nod(OAS, lhs.N, rhs.N) 712 } 713 // multiple 714 stmt := Nod(OAS2, nil, nil) 715 stmt.List = lhs 716 stmt.Rlist = rhs 717 return stmt 718 719 case LCOLAS: 720 lno := lineno 721 p.next() 722 723 if rangeOk && p.got(LRANGE) { 724 // expr_list LCOLAS LRANGE expr 725 r := Nod(ORANGE, nil, p.expr()) 726 r.List = lhs 727 r.Colas = true 728 colasdefn(lhs, r) 729 return r 730 } 731 732 // expr_list LCOLAS expr_list 733 rhs := p.expr_list() 734 735 if rhs.N.Op == OTYPESW { 736 ts := Nod(OTYPESW, nil, rhs.N.Right) 737 if rhs.Next != nil { 738 Yyerror("expr.(type) must be alone in list") 739 } 740 if lhs.Next != nil { 741 Yyerror("argument count mismatch: %d = %d", count(lhs), 1) 742 } else if (lhs.N.Op != ONAME && lhs.N.Op != OTYPE && lhs.N.Op != ONONAME && (lhs.N.Op != OLITERAL || lhs.N.Name == nil)) || isblank(lhs.N) { 743 Yyerror("invalid variable name %s in type switch", lhs.N) 744 } else { 745 ts.Left = dclname(lhs.N.Sym) 746 } // it's a colas, so must not re-use an oldname 747 return ts 748 } 749 return colas(lhs, rhs, int32(lno)) 750 751 default: 752 p.syntax_error("expecting := or = or comma") 753 p.advance(';', '}') 754 return nil 755 } 756 } 757 758 // LabeledStmt = Label ":" Statement . 759 // Label = identifier . 760 func (p *parser) labeled_stmt(label *Node) *Node { 761 if trace && Debug['x'] != 0 { 762 defer p.trace("labeled_stmt")() 763 } 764 765 var ls *Node // labeled statement 766 if p.tok != '}' && p.tok != EOF { 767 ls = p.stmt() 768 if ls == missing_stmt { 769 // report error at line of ':' token 770 p.syntax_error_at(prevlineno, "missing statement after label") 771 // we are already at the end of the labeled statement - no need to advance 772 return missing_stmt 773 } 774 } 775 776 label.Name.Defn = ls 777 l := list1(label) 778 if ls != nil { 779 l = list(l, ls) 780 } 781 return liststmt(l) 782 } 783 784 // case_ parses a superset of switch and select statement cases. 785 // Later checks restrict the syntax to valid forms. 786 // 787 // ExprSwitchCase = "case" ExpressionList | "default" . 788 // TypeSwitchCase = "case" TypeList | "default" . 789 // TypeList = Type { "," Type } . 790 // CommCase = "case" ( SendStmt | RecvStmt ) | "default" . 791 // RecvStmt = [ ExpressionList "=" | IdentifierList ":=" ] RecvExpr . 792 // RecvExpr = Expression . 793 func (p *parser) case_(tswitch *Node) *Node { 794 if trace && Debug['x'] != 0 { 795 defer p.trace("case_")() 796 } 797 798 switch p.tok { 799 case LCASE: 800 p.next() 801 cases := p.expr_list() // expr_or_type_list 802 switch p.tok { 803 case ':': 804 // LCASE expr_or_type_list ':' 805 806 // will be converted to OCASE 807 // right will point to next case 808 // done in casebody() 809 markdcl() // matching popdcl in caseblock 810 stmt := Nod(OXCASE, nil, nil) 811 stmt.List = cases 812 if tswitch != nil { 813 if n := tswitch.Left; n != nil { 814 // type switch - declare variable 815 nn := newname(n.Sym) 816 declare(nn, dclcontext) 817 stmt.Rlist = list1(nn) 818 819 // keep track of the instances for reporting unused 820 nn.Name.Defn = tswitch 821 } 822 } 823 824 p.next() // consume ':' after declaring type switch var for correct lineno 825 return stmt 826 827 case '=': 828 // LCASE expr_or_type_list '=' expr ':' 829 p.next() 830 rhs := p.expr() 831 832 // will be converted to OCASE 833 // right will point to next case 834 // done in casebody() 835 markdcl() // matching popdcl in caseblock 836 stmt := Nod(OXCASE, nil, nil) 837 var n *Node 838 if cases.Next == nil { 839 n = Nod(OAS, cases.N, rhs) 840 } else { 841 n = Nod(OAS2, nil, nil) 842 n.List = cases 843 n.Rlist = list1(rhs) 844 } 845 stmt.List = list1(n) 846 847 p.want(':') // consume ':' after declaring select cases for correct lineno 848 return stmt 849 850 case LCOLAS: 851 // LCASE expr_or_type_list LCOLAS expr ':' 852 lno := lineno 853 p.next() 854 rhs := p.expr() 855 856 // will be converted to OCASE 857 // right will point to next case 858 // done in casebody() 859 markdcl() // matching popdcl in caseblock 860 stmt := Nod(OXCASE, nil, nil) 861 stmt.List = list1(colas(cases, list1(rhs), int32(lno))) 862 863 p.want(':') // consume ':' after declaring select cases for correct lineno 864 return stmt 865 866 default: 867 markdcl() // for matching popdcl in caseblock 868 stmt := Nod(OXCASE, nil, nil) // don't return nil 869 p.syntax_error("expecting := or = or : or comma") 870 p.advance(LCASE, LDEFAULT, '}') 871 return stmt 872 } 873 874 case LDEFAULT: 875 // LDEFAULT ':' 876 p.next() 877 878 markdcl() // matching popdcl in caseblock 879 stmt := Nod(OXCASE, nil, nil) 880 if tswitch != nil { 881 if n := tswitch.Left; n != nil { 882 // type switch - declare variable 883 nn := newname(n.Sym) 884 declare(nn, dclcontext) 885 stmt.Rlist = list1(nn) 886 887 // keep track of the instances for reporting unused 888 nn.Name.Defn = tswitch 889 } 890 } 891 892 p.want(':') // consume ':' after declaring type switch var for correct lineno 893 return stmt 894 895 default: 896 markdcl() // matching popdcl in caseblock 897 stmt := Nod(OXCASE, nil, nil) // don't return nil 898 p.syntax_error("expecting case or default or }") 899 p.advance(LCASE, LDEFAULT, '}') 900 return stmt 901 } 902 } 903 904 // Block = "{" StatementList "}" . 905 // StatementList = { Statement ";" } . 906 func (p *parser) compound_stmt(else_clause bool) *Node { 907 if trace && Debug['x'] != 0 { 908 defer p.trace("compound_stmt")() 909 } 910 911 markdcl() 912 if p.got('{') { 913 // ok 914 } else if else_clause { 915 p.syntax_error("else must be followed by if or statement block") 916 p.advance(LNAME, '}') 917 } else { 918 panic("unreachable") 919 } 920 921 l := p.stmt_list() 922 p.want('}') 923 924 var stmt *Node 925 if l == nil { 926 stmt = Nod(OEMPTY, nil, nil) 927 } else { 928 stmt = liststmt(l) 929 } 930 popdcl() 931 932 return stmt 933 } 934 935 // caseblock parses a superset of switch and select clauses. 936 // 937 // ExprCaseClause = ExprSwitchCase ":" StatementList . 938 // TypeCaseClause = TypeSwitchCase ":" StatementList . 939 // CommClause = CommCase ":" StatementList . 940 func (p *parser) caseblock(tswitch *Node) *Node { 941 if trace && Debug['x'] != 0 { 942 defer p.trace("caseblock")() 943 } 944 945 stmt := p.case_(tswitch) // does markdcl 946 stmt.Xoffset = int64(block) 947 stmt.Nbody = p.stmt_list() 948 949 popdcl() 950 951 return stmt 952 } 953 954 // caseblock_list parses a superset of switch and select clause lists. 955 func (p *parser) caseblock_list(tswitch *Node) (l *NodeList) { 956 if trace && Debug['x'] != 0 { 957 defer p.trace("caseblock_list")() 958 } 959 960 if !p.got('{') { 961 p.syntax_error("missing { after switch clause") 962 p.advance(LCASE, LDEFAULT, '}') 963 } 964 965 for p.tok != EOF && p.tok != '}' { 966 l = list(l, p.caseblock(tswitch)) 967 } 968 p.want('}') 969 return 970 } 971 972 // loop_body parses if and for statement bodies. 973 func (p *parser) loop_body(context string) *NodeList { 974 if trace && Debug['x'] != 0 { 975 defer p.trace("loop_body")() 976 } 977 978 markdcl() 979 if !p.got('{') { 980 p.syntax_error("missing { after " + context) 981 p.advance(LNAME, '}') 982 } 983 984 body := p.stmt_list() 985 popdcl() 986 p.want('}') 987 988 return body 989 } 990 991 // for_header parses the header portion of a for statement. 992 // 993 // ForStmt = "for" [ Condition | ForClause | RangeClause ] Block . 994 // Condition = Expression . 995 func (p *parser) for_header() *Node { 996 if trace && Debug['x'] != 0 { 997 defer p.trace("for_header")() 998 } 999 1000 init, cond, post := p.header(true) 1001 1002 if init != nil || post != nil { 1003 // init ; test ; incr 1004 if post != nil && post.Colas { 1005 Yyerror("cannot declare in the for-increment") 1006 } 1007 h := Nod(OFOR, nil, nil) 1008 if init != nil { 1009 h.Ninit = list1(init) 1010 } 1011 h.Left = cond 1012 h.Right = post 1013 return h 1014 } 1015 1016 if cond != nil && cond.Op == ORANGE { 1017 // range_stmt - handled by pexpr 1018 return cond 1019 } 1020 1021 // normal test 1022 h := Nod(OFOR, nil, nil) 1023 h.Left = cond 1024 return h 1025 } 1026 1027 func (p *parser) for_body() *Node { 1028 if trace && Debug['x'] != 0 { 1029 defer p.trace("for_body")() 1030 } 1031 1032 stmt := p.for_header() 1033 body := p.loop_body("for clause") 1034 1035 stmt.Nbody = concat(stmt.Nbody, body) 1036 return stmt 1037 } 1038 1039 // ForStmt = "for" [ Condition | ForClause | RangeClause ] Block . 1040 func (p *parser) for_stmt() *Node { 1041 if trace && Debug['x'] != 0 { 1042 defer p.trace("for_stmt")() 1043 } 1044 1045 p.want(LFOR) 1046 markdcl() 1047 body := p.for_body() 1048 popdcl() 1049 1050 return body 1051 } 1052 1053 // header parses a combination of if, switch, and for statement headers: 1054 // 1055 // Header = [ InitStmt ";" ] [ Expression ] . 1056 // Header = [ InitStmt ] ";" [ Condition ] ";" [ PostStmt ] . // for_stmt only 1057 // InitStmt = SimpleStmt . 1058 // PostStmt = SimpleStmt . 1059 func (p *parser) header(for_stmt bool) (init, cond, post *Node) { 1060 if p.tok == '{' { 1061 return 1062 } 1063 1064 outer := p.xnest 1065 p.xnest = -1 1066 1067 if p.tok != ';' { 1068 // accept potential vardcl but complain 1069 // (for test/syntax/forvar.go) 1070 if for_stmt && p.tok == LVAR { 1071 Yyerror("var declaration not allowed in for initializer") 1072 p.next() 1073 } 1074 init = p.simple_stmt(false, for_stmt) 1075 // If we have a range clause, we are done. 1076 if for_stmt && init.Op == ORANGE { 1077 cond = init 1078 init = nil 1079 1080 p.xnest = outer 1081 return 1082 } 1083 } 1084 if p.got(';') { 1085 if for_stmt { 1086 if p.tok != ';' { 1087 cond = p.simple_stmt(false, false) 1088 } 1089 p.want(';') 1090 if p.tok != '{' { 1091 post = p.simple_stmt(false, false) 1092 } 1093 } else if p.tok != '{' { 1094 cond = p.simple_stmt(false, false) 1095 } 1096 } else { 1097 cond = init 1098 init = nil 1099 } 1100 1101 p.xnest = outer 1102 return 1103 } 1104 1105 func (p *parser) if_header() *Node { 1106 if trace && Debug['x'] != 0 { 1107 defer p.trace("if_header")() 1108 } 1109 1110 init, cond, _ := p.header(false) 1111 h := Nod(OIF, nil, nil) 1112 h.Ninit = list1(init) 1113 h.Left = cond 1114 return h 1115 } 1116 1117 // IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" ( IfStmt | Block ) ] . 1118 func (p *parser) if_stmt() *Node { 1119 if trace && Debug['x'] != 0 { 1120 defer p.trace("if_stmt")() 1121 } 1122 1123 p.want(LIF) 1124 1125 markdcl() 1126 1127 stmt := p.if_header() 1128 if stmt.Left == nil { 1129 Yyerror("missing condition in if statement") 1130 } 1131 1132 stmt.Nbody = p.loop_body("if clause") 1133 1134 l := p.elseif_list_else() // does markdcl 1135 1136 n := stmt 1137 popdcl() 1138 for nn := l; nn != nil; nn = nn.Next { 1139 if nn.N.Op == OIF { 1140 popdcl() 1141 } 1142 n.Rlist = list1(nn.N) 1143 n = nn.N 1144 } 1145 1146 return stmt 1147 } 1148 1149 func (p *parser) elseif() *NodeList { 1150 if trace && Debug['x'] != 0 { 1151 defer p.trace("elseif")() 1152 } 1153 1154 // LELSE LIF already consumed 1155 markdcl() // matching popdcl in if_stmt 1156 1157 stmt := p.if_header() 1158 if stmt.Left == nil { 1159 Yyerror("missing condition in if statement") 1160 } 1161 1162 stmt.Nbody = p.loop_body("if clause") 1163 1164 return list1(stmt) 1165 } 1166 1167 func (p *parser) elseif_list_else() (l *NodeList) { 1168 if trace && Debug['x'] != 0 { 1169 defer p.trace("elseif_list_else")() 1170 } 1171 1172 for p.got(LELSE) { 1173 if p.got(LIF) { 1174 l = concat(l, p.elseif()) 1175 } else { 1176 l = concat(l, p.else_()) 1177 break 1178 } 1179 } 1180 1181 return l 1182 } 1183 1184 func (p *parser) else_() *NodeList { 1185 if trace && Debug['x'] != 0 { 1186 defer p.trace("else")() 1187 } 1188 1189 l := &NodeList{N: p.compound_stmt(true)} 1190 l.End = l 1191 return l 1192 1193 } 1194 1195 // switch_stmt parses both expression and type switch statements. 1196 // 1197 // SwitchStmt = ExprSwitchStmt | TypeSwitchStmt . 1198 // ExprSwitchStmt = "switch" [ SimpleStmt ";" ] [ Expression ] "{" { ExprCaseClause } "}" . 1199 // TypeSwitchStmt = "switch" [ SimpleStmt ";" ] TypeSwitchGuard "{" { TypeCaseClause } "}" . 1200 func (p *parser) switch_stmt() *Node { 1201 if trace && Debug['x'] != 0 { 1202 defer p.trace("switch_stmt")() 1203 } 1204 1205 p.want(LSWITCH) 1206 markdcl() 1207 1208 hdr := p.if_header() 1209 hdr.Op = OSWITCH 1210 1211 tswitch := hdr.Left 1212 if tswitch != nil && tswitch.Op != OTYPESW { 1213 tswitch = nil 1214 } 1215 1216 hdr.List = p.caseblock_list(tswitch) 1217 popdcl() 1218 1219 return hdr 1220 } 1221 1222 // SelectStmt = "select" "{" { CommClause } "}" . 1223 func (p *parser) select_stmt() *Node { 1224 if trace && Debug['x'] != 0 { 1225 defer p.trace("select_stmt")() 1226 } 1227 1228 p.want(LSELECT) 1229 hdr := Nod(OSELECT, nil, nil) 1230 hdr.List = p.caseblock_list(nil) 1231 return hdr 1232 } 1233 1234 // TODO(gri) should have lexer return this info - no need for separate lookup 1235 // (issue 13244) 1236 var prectab = map[int32]struct { 1237 prec int // > 0 (0 indicates not found) 1238 op Op 1239 }{ 1240 // not an expression anymore, but left in so we can give a good error 1241 // message when used in expression context 1242 LCOMM: {1, OSEND}, 1243 1244 LOROR: {2, OOROR}, 1245 1246 LANDAND: {3, OANDAND}, 1247 1248 LEQ: {4, OEQ}, 1249 LNE: {4, ONE}, 1250 LLE: {4, OLE}, 1251 LGE: {4, OGE}, 1252 LLT: {4, OLT}, 1253 LGT: {4, OGT}, 1254 1255 '+': {5, OADD}, 1256 '-': {5, OSUB}, 1257 '|': {5, OOR}, 1258 '^': {5, OXOR}, 1259 1260 '*': {6, OMUL}, 1261 '/': {6, ODIV}, 1262 '%': {6, OMOD}, 1263 '&': {6, OAND}, 1264 LLSH: {6, OLSH}, 1265 LRSH: {6, ORSH}, 1266 LANDNOT: {6, OANDNOT}, 1267 } 1268 1269 // Expression = UnaryExpr | Expression binary_op Expression . 1270 func (p *parser) bexpr(prec int) *Node { 1271 // don't trace bexpr - only leads to overly nested trace output 1272 1273 x := p.uexpr() 1274 t := prectab[p.tok] 1275 for tprec := t.prec; tprec >= prec; tprec-- { 1276 for tprec == prec { 1277 p.next() 1278 y := p.bexpr(t.prec + 1) 1279 x = Nod(t.op, x, y) 1280 t = prectab[p.tok] 1281 tprec = t.prec 1282 } 1283 } 1284 return x 1285 } 1286 1287 func (p *parser) expr() *Node { 1288 if trace && Debug['x'] != 0 { 1289 defer p.trace("expr")() 1290 } 1291 1292 return p.bexpr(1) 1293 } 1294 1295 func unparen(x *Node) *Node { 1296 for x.Op == OPAREN { 1297 x = x.Left 1298 } 1299 return x 1300 } 1301 1302 // UnaryExpr = PrimaryExpr | unary_op UnaryExpr . 1303 func (p *parser) uexpr() *Node { 1304 if trace && Debug['x'] != 0 { 1305 defer p.trace("uexpr")() 1306 } 1307 1308 var op Op 1309 switch p.tok { 1310 case '*': 1311 op = OIND 1312 1313 case '&': 1314 p.next() 1315 // uexpr may have returned a parenthesized composite literal 1316 // (see comment in operand) - remove parentheses if any 1317 x := unparen(p.uexpr()) 1318 if x.Op == OCOMPLIT { 1319 // Special case for &T{...}: turn into (*T){...}. 1320 x.Right = Nod(OIND, x.Right, nil) 1321 x.Right.Implicit = true 1322 } else { 1323 x = Nod(OADDR, x, nil) 1324 } 1325 return x 1326 1327 case '+': 1328 op = OPLUS 1329 1330 case '-': 1331 op = OMINUS 1332 1333 case '!': 1334 op = ONOT 1335 1336 case '~': 1337 // TODO(gri) do this in the lexer instead (issue 13244) 1338 p.next() 1339 x := p.uexpr() 1340 Yyerror("the bitwise complement operator is ^") 1341 return Nod(OCOM, x, nil) 1342 1343 case '^': 1344 op = OCOM 1345 1346 case LCOMM: 1347 // receive op (<-x) or receive-only channel (<-chan E) 1348 p.next() 1349 1350 // If the next token is LCHAN we still don't know if it is 1351 // a channel (<-chan int) or a receive op (<-chan int(ch)). 1352 // We only know once we have found the end of the uexpr. 1353 1354 x := p.uexpr() 1355 1356 // There are two cases: 1357 // 1358 // <-chan... => <-x is a channel type 1359 // <-x => <-x is a receive operation 1360 // 1361 // In the first case, <- must be re-associated with 1362 // the channel type parsed already: 1363 // 1364 // <-(chan E) => (<-chan E) 1365 // <-(chan<-E) => (<-chan (<-E)) 1366 1367 if x.Op == OTCHAN { 1368 // x is a channel type => re-associate <- 1369 dir := EType(Csend) 1370 t := x 1371 for ; t.Op == OTCHAN && dir == Csend; t = t.Left { 1372 dir = t.Etype 1373 if dir == Crecv { 1374 // t is type <-chan E but <-<-chan E is not permitted 1375 // (report same error as for "type _ <-<-chan E") 1376 p.syntax_error("unexpected <-, expecting chan") 1377 // already progressed, no need to advance 1378 } 1379 t.Etype = Crecv 1380 } 1381 if dir == Csend { 1382 // channel dir is <- but channel element E is not a channel 1383 // (report same error as for "type _ <-chan<-E") 1384 p.syntax_error(fmt.Sprintf("unexpected %v, expecting chan", t)) 1385 // already progressed, no need to advance 1386 } 1387 return x 1388 } 1389 1390 // x is not a channel type => we have a receive op 1391 return Nod(ORECV, x, nil) 1392 1393 default: 1394 return p.pexpr(false) 1395 } 1396 1397 // simple uexpr 1398 p.next() 1399 return Nod(op, p.uexpr(), nil) 1400 } 1401 1402 // pseudocall parses call-like statements that can be preceded by 'defer' and 'go'. 1403 func (p *parser) pseudocall() *Node { 1404 if trace && Debug['x'] != 0 { 1405 defer p.trace("pseudocall")() 1406 } 1407 1408 x := p.pexpr(p.tok == '(') // keep_parens so we can report error below 1409 switch x.Op { 1410 case OCALL: 1411 return x 1412 case OPAREN: 1413 Yyerror("expression in go/defer must not be parenthesized") 1414 // already progressed, no need to advance 1415 default: 1416 Yyerror("expression in go/defer must be function call") 1417 // already progressed, no need to advance 1418 } 1419 return nil 1420 } 1421 1422 // Operand = Literal | OperandName | MethodExpr | "(" Expression ")" . 1423 // Literal = BasicLit | CompositeLit | FunctionLit . 1424 // BasicLit = int_lit | float_lit | imaginary_lit | rune_lit | string_lit . 1425 // OperandName = identifier | QualifiedIdent. 1426 func (p *parser) operand(keep_parens bool) *Node { 1427 if trace && Debug['x'] != 0 { 1428 defer p.trace("operand")() 1429 } 1430 1431 switch p.tok { 1432 case LLITERAL: 1433 x := nodlit(p.val) 1434 p.next() 1435 return x 1436 1437 case LNAME, '@', '?': 1438 return p.name() 1439 1440 case '(': 1441 p.next() 1442 p.xnest++ 1443 x := p.expr() // expr_or_type 1444 p.xnest-- 1445 p.want(')') 1446 1447 // Optimization: Record presence of ()'s only where needed 1448 // for error reporting. Don't bother in other cases; it is 1449 // just a waste of memory and time. 1450 1451 // Parentheses are not permitted on lhs of := . 1452 switch x.Op { 1453 case ONAME, ONONAME, OPACK, OTYPE, OLITERAL, OTYPESW: 1454 keep_parens = true 1455 } 1456 1457 // Parentheses are not permitted around T in a composite 1458 // literal T{}. If the next token is a {, assume x is a 1459 // composite literal type T (it may not be, { could be 1460 // the opening brace of a block, but we don't know yet). 1461 if p.tok == '{' { 1462 keep_parens = true 1463 } 1464 1465 // Parentheses are also not permitted around the expression 1466 // in a go/defer statement. In that case, operand is called 1467 // with keep_parens set. 1468 if keep_parens { 1469 x = Nod(OPAREN, x, nil) 1470 } 1471 return x 1472 1473 case LFUNC: 1474 t := p.ntype() // fntype 1475 if p.tok == '{' { 1476 // fnlitdcl 1477 closurehdr(t) 1478 // fnliteral 1479 p.next() // consume '{' 1480 p.fnest++ 1481 p.xnest++ 1482 body := p.stmt_list() 1483 p.xnest-- 1484 p.fnest-- 1485 p.want('}') 1486 return closurebody(body) 1487 } 1488 return t 1489 1490 case '[', LCHAN, LMAP, LSTRUCT, LINTERFACE: 1491 return p.ntype() // othertype 1492 1493 case '{': 1494 // common case: p.header is missing simple_stmt before { in if, for, switch 1495 p.syntax_error("missing operand") 1496 // '{' will be consumed in pexpr - no need to consume it here 1497 return nil 1498 1499 default: 1500 p.syntax_error("expecting expression") 1501 p.advance() 1502 return nil 1503 } 1504 1505 // Syntactically, composite literals are operands. Because a complit 1506 // type may be a qualified identifier which is handled by pexpr 1507 // (together with selector expressions), complits are parsed there 1508 // as well (operand is only called from pexpr). 1509 } 1510 1511 // PrimaryExpr = 1512 // Operand | 1513 // Conversion | 1514 // PrimaryExpr Selector | 1515 // PrimaryExpr Index | 1516 // PrimaryExpr Slice | 1517 // PrimaryExpr TypeAssertion | 1518 // PrimaryExpr Arguments . 1519 // 1520 // Selector = "." identifier . 1521 // Index = "[" Expression "]" . 1522 // Slice = "[" ( [ Expression ] ":" [ Expression ] ) | 1523 // ( [ Expression ] ":" Expression ":" Expression ) 1524 // "]" . 1525 // TypeAssertion = "." "(" Type ")" . 1526 // Arguments = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" . 1527 func (p *parser) pexpr(keep_parens bool) *Node { 1528 if trace && Debug['x'] != 0 { 1529 defer p.trace("pexpr")() 1530 } 1531 1532 x := p.operand(keep_parens) 1533 1534 loop: 1535 for { 1536 switch p.tok { 1537 case '.': 1538 p.next() 1539 switch p.tok { 1540 case LNAME, '@', '?': 1541 // pexpr '.' sym 1542 x = p.new_dotname(x) 1543 1544 case '(': 1545 p.next() 1546 switch p.tok { 1547 default: 1548 // pexpr '.' '(' expr_or_type ')' 1549 t := p.expr() // expr_or_type 1550 p.want(')') 1551 x = Nod(ODOTTYPE, x, t) 1552 1553 case LTYPE: 1554 // pexpr '.' '(' LTYPE ')' 1555 p.next() 1556 p.want(')') 1557 x = Nod(OTYPESW, nil, x) 1558 } 1559 1560 default: 1561 p.syntax_error("expecting name or (") 1562 p.advance(';', '}') 1563 } 1564 1565 case '[': 1566 p.next() 1567 p.xnest++ 1568 var index [3]*Node 1569 if p.tok != ':' { 1570 index[0] = p.expr() 1571 } 1572 ncol := 0 1573 for ncol < len(index)-1 && p.got(':') { 1574 ncol++ 1575 if p.tok != EOF && p.tok != ':' && p.tok != ']' { 1576 index[ncol] = p.expr() 1577 } 1578 } 1579 p.xnest-- 1580 p.want(']') 1581 1582 switch ncol { 1583 case 0: 1584 i := index[0] 1585 if i == nil { 1586 Yyerror("missing index in index expression") 1587 } 1588 x = Nod(OINDEX, x, i) 1589 case 1: 1590 i := index[0] 1591 j := index[1] 1592 x = Nod(OSLICE, x, Nod(OKEY, i, j)) 1593 case 2: 1594 i := index[0] 1595 j := index[1] 1596 k := index[2] 1597 if j == nil { 1598 Yyerror("middle index required in 3-index slice") 1599 } 1600 if k == nil { 1601 Yyerror("final index required in 3-index slice") 1602 } 1603 x = Nod(OSLICE3, x, Nod(OKEY, i, Nod(OKEY, j, k))) 1604 1605 default: 1606 panic("unreachable") 1607 } 1608 1609 case '(': 1610 // convtype '(' expr ocomma ')' 1611 args, ddd := p.arg_list() 1612 1613 // call or conversion 1614 x = Nod(OCALL, x, nil) 1615 x.List = args 1616 x.Isddd = ddd 1617 1618 case '{': 1619 // operand may have returned a parenthesized complit 1620 // type; accept it but complain if we have a complit 1621 t := unparen(x) 1622 // determine if '{' belongs to a complit or a compound_stmt 1623 complit_ok := false 1624 switch t.Op { 1625 case ONAME, ONONAME, OTYPE, OPACK, OXDOT, ODOT: 1626 if p.xnest >= 0 { 1627 // x is considered a comptype 1628 complit_ok = true 1629 } 1630 case OTARRAY, OTSTRUCT, OTMAP: 1631 // x is a comptype 1632 complit_ok = true 1633 } 1634 if !complit_ok { 1635 break loop 1636 } 1637 if t != x { 1638 p.syntax_error("cannot parenthesize type in composite literal") 1639 // already progressed, no need to advance 1640 } 1641 n := p.complitexpr() 1642 n.Right = x 1643 x = n 1644 1645 default: 1646 break loop 1647 } 1648 } 1649 1650 return x 1651 } 1652 1653 // KeyedElement = [ Key ":" ] Element . 1654 func (p *parser) keyval() *Node { 1655 if trace && Debug['x'] != 0 { 1656 defer p.trace("keyval")() 1657 } 1658 1659 // A composite literal commonly spans several lines, 1660 // so the line number on errors may be misleading. 1661 // Wrap values (but not keys!) that don't carry line 1662 // numbers. 1663 1664 x := p.bare_complitexpr() 1665 1666 if p.got(':') { 1667 // key ':' value 1668 return Nod(OKEY, x, wrapname(p.bare_complitexpr())) 1669 } 1670 1671 // value 1672 return wrapname(x) 1673 } 1674 1675 func wrapname(x *Node) *Node { 1676 // These nodes do not carry line numbers. 1677 // Introduce a wrapper node to give the correct line. 1678 switch x.Op { 1679 case ONAME, ONONAME, OTYPE, OPACK, OLITERAL: 1680 x = Nod(OPAREN, x, nil) 1681 x.Implicit = true 1682 } 1683 return x 1684 } 1685 1686 // Element = Expression | LiteralValue . 1687 func (p *parser) bare_complitexpr() *Node { 1688 if trace && Debug['x'] != 0 { 1689 defer p.trace("bare_complitexpr")() 1690 } 1691 1692 if p.tok == '{' { 1693 // '{' start_complit braced_keyval_list '}' 1694 return p.complitexpr() 1695 } 1696 1697 return p.expr() 1698 } 1699 1700 // LiteralValue = "{" [ ElementList [ "," ] ] "}" . 1701 func (p *parser) complitexpr() *Node { 1702 if trace && Debug['x'] != 0 { 1703 defer p.trace("complitexpr")() 1704 } 1705 1706 // make node early so we get the right line number 1707 n := Nod(OCOMPLIT, nil, nil) 1708 1709 p.want('{') 1710 p.xnest++ 1711 1712 var l *NodeList 1713 for p.tok != EOF && p.tok != '}' { 1714 l = list(l, p.keyval()) 1715 if !p.ocomma('}') { 1716 break 1717 } 1718 } 1719 1720 p.xnest-- 1721 p.want('}') 1722 1723 n.List = l 1724 return n 1725 } 1726 1727 // names and types 1728 // newname is used before declared 1729 // oldname is used after declared 1730 func (p *parser) new_name(sym *Sym) *Node { 1731 if trace && Debug['x'] != 0 { 1732 defer p.trace("new_name")() 1733 } 1734 1735 if sym != nil { 1736 return newname(sym) 1737 } 1738 return nil 1739 } 1740 1741 func (p *parser) dcl_name(sym *Sym) *Node { 1742 if trace && Debug['x'] != 0 { 1743 defer p.trace("dcl_name")() 1744 } 1745 1746 if sym == nil { 1747 yyerrorl(int(prevlineno), "invalid declaration") 1748 return nil 1749 } 1750 return dclname(sym) 1751 } 1752 1753 func (p *parser) onew_name() *Node { 1754 if trace && Debug['x'] != 0 { 1755 defer p.trace("onew_name")() 1756 } 1757 1758 switch p.tok { 1759 case LNAME, '@', '?': 1760 return p.new_name(p.sym()) 1761 } 1762 return nil 1763 } 1764 1765 func (p *parser) sym() *Sym { 1766 switch p.tok { 1767 case LNAME: 1768 s := p.sym_ 1769 p.next() 1770 // during imports, unqualified non-exported identifiers are from builtinpkg 1771 if importpkg != nil && !exportname(s.Name) { 1772 s = Pkglookup(s.Name, builtinpkg) 1773 } 1774 return s 1775 1776 case '@': 1777 return p.hidden_importsym() 1778 1779 case '?': 1780 p.next() 1781 return nil 1782 1783 default: 1784 p.syntax_error("expecting name") 1785 p.advance() 1786 return new(Sym) 1787 } 1788 } 1789 1790 func mkname(sym *Sym) *Node { 1791 n := oldname(sym) 1792 if n.Name != nil && n.Name.Pack != nil { 1793 n.Name.Pack.Used = true 1794 } 1795 return n 1796 } 1797 1798 func (p *parser) name() *Node { 1799 if trace && Debug['x'] != 0 { 1800 defer p.trace("name")() 1801 } 1802 1803 return mkname(p.sym()) 1804 } 1805 1806 // [ "..." ] Type 1807 func (p *parser) dotdotdot() *Node { 1808 if trace && Debug['x'] != 0 { 1809 defer p.trace("dotdotdot")() 1810 } 1811 1812 p.want(LDDD) 1813 if typ := p.try_ntype(); typ != nil { 1814 return Nod(ODDD, typ, nil) 1815 } 1816 1817 Yyerror("final argument in variadic function missing type") 1818 return Nod(ODDD, typenod(typ(TINTER)), nil) 1819 } 1820 1821 func (p *parser) ntype() *Node { 1822 if trace && Debug['x'] != 0 { 1823 defer p.trace("ntype")() 1824 } 1825 1826 if typ := p.try_ntype(); typ != nil { 1827 return typ 1828 } 1829 1830 p.syntax_error("") 1831 p.advance() 1832 return nil 1833 } 1834 1835 // try_ntype is like ntype but it returns nil if there was no type 1836 // instead of reporting an error. 1837 // 1838 // Type = TypeName | TypeLit | "(" Type ")" . 1839 // TypeName = identifier | QualifiedIdent . 1840 // TypeLit = ArrayType | StructType | PointerType | FunctionType | InterfaceType | 1841 // SliceType | MapType | ChannelType . 1842 func (p *parser) try_ntype() *Node { 1843 if trace && Debug['x'] != 0 { 1844 defer p.trace("try_ntype")() 1845 } 1846 1847 switch p.tok { 1848 case LCOMM: 1849 // recvchantype 1850 p.next() 1851 p.want(LCHAN) 1852 t := Nod(OTCHAN, p.chan_elem(), nil) 1853 t.Etype = Crecv 1854 return t 1855 1856 case LFUNC: 1857 // fntype 1858 p.next() 1859 params := p.param_list() 1860 result := p.fnres() 1861 params = checkarglist(params, 1) 1862 t := Nod(OTFUNC, nil, nil) 1863 t.List = params 1864 t.Rlist = result 1865 return t 1866 1867 case '[': 1868 // '[' oexpr ']' ntype 1869 // '[' LDDD ']' ntype 1870 p.next() 1871 p.xnest++ 1872 var len *Node 1873 if p.tok != ']' { 1874 if p.got(LDDD) { 1875 len = Nod(ODDD, nil, nil) 1876 } else { 1877 len = p.expr() 1878 } 1879 } 1880 p.xnest-- 1881 p.want(']') 1882 return Nod(OTARRAY, len, p.ntype()) 1883 1884 case LCHAN: 1885 // LCHAN non_recvchantype 1886 // LCHAN LCOMM ntype 1887 p.next() 1888 var dir EType = Cboth 1889 if p.got(LCOMM) { 1890 dir = Csend 1891 } 1892 t := Nod(OTCHAN, p.chan_elem(), nil) 1893 t.Etype = dir 1894 return t 1895 1896 case LMAP: 1897 // LMAP '[' ntype ']' ntype 1898 p.next() 1899 p.want('[') 1900 key := p.ntype() 1901 p.want(']') 1902 val := p.ntype() 1903 return Nod(OTMAP, key, val) 1904 1905 case LSTRUCT: 1906 return p.structtype() 1907 1908 case LINTERFACE: 1909 return p.interfacetype() 1910 1911 case '*': 1912 // ptrtype 1913 p.next() 1914 return Nod(OIND, p.ntype(), nil) 1915 1916 case LNAME, '@', '?': 1917 return p.dotname() 1918 1919 case '(': 1920 p.next() 1921 t := p.ntype() 1922 p.want(')') 1923 return t 1924 1925 default: 1926 return nil 1927 } 1928 } 1929 1930 func (p *parser) chan_elem() *Node { 1931 if trace && Debug['x'] != 0 { 1932 defer p.trace("chan_elem")() 1933 } 1934 1935 if typ := p.try_ntype(); typ != nil { 1936 return typ 1937 } 1938 1939 p.syntax_error("missing channel element type") 1940 // assume element type is simply absent - don't advance 1941 return nil 1942 } 1943 1944 func (p *parser) new_dotname(pkg *Node) *Node { 1945 if trace && Debug['x'] != 0 { 1946 defer p.trace("new_dotname")() 1947 } 1948 1949 sel := p.sym() 1950 if pkg.Op == OPACK { 1951 s := restrictlookup(sel.Name, pkg.Name.Pkg) 1952 pkg.Used = true 1953 return oldname(s) 1954 } 1955 return Nod(OXDOT, pkg, newname(sel)) 1956 1957 } 1958 1959 func (p *parser) dotname() *Node { 1960 if trace && Debug['x'] != 0 { 1961 defer p.trace("dotname")() 1962 } 1963 1964 name := p.name() 1965 if p.got('.') { 1966 return p.new_dotname(name) 1967 } 1968 return name 1969 } 1970 1971 // StructType = "struct" "{" { FieldDecl ";" } "}" . 1972 func (p *parser) structtype() *Node { 1973 if trace && Debug['x'] != 0 { 1974 defer p.trace("structtype")() 1975 } 1976 1977 p.want(LSTRUCT) 1978 p.want('{') 1979 var l *NodeList 1980 for p.tok != EOF && p.tok != '}' { 1981 l = concat(l, p.structdcl()) 1982 if !p.osemi('}') { 1983 break 1984 } 1985 } 1986 p.want('}') 1987 1988 t := Nod(OTSTRUCT, nil, nil) 1989 t.List = l 1990 return t 1991 } 1992 1993 // InterfaceType = "interface" "{" { MethodSpec ";" } "}" . 1994 func (p *parser) interfacetype() *Node { 1995 if trace && Debug['x'] != 0 { 1996 defer p.trace("interfacetype")() 1997 } 1998 1999 p.want(LINTERFACE) 2000 p.want('{') 2001 var l *NodeList 2002 for p.tok != EOF && p.tok != '}' { 2003 l = list(l, p.interfacedcl()) 2004 if !p.osemi('}') { 2005 break 2006 } 2007 } 2008 p.want('}') 2009 2010 t := Nod(OTINTER, nil, nil) 2011 t.List = l 2012 return t 2013 } 2014 2015 // Function stuff. 2016 // All in one place to show how crappy it all is. 2017 2018 func (p *parser) xfndcl() *Node { 2019 if trace && Debug['x'] != 0 { 2020 defer p.trace("xfndcl")() 2021 } 2022 2023 p.want(LFUNC) 2024 f := p.fndcl() 2025 body := p.fnbody() 2026 2027 if f == nil { 2028 return nil 2029 } 2030 if noescape && body != nil { 2031 Yyerror("can only use //go:noescape with external func implementations") 2032 } 2033 2034 f.Nbody = body 2035 f.Func.Endlineno = lineno 2036 f.Noescape = noescape 2037 f.Func.Norace = norace 2038 f.Func.Nosplit = nosplit 2039 f.Func.Noinline = noinline 2040 f.Func.Nowritebarrier = nowritebarrier 2041 f.Func.Nowritebarrierrec = nowritebarrierrec 2042 f.Func.Systemstack = systemstack 2043 funcbody(f) 2044 2045 return f 2046 } 2047 2048 // FunctionDecl = "func" FunctionName ( Function | Signature ) . 2049 // FunctionName = identifier . 2050 // Function = Signature FunctionBody . 2051 // MethodDecl = "func" Receiver MethodName ( Function | Signature ) . 2052 // Receiver = Parameters . 2053 func (p *parser) fndcl() *Node { 2054 if trace && Debug['x'] != 0 { 2055 defer p.trace("fndcl")() 2056 } 2057 2058 switch p.tok { 2059 case LNAME, '@', '?': 2060 // sym '(' oarg_type_list_ocomma ')' fnres 2061 name := p.sym() 2062 params := p.param_list() 2063 result := p.fnres() 2064 2065 params = checkarglist(params, 1) 2066 2067 if name.Name == "init" { 2068 name = renameinit() 2069 if params != nil || result != nil { 2070 Yyerror("func init must have no arguments and no return values") 2071 } 2072 } 2073 2074 if localpkg.Name == "main" && name.Name == "main" { 2075 if params != nil || result != nil { 2076 Yyerror("func main must have no arguments and no return values") 2077 } 2078 } 2079 2080 t := Nod(OTFUNC, nil, nil) 2081 t.List = params 2082 t.Rlist = result 2083 2084 f := Nod(ODCLFUNC, nil, nil) 2085 f.Func.Nname = newfuncname(name) 2086 f.Func.Nname.Name.Defn = f 2087 f.Func.Nname.Name.Param.Ntype = t // TODO: check if nname already has an ntype 2088 declare(f.Func.Nname, PFUNC) 2089 2090 funchdr(f) 2091 return f 2092 2093 case '(': 2094 // '(' oarg_type_list_ocomma ')' sym '(' oarg_type_list_ocomma ')' fnres 2095 rparam := p.param_list() 2096 name := p.sym() 2097 params := p.param_list() 2098 result := p.fnres() 2099 2100 rparam = checkarglist(rparam, 0) 2101 params = checkarglist(params, 1) 2102 2103 if rparam == nil { 2104 Yyerror("method has no receiver") 2105 return nil 2106 } 2107 2108 if rparam.Next != nil { 2109 Yyerror("method has multiple receivers") 2110 return nil 2111 } 2112 2113 rcvr := rparam.N 2114 if rcvr.Op != ODCLFIELD { 2115 Yyerror("bad receiver in method") 2116 return nil 2117 } 2118 2119 t := Nod(OTFUNC, rcvr, nil) 2120 t.List = params 2121 t.Rlist = result 2122 2123 f := Nod(ODCLFUNC, nil, nil) 2124 f.Func.Shortname = newfuncname(name) 2125 f.Func.Nname = methodname1(f.Func.Shortname, rcvr.Right) 2126 f.Func.Nname.Name.Defn = f 2127 f.Func.Nname.Name.Param.Ntype = t 2128 f.Func.Nname.Nointerface = nointerface 2129 declare(f.Func.Nname, PFUNC) 2130 2131 funchdr(f) 2132 return f 2133 2134 default: 2135 p.syntax_error("expecting name or (") 2136 p.advance('{', ';') 2137 return nil 2138 } 2139 } 2140 2141 func (p *parser) hidden_fndcl() *Node { 2142 if trace && Debug['x'] != 0 { 2143 defer p.trace("hidden_fndcl")() 2144 } 2145 2146 switch p.tok { 2147 default: 2148 // hidden_pkg_importsym '(' ohidden_funarg_list ')' ohidden_funres 2149 s1 := p.hidden_pkg_importsym() 2150 p.want('(') 2151 s3 := p.ohidden_funarg_list() 2152 p.want(')') 2153 s5 := p.ohidden_funres() 2154 2155 s := s1 2156 t := functype(nil, s3, s5) 2157 2158 importsym(s, ONAME) 2159 if s.Def != nil && s.Def.Op == ONAME { 2160 if Eqtype(t, s.Def.Type) { 2161 dclcontext = PDISCARD // since we skip funchdr below 2162 return nil 2163 } 2164 Yyerror("inconsistent definition for func %v during import\n\t%v\n\t%v", s, s.Def.Type, t) 2165 } 2166 2167 ss := newfuncname(s) 2168 ss.Type = t 2169 declare(ss, PFUNC) 2170 2171 funchdr(ss) 2172 return ss 2173 2174 case '(': 2175 // '(' hidden_funarg_list ')' sym '(' ohidden_funarg_list ')' ohidden_funres 2176 p.next() 2177 s2 := p.hidden_funarg_list() 2178 p.want(')') 2179 s4 := p.sym() 2180 p.want('(') 2181 s6 := p.ohidden_funarg_list() 2182 p.want(')') 2183 s8 := p.ohidden_funres() 2184 2185 ss := methodname1(newname(s4), s2.N.Right) 2186 ss.Type = functype(s2.N, s6, s8) 2187 2188 checkwidth(ss.Type) 2189 addmethod(s4, ss.Type, false, nointerface) 2190 nointerface = false 2191 funchdr(ss) 2192 2193 // inl.C's inlnode in on a dotmeth node expects to find the inlineable body as 2194 // (dotmeth's type).Nname.Inl, and dotmeth's type has been pulled 2195 // out by typecheck's lookdot as this $$.ttype. So by providing 2196 // this back link here we avoid special casing there. 2197 ss.Type.Nname = ss 2198 return ss 2199 } 2200 } 2201 2202 // FunctionBody = Block . 2203 func (p *parser) fnbody() *NodeList { 2204 if trace && Debug['x'] != 0 { 2205 defer p.trace("fnbody")() 2206 } 2207 2208 if p.got('{') { 2209 p.fnest++ 2210 body := p.stmt_list() 2211 p.fnest-- 2212 p.want('}') 2213 if body == nil { 2214 body = list1(Nod(OEMPTY, nil, nil)) 2215 } 2216 return body 2217 } 2218 2219 return nil 2220 } 2221 2222 // Result = Parameters | Type . 2223 func (p *parser) fnres() *NodeList { 2224 if trace && Debug['x'] != 0 { 2225 defer p.trace("fnres")() 2226 } 2227 2228 if p.tok == '(' { 2229 result := p.param_list() 2230 return checkarglist(result, 0) 2231 } 2232 2233 if result := p.try_ntype(); result != nil { 2234 return list1(Nod(ODCLFIELD, nil, result)) 2235 } 2236 2237 return nil 2238 } 2239 2240 // Declaration = ConstDecl | TypeDecl | VarDecl . 2241 // TopLevelDecl = Declaration | FunctionDecl | MethodDecl . 2242 func (p *parser) xdcl_list() (l *NodeList) { 2243 if trace && Debug['x'] != 0 { 2244 defer p.trace("xdcl_list")() 2245 } 2246 2247 loop: 2248 for p.tok != EOF { 2249 switch p.tok { 2250 case LVAR, LCONST, LTYPE: 2251 l = concat(l, p.common_dcl()) 2252 2253 case LFUNC: 2254 l = list(l, p.xfndcl()) 2255 2256 default: 2257 if p.tok == '{' && l != nil && l.End.N.Op == ODCLFUNC && l.End.N.Nbody == nil { 2258 // opening { of function declaration on next line 2259 p.syntax_error("unexpected semicolon or newline before {") 2260 } else { 2261 p.syntax_error("non-declaration statement outside function body") 2262 } 2263 p.advance(LVAR, LCONST, LTYPE, LFUNC) 2264 goto loop 2265 } 2266 2267 if nsyntaxerrors == 0 { 2268 testdclstack() 2269 } 2270 2271 noescape = false 2272 noinline = false 2273 nointerface = false 2274 norace = false 2275 nosplit = false 2276 nowritebarrier = false 2277 nowritebarrierrec = false 2278 systemstack = false 2279 2280 // Consume ';' AFTER resetting the above flags since 2281 // it may read the subsequent comment line which may 2282 // set the flags for the next function declaration. 2283 if p.tok != EOF && !p.got(';') { 2284 p.syntax_error("after top level declaration") 2285 p.advance(LVAR, LCONST, LTYPE, LFUNC) 2286 goto loop 2287 } 2288 } 2289 return 2290 } 2291 2292 // FieldDecl = (IdentifierList Type | AnonymousField) [ Tag ] . 2293 // AnonymousField = [ "*" ] TypeName . 2294 // Tag = string_lit . 2295 func (p *parser) structdcl() *NodeList { 2296 if trace && Debug['x'] != 0 { 2297 defer p.trace("structdcl")() 2298 } 2299 2300 var sym *Sym 2301 switch p.tok { 2302 case LNAME: 2303 sym = p.sym_ 2304 p.next() 2305 if sym == nil { 2306 panic("unreachable") // we must have a sym for LNAME 2307 } 2308 if p.tok == '.' || p.tok == LLITERAL || p.tok == ';' || p.tok == '}' { 2309 // embed oliteral 2310 field := p.embed(sym) 2311 tag := p.oliteral() 2312 2313 field.SetVal(tag) 2314 return list1(field) 2315 } 2316 2317 // LNAME belongs to first *Sym of new_name_list 2318 // 2319 // during imports, unqualified non-exported identifiers are from builtinpkg 2320 if importpkg != nil && !exportname(sym.Name) { 2321 sym = Pkglookup(sym.Name, builtinpkg) 2322 if sym == nil { 2323 p.import_error() 2324 } 2325 } 2326 fallthrough 2327 2328 case '@', '?': 2329 // new_name_list ntype oliteral 2330 fields := p.new_name_list(sym) 2331 typ := p.ntype() 2332 tag := p.oliteral() 2333 2334 if l := fields; l == nil || l.N.Sym.Name == "?" { 2335 // ? symbol, during import (list1(nil) == nil) 2336 n := typ 2337 if n.Op == OIND { 2338 n = n.Left 2339 } 2340 n = embedded(n.Sym, importpkg) 2341 n.Right = typ 2342 n.SetVal(tag) 2343 return list1(n) 2344 } 2345 2346 for l := fields; l != nil; l = l.Next { 2347 l.N = Nod(ODCLFIELD, l.N, typ) 2348 l.N.SetVal(tag) 2349 } 2350 return fields 2351 2352 case '(': 2353 p.next() 2354 if p.got('*') { 2355 // '(' '*' embed ')' oliteral 2356 field := p.embed(nil) 2357 p.want(')') 2358 tag := p.oliteral() 2359 2360 field.Right = Nod(OIND, field.Right, nil) 2361 field.SetVal(tag) 2362 Yyerror("cannot parenthesize embedded type") 2363 return list1(field) 2364 2365 } else { 2366 // '(' embed ')' oliteral 2367 field := p.embed(nil) 2368 p.want(')') 2369 tag := p.oliteral() 2370 2371 field.SetVal(tag) 2372 Yyerror("cannot parenthesize embedded type") 2373 return list1(field) 2374 } 2375 2376 case '*': 2377 p.next() 2378 if p.got('(') { 2379 // '*' '(' embed ')' oliteral 2380 field := p.embed(nil) 2381 p.want(')') 2382 tag := p.oliteral() 2383 2384 field.Right = Nod(OIND, field.Right, nil) 2385 field.SetVal(tag) 2386 Yyerror("cannot parenthesize embedded type") 2387 return list1(field) 2388 2389 } else { 2390 // '*' embed oliteral 2391 field := p.embed(nil) 2392 tag := p.oliteral() 2393 2394 field.Right = Nod(OIND, field.Right, nil) 2395 field.SetVal(tag) 2396 return list1(field) 2397 } 2398 2399 default: 2400 p.syntax_error("expecting field name or embedded type") 2401 p.advance(';', '}') 2402 return nil 2403 } 2404 } 2405 2406 func (p *parser) oliteral() (v Val) { 2407 if p.tok == LLITERAL { 2408 v = p.val 2409 p.next() 2410 } 2411 return 2412 } 2413 2414 func (p *parser) packname(name *Sym) *Sym { 2415 if trace && Debug['x'] != 0 { 2416 defer p.trace("embed")() 2417 } 2418 2419 if name != nil { 2420 // LNAME was already consumed and is coming in as name 2421 } else if p.tok == LNAME { 2422 name = p.sym_ 2423 p.next() 2424 } else { 2425 p.syntax_error("expecting name") 2426 p.advance('.', ';', '}') 2427 name = new(Sym) 2428 } 2429 2430 if p.got('.') { 2431 // LNAME '.' sym 2432 s := p.sym() 2433 2434 var pkg *Pkg 2435 if name.Def == nil || name.Def.Op != OPACK { 2436 Yyerror("%v is not a package", name) 2437 pkg = localpkg 2438 } else { 2439 name.Def.Used = true 2440 pkg = name.Def.Name.Pkg 2441 } 2442 return restrictlookup(s.Name, pkg) 2443 } 2444 2445 // LNAME 2446 if n := oldname(name); n.Name != nil && n.Name.Pack != nil { 2447 n.Name.Pack.Used = true 2448 } 2449 return name 2450 } 2451 2452 func (p *parser) embed(sym *Sym) *Node { 2453 if trace && Debug['x'] != 0 { 2454 defer p.trace("embed")() 2455 } 2456 2457 pkgname := p.packname(sym) 2458 return embedded(pkgname, localpkg) 2459 } 2460 2461 // MethodSpec = MethodName Signature | InterfaceTypeName . 2462 // MethodName = identifier . 2463 // InterfaceTypeName = TypeName . 2464 func (p *parser) interfacedcl() *Node { 2465 if trace && Debug['x'] != 0 { 2466 defer p.trace("interfacedcl")() 2467 } 2468 2469 switch p.tok { 2470 case LNAME: 2471 sym := p.sym_ 2472 p.next() 2473 2474 // accept potential name list but complain 2475 hasNameList := false 2476 for p.got(',') { 2477 p.sym() 2478 hasNameList = true 2479 } 2480 if hasNameList { 2481 p.syntax_error("name list not allowed in interface type") 2482 // already progressed, no need to advance 2483 } 2484 2485 if p.tok != '(' { 2486 // packname 2487 pname := p.packname(sym) 2488 return Nod(ODCLFIELD, nil, oldname(pname)) 2489 } 2490 2491 // newname indcl 2492 mname := newname(sym) 2493 sig := p.indcl() 2494 2495 meth := Nod(ODCLFIELD, mname, sig) 2496 ifacedcl(meth) 2497 return meth 2498 2499 case '(': 2500 p.next() 2501 pname := p.packname(nil) 2502 p.want(')') 2503 n := Nod(ODCLFIELD, nil, oldname(pname)) 2504 Yyerror("cannot parenthesize embedded type") 2505 return n 2506 2507 default: 2508 p.syntax_error("") 2509 p.advance(';', '}') 2510 return nil 2511 } 2512 } 2513 2514 // MethodSpec = MethodName Signature . 2515 // MethodName = identifier . 2516 func (p *parser) indcl() *Node { 2517 if trace && Debug['x'] != 0 { 2518 defer p.trace("indcl")() 2519 } 2520 2521 params := p.param_list() 2522 result := p.fnres() 2523 2524 // without func keyword 2525 params = checkarglist(params, 1) 2526 t := Nod(OTFUNC, fakethis(), nil) 2527 t.List = params 2528 t.Rlist = result 2529 2530 return t 2531 } 2532 2533 // ParameterDecl = [ IdentifierList ] [ "..." ] Type . 2534 func (p *parser) arg_type() *Node { 2535 if trace && Debug['x'] != 0 { 2536 defer p.trace("arg_type")() 2537 } 2538 2539 switch p.tok { 2540 case LNAME, '@', '?': 2541 name := p.sym() 2542 switch p.tok { 2543 case LCOMM, LFUNC, '[', LCHAN, LMAP, LSTRUCT, LINTERFACE, '*', LNAME, '@', '?', '(': 2544 // sym name_or_type 2545 typ := p.ntype() 2546 nn := Nod(ONONAME, nil, nil) 2547 nn.Sym = name 2548 return Nod(OKEY, nn, typ) 2549 2550 case LDDD: 2551 // sym dotdotdot 2552 typ := p.dotdotdot() 2553 nn := Nod(ONONAME, nil, nil) 2554 nn.Sym = name 2555 return Nod(OKEY, nn, typ) 2556 2557 default: 2558 // name_or_type 2559 name := mkname(name) 2560 // from dotname 2561 if p.got('.') { 2562 return p.new_dotname(name) 2563 } 2564 return name 2565 } 2566 2567 case LDDD: 2568 // dotdotdot 2569 return p.dotdotdot() 2570 2571 case LCOMM, LFUNC, '[', LCHAN, LMAP, LSTRUCT, LINTERFACE, '*', '(': 2572 // name_or_type 2573 return p.ntype() 2574 2575 default: 2576 p.syntax_error("expecting )") 2577 p.advance(',', ')') 2578 return nil 2579 } 2580 } 2581 2582 // Parameters = "(" [ ParameterList [ "," ] ] ")" . 2583 // ParameterList = ParameterDecl { "," ParameterDecl } . 2584 func (p *parser) param_list() (l *NodeList) { 2585 if trace && Debug['x'] != 0 { 2586 defer p.trace("param_list")() 2587 } 2588 2589 p.want('(') 2590 2591 for p.tok != EOF && p.tok != ')' { 2592 l = list(l, p.arg_type()) 2593 if !p.ocomma(')') { 2594 break 2595 } 2596 } 2597 2598 p.want(')') 2599 return 2600 } 2601 2602 var missing_stmt = Nod(OXXX, nil, nil) 2603 2604 // Statement = 2605 // Declaration | LabeledStmt | SimpleStmt | 2606 // GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt | 2607 // FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt | 2608 // DeferStmt . 2609 // 2610 // stmt may return missing_stmt. 2611 func (p *parser) stmt() *Node { 2612 if trace && Debug['x'] != 0 { 2613 defer p.trace("stmt")() 2614 } 2615 2616 switch p.tok { 2617 case '{': 2618 return p.compound_stmt(false) 2619 2620 case LVAR, LCONST, LTYPE: 2621 return liststmt(p.common_dcl()) 2622 2623 case LNAME, '@', '?', LLITERAL, LFUNC, '(', // operands 2624 '[', LSTRUCT, LMAP, LCHAN, LINTERFACE, // composite types 2625 '+', '-', '*', '&', '^', '~', LCOMM, '!': // unary operators 2626 return p.simple_stmt(true, false) 2627 2628 case LFOR: 2629 return p.for_stmt() 2630 2631 case LSWITCH: 2632 return p.switch_stmt() 2633 2634 case LSELECT: 2635 return p.select_stmt() 2636 2637 case LIF: 2638 return p.if_stmt() 2639 2640 case LFALL: 2641 p.next() 2642 // will be converted to OFALL 2643 stmt := Nod(OXFALL, nil, nil) 2644 stmt.Xoffset = int64(block) 2645 return stmt 2646 2647 case LBREAK: 2648 p.next() 2649 return Nod(OBREAK, p.onew_name(), nil) 2650 2651 case LCONTINUE: 2652 p.next() 2653 return Nod(OCONTINUE, p.onew_name(), nil) 2654 2655 case LGO: 2656 p.next() 2657 return Nod(OPROC, p.pseudocall(), nil) 2658 2659 case LDEFER: 2660 p.next() 2661 return Nod(ODEFER, p.pseudocall(), nil) 2662 2663 case LGOTO: 2664 p.next() 2665 stmt := Nod(OGOTO, p.new_name(p.sym()), nil) 2666 stmt.Sym = dclstack // context, for goto restrictions 2667 return stmt 2668 2669 case LRETURN: 2670 p.next() 2671 var results *NodeList 2672 if p.tok != ';' && p.tok != '}' { 2673 results = p.expr_list() 2674 } 2675 2676 stmt := Nod(ORETURN, nil, nil) 2677 stmt.List = results 2678 if stmt.List == nil && Curfn != nil { 2679 for l := Curfn.Func.Dcl; l != nil; l = l.Next { 2680 if l.N.Class == PPARAM { 2681 continue 2682 } 2683 if l.N.Class != PPARAMOUT { 2684 break 2685 } 2686 if l.N.Sym.Def != l.N { 2687 Yyerror("%s is shadowed during return", l.N.Sym.Name) 2688 } 2689 } 2690 } 2691 2692 return stmt 2693 2694 case ';': 2695 return nil 2696 2697 default: 2698 return missing_stmt 2699 } 2700 } 2701 2702 // StatementList = { Statement ";" } . 2703 func (p *parser) stmt_list() (l *NodeList) { 2704 if trace && Debug['x'] != 0 { 2705 defer p.trace("stmt_list")() 2706 } 2707 2708 for p.tok != EOF && p.tok != '}' && p.tok != LCASE && p.tok != LDEFAULT { 2709 s := p.stmt() 2710 if s == missing_stmt { 2711 break 2712 } 2713 l = list(l, s) 2714 // customized version of osemi: 2715 // ';' is optional before a closing ')' or '}' 2716 if p.tok == ')' || p.tok == '}' { 2717 continue 2718 } 2719 if !p.got(';') { 2720 p.syntax_error("at end of statement") 2721 p.advance(';', '}') 2722 } 2723 } 2724 return 2725 } 2726 2727 // IdentifierList = identifier { "," identifier } . 2728 // 2729 // If first != nil we have the first symbol already. 2730 func (p *parser) new_name_list(first *Sym) *NodeList { 2731 if trace && Debug['x'] != 0 { 2732 defer p.trace("new_name_list")() 2733 } 2734 2735 if first == nil { 2736 first = p.sym() // may still be nil 2737 } 2738 l := list1(p.new_name(first)) 2739 for p.got(',') { 2740 l = list(l, p.new_name(p.sym())) 2741 } 2742 return l 2743 } 2744 2745 // IdentifierList = identifier { "," identifier } . 2746 func (p *parser) dcl_name_list() *NodeList { 2747 if trace && Debug['x'] != 0 { 2748 defer p.trace("dcl_name_list")() 2749 } 2750 2751 l := list1(p.dcl_name(p.sym())) 2752 for p.got(',') { 2753 l = list(l, p.dcl_name(p.sym())) 2754 } 2755 return l 2756 } 2757 2758 // ExpressionList = Expression { "," Expression } . 2759 func (p *parser) expr_list() *NodeList { 2760 if trace && Debug['x'] != 0 { 2761 defer p.trace("expr_list")() 2762 } 2763 2764 l := list1(p.expr()) 2765 for p.got(',') { 2766 l = list(l, p.expr()) 2767 } 2768 return l 2769 } 2770 2771 // Arguments = "(" [ ( ExpressionList | Type [ "," ExpressionList ] ) [ "..." ] [ "," ] ] ")" . 2772 func (p *parser) arg_list() (l *NodeList, ddd bool) { 2773 if trace && Debug['x'] != 0 { 2774 defer p.trace("arg_list")() 2775 } 2776 2777 p.want('(') 2778 p.xnest++ 2779 2780 for p.tok != EOF && p.tok != ')' && !ddd { 2781 l = list(l, p.expr()) // expr_or_type 2782 ddd = p.got(LDDD) 2783 if !p.ocomma(')') { 2784 break 2785 } 2786 } 2787 2788 p.xnest-- 2789 p.want(')') 2790 2791 return 2792 } 2793 2794 // osemi parses an optional semicolon. 2795 func (p *parser) osemi(follow int32) bool { 2796 switch p.tok { 2797 case ';': 2798 p.next() 2799 return true 2800 2801 case ')', '}': 2802 // semicolon is optional before ) or } 2803 return true 2804 } 2805 2806 p.syntax_error("expecting semicolon, newline, or " + tokstring(follow)) 2807 p.advance(follow) 2808 return false 2809 } 2810 2811 // ocomma parses an optional comma. 2812 func (p *parser) ocomma(follow int32) bool { 2813 switch p.tok { 2814 case ',': 2815 p.next() 2816 return true 2817 2818 case ')', '}': 2819 // comma is optional before ) or } 2820 return true 2821 } 2822 2823 p.syntax_error("expecting comma or " + tokstring(follow)) 2824 p.advance(follow) 2825 return false 2826 } 2827 2828 // ---------------------------------------------------------------------------- 2829 // Importing packages 2830 2831 func (p *parser) import_error() { 2832 p.syntax_error("in export data of imported package") 2833 p.next() 2834 } 2835 2836 // The methods below reflect a 1:1 translation of the original (and now defunct) 2837 // go.y yacc productions. They could be simplified significantly and also use better 2838 // variable names. However, we will be able to delete them once we enable the 2839 // new export format by default, so it's not worth the effort (issue 13241). 2840 2841 func (p *parser) hidden_importsym() *Sym { 2842 if trace && Debug['x'] != 0 { 2843 defer p.trace("hidden_importsym")() 2844 } 2845 2846 p.want('@') 2847 var s2 Val 2848 if p.tok == LLITERAL { 2849 s2 = p.val 2850 p.next() 2851 } else { 2852 p.import_error() 2853 } 2854 p.want('.') 2855 2856 switch p.tok { 2857 case LNAME: 2858 s4 := p.sym_ 2859 p.next() 2860 2861 var p *Pkg 2862 2863 if s2.U.(string) == "" { 2864 p = importpkg 2865 } else { 2866 if isbadimport(s2.U.(string)) { 2867 errorexit() 2868 } 2869 p = mkpkg(s2.U.(string)) 2870 } 2871 return Pkglookup(s4.Name, p) 2872 2873 case '?': 2874 p.next() 2875 2876 var p *Pkg 2877 2878 if s2.U.(string) == "" { 2879 p = importpkg 2880 } else { 2881 if isbadimport(s2.U.(string)) { 2882 errorexit() 2883 } 2884 p = mkpkg(s2.U.(string)) 2885 } 2886 return Pkglookup("?", p) 2887 2888 default: 2889 p.import_error() 2890 return nil 2891 } 2892 } 2893 2894 func (p *parser) ohidden_funarg_list() *NodeList { 2895 if trace && Debug['x'] != 0 { 2896 defer p.trace("ohidden_funarg_list")() 2897 } 2898 2899 var ss *NodeList 2900 if p.tok != ')' { 2901 ss = p.hidden_funarg_list() 2902 } 2903 return ss 2904 } 2905 2906 func (p *parser) ohidden_structdcl_list() *NodeList { 2907 if trace && Debug['x'] != 0 { 2908 defer p.trace("ohidden_structdcl_list")() 2909 } 2910 2911 var ss *NodeList 2912 if p.tok != '}' { 2913 ss = p.hidden_structdcl_list() 2914 } 2915 return ss 2916 } 2917 2918 func (p *parser) ohidden_interfacedcl_list() *NodeList { 2919 if trace && Debug['x'] != 0 { 2920 defer p.trace("ohidden_interfacedcl_list")() 2921 } 2922 2923 var ss *NodeList 2924 if p.tok != '}' { 2925 ss = p.hidden_interfacedcl_list() 2926 } 2927 return ss 2928 } 2929 2930 // import syntax from package header 2931 func (p *parser) hidden_import() { 2932 if trace && Debug['x'] != 0 { 2933 defer p.trace("hidden_import")() 2934 } 2935 2936 switch p.tok { 2937 case LIMPORT: 2938 // LIMPORT LNAME LLITERAL ';' 2939 p.next() 2940 var s2 *Sym 2941 if p.tok == LNAME { 2942 s2 = p.sym_ 2943 p.next() 2944 } else { 2945 p.import_error() 2946 } 2947 var s3 Val 2948 if p.tok == LLITERAL { 2949 s3 = p.val 2950 p.next() 2951 } else { 2952 p.import_error() 2953 } 2954 p.want(';') 2955 2956 importimport(s2, s3.U.(string)) 2957 2958 case LVAR: 2959 // LVAR hidden_pkg_importsym hidden_type ';' 2960 p.next() 2961 s2 := p.hidden_pkg_importsym() 2962 s3 := p.hidden_type() 2963 p.want(';') 2964 2965 importvar(s2, s3) 2966 2967 case LCONST: 2968 // LCONST hidden_pkg_importsym '=' hidden_constant ';' 2969 // LCONST hidden_pkg_importsym hidden_type '=' hidden_constant ';' 2970 p.next() 2971 s2 := p.hidden_pkg_importsym() 2972 var s3 *Type = Types[TIDEAL] 2973 if p.tok != '=' { 2974 s3 = p.hidden_type() 2975 } 2976 p.want('=') 2977 s4 := p.hidden_constant() 2978 p.want(';') 2979 2980 importconst(s2, s3, s4) 2981 2982 case LTYPE: 2983 // LTYPE hidden_pkgtype hidden_type ';' 2984 p.next() 2985 s2 := p.hidden_pkgtype() 2986 s3 := p.hidden_type() 2987 p.want(';') 2988 2989 importtype(s2, s3) 2990 2991 case LFUNC: 2992 // LFUNC hidden_fndcl fnbody ';' 2993 p.next() 2994 s2 := p.hidden_fndcl() 2995 s3 := p.fnbody() 2996 p.want(';') 2997 2998 if s2 == nil { 2999 dclcontext = PEXTERN // since we skip the funcbody below 3000 return 3001 } 3002 3003 s2.Func.Inl = s3 3004 3005 funcbody(s2) 3006 importlist = append(importlist, s2) 3007 3008 if Debug['E'] > 0 { 3009 fmt.Printf("import [%q] func %v \n", importpkg.Path, s2) 3010 if Debug['m'] > 2 && s2.Func.Inl != nil { 3011 fmt.Printf("inl body:%v\n", s2.Func.Inl) 3012 } 3013 } 3014 3015 default: 3016 p.import_error() 3017 } 3018 } 3019 3020 func (p *parser) hidden_pkg_importsym() *Sym { 3021 if trace && Debug['x'] != 0 { 3022 defer p.trace("hidden_pkg_importsym")() 3023 } 3024 3025 s1 := p.hidden_importsym() 3026 3027 ss := s1 3028 structpkg = ss.Pkg 3029 3030 return ss 3031 } 3032 3033 func (p *parser) hidden_pkgtype() *Type { 3034 if trace && Debug['x'] != 0 { 3035 defer p.trace("hidden_pkgtype")() 3036 } 3037 3038 s1 := p.hidden_pkg_importsym() 3039 3040 ss := pkgtype(s1) 3041 importsym(s1, OTYPE) 3042 3043 return ss 3044 } 3045 3046 // ---------------------------------------------------------------------------- 3047 // Importing types 3048 3049 func (p *parser) hidden_type() *Type { 3050 if trace && Debug['x'] != 0 { 3051 defer p.trace("hidden_type")() 3052 } 3053 3054 switch p.tok { 3055 default: 3056 return p.hidden_type_misc() 3057 case LCOMM: 3058 return p.hidden_type_recv_chan() 3059 case LFUNC: 3060 return p.hidden_type_func() 3061 } 3062 } 3063 3064 func (p *parser) hidden_type_non_recv_chan() *Type { 3065 if trace && Debug['x'] != 0 { 3066 defer p.trace("hidden_type_non_recv_chan")() 3067 } 3068 3069 switch p.tok { 3070 default: 3071 return p.hidden_type_misc() 3072 case LFUNC: 3073 return p.hidden_type_func() 3074 } 3075 } 3076 3077 func (p *parser) hidden_type_misc() *Type { 3078 if trace && Debug['x'] != 0 { 3079 defer p.trace("hidden_type_misc")() 3080 } 3081 3082 switch p.tok { 3083 case '@': 3084 // hidden_importsym 3085 s1 := p.hidden_importsym() 3086 return pkgtype(s1) 3087 3088 case LNAME: 3089 // LNAME 3090 s1 := p.sym_ 3091 p.next() 3092 3093 // predefined name like uint8 3094 s1 = Pkglookup(s1.Name, builtinpkg) 3095 if s1.Def == nil || s1.Def.Op != OTYPE { 3096 Yyerror("%s is not a type", s1.Name) 3097 return nil 3098 } else { 3099 return s1.Def.Type 3100 } 3101 3102 case '[': 3103 // '[' ']' hidden_type 3104 // '[' LLITERAL ']' hidden_type 3105 p.next() 3106 var s2 *Node 3107 if p.tok == LLITERAL { 3108 s2 = nodlit(p.val) 3109 p.next() 3110 } 3111 p.want(']') 3112 s4 := p.hidden_type() 3113 3114 return aindex(s2, s4) 3115 3116 case LMAP: 3117 // LMAP '[' hidden_type ']' hidden_type 3118 p.next() 3119 p.want('[') 3120 s3 := p.hidden_type() 3121 p.want(']') 3122 s5 := p.hidden_type() 3123 3124 return maptype(s3, s5) 3125 3126 case LSTRUCT: 3127 // LSTRUCT '{' ohidden_structdcl_list '}' 3128 p.next() 3129 p.want('{') 3130 s3 := p.ohidden_structdcl_list() 3131 p.want('}') 3132 3133 return tostruct(s3) 3134 3135 case LINTERFACE: 3136 // LINTERFACE '{' ohidden_interfacedcl_list '}' 3137 p.next() 3138 p.want('{') 3139 s3 := p.ohidden_interfacedcl_list() 3140 p.want('}') 3141 3142 return tointerface(s3) 3143 3144 case '*': 3145 // '*' hidden_type 3146 p.next() 3147 s2 := p.hidden_type() 3148 return Ptrto(s2) 3149 3150 case LCHAN: 3151 p.next() 3152 switch p.tok { 3153 default: 3154 // LCHAN hidden_type_non_recv_chan 3155 s2 := p.hidden_type_non_recv_chan() 3156 ss := typ(TCHAN) 3157 ss.Type = s2 3158 ss.Chan = Cboth 3159 return ss 3160 3161 case '(': 3162 // LCHAN '(' hidden_type_recv_chan ')' 3163 p.next() 3164 s3 := p.hidden_type_recv_chan() 3165 p.want(')') 3166 ss := typ(TCHAN) 3167 ss.Type = s3 3168 ss.Chan = Cboth 3169 return ss 3170 3171 case LCOMM: 3172 // LCHAN hidden_type 3173 p.next() 3174 s3 := p.hidden_type() 3175 ss := typ(TCHAN) 3176 ss.Type = s3 3177 ss.Chan = Csend 3178 return ss 3179 } 3180 3181 default: 3182 p.import_error() 3183 return nil 3184 } 3185 } 3186 3187 func (p *parser) hidden_type_recv_chan() *Type { 3188 if trace && Debug['x'] != 0 { 3189 defer p.trace("hidden_type_recv_chan")() 3190 } 3191 3192 p.want(LCOMM) 3193 p.want(LCHAN) 3194 s3 := p.hidden_type() 3195 3196 ss := typ(TCHAN) 3197 ss.Type = s3 3198 ss.Chan = Crecv 3199 return ss 3200 } 3201 3202 func (p *parser) hidden_type_func() *Type { 3203 if trace && Debug['x'] != 0 { 3204 defer p.trace("hidden_type_func")() 3205 } 3206 3207 p.want(LFUNC) 3208 p.want('(') 3209 s3 := p.ohidden_funarg_list() 3210 p.want(')') 3211 s5 := p.ohidden_funres() 3212 3213 return functype(nil, s3, s5) 3214 } 3215 3216 func (p *parser) hidden_funarg() *Node { 3217 if trace && Debug['x'] != 0 { 3218 defer p.trace("hidden_funarg")() 3219 } 3220 3221 s1 := p.sym() 3222 switch p.tok { 3223 default: 3224 s2 := p.hidden_type() 3225 s3 := p.oliteral() 3226 3227 ss := Nod(ODCLFIELD, nil, typenod(s2)) 3228 if s1 != nil { 3229 ss.Left = newname(s1) 3230 } 3231 ss.SetVal(s3) 3232 return ss 3233 3234 case LDDD: 3235 p.next() 3236 s3 := p.hidden_type() 3237 s4 := p.oliteral() 3238 3239 var t *Type 3240 3241 t = typ(TARRAY) 3242 t.Bound = -1 3243 t.Type = s3 3244 3245 ss := Nod(ODCLFIELD, nil, typenod(t)) 3246 if s1 != nil { 3247 ss.Left = newname(s1) 3248 } 3249 ss.Isddd = true 3250 ss.SetVal(s4) 3251 3252 return ss 3253 } 3254 } 3255 3256 func (p *parser) hidden_structdcl() *Node { 3257 if trace && Debug['x'] != 0 { 3258 defer p.trace("hidden_structdcl")() 3259 } 3260 3261 s1 := p.sym() 3262 s2 := p.hidden_type() 3263 s3 := p.oliteral() 3264 3265 var s *Sym 3266 var pkg *Pkg 3267 3268 var ss *Node 3269 if s1 != nil && s1.Name != "?" { 3270 ss = Nod(ODCLFIELD, newname(s1), typenod(s2)) 3271 ss.SetVal(s3) 3272 } else { 3273 s = s2.Sym 3274 if s == nil && Isptr[s2.Etype] { 3275 s = s2.Type.Sym 3276 } 3277 pkg = importpkg 3278 if s1 != nil { 3279 pkg = s1.Pkg 3280 } 3281 ss = embedded(s, pkg) 3282 ss.Right = typenod(s2) 3283 ss.SetVal(s3) 3284 } 3285 3286 return ss 3287 } 3288 3289 func (p *parser) hidden_interfacedcl() *Node { 3290 if trace && Debug['x'] != 0 { 3291 defer p.trace("hidden_interfacedcl")() 3292 } 3293 3294 // The original (now defunct) grammar in go.y accepted both a method 3295 // or an (embedded) type: 3296 // 3297 // hidden_interfacedcl: 3298 // sym '(' ohidden_funarg_list ')' ohidden_funres 3299 // { 3300 // $$ = Nod(ODCLFIELD, newname($1), typenod(functype(fakethis(), $3, $5))); 3301 // } 3302 // | hidden_type 3303 // { 3304 // $$ = Nod(ODCLFIELD, nil, typenod($1)); 3305 // } 3306 // 3307 // But the current textual export code only exports (inlined) methods, 3308 // even if the methods came from embedded interfaces. Furthermore, in 3309 // the original grammar, hidden_type may also start with a sym (LNAME 3310 // or '@'), complicating matters further. Since we never have embedded 3311 // types, only parse methods here. 3312 3313 s1 := p.sym() 3314 p.want('(') 3315 s3 := p.ohidden_funarg_list() 3316 p.want(')') 3317 s5 := p.ohidden_funres() 3318 3319 return Nod(ODCLFIELD, newname(s1), typenod(functype(fakethis(), s3, s5))) 3320 } 3321 3322 func (p *parser) ohidden_funres() *NodeList { 3323 if trace && Debug['x'] != 0 { 3324 defer p.trace("ohidden_funres")() 3325 } 3326 3327 switch p.tok { 3328 default: 3329 return nil 3330 3331 case '(', '@', LNAME, '[', LMAP, LSTRUCT, LINTERFACE, '*', LCHAN, LCOMM, LFUNC: 3332 return p.hidden_funres() 3333 } 3334 } 3335 3336 func (p *parser) hidden_funres() *NodeList { 3337 if trace && Debug['x'] != 0 { 3338 defer p.trace("hidden_funres")() 3339 } 3340 3341 switch p.tok { 3342 case '(': 3343 p.next() 3344 s2 := p.ohidden_funarg_list() 3345 p.want(')') 3346 return s2 3347 3348 default: 3349 s1 := p.hidden_type() 3350 return list1(Nod(ODCLFIELD, nil, typenod(s1))) 3351 } 3352 } 3353 3354 // ---------------------------------------------------------------------------- 3355 // Importing constants 3356 3357 func (p *parser) hidden_literal() *Node { 3358 if trace && Debug['x'] != 0 { 3359 defer p.trace("hidden_literal")() 3360 } 3361 3362 switch p.tok { 3363 case LLITERAL: 3364 ss := nodlit(p.val) 3365 p.next() 3366 return ss 3367 3368 case '-': 3369 p.next() 3370 if p.tok == LLITERAL { 3371 ss := nodlit(p.val) 3372 p.next() 3373 switch ss.Val().Ctype() { 3374 case CTINT, CTRUNE: 3375 mpnegfix(ss.Val().U.(*Mpint)) 3376 break 3377 case CTFLT: 3378 mpnegflt(ss.Val().U.(*Mpflt)) 3379 break 3380 case CTCPLX: 3381 mpnegflt(&ss.Val().U.(*Mpcplx).Real) 3382 mpnegflt(&ss.Val().U.(*Mpcplx).Imag) 3383 break 3384 default: 3385 Yyerror("bad negated constant") 3386 } 3387 return ss 3388 } else { 3389 p.import_error() 3390 return nil 3391 } 3392 3393 case LNAME, '@', '?': 3394 s1 := p.sym() 3395 ss := oldname(Pkglookup(s1.Name, builtinpkg)) 3396 if ss.Op != OLITERAL { 3397 Yyerror("bad constant %v", ss.Sym) 3398 } 3399 return ss 3400 3401 default: 3402 p.import_error() 3403 return nil 3404 } 3405 } 3406 3407 func (p *parser) hidden_constant() *Node { 3408 if trace && Debug['x'] != 0 { 3409 defer p.trace("hidden_constant")() 3410 } 3411 3412 switch p.tok { 3413 default: 3414 return p.hidden_literal() 3415 case '(': 3416 p.next() 3417 s2 := p.hidden_literal() 3418 p.want('+') 3419 s4 := p.hidden_literal() 3420 p.want(')') 3421 3422 if s2.Val().Ctype() == CTRUNE && s4.Val().Ctype() == CTINT { 3423 ss := s2 3424 mpaddfixfix(s2.Val().U.(*Mpint), s4.Val().U.(*Mpint), 0) 3425 return ss 3426 } 3427 s4.Val().U.(*Mpcplx).Real = s4.Val().U.(*Mpcplx).Imag 3428 Mpmovecflt(&s4.Val().U.(*Mpcplx).Imag, 0.0) 3429 return nodcplxlit(s2.Val(), s4.Val()) 3430 } 3431 } 3432 3433 func (p *parser) hidden_import_list() { 3434 if trace && Debug['x'] != 0 { 3435 defer p.trace("hidden_import_list")() 3436 } 3437 3438 for p.tok != '$' { 3439 p.hidden_import() 3440 } 3441 } 3442 3443 func (p *parser) hidden_funarg_list() *NodeList { 3444 if trace && Debug['x'] != 0 { 3445 defer p.trace("hidden_funarg_list")() 3446 } 3447 3448 s1 := p.hidden_funarg() 3449 ss := list1(s1) 3450 for p.got(',') { 3451 s3 := p.hidden_funarg() 3452 ss = list(ss, s3) 3453 } 3454 return ss 3455 } 3456 3457 func (p *parser) hidden_structdcl_list() *NodeList { 3458 if trace && Debug['x'] != 0 { 3459 defer p.trace("hidden_structdcl_list")() 3460 } 3461 3462 s1 := p.hidden_structdcl() 3463 ss := list1(s1) 3464 for p.got(';') { 3465 s3 := p.hidden_structdcl() 3466 ss = list(ss, s3) 3467 } 3468 return ss 3469 } 3470 3471 func (p *parser) hidden_interfacedcl_list() *NodeList { 3472 if trace && Debug['x'] != 0 { 3473 defer p.trace("hidden_interfacedcl_list")() 3474 } 3475 3476 s1 := p.hidden_interfacedcl() 3477 ss := list1(s1) 3478 for p.got(';') { 3479 s3 := p.hidden_interfacedcl() 3480 ss = list(ss, s3) 3481 } 3482 return ss 3483 }