github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/ast/walk.go (about)

     1  // Copyright 2017 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package ast
     5  
     6  // Walk calls callback cb for every top-level node in description.
     7  // Note: it's not recursive. Use Recursive/PostRecursive helpers for recursive walk.
     8  func (desc *Description) Walk(cb func(Node)) {
     9  	for _, n := range desc.Nodes {
    10  		cb(n)
    11  	}
    12  }
    13  
    14  func Recursive(cb func(Node) bool) func(Node) {
    15  	var rec func(Node)
    16  	rec = func(n Node) {
    17  		if cb(n) {
    18  			n.walk(rec)
    19  		}
    20  	}
    21  	return rec
    22  }
    23  
    24  func PostRecursive(cb func(Node)) func(Node) {
    25  	var rec func(Node)
    26  	rec = func(n Node) {
    27  		n.walk(rec)
    28  		cb(n)
    29  	}
    30  	return rec
    31  }
    32  
    33  func (n *NewLine) walk(cb func(Node)) {}
    34  func (n *Comment) walk(cb func(Node)) {}
    35  func (n *Ident) walk(cb func(Node))   {}
    36  func (n *String) walk(cb func(Node))  {}
    37  func (n *Int) walk(cb func(Node))     {}
    38  
    39  func (n *Meta) walk(cb func(Node)) {
    40  	cb(n.Value)
    41  }
    42  
    43  func (n *Include) walk(cb func(Node)) {
    44  	cb(n.File)
    45  }
    46  
    47  func (n *Incdir) walk(cb func(Node)) {
    48  	cb(n.Dir)
    49  }
    50  
    51  func (n *Define) walk(cb func(Node)) {
    52  	cb(n.Name)
    53  	cb(n.Value)
    54  }
    55  
    56  func (n *Resource) walk(cb func(Node)) {
    57  	cb(n.Name)
    58  	cb(n.Base)
    59  	for _, v := range n.Values {
    60  		cb(v)
    61  	}
    62  }
    63  
    64  func (n *TypeDef) walk(cb func(Node)) {
    65  	cb(n.Name)
    66  	for _, a := range n.Args {
    67  		cb(a)
    68  	}
    69  	if n.Type != nil {
    70  		cb(n.Type)
    71  	}
    72  	if n.Struct != nil {
    73  		cb(n.Struct)
    74  	}
    75  }
    76  
    77  func (n *Call) walk(cb func(Node)) {
    78  	cb(n.Name)
    79  	for _, f := range n.Args {
    80  		cb(f)
    81  	}
    82  	if n.Ret != nil {
    83  		cb(n.Ret)
    84  	}
    85  	for _, a := range n.Attrs {
    86  		cb(a)
    87  	}
    88  }
    89  
    90  func (n *Struct) walk(cb func(Node)) {
    91  	cb(n.Name)
    92  	for _, f := range n.Fields {
    93  		cb(f)
    94  	}
    95  	for _, a := range n.Attrs {
    96  		cb(a)
    97  	}
    98  	for _, c := range n.Comments {
    99  		cb(c)
   100  	}
   101  }
   102  
   103  func (n *IntFlags) walk(cb func(Node)) {
   104  	cb(n.Name)
   105  	for _, v := range n.Values {
   106  		cb(v)
   107  	}
   108  }
   109  
   110  func (n *StrFlags) walk(cb func(Node)) {
   111  	cb(n.Name)
   112  	for _, v := range n.Values {
   113  		cb(v)
   114  	}
   115  }
   116  
   117  func (n *Type) walk(cb func(Node)) {
   118  	for _, t := range n.Args {
   119  		cb(t)
   120  	}
   121  	if n.Expression != nil {
   122  		cb(n.Expression)
   123  	}
   124  }
   125  
   126  func (n *Field) walk(cb func(Node)) {
   127  	cb(n.Name)
   128  	cb(n.Type)
   129  	for _, a := range n.Attrs {
   130  		cb(a)
   131  	}
   132  	for _, c := range n.Comments {
   133  		cb(c)
   134  	}
   135  }
   136  
   137  func (n *BinaryExpression) walk(cb func(Node)) {
   138  	cb(n.Left)
   139  	cb(n.Right)
   140  }