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