github.com/bir3/gocompiler@v0.3.205/src/cmd/compile/internal/syntax/nodes.go (about) 1 // Copyright 2016 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 syntax 6 7 // ---------------------------------------------------------------------------- 8 // Nodes 9 10 type Node interface { 11 // Pos() returns the position associated with the node as follows: 12 // 1) The position of a node representing a terminal syntax production 13 // (Name, BasicLit, etc.) is the position of the respective production 14 // in the source. 15 // 2) The position of a node representing a non-terminal production 16 // (IndexExpr, IfStmt, etc.) is the position of a token uniquely 17 // associated with that production; usually the left-most one 18 // ('[' for IndexExpr, 'if' for IfStmt, etc.) 19 Pos() Pos 20 aNode() 21 } 22 23 type node struct { 24 // commented out for now since not yet used 25 // doc *Comment // nil means no comment(s) attached 26 pos Pos 27 } 28 29 func (n *node) Pos() Pos { return n.pos } 30 func (*node) aNode() {} 31 32 // ---------------------------------------------------------------------------- 33 // Files 34 35 // package PkgName; DeclList[0], DeclList[1], ... 36 type File struct { 37 Pragma Pragma 38 PkgName *Name 39 DeclList []Decl 40 EOF Pos 41 node 42 } 43 44 // ---------------------------------------------------------------------------- 45 // Declarations 46 47 type ( 48 Decl interface { 49 Node 50 aDecl() 51 } 52 53 // Path 54 // LocalPkgName Path 55 ImportDecl struct { 56 Group *Group // nil means not part of a group 57 Pragma Pragma 58 LocalPkgName *Name // including "."; nil means no rename present 59 Path *BasicLit // Path.Bad || Path.Kind == StringLit; nil means no path 60 decl 61 } 62 63 // NameList 64 // NameList = Values 65 // NameList Type = Values 66 ConstDecl struct { 67 Group *Group // nil means not part of a group 68 Pragma Pragma 69 NameList []*Name 70 Type Expr // nil means no type 71 Values Expr // nil means no values 72 decl 73 } 74 75 // Name Type 76 TypeDecl struct { 77 Group *Group // nil means not part of a group 78 Pragma Pragma 79 Name *Name 80 TParamList []*Field // nil means no type parameters 81 Alias bool 82 Type Expr 83 decl 84 } 85 86 // NameList Type 87 // NameList Type = Values 88 // NameList = Values 89 VarDecl struct { 90 Group *Group // nil means not part of a group 91 Pragma Pragma 92 NameList []*Name 93 Type Expr // nil means no type 94 Values Expr // nil means no values 95 decl 96 } 97 98 // func Name Type { Body } 99 // func Name Type 100 // func Receiver Name Type { Body } 101 // func Receiver Name Type 102 FuncDecl struct { 103 Pragma Pragma 104 Recv *Field // nil means regular function 105 Name *Name 106 TParamList []*Field // nil means no type parameters 107 Type *FuncType 108 Body *BlockStmt // nil means no body (forward declaration) 109 decl 110 } 111 ) 112 113 type decl struct{ node } 114 115 func (*decl) aDecl() {} 116 117 // All declarations belonging to the same group point to the same Group node. 118 type Group struct { 119 _ int // not empty so we are guaranteed different Group instances 120 } 121 122 // ---------------------------------------------------------------------------- 123 // Expressions 124 125 func NewName(pos Pos, value string) *Name { 126 n := new(Name) 127 n.pos = pos 128 n.Value = value 129 return n 130 } 131 132 type ( 133 Expr interface { 134 Node 135 typeInfo 136 aExpr() 137 } 138 139 // Placeholder for an expression that failed to parse 140 // correctly and where we can't provide a better node. 141 BadExpr struct { 142 expr 143 } 144 145 // Value 146 Name struct { 147 Value string 148 expr 149 } 150 151 // Value 152 BasicLit struct { 153 Value string 154 Kind LitKind 155 Bad bool // true means the literal Value has syntax errors 156 expr 157 } 158 159 // Type { ElemList[0], ElemList[1], ... } 160 CompositeLit struct { 161 Type Expr // nil means no literal type 162 ElemList []Expr 163 NKeys int // number of elements with keys 164 Rbrace Pos 165 expr 166 } 167 168 // Key: Value 169 KeyValueExpr struct { 170 Key, Value Expr 171 expr 172 } 173 174 // func Type { Body } 175 FuncLit struct { 176 Type *FuncType 177 Body *BlockStmt 178 expr 179 } 180 181 // (X) 182 ParenExpr struct { 183 X Expr 184 expr 185 } 186 187 // X.Sel 188 SelectorExpr struct { 189 X Expr 190 Sel *Name 191 expr 192 } 193 194 // X[Index] 195 // X[T1, T2, ...] (with Ti = Index.(*ListExpr).ElemList[i]) 196 IndexExpr struct { 197 X Expr 198 Index Expr 199 expr 200 } 201 202 // X[Index[0] : Index[1] : Index[2]] 203 SliceExpr struct { 204 X Expr 205 Index [3]Expr 206 // Full indicates whether this is a simple or full slice expression. 207 // In a valid AST, this is equivalent to Index[2] != nil. 208 // TODO(mdempsky): This is only needed to report the "3-index 209 // slice of string" error when Index[2] is missing. 210 Full bool 211 expr 212 } 213 214 // X.(Type) 215 AssertExpr struct { 216 X Expr 217 Type Expr 218 expr 219 } 220 221 // X.(type) 222 // Lhs := X.(type) 223 TypeSwitchGuard struct { 224 Lhs *Name // nil means no Lhs := 225 X Expr // X.(type) 226 expr 227 } 228 229 Operation struct { 230 Op Operator 231 X, Y Expr // Y == nil means unary expression 232 expr 233 } 234 235 // Fun(ArgList[0], ArgList[1], ...) 236 CallExpr struct { 237 Fun Expr 238 ArgList []Expr // nil means no arguments 239 HasDots bool // last argument is followed by ... 240 expr 241 } 242 243 // ElemList[0], ElemList[1], ... 244 ListExpr struct { 245 ElemList []Expr 246 expr 247 } 248 249 // [Len]Elem 250 ArrayType struct { 251 // TODO(gri) consider using Name{"..."} instead of nil (permits attaching of comments) 252 Len Expr // nil means Len is ... 253 Elem Expr 254 expr 255 } 256 257 // []Elem 258 SliceType struct { 259 Elem Expr 260 expr 261 } 262 263 // ...Elem 264 DotsType struct { 265 Elem Expr 266 expr 267 } 268 269 // struct { FieldList[0] TagList[0]; FieldList[1] TagList[1]; ... } 270 StructType struct { 271 FieldList []*Field 272 TagList []*BasicLit // i >= len(TagList) || TagList[i] == nil means no tag for field i 273 expr 274 } 275 276 // Name Type 277 // Type 278 Field struct { 279 Name *Name // nil means anonymous field/parameter (structs/parameters), or embedded element (interfaces) 280 Type Expr // field names declared in a list share the same Type (identical pointers) 281 node 282 } 283 284 // interface { MethodList[0]; MethodList[1]; ... } 285 InterfaceType struct { 286 MethodList []*Field 287 expr 288 } 289 290 FuncType struct { 291 ParamList []*Field 292 ResultList []*Field 293 expr 294 } 295 296 // map[Key]Value 297 MapType struct { 298 Key, Value Expr 299 expr 300 } 301 302 // chan Elem 303 // <-chan Elem 304 // chan<- Elem 305 ChanType struct { 306 Dir ChanDir // 0 means no direction 307 Elem Expr 308 expr 309 } 310 ) 311 312 type expr struct { 313 node 314 typeAndValue // After typechecking, contains the results of typechecking this expression. 315 } 316 317 func (*expr) aExpr() {} 318 319 type ChanDir uint 320 321 const ( 322 _ ChanDir = iota 323 SendOnly 324 RecvOnly 325 ) 326 327 // ---------------------------------------------------------------------------- 328 // Statements 329 330 type ( 331 Stmt interface { 332 Node 333 aStmt() 334 } 335 336 SimpleStmt interface { 337 Stmt 338 aSimpleStmt() 339 } 340 341 EmptyStmt struct { 342 simpleStmt 343 } 344 345 LabeledStmt struct { 346 Label *Name 347 Stmt Stmt 348 stmt 349 } 350 351 BlockStmt struct { 352 List []Stmt 353 Rbrace Pos 354 stmt 355 } 356 357 ExprStmt struct { 358 X Expr 359 simpleStmt 360 } 361 362 SendStmt struct { 363 Chan, Value Expr // Chan <- Value 364 simpleStmt 365 } 366 367 DeclStmt struct { 368 DeclList []Decl 369 stmt 370 } 371 372 AssignStmt struct { 373 Op Operator // 0 means no operation 374 Lhs, Rhs Expr // Rhs == nil means Lhs++ (Op == Add) or Lhs-- (Op == Sub) 375 simpleStmt 376 } 377 378 BranchStmt struct { 379 Tok token // Break, Continue, Fallthrough, or Goto 380 Label *Name 381 // Target is the continuation of the control flow after executing 382 // the branch; it is computed by the parser if CheckBranches is set. 383 // Target is a *LabeledStmt for gotos, and a *SwitchStmt, *SelectStmt, 384 // or *ForStmt for breaks and continues, depending on the context of 385 // the branch. Target is not set for fallthroughs. 386 Target Stmt 387 stmt 388 } 389 390 CallStmt struct { 391 Tok token // Go or Defer 392 Call Expr 393 stmt 394 } 395 396 ReturnStmt struct { 397 Results Expr // nil means no explicit return values 398 stmt 399 } 400 401 IfStmt struct { 402 Init SimpleStmt 403 Cond Expr 404 Then *BlockStmt 405 Else Stmt // either nil, *IfStmt, or *BlockStmt 406 stmt 407 } 408 409 ForStmt struct { 410 Init SimpleStmt // incl. *RangeClause 411 Cond Expr 412 Post SimpleStmt 413 Body *BlockStmt 414 stmt 415 } 416 417 SwitchStmt struct { 418 Init SimpleStmt 419 Tag Expr // incl. *TypeSwitchGuard 420 Body []*CaseClause 421 Rbrace Pos 422 stmt 423 } 424 425 SelectStmt struct { 426 Body []*CommClause 427 Rbrace Pos 428 stmt 429 } 430 ) 431 432 type ( 433 RangeClause struct { 434 Lhs Expr // nil means no Lhs = or Lhs := 435 Def bool // means := 436 X Expr // range X 437 simpleStmt 438 } 439 440 CaseClause struct { 441 Cases Expr // nil means default clause 442 Body []Stmt 443 Colon Pos 444 node 445 } 446 447 CommClause struct { 448 Comm SimpleStmt // send or receive stmt; nil means default clause 449 Body []Stmt 450 Colon Pos 451 node 452 } 453 ) 454 455 type stmt struct{ node } 456 457 func (stmt) aStmt() {} 458 459 type simpleStmt struct { 460 stmt 461 } 462 463 func (simpleStmt) aSimpleStmt() {} 464 465 // ---------------------------------------------------------------------------- 466 // Comments 467 468 // TODO(gri) Consider renaming to CommentPos, CommentPlacement, etc. 469 // Kind = Above doesn't make much sense. 470 type CommentKind uint 471 472 const ( 473 Above CommentKind = iota 474 Below 475 Left 476 Right 477 ) 478 479 type Comment struct { 480 Kind CommentKind 481 Text string 482 Next *Comment 483 }