Debian Codegraph @

interp/ast.go

raw symbols SCIP
1 package interp
2
3 import (
4 "fmt"
5 "go/ast"
6 "go/constant"
7 "go/parser"
8 "go/scanner"
9 "go/token"
10 "reflect"
11 "strconv"
12 "strings"
13 "sync/atomic"
14 )
15
16 // nkind defines the kind of AST, i.e. the grammar category.
17 type nkind uint
18
19 // Node kinds for the go language.
20 const (
21 undefNode nkind = iota
22 addressExpr
23 arrayType
24 assignStmt
25 assignXStmt
26 basicLit
27 binaryExpr
28 blockStmt
29 branchStmt
30 breakStmt
31 callExpr
32 caseBody
33 caseClause
34 chanType
35 chanTypeSend
36 chanTypeRecv
37 commClause
38 commClauseDefault
39 compositeLitExpr
40 constDecl
41 continueStmt
42 declStmt
43 deferStmt
44 defineStmt
45 defineXStmt
46 ellipsisExpr
47 exprStmt
48 fallthroughtStmt
49 fieldExpr
50 fieldList
51 fileStmt
52 forStmt0 // for {}
53 forStmt1 // for init; ; {}
54 forStmt2 // for cond {}
55 forStmt3 // for init; cond; {}
56 forStmt4 // for ; ; post {}
57 forStmt5 // for ; cond; post {}
58 forStmt6 // for init; ; post {}
59 forStmt7 // for init; cond; post {}
60 forRangeStmt // for range {}
61 funcDecl
62 funcLit
63 funcType
64 goStmt
65 gotoStmt
66 identExpr
67 ifStmt0 // if cond {}
68 ifStmt1 // if cond {} else {}
69 ifStmt2 // if init; cond {}
70 ifStmt3 // if init; cond {} else {}
71 importDecl
72 importSpec
73 incDecStmt
74 indexExpr
75 indexListExpr
76 interfaceType
77 keyValueExpr
78 labeledStmt
79 landExpr
80 lorExpr
81 mapType
82 parenExpr
83 rangeStmt
84 returnStmt
85 selectStmt
86 selectorExpr
87 selectorImport
88 sendStmt
89 sliceExpr
90 starExpr
91 structType
92 switchStmt
93 switchIfStmt
94 typeAssertExpr
95 typeDecl
96 typeSpec // type A int
97 typeSpecAssign // type A = int
98 typeSwitch
99 unaryExpr
100 valueSpec
101 varDecl
102 )
103
104 var kinds = [...]string{
105 undefNode: "undefNode",
106 addressExpr: "addressExpr",
107 arrayType: "arrayType",
108 assignStmt: "assignStmt",
109 assignXStmt: "assignXStmt",
110 basicLit: "basicLit",
111 binaryExpr: "binaryExpr",
112 blockStmt: "blockStmt",
113 branchStmt: "branchStmt",
114 breakStmt: "breakStmt",
115 callExpr: "callExpr",
116 caseBody: "caseBody",
117 caseClause: "caseClause",
118 chanType: "chanType",
119 chanTypeSend: "chanTypeSend",
120 chanTypeRecv: "chanTypeRecv",
121 commClause: "commClause",
122 commClauseDefault: "commClauseDefault",
123 compositeLitExpr: "compositeLitExpr",
124 constDecl: "constDecl",
125 continueStmt: "continueStmt",
126 declStmt: "declStmt",
127 deferStmt: "deferStmt",
128 defineStmt: "defineStmt",
129 defineXStmt: "defineXStmt",
130 ellipsisExpr: "ellipsisExpr",
131 exprStmt: "exprStmt",
132 fallthroughtStmt: "fallthroughStmt",
133 fieldExpr: "fieldExpr",
134 fieldList: "fieldList",
135 fileStmt: "fileStmt",
136 forStmt0: "forStmt0",
137 forStmt1: "forStmt1",
138 forStmt2: "forStmt2",
139 forStmt3: "forStmt3",
140 forStmt4: "forStmt4",
141 forStmt5: "forStmt5",
142 forStmt6: "forStmt6",
143 forStmt7: "forStmt7",
144 forRangeStmt: "forRangeStmt",
145 funcDecl: "funcDecl",
146 funcType: "funcType",
147 funcLit: "funcLit",
148 goStmt: "goStmt",
149 gotoStmt: "gotoStmt",
150 identExpr: "identExpr",
151 ifStmt0: "ifStmt0",
152 ifStmt1: "ifStmt1",
153 ifStmt2: "ifStmt2",
154 ifStmt3: "ifStmt3",
155 importDecl: "importDecl",
156 importSpec: "importSpec",
157 incDecStmt: "incDecStmt",
158 indexExpr: "indexExpr",
159 indexListExpr: "indexListExpr",
160 interfaceType: "interfaceType",
161 keyValueExpr: "keyValueExpr",
162 labeledStmt: "labeledStmt",
163 landExpr: "landExpr",
164 lorExpr: "lorExpr",
165 mapType: "mapType",
166 parenExpr: "parenExpr",
167 rangeStmt: "rangeStmt",
168 returnStmt: "returnStmt",
169 selectStmt: "selectStmt",
170 selectorExpr: "selectorExpr",
171 selectorImport: "selectorImport",
172 sendStmt: "sendStmt",
173 sliceExpr: "sliceExpr",
174 starExpr: "starExpr",
175 structType: "structType",
176 switchStmt: "switchStmt",
177 switchIfStmt: "switchIfStmt",
178 typeAssertExpr: "typeAssertExpr",
179 typeDecl: "typeDecl",
180 typeSpec: "typeSpec",
181 typeSpecAssign: "typeSpecAssign",
182 typeSwitch: "typeSwitch",
183 unaryExpr: "unaryExpr",
184 valueSpec: "valueSpec",
185 varDecl: "varDecl",
186 }
187
188 func (k nkind) String() string {
189 if k < nkind(len(kinds)) {
190 return kinds[k]
191 }
192 return "nKind(" + strconv.Itoa(int(k)) + ")"
193 }
194
195 // astError represents an error during AST build stage.
196 type astError error
197
198 // action defines the node action to perform at execution.
199 type action uint
200
201 // Node actions for the go language.
202 // It is important for type checking that *Assign directly
203 // follows it non-assign counterpart.
204 const (
205 aNop action = iota
206 aAddr
207 aAssign
208 aAssignX
209 aAdd
210 aAddAssign
211 aAnd
212 aAndAssign
213 aAndNot
214 aAndNotAssign
215 aBitNot
216 aBranch
217 aCall
218 aCallSlice
219 aCase
220 aCompositeLit
221 aConvert
222 aDec
223 aEqual
224 aGreater
225 aGreaterEqual
226 aGetFunc
227 aGetIndex
228 aGetMethod
229 aGetSym
230 aInc
231 aLand
232 aLor
233 aLower
234 aLowerEqual
235 aMethod
236 aMul
237 aMulAssign
238 aNeg
239 aNot
240 aNotEqual
241 aOr
242 aOrAssign
243 aPos
244 aQuo
245 aQuoAssign
246 aRange
247 aRecv
248 aRem
249 aRemAssign
250 aReturn
251 aSend
252 aShl
253 aShlAssign
254 aShr
255 aShrAssign
256 aSlice
257 aSlice0
258 aStar
259 aSub
260 aSubAssign
261 aTypeAssert
262 aXor
263 aXorAssign
264 )
265
266 var actions = [...]string{
267 aNop: "nop",
268 aAddr: "&",
269 aAssign: "=",
270 aAssignX: "X=",
271 aAdd: "+",
272 aAddAssign: "+=",
273 aAnd: "&",
274 aAndAssign: "&=",
275 aAndNot: "&^",
276 aAndNotAssign: "&^=",
277 aBitNot: "^",
278 aBranch: "branch",
279 aCall: "call",
280 aCallSlice: "callSlice",
281 aCase: "case",
282 aCompositeLit: "compositeLit",
283 aConvert: "convert",
284 aDec: "--",
285 aEqual: "==",
286 aGreater: ">",
287 aGreaterEqual: ">=",
288 aGetFunc: "getFunc",
289 aGetIndex: "getIndex",
290 aGetMethod: "getMethod",
291 aGetSym: ".",
292 aInc: "++",
293 aLand: "&&",
294 aLor: "||",
295 aLower: "<",
296 aLowerEqual: "<=",
297 aMethod: "Method",
298 aMul: "*",
299 aMulAssign: "*=",
300 aNeg: "-",
301 aNot: "!",
302 aNotEqual: "!=",
303 aOr: "|",
304 aOrAssign: "|=",
305 aPos: "+",
306 aQuo: "/",
307 aQuoAssign: "/=",
308 aRange: "range",
309 aRecv: "<-",
310 aRem: "%",
311 aRemAssign: "%=",
312 aReturn: "return",
313 aSend: "<~",
314 aShl: "<<",
315 aShlAssign: "<<=",
316 aShr: ">>",
317 aShrAssign: ">>=",
318 aSlice: "slice",
319 aSlice0: "slice0",
320 aStar: "*",
321 aSub: "-",
322 aSubAssign: "-=",
323 aTypeAssert: "TypeAssert",
324 aXor: "^",
325 aXorAssign: "^=",
326 }
327
328 func (a action) String() string {
329 if a < action(len(actions)) {
330 return actions[a]
331 }
332 return "Action(" + strconv.Itoa(int(a)) + ")"
333 }
334
335 func isAssignAction(a action) bool {
336 switch a {
337 case aAddAssign, aAndAssign, aAndNotAssign, aMulAssign, aOrAssign,
338 aQuoAssign, aRemAssign, aShlAssign, aShrAssign, aSubAssign, aXorAssign:
339 return true
340 }
341 return false
342 }
343
344 func (interp *Interpreter) firstToken(src string) token.Token {
345 var s scanner.Scanner
346 file := interp.fset.AddFile("", interp.fset.Base(), len(src))
347 s.Init(file, []byte(src), nil, 0)
348
349 _, tok, _ := s.Scan()
350 return tok
351 }
352
353 func ignoreError(err error, src string) bool {
354 se, ok := err.(scanner.ErrorList)
355 if !ok {
356 return false
357 }
358 if len(se) == 0 {
359 return false
360 }
361 return ignoreScannerError(se[0], src)
362 }
363
364 func wrapInMain(src string) string {
365 return fmt.Sprintf("package main; func main() {%s\n}", src)
366 }
367
368 func (interp *Interpreter) parse(src, name string, inc bool) (node ast.Node, err error) {
369 mode := parser.DeclarationErrors
370
371 // Allow incremental parsing of declarations or statements, by inserting
372 // them in a pseudo file package or function. Those statements or
373 // declarations will be always evaluated in the global scope.
374 var tok token.Token
375 var inFunc bool
376 if inc {
377 tok = interp.firstToken(src)
378 switch tok {
379 case token.PACKAGE:
380 // nothing to do.
381 case token.CONST, token.FUNC, token.IMPORT, token.TYPE, token.VAR:
382 src = "package main;" + src
383 default:
384 inFunc = true
385 src = wrapInMain(src)
386 }
387 // Parse comments in REPL mode, to allow tag setting.
388 mode |= parser.ParseComments
389 }
390
391 if ok, err := interp.buildOk(&interp.context, name, src); !ok || err != nil {
392 return nil, err // skip source not matching build constraints
393 }
394
395 f, err := parser.ParseFile(interp.fset, name, src, mode)
396 if err != nil {
397 // only retry if we're on an expression/statement about a func
398 if !inc || tok != token.FUNC {
399 return nil, err
400 }
401 // do not bother retrying if we know it's an error we're going to ignore later on.
402 if ignoreError(err, src) {
403 return nil, err
404 }
405 // do not lose initial error, in case retrying fails.
406 initialError := err
407 // retry with default source code "wrapping", in the main function scope.
408 src := wrapInMain(strings.TrimPrefix(src, "package main;"))
409 f, err = parser.ParseFile(interp.fset, name, src, mode)
410 if err != nil {
411 return nil, initialError
412 }
413 }
414
415 if inFunc {
416 // return the body of the wrapper main function
417 return f.Decls[0].(*ast.FuncDecl).Body, nil
418 }
419
420 setYaegiTags(&interp.context, f.Comments)
421 return f, nil
422 }
423
424 // Note: no type analysis is performed at this stage, it is done in pre-order
425 // processing of CFG, in order to accommodate forward type declarations.
426
427 // ast parses src string containing Go code and generates the corresponding AST.
428 // The package name and the AST root node are returned.
429 // The given name is used to set the filename of the relevant source file in the
430 // interpreter's FileSet.
431 func (interp *Interpreter) ast(f ast.Node) (string, *node, error) {
432 var err error
433 var root *node
434 var anc astNode
435 var st nodestack
436 pkgName := "main"
437
438 addChild := func(root **node, anc astNode, pos token.Pos, kind nkind, act action) *node {
439 var i interface{}
440 nindex := atomic.AddInt64(&interp.nindex, 1)
441 n := &node{anc: anc.node, interp: interp, index: nindex, pos: pos, kind: kind, action: act, val: &i, gen: builtin[act]}
442 n.start = n
443 if anc.node == nil {
444 *root = n
445 } else {
446 anc.node.child = append(anc.node.child, n)
447 if anc.node.action == aCase {
448 ancAst := anc.ast.(*ast.CaseClause)
449 if len(ancAst.List)+len(ancAst.Body) == len(anc.node.child) {
450 // All case clause children are collected.
451 // Split children in condition and body nodes to desambiguify the AST.
452 nindex = atomic.AddInt64(&interp.nindex, 1)
453 body := &node{anc: anc.node, interp: interp, index: nindex, pos: pos, kind: caseBody, action: aNop, val: &i, gen: nop}
454
455 if ts := anc.node.anc.anc; ts.kind == typeSwitch && ts.child[1].action == aAssign {
456 // In type switch clause, if a switch guard is assigned, duplicate the switch guard symbol
457 // in each clause body, so a different guard type can be set in each clause
458 name := ts.child[1].child[0].ident
459 nindex = atomic.AddInt64(&interp.nindex, 1)
460 gn := &node{anc: body, interp: interp, ident: name, index: nindex, pos: pos, kind: identExpr, action: aNop, val: &i, gen: nop}
461 body.child = append(body.child, gn)
462 }
463
464 // Add regular body children
465 body.child = append(body.child, anc.node.child[len(ancAst.List):]...)
466 for i := range body.child {
467 body.child[i].anc = body
468 }
469 anc.node.child = append(anc.node.child[:len(ancAst.List)], body)
470 }
471 }
472 }
473 return n
474 }
475
476 // Populate our own private AST from Go parser AST.
477 // A stack of ancestor nodes is used to keep track of current ancestor for each depth level
478 ast.Inspect(f, func(nod ast.Node) bool {
479 anc = st.top()
480 var pos token.Pos
481 if nod != nil {
482 pos = nod.Pos()
483 }
484 switch a := nod.(type) {
485 case nil:
486 anc = st.pop()
487
488 case *ast.ArrayType:
489 st.push(addChild(&root, anc, pos, arrayType, aNop), nod)
490
491 case *ast.AssignStmt:
492 var act action
493 var kind nkind
494 if len(a.Lhs) > 1 && len(a.Rhs) == 1 {
495 if a.Tok == token.DEFINE {
496 kind = defineXStmt
497 } else {
498 kind = assignXStmt
499 }
500 act = aAssignX
501 } else {
502 kind = assignStmt
503 switch a.Tok {
504 case token.ASSIGN:
505 act = aAssign
506 case token.ADD_ASSIGN:
507 act = aAddAssign
508 case token.AND_ASSIGN:
509 act = aAndAssign
510 case token.AND_NOT_ASSIGN:
511 act = aAndNotAssign
512 case token.DEFINE:
513 kind = defineStmt
514 act = aAssign
515 case token.SHL_ASSIGN:
516 act = aShlAssign
517 case token.SHR_ASSIGN:
518 act = aShrAssign
519 case token.MUL_ASSIGN:
520 act = aMulAssign
521 case token.OR_ASSIGN:
522 act = aOrAssign
523 case token.QUO_ASSIGN:
524 act = aQuoAssign
525 case token.REM_ASSIGN:
526 act = aRemAssign
527 case token.SUB_ASSIGN:
528 act = aSubAssign
529 case token.XOR_ASSIGN:
530 act = aXorAssign
531 }
532 }
533 n := addChild(&root, anc, pos, kind, act)
534 n.nleft = len(a.Lhs)
535 n.nright = len(a.Rhs)
536 st.push(n, nod)
537
538 case *ast.BasicLit:
539 n := addChild(&root, anc, pos, basicLit, aNop)
540 n.ident = a.Value
541 switch a.Kind {
542 case token.CHAR:
543 // Char cannot be converted to a const here as we cannot tell the type.
544 v, _, _, _ := strconv.UnquoteChar(a.Value[1:len(a.Value)-1], '\'')
545 n.rval = reflect.ValueOf(v)
546 case token.FLOAT, token.IMAG, token.INT, token.STRING:
547 v := constant.MakeFromLiteral(a.Value, a.Kind, 0)
548 n.rval = reflect.ValueOf(v)
549 }
550 st.push(n, nod)
551
552 case *ast.BinaryExpr:
553 kind := binaryExpr
554 act := aNop
555 switch a.Op {
556 case token.ADD:
557 act = aAdd
558 case token.AND:
559 act = aAnd
560 case token.AND_NOT:
561 act = aAndNot
562 case token.EQL:
563 act = aEqual
564 case token.GEQ:
565 act = aGreaterEqual
566 case token.GTR:
567 act = aGreater
568 case token.LAND:
569 kind = landExpr
570 act = aLand
571 case token.LOR:
572 kind = lorExpr
573 act = aLor
574 case token.LEQ:
575 act = aLowerEqual
576 case token.LSS:
577 act = aLower
578 case token.MUL:
579 act = aMul
580 case token.NEQ:
581 act = aNotEqual
582 case token.OR:
583 act = aOr
584 case token.REM:
585 act = aRem
586 case token.SUB:
587 act = aSub
588 case token.SHL:
589 act = aShl
590 case token.SHR:
591 act = aShr
592 case token.QUO:
593 act = aQuo
594 case token.XOR:
595 act = aXor
596 }
597 st.push(addChild(&root, anc, pos, kind, act), nod)
598
599 case *ast.BlockStmt:
600 b := addChild(&root, anc, pos, blockStmt, aNop)
601 st.push(b, nod)
602 var kind nkind
603 if anc.node != nil {
604 kind = anc.node.kind
605 }
606 switch kind {
607 case rangeStmt:
608 k := addChild(&root, astNode{b, nod}, pos, identExpr, aNop)
609 k.ident = "_"
610 v := addChild(&root, astNode{b, nod}, pos, identExpr, aNop)
611 v.ident = "_"
612 case forStmt7:
613 k := addChild(&root, astNode{b, nod}, pos, identExpr, aNop)
614 k.ident = "_"
615 }
616
617 case *ast.BranchStmt:
618 var kind nkind
619 switch a.Tok {
620 case token.BREAK:
621 kind = breakStmt
622 case token.CONTINUE:
623 kind = continueStmt
624 case token.FALLTHROUGH:
625 kind = fallthroughtStmt
626 case token.GOTO:
627 kind = gotoStmt
628 }
629 st.push(addChild(&root, anc, pos, kind, aNop), nod)
630
631 case *ast.CallExpr:
632 action := aCall
633 if a.Ellipsis != token.NoPos {
634 action = aCallSlice
635 }
636
637 st.push(addChild(&root, anc, pos, callExpr, action), nod)
638
639 case *ast.CaseClause:
640 st.push(addChild(&root, anc, pos, caseClause, aCase), nod)
641
642 case *ast.ChanType:
643 switch a.Dir {
644 case ast.SEND | ast.RECV:
645 st.push(addChild(&root, anc, pos, chanType, aNop), nod)
646 case ast.SEND:
647 st.push(addChild(&root, anc, pos, chanTypeSend, aNop), nod)
648 case ast.RECV:
649 st.push(addChild(&root, anc, pos, chanTypeRecv, aNop), nod)
650 }
651
652 case *ast.CommClause:
653 kind := commClause
654 if a.Comm == nil {
655 kind = commClauseDefault
656 }
657 st.push(addChild(&root, anc, pos, kind, aNop), nod)
658
659 case *ast.CommentGroup, *ast.EmptyStmt:
660 return false
661
662 case *ast.CompositeLit:
663 st.push(addChild(&root, anc, pos, compositeLitExpr, aCompositeLit), nod)
664
665 case *ast.DeclStmt:
666 st.push(addChild(&root, anc, pos, declStmt, aNop), nod)
667
668 case *ast.DeferStmt:
669 st.push(addChild(&root, anc, pos, deferStmt, aNop), nod)
670
671 case *ast.Ellipsis:
672 st.push(addChild(&root, anc, pos, ellipsisExpr, aNop), nod)
673
674 case *ast.ExprStmt:
675 st.push(addChild(&root, anc, pos, exprStmt, aNop), nod)
676
677 case *ast.Field:
678 st.push(addChild(&root, anc, pos, fieldExpr, aNop), nod)
679
680 case *ast.FieldList:
681 st.push(addChild(&root, anc, pos, fieldList, aNop), nod)
682
683 case *ast.File:
684 pkgName = a.Name.Name
685 st.push(addChild(&root, anc, pos, fileStmt, aNop), nod)
686
687 case *ast.ForStmt:
688 // Disambiguate variants of FOR statements with a node kind per variant
689 var kind nkind
690 switch {
691 case a.Cond == nil && a.Init == nil && a.Post == nil:
692 kind = forStmt0
693 case a.Cond == nil && a.Init != nil && a.Post == nil:
694 kind = forStmt1
695 case a.Cond != nil && a.Init == nil && a.Post == nil:
696 kind = forStmt2
697 case a.Cond != nil && a.Init != nil && a.Post == nil:
698 kind = forStmt3
699 case a.Cond == nil && a.Init == nil && a.Post != nil:
700 kind = forStmt4
701 case a.Cond != nil && a.Init == nil && a.Post != nil:
702 kind = forStmt5
703 case a.Cond == nil && a.Init != nil && a.Post != nil:
704 kind = forStmt6
705 case a.Cond != nil && a.Init != nil && a.Post != nil:
706 kind = forStmt7
707 }
708 st.push(addChild(&root, anc, pos, kind, aNop), nod)
709
710 case *ast.FuncDecl:
711 n := addChild(&root, anc, pos, funcDecl, aNop)
712 n.val = n
713 if a.Recv == nil {
714 // Function is not a method, create an empty receiver list.
715 addChild(&root, astNode{n, nod}, pos, fieldList, aNop)
716 }
717 st.push(n, nod)
718
719 case *ast.FuncLit:
720 n := addChild(&root, anc, pos, funcLit, aGetFunc)
721 addChild(&root, astNode{n, nod}, pos, fieldList, aNop)
722 addChild(&root, astNode{n, nod}, pos, undefNode, aNop)
723 st.push(n, nod)
724
725 case *ast.FuncType:
726 n := addChild(&root, anc, pos, funcType, aNop)
727 n.val = n
728 if a.TypeParams == nil {
729 // Function has no type parameters, create an empty fied list.
730 addChild(&root, astNode{n, nod}, pos, fieldList, aNop)
731 }
732 st.push(n, nod)
733
734 case *ast.GenDecl:
735 var kind nkind
736 switch a.Tok {
737 case token.CONST:
738 kind = constDecl
739 case token.IMPORT:
740 kind = importDecl
741 case token.TYPE:
742 kind = typeDecl
743 case token.VAR:
744 kind = varDecl
745 }
746 st.push(addChild(&root, anc, pos, kind, aNop), nod)
747
748 case *ast.GoStmt:
749 st.push(addChild(&root, anc, pos, goStmt, aNop), nod)
750
751 case *ast.Ident:
752 n := addChild(&root, anc, pos, identExpr, aNop)
753 n.ident = a.Name
754 st.push(n, nod)
755 if n.anc.kind == defineStmt && n.anc.anc.kind == constDecl && n.anc.nright == 0 {
756 // Implicit assign expression (in a ConstDecl block).
757 // Clone assign source and type from previous
758 a := n.anc
759 pa := a.anc.child[childPos(a)-1]
760
761 if len(pa.child) > pa.nleft+pa.nright {
762 // duplicate previous type spec
763 a.child = append(a.child, interp.dup(pa.child[a.nleft], a))
764 }
765
766 // duplicate previous assign right hand side
767 a.child = append(a.child, interp.dup(pa.lastChild(), a))
768 a.nright++
769 }
770
771 case *ast.IfStmt:
772 // Disambiguate variants of IF statements with a node kind per variant
773 var kind nkind
774 switch {
775 case a.Init == nil && a.Else == nil:
776 kind = ifStmt0
777 case a.Init == nil && a.Else != nil:
778 kind = ifStmt1
779 case a.Else == nil:
780 kind = ifStmt2
781 default:
782 kind = ifStmt3
783 }
784 st.push(addChild(&root, anc, pos, kind, aNop), nod)
785
786 case *ast.ImportSpec:
787 st.push(addChild(&root, anc, pos, importSpec, aNop), nod)
788
789 case *ast.IncDecStmt:
790 var act action
791 switch a.Tok {
792 case token.INC:
793 act = aInc
794 case token.DEC:
795 act = aDec
796 }
797 st.push(addChild(&root, anc, pos, incDecStmt, act), nod)
798
799 case *ast.IndexExpr:
800 st.push(addChild(&root, anc, pos, indexExpr, aGetIndex), nod)
801
802 case *ast.IndexListExpr:
803 st.push(addChild(&root, anc, pos, indexListExpr, aNop), nod)
804
805 case *ast.InterfaceType:
806 st.push(addChild(&root, anc, pos, interfaceType, aNop), nod)
807
808 case *ast.KeyValueExpr:
809 st.push(addChild(&root, anc, pos, keyValueExpr, aNop), nod)
810
811 case *ast.LabeledStmt:
812 st.push(addChild(&root, anc, pos, labeledStmt, aNop), nod)
813
814 case *ast.MapType:
815 st.push(addChild(&root, anc, pos, mapType, aNop), nod)
816
817 case *ast.ParenExpr:
818 st.push(addChild(&root, anc, pos, parenExpr, aNop), nod)
819
820 case *ast.RangeStmt:
821 // Insert a missing ForRangeStmt for AST correctness
822 n := addChild(&root, anc, pos, forRangeStmt, aNop)
823 r := addChild(&root, astNode{n, nod}, pos, rangeStmt, aRange)
824 st.push(r, nod)
825 if a.Key == nil {
826 // range not in an assign expression: insert a "_" key variable to store iteration index
827 k := addChild(&root, astNode{r, nod}, pos, identExpr, aNop)
828 k.ident = "_"
829 }
830
831 case *ast.ReturnStmt:
832 st.push(addChild(&root, anc, pos, returnStmt, aReturn), nod)
833
834 case *ast.SelectStmt:
835 st.push(addChild(&root, anc, pos, selectStmt, aNop), nod)
836
837 case *ast.SelectorExpr:
838 st.push(addChild(&root, anc, pos, selectorExpr, aGetIndex), nod)
839
840 case *ast.SendStmt:
841 st.push(addChild(&root, anc, pos, sendStmt, aSend), nod)
842
843 case *ast.SliceExpr:
844 if a.Low == nil {
845 st.push(addChild(&root, anc, pos, sliceExpr, aSlice0), nod)
846 } else {
847 st.push(addChild(&root, anc, pos, sliceExpr, aSlice), nod)
848 }
849
850 case *ast.StarExpr:
851 st.push(addChild(&root, anc, pos, starExpr, aStar), nod)
852
853 case *ast.StructType:
854 st.push(addChild(&root, anc, pos, structType, aNop), nod)
855
856 case *ast.SwitchStmt:
857 if a.Tag == nil {
858 st.push(addChild(&root, anc, pos, switchIfStmt, aNop), nod)
859 } else {
860 st.push(addChild(&root, anc, pos, switchStmt, aNop), nod)
861 }
862
863 case *ast.TypeAssertExpr:
864 st.push(addChild(&root, anc, pos, typeAssertExpr, aTypeAssert), nod)
865
866 case *ast.TypeSpec:
867 if a.Assign.IsValid() {
868 st.push(addChild(&root, anc, pos, typeSpecAssign, aNop), nod)
869 break
870 }
871 st.push(addChild(&root, anc, pos, typeSpec, aNop), nod)
872
873 case *ast.TypeSwitchStmt:
874 n := addChild(&root, anc, pos, typeSwitch, aNop)
875 st.push(n, nod)
876 if a.Init == nil {
877 // add an empty init node to disambiguate AST
878 addChild(&root, astNode{n, nil}, pos, fieldList, aNop)
879 }
880
881 case *ast.UnaryExpr:
882 kind := unaryExpr
883 var act action
884 switch a.Op {
885 case token.ADD:
886 act = aPos
887 case token.AND:
888 kind = addressExpr
889 act = aAddr
890 case token.ARROW:
891 act = aRecv
892 case token.NOT:
893 act = aNot
894 case token.SUB:
895 act = aNeg
896 case token.XOR:
897 act = aBitNot
898 }
899 st.push(addChild(&root, anc, pos, kind, act), nod)
900
901 case *ast.ValueSpec:
902 kind := valueSpec
903 act := aNop
904 switch {
905 case a.Values != nil:
906 if len(a.Names) > 1 && len(a.Values) == 1 {
907 if anc.node.kind == constDecl || anc.node.kind == varDecl {
908 kind = defineXStmt
909 } else {
910 kind = assignXStmt
911 }
912 act = aAssignX
913 } else {
914 if anc.node.kind == constDecl || anc.node.kind == varDecl {
915 kind = defineStmt
916 } else {
917 kind = assignStmt
918 }
919 act = aAssign
920 }
921 case anc.node.kind == constDecl:
922 kind, act = defineStmt, aAssign
923 case anc.node.kind == varDecl && anc.node.anc.kind != fileStmt:
924 kind, act = defineStmt, aAssign
925 }
926 n := addChild(&root, anc, pos, kind, act)
927 n.nleft = len(a.Names)
928 n.nright = len(a.Values)
929 st.push(n, nod)
930
931 default:
932 err = astError(fmt.Errorf("ast: %T not implemented, line %s", a, interp.fset.Position(pos)))
933 return false
934 }
935 return true
936 })
937
938 interp.roots = append(interp.roots, root)
939 return pkgName, root, err
940 }
941
942 type astNode struct {
943 node *node
944 ast ast.Node
945 }
946
947 type nodestack []astNode
948
949 func (s *nodestack) push(n *node, a ast.Node) {
950 *s = append(*s, astNode{n, a})
951 }
952
953 func (s *nodestack) pop() astNode {
954 l := len(*s) - 1
955 res := (*s)[l]
956 *s = (*s)[:l]
957 return res
958 }
959
960 func (s *nodestack) top() astNode {
961 l := len(*s)
962 if l > 0 {
963 return (*s)[l-1]
964 }
965 return astNode{}
966 }
967
968 // dup returns a duplicated node subtree.
969 func (interp *Interpreter) dup(nod, anc *node) *node {
970 nindex := atomic.AddInt64(&interp.nindex, 1)
971 n := *nod
972 n.index = nindex
973 n.anc = anc
974 n.start = &n
975 n.pos = anc.pos
976 n.child = nil
977 for _, c := range nod.child {
978 n.child = append(n.child, interp.dup(c, &n))
979 }
980 return &n
981 }