github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/ast/clone.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  func (desc *Description) Clone() *Description {
     7  	desc1 := &Description{}
     8  	for _, n := range desc.Nodes {
     9  		desc1.Nodes = append(desc1.Nodes, n.Clone())
    10  	}
    11  	return desc1
    12  }
    13  
    14  func (n *NewLine) Clone() Node {
    15  	return &NewLine{
    16  		Pos: n.Pos,
    17  	}
    18  }
    19  
    20  func (n *Comment) Clone() Node {
    21  	return &Comment{
    22  		Pos:  n.Pos,
    23  		Text: n.Text,
    24  	}
    25  }
    26  
    27  func (n *Meta) Clone() Node {
    28  	return &Meta{
    29  		Pos:   n.Pos,
    30  		Value: n.Value.Clone().(*Type),
    31  	}
    32  }
    33  
    34  func (n *Include) Clone() Node {
    35  	return &Include{
    36  		Pos:  n.Pos,
    37  		File: n.File.Clone().(*String),
    38  	}
    39  }
    40  
    41  func (n *Incdir) Clone() Node {
    42  	return &Incdir{
    43  		Pos: n.Pos,
    44  		Dir: n.Dir.Clone().(*String),
    45  	}
    46  }
    47  
    48  func (n *Define) Clone() Node {
    49  	return &Define{
    50  		Pos:   n.Pos,
    51  		Name:  n.Name.Clone().(*Ident),
    52  		Value: n.Value.Clone().(*Int),
    53  	}
    54  }
    55  
    56  func (n *Resource) Clone() Node {
    57  	return &Resource{
    58  		Pos:    n.Pos,
    59  		Name:   n.Name.Clone().(*Ident),
    60  		Base:   n.Base.Clone().(*Type),
    61  		Values: cloneInts(n.Values),
    62  	}
    63  }
    64  
    65  func (n *TypeDef) Clone() Node {
    66  	var args []*Ident
    67  	for _, v := range n.Args {
    68  		args = append(args, v.Clone().(*Ident))
    69  	}
    70  	var typ *Type
    71  	if n.Type != nil {
    72  		typ = n.Type.Clone().(*Type)
    73  	}
    74  	var str *Struct
    75  	if n.Struct != nil {
    76  		str = n.Struct.Clone().(*Struct)
    77  	}
    78  	return &TypeDef{
    79  		Pos:    n.Pos,
    80  		Name:   n.Name.Clone().(*Ident),
    81  		Args:   args,
    82  		Type:   typ,
    83  		Struct: str,
    84  	}
    85  }
    86  
    87  func (n *Call) Clone() Node {
    88  	var ret *Type
    89  	if n.Ret != nil {
    90  		ret = n.Ret.Clone().(*Type)
    91  	}
    92  	return &Call{
    93  		Pos:      n.Pos,
    94  		Name:     n.Name.Clone().(*Ident),
    95  		CallName: n.CallName,
    96  		NR:       n.NR,
    97  		Args:     cloneFields(n.Args),
    98  		Ret:      ret,
    99  		Attrs:    cloneTypes(n.Attrs),
   100  	}
   101  }
   102  
   103  func (n *Struct) Clone() Node {
   104  	return &Struct{
   105  		Pos:      n.Pos,
   106  		Name:     n.Name.Clone().(*Ident),
   107  		Fields:   cloneFields(n.Fields),
   108  		Attrs:    cloneTypes(n.Attrs),
   109  		Comments: cloneComments(n.Comments),
   110  		IsUnion:  n.IsUnion,
   111  	}
   112  }
   113  
   114  func (n *IntFlags) Clone() Node {
   115  	return &IntFlags{
   116  		Pos:    n.Pos,
   117  		Name:   n.Name.Clone().(*Ident),
   118  		Values: cloneInts(n.Values),
   119  	}
   120  }
   121  
   122  func (n *StrFlags) Clone() Node {
   123  	var values []*String
   124  	for _, v := range n.Values {
   125  		values = append(values, v.Clone().(*String))
   126  	}
   127  	return &StrFlags{
   128  		Pos:    n.Pos,
   129  		Name:   n.Name.Clone().(*Ident),
   130  		Values: values,
   131  	}
   132  }
   133  
   134  func (n *Ident) Clone() Node {
   135  	return &Ident{
   136  		Pos:  n.Pos,
   137  		Name: n.Name,
   138  	}
   139  }
   140  
   141  func (n *String) Clone() Node {
   142  	return &String{
   143  		Pos:   n.Pos,
   144  		Value: n.Value,
   145  		Fmt:   n.Fmt,
   146  	}
   147  }
   148  
   149  func (n *Int) Clone() Node {
   150  	return &Int{
   151  		Pos:      n.Pos,
   152  		Value:    n.Value,
   153  		ValueFmt: n.ValueFmt,
   154  		Ident:    n.Ident,
   155  		CExpr:    n.CExpr,
   156  	}
   157  }
   158  
   159  func (n *Type) Clone() Node {
   160  	ret := &Type{
   161  		Pos:       n.Pos,
   162  		Value:     n.Value,
   163  		ValueFmt:  n.ValueFmt,
   164  		Ident:     n.Ident,
   165  		String:    n.String,
   166  		StringFmt: n.StringFmt,
   167  		HasString: n.HasString,
   168  		Colon:     cloneTypes(n.Colon),
   169  		Args:      cloneTypes(n.Args),
   170  	}
   171  	if n.Expression != nil {
   172  		ret.Expression = n.Expression.Clone().(*BinaryExpression)
   173  	}
   174  	return ret
   175  }
   176  
   177  func (n *Field) Clone() Node {
   178  	return &Field{
   179  		Pos:      n.Pos,
   180  		Name:     n.Name.Clone().(*Ident),
   181  		Type:     n.Type.Clone().(*Type),
   182  		Attrs:    cloneTypes(n.Attrs),
   183  		NewBlock: n.NewBlock,
   184  		Comments: cloneComments(n.Comments),
   185  	}
   186  }
   187  
   188  func (n *BinaryExpression) Clone() Node {
   189  	return &BinaryExpression{
   190  		Pos:      n.Pos,
   191  		Operator: n.Operator,
   192  		Left:     n.Left.Clone().(*Type),
   193  		Right:    n.Right.Clone().(*Type),
   194  	}
   195  }
   196  
   197  func cloneFields(list []*Field) (res []*Field) {
   198  	for _, n := range list {
   199  		res = append(res, n.Clone().(*Field))
   200  	}
   201  	return
   202  }
   203  
   204  func cloneInts(list []*Int) (res []*Int) {
   205  	for _, n := range list {
   206  		res = append(res, n.Clone().(*Int))
   207  	}
   208  	return
   209  }
   210  
   211  func cloneTypes(list []*Type) (res []*Type) {
   212  	for _, n := range list {
   213  		res = append(res, n.Clone().(*Type))
   214  	}
   215  	return
   216  }
   217  
   218  func cloneComments(list []*Comment) (res []*Comment) {
   219  	for _, n := range list {
   220  		res = append(res, n.Clone().(*Comment))
   221  	}
   222  	return
   223  }