Source file src/go/types/generate_test.go

     1  // Copyright 2023 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  // This file implements a custom generator to create various go/types
     6  // source files from the corresponding types2 files.
     7  
     8  package types_test
     9  
    10  import (
    11  	"bytes"
    12  	"flag"
    13  	"fmt"
    14  	"go/ast"
    15  	"go/format"
    16  	"go/parser"
    17  	"go/token"
    18  	"internal/diff"
    19  	"os"
    20  	"path/filepath"
    21  	"runtime"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  var filesToWrite = flag.String("write", "", `go/types files to generate, or "all" for all files`)
    27  
    28  const (
    29  	srcDir = "/src/cmd/compile/internal/types2/"
    30  	dstDir = "/src/go/types/"
    31  )
    32  
    33  // TestGenerate verifies that generated files in go/types match their types2
    34  // counterpart. If -write is set, this test actually writes the expected
    35  // content to go/types; otherwise, it just compares with the existing content.
    36  func TestGenerate(t *testing.T) {
    37  	// If filesToWrite is set, write the generated content to disk.
    38  	// In the special case of "all", write all files in filemap.
    39  	write := *filesToWrite != ""
    40  	var files []string // files to process
    41  	if *filesToWrite != "" && *filesToWrite != "all" {
    42  		files = strings.Split(*filesToWrite, ",")
    43  	} else {
    44  		for file := range filemap {
    45  			files = append(files, file)
    46  		}
    47  	}
    48  
    49  	for _, filename := range files {
    50  		generate(t, filename, write)
    51  	}
    52  }
    53  
    54  func generate(t *testing.T, filename string, write bool) {
    55  	// parse src (cmd/compile/internal/types2)
    56  	srcFilename := filepath.FromSlash(runtime.GOROOT() + srcDir + filename)
    57  	file, err := parser.ParseFile(fset, srcFilename, nil, parser.ParseComments|parser.SkipObjectResolution)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	// fix package name
    63  	file.Name.Name = strings.ReplaceAll(file.Name.Name, "types2", "types")
    64  
    65  	// rewrite AST as needed
    66  	if action := filemap[filename]; action != nil {
    67  		action(file)
    68  	}
    69  
    70  	// format AST
    71  	var buf bytes.Buffer
    72  	rel, _ := filepath.Rel(dstDir, srcDir)
    73  	fmt.Fprintf(&buf, "// Code generated by \"go test -run=Generate -write=all\"; DO NOT EDIT.\n")
    74  	fmt.Fprintf(&buf, "// Source: %s/%s\n\n", filepath.ToSlash(rel), filename)
    75  	if err := format.Node(&buf, fset, file); err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	generatedContent := buf.Bytes()
    79  
    80  	// read dst (go/types)
    81  	dstFilename := filepath.FromSlash(runtime.GOROOT() + dstDir + filename)
    82  	onDiskContent, err := os.ReadFile(dstFilename)
    83  	if err != nil {
    84  		t.Fatalf("reading %q: %v", filename, err)
    85  	}
    86  
    87  	// compare on-disk dst with buffer generated from src.
    88  	if d := diff.Diff(filename+" (on disk in "+dstDir+")", onDiskContent, filename+" (generated from "+srcDir+")", generatedContent); d != nil {
    89  		if write {
    90  			t.Logf("applying change:\n%s", d)
    91  			if err := os.WriteFile(dstFilename, generatedContent, 0o644); err != nil {
    92  				t.Fatalf("writing %q: %v", filename, err)
    93  			}
    94  		} else {
    95  			t.Errorf("file on disk in %s is stale:\n%s", dstDir, d)
    96  		}
    97  	}
    98  }
    99  
   100  type action func(in *ast.File)
   101  
   102  var filemap = map[string]action{
   103  	"alias.go": fixTokenPos,
   104  	"alias_test.go": func(f *ast.File) {
   105  		renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`)
   106  		renameIdents(f, "types2->types")
   107  	},
   108  	"assignments.go": func(f *ast.File) {
   109  		renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
   110  		renameSelectorExprs(f, "syntax.Name->ast.Ident", "ident.Value->ident.Name", "ast.Pos->token.Pos") // must happen before renaming identifiers
   111  		renameIdents(f, "syntax->ast", "poser->positioner", "nopos->noposn")
   112  	},
   113  	"array.go":          nil,
   114  	"api_predicates.go": nil,
   115  	"basic.go":          nil,
   116  	"builtins.go": func(f *ast.File) {
   117  		renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
   118  		renameIdents(f, "syntax->ast")
   119  		renameSelectors(f, "ArgList->Args")
   120  		fixSelValue(f)
   121  		fixAtPosCall(f)
   122  	},
   123  	"builtins_test.go": func(f *ast.File) {
   124  		renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`, `"cmd/compile/internal/types2"->"go/types"`)
   125  		renameSelectorExprs(f, "syntax.Name->ast.Ident", "p.Value->p.Name") // must happen before renaming identifiers
   126  		renameIdents(f, "syntax->ast")
   127  	},
   128  	"chan.go":         nil,
   129  	"const.go":        fixTokenPos,
   130  	"context.go":      nil,
   131  	"context_test.go": nil,
   132  	"conversions.go":  nil,
   133  	"cycles.go": func(f *ast.File) {
   134  		renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
   135  		renameSelectorExprs(f, "syntax.Name->ast.Ident", "rhs.Value->rhs.Name")
   136  		renameSelectors(f, "Trace->_Trace")
   137  	},
   138  	"errors_test.go":  func(f *ast.File) { renameIdents(f, "nopos->noposn") },
   139  	"errsupport.go":   nil,
   140  	"gccgosizes.go":   nil,
   141  	"gcsizes.go":      func(f *ast.File) { renameIdents(f, "IsSyncAtomicAlign64->_IsSyncAtomicAlign64") },
   142  	"hilbert_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
   143  	"infer.go":        func(f *ast.File) { fixTokenPos(f); fixInferSig(f) },
   144  	"initorder.go":    nil,
   145  	// "initorder.go": fixErrErrorfCall, // disabled for now due to unresolved error_ use implications for gopls
   146  	"instantiate.go":      func(f *ast.File) { fixTokenPos(f); fixCheckErrorfCall(f) },
   147  	"instantiate_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
   148  	"literals.go": func(f *ast.File) {
   149  		insertImportPath(f, `"go/token"`)
   150  		renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
   151  		renameSelectorExprs(f,
   152  			"syntax.IntLit->token.INT", "syntax.FloatLit->token.FLOAT", "syntax.ImagLit->token.IMAG",
   153  			"syntax.Name->ast.Ident", "key.Value->key.Name", "atyp.Elem->atyp.Elt") // must happen before renaming identifiers
   154  		renameIdents(f, "syntax->ast")
   155  		renameSelectors(f, "ElemList->Elts")
   156  	},
   157  	"lookup.go":    fixTokenPos,
   158  	"main_test.go": nil,
   159  	"map.go":       nil,
   160  	"mono.go": func(f *ast.File) {
   161  		fixTokenPos(f)
   162  		insertImportPath(f, `"go/ast"`)
   163  		renameSelectorExprs(f, "syntax.Expr->ast.Expr")
   164  	},
   165  	"named.go":  func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
   166  	"object.go": func(f *ast.File) { fixTokenPos(f); renameIdents(f, "NewTypeNameLazy->_NewTypeNameLazy") },
   167  	// TODO(gri) needs adjustments for TestObjectString - disabled for now
   168  	// "object_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"->"go/types"`) },
   169  	"objset.go": nil,
   170  	"operand.go": func(f *ast.File) {
   171  		insertImportPath(f, `"go/token"`)
   172  		renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
   173  		renameSelectorExprs(f,
   174  			"syntax.Pos->token.Pos", "syntax.LitKind->token.Token",
   175  			"syntax.IntLit->token.INT", "syntax.FloatLit->token.FLOAT",
   176  			"syntax.ImagLit->token.IMAG", "syntax.RuneLit->token.CHAR",
   177  			"syntax.StringLit->token.STRING") // must happen before renaming identifiers
   178  		renameIdents(f, "syntax->ast")
   179  	},
   180  	"package.go":    nil,
   181  	"pointer.go":    nil,
   182  	"predicates.go": nil,
   183  	"range.go": func(f *ast.File) {
   184  		renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
   185  		renameSelectorExprs(f, "syntax.Name->ast.Ident", "syntax.ForStmt->ast.RangeStmt", "ident.Value->ident.Name") // must happen before renaming identifiers
   186  		renameIdents(f, "syntax->ast", "poser->positioner")
   187  	},
   188  	"recording.go": func(f *ast.File) {
   189  		renameImportPath(f, `"cmd/compile/internal/syntax"->"go/ast"`)
   190  		renameSelectorExprs(f, "syntax.Name->ast.Ident") // must happen before renaming identifiers
   191  		renameIdents(f, "syntax->ast")
   192  		fixAtPosCall(f)
   193  	},
   194  	"scope.go":         func(f *ast.File) { fixTokenPos(f); renameIdents(f, "InsertLazy->_InsertLazy") },
   195  	"selection.go":     nil,
   196  	"sizes.go":         func(f *ast.File) { renameIdents(f, "IsSyncAtomicAlign64->_IsSyncAtomicAlign64") },
   197  	"slice.go":         nil,
   198  	"subst.go":         func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
   199  	"termlist.go":      nil,
   200  	"termlist_test.go": nil,
   201  	"trie.go":          nil,
   202  	"trie_test.go":     nil,
   203  	"tuple.go":         nil,
   204  	"typelists.go":     nil,
   205  	"typeset.go":       func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
   206  	"typeparam.go":     nil,
   207  	"typeterm_test.go": nil,
   208  	"typeterm.go":      nil,
   209  	"typestring.go":    nil,
   210  	"under.go":         nil,
   211  	"unify.go":         fixSprintf,
   212  	"universe.go":      fixGlobalTypVarDecl,
   213  	"util_test.go":     fixTokenPos,
   214  	"validtype.go":     func(f *ast.File) { fixTokenPos(f); renameSelectors(f, "Trace->_Trace") },
   215  }
   216  
   217  // TODO(gri) We should be able to make these rewriters more configurable/composable.
   218  //           For now this is a good starting point.
   219  
   220  // A renameMap maps old strings to new strings.
   221  type renameMap map[string]string
   222  
   223  // makeRenameMap returns a renameMap populates from renames entries of the form "from->to".
   224  func makeRenameMap(renames ...string) renameMap {
   225  	m := make(renameMap)
   226  	for _, r := range renames {
   227  		s := strings.Split(r, "->")
   228  		if len(s) != 2 {
   229  			panic("invalid rename entry: " + r)
   230  		}
   231  		m[s[0]] = s[1]
   232  	}
   233  	return m
   234  }
   235  
   236  // rename renames the given string s if a corresponding rename exists in m.
   237  func (m renameMap) rename(s *string) {
   238  	if r, ok := m[*s]; ok {
   239  		*s = r
   240  	}
   241  }
   242  
   243  // renameSel renames a selector expression of the form a.x to b.x (where a, b are identifiers)
   244  // if m contains the ("a.x" : "b.y") key-value pair.
   245  func (m renameMap) renameSel(n *ast.SelectorExpr) {
   246  	if a, _ := n.X.(*ast.Ident); a != nil {
   247  		a_x := a.Name + "." + n.Sel.Name
   248  		if r, ok := m[a_x]; ok {
   249  			b_y := strings.Split(r, ".")
   250  			if len(b_y) != 2 {
   251  				panic("invalid selector expression: " + r)
   252  			}
   253  			a.Name = b_y[0]
   254  			n.Sel.Name = b_y[1]
   255  		}
   256  	}
   257  }
   258  
   259  // renameIdents renames identifiers: each renames entry is of the form "from->to".
   260  // Note: This doesn't change the use of the identifiers in comments.
   261  func renameIdents(f *ast.File, renames ...string) {
   262  	m := makeRenameMap(renames...)
   263  	ast.Inspect(f, func(n ast.Node) bool {
   264  		switch n := n.(type) {
   265  		case *ast.Ident:
   266  			m.rename(&n.Name)
   267  			return false
   268  		}
   269  		return true
   270  	})
   271  }
   272  
   273  // renameSelectors is like renameIdents but only looks at selectors.
   274  func renameSelectors(f *ast.File, renames ...string) {
   275  	m := makeRenameMap(renames...)
   276  	ast.Inspect(f, func(n ast.Node) bool {
   277  		switch n := n.(type) {
   278  		case *ast.SelectorExpr:
   279  			m.rename(&n.Sel.Name)
   280  			return false
   281  		}
   282  		return true
   283  	})
   284  
   285  }
   286  
   287  // renameSelectorExprs is like renameIdents but only looks at selector expressions.
   288  // Each renames entry must be of the form "x.a->y.b".
   289  func renameSelectorExprs(f *ast.File, renames ...string) {
   290  	m := makeRenameMap(renames...)
   291  	ast.Inspect(f, func(n ast.Node) bool {
   292  		switch n := n.(type) {
   293  		case *ast.SelectorExpr:
   294  			m.renameSel(n)
   295  			return false
   296  		}
   297  		return true
   298  	})
   299  }
   300  
   301  // renameImportPath is like renameIdents but renames import paths.
   302  func renameImportPath(f *ast.File, renames ...string) {
   303  	m := makeRenameMap(renames...)
   304  	ast.Inspect(f, func(n ast.Node) bool {
   305  		switch n := n.(type) {
   306  		case *ast.ImportSpec:
   307  			if n.Path.Kind != token.STRING {
   308  				panic("invalid import path")
   309  			}
   310  			m.rename(&n.Path.Value)
   311  			return false
   312  		}
   313  		return true
   314  	})
   315  }
   316  
   317  // insertImportPath inserts the given import path.
   318  // There must be at least one import declaration present already.
   319  func insertImportPath(f *ast.File, path string) {
   320  	for _, d := range f.Decls {
   321  		if g, _ := d.(*ast.GenDecl); g != nil && g.Tok == token.IMPORT {
   322  			g.Specs = append(g.Specs, &ast.ImportSpec{Path: &ast.BasicLit{ValuePos: g.End(), Kind: token.STRING, Value: path}})
   323  			return
   324  		}
   325  	}
   326  	panic("no import declaration present")
   327  }
   328  
   329  // fixTokenPos changes imports of "cmd/compile/internal/syntax" to "go/token",
   330  // uses of syntax.Pos to token.Pos, and calls to x.IsKnown() to x.IsValid().
   331  func fixTokenPos(f *ast.File) {
   332  	m := makeRenameMap(`"cmd/compile/internal/syntax"->"go/token"`, "syntax.Pos->token.Pos", "IsKnown->IsValid")
   333  	ast.Inspect(f, func(n ast.Node) bool {
   334  		switch n := n.(type) {
   335  		case *ast.ImportSpec:
   336  			// rewrite import path "cmd/compile/internal/syntax" to "go/token"
   337  			if n.Path.Kind != token.STRING {
   338  				panic("invalid import path")
   339  			}
   340  			m.rename(&n.Path.Value)
   341  			return false
   342  		case *ast.SelectorExpr:
   343  			// rewrite syntax.Pos to token.Pos
   344  			m.renameSel(n)
   345  		case *ast.CallExpr:
   346  			// rewrite x.IsKnown() to x.IsValid()
   347  			if fun, _ := n.Fun.(*ast.SelectorExpr); fun != nil && len(n.Args) == 0 {
   348  				m.rename(&fun.Sel.Name)
   349  				return false
   350  			}
   351  		}
   352  		return true
   353  	})
   354  }
   355  
   356  // fixSelValue updates the selector x.Sel.Value to x.Sel.Name.
   357  func fixSelValue(f *ast.File) {
   358  	ast.Inspect(f, func(n ast.Node) bool {
   359  		switch n := n.(type) {
   360  		case *ast.SelectorExpr:
   361  			if n.Sel.Name == "Value" {
   362  				if selx, _ := n.X.(*ast.SelectorExpr); selx != nil && selx.Sel.Name == "Sel" {
   363  					n.Sel.Name = "Name"
   364  					return false
   365  				}
   366  			}
   367  		}
   368  		return true
   369  	})
   370  }
   371  
   372  // fixInferSig updates the Checker.infer signature to use a positioner instead of a token.Position
   373  // as first argument, renames the argument from "pos" to "posn", and updates a few internal uses of
   374  // "pos" to "posn" and "posn.Pos()" respectively.
   375  func fixInferSig(f *ast.File) {
   376  	ast.Inspect(f, func(n ast.Node) bool {
   377  		switch n := n.(type) {
   378  		case *ast.FuncDecl:
   379  			if n.Name.Name == "infer" {
   380  				// rewrite (pos token.Pos, ...) to (posn positioner, ...)
   381  				par := n.Type.Params.List[0]
   382  				if len(par.Names) == 1 && par.Names[0].Name == "pos" {
   383  					par.Names[0] = newIdent(par.Names[0].Pos(), "posn")
   384  					par.Type = newIdent(par.Type.Pos(), "positioner")
   385  					return true
   386  				}
   387  			}
   388  		case *ast.CallExpr:
   389  			if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
   390  				switch selx.Sel.Name {
   391  				case "renameTParams":
   392  					// rewrite check.renameTParams(pos, ... ) to check.renameTParams(posn.Pos(), ... )
   393  					if isIdent(n.Args[0], "pos") {
   394  						pos := n.Args[0].Pos()
   395  						fun := &ast.SelectorExpr{X: newIdent(pos, "posn"), Sel: newIdent(pos, "Pos")}
   396  						arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos}
   397  						n.Args[0] = arg
   398  						return false
   399  					}
   400  				case "addf":
   401  					// rewrite err.addf(pos, ...) to err.addf(posn, ...)
   402  					if isIdent(n.Args[0], "pos") {
   403  						pos := n.Args[0].Pos()
   404  						arg := newIdent(pos, "posn")
   405  						n.Args[0] = arg
   406  						return false
   407  					}
   408  				case "allowVersion":
   409  					// rewrite check.allowVersion(pos, ...) to check.allowVersion(posn, ...)
   410  					if isIdent(n.Args[0], "pos") {
   411  						pos := n.Args[0].Pos()
   412  						arg := newIdent(pos, "posn")
   413  						n.Args[0] = arg
   414  						return false
   415  					}
   416  				}
   417  			}
   418  		}
   419  		return true
   420  	})
   421  }
   422  
   423  // fixAtPosCall updates calls of the form atPos(x) to x.Pos() in argument lists of (check).dump calls.
   424  // TODO(gri) can we avoid this and just use atPos consistently in go/types and types2?
   425  func fixAtPosCall(f *ast.File) {
   426  	ast.Inspect(f, func(n ast.Node) bool {
   427  		switch n := n.(type) {
   428  		case *ast.CallExpr:
   429  			if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil && selx.Sel.Name == "dump" {
   430  				for i, arg := range n.Args {
   431  					if call, _ := arg.(*ast.CallExpr); call != nil {
   432  						// rewrite xxx.dump(..., atPos(x), ...) to xxx.dump(..., x.Pos(), ...)
   433  						if isIdent(call.Fun, "atPos") {
   434  							pos := call.Args[0].Pos()
   435  							fun := &ast.SelectorExpr{X: call.Args[0], Sel: newIdent(pos, "Pos")}
   436  							n.Args[i] = &ast.CallExpr{Fun: fun, Lparen: pos, Rparen: pos}
   437  							return false
   438  						}
   439  					}
   440  				}
   441  			}
   442  		}
   443  		return true
   444  	})
   445  }
   446  
   447  // fixErrErrorfCall updates calls of the form err.addf(obj, ...) to err.addf(obj.Pos(), ...).
   448  func fixErrErrorfCall(f *ast.File) {
   449  	ast.Inspect(f, func(n ast.Node) bool {
   450  		switch n := n.(type) {
   451  		case *ast.CallExpr:
   452  			if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
   453  				if isIdent(selx.X, "err") {
   454  					switch selx.Sel.Name {
   455  					case "errorf":
   456  						// rewrite err.addf(obj, ... ) to err.addf(obj.Pos(), ... )
   457  						if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "obj" {
   458  							pos := n.Args[0].Pos()
   459  							fun := &ast.SelectorExpr{X: ident, Sel: newIdent(pos, "Pos")}
   460  							n.Args[0] = &ast.CallExpr{Fun: fun, Lparen: pos, Rparen: pos}
   461  							return false
   462  						}
   463  					}
   464  				}
   465  			}
   466  		}
   467  		return true
   468  	})
   469  }
   470  
   471  // fixCheckErrorfCall updates calls of the form check.errorf(pos, ...) to check.errorf(atPos(pos), ...).
   472  func fixCheckErrorfCall(f *ast.File) {
   473  	ast.Inspect(f, func(n ast.Node) bool {
   474  		switch n := n.(type) {
   475  		case *ast.CallExpr:
   476  			if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
   477  				if isIdent(selx.X, "check") {
   478  					switch selx.Sel.Name {
   479  					case "errorf":
   480  						// rewrite check.errorf(pos, ... ) to check.errorf(atPos(pos), ... )
   481  						if ident := asIdent(n.Args[0], "pos"); ident != nil {
   482  							pos := n.Args[0].Pos()
   483  							fun := newIdent(pos, "atPos")
   484  							n.Args[0] = &ast.CallExpr{Fun: fun, Lparen: pos, Args: []ast.Expr{ident}, Rparen: pos}
   485  							return false
   486  						}
   487  					}
   488  				}
   489  			}
   490  		}
   491  		return true
   492  	})
   493  }
   494  
   495  // fixGlobalTypVarDecl changes the global Typ variable from an array to a slice
   496  // (in types2 we use an array for efficiency, in go/types it's a slice and we
   497  // cannot change that).
   498  func fixGlobalTypVarDecl(f *ast.File) {
   499  	ast.Inspect(f, func(n ast.Node) bool {
   500  		switch n := n.(type) {
   501  		case *ast.ValueSpec:
   502  			// rewrite type Typ = [...]Type{...} to type Typ = []Type{...}
   503  			if len(n.Names) == 1 && n.Names[0].Name == "Typ" && len(n.Values) == 1 {
   504  				n.Values[0].(*ast.CompositeLit).Type.(*ast.ArrayType).Len = nil
   505  				return false
   506  			}
   507  		}
   508  		return true
   509  	})
   510  }
   511  
   512  // fixSprintf adds an extra nil argument for the *token.FileSet parameter in sprintf calls.
   513  func fixSprintf(f *ast.File) {
   514  	ast.Inspect(f, func(n ast.Node) bool {
   515  		switch n := n.(type) {
   516  		case *ast.CallExpr:
   517  			if isIdent(n.Fun, "sprintf") && len(n.Args) >= 4 /* ... args */ {
   518  				n.Args = insert(n.Args, 1, newIdent(n.Args[1].Pos(), "nil"))
   519  				return false
   520  			}
   521  		}
   522  		return true
   523  	})
   524  }
   525  
   526  // asIdent returns x as *ast.Ident if it is an identifier with the given name.
   527  func asIdent(x ast.Node, name string) *ast.Ident {
   528  	if ident, _ := x.(*ast.Ident); ident != nil && ident.Name == name {
   529  		return ident
   530  	}
   531  	return nil
   532  }
   533  
   534  // isIdent reports whether x is an identifier with the given name.
   535  func isIdent(x ast.Node, name string) bool {
   536  	return asIdent(x, name) != nil
   537  }
   538  
   539  // newIdent returns a new identifier with the given position and name.
   540  func newIdent(pos token.Pos, name string) *ast.Ident {
   541  	id := ast.NewIdent(name)
   542  	id.NamePos = pos
   543  	return id
   544  }
   545  
   546  // insert inserts x at list[at] and moves the remaining elements up.
   547  func insert(list []ast.Expr, at int, x ast.Expr) []ast.Expr {
   548  	list = append(list, nil)
   549  	copy(list[at+1:], list[at:])
   550  	list[at] = x
   551  	return list
   552  }
   553  

View as plain text