| 1 |
// Copyright 2009 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 ast declares the types used to represent syntax trees for Go |
| 6 |
// packages. |
| 7 |
// |
| 8 |
// Syntax trees may be constructed directly, but they are typically |
| 9 |
// produced from Go source code by the parser; see the ParseFile |
| 10 |
// function in package [go/parser]. |
| 11 |
package ast |
| 12 |
|
| 13 |
import ( |
| 14 |
"go/token" |
| 15 |
"strings" |
| 16 |
) |
| 17 |
|
| 18 |
// ---------------------------------------------------------------------------- |
| 19 |
// Interfaces |
| 20 |
// |
| 21 |
// There are 3 main classes of nodes: Expressions and type nodes, |
| 22 |
// statement nodes, and declaration nodes. The node names usually |
| 23 |
// match the corresponding Go spec production names to which they |
| 24 |
// correspond. The node fields correspond to the individual parts |
| 25 |
// of the respective productions. |
| 26 |
// |
| 27 |
// All nodes contain position information marking the beginning of |
| 28 |
// the corresponding source text segment; it is accessible via the |
| 29 |
// Pos accessor method. Nodes may contain additional position info |
| 30 |
// for language constructs where comments may be found between parts |
| 31 |
// of the construct (typically any larger, parenthesized subpart). |
| 32 |
// That position information is needed to properly position comments |
| 33 |
// when printing the construct. |
| 34 |
|
| 35 |
// All node types implement the Node interface. |
| 36 |
type Node interface { |
| 37 |
Pos() token.Pos // position of first character belonging to the node |
| 38 |
End() token.Pos // position of first character immediately after the node |
| 39 |
} |
| 40 |
|
| 41 |
// All expression nodes implement the Expr interface. |
| 42 |
type Expr interface { |
| 43 |
Node |
| 44 |
exprNode() |
| 45 |
} |
| 46 |
|
| 47 |
// All statement nodes implement the Stmt interface. |
| 48 |
type Stmt interface { |
| 49 |
Node |
| 50 |
stmtNode() |
| 51 |
} |
| 52 |
|
| 53 |
// All declaration nodes implement the Decl interface. |
| 54 |
type Decl interface { |
| 55 |
Node |
| 56 |
declNode() |
| 57 |
} |
| 58 |
|
| 59 |
// ---------------------------------------------------------------------------- |
| 60 |
// Comments |
| 61 |
|
| 62 |
// A Comment node represents a single //-style or /*-style comment. |
| 63 |
// |
| 64 |
// The Text field contains the comment text without carriage returns (\r) that |
| 65 |
// may have been present in the source. Because a comment's end position is |
| 66 |
// computed using len(Text), the position reported by [Comment.End] does not match the |
| 67 |
// true source end position for comments containing carriage returns. |
| 68 |
type Comment struct { |
| 69 |
Slash token.Pos // position of "/" starting the comment |
| 70 |
Text string // comment text (excluding '\n' for //-style comments) |
| 71 |
} |
| 72 |
|
| 73 |
func (c *Comment) Pos() token.Pos { return c.Slash } |
| 74 |
func (c *Comment) End() token.Pos { return token.Pos(int(c.Slash) + len(c.Text)) } |
| 75 |
|
| 76 |
// A CommentGroup represents a sequence of comments |
| 77 |
// with no other tokens and no empty lines between. |
| 78 |
type CommentGroup struct { |
| 79 |
List []*Comment // len(List) > 0 |
| 80 |
} |
| 81 |
|
| 82 |
func (g *CommentGroup) Pos() token.Pos { return g.List[0].Pos() } |
| 83 |
func (g *CommentGroup) End() token.Pos { return g.List[len(g.List)-1].End() } |
| 84 |
|
| 85 |
func isWhitespace(ch byte) bool { return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' } |
| 86 |
|
| 87 |
func stripTrailingWhitespace(s string) string { |
| 88 |
i := len(s) |
| 89 |
for i > 0 && isWhitespace(s[i-1]) { |
| 90 |
i-- |
| 91 |
} |
| 92 |
return s[0:i] |
| 93 |
} |
| 94 |
|
| 95 |
// Text returns the text of the comment. |
| 96 |
// Comment markers (//, /*, and */), the first space of a line comment, and |
| 97 |
// leading and trailing empty lines are removed. |
| 98 |
// Comment directives like "//line" and "//go:noinline" are also removed. |
| 99 |
// Multiple empty lines are reduced to one, and trailing space on lines is trimmed. |
| 100 |
// Unless the result is empty, it is newline-terminated. |
| 101 |
func (g *CommentGroup) Text() string { |
| 102 |
if g == nil { |
| 103 |
return "" |
| 104 |
} |
| 105 |
comments := make([]string, len(g.List)) |
| 106 |
for i, c := range g.List { |
| 107 |
comments[i] = c.Text |
| 108 |
} |
| 109 |
|
| 110 |
lines := make([]string, 0, 10) // most comments are less than 10 lines |
| 111 |
for _, c := range comments { |
| 112 |
// Remove comment markers. |
| 113 |
// The parser has given us exactly the comment text. |
| 114 |
switch c[1] { |
| 115 |
case '/': |
| 116 |
//-style comment (no newline at the end) |
| 117 |
c = c[2:] |
| 118 |
if len(c) == 0 { |
| 119 |
// empty line |
| 120 |
break |
| 121 |
} |
| 122 |
if c[0] == ' ' { |
| 123 |
// strip first space - required for Example tests |
| 124 |
c = c[1:] |
| 125 |
break |
| 126 |
} |
| 127 |
if isDirective(c) { |
| 128 |
// Ignore //go:noinline, //line, and so on. |
| 129 |
continue |
| 130 |
} |
| 131 |
case '*': |
| 132 |
/*-style comment */ |
| 133 |
c = c[2 : len(c)-2] |
| 134 |
} |
| 135 |
|
| 136 |
// Split on newlines. |
| 137 |
cl := strings.Split(c, "\n") |
| 138 |
|
| 139 |
// Walk lines, stripping trailing white space and adding to list. |
| 140 |
for _, l := range cl { |
| 141 |
lines = append(lines, stripTrailingWhitespace(l)) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Remove leading blank lines; convert runs of |
| 146 |
// interior blank lines to a single blank line. |
| 147 |
n := 0 |
| 148 |
for _, line := range lines { |
| 149 |
if line != "" || n > 0 && lines[n-1] != "" { |
| 150 |
lines[n] = line |
| 151 |
n++ |
| 152 |
} |
| 153 |
} |
| 154 |
lines = lines[0:n] |
| 155 |
|
| 156 |
// Add final "" entry to get trailing newline from Join. |
| 157 |
if n > 0 && lines[n-1] != "" { |
| 158 |
lines = append(lines, "") |
| 159 |
} |
| 160 |
|
| 161 |
return strings.Join(lines, "\n") |
| 162 |
} |
| 163 |
|
| 164 |
// isDirective reports whether c is a comment directive. |
| 165 |
// This code is also in go/printer. |
| 166 |
func isDirective(c string) bool { |
| 167 |
// "//line " is a line directive. |
| 168 |
// "//extern " is for gccgo. |
| 169 |
// "//export " is for cgo. |
| 170 |
// (The // has been removed.) |
| 171 |
if strings.HasPrefix(c, "line ") || strings.HasPrefix(c, "extern ") || strings.HasPrefix(c, "export ") { |
| 172 |
return true |
| 173 |
} |
| 174 |
|
| 175 |
// "//[a-z0-9]+:[a-z0-9]" |
| 176 |
// (The // has been removed.) |
| 177 |
colon := strings.Index(c, ":") |
| 178 |
if colon <= 0 || colon+1 >= len(c) { |
| 179 |
return false |
| 180 |
} |
| 181 |
for i := 0; i <= colon+1; i++ { |
| 182 |
if i == colon { |
| 183 |
continue |
| 184 |
} |
| 185 |
b := c[i] |
| 186 |
if !('a' <= b && b <= 'z' || '0' <= b && b <= '9') { |
| 187 |
return false |
| 188 |
} |
| 189 |
} |
| 190 |
return true |
| 191 |
} |
| 192 |
|
| 193 |
// ---------------------------------------------------------------------------- |
| 194 |
// Expressions and types |
| 195 |
|
| 196 |
// A Field represents a Field declaration list in a struct type, |
| 197 |
// a method list in an interface type, or a parameter/result declaration |
| 198 |
// in a signature. |
| 199 |
// [Field.Names] is nil for unnamed parameters (parameter lists which only contain types) |
| 200 |
// and embedded struct fields. In the latter case, the field name is the type name. |
| 201 |
type Field struct { |
| 202 |
Doc *CommentGroup // associated documentation; or nil |
| 203 |
Names []*Ident // field/method/(type) parameter names; or nil |
| 204 |
Type Expr // field/method/parameter type; or nil |
| 205 |
Tag *BasicLit // field tag; or nil |
| 206 |
Comment *CommentGroup // line comments; or nil |
| 207 |
} |
| 208 |
|
| 209 |
func (f *Field) Pos() token.Pos { |
| 210 |
if len(f.Names) > 0 { |
| 211 |
return f.Names[0].Pos() |
| 212 |
} |
| 213 |
if f.Type != nil { |
| 214 |
return f.Type.Pos() |
| 215 |
} |
| 216 |
return token.NoPos |
| 217 |
} |
| 218 |
|
| 219 |
func (f *Field) End() token.Pos { |
| 220 |
if f.Tag != nil { |
| 221 |
return f.Tag.End() |
| 222 |
} |
| 223 |
if f.Type != nil { |
| 224 |
return f.Type.End() |
| 225 |
} |
| 226 |
if len(f.Names) > 0 { |
| 227 |
return f.Names[len(f.Names)-1].End() |
| 228 |
} |
| 229 |
return token.NoPos |
| 230 |
} |
| 231 |
|
| 232 |
// A FieldList represents a list of Fields, enclosed by parentheses, |
| 233 |
// curly braces, or square brackets. |
| 234 |
type FieldList struct { |
| 235 |
Opening token.Pos // position of opening parenthesis/brace/bracket, if any |
| 236 |
List []*Field // field list; or nil |
| 237 |
Closing token.Pos // position of closing parenthesis/brace/bracket, if any |
| 238 |
} |
| 239 |
|
| 240 |
func (f *FieldList) Pos() token.Pos { |
| 241 |
if f.Opening.IsValid() { |
| 242 |
return f.Opening |
| 243 |
} |
| 244 |
// the list should not be empty in this case; |
| 245 |
// be conservative and guard against bad ASTs |
| 246 |
if len(f.List) > 0 { |
| 247 |
return f.List[0].Pos() |
| 248 |
} |
| 249 |
return token.NoPos |
| 250 |
} |
| 251 |
|
| 252 |
func (f *FieldList) End() token.Pos { |
| 253 |
if f.Closing.IsValid() { |
| 254 |
return f.Closing + 1 |
| 255 |
} |
| 256 |
// the list should not be empty in this case; |
| 257 |
// be conservative and guard against bad ASTs |
| 258 |
if n := len(f.List); n > 0 { |
| 259 |
return f.List[n-1].End() |
| 260 |
} |
| 261 |
return token.NoPos |
| 262 |
} |
| 263 |
|
| 264 |
// NumFields returns the number of parameters or struct fields represented by a [FieldList]. |
| 265 |
func (f *FieldList) NumFields() int { |
| 266 |
n := 0 |
| 267 |
if f != nil { |
| 268 |
for _, g := range f.List { |
| 269 |
m := len(g.Names) |
| 270 |
if m == 0 { |
| 271 |
m = 1 |
| 272 |
} |
| 273 |
n += m |
| 274 |
} |
| 275 |
} |
| 276 |
return n |
| 277 |
} |
| 278 |
|
| 279 |
// An expression is represented by a tree consisting of one |
| 280 |
// or more of the following concrete expression nodes. |
| 281 |
type ( |
| 282 |
// A BadExpr node is a placeholder for an expression containing |
| 283 |
// syntax errors for which a correct expression node cannot be |
| 284 |
// created. |
| 285 |
// |
| 286 |
BadExpr struct { |
| 287 |
From, To token.Pos // position range of bad expression |
| 288 |
} |
| 289 |
|
| 290 |
// An Ident node represents an identifier. |
| 291 |
Ident struct { |
| 292 |
NamePos token.Pos // identifier position |
| 293 |
Name string // identifier name |
| 294 |
Obj *Object // denoted object, or nil. Deprecated: see Object. |
| 295 |
} |
| 296 |
|
| 297 |
// An Ellipsis node stands for the "..." type in a |
| 298 |
// parameter list or the "..." length in an array type. |
| 299 |
// |
| 300 |
Ellipsis struct { |
| 301 |
Ellipsis token.Pos // position of "..." |
| 302 |
Elt Expr // ellipsis element type (parameter lists only); or nil |
| 303 |
} |
| 304 |
|
| 305 |
// A BasicLit node represents a literal of basic type. |
| 306 |
// |
| 307 |
// Note that for the CHAR and STRING kinds, the literal is stored |
| 308 |
// with its quotes. For example, for a double-quoted STRING, the |
| 309 |
// first and the last rune in the Value field will be ". The |
| 310 |
// [strconv.Unquote] and [strconv.UnquoteChar] functions can be |
| 311 |
// used to unquote STRING and CHAR values, respectively. |
| 312 |
// |
| 313 |
// For raw string literals (Kind == token.STRING && Value[0] == '`'), |
| 314 |
// the Value field contains the string text without carriage returns (\r) that |
| 315 |
// may have been present in the source. Because the end position is |
| 316 |
// computed using len(Value), the position reported by [BasicLit.End] does not match the |
| 317 |
// true source end position for raw string literals containing carriage returns. |
| 318 |
BasicLit struct { |
| 319 |
ValuePos token.Pos // literal position |
| 320 |
Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING |
| 321 |
Value string // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o` |
| 322 |
} |
| 323 |
|
| 324 |
// A FuncLit node represents a function literal. |
| 325 |
FuncLit struct { |
| 326 |
Type *FuncType // function type |
| 327 |
Body *BlockStmt // function body |
| 328 |
} |
| 329 |
|
| 330 |
// A CompositeLit node represents a composite literal. |
| 331 |
CompositeLit struct { |
| 332 |
Type Expr // literal type; or nil |
| 333 |
Lbrace token.Pos // position of "{" |
| 334 |
Elts []Expr // list of composite elements; or nil |
| 335 |
Rbrace token.Pos // position of "}" |
| 336 |
Incomplete bool // true if (source) expressions are missing in the Elts list |
| 337 |
} |
| 338 |
|
| 339 |
// A ParenExpr node represents a parenthesized expression. |
| 340 |
ParenExpr struct { |
| 341 |
Lparen token.Pos // position of "(" |
| 342 |
X Expr // parenthesized expression |
| 343 |
Rparen token.Pos // position of ")" |
| 344 |
} |
| 345 |
|
| 346 |
// A SelectorExpr node represents an expression followed by a selector. |
| 347 |
SelectorExpr struct { |
| 348 |
X Expr // expression |
| 349 |
Sel *Ident // field selector |
| 350 |
} |
| 351 |
|
| 352 |
// An IndexExpr node represents an expression followed by an index. |
| 353 |
IndexExpr struct { |
| 354 |
X Expr // expression |
| 355 |
Lbrack token.Pos // position of "[" |
| 356 |
Index Expr // index expression |
| 357 |
Rbrack token.Pos // position of "]" |
| 358 |
} |
| 359 |
|
| 360 |
// An IndexListExpr node represents an expression followed by multiple |
| 361 |
// indices. |
| 362 |
IndexListExpr struct { |
| 363 |
X Expr // expression |
| 364 |
Lbrack token.Pos // position of "[" |
| 365 |
Indices []Expr // index expressions |
| 366 |
Rbrack token.Pos // position of "]" |
| 367 |
} |
| 368 |
|
| 369 |
// A SliceExpr node represents an expression followed by slice indices. |
| 370 |
SliceExpr struct { |
| 371 |
X Expr // expression |
| 372 |
Lbrack token.Pos // position of "[" |
| 373 |
Low Expr // begin of slice range; or nil |
| 374 |
High Expr // end of slice range; or nil |
| 375 |
Max Expr // maximum capacity of slice; or nil |
| 376 |
Slice3 bool // true if 3-index slice (2 colons present) |
| 377 |
Rbrack token.Pos // position of "]" |
| 378 |
} |
| 379 |
|
| 380 |
// A TypeAssertExpr node represents an expression followed by a |
| 381 |
// type assertion. |
| 382 |
// |
| 383 |
TypeAssertExpr struct { |
| 384 |
X Expr // expression |
| 385 |
Lparen token.Pos // position of "(" |
| 386 |
Type Expr // asserted type; nil means type switch X.(type) |
| 387 |
Rparen token.Pos // position of ")" |
| 388 |
} |
| 389 |
|
| 390 |
// A CallExpr node represents an expression followed by an argument list. |
| 391 |
CallExpr struct { |
| 392 |
Fun Expr // function expression |
| 393 |
Lparen token.Pos // position of "(" |
| 394 |
Args []Expr // function arguments; or nil |
| 395 |
Ellipsis token.Pos // position of "..." (token.NoPos if there is no "...") |
| 396 |
Rparen token.Pos // position of ")" |
| 397 |
} |
| 398 |
|
| 399 |
// A StarExpr node represents an expression of the form "*" Expression. |
| 400 |
// Semantically it could be a unary "*" expression, or a pointer type. |
| 401 |
// |
| 402 |
StarExpr struct { |
| 403 |
Star token.Pos // position of "*" |
| 404 |
X Expr // operand |
| 405 |
} |
| 406 |
|
| 407 |
// A UnaryExpr node represents a unary expression. |
| 408 |
// Unary "*" expressions are represented via StarExpr nodes. |
| 409 |
// |
| 410 |
UnaryExpr struct { |
| 411 |
OpPos token.Pos // position of Op |
| 412 |
Op token.Token // operator |
| 413 |
X Expr // operand |
| 414 |
} |
| 415 |
|
| 416 |
// A BinaryExpr node represents a binary expression. |
| 417 |
BinaryExpr struct { |
| 418 |
X Expr // left operand |
| 419 |
OpPos token.Pos // position of Op |
| 420 |
Op token.Token // operator |
| 421 |
Y Expr // right operand |
| 422 |
} |
| 423 |
|
| 424 |
// A KeyValueExpr node represents (key : value) pairs |
| 425 |
// in composite literals. |
| 426 |
// |
| 427 |
KeyValueExpr struct { |
| 428 |
Key Expr |
| 429 |
Colon token.Pos // position of ":" |
| 430 |
Value Expr |
| 431 |
} |
| 432 |
) |
| 433 |
|
| 434 |
// The direction of a channel type is indicated by a bit |
| 435 |
// mask including one or both of the following constants. |
| 436 |
type ChanDir int |
| 437 |
|
| 438 |
const ( |
| 439 |
SEND ChanDir = 1 << iota |
| 440 |
RECV |
| 441 |
) |
| 442 |
|
| 443 |
// A type is represented by a tree consisting of one |
| 444 |
// or more of the following type-specific expression |
| 445 |
// nodes. |
| 446 |
type ( |
| 447 |
// An ArrayType node represents an array or slice type. |
| 448 |
ArrayType struct { |
| 449 |
Lbrack token.Pos // position of "[" |
| 450 |
Len Expr // Ellipsis node for [...]T array types, nil for slice types |
| 451 |
Elt Expr // element type |
| 452 |
} |
| 453 |
|
| 454 |
// A StructType node represents a struct type. |
| 455 |
StructType struct { |
| 456 |
Struct token.Pos // position of "struct" keyword |
| 457 |
Fields *FieldList // list of field declarations |
| 458 |
Incomplete bool // true if (source) fields are missing in the Fields list |
| 459 |
} |
| 460 |
|
| 461 |
// Pointer types are represented via StarExpr nodes. |
| 462 |
|
| 463 |
// A FuncType node represents a function type. |
| 464 |
FuncType struct { |
| 465 |
Func token.Pos // position of "func" keyword (token.NoPos if there is no "func") |
| 466 |
TypeParams *FieldList // type parameters; or nil |
| 467 |
Params *FieldList // (incoming) parameters; non-nil |
| 468 |
Results *FieldList // (outgoing) results; or nil |
| 469 |
} |
| 470 |
|
| 471 |
// An InterfaceType node represents an interface type. |
| 472 |
InterfaceType struct { |
| 473 |
Interface token.Pos // position of "interface" keyword |
| 474 |
Methods *FieldList // list of embedded interfaces, methods, or types |
| 475 |
Incomplete bool // true if (source) methods or types are missing in the Methods list |
| 476 |
} |
| 477 |
|
| 478 |
// A MapType node represents a map type. |
| 479 |
MapType struct { |
| 480 |
Map token.Pos // position of "map" keyword |
| 481 |
Key Expr |
| 482 |
Value Expr |
| 483 |
} |
| 484 |
|
| 485 |
// A ChanType node represents a channel type. |
| 486 |
ChanType struct { |
| 487 |
Begin token.Pos // position of "chan" keyword or "<-" (whichever comes first) |
| 488 |
Arrow token.Pos // position of "<-" (token.NoPos if there is no "<-") |
| 489 |
Dir ChanDir // channel direction |
| 490 |
Value Expr // value type |
| 491 |
} |
| 492 |
) |
| 493 |
|
| 494 |
// Pos and End implementations for expression/type nodes. |
| 495 |
|
| 496 |
func (x *BadExpr) Pos() token.Pos { return x.From } |
| 497 |
func (x *Ident) Pos() token.Pos { return x.NamePos } |
| 498 |
func (x *Ellipsis) Pos() token.Pos { return x.Ellipsis } |
| 499 |
func (x *BasicLit) Pos() token.Pos { return x.ValuePos } |
| 500 |
func (x *FuncLit) Pos() token.Pos { return x.Type.Pos() } |
| 501 |
func (x *CompositeLit) Pos() token.Pos { |
| 502 |
if x.Type != nil { |
| 503 |
return x.Type.Pos() |
| 504 |
} |
| 505 |
return x.Lbrace |
| 506 |
} |
| 507 |
func (x *ParenExpr) Pos() token.Pos { return x.Lparen } |
| 508 |
func (x *SelectorExpr) Pos() token.Pos { return x.X.Pos() } |
| 509 |
func (x *IndexExpr) Pos() token.Pos { return x.X.Pos() } |
| 510 |
func (x *IndexListExpr) Pos() token.Pos { return x.X.Pos() } |
| 511 |
func (x *SliceExpr) Pos() token.Pos { return x.X.Pos() } |
| 512 |
func (x *TypeAssertExpr) Pos() token.Pos { return x.X.Pos() } |
| 513 |
func (x *CallExpr) Pos() token.Pos { return x.Fun.Pos() } |
| 514 |
func (x *StarExpr) Pos() token.Pos { return x.Star } |
| 515 |
func (x *UnaryExpr) Pos() token.Pos { return x.OpPos } |
| 516 |
func (x *BinaryExpr) Pos() token.Pos { return x.X.Pos() } |
| 517 |
func (x *KeyValueExpr) Pos() token.Pos { return x.Key.Pos() } |
| 518 |
func (x *ArrayType) Pos() token.Pos { return x.Lbrack } |
| 519 |
func (x *StructType) Pos() token.Pos { return x.Struct } |
| 520 |
func (x *FuncType) Pos() token.Pos { |
| 521 |
if x.Func.IsValid() || x.Params == nil { // see issue 3870 |
| 522 |
return x.Func |
| 523 |
} |
| 524 |
return x.Params.Pos() // interface method declarations have no "func" keyword |
| 525 |
} |
| 526 |
func (x *InterfaceType) Pos() token.Pos { return x.Interface } |
| 527 |
func (x *MapType) Pos() token.Pos { return x.Map } |
| 528 |
func (x *ChanType) Pos() token.Pos { return x.Begin } |
| 529 |
|
| 530 |
func (x *BadExpr) End() token.Pos { return x.To } |
| 531 |
func (x *Ident) End() token.Pos { return token.Pos(int(x.NamePos) + len(x.Name)) } |
| 532 |
func (x *Ellipsis) End() token.Pos { |
| 533 |
if x.Elt != nil { |
| 534 |
return x.Elt.End() |
| 535 |
} |
| 536 |
return x.Ellipsis + 3 // len("...") |
| 537 |
} |
| 538 |
func (x *BasicLit) End() token.Pos { return token.Pos(int(x.ValuePos) + len(x.Value)) } |
| 539 |
func (x *FuncLit) End() token.Pos { return x.Body.End() } |
| 540 |
func (x *CompositeLit) End() token.Pos { return x.Rbrace + 1 } |
| 541 |
func (x *ParenExpr) End() token.Pos { return x.Rparen + 1 } |
| 542 |
func (x *SelectorExpr) End() token.Pos { return x.Sel.End() } |
| 543 |
func (x *IndexExpr) End() token.Pos { return x.Rbrack + 1 } |
| 544 |
func (x *IndexListExpr) End() token.Pos { return x.Rbrack + 1 } |
| 545 |
func (x *SliceExpr) End() token.Pos { return x.Rbrack + 1 } |
| 546 |
func (x *TypeAssertExpr) End() token.Pos { return x.Rparen + 1 } |
| 547 |
func (x *CallExpr) End() token.Pos { return x.Rparen + 1 } |
| 548 |
func (x *StarExpr) End() token.Pos { return x.X.End() } |
| 549 |
func (x *UnaryExpr) End() token.Pos { return x.X.End() } |
| 550 |
func (x *BinaryExpr) End() token.Pos { return x.Y.End() } |
| 551 |
func (x *KeyValueExpr) End() token.Pos { return x.Value.End() } |
| 552 |
func (x *ArrayType) End() token.Pos { return x.Elt.End() } |
| 553 |
func (x *StructType) End() token.Pos { return x.Fields.End() } |
| 554 |
func (x *FuncType) End() token.Pos { |
| 555 |
if x.Results != nil { |
| 556 |
return x.Results.End() |
| 557 |
} |
| 558 |
return x.Params.End() |
| 559 |
} |
| 560 |
func (x *InterfaceType) End() token.Pos { return x.Methods.End() } |
| 561 |
func (x *MapType) End() token.Pos { return x.Value.End() } |
| 562 |
func (x *ChanType) End() token.Pos { return x.Value.End() } |
| 563 |
|
| 564 |
// exprNode() ensures that only expression/type nodes can be |
| 565 |
// assigned to an Expr. |
| 566 |
func (*BadExpr) exprNode() {} |
| 567 |
func (*Ident) exprNode() {} |
| 568 |
func (*Ellipsis) exprNode() {} |
| 569 |
func (*BasicLit) exprNode() {} |
| 570 |
func (*FuncLit) exprNode() {} |
| 571 |
func (*CompositeLit) exprNode() {} |
| 572 |
func (*ParenExpr) exprNode() {} |
| 573 |
func (*SelectorExpr) exprNode() {} |
| 574 |
func (*IndexExpr) exprNode() {} |
| 575 |
func (*IndexListExpr) exprNode() {} |
| 576 |
func (*SliceExpr) exprNode() {} |
| 577 |
func (*TypeAssertExpr) exprNode() {} |
| 578 |
func (*CallExpr) exprNode() {} |
| 579 |
func (*StarExpr) exprNode() {} |
| 580 |
func (*UnaryExpr) exprNode() {} |
| 581 |
func (*BinaryExpr) exprNode() {} |
| 582 |
func (*KeyValueExpr) exprNode() {} |
| 583 |
|
| 584 |
func (*ArrayType) exprNode() {} |
| 585 |
func (*StructType) exprNode() {} |
| 586 |
func (*FuncType) exprNode() {} |
| 587 |
func (*InterfaceType) exprNode() {} |
| 588 |
func (*MapType) exprNode() {} |
| 589 |
func (*ChanType) exprNode() {} |
| 590 |
|
| 591 |
// ---------------------------------------------------------------------------- |
| 592 |
// Convenience functions for Idents |
| 593 |
|
| 594 |
// NewIdent creates a new [Ident] without position. |
| 595 |
// Useful for ASTs generated by code other than the Go parser. |
| 596 |
func NewIdent(name string) *Ident { return &Ident{token.NoPos, name, nil} } |
| 597 |
|
| 598 |
// IsExported reports whether name starts with an upper-case letter. |
| 599 |
func IsExported(name string) bool { return token.IsExported(name) } |
| 600 |
|
| 601 |
// IsExported reports whether id starts with an upper-case letter. |
| 602 |
func (id *Ident) IsExported() bool { return token.IsExported(id.Name) } |
| 603 |
|
| 604 |
func (id *Ident) String() string { |
| 605 |
if id != nil { |
| 606 |
return id.Name |
| 607 |
} |
| 608 |
return "<nil>" |
| 609 |
} |
| 610 |
|
| 611 |
// ---------------------------------------------------------------------------- |
| 612 |
// Statements |
| 613 |
|
| 614 |
// A statement is represented by a tree consisting of one |
| 615 |
// or more of the following concrete statement nodes. |
| 616 |
type ( |
| 617 |
// A BadStmt node is a placeholder for statements containing |
| 618 |
// syntax errors for which no correct statement nodes can be |
| 619 |
// created. |
| 620 |
// |
| 621 |
BadStmt struct { |
| 622 |
From, To token.Pos // position range of bad statement |
| 623 |
} |
| 624 |
|
| 625 |
// A DeclStmt node represents a declaration in a statement list. |
| 626 |
DeclStmt struct { |
| 627 |
Decl Decl // *GenDecl with CONST, TYPE, or VAR token |
| 628 |
} |
| 629 |
|
| 630 |
// An EmptyStmt node represents an empty statement. |
| 631 |
// The "position" of the empty statement is the position |
| 632 |
// of the immediately following (explicit or implicit) semicolon. |
| 633 |
// |
| 634 |
EmptyStmt struct { |
| 635 |
Semicolon token.Pos // position of following ";" |
| 636 |
Implicit bool // if set, ";" was omitted in the source |
| 637 |
} |
| 638 |
|
| 639 |
// A LabeledStmt node represents a labeled statement. |
| 640 |
LabeledStmt struct { |
| 641 |
Label *Ident |
| 642 |
Colon token.Pos // position of ":" |
| 643 |
Stmt Stmt |
| 644 |
} |
| 645 |
|
| 646 |
// An ExprStmt node represents a (stand-alone) expression |
| 647 |
// in a statement list. |
| 648 |
// |
| 649 |
ExprStmt struct { |
| 650 |
X Expr // expression |
| 651 |
} |
| 652 |
|
| 653 |
// A SendStmt node represents a send statement. |
| 654 |
SendStmt struct { |
| 655 |
Chan Expr |
| 656 |
Arrow token.Pos // position of "<-" |
| 657 |
Value Expr |
| 658 |
} |
| 659 |
|
| 660 |
// An IncDecStmt node represents an increment or decrement statement. |
| 661 |
IncDecStmt struct { |
| 662 |
X Expr |
| 663 |
TokPos token.Pos // position of Tok |
| 664 |
Tok token.Token // INC or DEC |
| 665 |
} |
| 666 |
|
| 667 |
// An AssignStmt node represents an assignment or |
| 668 |
// a short variable declaration. |
| 669 |
// |
| 670 |
AssignStmt struct { |
| 671 |
Lhs []Expr |
| 672 |
TokPos token.Pos // position of Tok |
| 673 |
Tok token.Token // assignment token, DEFINE |
| 674 |
Rhs []Expr |
| 675 |
} |
| 676 |
|
| 677 |
// A GoStmt node represents a go statement. |
| 678 |
GoStmt struct { |
| 679 |
Go token.Pos // position of "go" keyword |
| 680 |
Call *CallExpr |
| 681 |
} |
| 682 |
|
| 683 |
// A DeferStmt node represents a defer statement. |
| 684 |
DeferStmt struct { |
| 685 |
Defer token.Pos // position of "defer" keyword |
| 686 |
Call *CallExpr |
| 687 |
} |
| 688 |
|
| 689 |
// A ReturnStmt node represents a return statement. |
| 690 |
ReturnStmt struct { |
| 691 |
Return token.Pos // position of "return" keyword |
| 692 |
Results []Expr // result expressions; or nil |
| 693 |
} |
| 694 |
|
| 695 |
// A BranchStmt node represents a break, continue, goto, |
| 696 |
// or fallthrough statement. |
| 697 |
// |
| 698 |
BranchStmt struct { |
| 699 |
TokPos token.Pos // position of Tok |
| 700 |
Tok token.Token // keyword token (BREAK, CONTINUE, GOTO, FALLTHROUGH) |
| 701 |
Label *Ident // label name; or nil |
| 702 |
} |
| 703 |
|
| 704 |
// A BlockStmt node represents a braced statement list. |
| 705 |
BlockStmt struct { |
| 706 |
Lbrace token.Pos // position of "{" |
| 707 |
List []Stmt |
| 708 |
Rbrace token.Pos // position of "}", if any (may be absent due to syntax error) |
| 709 |
} |
| 710 |
|
| 711 |
// An IfStmt node represents an if statement. |
| 712 |
IfStmt struct { |
| 713 |
If token.Pos // position of "if" keyword |
| 714 |
Init Stmt // initialization statement; or nil |
| 715 |
Cond Expr // condition |
| 716 |
Body *BlockStmt |
| 717 |
Else Stmt // else branch; or nil |
| 718 |
} |
| 719 |
|
| 720 |
// A CaseClause represents a case of an expression or type switch statement. |
| 721 |
CaseClause struct { |
| 722 |
Case token.Pos // position of "case" or "default" keyword |
| 723 |
List []Expr // list of expressions or types; nil means default case |
| 724 |
Colon token.Pos // position of ":" |
| 725 |
Body []Stmt // statement list; or nil |
| 726 |
} |
| 727 |
|
| 728 |
// A SwitchStmt node represents an expression switch statement. |
| 729 |
SwitchStmt struct { |
| 730 |
Switch token.Pos // position of "switch" keyword |
| 731 |
Init Stmt // initialization statement; or nil |
| 732 |
Tag Expr // tag expression; or nil |
| 733 |
Body *BlockStmt // CaseClauses only |
| 734 |
} |
| 735 |
|
| 736 |
// A TypeSwitchStmt node represents a type switch statement. |
| 737 |
TypeSwitchStmt struct { |
| 738 |
Switch token.Pos // position of "switch" keyword |
| 739 |
Init Stmt // initialization statement; or nil |
| 740 |
Assign Stmt // x := y.(type) or y.(type) |
| 741 |
Body *BlockStmt // CaseClauses only |
| 742 |
} |
| 743 |
|
| 744 |
// A CommClause node represents a case of a select statement. |
| 745 |
CommClause struct { |
| 746 |
Case token.Pos // position of "case" or "default" keyword |
| 747 |
Comm Stmt // send or receive statement; nil means default case |
| 748 |
Colon token.Pos // position of ":" |
| 749 |
Body []Stmt // statement list; or nil |
| 750 |
} |
| 751 |
|
| 752 |
// A SelectStmt node represents a select statement. |
| 753 |
SelectStmt struct { |
| 754 |
Select token.Pos // position of "select" keyword |
| 755 |
Body *BlockStmt // CommClauses only |
| 756 |
} |
| 757 |
|
| 758 |
// A ForStmt represents a for statement. |
| 759 |
ForStmt struct { |
| 760 |
For token.Pos // position of "for" keyword |
| 761 |
Init Stmt // initialization statement; or nil |
| 762 |
Cond Expr // condition; or nil |
| 763 |
Post Stmt // post iteration statement; or nil |
| 764 |
Body *BlockStmt |
| 765 |
} |
| 766 |
|
| 767 |
// A RangeStmt represents a for statement with a range clause. |
| 768 |
RangeStmt struct { |
| 769 |
For token.Pos // position of "for" keyword |
| 770 |
Key, Value Expr // Key, Value may be nil |
| 771 |
TokPos token.Pos // position of Tok; invalid if Key == nil |
| 772 |
Tok token.Token // ILLEGAL if Key == nil, ASSIGN, DEFINE |
| 773 |
Range token.Pos // position of "range" keyword |
| 774 |
X Expr // value to range over |
| 775 |
Body *BlockStmt |
| 776 |
} |
| 777 |
) |
| 778 |
|
| 779 |
// Pos and End implementations for statement nodes. |
| 780 |
|
| 781 |
func (s *BadStmt) Pos() token.Pos { return s.From } |
| 782 |
func (s *DeclStmt) Pos() token.Pos { return s.Decl.Pos() } |
| 783 |
func (s *EmptyStmt) Pos() token.Pos { return s.Semicolon } |
| 784 |
func (s *LabeledStmt) Pos() token.Pos { return s.Label.Pos() } |
| 785 |
func (s *ExprStmt) Pos() token.Pos { return s.X.Pos() } |
| 786 |
func (s *SendStmt) Pos() token.Pos { return s.Chan.Pos() } |
| 787 |
func (s *IncDecStmt) Pos() token.Pos { return s.X.Pos() } |
| 788 |
func (s *AssignStmt) Pos() token.Pos { return s.Lhs[0].Pos() } |
| 789 |
func (s *GoStmt) Pos() token.Pos { return s.Go } |
| 790 |
func (s *DeferStmt) Pos() token.Pos { return s.Defer } |
| 791 |
func (s *ReturnStmt) Pos() token.Pos { return s.Return } |
| 792 |
func (s *BranchStmt) Pos() token.Pos { return s.TokPos } |
| 793 |
func (s *BlockStmt) Pos() token.Pos { return s.Lbrace } |
| 794 |
func (s *IfStmt) Pos() token.Pos { return s.If } |
| 795 |
func (s *CaseClause) Pos() token.Pos { return s.Case } |
| 796 |
func (s *SwitchStmt) Pos() token.Pos { return s.Switch } |
| 797 |
func (s *TypeSwitchStmt) Pos() token.Pos { return s.Switch } |
| 798 |
func (s *CommClause) Pos() token.Pos { return s.Case } |
| 799 |
func (s *SelectStmt) Pos() token.Pos { return s.Select } |
| 800 |
func (s *ForStmt) Pos() token.Pos { return s.For } |
| 801 |
func (s *RangeStmt) Pos() token.Pos { return s.For } |
| 802 |
|
| 803 |
func (s *BadStmt) End() token.Pos { return s.To } |
| 804 |
func (s *DeclStmt) End() token.Pos { return s.Decl.End() } |
| 805 |
func (s *EmptyStmt) End() token.Pos { |
| 806 |
if s.Implicit { |
| 807 |
return s.Semicolon |
| 808 |
} |
| 809 |
return s.Semicolon + 1 /* len(";") */ |
| 810 |
} |
| 811 |
func (s *LabeledStmt) End() token.Pos { return s.Stmt.End() } |
| 812 |
func (s *ExprStmt) End() token.Pos { return s.X.End() } |
| 813 |
func (s *SendStmt) End() token.Pos { return s.Value.End() } |
| 814 |
func (s *IncDecStmt) End() token.Pos { |
| 815 |
return s.TokPos + 2 /* len("++") */ |
| 816 |
} |
| 817 |
func (s *AssignStmt) End() token.Pos { return s.Rhs[len(s.Rhs)-1].End() } |
| 818 |
func (s *GoStmt) End() token.Pos { return s.Call.End() } |
| 819 |
func (s *DeferStmt) End() token.Pos { return s.Call.End() } |
| 820 |
func (s *ReturnStmt) End() token.Pos { |
| 821 |
if n := len(s.Results); n > 0 { |
| 822 |
return s.Results[n-1].End() |
| 823 |
} |
| 824 |
return s.Return + 6 // len("return") |
| 825 |
} |
| 826 |
func (s *BranchStmt) End() token.Pos { |
| 827 |
if s.Label != nil { |
| 828 |
return s.Label.End() |
| 829 |
} |
| 830 |
return token.Pos(int(s.TokPos) + len(s.Tok.String())) |
| 831 |
} |
| 832 |
func (s *BlockStmt) End() token.Pos { |
| 833 |
if s.Rbrace.IsValid() { |
| 834 |
return s.Rbrace + 1 |
| 835 |
} |
| 836 |
if n := len(s.List); n > 0 { |
| 837 |
return s.List[n-1].End() |
| 838 |
} |
| 839 |
return s.Lbrace + 1 |
| 840 |
} |
| 841 |
func (s *IfStmt) End() token.Pos { |
| 842 |
if s.Else != nil { |
| 843 |
return s.Else.End() |
| 844 |
} |
| 845 |
return s.Body.End() |
| 846 |
} |
| 847 |
func (s *CaseClause) End() token.Pos { |
| 848 |
if n := len(s.Body); n > 0 { |
| 849 |
return s.Body[n-1].End() |
| 850 |
} |
| 851 |
return s.Colon + 1 |
| 852 |
} |
| 853 |
func (s *SwitchStmt) End() token.Pos { return s.Body.End() } |
| 854 |
func (s *TypeSwitchStmt) End() token.Pos { return s.Body.End() } |
| 855 |
func (s *CommClause) End() token.Pos { |
| 856 |
if n := len(s.Body); n > 0 { |
| 857 |
return s.Body[n-1].End() |
| 858 |
} |
| 859 |
return s.Colon + 1 |
| 860 |
} |
| 861 |
func (s *SelectStmt) End() token.Pos { return s.Body.End() } |
| 862 |
func (s *ForStmt) End() token.Pos { return s.Body.End() } |
| 863 |
func (s *RangeStmt) End() token.Pos { return s.Body.End() } |
| 864 |
|
| 865 |
// stmtNode() ensures that only statement nodes can be |
| 866 |
// assigned to a Stmt. |
| 867 |
func (*BadStmt) stmtNode() {} |
| 868 |
func (*DeclStmt) stmtNode() {} |
| 869 |
func (*EmptyStmt) stmtNode() {} |
| 870 |
func (*LabeledStmt) stmtNode() {} |
| 871 |
func (*ExprStmt) stmtNode() {} |
| 872 |
func (*SendStmt) stmtNode() {} |
| 873 |
func (*IncDecStmt) stmtNode() {} |
| 874 |
func (*AssignStmt) stmtNode() {} |
| 875 |
func (*GoStmt) stmtNode() {} |
| 876 |
func (*DeferStmt) stmtNode() {} |
| 877 |
func (*ReturnStmt) stmtNode() {} |
| 878 |
func (*BranchStmt) stmtNode() {} |
| 879 |
func (*BlockStmt) stmtNode() {} |
| 880 |
func (*IfStmt) stmtNode() {} |
| 881 |
func (*CaseClause) stmtNode() {} |
| 882 |
func (*SwitchStmt) stmtNode() {} |
| 883 |
func (*TypeSwitchStmt) stmtNode() {} |
| 884 |
func (*CommClause) stmtNode() {} |
| 885 |
func (*SelectStmt) stmtNode() {} |
| 886 |
func (*ForStmt) stmtNode() {} |
| 887 |
func (*RangeStmt) stmtNode() {} |
| 888 |
|
| 889 |
// ---------------------------------------------------------------------------- |
| 890 |
// Declarations |
| 891 |
|
| 892 |
// A Spec node represents a single (non-parenthesized) import, |
| 893 |
// constant, type, or variable declaration. |
| 894 |
type ( |
| 895 |
// The Spec type stands for any of *ImportSpec, *ValueSpec, and *TypeSpec. |
| 896 |
Spec interface { |
| 897 |
Node |
| 898 |
specNode() |
| 899 |
} |
| 900 |
|
| 901 |
// An ImportSpec node represents a single package import. |
| 902 |
ImportSpec struct { |
| 903 |
Doc *CommentGroup // associated documentation; or nil |
| 904 |
Name *Ident // local package name (including "."); or nil |
| 905 |
Path *BasicLit // import path |
| 906 |
Comment *CommentGroup // line comments; or nil |
| 907 |
EndPos token.Pos // end of spec (overrides Path.Pos if nonzero) |
| 908 |
} |
| 909 |
|
| 910 |
// A ValueSpec node represents a constant or variable declaration |
| 911 |
// (ConstSpec or VarSpec production). |
| 912 |
// |
| 913 |
ValueSpec struct { |
| 914 |
Doc *CommentGroup // associated documentation; or nil |
| 915 |
Names []*Ident // value names (len(Names) > 0) |
| 916 |
Type Expr // value type; or nil |
| 917 |
Values []Expr // initial values; or nil |
| 918 |
Comment *CommentGroup // line comments; or nil |
| 919 |
} |
| 920 |
|
| 921 |
// A TypeSpec node represents a type declaration (TypeSpec production). |
| 922 |
TypeSpec struct { |
| 923 |
Doc *CommentGroup // associated documentation; or nil |
| 924 |
Name *Ident // type name |
| 925 |
TypeParams *FieldList // type parameters; or nil |
| 926 |
Assign token.Pos // position of '=', if any |
| 927 |
Type Expr // *Ident, *ParenExpr, *SelectorExpr, *StarExpr, or any of the *XxxTypes |
| 928 |
Comment *CommentGroup // line comments; or nil |
| 929 |
} |
| 930 |
) |
| 931 |
|
| 932 |
// Pos and End implementations for spec nodes. |
| 933 |
|
| 934 |
func (s *ImportSpec) Pos() token.Pos { |
| 935 |
if s.Name != nil { |
| 936 |
return s.Name.Pos() |
| 937 |
} |
| 938 |
return s.Path.Pos() |
| 939 |
} |
| 940 |
func (s *ValueSpec) Pos() token.Pos { return s.Names[0].Pos() } |
| 941 |
func (s *TypeSpec) Pos() token.Pos { return s.Name.Pos() } |
| 942 |
|
| 943 |
func (s *ImportSpec) End() token.Pos { |
| 944 |
if s.EndPos != 0 { |
| 945 |
return s.EndPos |
| 946 |
} |
| 947 |
return s.Path.End() |
| 948 |
} |
| 949 |
|
| 950 |
func (s *ValueSpec) End() token.Pos { |
| 951 |
if n := len(s.Values); n > 0 { |
| 952 |
return s.Values[n-1].End() |
| 953 |
} |
| 954 |
if s.Type != nil { |
| 955 |
return s.Type.End() |
| 956 |
} |
| 957 |
return s.Names[len(s.Names)-1].End() |
| 958 |
} |
| 959 |
func (s *TypeSpec) End() token.Pos { return s.Type.End() } |
| 960 |
|
| 961 |
// specNode() ensures that only spec nodes can be |
| 962 |
// assigned to a Spec. |
| 963 |
func (*ImportSpec) specNode() {} |
| 964 |
func (*ValueSpec) specNode() {} |
| 965 |
func (*TypeSpec) specNode() {} |
| 966 |
|
| 967 |
// A declaration is represented by one of the following declaration nodes. |
| 968 |
type ( |
| 969 |
// A BadDecl node is a placeholder for a declaration containing |
| 970 |
// syntax errors for which a correct declaration node cannot be |
| 971 |
// created. |
| 972 |
// |
| 973 |
BadDecl struct { |
| 974 |
From, To token.Pos // position range of bad declaration |
| 975 |
} |
| 976 |
|
| 977 |
// A GenDecl node (generic declaration node) represents an import, |
| 978 |
// constant, type or variable declaration. A valid Lparen position |
| 979 |
// (Lparen.IsValid()) indicates a parenthesized declaration. |
| 980 |
// |
| 981 |
// Relationship between Tok value and Specs element type: |
| 982 |
// |
| 983 |
// token.IMPORT *ImportSpec |
| 984 |
// token.CONST *ValueSpec |
| 985 |
// token.TYPE *TypeSpec |
| 986 |
// token.VAR *ValueSpec |
| 987 |
// |
| 988 |
GenDecl struct { |
| 989 |
Doc *CommentGroup // associated documentation; or nil |
| 990 |
TokPos token.Pos // position of Tok |
| 991 |
Tok token.Token // IMPORT, CONST, TYPE, or VAR |
| 992 |
Lparen token.Pos // position of '(', if any |
| 993 |
Specs []Spec |
| 994 |
Rparen token.Pos // position of ')', if any |
| 995 |
} |
| 996 |
|
| 997 |
// A FuncDecl node represents a function declaration. |
| 998 |
FuncDecl struct { |
| 999 |
Doc *CommentGroup // associated documentation; or nil |
| 1000 |
Recv *FieldList // receiver (methods); or nil (functions) |
| 1001 |
Name *Ident // function/method name |
| 1002 |
Type *FuncType // function signature: type and value parameters, results, and position of "func" keyword |
| 1003 |
Body *BlockStmt // function body; or nil for external (non-Go) function |
| 1004 |
} |
| 1005 |
) |
| 1006 |
|
| 1007 |
// Pos and End implementations for declaration nodes. |
| 1008 |
|
| 1009 |
func (d *BadDecl) Pos() token.Pos { return d.From } |
| 1010 |
func (d *GenDecl) Pos() token.Pos { return d.TokPos } |
| 1011 |
func (d *FuncDecl) Pos() token.Pos { return d.Type.Pos() } |
| 1012 |
|
| 1013 |
func (d *BadDecl) End() token.Pos { return d.To } |
| 1014 |
func (d *GenDecl) End() token.Pos { |
| 1015 |
if d.Rparen.IsValid() { |
| 1016 |
return d.Rparen + 1 |
| 1017 |
} |
| 1018 |
return d.Specs[0].End() |
| 1019 |
} |
| 1020 |
func (d *FuncDecl) End() token.Pos { |
| 1021 |
if d.Body != nil { |
| 1022 |
return d.Body.End() |
| 1023 |
} |
| 1024 |
return d.Type.End() |
| 1025 |
} |
| 1026 |
|
| 1027 |
// declNode() ensures that only declaration nodes can be |
| 1028 |
// assigned to a Decl. |
| 1029 |
func (*BadDecl) declNode() {} |
| 1030 |
func (*GenDecl) declNode() {} |
| 1031 |
func (*FuncDecl) declNode() {} |
| 1032 |
|
| 1033 |
// ---------------------------------------------------------------------------- |
| 1034 |
// Files and packages |
| 1035 |
|
| 1036 |
// A File node represents a Go source file. |
| 1037 |
// |
| 1038 |
// The Comments list contains all comments in the source file in order of |
| 1039 |
// appearance, including the comments that are pointed to from other nodes |
| 1040 |
// via Doc and Comment fields. |
| 1041 |
// |
| 1042 |
// For correct printing of source code containing comments (using packages |
| 1043 |
// go/format and go/printer), special care must be taken to update comments |
| 1044 |
// when a File's syntax tree is modified: For printing, comments are interspersed |
| 1045 |
// between tokens based on their position. If syntax tree nodes are |
| 1046 |
// removed or moved, relevant comments in their vicinity must also be removed |
| 1047 |
// (from the [File.Comments] list) or moved accordingly (by updating their |
| 1048 |
// positions). A [CommentMap] may be used to facilitate some of these operations. |
| 1049 |
// |
| 1050 |
// Whether and how a comment is associated with a node depends on the |
| 1051 |
// interpretation of the syntax tree by the manipulating program: except for Doc |
| 1052 |
// and [Comment] comments directly associated with nodes, the remaining comments |
| 1053 |
// are "free-floating" (see also issues [#18593], [#20744]). |
| 1054 |
// |
| 1055 |
// [#18593]: https://go.dev/issue/18593 |
| 1056 |
// [#20744]: https://go.dev/issue/20744 |
| 1057 |
type File struct { |
| 1058 |
Doc *CommentGroup // associated documentation; or nil |
| 1059 |
Package token.Pos // position of "package" keyword |
| 1060 |
Name *Ident // package name |
| 1061 |
Decls []Decl // top-level declarations; or nil |
| 1062 |
|
| 1063 |
FileStart, FileEnd token.Pos // start and end of entire file |
| 1064 |
Scope *Scope // package scope (this file only). Deprecated: see Object |
| 1065 |
Imports []*ImportSpec // imports in this file |
| 1066 |
Unresolved []*Ident // unresolved identifiers in this file. Deprecated: see Object |
| 1067 |
Comments []*CommentGroup // list of all comments in the source file |
| 1068 |
GoVersion string // minimum Go version required by //go:build or // +build directives |
| 1069 |
} |
| 1070 |
|
| 1071 |
// Pos returns the position of the package declaration. |
| 1072 |
// It may be invalid, for example in an empty file. |
| 1073 |
// |
| 1074 |
// (Use FileStart for the start of the entire file. It is always valid.) |
| 1075 |
func (f *File) Pos() token.Pos { return f.Package } |
| 1076 |
|
| 1077 |
// End returns the end of the last declaration in the file. |
| 1078 |
// It may be invalid, for example in an empty file. |
| 1079 |
// |
| 1080 |
// (Use FileEnd for the end of the entire file. It is always valid.) |
| 1081 |
func (f *File) End() token.Pos { |
| 1082 |
if n := len(f.Decls); n > 0 { |
| 1083 |
return f.Decls[n-1].End() |
| 1084 |
} |
| 1085 |
return f.Name.End() |
| 1086 |
} |
| 1087 |
|
| 1088 |
// A Package node represents a set of source files |
| 1089 |
// collectively building a Go package. |
| 1090 |
// |
| 1091 |
// Deprecated: use the type checker [go/types] instead; see [Object]. |
| 1092 |
type Package struct { |
| 1093 |
Name string // package name |
| 1094 |
Scope *Scope // package scope across all files |
| 1095 |
Imports map[string]*Object // map of package id -> package object |
| 1096 |
Files map[string]*File // Go source files by filename |
| 1097 |
} |
| 1098 |
|
| 1099 |
func (p *Package) Pos() token.Pos { return token.NoPos } |
| 1100 |
func (p *Package) End() token.Pos { return token.NoPos } |
| 1101 |
|
| 1102 |
// IsGenerated reports whether the file was generated by a program, |
| 1103 |
// not handwritten, by detecting the special comment described |
| 1104 |
// at https://go.dev/s/generatedcode. |
| 1105 |
// |
| 1106 |
// The syntax tree must have been parsed with the [parser.ParseComments] flag. |
| 1107 |
// Example: |
| 1108 |
// |
| 1109 |
// f, err := parser.ParseFile(fset, filename, src, parser.ParseComments|parser.PackageClauseOnly) |
| 1110 |
// if err != nil { ... } |
| 1111 |
// gen := ast.IsGenerated(f) |
| 1112 |
func IsGenerated(file *File) bool { |
| 1113 |
_, ok := generator(file) |
| 1114 |
return ok |
| 1115 |
} |
| 1116 |
|
| 1117 |
func generator(file *File) (string, bool) { |
| 1118 |
for _, group := range file.Comments { |
| 1119 |
for _, comment := range group.List { |
| 1120 |
if comment.Pos() > file.Package { |
| 1121 |
break // after package declaration |
| 1122 |
} |
| 1123 |
// opt: check Contains first to avoid unnecessary array allocation in Split. |
| 1124 |
const prefix = "// Code generated " |
| 1125 |
if strings.Contains(comment.Text, prefix) { |
| 1126 |
for _, line := range strings.Split(comment.Text, "\n") { |
| 1127 |
if rest, ok := strings.CutPrefix(line, prefix); ok { |
| 1128 |
if gen, ok := strings.CutSuffix(rest, " DO NOT EDIT."); ok { |
| 1129 |
return gen, true |
| 1130 |
} |
| 1131 |
} |
| 1132 |
} |
| 1133 |
} |
| 1134 |
} |
| 1135 |
} |
| 1136 |
return "", false |
| 1137 |
} |
| 1138 |
|
| 1139 |
// Unparen returns the expression with any enclosing parentheses removed. |
| 1140 |
func Unparen(e Expr) Expr { |
| 1141 |
for { |
| 1142 |
paren, ok := e.(*ParenExpr) |
| 1143 |
if !ok { |
| 1144 |
return e |
| 1145 |
} |
| 1146 |
e = paren.X |
| 1147 |
} |
| 1148 |
} |