diff --git a/Makefile b/Makefile index c2e66a579a1..e7b4b260ab0 100644 --- a/Makefile +++ b/Makefile @@ -87,7 +87,7 @@ ql/src/go.dbscheme: tools/$(CODEQL_PLATFORM)/go-extractor$(EXE) build/stats/src.stamp: mkdir -p $(@D)/src git clone 'https://github.com/golang/tools' $(@D)/src - git -C $(@D)/src checkout ac4f524c1612 -q + git -C $(@D)/src checkout 9b52d559c609 -q touch $@ ql/src/go.dbscheme.stats: ql/src/go.dbscheme build/stats/src.stamp extractor diff --git a/extractor/dbscheme/tables.go b/extractor/dbscheme/tables.go index 2a978bc5c18..a367a7de18f 100644 --- a/extractor/dbscheme/tables.go +++ b/extractor/dbscheme/tables.go @@ -58,6 +58,9 @@ var DocumentableType = NewUnionType("@documentable", NodeType) // ExprParentType is the type of AST nodes that can have expressions as children var ExprParentType = NewUnionType("@exprparent", NodeType) +// ModExprParentType is the type of go.mod nodes that can have go.mod expressions as children +var ModExprParentType = NewUnionType("@modexprparent", NodeType) + // FieldParentType is the type of AST nodes that can have fields as children var FieldParentType = NewUnionType("@fieldparent", NodeType) @@ -71,14 +74,14 @@ var DeclParentType = NewUnionType("@declparent", NodeType) // declarations and function literals var FuncDefType = NewUnionType("@funcdef", StmtParentType, ExprParentType) -// ScopeNodeType is the type of AST nodes that mapy have a scope attached to them +// ScopeNodeType is the type of AST nodes that may have a scope attached to them var ScopeNodeType = NewUnionType("@scopenode", NodeType) // LocationDefaultType is the type of source locations var LocationDefaultType = NewPrimaryKeyType("@location_default") // FileType is the type of file AST nodes -var FileType = NewPrimaryKeyType("@file", ContainerType, DocumentableType, ExprParentType, DeclParentType, ScopeNodeType) +var FileType = NewPrimaryKeyType("@file", ContainerType, DocumentableType, ExprParentType, ModExprParentType, DeclParentType, ScopeNodeType) // FolderType is the type of folders var FolderType = NewPrimaryKeyType("@folder", ContainerType) @@ -618,6 +621,27 @@ var NamedType = TypeKind.NewBranch("@namedtype", CompositeType) // PackageType is the type of packages var PackageType = NewPrimaryKeyType("@package") +// ModExprType is the type of go.mod expression nodes +var ModExprType = NewPrimaryKeyType("@modexpr", ModExprParentType, DocumentableType) + +// ModExprKind is a case type for distinguishing different kinds of go.mod expression nodes +var ModExprKind = NewCaseType(ModExprType, "kind") + +// ModCommentBlockType is the type of go.mod comment block AST nodes +var ModCommentBlockType = ModExprKind.NewBranch("@modcommentblock") + +// ModLineType is the type of go.mod line AST nodes +var ModLineType = ModExprKind.NewBranch("@modline") + +// ModLineBlockType is the type of go.mod line block AST nodes +var ModLineBlockType = ModExprKind.NewBranch("@modlineblock") + +// ModLParenType is the type of go.mod line block start AST nodes +var ModLParenType = ModExprKind.NewBranch("@modlparen") + +// ModRParenType is the type of go.mod line block end AST nodes +var ModRParenType = ModExprKind.NewBranch("@modrparen") + // LocationsDefaultTable is the table defining location objects var LocationsDefaultTable = NewTable("locations_default", EntityColumn(LocationDefaultType, "id").Key(), @@ -684,7 +708,7 @@ var DocCommentsTable = NewTable("doc_comments", EntityColumn(CommentGroupType, "comment"), ) -// ExprsTable is the table defininig expression AST nodes +// ExprsTable is the table defining expression AST nodes var ExprsTable = NewTable("exprs", EntityColumn(ExprType, "id").Key(), IntColumn("kind"), @@ -876,3 +900,18 @@ var PackagesTable = NewTable("packages", StringColumn("path"), EntityColumn(PackageScopeType, "scope"), ) + +// ModExprsTable is the table defining expression AST nodes for go.mod files +var ModExprsTable = NewTable("modexprs", + EntityColumn(ModExprType, "id").Key(), + IntColumn("kind"), + EntityColumn(ModExprParentType, "parent"), + IntColumn("idx"), +).KeySet("parent", "idx") + +// ModTokensTable is the table associating go.mod tokens with their Line or LineBlock +var ModTokensTable = NewTable("modtokens", + StringColumn("token"), + EntityColumn(ModExprType, "parent"), + IntColumn("idx"), +).KeySet("parent", "idx") diff --git a/extractor/extractor.go b/extractor/extractor.go index c1169044000..ca99c51e87a 100644 --- a/extractor/extractor.go +++ b/extractor/extractor.go @@ -111,6 +111,45 @@ func ExtractWithFlags(buildFlags []string, patterns []string) error { wg.Wait() + cwd, err := os.Getwd() + if err != nil { + log.Printf("Warning: unable to get working directory: %s", err.Error()) + log.Println("Skipping go.mod extraction") + } + rcwd, err := filepath.EvalSymlinks(cwd) + if err == nil { + cwd = rcwd + } + + log.Printf("Walking file tree from %s to discover go.mod files...", cwd) + + goModPaths := make([]string, 0, 10) + + filepath.Walk(cwd, func(path string, info os.FileInfo, err error) error { + if filepath.Base(path) == "go.mod" && info != nil && info.Mode().IsRegular() { + if err != nil { + log.Printf("Found go.mod with path %s, but encountered error %s", path, err.Error()) + } + + goModPaths = append(goModPaths, path) + } + + return nil + }) + + for _, path := range goModPaths { + log.Printf("Extracting %s", path) + start := time.Now() + + err := extractGoMod(path) + if err != nil { + log.Printf("Failed to extract go.mod: %s", err.Error()) + } + + end := time.Since(start) + log.Printf("Done extracting %s (%dms)", path, end.Nanoseconds()/1000000) + } + return nil } diff --git a/extractor/gomodextractor.go b/extractor/gomodextractor.go new file mode 100644 index 00000000000..a59c076eb71 --- /dev/null +++ b/extractor/gomodextractor.go @@ -0,0 +1,131 @@ +package extractor + +import ( + "fmt" + "golang.org/x/mod/modfile" + "io/ioutil" + "log" + "os" + "path/filepath" + + "github.com/github/codeql-go/extractor/dbscheme" + "github.com/github/codeql-go/extractor/srcarchive" + "github.com/github/codeql-go/extractor/trap" +) + +func extractGoMod(path string) error { + if normPath, err := filepath.EvalSymlinks(path); err == nil { + path = normPath + } + + tw, err := trap.NewWriter(path, nil) + if err != nil { + return err + } + defer tw.Close() + + err = srcarchive.Add(path) + if err != nil { + return err + } + + extractFileInfo(tw, path) + + file, err := os.Open(path) + if err != nil { + return fmt.Errorf("failed to open go.mod file %s: %s", path, err.Error()) + } + data, err := ioutil.ReadAll(file) + if err != nil { + return fmt.Errorf("failed to read go.mod file %s: %s", path, err.Error()) + } + + modfile, err := modfile.Parse(path, data, nil) + if err != nil { + return fmt.Errorf("failed to parse go.mod file %s: %s", path, err.Error()) + } + + extractGoModFile(tw, modfile.Syntax) + + return nil +} + +func extractGoModFile(tw *trap.Writer, file *modfile.FileSyntax) { + for idx, stmt := range file.Stmt { + extractGoModExpr(tw, stmt, tw.Labeler.FileLabel(), idx) + } + + extractGoModComments(tw, file, tw.Labeler.FileLabel()) +} + +func extractGoModExpr(tw *trap.Writer, expr modfile.Expr, parent trap.Label, idx int) { + lbl := tw.Labeler.LocalID(expr) + + var kind int + switch expr := expr.(type) { + case *modfile.CommentBlock: + kind = dbscheme.ModCommentBlockType.Index() + case *modfile.LParen: + kind = dbscheme.ModLParenType.Index() + case *modfile.RParen: + kind = dbscheme.ModRParenType.Index() + case *modfile.Line: + kind = dbscheme.ModLineType.Index() + for idx, tok := range expr.Token { + dbscheme.ModTokensTable.Emit(tw, tok, lbl, idx) + } + case *modfile.LineBlock: + kind = dbscheme.ModLineBlockType.Index() + for idx, tok := range expr.Token { + dbscheme.ModTokensTable.Emit(tw, tok, lbl, idx) + } + extractGoModExpr(tw, &expr.LParen, lbl, 0) + for idx, line := range expr.Line { + extractGoModExpr(tw, line, lbl, idx+1) + } + extractGoModExpr(tw, &expr.RParen, lbl, len(expr.Line)+1) + default: + log.Fatalf("unknown go.mod expression of type %T", expr) + } + + dbscheme.ModExprsTable.Emit(tw, lbl, kind, parent, idx) + + extractGoModComments(tw, expr, lbl) + + start, end := expr.Span() + extractLocation(tw, lbl, start.Line, start.LineRune, end.Line, end.LineRune) +} + +type GoModExprCommentWrapper struct { + expr modfile.Expr +} + +func extractGoModComments(tw *trap.Writer, expr modfile.Expr, exprlbl trap.Label) { + // extract a pseudo `@commentgroup` for each expr that contains their associated comments + grouplbl := tw.Labeler.LocalID(GoModExprCommentWrapper{expr}) + dbscheme.CommentGroupsTable.Emit(tw, grouplbl) + dbscheme.DocCommentsTable.Emit(tw, exprlbl, grouplbl) + + comments := expr.Comment() + + idx := 0 + for _, comment := range comments.Before { + extractGoModComment(tw, comment, grouplbl, idx) + idx++ + } + for _, comment := range comments.Suffix { + extractGoModComment(tw, comment, grouplbl, idx) + idx++ + } + for _, comment := range comments.After { + extractGoModComment(tw, comment, grouplbl, idx) + idx++ + } +} + +func extractGoModComment(tw *trap.Writer, comment modfile.Comment, grouplbl trap.Label, idx int) { + lbl := tw.Labeler.LocalID(comment) + dbscheme.CommentsTable.Emit(tw, lbl, dbscheme.SlashSlashComment.Index(), grouplbl, idx, comment.Token) + + extractLocation(tw, lbl, comment.Start.Line, comment.Start.LineRune, comment.Start.Line, comment.Start.LineRune+len(comment.Token)) +} diff --git a/extractor/trap/labels.go b/extractor/trap/labels.go index 3ebe9c35143..9c63f76dbe0 100644 --- a/extractor/trap/labels.go +++ b/extractor/trap/labels.go @@ -2,7 +2,6 @@ package trap import ( "fmt" - "go/ast" "go/types" ) @@ -25,7 +24,7 @@ type Labeler struct { nextid int fileLabel Label - nodeLabels map[ast.Node]Label // labels associated with AST nodes + nodeLabels map[interface{}]Label // labels associated with AST nodes scopeLabels map[*types.Scope]Label // labels associated with scopes objectLabels map[types.Object]Label // labels associated with objects (that is, declared entities) TypeLabels map[types.Type]Label // labels associated with types @@ -37,7 +36,7 @@ func newLabeler(tw *Writer) *Labeler { tw, 10000, InvalidLabel, - make(map[ast.Node]Label), + make(map[interface{}]Label), make(map[*types.Scope]Label), make(map[types.Object]Label), make(map[types.Type]Label), @@ -72,7 +71,7 @@ func (l *Labeler) FileLabel() Label { } // LocalID associates a label with the given AST node `nd` and returns it -func (l *Labeler) LocalID(nd ast.Node) Label { +func (l *Labeler) LocalID(nd interface{}) Label { label, exists := l.nodeLabels[nd] if !exists { label = l.FreshID() @@ -162,7 +161,7 @@ func (l *Labeler) ScopedObjectID(object types.Object, typelbl Label) (Label, boo if !isRecv { scopeLbl := l.ScopeID(scope, object.Pkg()) - label = l.GlobalID(fmt.Sprintf("{%s},%s;object", scopeLbl.String(), object.Name())) + label = l.GlobalID(fmt.Sprintf("{%v},%s;object", scopeLbl, object.Name())) } } l.objectLabels[object] = label @@ -177,7 +176,7 @@ func (l *Labeler) ReceiverObjectID(object types.Object, methlbl Label) (Label, b label, exists := l.objectLabels[object] if !exists { // if we can't, construct a special label - label = l.GlobalID(fmt.Sprintf("{%s},%s;receiver", methlbl.String(), object.Name())) + label = l.GlobalID(fmt.Sprintf("{%v},%s;receiver", methlbl, object.Name())) l.objectLabels[object] = label } return label, exists @@ -197,7 +196,7 @@ func (l *Labeler) FieldID(field *types.Var, idx int, structlbl Label) (Label, bo if field.Name() == "_" { name = fmt.Sprintf("_%d", idx) } - label = l.GlobalID(fmt.Sprintf("{%s},%s;field", structlbl.String(), name)) + label = l.GlobalID(fmt.Sprintf("{%v},%s;field", structlbl, name)) l.objectLabels[field] = label } return label, exists @@ -209,7 +208,7 @@ func (l *Labeler) FieldID(field *types.Var, idx int, structlbl Label) (Label, bo func (l *Labeler) MethodID(method types.Object, recvlbl Label) (Label, bool) { label, exists := l.objectLabels[method] if !exists { - label = l.GlobalID(fmt.Sprintf("{%s},%s;method", recvlbl, method.Name())) + label = l.GlobalID(fmt.Sprintf("{%v},%s;method", recvlbl, method.Name())) l.objectLabels[method] = label } return label, exists diff --git a/go.mod b/go.mod index 1e7a52ebaab..06ecc65d84a 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/github/codeql-go go 1.13 -require golang.org/x/tools v0.0.0-20200109174759-ac4f524c1612 +require ( + golang.org/x/mod v0.2.0 + golang.org/x/tools v0.0.0-20200302225559-9b52d559c609 +) diff --git a/go.sum b/go.sum index 0759dfc8c7b..ca1e117d5de 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,17 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20200109174759-ac4f524c1612 h1:wRxHHuBMuDzijfZQMAgmVpDDTra91XF84qmoVTyj+U0= -golang.org/x/tools v0.0.0-20200109174759-ac4f524c1612/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200302225559-9b52d559c609 h1:3/QY44rOqJoMLCsQz9bAgInYa08qsu+dH52Uk4DWH3w= +golang.org/x/tools v0.0.0-20200302225559-9b52d559c609/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/ql/src/go.dbscheme b/ql/src/go.dbscheme index f1263a74534..b9a2082d22a 100644 --- a/ql/src/go.dbscheme +++ b/ql/src/go.dbscheme @@ -118,16 +118,25 @@ type_objects(unique int tp: @type ref, int object: @object ref); packages(unique int id: @package, string name: string ref, string path: string ref, int scope: @packagescope ref); +#keyset[parent, idx] +modexprs(unique int id: @modexpr, int kind: int ref, int parent: @modexprparent ref, int idx: int ref); + +#keyset[parent, idx] +modtokens(string token: string ref, int parent: @modexpr ref, int idx: int ref); + @container = @file | @folder; @locatable = @node | @localscope; -@node = @documentable | @exprparent | @fieldparent | @stmtparent | @declparent | @scopenode | @comment_group | @comment; +@node = @documentable | @exprparent | @modexprparent | @fieldparent | @stmtparent | @declparent | @scopenode + | @comment_group | @comment; -@documentable = @file | @field | @spec | @gendecl | @funcdecl; +@documentable = @file | @field | @spec | @gendecl | @funcdecl | @modexpr; @exprparent = @funcdef | @file | @expr | @field | @stmt | @decl | @spec; +@modexprparent = @file | @modexpr; + @fieldparent = @decl | @structtypeexpr | @functypeexpr | @interfacetypeexpr; @stmtparent = @funcdef | @stmt | @decl; @@ -402,3 +411,10 @@ case @type.kind of @chantype = @sendchantype | @recvchantype | @sendrcvchantype; +case @modexpr.kind of + 0 = @modcommentblock +| 1 = @modline +| 2 = @modlineblock +| 3 = @modlparen +| 4 = @modrparen; + diff --git a/ql/src/go.dbscheme.stats b/ql/src/go.dbscheme.stats index 74eefcc8e27..ee906be43bd 100644 --- a/ql/src/go.dbscheme.stats +++ b/ql/src/go.dbscheme.stats @@ -14,39 +14,39 @@ @location_default -510886 +534814 @file -500 +523 @folder -196 +223 @comment_group -11598 +12042 @slashslashcomment -24157 +24813 @slashstarcomment -794 +846 @ident -224564 +236050 @ellipsis -137 +145 @intlit -7561 +7667 @floatlit @@ -54,83 +54,79 @@ @charlit -929 +837 @stringlit -24326 +24857 @funclit -604 +676 @compositelit -2452 +2672 @parenexpr -345 +343 @selectorexpr -50875 +53998 @indexexpr -4397 +4563 @sliceexpr -852 +833 @typeassertexpr -2060 +2125 @callorconversionexpr -30447 +31885 @starexpr -9905 +10346 @keyvalueexpr -5224 +5543 @arraytypeexpr -3254 +3448 @structtypeexpr -1129 +1209 @functypeexpr -5697 +6018 @interfacetypeexpr -462 +506 @maptypeexpr -913 - - -@plusexpr -8 +1008 @minusexpr -270 +268 @notexpr -1116 +1186 @complementexpr @@ -138,15 +134,15 @@ @addressexpr -1637 +1733 @arrowexpr -70 +87 @lorexpr -625 +611 @landexpr @@ -154,39 +150,39 @@ @eqlexpr -3156 +3211 @neqexpr -3850 +4073 @lssexpr -778 +782 @leqexpr -281 +245 @gtrexpr -599 +615 @geqexpr -266 +270 @addexpr -1242 +1267 @subexpr -571 +553 @orexpr -130 +147 @xorexpr @@ -206,7 +202,7 @@ @shlexpr -160 +164 @shrexpr @@ -214,7 +210,7 @@ @andexpr -230 +235 @andnotexpr @@ -230,7 +226,7 @@ @sendrcvchantypeexpr -90 +100 @badexpr @@ -241,48 +237,52 @@ 0 +@plusexpr +0 + + @derefexpr 0 @field -18766 +19939 @declstmt -1396 +1448 @labeledstmt -45 +46 @exprstmt -7242 +7568 @sendstmt -59 +68 @incstmt -607 +613 @decstmt -72 +71 @gostmt -69 +71 @deferstmt -318 +356 @returnstmt -8728 +9160 @breakstmt @@ -290,7 +290,7 @@ @continuestmt -577 +600 @gotostmt @@ -302,55 +302,55 @@ @blockstmt -18460 +19269 @ifstmt -9241 +9657 @caseclause -3396 +3463 @exprswitchstmt -370 +374 @typeswitchstmt -384 +397 @commclause -51 +68 @selectstmt -26 +33 @forstmt -648 +652 @rangestmt -2036 +2126 @assignstmt -7248 +7458 @definestmt -8949 +9450 @addassignstmt -212 +222 @subassignstmt -19 +18 @mulassignstmt @@ -358,11 +358,11 @@ @quoassignstmt -2 +3 @orassignstmt -61 +63 @xorassignstmt @@ -398,23 +398,23 @@ @importdecl -464 +479 @constdecl -265 +279 @typedecl -1288 +1351 @vardecl -1702 +1762 @funcdecl -4625 +4820 @baddecl @@ -422,15 +422,15 @@ @importspec -3315 +3453 @valuespec -2939 +3046 @typespec -1304 +1367 @universescope @@ -438,19 +438,19 @@ @packagescope -330 +339 @localscope -34776 +36240 @pkgobject -3315 +3453 @decltypeobject -3431 +3501 @builtintypeobject @@ -458,7 +458,7 @@ @declconstobject -8264 +8487 @builtinconstobject @@ -466,11 +466,11 @@ @declvarobject -48189 +50205 @declfunctionobject -16817 +17259 @builtinfunctionobject @@ -478,7 +478,7 @@ @labelobject -45 +46 @invalidtype @@ -582,35 +582,35 @@ @arraytype -286 +291 @slicetype -595 +618 @structtype -2291 +2338 @pointertype -1821 +1860 @interfacetype -226 +232 @tupletype -527 +556 @signaturetype -7532 +7762 @maptype -397 +415 @sendchantype @@ -626,7 +626,7 @@ @namedtype -3402 +3473 @complexliteraltype @@ -634,7 +634,27 @@ @package -330 +339 + + +@modline +32 + + +@modlineblock +2 + + +@modlparen +2 + + +@modrparen +2 + + +@modcommentblock +0 @@ -1322,23 +1342,23 @@ locations_default -510886 +534814 id -510886 +534814 file -500 +523 beginLine -10316 +10312 beginColumn -201 +200 endLine @@ -1346,7 +1366,7 @@ endColumn -253 +255 @@ -1360,7 +1380,7 @@ 1 2 -510886 +534814 @@ -1376,7 +1396,7 @@ 1 2 -510886 +534814 @@ -1392,7 +1412,7 @@ 1 2 -510886 +534814 @@ -1408,7 +1428,7 @@ 1 2 -510886 +534814 @@ -1424,7 +1444,7 @@ 1 2 -510886 +534814 @@ -1438,74 +1458,74 @@ 12 -3 -45 -38 +2 +30 +40 -45 -140 -38 +30 +122 +40 -141 -225 -38 +122 +201 +41 -228 -301 -38 +207 +288 +40 -301 -377 -38 +289 +374 +40 -377 -479 -38 +376 +462 +40 -483 -590 -38 +463 +593 +40 -592 +593 787 -38 +40 801 -1004 -38 +1014 +40 -1006 -1323 -38 +1014 +1357 +40 -1324 -1660 -38 +1362 +1723 +40 -1692 -2669 -38 +1725 +2874 +40 -2676 -6473 -38 +2901 +10813 +40 -6917 +10827 11207 -6 +2 @@ -1520,68 +1540,68 @@ 2 -16 -39 +12 +41 -16 -31 +12 +27 +41 + + +27 +45 +43 + + +45 +60 40 -31 -49 -39 +60 +75 +41 -49 -63 -39 - - -63 -80 +75 +93 40 -80 -100 +93 +121 40 -100 -125 -39 +121 +152 +41 -125 -155 -38 - - -155 +153 190 -38 +40 -191 -246 -38 +193 +250 +41 -246 +254 337 -38 +40 340 -544 -38 +555 +40 -556 +560 10233 -34 +35 @@ -1595,39 +1615,39 @@ 12 -3 -17 -38 +2 +11 +41 -17 -37 -39 +11 +34 +40 -37 +34 46 -38 +44 46 52 -38 +42 52 -58 -38 +59 +43 -58 +59 63 -43 +42 63 68 -46 +44 68 @@ -1637,26 +1657,26 @@ 73 80 -42 +43 80 86 -42 - - -86 -97 40 -97 -113 -38 +86 +98 +43 -113 -153 +98 +115 +43 + + +115 +157 15 @@ -1672,73 +1692,68 @@ 2 -17 -38 +13 +40 -17 -36 -41 +13 +32 +43 -36 -58 -38 +32 +52 +40 -58 -76 -38 +52 +73 +43 -76 -93 -38 +73 +92 +42 -93 +92 115 -38 +40 115 -143 -38 +147 +40 -144 -183 -38 +147 +189 +40 -183 -221 -38 +190 +230 +40 -223 -282 -38 +230 +312 +40 -282 -361 -38 +316 +398 +40 -363 -594 -38 +400 +679 +40 -615 -1715 -38 - - -2166 +685 10377 -3 +35 @@ -1752,69 +1767,69 @@ 12 -3 -26 -38 - - -26 -50 -43 - - -50 -60 -38 - - -60 -66 +2 +18 40 -66 -72 +19 +45 42 -72 -77 +46 +59 44 -77 +59 +65 +41 + + +65 +71 +41 + + +71 +76 +47 + + +76 81 -37 - - -81 -85 -42 - - -85 -91 38 -91 -97 -40 +81 +84 +35 -97 +84 +89 +42 + + +89 +96 +46 + + +96 105 -40 +44 105 117 -38 +40 117 -162 -20 +166 +23 @@ -1830,32 +1845,32 @@ 1 2 -6993 +6869 2 -8 -854 +7 +841 -8 -23 -793 +7 +20 +794 -23 -102 -777 +20 +80 +778 -102 -1085 +80 +681 774 -1085 -1790 -125 +683 +1764 +256 @@ -1871,32 +1886,32 @@ 1 2 -7077 +6947 2 3 -771 +871 3 6 -898 +900 6 25 -790 +784 25 -401 +315 774 -428 -501 -6 +315 +524 +36 @@ -1912,32 +1927,32 @@ 1 2 -7012 +6895 2 -6 -925 +5 +788 -6 -15 -782 +5 +11 +781 -15 -39 -785 +11 +30 +794 -39 -92 -778 +30 +72 +791 -92 -112 -34 +72 +110 +263 @@ -1953,22 +1968,27 @@ 1 2 -7727 +7635 2 3 -971 +1017 3 -7 -874 +6 +774 -7 -313 -744 +6 +21 +793 + + +21 +318 +93 @@ -1984,32 +2004,32 @@ 1 2 -6994 +6871 2 -6 -845 +5 +793 -6 -15 -819 - - -15 -43 +5 +12 778 -43 -96 -778 +12 +33 +782 -96 -124 -102 +33 +80 +782 + + +80 +115 +306 @@ -2025,68 +2045,63 @@ 1 2 -23 +22 2 -3 +4 +16 + + +4 +10 17 -3 -10 +11 +25 16 -10 -22 +26 +68 +17 + + +69 +174 16 -24 -56 +179 +351 16 -56 -146 +369 +873 16 -153 -302 +885 +2052 16 -319 -712 +2170 +4274 16 -827 -1763 +4466 +9665 16 -1950 -3919 +10997 +55301 16 - -4055 -9060 -16 - - -9151 -44250 -16 - - -52821 -52822 -1 - @@ -2101,61 +2116,61 @@ 1 2 -32 +28 2 5 -16 +18 5 -12 -15 - - -12 -24 +13 16 -25 -52 -16 - - -54 -92 -16 - - -97 -178 -16 - - -184 -286 -16 - - -286 -376 -16 - - -380 -420 -16 - - -420 -447 +13 +28 17 -447 -501 +29 +60 +16 + + +60 +101 +16 + + +118 +192 +16 + + +203 +298 +16 + + +312 +387 +16 + + +388 +431 +16 + + +431 +458 +16 + + +458 +524 9 @@ -2172,62 +2187,62 @@ 1 2 -31 +28 2 5 -16 - - -5 -13 -18 - - -13 -29 17 -33 -77 +5 +15 +18 + + +16 +31 +17 + + +31 +76 16 -77 -147 +83 +152 16 -160 -301 +163 +310 16 -315 -578 +342 +591 16 -584 -837 +598 +852 16 -864 -1228 +882 +1236 16 -1233 -1705 +1251 +1731 16 -1716 -10181 -7 +1745 +10182 +8 @@ -2243,62 +2258,62 @@ 1 2 -31 +28 2 5 -16 +17 5 -12 -14 +15 +17 -12 -24 +15 +30 16 -25 -51 +30 +67 16 -51 -117 +69 +147 16 -125 -259 +147 +291 16 -261 -471 +307 +553 16 -472 -768 +583 +827 16 -781 -1102 +839 +1231 16 -1130 -1523 +1236 +1567 16 -1523 -10177 -12 +1598 +10178 +10 @@ -2314,62 +2329,67 @@ 1 2 -25 +24 2 3 -18 +14 3 -7 +6 +15 + + +6 +12 16 -7 -13 -16 - - -13 +12 20 -17 - - -20 -28 -16 - - -28 -36 -16 - - -36 -45 -17 - - -45 -61 -16 - - -61 -80 18 -80 -103 +21 +28 +15 + + +28 +34 16 -105 -197 -10 +34 +44 +16 + + +44 +55 +16 + + +55 +74 +16 + + +75 +99 +16 + + +99 +137 +16 + + +176 +201 +2 @@ -2385,32 +2405,32 @@ 1 2 -6843 +6731 2 6 -822 +902 6 -15 -795 +16 +809 -15 -58 -785 +16 +57 +786 -58 -439 +57 +428 779 -440 -1749 -354 +430 +1727 +371 @@ -2426,32 +2446,32 @@ 1 2 -6964 +6847 2 3 -721 +812 3 5 -887 +784 5 -19 -800 +14 +802 -19 -153 +14 +103 780 -153 -501 -226 +103 +524 +353 @@ -2467,22 +2487,27 @@ 1 2 -7859 +7769 2 3 -897 +946 3 7 -849 +877 7 -32 -773 +28 +783 + + +28 +31 +3 @@ -2498,32 +2523,32 @@ 1 2 -6857 +6750 2 -5 -914 +4 +617 -5 -11 -793 +4 +8 +878 -11 -30 -788 +8 +21 +791 -30 -73 -781 +21 +53 +790 -73 -110 -245 +53 +106 +552 @@ -2539,32 +2564,32 @@ 1 2 -6903 +6792 2 5 -862 +938 5 12 -807 +797 12 -35 -798 +34 +790 -35 -85 -781 +34 +82 +785 -85 -123 -227 +82 +115 +276 @@ -2590,52 +2615,47 @@ 3 7 -21 +22 -7 -28 -19 +8 +33 +20 -28 -76 -19 +34 +95 +20 -76 -181 -19 +96 +253 +20 -232 -651 -19 +254 +812 +20 -675 -2111 -19 +915 +2385 +20 -2168 -4225 -19 +2509 +5166 +20 -4277 -7772 -19 +5223 +9871 +20 -8110 -13229 -19 - - -13259 -19479 -7 +10015 +20186 +20 @@ -2651,57 +2671,52 @@ 1 2 -70 +74 2 -4 +6 22 -4 -13 +6 +17 +21 + + +21 +47 +22 + + +55 +122 20 -13 -32 -19 +127 +286 +20 -37 -88 -19 +287 +386 +20 -88 -193 -19 - - -194 -346 -19 - - -349 -406 -19 - - -407 +390 437 20 -437 -465 -19 +438 +456 +20 -469 -501 -7 +461 +524 +16 @@ -2717,57 +2732,52 @@ 1 2 -72 +74 2 -4 +6 +23 + + +6 +22 +20 + + +22 +58 +20 + + +58 +154 21 -4 -14 -19 +158 +437 +20 -14 -35 -19 +442 +788 +20 -37 -105 -19 +807 +1054 +20 -105 -281 -19 +1094 +1670 +20 -315 -661 -19 - - -679 -892 -19 - - -916 -1273 -19 - - -1345 -1943 -19 - - -1984 -2347 -8 +1706 +2367 +17 @@ -2783,62 +2793,57 @@ 1 2 -17 +21 2 3 -59 +55 3 7 -21 +23 7 -15 -19 - - -15 -22 +16 20 -22 -31 -19 +16 +25 +22 -31 -41 -19 - - -41 -51 -19 - - -51 -64 -19 - - -64 -73 +25 +35 20 -73 -106 -19 +35 +46 +21 -115 -171 -2 +46 +56 +20 + + +56 +70 +22 + + +70 +77 +20 + + +77 +173 +11 @@ -2854,57 +2859,52 @@ 1 2 -72 +74 2 -4 +6 +23 + + +6 +22 +20 + + +22 +57 +20 + + +58 +153 21 -4 -14 -19 +157 +426 +20 -14 -35 -19 +439 +770 +20 -35 -103 -19 +788 +1047 +20 -103 -279 -19 +1083 +1665 +20 -309 -650 -19 - - -672 -885 -19 - - -907 -1297 -19 - - -1333 -1936 -19 - - -1978 -2341 -8 +1701 +2359 +17 @@ -2914,23 +2914,23 @@ numlines -500 +514 element_id -500 +514 num_lines -307 +312 num_code -330 +347 num_comment -149 +150 @@ -2944,7 +2944,7 @@ 1 2 -500 +514 @@ -2960,7 +2960,7 @@ 1 2 -500 +514 @@ -2976,7 +2976,7 @@ 1 2 -500 +514 @@ -2997,17 +2997,17 @@ 2 3 -68 +72 3 4 -32 +29 4 7 -18 +22 @@ -3023,12 +3023,43 @@ 1 2 -196 +194 2 3 -68 +76 + + +3 +4 +27 + + +4 +7 +15 + + + + + + +num_lines +num_comment + + +12 + + +1 +2 +194 + + +2 +3 +75 3 @@ -3045,37 +3076,6 @@ -num_lines -num_comment - - -12 - - -1 -2 -196 - - -2 -3 -70 - - -3 -4 -27 - - -4 -6 -14 - - - - - - num_code element_id @@ -3085,22 +3085,22 @@ 1 2 -226 +248 2 3 -71 +64 3 5 -26 +30 -5 +6 18 -7 +5 @@ -3116,22 +3116,17 @@ 1 2 -230 +251 2 3 -73 +69 3 -5 -25 - - -5 18 -2 +27 @@ -3147,17 +3142,17 @@ 1 2 -232 +254 2 3 -72 +65 3 -6 -25 +7 +27 17 @@ -3178,37 +3173,37 @@ 1 2 -67 +65 2 3 -27 +29 3 4 -12 +10 4 5 -10 +14 5 -6 -10 - - -6 -10 +7 12 -10 -30 -11 +7 +11 +12 + + +11 +32 +8 @@ -3224,37 +3219,37 @@ 1 2 -67 +65 2 3 -27 +29 3 4 -12 +10 4 5 -12 +15 5 -6 -9 +7 +12 -6 -10 -13 +7 +11 +12 -10 -23 -9 +12 +26 +7 @@ -3270,42 +3265,37 @@ 1 2 -68 +66 2 3 -26 +28 3 4 -12 +10 4 5 -12 +15 5 -6 -8 +7 +11 -6 -9 -9 +7 +11 +13 -9 -17 -12 - - -21 +11 24 -2 +7 @@ -3315,23 +3305,23 @@ files -500 +523 id -500 +523 name -500 +523 simple -357 +365 ext -1 +2 fromSource @@ -3349,7 +3339,7 @@ 1 2 -500 +523 @@ -3365,7 +3355,7 @@ 1 2 -500 +523 @@ -3381,7 +3371,7 @@ 1 2 -500 +523 @@ -3397,7 +3387,7 @@ 1 2 -500 +523 @@ -3413,7 +3403,7 @@ 1 2 -500 +523 @@ -3429,7 +3419,7 @@ 1 2 -500 +523 @@ -3445,7 +3435,7 @@ 1 2 -500 +523 @@ -3461,7 +3451,7 @@ 1 2 -500 +523 @@ -3477,17 +3467,17 @@ 1 2 -293 +295 2 3 -39 +46 3 -20 -25 +22 +24 @@ -3503,17 +3493,17 @@ 1 2 -293 +295 2 3 -39 +46 3 -20 -25 +22 +24 @@ -3529,7 +3519,7 @@ 1 2 -357 +365 @@ -3545,7 +3535,7 @@ 1 2 -357 +365 @@ -3559,8 +3549,13 @@ 12 -500 -501 +9 +10 +1 + + +514 +515 1 @@ -3575,8 +3570,13 @@ 12 -500 -501 +9 +10 +1 + + +514 +515 1 @@ -3591,8 +3591,13 @@ 12 -357 -358 +1 +2 +1 + + +364 +365 1 @@ -3609,7 +3614,7 @@ 1 2 -1 +2 @@ -3623,8 +3628,8 @@ 12 -500 -501 +523 +524 1 @@ -3639,8 +3644,8 @@ 12 -500 -501 +523 +524 1 @@ -3655,8 +3660,8 @@ 12 -357 -358 +365 +366 1 @@ -3671,8 +3676,8 @@ 12 -1 -2 +2 +3 1 @@ -3683,19 +3688,19 @@ folders -196 +223 id -196 +223 name -196 +223 simple -169 +186 @@ -3709,7 +3714,7 @@ 1 2 -196 +223 @@ -3725,7 +3730,7 @@ 1 2 -196 +223 @@ -3741,7 +3746,7 @@ 1 2 -196 +223 @@ -3757,7 +3762,7 @@ 1 2 -196 +223 @@ -3773,17 +3778,17 @@ 1 2 -150 +163 2 3 -17 +19 -5 -8 -2 +3 +10 +4 @@ -3799,17 +3804,17 @@ 1 2 -150 +163 2 3 -17 +19 -5 -8 -2 +3 +10 +4 @@ -3819,15 +3824,15 @@ containerparent -695 +745 parent -196 +223 child -695 +745 @@ -3841,38 +3846,33 @@ 1 2 -107 +128 2 3 -29 +30 3 4 -10 +14 4 -5 -12 +6 +19 -5 -7 +6 +12 +17 + + +13 +39 15 - -7 -18 -16 - - -18 -35 -7 - @@ -3887,7 +3887,7 @@ 1 2 -695 +745 @@ -3897,15 +3897,15 @@ has_location -569920 +596527 locatable -569920 +596527 location -510886 +534814 @@ -3919,7 +3919,7 @@ 1 2 -569920 +596527 @@ -3935,17 +3935,17 @@ 1 2 -452126 +473415 2 3 -58720 +61355 3 -71 -40 +73 +44 @@ -3955,22 +3955,22 @@ comment_groups -11598 +12042 id -11598 +12042 comments -24951 +25659 id -24951 +25659 kind @@ -3978,7 +3978,7 @@ parent -11598 +12049 idx @@ -3986,7 +3986,7 @@ text -19995 +20595 @@ -4000,7 +4000,7 @@ 1 2 -24951 +25659 @@ -4016,7 +4016,7 @@ 1 2 -24951 +25659 @@ -4032,7 +4032,7 @@ 1 2 -24951 +25659 @@ -4048,7 +4048,7 @@ 1 2 -24951 +25659 @@ -4062,13 +4062,13 @@ 12 -794 -795 +846 +847 1 -24157 -24158 +24813 +24814 1 @@ -4083,13 +4083,13 @@ 12 -794 -795 +846 +847 1 -10806 -10807 +11205 +11206 1 @@ -4125,13 +4125,13 @@ 12 -658 -659 +690 +691 1 -19337 -19338 +19905 +19906 1 @@ -4148,27 +4148,27 @@ 1 2 -7529 +7809 2 3 -1663 +1776 3 4 -1239 +1284 4 -10 -887 +11 +939 -10 +11 157 -280 +241 @@ -4184,7 +4184,7 @@ 1 2 -11596 +12047 2 @@ -4205,27 +4205,27 @@ 1 2 -7529 +7809 2 3 -1663 +1776 3 4 -1239 +1284 4 -10 -887 +11 +939 -10 +11 157 -280 +241 @@ -4241,27 +4241,27 @@ 1 2 -7529 +7809 2 3 -1693 +1806 3 4 -1226 +1270 4 10 -928 +940 10 131 -222 +224 @@ -4301,32 +4301,32 @@ 9 -13 -8 +11 +6 -13 -16 -12 +12 +14 +13 -16 -25 -12 +14 +24 +13 -27 -53 +26 +52 12 59 -215 +217 12 -238 -11599 +241 +12050 11 @@ -4388,32 +4388,32 @@ 9 -13 -8 +11 +6 -13 -16 -12 +12 +14 +13 -16 -25 -12 +14 +24 +13 -27 -53 +26 +52 12 59 -215 +217 12 -238 -11599 +241 +12050 11 @@ -4449,34 +4449,34 @@ 7 -10 +9 +11 + + +9 +12 14 -10 -14 -14 +12 +17 +12 -14 -24 +19 +36 13 -25 -51 +39 +127 12 -51 -221 +164 +10459 12 - -241 -10061 -9 - @@ -4491,12 +4491,12 @@ 1 2 -18865 +19444 2 -1930 -1130 +1932 +1151 @@ -4512,7 +4512,7 @@ 1 2 -19995 +20595 @@ -4528,12 +4528,12 @@ 1 2 -18889 +19469 2 -821 -1106 +826 +1126 @@ -4549,12 +4549,12 @@ 1 2 -19843 +20434 2 107 -152 +161 @@ -4564,15 +4564,15 @@ doc_comments -4111 +4360 node -4111 +4360 comment -4111 +4360 @@ -4586,7 +4586,7 @@ 1 2 -4111 +4360 @@ -4602,7 +4602,7 @@ 1 2 -4111 +4360 @@ -4612,19 +4612,19 @@ exprs -392882 +411946 id -392882 +411946 kind -49 +48 parent -207846 +218211 idx @@ -4642,7 +4642,7 @@ 1 2 -392882 +411946 @@ -4658,7 +4658,7 @@ 1 2 -392882 +411946 @@ -4674,7 +4674,7 @@ 1 2 -392882 +411946 @@ -4689,69 +4689,64 @@ 7 -15 +20 4 -19 -28 +21 +54 4 -53 -91 +57 +146 4 -130 -208 +147 +236 4 -230 -282 +245 +344 4 -345 -600 +506 +616 4 -604 -853 +676 +838 4 -913 -1130 +1008 +1233 4 -1232 -2061 +1267 +2673 4 -2452 -3851 +3211 +4564 4 -4397 -7562 +5543 +10347 4 -9905 -50876 +24857 +236051 4 - -224564 -224565 -1 - @@ -4765,67 +4760,67 @@ 7 -15 +20 4 -19 -27 +21 +54 4 -53 -91 +57 +101 +3 + + +145 +184 4 -128 -183 +204 +270 4 -221 -269 +323 +607 4 -326 -592 +611 +778 4 -602 -797 +786 +1210 4 -868 -1130 +1223 +1671 4 -1210 -1578 +1946 +3449 4 -1881 -3255 +3951 +7357 4 -3726 -7254 +10100 +48997 4 -9657 -46153 -4 - - -146437 -146438 +153945 +153946 1 @@ -4842,7 +4837,7 @@ 1 2 -4 +3 2 @@ -4852,42 +4847,42 @@ 3 4 -11 +10 4 5 -5 +6 5 -6 -3 +7 +4 7 8 -5 +4 8 -15 -4 +14 +3 15 -24 +22 4 -25 -1058 +23 +34 4 -5163 +1057 5164 -1 +2 @@ -4903,22 +4898,22 @@ 1 2 -62335 +65505 2 3 -126418 +132630 3 5 -16910 +17788 5 5144 -2183 +2288 @@ -4934,17 +4929,17 @@ 1 2 -130121 +136577 2 3 -71766 +75398 3 7 -5959 +6236 @@ -4960,22 +4955,22 @@ 1 2 -62335 +65505 2 3 -126418 +132630 3 5 -16910 +17788 5 5144 -2183 +2288 @@ -5005,7 +5000,7 @@ 4 -170332 +179100 288 @@ -5031,7 +5026,7 @@ 3 -49 +48 43 @@ -5062,7 +5057,7 @@ 4 -170332 +179100 288 @@ -5073,19 +5068,19 @@ literals -257407 +269438 expr -257407 +269438 value -25122 +25744 raw -26879 +27542 @@ -5099,7 +5094,7 @@ 1 2 -257407 +269438 @@ -5115,7 +5110,7 @@ 1 2 -257407 +269438 @@ -5131,37 +5126,37 @@ 1 2 -14520 +14621 2 3 -3141 +3283 3 4 -1675 +1782 4 6 -1843 +1920 6 -13 -1985 +12 +1965 -13 -502 -1885 +12 +142 +1931 -505 -6338 -73 +142 +6774 +242 @@ -5177,12 +5172,12 @@ 1 2 -23419 +24001 2 5 -1703 +1743 @@ -5198,32 +5193,32 @@ 1 2 -15890 +16023 2 3 -3364 +3511 3 4 -1768 +1867 4 7 -2397 +2542 7 18 -2027 +2115 18 -6331 -1433 +6767 +1484 @@ -5239,7 +5234,7 @@ 1 2 -26879 +27542 @@ -5249,19 +5244,19 @@ constvalues -42811 +43806 expr -42811 +43806 value -16728 +16879 exact -16730 +16880 @@ -5275,7 +5270,7 @@ 1 2 -42811 +43806 @@ -5291,7 +5286,7 @@ 1 2 -42811 +43806 @@ -5307,17 +5302,17 @@ 1 2 -14806 +14911 2 4 -1368 +1394 4 -6267 -554 +6700 +574 @@ -5333,12 +5328,12 @@ 1 2 -16726 +16878 2 3 -2 +1 @@ -5354,17 +5349,17 @@ 1 2 -14808 +14912 2 4 -1369 +1394 4 -6267 -553 +6700 +574 @@ -5380,7 +5375,7 @@ 1 2 -16730 +16880 @@ -5390,19 +5385,19 @@ fields -18766 +19939 id -18766 +19939 parent -9097 +9606 idx -47 +57 @@ -5416,7 +5411,7 @@ 1 2 -18766 +19939 @@ -5432,7 +5427,7 @@ 1 2 -18766 +19939 @@ -5448,27 +5443,27 @@ 1 2 -4532 +4802 2 3 -2155 +2221 3 4 -1288 +1362 4 -6 -827 +5 +648 -6 -44 -295 +5 +53 +573 @@ -5484,27 +5479,27 @@ 1 2 -4532 +4802 2 3 -2155 +2221 3 4 -1288 +1362 4 -6 -827 +5 +648 -6 -44 -295 +5 +53 +573 @@ -5520,57 +5515,47 @@ 1 2 -12 +14 2 3 -1 +8 3 -4 -4 +6 +5 -4 -7 -3 +6 +11 +5 -7 -13 -4 +11 +18 +5 -15 -19 -4 +19 +31 +5 -21 -28 -4 +45 +105 +5 -39 -78 -4 +111 +630 +5 -94 -182 -4 - - -297 -1182 -4 - - -2872 -6658 -3 +909 +7058 +5 @@ -5586,57 +5571,47 @@ 1 2 -12 +14 2 3 -1 +8 3 -4 -4 +6 +5 -4 -7 -3 +6 +11 +5 -7 -13 -4 +11 +18 +5 -15 -19 -4 +19 +31 +5 -21 -28 -4 +45 +105 +5 -39 -78 -4 +111 +630 +5 -94 -182 -4 - - -297 -1182 -4 - - -2872 -6658 -3 +909 +7058 +5 @@ -5646,11 +5621,11 @@ stmts -70545 +73583 id -70545 +73583 kind @@ -5658,7 +5633,7 @@ parent -39641 +41328 idx @@ -5676,7 +5651,7 @@ 1 2 -70545 +73583 @@ -5692,7 +5667,7 @@ 1 2 -70545 +73583 @@ -5708,7 +5683,7 @@ 1 2 -70545 +73583 @@ -5724,12 +5699,12 @@ 2 3 -2 +1 3 4 -3 +4 5 @@ -5737,48 +5712,48 @@ 3 -19 -46 +18 +47 3 -51 -62 +63 +69 3 -69 -213 +71 +223 3 298 -371 +375 3 -384 -608 +397 +614 3 -648 -2037 +652 +2127 3 -3396 -7249 +3463 +7569 3 -8728 -9242 +9160 +9658 3 -18460 -18461 +19269 +19270 1 @@ -5795,12 +5770,12 @@ 2 3 -2 +1 3 4 -3 +4 5 @@ -5808,48 +5783,48 @@ 3 -18 -26 +17 +33 3 -43 -60 +44 +68 3 -64 -196 +68 +204 3 -296 -359 +298 +363 3 -372 -578 +383 +585 3 -578 -1092 +600 +1149 3 -1648 -5430 +1716 +5643 3 -5636 -8729 +5874 +9161 3 -17933 -17934 +18734 +18735 1 @@ -5876,7 +5851,7 @@ 3 5 -3 +2 5 @@ -5890,11 +5865,11 @@ 9 -14 +13 3 -14 +13 15 2 @@ -5905,23 +5880,23 @@ 17 -28 +27 3 -32 -44 +27 +33 3 44 -56 +47 3 -81 +55 82 -1 +2 @@ -5937,22 +5912,22 @@ 1 2 -28014 +29217 2 3 -5850 +6095 3 5 -3518 +3637 5 82 -2259 +2379 @@ -5968,22 +5943,22 @@ 1 2 -30001 +31256 2 3 -5475 +5729 3 5 -3454 +3585 5 11 -711 +758 @@ -5999,22 +5974,22 @@ 1 2 -28014 +29217 2 3 -5850 +6095 3 5 -3518 +3637 5 82 -2259 +2379 @@ -6043,44 +6018,39 @@ 3 -6 -7 -8 - - 7 -11 -5 +8 +11 -11 -17 +10 +15 7 -17 -27 +15 +26 7 -32 -96 +26 +68 7 -120 -368 +83 +256 7 -473 -2260 +313 +1268 7 -6703 -23706 -4 +1707 +24738 +6 @@ -6105,43 +6075,43 @@ 3 -4 -6 +5 +7 -4 +5 6 6 6 7 -5 - - -7 -8 4 -8 -11 +7 +9 7 -11 -15 +9 +13 7 -15 -21 +13 +18 +7 + + +20 +28 6 -23 +29 34 -6 +3 @@ -6170,44 +6140,39 @@ 3 -6 -7 -8 - - 7 -11 -5 +8 +11 -11 -17 +10 +15 7 -17 -27 +15 +26 7 -32 -96 +26 +68 7 -120 -368 +83 +256 7 -473 -2260 +313 +1268 7 -6703 -23706 -4 +1707 +24738 +6 @@ -6217,11 +6182,11 @@ decls -8344 +8691 id -8344 +8691 kind @@ -6229,11 +6194,11 @@ parent -1879 +1945 idx -222 +226 @@ -6247,7 +6212,7 @@ 1 2 -8344 +8691 @@ -6263,7 +6228,7 @@ 1 2 -8344 +8691 @@ -6279,7 +6244,7 @@ 1 2 -8344 +8691 @@ -6293,28 +6258,28 @@ 12 -265 -266 +279 +280 1 -464 -465 +479 +480 1 -1288 -1289 +1351 +1352 1 -1702 -1703 +1762 +1763 1 -4625 -4626 +4820 +4821 1 @@ -6329,28 +6294,28 @@ 12 -188 -189 +195 +196 1 -329 -330 +337 +338 1 -463 -464 +478 +479 1 -470 -471 +483 +484 1 -1504 -1505 +1560 +1561 1 @@ -6370,18 +6335,13 @@ 1 -35 -36 +39 +40 1 -106 -107 -1 - - -212 -213 +105 +106 1 @@ -6389,6 +6349,11 @@ 220 1 + +225 +226 +1 + @@ -6403,27 +6368,27 @@ 1 2 -1402 +1454 2 -7 -163 +6 +147 -7 -13 -148 +6 +12 +158 -13 -44 -141 +12 +36 +146 -44 -223 -25 +36 +227 +40 @@ -6439,22 +6404,22 @@ 1 2 -1405 +1457 2 3 -107 +110 3 4 -184 +187 4 5 -132 +140 5 @@ -6475,27 +6440,27 @@ 1 2 -1402 +1454 2 -7 -163 +6 +147 -7 -13 -148 +6 +12 +158 -13 -44 -141 +12 +36 +146 -44 -223 -25 +36 +227 +40 @@ -6511,17 +6476,17 @@ 1 2 -9 +4 2 3 -22 +30 3 4 -17 +18 4 @@ -6536,32 +6501,32 @@ 6 7 -34 +31 -7 -11 +8 +10 17 -13 -33 +11 +31 17 -34 -77 +31 +64 17 -81 -369 +66 +278 17 -397 -1880 -4 +325 +1946 +7 @@ -6577,22 +6542,22 @@ 1 2 -12 +7 2 3 -103 +113 3 4 -74 +69 4 5 -31 +35 5 @@ -6613,17 +6578,17 @@ 1 2 -9 +4 2 3 -22 +30 3 4 -17 +18 4 @@ -6638,32 +6603,32 @@ 6 7 -34 +31 -7 -11 +8 +10 17 -13 -33 +11 +31 17 -34 -77 +31 +64 17 -81 -369 +66 +278 17 -397 -1880 -4 +325 +1946 +7 @@ -6673,11 +6638,11 @@ specs -7558 +7866 id -7558 +7866 kind @@ -6685,11 +6650,11 @@ parent -3719 +3871 idx -106 +108 @@ -6703,7 +6668,7 @@ 1 2 -7558 +7866 @@ -6719,7 +6684,7 @@ 1 2 -7558 +7866 @@ -6735,7 +6700,7 @@ 1 2 -7558 +7866 @@ -6749,18 +6714,18 @@ 12 -1304 -1305 +1367 +1368 1 -2939 -2940 +3046 +3047 1 -3315 -3316 +3453 +3454 1 @@ -6775,18 +6740,18 @@ 12 -464 -465 +479 +480 1 -1288 -1289 +1351 +1352 1 -1967 -1968 +2041 +2042 1 @@ -6806,13 +6771,13 @@ 1 -33 -34 +36 +37 1 -106 -107 +108 +109 1 @@ -6829,22 +6794,22 @@ 1 2 -3078 +3201 2 6 -323 +345 6 -16 -280 +18 +296 -16 -107 -38 +18 +109 +29 @@ -6860,7 +6825,7 @@ 1 2 -3719 +3871 @@ -6876,22 +6841,22 @@ 1 2 -3078 +3201 2 6 -323 +345 6 -16 -280 +18 +296 -16 -107 -38 +18 +109 +29 @@ -6907,37 +6872,32 @@ 1 2 -51 +53 2 3 -22 +19 3 -8 -8 +5 +9 -9 -30 -8 +6 +28 +9 -35 -144 -8 +29 +156 +9 -177 -642 -8 - - -3719 -3720 -1 +187 +3872 +9 @@ -6953,12 +6913,12 @@ 1 2 -73 +72 2 3 -19 +22 3 @@ -6979,37 +6939,32 @@ 1 2 -51 +53 2 3 -22 +19 3 -8 -8 +5 +9 -9 -30 -8 +6 +28 +9 -35 -144 -8 +29 +156 +9 -177 -642 -8 - - -3719 -3720 -1 +187 +3872 +9 @@ -7019,11 +6974,11 @@ scopes -35107 +36580 id -35107 +36580 kind @@ -7041,7 +6996,7 @@ 1 2 -35107 +36580 @@ -7060,13 +7015,13 @@ 1 -330 -331 +339 +340 1 -34776 -34777 +36240 +36241 1 @@ -7077,15 +7032,15 @@ scopenesting -35106 +36579 inner -35106 +36579 outer -20732 +21587 @@ -7099,7 +7054,7 @@ 1 2 -35106 +36579 @@ -7115,22 +7070,22 @@ 1 2 -16212 +16872 2 3 -2356 +2451 3 7 -1664 +1750 7 -331 -500 +340 +514 @@ -7140,15 +7095,15 @@ scopenodes -34776 +36240 node -34776 +36240 scope -34776 +36240 @@ -7162,7 +7117,7 @@ 1 2 -34776 +36240 @@ -7178,7 +7133,7 @@ 1 2 -34776 +36240 @@ -7188,11 +7143,11 @@ objects -80103 +82993 id -80103 +82993 kind @@ -7200,7 +7155,7 @@ name -28614 +29498 @@ -7214,7 +7169,7 @@ 1 2 -80103 +82993 @@ -7230,7 +7185,7 @@ 1 2 -80103 +82993 @@ -7259,33 +7214,33 @@ 1 -45 -46 +46 +47 1 -3315 -3316 +3453 +3454 1 -3431 -3432 +3501 +3502 1 -8264 -8265 +8487 +8488 1 -16817 -16818 +17259 +17260 1 -48189 -48190 +50205 +50206 1 @@ -7320,28 +7275,28 @@ 1 -191 -192 +202 +203 1 -2864 -2865 +2926 +2927 1 -7839 -7840 +8046 +8047 1 -9478 -9479 +9812 +9813 1 -10239 -10240 +10565 +10566 1 @@ -7358,22 +7313,22 @@ 1 2 -23539 +24295 2 3 -2517 +2559 3 -18 -2168 +17 +2224 -18 -2051 -390 +17 +2187 +420 @@ -7389,12 +7344,12 @@ 1 2 -26928 +27765 2 6 -1686 +1733 @@ -7404,15 +7359,15 @@ objectscopes -51672 +53703 object -51672 +53703 scope -13271 +13896 @@ -7426,7 +7381,7 @@ 1 2 -51672 +53703 @@ -7442,32 +7397,32 @@ 1 2 -6754 +7092 2 3 -2833 +2952 3 4 -1247 +1288 4 6 -1199 +1258 6 16 -1007 +1067 16 -2383 -231 +2515 +239 @@ -7477,15 +7432,15 @@ objecttypes -80101 +82991 object -80101 +82991 tp -12723 +13057 @@ -7499,7 +7454,7 @@ 1 2 -80101 +82991 @@ -7515,32 +7470,32 @@ 1 2 -7458 +7629 2 3 -2013 +2065 3 4 -873 +866 4 7 -1072 +1149 7 25 -959 +992 25 -4040 -348 +4217 +356 @@ -7550,15 +7505,15 @@ methodreceivers -9345 +9635 method -9345 +9635 receiver -9345 +9635 @@ -7572,7 +7527,7 @@ 1 2 -9345 +9635 @@ -7588,7 +7543,7 @@ 1 2 -9345 +9635 @@ -7598,15 +7553,15 @@ fieldstructs -10266 +10580 field -10266 +10580 struct -2290 +2337 @@ -7620,7 +7575,7 @@ 1 2 -10266 +10580 @@ -7636,42 +7591,42 @@ 1 2 -248 +249 2 3 -658 +664 3 4 -429 +446 4 5 -275 +286 5 6 -200 +192 6 8 -200 +202 8 13 -174 +190 13 61 -106 +108 @@ -7681,15 +7636,15 @@ methodhosts -8700 +791 method -8700 +791 host -1765 +241 @@ -7703,7 +7658,7 @@ 1 2 -8700 +791 @@ -7719,42 +7674,37 @@ 1 2 -466 +95 2 3 -374 +51 3 4 -308 +33 4 5 -116 +16 5 6 -144 +16 6 -9 -146 +11 +20 -9 -20 -136 - - -20 -124 -75 +11 +53 +10 @@ -7764,15 +7714,15 @@ defs -38467 +40547 ident -38467 +40547 object -38265 +40330 @@ -7786,7 +7736,7 @@ 1 2 -38467 +40547 @@ -7802,12 +7752,12 @@ 1 2 -38163 +40219 2 -14 -102 +15 +111 @@ -7817,15 +7767,15 @@ uses -185400 +194793 ident -185400 +194793 object -39382 +41391 @@ -7839,7 +7789,7 @@ 1 2 -185400 +194793 @@ -7855,37 +7805,37 @@ 1 2 -14573 +15402 2 3 -9216 +9683 3 4 -4787 +5014 4 5 -2855 +2968 5 7 -3047 +3191 7 14 -3166 +3324 14 -6331 -1738 +6767 +1809 @@ -7895,11 +7845,11 @@ types -17155 +17623 id -17155 +17623 kind @@ -7917,7 +7867,7 @@ 1 2 -17155 +17623 @@ -7941,18 +7891,18 @@ 3 -226 -398 +232 +416 3 -527 -1822 +556 +1861 3 -2291 -7533 +2338 +7763 3 @@ -7963,15 +7913,15 @@ type_of -377624 +395959 expr -377624 +395959 tp -8248 +8659 @@ -7985,7 +7935,7 @@ 1 2 -377624 +395959 @@ -8001,57 +7951,57 @@ 1 2 -1903 +2008 2 3 -922 +960 3 4 -685 +714 4 5 -359 +383 5 7 -742 +783 7 10 -754 +766 10 15 -696 +752 15 23 -659 +686 23 -43 -619 +44 +660 -43 -149 -620 +44 +151 +650 -149 -45385 -289 +152 +46814 +297 @@ -8061,15 +8011,15 @@ typename -3402 +3473 tp -3402 +3473 name -2842 +2905 @@ -8083,7 +8033,7 @@ 1 2 -3402 +3473 @@ -8099,17 +8049,17 @@ 1 2 -2523 +2585 2 -3 -214 +4 +267 -3 +4 16 -105 +53 @@ -8119,15 +8069,15 @@ key_type -397 +415 map -397 +415 tp -139 +143 @@ -8141,7 +8091,7 @@ 1 2 -397 +415 @@ -8157,7 +8107,7 @@ 1 2 -96 +101 2 @@ -8166,18 +8116,18 @@ 3 -5 -9 +6 +10 -5 -8 -9 +6 +11 +11 -8 -125 -7 +12 +134 +3 @@ -8187,15 +8137,15 @@ element_type -1331 +1377 container -1331 +1377 tp -853 +892 @@ -8209,7 +8159,7 @@ 1 2 -1331 +1377 @@ -8225,17 +8175,17 @@ 1 2 -722 +756 2 3 -86 +90 3 65 -45 +46 @@ -8245,15 +8195,15 @@ base_type -1821 +1860 ptr -1821 +1860 tp -1821 +1860 @@ -8267,7 +8217,7 @@ 1 2 -1821 +1860 @@ -8283,7 +8233,7 @@ 1 2 -1821 +1860 @@ -8293,15 +8243,15 @@ underlying_type -3402 +3473 named -3402 +3473 tp -2622 +2676 @@ -8315,7 +8265,7 @@ 1 2 -3402 +3473 @@ -8331,12 +8281,12 @@ 1 2 -2451 +2503 2 -143 -171 +148 +173 @@ -8346,11 +8296,11 @@ component_types -34044 +35149 parent -10572 +10884 index @@ -8358,11 +8308,11 @@ name -5162 +5333 tp -4046 +4167 @@ -8376,37 +8326,37 @@ 1 2 -1128 +1158 2 3 -3765 +3868 3 4 -2718 +2795 4 5 -1313 +1372 5 6 -751 +756 6 -14 -796 +13 +817 -14 +13 61 -101 +118 @@ -8422,22 +8372,22 @@ 1 2 -8409 +8671 2 3 -705 +713 3 6 -953 +974 6 60 -505 +526 @@ -8453,32 +8403,32 @@ 1 2 -2139 +2230 2 3 -4233 +4341 3 4 -2323 +2375 4 5 -1037 +1068 5 13 -793 +821 13 -43 -47 +52 +49 @@ -8494,56 +8444,61 @@ 1 2 -16 +12 2 -5 -6 - - -5 -10 +3 4 -10 -15 +3 +6 +5 + + +6 +11 6 -15 -22 +11 +17 +5 + + +17 +23 6 -23 -40 +24 +42 6 -46 -65 +47 +70 6 -75 -141 +81 +151 6 -174 -474 +184 +488 6 -759 -5653 +794 +5834 6 -6052 -9686 +6248 +9970 2 @@ -8560,53 +8515,53 @@ 1 2 -22 +18 2 -6 +4 6 -7 -10 -3 +4 +9 +6 -10 -16 -6 +9 +15 +5 16 -24 +22 +5 + + +22 +33 6 -24 -45 +36 +59 6 -48 -83 +66 +112 6 -88 -189 +142 +355 6 -215 -756 +454 +1227 6 - -972 -1195 -3 - @@ -8621,57 +8576,62 @@ 1 2 -16 +12 2 -5 -6 +3 +4 -5 -8 +3 +6 5 +6 +8 +6 + + 9 -13 +14 6 14 -17 -5 - - -17 -25 +18 6 -25 -42 +18 +28 6 -42 -66 +32 +46 6 -72 -155 +47 +83 6 -212 -1343 +103 +215 6 -1761 -2036 -2 +296 +1835 +6 + + +2094 +2095 +1 @@ -8687,22 +8647,22 @@ 1 2 -3632 +3757 2 3 -819 +843 3 5 -398 +407 5 -8392 -313 +8658 +326 @@ -8718,22 +8678,22 @@ 1 2 -4004 +4128 2 3 -678 +704 3 6 -404 +423 6 28 -76 +78 @@ -8749,21 +8709,21 @@ 1 2 -4282 +4422 2 3 -476 +497 3 -15 -391 +16 +401 -15 -2846 +16 +2934 13 @@ -8780,32 +8740,32 @@ 1 2 -1936 +1991 2 3 -768 +789 3 4 -385 +403 4 6 -372 +379 6 11 -314 +327 11 -2053 -271 +2116 +278 @@ -8821,32 +8781,32 @@ 1 2 -1997 +2041 2 3 -811 +832 3 4 -532 +563 4 5 -332 +348 5 10 -318 +323 10 -48 -56 +49 +60 @@ -8862,22 +8822,22 @@ 1 2 -2733 +2807 2 3 -815 +839 3 5 -318 +333 5 -662 -180 +678 +188 @@ -8887,15 +8847,15 @@ array_length -286 +291 tp -286 +291 len -101 +102 @@ -8909,7 +8869,7 @@ 1 2 -286 +291 @@ -8935,18 +8895,23 @@ 3 5 -9 +8 5 -11 -7 +8 +8 -11 -28 +10 +16 8 + +26 +27 +1 + @@ -8955,15 +8920,15 @@ type_objects -3402 +3473 tp -3402 +3473 object -3402 +3473 @@ -8977,7 +8942,7 @@ 1 2 -3402 +3473 @@ -8993,7 +8958,7 @@ 1 2 -3402 +3473 @@ -9003,23 +8968,23 @@ packages -330 +339 id -330 +339 name -266 +274 path -330 +339 scope -330 +339 @@ -9033,7 +8998,7 @@ 1 2 -330 +339 @@ -9049,7 +9014,7 @@ 1 2 -330 +339 @@ -9065,7 +9030,7 @@ 1 2 -330 +339 @@ -9081,16 +9046,16 @@ 1 2 -238 +248 2 3 -25 +23 3 -37 +40 3 @@ -9107,16 +9072,16 @@ 1 2 -238 +248 2 3 -25 +23 3 -37 +40 3 @@ -9133,16 +9098,16 @@ 1 2 -238 +248 2 3 -25 +23 3 -37 +40 3 @@ -9159,7 +9124,7 @@ 1 2 -330 +339 @@ -9175,7 +9140,7 @@ 1 2 -330 +339 @@ -9191,7 +9156,7 @@ 1 2 -330 +339 @@ -9207,7 +9172,7 @@ 1 2 -330 +339 @@ -9223,7 +9188,7 @@ 1 2 -330 +339 @@ -9239,7 +9204,554 @@ 1 2 -330 +339 + + + + + + + + +modexprs +38 + + +id +38 + + +kind +4 + + +parent +11 + + +idx +7 + + + + +id +kind + + +12 + + +1 +2 +38 + + + + + + +id +parent + + +12 + + +1 +2 +38 + + + + + + +id +idx + + +12 + + +1 +2 +38 + + + + + + +kind +id + + +12 + + +2 +3 +3 + + +32 +33 +1 + + + + + + +kind +parent + + +12 + + +2 +3 +3 + + +11 +12 +1 + + + + + + +kind +idx + + +12 + + +1 +2 +2 + + +2 +3 +1 + + +6 +7 +1 + + + + + + +parent +id + + +12 + + +1 +2 +1 + + +2 +3 +2 + + +3 +4 +4 + + +4 +5 +2 + + +6 +7 +1 + + +7 +8 +1 + + + + + + +parent +kind + + +12 + + +1 +2 +7 + + +2 +3 +2 + + +3 +4 +2 + + + + + + +parent +idx + + +12 + + +1 +2 +1 + + +2 +3 +2 + + +3 +4 +4 + + +4 +5 +2 + + +6 +7 +1 + + +7 +8 +1 + + + + + + +idx +id + + +12 + + +1 +2 +1 + + +2 +3 +2 + + +4 +5 +1 + + +8 +9 +1 + + +10 +11 +1 + + +11 +12 +1 + + + + + + +idx +kind + + +12 + + +1 +2 +4 + + +2 +3 +3 + + + + + + +idx +parent + + +12 + + +1 +2 +1 + + +2 +3 +2 + + +4 +5 +1 + + +8 +9 +1 + + +10 +11 +1 + + +11 +12 +1 + + + + + + + + +modtokens +73 + + +token +38 + + +parent +34 + + +idx +4 + + + + +token +parent + + +12 + + +1 +2 +30 + + +2 +5 +3 + + +5 +8 +3 + + +8 +10 +2 + + + + + + +token +idx + + +12 + + +1 +2 +36 + + +2 +3 +2 + + + + + + +parent +token + + +12 + + +1 +2 +2 + + +2 +3 +26 + + +3 +4 +5 + + +4 +5 +1 + + + + + + +parent +idx + + +12 + + +1 +2 +2 + + +2 +3 +26 + + +3 +4 +5 + + +4 +5 +1 + + + + + + +idx +token + + +12 + + +1 +2 +1 + + +3 +4 +1 + + +13 +14 +1 + + +23 +24 +1 + + + + + + +idx +parent + + +12 + + +1 +2 +1 + + +6 +7 +1 + + +32 +33 +1 + + +34 +35 +1 diff --git a/upgrades/f1263a745347568af228ad3ddb2decb142c3a1a8/go.dbscheme b/upgrades/f1263a745347568af228ad3ddb2decb142c3a1a8/go.dbscheme new file mode 100644 index 00000000000..b9a2082d22a --- /dev/null +++ b/upgrades/f1263a745347568af228ad3ddb2decb142c3a1a8/go.dbscheme @@ -0,0 +1,420 @@ +/** Auto-generated dbscheme; do not edit. */ + + +/** Duplicate code **/ + +duplicateCode( + unique int id : @duplication, + varchar(900) relativePath : string ref, + int equivClass : int ref); + +similarCode( + unique int id : @similarity, + varchar(900) relativePath : string ref, + int equivClass : int ref); + +@duplication_or_similarity = @duplication | @similarity; + +tokens( + int id : @duplication_or_similarity ref, + int offset : int ref, + int beginLine : int ref, + int beginColumn : int ref, + int endLine : int ref, + int endColumn : int ref); + +/** External data **/ + +externalData( + int id : @externalDataElement, + varchar(900) path : string ref, + int column: int ref, + varchar(900) value : string ref +); + +snapshotDate(unique date snapshotDate : date ref); + +sourceLocationPrefix(varchar(900) prefix : string ref); + +locations_default(unique int id: @location_default, int file: @file ref, int beginLine: int ref, int beginColumn: int ref, + int endLine: int ref, int endColumn: int ref); + +numlines(int element_id: @sourceline ref, int num_lines: int ref, int num_code: int ref, int num_comment: int ref); + +files(unique int id: @file, string name: string ref, string simple: string ref, string ext: string ref, int fromSource: int ref); + +folders(unique int id: @folder, string name: string ref, string simple: string ref); + +containerparent(int parent: @container ref, unique int child: @container ref); + +has_location(unique int locatable: @locatable ref, int location: @location ref); + +comment_groups(unique int id: @comment_group); + +comments(unique int id: @comment, int kind: int ref, int parent: @comment_group ref, int idx: int ref, string text: string ref); + +doc_comments(unique int node: @documentable ref, int comment: @comment_group ref); + +#keyset[parent, idx] +exprs(unique int id: @expr, int kind: int ref, int parent: @exprparent ref, int idx: int ref); + +literals(unique int expr: @expr ref, string value: string ref, string raw: string ref); + +constvalues(unique int expr: @expr ref, string value: string ref, string exact: string ref); + +fields(unique int id: @field, int parent: @fieldparent ref, int idx: int ref); + +#keyset[parent, idx] +stmts(unique int id: @stmt, int kind: int ref, int parent: @stmtparent ref, int idx: int ref); + +#keyset[parent, idx] +decls(unique int id: @decl, int kind: int ref, int parent: @declparent ref, int idx: int ref); + +#keyset[parent, idx] +specs(unique int id: @spec, int kind: int ref, int parent: @gendecl ref, int idx: int ref); + +scopes(unique int id: @scope, int kind: int ref); + +scopenesting(unique int inner: @scope ref, int outer: @scope ref); + +scopenodes(unique int node: @scopenode ref, int scope: @localscope ref); + +objects(unique int id: @object, int kind: int ref, string name: string ref); + +objectscopes(unique int object: @object ref, int scope: @scope ref); + +objecttypes(unique int object: @object ref, int tp: @type ref); + +methodreceivers(unique int method: @object ref, int receiver: @object ref); + +fieldstructs(unique int field: @object ref, int struct: @structtype ref); + +methodhosts(int method: @object ref, int host: @namedtype ref); + +defs(int ident: @ident ref, int object: @object ref); + +uses(int ident: @ident ref, int object: @object ref); + +types(unique int id: @type, int kind: int ref); + +type_of(unique int expr: @expr ref, int tp: @type ref); + +typename(unique int tp: @type ref, string name: string ref); + +key_type(unique int map: @maptype ref, int tp: @type ref); + +element_type(unique int container: @containertype ref, int tp: @type ref); + +base_type(unique int ptr: @pointertype ref, int tp: @type ref); + +underlying_type(unique int named: @namedtype ref, int tp: @type ref); + +#keyset[parent, index] +component_types(int parent: @compositetype ref, int index: int ref, string name: string ref, int tp: @type ref); + +array_length(unique int tp: @arraytype ref, string len: string ref); + +type_objects(unique int tp: @type ref, int object: @object ref); + +packages(unique int id: @package, string name: string ref, string path: string ref, int scope: @packagescope ref); + +#keyset[parent, idx] +modexprs(unique int id: @modexpr, int kind: int ref, int parent: @modexprparent ref, int idx: int ref); + +#keyset[parent, idx] +modtokens(string token: string ref, int parent: @modexpr ref, int idx: int ref); + +@container = @file | @folder; + +@locatable = @node | @localscope; + +@node = @documentable | @exprparent | @modexprparent | @fieldparent | @stmtparent | @declparent | @scopenode + | @comment_group | @comment; + +@documentable = @file | @field | @spec | @gendecl | @funcdecl | @modexpr; + +@exprparent = @funcdef | @file | @expr | @field | @stmt | @decl | @spec; + +@modexprparent = @file | @modexpr; + +@fieldparent = @decl | @structtypeexpr | @functypeexpr | @interfacetypeexpr; + +@stmtparent = @funcdef | @stmt | @decl; + +@declparent = @file | @declstmt; + +@funcdef = @funclit | @funcdecl; + +@scopenode = @file | @functypeexpr | @blockstmt | @ifstmt | @caseclause | @switchstmt | @commclause | @loopstmt; + +@location = @location_default; + +@sourceline = @locatable; + +case @comment.kind of + 0 = @slashslashcomment +| 1 = @slashstarcomment; + +case @expr.kind of + 0 = @badexpr +| 1 = @ident +| 2 = @ellipsis +| 3 = @intlit +| 4 = @floatlit +| 5 = @imaglit +| 6 = @charlit +| 7 = @stringlit +| 8 = @funclit +| 9 = @compositelit +| 10 = @parenexpr +| 11 = @selectorexpr +| 12 = @indexexpr +| 13 = @sliceexpr +| 14 = @typeassertexpr +| 15 = @callorconversionexpr +| 16 = @starexpr +| 17 = @keyvalueexpr +| 18 = @arraytypeexpr +| 19 = @structtypeexpr +| 20 = @functypeexpr +| 21 = @interfacetypeexpr +| 22 = @maptypeexpr +| 23 = @plusexpr +| 24 = @minusexpr +| 25 = @notexpr +| 26 = @complementexpr +| 27 = @derefexpr +| 28 = @addressexpr +| 29 = @arrowexpr +| 30 = @lorexpr +| 31 = @landexpr +| 32 = @eqlexpr +| 33 = @neqexpr +| 34 = @lssexpr +| 35 = @leqexpr +| 36 = @gtrexpr +| 37 = @geqexpr +| 38 = @addexpr +| 39 = @subexpr +| 40 = @orexpr +| 41 = @xorexpr +| 42 = @mulexpr +| 43 = @quoexpr +| 44 = @remexpr +| 45 = @shlexpr +| 46 = @shrexpr +| 47 = @andexpr +| 48 = @andnotexpr +| 49 = @sendchantypeexpr +| 50 = @recvchantypeexpr +| 51 = @sendrcvchantypeexpr; + +@basiclit = @intlit | @floatlit | @imaglit | @charlit | @stringlit; + +@operatorexpr = @logicalexpr | @arithmeticexpr | @bitwiseexpr | @unaryexpr | @binaryexpr; + +@logicalexpr = @logicalunaryexpr | @logicalbinaryexpr; + +@arithmeticexpr = @arithmeticunaryexpr | @arithmeticbinaryexpr; + +@bitwiseexpr = @bitwiseunaryexpr | @bitwisebinaryexpr; + +@unaryexpr = @logicalunaryexpr | @bitwiseunaryexpr | @arithmeticunaryexpr | @derefexpr | @addressexpr | @arrowexpr; + +@logicalunaryexpr = @notexpr; + +@bitwiseunaryexpr = @complementexpr; + +@arithmeticunaryexpr = @plusexpr | @minusexpr; + +@binaryexpr = @logicalbinaryexpr | @bitwisebinaryexpr | @arithmeticbinaryexpr | @comparison; + +@logicalbinaryexpr = @lorexpr | @landexpr; + +@bitwisebinaryexpr = @shiftexpr | @orexpr | @xorexpr | @andexpr | @andnotexpr; + +@arithmeticbinaryexpr = @addexpr | @subexpr | @mulexpr | @quoexpr | @remexpr; + +@shiftexpr = @shlexpr | @shrexpr; + +@comparison = @equalitytest | @relationalcomparison; + +@equalitytest = @eqlexpr | @neqexpr; + +@relationalcomparison = @lssexpr | @leqexpr | @gtrexpr | @geqexpr; + +@chantypeexpr = @sendchantypeexpr | @recvchantypeexpr | @sendrcvchantypeexpr; + +case @stmt.kind of + 0 = @badstmt +| 1 = @declstmt +| 2 = @emptystmt +| 3 = @labeledstmt +| 4 = @exprstmt +| 5 = @sendstmt +| 6 = @incstmt +| 7 = @decstmt +| 8 = @gostmt +| 9 = @deferstmt +| 10 = @returnstmt +| 11 = @breakstmt +| 12 = @continuestmt +| 13 = @gotostmt +| 14 = @fallthroughstmt +| 15 = @blockstmt +| 16 = @ifstmt +| 17 = @caseclause +| 18 = @exprswitchstmt +| 19 = @typeswitchstmt +| 20 = @commclause +| 21 = @selectstmt +| 22 = @forstmt +| 23 = @rangestmt +| 24 = @assignstmt +| 25 = @definestmt +| 26 = @addassignstmt +| 27 = @subassignstmt +| 28 = @mulassignstmt +| 29 = @quoassignstmt +| 30 = @remassignstmt +| 31 = @andassignstmt +| 32 = @orassignstmt +| 33 = @xorassignstmt +| 34 = @shlassignstmt +| 35 = @shrassignstmt +| 36 = @andnotassignstmt; + +@incdecstmt = @incstmt | @decstmt; + +@assignment = @simpleassignstmt | @compoundassignstmt; + +@simpleassignstmt = @assignstmt | @definestmt; + +@compoundassignstmt = @addassignstmt | @subassignstmt | @mulassignstmt | @quoassignstmt | @remassignstmt + | @andassignstmt | @orassignstmt | @xorassignstmt | @shlassignstmt | @shrassignstmt | @andnotassignstmt; + +@branchstmt = @breakstmt | @continuestmt | @gotostmt | @fallthroughstmt; + +@switchstmt = @exprswitchstmt | @typeswitchstmt; + +@loopstmt = @forstmt | @rangestmt; + +case @decl.kind of + 0 = @baddecl +| 1 = @importdecl +| 2 = @constdecl +| 3 = @typedecl +| 4 = @vardecl +| 5 = @funcdecl; + +@gendecl = @importdecl | @constdecl | @typedecl | @vardecl; + +case @spec.kind of + 0 = @importspec +| 1 = @valuespec +| 2 = @typespec; + +case @object.kind of + 0 = @pkgobject +| 1 = @decltypeobject +| 2 = @builtintypeobject +| 3 = @declconstobject +| 4 = @builtinconstobject +| 5 = @declvarobject +| 6 = @declfunctionobject +| 7 = @builtinfunctionobject +| 8 = @labelobject; + +@declobject = @decltypeobject | @declconstobject | @declvarobject | @declfunctionobject; + +@builtinobject = @builtintypeobject | @builtinconstobject | @builtinfunctionobject; + +@typeobject = @decltypeobject | @builtintypeobject; + +@valueobject = @constobject | @varobject | @functionobject; + +@constobject = @declconstobject | @builtinconstobject; + +@varobject = @declvarobject; + +@functionobject = @declfunctionobject | @builtinfunctionobject; + +case @scope.kind of + 0 = @universescope +| 1 = @packagescope +| 2 = @localscope; + +case @type.kind of + 0 = @invalidtype +| 1 = @boolexprtype +| 2 = @inttype +| 3 = @int8type +| 4 = @int16type +| 5 = @int32type +| 6 = @int64type +| 7 = @uinttype +| 8 = @uint8type +| 9 = @uint16type +| 10 = @uint32type +| 11 = @uint64type +| 12 = @uintptrtype +| 13 = @float32type +| 14 = @float64type +| 15 = @complex64type +| 16 = @complex128type +| 17 = @stringexprtype +| 18 = @unsafepointertype +| 19 = @boolliteraltype +| 20 = @intliteraltype +| 21 = @runeliteraltype +| 22 = @floatliteraltype +| 23 = @complexliteraltype +| 24 = @stringliteraltype +| 25 = @nilliteraltype +| 26 = @arraytype +| 27 = @slicetype +| 28 = @structtype +| 29 = @pointertype +| 30 = @interfacetype +| 31 = @tupletype +| 32 = @signaturetype +| 33 = @maptype +| 34 = @sendchantype +| 35 = @recvchantype +| 36 = @sendrcvchantype +| 37 = @namedtype; + +@basictype = @booltype | @numerictype | @stringtype | @literaltype | @invalidtype | @uintptrtype | @unsafepointertype; + +@booltype = @boolexprtype | @boolliteraltype; + +@numerictype = @integertype | @floattype | @complextype; + +@integertype = @signedintegertype | @unsignedintegertype; + +@signedintegertype = @inttype | @int8type | @int16type | @int32type | @int64type | @intliteraltype | @runeliteraltype; + +@unsignedintegertype = @uinttype | @uint8type | @uint16type | @uint32type | @uint64type; + +@floattype = @float32type | @float64type | @floatliteraltype; + +@complextype = @complex64type | @complex128type | @complexliteraltype; + +@stringtype = @stringexprtype | @stringliteraltype; + +@literaltype = @boolliteraltype | @intliteraltype | @runeliteraltype | @floatliteraltype | @complexliteraltype + | @stringliteraltype | @nilliteraltype; + +@compositetype = @containertype | @structtype | @pointertype | @interfacetype | @tupletype | @signaturetype | @namedtype; + +@containertype = @arraytype | @slicetype | @maptype | @chantype; + +@chantype = @sendchantype | @recvchantype | @sendrcvchantype; + +case @modexpr.kind of + 0 = @modcommentblock +| 1 = @modline +| 2 = @modlineblock +| 3 = @modlparen +| 4 = @modrparen; + diff --git a/upgrades/f1263a745347568af228ad3ddb2decb142c3a1a8/old.dbscheme b/upgrades/f1263a745347568af228ad3ddb2decb142c3a1a8/old.dbscheme new file mode 100644 index 00000000000..f1263a74534 --- /dev/null +++ b/upgrades/f1263a745347568af228ad3ddb2decb142c3a1a8/old.dbscheme @@ -0,0 +1,404 @@ +/** Auto-generated dbscheme; do not edit. */ + + +/** Duplicate code **/ + +duplicateCode( + unique int id : @duplication, + varchar(900) relativePath : string ref, + int equivClass : int ref); + +similarCode( + unique int id : @similarity, + varchar(900) relativePath : string ref, + int equivClass : int ref); + +@duplication_or_similarity = @duplication | @similarity; + +tokens( + int id : @duplication_or_similarity ref, + int offset : int ref, + int beginLine : int ref, + int beginColumn : int ref, + int endLine : int ref, + int endColumn : int ref); + +/** External data **/ + +externalData( + int id : @externalDataElement, + varchar(900) path : string ref, + int column: int ref, + varchar(900) value : string ref +); + +snapshotDate(unique date snapshotDate : date ref); + +sourceLocationPrefix(varchar(900) prefix : string ref); + +locations_default(unique int id: @location_default, int file: @file ref, int beginLine: int ref, int beginColumn: int ref, + int endLine: int ref, int endColumn: int ref); + +numlines(int element_id: @sourceline ref, int num_lines: int ref, int num_code: int ref, int num_comment: int ref); + +files(unique int id: @file, string name: string ref, string simple: string ref, string ext: string ref, int fromSource: int ref); + +folders(unique int id: @folder, string name: string ref, string simple: string ref); + +containerparent(int parent: @container ref, unique int child: @container ref); + +has_location(unique int locatable: @locatable ref, int location: @location ref); + +comment_groups(unique int id: @comment_group); + +comments(unique int id: @comment, int kind: int ref, int parent: @comment_group ref, int idx: int ref, string text: string ref); + +doc_comments(unique int node: @documentable ref, int comment: @comment_group ref); + +#keyset[parent, idx] +exprs(unique int id: @expr, int kind: int ref, int parent: @exprparent ref, int idx: int ref); + +literals(unique int expr: @expr ref, string value: string ref, string raw: string ref); + +constvalues(unique int expr: @expr ref, string value: string ref, string exact: string ref); + +fields(unique int id: @field, int parent: @fieldparent ref, int idx: int ref); + +#keyset[parent, idx] +stmts(unique int id: @stmt, int kind: int ref, int parent: @stmtparent ref, int idx: int ref); + +#keyset[parent, idx] +decls(unique int id: @decl, int kind: int ref, int parent: @declparent ref, int idx: int ref); + +#keyset[parent, idx] +specs(unique int id: @spec, int kind: int ref, int parent: @gendecl ref, int idx: int ref); + +scopes(unique int id: @scope, int kind: int ref); + +scopenesting(unique int inner: @scope ref, int outer: @scope ref); + +scopenodes(unique int node: @scopenode ref, int scope: @localscope ref); + +objects(unique int id: @object, int kind: int ref, string name: string ref); + +objectscopes(unique int object: @object ref, int scope: @scope ref); + +objecttypes(unique int object: @object ref, int tp: @type ref); + +methodreceivers(unique int method: @object ref, int receiver: @object ref); + +fieldstructs(unique int field: @object ref, int struct: @structtype ref); + +methodhosts(int method: @object ref, int host: @namedtype ref); + +defs(int ident: @ident ref, int object: @object ref); + +uses(int ident: @ident ref, int object: @object ref); + +types(unique int id: @type, int kind: int ref); + +type_of(unique int expr: @expr ref, int tp: @type ref); + +typename(unique int tp: @type ref, string name: string ref); + +key_type(unique int map: @maptype ref, int tp: @type ref); + +element_type(unique int container: @containertype ref, int tp: @type ref); + +base_type(unique int ptr: @pointertype ref, int tp: @type ref); + +underlying_type(unique int named: @namedtype ref, int tp: @type ref); + +#keyset[parent, index] +component_types(int parent: @compositetype ref, int index: int ref, string name: string ref, int tp: @type ref); + +array_length(unique int tp: @arraytype ref, string len: string ref); + +type_objects(unique int tp: @type ref, int object: @object ref); + +packages(unique int id: @package, string name: string ref, string path: string ref, int scope: @packagescope ref); + +@container = @file | @folder; + +@locatable = @node | @localscope; + +@node = @documentable | @exprparent | @fieldparent | @stmtparent | @declparent | @scopenode | @comment_group | @comment; + +@documentable = @file | @field | @spec | @gendecl | @funcdecl; + +@exprparent = @funcdef | @file | @expr | @field | @stmt | @decl | @spec; + +@fieldparent = @decl | @structtypeexpr | @functypeexpr | @interfacetypeexpr; + +@stmtparent = @funcdef | @stmt | @decl; + +@declparent = @file | @declstmt; + +@funcdef = @funclit | @funcdecl; + +@scopenode = @file | @functypeexpr | @blockstmt | @ifstmt | @caseclause | @switchstmt | @commclause | @loopstmt; + +@location = @location_default; + +@sourceline = @locatable; + +case @comment.kind of + 0 = @slashslashcomment +| 1 = @slashstarcomment; + +case @expr.kind of + 0 = @badexpr +| 1 = @ident +| 2 = @ellipsis +| 3 = @intlit +| 4 = @floatlit +| 5 = @imaglit +| 6 = @charlit +| 7 = @stringlit +| 8 = @funclit +| 9 = @compositelit +| 10 = @parenexpr +| 11 = @selectorexpr +| 12 = @indexexpr +| 13 = @sliceexpr +| 14 = @typeassertexpr +| 15 = @callorconversionexpr +| 16 = @starexpr +| 17 = @keyvalueexpr +| 18 = @arraytypeexpr +| 19 = @structtypeexpr +| 20 = @functypeexpr +| 21 = @interfacetypeexpr +| 22 = @maptypeexpr +| 23 = @plusexpr +| 24 = @minusexpr +| 25 = @notexpr +| 26 = @complementexpr +| 27 = @derefexpr +| 28 = @addressexpr +| 29 = @arrowexpr +| 30 = @lorexpr +| 31 = @landexpr +| 32 = @eqlexpr +| 33 = @neqexpr +| 34 = @lssexpr +| 35 = @leqexpr +| 36 = @gtrexpr +| 37 = @geqexpr +| 38 = @addexpr +| 39 = @subexpr +| 40 = @orexpr +| 41 = @xorexpr +| 42 = @mulexpr +| 43 = @quoexpr +| 44 = @remexpr +| 45 = @shlexpr +| 46 = @shrexpr +| 47 = @andexpr +| 48 = @andnotexpr +| 49 = @sendchantypeexpr +| 50 = @recvchantypeexpr +| 51 = @sendrcvchantypeexpr; + +@basiclit = @intlit | @floatlit | @imaglit | @charlit | @stringlit; + +@operatorexpr = @logicalexpr | @arithmeticexpr | @bitwiseexpr | @unaryexpr | @binaryexpr; + +@logicalexpr = @logicalunaryexpr | @logicalbinaryexpr; + +@arithmeticexpr = @arithmeticunaryexpr | @arithmeticbinaryexpr; + +@bitwiseexpr = @bitwiseunaryexpr | @bitwisebinaryexpr; + +@unaryexpr = @logicalunaryexpr | @bitwiseunaryexpr | @arithmeticunaryexpr | @derefexpr | @addressexpr | @arrowexpr; + +@logicalunaryexpr = @notexpr; + +@bitwiseunaryexpr = @complementexpr; + +@arithmeticunaryexpr = @plusexpr | @minusexpr; + +@binaryexpr = @logicalbinaryexpr | @bitwisebinaryexpr | @arithmeticbinaryexpr | @comparison; + +@logicalbinaryexpr = @lorexpr | @landexpr; + +@bitwisebinaryexpr = @shiftexpr | @orexpr | @xorexpr | @andexpr | @andnotexpr; + +@arithmeticbinaryexpr = @addexpr | @subexpr | @mulexpr | @quoexpr | @remexpr; + +@shiftexpr = @shlexpr | @shrexpr; + +@comparison = @equalitytest | @relationalcomparison; + +@equalitytest = @eqlexpr | @neqexpr; + +@relationalcomparison = @lssexpr | @leqexpr | @gtrexpr | @geqexpr; + +@chantypeexpr = @sendchantypeexpr | @recvchantypeexpr | @sendrcvchantypeexpr; + +case @stmt.kind of + 0 = @badstmt +| 1 = @declstmt +| 2 = @emptystmt +| 3 = @labeledstmt +| 4 = @exprstmt +| 5 = @sendstmt +| 6 = @incstmt +| 7 = @decstmt +| 8 = @gostmt +| 9 = @deferstmt +| 10 = @returnstmt +| 11 = @breakstmt +| 12 = @continuestmt +| 13 = @gotostmt +| 14 = @fallthroughstmt +| 15 = @blockstmt +| 16 = @ifstmt +| 17 = @caseclause +| 18 = @exprswitchstmt +| 19 = @typeswitchstmt +| 20 = @commclause +| 21 = @selectstmt +| 22 = @forstmt +| 23 = @rangestmt +| 24 = @assignstmt +| 25 = @definestmt +| 26 = @addassignstmt +| 27 = @subassignstmt +| 28 = @mulassignstmt +| 29 = @quoassignstmt +| 30 = @remassignstmt +| 31 = @andassignstmt +| 32 = @orassignstmt +| 33 = @xorassignstmt +| 34 = @shlassignstmt +| 35 = @shrassignstmt +| 36 = @andnotassignstmt; + +@incdecstmt = @incstmt | @decstmt; + +@assignment = @simpleassignstmt | @compoundassignstmt; + +@simpleassignstmt = @assignstmt | @definestmt; + +@compoundassignstmt = @addassignstmt | @subassignstmt | @mulassignstmt | @quoassignstmt | @remassignstmt + | @andassignstmt | @orassignstmt | @xorassignstmt | @shlassignstmt | @shrassignstmt | @andnotassignstmt; + +@branchstmt = @breakstmt | @continuestmt | @gotostmt | @fallthroughstmt; + +@switchstmt = @exprswitchstmt | @typeswitchstmt; + +@loopstmt = @forstmt | @rangestmt; + +case @decl.kind of + 0 = @baddecl +| 1 = @importdecl +| 2 = @constdecl +| 3 = @typedecl +| 4 = @vardecl +| 5 = @funcdecl; + +@gendecl = @importdecl | @constdecl | @typedecl | @vardecl; + +case @spec.kind of + 0 = @importspec +| 1 = @valuespec +| 2 = @typespec; + +case @object.kind of + 0 = @pkgobject +| 1 = @decltypeobject +| 2 = @builtintypeobject +| 3 = @declconstobject +| 4 = @builtinconstobject +| 5 = @declvarobject +| 6 = @declfunctionobject +| 7 = @builtinfunctionobject +| 8 = @labelobject; + +@declobject = @decltypeobject | @declconstobject | @declvarobject | @declfunctionobject; + +@builtinobject = @builtintypeobject | @builtinconstobject | @builtinfunctionobject; + +@typeobject = @decltypeobject | @builtintypeobject; + +@valueobject = @constobject | @varobject | @functionobject; + +@constobject = @declconstobject | @builtinconstobject; + +@varobject = @declvarobject; + +@functionobject = @declfunctionobject | @builtinfunctionobject; + +case @scope.kind of + 0 = @universescope +| 1 = @packagescope +| 2 = @localscope; + +case @type.kind of + 0 = @invalidtype +| 1 = @boolexprtype +| 2 = @inttype +| 3 = @int8type +| 4 = @int16type +| 5 = @int32type +| 6 = @int64type +| 7 = @uinttype +| 8 = @uint8type +| 9 = @uint16type +| 10 = @uint32type +| 11 = @uint64type +| 12 = @uintptrtype +| 13 = @float32type +| 14 = @float64type +| 15 = @complex64type +| 16 = @complex128type +| 17 = @stringexprtype +| 18 = @unsafepointertype +| 19 = @boolliteraltype +| 20 = @intliteraltype +| 21 = @runeliteraltype +| 22 = @floatliteraltype +| 23 = @complexliteraltype +| 24 = @stringliteraltype +| 25 = @nilliteraltype +| 26 = @arraytype +| 27 = @slicetype +| 28 = @structtype +| 29 = @pointertype +| 30 = @interfacetype +| 31 = @tupletype +| 32 = @signaturetype +| 33 = @maptype +| 34 = @sendchantype +| 35 = @recvchantype +| 36 = @sendrcvchantype +| 37 = @namedtype; + +@basictype = @booltype | @numerictype | @stringtype | @literaltype | @invalidtype | @uintptrtype | @unsafepointertype; + +@booltype = @boolexprtype | @boolliteraltype; + +@numerictype = @integertype | @floattype | @complextype; + +@integertype = @signedintegertype | @unsignedintegertype; + +@signedintegertype = @inttype | @int8type | @int16type | @int32type | @int64type | @intliteraltype | @runeliteraltype; + +@unsignedintegertype = @uinttype | @uint8type | @uint16type | @uint32type | @uint64type; + +@floattype = @float32type | @float64type | @floatliteraltype; + +@complextype = @complex64type | @complex128type | @complexliteraltype; + +@stringtype = @stringexprtype | @stringliteraltype; + +@literaltype = @boolliteraltype | @intliteraltype | @runeliteraltype | @floatliteraltype | @complexliteraltype + | @stringliteraltype | @nilliteraltype; + +@compositetype = @containertype | @structtype | @pointertype | @interfacetype | @tupletype | @signaturetype | @namedtype; + +@containertype = @arraytype | @slicetype | @maptype | @chantype; + +@chantype = @sendchantype | @recvchantype | @sendrcvchantype; + diff --git a/upgrades/f1263a745347568af228ad3ddb2decb142c3a1a8/upgrade.properties b/upgrades/f1263a745347568af228ad3ddb2decb142c3a1a8/upgrade.properties new file mode 100644 index 00000000000..fc647716b62 --- /dev/null +++ b/upgrades/f1263a745347568af228ad3ddb2decb142c3a1a8/upgrade.properties @@ -0,0 +1,2 @@ +description: Add database tables for go.mod files +compatibility: backwards diff --git a/vendor/golang.org/x/mod/LICENSE b/vendor/golang.org/x/mod/LICENSE new file mode 100644 index 00000000000..6a66aea5eaf --- /dev/null +++ b/vendor/golang.org/x/mod/LICENSE @@ -0,0 +1,27 @@ +Copyright (c) 2009 The Go Authors. All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + + * Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the following disclaimer +in the documentation and/or other materials provided with the +distribution. + * Neither the name of Google Inc. nor the names of its +contributors may be used to endorse or promote products derived from +this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/vendor/golang.org/x/mod/PATENTS b/vendor/golang.org/x/mod/PATENTS new file mode 100644 index 00000000000..733099041f8 --- /dev/null +++ b/vendor/golang.org/x/mod/PATENTS @@ -0,0 +1,22 @@ +Additional IP Rights Grant (Patents) + +"This implementation" means the copyrightable works distributed by +Google as part of the Go project. + +Google hereby grants to You a perpetual, worldwide, non-exclusive, +no-charge, royalty-free, irrevocable (except as stated in this section) +patent license to make, have made, use, offer to sell, sell, import, +transfer and otherwise run, modify and propagate the contents of this +implementation of Go, where such license applies only to those patent +claims, both currently owned or controlled by Google and acquired in +the future, licensable by Google that are necessarily infringed by this +implementation of Go. This grant does not include claims that would be +infringed only as a consequence of further modification of this +implementation. If you or your agent or exclusive licensee institute or +order or agree to the institution of patent litigation against any +entity (including a cross-claim or counterclaim in a lawsuit) alleging +that this implementation of Go or any code incorporated within this +implementation of Go constitutes direct or contributory patent +infringement, or inducement of patent infringement, then any patent +rights granted to you under this License for this implementation of Go +shall terminate as of the date such litigation is filed. diff --git a/vendor/golang.org/x/mod/internal/lazyregexp/lazyre.go b/vendor/golang.org/x/mod/internal/lazyregexp/lazyre.go new file mode 100644 index 00000000000..2681af35af1 --- /dev/null +++ b/vendor/golang.org/x/mod/internal/lazyregexp/lazyre.go @@ -0,0 +1,78 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package lazyregexp is a thin wrapper over regexp, allowing the use of global +// regexp variables without forcing them to be compiled at init. +package lazyregexp + +import ( + "os" + "regexp" + "strings" + "sync" +) + +// Regexp is a wrapper around regexp.Regexp, where the underlying regexp will be +// compiled the first time it is needed. +type Regexp struct { + str string + once sync.Once + rx *regexp.Regexp +} + +func (r *Regexp) re() *regexp.Regexp { + r.once.Do(r.build) + return r.rx +} + +func (r *Regexp) build() { + r.rx = regexp.MustCompile(r.str) + r.str = "" +} + +func (r *Regexp) FindSubmatch(s []byte) [][]byte { + return r.re().FindSubmatch(s) +} + +func (r *Regexp) FindStringSubmatch(s string) []string { + return r.re().FindStringSubmatch(s) +} + +func (r *Regexp) FindStringSubmatchIndex(s string) []int { + return r.re().FindStringSubmatchIndex(s) +} + +func (r *Regexp) ReplaceAllString(src, repl string) string { + return r.re().ReplaceAllString(src, repl) +} + +func (r *Regexp) FindString(s string) string { + return r.re().FindString(s) +} + +func (r *Regexp) FindAllString(s string, n int) []string { + return r.re().FindAllString(s, n) +} + +func (r *Regexp) MatchString(s string) bool { + return r.re().MatchString(s) +} + +func (r *Regexp) SubexpNames() []string { + return r.re().SubexpNames() +} + +var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test") + +// New creates a new lazy regexp, delaying the compiling work until it is first +// needed. If the code is being run as part of tests, the regexp compiling will +// happen immediately. +func New(str string) *Regexp { + lr := &Regexp{str: str} + if inTest { + // In tests, always compile the regexps early. + lr.re() + } + return lr +} diff --git a/vendor/golang.org/x/mod/modfile/print.go b/vendor/golang.org/x/mod/modfile/print.go new file mode 100644 index 00000000000..3bbea38529f --- /dev/null +++ b/vendor/golang.org/x/mod/modfile/print.go @@ -0,0 +1,165 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Module file printer. + +package modfile + +import ( + "bytes" + "fmt" + "strings" +) + +// Format returns a go.mod file as a byte slice, formatted in standard style. +func Format(f *FileSyntax) []byte { + pr := &printer{} + pr.file(f) + return pr.Bytes() +} + +// A printer collects the state during printing of a file or expression. +type printer struct { + bytes.Buffer // output buffer + comment []Comment // pending end-of-line comments + margin int // left margin (indent), a number of tabs +} + +// printf prints to the buffer. +func (p *printer) printf(format string, args ...interface{}) { + fmt.Fprintf(p, format, args...) +} + +// indent returns the position on the current line, in bytes, 0-indexed. +func (p *printer) indent() int { + b := p.Bytes() + n := 0 + for n < len(b) && b[len(b)-1-n] != '\n' { + n++ + } + return n +} + +// newline ends the current line, flushing end-of-line comments. +func (p *printer) newline() { + if len(p.comment) > 0 { + p.printf(" ") + for i, com := range p.comment { + if i > 0 { + p.trim() + p.printf("\n") + for i := 0; i < p.margin; i++ { + p.printf("\t") + } + } + p.printf("%s", strings.TrimSpace(com.Token)) + } + p.comment = p.comment[:0] + } + + p.trim() + p.printf("\n") + for i := 0; i < p.margin; i++ { + p.printf("\t") + } +} + +// trim removes trailing spaces and tabs from the current line. +func (p *printer) trim() { + // Remove trailing spaces and tabs from line we're about to end. + b := p.Bytes() + n := len(b) + for n > 0 && (b[n-1] == '\t' || b[n-1] == ' ') { + n-- + } + p.Truncate(n) +} + +// file formats the given file into the print buffer. +func (p *printer) file(f *FileSyntax) { + for _, com := range f.Before { + p.printf("%s", strings.TrimSpace(com.Token)) + p.newline() + } + + for i, stmt := range f.Stmt { + switch x := stmt.(type) { + case *CommentBlock: + // comments already handled + p.expr(x) + + default: + p.expr(x) + p.newline() + } + + for _, com := range stmt.Comment().After { + p.printf("%s", strings.TrimSpace(com.Token)) + p.newline() + } + + if i+1 < len(f.Stmt) { + p.newline() + } + } +} + +func (p *printer) expr(x Expr) { + // Emit line-comments preceding this expression. + if before := x.Comment().Before; len(before) > 0 { + // Want to print a line comment. + // Line comments must be at the current margin. + p.trim() + if p.indent() > 0 { + // There's other text on the line. Start a new line. + p.printf("\n") + } + // Re-indent to margin. + for i := 0; i < p.margin; i++ { + p.printf("\t") + } + for _, com := range before { + p.printf("%s", strings.TrimSpace(com.Token)) + p.newline() + } + } + + switch x := x.(type) { + default: + panic(fmt.Errorf("printer: unexpected type %T", x)) + + case *CommentBlock: + // done + + case *LParen: + p.printf("(") + case *RParen: + p.printf(")") + + case *Line: + sep := "" + for _, tok := range x.Token { + p.printf("%s%s", sep, tok) + sep = " " + } + + case *LineBlock: + for _, tok := range x.Token { + p.printf("%s ", tok) + } + p.expr(&x.LParen) + p.margin++ + for _, l := range x.Line { + p.newline() + p.expr(l) + } + p.margin-- + p.newline() + p.expr(&x.RParen) + } + + // Queue end-of-line comments for printing when we + // reach the end of the line. + p.comment = append(p.comment, x.Comment().Suffix...) +} diff --git a/vendor/golang.org/x/mod/modfile/read.go b/vendor/golang.org/x/mod/modfile/read.go new file mode 100644 index 00000000000..616d00efdb1 --- /dev/null +++ b/vendor/golang.org/x/mod/modfile/read.go @@ -0,0 +1,909 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Module file parser. +// This is a simplified copy of Google's buildifier parser. + +package modfile + +import ( + "bytes" + "fmt" + "os" + "strconv" + "strings" + "unicode" + "unicode/utf8" +) + +// A Position describes an arbitrary source position in a file, including the +// file, line, column, and byte offset. +type Position struct { + Line int // line in input (starting at 1) + LineRune int // rune in line (starting at 1) + Byte int // byte in input (starting at 0) +} + +// add returns the position at the end of s, assuming it starts at p. +func (p Position) add(s string) Position { + p.Byte += len(s) + if n := strings.Count(s, "\n"); n > 0 { + p.Line += n + s = s[strings.LastIndex(s, "\n")+1:] + p.LineRune = 1 + } + p.LineRune += utf8.RuneCountInString(s) + return p +} + +// An Expr represents an input element. +type Expr interface { + // Span returns the start and end position of the expression, + // excluding leading or trailing comments. + Span() (start, end Position) + + // Comment returns the comments attached to the expression. + // This method would normally be named 'Comments' but that + // would interfere with embedding a type of the same name. + Comment() *Comments +} + +// A Comment represents a single // comment. +type Comment struct { + Start Position + Token string // without trailing newline + Suffix bool // an end of line (not whole line) comment +} + +// Comments collects the comments associated with an expression. +type Comments struct { + Before []Comment // whole-line comments before this expression + Suffix []Comment // end-of-line comments after this expression + + // For top-level expressions only, After lists whole-line + // comments following the expression. + After []Comment +} + +// Comment returns the receiver. This isn't useful by itself, but +// a Comments struct is embedded into all the expression +// implementation types, and this gives each of those a Comment +// method to satisfy the Expr interface. +func (c *Comments) Comment() *Comments { + return c +} + +// A FileSyntax represents an entire go.mod file. +type FileSyntax struct { + Name string // file path + Comments + Stmt []Expr +} + +func (x *FileSyntax) Span() (start, end Position) { + if len(x.Stmt) == 0 { + return + } + start, _ = x.Stmt[0].Span() + _, end = x.Stmt[len(x.Stmt)-1].Span() + return start, end +} + +// addLine adds a line containing the given tokens to the file. +// +// If the first token of the hint matches the first token of the +// line, the new line is added at the end of the block containing hint, +// extracting hint into a new block if it is not yet in one. +// +// If the hint is non-nil buts its first token does not match, +// the new line is added after the block containing hint +// (or hint itself, if not in a block). +// +// If no hint is provided, addLine appends the line to the end of +// the last block with a matching first token, +// or to the end of the file if no such block exists. +func (x *FileSyntax) addLine(hint Expr, tokens ...string) *Line { + if hint == nil { + // If no hint given, add to the last statement of the given type. + Loop: + for i := len(x.Stmt) - 1; i >= 0; i-- { + stmt := x.Stmt[i] + switch stmt := stmt.(type) { + case *Line: + if stmt.Token != nil && stmt.Token[0] == tokens[0] { + hint = stmt + break Loop + } + case *LineBlock: + if stmt.Token[0] == tokens[0] { + hint = stmt + break Loop + } + } + } + } + + newLineAfter := func(i int) *Line { + new := &Line{Token: tokens} + if i == len(x.Stmt) { + x.Stmt = append(x.Stmt, new) + } else { + x.Stmt = append(x.Stmt, nil) + copy(x.Stmt[i+2:], x.Stmt[i+1:]) + x.Stmt[i+1] = new + } + return new + } + + if hint != nil { + for i, stmt := range x.Stmt { + switch stmt := stmt.(type) { + case *Line: + if stmt == hint { + if stmt.Token == nil || stmt.Token[0] != tokens[0] { + return newLineAfter(i) + } + + // Convert line to line block. + stmt.InBlock = true + block := &LineBlock{Token: stmt.Token[:1], Line: []*Line{stmt}} + stmt.Token = stmt.Token[1:] + x.Stmt[i] = block + new := &Line{Token: tokens[1:], InBlock: true} + block.Line = append(block.Line, new) + return new + } + + case *LineBlock: + if stmt == hint { + if stmt.Token[0] != tokens[0] { + return newLineAfter(i) + } + + new := &Line{Token: tokens[1:], InBlock: true} + stmt.Line = append(stmt.Line, new) + return new + } + + for j, line := range stmt.Line { + if line == hint { + if stmt.Token[0] != tokens[0] { + return newLineAfter(i) + } + + // Add new line after hint within the block. + stmt.Line = append(stmt.Line, nil) + copy(stmt.Line[j+2:], stmt.Line[j+1:]) + new := &Line{Token: tokens[1:], InBlock: true} + stmt.Line[j+1] = new + return new + } + } + } + } + } + + new := &Line{Token: tokens} + x.Stmt = append(x.Stmt, new) + return new +} + +func (x *FileSyntax) updateLine(line *Line, tokens ...string) { + if line.InBlock { + tokens = tokens[1:] + } + line.Token = tokens +} + +func (x *FileSyntax) removeLine(line *Line) { + line.Token = nil +} + +// Cleanup cleans up the file syntax x after any edit operations. +// To avoid quadratic behavior, removeLine marks the line as dead +// by setting line.Token = nil but does not remove it from the slice +// in which it appears. After edits have all been indicated, +// calling Cleanup cleans out the dead lines. +func (x *FileSyntax) Cleanup() { + w := 0 + for _, stmt := range x.Stmt { + switch stmt := stmt.(type) { + case *Line: + if stmt.Token == nil { + continue + } + case *LineBlock: + ww := 0 + for _, line := range stmt.Line { + if line.Token != nil { + stmt.Line[ww] = line + ww++ + } + } + if ww == 0 { + continue + } + if ww == 1 { + // Collapse block into single line. + line := &Line{ + Comments: Comments{ + Before: commentsAdd(stmt.Before, stmt.Line[0].Before), + Suffix: commentsAdd(stmt.Line[0].Suffix, stmt.Suffix), + After: commentsAdd(stmt.Line[0].After, stmt.After), + }, + Token: stringsAdd(stmt.Token, stmt.Line[0].Token), + } + x.Stmt[w] = line + w++ + continue + } + stmt.Line = stmt.Line[:ww] + } + x.Stmt[w] = stmt + w++ + } + x.Stmt = x.Stmt[:w] +} + +func commentsAdd(x, y []Comment) []Comment { + return append(x[:len(x):len(x)], y...) +} + +func stringsAdd(x, y []string) []string { + return append(x[:len(x):len(x)], y...) +} + +// A CommentBlock represents a top-level block of comments separate +// from any rule. +type CommentBlock struct { + Comments + Start Position +} + +func (x *CommentBlock) Span() (start, end Position) { + return x.Start, x.Start +} + +// A Line is a single line of tokens. +type Line struct { + Comments + Start Position + Token []string + InBlock bool + End Position +} + +func (x *Line) Span() (start, end Position) { + return x.Start, x.End +} + +// A LineBlock is a factored block of lines, like +// +// require ( +// "x" +// "y" +// ) +// +type LineBlock struct { + Comments + Start Position + LParen LParen + Token []string + Line []*Line + RParen RParen +} + +func (x *LineBlock) Span() (start, end Position) { + return x.Start, x.RParen.Pos.add(")") +} + +// An LParen represents the beginning of a parenthesized line block. +// It is a place to store suffix comments. +type LParen struct { + Comments + Pos Position +} + +func (x *LParen) Span() (start, end Position) { + return x.Pos, x.Pos.add(")") +} + +// An RParen represents the end of a parenthesized line block. +// It is a place to store whole-line (before) comments. +type RParen struct { + Comments + Pos Position +} + +func (x *RParen) Span() (start, end Position) { + return x.Pos, x.Pos.add(")") +} + +// An input represents a single input file being parsed. +type input struct { + // Lexing state. + filename string // name of input file, for errors + complete []byte // entire input + remaining []byte // remaining input + token []byte // token being scanned + lastToken string // most recently returned token, for error messages + pos Position // current input position + comments []Comment // accumulated comments + endRule int // position of end of current rule + + // Parser state. + file *FileSyntax // returned top-level syntax tree + parseError error // error encountered during parsing + + // Comment assignment state. + pre []Expr // all expressions, in preorder traversal + post []Expr // all expressions, in postorder traversal +} + +func newInput(filename string, data []byte) *input { + return &input{ + filename: filename, + complete: data, + remaining: data, + pos: Position{Line: 1, LineRune: 1, Byte: 0}, + } +} + +// parse parses the input file. +func parse(file string, data []byte) (f *FileSyntax, err error) { + in := newInput(file, data) + // The parser panics for both routine errors like syntax errors + // and for programmer bugs like array index errors. + // Turn both into error returns. Catching bug panics is + // especially important when processing many files. + defer func() { + if e := recover(); e != nil { + if e == in.parseError { + err = in.parseError + } else { + err = fmt.Errorf("%s:%d:%d: internal error: %v", in.filename, in.pos.Line, in.pos.LineRune, e) + } + } + }() + + // Invoke the parser. + in.parseFile() + if in.parseError != nil { + return nil, in.parseError + } + in.file.Name = in.filename + + // Assign comments to nearby syntax. + in.assignComments() + + return in.file, nil +} + +// Error is called to report an error. +// The reason s is often "syntax error". +// Error does not return: it panics. +func (in *input) Error(s string) { + if s == "syntax error" && in.lastToken != "" { + s += " near " + in.lastToken + } + in.parseError = fmt.Errorf("%s:%d:%d: %v", in.filename, in.pos.Line, in.pos.LineRune, s) + panic(in.parseError) +} + +// eof reports whether the input has reached end of file. +func (in *input) eof() bool { + return len(in.remaining) == 0 +} + +// peekRune returns the next rune in the input without consuming it. +func (in *input) peekRune() int { + if len(in.remaining) == 0 { + return 0 + } + r, _ := utf8.DecodeRune(in.remaining) + return int(r) +} + +// peekPrefix reports whether the remaining input begins with the given prefix. +func (in *input) peekPrefix(prefix string) bool { + // This is like bytes.HasPrefix(in.remaining, []byte(prefix)) + // but without the allocation of the []byte copy of prefix. + for i := 0; i < len(prefix); i++ { + if i >= len(in.remaining) || in.remaining[i] != prefix[i] { + return false + } + } + return true +} + +// readRune consumes and returns the next rune in the input. +func (in *input) readRune() int { + if len(in.remaining) == 0 { + in.Error("internal lexer error: readRune at EOF") + } + r, size := utf8.DecodeRune(in.remaining) + in.remaining = in.remaining[size:] + if r == '\n' { + in.pos.Line++ + in.pos.LineRune = 1 + } else { + in.pos.LineRune++ + } + in.pos.Byte += size + return int(r) +} + +type symType struct { + pos Position + endPos Position + text string +} + +// startToken marks the beginning of the next input token. +// It must be followed by a call to endToken, once the token has +// been consumed using readRune. +func (in *input) startToken(sym *symType) { + in.token = in.remaining + sym.text = "" + sym.pos = in.pos +} + +// endToken marks the end of an input token. +// It records the actual token string in sym.text if the caller +// has not done that already. +func (in *input) endToken(sym *symType) { + if sym.text == "" { + tok := string(in.token[:len(in.token)-len(in.remaining)]) + sym.text = tok + in.lastToken = sym.text + } + sym.endPos = in.pos +} + +// lex is called from the parser to obtain the next input token. +// It returns the token value (either a rune like '+' or a symbolic token _FOR) +// and sets val to the data associated with the token. +// For all our input tokens, the associated data is +// val.Pos (the position where the token begins) +// and val.Token (the input string corresponding to the token). +func (in *input) lex(sym *symType) int { + // Skip past spaces, stopping at non-space or EOF. + countNL := 0 // number of newlines we've skipped past + for !in.eof() { + // Skip over spaces. Count newlines so we can give the parser + // information about where top-level blank lines are, + // for top-level comment assignment. + c := in.peekRune() + if c == ' ' || c == '\t' || c == '\r' { + in.readRune() + continue + } + + // Comment runs to end of line. + if in.peekPrefix("//") { + in.startToken(sym) + + // Is this comment the only thing on its line? + // Find the last \n before this // and see if it's all + // spaces from there to here. + i := bytes.LastIndex(in.complete[:in.pos.Byte], []byte("\n")) + suffix := len(bytes.TrimSpace(in.complete[i+1:in.pos.Byte])) > 0 + in.readRune() + in.readRune() + + // Consume comment. + for len(in.remaining) > 0 && in.readRune() != '\n' { + } + in.endToken(sym) + + sym.text = strings.TrimRight(sym.text, "\n") + in.lastToken = "comment" + + // If we are at top level (not in a statement), hand the comment to + // the parser as a _COMMENT token. The grammar is written + // to handle top-level comments itself. + if !suffix { + // Not in a statement. Tell parser about top-level comment. + return _COMMENT + } + + // Otherwise, save comment for later attachment to syntax tree. + if countNL > 1 { + in.comments = append(in.comments, Comment{sym.pos, "", false}) + } + in.comments = append(in.comments, Comment{sym.pos, sym.text, suffix}) + countNL = 1 + return _EOL + } + + if in.peekPrefix("/*") { + in.Error(fmt.Sprintf("mod files must use // comments (not /* */ comments)")) + } + + // Found non-space non-comment. + break + } + + // Found the beginning of the next token. + in.startToken(sym) + defer in.endToken(sym) + + // End of file. + if in.eof() { + in.lastToken = "EOF" + return _EOF + } + + // Punctuation tokens. + switch c := in.peekRune(); c { + case '\n': + in.readRune() + return c + + case '(': + in.readRune() + return c + + case ')': + in.readRune() + return c + + case '"', '`': // quoted string + quote := c + in.readRune() + for { + if in.eof() { + in.pos = sym.pos + in.Error("unexpected EOF in string") + } + if in.peekRune() == '\n' { + in.Error("unexpected newline in string") + } + c := in.readRune() + if c == quote { + break + } + if c == '\\' && quote != '`' { + if in.eof() { + in.pos = sym.pos + in.Error("unexpected EOF in string") + } + in.readRune() + } + } + in.endToken(sym) + return _STRING + } + + // Checked all punctuation. Must be identifier token. + if c := in.peekRune(); !isIdent(c) { + in.Error(fmt.Sprintf("unexpected input character %#q", c)) + } + + // Scan over identifier. + for isIdent(in.peekRune()) { + if in.peekPrefix("//") { + break + } + if in.peekPrefix("/*") { + in.Error(fmt.Sprintf("mod files must use // comments (not /* */ comments)")) + } + in.readRune() + } + return _IDENT +} + +// isIdent reports whether c is an identifier rune. +// We treat nearly all runes as identifier runes. +func isIdent(c int) bool { + return c != 0 && !unicode.IsSpace(rune(c)) +} + +// Comment assignment. +// We build two lists of all subexpressions, preorder and postorder. +// The preorder list is ordered by start location, with outer expressions first. +// The postorder list is ordered by end location, with outer expressions last. +// We use the preorder list to assign each whole-line comment to the syntax +// immediately following it, and we use the postorder list to assign each +// end-of-line comment to the syntax immediately preceding it. + +// order walks the expression adding it and its subexpressions to the +// preorder and postorder lists. +func (in *input) order(x Expr) { + if x != nil { + in.pre = append(in.pre, x) + } + switch x := x.(type) { + default: + panic(fmt.Errorf("order: unexpected type %T", x)) + case nil: + // nothing + case *LParen, *RParen: + // nothing + case *CommentBlock: + // nothing + case *Line: + // nothing + case *FileSyntax: + for _, stmt := range x.Stmt { + in.order(stmt) + } + case *LineBlock: + in.order(&x.LParen) + for _, l := range x.Line { + in.order(l) + } + in.order(&x.RParen) + } + if x != nil { + in.post = append(in.post, x) + } +} + +// assignComments attaches comments to nearby syntax. +func (in *input) assignComments() { + const debug = false + + // Generate preorder and postorder lists. + in.order(in.file) + + // Split into whole-line comments and suffix comments. + var line, suffix []Comment + for _, com := range in.comments { + if com.Suffix { + suffix = append(suffix, com) + } else { + line = append(line, com) + } + } + + if debug { + for _, c := range line { + fmt.Fprintf(os.Stderr, "LINE %q :%d:%d #%d\n", c.Token, c.Start.Line, c.Start.LineRune, c.Start.Byte) + } + } + + // Assign line comments to syntax immediately following. + for _, x := range in.pre { + start, _ := x.Span() + if debug { + fmt.Printf("pre %T :%d:%d #%d\n", x, start.Line, start.LineRune, start.Byte) + } + xcom := x.Comment() + for len(line) > 0 && start.Byte >= line[0].Start.Byte { + if debug { + fmt.Fprintf(os.Stderr, "ASSIGN LINE %q #%d\n", line[0].Token, line[0].Start.Byte) + } + xcom.Before = append(xcom.Before, line[0]) + line = line[1:] + } + } + + // Remaining line comments go at end of file. + in.file.After = append(in.file.After, line...) + + if debug { + for _, c := range suffix { + fmt.Fprintf(os.Stderr, "SUFFIX %q :%d:%d #%d\n", c.Token, c.Start.Line, c.Start.LineRune, c.Start.Byte) + } + } + + // Assign suffix comments to syntax immediately before. + for i := len(in.post) - 1; i >= 0; i-- { + x := in.post[i] + + start, end := x.Span() + if debug { + fmt.Printf("post %T :%d:%d #%d :%d:%d #%d\n", x, start.Line, start.LineRune, start.Byte, end.Line, end.LineRune, end.Byte) + } + + // Do not assign suffix comments to end of line block or whole file. + // Instead assign them to the last element inside. + switch x.(type) { + case *FileSyntax: + continue + } + + // Do not assign suffix comments to something that starts + // on an earlier line, so that in + // + // x ( y + // z ) // comment + // + // we assign the comment to z and not to x ( ... ). + if start.Line != end.Line { + continue + } + xcom := x.Comment() + for len(suffix) > 0 && end.Byte <= suffix[len(suffix)-1].Start.Byte { + if debug { + fmt.Fprintf(os.Stderr, "ASSIGN SUFFIX %q #%d\n", suffix[len(suffix)-1].Token, suffix[len(suffix)-1].Start.Byte) + } + xcom.Suffix = append(xcom.Suffix, suffix[len(suffix)-1]) + suffix = suffix[:len(suffix)-1] + } + } + + // We assigned suffix comments in reverse. + // If multiple suffix comments were appended to the same + // expression node, they are now in reverse. Fix that. + for _, x := range in.post { + reverseComments(x.Comment().Suffix) + } + + // Remaining suffix comments go at beginning of file. + in.file.Before = append(in.file.Before, suffix...) +} + +// reverseComments reverses the []Comment list. +func reverseComments(list []Comment) { + for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 { + list[i], list[j] = list[j], list[i] + } +} + +func (in *input) parseFile() { + in.file = new(FileSyntax) + var sym symType + var cb *CommentBlock + for { + tok := in.lex(&sym) + switch tok { + case '\n': + if cb != nil { + in.file.Stmt = append(in.file.Stmt, cb) + cb = nil + } + case _COMMENT: + if cb == nil { + cb = &CommentBlock{Start: sym.pos} + } + com := cb.Comment() + com.Before = append(com.Before, Comment{Start: sym.pos, Token: sym.text}) + case _EOF: + if cb != nil { + in.file.Stmt = append(in.file.Stmt, cb) + } + return + default: + in.parseStmt(&sym) + if cb != nil { + in.file.Stmt[len(in.file.Stmt)-1].Comment().Before = cb.Before + cb = nil + } + } + } +} + +func (in *input) parseStmt(sym *symType) { + start := sym.pos + end := sym.endPos + token := []string{sym.text} + for { + tok := in.lex(sym) + switch tok { + case '\n', _EOF, _EOL: + in.file.Stmt = append(in.file.Stmt, &Line{ + Start: start, + Token: token, + End: end, + }) + return + case '(': + in.file.Stmt = append(in.file.Stmt, in.parseLineBlock(start, token, sym)) + return + default: + token = append(token, sym.text) + end = sym.endPos + } + } +} + +func (in *input) parseLineBlock(start Position, token []string, sym *symType) *LineBlock { + x := &LineBlock{ + Start: start, + Token: token, + LParen: LParen{Pos: sym.pos}, + } + var comments []Comment + for { + tok := in.lex(sym) + switch tok { + case _EOL: + // ignore + case '\n': + if len(comments) == 0 && len(x.Line) > 0 || len(comments) > 0 && comments[len(comments)-1].Token != "" { + comments = append(comments, Comment{}) + } + case _COMMENT: + comments = append(comments, Comment{Start: sym.pos, Token: sym.text}) + case _EOF: + in.Error(fmt.Sprintf("syntax error (unterminated block started at %s:%d:%d)", in.filename, x.Start.Line, x.Start.LineRune)) + case ')': + x.RParen.Before = comments + x.RParen.Pos = sym.pos + tok = in.lex(sym) + if tok != '\n' && tok != _EOF && tok != _EOL { + in.Error("syntax error (expected newline after closing paren)") + } + return x + default: + l := in.parseLine(sym) + x.Line = append(x.Line, l) + l.Comment().Before = comments + comments = nil + } + } +} + +func (in *input) parseLine(sym *symType) *Line { + start := sym.pos + end := sym.endPos + token := []string{sym.text} + for { + tok := in.lex(sym) + switch tok { + case '\n', _EOF, _EOL: + return &Line{ + Start: start, + Token: token, + End: end, + InBlock: true, + } + default: + token = append(token, sym.text) + end = sym.endPos + } + } +} + +const ( + _EOF = -(1 + iota) + _EOL + _IDENT + _STRING + _COMMENT +) + +var ( + slashSlash = []byte("//") + moduleStr = []byte("module") +) + +// ModulePath returns the module path from the gomod file text. +// If it cannot find a module path, it returns an empty string. +// It is tolerant of unrelated problems in the go.mod file. +func ModulePath(mod []byte) string { + for len(mod) > 0 { + line := mod + mod = nil + if i := bytes.IndexByte(line, '\n'); i >= 0 { + line, mod = line[:i], line[i+1:] + } + if i := bytes.Index(line, slashSlash); i >= 0 { + line = line[:i] + } + line = bytes.TrimSpace(line) + if !bytes.HasPrefix(line, moduleStr) { + continue + } + line = line[len(moduleStr):] + n := len(line) + line = bytes.TrimSpace(line) + if len(line) == n || len(line) == 0 { + continue + } + + if line[0] == '"' || line[0] == '`' { + p, err := strconv.Unquote(string(line)) + if err != nil { + return "" // malformed quoted string or multiline module path + } + return p + } + + return string(line) + } + return "" // missing module path +} diff --git a/vendor/golang.org/x/mod/modfile/rule.go b/vendor/golang.org/x/mod/modfile/rule.go new file mode 100644 index 00000000000..62af06889f6 --- /dev/null +++ b/vendor/golang.org/x/mod/modfile/rule.go @@ -0,0 +1,776 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package modfile + +import ( + "bytes" + "errors" + "fmt" + "path/filepath" + "sort" + "strconv" + "strings" + "unicode" + + "golang.org/x/mod/internal/lazyregexp" + "golang.org/x/mod/module" +) + +// A File is the parsed, interpreted form of a go.mod file. +type File struct { + Module *Module + Go *Go + Require []*Require + Exclude []*Exclude + Replace []*Replace + + Syntax *FileSyntax +} + +// A Module is the module statement. +type Module struct { + Mod module.Version + Syntax *Line +} + +// A Go is the go statement. +type Go struct { + Version string // "1.23" + Syntax *Line +} + +// A Require is a single require statement. +type Require struct { + Mod module.Version + Indirect bool // has "// indirect" comment + Syntax *Line +} + +// An Exclude is a single exclude statement. +type Exclude struct { + Mod module.Version + Syntax *Line +} + +// A Replace is a single replace statement. +type Replace struct { + Old module.Version + New module.Version + Syntax *Line +} + +func (f *File) AddModuleStmt(path string) error { + if f.Syntax == nil { + f.Syntax = new(FileSyntax) + } + if f.Module == nil { + f.Module = &Module{ + Mod: module.Version{Path: path}, + Syntax: f.Syntax.addLine(nil, "module", AutoQuote(path)), + } + } else { + f.Module.Mod.Path = path + f.Syntax.updateLine(f.Module.Syntax, "module", AutoQuote(path)) + } + return nil +} + +func (f *File) AddComment(text string) { + if f.Syntax == nil { + f.Syntax = new(FileSyntax) + } + f.Syntax.Stmt = append(f.Syntax.Stmt, &CommentBlock{ + Comments: Comments{ + Before: []Comment{ + { + Token: text, + }, + }, + }, + }) +} + +type VersionFixer func(path, version string) (string, error) + +// Parse parses the data, reported in errors as being from file, +// into a File struct. It applies fix, if non-nil, to canonicalize all module versions found. +func Parse(file string, data []byte, fix VersionFixer) (*File, error) { + return parseToFile(file, data, fix, true) +} + +// ParseLax is like Parse but ignores unknown statements. +// It is used when parsing go.mod files other than the main module, +// under the theory that most statement types we add in the future will +// only apply in the main module, like exclude and replace, +// and so we get better gradual deployments if old go commands +// simply ignore those statements when found in go.mod files +// in dependencies. +func ParseLax(file string, data []byte, fix VersionFixer) (*File, error) { + return parseToFile(file, data, fix, false) +} + +func parseToFile(file string, data []byte, fix VersionFixer, strict bool) (*File, error) { + fs, err := parse(file, data) + if err != nil { + return nil, err + } + f := &File{ + Syntax: fs, + } + + var errs bytes.Buffer + for _, x := range fs.Stmt { + switch x := x.(type) { + case *Line: + f.add(&errs, x, x.Token[0], x.Token[1:], fix, strict) + + case *LineBlock: + if len(x.Token) > 1 { + if strict { + fmt.Fprintf(&errs, "%s:%d: unknown block type: %s\n", file, x.Start.Line, strings.Join(x.Token, " ")) + } + continue + } + switch x.Token[0] { + default: + if strict { + fmt.Fprintf(&errs, "%s:%d: unknown block type: %s\n", file, x.Start.Line, strings.Join(x.Token, " ")) + } + continue + case "module", "require", "exclude", "replace": + for _, l := range x.Line { + f.add(&errs, l, x.Token[0], l.Token, fix, strict) + } + } + } + } + + if errs.Len() > 0 { + return nil, errors.New(strings.TrimRight(errs.String(), "\n")) + } + return f, nil +} + +var GoVersionRE = lazyregexp.New(`^([1-9][0-9]*)\.(0|[1-9][0-9]*)$`) + +func (f *File) add(errs *bytes.Buffer, line *Line, verb string, args []string, fix VersionFixer, strict bool) { + // If strict is false, this module is a dependency. + // We ignore all unknown directives as well as main-module-only + // directives like replace and exclude. It will work better for + // forward compatibility if we can depend on modules that have unknown + // statements (presumed relevant only when acting as the main module) + // and simply ignore those statements. + if !strict { + switch verb { + case "module", "require", "go": + // want these even for dependency go.mods + default: + return + } + } + + switch verb { + default: + fmt.Fprintf(errs, "%s:%d: unknown directive: %s\n", f.Syntax.Name, line.Start.Line, verb) + + case "go": + if f.Go != nil { + fmt.Fprintf(errs, "%s:%d: repeated go statement\n", f.Syntax.Name, line.Start.Line) + return + } + if len(args) != 1 || !GoVersionRE.MatchString(args[0]) { + fmt.Fprintf(errs, "%s:%d: usage: go 1.23\n", f.Syntax.Name, line.Start.Line) + return + } + f.Go = &Go{Syntax: line} + f.Go.Version = args[0] + case "module": + if f.Module != nil { + fmt.Fprintf(errs, "%s:%d: repeated module statement\n", f.Syntax.Name, line.Start.Line) + return + } + f.Module = &Module{Syntax: line} + if len(args) != 1 { + + fmt.Fprintf(errs, "%s:%d: usage: module module/path\n", f.Syntax.Name, line.Start.Line) + return + } + s, err := parseString(&args[0]) + if err != nil { + fmt.Fprintf(errs, "%s:%d: invalid quoted string: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + f.Module.Mod = module.Version{Path: s} + case "require", "exclude": + if len(args) != 2 { + fmt.Fprintf(errs, "%s:%d: usage: %s module/path v1.2.3\n", f.Syntax.Name, line.Start.Line, verb) + return + } + s, err := parseString(&args[0]) + if err != nil { + fmt.Fprintf(errs, "%s:%d: invalid quoted string: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + v, err := parseVersion(verb, s, &args[1], fix) + if err != nil { + fmt.Fprintf(errs, "%s:%d: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + pathMajor, err := modulePathMajor(s) + if err != nil { + fmt.Fprintf(errs, "%s:%d: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + if err := module.CheckPathMajor(v, pathMajor); err != nil { + fmt.Fprintf(errs, "%s:%d: %v\n", f.Syntax.Name, line.Start.Line, &Error{Verb: verb, ModPath: s, Err: err}) + return + } + if verb == "require" { + f.Require = append(f.Require, &Require{ + Mod: module.Version{Path: s, Version: v}, + Syntax: line, + Indirect: isIndirect(line), + }) + } else { + f.Exclude = append(f.Exclude, &Exclude{ + Mod: module.Version{Path: s, Version: v}, + Syntax: line, + }) + } + case "replace": + arrow := 2 + if len(args) >= 2 && args[1] == "=>" { + arrow = 1 + } + if len(args) < arrow+2 || len(args) > arrow+3 || args[arrow] != "=>" { + fmt.Fprintf(errs, "%s:%d: usage: %s module/path [v1.2.3] => other/module v1.4\n\t or %s module/path [v1.2.3] => ../local/directory\n", f.Syntax.Name, line.Start.Line, verb, verb) + return + } + s, err := parseString(&args[0]) + if err != nil { + fmt.Fprintf(errs, "%s:%d: invalid quoted string: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + pathMajor, err := modulePathMajor(s) + if err != nil { + fmt.Fprintf(errs, "%s:%d: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + var v string + if arrow == 2 { + v, err = parseVersion(verb, s, &args[1], fix) + if err != nil { + fmt.Fprintf(errs, "%s:%d: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + if err := module.CheckPathMajor(v, pathMajor); err != nil { + fmt.Fprintf(errs, "%s:%d: %v\n", f.Syntax.Name, line.Start.Line, &Error{Verb: verb, ModPath: s, Err: err}) + return + } + } + ns, err := parseString(&args[arrow+1]) + if err != nil { + fmt.Fprintf(errs, "%s:%d: invalid quoted string: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + nv := "" + if len(args) == arrow+2 { + if !IsDirectoryPath(ns) { + fmt.Fprintf(errs, "%s:%d: replacement module without version must be directory path (rooted or starting with ./ or ../)\n", f.Syntax.Name, line.Start.Line) + return + } + if filepath.Separator == '/' && strings.Contains(ns, `\`) { + fmt.Fprintf(errs, "%s:%d: replacement directory appears to be Windows path (on a non-windows system)\n", f.Syntax.Name, line.Start.Line) + return + } + } + if len(args) == arrow+3 { + nv, err = parseVersion(verb, ns, &args[arrow+2], fix) + if err != nil { + fmt.Fprintf(errs, "%s:%d: %v\n", f.Syntax.Name, line.Start.Line, err) + return + } + if IsDirectoryPath(ns) { + fmt.Fprintf(errs, "%s:%d: replacement module directory path %q cannot have version\n", f.Syntax.Name, line.Start.Line, ns) + return + } + } + f.Replace = append(f.Replace, &Replace{ + Old: module.Version{Path: s, Version: v}, + New: module.Version{Path: ns, Version: nv}, + Syntax: line, + }) + } +} + +// isIndirect reports whether line has a "// indirect" comment, +// meaning it is in go.mod only for its effect on indirect dependencies, +// so that it can be dropped entirely once the effective version of the +// indirect dependency reaches the given minimum version. +func isIndirect(line *Line) bool { + if len(line.Suffix) == 0 { + return false + } + f := strings.Fields(strings.TrimPrefix(line.Suffix[0].Token, string(slashSlash))) + return (len(f) == 1 && f[0] == "indirect" || len(f) > 1 && f[0] == "indirect;") +} + +// setIndirect sets line to have (or not have) a "// indirect" comment. +func setIndirect(line *Line, indirect bool) { + if isIndirect(line) == indirect { + return + } + if indirect { + // Adding comment. + if len(line.Suffix) == 0 { + // New comment. + line.Suffix = []Comment{{Token: "// indirect", Suffix: true}} + return + } + + com := &line.Suffix[0] + text := strings.TrimSpace(strings.TrimPrefix(com.Token, string(slashSlash))) + if text == "" { + // Empty comment. + com.Token = "// indirect" + return + } + + // Insert at beginning of existing comment. + com.Token = "// indirect; " + text + return + } + + // Removing comment. + f := strings.Fields(line.Suffix[0].Token) + if len(f) == 2 { + // Remove whole comment. + line.Suffix = nil + return + } + + // Remove comment prefix. + com := &line.Suffix[0] + i := strings.Index(com.Token, "indirect;") + com.Token = "//" + com.Token[i+len("indirect;"):] +} + +// IsDirectoryPath reports whether the given path should be interpreted +// as a directory path. Just like on the go command line, relative paths +// and rooted paths are directory paths; the rest are module paths. +func IsDirectoryPath(ns string) bool { + // Because go.mod files can move from one system to another, + // we check all known path syntaxes, both Unix and Windows. + return strings.HasPrefix(ns, "./") || strings.HasPrefix(ns, "../") || strings.HasPrefix(ns, "/") || + strings.HasPrefix(ns, `.\`) || strings.HasPrefix(ns, `..\`) || strings.HasPrefix(ns, `\`) || + len(ns) >= 2 && ('A' <= ns[0] && ns[0] <= 'Z' || 'a' <= ns[0] && ns[0] <= 'z') && ns[1] == ':' +} + +// MustQuote reports whether s must be quoted in order to appear as +// a single token in a go.mod line. +func MustQuote(s string) bool { + for _, r := range s { + if !unicode.IsPrint(r) || r == ' ' || r == '"' || r == '\'' || r == '`' { + return true + } + } + return s == "" || strings.Contains(s, "//") || strings.Contains(s, "/*") +} + +// AutoQuote returns s or, if quoting is required for s to appear in a go.mod, +// the quotation of s. +func AutoQuote(s string) string { + if MustQuote(s) { + return strconv.Quote(s) + } + return s +} + +func parseString(s *string) (string, error) { + t := *s + if strings.HasPrefix(t, `"`) { + var err error + if t, err = strconv.Unquote(t); err != nil { + return "", err + } + } else if strings.ContainsAny(t, "\"'`") { + // Other quotes are reserved both for possible future expansion + // and to avoid confusion. For example if someone types 'x' + // we want that to be a syntax error and not a literal x in literal quotation marks. + return "", fmt.Errorf("unquoted string cannot contain quote") + } + *s = AutoQuote(t) + return t, nil +} + +type Error struct { + Verb string + ModPath string + Err error +} + +func (e *Error) Error() string { + return fmt.Sprintf("%s %s: %v", e.Verb, e.ModPath, e.Err) +} + +func (e *Error) Unwrap() error { return e.Err } + +func parseVersion(verb string, path string, s *string, fix VersionFixer) (string, error) { + t, err := parseString(s) + if err != nil { + return "", &Error{ + Verb: verb, + ModPath: path, + Err: &module.InvalidVersionError{ + Version: *s, + Err: err, + }, + } + } + if fix != nil { + var err error + t, err = fix(path, t) + if err != nil { + if err, ok := err.(*module.ModuleError); ok { + return "", &Error{ + Verb: verb, + ModPath: path, + Err: err.Err, + } + } + return "", err + } + } + if v := module.CanonicalVersion(t); v != "" { + *s = v + return *s, nil + } + return "", &Error{ + Verb: verb, + ModPath: path, + Err: &module.InvalidVersionError{ + Version: t, + Err: errors.New("must be of the form v1.2.3"), + }, + } +} + +func modulePathMajor(path string) (string, error) { + _, major, ok := module.SplitPathVersion(path) + if !ok { + return "", fmt.Errorf("invalid module path") + } + return major, nil +} + +func (f *File) Format() ([]byte, error) { + return Format(f.Syntax), nil +} + +// Cleanup cleans up the file f after any edit operations. +// To avoid quadratic behavior, modifications like DropRequire +// clear the entry but do not remove it from the slice. +// Cleanup cleans out all the cleared entries. +func (f *File) Cleanup() { + w := 0 + for _, r := range f.Require { + if r.Mod.Path != "" { + f.Require[w] = r + w++ + } + } + f.Require = f.Require[:w] + + w = 0 + for _, x := range f.Exclude { + if x.Mod.Path != "" { + f.Exclude[w] = x + w++ + } + } + f.Exclude = f.Exclude[:w] + + w = 0 + for _, r := range f.Replace { + if r.Old.Path != "" { + f.Replace[w] = r + w++ + } + } + f.Replace = f.Replace[:w] + + f.Syntax.Cleanup() +} + +func (f *File) AddGoStmt(version string) error { + if !GoVersionRE.MatchString(version) { + return fmt.Errorf("invalid language version string %q", version) + } + if f.Go == nil { + var hint Expr + if f.Module != nil && f.Module.Syntax != nil { + hint = f.Module.Syntax + } + f.Go = &Go{ + Version: version, + Syntax: f.Syntax.addLine(hint, "go", version), + } + } else { + f.Go.Version = version + f.Syntax.updateLine(f.Go.Syntax, "go", version) + } + return nil +} + +func (f *File) AddRequire(path, vers string) error { + need := true + for _, r := range f.Require { + if r.Mod.Path == path { + if need { + r.Mod.Version = vers + f.Syntax.updateLine(r.Syntax, "require", AutoQuote(path), vers) + need = false + } else { + f.Syntax.removeLine(r.Syntax) + *r = Require{} + } + } + } + + if need { + f.AddNewRequire(path, vers, false) + } + return nil +} + +func (f *File) AddNewRequire(path, vers string, indirect bool) { + line := f.Syntax.addLine(nil, "require", AutoQuote(path), vers) + setIndirect(line, indirect) + f.Require = append(f.Require, &Require{module.Version{Path: path, Version: vers}, indirect, line}) +} + +func (f *File) SetRequire(req []*Require) { + need := make(map[string]string) + indirect := make(map[string]bool) + for _, r := range req { + need[r.Mod.Path] = r.Mod.Version + indirect[r.Mod.Path] = r.Indirect + } + + for _, r := range f.Require { + if v, ok := need[r.Mod.Path]; ok { + r.Mod.Version = v + r.Indirect = indirect[r.Mod.Path] + } else { + *r = Require{} + } + } + + var newStmts []Expr + for _, stmt := range f.Syntax.Stmt { + switch stmt := stmt.(type) { + case *LineBlock: + if len(stmt.Token) > 0 && stmt.Token[0] == "require" { + var newLines []*Line + for _, line := range stmt.Line { + if p, err := parseString(&line.Token[0]); err == nil && need[p] != "" { + if len(line.Comments.Before) == 1 && len(line.Comments.Before[0].Token) == 0 { + line.Comments.Before = line.Comments.Before[:0] + } + line.Token[1] = need[p] + delete(need, p) + setIndirect(line, indirect[p]) + newLines = append(newLines, line) + } + } + if len(newLines) == 0 { + continue // drop stmt + } + stmt.Line = newLines + } + + case *Line: + if len(stmt.Token) > 0 && stmt.Token[0] == "require" { + if p, err := parseString(&stmt.Token[1]); err == nil && need[p] != "" { + stmt.Token[2] = need[p] + delete(need, p) + setIndirect(stmt, indirect[p]) + } else { + continue // drop stmt + } + } + } + newStmts = append(newStmts, stmt) + } + f.Syntax.Stmt = newStmts + + for path, vers := range need { + f.AddNewRequire(path, vers, indirect[path]) + } + f.SortBlocks() +} + +func (f *File) DropRequire(path string) error { + for _, r := range f.Require { + if r.Mod.Path == path { + f.Syntax.removeLine(r.Syntax) + *r = Require{} + } + } + return nil +} + +func (f *File) AddExclude(path, vers string) error { + var hint *Line + for _, x := range f.Exclude { + if x.Mod.Path == path && x.Mod.Version == vers { + return nil + } + if x.Mod.Path == path { + hint = x.Syntax + } + } + + f.Exclude = append(f.Exclude, &Exclude{Mod: module.Version{Path: path, Version: vers}, Syntax: f.Syntax.addLine(hint, "exclude", AutoQuote(path), vers)}) + return nil +} + +func (f *File) DropExclude(path, vers string) error { + for _, x := range f.Exclude { + if x.Mod.Path == path && x.Mod.Version == vers { + f.Syntax.removeLine(x.Syntax) + *x = Exclude{} + } + } + return nil +} + +func (f *File) AddReplace(oldPath, oldVers, newPath, newVers string) error { + need := true + old := module.Version{Path: oldPath, Version: oldVers} + new := module.Version{Path: newPath, Version: newVers} + tokens := []string{"replace", AutoQuote(oldPath)} + if oldVers != "" { + tokens = append(tokens, oldVers) + } + tokens = append(tokens, "=>", AutoQuote(newPath)) + if newVers != "" { + tokens = append(tokens, newVers) + } + + var hint *Line + for _, r := range f.Replace { + if r.Old.Path == oldPath && (oldVers == "" || r.Old.Version == oldVers) { + if need { + // Found replacement for old; update to use new. + r.New = new + f.Syntax.updateLine(r.Syntax, tokens...) + need = false + continue + } + // Already added; delete other replacements for same. + f.Syntax.removeLine(r.Syntax) + *r = Replace{} + } + if r.Old.Path == oldPath { + hint = r.Syntax + } + } + if need { + f.Replace = append(f.Replace, &Replace{Old: old, New: new, Syntax: f.Syntax.addLine(hint, tokens...)}) + } + return nil +} + +func (f *File) DropReplace(oldPath, oldVers string) error { + for _, r := range f.Replace { + if r.Old.Path == oldPath && r.Old.Version == oldVers { + f.Syntax.removeLine(r.Syntax) + *r = Replace{} + } + } + return nil +} + +func (f *File) SortBlocks() { + f.removeDups() // otherwise sorting is unsafe + + for _, stmt := range f.Syntax.Stmt { + block, ok := stmt.(*LineBlock) + if !ok { + continue + } + sort.Slice(block.Line, func(i, j int) bool { + li := block.Line[i] + lj := block.Line[j] + for k := 0; k < len(li.Token) && k < len(lj.Token); k++ { + if li.Token[k] != lj.Token[k] { + return li.Token[k] < lj.Token[k] + } + } + return len(li.Token) < len(lj.Token) + }) + } +} + +func (f *File) removeDups() { + have := make(map[module.Version]bool) + kill := make(map[*Line]bool) + for _, x := range f.Exclude { + if have[x.Mod] { + kill[x.Syntax] = true + continue + } + have[x.Mod] = true + } + var excl []*Exclude + for _, x := range f.Exclude { + if !kill[x.Syntax] { + excl = append(excl, x) + } + } + f.Exclude = excl + + have = make(map[module.Version]bool) + // Later replacements take priority over earlier ones. + for i := len(f.Replace) - 1; i >= 0; i-- { + x := f.Replace[i] + if have[x.Old] { + kill[x.Syntax] = true + continue + } + have[x.Old] = true + } + var repl []*Replace + for _, x := range f.Replace { + if !kill[x.Syntax] { + repl = append(repl, x) + } + } + f.Replace = repl + + var stmts []Expr + for _, stmt := range f.Syntax.Stmt { + switch stmt := stmt.(type) { + case *Line: + if kill[stmt] { + continue + } + case *LineBlock: + var lines []*Line + for _, line := range stmt.Line { + if !kill[line] { + lines = append(lines, line) + } + } + stmt.Line = lines + if len(lines) == 0 { + continue + } + } + stmts = append(stmts, stmt) + } + f.Syntax.Stmt = stmts +} diff --git a/vendor/golang.org/x/mod/module/module.go b/vendor/golang.org/x/mod/module/module.go new file mode 100644 index 00000000000..6cd37280a85 --- /dev/null +++ b/vendor/golang.org/x/mod/module/module.go @@ -0,0 +1,718 @@ +// Copyright 2018 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package module defines the module.Version type along with support code. +// +// The module.Version type is a simple Path, Version pair: +// +// type Version struct { +// Path string +// Version string +// } +// +// There are no restrictions imposed directly by use of this structure, +// but additional checking functions, most notably Check, verify that +// a particular path, version pair is valid. +// +// Escaped Paths +// +// Module paths appear as substrings of file system paths +// (in the download cache) and of web server URLs in the proxy protocol. +// In general we cannot rely on file systems to be case-sensitive, +// nor can we rely on web servers, since they read from file systems. +// That is, we cannot rely on the file system to keep rsc.io/QUOTE +// and rsc.io/quote separate. Windows and macOS don't. +// Instead, we must never require two different casings of a file path. +// Because we want the download cache to match the proxy protocol, +// and because we want the proxy protocol to be possible to serve +// from a tree of static files (which might be stored on a case-insensitive +// file system), the proxy protocol must never require two different casings +// of a URL path either. +// +// One possibility would be to make the escaped form be the lowercase +// hexadecimal encoding of the actual path bytes. This would avoid ever +// needing different casings of a file path, but it would be fairly illegible +// to most programmers when those paths appeared in the file system +// (including in file paths in compiler errors and stack traces) +// in web server logs, and so on. Instead, we want a safe escaped form that +// leaves most paths unaltered. +// +// The safe escaped form is to replace every uppercase letter +// with an exclamation mark followed by the letter's lowercase equivalent. +// +// For example, +// +// github.com/Azure/azure-sdk-for-go -> github.com/!azure/azure-sdk-for-go. +// github.com/GoogleCloudPlatform/cloudsql-proxy -> github.com/!google!cloud!platform/cloudsql-proxy +// github.com/Sirupsen/logrus -> github.com/!sirupsen/logrus. +// +// Import paths that avoid upper-case letters are left unchanged. +// Note that because import paths are ASCII-only and avoid various +// problematic punctuation (like : < and >), the escaped form is also ASCII-only +// and avoids the same problematic punctuation. +// +// Import paths have never allowed exclamation marks, so there is no +// need to define how to escape a literal !. +// +// Unicode Restrictions +// +// Today, paths are disallowed from using Unicode. +// +// Although paths are currently disallowed from using Unicode, +// we would like at some point to allow Unicode letters as well, to assume that +// file systems and URLs are Unicode-safe (storing UTF-8), and apply +// the !-for-uppercase convention for escaping them in the file system. +// But there are at least two subtle considerations. +// +// First, note that not all case-fold equivalent distinct runes +// form an upper/lower pair. +// For example, U+004B ('K'), U+006B ('k'), and U+212A ('K' for Kelvin) +// are three distinct runes that case-fold to each other. +// When we do add Unicode letters, we must not assume that upper/lower +// are the only case-equivalent pairs. +// Perhaps the Kelvin symbol would be disallowed entirely, for example. +// Or perhaps it would escape as "!!k", or perhaps as "(212A)". +// +// Second, it would be nice to allow Unicode marks as well as letters, +// but marks include combining marks, and then we must deal not +// only with case folding but also normalization: both U+00E9 ('é') +// and U+0065 U+0301 ('e' followed by combining acute accent) +// look the same on the page and are treated by some file systems +// as the same path. If we do allow Unicode marks in paths, there +// must be some kind of normalization to allow only one canonical +// encoding of any character used in an import path. +package module + +// IMPORTANT NOTE +// +// This file essentially defines the set of valid import paths for the go command. +// There are many subtle considerations, including Unicode ambiguity, +// security, network, and file system representations. +// +// This file also defines the set of valid module path and version combinations, +// another topic with many subtle considerations. +// +// Changes to the semantics in this file require approval from rsc. + +import ( + "fmt" + "sort" + "strings" + "unicode" + "unicode/utf8" + + "golang.org/x/mod/semver" + errors "golang.org/x/xerrors" +) + +// A Version (for clients, a module.Version) is defined by a module path and version pair. +// These are stored in their plain (unescaped) form. +type Version struct { + // Path is a module path, like "golang.org/x/text" or "rsc.io/quote/v2". + Path string + + // Version is usually a semantic version in canonical form. + // There are three exceptions to this general rule. + // First, the top-level target of a build has no specific version + // and uses Version = "". + // Second, during MVS calculations the version "none" is used + // to represent the decision to take no version of a given module. + // Third, filesystem paths found in "replace" directives are + // represented by a path with an empty version. + Version string `json:",omitempty"` +} + +// String returns a representation of the Version suitable for logging +// (Path@Version, or just Path if Version is empty). +func (m Version) String() string { + if m.Version == "" { + return m.Path + } + return m.Path + "@" + m.Version +} + +// A ModuleError indicates an error specific to a module. +type ModuleError struct { + Path string + Version string + Err error +} + +// VersionError returns a ModuleError derived from a Version and error, +// or err itself if it is already such an error. +func VersionError(v Version, err error) error { + var mErr *ModuleError + if errors.As(err, &mErr) && mErr.Path == v.Path && mErr.Version == v.Version { + return err + } + return &ModuleError{ + Path: v.Path, + Version: v.Version, + Err: err, + } +} + +func (e *ModuleError) Error() string { + if v, ok := e.Err.(*InvalidVersionError); ok { + return fmt.Sprintf("%s@%s: invalid %s: %v", e.Path, v.Version, v.noun(), v.Err) + } + if e.Version != "" { + return fmt.Sprintf("%s@%s: %v", e.Path, e.Version, e.Err) + } + return fmt.Sprintf("module %s: %v", e.Path, e.Err) +} + +func (e *ModuleError) Unwrap() error { return e.Err } + +// An InvalidVersionError indicates an error specific to a version, with the +// module path unknown or specified externally. +// +// A ModuleError may wrap an InvalidVersionError, but an InvalidVersionError +// must not wrap a ModuleError. +type InvalidVersionError struct { + Version string + Pseudo bool + Err error +} + +// noun returns either "version" or "pseudo-version", depending on whether +// e.Version is a pseudo-version. +func (e *InvalidVersionError) noun() string { + if e.Pseudo { + return "pseudo-version" + } + return "version" +} + +func (e *InvalidVersionError) Error() string { + return fmt.Sprintf("%s %q invalid: %s", e.noun(), e.Version, e.Err) +} + +func (e *InvalidVersionError) Unwrap() error { return e.Err } + +// Check checks that a given module path, version pair is valid. +// In addition to the path being a valid module path +// and the version being a valid semantic version, +// the two must correspond. +// For example, the path "yaml/v2" only corresponds to +// semantic versions beginning with "v2.". +func Check(path, version string) error { + if err := CheckPath(path); err != nil { + return err + } + if !semver.IsValid(version) { + return &ModuleError{ + Path: path, + Err: &InvalidVersionError{Version: version, Err: errors.New("not a semantic version")}, + } + } + _, pathMajor, _ := SplitPathVersion(path) + if err := CheckPathMajor(version, pathMajor); err != nil { + return &ModuleError{Path: path, Err: err} + } + return nil +} + +// firstPathOK reports whether r can appear in the first element of a module path. +// The first element of the path must be an LDH domain name, at least for now. +// To avoid case ambiguity, the domain name must be entirely lower case. +func firstPathOK(r rune) bool { + return r == '-' || r == '.' || + '0' <= r && r <= '9' || + 'a' <= r && r <= 'z' +} + +// pathOK reports whether r can appear in an import path element. +// Paths can be ASCII letters, ASCII digits, and limited ASCII punctuation: + - . _ and ~. +// This matches what "go get" has historically recognized in import paths. +// TODO(rsc): We would like to allow Unicode letters, but that requires additional +// care in the safe encoding (see "escaped paths" above). +func pathOK(r rune) bool { + if r < utf8.RuneSelf { + return r == '+' || r == '-' || r == '.' || r == '_' || r == '~' || + '0' <= r && r <= '9' || + 'A' <= r && r <= 'Z' || + 'a' <= r && r <= 'z' + } + return false +} + +// fileNameOK reports whether r can appear in a file name. +// For now we allow all Unicode letters but otherwise limit to pathOK plus a few more punctuation characters. +// If we expand the set of allowed characters here, we have to +// work harder at detecting potential case-folding and normalization collisions. +// See note about "escaped paths" above. +func fileNameOK(r rune) bool { + if r < utf8.RuneSelf { + // Entire set of ASCII punctuation, from which we remove characters: + // ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ + // We disallow some shell special characters: " ' * < > ? ` | + // (Note that some of those are disallowed by the Windows file system as well.) + // We also disallow path separators / : and \ (fileNameOK is only called on path element characters). + // We allow spaces (U+0020) in file names. + const allowed = "!#$%&()+,-.=@[]^_{}~ " + if '0' <= r && r <= '9' || 'A' <= r && r <= 'Z' || 'a' <= r && r <= 'z' { + return true + } + for i := 0; i < len(allowed); i++ { + if rune(allowed[i]) == r { + return true + } + } + return false + } + // It may be OK to add more ASCII punctuation here, but only carefully. + // For example Windows disallows < > \, and macOS disallows :, so we must not allow those. + return unicode.IsLetter(r) +} + +// CheckPath checks that a module path is valid. +// A valid module path is a valid import path, as checked by CheckImportPath, +// with two additional constraints. +// First, the leading path element (up to the first slash, if any), +// by convention a domain name, must contain only lower-case ASCII letters, +// ASCII digits, dots (U+002E), and dashes (U+002D); +// it must contain at least one dot and cannot start with a dash. +// Second, for a final path element of the form /vN, where N looks numeric +// (ASCII digits and dots) must not begin with a leading zero, must not be /v1, +// and must not contain any dots. For paths beginning with "gopkg.in/", +// this second requirement is replaced by a requirement that the path +// follow the gopkg.in server's conventions. +func CheckPath(path string) error { + if err := checkPath(path, false); err != nil { + return fmt.Errorf("malformed module path %q: %v", path, err) + } + i := strings.Index(path, "/") + if i < 0 { + i = len(path) + } + if i == 0 { + return fmt.Errorf("malformed module path %q: leading slash", path) + } + if !strings.Contains(path[:i], ".") { + return fmt.Errorf("malformed module path %q: missing dot in first path element", path) + } + if path[0] == '-' { + return fmt.Errorf("malformed module path %q: leading dash in first path element", path) + } + for _, r := range path[:i] { + if !firstPathOK(r) { + return fmt.Errorf("malformed module path %q: invalid char %q in first path element", path, r) + } + } + if _, _, ok := SplitPathVersion(path); !ok { + return fmt.Errorf("malformed module path %q: invalid version", path) + } + return nil +} + +// CheckImportPath checks that an import path is valid. +// +// A valid import path consists of one or more valid path elements +// separated by slashes (U+002F). (It must not begin with nor end in a slash.) +// +// A valid path element is a non-empty string made up of +// ASCII letters, ASCII digits, and limited ASCII punctuation: + - . _ and ~. +// It must not begin or end with a dot (U+002E), nor contain two dots in a row. +// +// The element prefix up to the first dot must not be a reserved file name +// on Windows, regardless of case (CON, com1, NuL, and so on). +// +// CheckImportPath may be less restrictive in the future, but see the +// top-level package documentation for additional information about +// subtleties of Unicode. +func CheckImportPath(path string) error { + if err := checkPath(path, false); err != nil { + return fmt.Errorf("malformed import path %q: %v", path, err) + } + return nil +} + +// checkPath checks that a general path is valid. +// It returns an error describing why but not mentioning path. +// Because these checks apply to both module paths and import paths, +// the caller is expected to add the "malformed ___ path %q: " prefix. +// fileName indicates whether the final element of the path is a file name +// (as opposed to a directory name). +func checkPath(path string, fileName bool) error { + if !utf8.ValidString(path) { + return fmt.Errorf("invalid UTF-8") + } + if path == "" { + return fmt.Errorf("empty string") + } + if path[0] == '-' { + return fmt.Errorf("leading dash") + } + if strings.Contains(path, "//") { + return fmt.Errorf("double slash") + } + if path[len(path)-1] == '/' { + return fmt.Errorf("trailing slash") + } + elemStart := 0 + for i, r := range path { + if r == '/' { + if err := checkElem(path[elemStart:i], fileName); err != nil { + return err + } + elemStart = i + 1 + } + } + if err := checkElem(path[elemStart:], fileName); err != nil { + return err + } + return nil +} + +// checkElem checks whether an individual path element is valid. +// fileName indicates whether the element is a file name (not a directory name). +func checkElem(elem string, fileName bool) error { + if elem == "" { + return fmt.Errorf("empty path element") + } + if strings.Count(elem, ".") == len(elem) { + return fmt.Errorf("invalid path element %q", elem) + } + if elem[0] == '.' && !fileName { + return fmt.Errorf("leading dot in path element") + } + if elem[len(elem)-1] == '.' { + return fmt.Errorf("trailing dot in path element") + } + charOK := pathOK + if fileName { + charOK = fileNameOK + } + for _, r := range elem { + if !charOK(r) { + return fmt.Errorf("invalid char %q", r) + } + } + + // Windows disallows a bunch of path elements, sadly. + // See https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file + short := elem + if i := strings.Index(short, "."); i >= 0 { + short = short[:i] + } + for _, bad := range badWindowsNames { + if strings.EqualFold(bad, short) { + return fmt.Errorf("%q disallowed as path element component on Windows", short) + } + } + return nil +} + +// CheckFilePath checks that a slash-separated file path is valid. +// The definition of a valid file path is the same as the definition +// of a valid import path except that the set of allowed characters is larger: +// all Unicode letters, ASCII digits, the ASCII space character (U+0020), +// and the ASCII punctuation characters +// “!#$%&()+,-.=@[]^_{}~”. +// (The excluded punctuation characters, " * < > ? ` ' | / \ and :, +// have special meanings in certain shells or operating systems.) +// +// CheckFilePath may be less restrictive in the future, but see the +// top-level package documentation for additional information about +// subtleties of Unicode. +func CheckFilePath(path string) error { + if err := checkPath(path, true); err != nil { + return fmt.Errorf("malformed file path %q: %v", path, err) + } + return nil +} + +// badWindowsNames are the reserved file path elements on Windows. +// See https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file +var badWindowsNames = []string{ + "CON", + "PRN", + "AUX", + "NUL", + "COM1", + "COM2", + "COM3", + "COM4", + "COM5", + "COM6", + "COM7", + "COM8", + "COM9", + "LPT1", + "LPT2", + "LPT3", + "LPT4", + "LPT5", + "LPT6", + "LPT7", + "LPT8", + "LPT9", +} + +// SplitPathVersion returns prefix and major version such that prefix+pathMajor == path +// and version is either empty or "/vN" for N >= 2. +// As a special case, gopkg.in paths are recognized directly; +// they require ".vN" instead of "/vN", and for all N, not just N >= 2. +// SplitPathVersion returns with ok = false when presented with +// a path whose last path element does not satisfy the constraints +// applied by CheckPath, such as "example.com/pkg/v1" or "example.com/pkg/v1.2". +func SplitPathVersion(path string) (prefix, pathMajor string, ok bool) { + if strings.HasPrefix(path, "gopkg.in/") { + return splitGopkgIn(path) + } + + i := len(path) + dot := false + for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9' || path[i-1] == '.') { + if path[i-1] == '.' { + dot = true + } + i-- + } + if i <= 1 || i == len(path) || path[i-1] != 'v' || path[i-2] != '/' { + return path, "", true + } + prefix, pathMajor = path[:i-2], path[i-2:] + if dot || len(pathMajor) <= 2 || pathMajor[2] == '0' || pathMajor == "/v1" { + return path, "", false + } + return prefix, pathMajor, true +} + +// splitGopkgIn is like SplitPathVersion but only for gopkg.in paths. +func splitGopkgIn(path string) (prefix, pathMajor string, ok bool) { + if !strings.HasPrefix(path, "gopkg.in/") { + return path, "", false + } + i := len(path) + if strings.HasSuffix(path, "-unstable") { + i -= len("-unstable") + } + for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9') { + i-- + } + if i <= 1 || path[i-1] != 'v' || path[i-2] != '.' { + // All gopkg.in paths must end in vN for some N. + return path, "", false + } + prefix, pathMajor = path[:i-2], path[i-2:] + if len(pathMajor) <= 2 || pathMajor[2] == '0' && pathMajor != ".v0" { + return path, "", false + } + return prefix, pathMajor, true +} + +// MatchPathMajor reports whether the semantic version v +// matches the path major version pathMajor. +// +// MatchPathMajor returns true if and only if CheckPathMajor returns nil. +func MatchPathMajor(v, pathMajor string) bool { + return CheckPathMajor(v, pathMajor) == nil +} + +// CheckPathMajor returns a non-nil error if the semantic version v +// does not match the path major version pathMajor. +func CheckPathMajor(v, pathMajor string) error { + // TODO(jayconrod): return errors or panic for invalid inputs. This function + // (and others) was covered by integration tests for cmd/go, and surrounding + // code protected against invalid inputs like non-canonical versions. + if strings.HasPrefix(pathMajor, ".v") && strings.HasSuffix(pathMajor, "-unstable") { + pathMajor = strings.TrimSuffix(pathMajor, "-unstable") + } + if strings.HasPrefix(v, "v0.0.0-") && pathMajor == ".v1" { + // Allow old bug in pseudo-versions that generated v0.0.0- pseudoversion for gopkg .v1. + // For example, gopkg.in/yaml.v2@v2.2.1's go.mod requires gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405. + return nil + } + m := semver.Major(v) + if pathMajor == "" { + if m == "v0" || m == "v1" || semver.Build(v) == "+incompatible" { + return nil + } + pathMajor = "v0 or v1" + } else if pathMajor[0] == '/' || pathMajor[0] == '.' { + if m == pathMajor[1:] { + return nil + } + pathMajor = pathMajor[1:] + } + return &InvalidVersionError{ + Version: v, + Err: fmt.Errorf("should be %s, not %s", pathMajor, semver.Major(v)), + } +} + +// PathMajorPrefix returns the major-version tag prefix implied by pathMajor. +// An empty PathMajorPrefix allows either v0 or v1. +// +// Note that MatchPathMajor may accept some versions that do not actually begin +// with this prefix: namely, it accepts a 'v0.0.0-' prefix for a '.v1' +// pathMajor, even though that pathMajor implies 'v1' tagging. +func PathMajorPrefix(pathMajor string) string { + if pathMajor == "" { + return "" + } + if pathMajor[0] != '/' && pathMajor[0] != '.' { + panic("pathMajor suffix " + pathMajor + " passed to PathMajorPrefix lacks separator") + } + if strings.HasPrefix(pathMajor, ".v") && strings.HasSuffix(pathMajor, "-unstable") { + pathMajor = strings.TrimSuffix(pathMajor, "-unstable") + } + m := pathMajor[1:] + if m != semver.Major(m) { + panic("pathMajor suffix " + pathMajor + "passed to PathMajorPrefix is not a valid major version") + } + return m +} + +// CanonicalVersion returns the canonical form of the version string v. +// It is the same as semver.Canonical(v) except that it preserves the special build suffix "+incompatible". +func CanonicalVersion(v string) string { + cv := semver.Canonical(v) + if semver.Build(v) == "+incompatible" { + cv += "+incompatible" + } + return cv +} + +// Sort sorts the list by Path, breaking ties by comparing Version fields. +// The Version fields are interpreted as semantic versions (using semver.Compare) +// optionally followed by a tie-breaking suffix introduced by a slash character, +// like in "v0.0.1/go.mod". +func Sort(list []Version) { + sort.Slice(list, func(i, j int) bool { + mi := list[i] + mj := list[j] + if mi.Path != mj.Path { + return mi.Path < mj.Path + } + // To help go.sum formatting, allow version/file. + // Compare semver prefix by semver rules, + // file by string order. + vi := mi.Version + vj := mj.Version + var fi, fj string + if k := strings.Index(vi, "/"); k >= 0 { + vi, fi = vi[:k], vi[k:] + } + if k := strings.Index(vj, "/"); k >= 0 { + vj, fj = vj[:k], vj[k:] + } + if vi != vj { + return semver.Compare(vi, vj) < 0 + } + return fi < fj + }) +} + +// EscapePath returns the escaped form of the given module path. +// It fails if the module path is invalid. +func EscapePath(path string) (escaped string, err error) { + if err := CheckPath(path); err != nil { + return "", err + } + + return escapeString(path) +} + +// EscapeVersion returns the escaped form of the given module version. +// Versions are allowed to be in non-semver form but must be valid file names +// and not contain exclamation marks. +func EscapeVersion(v string) (escaped string, err error) { + if err := checkElem(v, true); err != nil || strings.Contains(v, "!") { + return "", &InvalidVersionError{ + Version: v, + Err: fmt.Errorf("disallowed version string"), + } + } + return escapeString(v) +} + +func escapeString(s string) (escaped string, err error) { + haveUpper := false + for _, r := range s { + if r == '!' || r >= utf8.RuneSelf { + // This should be disallowed by CheckPath, but diagnose anyway. + // The correctness of the escaping loop below depends on it. + return "", fmt.Errorf("internal error: inconsistency in EscapePath") + } + if 'A' <= r && r <= 'Z' { + haveUpper = true + } + } + + if !haveUpper { + return s, nil + } + + var buf []byte + for _, r := range s { + if 'A' <= r && r <= 'Z' { + buf = append(buf, '!', byte(r+'a'-'A')) + } else { + buf = append(buf, byte(r)) + } + } + return string(buf), nil +} + +// UnescapePath returns the module path for the given escaped path. +// It fails if the escaped path is invalid or describes an invalid path. +func UnescapePath(escaped string) (path string, err error) { + path, ok := unescapeString(escaped) + if !ok { + return "", fmt.Errorf("invalid escaped module path %q", escaped) + } + if err := CheckPath(path); err != nil { + return "", fmt.Errorf("invalid escaped module path %q: %v", escaped, err) + } + return path, nil +} + +// UnescapeVersion returns the version string for the given escaped version. +// It fails if the escaped form is invalid or describes an invalid version. +// Versions are allowed to be in non-semver form but must be valid file names +// and not contain exclamation marks. +func UnescapeVersion(escaped string) (v string, err error) { + v, ok := unescapeString(escaped) + if !ok { + return "", fmt.Errorf("invalid escaped version %q", escaped) + } + if err := checkElem(v, true); err != nil { + return "", fmt.Errorf("invalid escaped version %q: %v", v, err) + } + return v, nil +} + +func unescapeString(escaped string) (string, bool) { + var buf []byte + + bang := false + for _, r := range escaped { + if r >= utf8.RuneSelf { + return "", false + } + if bang { + bang = false + if r < 'a' || 'z' < r { + return "", false + } + buf = append(buf, byte(r+'A'-'a')) + continue + } + if r == '!' { + bang = true + continue + } + if 'A' <= r && r <= 'Z' { + return "", false + } + buf = append(buf, byte(r)) + } + if bang { + return "", false + } + return string(buf), true +} diff --git a/vendor/golang.org/x/tools/internal/semver/semver.go b/vendor/golang.org/x/mod/semver/semver.go similarity index 99% rename from vendor/golang.org/x/tools/internal/semver/semver.go rename to vendor/golang.org/x/mod/semver/semver.go index 4af7118e55d..2988e3cf9c5 100644 --- a/vendor/golang.org/x/tools/internal/semver/semver.go +++ b/vendor/golang.org/x/mod/semver/semver.go @@ -107,7 +107,7 @@ func Build(v string) string { } // Compare returns an integer comparing two versions according to -// according to semantic version precedence. +// semantic version precedence. // The result will be 0 if v == w, -1 if v < w, or +1 if v > w. // // An invalid semantic version string is considered less than a valid one. @@ -263,7 +263,7 @@ func parseBuild(v string) (t, rest string, ok bool) { i := 1 start := 1 for i < len(v) { - if !isIdentChar(v[i]) { + if !isIdentChar(v[i]) && v[i] != '.' { return } if v[i] == '.' { diff --git a/vendor/golang.org/x/tools/benchmark/parse/parse.go b/vendor/golang.org/x/tools/benchmark/parse/parse.go deleted file mode 100644 index b37e6f0f975..00000000000 --- a/vendor/golang.org/x/tools/benchmark/parse/parse.go +++ /dev/null @@ -1,131 +0,0 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package parse provides support for parsing benchmark results as -// generated by 'go test -bench'. -package parse // import "golang.org/x/tools/benchmark/parse" - -import ( - "bufio" - "bytes" - "fmt" - "io" - "strconv" - "strings" -) - -// Flags used by Benchmark.Measured to indicate -// which measurements a Benchmark contains. -const ( - NsPerOp = 1 << iota - MBPerS - AllocedBytesPerOp - AllocsPerOp -) - -// Benchmark is one run of a single benchmark. -type Benchmark struct { - Name string // benchmark name - N int // number of iterations - NsPerOp float64 // nanoseconds per iteration - AllocedBytesPerOp uint64 // bytes allocated per iteration - AllocsPerOp uint64 // allocs per iteration - MBPerS float64 // MB processed per second - Measured int // which measurements were recorded - Ord int // ordinal position within a benchmark run -} - -// ParseLine extracts a Benchmark from a single line of testing.B -// output. -func ParseLine(line string) (*Benchmark, error) { - fields := strings.Fields(line) - - // Two required, positional fields: Name and iterations. - if len(fields) < 2 { - return nil, fmt.Errorf("two fields required, have %d", len(fields)) - } - if !strings.HasPrefix(fields[0], "Benchmark") { - return nil, fmt.Errorf(`first field does not start with "Benchmark"`) - } - n, err := strconv.Atoi(fields[1]) - if err != nil { - return nil, err - } - b := &Benchmark{Name: fields[0], N: n} - - // Parse any remaining pairs of fields; we've parsed one pair already. - for i := 1; i < len(fields)/2; i++ { - b.parseMeasurement(fields[i*2], fields[i*2+1]) - } - return b, nil -} - -func (b *Benchmark) parseMeasurement(quant string, unit string) { - switch unit { - case "ns/op": - if f, err := strconv.ParseFloat(quant, 64); err == nil { - b.NsPerOp = f - b.Measured |= NsPerOp - } - case "MB/s": - if f, err := strconv.ParseFloat(quant, 64); err == nil { - b.MBPerS = f - b.Measured |= MBPerS - } - case "B/op": - if i, err := strconv.ParseUint(quant, 10, 64); err == nil { - b.AllocedBytesPerOp = i - b.Measured |= AllocedBytesPerOp - } - case "allocs/op": - if i, err := strconv.ParseUint(quant, 10, 64); err == nil { - b.AllocsPerOp = i - b.Measured |= AllocsPerOp - } - } -} - -func (b *Benchmark) String() string { - buf := new(bytes.Buffer) - fmt.Fprintf(buf, "%s %d", b.Name, b.N) - if (b.Measured & NsPerOp) != 0 { - fmt.Fprintf(buf, " %.2f ns/op", b.NsPerOp) - } - if (b.Measured & MBPerS) != 0 { - fmt.Fprintf(buf, " %.2f MB/s", b.MBPerS) - } - if (b.Measured & AllocedBytesPerOp) != 0 { - fmt.Fprintf(buf, " %d B/op", b.AllocedBytesPerOp) - } - if (b.Measured & AllocsPerOp) != 0 { - fmt.Fprintf(buf, " %d allocs/op", b.AllocsPerOp) - } - return buf.String() -} - -// Set is a collection of benchmarks from one -// testing.B run, keyed by name to facilitate comparison. -type Set map[string][]*Benchmark - -// ParseSet extracts a Set from testing.B output. -// ParseSet preserves the order of benchmarks that have identical -// names. -func ParseSet(r io.Reader) (Set, error) { - bb := make(Set) - scan := bufio.NewScanner(r) - ord := 0 - for scan.Scan() { - if b, err := ParseLine(scan.Text()); err == nil { - b.Ord = ord - ord++ - bb[b.Name] = append(bb[b.Name], b) - } - } - - if err := scan.Err(); err != nil { - return nil, err - } - - return bb, nil -} diff --git a/vendor/golang.org/x/tools/blog/atom/atom.go b/vendor/golang.org/x/tools/blog/atom/atom.go deleted file mode 100644 index 542c50e6e41..00000000000 --- a/vendor/golang.org/x/tools/blog/atom/atom.go +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2009 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Adapted from encoding/xml/read_test.go. - -// Package atom defines XML data structures for an Atom feed. -package atom // import "golang.org/x/tools/blog/atom" - -import ( - "encoding/xml" - "time" -) - -type Feed struct { - XMLName xml.Name `xml:"http://www.w3.org/2005/Atom feed"` - Title string `xml:"title"` - ID string `xml:"id"` - Link []Link `xml:"link"` - Updated TimeStr `xml:"updated"` - Author *Person `xml:"author"` - Entry []*Entry `xml:"entry"` -} - -type Entry struct { - Title string `xml:"title"` - ID string `xml:"id"` - Link []Link `xml:"link"` - Published TimeStr `xml:"published"` - Updated TimeStr `xml:"updated"` - Author *Person `xml:"author"` - Summary *Text `xml:"summary"` - Content *Text `xml:"content"` -} - -type Link struct { - Rel string `xml:"rel,attr,omitempty"` - Href string `xml:"href,attr"` - Type string `xml:"type,attr,omitempty"` - HrefLang string `xml:"hreflang,attr,omitempty"` - Title string `xml:"title,attr,omitempty"` - Length uint `xml:"length,attr,omitempty"` -} - -type Person struct { - Name string `xml:"name"` - URI string `xml:"uri,omitempty"` - Email string `xml:"email,omitempty"` - InnerXML string `xml:",innerxml"` -} - -type Text struct { - Type string `xml:"type,attr"` - Body string `xml:",chardata"` -} - -type TimeStr string - -func Time(t time.Time) TimeStr { - return TimeStr(t.Format("2006-01-02T15:04:05-07:00")) -} diff --git a/vendor/golang.org/x/tools/container/intsets/popcnt_amd64.go b/vendor/golang.org/x/tools/container/intsets/popcnt_amd64.go deleted file mode 100644 index 99ea813d284..00000000000 --- a/vendor/golang.org/x/tools/container/intsets/popcnt_amd64.go +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build amd64,!appengine,!gccgo - -package intsets - -func popcnt(x word) int -func havePOPCNT() bool - -var hasPOPCNT = havePOPCNT() - -// popcount returns the population count (number of set bits) of x. -func popcount(x word) int { - if hasPOPCNT { - return popcnt(x) - } - return popcountTable(x) // faster than Hacker's Delight -} diff --git a/vendor/golang.org/x/tools/container/intsets/popcnt_amd64.s b/vendor/golang.org/x/tools/container/intsets/popcnt_amd64.s deleted file mode 100644 index 05c3d6fb573..00000000000 --- a/vendor/golang.org/x/tools/container/intsets/popcnt_amd64.s +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build amd64,!appengine,!gccgo - -#include "textflag.h" - -// func havePOPCNT() bool -TEXT ·havePOPCNT(SB),4,$0 - MOVQ $1, AX - CPUID - SHRQ $23, CX - ANDQ $1, CX - MOVB CX, ret+0(FP) - RET - -// func popcnt(word) int -TEXT ·popcnt(SB),NOSPLIT,$0-8 - XORQ AX, AX - MOVQ x+0(FP), SI - // POPCNT (SI), AX is not recognized by Go assembler, - // so we assemble it ourselves. - BYTE $0xf3 - BYTE $0x48 - BYTE $0x0f - BYTE $0xb8 - BYTE $0xc6 - MOVQ AX, ret+8(FP) - RET diff --git a/vendor/golang.org/x/tools/container/intsets/popcnt_gccgo.go b/vendor/golang.org/x/tools/container/intsets/popcnt_gccgo.go deleted file mode 100644 index 82a8875c85d..00000000000 --- a/vendor/golang.org/x/tools/container/intsets/popcnt_gccgo.go +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build gccgo - -package intsets - -func popcount(x word) int diff --git a/vendor/golang.org/x/tools/container/intsets/popcnt_gccgo_c.c b/vendor/golang.org/x/tools/container/intsets/popcnt_gccgo_c.c deleted file mode 100644 index 08abb32ec46..00000000000 --- a/vendor/golang.org/x/tools/container/intsets/popcnt_gccgo_c.c +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build gccgo - -#include -#include -#include - -#define _STRINGIFY2_(x) #x -#define _STRINGIFY_(x) _STRINGIFY2_(x) -#define GOSYM_PREFIX _STRINGIFY_(__USER_LABEL_PREFIX__) - -extern intptr_t popcount(uintptr_t x) __asm__(GOSYM_PREFIX GOPKGPATH ".popcount"); - -intptr_t popcount(uintptr_t x) { - return __builtin_popcountl((unsigned long)(x)); -} diff --git a/vendor/golang.org/x/tools/container/intsets/popcnt_generic.go b/vendor/golang.org/x/tools/container/intsets/popcnt_generic.go deleted file mode 100644 index 3985a1da1a2..00000000000 --- a/vendor/golang.org/x/tools/container/intsets/popcnt_generic.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build !amd64 appengine -// +build !gccgo - -package intsets - -import "runtime" - -// We compared three algorithms---Hacker's Delight, table lookup, -// and AMD64's SSE4.1 hardware POPCNT---on a 2.67GHz Xeon X5550. -// -// % GOARCH=amd64 go test -run=NONE -bench=Popcount -// POPCNT 5.12 ns/op -// Table 8.53 ns/op -// HackersDelight 9.96 ns/op -// -// % GOARCH=386 go test -run=NONE -bench=Popcount -// Table 10.4 ns/op -// HackersDelight 5.23 ns/op -// -// (AMD64's ABM1 hardware supports ntz and nlz too, -// but they aren't critical.) - -// popcount returns the population count (number of set bits) of x. -func popcount(x word) int { - if runtime.GOARCH == "386" { - return popcountHD(uint32(x)) - } - return popcountTable(x) -} diff --git a/vendor/golang.org/x/tools/container/intsets/sparse.go b/vendor/golang.org/x/tools/container/intsets/sparse.go deleted file mode 100644 index 5db01c1a448..00000000000 --- a/vendor/golang.org/x/tools/container/intsets/sparse.go +++ /dev/null @@ -1,1091 +0,0 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package intsets provides Sparse, a compact and fast representation -// for sparse sets of int values. -// -// The time complexity of the operations Len, Insert, Remove and Has -// is in O(n) but in practice those methods are faster and more -// space-efficient than equivalent operations on sets based on the Go -// map type. The IsEmpty, Min, Max, Clear and TakeMin operations -// require constant time. -// -package intsets // import "golang.org/x/tools/container/intsets" - -// TODO(adonovan): -// - Add InsertAll(...int), RemoveAll(...int) -// - Add 'bool changed' results for {Intersection,Difference}With too. -// -// TODO(adonovan): implement Dense, a dense bit vector with a similar API. -// The space usage would be proportional to Max(), not Len(), and the -// implementation would be based upon big.Int. -// -// TODO(adonovan): opt: make UnionWith and Difference faster. -// These are the hot-spots for go/pointer. - -import ( - "bytes" - "fmt" -) - -// A Sparse is a set of int values. -// Sparse operations (even queries) are not concurrency-safe. -// -// The zero value for Sparse is a valid empty set. -// -// Sparse sets must be copied using the Copy method, not by assigning -// a Sparse value. -// -type Sparse struct { - // An uninitialized Sparse represents an empty set. - // An empty set may also be represented by - // root.next == root.prev == &root. - // - // The root is always the block with the smallest offset. - // It can be empty, but only if it is the only block; in that case, offset is - // MaxInt (which is not a valid offset). - root block -} - -type word uintptr - -const ( - _m = ^word(0) - bitsPerWord = 8 << (_m>>8&1 + _m>>16&1 + _m>>32&1) - bitsPerBlock = 256 // optimal value for go/pointer solver performance - wordsPerBlock = bitsPerBlock / bitsPerWord -) - -// Limit values of implementation-specific int type. -const ( - MaxInt = int(^uint(0) >> 1) - MinInt = -MaxInt - 1 -) - -// -- block ------------------------------------------------------------ - -// A set is represented as a circular doubly-linked list of blocks, -// each containing an offset and a bit array of fixed size -// bitsPerBlock; the blocks are ordered by increasing offset. -// -// The set contains an element x iff the block whose offset is x - (x -// mod bitsPerBlock) has the bit (x mod bitsPerBlock) set, where mod -// is the Euclidean remainder. -// -// A block may only be empty transiently. -// -type block struct { - offset int // offset mod bitsPerBlock == 0 - bits [wordsPerBlock]word // contains at least one set bit - next, prev *block // doubly-linked list of blocks -} - -// wordMask returns the word index (in block.bits) -// and single-bit mask for the block's ith bit. -func wordMask(i uint) (w uint, mask word) { - w = i / bitsPerWord - mask = 1 << (i % bitsPerWord) - return -} - -// insert sets the block b's ith bit and -// returns true if it was not already set. -// -func (b *block) insert(i uint) bool { - w, mask := wordMask(i) - if b.bits[w]&mask == 0 { - b.bits[w] |= mask - return true - } - return false -} - -// remove clears the block's ith bit and -// returns true if the bit was previously set. -// NB: may leave the block empty. -// -func (b *block) remove(i uint) bool { - w, mask := wordMask(i) - if b.bits[w]&mask != 0 { - b.bits[w] &^= mask - return true - } - return false -} - -// has reports whether the block's ith bit is set. -func (b *block) has(i uint) bool { - w, mask := wordMask(i) - return b.bits[w]&mask != 0 -} - -// empty reports whether b.len()==0, but more efficiently. -func (b *block) empty() bool { - for _, w := range b.bits { - if w != 0 { - return false - } - } - return true -} - -// len returns the number of set bits in block b. -func (b *block) len() int { - var l int - for _, w := range b.bits { - l += popcount(w) - } - return l -} - -// max returns the maximum element of the block. -// The block must not be empty. -func (b *block) max() int { - bi := b.offset + bitsPerBlock - // Decrement bi by number of high zeros in last.bits. - for i := len(b.bits) - 1; i >= 0; i-- { - if w := b.bits[i]; w != 0 { - return bi - nlz(w) - 1 - } - bi -= bitsPerWord - } - panic("BUG: empty block") -} - -// min returns the minimum element of the block, -// and also removes it if take is set. -// The block must not be initially empty. -// NB: may leave the block empty. -func (b *block) min(take bool) int { - for i, w := range b.bits { - if w != 0 { - tz := ntz(w) - if take { - b.bits[i] = w &^ (1 << uint(tz)) - } - return b.offset + int(i*bitsPerWord) + tz - } - } - panic("BUG: empty block") -} - -// lowerBound returns the smallest element of the block that is greater than or -// equal to the element corresponding to the ith bit. If there is no such -// element, the second return value is false. -func (b *block) lowerBound(i uint) (int, bool) { - w := i / bitsPerWord - bit := i % bitsPerWord - - if val := b.bits[w] >> bit; val != 0 { - return b.offset + int(i) + ntz(val), true - } - - for w++; w < wordsPerBlock; w++ { - if val := b.bits[w]; val != 0 { - return b.offset + int(w*bitsPerWord) + ntz(val), true - } - } - - return 0, false -} - -// forEach calls f for each element of block b. -// f must not mutate b's enclosing Sparse. -func (b *block) forEach(f func(int)) { - for i, w := range b.bits { - offset := b.offset + i*bitsPerWord - for bi := 0; w != 0 && bi < bitsPerWord; bi++ { - if w&1 != 0 { - f(offset) - } - offset++ - w >>= 1 - } - } -} - -// offsetAndBitIndex returns the offset of the block that would -// contain x and the bit index of x within that block. -// -func offsetAndBitIndex(x int) (int, uint) { - mod := x % bitsPerBlock - if mod < 0 { - // Euclidean (non-negative) remainder - mod += bitsPerBlock - } - return x - mod, uint(mod) -} - -// -- Sparse -------------------------------------------------------------- - -// none is a shared, empty, sentinel block that indicates the end of a block -// list. -var none block - -// Dummy type used to generate an implicit panic. This must be defined at the -// package level; if it is defined inside a function, it prevents the inlining -// of that function. -type to_copy_a_sparse_you_must_call_its_Copy_method struct{} - -// init ensures s is properly initialized. -func (s *Sparse) init() { - root := &s.root - if root.next == nil { - root.offset = MaxInt - root.next = root - root.prev = root - } else if root.next.prev != root { - // Copying a Sparse x leads to pernicious corruption: the - // new Sparse y shares the old linked list, but iteration - // on y will never encounter &y.root so it goes into a - // loop. Fail fast before this occurs. - // We don't want to call panic here because it prevents the - // inlining of this function. - _ = (interface{}(nil)).(to_copy_a_sparse_you_must_call_its_Copy_method) - } -} - -func (s *Sparse) first() *block { - s.init() - if s.root.offset == MaxInt { - return &none - } - return &s.root -} - -// next returns the next block in the list, or end if b is the last block. -func (s *Sparse) next(b *block) *block { - if b.next == &s.root { - return &none - } - return b.next -} - -// prev returns the previous block in the list, or end if b is the first block. -func (s *Sparse) prev(b *block) *block { - if b.prev == &s.root { - return &none - } - return b.prev -} - -// IsEmpty reports whether the set s is empty. -func (s *Sparse) IsEmpty() bool { - return s.root.next == nil || s.root.offset == MaxInt -} - -// Len returns the number of elements in the set s. -func (s *Sparse) Len() int { - var l int - for b := s.first(); b != &none; b = s.next(b) { - l += b.len() - } - return l -} - -// Max returns the maximum element of the set s, or MinInt if s is empty. -func (s *Sparse) Max() int { - if s.IsEmpty() { - return MinInt - } - return s.root.prev.max() -} - -// Min returns the minimum element of the set s, or MaxInt if s is empty. -func (s *Sparse) Min() int { - if s.IsEmpty() { - return MaxInt - } - return s.root.min(false) -} - -// LowerBound returns the smallest element >= x, or MaxInt if there is no such -// element. -func (s *Sparse) LowerBound(x int) int { - offset, i := offsetAndBitIndex(x) - for b := s.first(); b != &none; b = s.next(b) { - if b.offset > offset { - return b.min(false) - } - if b.offset == offset { - if y, ok := b.lowerBound(i); ok { - return y - } - } - } - return MaxInt -} - -// block returns the block that would contain offset, -// or nil if s contains no such block. -// Precondition: offset is a multiple of bitsPerBlock. -func (s *Sparse) block(offset int) *block { - for b := s.first(); b != &none && b.offset <= offset; b = s.next(b) { - if b.offset == offset { - return b - } - } - return nil -} - -// Insert adds x to the set s, and reports whether the set grew. -func (s *Sparse) Insert(x int) bool { - offset, i := offsetAndBitIndex(x) - - b := s.first() - for ; b != &none && b.offset <= offset; b = s.next(b) { - if b.offset == offset { - return b.insert(i) - } - } - - // Insert new block before b. - new := s.insertBlockBefore(b) - new.offset = offset - return new.insert(i) -} - -// removeBlock removes a block and returns the block that followed it (or end if -// it was the last block). -func (s *Sparse) removeBlock(b *block) *block { - if b != &s.root { - b.prev.next = b.next - b.next.prev = b.prev - if b.next == &s.root { - return &none - } - return b.next - } - - first := s.root.next - if first == &s.root { - // This was the only block. - s.Clear() - return &none - } - s.root.offset = first.offset - s.root.bits = first.bits - if first.next == &s.root { - // Single block remaining. - s.root.next = &s.root - s.root.prev = &s.root - } else { - s.root.next = first.next - first.next.prev = &s.root - } - return &s.root -} - -// Remove removes x from the set s, and reports whether the set shrank. -func (s *Sparse) Remove(x int) bool { - offset, i := offsetAndBitIndex(x) - if b := s.block(offset); b != nil { - if !b.remove(i) { - return false - } - if b.empty() { - s.removeBlock(b) - } - return true - } - return false -} - -// Clear removes all elements from the set s. -func (s *Sparse) Clear() { - s.root = block{ - offset: MaxInt, - next: &s.root, - prev: &s.root, - } -} - -// If set s is non-empty, TakeMin sets *p to the minimum element of -// the set s, removes that element from the set and returns true. -// Otherwise, it returns false and *p is undefined. -// -// This method may be used for iteration over a worklist like so: -// -// var x int -// for worklist.TakeMin(&x) { use(x) } -// -func (s *Sparse) TakeMin(p *int) bool { - if s.IsEmpty() { - return false - } - *p = s.root.min(true) - if s.root.empty() { - s.removeBlock(&s.root) - } - return true -} - -// Has reports whether x is an element of the set s. -func (s *Sparse) Has(x int) bool { - offset, i := offsetAndBitIndex(x) - if b := s.block(offset); b != nil { - return b.has(i) - } - return false -} - -// forEach applies function f to each element of the set s in order. -// -// f must not mutate s. Consequently, forEach is not safe to expose -// to clients. In any case, using "range s.AppendTo()" allows more -// natural control flow with continue/break/return. -// -func (s *Sparse) forEach(f func(int)) { - for b := s.first(); b != &none; b = s.next(b) { - b.forEach(f) - } -} - -// Copy sets s to the value of x. -func (s *Sparse) Copy(x *Sparse) { - if s == x { - return - } - - xb := x.first() - sb := s.first() - for xb != &none { - if sb == &none { - sb = s.insertBlockBefore(sb) - } - sb.offset = xb.offset - sb.bits = xb.bits - xb = x.next(xb) - sb = s.next(sb) - } - s.discardTail(sb) -} - -// insertBlockBefore returns a new block, inserting it before next. -// If next is the root, the root is replaced. If next is end, the block is -// inserted at the end. -func (s *Sparse) insertBlockBefore(next *block) *block { - if s.IsEmpty() { - if next != &none { - panic("BUG: passed block with empty set") - } - return &s.root - } - - if next == &s.root { - // Special case: we need to create a new block that will become the root - // block.The old root block becomes the second block. - second := s.root - s.root = block{ - next: &second, - } - if second.next == &s.root { - s.root.prev = &second - } else { - s.root.prev = second.prev - second.next.prev = &second - second.prev = &s.root - } - return &s.root - } - if next == &none { - // Insert before root. - next = &s.root - } - b := new(block) - b.next = next - b.prev = next.prev - b.prev.next = b - next.prev = b - return b -} - -// discardTail removes block b and all its successors from s. -func (s *Sparse) discardTail(b *block) { - if b != &none { - if b == &s.root { - s.Clear() - } else { - b.prev.next = &s.root - s.root.prev = b.prev - } - } -} - -// IntersectionWith sets s to the intersection s ∩ x. -func (s *Sparse) IntersectionWith(x *Sparse) { - if s == x { - return - } - - xb := x.first() - sb := s.first() - for xb != &none && sb != &none { - switch { - case xb.offset < sb.offset: - xb = x.next(xb) - - case xb.offset > sb.offset: - sb = s.removeBlock(sb) - - default: - var sum word - for i := range sb.bits { - r := xb.bits[i] & sb.bits[i] - sb.bits[i] = r - sum |= r - } - if sum != 0 { - sb = s.next(sb) - } else { - // sb will be overwritten or removed - } - - xb = x.next(xb) - } - } - - s.discardTail(sb) -} - -// Intersection sets s to the intersection x ∩ y. -func (s *Sparse) Intersection(x, y *Sparse) { - switch { - case s == x: - s.IntersectionWith(y) - return - case s == y: - s.IntersectionWith(x) - return - case x == y: - s.Copy(x) - return - } - - xb := x.first() - yb := y.first() - sb := s.first() - for xb != &none && yb != &none { - switch { - case xb.offset < yb.offset: - xb = x.next(xb) - continue - case xb.offset > yb.offset: - yb = y.next(yb) - continue - } - - if sb == &none { - sb = s.insertBlockBefore(sb) - } - sb.offset = xb.offset - - var sum word - for i := range sb.bits { - r := xb.bits[i] & yb.bits[i] - sb.bits[i] = r - sum |= r - } - if sum != 0 { - sb = s.next(sb) - } else { - // sb will be overwritten or removed - } - - xb = x.next(xb) - yb = y.next(yb) - } - - s.discardTail(sb) -} - -// Intersects reports whether s ∩ x ≠ ∅. -func (s *Sparse) Intersects(x *Sparse) bool { - sb := s.first() - xb := x.first() - for sb != &none && xb != &none { - switch { - case xb.offset < sb.offset: - xb = x.next(xb) - case xb.offset > sb.offset: - sb = s.next(sb) - default: - for i := range sb.bits { - if sb.bits[i]&xb.bits[i] != 0 { - return true - } - } - sb = s.next(sb) - xb = x.next(xb) - } - } - return false -} - -// UnionWith sets s to the union s ∪ x, and reports whether s grew. -func (s *Sparse) UnionWith(x *Sparse) bool { - if s == x { - return false - } - - var changed bool - xb := x.first() - sb := s.first() - for xb != &none { - if sb != &none && sb.offset == xb.offset { - for i := range xb.bits { - if sb.bits[i] != xb.bits[i] { - sb.bits[i] |= xb.bits[i] - changed = true - } - } - xb = x.next(xb) - } else if sb == &none || sb.offset > xb.offset { - sb = s.insertBlockBefore(sb) - sb.offset = xb.offset - sb.bits = xb.bits - changed = true - - xb = x.next(xb) - } - sb = s.next(sb) - } - return changed -} - -// Union sets s to the union x ∪ y. -func (s *Sparse) Union(x, y *Sparse) { - switch { - case x == y: - s.Copy(x) - return - case s == x: - s.UnionWith(y) - return - case s == y: - s.UnionWith(x) - return - } - - xb := x.first() - yb := y.first() - sb := s.first() - for xb != &none || yb != &none { - if sb == &none { - sb = s.insertBlockBefore(sb) - } - switch { - case yb == &none || (xb != &none && xb.offset < yb.offset): - sb.offset = xb.offset - sb.bits = xb.bits - xb = x.next(xb) - - case xb == &none || (yb != &none && yb.offset < xb.offset): - sb.offset = yb.offset - sb.bits = yb.bits - yb = y.next(yb) - - default: - sb.offset = xb.offset - for i := range xb.bits { - sb.bits[i] = xb.bits[i] | yb.bits[i] - } - xb = x.next(xb) - yb = y.next(yb) - } - sb = s.next(sb) - } - - s.discardTail(sb) -} - -// DifferenceWith sets s to the difference s ∖ x. -func (s *Sparse) DifferenceWith(x *Sparse) { - if s == x { - s.Clear() - return - } - - xb := x.first() - sb := s.first() - for xb != &none && sb != &none { - switch { - case xb.offset > sb.offset: - sb = s.next(sb) - - case xb.offset < sb.offset: - xb = x.next(xb) - - default: - var sum word - for i := range sb.bits { - r := sb.bits[i] & ^xb.bits[i] - sb.bits[i] = r - sum |= r - } - if sum == 0 { - sb = s.removeBlock(sb) - } else { - sb = s.next(sb) - } - xb = x.next(xb) - } - } -} - -// Difference sets s to the difference x ∖ y. -func (s *Sparse) Difference(x, y *Sparse) { - switch { - case x == y: - s.Clear() - return - case s == x: - s.DifferenceWith(y) - return - case s == y: - var y2 Sparse - y2.Copy(y) - s.Difference(x, &y2) - return - } - - xb := x.first() - yb := y.first() - sb := s.first() - for xb != &none && yb != &none { - if xb.offset > yb.offset { - // y has block, x has &none - yb = y.next(yb) - continue - } - - if sb == &none { - sb = s.insertBlockBefore(sb) - } - sb.offset = xb.offset - - switch { - case xb.offset < yb.offset: - // x has block, y has &none - sb.bits = xb.bits - - sb = s.next(sb) - - default: - // x and y have corresponding blocks - var sum word - for i := range sb.bits { - r := xb.bits[i] & ^yb.bits[i] - sb.bits[i] = r - sum |= r - } - if sum != 0 { - sb = s.next(sb) - } else { - // sb will be overwritten or removed - } - - yb = y.next(yb) - } - xb = x.next(xb) - } - - for xb != &none { - if sb == &none { - sb = s.insertBlockBefore(sb) - } - sb.offset = xb.offset - sb.bits = xb.bits - sb = s.next(sb) - - xb = x.next(xb) - } - - s.discardTail(sb) -} - -// SymmetricDifferenceWith sets s to the symmetric difference s ∆ x. -func (s *Sparse) SymmetricDifferenceWith(x *Sparse) { - if s == x { - s.Clear() - return - } - - sb := s.first() - xb := x.first() - for xb != &none && sb != &none { - switch { - case sb.offset < xb.offset: - sb = s.next(sb) - case xb.offset < sb.offset: - nb := s.insertBlockBefore(sb) - nb.offset = xb.offset - nb.bits = xb.bits - xb = x.next(xb) - default: - var sum word - for i := range sb.bits { - r := sb.bits[i] ^ xb.bits[i] - sb.bits[i] = r - sum |= r - } - if sum == 0 { - sb = s.removeBlock(sb) - } else { - sb = s.next(sb) - } - xb = x.next(xb) - } - } - - for xb != &none { // append the tail of x to s - sb = s.insertBlockBefore(sb) - sb.offset = xb.offset - sb.bits = xb.bits - sb = s.next(sb) - xb = x.next(xb) - } -} - -// SymmetricDifference sets s to the symmetric difference x ∆ y. -func (s *Sparse) SymmetricDifference(x, y *Sparse) { - switch { - case x == y: - s.Clear() - return - case s == x: - s.SymmetricDifferenceWith(y) - return - case s == y: - s.SymmetricDifferenceWith(x) - return - } - - sb := s.first() - xb := x.first() - yb := y.first() - for xb != &none && yb != &none { - if sb == &none { - sb = s.insertBlockBefore(sb) - } - switch { - case yb.offset < xb.offset: - sb.offset = yb.offset - sb.bits = yb.bits - sb = s.next(sb) - yb = y.next(yb) - case xb.offset < yb.offset: - sb.offset = xb.offset - sb.bits = xb.bits - sb = s.next(sb) - xb = x.next(xb) - default: - var sum word - for i := range sb.bits { - r := xb.bits[i] ^ yb.bits[i] - sb.bits[i] = r - sum |= r - } - if sum != 0 { - sb.offset = xb.offset - sb = s.next(sb) - } - xb = x.next(xb) - yb = y.next(yb) - } - } - - for xb != &none { // append the tail of x to s - if sb == &none { - sb = s.insertBlockBefore(sb) - } - sb.offset = xb.offset - sb.bits = xb.bits - sb = s.next(sb) - xb = x.next(xb) - } - - for yb != &none { // append the tail of y to s - if sb == &none { - sb = s.insertBlockBefore(sb) - } - sb.offset = yb.offset - sb.bits = yb.bits - sb = s.next(sb) - yb = y.next(yb) - } - - s.discardTail(sb) -} - -// SubsetOf reports whether s ∖ x = ∅. -func (s *Sparse) SubsetOf(x *Sparse) bool { - if s == x { - return true - } - - sb := s.first() - xb := x.first() - for sb != &none { - switch { - case xb == &none || xb.offset > sb.offset: - return false - case xb.offset < sb.offset: - xb = x.next(xb) - default: - for i := range sb.bits { - if sb.bits[i]&^xb.bits[i] != 0 { - return false - } - } - sb = s.next(sb) - xb = x.next(xb) - } - } - return true -} - -// Equals reports whether the sets s and t have the same elements. -func (s *Sparse) Equals(t *Sparse) bool { - if s == t { - return true - } - sb := s.first() - tb := t.first() - for { - switch { - case sb == &none && tb == &none: - return true - case sb == &none || tb == &none: - return false - case sb.offset != tb.offset: - return false - case sb.bits != tb.bits: - return false - } - - sb = s.next(sb) - tb = t.next(tb) - } -} - -// String returns a human-readable description of the set s. -func (s *Sparse) String() string { - var buf bytes.Buffer - buf.WriteByte('{') - s.forEach(func(x int) { - if buf.Len() > 1 { - buf.WriteByte(' ') - } - fmt.Fprintf(&buf, "%d", x) - }) - buf.WriteByte('}') - return buf.String() -} - -// BitString returns the set as a string of 1s and 0s denoting the sum -// of the i'th powers of 2, for each i in s. A radix point, always -// preceded by a digit, appears if the sum is non-integral. -// -// Examples: -// {}.BitString() = "0" -// {4,5}.BitString() = "110000" -// {-3}.BitString() = "0.001" -// {-3,0,4,5}.BitString() = "110001.001" -// -func (s *Sparse) BitString() string { - if s.IsEmpty() { - return "0" - } - - min, max := s.Min(), s.Max() - var nbytes int - if max > 0 { - nbytes = max - } - nbytes++ // zero bit - radix := nbytes - if min < 0 { - nbytes += len(".") - min - } - - b := make([]byte, nbytes) - for i := range b { - b[i] = '0' - } - if radix < nbytes { - b[radix] = '.' - } - s.forEach(func(x int) { - if x >= 0 { - x += len(".") - } - b[radix-x] = '1' - }) - return string(b) -} - -// GoString returns a string showing the internal representation of -// the set s. -// -func (s *Sparse) GoString() string { - var buf bytes.Buffer - for b := s.first(); b != &none; b = s.next(b) { - fmt.Fprintf(&buf, "block %p {offset=%d next=%p prev=%p", - b, b.offset, b.next, b.prev) - for _, w := range b.bits { - fmt.Fprintf(&buf, " 0%016x", w) - } - fmt.Fprintf(&buf, "}\n") - } - return buf.String() -} - -// AppendTo returns the result of appending the elements of s to slice -// in order. -func (s *Sparse) AppendTo(slice []int) []int { - s.forEach(func(x int) { - slice = append(slice, x) - }) - return slice -} - -// -- Testing/debugging ------------------------------------------------ - -// check returns an error if the representation invariants of s are violated. -func (s *Sparse) check() error { - s.init() - if s.root.empty() { - // An empty set must have only the root block with offset MaxInt. - if s.root.next != &s.root { - return fmt.Errorf("multiple blocks with empty root block") - } - if s.root.offset != MaxInt { - return fmt.Errorf("empty set has offset %d, should be MaxInt", s.root.offset) - } - return nil - } - for b := s.first(); ; b = s.next(b) { - if b.offset%bitsPerBlock != 0 { - return fmt.Errorf("bad offset modulo: %d", b.offset) - } - if b.empty() { - return fmt.Errorf("empty block") - } - if b.prev.next != b { - return fmt.Errorf("bad prev.next link") - } - if b.next.prev != b { - return fmt.Errorf("bad next.prev link") - } - if b.next == &s.root { - break - } - if b.offset >= b.next.offset { - return fmt.Errorf("bad offset order: b.offset=%d, b.next.offset=%d", - b.offset, b.next.offset) - } - } - return nil -} diff --git a/vendor/golang.org/x/tools/container/intsets/util.go b/vendor/golang.org/x/tools/container/intsets/util.go deleted file mode 100644 index dd1db86b1c1..00000000000 --- a/vendor/golang.org/x/tools/container/intsets/util.go +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package intsets - -// From Hacker's Delight, fig 5.2. -func popcountHD(x uint32) int { - x -= (x >> 1) & 0x55555555 - x = (x & 0x33333333) + ((x >> 2) & 0x33333333) - x = (x + (x >> 4)) & 0x0f0f0f0f - x = x + (x >> 8) - x = x + (x >> 16) - return int(x & 0x0000003f) -} - -var a [1 << 8]byte - -func init() { - for i := range a { - var n byte - for x := i; x != 0; x >>= 1 { - if x&1 != 0 { - n++ - } - } - a[i] = n - } -} - -func popcountTable(x word) int { - return int(a[byte(x>>(0*8))] + - a[byte(x>>(1*8))] + - a[byte(x>>(2*8))] + - a[byte(x>>(3*8))] + - a[byte(x>>(4*8))] + - a[byte(x>>(5*8))] + - a[byte(x>>(6*8))] + - a[byte(x>>(7*8))]) -} - -// nlz returns the number of leading zeros of x. -// From Hacker's Delight, fig 5.11. -func nlz(x word) int { - x |= (x >> 1) - x |= (x >> 2) - x |= (x >> 4) - x |= (x >> 8) - x |= (x >> 16) - x |= (x >> 32) - return popcount(^x) -} - -// ntz returns the number of trailing zeros of x. -// From Hacker's Delight, fig 5.13. -func ntz(x word) int { - if x == 0 { - return bitsPerWord - } - n := 1 - if bitsPerWord == 64 { - if (x & 0xffffffff) == 0 { - n = n + 32 - x = x >> 32 - } - } - if (x & 0x0000ffff) == 0 { - n = n + 16 - x = x >> 16 - } - if (x & 0x000000ff) == 0 { - n = n + 8 - x = x >> 8 - } - if (x & 0x0000000f) == 0 { - n = n + 4 - x = x >> 4 - } - if (x & 0x00000003) == 0 { - n = n + 2 - x = x >> 2 - } - return n - int(x&1) -} diff --git a/vendor/golang.org/x/tools/go/ast/astutil/enclosing.go b/vendor/golang.org/x/tools/go/ast/astutil/enclosing.go deleted file mode 100644 index 6b7052b892c..00000000000 --- a/vendor/golang.org/x/tools/go/ast/astutil/enclosing.go +++ /dev/null @@ -1,627 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package astutil - -// This file defines utilities for working with source positions. - -import ( - "fmt" - "go/ast" - "go/token" - "sort" -) - -// PathEnclosingInterval returns the node that encloses the source -// interval [start, end), and all its ancestors up to the AST root. -// -// The definition of "enclosing" used by this function considers -// additional whitespace abutting a node to be enclosed by it. -// In this example: -// -// z := x + y // add them -// <-A-> -// <----B-----> -// -// the ast.BinaryExpr(+) node is considered to enclose interval B -// even though its [Pos()..End()) is actually only interval A. -// This behaviour makes user interfaces more tolerant of imperfect -// input. -// -// This function treats tokens as nodes, though they are not included -// in the result. e.g. PathEnclosingInterval("+") returns the -// enclosing ast.BinaryExpr("x + y"). -// -// If start==end, the 1-char interval following start is used instead. -// -// The 'exact' result is true if the interval contains only path[0] -// and perhaps some adjacent whitespace. It is false if the interval -// overlaps multiple children of path[0], or if it contains only -// interior whitespace of path[0]. -// In this example: -// -// z := x + y // add them -// <--C--> <---E--> -// ^ -// D -// -// intervals C, D and E are inexact. C is contained by the -// z-assignment statement, because it spans three of its children (:=, -// x, +). So too is the 1-char interval D, because it contains only -// interior whitespace of the assignment. E is considered interior -// whitespace of the BlockStmt containing the assignment. -// -// Precondition: [start, end) both lie within the same file as root. -// TODO(adonovan): return (nil, false) in this case and remove precond. -// Requires FileSet; see loader.tokenFileContainsPos. -// -// Postcondition: path is never nil; it always contains at least 'root'. -// -func PathEnclosingInterval(root *ast.File, start, end token.Pos) (path []ast.Node, exact bool) { - // fmt.Printf("EnclosingInterval %d %d\n", start, end) // debugging - - // Precondition: node.[Pos..End) and adjoining whitespace contain [start, end). - var visit func(node ast.Node) bool - visit = func(node ast.Node) bool { - path = append(path, node) - - nodePos := node.Pos() - nodeEnd := node.End() - - // fmt.Printf("visit(%T, %d, %d)\n", node, nodePos, nodeEnd) // debugging - - // Intersect [start, end) with interval of node. - if start < nodePos { - start = nodePos - } - if end > nodeEnd { - end = nodeEnd - } - - // Find sole child that contains [start, end). - children := childrenOf(node) - l := len(children) - for i, child := range children { - // [childPos, childEnd) is unaugmented interval of child. - childPos := child.Pos() - childEnd := child.End() - - // [augPos, augEnd) is whitespace-augmented interval of child. - augPos := childPos - augEnd := childEnd - if i > 0 { - augPos = children[i-1].End() // start of preceding whitespace - } - if i < l-1 { - nextChildPos := children[i+1].Pos() - // Does [start, end) lie between child and next child? - if start >= augEnd && end <= nextChildPos { - return false // inexact match - } - augEnd = nextChildPos // end of following whitespace - } - - // fmt.Printf("\tchild %d: [%d..%d)\tcontains interval [%d..%d)?\n", - // i, augPos, augEnd, start, end) // debugging - - // Does augmented child strictly contain [start, end)? - if augPos <= start && end <= augEnd { - _, isToken := child.(tokenNode) - return isToken || visit(child) - } - - // Does [start, end) overlap multiple children? - // i.e. left-augmented child contains start - // but LR-augmented child does not contain end. - if start < childEnd && end > augEnd { - break - } - } - - // No single child contained [start, end), - // so node is the result. Is it exact? - - // (It's tempting to put this condition before the - // child loop, but it gives the wrong result in the - // case where a node (e.g. ExprStmt) and its sole - // child have equal intervals.) - if start == nodePos && end == nodeEnd { - return true // exact match - } - - return false // inexact: overlaps multiple children - } - - if start > end { - start, end = end, start - } - - if start < root.End() && end > root.Pos() { - if start == end { - end = start + 1 // empty interval => interval of size 1 - } - exact = visit(root) - - // Reverse the path: - for i, l := 0, len(path); i < l/2; i++ { - path[i], path[l-1-i] = path[l-1-i], path[i] - } - } else { - // Selection lies within whitespace preceding the - // first (or following the last) declaration in the file. - // The result nonetheless always includes the ast.File. - path = append(path, root) - } - - return -} - -// tokenNode is a dummy implementation of ast.Node for a single token. -// They are used transiently by PathEnclosingInterval but never escape -// this package. -// -type tokenNode struct { - pos token.Pos - end token.Pos -} - -func (n tokenNode) Pos() token.Pos { - return n.pos -} - -func (n tokenNode) End() token.Pos { - return n.end -} - -func tok(pos token.Pos, len int) ast.Node { - return tokenNode{pos, pos + token.Pos(len)} -} - -// childrenOf returns the direct non-nil children of ast.Node n. -// It may include fake ast.Node implementations for bare tokens. -// it is not safe to call (e.g.) ast.Walk on such nodes. -// -func childrenOf(n ast.Node) []ast.Node { - var children []ast.Node - - // First add nodes for all true subtrees. - ast.Inspect(n, func(node ast.Node) bool { - if node == n { // push n - return true // recur - } - if node != nil { // push child - children = append(children, node) - } - return false // no recursion - }) - - // Then add fake Nodes for bare tokens. - switch n := n.(type) { - case *ast.ArrayType: - children = append(children, - tok(n.Lbrack, len("[")), - tok(n.Elt.End(), len("]"))) - - case *ast.AssignStmt: - children = append(children, - tok(n.TokPos, len(n.Tok.String()))) - - case *ast.BasicLit: - children = append(children, - tok(n.ValuePos, len(n.Value))) - - case *ast.BinaryExpr: - children = append(children, tok(n.OpPos, len(n.Op.String()))) - - case *ast.BlockStmt: - children = append(children, - tok(n.Lbrace, len("{")), - tok(n.Rbrace, len("}"))) - - case *ast.BranchStmt: - children = append(children, - tok(n.TokPos, len(n.Tok.String()))) - - case *ast.CallExpr: - children = append(children, - tok(n.Lparen, len("(")), - tok(n.Rparen, len(")"))) - if n.Ellipsis != 0 { - children = append(children, tok(n.Ellipsis, len("..."))) - } - - case *ast.CaseClause: - if n.List == nil { - children = append(children, - tok(n.Case, len("default"))) - } else { - children = append(children, - tok(n.Case, len("case"))) - } - children = append(children, tok(n.Colon, len(":"))) - - case *ast.ChanType: - switch n.Dir { - case ast.RECV: - children = append(children, tok(n.Begin, len("<-chan"))) - case ast.SEND: - children = append(children, tok(n.Begin, len("chan<-"))) - case ast.RECV | ast.SEND: - children = append(children, tok(n.Begin, len("chan"))) - } - - case *ast.CommClause: - if n.Comm == nil { - children = append(children, - tok(n.Case, len("default"))) - } else { - children = append(children, - tok(n.Case, len("case"))) - } - children = append(children, tok(n.Colon, len(":"))) - - case *ast.Comment: - // nop - - case *ast.CommentGroup: - // nop - - case *ast.CompositeLit: - children = append(children, - tok(n.Lbrace, len("{")), - tok(n.Rbrace, len("{"))) - - case *ast.DeclStmt: - // nop - - case *ast.DeferStmt: - children = append(children, - tok(n.Defer, len("defer"))) - - case *ast.Ellipsis: - children = append(children, - tok(n.Ellipsis, len("..."))) - - case *ast.EmptyStmt: - // nop - - case *ast.ExprStmt: - // nop - - case *ast.Field: - // TODO(adonovan): Field.{Doc,Comment,Tag}? - - case *ast.FieldList: - children = append(children, - tok(n.Opening, len("(")), - tok(n.Closing, len(")"))) - - case *ast.File: - // TODO test: Doc - children = append(children, - tok(n.Package, len("package"))) - - case *ast.ForStmt: - children = append(children, - tok(n.For, len("for"))) - - case *ast.FuncDecl: - // TODO(adonovan): FuncDecl.Comment? - - // Uniquely, FuncDecl breaks the invariant that - // preorder traversal yields tokens in lexical order: - // in fact, FuncDecl.Recv precedes FuncDecl.Type.Func. - // - // As a workaround, we inline the case for FuncType - // here and order things correctly. - // - children = nil // discard ast.Walk(FuncDecl) info subtrees - children = append(children, tok(n.Type.Func, len("func"))) - if n.Recv != nil { - children = append(children, n.Recv) - } - children = append(children, n.Name) - if n.Type.Params != nil { - children = append(children, n.Type.Params) - } - if n.Type.Results != nil { - children = append(children, n.Type.Results) - } - if n.Body != nil { - children = append(children, n.Body) - } - - case *ast.FuncLit: - // nop - - case *ast.FuncType: - if n.Func != 0 { - children = append(children, - tok(n.Func, len("func"))) - } - - case *ast.GenDecl: - children = append(children, - tok(n.TokPos, len(n.Tok.String()))) - if n.Lparen != 0 { - children = append(children, - tok(n.Lparen, len("(")), - tok(n.Rparen, len(")"))) - } - - case *ast.GoStmt: - children = append(children, - tok(n.Go, len("go"))) - - case *ast.Ident: - children = append(children, - tok(n.NamePos, len(n.Name))) - - case *ast.IfStmt: - children = append(children, - tok(n.If, len("if"))) - - case *ast.ImportSpec: - // TODO(adonovan): ImportSpec.{Doc,EndPos}? - - case *ast.IncDecStmt: - children = append(children, - tok(n.TokPos, len(n.Tok.String()))) - - case *ast.IndexExpr: - children = append(children, - tok(n.Lbrack, len("{")), - tok(n.Rbrack, len("}"))) - - case *ast.InterfaceType: - children = append(children, - tok(n.Interface, len("interface"))) - - case *ast.KeyValueExpr: - children = append(children, - tok(n.Colon, len(":"))) - - case *ast.LabeledStmt: - children = append(children, - tok(n.Colon, len(":"))) - - case *ast.MapType: - children = append(children, - tok(n.Map, len("map"))) - - case *ast.ParenExpr: - children = append(children, - tok(n.Lparen, len("(")), - tok(n.Rparen, len(")"))) - - case *ast.RangeStmt: - children = append(children, - tok(n.For, len("for")), - tok(n.TokPos, len(n.Tok.String()))) - - case *ast.ReturnStmt: - children = append(children, - tok(n.Return, len("return"))) - - case *ast.SelectStmt: - children = append(children, - tok(n.Select, len("select"))) - - case *ast.SelectorExpr: - // nop - - case *ast.SendStmt: - children = append(children, - tok(n.Arrow, len("<-"))) - - case *ast.SliceExpr: - children = append(children, - tok(n.Lbrack, len("[")), - tok(n.Rbrack, len("]"))) - - case *ast.StarExpr: - children = append(children, tok(n.Star, len("*"))) - - case *ast.StructType: - children = append(children, tok(n.Struct, len("struct"))) - - case *ast.SwitchStmt: - children = append(children, tok(n.Switch, len("switch"))) - - case *ast.TypeAssertExpr: - children = append(children, - tok(n.Lparen-1, len(".")), - tok(n.Lparen, len("(")), - tok(n.Rparen, len(")"))) - - case *ast.TypeSpec: - // TODO(adonovan): TypeSpec.{Doc,Comment}? - - case *ast.TypeSwitchStmt: - children = append(children, tok(n.Switch, len("switch"))) - - case *ast.UnaryExpr: - children = append(children, tok(n.OpPos, len(n.Op.String()))) - - case *ast.ValueSpec: - // TODO(adonovan): ValueSpec.{Doc,Comment}? - - case *ast.BadDecl, *ast.BadExpr, *ast.BadStmt: - // nop - } - - // TODO(adonovan): opt: merge the logic of ast.Inspect() into - // the switch above so we can make interleaved callbacks for - // both Nodes and Tokens in the right order and avoid the need - // to sort. - sort.Sort(byPos(children)) - - return children -} - -type byPos []ast.Node - -func (sl byPos) Len() int { - return len(sl) -} -func (sl byPos) Less(i, j int) bool { - return sl[i].Pos() < sl[j].Pos() -} -func (sl byPos) Swap(i, j int) { - sl[i], sl[j] = sl[j], sl[i] -} - -// NodeDescription returns a description of the concrete type of n suitable -// for a user interface. -// -// TODO(adonovan): in some cases (e.g. Field, FieldList, Ident, -// StarExpr) we could be much more specific given the path to the AST -// root. Perhaps we should do that. -// -func NodeDescription(n ast.Node) string { - switch n := n.(type) { - case *ast.ArrayType: - return "array type" - case *ast.AssignStmt: - return "assignment" - case *ast.BadDecl: - return "bad declaration" - case *ast.BadExpr: - return "bad expression" - case *ast.BadStmt: - return "bad statement" - case *ast.BasicLit: - return "basic literal" - case *ast.BinaryExpr: - return fmt.Sprintf("binary %s operation", n.Op) - case *ast.BlockStmt: - return "block" - case *ast.BranchStmt: - switch n.Tok { - case token.BREAK: - return "break statement" - case token.CONTINUE: - return "continue statement" - case token.GOTO: - return "goto statement" - case token.FALLTHROUGH: - return "fall-through statement" - } - case *ast.CallExpr: - if len(n.Args) == 1 && !n.Ellipsis.IsValid() { - return "function call (or conversion)" - } - return "function call" - case *ast.CaseClause: - return "case clause" - case *ast.ChanType: - return "channel type" - case *ast.CommClause: - return "communication clause" - case *ast.Comment: - return "comment" - case *ast.CommentGroup: - return "comment group" - case *ast.CompositeLit: - return "composite literal" - case *ast.DeclStmt: - return NodeDescription(n.Decl) + " statement" - case *ast.DeferStmt: - return "defer statement" - case *ast.Ellipsis: - return "ellipsis" - case *ast.EmptyStmt: - return "empty statement" - case *ast.ExprStmt: - return "expression statement" - case *ast.Field: - // Can be any of these: - // struct {x, y int} -- struct field(s) - // struct {T} -- anon struct field - // interface {I} -- interface embedding - // interface {f()} -- interface method - // func (A) func(B) C -- receiver, param(s), result(s) - return "field/method/parameter" - case *ast.FieldList: - return "field/method/parameter list" - case *ast.File: - return "source file" - case *ast.ForStmt: - return "for loop" - case *ast.FuncDecl: - return "function declaration" - case *ast.FuncLit: - return "function literal" - case *ast.FuncType: - return "function type" - case *ast.GenDecl: - switch n.Tok { - case token.IMPORT: - return "import declaration" - case token.CONST: - return "constant declaration" - case token.TYPE: - return "type declaration" - case token.VAR: - return "variable declaration" - } - case *ast.GoStmt: - return "go statement" - case *ast.Ident: - return "identifier" - case *ast.IfStmt: - return "if statement" - case *ast.ImportSpec: - return "import specification" - case *ast.IncDecStmt: - if n.Tok == token.INC { - return "increment statement" - } - return "decrement statement" - case *ast.IndexExpr: - return "index expression" - case *ast.InterfaceType: - return "interface type" - case *ast.KeyValueExpr: - return "key/value association" - case *ast.LabeledStmt: - return "statement label" - case *ast.MapType: - return "map type" - case *ast.Package: - return "package" - case *ast.ParenExpr: - return "parenthesized " + NodeDescription(n.X) - case *ast.RangeStmt: - return "range loop" - case *ast.ReturnStmt: - return "return statement" - case *ast.SelectStmt: - return "select statement" - case *ast.SelectorExpr: - return "selector" - case *ast.SendStmt: - return "channel send" - case *ast.SliceExpr: - return "slice expression" - case *ast.StarExpr: - return "*-operation" // load/store expr or pointer type - case *ast.StructType: - return "struct type" - case *ast.SwitchStmt: - return "switch statement" - case *ast.TypeAssertExpr: - return "type assertion" - case *ast.TypeSpec: - return "type specification" - case *ast.TypeSwitchStmt: - return "type switch" - case *ast.UnaryExpr: - return fmt.Sprintf("unary %s operation", n.Op) - case *ast.ValueSpec: - return "value specification" - - } - panic(fmt.Sprintf("unexpected node type: %T", n)) -} diff --git a/vendor/golang.org/x/tools/go/ast/astutil/imports.go b/vendor/golang.org/x/tools/go/ast/astutil/imports.go deleted file mode 100644 index 3e4b195368b..00000000000 --- a/vendor/golang.org/x/tools/go/ast/astutil/imports.go +++ /dev/null @@ -1,481 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package astutil contains common utilities for working with the Go AST. -package astutil // import "golang.org/x/tools/go/ast/astutil" - -import ( - "fmt" - "go/ast" - "go/token" - "strconv" - "strings" -) - -// AddImport adds the import path to the file f, if absent. -func AddImport(fset *token.FileSet, f *ast.File, path string) (added bool) { - return AddNamedImport(fset, f, "", path) -} - -// AddNamedImport adds the import with the given name and path to the file f, if absent. -// If name is not empty, it is used to rename the import. -// -// For example, calling -// AddNamedImport(fset, f, "pathpkg", "path") -// adds -// import pathpkg "path" -func AddNamedImport(fset *token.FileSet, f *ast.File, name, path string) (added bool) { - if imports(f, name, path) { - return false - } - - newImport := &ast.ImportSpec{ - Path: &ast.BasicLit{ - Kind: token.STRING, - Value: strconv.Quote(path), - }, - } - if name != "" { - newImport.Name = &ast.Ident{Name: name} - } - - // Find an import decl to add to. - // The goal is to find an existing import - // whose import path has the longest shared - // prefix with path. - var ( - bestMatch = -1 // length of longest shared prefix - lastImport = -1 // index in f.Decls of the file's final import decl - impDecl *ast.GenDecl // import decl containing the best match - impIndex = -1 // spec index in impDecl containing the best match - - isThirdPartyPath = isThirdParty(path) - ) - for i, decl := range f.Decls { - gen, ok := decl.(*ast.GenDecl) - if ok && gen.Tok == token.IMPORT { - lastImport = i - // Do not add to import "C", to avoid disrupting the - // association with its doc comment, breaking cgo. - if declImports(gen, "C") { - continue - } - - // Match an empty import decl if that's all that is available. - if len(gen.Specs) == 0 && bestMatch == -1 { - impDecl = gen - } - - // Compute longest shared prefix with imports in this group and find best - // matched import spec. - // 1. Always prefer import spec with longest shared prefix. - // 2. While match length is 0, - // - for stdlib package: prefer first import spec. - // - for third party package: prefer first third party import spec. - // We cannot use last import spec as best match for third party package - // because grouped imports are usually placed last by goimports -local - // flag. - // See issue #19190. - seenAnyThirdParty := false - for j, spec := range gen.Specs { - impspec := spec.(*ast.ImportSpec) - p := importPath(impspec) - n := matchLen(p, path) - if n > bestMatch || (bestMatch == 0 && !seenAnyThirdParty && isThirdPartyPath) { - bestMatch = n - impDecl = gen - impIndex = j - } - seenAnyThirdParty = seenAnyThirdParty || isThirdParty(p) - } - } - } - - // If no import decl found, add one after the last import. - if impDecl == nil { - impDecl = &ast.GenDecl{ - Tok: token.IMPORT, - } - if lastImport >= 0 { - impDecl.TokPos = f.Decls[lastImport].End() - } else { - // There are no existing imports. - // Our new import, preceded by a blank line, goes after the package declaration - // and after the comment, if any, that starts on the same line as the - // package declaration. - impDecl.TokPos = f.Package - - file := fset.File(f.Package) - pkgLine := file.Line(f.Package) - for _, c := range f.Comments { - if file.Line(c.Pos()) > pkgLine { - break - } - // +2 for a blank line - impDecl.TokPos = c.End() + 2 - } - } - f.Decls = append(f.Decls, nil) - copy(f.Decls[lastImport+2:], f.Decls[lastImport+1:]) - f.Decls[lastImport+1] = impDecl - } - - // Insert new import at insertAt. - insertAt := 0 - if impIndex >= 0 { - // insert after the found import - insertAt = impIndex + 1 - } - impDecl.Specs = append(impDecl.Specs, nil) - copy(impDecl.Specs[insertAt+1:], impDecl.Specs[insertAt:]) - impDecl.Specs[insertAt] = newImport - pos := impDecl.Pos() - if insertAt > 0 { - // If there is a comment after an existing import, preserve the comment - // position by adding the new import after the comment. - if spec, ok := impDecl.Specs[insertAt-1].(*ast.ImportSpec); ok && spec.Comment != nil { - pos = spec.Comment.End() - } else { - // Assign same position as the previous import, - // so that the sorter sees it as being in the same block. - pos = impDecl.Specs[insertAt-1].Pos() - } - } - if newImport.Name != nil { - newImport.Name.NamePos = pos - } - newImport.Path.ValuePos = pos - newImport.EndPos = pos - - // Clean up parens. impDecl contains at least one spec. - if len(impDecl.Specs) == 1 { - // Remove unneeded parens. - impDecl.Lparen = token.NoPos - } else if !impDecl.Lparen.IsValid() { - // impDecl needs parens added. - impDecl.Lparen = impDecl.Specs[0].Pos() - } - - f.Imports = append(f.Imports, newImport) - - if len(f.Decls) <= 1 { - return true - } - - // Merge all the import declarations into the first one. - var first *ast.GenDecl - for i := 0; i < len(f.Decls); i++ { - decl := f.Decls[i] - gen, ok := decl.(*ast.GenDecl) - if !ok || gen.Tok != token.IMPORT || declImports(gen, "C") { - continue - } - if first == nil { - first = gen - continue // Don't touch the first one. - } - // We now know there is more than one package in this import - // declaration. Ensure that it ends up parenthesized. - first.Lparen = first.Pos() - // Move the imports of the other import declaration to the first one. - for _, spec := range gen.Specs { - spec.(*ast.ImportSpec).Path.ValuePos = first.Pos() - first.Specs = append(first.Specs, spec) - } - f.Decls = append(f.Decls[:i], f.Decls[i+1:]...) - i-- - } - - return true -} - -func isThirdParty(importPath string) bool { - // Third party package import path usually contains "." (".com", ".org", ...) - // This logic is taken from golang.org/x/tools/imports package. - return strings.Contains(importPath, ".") -} - -// DeleteImport deletes the import path from the file f, if present. -// If there are duplicate import declarations, all matching ones are deleted. -func DeleteImport(fset *token.FileSet, f *ast.File, path string) (deleted bool) { - return DeleteNamedImport(fset, f, "", path) -} - -// DeleteNamedImport deletes the import with the given name and path from the file f, if present. -// If there are duplicate import declarations, all matching ones are deleted. -func DeleteNamedImport(fset *token.FileSet, f *ast.File, name, path string) (deleted bool) { - var delspecs []*ast.ImportSpec - var delcomments []*ast.CommentGroup - - // Find the import nodes that import path, if any. - for i := 0; i < len(f.Decls); i++ { - decl := f.Decls[i] - gen, ok := decl.(*ast.GenDecl) - if !ok || gen.Tok != token.IMPORT { - continue - } - for j := 0; j < len(gen.Specs); j++ { - spec := gen.Specs[j] - impspec := spec.(*ast.ImportSpec) - if importName(impspec) != name || importPath(impspec) != path { - continue - } - - // We found an import spec that imports path. - // Delete it. - delspecs = append(delspecs, impspec) - deleted = true - copy(gen.Specs[j:], gen.Specs[j+1:]) - gen.Specs = gen.Specs[:len(gen.Specs)-1] - - // If this was the last import spec in this decl, - // delete the decl, too. - if len(gen.Specs) == 0 { - copy(f.Decls[i:], f.Decls[i+1:]) - f.Decls = f.Decls[:len(f.Decls)-1] - i-- - break - } else if len(gen.Specs) == 1 { - if impspec.Doc != nil { - delcomments = append(delcomments, impspec.Doc) - } - if impspec.Comment != nil { - delcomments = append(delcomments, impspec.Comment) - } - for _, cg := range f.Comments { - // Found comment on the same line as the import spec. - if cg.End() < impspec.Pos() && fset.Position(cg.End()).Line == fset.Position(impspec.Pos()).Line { - delcomments = append(delcomments, cg) - break - } - } - - spec := gen.Specs[0].(*ast.ImportSpec) - - // Move the documentation right after the import decl. - if spec.Doc != nil { - for fset.Position(gen.TokPos).Line+1 < fset.Position(spec.Doc.Pos()).Line { - fset.File(gen.TokPos).MergeLine(fset.Position(gen.TokPos).Line) - } - } - for _, cg := range f.Comments { - if cg.End() < spec.Pos() && fset.Position(cg.End()).Line == fset.Position(spec.Pos()).Line { - for fset.Position(gen.TokPos).Line+1 < fset.Position(spec.Pos()).Line { - fset.File(gen.TokPos).MergeLine(fset.Position(gen.TokPos).Line) - } - break - } - } - } - if j > 0 { - lastImpspec := gen.Specs[j-1].(*ast.ImportSpec) - lastLine := fset.Position(lastImpspec.Path.ValuePos).Line - line := fset.Position(impspec.Path.ValuePos).Line - - // We deleted an entry but now there may be - // a blank line-sized hole where the import was. - if line-lastLine > 1 { - // There was a blank line immediately preceding the deleted import, - // so there's no need to close the hole. - // Do nothing. - } else if line != fset.File(gen.Rparen).LineCount() { - // There was no blank line. Close the hole. - fset.File(gen.Rparen).MergeLine(line) - } - } - j-- - } - } - - // Delete imports from f.Imports. - for i := 0; i < len(f.Imports); i++ { - imp := f.Imports[i] - for j, del := range delspecs { - if imp == del { - copy(f.Imports[i:], f.Imports[i+1:]) - f.Imports = f.Imports[:len(f.Imports)-1] - copy(delspecs[j:], delspecs[j+1:]) - delspecs = delspecs[:len(delspecs)-1] - i-- - break - } - } - } - - // Delete comments from f.Comments. - for i := 0; i < len(f.Comments); i++ { - cg := f.Comments[i] - for j, del := range delcomments { - if cg == del { - copy(f.Comments[i:], f.Comments[i+1:]) - f.Comments = f.Comments[:len(f.Comments)-1] - copy(delcomments[j:], delcomments[j+1:]) - delcomments = delcomments[:len(delcomments)-1] - i-- - break - } - } - } - - if len(delspecs) > 0 { - panic(fmt.Sprintf("deleted specs from Decls but not Imports: %v", delspecs)) - } - - return -} - -// RewriteImport rewrites any import of path oldPath to path newPath. -func RewriteImport(fset *token.FileSet, f *ast.File, oldPath, newPath string) (rewrote bool) { - for _, imp := range f.Imports { - if importPath(imp) == oldPath { - rewrote = true - // record old End, because the default is to compute - // it using the length of imp.Path.Value. - imp.EndPos = imp.End() - imp.Path.Value = strconv.Quote(newPath) - } - } - return -} - -// UsesImport reports whether a given import is used. -func UsesImport(f *ast.File, path string) (used bool) { - spec := importSpec(f, path) - if spec == nil { - return - } - - name := spec.Name.String() - switch name { - case "": - // If the package name is not explicitly specified, - // make an educated guess. This is not guaranteed to be correct. - lastSlash := strings.LastIndex(path, "/") - if lastSlash == -1 { - name = path - } else { - name = path[lastSlash+1:] - } - case "_", ".": - // Not sure if this import is used - err on the side of caution. - return true - } - - ast.Walk(visitFn(func(n ast.Node) { - sel, ok := n.(*ast.SelectorExpr) - if ok && isTopName(sel.X, name) { - used = true - } - }), f) - - return -} - -type visitFn func(node ast.Node) - -func (fn visitFn) Visit(node ast.Node) ast.Visitor { - fn(node) - return fn -} - -// imports reports whether f has an import with the specified name and path. -func imports(f *ast.File, name, path string) bool { - for _, s := range f.Imports { - if importName(s) == name && importPath(s) == path { - return true - } - } - return false -} - -// importSpec returns the import spec if f imports path, -// or nil otherwise. -func importSpec(f *ast.File, path string) *ast.ImportSpec { - for _, s := range f.Imports { - if importPath(s) == path { - return s - } - } - return nil -} - -// importName returns the name of s, -// or "" if the import is not named. -func importName(s *ast.ImportSpec) string { - if s.Name == nil { - return "" - } - return s.Name.Name -} - -// importPath returns the unquoted import path of s, -// or "" if the path is not properly quoted. -func importPath(s *ast.ImportSpec) string { - t, err := strconv.Unquote(s.Path.Value) - if err != nil { - return "" - } - return t -} - -// declImports reports whether gen contains an import of path. -func declImports(gen *ast.GenDecl, path string) bool { - if gen.Tok != token.IMPORT { - return false - } - for _, spec := range gen.Specs { - impspec := spec.(*ast.ImportSpec) - if importPath(impspec) == path { - return true - } - } - return false -} - -// matchLen returns the length of the longest path segment prefix shared by x and y. -func matchLen(x, y string) int { - n := 0 - for i := 0; i < len(x) && i < len(y) && x[i] == y[i]; i++ { - if x[i] == '/' { - n++ - } - } - return n -} - -// isTopName returns true if n is a top-level unresolved identifier with the given name. -func isTopName(n ast.Expr, name string) bool { - id, ok := n.(*ast.Ident) - return ok && id.Name == name && id.Obj == nil -} - -// Imports returns the file imports grouped by paragraph. -func Imports(fset *token.FileSet, f *ast.File) [][]*ast.ImportSpec { - var groups [][]*ast.ImportSpec - - for _, decl := range f.Decls { - genDecl, ok := decl.(*ast.GenDecl) - if !ok || genDecl.Tok != token.IMPORT { - break - } - - group := []*ast.ImportSpec{} - - var lastLine int - for _, spec := range genDecl.Specs { - importSpec := spec.(*ast.ImportSpec) - pos := importSpec.Path.ValuePos - line := fset.Position(pos).Line - if lastLine > 0 && pos > 0 && line-lastLine > 1 { - groups = append(groups, group) - group = []*ast.ImportSpec{} - } - group = append(group, importSpec) - lastLine = line - } - groups = append(groups, group) - } - - return groups -} diff --git a/vendor/golang.org/x/tools/go/ast/astutil/rewrite.go b/vendor/golang.org/x/tools/go/ast/astutil/rewrite.go deleted file mode 100644 index cf72ea990bd..00000000000 --- a/vendor/golang.org/x/tools/go/ast/astutil/rewrite.go +++ /dev/null @@ -1,477 +0,0 @@ -// Copyright 2017 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package astutil - -import ( - "fmt" - "go/ast" - "reflect" - "sort" -) - -// An ApplyFunc is invoked by Apply for each node n, even if n is nil, -// before and/or after the node's children, using a Cursor describing -// the current node and providing operations on it. -// -// The return value of ApplyFunc controls the syntax tree traversal. -// See Apply for details. -type ApplyFunc func(*Cursor) bool - -// Apply traverses a syntax tree recursively, starting with root, -// and calling pre and post for each node as described below. -// Apply returns the syntax tree, possibly modified. -// -// If pre is not nil, it is called for each node before the node's -// children are traversed (pre-order). If pre returns false, no -// children are traversed, and post is not called for that node. -// -// If post is not nil, and a prior call of pre didn't return false, -// post is called for each node after its children are traversed -// (post-order). If post returns false, traversal is terminated and -// Apply returns immediately. -// -// Only fields that refer to AST nodes are considered children; -// i.e., token.Pos, Scopes, Objects, and fields of basic types -// (strings, etc.) are ignored. -// -// Children are traversed in the order in which they appear in the -// respective node's struct definition. A package's files are -// traversed in the filenames' alphabetical order. -// -func Apply(root ast.Node, pre, post ApplyFunc) (result ast.Node) { - parent := &struct{ ast.Node }{root} - defer func() { - if r := recover(); r != nil && r != abort { - panic(r) - } - result = parent.Node - }() - a := &application{pre: pre, post: post} - a.apply(parent, "Node", nil, root) - return -} - -var abort = new(int) // singleton, to signal termination of Apply - -// A Cursor describes a node encountered during Apply. -// Information about the node and its parent is available -// from the Node, Parent, Name, and Index methods. -// -// If p is a variable of type and value of the current parent node -// c.Parent(), and f is the field identifier with name c.Name(), -// the following invariants hold: -// -// p.f == c.Node() if c.Index() < 0 -// p.f[c.Index()] == c.Node() if c.Index() >= 0 -// -// The methods Replace, Delete, InsertBefore, and InsertAfter -// can be used to change the AST without disrupting Apply. -type Cursor struct { - parent ast.Node - name string - iter *iterator // valid if non-nil - node ast.Node -} - -// Node returns the current Node. -func (c *Cursor) Node() ast.Node { return c.node } - -// Parent returns the parent of the current Node. -func (c *Cursor) Parent() ast.Node { return c.parent } - -// Name returns the name of the parent Node field that contains the current Node. -// If the parent is a *ast.Package and the current Node is a *ast.File, Name returns -// the filename for the current Node. -func (c *Cursor) Name() string { return c.name } - -// Index reports the index >= 0 of the current Node in the slice of Nodes that -// contains it, or a value < 0 if the current Node is not part of a slice. -// The index of the current node changes if InsertBefore is called while -// processing the current node. -func (c *Cursor) Index() int { - if c.iter != nil { - return c.iter.index - } - return -1 -} - -// field returns the current node's parent field value. -func (c *Cursor) field() reflect.Value { - return reflect.Indirect(reflect.ValueOf(c.parent)).FieldByName(c.name) -} - -// Replace replaces the current Node with n. -// The replacement node is not walked by Apply. -func (c *Cursor) Replace(n ast.Node) { - if _, ok := c.node.(*ast.File); ok { - file, ok := n.(*ast.File) - if !ok { - panic("attempt to replace *ast.File with non-*ast.File") - } - c.parent.(*ast.Package).Files[c.name] = file - return - } - - v := c.field() - if i := c.Index(); i >= 0 { - v = v.Index(i) - } - v.Set(reflect.ValueOf(n)) -} - -// Delete deletes the current Node from its containing slice. -// If the current Node is not part of a slice, Delete panics. -// As a special case, if the current node is a package file, -// Delete removes it from the package's Files map. -func (c *Cursor) Delete() { - if _, ok := c.node.(*ast.File); ok { - delete(c.parent.(*ast.Package).Files, c.name) - return - } - - i := c.Index() - if i < 0 { - panic("Delete node not contained in slice") - } - v := c.field() - l := v.Len() - reflect.Copy(v.Slice(i, l), v.Slice(i+1, l)) - v.Index(l - 1).Set(reflect.Zero(v.Type().Elem())) - v.SetLen(l - 1) - c.iter.step-- -} - -// InsertAfter inserts n after the current Node in its containing slice. -// If the current Node is not part of a slice, InsertAfter panics. -// Apply does not walk n. -func (c *Cursor) InsertAfter(n ast.Node) { - i := c.Index() - if i < 0 { - panic("InsertAfter node not contained in slice") - } - v := c.field() - v.Set(reflect.Append(v, reflect.Zero(v.Type().Elem()))) - l := v.Len() - reflect.Copy(v.Slice(i+2, l), v.Slice(i+1, l)) - v.Index(i + 1).Set(reflect.ValueOf(n)) - c.iter.step++ -} - -// InsertBefore inserts n before the current Node in its containing slice. -// If the current Node is not part of a slice, InsertBefore panics. -// Apply will not walk n. -func (c *Cursor) InsertBefore(n ast.Node) { - i := c.Index() - if i < 0 { - panic("InsertBefore node not contained in slice") - } - v := c.field() - v.Set(reflect.Append(v, reflect.Zero(v.Type().Elem()))) - l := v.Len() - reflect.Copy(v.Slice(i+1, l), v.Slice(i, l)) - v.Index(i).Set(reflect.ValueOf(n)) - c.iter.index++ -} - -// application carries all the shared data so we can pass it around cheaply. -type application struct { - pre, post ApplyFunc - cursor Cursor - iter iterator -} - -func (a *application) apply(parent ast.Node, name string, iter *iterator, n ast.Node) { - // convert typed nil into untyped nil - if v := reflect.ValueOf(n); v.Kind() == reflect.Ptr && v.IsNil() { - n = nil - } - - // avoid heap-allocating a new cursor for each apply call; reuse a.cursor instead - saved := a.cursor - a.cursor.parent = parent - a.cursor.name = name - a.cursor.iter = iter - a.cursor.node = n - - if a.pre != nil && !a.pre(&a.cursor) { - a.cursor = saved - return - } - - // walk children - // (the order of the cases matches the order of the corresponding node types in go/ast) - switch n := n.(type) { - case nil: - // nothing to do - - // Comments and fields - case *ast.Comment: - // nothing to do - - case *ast.CommentGroup: - if n != nil { - a.applyList(n, "List") - } - - case *ast.Field: - a.apply(n, "Doc", nil, n.Doc) - a.applyList(n, "Names") - a.apply(n, "Type", nil, n.Type) - a.apply(n, "Tag", nil, n.Tag) - a.apply(n, "Comment", nil, n.Comment) - - case *ast.FieldList: - a.applyList(n, "List") - - // Expressions - case *ast.BadExpr, *ast.Ident, *ast.BasicLit: - // nothing to do - - case *ast.Ellipsis: - a.apply(n, "Elt", nil, n.Elt) - - case *ast.FuncLit: - a.apply(n, "Type", nil, n.Type) - a.apply(n, "Body", nil, n.Body) - - case *ast.CompositeLit: - a.apply(n, "Type", nil, n.Type) - a.applyList(n, "Elts") - - case *ast.ParenExpr: - a.apply(n, "X", nil, n.X) - - case *ast.SelectorExpr: - a.apply(n, "X", nil, n.X) - a.apply(n, "Sel", nil, n.Sel) - - case *ast.IndexExpr: - a.apply(n, "X", nil, n.X) - a.apply(n, "Index", nil, n.Index) - - case *ast.SliceExpr: - a.apply(n, "X", nil, n.X) - a.apply(n, "Low", nil, n.Low) - a.apply(n, "High", nil, n.High) - a.apply(n, "Max", nil, n.Max) - - case *ast.TypeAssertExpr: - a.apply(n, "X", nil, n.X) - a.apply(n, "Type", nil, n.Type) - - case *ast.CallExpr: - a.apply(n, "Fun", nil, n.Fun) - a.applyList(n, "Args") - - case *ast.StarExpr: - a.apply(n, "X", nil, n.X) - - case *ast.UnaryExpr: - a.apply(n, "X", nil, n.X) - - case *ast.BinaryExpr: - a.apply(n, "X", nil, n.X) - a.apply(n, "Y", nil, n.Y) - - case *ast.KeyValueExpr: - a.apply(n, "Key", nil, n.Key) - a.apply(n, "Value", nil, n.Value) - - // Types - case *ast.ArrayType: - a.apply(n, "Len", nil, n.Len) - a.apply(n, "Elt", nil, n.Elt) - - case *ast.StructType: - a.apply(n, "Fields", nil, n.Fields) - - case *ast.FuncType: - a.apply(n, "Params", nil, n.Params) - a.apply(n, "Results", nil, n.Results) - - case *ast.InterfaceType: - a.apply(n, "Methods", nil, n.Methods) - - case *ast.MapType: - a.apply(n, "Key", nil, n.Key) - a.apply(n, "Value", nil, n.Value) - - case *ast.ChanType: - a.apply(n, "Value", nil, n.Value) - - // Statements - case *ast.BadStmt: - // nothing to do - - case *ast.DeclStmt: - a.apply(n, "Decl", nil, n.Decl) - - case *ast.EmptyStmt: - // nothing to do - - case *ast.LabeledStmt: - a.apply(n, "Label", nil, n.Label) - a.apply(n, "Stmt", nil, n.Stmt) - - case *ast.ExprStmt: - a.apply(n, "X", nil, n.X) - - case *ast.SendStmt: - a.apply(n, "Chan", nil, n.Chan) - a.apply(n, "Value", nil, n.Value) - - case *ast.IncDecStmt: - a.apply(n, "X", nil, n.X) - - case *ast.AssignStmt: - a.applyList(n, "Lhs") - a.applyList(n, "Rhs") - - case *ast.GoStmt: - a.apply(n, "Call", nil, n.Call) - - case *ast.DeferStmt: - a.apply(n, "Call", nil, n.Call) - - case *ast.ReturnStmt: - a.applyList(n, "Results") - - case *ast.BranchStmt: - a.apply(n, "Label", nil, n.Label) - - case *ast.BlockStmt: - a.applyList(n, "List") - - case *ast.IfStmt: - a.apply(n, "Init", nil, n.Init) - a.apply(n, "Cond", nil, n.Cond) - a.apply(n, "Body", nil, n.Body) - a.apply(n, "Else", nil, n.Else) - - case *ast.CaseClause: - a.applyList(n, "List") - a.applyList(n, "Body") - - case *ast.SwitchStmt: - a.apply(n, "Init", nil, n.Init) - a.apply(n, "Tag", nil, n.Tag) - a.apply(n, "Body", nil, n.Body) - - case *ast.TypeSwitchStmt: - a.apply(n, "Init", nil, n.Init) - a.apply(n, "Assign", nil, n.Assign) - a.apply(n, "Body", nil, n.Body) - - case *ast.CommClause: - a.apply(n, "Comm", nil, n.Comm) - a.applyList(n, "Body") - - case *ast.SelectStmt: - a.apply(n, "Body", nil, n.Body) - - case *ast.ForStmt: - a.apply(n, "Init", nil, n.Init) - a.apply(n, "Cond", nil, n.Cond) - a.apply(n, "Post", nil, n.Post) - a.apply(n, "Body", nil, n.Body) - - case *ast.RangeStmt: - a.apply(n, "Key", nil, n.Key) - a.apply(n, "Value", nil, n.Value) - a.apply(n, "X", nil, n.X) - a.apply(n, "Body", nil, n.Body) - - // Declarations - case *ast.ImportSpec: - a.apply(n, "Doc", nil, n.Doc) - a.apply(n, "Name", nil, n.Name) - a.apply(n, "Path", nil, n.Path) - a.apply(n, "Comment", nil, n.Comment) - - case *ast.ValueSpec: - a.apply(n, "Doc", nil, n.Doc) - a.applyList(n, "Names") - a.apply(n, "Type", nil, n.Type) - a.applyList(n, "Values") - a.apply(n, "Comment", nil, n.Comment) - - case *ast.TypeSpec: - a.apply(n, "Doc", nil, n.Doc) - a.apply(n, "Name", nil, n.Name) - a.apply(n, "Type", nil, n.Type) - a.apply(n, "Comment", nil, n.Comment) - - case *ast.BadDecl: - // nothing to do - - case *ast.GenDecl: - a.apply(n, "Doc", nil, n.Doc) - a.applyList(n, "Specs") - - case *ast.FuncDecl: - a.apply(n, "Doc", nil, n.Doc) - a.apply(n, "Recv", nil, n.Recv) - a.apply(n, "Name", nil, n.Name) - a.apply(n, "Type", nil, n.Type) - a.apply(n, "Body", nil, n.Body) - - // Files and packages - case *ast.File: - a.apply(n, "Doc", nil, n.Doc) - a.apply(n, "Name", nil, n.Name) - a.applyList(n, "Decls") - // Don't walk n.Comments; they have either been walked already if - // they are Doc comments, or they can be easily walked explicitly. - - case *ast.Package: - // collect and sort names for reproducible behavior - var names []string - for name := range n.Files { - names = append(names, name) - } - sort.Strings(names) - for _, name := range names { - a.apply(n, name, nil, n.Files[name]) - } - - default: - panic(fmt.Sprintf("Apply: unexpected node type %T", n)) - } - - if a.post != nil && !a.post(&a.cursor) { - panic(abort) - } - - a.cursor = saved -} - -// An iterator controls iteration over a slice of nodes. -type iterator struct { - index, step int -} - -func (a *application) applyList(parent ast.Node, name string) { - // avoid heap-allocating a new iterator for each applyList call; reuse a.iter instead - saved := a.iter - a.iter.index = 0 - for { - // must reload parent.name each time, since cursor modifications might change it - v := reflect.Indirect(reflect.ValueOf(parent)).FieldByName(name) - if a.iter.index >= v.Len() { - break - } - - // element x may be nil in a bad AST - be cautious - var x ast.Node - if e := v.Index(a.iter.index); e.IsValid() { - x = e.Interface().(ast.Node) - } - - a.iter.step = 1 - a.apply(parent, name, &a.iter, x) - a.iter.index += a.iter.step - } - a.iter = saved -} diff --git a/vendor/golang.org/x/tools/go/ast/astutil/util.go b/vendor/golang.org/x/tools/go/ast/astutil/util.go deleted file mode 100644 index 7630629824a..00000000000 --- a/vendor/golang.org/x/tools/go/ast/astutil/util.go +++ /dev/null @@ -1,14 +0,0 @@ -package astutil - -import "go/ast" - -// Unparen returns e with any enclosing parentheses stripped. -func Unparen(e ast.Expr) ast.Expr { - for { - p, ok := e.(*ast.ParenExpr) - if !ok { - return e - } - e = p.X - } -} diff --git a/vendor/golang.org/x/tools/go/buildutil/allpackages.go b/vendor/golang.org/x/tools/go/buildutil/allpackages.go deleted file mode 100644 index c0cb03e7bee..00000000000 --- a/vendor/golang.org/x/tools/go/buildutil/allpackages.go +++ /dev/null @@ -1,198 +0,0 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package buildutil provides utilities related to the go/build -// package in the standard library. -// -// All I/O is done via the build.Context file system interface, which must -// be concurrency-safe. -package buildutil // import "golang.org/x/tools/go/buildutil" - -import ( - "go/build" - "os" - "path/filepath" - "sort" - "strings" - "sync" -) - -// AllPackages returns the package path of each Go package in any source -// directory of the specified build context (e.g. $GOROOT or an element -// of $GOPATH). Errors are ignored. The results are sorted. -// All package paths are canonical, and thus may contain "/vendor/". -// -// The result may include import paths for directories that contain no -// *.go files, such as "archive" (in $GOROOT/src). -// -// All I/O is done via the build.Context file system interface, -// which must be concurrency-safe. -// -func AllPackages(ctxt *build.Context) []string { - var list []string - ForEachPackage(ctxt, func(pkg string, _ error) { - list = append(list, pkg) - }) - sort.Strings(list) - return list -} - -// ForEachPackage calls the found function with the package path of -// each Go package it finds in any source directory of the specified -// build context (e.g. $GOROOT or an element of $GOPATH). -// All package paths are canonical, and thus may contain "/vendor/". -// -// If the package directory exists but could not be read, the second -// argument to the found function provides the error. -// -// All I/O is done via the build.Context file system interface, -// which must be concurrency-safe. -// -func ForEachPackage(ctxt *build.Context, found func(importPath string, err error)) { - ch := make(chan item) - - var wg sync.WaitGroup - for _, root := range ctxt.SrcDirs() { - root := root - wg.Add(1) - go func() { - allPackages(ctxt, root, ch) - wg.Done() - }() - } - go func() { - wg.Wait() - close(ch) - }() - - // All calls to found occur in the caller's goroutine. - for i := range ch { - found(i.importPath, i.err) - } -} - -type item struct { - importPath string - err error // (optional) -} - -// We use a process-wide counting semaphore to limit -// the number of parallel calls to ReadDir. -var ioLimit = make(chan bool, 20) - -func allPackages(ctxt *build.Context, root string, ch chan<- item) { - root = filepath.Clean(root) + string(os.PathSeparator) - - var wg sync.WaitGroup - - var walkDir func(dir string) - walkDir = func(dir string) { - // Avoid .foo, _foo, and testdata directory trees. - base := filepath.Base(dir) - if base == "" || base[0] == '.' || base[0] == '_' || base == "testdata" { - return - } - - pkg := filepath.ToSlash(strings.TrimPrefix(dir, root)) - - // Prune search if we encounter any of these import paths. - switch pkg { - case "builtin": - return - } - - ioLimit <- true - files, err := ReadDir(ctxt, dir) - <-ioLimit - if pkg != "" || err != nil { - ch <- item{pkg, err} - } - for _, fi := range files { - fi := fi - if fi.IsDir() { - wg.Add(1) - go func() { - walkDir(filepath.Join(dir, fi.Name())) - wg.Done() - }() - } - } - } - - walkDir(root) - wg.Wait() -} - -// ExpandPatterns returns the set of packages matched by patterns, -// which may have the following forms: -// -// golang.org/x/tools/cmd/guru # a single package -// golang.org/x/tools/... # all packages beneath dir -// ... # the entire workspace. -// -// Order is significant: a pattern preceded by '-' removes matching -// packages from the set. For example, these patterns match all encoding -// packages except encoding/xml: -// -// encoding/... -encoding/xml -// -// A trailing slash in a pattern is ignored. (Path components of Go -// package names are separated by slash, not the platform's path separator.) -// -func ExpandPatterns(ctxt *build.Context, patterns []string) map[string]bool { - // TODO(adonovan): support other features of 'go list': - // - "std"/"cmd"/"all" meta-packages - // - "..." not at the end of a pattern - // - relative patterns using "./" or "../" prefix - - pkgs := make(map[string]bool) - doPkg := func(pkg string, neg bool) { - if neg { - delete(pkgs, pkg) - } else { - pkgs[pkg] = true - } - } - - // Scan entire workspace if wildcards are present. - // TODO(adonovan): opt: scan only the necessary subtrees of the workspace. - var all []string - for _, arg := range patterns { - if strings.HasSuffix(arg, "...") { - all = AllPackages(ctxt) - break - } - } - - for _, arg := range patterns { - if arg == "" { - continue - } - - neg := arg[0] == '-' - if neg { - arg = arg[1:] - } - - if arg == "..." { - // ... matches all packages - for _, pkg := range all { - doPkg(pkg, neg) - } - } else if dir := strings.TrimSuffix(arg, "/..."); dir != arg { - // dir/... matches all packages beneath dir - for _, pkg := range all { - if strings.HasPrefix(pkg, dir) && - (len(pkg) == len(dir) || pkg[len(dir)] == '/') { - doPkg(pkg, neg) - } - } - } else { - // single package - doPkg(strings.TrimSuffix(arg, "/"), neg) - } - } - - return pkgs -} diff --git a/vendor/golang.org/x/tools/go/buildutil/fakecontext.go b/vendor/golang.org/x/tools/go/buildutil/fakecontext.go deleted file mode 100644 index 8b7f066739f..00000000000 --- a/vendor/golang.org/x/tools/go/buildutil/fakecontext.go +++ /dev/null @@ -1,109 +0,0 @@ -package buildutil - -import ( - "fmt" - "go/build" - "io" - "io/ioutil" - "os" - "path" - "path/filepath" - "sort" - "strings" - "time" -) - -// FakeContext returns a build.Context for the fake file tree specified -// by pkgs, which maps package import paths to a mapping from file base -// names to contents. -// -// The fake Context has a GOROOT of "/go" and no GOPATH, and overrides -// the necessary file access methods to read from memory instead of the -// real file system. -// -// Unlike a real file tree, the fake one has only two levels---packages -// and files---so ReadDir("/go/src/") returns all packages under -// /go/src/ including, for instance, "math" and "math/big". -// ReadDir("/go/src/math/big") would return all the files in the -// "math/big" package. -// -func FakeContext(pkgs map[string]map[string]string) *build.Context { - clean := func(filename string) string { - f := path.Clean(filepath.ToSlash(filename)) - // Removing "/go/src" while respecting segment - // boundaries has this unfortunate corner case: - if f == "/go/src" { - return "" - } - return strings.TrimPrefix(f, "/go/src/") - } - - ctxt := build.Default // copy - ctxt.GOROOT = "/go" - ctxt.GOPATH = "" - ctxt.Compiler = "gc" - ctxt.IsDir = func(dir string) bool { - dir = clean(dir) - if dir == "" { - return true // needed by (*build.Context).SrcDirs - } - return pkgs[dir] != nil - } - ctxt.ReadDir = func(dir string) ([]os.FileInfo, error) { - dir = clean(dir) - var fis []os.FileInfo - if dir == "" { - // enumerate packages - for importPath := range pkgs { - fis = append(fis, fakeDirInfo(importPath)) - } - } else { - // enumerate files of package - for basename := range pkgs[dir] { - fis = append(fis, fakeFileInfo(basename)) - } - } - sort.Sort(byName(fis)) - return fis, nil - } - ctxt.OpenFile = func(filename string) (io.ReadCloser, error) { - filename = clean(filename) - dir, base := path.Split(filename) - content, ok := pkgs[path.Clean(dir)][base] - if !ok { - return nil, fmt.Errorf("file not found: %s", filename) - } - return ioutil.NopCloser(strings.NewReader(content)), nil - } - ctxt.IsAbsPath = func(path string) bool { - path = filepath.ToSlash(path) - // Don't rely on the default (filepath.Path) since on - // Windows, it reports virtual paths as non-absolute. - return strings.HasPrefix(path, "/") - } - return &ctxt -} - -type byName []os.FileInfo - -func (s byName) Len() int { return len(s) } -func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } -func (s byName) Less(i, j int) bool { return s[i].Name() < s[j].Name() } - -type fakeFileInfo string - -func (fi fakeFileInfo) Name() string { return string(fi) } -func (fakeFileInfo) Sys() interface{} { return nil } -func (fakeFileInfo) ModTime() time.Time { return time.Time{} } -func (fakeFileInfo) IsDir() bool { return false } -func (fakeFileInfo) Size() int64 { return 0 } -func (fakeFileInfo) Mode() os.FileMode { return 0644 } - -type fakeDirInfo string - -func (fd fakeDirInfo) Name() string { return string(fd) } -func (fakeDirInfo) Sys() interface{} { return nil } -func (fakeDirInfo) ModTime() time.Time { return time.Time{} } -func (fakeDirInfo) IsDir() bool { return true } -func (fakeDirInfo) Size() int64 { return 0 } -func (fakeDirInfo) Mode() os.FileMode { return 0755 } diff --git a/vendor/golang.org/x/tools/go/buildutil/overlay.go b/vendor/golang.org/x/tools/go/buildutil/overlay.go deleted file mode 100644 index 8e239086bd4..00000000000 --- a/vendor/golang.org/x/tools/go/buildutil/overlay.go +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package buildutil - -import ( - "bufio" - "bytes" - "fmt" - "go/build" - "io" - "io/ioutil" - "path/filepath" - "strconv" - "strings" -) - -// OverlayContext overlays a build.Context with additional files from -// a map. Files in the map take precedence over other files. -// -// In addition to plain string comparison, two file names are -// considered equal if their base names match and their directory -// components point at the same directory on the file system. That is, -// symbolic links are followed for directories, but not files. -// -// A common use case for OverlayContext is to allow editors to pass in -// a set of unsaved, modified files. -// -// Currently, only the Context.OpenFile function will respect the -// overlay. This may change in the future. -func OverlayContext(orig *build.Context, overlay map[string][]byte) *build.Context { - // TODO(dominikh): Implement IsDir, HasSubdir and ReadDir - - rc := func(data []byte) (io.ReadCloser, error) { - return ioutil.NopCloser(bytes.NewBuffer(data)), nil - } - - copy := *orig // make a copy - ctxt := © - ctxt.OpenFile = func(path string) (io.ReadCloser, error) { - // Fast path: names match exactly. - if content, ok := overlay[path]; ok { - return rc(content) - } - - // Slow path: check for same file under a different - // alias, perhaps due to a symbolic link. - for filename, content := range overlay { - if sameFile(path, filename) { - return rc(content) - } - } - - return OpenFile(orig, path) - } - return ctxt -} - -// ParseOverlayArchive parses an archive containing Go files and their -// contents. The result is intended to be used with OverlayContext. -// -// -// Archive format -// -// The archive consists of a series of files. Each file consists of a -// name, a decimal file size and the file contents, separated by -// newlines. No newline follows after the file contents. -func ParseOverlayArchive(archive io.Reader) (map[string][]byte, error) { - overlay := make(map[string][]byte) - r := bufio.NewReader(archive) - for { - // Read file name. - filename, err := r.ReadString('\n') - if err != nil { - if err == io.EOF { - break // OK - } - return nil, fmt.Errorf("reading archive file name: %v", err) - } - filename = filepath.Clean(strings.TrimSpace(filename)) - - // Read file size. - sz, err := r.ReadString('\n') - if err != nil { - return nil, fmt.Errorf("reading size of archive file %s: %v", filename, err) - } - sz = strings.TrimSpace(sz) - size, err := strconv.ParseUint(sz, 10, 32) - if err != nil { - return nil, fmt.Errorf("parsing size of archive file %s: %v", filename, err) - } - - // Read file content. - content := make([]byte, size) - if _, err := io.ReadFull(r, content); err != nil { - return nil, fmt.Errorf("reading archive file %s: %v", filename, err) - } - overlay[filename] = content - } - - return overlay, nil -} diff --git a/vendor/golang.org/x/tools/go/buildutil/tags.go b/vendor/golang.org/x/tools/go/buildutil/tags.go deleted file mode 100644 index 486606f3768..00000000000 --- a/vendor/golang.org/x/tools/go/buildutil/tags.go +++ /dev/null @@ -1,75 +0,0 @@ -package buildutil - -// This logic was copied from stringsFlag from $GOROOT/src/cmd/go/build.go. - -import "fmt" - -const TagsFlagDoc = "a list of `build tags` to consider satisfied during the build. " + - "For more information about build tags, see the description of " + - "build constraints in the documentation for the go/build package" - -// TagsFlag is an implementation of the flag.Value and flag.Getter interfaces that parses -// a flag value in the same manner as go build's -tags flag and -// populates a []string slice. -// -// See $GOROOT/src/go/build/doc.go for description of build tags. -// See $GOROOT/src/cmd/go/doc.go for description of 'go build -tags' flag. -// -// Example: -// flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc) -type TagsFlag []string - -func (v *TagsFlag) Set(s string) error { - var err error - *v, err = splitQuotedFields(s) - if *v == nil { - *v = []string{} - } - return err -} - -func (v *TagsFlag) Get() interface{} { return *v } - -func splitQuotedFields(s string) ([]string, error) { - // Split fields allowing '' or "" around elements. - // Quotes further inside the string do not count. - var f []string - for len(s) > 0 { - for len(s) > 0 && isSpaceByte(s[0]) { - s = s[1:] - } - if len(s) == 0 { - break - } - // Accepted quoted string. No unescaping inside. - if s[0] == '"' || s[0] == '\'' { - quote := s[0] - s = s[1:] - i := 0 - for i < len(s) && s[i] != quote { - i++ - } - if i >= len(s) { - return nil, fmt.Errorf("unterminated %c string", quote) - } - f = append(f, s[:i]) - s = s[i+1:] - continue - } - i := 0 - for i < len(s) && !isSpaceByte(s[i]) { - i++ - } - f = append(f, s[:i]) - s = s[i:] - } - return f, nil -} - -func (v *TagsFlag) String() string { - return "" -} - -func isSpaceByte(c byte) bool { - return c == ' ' || c == '\t' || c == '\n' || c == '\r' -} diff --git a/vendor/golang.org/x/tools/go/buildutil/util.go b/vendor/golang.org/x/tools/go/buildutil/util.go deleted file mode 100644 index fc923d7a702..00000000000 --- a/vendor/golang.org/x/tools/go/buildutil/util.go +++ /dev/null @@ -1,212 +0,0 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package buildutil - -import ( - "fmt" - "go/ast" - "go/build" - "go/parser" - "go/token" - "io" - "io/ioutil" - "os" - "path" - "path/filepath" - "strings" -) - -// ParseFile behaves like parser.ParseFile, -// but uses the build context's file system interface, if any. -// -// If file is not absolute (as defined by IsAbsPath), the (dir, file) -// components are joined using JoinPath; dir must be absolute. -// -// The displayPath function, if provided, is used to transform the -// filename that will be attached to the ASTs. -// -// TODO(adonovan): call this from go/loader.parseFiles when the tree thaws. -// -func ParseFile(fset *token.FileSet, ctxt *build.Context, displayPath func(string) string, dir string, file string, mode parser.Mode) (*ast.File, error) { - if !IsAbsPath(ctxt, file) { - file = JoinPath(ctxt, dir, file) - } - rd, err := OpenFile(ctxt, file) - if err != nil { - return nil, err - } - defer rd.Close() // ignore error - if displayPath != nil { - file = displayPath(file) - } - return parser.ParseFile(fset, file, rd, mode) -} - -// ContainingPackage returns the package containing filename. -// -// If filename is not absolute, it is interpreted relative to working directory dir. -// All I/O is via the build context's file system interface, if any. -// -// The '...Files []string' fields of the resulting build.Package are not -// populated (build.FindOnly mode). -// -func ContainingPackage(ctxt *build.Context, dir, filename string) (*build.Package, error) { - if !IsAbsPath(ctxt, filename) { - filename = JoinPath(ctxt, dir, filename) - } - - // We must not assume the file tree uses - // "/" always, - // `\` always, - // or os.PathSeparator (which varies by platform), - // but to make any progress, we are forced to assume that - // paths will not use `\` unless the PathSeparator - // is also `\`, thus we can rely on filepath.ToSlash for some sanity. - - dirSlash := path.Dir(filepath.ToSlash(filename)) + "/" - - // We assume that no source root (GOPATH[i] or GOROOT) contains any other. - for _, srcdir := range ctxt.SrcDirs() { - srcdirSlash := filepath.ToSlash(srcdir) + "/" - if importPath, ok := HasSubdir(ctxt, srcdirSlash, dirSlash); ok { - return ctxt.Import(importPath, dir, build.FindOnly) - } - } - - return nil, fmt.Errorf("can't find package containing %s", filename) -} - -// -- Effective methods of file system interface ------------------------- - -// (go/build.Context defines these as methods, but does not export them.) - -// hasSubdir calls ctxt.HasSubdir (if not nil) or else uses -// the local file system to answer the question. -func HasSubdir(ctxt *build.Context, root, dir string) (rel string, ok bool) { - if f := ctxt.HasSubdir; f != nil { - return f(root, dir) - } - - // Try using paths we received. - if rel, ok = hasSubdir(root, dir); ok { - return - } - - // Try expanding symlinks and comparing - // expanded against unexpanded and - // expanded against expanded. - rootSym, _ := filepath.EvalSymlinks(root) - dirSym, _ := filepath.EvalSymlinks(dir) - - if rel, ok = hasSubdir(rootSym, dir); ok { - return - } - if rel, ok = hasSubdir(root, dirSym); ok { - return - } - return hasSubdir(rootSym, dirSym) -} - -func hasSubdir(root, dir string) (rel string, ok bool) { - const sep = string(filepath.Separator) - root = filepath.Clean(root) - if !strings.HasSuffix(root, sep) { - root += sep - } - - dir = filepath.Clean(dir) - if !strings.HasPrefix(dir, root) { - return "", false - } - - return filepath.ToSlash(dir[len(root):]), true -} - -// FileExists returns true if the specified file exists, -// using the build context's file system interface. -func FileExists(ctxt *build.Context, path string) bool { - if ctxt.OpenFile != nil { - r, err := ctxt.OpenFile(path) - if err != nil { - return false - } - r.Close() // ignore error - return true - } - _, err := os.Stat(path) - return err == nil -} - -// OpenFile behaves like os.Open, -// but uses the build context's file system interface, if any. -func OpenFile(ctxt *build.Context, path string) (io.ReadCloser, error) { - if ctxt.OpenFile != nil { - return ctxt.OpenFile(path) - } - return os.Open(path) -} - -// IsAbsPath behaves like filepath.IsAbs, -// but uses the build context's file system interface, if any. -func IsAbsPath(ctxt *build.Context, path string) bool { - if ctxt.IsAbsPath != nil { - return ctxt.IsAbsPath(path) - } - return filepath.IsAbs(path) -} - -// JoinPath behaves like filepath.Join, -// but uses the build context's file system interface, if any. -func JoinPath(ctxt *build.Context, path ...string) string { - if ctxt.JoinPath != nil { - return ctxt.JoinPath(path...) - } - return filepath.Join(path...) -} - -// IsDir behaves like os.Stat plus IsDir, -// but uses the build context's file system interface, if any. -func IsDir(ctxt *build.Context, path string) bool { - if ctxt.IsDir != nil { - return ctxt.IsDir(path) - } - fi, err := os.Stat(path) - return err == nil && fi.IsDir() -} - -// ReadDir behaves like ioutil.ReadDir, -// but uses the build context's file system interface, if any. -func ReadDir(ctxt *build.Context, path string) ([]os.FileInfo, error) { - if ctxt.ReadDir != nil { - return ctxt.ReadDir(path) - } - return ioutil.ReadDir(path) -} - -// SplitPathList behaves like filepath.SplitList, -// but uses the build context's file system interface, if any. -func SplitPathList(ctxt *build.Context, s string) []string { - if ctxt.SplitPathList != nil { - return ctxt.SplitPathList(s) - } - return filepath.SplitList(s) -} - -// sameFile returns true if x and y have the same basename and denote -// the same file. -// -func sameFile(x, y string) bool { - if path.Clean(x) == path.Clean(y) { - return true - } - if filepath.Base(x) == filepath.Base(y) { // (optimisation) - if xi, err := os.Stat(x); err == nil { - if yi, err := os.Stat(y); err == nil { - return os.SameFile(xi, yi) - } - } - } - return false -} diff --git a/vendor/golang.org/x/tools/go/callgraph/callgraph.go b/vendor/golang.org/x/tools/go/callgraph/callgraph.go deleted file mode 100644 index 707a31931a7..00000000000 --- a/vendor/golang.org/x/tools/go/callgraph/callgraph.go +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -/* - -Package callgraph defines the call graph and various algorithms -and utilities to operate on it. - -A call graph is a labelled directed graph whose nodes represent -functions and whose edge labels represent syntactic function call -sites. The presence of a labelled edge (caller, site, callee) -indicates that caller may call callee at the specified call site. - -A call graph is a multigraph: it may contain multiple edges (caller, -*, callee) connecting the same pair of nodes, so long as the edges -differ by label; this occurs when one function calls another function -from multiple call sites. Also, it may contain multiple edges -(caller, site, *) that differ only by callee; this indicates a -polymorphic call. - -A SOUND call graph is one that overapproximates the dynamic calling -behaviors of the program in all possible executions. One call graph -is more PRECISE than another if it is a smaller overapproximation of -the dynamic behavior. - -All call graphs have a synthetic root node which is responsible for -calling main() and init(). - -Calls to built-in functions (e.g. panic, println) are not represented -in the call graph; they are treated like built-in operators of the -language. - -*/ -package callgraph // import "golang.org/x/tools/go/callgraph" - -// TODO(adonovan): add a function to eliminate wrappers from the -// callgraph, preserving topology. -// More generally, we could eliminate "uninteresting" nodes such as -// nodes from packages we don't care about. - -import ( - "fmt" - "go/token" - - "golang.org/x/tools/go/ssa" -) - -// A Graph represents a call graph. -// -// A graph may contain nodes that are not reachable from the root. -// If the call graph is sound, such nodes indicate unreachable -// functions. -// -type Graph struct { - Root *Node // the distinguished root node - Nodes map[*ssa.Function]*Node // all nodes by function -} - -// New returns a new Graph with the specified root node. -func New(root *ssa.Function) *Graph { - g := &Graph{Nodes: make(map[*ssa.Function]*Node)} - g.Root = g.CreateNode(root) - return g -} - -// CreateNode returns the Node for fn, creating it if not present. -func (g *Graph) CreateNode(fn *ssa.Function) *Node { - n, ok := g.Nodes[fn] - if !ok { - n = &Node{Func: fn, ID: len(g.Nodes)} - g.Nodes[fn] = n - } - return n -} - -// A Node represents a node in a call graph. -type Node struct { - Func *ssa.Function // the function this node represents - ID int // 0-based sequence number - In []*Edge // unordered set of incoming call edges (n.In[*].Callee == n) - Out []*Edge // unordered set of outgoing call edges (n.Out[*].Caller == n) -} - -func (n *Node) String() string { - return fmt.Sprintf("n%d:%s", n.ID, n.Func) -} - -// A Edge represents an edge in the call graph. -// -// Site is nil for edges originating in synthetic or intrinsic -// functions, e.g. reflect.Call or the root of the call graph. -type Edge struct { - Caller *Node - Site ssa.CallInstruction - Callee *Node -} - -func (e Edge) String() string { - return fmt.Sprintf("%s --> %s", e.Caller, e.Callee) -} - -func (e Edge) Description() string { - var prefix string - switch e.Site.(type) { - case nil: - return "synthetic call" - case *ssa.Go: - prefix = "concurrent " - case *ssa.Defer: - prefix = "deferred " - } - return prefix + e.Site.Common().Description() -} - -func (e Edge) Pos() token.Pos { - if e.Site == nil { - return token.NoPos - } - return e.Site.Pos() -} - -// AddEdge adds the edge (caller, site, callee) to the call graph. -// Elimination of duplicate edges is the caller's responsibility. -func AddEdge(caller *Node, site ssa.CallInstruction, callee *Node) { - e := &Edge{caller, site, callee} - callee.In = append(callee.In, e) - caller.Out = append(caller.Out, e) -} diff --git a/vendor/golang.org/x/tools/go/callgraph/cha/cha.go b/vendor/golang.org/x/tools/go/callgraph/cha/cha.go deleted file mode 100644 index 215ff173d95..00000000000 --- a/vendor/golang.org/x/tools/go/callgraph/cha/cha.go +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package cha computes the call graph of a Go program using the Class -// Hierarchy Analysis (CHA) algorithm. -// -// CHA was first described in "Optimization of Object-Oriented Programs -// Using Static Class Hierarchy Analysis", Jeffrey Dean, David Grove, -// and Craig Chambers, ECOOP'95. -// -// CHA is related to RTA (see go/callgraph/rta); the difference is that -// CHA conservatively computes the entire "implements" relation between -// interfaces and concrete types ahead of time, whereas RTA uses dynamic -// programming to construct it on the fly as it encounters new functions -// reachable from main. CHA may thus include spurious call edges for -// types that haven't been instantiated yet, or types that are never -// instantiated. -// -// Since CHA conservatively assumes that all functions are address-taken -// and all concrete types are put into interfaces, it is sound to run on -// partial programs, such as libraries without a main or test function. -// -package cha // import "golang.org/x/tools/go/callgraph/cha" - -import ( - "go/types" - - "golang.org/x/tools/go/callgraph" - "golang.org/x/tools/go/ssa" - "golang.org/x/tools/go/ssa/ssautil" - "golang.org/x/tools/go/types/typeutil" -) - -// CallGraph computes the call graph of the specified program using the -// Class Hierarchy Analysis algorithm. -// -func CallGraph(prog *ssa.Program) *callgraph.Graph { - cg := callgraph.New(nil) // TODO(adonovan) eliminate concept of rooted callgraph - - allFuncs := ssautil.AllFunctions(prog) - - // funcsBySig contains all functions, keyed by signature. It is - // the effective set of address-taken functions used to resolve - // a dynamic call of a particular signature. - var funcsBySig typeutil.Map // value is []*ssa.Function - - // methodsByName contains all methods, - // grouped by name for efficient lookup. - // (methodsById would be better but not every SSA method has a go/types ID.) - methodsByName := make(map[string][]*ssa.Function) - - // An imethod represents an interface method I.m. - // (There's no go/types object for it; - // a *types.Func may be shared by many interfaces due to interface embedding.) - type imethod struct { - I *types.Interface - id string - } - // methodsMemo records, for every abstract method call I.m on - // interface type I, the set of concrete methods C.m of all - // types C that satisfy interface I. - // - // Abstract methods may be shared by several interfaces, - // hence we must pass I explicitly, not guess from m. - // - // methodsMemo is just a cache, so it needn't be a typeutil.Map. - methodsMemo := make(map[imethod][]*ssa.Function) - lookupMethods := func(I *types.Interface, m *types.Func) []*ssa.Function { - id := m.Id() - methods, ok := methodsMemo[imethod{I, id}] - if !ok { - for _, f := range methodsByName[m.Name()] { - C := f.Signature.Recv().Type() // named or *named - if types.Implements(C, I) { - methods = append(methods, f) - } - } - methodsMemo[imethod{I, id}] = methods - } - return methods - } - - for f := range allFuncs { - if f.Signature.Recv() == nil { - // Package initializers can never be address-taken. - if f.Name() == "init" && f.Synthetic == "package initializer" { - continue - } - funcs, _ := funcsBySig.At(f.Signature).([]*ssa.Function) - funcs = append(funcs, f) - funcsBySig.Set(f.Signature, funcs) - } else { - methodsByName[f.Name()] = append(methodsByName[f.Name()], f) - } - } - - addEdge := func(fnode *callgraph.Node, site ssa.CallInstruction, g *ssa.Function) { - gnode := cg.CreateNode(g) - callgraph.AddEdge(fnode, site, gnode) - } - - addEdges := func(fnode *callgraph.Node, site ssa.CallInstruction, callees []*ssa.Function) { - // Because every call to a highly polymorphic and - // frequently used abstract method such as - // (io.Writer).Write is assumed to call every concrete - // Write method in the program, the call graph can - // contain a lot of duplication. - // - // TODO(adonovan): opt: consider factoring the callgraph - // API so that the Callers component of each edge is a - // slice of nodes, not a singleton. - for _, g := range callees { - addEdge(fnode, site, g) - } - } - - for f := range allFuncs { - fnode := cg.CreateNode(f) - for _, b := range f.Blocks { - for _, instr := range b.Instrs { - if site, ok := instr.(ssa.CallInstruction); ok { - call := site.Common() - if call.IsInvoke() { - tiface := call.Value.Type().Underlying().(*types.Interface) - addEdges(fnode, site, lookupMethods(tiface, call.Method)) - } else if g := call.StaticCallee(); g != nil { - addEdge(fnode, site, g) - } else if _, ok := call.Value.(*ssa.Builtin); !ok { - callees, _ := funcsBySig.At(call.Signature()).([]*ssa.Function) - addEdges(fnode, site, callees) - } - } - } - } - } - - return cg -} diff --git a/vendor/golang.org/x/tools/go/callgraph/rta/rta.go b/vendor/golang.org/x/tools/go/callgraph/rta/rta.go deleted file mode 100644 index e6b44606ae8..00000000000 --- a/vendor/golang.org/x/tools/go/callgraph/rta/rta.go +++ /dev/null @@ -1,459 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// This package provides Rapid Type Analysis (RTA) for Go, a fast -// algorithm for call graph construction and discovery of reachable code -// (and hence dead code) and runtime types. The algorithm was first -// described in: -// -// David F. Bacon and Peter F. Sweeney. 1996. -// Fast static analysis of C++ virtual function calls. (OOPSLA '96) -// http://doi.acm.org/10.1145/236337.236371 -// -// The algorithm uses dynamic programming to tabulate the cross-product -// of the set of known "address taken" functions with the set of known -// dynamic calls of the same type. As each new address-taken function -// is discovered, call graph edges are added from each known callsite, -// and as each new call site is discovered, call graph edges are added -// from it to each known address-taken function. -// -// A similar approach is used for dynamic calls via interfaces: it -// tabulates the cross-product of the set of known "runtime types", -// i.e. types that may appear in an interface value, or be derived from -// one via reflection, with the set of known "invoke"-mode dynamic -// calls. As each new "runtime type" is discovered, call edges are -// added from the known call sites, and as each new call site is -// discovered, call graph edges are added to each compatible -// method. -// -// In addition, we must consider all exported methods of any runtime type -// as reachable, since they may be called via reflection. -// -// Each time a newly added call edge causes a new function to become -// reachable, the code of that function is analyzed for more call sites, -// address-taken functions, and runtime types. The process continues -// until a fixed point is achieved. -// -// The resulting call graph is less precise than one produced by pointer -// analysis, but the algorithm is much faster. For example, running the -// cmd/callgraph tool on its own source takes ~2.1s for RTA and ~5.4s -// for points-to analysis. -// -package rta // import "golang.org/x/tools/go/callgraph/rta" - -// TODO(adonovan): test it by connecting it to the interpreter and -// replacing all "unreachable" functions by a special intrinsic, and -// ensure that that intrinsic is never called. - -import ( - "fmt" - "go/types" - - "golang.org/x/tools/go/callgraph" - "golang.org/x/tools/go/ssa" - "golang.org/x/tools/go/types/typeutil" -) - -// A Result holds the results of Rapid Type Analysis, which includes the -// set of reachable functions/methods, runtime types, and the call graph. -// -type Result struct { - // CallGraph is the discovered callgraph. - // It does not include edges for calls made via reflection. - CallGraph *callgraph.Graph - - // Reachable contains the set of reachable functions and methods. - // This includes exported methods of runtime types, since - // they may be accessed via reflection. - // The value indicates whether the function is address-taken. - // - // (We wrap the bool in a struct to avoid inadvertent use of - // "if Reachable[f] {" to test for set membership.) - Reachable map[*ssa.Function]struct{ AddrTaken bool } - - // RuntimeTypes contains the set of types that are needed at - // runtime, for interfaces or reflection. - // - // The value indicates whether the type is inaccessible to reflection. - // Consider: - // type A struct{B} - // fmt.Println(new(A)) - // Types *A, A and B are accessible to reflection, but the unnamed - // type struct{B} is not. - RuntimeTypes typeutil.Map -} - -// Working state of the RTA algorithm. -type rta struct { - result *Result - - prog *ssa.Program - - worklist []*ssa.Function // list of functions to visit - - // addrTakenFuncsBySig contains all address-taken *Functions, grouped by signature. - // Keys are *types.Signature, values are map[*ssa.Function]bool sets. - addrTakenFuncsBySig typeutil.Map - - // dynCallSites contains all dynamic "call"-mode call sites, grouped by signature. - // Keys are *types.Signature, values are unordered []ssa.CallInstruction. - dynCallSites typeutil.Map - - // invokeSites contains all "invoke"-mode call sites, grouped by interface. - // Keys are *types.Interface (never *types.Named), - // Values are unordered []ssa.CallInstruction sets. - invokeSites typeutil.Map - - // The following two maps together define the subset of the - // m:n "implements" relation needed by the algorithm. - - // concreteTypes maps each concrete type to the set of interfaces that it implements. - // Keys are types.Type, values are unordered []*types.Interface. - // Only concrete types used as MakeInterface operands are included. - concreteTypes typeutil.Map - - // interfaceTypes maps each interface type to - // the set of concrete types that implement it. - // Keys are *types.Interface, values are unordered []types.Type. - // Only interfaces used in "invoke"-mode CallInstructions are included. - interfaceTypes typeutil.Map -} - -// addReachable marks a function as potentially callable at run-time, -// and ensures that it gets processed. -func (r *rta) addReachable(f *ssa.Function, addrTaken bool) { - reachable := r.result.Reachable - n := len(reachable) - v := reachable[f] - if addrTaken { - v.AddrTaken = true - } - reachable[f] = v - if len(reachable) > n { - // First time seeing f. Add it to the worklist. - r.worklist = append(r.worklist, f) - } -} - -// addEdge adds the specified call graph edge, and marks it reachable. -// addrTaken indicates whether to mark the callee as "address-taken". -func (r *rta) addEdge(site ssa.CallInstruction, callee *ssa.Function, addrTaken bool) { - r.addReachable(callee, addrTaken) - - if g := r.result.CallGraph; g != nil { - if site.Parent() == nil { - panic(site) - } - from := g.CreateNode(site.Parent()) - to := g.CreateNode(callee) - callgraph.AddEdge(from, site, to) - } -} - -// ---------- addrTakenFuncs × dynCallSites ---------- - -// visitAddrTakenFunc is called each time we encounter an address-taken function f. -func (r *rta) visitAddrTakenFunc(f *ssa.Function) { - // Create two-level map (Signature -> Function -> bool). - S := f.Signature - funcs, _ := r.addrTakenFuncsBySig.At(S).(map[*ssa.Function]bool) - if funcs == nil { - funcs = make(map[*ssa.Function]bool) - r.addrTakenFuncsBySig.Set(S, funcs) - } - if !funcs[f] { - // First time seeing f. - funcs[f] = true - - // If we've seen any dyncalls of this type, mark it reachable, - // and add call graph edges. - sites, _ := r.dynCallSites.At(S).([]ssa.CallInstruction) - for _, site := range sites { - r.addEdge(site, f, true) - } - } -} - -// visitDynCall is called each time we encounter a dynamic "call"-mode call. -func (r *rta) visitDynCall(site ssa.CallInstruction) { - S := site.Common().Signature() - - // Record the call site. - sites, _ := r.dynCallSites.At(S).([]ssa.CallInstruction) - r.dynCallSites.Set(S, append(sites, site)) - - // For each function of signature S that we know is address-taken, - // add an edge and mark it reachable. - funcs, _ := r.addrTakenFuncsBySig.At(S).(map[*ssa.Function]bool) - for g := range funcs { - r.addEdge(site, g, true) - } -} - -// ---------- concrete types × invoke sites ---------- - -// addInvokeEdge is called for each new pair (site, C) in the matrix. -func (r *rta) addInvokeEdge(site ssa.CallInstruction, C types.Type) { - // Ascertain the concrete method of C to be called. - imethod := site.Common().Method - cmethod := r.prog.MethodValue(r.prog.MethodSets.MethodSet(C).Lookup(imethod.Pkg(), imethod.Name())) - r.addEdge(site, cmethod, true) -} - -// visitInvoke is called each time the algorithm encounters an "invoke"-mode call. -func (r *rta) visitInvoke(site ssa.CallInstruction) { - I := site.Common().Value.Type().Underlying().(*types.Interface) - - // Record the invoke site. - sites, _ := r.invokeSites.At(I).([]ssa.CallInstruction) - r.invokeSites.Set(I, append(sites, site)) - - // Add callgraph edge for each existing - // address-taken concrete type implementing I. - for _, C := range r.implementations(I) { - r.addInvokeEdge(site, C) - } -} - -// ---------- main algorithm ---------- - -// visitFunc processes function f. -func (r *rta) visitFunc(f *ssa.Function) { - var space [32]*ssa.Value // preallocate space for common case - - for _, b := range f.Blocks { - for _, instr := range b.Instrs { - rands := instr.Operands(space[:0]) - - switch instr := instr.(type) { - case ssa.CallInstruction: - call := instr.Common() - if call.IsInvoke() { - r.visitInvoke(instr) - } else if g := call.StaticCallee(); g != nil { - r.addEdge(instr, g, false) - } else if _, ok := call.Value.(*ssa.Builtin); !ok { - r.visitDynCall(instr) - } - - // Ignore the call-position operand when - // looking for address-taken Functions. - // Hack: assume this is rands[0]. - rands = rands[1:] - - case *ssa.MakeInterface: - r.addRuntimeType(instr.X.Type(), false) - } - - // Process all address-taken functions. - for _, op := range rands { - if g, ok := (*op).(*ssa.Function); ok { - r.visitAddrTakenFunc(g) - } - } - } - } -} - -// Analyze performs Rapid Type Analysis, starting at the specified root -// functions. It returns nil if no roots were specified. -// -// If buildCallGraph is true, Result.CallGraph will contain a call -// graph; otherwise, only the other fields (reachable functions) are -// populated. -// -func Analyze(roots []*ssa.Function, buildCallGraph bool) *Result { - if len(roots) == 0 { - return nil - } - - r := &rta{ - result: &Result{Reachable: make(map[*ssa.Function]struct{ AddrTaken bool })}, - prog: roots[0].Prog, - } - - if buildCallGraph { - // TODO(adonovan): change callgraph API to eliminate the - // notion of a distinguished root node. Some callgraphs - // have many roots, or none. - r.result.CallGraph = callgraph.New(roots[0]) - } - - hasher := typeutil.MakeHasher() - r.result.RuntimeTypes.SetHasher(hasher) - r.addrTakenFuncsBySig.SetHasher(hasher) - r.dynCallSites.SetHasher(hasher) - r.invokeSites.SetHasher(hasher) - r.concreteTypes.SetHasher(hasher) - r.interfaceTypes.SetHasher(hasher) - - // Visit functions, processing their instructions, and adding - // new functions to the worklist, until a fixed point is - // reached. - var shadow []*ssa.Function // for efficiency, we double-buffer the worklist - r.worklist = append(r.worklist, roots...) - for len(r.worklist) > 0 { - shadow, r.worklist = r.worklist, shadow[:0] - for _, f := range shadow { - r.visitFunc(f) - } - } - return r.result -} - -// interfaces(C) returns all currently known interfaces implemented by C. -func (r *rta) interfaces(C types.Type) []*types.Interface { - // Ascertain set of interfaces C implements - // and update 'implements' relation. - var ifaces []*types.Interface - r.interfaceTypes.Iterate(func(I types.Type, concs interface{}) { - if I := I.(*types.Interface); types.Implements(C, I) { - concs, _ := concs.([]types.Type) - r.interfaceTypes.Set(I, append(concs, C)) - ifaces = append(ifaces, I) - } - }) - r.concreteTypes.Set(C, ifaces) - return ifaces -} - -// implementations(I) returns all currently known concrete types that implement I. -func (r *rta) implementations(I *types.Interface) []types.Type { - var concs []types.Type - if v := r.interfaceTypes.At(I); v != nil { - concs = v.([]types.Type) - } else { - // First time seeing this interface. - // Update the 'implements' relation. - r.concreteTypes.Iterate(func(C types.Type, ifaces interface{}) { - if types.Implements(C, I) { - ifaces, _ := ifaces.([]*types.Interface) - r.concreteTypes.Set(C, append(ifaces, I)) - concs = append(concs, C) - } - }) - r.interfaceTypes.Set(I, concs) - } - return concs -} - -// addRuntimeType is called for each concrete type that can be the -// dynamic type of some interface or reflect.Value. -// Adapted from needMethods in go/ssa/builder.go -// -func (r *rta) addRuntimeType(T types.Type, skip bool) { - if prev, ok := r.result.RuntimeTypes.At(T).(bool); ok { - if skip && !prev { - r.result.RuntimeTypes.Set(T, skip) - } - return - } - r.result.RuntimeTypes.Set(T, skip) - - mset := r.prog.MethodSets.MethodSet(T) - - if _, ok := T.Underlying().(*types.Interface); !ok { - // T is a new concrete type. - for i, n := 0, mset.Len(); i < n; i++ { - sel := mset.At(i) - m := sel.Obj() - - if m.Exported() { - // Exported methods are always potentially callable via reflection. - r.addReachable(r.prog.MethodValue(sel), true) - } - } - - // Add callgraph edge for each existing dynamic - // "invoke"-mode call via that interface. - for _, I := range r.interfaces(T) { - sites, _ := r.invokeSites.At(I).([]ssa.CallInstruction) - for _, site := range sites { - r.addInvokeEdge(site, T) - } - } - } - - // Precondition: T is not a method signature (*Signature with Recv()!=nil). - // Recursive case: skip => don't call makeMethods(T). - // Each package maintains its own set of types it has visited. - - var n *types.Named - switch T := T.(type) { - case *types.Named: - n = T - case *types.Pointer: - n, _ = T.Elem().(*types.Named) - } - if n != nil { - owner := n.Obj().Pkg() - if owner == nil { - return // built-in error type - } - } - - // Recursion over signatures of each exported method. - for i := 0; i < mset.Len(); i++ { - if mset.At(i).Obj().Exported() { - sig := mset.At(i).Type().(*types.Signature) - r.addRuntimeType(sig.Params(), true) // skip the Tuple itself - r.addRuntimeType(sig.Results(), true) // skip the Tuple itself - } - } - - switch t := T.(type) { - case *types.Basic: - // nop - - case *types.Interface: - // nop---handled by recursion over method set. - - case *types.Pointer: - r.addRuntimeType(t.Elem(), false) - - case *types.Slice: - r.addRuntimeType(t.Elem(), false) - - case *types.Chan: - r.addRuntimeType(t.Elem(), false) - - case *types.Map: - r.addRuntimeType(t.Key(), false) - r.addRuntimeType(t.Elem(), false) - - case *types.Signature: - if t.Recv() != nil { - panic(fmt.Sprintf("Signature %s has Recv %s", t, t.Recv())) - } - r.addRuntimeType(t.Params(), true) // skip the Tuple itself - r.addRuntimeType(t.Results(), true) // skip the Tuple itself - - case *types.Named: - // A pointer-to-named type can be derived from a named - // type via reflection. It may have methods too. - r.addRuntimeType(types.NewPointer(T), false) - - // Consider 'type T struct{S}' where S has methods. - // Reflection provides no way to get from T to struct{S}, - // only to S, so the method set of struct{S} is unwanted, - // so set 'skip' flag during recursion. - r.addRuntimeType(t.Underlying(), true) - - case *types.Array: - r.addRuntimeType(t.Elem(), false) - - case *types.Struct: - for i, n := 0, t.NumFields(); i < n; i++ { - r.addRuntimeType(t.Field(i).Type(), false) - } - - case *types.Tuple: - for i, n := 0, t.Len(); i < n; i++ { - r.addRuntimeType(t.At(i).Type(), false) - } - - default: - panic(T) - } -} diff --git a/vendor/golang.org/x/tools/go/callgraph/static/static.go b/vendor/golang.org/x/tools/go/callgraph/static/static.go deleted file mode 100644 index 709bb7b6bd8..00000000000 --- a/vendor/golang.org/x/tools/go/callgraph/static/static.go +++ /dev/null @@ -1,35 +0,0 @@ -// Package static computes the call graph of a Go program containing -// only static call edges. -package static // import "golang.org/x/tools/go/callgraph/static" - -import ( - "golang.org/x/tools/go/callgraph" - "golang.org/x/tools/go/ssa" - "golang.org/x/tools/go/ssa/ssautil" -) - -// CallGraph computes the call graph of the specified program -// considering only static calls. -// -func CallGraph(prog *ssa.Program) *callgraph.Graph { - cg := callgraph.New(nil) // TODO(adonovan) eliminate concept of rooted callgraph - - // TODO(adonovan): opt: use only a single pass over the ssa.Program. - // TODO(adonovan): opt: this is slower than RTA (perhaps because - // the lower precision means so many edges are allocated)! - for f := range ssautil.AllFunctions(prog) { - fnode := cg.CreateNode(f) - for _, b := range f.Blocks { - for _, instr := range b.Instrs { - if site, ok := instr.(ssa.CallInstruction); ok { - if g := site.Common().StaticCallee(); g != nil { - gnode := cg.CreateNode(g) - callgraph.AddEdge(fnode, site, gnode) - } - } - } - } - } - - return cg -} diff --git a/vendor/golang.org/x/tools/go/callgraph/util.go b/vendor/golang.org/x/tools/go/callgraph/util.go deleted file mode 100644 index a8f89031c05..00000000000 --- a/vendor/golang.org/x/tools/go/callgraph/util.go +++ /dev/null @@ -1,181 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package callgraph - -import "golang.org/x/tools/go/ssa" - -// This file provides various utilities over call graphs, such as -// visitation and path search. - -// CalleesOf returns a new set containing all direct callees of the -// caller node. -// -func CalleesOf(caller *Node) map[*Node]bool { - callees := make(map[*Node]bool) - for _, e := range caller.Out { - callees[e.Callee] = true - } - return callees -} - -// GraphVisitEdges visits all the edges in graph g in depth-first order. -// The edge function is called for each edge in postorder. If it -// returns non-nil, visitation stops and GraphVisitEdges returns that -// value. -// -func GraphVisitEdges(g *Graph, edge func(*Edge) error) error { - seen := make(map[*Node]bool) - var visit func(n *Node) error - visit = func(n *Node) error { - if !seen[n] { - seen[n] = true - for _, e := range n.Out { - if err := visit(e.Callee); err != nil { - return err - } - if err := edge(e); err != nil { - return err - } - } - } - return nil - } - for _, n := range g.Nodes { - if err := visit(n); err != nil { - return err - } - } - return nil -} - -// PathSearch finds an arbitrary path starting at node start and -// ending at some node for which isEnd() returns true. On success, -// PathSearch returns the path as an ordered list of edges; on -// failure, it returns nil. -// -func PathSearch(start *Node, isEnd func(*Node) bool) []*Edge { - stack := make([]*Edge, 0, 32) - seen := make(map[*Node]bool) - var search func(n *Node) []*Edge - search = func(n *Node) []*Edge { - if !seen[n] { - seen[n] = true - if isEnd(n) { - return stack - } - for _, e := range n.Out { - stack = append(stack, e) // push - if found := search(e.Callee); found != nil { - return found - } - stack = stack[:len(stack)-1] // pop - } - } - return nil - } - return search(start) -} - -// DeleteSyntheticNodes removes from call graph g all nodes for -// synthetic functions (except g.Root and package initializers), -// preserving the topology. In effect, calls to synthetic wrappers -// are "inlined". -// -func (g *Graph) DeleteSyntheticNodes() { - // Measurements on the standard library and go.tools show that - // resulting graph has ~15% fewer nodes and 4-8% fewer edges - // than the input. - // - // Inlining a wrapper of in-degree m, out-degree n adds m*n - // and removes m+n edges. Since most wrappers are monomorphic - // (n=1) this results in a slight reduction. Polymorphic - // wrappers (n>1), e.g. from embedding an interface value - // inside a struct to satisfy some interface, cause an - // increase in the graph, but they seem to be uncommon. - - // Hash all existing edges to avoid creating duplicates. - edges := make(map[Edge]bool) - for _, cgn := range g.Nodes { - for _, e := range cgn.Out { - edges[*e] = true - } - } - for fn, cgn := range g.Nodes { - if cgn == g.Root || fn.Synthetic == "" || isInit(cgn.Func) { - continue // keep - } - for _, eIn := range cgn.In { - for _, eOut := range cgn.Out { - newEdge := Edge{eIn.Caller, eIn.Site, eOut.Callee} - if edges[newEdge] { - continue // don't add duplicate - } - AddEdge(eIn.Caller, eIn.Site, eOut.Callee) - edges[newEdge] = true - } - } - g.DeleteNode(cgn) - } -} - -func isInit(fn *ssa.Function) bool { - return fn.Pkg != nil && fn.Pkg.Func("init") == fn -} - -// DeleteNode removes node n and its edges from the graph g. -// (NB: not efficient for batch deletion.) -func (g *Graph) DeleteNode(n *Node) { - n.deleteIns() - n.deleteOuts() - delete(g.Nodes, n.Func) -} - -// deleteIns deletes all incoming edges to n. -func (n *Node) deleteIns() { - for _, e := range n.In { - removeOutEdge(e) - } - n.In = nil -} - -// deleteOuts deletes all outgoing edges from n. -func (n *Node) deleteOuts() { - for _, e := range n.Out { - removeInEdge(e) - } - n.Out = nil -} - -// removeOutEdge removes edge.Caller's outgoing edge 'edge'. -func removeOutEdge(edge *Edge) { - caller := edge.Caller - n := len(caller.Out) - for i, e := range caller.Out { - if e == edge { - // Replace it with the final element and shrink the slice. - caller.Out[i] = caller.Out[n-1] - caller.Out[n-1] = nil // aid GC - caller.Out = caller.Out[:n-1] - return - } - } - panic("edge not found: " + edge.String()) -} - -// removeInEdge removes edge.Callee's incoming edge 'edge'. -func removeInEdge(edge *Edge) { - caller := edge.Callee - n := len(caller.In) - for i, e := range caller.In { - if e == edge { - // Replace it with the final element and shrink the slice. - caller.In[i] = caller.In[n-1] - caller.In[n-1] = nil // aid GC - caller.In = caller.In[:n-1] - return - } - } - panic("edge not found: " + edge.String()) -} diff --git a/vendor/golang.org/x/tools/go/internal/cgo/cgo.go b/vendor/golang.org/x/tools/go/internal/cgo/cgo.go deleted file mode 100644 index 0f652ea6fb5..00000000000 --- a/vendor/golang.org/x/tools/go/internal/cgo/cgo.go +++ /dev/null @@ -1,220 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package cgo - -// This file handles cgo preprocessing of files containing `import "C"`. -// -// DESIGN -// -// The approach taken is to run the cgo processor on the package's -// CgoFiles and parse the output, faking the filenames of the -// resulting ASTs so that the synthetic file containing the C types is -// called "C" (e.g. "~/go/src/net/C") and the preprocessed files -// have their original names (e.g. "~/go/src/net/cgo_unix.go"), -// not the names of the actual temporary files. -// -// The advantage of this approach is its fidelity to 'go build'. The -// downside is that the token.Position.Offset for each AST node is -// incorrect, being an offset within the temporary file. Line numbers -// should still be correct because of the //line comments. -// -// The logic of this file is mostly plundered from the 'go build' -// tool, which also invokes the cgo preprocessor. -// -// -// REJECTED ALTERNATIVE -// -// An alternative approach that we explored is to extend go/types' -// Importer mechanism to provide the identity of the importing package -// so that each time `import "C"` appears it resolves to a different -// synthetic package containing just the objects needed in that case. -// The loader would invoke cgo but parse only the cgo_types.go file -// defining the package-level objects, discarding the other files -// resulting from preprocessing. -// -// The benefit of this approach would have been that source-level -// syntax information would correspond exactly to the original cgo -// file, with no preprocessing involved, making source tools like -// godoc, guru, and eg happy. However, the approach was rejected -// due to the additional complexity it would impose on go/types. (It -// made for a beautiful demo, though.) -// -// cgo files, despite their *.go extension, are not legal Go source -// files per the specification since they may refer to unexported -// members of package "C" such as C.int. Also, a function such as -// C.getpwent has in effect two types, one matching its C type and one -// which additionally returns (errno C.int). The cgo preprocessor -// uses name mangling to distinguish these two functions in the -// processed code, but go/types would need to duplicate this logic in -// its handling of function calls, analogous to the treatment of map -// lookups in which y=m[k] and y,ok=m[k] are both legal. - -import ( - "fmt" - "go/ast" - "go/build" - "go/parser" - "go/token" - "io/ioutil" - "log" - "os" - "os/exec" - "path/filepath" - "regexp" - "strings" -) - -// ProcessFiles invokes the cgo preprocessor on bp.CgoFiles, parses -// the output and returns the resulting ASTs. -// -func ProcessFiles(bp *build.Package, fset *token.FileSet, DisplayPath func(path string) string, mode parser.Mode) ([]*ast.File, error) { - tmpdir, err := ioutil.TempDir("", strings.Replace(bp.ImportPath, "/", "_", -1)+"_C") - if err != nil { - return nil, err - } - defer os.RemoveAll(tmpdir) - - pkgdir := bp.Dir - if DisplayPath != nil { - pkgdir = DisplayPath(pkgdir) - } - - cgoFiles, cgoDisplayFiles, err := Run(bp, pkgdir, tmpdir, false) - if err != nil { - return nil, err - } - var files []*ast.File - for i := range cgoFiles { - rd, err := os.Open(cgoFiles[i]) - if err != nil { - return nil, err - } - display := filepath.Join(bp.Dir, cgoDisplayFiles[i]) - f, err := parser.ParseFile(fset, display, rd, mode) - rd.Close() - if err != nil { - return nil, err - } - files = append(files, f) - } - return files, nil -} - -var cgoRe = regexp.MustCompile(`[/\\:]`) - -// Run invokes the cgo preprocessor on bp.CgoFiles and returns two -// lists of files: the resulting processed files (in temporary -// directory tmpdir) and the corresponding names of the unprocessed files. -// -// Run is adapted from (*builder).cgo in -// $GOROOT/src/cmd/go/build.go, but these features are unsupported: -// Objective C, CGOPKGPATH, CGO_FLAGS. -// -// If useabs is set to true, absolute paths of the bp.CgoFiles will be passed in -// to the cgo preprocessor. This in turn will set the // line comments -// referring to those files to use absolute paths. This is needed for -// go/packages using the legacy go list support so it is able to find -// the original files. -func Run(bp *build.Package, pkgdir, tmpdir string, useabs bool) (files, displayFiles []string, err error) { - cgoCPPFLAGS, _, _, _ := cflags(bp, true) - _, cgoexeCFLAGS, _, _ := cflags(bp, false) - - if len(bp.CgoPkgConfig) > 0 { - pcCFLAGS, err := pkgConfigFlags(bp) - if err != nil { - return nil, nil, err - } - cgoCPPFLAGS = append(cgoCPPFLAGS, pcCFLAGS...) - } - - // Allows including _cgo_export.h from .[ch] files in the package. - cgoCPPFLAGS = append(cgoCPPFLAGS, "-I", tmpdir) - - // _cgo_gotypes.go (displayed "C") contains the type definitions. - files = append(files, filepath.Join(tmpdir, "_cgo_gotypes.go")) - displayFiles = append(displayFiles, "C") - for _, fn := range bp.CgoFiles { - // "foo.cgo1.go" (displayed "foo.go") is the processed Go source. - f := cgoRe.ReplaceAllString(fn[:len(fn)-len("go")], "_") - files = append(files, filepath.Join(tmpdir, f+"cgo1.go")) - displayFiles = append(displayFiles, fn) - } - - var cgoflags []string - if bp.Goroot && bp.ImportPath == "runtime/cgo" { - cgoflags = append(cgoflags, "-import_runtime_cgo=false") - } - if bp.Goroot && bp.ImportPath == "runtime/race" || bp.ImportPath == "runtime/cgo" { - cgoflags = append(cgoflags, "-import_syscall=false") - } - - var cgoFiles []string = bp.CgoFiles - if useabs { - cgoFiles = make([]string, len(bp.CgoFiles)) - for i := range cgoFiles { - cgoFiles[i] = filepath.Join(pkgdir, bp.CgoFiles[i]) - } - } - - args := stringList( - "go", "tool", "cgo", "-objdir", tmpdir, cgoflags, "--", - cgoCPPFLAGS, cgoexeCFLAGS, cgoFiles, - ) - if false { - log.Printf("Running cgo for package %q: %s (dir=%s)", bp.ImportPath, args, pkgdir) - } - cmd := exec.Command(args[0], args[1:]...) - cmd.Dir = pkgdir - cmd.Stdout = os.Stderr - cmd.Stderr = os.Stderr - if err := cmd.Run(); err != nil { - return nil, nil, fmt.Errorf("cgo failed: %s: %s", args, err) - } - - return files, displayFiles, nil -} - -// -- unmodified from 'go build' --------------------------------------- - -// Return the flags to use when invoking the C or C++ compilers, or cgo. -func cflags(p *build.Package, def bool) (cppflags, cflags, cxxflags, ldflags []string) { - var defaults string - if def { - defaults = "-g -O2" - } - - cppflags = stringList(envList("CGO_CPPFLAGS", ""), p.CgoCPPFLAGS) - cflags = stringList(envList("CGO_CFLAGS", defaults), p.CgoCFLAGS) - cxxflags = stringList(envList("CGO_CXXFLAGS", defaults), p.CgoCXXFLAGS) - ldflags = stringList(envList("CGO_LDFLAGS", defaults), p.CgoLDFLAGS) - return -} - -// envList returns the value of the given environment variable broken -// into fields, using the default value when the variable is empty. -func envList(key, def string) []string { - v := os.Getenv(key) - if v == "" { - v = def - } - return strings.Fields(v) -} - -// stringList's arguments should be a sequence of string or []string values. -// stringList flattens them into a single []string. -func stringList(args ...interface{}) []string { - var x []string - for _, arg := range args { - switch arg := arg.(type) { - case []string: - x = append(x, arg...) - case string: - x = append(x, arg) - default: - panic("stringList: invalid argument") - } - } - return x -} diff --git a/vendor/golang.org/x/tools/go/internal/cgo/cgo_pkgconfig.go b/vendor/golang.org/x/tools/go/internal/cgo/cgo_pkgconfig.go deleted file mode 100644 index b5bb95a63e5..00000000000 --- a/vendor/golang.org/x/tools/go/internal/cgo/cgo_pkgconfig.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package cgo - -import ( - "errors" - "fmt" - "go/build" - "os/exec" - "strings" -) - -// pkgConfig runs pkg-config with the specified arguments and returns the flags it prints. -func pkgConfig(mode string, pkgs []string) (flags []string, err error) { - cmd := exec.Command("pkg-config", append([]string{mode}, pkgs...)...) - out, err := cmd.CombinedOutput() - if err != nil { - s := fmt.Sprintf("%s failed: %v", strings.Join(cmd.Args, " "), err) - if len(out) > 0 { - s = fmt.Sprintf("%s: %s", s, out) - } - return nil, errors.New(s) - } - if len(out) > 0 { - flags = strings.Fields(string(out)) - } - return -} - -// pkgConfigFlags calls pkg-config if needed and returns the cflags -// needed to build the package. -func pkgConfigFlags(p *build.Package) (cflags []string, err error) { - if len(p.CgoPkgConfig) == 0 { - return nil, nil - } - return pkgConfig("--cflags", p.CgoPkgConfig) -} diff --git a/vendor/golang.org/x/tools/go/internal/gcimporter/gcimporter.go b/vendor/golang.org/x/tools/go/internal/gcimporter/gcimporter.go index 9cf186605f6..8dcd8bbb71a 100644 --- a/vendor/golang.org/x/tools/go/internal/gcimporter/gcimporter.go +++ b/vendor/golang.org/x/tools/go/internal/gcimporter/gcimporter.go @@ -344,7 +344,7 @@ func (p *parser) expectKeyword(keyword string) { // PackageId = string_lit . // -func (p *parser) parsePackageId() string { +func (p *parser) parsePackageID() string { id, err := strconv.Unquote(p.expect(scanner.String)) if err != nil { p.error(err) @@ -384,7 +384,7 @@ func (p *parser) parseDotIdent() string { // func (p *parser) parseQualifiedName() (id, name string) { p.expect('@') - id = p.parsePackageId() + id = p.parsePackageID() p.expect('.') // Per rev f280b8a485fd (10/2/2013), qualified names may be used for anonymous fields. if p.tok == '?' { @@ -696,7 +696,7 @@ func (p *parser) parseInterfaceType(parent *types.Package) types.Type { // Complete requires the type's embedded interfaces to be fully defined, // but we do not define any - return types.NewInterface(methods, nil).Complete() + return newInterface(methods, nil).Complete() } // ChanType = ( "chan" [ "<-" ] | "<-" "chan" ) Type . @@ -785,7 +785,7 @@ func (p *parser) parseType(parent *types.Package) types.Type { func (p *parser) parseImportDecl() { p.expectKeyword("import") name := p.parsePackageName() - p.getPkg(p.parsePackageId(), name) + p.getPkg(p.parsePackageID(), name) } // int_lit = [ "+" | "-" ] { "0" ... "9" } . diff --git a/vendor/golang.org/x/tools/go/internal/packagesdriver/sizes.go b/vendor/golang.org/x/tools/go/internal/packagesdriver/sizes.go index db0c9a7ea61..5ee692d3833 100644 --- a/vendor/golang.org/x/tools/go/internal/packagesdriver/sizes.go +++ b/vendor/golang.org/x/tools/go/internal/packagesdriver/sizes.go @@ -11,11 +11,10 @@ import ( "encoding/json" "fmt" "go/types" - "log" - "os" "os/exec" "strings" - "time" + + "golang.org/x/tools/internal/gocommand" ) var debug = false @@ -78,97 +77,42 @@ func GetSizes(ctx context.Context, buildFlags, env []string, dir string, usesExp } func GetSizesGolist(ctx context.Context, buildFlags, env []string, dir string, usesExportData bool) (types.Sizes, error) { - args := []string{"list", "-f", "{{context.GOARCH}} {{context.Compiler}}"} - args = append(args, buildFlags...) - args = append(args, "--", "unsafe") - stdout, stderr, err := invokeGo(ctx, env, dir, usesExportData, args...) + inv := gocommand.Invocation{ + Verb: "list", + Args: []string{"-f", "{{context.GOARCH}} {{context.Compiler}}", "--", "unsafe"}, + Env: env, + BuildFlags: buildFlags, + WorkingDir: dir, + } + stdout, stderr, friendlyErr, rawErr := inv.RunRaw(ctx) var goarch, compiler string - if err != nil { - if strings.Contains(err.Error(), "cannot find main module") { + if rawErr != nil { + if strings.Contains(rawErr.Error(), "cannot find main module") { // User's running outside of a module. All bets are off. Get GOARCH and guess compiler is gc. // TODO(matloob): Is this a problem in practice? - envout, _, enverr := invokeGo(ctx, env, dir, usesExportData, "env", "GOARCH") + inv := gocommand.Invocation{ + Verb: "env", + Args: []string{"GOARCH"}, + Env: env, + WorkingDir: dir, + } + envout, enverr := inv.Run(ctx) if enverr != nil { - return nil, err + return nil, enverr } goarch = strings.TrimSpace(envout.String()) compiler = "gc" } else { - return nil, err + return nil, friendlyErr } } else { fields := strings.Fields(stdout.String()) if len(fields) < 2 { - return nil, fmt.Errorf("could not parse GOARCH and Go compiler in format \" \" from stdout of go command:\n%s\ndir: %s\nstdout: <<%s>>\nstderr: <<%s>>", - cmdDebugStr(env, args...), dir, stdout.String(), stderr.String()) + return nil, fmt.Errorf("could not parse GOARCH and Go compiler in format \" \":\nstdout: <<%s>>\nstderr: <<%s>>", + stdout.String(), stderr.String()) } goarch = fields[0] compiler = fields[1] } return types.SizesFor(compiler, goarch), nil } - -// invokeGo returns the stdout and stderr of a go command invocation. -func invokeGo(ctx context.Context, env []string, dir string, usesExportData bool, args ...string) (*bytes.Buffer, *bytes.Buffer, error) { - if debug { - defer func(start time.Time) { log.Printf("%s for %v", time.Since(start), cmdDebugStr(env, args...)) }(time.Now()) - } - stdout := new(bytes.Buffer) - stderr := new(bytes.Buffer) - cmd := exec.CommandContext(ctx, "go", args...) - // On darwin the cwd gets resolved to the real path, which breaks anything that - // expects the working directory to keep the original path, including the - // go command when dealing with modules. - // The Go stdlib has a special feature where if the cwd and the PWD are the - // same node then it trusts the PWD, so by setting it in the env for the child - // process we fix up all the paths returned by the go command. - cmd.Env = append(append([]string{}, env...), "PWD="+dir) - cmd.Dir = dir - cmd.Stdout = stdout - cmd.Stderr = stderr - if err := cmd.Run(); err != nil { - exitErr, ok := err.(*exec.ExitError) - if !ok { - // Catastrophic error: - // - executable not found - // - context cancellation - return nil, nil, fmt.Errorf("couldn't exec 'go %v': %s %T", args, err, err) - } - - // Export mode entails a build. - // If that build fails, errors appear on stderr - // (despite the -e flag) and the Export field is blank. - // Do not fail in that case. - if !usesExportData { - return nil, nil, fmt.Errorf("go %v: %s: %s", args, exitErr, stderr) - } - } - - // As of writing, go list -export prints some non-fatal compilation - // errors to stderr, even with -e set. We would prefer that it put - // them in the Package.Error JSON (see https://golang.org/issue/26319). - // In the meantime, there's nowhere good to put them, but they can - // be useful for debugging. Print them if $GOPACKAGESPRINTGOLISTERRORS - // is set. - if len(stderr.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTGOLISTERRORS") != "" { - fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(env, args...), stderr) - } - - // debugging - if false { - fmt.Fprintf(os.Stderr, "%s stdout: <<%s>>\n", cmdDebugStr(env, args...), stdout) - } - - return stdout, stderr, nil -} - -func cmdDebugStr(envlist []string, args ...string) string { - env := make(map[string]string) - for _, kv := range envlist { - split := strings.Split(kv, "=") - k, v := split[0], split[1] - env[k] = v - } - - return fmt.Sprintf("GOROOT=%v GOPATH=%v GO111MODULE=%v PWD=%v go %v", env["GOROOT"], env["GOPATH"], env["GO111MODULE"], env["PWD"], args) -} diff --git a/vendor/golang.org/x/tools/go/loader/doc.go b/vendor/golang.org/x/tools/go/loader/doc.go deleted file mode 100644 index c5aa31c1a02..00000000000 --- a/vendor/golang.org/x/tools/go/loader/doc.go +++ /dev/null @@ -1,204 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package loader loads a complete Go program from source code, parsing -// and type-checking the initial packages plus their transitive closure -// of dependencies. The ASTs and the derived facts are retained for -// later use. -// -// Deprecated: This is an older API and does not have support -// for modules. Use golang.org/x/tools/go/packages instead. -// -// The package defines two primary types: Config, which specifies a -// set of initial packages to load and various other options; and -// Program, which is the result of successfully loading the packages -// specified by a configuration. -// -// The configuration can be set directly, but *Config provides various -// convenience methods to simplify the common cases, each of which can -// be called any number of times. Finally, these are followed by a -// call to Load() to actually load and type-check the program. -// -// var conf loader.Config -// -// // Use the command-line arguments to specify -// // a set of initial packages to load from source. -// // See FromArgsUsage for help. -// rest, err := conf.FromArgs(os.Args[1:], wantTests) -// -// // Parse the specified files and create an ad hoc package with path "foo". -// // All files must have the same 'package' declaration. -// conf.CreateFromFilenames("foo", "foo.go", "bar.go") -// -// // Create an ad hoc package with path "foo" from -// // the specified already-parsed files. -// // All ASTs must have the same 'package' declaration. -// conf.CreateFromFiles("foo", parsedFiles) -// -// // Add "runtime" to the set of packages to be loaded. -// conf.Import("runtime") -// -// // Adds "fmt" and "fmt_test" to the set of packages -// // to be loaded. "fmt" will include *_test.go files. -// conf.ImportWithTests("fmt") -// -// // Finally, load all the packages specified by the configuration. -// prog, err := conf.Load() -// -// See examples_test.go for examples of API usage. -// -// -// CONCEPTS AND TERMINOLOGY -// -// The WORKSPACE is the set of packages accessible to the loader. The -// workspace is defined by Config.Build, a *build.Context. The -// default context treats subdirectories of $GOROOT and $GOPATH as -// packages, but this behavior may be overridden. -// -// An AD HOC package is one specified as a set of source files on the -// command line. In the simplest case, it may consist of a single file -// such as $GOROOT/src/net/http/triv.go. -// -// EXTERNAL TEST packages are those comprised of a set of *_test.go -// files all with the same 'package foo_test' declaration, all in the -// same directory. (go/build.Package calls these files XTestFiles.) -// -// An IMPORTABLE package is one that can be referred to by some import -// spec. Every importable package is uniquely identified by its -// PACKAGE PATH or just PATH, a string such as "fmt", "encoding/json", -// or "cmd/vendor/golang.org/x/arch/x86/x86asm". A package path -// typically denotes a subdirectory of the workspace. -// -// An import declaration uses an IMPORT PATH to refer to a package. -// Most import declarations use the package path as the import path. -// -// Due to VENDORING (https://golang.org/s/go15vendor), the -// interpretation of an import path may depend on the directory in which -// it appears. To resolve an import path to a package path, go/build -// must search the enclosing directories for a subdirectory named -// "vendor". -// -// ad hoc packages and external test packages are NON-IMPORTABLE. The -// path of an ad hoc package is inferred from the package -// declarations of its files and is therefore not a unique package key. -// For example, Config.CreatePkgs may specify two initial ad hoc -// packages, both with path "main". -// -// An AUGMENTED package is an importable package P plus all the -// *_test.go files with same 'package foo' declaration as P. -// (go/build.Package calls these files TestFiles.) -// -// The INITIAL packages are those specified in the configuration. A -// DEPENDENCY is a package loaded to satisfy an import in an initial -// package or another dependency. -// -package loader - -// IMPLEMENTATION NOTES -// -// 'go test', in-package test files, and import cycles -// --------------------------------------------------- -// -// An external test package may depend upon members of the augmented -// package that are not in the unaugmented package, such as functions -// that expose internals. (See bufio/export_test.go for an example.) -// So, the loader must ensure that for each external test package -// it loads, it also augments the corresponding non-test package. -// -// The import graph over n unaugmented packages must be acyclic; the -// import graph over n-1 unaugmented packages plus one augmented -// package must also be acyclic. ('go test' relies on this.) But the -// import graph over n augmented packages may contain cycles. -// -// First, all the (unaugmented) non-test packages and their -// dependencies are imported in the usual way; the loader reports an -// error if it detects an import cycle. -// -// Then, each package P for which testing is desired is augmented by -// the list P' of its in-package test files, by calling -// (*types.Checker).Files. This arrangement ensures that P' may -// reference definitions within P, but P may not reference definitions -// within P'. Furthermore, P' may import any other package, including -// ones that depend upon P, without an import cycle error. -// -// Consider two packages A and B, both of which have lists of -// in-package test files we'll call A' and B', and which have the -// following import graph edges: -// B imports A -// B' imports A -// A' imports B -// This last edge would be expected to create an error were it not -// for the special type-checking discipline above. -// Cycles of size greater than two are possible. For example: -// compress/bzip2/bzip2_test.go (package bzip2) imports "io/ioutil" -// io/ioutil/tempfile_test.go (package ioutil) imports "regexp" -// regexp/exec_test.go (package regexp) imports "compress/bzip2" -// -// -// Concurrency -// ----------- -// -// Let us define the import dependency graph as follows. Each node is a -// list of files passed to (Checker).Files at once. Many of these lists -// are the production code of an importable Go package, so those nodes -// are labelled by the package's path. The remaining nodes are -// ad hoc packages and lists of in-package *_test.go files that augment -// an importable package; those nodes have no label. -// -// The edges of the graph represent import statements appearing within a -// file. An edge connects a node (a list of files) to the node it -// imports, which is importable and thus always labelled. -// -// Loading is controlled by this dependency graph. -// -// To reduce I/O latency, we start loading a package's dependencies -// asynchronously as soon as we've parsed its files and enumerated its -// imports (scanImports). This performs a preorder traversal of the -// import dependency graph. -// -// To exploit hardware parallelism, we type-check unrelated packages in -// parallel, where "unrelated" means not ordered by the partial order of -// the import dependency graph. -// -// We use a concurrency-safe non-blocking cache (importer.imported) to -// record the results of type-checking, whether success or failure. An -// entry is created in this cache by startLoad the first time the -// package is imported. The first goroutine to request an entry becomes -// responsible for completing the task and broadcasting completion to -// subsequent requestors, which block until then. -// -// Type checking occurs in (parallel) postorder: we cannot type-check a -// set of files until we have loaded and type-checked all of their -// immediate dependencies (and thus all of their transitive -// dependencies). If the input were guaranteed free of import cycles, -// this would be trivial: we could simply wait for completion of the -// dependencies and then invoke the typechecker. -// -// But as we saw in the 'go test' section above, some cycles in the -// import graph over packages are actually legal, so long as the -// cycle-forming edge originates in the in-package test files that -// augment the package. This explains why the nodes of the import -// dependency graph are not packages, but lists of files: the unlabelled -// nodes avoid the cycles. Consider packages A and B where B imports A -// and A's in-package tests AT import B. The naively constructed import -// graph over packages would contain a cycle (A+AT) --> B --> (A+AT) but -// the graph over lists of files is AT --> B --> A, where AT is an -// unlabelled node. -// -// Awaiting completion of the dependencies in a cyclic graph would -// deadlock, so we must materialize the import dependency graph (as -// importer.graph) and check whether each import edge forms a cycle. If -// x imports y, and the graph already contains a path from y to x, then -// there is an import cycle, in which case the processing of x must not -// wait for the completion of processing of y. -// -// When the type-checker makes a callback (doImport) to the loader for a -// given import edge, there are two possible cases. In the normal case, -// the dependency has already been completely type-checked; doImport -// does a cache lookup and returns it. In the cyclic case, the entry in -// the cache is still necessarily incomplete, indicating a cycle. We -// perform the cycle check again to obtain the error message, and return -// the error. -// -// The result of using concurrency is about a 2.5x speedup for stdlib_test. diff --git a/vendor/golang.org/x/tools/go/loader/loader.go b/vendor/golang.org/x/tools/go/loader/loader.go deleted file mode 100644 index bc12ca33d1a..00000000000 --- a/vendor/golang.org/x/tools/go/loader/loader.go +++ /dev/null @@ -1,1086 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package loader - -// See doc.go for package documentation and implementation notes. - -import ( - "errors" - "fmt" - "go/ast" - "go/build" - "go/parser" - "go/token" - "go/types" - "os" - "path/filepath" - "sort" - "strings" - "sync" - "time" - - "golang.org/x/tools/go/ast/astutil" - "golang.org/x/tools/go/internal/cgo" -) - -var ignoreVendor build.ImportMode - -const trace = false // show timing info for type-checking - -// Config specifies the configuration for loading a whole program from -// Go source code. -// The zero value for Config is a ready-to-use default configuration. -type Config struct { - // Fset is the file set for the parser to use when loading the - // program. If nil, it may be lazily initialized by any - // method of Config. - Fset *token.FileSet - - // ParserMode specifies the mode to be used by the parser when - // loading source packages. - ParserMode parser.Mode - - // TypeChecker contains options relating to the type checker. - // - // The supplied IgnoreFuncBodies is not used; the effective - // value comes from the TypeCheckFuncBodies func below. - // The supplied Import function is not used either. - TypeChecker types.Config - - // TypeCheckFuncBodies is a predicate over package paths. - // A package for which the predicate is false will - // have its package-level declarations type checked, but not - // its function bodies; this can be used to quickly load - // dependencies from source. If nil, all func bodies are type - // checked. - TypeCheckFuncBodies func(path string) bool - - // If Build is non-nil, it is used to locate source packages. - // Otherwise &build.Default is used. - // - // By default, cgo is invoked to preprocess Go files that - // import the fake package "C". This behaviour can be - // disabled by setting CGO_ENABLED=0 in the environment prior - // to startup, or by setting Build.CgoEnabled=false. - Build *build.Context - - // The current directory, used for resolving relative package - // references such as "./go/loader". If empty, os.Getwd will be - // used instead. - Cwd string - - // If DisplayPath is non-nil, it is used to transform each - // file name obtained from Build.Import(). This can be used - // to prevent a virtualized build.Config's file names from - // leaking into the user interface. - DisplayPath func(path string) string - - // If AllowErrors is true, Load will return a Program even - // if some of the its packages contained I/O, parser or type - // errors; such errors are accessible via PackageInfo.Errors. If - // false, Load will fail if any package had an error. - AllowErrors bool - - // CreatePkgs specifies a list of non-importable initial - // packages to create. The resulting packages will appear in - // the corresponding elements of the Program.Created slice. - CreatePkgs []PkgSpec - - // ImportPkgs specifies a set of initial packages to load. - // The map keys are package paths. - // - // The map value indicates whether to load tests. If true, Load - // will add and type-check two lists of files to the package: - // non-test files followed by in-package *_test.go files. In - // addition, it will append the external test package (if any) - // to Program.Created. - ImportPkgs map[string]bool - - // FindPackage is called during Load to create the build.Package - // for a given import path from a given directory. - // If FindPackage is nil, (*build.Context).Import is used. - // A client may use this hook to adapt to a proprietary build - // system that does not follow the "go build" layout - // conventions, for example. - // - // It must be safe to call concurrently from multiple goroutines. - FindPackage func(ctxt *build.Context, importPath, fromDir string, mode build.ImportMode) (*build.Package, error) - - // AfterTypeCheck is called immediately after a list of files - // has been type-checked and appended to info.Files. - // - // This optional hook function is the earliest opportunity for - // the client to observe the output of the type checker, - // which may be useful to reduce analysis latency when loading - // a large program. - // - // The function is permitted to modify info.Info, for instance - // to clear data structures that are no longer needed, which can - // dramatically reduce peak memory consumption. - // - // The function may be called twice for the same PackageInfo: - // once for the files of the package and again for the - // in-package test files. - // - // It must be safe to call concurrently from multiple goroutines. - AfterTypeCheck func(info *PackageInfo, files []*ast.File) -} - -// A PkgSpec specifies a non-importable package to be created by Load. -// Files are processed first, but typically only one of Files and -// Filenames is provided. The path needn't be globally unique. -// -// For vendoring purposes, the package's directory is the one that -// contains the first file. -type PkgSpec struct { - Path string // package path ("" => use package declaration) - Files []*ast.File // ASTs of already-parsed files - Filenames []string // names of files to be parsed -} - -// A Program is a Go program loaded from source as specified by a Config. -type Program struct { - Fset *token.FileSet // the file set for this program - - // Created[i] contains the initial package whose ASTs or - // filenames were supplied by Config.CreatePkgs[i], followed by - // the external test package, if any, of each package in - // Config.ImportPkgs ordered by ImportPath. - // - // NOTE: these files must not import "C". Cgo preprocessing is - // only performed on imported packages, not ad hoc packages. - // - // TODO(adonovan): we need to copy and adapt the logic of - // goFilesPackage (from $GOROOT/src/cmd/go/build.go) and make - // Config.Import and Config.Create methods return the same kind - // of entity, essentially a build.Package. - // Perhaps we can even reuse that type directly. - Created []*PackageInfo - - // Imported contains the initially imported packages, - // as specified by Config.ImportPkgs. - Imported map[string]*PackageInfo - - // AllPackages contains the PackageInfo of every package - // encountered by Load: all initial packages and all - // dependencies, including incomplete ones. - AllPackages map[*types.Package]*PackageInfo - - // importMap is the canonical mapping of package paths to - // packages. It contains all Imported initial packages, but not - // Created ones, and all imported dependencies. - importMap map[string]*types.Package -} - -// PackageInfo holds the ASTs and facts derived by the type-checker -// for a single package. -// -// Not mutated once exposed via the API. -// -type PackageInfo struct { - Pkg *types.Package - Importable bool // true if 'import "Pkg.Path()"' would resolve to this - TransitivelyErrorFree bool // true if Pkg and all its dependencies are free of errors - Files []*ast.File // syntax trees for the package's files - Errors []error // non-nil if the package had errors - types.Info // type-checker deductions. - dir string // package directory - - checker *types.Checker // transient type-checker state - errorFunc func(error) -} - -func (info *PackageInfo) String() string { return info.Pkg.Path() } - -func (info *PackageInfo) appendError(err error) { - if info.errorFunc != nil { - info.errorFunc(err) - } else { - fmt.Fprintln(os.Stderr, err) - } - info.Errors = append(info.Errors, err) -} - -func (conf *Config) fset() *token.FileSet { - if conf.Fset == nil { - conf.Fset = token.NewFileSet() - } - return conf.Fset -} - -// ParseFile is a convenience function (intended for testing) that invokes -// the parser using the Config's FileSet, which is initialized if nil. -// -// src specifies the parser input as a string, []byte, or io.Reader, and -// filename is its apparent name. If src is nil, the contents of -// filename are read from the file system. -// -func (conf *Config) ParseFile(filename string, src interface{}) (*ast.File, error) { - // TODO(adonovan): use conf.build() etc like parseFiles does. - return parser.ParseFile(conf.fset(), filename, src, conf.ParserMode) -} - -// FromArgsUsage is a partial usage message that applications calling -// FromArgs may wish to include in their -help output. -const FromArgsUsage = ` - is a list of arguments denoting a set of initial packages. -It may take one of two forms: - -1. A list of *.go source files. - - All of the specified files are loaded, parsed and type-checked - as a single package. All the files must belong to the same directory. - -2. A list of import paths, each denoting a package. - - The package's directory is found relative to the $GOROOT and - $GOPATH using similar logic to 'go build', and the *.go files in - that directory are loaded, parsed and type-checked as a single - package. - - In addition, all *_test.go files in the directory are then loaded - and parsed. Those files whose package declaration equals that of - the non-*_test.go files are included in the primary package. Test - files whose package declaration ends with "_test" are type-checked - as another package, the 'external' test package, so that a single - import path may denote two packages. (Whether this behaviour is - enabled is tool-specific, and may depend on additional flags.) - -A '--' argument terminates the list of packages. -` - -// FromArgs interprets args as a set of initial packages to load from -// source and updates the configuration. It returns the list of -// unconsumed arguments. -// -// It is intended for use in command-line interfaces that require a -// set of initial packages to be specified; see FromArgsUsage message -// for details. -// -// Only superficial errors are reported at this stage; errors dependent -// on I/O are detected during Load. -// -func (conf *Config) FromArgs(args []string, xtest bool) ([]string, error) { - var rest []string - for i, arg := range args { - if arg == "--" { - rest = args[i+1:] - args = args[:i] - break // consume "--" and return the remaining args - } - } - - if len(args) > 0 && strings.HasSuffix(args[0], ".go") { - // Assume args is a list of a *.go files - // denoting a single ad hoc package. - for _, arg := range args { - if !strings.HasSuffix(arg, ".go") { - return nil, fmt.Errorf("named files must be .go files: %s", arg) - } - } - conf.CreateFromFilenames("", args...) - } else { - // Assume args are directories each denoting a - // package and (perhaps) an external test, iff xtest. - for _, arg := range args { - if xtest { - conf.ImportWithTests(arg) - } else { - conf.Import(arg) - } - } - } - - return rest, nil -} - -// CreateFromFilenames is a convenience function that adds -// a conf.CreatePkgs entry to create a package of the specified *.go -// files. -// -func (conf *Config) CreateFromFilenames(path string, filenames ...string) { - conf.CreatePkgs = append(conf.CreatePkgs, PkgSpec{Path: path, Filenames: filenames}) -} - -// CreateFromFiles is a convenience function that adds a conf.CreatePkgs -// entry to create package of the specified path and parsed files. -// -func (conf *Config) CreateFromFiles(path string, files ...*ast.File) { - conf.CreatePkgs = append(conf.CreatePkgs, PkgSpec{Path: path, Files: files}) -} - -// ImportWithTests is a convenience function that adds path to -// ImportPkgs, the set of initial source packages located relative to -// $GOPATH. The package will be augmented by any *_test.go files in -// its directory that contain a "package x" (not "package x_test") -// declaration. -// -// In addition, if any *_test.go files contain a "package x_test" -// declaration, an additional package comprising just those files will -// be added to CreatePkgs. -// -func (conf *Config) ImportWithTests(path string) { conf.addImport(path, true) } - -// Import is a convenience function that adds path to ImportPkgs, the -// set of initial packages that will be imported from source. -// -func (conf *Config) Import(path string) { conf.addImport(path, false) } - -func (conf *Config) addImport(path string, tests bool) { - if path == "C" { - return // ignore; not a real package - } - if conf.ImportPkgs == nil { - conf.ImportPkgs = make(map[string]bool) - } - conf.ImportPkgs[path] = conf.ImportPkgs[path] || tests -} - -// PathEnclosingInterval returns the PackageInfo and ast.Node that -// contain source interval [start, end), and all the node's ancestors -// up to the AST root. It searches all ast.Files of all packages in prog. -// exact is defined as for astutil.PathEnclosingInterval. -// -// The zero value is returned if not found. -// -func (prog *Program) PathEnclosingInterval(start, end token.Pos) (pkg *PackageInfo, path []ast.Node, exact bool) { - for _, info := range prog.AllPackages { - for _, f := range info.Files { - if f.Pos() == token.NoPos { - // This can happen if the parser saw - // too many errors and bailed out. - // (Use parser.AllErrors to prevent that.) - continue - } - if !tokenFileContainsPos(prog.Fset.File(f.Pos()), start) { - continue - } - if path, exact := astutil.PathEnclosingInterval(f, start, end); path != nil { - return info, path, exact - } - } - } - return nil, nil, false -} - -// InitialPackages returns a new slice containing the set of initial -// packages (Created + Imported) in unspecified order. -// -func (prog *Program) InitialPackages() []*PackageInfo { - infos := make([]*PackageInfo, 0, len(prog.Created)+len(prog.Imported)) - infos = append(infos, prog.Created...) - for _, info := range prog.Imported { - infos = append(infos, info) - } - return infos -} - -// Package returns the ASTs and results of type checking for the -// specified package. -func (prog *Program) Package(path string) *PackageInfo { - if info, ok := prog.AllPackages[prog.importMap[path]]; ok { - return info - } - for _, info := range prog.Created { - if path == info.Pkg.Path() { - return info - } - } - return nil -} - -// ---------- Implementation ---------- - -// importer holds the working state of the algorithm. -type importer struct { - conf *Config // the client configuration - start time.Time // for logging - - progMu sync.Mutex // guards prog - prog *Program // the resulting program - - // findpkg is a memoization of FindPackage. - findpkgMu sync.Mutex // guards findpkg - findpkg map[findpkgKey]*findpkgValue - - importedMu sync.Mutex // guards imported - imported map[string]*importInfo // all imported packages (incl. failures) by import path - - // import dependency graph: graph[x][y] => x imports y - // - // Since non-importable packages cannot be cyclic, we ignore - // their imports, thus we only need the subgraph over importable - // packages. Nodes are identified by their import paths. - graphMu sync.Mutex - graph map[string]map[string]bool -} - -type findpkgKey struct { - importPath string - fromDir string - mode build.ImportMode -} - -type findpkgValue struct { - ready chan struct{} // closed to broadcast readiness - bp *build.Package - err error -} - -// importInfo tracks the success or failure of a single import. -// -// Upon completion, exactly one of info and err is non-nil: -// info on successful creation of a package, err otherwise. -// A successful package may still contain type errors. -// -type importInfo struct { - path string // import path - info *PackageInfo // results of typechecking (including errors) - complete chan struct{} // closed to broadcast that info is set. -} - -// awaitCompletion blocks until ii is complete, -// i.e. the info field is safe to inspect. -func (ii *importInfo) awaitCompletion() { - <-ii.complete // wait for close -} - -// Complete marks ii as complete. -// Its info and err fields will not be subsequently updated. -func (ii *importInfo) Complete(info *PackageInfo) { - if info == nil { - panic("info == nil") - } - ii.info = info - close(ii.complete) -} - -type importError struct { - path string // import path - err error // reason for failure to create a package -} - -// Load creates the initial packages specified by conf.{Create,Import}Pkgs, -// loading their dependencies packages as needed. -// -// On success, Load returns a Program containing a PackageInfo for -// each package. On failure, it returns an error. -// -// If AllowErrors is true, Load will return a Program even if some -// packages contained I/O, parser or type errors, or if dependencies -// were missing. (Such errors are accessible via PackageInfo.Errors. If -// false, Load will fail if any package had an error. -// -// It is an error if no packages were loaded. -// -func (conf *Config) Load() (*Program, error) { - // Create a simple default error handler for parse/type errors. - if conf.TypeChecker.Error == nil { - conf.TypeChecker.Error = func(e error) { fmt.Fprintln(os.Stderr, e) } - } - - // Set default working directory for relative package references. - if conf.Cwd == "" { - var err error - conf.Cwd, err = os.Getwd() - if err != nil { - return nil, err - } - } - - // Install default FindPackage hook using go/build logic. - if conf.FindPackage == nil { - conf.FindPackage = (*build.Context).Import - } - - prog := &Program{ - Fset: conf.fset(), - Imported: make(map[string]*PackageInfo), - importMap: make(map[string]*types.Package), - AllPackages: make(map[*types.Package]*PackageInfo), - } - - imp := importer{ - conf: conf, - prog: prog, - findpkg: make(map[findpkgKey]*findpkgValue), - imported: make(map[string]*importInfo), - start: time.Now(), - graph: make(map[string]map[string]bool), - } - - // -- loading proper (concurrent phase) -------------------------------- - - var errpkgs []string // packages that contained errors - - // Load the initially imported packages and their dependencies, - // in parallel. - // No vendor check on packages imported from the command line. - infos, importErrors := imp.importAll("", conf.Cwd, conf.ImportPkgs, ignoreVendor) - for _, ie := range importErrors { - conf.TypeChecker.Error(ie.err) // failed to create package - errpkgs = append(errpkgs, ie.path) - } - for _, info := range infos { - prog.Imported[info.Pkg.Path()] = info - } - - // Augment the designated initial packages by their tests. - // Dependencies are loaded in parallel. - var xtestPkgs []*build.Package - for importPath, augment := range conf.ImportPkgs { - if !augment { - continue - } - - // No vendor check on packages imported from command line. - bp, err := imp.findPackage(importPath, conf.Cwd, ignoreVendor) - if err != nil { - // Package not found, or can't even parse package declaration. - // Already reported by previous loop; ignore it. - continue - } - - // Needs external test package? - if len(bp.XTestGoFiles) > 0 { - xtestPkgs = append(xtestPkgs, bp) - } - - // Consult the cache using the canonical package path. - path := bp.ImportPath - imp.importedMu.Lock() // (unnecessary, we're sequential here) - ii, ok := imp.imported[path] - // Paranoid checks added due to issue #11012. - if !ok { - // Unreachable. - // The previous loop called importAll and thus - // startLoad for each path in ImportPkgs, which - // populates imp.imported[path] with a non-zero value. - panic(fmt.Sprintf("imported[%q] not found", path)) - } - if ii == nil { - // Unreachable. - // The ii values in this loop are the same as in - // the previous loop, which enforced the invariant - // that at least one of ii.err and ii.info is non-nil. - panic(fmt.Sprintf("imported[%q] == nil", path)) - } - if ii.info == nil { - // Unreachable. - // awaitCompletion has the postcondition - // ii.info != nil. - panic(fmt.Sprintf("imported[%q].info = nil", path)) - } - info := ii.info - imp.importedMu.Unlock() - - // Parse the in-package test files. - files, errs := imp.conf.parsePackageFiles(bp, 't') - for _, err := range errs { - info.appendError(err) - } - - // The test files augmenting package P cannot be imported, - // but may import packages that import P, - // so we must disable the cycle check. - imp.addFiles(info, files, false) - } - - createPkg := func(path, dir string, files []*ast.File, errs []error) { - info := imp.newPackageInfo(path, dir) - for _, err := range errs { - info.appendError(err) - } - - // Ad hoc packages are non-importable, - // so no cycle check is needed. - // addFiles loads dependencies in parallel. - imp.addFiles(info, files, false) - prog.Created = append(prog.Created, info) - } - - // Create packages specified by conf.CreatePkgs. - for _, cp := range conf.CreatePkgs { - files, errs := parseFiles(conf.fset(), conf.build(), nil, conf.Cwd, cp.Filenames, conf.ParserMode) - files = append(files, cp.Files...) - - path := cp.Path - if path == "" { - if len(files) > 0 { - path = files[0].Name.Name - } else { - path = "(unnamed)" - } - } - - dir := conf.Cwd - if len(files) > 0 && files[0].Pos().IsValid() { - dir = filepath.Dir(conf.fset().File(files[0].Pos()).Name()) - } - createPkg(path, dir, files, errs) - } - - // Create external test packages. - sort.Sort(byImportPath(xtestPkgs)) - for _, bp := range xtestPkgs { - files, errs := imp.conf.parsePackageFiles(bp, 'x') - createPkg(bp.ImportPath+"_test", bp.Dir, files, errs) - } - - // -- finishing up (sequential) ---------------------------------------- - - if len(prog.Imported)+len(prog.Created) == 0 { - return nil, errors.New("no initial packages were loaded") - } - - // Create infos for indirectly imported packages. - // e.g. incomplete packages without syntax, loaded from export data. - for _, obj := range prog.importMap { - info := prog.AllPackages[obj] - if info == nil { - prog.AllPackages[obj] = &PackageInfo{Pkg: obj, Importable: true} - } else { - // finished - info.checker = nil - info.errorFunc = nil - } - } - - if !conf.AllowErrors { - // Report errors in indirectly imported packages. - for _, info := range prog.AllPackages { - if len(info.Errors) > 0 { - errpkgs = append(errpkgs, info.Pkg.Path()) - } - } - if errpkgs != nil { - var more string - if len(errpkgs) > 3 { - more = fmt.Sprintf(" and %d more", len(errpkgs)-3) - errpkgs = errpkgs[:3] - } - return nil, fmt.Errorf("couldn't load packages due to errors: %s%s", - strings.Join(errpkgs, ", "), more) - } - } - - markErrorFreePackages(prog.AllPackages) - - return prog, nil -} - -type byImportPath []*build.Package - -func (b byImportPath) Len() int { return len(b) } -func (b byImportPath) Less(i, j int) bool { return b[i].ImportPath < b[j].ImportPath } -func (b byImportPath) Swap(i, j int) { b[i], b[j] = b[j], b[i] } - -// markErrorFreePackages sets the TransitivelyErrorFree flag on all -// applicable packages. -func markErrorFreePackages(allPackages map[*types.Package]*PackageInfo) { - // Build the transpose of the import graph. - importedBy := make(map[*types.Package]map[*types.Package]bool) - for P := range allPackages { - for _, Q := range P.Imports() { - clients, ok := importedBy[Q] - if !ok { - clients = make(map[*types.Package]bool) - importedBy[Q] = clients - } - clients[P] = true - } - } - - // Find all packages reachable from some error package. - reachable := make(map[*types.Package]bool) - var visit func(*types.Package) - visit = func(p *types.Package) { - if !reachable[p] { - reachable[p] = true - for q := range importedBy[p] { - visit(q) - } - } - } - for _, info := range allPackages { - if len(info.Errors) > 0 { - visit(info.Pkg) - } - } - - // Mark the others as "transitively error-free". - for _, info := range allPackages { - if !reachable[info.Pkg] { - info.TransitivelyErrorFree = true - } - } -} - -// build returns the effective build context. -func (conf *Config) build() *build.Context { - if conf.Build != nil { - return conf.Build - } - return &build.Default -} - -// parsePackageFiles enumerates the files belonging to package path, -// then loads, parses and returns them, plus a list of I/O or parse -// errors that were encountered. -// -// 'which' indicates which files to include: -// 'g': include non-test *.go source files (GoFiles + processed CgoFiles) -// 't': include in-package *_test.go source files (TestGoFiles) -// 'x': include external *_test.go source files. (XTestGoFiles) -// -func (conf *Config) parsePackageFiles(bp *build.Package, which rune) ([]*ast.File, []error) { - if bp.ImportPath == "unsafe" { - return nil, nil - } - var filenames []string - switch which { - case 'g': - filenames = bp.GoFiles - case 't': - filenames = bp.TestGoFiles - case 'x': - filenames = bp.XTestGoFiles - default: - panic(which) - } - - files, errs := parseFiles(conf.fset(), conf.build(), conf.DisplayPath, bp.Dir, filenames, conf.ParserMode) - - // Preprocess CgoFiles and parse the outputs (sequentially). - if which == 'g' && bp.CgoFiles != nil { - cgofiles, err := cgo.ProcessFiles(bp, conf.fset(), conf.DisplayPath, conf.ParserMode) - if err != nil { - errs = append(errs, err) - } else { - files = append(files, cgofiles...) - } - } - - return files, errs -} - -// doImport imports the package denoted by path. -// It implements the types.Importer signature. -// -// It returns an error if a package could not be created -// (e.g. go/build or parse error), but type errors are reported via -// the types.Config.Error callback (the first of which is also saved -// in the package's PackageInfo). -// -// Idempotent. -// -func (imp *importer) doImport(from *PackageInfo, to string) (*types.Package, error) { - if to == "C" { - // This should be unreachable, but ad hoc packages are - // not currently subject to cgo preprocessing. - // See https://golang.org/issue/11627. - return nil, fmt.Errorf(`the loader doesn't cgo-process ad hoc packages like %q; see Go issue 11627`, - from.Pkg.Path()) - } - - bp, err := imp.findPackage(to, from.dir, 0) - if err != nil { - return nil, err - } - - // The standard unsafe package is handled specially, - // and has no PackageInfo. - if bp.ImportPath == "unsafe" { - return types.Unsafe, nil - } - - // Look for the package in the cache using its canonical path. - path := bp.ImportPath - imp.importedMu.Lock() - ii := imp.imported[path] - imp.importedMu.Unlock() - if ii == nil { - panic("internal error: unexpected import: " + path) - } - if ii.info != nil { - return ii.info.Pkg, nil - } - - // Import of incomplete package: this indicates a cycle. - fromPath := from.Pkg.Path() - if cycle := imp.findPath(path, fromPath); cycle != nil { - // Normalize cycle: start from alphabetically largest node. - pos, start := -1, "" - for i, s := range cycle { - if pos < 0 || s > start { - pos, start = i, s - } - } - cycle = append(cycle, cycle[:pos]...)[pos:] // rotate cycle to start from largest - cycle = append(cycle, cycle[0]) // add start node to end to show cycliness - return nil, fmt.Errorf("import cycle: %s", strings.Join(cycle, " -> ")) - } - - panic("internal error: import of incomplete (yet acyclic) package: " + fromPath) -} - -// findPackage locates the package denoted by the importPath in the -// specified directory. -func (imp *importer) findPackage(importPath, fromDir string, mode build.ImportMode) (*build.Package, error) { - // We use a non-blocking duplicate-suppressing cache (gopl.io §9.7) - // to avoid holding the lock around FindPackage. - key := findpkgKey{importPath, fromDir, mode} - imp.findpkgMu.Lock() - v, ok := imp.findpkg[key] - if ok { - // cache hit - imp.findpkgMu.Unlock() - - <-v.ready // wait for entry to become ready - } else { - // Cache miss: this goroutine becomes responsible for - // populating the map entry and broadcasting its readiness. - v = &findpkgValue{ready: make(chan struct{})} - imp.findpkg[key] = v - imp.findpkgMu.Unlock() - - ioLimit <- true - v.bp, v.err = imp.conf.FindPackage(imp.conf.build(), importPath, fromDir, mode) - <-ioLimit - - if _, ok := v.err.(*build.NoGoError); ok { - v.err = nil // empty directory is not an error - } - - close(v.ready) // broadcast ready condition - } - return v.bp, v.err -} - -// importAll loads, parses, and type-checks the specified packages in -// parallel and returns their completed importInfos in unspecified order. -// -// fromPath is the package path of the importing package, if it is -// importable, "" otherwise. It is used for cycle detection. -// -// fromDir is the directory containing the import declaration that -// caused these imports. -// -func (imp *importer) importAll(fromPath, fromDir string, imports map[string]bool, mode build.ImportMode) (infos []*PackageInfo, errors []importError) { - // TODO(adonovan): opt: do the loop in parallel once - // findPackage is non-blocking. - var pending []*importInfo - for importPath := range imports { - bp, err := imp.findPackage(importPath, fromDir, mode) - if err != nil { - errors = append(errors, importError{ - path: importPath, - err: err, - }) - continue - } - pending = append(pending, imp.startLoad(bp)) - } - - if fromPath != "" { - // We're loading a set of imports. - // - // We must record graph edges from the importing package - // to its dependencies, and check for cycles. - imp.graphMu.Lock() - deps, ok := imp.graph[fromPath] - if !ok { - deps = make(map[string]bool) - imp.graph[fromPath] = deps - } - for _, ii := range pending { - deps[ii.path] = true - } - imp.graphMu.Unlock() - } - - for _, ii := range pending { - if fromPath != "" { - if cycle := imp.findPath(ii.path, fromPath); cycle != nil { - // Cycle-forming import: we must not await its - // completion since it would deadlock. - // - // We don't record the error in ii since - // the error is really associated with the - // cycle-forming edge, not the package itself. - // (Also it would complicate the - // invariants of importPath completion.) - if trace { - fmt.Fprintf(os.Stderr, "import cycle: %q\n", cycle) - } - continue - } - } - ii.awaitCompletion() - infos = append(infos, ii.info) - } - - return infos, errors -} - -// findPath returns an arbitrary path from 'from' to 'to' in the import -// graph, or nil if there was none. -func (imp *importer) findPath(from, to string) []string { - imp.graphMu.Lock() - defer imp.graphMu.Unlock() - - seen := make(map[string]bool) - var search func(stack []string, importPath string) []string - search = func(stack []string, importPath string) []string { - if !seen[importPath] { - seen[importPath] = true - stack = append(stack, importPath) - if importPath == to { - return stack - } - for x := range imp.graph[importPath] { - if p := search(stack, x); p != nil { - return p - } - } - } - return nil - } - return search(make([]string, 0, 20), from) -} - -// startLoad initiates the loading, parsing and type-checking of the -// specified package and its dependencies, if it has not already begun. -// -// It returns an importInfo, not necessarily in a completed state. The -// caller must call awaitCompletion() before accessing its info field. -// -// startLoad is concurrency-safe and idempotent. -// -func (imp *importer) startLoad(bp *build.Package) *importInfo { - path := bp.ImportPath - imp.importedMu.Lock() - ii, ok := imp.imported[path] - if !ok { - ii = &importInfo{path: path, complete: make(chan struct{})} - imp.imported[path] = ii - go func() { - info := imp.load(bp) - ii.Complete(info) - }() - } - imp.importedMu.Unlock() - - return ii -} - -// load implements package loading by parsing Go source files -// located by go/build. -func (imp *importer) load(bp *build.Package) *PackageInfo { - info := imp.newPackageInfo(bp.ImportPath, bp.Dir) - info.Importable = true - files, errs := imp.conf.parsePackageFiles(bp, 'g') - for _, err := range errs { - info.appendError(err) - } - - imp.addFiles(info, files, true) - - imp.progMu.Lock() - imp.prog.importMap[bp.ImportPath] = info.Pkg - imp.progMu.Unlock() - - return info -} - -// addFiles adds and type-checks the specified files to info, loading -// their dependencies if needed. The order of files determines the -// package initialization order. It may be called multiple times on the -// same package. Errors are appended to the info.Errors field. -// -// cycleCheck determines whether the imports within files create -// dependency edges that should be checked for potential cycles. -// -func (imp *importer) addFiles(info *PackageInfo, files []*ast.File, cycleCheck bool) { - // Ensure the dependencies are loaded, in parallel. - var fromPath string - if cycleCheck { - fromPath = info.Pkg.Path() - } - // TODO(adonovan): opt: make the caller do scanImports. - // Callers with a build.Package can skip it. - imp.importAll(fromPath, info.dir, scanImports(files), 0) - - if trace { - fmt.Fprintf(os.Stderr, "%s: start %q (%d)\n", - time.Since(imp.start), info.Pkg.Path(), len(files)) - } - - // Don't call checker.Files on Unsafe, even with zero files, - // because it would mutate the package, which is a global. - if info.Pkg == types.Unsafe { - if len(files) > 0 { - panic(`"unsafe" package contains unexpected files`) - } - } else { - // Ignore the returned (first) error since we - // already collect them all in the PackageInfo. - info.checker.Files(files) - info.Files = append(info.Files, files...) - } - - if imp.conf.AfterTypeCheck != nil { - imp.conf.AfterTypeCheck(info, files) - } - - if trace { - fmt.Fprintf(os.Stderr, "%s: stop %q\n", - time.Since(imp.start), info.Pkg.Path()) - } -} - -func (imp *importer) newPackageInfo(path, dir string) *PackageInfo { - var pkg *types.Package - if path == "unsafe" { - pkg = types.Unsafe - } else { - pkg = types.NewPackage(path, "") - } - info := &PackageInfo{ - Pkg: pkg, - Info: types.Info{ - Types: make(map[ast.Expr]types.TypeAndValue), - Defs: make(map[*ast.Ident]types.Object), - Uses: make(map[*ast.Ident]types.Object), - Implicits: make(map[ast.Node]types.Object), - Scopes: make(map[ast.Node]*types.Scope), - Selections: make(map[*ast.SelectorExpr]*types.Selection), - }, - errorFunc: imp.conf.TypeChecker.Error, - dir: dir, - } - - // Copy the types.Config so we can vary it across PackageInfos. - tc := imp.conf.TypeChecker - tc.IgnoreFuncBodies = false - if f := imp.conf.TypeCheckFuncBodies; f != nil { - tc.IgnoreFuncBodies = !f(path) - } - tc.Importer = closure{imp, info} - tc.Error = info.appendError // appendError wraps the user's Error function - - info.checker = types.NewChecker(&tc, imp.conf.fset(), pkg, &info.Info) - imp.progMu.Lock() - imp.prog.AllPackages[pkg] = info - imp.progMu.Unlock() - return info -} - -type closure struct { - imp *importer - info *PackageInfo -} - -func (c closure) Import(to string) (*types.Package, error) { return c.imp.doImport(c.info, to) } diff --git a/vendor/golang.org/x/tools/go/loader/util.go b/vendor/golang.org/x/tools/go/loader/util.go deleted file mode 100644 index 7f38dd74077..00000000000 --- a/vendor/golang.org/x/tools/go/loader/util.go +++ /dev/null @@ -1,124 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package loader - -import ( - "go/ast" - "go/build" - "go/parser" - "go/token" - "io" - "os" - "strconv" - "sync" - - "golang.org/x/tools/go/buildutil" -) - -// We use a counting semaphore to limit -// the number of parallel I/O calls per process. -var ioLimit = make(chan bool, 10) - -// parseFiles parses the Go source files within directory dir and -// returns the ASTs of the ones that could be at least partially parsed, -// along with a list of I/O and parse errors encountered. -// -// I/O is done via ctxt, which may specify a virtual file system. -// displayPath is used to transform the filenames attached to the ASTs. -// -func parseFiles(fset *token.FileSet, ctxt *build.Context, displayPath func(string) string, dir string, files []string, mode parser.Mode) ([]*ast.File, []error) { - if displayPath == nil { - displayPath = func(path string) string { return path } - } - var wg sync.WaitGroup - n := len(files) - parsed := make([]*ast.File, n) - errors := make([]error, n) - for i, file := range files { - if !buildutil.IsAbsPath(ctxt, file) { - file = buildutil.JoinPath(ctxt, dir, file) - } - wg.Add(1) - go func(i int, file string) { - ioLimit <- true // wait - defer func() { - wg.Done() - <-ioLimit // signal - }() - var rd io.ReadCloser - var err error - if ctxt.OpenFile != nil { - rd, err = ctxt.OpenFile(file) - } else { - rd, err = os.Open(file) - } - if err != nil { - errors[i] = err // open failed - return - } - - // ParseFile may return both an AST and an error. - parsed[i], errors[i] = parser.ParseFile(fset, displayPath(file), rd, mode) - rd.Close() - }(i, file) - } - wg.Wait() - - // Eliminate nils, preserving order. - var o int - for _, f := range parsed { - if f != nil { - parsed[o] = f - o++ - } - } - parsed = parsed[:o] - - o = 0 - for _, err := range errors { - if err != nil { - errors[o] = err - o++ - } - } - errors = errors[:o] - - return parsed, errors -} - -// scanImports returns the set of all import paths from all -// import specs in the specified files. -func scanImports(files []*ast.File) map[string]bool { - imports := make(map[string]bool) - for _, f := range files { - for _, decl := range f.Decls { - if decl, ok := decl.(*ast.GenDecl); ok && decl.Tok == token.IMPORT { - for _, spec := range decl.Specs { - spec := spec.(*ast.ImportSpec) - - // NB: do not assume the program is well-formed! - path, err := strconv.Unquote(spec.Path.Value) - if err != nil { - continue // quietly ignore the error - } - if path == "C" { - continue // skip pseudopackage - } - imports[path] = true - } - } - } - } - return imports -} - -// ---------- Internal helpers ---------- - -// TODO(adonovan): make this a method: func (*token.File) Contains(token.Pos) -func tokenFileContainsPos(f *token.File, pos token.Pos) bool { - p := int(pos) - base := f.Base() - return base <= p && p < base+f.Size() -} diff --git a/vendor/golang.org/x/tools/go/packages/golist.go b/vendor/golang.org/x/tools/go/packages/golist.go index a9a1ba89e81..b4a13ef4545 100644 --- a/vendor/golang.org/x/tools/go/packages/golist.go +++ b/vendor/golang.org/x/tools/go/packages/golist.go @@ -6,26 +6,25 @@ package packages import ( "bytes" + "context" "encoding/json" "fmt" "go/types" - "io/ioutil" "log" "os" "os/exec" "path" "path/filepath" "reflect" - "regexp" + "sort" "strconv" "strings" "sync" - "time" "unicode" "golang.org/x/tools/go/internal/packagesdriver" - "golang.org/x/tools/internal/gopathwalk" - "golang.org/x/tools/internal/semver" + "golang.org/x/tools/internal/gocommand" + "golang.org/x/tools/internal/packagesinternal" ) // debug controls verbose logging. @@ -44,16 +43,21 @@ type responseDeduper struct { dr *driverResponse } -// init fills in r with a driverResponse. -func (r *responseDeduper) init(dr *driverResponse) { - r.dr = dr - r.seenRoots = map[string]bool{} - r.seenPackages = map[string]*Package{} +func newDeduper() *responseDeduper { + return &responseDeduper{ + dr: &driverResponse{}, + seenRoots: map[string]bool{}, + seenPackages: map[string]*Package{}, + } +} + +// addAll fills in r with a driverResponse. +func (r *responseDeduper) addAll(dr *driverResponse) { for _, pkg := range dr.Packages { - r.seenPackages[pkg.ID] = pkg + r.addPackage(pkg) } for _, root := range dr.Roots { - r.seenRoots[root] = true + r.addRoot(root) } } @@ -73,25 +77,47 @@ func (r *responseDeduper) addRoot(id string) { r.dr.Roots = append(r.dr.Roots, id) } -// goInfo contains global information from the go tool. -type goInfo struct { - rootDirs map[string]string - env goEnv +type golistState struct { + cfg *Config + ctx context.Context + + envOnce sync.Once + goEnvError error + goEnv map[string]string + + rootsOnce sync.Once + rootDirsError error + rootDirs map[string]string + + // vendorDirs caches the (non)existence of vendor directories. + vendorDirs map[string]bool } -type goEnv struct { - modulesOn bool +// getEnv returns Go environment variables. Only specific variables are +// populated -- computing all of them is slow. +func (state *golistState) getEnv() (map[string]string, error) { + state.envOnce.Do(func() { + var b *bytes.Buffer + b, state.goEnvError = state.invokeGo("env", "-json", "GOMOD", "GOPATH") + if state.goEnvError != nil { + return + } + + state.goEnv = make(map[string]string) + decoder := json.NewDecoder(b) + if state.goEnvError = decoder.Decode(&state.goEnv); state.goEnvError != nil { + return + } + }) + return state.goEnv, state.goEnvError } -func determineEnv(cfg *Config) goEnv { - buf, err := invokeGo(cfg, "env", "GOMOD") +// mustGetEnv is a convenience function that can be used if getEnv has already succeeded. +func (state *golistState) mustGetEnv() map[string]string { + env, err := state.getEnv() if err != nil { - return goEnv{} + panic(fmt.Sprintf("mustGetEnv: %v", err)) } - gomod := bytes.TrimSpace(buf.Bytes()) - - env := goEnv{} - env.modulesOn = len(gomod) > 0 return env } @@ -99,47 +125,38 @@ func determineEnv(cfg *Config) goEnv { // the build system package structure. // See driver for more details. func goListDriver(cfg *Config, patterns ...string) (*driverResponse, error) { - var sizes types.Sizes + // Make sure that any asynchronous go commands are killed when we return. + parentCtx := cfg.Context + if parentCtx == nil { + parentCtx = context.Background() + } + ctx, cancel := context.WithCancel(parentCtx) + defer cancel() + + response := newDeduper() + + // Fill in response.Sizes asynchronously if necessary. var sizeserr error var sizeswg sync.WaitGroup if cfg.Mode&NeedTypesSizes != 0 || cfg.Mode&NeedTypes != 0 { sizeswg.Add(1) go func() { - sizes, sizeserr = getSizes(cfg) + var sizes types.Sizes + sizes, sizeserr = packagesdriver.GetSizesGolist(ctx, cfg.BuildFlags, cfg.Env, cfg.Dir, usesExportData(cfg)) + // types.SizesFor always returns nil or a *types.StdSizes. + response.dr.Sizes, _ = sizes.(*types.StdSizes) sizeswg.Done() }() } - defer sizeswg.Wait() - // start fetching rootDirs - var info goInfo - var rootDirsReady, envReady = make(chan struct{}), make(chan struct{}) - go func() { - info.rootDirs = determineRootDirs(cfg) - close(rootDirsReady) - }() - go func() { - info.env = determineEnv(cfg) - close(envReady) - }() - getGoInfo := func() *goInfo { - <-rootDirsReady - <-envReady - return &info - } - - // Ensure that we don't leak goroutines: Load is synchronous, so callers will - // not expect it to access the fields of cfg after the call returns. - defer getGoInfo() - - // always pass getGoInfo to golistDriver - golistDriver := func(cfg *Config, patterns ...string) (*driverResponse, error) { - return golistDriver(cfg, getGoInfo, patterns...) + state := &golistState{ + cfg: cfg, + ctx: ctx, + vendorDirs: map[string]bool{}, } // Determine files requested in contains patterns var containFiles []string - var packagesNamed []string restPatterns := make([]string, 0, len(patterns)) // Extract file= and other [querytype]= patterns. Report an error if querytype // doesn't exist. @@ -155,8 +172,6 @@ extractQueries: containFiles = append(containFiles, value) case "pattern": restPatterns = append(restPatterns, value) - case "iamashamedtousethedisabledqueryname": - packagesNamed = append(packagesNamed, value) case "": // not a reserved query restPatterns = append(restPatterns, pattern) default: @@ -172,52 +187,34 @@ extractQueries: } } - response := &responseDeduper{} - var err error - // See if we have any patterns to pass through to go list. Zero initial // patterns also requires a go list call, since it's the equivalent of // ".". if len(restPatterns) > 0 || len(patterns) == 0 { - dr, err := golistDriver(cfg, restPatterns...) + dr, err := state.createDriverResponse(restPatterns...) if err != nil { return nil, err } - response.init(dr) - } else { - response.init(&driverResponse{}) + response.addAll(dr) } - sizeswg.Wait() - if sizeserr != nil { - return nil, sizeserr - } - // types.SizesFor always returns nil or a *types.StdSizes - response.dr.Sizes, _ = sizes.(*types.StdSizes) - - var containsCandidates []string - if len(containFiles) != 0 { - if err := runContainsQueries(cfg, golistDriver, response, containFiles, getGoInfo); err != nil { + if err := state.runContainsQueries(response, containFiles); err != nil { return nil, err } } - if len(packagesNamed) != 0 { - if err := runNamedQueries(cfg, golistDriver, response, packagesNamed); err != nil { - return nil, err - } - } - - modifiedPkgs, needPkgs, err := processGolistOverlay(cfg, response, getGoInfo) + modifiedPkgs, needPkgs, err := state.processGolistOverlay(response) if err != nil { return nil, err } + + var containsCandidates []string if len(containFiles) > 0 { containsCandidates = append(containsCandidates, modifiedPkgs...) containsCandidates = append(containsCandidates, needPkgs...) } - if err := addNeededOverlayPackages(cfg, golistDriver, response, needPkgs, getGoInfo); err != nil { + if err := state.addNeededOverlayPackages(response, needPkgs); err != nil { return nil, err } // Check candidate packages for containFiles. @@ -246,28 +243,32 @@ extractQueries: } } + sizeswg.Wait() + if sizeserr != nil { + return nil, sizeserr + } return response.dr, nil } -func addNeededOverlayPackages(cfg *Config, driver driver, response *responseDeduper, pkgs []string, getGoInfo func() *goInfo) error { +func (state *golistState) addNeededOverlayPackages(response *responseDeduper, pkgs []string) error { if len(pkgs) == 0 { return nil } - dr, err := driver(cfg, pkgs...) + dr, err := state.createDriverResponse(pkgs...) if err != nil { return err } for _, pkg := range dr.Packages { response.addPackage(pkg) } - _, needPkgs, err := processGolistOverlay(cfg, response, getGoInfo) + _, needPkgs, err := state.processGolistOverlay(response) if err != nil { return err } - return addNeededOverlayPackages(cfg, driver, response, needPkgs, getGoInfo) + return state.addNeededOverlayPackages(response, needPkgs) } -func runContainsQueries(cfg *Config, driver driver, response *responseDeduper, queries []string, goInfo func() *goInfo) error { +func (state *golistState) runContainsQueries(response *responseDeduper, queries []string) error { for _, query := range queries { // TODO(matloob): Do only one query per directory. fdir := filepath.Dir(query) @@ -277,44 +278,17 @@ func runContainsQueries(cfg *Config, driver driver, response *responseDeduper, q if err != nil { return fmt.Errorf("could not determine absolute path of file= query path %q: %v", query, err) } - dirResponse, err := driver(cfg, pattern) - if err != nil || (len(dirResponse.Packages) == 1 && len(dirResponse.Packages[0].Errors) == 1) { - // There was an error loading the package. Try to load the file as an ad-hoc package. - // Usually the error will appear in a returned package, but may not if we're in modules mode - // and the ad-hoc is located outside a module. + dirResponse, err := state.createDriverResponse(pattern) + + // If there was an error loading the package, or the package is returned + // with errors, try to load the file as an ad-hoc package. + // Usually the error will appear in a returned package, but may not if we're + // in module mode and the ad-hoc is located outside a module. + if err != nil || len(dirResponse.Packages) == 1 && len(dirResponse.Packages[0].GoFiles) == 0 && + len(dirResponse.Packages[0].Errors) == 1 { var queryErr error - dirResponse, queryErr = driver(cfg, query) - if queryErr != nil { - // Return the original error if the attempt to fall back failed. - return err - } - // If we get nothing back from `go list`, try to make this file into its own ad-hoc package. - if len(dirResponse.Packages) == 0 && queryErr == nil { - dirResponse.Packages = append(dirResponse.Packages, &Package{ - ID: "command-line-arguments", - PkgPath: query, - GoFiles: []string{query}, - CompiledGoFiles: []string{query}, - Imports: make(map[string]*Package), - }) - dirResponse.Roots = append(dirResponse.Roots, "command-line-arguments") - } - // Special case to handle issue #33482: - // If this is a file= query for ad-hoc packages where the file only exists on an overlay, - // and exists outside of a module, add the file in for the package. - if len(dirResponse.Packages) == 1 && (dirResponse.Packages[0].ID == "command-line-arguments" || - filepath.ToSlash(dirResponse.Packages[0].PkgPath) == filepath.ToSlash(query)) { - if len(dirResponse.Packages[0].GoFiles) == 0 { - filename := filepath.Join(pattern, filepath.Base(query)) // avoid recomputing abspath - // TODO(matloob): check if the file is outside of a root dir? - for path := range cfg.Overlay { - if path == filename { - dirResponse.Packages[0].Errors = nil - dirResponse.Packages[0].GoFiles = []string{path} - dirResponse.Packages[0].CompiledGoFiles = []string{path} - } - } - } + if dirResponse, queryErr = state.adhocPackage(pattern, query); queryErr != nil { + return err // return the original error } } isRoot := make(map[string]bool, len(dirResponse.Roots)) @@ -342,276 +316,47 @@ func runContainsQueries(cfg *Config, driver driver, response *responseDeduper, q return nil } -// modCacheRegexp splits a path in a module cache into module, module version, and package. -var modCacheRegexp = regexp.MustCompile(`(.*)@([^/\\]*)(.*)`) - -func runNamedQueries(cfg *Config, driver driver, response *responseDeduper, queries []string) error { - // calling `go env` isn't free; bail out if there's nothing to do. - if len(queries) == 0 { - return nil - } - // Determine which directories are relevant to scan. - roots, modRoot, err := roots(cfg) +// adhocPackage attempts to load or construct an ad-hoc package for a given +// query, if the original call to the driver produced inadequate results. +func (state *golistState) adhocPackage(pattern, query string) (*driverResponse, error) { + response, err := state.createDriverResponse(query) if err != nil { - return err + return nil, err } - - // Scan the selected directories. Simple matches, from GOPATH/GOROOT - // or the local module, can simply be "go list"ed. Matches from the - // module cache need special treatment. - var matchesMu sync.Mutex - var simpleMatches, modCacheMatches []string - add := func(root gopathwalk.Root, dir string) { - // Walk calls this concurrently; protect the result slices. - matchesMu.Lock() - defer matchesMu.Unlock() - - path := dir - if dir != root.Path { - path = dir[len(root.Path)+1:] - } - if pathMatchesQueries(path, queries) { - switch root.Type { - case gopathwalk.RootModuleCache: - modCacheMatches = append(modCacheMatches, path) - case gopathwalk.RootCurrentModule: - // We'd need to read go.mod to find the full - // import path. Relative's easier. - rel, err := filepath.Rel(cfg.Dir, dir) - if err != nil { - // This ought to be impossible, since - // we found dir in the current module. - panic(err) - } - simpleMatches = append(simpleMatches, "./"+rel) - case gopathwalk.RootGOPATH, gopathwalk.RootGOROOT: - simpleMatches = append(simpleMatches, path) - } - } - } - - startWalk := time.Now() - gopathwalk.Walk(roots, add, gopathwalk.Options{ModulesEnabled: modRoot != "", Debug: debug}) - cfg.Logf("%v for walk", time.Since(startWalk)) - - // Weird special case: the top-level package in a module will be in - // whatever directory the user checked the repository out into. It's - // more reasonable for that to not match the package name. So, if there - // are any Go files in the mod root, query it just to be safe. - if modRoot != "" { - rel, err := filepath.Rel(cfg.Dir, modRoot) - if err != nil { - panic(err) // See above. - } - - files, err := ioutil.ReadDir(modRoot) - if err != nil { - panic(err) // See above. - } - - for _, f := range files { - if strings.HasSuffix(f.Name(), ".go") { - simpleMatches = append(simpleMatches, rel) - break - } - } - } - - addResponse := func(r *driverResponse) { - for _, pkg := range r.Packages { - response.addPackage(pkg) - for _, name := range queries { - if pkg.Name == name { - response.addRoot(pkg.ID) - break - } - } - } - } - - if len(simpleMatches) != 0 { - resp, err := driver(cfg, simpleMatches...) - if err != nil { - return err - } - addResponse(resp) - } - - // Module cache matches are tricky. We want to avoid downloading new - // versions of things, so we need to use the ones present in the cache. - // go list doesn't accept version specifiers, so we have to write out a - // temporary module, and do the list in that module. - if len(modCacheMatches) != 0 { - // Collect all the matches, deduplicating by major version - // and preferring the newest. - type modInfo struct { - mod string - major string - } - mods := make(map[modInfo]string) - var imports []string - for _, modPath := range modCacheMatches { - matches := modCacheRegexp.FindStringSubmatch(modPath) - mod, ver := filepath.ToSlash(matches[1]), matches[2] - importPath := filepath.ToSlash(filepath.Join(matches[1], matches[3])) - - major := semver.Major(ver) - if prevVer, ok := mods[modInfo{mod, major}]; !ok || semver.Compare(ver, prevVer) > 0 { - mods[modInfo{mod, major}] = ver - } - - imports = append(imports, importPath) - } - - // Build the temporary module. - var gomod bytes.Buffer - gomod.WriteString("module modquery\nrequire (\n") - for mod, version := range mods { - gomod.WriteString("\t" + mod.mod + " " + version + "\n") - } - gomod.WriteString(")\n") - - tmpCfg := *cfg - - // We're only trying to look at stuff in the module cache, so - // disable the network. This should speed things up, and has - // prevented errors in at least one case, #28518. - tmpCfg.Env = append([]string{"GOPROXY=off"}, cfg.Env...) - - var err error - tmpCfg.Dir, err = ioutil.TempDir("", "gopackages-modquery") - if err != nil { - return err - } - defer os.RemoveAll(tmpCfg.Dir) - - if err := ioutil.WriteFile(filepath.Join(tmpCfg.Dir, "go.mod"), gomod.Bytes(), 0777); err != nil { - return fmt.Errorf("writing go.mod for module cache query: %v", err) - } - - // Run the query, using the import paths calculated from the matches above. - resp, err := driver(&tmpCfg, imports...) - if err != nil { - return fmt.Errorf("querying module cache matches: %v", err) - } - addResponse(resp) - } - - return nil -} - -func getSizes(cfg *Config) (types.Sizes, error) { - return packagesdriver.GetSizesGolist(cfg.Context, cfg.BuildFlags, cfg.Env, cfg.Dir, usesExportData(cfg)) -} - -// roots selects the appropriate paths to walk based on the passed-in configuration, -// particularly the environment and the presence of a go.mod in cfg.Dir's parents. -func roots(cfg *Config) ([]gopathwalk.Root, string, error) { - stdout, err := invokeGo(cfg, "env", "GOROOT", "GOPATH", "GOMOD") - if err != nil { - return nil, "", err - } - - fields := strings.Split(stdout.String(), "\n") - if len(fields) != 4 || len(fields[3]) != 0 { - return nil, "", fmt.Errorf("go env returned unexpected output: %q", stdout.String()) - } - goroot, gopath, gomod := fields[0], filepath.SplitList(fields[1]), fields[2] - var modDir string - if gomod != "" { - modDir = filepath.Dir(gomod) - } - - var roots []gopathwalk.Root - // Always add GOROOT. - roots = append(roots, gopathwalk.Root{ - Path: filepath.Join(goroot, "/src"), - Type: gopathwalk.RootGOROOT, - }) - // If modules are enabled, scan the module dir. - if modDir != "" { - roots = append(roots, gopathwalk.Root{ - Path: modDir, - Type: gopathwalk.RootCurrentModule, + // If we get nothing back from `go list`, + // try to make this file into its own ad-hoc package. + // TODO(rstambler): Should this check against the original response? + if len(response.Packages) == 0 { + response.Packages = append(response.Packages, &Package{ + ID: "command-line-arguments", + PkgPath: query, + GoFiles: []string{query}, + CompiledGoFiles: []string{query}, + Imports: make(map[string]*Package), }) + response.Roots = append(response.Roots, "command-line-arguments") } - // Add either GOPATH/src or GOPATH/pkg/mod, depending on module mode. - for _, p := range gopath { - if modDir != "" { - roots = append(roots, gopathwalk.Root{ - Path: filepath.Join(p, "/pkg/mod"), - Type: gopathwalk.RootModuleCache, - }) - } else { - roots = append(roots, gopathwalk.Root{ - Path: filepath.Join(p, "/src"), - Type: gopathwalk.RootGOPATH, - }) - } - } - - return roots, modDir, nil -} - -// These functions were copied from goimports. See further documentation there. - -// pathMatchesQueries is adapted from pkgIsCandidate. -// TODO: is it reasonable to do Contains here, rather than an exact match on a path component? -func pathMatchesQueries(path string, queries []string) bool { - lastTwo := lastTwoComponents(path) - for _, query := range queries { - if strings.Contains(lastTwo, query) { - return true - } - if hasHyphenOrUpperASCII(lastTwo) && !hasHyphenOrUpperASCII(query) { - lastTwo = lowerASCIIAndRemoveHyphen(lastTwo) - if strings.Contains(lastTwo, query) { - return true + // Handle special cases. + if len(response.Packages) == 1 { + // golang/go#33482: If this is a file= query for ad-hoc packages where + // the file only exists on an overlay, and exists outside of a module, + // add the file to the package and remove the errors. + if response.Packages[0].ID == "command-line-arguments" || + filepath.ToSlash(response.Packages[0].PkgPath) == filepath.ToSlash(query) { + if len(response.Packages[0].GoFiles) == 0 { + filename := filepath.Join(pattern, filepath.Base(query)) // avoid recomputing abspath + // TODO(matloob): check if the file is outside of a root dir? + for path := range state.cfg.Overlay { + if path == filename { + response.Packages[0].Errors = nil + response.Packages[0].GoFiles = []string{path} + response.Packages[0].CompiledGoFiles = []string{path} + } + } } } } - return false -} - -// lastTwoComponents returns at most the last two path components -// of v, using either / or \ as the path separator. -func lastTwoComponents(v string) string { - nslash := 0 - for i := len(v) - 1; i >= 0; i-- { - if v[i] == '/' || v[i] == '\\' { - nslash++ - if nslash == 2 { - return v[i:] - } - } - } - return v -} - -func hasHyphenOrUpperASCII(s string) bool { - for i := 0; i < len(s); i++ { - b := s[i] - if b == '-' || ('A' <= b && b <= 'Z') { - return true - } - } - return false -} - -func lowerASCIIAndRemoveHyphen(s string) (ret string) { - buf := make([]byte, 0, len(s)) - for i := 0; i < len(s); i++ { - b := s[i] - switch { - case b == '-': - continue - case 'A' <= b && b <= 'Z': - buf = append(buf, b+('a'-'A')) - default: - buf = append(buf, b) - } - } - return string(buf) + return response, nil } // Fields must match go list; @@ -636,6 +381,7 @@ type jsonPackage struct { Imports []string ImportMap map[string]string Deps []string + Module *packagesinternal.Module TestGoFiles []string TestImports []string XTestGoFiles []string @@ -656,10 +402,9 @@ func otherFiles(p *jsonPackage) [][]string { return [][]string{p.CFiles, p.CXXFiles, p.MFiles, p.HFiles, p.FFiles, p.SFiles, p.SwigFiles, p.SwigCXXFiles, p.SysoFiles} } -// golistDriver uses the "go list" command to expand the pattern -// words and return metadata for the specified packages. dir may be -// "" and env may be nil, as per os/exec.Command. -func golistDriver(cfg *Config, rootsDirs func() *goInfo, words ...string) (*driverResponse, error) { +// createDriverResponse uses the "go list" command to expand the pattern +// words and return a response for the specified packages. +func (state *golistState) createDriverResponse(words ...string) (*driverResponse, error) { // go list uses the following identifiers in ImportPath and Imports: // // "p" -- importable package or main (command) @@ -673,11 +418,13 @@ func golistDriver(cfg *Config, rootsDirs func() *goInfo, words ...string) (*driv // Run "go list" for complete // information on the specified packages. - buf, err := invokeGo(cfg, "list", golistargs(cfg, words)...) + buf, err := state.invokeGo("list", golistargs(state.cfg, words)...) if err != nil { return nil, err } seen := make(map[string]*jsonPackage) + pkgs := make(map[string]*Package) + additionalErrors := make(map[string][]Error) // Decode the JSON and convert it to Package form. var response driverResponse for dec := json.NewDecoder(buf); dec.More(); { @@ -708,18 +455,72 @@ func golistDriver(cfg *Config, rootsDirs func() *goInfo, words ...string) (*driv // contained in a known module or GOPATH entry. This will allow the package to be // properly "reclaimed" when overlays are processed. if filepath.IsAbs(p.ImportPath) && p.Error != nil { - pkgPath, ok := getPkgPath(cfg, p.ImportPath, rootsDirs) + pkgPath, ok, err := state.getPkgPath(p.ImportPath) + if err != nil { + return nil, err + } if ok { p.ImportPath = pkgPath } } if old, found := seen[p.ImportPath]; found { - if !reflect.DeepEqual(p, old) { - return nil, fmt.Errorf("internal error: go list gives conflicting information for package %v", p.ImportPath) + // If one version of the package has an error, and the other doesn't, assume + // that this is a case where go list is reporting a fake dependency variant + // of the imported package: When a package tries to invalidly import another + // package, go list emits a variant of the imported package (with the same + // import path, but with an error on it, and the package will have a + // DepError set on it). An example of when this can happen is for imports of + // main packages: main packages can not be imported, but they may be + // separately matched and listed by another pattern. + // See golang.org/issue/36188 for more details. + + // The plan is that eventually, hopefully in Go 1.15, the error will be + // reported on the importing package rather than the duplicate "fake" + // version of the imported package. Once all supported versions of Go + // have the new behavior this logic can be deleted. + // TODO(matloob): delete the workaround logic once all supported versions of + // Go return the errors on the proper package. + + // There should be exactly one version of a package that doesn't have an + // error. + if old.Error == nil && p.Error == nil { + if !reflect.DeepEqual(p, old) { + return nil, fmt.Errorf("internal error: go list gives conflicting information for package %v", p.ImportPath) + } + continue } - // skip the duplicate - continue + + // Determine if this package's error needs to be bubbled up. + // This is a hack, and we expect for go list to eventually set the error + // on the package. + if old.Error != nil { + var errkind string + if strings.Contains(old.Error.Err, "not an importable package") { + errkind = "not an importable package" + } else if strings.Contains(old.Error.Err, "use of internal package") && strings.Contains(old.Error.Err, "not allowed") { + errkind = "use of internal package not allowed" + } + if errkind != "" { + if len(old.Error.ImportStack) < 2 { + return nil, fmt.Errorf(`internal error: go list gave a %q error with an import stack with fewer than two elements`, errkind) + } + importingPkg := old.Error.ImportStack[len(old.Error.ImportStack)-2] + additionalErrors[importingPkg] = append(additionalErrors[importingPkg], Error{ + Pos: old.Error.Pos, + Msg: old.Error.Err, + Kind: ListError, + }) + } + } + + // Make sure that if there's a version of the package without an error, + // that's the one reported to the user. + if old.Error == nil { + continue + } + + // This package will replace the old one at the end of the loop. } seen[p.ImportPath] = p @@ -729,6 +530,8 @@ func golistDriver(cfg *Config, rootsDirs func() *goInfo, words ...string) (*driv GoFiles: absJoin(p.Dir, p.GoFiles, p.CgoFiles), CompiledGoFiles: absJoin(p.Dir, p.CompiledGoFiles), OtherFiles: absJoin(p.Dir, otherFiles(p)...), + forTest: p.ForTest, + module: p.Module, } // Work around https://golang.org/issue/28749: @@ -817,29 +620,37 @@ func golistDriver(cfg *Config, rootsDirs func() *goInfo, words ...string) (*driv }) } + pkgs[pkg.ID] = pkg + } + + for id, errs := range additionalErrors { + if p, ok := pkgs[id]; ok { + p.Errors = append(p.Errors, errs...) + } + } + for _, pkg := range pkgs { response.Packages = append(response.Packages, pkg) } + sort.Slice(response.Packages, func(i, j int) bool { return response.Packages[i].ID < response.Packages[j].ID }) return &response, nil } // getPkgPath finds the package path of a directory if it's relative to a root directory. -func getPkgPath(cfg *Config, dir string, goInfo func() *goInfo) (string, bool) { +func (state *golistState) getPkgPath(dir string) (string, bool, error) { absDir, err := filepath.Abs(dir) if err != nil { - cfg.Logf("error getting absolute path of %s: %v", dir, err) - return "", false + return "", false, err } - for rdir, rpath := range goInfo().rootDirs { - absRdir, err := filepath.Abs(rdir) - if err != nil { - cfg.Logf("error getting absolute path of %s: %v", rdir, err) - continue - } + roots, err := state.determineRootDirs() + if err != nil { + return "", false, err + } + + for rdir, rpath := range roots { // Make sure that the directory is in the module, // to avoid creating a path relative to another module. - if !strings.HasPrefix(absDir, absRdir) { - cfg.Logf("%s does not have prefix %s", absDir, absRdir) + if !strings.HasPrefix(absDir, rdir) { continue } // TODO(matloob): This doesn't properly handle symlinks. @@ -854,11 +665,11 @@ func getPkgPath(cfg *Config, dir string, goInfo func() *goInfo) (string, bool) { // Once the file is saved, gopls, or the next invocation of the tool will get the correct // result straight from golist. // TODO(matloob): Implement module tiebreaking? - return path.Join(rpath, filepath.ToSlash(r)), true + return path.Join(rpath, filepath.ToSlash(r)), true, nil } - return filepath.ToSlash(r), true + return filepath.ToSlash(r), true, nil } - return "", false + return "", false, nil } // absJoin absolutizes and flattens the lists of files. @@ -878,7 +689,7 @@ func golistargs(cfg *Config, words []string) []string { const findFlags = NeedImports | NeedTypes | NeedSyntax | NeedTypesInfo fullargs := []string{ "-e", "-json", - fmt.Sprintf("-compiled=%t", cfg.Mode&(NeedCompiledGoFiles|NeedSyntax|NeedTypesInfo|NeedTypesSizes) != 0), + fmt.Sprintf("-compiled=%t", cfg.Mode&(NeedCompiledGoFiles|NeedSyntax|NeedTypes|NeedTypesInfo|NeedTypesSizes) != 0), fmt.Sprintf("-test=%t", cfg.Tests), fmt.Sprintf("-export=%t", usesExportData(cfg)), fmt.Sprintf("-deps=%t", cfg.Mode&NeedImports != 0), @@ -893,28 +704,20 @@ func golistargs(cfg *Config, words []string) []string { } // invokeGo returns the stdout of a go command invocation. -func invokeGo(cfg *Config, verb string, args ...string) (*bytes.Buffer, error) { - stdout := new(bytes.Buffer) - stderr := new(bytes.Buffer) - goArgs := []string{verb} - goArgs = append(goArgs, cfg.BuildFlags...) - goArgs = append(goArgs, args...) - cmd := exec.CommandContext(cfg.Context, "go", goArgs...) - // On darwin the cwd gets resolved to the real path, which breaks anything that - // expects the working directory to keep the original path, including the - // go command when dealing with modules. - // The Go stdlib has a special feature where if the cwd and the PWD are the - // same node then it trusts the PWD, so by setting it in the env for the child - // process we fix up all the paths returned by the go command. - cmd.Env = append(append([]string{}, cfg.Env...), "PWD="+cfg.Dir) - cmd.Dir = cfg.Dir - cmd.Stdout = stdout - cmd.Stderr = stderr - defer func(start time.Time) { - cfg.Logf("%s for %v, stderr: <<%s>> stdout: <<%s>>\n", time.Since(start), cmdDebugStr(cmd, args...), stderr, stdout) - }(time.Now()) +func (state *golistState) invokeGo(verb string, args ...string) (*bytes.Buffer, error) { + cfg := state.cfg - if err := cmd.Run(); err != nil { + inv := &gocommand.Invocation{ + Verb: verb, + Args: args, + BuildFlags: cfg.BuildFlags, + Env: cfg.Env, + Logf: cfg.Logf, + WorkingDir: cfg.Dir, + } + + stdout, stderr, _, err := inv.RunRaw(cfg.Context) + if err != nil { // Check for 'go' executable not being found. if ee, ok := err.(*exec.Error); ok && ee.Err == exec.ErrNotFound { return nil, fmt.Errorf("'go list' driver requires 'go', but %s", exec.ErrNotFound) @@ -924,7 +727,7 @@ func invokeGo(cfg *Config, verb string, args ...string) (*bytes.Buffer, error) { if !ok { // Catastrophic error: // - context cancellation - return nil, fmt.Errorf("couldn't exec 'go %v': %s %T", args, err, err) + return nil, fmt.Errorf("couldn't run 'go': %v", err) } // Old go version? @@ -951,7 +754,12 @@ func invokeGo(cfg *Config, verb string, args ...string) (*bytes.Buffer, error) { !strings.ContainsRune("!\"#$%&'()*,:;<=>?[\\]^`{|}\uFFFD", r) } if len(stderr.String()) > 0 && strings.HasPrefix(stderr.String(), "# ") { - if strings.HasPrefix(strings.TrimLeftFunc(stderr.String()[len("# "):], isPkgPathRune), "\n") { + msg := stderr.String()[len("# "):] + if strings.HasPrefix(strings.TrimLeftFunc(msg, isPkgPathRune), "\n") { + return stdout, nil + } + // Treat pkg-config errors as a special case (golang.org/issue/36770). + if strings.HasPrefix(msg, "pkg-config") { return stdout, nil } } @@ -1040,16 +848,6 @@ func invokeGo(cfg *Config, verb string, args ...string) (*bytes.Buffer, error) { return nil, fmt.Errorf("go %v: %s: %s", args, exitErr, stderr) } } - - // As of writing, go list -export prints some non-fatal compilation - // errors to stderr, even with -e set. We would prefer that it put - // them in the Package.Error JSON (see https://golang.org/issue/26319). - // In the meantime, there's nowhere good to put them, but they can - // be useful for debugging. Print them if $GOPACKAGESPRINTGOLISTERRORS - // is set. - if len(stderr.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTGOLISTERRORS") != "" { - fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(cmd, args...), stderr) - } return stdout, nil } diff --git a/vendor/golang.org/x/tools/go/packages/golist_overlay.go b/vendor/golang.org/x/tools/go/packages/golist_overlay.go index a7de62299d6..7974a6c9bb6 100644 --- a/vendor/golang.org/x/tools/go/packages/golist_overlay.go +++ b/vendor/golang.org/x/tools/go/packages/golist_overlay.go @@ -1,12 +1,13 @@ package packages import ( - "bytes" "encoding/json" "fmt" "go/parser" "go/token" + "os" "path/filepath" + "sort" "strconv" "strings" ) @@ -16,7 +17,7 @@ import ( // sometimes incorrect. // TODO(matloob): Handle unsupported cases, including the following: // - determining the correct package to add given a new import path -func processGolistOverlay(cfg *Config, response *responseDeduper, rootDirs func() *goInfo) (modifiedPkgs, needPkgs []string, err error) { +func (state *golistState) processGolistOverlay(response *responseDeduper) (modifiedPkgs, needPkgs []string, err error) { havePkgs := make(map[string]string) // importPath -> non-test package ID needPkgsSet := make(map[string]bool) modifiedPkgsSet := make(map[string]bool) @@ -34,7 +35,23 @@ func processGolistOverlay(cfg *Config, response *responseDeduper, rootDirs func( // potentially modifying the transitive set of dependencies). var overlayAddsImports bool - for opath, contents := range cfg.Overlay { + // If both a package and its test package are created by the overlay, we + // need the real package first. Process all non-test files before test + // files, and make the whole process deterministic while we're at it. + var overlayFiles []string + for opath := range state.cfg.Overlay { + overlayFiles = append(overlayFiles, opath) + } + sort.Slice(overlayFiles, func(i, j int) bool { + iTest := strings.HasSuffix(overlayFiles[i], "_test.go") + jTest := strings.HasSuffix(overlayFiles[j], "_test.go") + if iTest != jTest { + return !iTest // non-tests are before tests. + } + return overlayFiles[i] < overlayFiles[j] + }) + for _, opath := range overlayFiles { + contents := state.cfg.Overlay[opath] base := filepath.Base(opath) dir := filepath.Dir(opath) var pkg *Package // if opath belongs to both a package and its test variant, this will be the test variant @@ -64,14 +81,8 @@ func processGolistOverlay(cfg *Config, response *responseDeduper, rootDirs func( testVariantOf = p continue nextPackage } + // We must have already seen the package of which this is a test variant. if pkg != nil && p != pkg && pkg.PkgPath == p.PkgPath { - // If we've already seen the test variant, - // make sure to label which package it is a test variant of. - if hasTestFiles(pkg) { - testVariantOf = p - continue nextPackage - } - // If we have already seen the package of which this is a test variant. if hasTestFiles(p) { testVariantOf = pkg } @@ -86,7 +97,10 @@ func processGolistOverlay(cfg *Config, response *responseDeduper, rootDirs func( if pkg == nil { // Try to find the module or gopath dir the file is contained in. // Then for modules, add the module opath to the beginning. - pkgPath, ok := getPkgPath(cfg, dir, rootDirs) + pkgPath, ok, err := state.getPkgPath(dir) + if err != nil { + return nil, nil, err + } if !ok { break } @@ -114,6 +128,11 @@ func processGolistOverlay(cfg *Config, response *responseDeduper, rootDirs func( if isTestFile && !isXTest && testVariantOf != nil { pkg.GoFiles = append(pkg.GoFiles, testVariantOf.GoFiles...) pkg.CompiledGoFiles = append(pkg.CompiledGoFiles, testVariantOf.CompiledGoFiles...) + // Add the package under test and its imports to the test variant. + pkg.forTest = testVariantOf.PkgPath + for k, v := range testVariantOf.Imports { + pkg.Imports[k] = &Package{ID: v.ID} + } } } } @@ -130,42 +149,45 @@ func processGolistOverlay(cfg *Config, response *responseDeduper, rootDirs func( continue } for _, imp := range imports { - _, found := pkg.Imports[imp] - if !found { - overlayAddsImports = true - // TODO(matloob): Handle cases when the following block isn't correct. - // These include imports of vendored packages, etc. - id, ok := havePkgs[imp] - if !ok { - id = imp - } - pkg.Imports[imp] = &Package{ID: id} - // Add dependencies to the non-test variant version of this package as wel. - if testVariantOf != nil { - testVariantOf.Imports[imp] = &Package{ID: id} + if _, found := pkg.Imports[imp]; found { + continue + } + overlayAddsImports = true + id, ok := havePkgs[imp] + if !ok { + var err error + id, err = state.resolveImport(dir, imp) + if err != nil { + return nil, nil, err } } + pkg.Imports[imp] = &Package{ID: id} + // Add dependencies to the non-test variant version of this package as well. + if testVariantOf != nil { + testVariantOf.Imports[imp] = &Package{ID: id} + } } - continue } - // toPkgPath tries to guess the package path given the id. - // This isn't always correct -- it's certainly wrong for - // vendored packages' paths. - toPkgPath := func(id string) string { - // TODO(matloob): Handle vendor paths. - i := strings.IndexByte(id, ' ') - if i >= 0 { - return id[:i] + // toPkgPath guesses the package path given the id. + toPkgPath := func(sourceDir, id string) (string, error) { + if i := strings.IndexByte(id, ' '); i >= 0 { + return state.resolveImport(sourceDir, id[:i]) } - return id + return state.resolveImport(sourceDir, id) } - // Do another pass now that new packages have been created to determine the - // set of missing packages. + // Now that new packages have been created, do another pass to determine + // the new set of missing packages. for _, pkg := range response.dr.Packages { for _, imp := range pkg.Imports { - pkgPath := toPkgPath(imp.ID) + if len(pkg.GoFiles) == 0 { + return nil, nil, fmt.Errorf("cannot resolve imports for package %q with no Go files", pkg.PkgPath) + } + pkgPath, err := toPkgPath(filepath.Dir(pkg.GoFiles[0]), imp.ID) + if err != nil { + return nil, nil, err + } if _, ok := havePkgs[pkgPath]; !ok { needPkgsSet[pkgPath] = true } @@ -185,6 +207,52 @@ func processGolistOverlay(cfg *Config, response *responseDeduper, rootDirs func( return modifiedPkgs, needPkgs, err } +// resolveImport finds the the ID of a package given its import path. +// In particular, it will find the right vendored copy when in GOPATH mode. +func (state *golistState) resolveImport(sourceDir, importPath string) (string, error) { + env, err := state.getEnv() + if err != nil { + return "", err + } + if env["GOMOD"] != "" { + return importPath, nil + } + + searchDir := sourceDir + for { + vendorDir := filepath.Join(searchDir, "vendor") + exists, ok := state.vendorDirs[vendorDir] + if !ok { + info, err := os.Stat(vendorDir) + exists = err == nil && info.IsDir() + state.vendorDirs[vendorDir] = exists + } + + if exists { + vendoredPath := filepath.Join(vendorDir, importPath) + if info, err := os.Stat(vendoredPath); err == nil && info.IsDir() { + // We should probably check for .go files here, but shame on anyone who fools us. + path, ok, err := state.getPkgPath(vendoredPath) + if err != nil { + return "", err + } + if ok { + return path, nil + } + } + } + + // We know we've hit the top of the filesystem when we Dir / and get /, + // or C:\ and get C:\, etc. + next := filepath.Dir(searchDir) + if next == searchDir { + break + } + searchDir = next + } + return importPath, nil +} + func hasTestFiles(p *Package) bool { for _, f := range p.GoFiles { if strings.HasSuffix(f, "_test.go") { @@ -194,44 +262,59 @@ func hasTestFiles(p *Package) bool { return false } -// determineRootDirs returns a mapping from directories code can be contained in to the -// corresponding import path prefixes of those directories. -// Its result is used to try to determine the import path for a package containing -// an overlay file. -func determineRootDirs(cfg *Config) map[string]string { - // Assume modules first: - out, err := invokeGo(cfg, "list", "-m", "-json", "all") +// determineRootDirs returns a mapping from absolute directories that could +// contain code to their corresponding import path prefixes. +func (state *golistState) determineRootDirs() (map[string]string, error) { + env, err := state.getEnv() if err != nil { - return determineRootDirsGOPATH(cfg) + return nil, err + } + if env["GOMOD"] != "" { + state.rootsOnce.Do(func() { + state.rootDirs, state.rootDirsError = state.determineRootDirsModules() + }) + } else { + state.rootsOnce.Do(func() { + state.rootDirs, state.rootDirsError = state.determineRootDirsGOPATH() + }) + } + return state.rootDirs, state.rootDirsError +} + +func (state *golistState) determineRootDirsModules() (map[string]string, error) { + out, err := state.invokeGo("list", "-m", "-json", "all") + if err != nil { + return nil, err } m := map[string]string{} type jsonMod struct{ Path, Dir string } for dec := json.NewDecoder(out); dec.More(); { mod := new(jsonMod) if err := dec.Decode(mod); err != nil { - return m // Give up and return an empty map. Package won't be found for overlay. + return nil, err } if mod.Dir != "" && mod.Path != "" { // This is a valid module; add it to the map. - m[mod.Dir] = mod.Path + absDir, err := filepath.Abs(mod.Dir) + if err != nil { + return nil, err + } + m[absDir] = mod.Path } } - return m + return m, nil } -func determineRootDirsGOPATH(cfg *Config) map[string]string { +func (state *golistState) determineRootDirsGOPATH() (map[string]string, error) { m := map[string]string{} - out, err := invokeGo(cfg, "env", "GOPATH") - if err != nil { - // Could not determine root dir mapping. Everything is best-effort, so just return an empty map. - // When we try to find the import path for a directory, there will be no root-dir match and - // we'll give up. - return m + for _, dir := range filepath.SplitList(state.mustGetEnv()["GOPATH"]) { + absDir, err := filepath.Abs(dir) + if err != nil { + return nil, err + } + m[filepath.Join(absDir, "src")] = "" } - for _, p := range filepath.SplitList(string(bytes.TrimSpace(out.Bytes()))) { - m[filepath.Join(p, "src")] = "" - } - return m + return m, nil } func extractImports(filename string, contents []byte) ([]string, error) { diff --git a/vendor/golang.org/x/tools/go/packages/packages.go b/vendor/golang.org/x/tools/go/packages/packages.go index f98a0bdcae4..1ac6558c1d3 100644 --- a/vendor/golang.org/x/tools/go/packages/packages.go +++ b/vendor/golang.org/x/tools/go/packages/packages.go @@ -23,6 +23,7 @@ import ( "sync" "golang.org/x/tools/go/gcexportdata" + "golang.org/x/tools/internal/packagesinternal" ) // A LoadMode controls the amount of detail to return when loading. @@ -34,6 +35,9 @@ import ( // Load may return more information than requested. type LoadMode int +// TODO(matloob): When a V2 of go/packages is released, rename NeedExportsFile to +// NeedExportFile to make it consistent with the Package field it's adding. + const ( // NeedName adds Name and PkgPath. NeedName LoadMode = 1 << iota @@ -51,7 +55,7 @@ const ( // NeedDeps adds the fields requested by the LoadMode in the packages in Imports. NeedDeps - // NeedExportsFile adds ExportsFile. + // NeedExportsFile adds ExportFile. NeedExportsFile // NeedTypes adds Types, Fset, and IllTyped. @@ -292,6 +296,21 @@ type Package struct { // TypesSizes provides the effective size function for types in TypesInfo. TypesSizes types.Sizes + + // forTest is the package under test, if any. + forTest string + + // module is the module information for the package if it exists. + module *packagesinternal.Module +} + +func init() { + packagesinternal.GetForTest = func(p interface{}) string { + return p.(*Package).forTest + } + packagesinternal.GetModule = func(p interface{}) *packagesinternal.Module { + return p.(*Package).module + } } // An Error describes a problem with a package's metadata, syntax, or types. @@ -500,12 +519,23 @@ func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) { if i, found := rootMap[pkg.ID]; found { rootIndex = i } + + // Overlays can invalidate export data. + // TODO(matloob): make this check fine-grained based on dependencies on overlaid files + exportDataInvalid := len(ld.Overlay) > 0 || pkg.ExportFile == "" && pkg.PkgPath != "unsafe" + // This package needs type information if the caller requested types and the package is + // either a root, or it's a non-root and the user requested dependencies ... + needtypes := (ld.Mode&NeedTypes|NeedTypesInfo != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0)) + // This package needs source if the call requested source (or types info, which implies source) + // and the package is either a root, or itas a non- root and the user requested dependencies... + needsrc := ((ld.Mode&(NeedSyntax|NeedTypesInfo) != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0)) || + // ... or if we need types and the exportData is invalid. We fall back to (incompletely) + // typechecking packages from source if they fail to compile. + (ld.Mode&NeedTypes|NeedTypesInfo != 0 && exportDataInvalid)) && pkg.PkgPath != "unsafe" lpkg := &loaderPackage{ Package: pkg, - needtypes: (ld.Mode&(NeedTypes|NeedTypesInfo) != 0 && ld.Mode&NeedDeps != 0 && rootIndex < 0) || rootIndex >= 0, - needsrc: (ld.Mode&(NeedSyntax|NeedTypesInfo) != 0 && ld.Mode&NeedDeps != 0 && rootIndex < 0) || rootIndex >= 0 || - len(ld.Overlay) > 0 || // Overlays can invalidate export data. TODO(matloob): make this check fine-grained based on dependencies on overlaid files - pkg.ExportFile == "" && pkg.PkgPath != "unsafe", + needtypes: needtypes, + needsrc: needsrc, } ld.pkgs[lpkg.ID] = lpkg if rootIndex >= 0 { diff --git a/vendor/golang.org/x/tools/go/pointer/TODO b/vendor/golang.org/x/tools/go/pointer/TODO deleted file mode 100644 index f95e70621d9..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/TODO +++ /dev/null @@ -1,33 +0,0 @@ --*- text -*- - -Pointer analysis to-do list -=========================== - -CONSTRAINT GENERATION: -- support reflection: - - a couple of operators are missing - - reflect.Values may contain lvalues (CanAddr) -- implement native intrinsics. These vary by platform. -- add to pts(a.panic) a label representing all runtime panics, e.g. - runtime.{TypeAssertionError,errorString,errorCString}. - -OPTIMISATIONS -- pre-solver: - pointer equivalence: extend HVN to HRU - location equivalence -- solver: HCD, LCD. -- experiment with map+slice worklist in lieu of bitset. - It may have faster insert. - -MISC: -- Test on all platforms. - Currently we assume these go/build tags: linux, amd64, !cgo. - -MAINTAINABILITY -- Think about ways to make debugging this code easier. PTA logs - routinely exceed a million lines and require training to read. - -BUGS: -- There's a crash bug in stdlib_test + reflection, rVCallConstraint. - - diff --git a/vendor/golang.org/x/tools/go/pointer/analysis.go b/vendor/golang.org/x/tools/go/pointer/analysis.go deleted file mode 100644 index 0abb04dd80d..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/analysis.go +++ /dev/null @@ -1,452 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -// This file defines the main datatypes and Analyze function of the pointer analysis. - -import ( - "fmt" - "go/token" - "go/types" - "io" - "os" - "reflect" - "runtime" - "runtime/debug" - "sort" - - "golang.org/x/tools/go/callgraph" - "golang.org/x/tools/go/ssa" - "golang.org/x/tools/go/types/typeutil" -) - -const ( - // optimization options; enable all when committing - optRenumber = true // enable renumbering optimization (makes logs hard to read) - optHVN = true // enable pointer equivalence via Hash-Value Numbering - - // debugging options; disable all when committing - debugHVN = false // enable assertions in HVN - debugHVNVerbose = false // enable extra HVN logging - debugHVNCrossCheck = false // run solver with/without HVN and compare (caveats below) - debugTimers = false // show running time of each phase -) - -// object.flags bitmask values. -const ( - otTagged = 1 << iota // type-tagged object - otIndirect // type-tagged object with indirect payload - otFunction // function object -) - -// An object represents a contiguous block of memory to which some -// (generalized) pointer may point. -// -// (Note: most variables called 'obj' are not *objects but nodeids -// such that a.nodes[obj].obj != nil.) -// -type object struct { - // flags is a bitset of the node type (ot*) flags defined above. - flags uint32 - - // Number of following nodes belonging to the same "object" - // allocation. Zero for all other nodes. - size uint32 - - // data describes this object; it has one of these types: - // - // ssa.Value for an object allocated by an SSA operation. - // types.Type for an rtype instance object or *rtype-tagged object. - // string for an instrinsic object, e.g. the array behind os.Args. - // nil for an object allocated by an instrinsic. - // (cgn provides the identity of the intrinsic.) - data interface{} - - // The call-graph node (=context) in which this object was allocated. - // May be nil for global objects: Global, Const, some Functions. - cgn *cgnode -} - -// nodeid denotes a node. -// It is an index within analysis.nodes. -// We use small integers, not *node pointers, for many reasons: -// - they are smaller on 64-bit systems. -// - sets of them can be represented compactly in bitvectors or BDDs. -// - order matters; a field offset can be computed by simple addition. -type nodeid uint32 - -// A node is an equivalence class of memory locations. -// Nodes may be pointers, pointed-to locations, neither, or both. -// -// Nodes that are pointed-to locations ("labels") have an enclosing -// object (see analysis.enclosingObject). -// -type node struct { - // If non-nil, this node is the start of an object - // (addressable memory location). - // The following obj.size nodes implicitly belong to the object; - // they locate their object by scanning back. - obj *object - - // The type of the field denoted by this node. Non-aggregate, - // unless this is an tagged.T node (i.e. the thing - // pointed to by an interface) in which case typ is that type. - typ types.Type - - // subelement indicates which directly embedded subelement of - // an object of aggregate type (struct, tuple, array) this is. - subelement *fieldInfo // e.g. ".a.b[*].c" - - // Solver state for the canonical node of this pointer- - // equivalence class. Each node is created with its own state - // but they become shared after HVN. - solve *solverState -} - -// An analysis instance holds the state of a single pointer analysis problem. -type analysis struct { - config *Config // the client's control/observer interface - prog *ssa.Program // the program being analyzed - log io.Writer // log stream; nil to disable - panicNode nodeid // sink for panic, source for recover - nodes []*node // indexed by nodeid - flattenMemo map[types.Type][]*fieldInfo // memoization of flatten() - trackTypes map[types.Type]bool // memoization of shouldTrack() - constraints []constraint // set of constraints - cgnodes []*cgnode // all cgnodes - genq []*cgnode // queue of functions to generate constraints for - intrinsics map[*ssa.Function]intrinsic // non-nil values are summaries for intrinsic fns - globalval map[ssa.Value]nodeid // node for each global ssa.Value - globalobj map[ssa.Value]nodeid // maps v to sole member of pts(v), if singleton - localval map[ssa.Value]nodeid // node for each local ssa.Value - localobj map[ssa.Value]nodeid // maps v to sole member of pts(v), if singleton - atFuncs map[*ssa.Function]bool // address-taken functions (for presolver) - mapValues []nodeid // values of makemap objects (indirect in HVN) - work nodeset // solver's worklist - result *Result // results of the analysis - track track // pointerlike types whose aliasing we track - deltaSpace []int // working space for iterating over PTS deltas - - // Reflection & intrinsics: - hasher typeutil.Hasher // cache of type hashes - reflectValueObj types.Object // type symbol for reflect.Value (if present) - reflectValueCall *ssa.Function // (reflect.Value).Call - reflectRtypeObj types.Object // *types.TypeName for reflect.rtype (if present) - reflectRtypePtr *types.Pointer // *reflect.rtype - reflectType *types.Named // reflect.Type - rtypes typeutil.Map // nodeid of canonical *rtype-tagged object for type T - reflectZeros typeutil.Map // nodeid of canonical T-tagged object for zero value - runtimeSetFinalizer *ssa.Function // runtime.SetFinalizer -} - -// enclosingObj returns the first node of the addressable memory -// object that encloses node id. Panic ensues if that node does not -// belong to any object. -func (a *analysis) enclosingObj(id nodeid) nodeid { - // Find previous node with obj != nil. - for i := id; i >= 0; i-- { - n := a.nodes[i] - if obj := n.obj; obj != nil { - if i+nodeid(obj.size) <= id { - break // out of bounds - } - return i - } - } - panic("node has no enclosing object") -} - -// labelFor returns the Label for node id. -// Panic ensues if that node is not addressable. -func (a *analysis) labelFor(id nodeid) *Label { - return &Label{ - obj: a.nodes[a.enclosingObj(id)].obj, - subelement: a.nodes[id].subelement, - } -} - -func (a *analysis) warnf(pos token.Pos, format string, args ...interface{}) { - msg := fmt.Sprintf(format, args...) - if a.log != nil { - fmt.Fprintf(a.log, "%s: warning: %s\n", a.prog.Fset.Position(pos), msg) - } - a.result.Warnings = append(a.result.Warnings, Warning{pos, msg}) -} - -// computeTrackBits sets a.track to the necessary 'track' bits for the pointer queries. -func (a *analysis) computeTrackBits() { - if len(a.config.extendedQueries) != 0 { - // TODO(dh): only track the types necessary for the query. - a.track = trackAll - return - } - var queryTypes []types.Type - for v := range a.config.Queries { - queryTypes = append(queryTypes, v.Type()) - } - for v := range a.config.IndirectQueries { - queryTypes = append(queryTypes, mustDeref(v.Type())) - } - for _, t := range queryTypes { - switch t.Underlying().(type) { - case *types.Chan: - a.track |= trackChan - case *types.Map: - a.track |= trackMap - case *types.Pointer: - a.track |= trackPtr - case *types.Slice: - a.track |= trackSlice - case *types.Interface: - a.track = trackAll - return - } - if rVObj := a.reflectValueObj; rVObj != nil && types.Identical(t, rVObj.Type()) { - a.track = trackAll - return - } - } -} - -// Analyze runs the pointer analysis with the scope and options -// specified by config, and returns the (synthetic) root of the callgraph. -// -// Pointer analysis of a transitively closed well-typed program should -// always succeed. An error can occur only due to an internal bug. -// -func Analyze(config *Config) (result *Result, err error) { - if config.Mains == nil { - return nil, fmt.Errorf("no main/test packages to analyze (check $GOROOT/$GOPATH)") - } - defer func() { - if p := recover(); p != nil { - err = fmt.Errorf("internal error in pointer analysis: %v (please report this bug)", p) - fmt.Fprintln(os.Stderr, "Internal panic in pointer analysis:") - debug.PrintStack() - } - }() - - a := &analysis{ - config: config, - log: config.Log, - prog: config.prog(), - globalval: make(map[ssa.Value]nodeid), - globalobj: make(map[ssa.Value]nodeid), - flattenMemo: make(map[types.Type][]*fieldInfo), - trackTypes: make(map[types.Type]bool), - atFuncs: make(map[*ssa.Function]bool), - hasher: typeutil.MakeHasher(), - intrinsics: make(map[*ssa.Function]intrinsic), - result: &Result{ - Queries: make(map[ssa.Value]Pointer), - IndirectQueries: make(map[ssa.Value]Pointer), - }, - deltaSpace: make([]int, 0, 100), - } - - if false { - a.log = os.Stderr // for debugging crashes; extremely verbose - } - - if a.log != nil { - fmt.Fprintln(a.log, "==== Starting analysis") - } - - // Pointer analysis requires a complete program for soundness. - // Check to prevent accidental misconfiguration. - for _, pkg := range a.prog.AllPackages() { - // (This only checks that the package scope is complete, - // not that func bodies exist, but it's a good signal.) - if !pkg.Pkg.Complete() { - return nil, fmt.Errorf(`pointer analysis requires a complete program yet package %q was incomplete`, pkg.Pkg.Path()) - } - } - - if reflect := a.prog.ImportedPackage("reflect"); reflect != nil { - rV := reflect.Pkg.Scope().Lookup("Value") - a.reflectValueObj = rV - a.reflectValueCall = a.prog.LookupMethod(rV.Type(), nil, "Call") - a.reflectType = reflect.Pkg.Scope().Lookup("Type").Type().(*types.Named) - a.reflectRtypeObj = reflect.Pkg.Scope().Lookup("rtype") - a.reflectRtypePtr = types.NewPointer(a.reflectRtypeObj.Type()) - - // Override flattening of reflect.Value, treating it like a basic type. - tReflectValue := a.reflectValueObj.Type() - a.flattenMemo[tReflectValue] = []*fieldInfo{{typ: tReflectValue}} - - // Override shouldTrack of reflect.Value and *reflect.rtype. - // Always track pointers of these types. - a.trackTypes[tReflectValue] = true - a.trackTypes[a.reflectRtypePtr] = true - - a.rtypes.SetHasher(a.hasher) - a.reflectZeros.SetHasher(a.hasher) - } - if runtime := a.prog.ImportedPackage("runtime"); runtime != nil { - a.runtimeSetFinalizer = runtime.Func("SetFinalizer") - } - a.computeTrackBits() - - a.generate() - a.showCounts() - - if optRenumber { - a.renumber() - } - - N := len(a.nodes) // excludes solver-created nodes - - if optHVN { - if debugHVNCrossCheck { - // Cross-check: run the solver once without - // optimization, once with, and compare the - // solutions. - savedConstraints := a.constraints - - a.solve() - a.dumpSolution("A.pts", N) - - // Restore. - a.constraints = savedConstraints - for _, n := range a.nodes { - n.solve = new(solverState) - } - a.nodes = a.nodes[:N] - - // rtypes is effectively part of the solver state. - a.rtypes = typeutil.Map{} - a.rtypes.SetHasher(a.hasher) - } - - a.hvn() - } - - if debugHVNCrossCheck { - runtime.GC() - runtime.GC() - } - - a.solve() - - // Compare solutions. - if optHVN && debugHVNCrossCheck { - a.dumpSolution("B.pts", N) - - if !diff("A.pts", "B.pts") { - return nil, fmt.Errorf("internal error: optimization changed solution") - } - } - - // Create callgraph.Nodes in deterministic order. - if cg := a.result.CallGraph; cg != nil { - for _, caller := range a.cgnodes { - cg.CreateNode(caller.fn) - } - } - - // Add dynamic edges to call graph. - var space [100]int - for _, caller := range a.cgnodes { - for _, site := range caller.sites { - for _, callee := range a.nodes[site.targets].solve.pts.AppendTo(space[:0]) { - a.callEdge(caller, site, nodeid(callee)) - } - } - } - - return a.result, nil -} - -// callEdge is called for each edge in the callgraph. -// calleeid is the callee's object node (has otFunction flag). -// -func (a *analysis) callEdge(caller *cgnode, site *callsite, calleeid nodeid) { - obj := a.nodes[calleeid].obj - if obj.flags&otFunction == 0 { - panic(fmt.Sprintf("callEdge %s -> n%d: not a function object", site, calleeid)) - } - callee := obj.cgn - - if cg := a.result.CallGraph; cg != nil { - // TODO(adonovan): opt: I would expect duplicate edges - // (to wrappers) to arise due to the elimination of - // context information, but I haven't observed any. - // Understand this better. - callgraph.AddEdge(cg.CreateNode(caller.fn), site.instr, cg.CreateNode(callee.fn)) - } - - if a.log != nil { - fmt.Fprintf(a.log, "\tcall edge %s -> %s\n", site, callee) - } - - // Warn about calls to non-intrinsic external functions. - // TODO(adonovan): de-dup these messages. - if fn := callee.fn; fn.Blocks == nil && a.findIntrinsic(fn) == nil { - a.warnf(site.pos(), "unsound call to unknown intrinsic: %s", fn) - a.warnf(fn.Pos(), " (declared here)") - } -} - -// dumpSolution writes the PTS solution to the specified file. -// -// It only dumps the nodes that existed before solving. The order in -// which solver-created nodes are created depends on pre-solver -// optimization, so we can't include them in the cross-check. -// -func (a *analysis) dumpSolution(filename string, N int) { - f, err := os.Create(filename) - if err != nil { - panic(err) - } - for id, n := range a.nodes[:N] { - if _, err := fmt.Fprintf(f, "pts(n%d) = {", id); err != nil { - panic(err) - } - var sep string - for _, l := range n.solve.pts.AppendTo(a.deltaSpace) { - if l >= N { - break - } - fmt.Fprintf(f, "%s%d", sep, l) - sep = " " - } - fmt.Fprintf(f, "} : %s\n", n.typ) - } - if err := f.Close(); err != nil { - panic(err) - } -} - -// showCounts logs the size of the constraint system. A typical -// optimized distribution is 65% copy, 13% load, 11% addr, 5% -// offsetAddr, 4% store, 2% others. -// -func (a *analysis) showCounts() { - if a.log != nil { - counts := make(map[reflect.Type]int) - for _, c := range a.constraints { - counts[reflect.TypeOf(c)]++ - } - fmt.Fprintf(a.log, "# constraints:\t%d\n", len(a.constraints)) - var lines []string - for t, n := range counts { - line := fmt.Sprintf("%7d (%2d%%)\t%s", n, 100*n/len(a.constraints), t) - lines = append(lines, line) - } - sort.Sort(sort.Reverse(sort.StringSlice(lines))) - for _, line := range lines { - fmt.Fprintf(a.log, "\t%s\n", line) - } - - fmt.Fprintf(a.log, "# nodes:\t%d\n", len(a.nodes)) - - // Show number of pointer equivalence classes. - m := make(map[*solverState]bool) - for _, n := range a.nodes { - m[n.solve] = true - } - fmt.Fprintf(a.log, "# ptsets:\t%d\n", len(m)) - } -} diff --git a/vendor/golang.org/x/tools/go/pointer/api.go b/vendor/golang.org/x/tools/go/pointer/api.go deleted file mode 100644 index 3c5c6dce92b..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/api.go +++ /dev/null @@ -1,285 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -import ( - "bytes" - "fmt" - "go/token" - "io" - - "golang.org/x/tools/container/intsets" - "golang.org/x/tools/go/callgraph" - "golang.org/x/tools/go/ssa" - "golang.org/x/tools/go/types/typeutil" -) - -// A Config formulates a pointer analysis problem for Analyze. It is -// only usable for a single invocation of Analyze and must not be -// reused. -type Config struct { - // Mains contains the set of 'main' packages to analyze - // Clients must provide the analysis with at least one - // package defining a main() function. - // - // Non-main packages in the ssa.Program that are not - // dependencies of any main package may still affect the - // analysis result, because they contribute runtime types and - // thus methods. - // TODO(adonovan): investigate whether this is desirable. - Mains []*ssa.Package - - // Reflection determines whether to handle reflection - // operators soundly, which is currently rather slow since it - // causes constraint to be generated during solving - // proportional to the number of constraint variables, which - // has not yet been reduced by presolver optimisation. - Reflection bool - - // BuildCallGraph determines whether to construct a callgraph. - // If enabled, the graph will be available in Result.CallGraph. - BuildCallGraph bool - - // The client populates Queries[v] or IndirectQueries[v] - // for each ssa.Value v of interest, to request that the - // points-to sets pts(v) or pts(*v) be computed. If the - // client needs both points-to sets, v may appear in both - // maps. - // - // (IndirectQueries is typically used for Values corresponding - // to source-level lvalues, e.g. an *ssa.Global.) - // - // The analysis populates the corresponding - // Result.{Indirect,}Queries map when it creates the pointer - // variable for v or *v. Upon completion the client can - // inspect that map for the results. - // - // TODO(adonovan): this API doesn't scale well for batch tools - // that want to dump the entire solution. Perhaps optionally - // populate a map[*ssa.DebugRef]Pointer in the Result, one - // entry per source expression. - // - Queries map[ssa.Value]struct{} - IndirectQueries map[ssa.Value]struct{} - extendedQueries map[ssa.Value][]*extendedQuery - - // If Log is non-nil, log messages are written to it. - // Logging is extremely verbose. - Log io.Writer -} - -type track uint32 - -const ( - trackChan track = 1 << iota // track 'chan' references - trackMap // track 'map' references - trackPtr // track regular pointers - trackSlice // track slice references - - trackAll = ^track(0) -) - -// AddQuery adds v to Config.Queries. -// Precondition: CanPoint(v.Type()). -func (c *Config) AddQuery(v ssa.Value) { - if !CanPoint(v.Type()) { - panic(fmt.Sprintf("%s is not a pointer-like value: %s", v, v.Type())) - } - if c.Queries == nil { - c.Queries = make(map[ssa.Value]struct{}) - } - c.Queries[v] = struct{}{} -} - -// AddQuery adds v to Config.IndirectQueries. -// Precondition: CanPoint(v.Type().Underlying().(*types.Pointer).Elem()). -func (c *Config) AddIndirectQuery(v ssa.Value) { - if c.IndirectQueries == nil { - c.IndirectQueries = make(map[ssa.Value]struct{}) - } - if !CanPoint(mustDeref(v.Type())) { - panic(fmt.Sprintf("%s is not the address of a pointer-like value: %s", v, v.Type())) - } - c.IndirectQueries[v] = struct{}{} -} - -// AddExtendedQuery adds an extended, AST-based query on v to the -// analysis. The query, which must be a single Go expression, allows -// destructuring the value. -// -// The query must operate on a variable named 'x', which represents -// the value, and result in a pointer-like object. Only a subset of -// Go expressions are permitted in queries, namely channel receives, -// pointer dereferences, field selectors, array/slice/map/tuple -// indexing and grouping with parentheses. The specific indices when -// indexing arrays, slices and maps have no significance. Indices used -// on tuples must be numeric and within bounds. -// -// All field selectors must be explicit, even ones usually elided -// due to promotion of embedded fields. -// -// The query 'x' is identical to using AddQuery. The query '*x' is -// identical to using AddIndirectQuery. -// -// On success, AddExtendedQuery returns a Pointer to the queried -// value. This Pointer will be initialized during analysis. Using it -// before analysis has finished has undefined behavior. -// -// Example: -// // given v, which represents a function call to 'fn() (int, []*T)', and -// // 'type T struct { F *int }', the following query will access the field F. -// c.AddExtendedQuery(v, "x[1][0].F") -func (c *Config) AddExtendedQuery(v ssa.Value, query string) (*Pointer, error) { - ops, _, err := parseExtendedQuery(v.Type(), query) - if err != nil { - return nil, fmt.Errorf("invalid query %q: %s", query, err) - } - if c.extendedQueries == nil { - c.extendedQueries = make(map[ssa.Value][]*extendedQuery) - } - - ptr := &Pointer{} - c.extendedQueries[v] = append(c.extendedQueries[v], &extendedQuery{ops: ops, ptr: ptr}) - return ptr, nil -} - -func (c *Config) prog() *ssa.Program { - for _, main := range c.Mains { - return main.Prog - } - panic("empty scope") -} - -type Warning struct { - Pos token.Pos - Message string -} - -// A Result contains the results of a pointer analysis. -// -// See Config for how to request the various Result components. -// -type Result struct { - CallGraph *callgraph.Graph // discovered call graph - Queries map[ssa.Value]Pointer // pts(v) for each v in Config.Queries. - IndirectQueries map[ssa.Value]Pointer // pts(*v) for each v in Config.IndirectQueries. - Warnings []Warning // warnings of unsoundness -} - -// A Pointer is an equivalence class of pointer-like values. -// -// A Pointer doesn't have a unique type because pointers of distinct -// types may alias the same object. -// -type Pointer struct { - a *analysis - n nodeid -} - -// A PointsToSet is a set of labels (locations or allocations). -type PointsToSet struct { - a *analysis // may be nil if pts is nil - pts *nodeset -} - -func (s PointsToSet) String() string { - var buf bytes.Buffer - buf.WriteByte('[') - if s.pts != nil { - var space [50]int - for i, l := range s.pts.AppendTo(space[:0]) { - if i > 0 { - buf.WriteString(", ") - } - buf.WriteString(s.a.labelFor(nodeid(l)).String()) - } - } - buf.WriteByte(']') - return buf.String() -} - -// PointsTo returns the set of labels that this points-to set -// contains. -func (s PointsToSet) Labels() []*Label { - var labels []*Label - if s.pts != nil { - var space [50]int - for _, l := range s.pts.AppendTo(space[:0]) { - labels = append(labels, s.a.labelFor(nodeid(l))) - } - } - return labels -} - -// If this PointsToSet came from a Pointer of interface kind -// or a reflect.Value, DynamicTypes returns the set of dynamic -// types that it may contain. (For an interface, they will -// always be concrete types.) -// -// The result is a mapping whose keys are the dynamic types to which -// it may point. For each pointer-like key type, the corresponding -// map value is the PointsToSet for pointers of that type. -// -// The result is empty unless CanHaveDynamicTypes(T). -// -func (s PointsToSet) DynamicTypes() *typeutil.Map { - var tmap typeutil.Map - tmap.SetHasher(s.a.hasher) - if s.pts != nil { - var space [50]int - for _, x := range s.pts.AppendTo(space[:0]) { - ifaceObjId := nodeid(x) - if !s.a.isTaggedObject(ifaceObjId) { - continue // !CanHaveDynamicTypes(tDyn) - } - tDyn, v, indirect := s.a.taggedValue(ifaceObjId) - if indirect { - panic("indirect tagged object") // implement later - } - pts, ok := tmap.At(tDyn).(PointsToSet) - if !ok { - pts = PointsToSet{s.a, new(nodeset)} - tmap.Set(tDyn, pts) - } - pts.pts.addAll(&s.a.nodes[v].solve.pts) - } - } - return &tmap -} - -// Intersects reports whether this points-to set and the -// argument points-to set contain common members. -func (x PointsToSet) Intersects(y PointsToSet) bool { - if x.pts == nil || y.pts == nil { - return false - } - // This takes Θ(|x|+|y|) time. - var z intsets.Sparse - z.Intersection(&x.pts.Sparse, &y.pts.Sparse) - return !z.IsEmpty() -} - -func (p Pointer) String() string { - return fmt.Sprintf("n%d", p.n) -} - -// PointsTo returns the points-to set of this pointer. -func (p Pointer) PointsTo() PointsToSet { - if p.n == 0 { - return PointsToSet{} - } - return PointsToSet{p.a, &p.a.nodes[p.n].solve.pts} -} - -// MayAlias reports whether the receiver pointer may alias -// the argument pointer. -func (p Pointer) MayAlias(q Pointer) bool { - return p.PointsTo().Intersects(q.PointsTo()) -} - -// DynamicTypes returns p.PointsTo().DynamicTypes(). -func (p Pointer) DynamicTypes() *typeutil.Map { - return p.PointsTo().DynamicTypes() -} diff --git a/vendor/golang.org/x/tools/go/pointer/callgraph.go b/vendor/golang.org/x/tools/go/pointer/callgraph.go deleted file mode 100644 index 48e152e4afa..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/callgraph.go +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -// This file defines the internal (context-sensitive) call graph. - -import ( - "fmt" - "go/token" - - "golang.org/x/tools/go/ssa" -) - -type cgnode struct { - fn *ssa.Function - obj nodeid // start of this contour's object block - sites []*callsite // ordered list of callsites within this function - callersite *callsite // where called from, if known; nil for shared contours -} - -// contour returns a description of this node's contour. -func (n *cgnode) contour() string { - if n.callersite == nil { - return "shared contour" - } - if n.callersite.instr != nil { - return fmt.Sprintf("as called from %s", n.callersite.instr.Parent()) - } - return fmt.Sprintf("as called from intrinsic (targets=n%d)", n.callersite.targets) -} - -func (n *cgnode) String() string { - return fmt.Sprintf("cg%d:%s", n.obj, n.fn) -} - -// A callsite represents a single call site within a cgnode; -// it is implicitly context-sensitive. -// callsites never represent calls to built-ins; -// they are handled as intrinsics. -// -type callsite struct { - targets nodeid // pts(·) contains objects for dynamically called functions - instr ssa.CallInstruction // the call instruction; nil for synthetic/intrinsic -} - -func (c *callsite) String() string { - if c.instr != nil { - return c.instr.Common().Description() - } - return "synthetic function call" -} - -// pos returns the source position of this callsite, or token.NoPos if implicit. -func (c *callsite) pos() token.Pos { - if c.instr != nil { - return c.instr.Pos() - } - return token.NoPos -} diff --git a/vendor/golang.org/x/tools/go/pointer/constraint.go b/vendor/golang.org/x/tools/go/pointer/constraint.go deleted file mode 100644 index 54b54288a0d..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/constraint.go +++ /dev/null @@ -1,149 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -import "go/types" - -type constraint interface { - // For a complex constraint, returns the nodeid of the pointer - // to which it is attached. For addr and copy, returns dst. - ptr() nodeid - - // renumber replaces each nodeid n in the constraint by mapping[n]. - renumber(mapping []nodeid) - - // presolve is a hook for constraint-specific behaviour during - // pre-solver optimization. Typical implementations mark as - // indirect the set of nodes to which the solver will add copy - // edges or PTS labels. - presolve(h *hvn) - - // solve is called for complex constraints when the pts for - // the node to which they are attached has changed. - solve(a *analysis, delta *nodeset) - - String() string -} - -// dst = &src -// pts(dst) ⊇ {src} -// A base constraint used to initialize the solver's pt sets -type addrConstraint struct { - dst nodeid // (ptr) - src nodeid -} - -func (c *addrConstraint) ptr() nodeid { return c.dst } -func (c *addrConstraint) renumber(mapping []nodeid) { - c.dst = mapping[c.dst] - c.src = mapping[c.src] -} - -// dst = src -// A simple constraint represented directly as a copyTo graph edge. -type copyConstraint struct { - dst nodeid // (ptr) - src nodeid -} - -func (c *copyConstraint) ptr() nodeid { return c.dst } -func (c *copyConstraint) renumber(mapping []nodeid) { - c.dst = mapping[c.dst] - c.src = mapping[c.src] -} - -// dst = src[offset] -// A complex constraint attached to src (the pointer) -type loadConstraint struct { - offset uint32 - dst nodeid - src nodeid // (ptr) -} - -func (c *loadConstraint) ptr() nodeid { return c.src } -func (c *loadConstraint) renumber(mapping []nodeid) { - c.dst = mapping[c.dst] - c.src = mapping[c.src] -} - -// dst[offset] = src -// A complex constraint attached to dst (the pointer) -type storeConstraint struct { - offset uint32 - dst nodeid // (ptr) - src nodeid -} - -func (c *storeConstraint) ptr() nodeid { return c.dst } -func (c *storeConstraint) renumber(mapping []nodeid) { - c.dst = mapping[c.dst] - c.src = mapping[c.src] -} - -// dst = &src.f or dst = &src[0] -// A complex constraint attached to dst (the pointer) -type offsetAddrConstraint struct { - offset uint32 - dst nodeid - src nodeid // (ptr) -} - -func (c *offsetAddrConstraint) ptr() nodeid { return c.src } -func (c *offsetAddrConstraint) renumber(mapping []nodeid) { - c.dst = mapping[c.dst] - c.src = mapping[c.src] -} - -// dst = src.(typ) where typ is an interface -// A complex constraint attached to src (the interface). -// No representation change: pts(dst) and pts(src) contains tagged objects. -type typeFilterConstraint struct { - typ types.Type // an interface type - dst nodeid - src nodeid // (ptr) -} - -func (c *typeFilterConstraint) ptr() nodeid { return c.src } -func (c *typeFilterConstraint) renumber(mapping []nodeid) { - c.dst = mapping[c.dst] - c.src = mapping[c.src] -} - -// dst = src.(typ) where typ is a concrete type -// A complex constraint attached to src (the interface). -// -// If exact, only tagged objects identical to typ are untagged. -// If !exact, tagged objects assignable to typ are untagged too. -// The latter is needed for various reflect operators, e.g. Send. -// -// This entails a representation change: -// pts(src) contains tagged objects, -// pts(dst) contains their payloads. -type untagConstraint struct { - typ types.Type // a concrete type - dst nodeid - src nodeid // (ptr) - exact bool -} - -func (c *untagConstraint) ptr() nodeid { return c.src } -func (c *untagConstraint) renumber(mapping []nodeid) { - c.dst = mapping[c.dst] - c.src = mapping[c.src] -} - -// src.method(params...) -// A complex constraint attached to iface. -type invokeConstraint struct { - method *types.Func // the abstract method - iface nodeid // (ptr) the interface - params nodeid // the start of the identity/params/results block -} - -func (c *invokeConstraint) ptr() nodeid { return c.iface } -func (c *invokeConstraint) renumber(mapping []nodeid) { - c.iface = mapping[c.iface] - c.params = mapping[c.params] -} diff --git a/vendor/golang.org/x/tools/go/pointer/doc.go b/vendor/golang.org/x/tools/go/pointer/doc.go deleted file mode 100644 index e317cf5c397..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/doc.go +++ /dev/null @@ -1,610 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -/* - -Package pointer implements Andersen's analysis, an inclusion-based -pointer analysis algorithm first described in (Andersen, 1994). - -A pointer analysis relates every pointer expression in a whole program -to the set of memory locations to which it might point. This -information can be used to construct a call graph of the program that -precisely represents the destinations of dynamic function and method -calls. It can also be used to determine, for example, which pairs of -channel operations operate on the same channel. - -The package allows the client to request a set of expressions of -interest for which the points-to information will be returned once the -analysis is complete. In addition, the client may request that a -callgraph is constructed. The example program in example_test.go -demonstrates both of these features. Clients should not request more -information than they need since it may increase the cost of the -analysis significantly. - - -CLASSIFICATION - -Our algorithm is INCLUSION-BASED: the points-to sets for x and y will -be related by pts(y) ⊇ pts(x) if the program contains the statement -y = x. - -It is FLOW-INSENSITIVE: it ignores all control flow constructs and the -order of statements in a program. It is therefore a "MAY ALIAS" -analysis: its facts are of the form "P may/may not point to L", -not "P must point to L". - -It is FIELD-SENSITIVE: it builds separate points-to sets for distinct -fields, such as x and y in struct { x, y *int }. - -It is mostly CONTEXT-INSENSITIVE: most functions are analyzed once, -so values can flow in at one call to the function and return out at -another. Only some smaller functions are analyzed with consideration -of their calling context. - -It has a CONTEXT-SENSITIVE HEAP: objects are named by both allocation -site and context, so the objects returned by two distinct calls to f: - func f() *T { return new(T) } -are distinguished up to the limits of the calling context. - -It is a WHOLE PROGRAM analysis: it requires SSA-form IR for the -complete Go program and summaries for native code. - -See the (Hind, PASTE'01) survey paper for an explanation of these terms. - - -SOUNDNESS - -The analysis is fully sound when invoked on pure Go programs that do not -use reflection or unsafe.Pointer conversions. In other words, if there -is any possible execution of the program in which pointer P may point to -object O, the analysis will report that fact. - - -REFLECTION - -By default, the "reflect" library is ignored by the analysis, as if all -its functions were no-ops, but if the client enables the Reflection flag, -the analysis will make a reasonable attempt to model the effects of -calls into this library. However, this comes at a significant -performance cost, and not all features of that library are yet -implemented. In addition, some simplifying approximations must be made -to ensure that the analysis terminates; for example, reflection can be -used to construct an infinite set of types and values of those types, -but the analysis arbitrarily bounds the depth of such types. - -Most but not all reflection operations are supported. -In particular, addressable reflect.Values are not yet implemented, so -operations such as (reflect.Value).Set have no analytic effect. - - -UNSAFE POINTER CONVERSIONS - -The pointer analysis makes no attempt to understand aliasing between the -operand x and result y of an unsafe.Pointer conversion: - y = (*T)(unsafe.Pointer(x)) -It is as if the conversion allocated an entirely new object: - y = new(T) - - -NATIVE CODE - -The analysis cannot model the aliasing effects of functions written in -languages other than Go, such as runtime intrinsics in C or assembly, or -code accessed via cgo. The result is as if such functions are no-ops. -However, various important intrinsics are understood by the analysis, -along with built-ins such as append. - -The analysis currently provides no way for users to specify the aliasing -effects of native code. - ------------------------------------------------------------------------- - -IMPLEMENTATION - -The remaining documentation is intended for package maintainers and -pointer analysis specialists. Maintainers should have a solid -understanding of the referenced papers (especially those by H&L and PKH) -before making making significant changes. - -The implementation is similar to that described in (Pearce et al, -PASTE'04). Unlike many algorithms which interleave constraint -generation and solving, constructing the callgraph as they go, this -implementation for the most part observes a phase ordering (generation -before solving), with only simple (copy) constraints being generated -during solving. (The exception is reflection, which creates various -constraints during solving as new types flow to reflect.Value -operations.) This improves the traction of presolver optimisations, -but imposes certain restrictions, e.g. potential context sensitivity -is limited since all variants must be created a priori. - - -TERMINOLOGY - -A type is said to be "pointer-like" if it is a reference to an object. -Pointer-like types include pointers and also interfaces, maps, channels, -functions and slices. - -We occasionally use C's x->f notation to distinguish the case where x -is a struct pointer from x.f where is a struct value. - -Pointer analysis literature (and our comments) often uses the notation -dst=*src+offset to mean something different than what it means in Go. -It means: for each node index p in pts(src), the node index p+offset is -in pts(dst). Similarly *dst+offset=src is used for store constraints -and dst=src+offset for offset-address constraints. - - -NODES - -Nodes are the key datastructure of the analysis, and have a dual role: -they represent both constraint variables (equivalence classes of -pointers) and members of points-to sets (things that can be pointed -at, i.e. "labels"). - -Nodes are naturally numbered. The numbering enables compact -representations of sets of nodes such as bitvectors (or BDDs); and the -ordering enables a very cheap way to group related nodes together. For -example, passing n parameters consists of generating n parallel -constraints from caller+i to callee+i for 0<=i y is added. - - ChangeInterface is a simple copy because the representation of - tagged objects is independent of the interface type (in contrast - to the "method tables" approach used by the gc runtime). - - y := Invoke x.m(...) is implemented by allocating contiguous P/R - blocks for the callsite and adding a dynamic rule triggered by each - tagged object added to pts(x). The rule adds param/results copy - edges to/from each discovered concrete method. - - (Q. Why do we model an interface as a pointer to a pair of type and - value, rather than as a pair of a pointer to type and a pointer to - value? - A. Control-flow joins would merge interfaces ({T1}, {V1}) and ({T2}, - {V2}) to make ({T1,T2}, {V1,V2}), leading to the infeasible and - type-unsafe combination (T1,V2). Treating the value and its concrete - type as inseparable makes the analysis type-safe.) - -reflect.Value - A reflect.Value is modelled very similar to an interface{}, i.e. as - a pointer exclusively to tagged objects, but with two generalizations. - - 1) a reflect.Value that represents an lvalue points to an indirect - (obj.flags ⊇ {otIndirect}) tagged object, which has a similar - layout to an tagged object except that the value is a pointer to - the dynamic type. Indirect tagged objects preserve the correct - aliasing so that mutations made by (reflect.Value).Set can be - observed. - - Indirect objects only arise when an lvalue is derived from an - rvalue by indirection, e.g. the following code: - - type S struct { X T } - var s S - var i interface{} = &s // i points to a *S-tagged object (from MakeInterface) - v1 := reflect.ValueOf(i) // v1 points to same *S-tagged object as i - v2 := v1.Elem() // v2 points to an indirect S-tagged object, pointing to s - v3 := v2.FieldByName("X") // v3 points to an indirect int-tagged object, pointing to s.X - v3.Set(y) // pts(s.X) ⊇ pts(y) - - Whether indirect or not, the concrete type of the tagged object - corresponds to the user-visible dynamic type, and the existence - of a pointer is an implementation detail. - - (NB: indirect tagged objects are not yet implemented) - - 2) The dynamic type tag of a tagged object pointed to by a - reflect.Value may be an interface type; it need not be concrete. - - This arises in code such as this: - tEface := reflect.TypeOf(new(interface{}).Elem() // interface{} - eface := reflect.Zero(tEface) - pts(eface) is a singleton containing an interface{}-tagged - object. That tagged object's payload is an interface{} value, - i.e. the pts of the payload contains only concrete-tagged - objects, although in this example it's the zero interface{} value, - so its pts is empty. - -reflect.Type - Just as in the real "reflect" library, we represent a reflect.Type - as an interface whose sole implementation is the concrete type, - *reflect.rtype. (This choice is forced on us by go/types: clients - cannot fabricate types with arbitrary method sets.) - - rtype instances are canonical: there is at most one per dynamic - type. (rtypes are in fact large structs but since identity is all - that matters, we represent them by a single node.) - - The payload of each *rtype-tagged object is an *rtype pointer that - points to exactly one such canonical rtype object. We exploit this - by setting the node.typ of the payload to the dynamic type, not - '*rtype'. This saves us an indirection in each resolution rule. As - an optimisation, *rtype-tagged objects are canonicalized too. - - -Aggregate types: - -Aggregate types are treated as if all directly contained -aggregates are recursively flattened out. - -Structs - *ssa.Field y = x.f creates a simple edge to y from x's node at f's offset. - - *ssa.FieldAddr y = &x->f requires a dynamic closure rule to create - simple edges for each struct discovered in pts(x). - - The nodes of a struct consist of a special 'identity' node (whose - type is that of the struct itself), followed by the nodes for all - the struct's fields, recursively flattened out. A pointer to the - struct is a pointer to its identity node. That node allows us to - distinguish a pointer to a struct from a pointer to its first field. - - Field offsets are logical field offsets (plus one for the identity - node), so the sizes of the fields can be ignored by the analysis. - - (The identity node is non-traditional but enables the distinction - described above, which is valuable for code comprehension tools. - Typical pointer analyses for C, whose purpose is compiler - optimization, must soundly model unsafe.Pointer (void*) conversions, - and this requires fidelity to the actual memory layout using physical - field offsets.) - - *ssa.Field y = x.f creates a simple edge to y from x's node at f's offset. - - *ssa.FieldAddr y = &x->f requires a dynamic closure rule to create - simple edges for each struct discovered in pts(x). - -Arrays - We model an array by an identity node (whose type is that of the - array itself) followed by a node representing all the elements of - the array; the analysis does not distinguish elements with different - indices. Effectively, an array is treated like struct{elem T}, a - load y=x[i] like y=x.elem, and a store x[i]=y like x.elem=y; the - index i is ignored. - - A pointer to an array is pointer to its identity node. (A slice is - also a pointer to an array's identity node.) The identity node - allows us to distinguish a pointer to an array from a pointer to one - of its elements, but it is rather costly because it introduces more - offset constraints into the system. Furthermore, sound treatment of - unsafe.Pointer would require us to dispense with this node. - - Arrays may be allocated by Alloc, by make([]T), by calls to append, - and via reflection. - -Tuples (T, ...) - Tuples are treated like structs with naturally numbered fields. - *ssa.Extract is analogous to *ssa.Field. - - However, tuples have no identity field since by construction, they - cannot be address-taken. - - -FUNCTION CALLS - - There are three kinds of function call: - (1) static "call"-mode calls of functions. - (2) dynamic "call"-mode calls of functions. - (3) dynamic "invoke"-mode calls of interface methods. - Cases 1 and 2 apply equally to methods and standalone functions. - - Static calls. - A static call consists three steps: - - finding the function object of the callee; - - creating copy edges from the actual parameter value nodes to the - P-block in the function object (this includes the receiver if - the callee is a method); - - creating copy edges from the R-block in the function object to - the value nodes for the result of the call. - - A static function call is little more than two struct value copies - between the P/R blocks of caller and callee: - - callee.P = caller.P - caller.R = callee.R - - Context sensitivity - - Static calls (alone) may be treated context sensitively, - i.e. each callsite may cause a distinct re-analysis of the - callee, improving precision. Our current context-sensitivity - policy treats all intrinsics and getter/setter methods in this - manner since such functions are small and seem like an obvious - source of spurious confluences, though this has not yet been - evaluated. - - Dynamic function calls - - Dynamic calls work in a similar manner except that the creation of - copy edges occurs dynamically, in a similar fashion to a pair of - struct copies in which the callee is indirect: - - callee->P = caller.P - caller.R = callee->R - - (Recall that the function object's P- and R-blocks are contiguous.) - - Interface method invocation - - For invoke-mode calls, we create a params/results block for the - callsite and attach a dynamic closure rule to the interface. For - each new tagged object that flows to the interface, we look up - the concrete method, find its function object, and connect its P/R - blocks to the callsite's P/R blocks, adding copy edges to the graph - during solving. - - Recording call targets - - The analysis notifies its clients of each callsite it encounters, - passing a CallSite interface. Among other things, the CallSite - contains a synthetic constraint variable ("targets") whose - points-to solution includes the set of all function objects to - which the call may dispatch. - - It is via this mechanism that the callgraph is made available. - Clients may also elect to be notified of callgraph edges directly; - internally this just iterates all "targets" variables' pts(·)s. - - -PRESOLVER - -We implement Hash-Value Numbering (HVN), a pre-solver constraint -optimization described in Hardekopf & Lin, SAS'07. This is documented -in more detail in hvn.go. We intend to add its cousins HR and HU in -future. - - -SOLVER - -The solver is currently a naive Andersen-style implementation; it does -not perform online cycle detection, though we plan to add solver -optimisations such as Hybrid- and Lazy- Cycle Detection from (Hardekopf -& Lin, PLDI'07). - -It uses difference propagation (Pearce et al, SQC'04) to avoid -redundant re-triggering of closure rules for values already seen. - -Points-to sets are represented using sparse bit vectors (similar to -those used in LLVM and gcc), which are more space- and time-efficient -than sets based on Go's built-in map type or dense bit vectors. - -Nodes are permuted prior to solving so that object nodes (which may -appear in points-to sets) are lower numbered than non-object (var) -nodes. This improves the density of the set over which the PTSs -range, and thus the efficiency of the representation. - -Partly thanks to avoiding map iteration, the execution of the solver is -100% deterministic, a great help during debugging. - - -FURTHER READING - -Andersen, L. O. 1994. Program analysis and specialization for the C -programming language. Ph.D. dissertation. DIKU, University of -Copenhagen. - -David J. Pearce, Paul H. J. Kelly, and Chris Hankin. 2004. Efficient -field-sensitive pointer analysis for C. In Proceedings of the 5th ACM -SIGPLAN-SIGSOFT workshop on Program analysis for software tools and -engineering (PASTE '04). ACM, New York, NY, USA, 37-42. -http://doi.acm.org/10.1145/996821.996835 - -David J. Pearce, Paul H. J. Kelly, and Chris Hankin. 2004. Online -Cycle Detection and Difference Propagation: Applications to Pointer -Analysis. Software Quality Control 12, 4 (December 2004), 311-337. -http://dx.doi.org/10.1023/B:SQJO.0000039791.93071.a2 - -David Grove and Craig Chambers. 2001. A framework for call graph -construction algorithms. ACM Trans. Program. Lang. Syst. 23, 6 -(November 2001), 685-746. -http://doi.acm.org/10.1145/506315.506316 - -Ben Hardekopf and Calvin Lin. 2007. The ant and the grasshopper: fast -and accurate pointer analysis for millions of lines of code. In -Proceedings of the 2007 ACM SIGPLAN conference on Programming language -design and implementation (PLDI '07). ACM, New York, NY, USA, 290-299. -http://doi.acm.org/10.1145/1250734.1250767 - -Ben Hardekopf and Calvin Lin. 2007. Exploiting pointer and location -equivalence to optimize pointer analysis. In Proceedings of the 14th -international conference on Static Analysis (SAS'07), Hanne Riis -Nielson and Gilberto Filé (Eds.). Springer-Verlag, Berlin, Heidelberg, -265-280. - -Atanas Rountev and Satish Chandra. 2000. Off-line variable substitution -for scaling points-to analysis. In Proceedings of the ACM SIGPLAN 2000 -conference on Programming language design and implementation (PLDI '00). -ACM, New York, NY, USA, 47-56. DOI=10.1145/349299.349310 -http://doi.acm.org/10.1145/349299.349310 - -*/ -package pointer // import "golang.org/x/tools/go/pointer" diff --git a/vendor/golang.org/x/tools/go/pointer/gen.go b/vendor/golang.org/x/tools/go/pointer/gen.go deleted file mode 100644 index f2a51715b7e..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/gen.go +++ /dev/null @@ -1,1325 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -// This file defines the constraint generation phase. - -// TODO(adonovan): move the constraint definitions and the store() etc -// functions which add them (and are also used by the solver) into a -// new file, constraints.go. - -import ( - "fmt" - "go/token" - "go/types" - - "golang.org/x/tools/go/callgraph" - "golang.org/x/tools/go/ssa" -) - -var ( - tEface = types.NewInterface(nil, nil).Complete() - tInvalid = types.Typ[types.Invalid] - tUnsafePtr = types.Typ[types.UnsafePointer] -) - -// ---------- Node creation ---------- - -// nextNode returns the index of the next unused node. -func (a *analysis) nextNode() nodeid { - return nodeid(len(a.nodes)) -} - -// addNodes creates nodes for all scalar elements in type typ, and -// returns the id of the first one, or zero if the type was -// analytically uninteresting. -// -// comment explains the origin of the nodes, as a debugging aid. -// -func (a *analysis) addNodes(typ types.Type, comment string) nodeid { - id := a.nextNode() - for _, fi := range a.flatten(typ) { - a.addOneNode(fi.typ, comment, fi) - } - if id == a.nextNode() { - return 0 // type contained no pointers - } - return id -} - -// addOneNode creates a single node with type typ, and returns its id. -// -// typ should generally be scalar (except for tagged.T nodes -// and struct/array identity nodes). Use addNodes for non-scalar types. -// -// comment explains the origin of the nodes, as a debugging aid. -// subelement indicates the subelement, e.g. ".a.b[*].c". -// -func (a *analysis) addOneNode(typ types.Type, comment string, subelement *fieldInfo) nodeid { - id := a.nextNode() - a.nodes = append(a.nodes, &node{typ: typ, subelement: subelement, solve: new(solverState)}) - if a.log != nil { - fmt.Fprintf(a.log, "\tcreate n%d %s for %s%s\n", - id, typ, comment, subelement.path()) - } - return id -} - -// setValueNode associates node id with the value v. -// cgn identifies the context iff v is a local variable. -// -func (a *analysis) setValueNode(v ssa.Value, id nodeid, cgn *cgnode) { - if cgn != nil { - a.localval[v] = id - } else { - a.globalval[v] = id - } - if a.log != nil { - fmt.Fprintf(a.log, "\tval[%s] = n%d (%T)\n", v.Name(), id, v) - } - - // Due to context-sensitivity, we may encounter the same Value - // in many contexts. We merge them to a canonical node, since - // that's what all clients want. - - // Record the (v, id) relation if the client has queried pts(v). - if _, ok := a.config.Queries[v]; ok { - t := v.Type() - ptr, ok := a.result.Queries[v] - if !ok { - // First time? Create the canonical query node. - ptr = Pointer{a, a.addNodes(t, "query")} - a.result.Queries[v] = ptr - } - a.result.Queries[v] = ptr - a.copy(ptr.n, id, a.sizeof(t)) - } - - // Record the (*v, id) relation if the client has queried pts(*v). - if _, ok := a.config.IndirectQueries[v]; ok { - t := v.Type() - ptr, ok := a.result.IndirectQueries[v] - if !ok { - // First time? Create the canonical indirect query node. - ptr = Pointer{a, a.addNodes(v.Type(), "query.indirect")} - a.result.IndirectQueries[v] = ptr - } - a.genLoad(cgn, ptr.n, v, 0, a.sizeof(t)) - } - - for _, query := range a.config.extendedQueries[v] { - t, nid := a.evalExtendedQuery(v.Type().Underlying(), id, query.ops) - - if query.ptr.a == nil { - query.ptr.a = a - query.ptr.n = a.addNodes(t, "query.extended") - } - a.copy(query.ptr.n, nid, a.sizeof(t)) - } -} - -// endObject marks the end of a sequence of calls to addNodes denoting -// a single object allocation. -// -// obj is the start node of the object, from a prior call to nextNode. -// Its size, flags and optional data will be updated. -// -func (a *analysis) endObject(obj nodeid, cgn *cgnode, data interface{}) *object { - // Ensure object is non-empty by padding; - // the pad will be the object node. - size := uint32(a.nextNode() - obj) - if size == 0 { - a.addOneNode(tInvalid, "padding", nil) - } - objNode := a.nodes[obj] - o := &object{ - size: size, // excludes padding - cgn: cgn, - data: data, - } - objNode.obj = o - - return o -} - -// makeFunctionObject creates and returns a new function object -// (contour) for fn, and returns the id of its first node. It also -// enqueues fn for subsequent constraint generation. -// -// For a context-sensitive contour, callersite identifies the sole -// callsite; for shared contours, caller is nil. -// -func (a *analysis) makeFunctionObject(fn *ssa.Function, callersite *callsite) nodeid { - if a.log != nil { - fmt.Fprintf(a.log, "\t---- makeFunctionObject %s\n", fn) - } - - // obj is the function object (identity, params, results). - obj := a.nextNode() - cgn := a.makeCGNode(fn, obj, callersite) - sig := fn.Signature - a.addOneNode(sig, "func.cgnode", nil) // (scalar with Signature type) - if recv := sig.Recv(); recv != nil { - a.addNodes(recv.Type(), "func.recv") - } - a.addNodes(sig.Params(), "func.params") - a.addNodes(sig.Results(), "func.results") - a.endObject(obj, cgn, fn).flags |= otFunction - - if a.log != nil { - fmt.Fprintf(a.log, "\t----\n") - } - - // Queue it up for constraint processing. - a.genq = append(a.genq, cgn) - - return obj -} - -// makeTagged creates a tagged object of type typ. -func (a *analysis) makeTagged(typ types.Type, cgn *cgnode, data interface{}) nodeid { - obj := a.addOneNode(typ, "tagged.T", nil) // NB: type may be non-scalar! - a.addNodes(typ, "tagged.v") - a.endObject(obj, cgn, data).flags |= otTagged - return obj -} - -// makeRtype returns the canonical tagged object of type *rtype whose -// payload points to the sole rtype object for T. -// -// TODO(adonovan): move to reflect.go; it's part of the solver really. -// -func (a *analysis) makeRtype(T types.Type) nodeid { - if v := a.rtypes.At(T); v != nil { - return v.(nodeid) - } - - // Create the object for the reflect.rtype itself, which is - // ordinarily a large struct but here a single node will do. - obj := a.nextNode() - a.addOneNode(T, "reflect.rtype", nil) - a.endObject(obj, nil, T) - - id := a.makeTagged(a.reflectRtypePtr, nil, T) - a.nodes[id+1].typ = T // trick (each *rtype tagged object is a singleton) - a.addressOf(a.reflectRtypePtr, id+1, obj) - - a.rtypes.Set(T, id) - return id -} - -// rtypeValue returns the type of the *reflect.rtype-tagged object obj. -func (a *analysis) rtypeTaggedValue(obj nodeid) types.Type { - tDyn, t, _ := a.taggedValue(obj) - if tDyn != a.reflectRtypePtr { - panic(fmt.Sprintf("not a *reflect.rtype-tagged object: obj=n%d tag=%v payload=n%d", obj, tDyn, t)) - } - return a.nodes[t].typ -} - -// valueNode returns the id of the value node for v, creating it (and -// the association) as needed. It may return zero for uninteresting -// values containing no pointers. -// -func (a *analysis) valueNode(v ssa.Value) nodeid { - // Value nodes for locals are created en masse by genFunc. - if id, ok := a.localval[v]; ok { - return id - } - - // Value nodes for globals are created on demand. - id, ok := a.globalval[v] - if !ok { - var comment string - if a.log != nil { - comment = v.String() - } - id = a.addNodes(v.Type(), comment) - if obj := a.objectNode(nil, v); obj != 0 { - a.addressOf(v.Type(), id, obj) - } - a.setValueNode(v, id, nil) - } - return id -} - -// valueOffsetNode ascertains the node for tuple/struct value v, -// then returns the node for its subfield #index. -// -func (a *analysis) valueOffsetNode(v ssa.Value, index int) nodeid { - id := a.valueNode(v) - if id == 0 { - panic(fmt.Sprintf("cannot offset within n0: %s = %s", v.Name(), v)) - } - return id + nodeid(a.offsetOf(v.Type(), index)) -} - -// isTaggedObject reports whether object obj is a tagged object. -func (a *analysis) isTaggedObject(obj nodeid) bool { - return a.nodes[obj].obj.flags&otTagged != 0 -} - -// taggedValue returns the dynamic type tag, the (first node of the) -// payload, and the indirect flag of the tagged object starting at id. -// Panic ensues if !isTaggedObject(id). -// -func (a *analysis) taggedValue(obj nodeid) (tDyn types.Type, v nodeid, indirect bool) { - n := a.nodes[obj] - flags := n.obj.flags - if flags&otTagged == 0 { - panic(fmt.Sprintf("not a tagged object: n%d", obj)) - } - return n.typ, obj + 1, flags&otIndirect != 0 -} - -// funcParams returns the first node of the params (P) block of the -// function whose object node (obj.flags&otFunction) is id. -// -func (a *analysis) funcParams(id nodeid) nodeid { - n := a.nodes[id] - if n.obj == nil || n.obj.flags&otFunction == 0 { - panic(fmt.Sprintf("funcParams(n%d): not a function object block", id)) - } - return id + 1 -} - -// funcResults returns the first node of the results (R) block of the -// function whose object node (obj.flags&otFunction) is id. -// -func (a *analysis) funcResults(id nodeid) nodeid { - n := a.nodes[id] - if n.obj == nil || n.obj.flags&otFunction == 0 { - panic(fmt.Sprintf("funcResults(n%d): not a function object block", id)) - } - sig := n.typ.(*types.Signature) - id += 1 + nodeid(a.sizeof(sig.Params())) - if sig.Recv() != nil { - id += nodeid(a.sizeof(sig.Recv().Type())) - } - return id -} - -// ---------- Constraint creation ---------- - -// copy creates a constraint of the form dst = src. -// sizeof is the width (in logical fields) of the copied type. -// -func (a *analysis) copy(dst, src nodeid, sizeof uint32) { - if src == dst || sizeof == 0 { - return // trivial - } - if src == 0 || dst == 0 { - panic(fmt.Sprintf("ill-typed copy dst=n%d src=n%d", dst, src)) - } - for i := uint32(0); i < sizeof; i++ { - a.addConstraint(©Constraint{dst, src}) - src++ - dst++ - } -} - -// addressOf creates a constraint of the form id = &obj. -// T is the type of the address. -func (a *analysis) addressOf(T types.Type, id, obj nodeid) { - if id == 0 { - panic("addressOf: zero id") - } - if obj == 0 { - panic("addressOf: zero obj") - } - if a.shouldTrack(T) { - a.addConstraint(&addrConstraint{id, obj}) - } -} - -// load creates a load constraint of the form dst = src[offset]. -// offset is the pointer offset in logical fields. -// sizeof is the width (in logical fields) of the loaded type. -// -func (a *analysis) load(dst, src nodeid, offset, sizeof uint32) { - if dst == 0 { - return // load of non-pointerlike value - } - if src == 0 && dst == 0 { - return // non-pointerlike operation - } - if src == 0 || dst == 0 { - panic(fmt.Sprintf("ill-typed load dst=n%d src=n%d", dst, src)) - } - for i := uint32(0); i < sizeof; i++ { - a.addConstraint(&loadConstraint{offset, dst, src}) - offset++ - dst++ - } -} - -// store creates a store constraint of the form dst[offset] = src. -// offset is the pointer offset in logical fields. -// sizeof is the width (in logical fields) of the stored type. -// -func (a *analysis) store(dst, src nodeid, offset uint32, sizeof uint32) { - if src == 0 { - return // store of non-pointerlike value - } - if src == 0 && dst == 0 { - return // non-pointerlike operation - } - if src == 0 || dst == 0 { - panic(fmt.Sprintf("ill-typed store dst=n%d src=n%d", dst, src)) - } - for i := uint32(0); i < sizeof; i++ { - a.addConstraint(&storeConstraint{offset, dst, src}) - offset++ - src++ - } -} - -// offsetAddr creates an offsetAddr constraint of the form dst = &src.#offset. -// offset is the field offset in logical fields. -// T is the type of the address. -// -func (a *analysis) offsetAddr(T types.Type, dst, src nodeid, offset uint32) { - if !a.shouldTrack(T) { - return - } - if offset == 0 { - // Simplify dst = &src->f0 - // to dst = src - // (NB: this optimisation is defeated by the identity - // field prepended to struct and array objects.) - a.copy(dst, src, 1) - } else { - a.addConstraint(&offsetAddrConstraint{offset, dst, src}) - } -} - -// typeAssert creates a typeFilter or untag constraint of the form dst = src.(T): -// typeFilter for an interface, untag for a concrete type. -// The exact flag is specified as for untagConstraint. -// -func (a *analysis) typeAssert(T types.Type, dst, src nodeid, exact bool) { - if isInterface(T) { - a.addConstraint(&typeFilterConstraint{T, dst, src}) - } else { - a.addConstraint(&untagConstraint{T, dst, src, exact}) - } -} - -// addConstraint adds c to the constraint set. -func (a *analysis) addConstraint(c constraint) { - a.constraints = append(a.constraints, c) - if a.log != nil { - fmt.Fprintf(a.log, "\t%s\n", c) - } -} - -// copyElems generates load/store constraints for *dst = *src, -// where src and dst are slices or *arrays. -// -func (a *analysis) copyElems(cgn *cgnode, typ types.Type, dst, src ssa.Value) { - tmp := a.addNodes(typ, "copy") - sz := a.sizeof(typ) - a.genLoad(cgn, tmp, src, 1, sz) - a.genStore(cgn, dst, tmp, 1, sz) -} - -// ---------- Constraint generation ---------- - -// genConv generates constraints for the conversion operation conv. -func (a *analysis) genConv(conv *ssa.Convert, cgn *cgnode) { - res := a.valueNode(conv) - if res == 0 { - return // result is non-pointerlike - } - - tSrc := conv.X.Type() - tDst := conv.Type() - - switch utSrc := tSrc.Underlying().(type) { - case *types.Slice: - // []byte/[]rune -> string? - return - - case *types.Pointer: - // *T -> unsafe.Pointer? - if tDst.Underlying() == tUnsafePtr { - return // we don't model unsafe aliasing (unsound) - } - - case *types.Basic: - switch tDst.Underlying().(type) { - case *types.Pointer: - // Treat unsafe.Pointer->*T conversions like - // new(T) and create an unaliased object. - if utSrc == tUnsafePtr { - obj := a.addNodes(mustDeref(tDst), "unsafe.Pointer conversion") - a.endObject(obj, cgn, conv) - a.addressOf(tDst, res, obj) - return - } - - case *types.Slice: - // string -> []byte/[]rune (or named aliases)? - if utSrc.Info()&types.IsString != 0 { - obj := a.addNodes(sliceToArray(tDst), "convert") - a.endObject(obj, cgn, conv) - a.addressOf(tDst, res, obj) - return - } - - case *types.Basic: - // All basic-to-basic type conversions are no-ops. - // This includes uintptr<->unsafe.Pointer conversions, - // which we (unsoundly) ignore. - return - } - } - - panic(fmt.Sprintf("illegal *ssa.Convert %s -> %s: %s", tSrc, tDst, conv.Parent())) -} - -// genAppend generates constraints for a call to append. -func (a *analysis) genAppend(instr *ssa.Call, cgn *cgnode) { - // Consider z = append(x, y). y is optional. - // This may allocate a new [1]T array; call its object w. - // We get the following constraints: - // z = x - // z = &w - // *z = *y - - x := instr.Call.Args[0] - - z := instr - a.copy(a.valueNode(z), a.valueNode(x), 1) // z = x - - if len(instr.Call.Args) == 1 { - return // no allocation for z = append(x) or _ = append(x). - } - - // TODO(adonovan): test append([]byte, ...string) []byte. - - y := instr.Call.Args[1] - tArray := sliceToArray(instr.Call.Args[0].Type()) - - var w nodeid - w = a.nextNode() - a.addNodes(tArray, "append") - a.endObject(w, cgn, instr) - - a.copyElems(cgn, tArray.Elem(), z, y) // *z = *y - a.addressOf(instr.Type(), a.valueNode(z), w) // z = &w -} - -// genBuiltinCall generates constraints for a call to a built-in. -func (a *analysis) genBuiltinCall(instr ssa.CallInstruction, cgn *cgnode) { - call := instr.Common() - switch call.Value.(*ssa.Builtin).Name() { - case "append": - // Safe cast: append cannot appear in a go or defer statement. - a.genAppend(instr.(*ssa.Call), cgn) - - case "copy": - tElem := call.Args[0].Type().Underlying().(*types.Slice).Elem() - a.copyElems(cgn, tElem, call.Args[0], call.Args[1]) - - case "panic": - a.copy(a.panicNode, a.valueNode(call.Args[0]), 1) - - case "recover": - if v := instr.Value(); v != nil { - a.copy(a.valueNode(v), a.panicNode, 1) - } - - case "print": - // In the tests, the probe might be the sole reference - // to its arg, so make sure we create nodes for it. - if len(call.Args) > 0 { - a.valueNode(call.Args[0]) - } - - case "ssa:wrapnilchk": - a.copy(a.valueNode(instr.Value()), a.valueNode(call.Args[0]), 1) - - default: - // No-ops: close len cap real imag complex print println delete. - } -} - -// shouldUseContext defines the context-sensitivity policy. It -// returns true if we should analyse all static calls to fn anew. -// -// Obviously this interface rather limits how much freedom we have to -// choose a policy. The current policy, rather arbitrarily, is true -// for intrinsics and accessor methods (actually: short, single-block, -// call-free functions). This is just a starting point. -// -func (a *analysis) shouldUseContext(fn *ssa.Function) bool { - if a.findIntrinsic(fn) != nil { - return true // treat intrinsics context-sensitively - } - if len(fn.Blocks) != 1 { - return false // too expensive - } - blk := fn.Blocks[0] - if len(blk.Instrs) > 10 { - return false // too expensive - } - if fn.Synthetic != "" && (fn.Pkg == nil || fn != fn.Pkg.Func("init")) { - return true // treat synthetic wrappers context-sensitively - } - for _, instr := range blk.Instrs { - switch instr := instr.(type) { - case ssa.CallInstruction: - // Disallow function calls (except to built-ins) - // because of the danger of unbounded recursion. - if _, ok := instr.Common().Value.(*ssa.Builtin); !ok { - return false - } - } - } - return true -} - -// genStaticCall generates constraints for a statically dispatched function call. -func (a *analysis) genStaticCall(caller *cgnode, site *callsite, call *ssa.CallCommon, result nodeid) { - fn := call.StaticCallee() - - // Special cases for inlined intrinsics. - switch fn { - case a.runtimeSetFinalizer: - // Inline SetFinalizer so the call appears direct. - site.targets = a.addOneNode(tInvalid, "SetFinalizer.targets", nil) - a.addConstraint(&runtimeSetFinalizerConstraint{ - targets: site.targets, - x: a.valueNode(call.Args[0]), - f: a.valueNode(call.Args[1]), - }) - return - - case a.reflectValueCall: - // Inline (reflect.Value).Call so the call appears direct. - dotdotdot := false - ret := reflectCallImpl(a, caller, site, a.valueNode(call.Args[0]), a.valueNode(call.Args[1]), dotdotdot) - if result != 0 { - a.addressOf(fn.Signature.Results().At(0).Type(), result, ret) - } - return - } - - // Ascertain the context (contour/cgnode) for a particular call. - var obj nodeid - if a.shouldUseContext(fn) { - obj = a.makeFunctionObject(fn, site) // new contour - } else { - obj = a.objectNode(nil, fn) // shared contour - } - a.callEdge(caller, site, obj) - - sig := call.Signature() - - // Copy receiver, if any. - params := a.funcParams(obj) - args := call.Args - if sig.Recv() != nil { - sz := a.sizeof(sig.Recv().Type()) - a.copy(params, a.valueNode(args[0]), sz) - params += nodeid(sz) - args = args[1:] - } - - // Copy actual parameters into formal params block. - // Must loop, since the actuals aren't contiguous. - for i, arg := range args { - sz := a.sizeof(sig.Params().At(i).Type()) - a.copy(params, a.valueNode(arg), sz) - params += nodeid(sz) - } - - // Copy formal results block to actual result. - if result != 0 { - a.copy(result, a.funcResults(obj), a.sizeof(sig.Results())) - } -} - -// genDynamicCall generates constraints for a dynamic function call. -func (a *analysis) genDynamicCall(caller *cgnode, site *callsite, call *ssa.CallCommon, result nodeid) { - // pts(targets) will be the set of possible call targets. - site.targets = a.valueNode(call.Value) - - // We add dynamic closure rules that store the arguments into - // the P-block and load the results from the R-block of each - // function discovered in pts(targets). - - sig := call.Signature() - var offset uint32 = 1 // P/R block starts at offset 1 - for i, arg := range call.Args { - sz := a.sizeof(sig.Params().At(i).Type()) - a.genStore(caller, call.Value, a.valueNode(arg), offset, sz) - offset += sz - } - if result != 0 { - a.genLoad(caller, result, call.Value, offset, a.sizeof(sig.Results())) - } -} - -// genInvoke generates constraints for a dynamic method invocation. -func (a *analysis) genInvoke(caller *cgnode, site *callsite, call *ssa.CallCommon, result nodeid) { - if call.Value.Type() == a.reflectType { - a.genInvokeReflectType(caller, site, call, result) - return - } - - sig := call.Signature() - - // Allocate a contiguous targets/params/results block for this call. - block := a.nextNode() - // pts(targets) will be the set of possible call targets - site.targets = a.addOneNode(sig, "invoke.targets", nil) - p := a.addNodes(sig.Params(), "invoke.params") - r := a.addNodes(sig.Results(), "invoke.results") - - // Copy the actual parameters into the call's params block. - for i, n := 0, sig.Params().Len(); i < n; i++ { - sz := a.sizeof(sig.Params().At(i).Type()) - a.copy(p, a.valueNode(call.Args[i]), sz) - p += nodeid(sz) - } - // Copy the call's results block to the actual results. - if result != 0 { - a.copy(result, r, a.sizeof(sig.Results())) - } - - // We add a dynamic invoke constraint that will connect the - // caller's and the callee's P/R blocks for each discovered - // call target. - a.addConstraint(&invokeConstraint{call.Method, a.valueNode(call.Value), block}) -} - -// genInvokeReflectType is a specialization of genInvoke where the -// receiver type is a reflect.Type, under the assumption that there -// can be at most one implementation of this interface, *reflect.rtype. -// -// (Though this may appear to be an instance of a pattern---method -// calls on interfaces known to have exactly one implementation---in -// practice it occurs rarely, so we special case for reflect.Type.) -// -// In effect we treat this: -// var rt reflect.Type = ... -// rt.F() -// as this: -// rt.(*reflect.rtype).F() -// -func (a *analysis) genInvokeReflectType(caller *cgnode, site *callsite, call *ssa.CallCommon, result nodeid) { - // Unpack receiver into rtype - rtype := a.addOneNode(a.reflectRtypePtr, "rtype.recv", nil) - recv := a.valueNode(call.Value) - a.typeAssert(a.reflectRtypePtr, rtype, recv, true) - - // Look up the concrete method. - fn := a.prog.LookupMethod(a.reflectRtypePtr, call.Method.Pkg(), call.Method.Name()) - - obj := a.makeFunctionObject(fn, site) // new contour for this call - a.callEdge(caller, site, obj) - - // From now on, it's essentially a static call, but little is - // gained by factoring together the code for both cases. - - sig := fn.Signature // concrete method - targets := a.addOneNode(sig, "call.targets", nil) - a.addressOf(sig, targets, obj) // (a singleton) - - // Copy receiver. - params := a.funcParams(obj) - a.copy(params, rtype, 1) - params++ - - // Copy actual parameters into formal P-block. - // Must loop, since the actuals aren't contiguous. - for i, arg := range call.Args { - sz := a.sizeof(sig.Params().At(i).Type()) - a.copy(params, a.valueNode(arg), sz) - params += nodeid(sz) - } - - // Copy formal R-block to actual R-block. - if result != 0 { - a.copy(result, a.funcResults(obj), a.sizeof(sig.Results())) - } -} - -// genCall generates constraints for call instruction instr. -func (a *analysis) genCall(caller *cgnode, instr ssa.CallInstruction) { - call := instr.Common() - - // Intrinsic implementations of built-in functions. - if _, ok := call.Value.(*ssa.Builtin); ok { - a.genBuiltinCall(instr, caller) - return - } - - var result nodeid - if v := instr.Value(); v != nil { - result = a.valueNode(v) - } - - site := &callsite{instr: instr} - if call.StaticCallee() != nil { - a.genStaticCall(caller, site, call, result) - } else if call.IsInvoke() { - a.genInvoke(caller, site, call, result) - } else { - a.genDynamicCall(caller, site, call, result) - } - - caller.sites = append(caller.sites, site) - - if a.log != nil { - // TODO(adonovan): debug: improve log message. - fmt.Fprintf(a.log, "\t%s to targets %s from %s\n", site, site.targets, caller) - } -} - -// objectNode returns the object to which v points, if known. -// In other words, if the points-to set of v is a singleton, it -// returns the sole label, zero otherwise. -// -// We exploit this information to make the generated constraints less -// dynamic. For example, a complex load constraint can be replaced by -// a simple copy constraint when the sole destination is known a priori. -// -// Some SSA instructions always have singletons points-to sets: -// Alloc, Function, Global, MakeChan, MakeClosure, MakeInterface, MakeMap, MakeSlice. -// Others may be singletons depending on their operands: -// FreeVar, Const, Convert, FieldAddr, IndexAddr, Slice. -// -// Idempotent. Objects are created as needed, possibly via recursion -// down the SSA value graph, e.g IndexAddr(FieldAddr(Alloc))). -// -func (a *analysis) objectNode(cgn *cgnode, v ssa.Value) nodeid { - switch v.(type) { - case *ssa.Global, *ssa.Function, *ssa.Const, *ssa.FreeVar: - // Global object. - obj, ok := a.globalobj[v] - if !ok { - switch v := v.(type) { - case *ssa.Global: - obj = a.nextNode() - a.addNodes(mustDeref(v.Type()), "global") - a.endObject(obj, nil, v) - - case *ssa.Function: - obj = a.makeFunctionObject(v, nil) - - case *ssa.Const: - // not addressable - - case *ssa.FreeVar: - // not addressable - } - - if a.log != nil { - fmt.Fprintf(a.log, "\tglobalobj[%s] = n%d\n", v, obj) - } - a.globalobj[v] = obj - } - return obj - } - - // Local object. - obj, ok := a.localobj[v] - if !ok { - switch v := v.(type) { - case *ssa.Alloc: - obj = a.nextNode() - a.addNodes(mustDeref(v.Type()), "alloc") - a.endObject(obj, cgn, v) - - case *ssa.MakeSlice: - obj = a.nextNode() - a.addNodes(sliceToArray(v.Type()), "makeslice") - a.endObject(obj, cgn, v) - - case *ssa.MakeChan: - obj = a.nextNode() - a.addNodes(v.Type().Underlying().(*types.Chan).Elem(), "makechan") - a.endObject(obj, cgn, v) - - case *ssa.MakeMap: - obj = a.nextNode() - tmap := v.Type().Underlying().(*types.Map) - a.addNodes(tmap.Key(), "makemap.key") - elem := a.addNodes(tmap.Elem(), "makemap.value") - - // To update the value field, MapUpdate - // generates store-with-offset constraints which - // the presolver can't model, so we must mark - // those nodes indirect. - for id, end := elem, elem+nodeid(a.sizeof(tmap.Elem())); id < end; id++ { - a.mapValues = append(a.mapValues, id) - } - a.endObject(obj, cgn, v) - - case *ssa.MakeInterface: - tConc := v.X.Type() - obj = a.makeTagged(tConc, cgn, v) - - // Copy the value into it, if nontrivial. - if x := a.valueNode(v.X); x != 0 { - a.copy(obj+1, x, a.sizeof(tConc)) - } - - case *ssa.FieldAddr: - if xobj := a.objectNode(cgn, v.X); xobj != 0 { - obj = xobj + nodeid(a.offsetOf(mustDeref(v.X.Type()), v.Field)) - } - - case *ssa.IndexAddr: - if xobj := a.objectNode(cgn, v.X); xobj != 0 { - obj = xobj + 1 - } - - case *ssa.Slice: - obj = a.objectNode(cgn, v.X) - - case *ssa.Convert: - // TODO(adonovan): opt: handle these cases too: - // - unsafe.Pointer->*T conversion acts like Alloc - // - string->[]byte/[]rune conversion acts like MakeSlice - } - - if a.log != nil { - fmt.Fprintf(a.log, "\tlocalobj[%s] = n%d\n", v.Name(), obj) - } - a.localobj[v] = obj - } - return obj -} - -// genLoad generates constraints for result = *(ptr + val). -func (a *analysis) genLoad(cgn *cgnode, result nodeid, ptr ssa.Value, offset, sizeof uint32) { - if obj := a.objectNode(cgn, ptr); obj != 0 { - // Pre-apply loadConstraint.solve(). - a.copy(result, obj+nodeid(offset), sizeof) - } else { - a.load(result, a.valueNode(ptr), offset, sizeof) - } -} - -// genOffsetAddr generates constraints for a 'v=ptr.field' (FieldAddr) -// or 'v=ptr[*]' (IndexAddr) instruction v. -func (a *analysis) genOffsetAddr(cgn *cgnode, v ssa.Value, ptr nodeid, offset uint32) { - dst := a.valueNode(v) - if obj := a.objectNode(cgn, v); obj != 0 { - // Pre-apply offsetAddrConstraint.solve(). - a.addressOf(v.Type(), dst, obj) - } else { - a.offsetAddr(v.Type(), dst, ptr, offset) - } -} - -// genStore generates constraints for *(ptr + offset) = val. -func (a *analysis) genStore(cgn *cgnode, ptr ssa.Value, val nodeid, offset, sizeof uint32) { - if obj := a.objectNode(cgn, ptr); obj != 0 { - // Pre-apply storeConstraint.solve(). - a.copy(obj+nodeid(offset), val, sizeof) - } else { - a.store(a.valueNode(ptr), val, offset, sizeof) - } -} - -// genInstr generates constraints for instruction instr in context cgn. -func (a *analysis) genInstr(cgn *cgnode, instr ssa.Instruction) { - if a.log != nil { - var prefix string - if val, ok := instr.(ssa.Value); ok { - prefix = val.Name() + " = " - } - fmt.Fprintf(a.log, "; %s%s\n", prefix, instr) - } - - switch instr := instr.(type) { - case *ssa.DebugRef: - // no-op. - - case *ssa.UnOp: - switch instr.Op { - case token.ARROW: // <-x - // We can ignore instr.CommaOk because the node we're - // altering is always at zero offset relative to instr - tElem := instr.X.Type().Underlying().(*types.Chan).Elem() - a.genLoad(cgn, a.valueNode(instr), instr.X, 0, a.sizeof(tElem)) - - case token.MUL: // *x - a.genLoad(cgn, a.valueNode(instr), instr.X, 0, a.sizeof(instr.Type())) - - default: - // NOT, SUB, XOR: no-op. - } - - case *ssa.BinOp: - // All no-ops. - - case ssa.CallInstruction: // *ssa.Call, *ssa.Go, *ssa.Defer - a.genCall(cgn, instr) - - case *ssa.ChangeType: - a.copy(a.valueNode(instr), a.valueNode(instr.X), 1) - - case *ssa.Convert: - a.genConv(instr, cgn) - - case *ssa.Extract: - a.copy(a.valueNode(instr), - a.valueOffsetNode(instr.Tuple, instr.Index), - a.sizeof(instr.Type())) - - case *ssa.FieldAddr: - a.genOffsetAddr(cgn, instr, a.valueNode(instr.X), - a.offsetOf(mustDeref(instr.X.Type()), instr.Field)) - - case *ssa.IndexAddr: - a.genOffsetAddr(cgn, instr, a.valueNode(instr.X), 1) - - case *ssa.Field: - a.copy(a.valueNode(instr), - a.valueOffsetNode(instr.X, instr.Field), - a.sizeof(instr.Type())) - - case *ssa.Index: - a.copy(a.valueNode(instr), 1+a.valueNode(instr.X), a.sizeof(instr.Type())) - - case *ssa.Select: - recv := a.valueOffsetNode(instr, 2) // instr : (index, recvOk, recv0, ... recv_n-1) - for _, st := range instr.States { - elemSize := a.sizeof(st.Chan.Type().Underlying().(*types.Chan).Elem()) - switch st.Dir { - case types.RecvOnly: - a.genLoad(cgn, recv, st.Chan, 0, elemSize) - recv += nodeid(elemSize) - - case types.SendOnly: - a.genStore(cgn, st.Chan, a.valueNode(st.Send), 0, elemSize) - } - } - - case *ssa.Return: - results := a.funcResults(cgn.obj) - for _, r := range instr.Results { - sz := a.sizeof(r.Type()) - a.copy(results, a.valueNode(r), sz) - results += nodeid(sz) - } - - case *ssa.Send: - a.genStore(cgn, instr.Chan, a.valueNode(instr.X), 0, a.sizeof(instr.X.Type())) - - case *ssa.Store: - a.genStore(cgn, instr.Addr, a.valueNode(instr.Val), 0, a.sizeof(instr.Val.Type())) - - case *ssa.Alloc, *ssa.MakeSlice, *ssa.MakeChan, *ssa.MakeMap, *ssa.MakeInterface: - v := instr.(ssa.Value) - a.addressOf(v.Type(), a.valueNode(v), a.objectNode(cgn, v)) - - case *ssa.ChangeInterface: - a.copy(a.valueNode(instr), a.valueNode(instr.X), 1) - - case *ssa.TypeAssert: - a.typeAssert(instr.AssertedType, a.valueNode(instr), a.valueNode(instr.X), true) - - case *ssa.Slice: - a.copy(a.valueNode(instr), a.valueNode(instr.X), 1) - - case *ssa.If, *ssa.Jump: - // no-op. - - case *ssa.Phi: - sz := a.sizeof(instr.Type()) - for _, e := range instr.Edges { - a.copy(a.valueNode(instr), a.valueNode(e), sz) - } - - case *ssa.MakeClosure: - fn := instr.Fn.(*ssa.Function) - a.copy(a.valueNode(instr), a.valueNode(fn), 1) - // Free variables are treated like global variables. - for i, b := range instr.Bindings { - a.copy(a.valueNode(fn.FreeVars[i]), a.valueNode(b), a.sizeof(b.Type())) - } - - case *ssa.RunDefers: - // The analysis is flow insensitive, so we just "call" - // defers as we encounter them. - - case *ssa.Range: - // Do nothing. Next{Iter: *ssa.Range} handles this case. - - case *ssa.Next: - if !instr.IsString { // map - // Assumes that Next is always directly applied to a Range result. - theMap := instr.Iter.(*ssa.Range).X - tMap := theMap.Type().Underlying().(*types.Map) - - ksize := a.sizeof(tMap.Key()) - vsize := a.sizeof(tMap.Elem()) - - // The k/v components of the Next tuple may each be invalid. - tTuple := instr.Type().(*types.Tuple) - - // Load from the map's (k,v) into the tuple's (ok, k, v). - osrc := uint32(0) // offset within map object - odst := uint32(1) // offset within tuple (initially just after 'ok bool') - sz := uint32(0) // amount to copy - - // Is key valid? - if tTuple.At(1).Type() != tInvalid { - sz += ksize - } else { - odst += ksize - osrc += ksize - } - - // Is value valid? - if tTuple.At(2).Type() != tInvalid { - sz += vsize - } - - a.genLoad(cgn, a.valueNode(instr)+nodeid(odst), theMap, osrc, sz) - } - - case *ssa.Lookup: - if tMap, ok := instr.X.Type().Underlying().(*types.Map); ok { - // CommaOk can be ignored: field 0 is a no-op. - ksize := a.sizeof(tMap.Key()) - vsize := a.sizeof(tMap.Elem()) - a.genLoad(cgn, a.valueNode(instr), instr.X, ksize, vsize) - } - - case *ssa.MapUpdate: - tmap := instr.Map.Type().Underlying().(*types.Map) - ksize := a.sizeof(tmap.Key()) - vsize := a.sizeof(tmap.Elem()) - a.genStore(cgn, instr.Map, a.valueNode(instr.Key), 0, ksize) - a.genStore(cgn, instr.Map, a.valueNode(instr.Value), ksize, vsize) - - case *ssa.Panic: - a.copy(a.panicNode, a.valueNode(instr.X), 1) - - default: - panic(fmt.Sprintf("unimplemented: %T", instr)) - } -} - -func (a *analysis) makeCGNode(fn *ssa.Function, obj nodeid, callersite *callsite) *cgnode { - cgn := &cgnode{fn: fn, obj: obj, callersite: callersite} - a.cgnodes = append(a.cgnodes, cgn) - return cgn -} - -// genRootCalls generates the synthetic root of the callgraph and the -// initial calls from it to the analysis scope, such as main, a test -// or a library. -// -func (a *analysis) genRootCalls() *cgnode { - r := a.prog.NewFunction("", new(types.Signature), "root of callgraph") - root := a.makeCGNode(r, 0, nil) - - // TODO(adonovan): make an ssa utility to construct an actual - // root function so we don't need to special-case site-less - // call edges. - - // For each main package, call main.init(), main.main(). - for _, mainPkg := range a.config.Mains { - main := mainPkg.Func("main") - if main == nil { - panic(fmt.Sprintf("%s has no main function", mainPkg)) - } - - targets := a.addOneNode(main.Signature, "root.targets", nil) - site := &callsite{targets: targets} - root.sites = append(root.sites, site) - for _, fn := range [2]*ssa.Function{mainPkg.Func("init"), main} { - if a.log != nil { - fmt.Fprintf(a.log, "\troot call to %s:\n", fn) - } - a.copy(targets, a.valueNode(fn), 1) - } - } - - return root -} - -// genFunc generates constraints for function fn. -func (a *analysis) genFunc(cgn *cgnode) { - fn := cgn.fn - - impl := a.findIntrinsic(fn) - - if a.log != nil { - fmt.Fprintf(a.log, "\n\n==== Generating constraints for %s, %s\n", cgn, cgn.contour()) - - // Hack: don't display body if intrinsic. - if impl != nil { - fn2 := *cgn.fn // copy - fn2.Locals = nil - fn2.Blocks = nil - fn2.WriteTo(a.log) - } else { - cgn.fn.WriteTo(a.log) - } - } - - if impl != nil { - impl(a, cgn) - return - } - - if fn.Blocks == nil { - // External function with no intrinsic treatment. - // We'll warn about calls to such functions at the end. - return - } - - if a.log != nil { - fmt.Fprintln(a.log, "; Creating nodes for local values") - } - - a.localval = make(map[ssa.Value]nodeid) - a.localobj = make(map[ssa.Value]nodeid) - - // The value nodes for the params are in the func object block. - params := a.funcParams(cgn.obj) - for _, p := range fn.Params { - a.setValueNode(p, params, cgn) - params += nodeid(a.sizeof(p.Type())) - } - - // Free variables have global cardinality: - // the outer function sets them with MakeClosure; - // the inner function accesses them with FreeVar. - // - // TODO(adonovan): treat free vars context-sensitively. - - // Create value nodes for all value instructions - // since SSA may contain forward references. - var space [10]*ssa.Value - for _, b := range fn.Blocks { - for _, instr := range b.Instrs { - switch instr := instr.(type) { - case *ssa.Range: - // do nothing: it has a funky type, - // and *ssa.Next does all the work. - - case ssa.Value: - var comment string - if a.log != nil { - comment = instr.Name() - } - id := a.addNodes(instr.Type(), comment) - a.setValueNode(instr, id, cgn) - } - - // Record all address-taken functions (for presolver). - rands := instr.Operands(space[:0]) - if call, ok := instr.(ssa.CallInstruction); ok && !call.Common().IsInvoke() { - // Skip CallCommon.Value in "call" mode. - // TODO(adonovan): fix: relies on unspecified ordering. Specify it. - rands = rands[1:] - } - for _, rand := range rands { - if atf, ok := (*rand).(*ssa.Function); ok { - a.atFuncs[atf] = true - } - } - } - } - - // Generate constraints for instructions. - for _, b := range fn.Blocks { - for _, instr := range b.Instrs { - a.genInstr(cgn, instr) - } - } - - a.localval = nil - a.localobj = nil -} - -// genMethodsOf generates nodes and constraints for all methods of type T. -func (a *analysis) genMethodsOf(T types.Type) { - itf := isInterface(T) - - // TODO(adonovan): can we skip this entirely if itf is true? - // I think so, but the answer may depend on reflection. - mset := a.prog.MethodSets.MethodSet(T) - for i, n := 0, mset.Len(); i < n; i++ { - m := a.prog.MethodValue(mset.At(i)) - a.valueNode(m) - - if !itf { - // Methods of concrete types are address-taken functions. - a.atFuncs[m] = true - } - } -} - -// generate generates offline constraints for the entire program. -func (a *analysis) generate() { - start("Constraint generation") - if a.log != nil { - fmt.Fprintln(a.log, "==== Generating constraints") - } - - // Create a dummy node since we use the nodeid 0 for - // non-pointerlike variables. - a.addNodes(tInvalid, "(zero)") - - // Create the global node for panic values. - a.panicNode = a.addNodes(tEface, "panic") - - // Create nodes and constraints for all methods of reflect.rtype. - // (Shared contours are used by dynamic calls to reflect.Type - // methods---typically just String().) - if rtype := a.reflectRtypePtr; rtype != nil { - a.genMethodsOf(rtype) - } - - root := a.genRootCalls() - - if a.config.BuildCallGraph { - a.result.CallGraph = callgraph.New(root.fn) - } - - // Create nodes and constraints for all methods of all types - // that are dynamically accessible via reflection or interfaces. - for _, T := range a.prog.RuntimeTypes() { - a.genMethodsOf(T) - } - - // Generate constraints for functions as they become reachable - // from the roots. (No constraints are generated for functions - // that are dead in this analysis scope.) - for len(a.genq) > 0 { - cgn := a.genq[0] - a.genq = a.genq[1:] - a.genFunc(cgn) - } - - // The runtime magically allocates os.Args; so should we. - if os := a.prog.ImportedPackage("os"); os != nil { - // In effect: os.Args = new([1]string)[:] - T := types.NewSlice(types.Typ[types.String]) - obj := a.addNodes(sliceToArray(T), "") - a.endObject(obj, nil, "") - a.addressOf(T, a.objectNode(nil, os.Var("Args")), obj) - } - - // Discard generation state, to avoid confusion after node renumbering. - a.panicNode = 0 - a.globalval = nil - a.localval = nil - a.localobj = nil - - stop("Constraint generation") -} diff --git a/vendor/golang.org/x/tools/go/pointer/hvn.go b/vendor/golang.org/x/tools/go/pointer/hvn.go deleted file mode 100644 index 192e4050ddb..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/hvn.go +++ /dev/null @@ -1,973 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -// This file implements Hash-Value Numbering (HVN), a pre-solver -// constraint optimization described in Hardekopf & Lin, SAS'07 (see -// doc.go) that analyses the graph topology to determine which sets of -// variables are "pointer equivalent" (PE), i.e. must have identical -// points-to sets in the solution. -// -// A separate ("offline") graph is constructed. Its nodes are those of -// the main-graph, plus an additional node *X for each pointer node X. -// With this graph we can reason about the unknown points-to set of -// dereferenced pointers. (We do not generalize this to represent -// unknown fields x->f, perhaps because such fields would be numerous, -// though it might be worth an experiment.) -// -// Nodes whose points-to relations are not entirely captured by the -// graph are marked as "indirect": the *X nodes, the parameters of -// address-taken functions (which includes all functions in method -// sets), or nodes updated by the solver rules for reflection, etc. -// -// All addr (y=&x) nodes are initially assigned a pointer-equivalence -// (PE) label equal to x's nodeid in the main graph. (These are the -// only PE labels that are less than len(a.nodes).) -// -// All offsetAddr (y=&x.f) constraints are initially assigned a PE -// label; such labels are memoized, keyed by (x, f), so that equivalent -// nodes y as assigned the same label. -// -// Then we process each strongly connected component (SCC) of the graph -// in topological order, assigning it a PE label based on the set P of -// PE labels that flow to it from its immediate dependencies. -// -// If any node in P is "indirect", the entire SCC is assigned a fresh PE -// label. Otherwise: -// -// |P|=0 if P is empty, all nodes in the SCC are non-pointers (e.g. -// uninitialized variables, or formal params of dead functions) -// and the SCC is assigned the PE label of zero. -// -// |P|=1 if P is a singleton, the SCC is assigned the same label as the -// sole element of P. -// -// |P|>1 if P contains multiple labels, a unique label representing P is -// invented and recorded in an hash table, so that other -// equivalent SCCs may also be assigned this label, akin to -// conventional hash-value numbering in a compiler. -// -// Finally, a renumbering is computed such that each node is replaced by -// the lowest-numbered node with the same PE label. All constraints are -// renumbered, and any resulting duplicates are eliminated. -// -// The only nodes that are not renumbered are the objects x in addr -// (y=&x) constraints, since the ids of these nodes (and fields derived -// from them via offsetAddr rules) are the elements of all points-to -// sets, so they must remain as they are if we want the same solution. -// -// The solverStates (node.solve) for nodes in the same equivalence class -// are linked together so that all nodes in the class have the same -// solution. This avoids the need to renumber nodeids buried in -// Queries, cgnodes, etc (like (*analysis).renumber() does) since only -// the solution is needed. -// -// The result of HVN is that the number of distinct nodes and -// constraints is reduced, but the solution is identical (almost---see -// CROSS-CHECK below). In particular, both linear and cyclic chains of -// copies are each replaced by a single node. -// -// Nodes and constraints created "online" (e.g. while solving reflection -// constraints) are not subject to this optimization. -// -// PERFORMANCE -// -// In two benchmarks (guru and godoc), HVN eliminates about two thirds -// of nodes, the majority accounted for by non-pointers: nodes of -// non-pointer type, pointers that remain nil, formal parameters of dead -// functions, nodes of untracked types, etc. It also reduces the number -// of constraints, also by about two thirds, and the solving time by -// 30--42%, although we must pay about 15% for the running time of HVN -// itself. The benefit is greater for larger applications. -// -// There are many possible optimizations to improve the performance: -// * Use fewer than 1:1 onodes to main graph nodes: many of the onodes -// we create are not needed. -// * HU (HVN with Union---see paper): coalesce "union" peLabels when -// their expanded-out sets are equal. -// * HR (HVN with deReference---see paper): this will require that we -// apply HVN until fixed point, which may need more bookkeeping of the -// correspondence of main nodes to onodes. -// * Location Equivalence (see paper): have points-to sets contain not -// locations but location-equivalence class labels, each representing -// a set of locations. -// * HVN with field-sensitive ref: model each of the fields of a -// pointer-to-struct. -// -// CROSS-CHECK -// -// To verify the soundness of the optimization, when the -// debugHVNCrossCheck option is enabled, we run the solver twice, once -// before and once after running HVN, dumping the solution to disk, and -// then we compare the results. If they are not identical, the analysis -// panics. -// -// The solution dumped to disk includes only the N*N submatrix of the -// complete solution where N is the number of nodes after generation. -// In other words, we ignore pointer variables and objects created by -// the solver itself, since their numbering depends on the solver order, -// which is affected by the optimization. In any case, that's the only -// part the client cares about. -// -// The cross-check is too strict and may fail spuriously. Although the -// H&L paper describing HVN states that the solutions obtained should be -// identical, this is not the case in practice because HVN can collapse -// cycles involving *p even when pts(p)={}. Consider this example -// distilled from testdata/hello.go: -// -// var x T -// func f(p **T) { -// t0 = *p -// ... -// t1 = φ(t0, &x) -// *p = t1 -// } -// -// If f is dead code, we get: -// unoptimized: pts(p)={} pts(t0)={} pts(t1)={&x} -// optimized: pts(p)={} pts(t0)=pts(t1)=pts(*p)={&x} -// -// It's hard to argue that this is a bug: the result is sound and the -// loss of precision is inconsequential---f is dead code, after all. -// But unfortunately it limits the usefulness of the cross-check since -// failures must be carefully analyzed. Ben Hardekopf suggests (in -// personal correspondence) some approaches to mitigating it: -// -// If there is a node with an HVN points-to set that is a superset -// of the NORM points-to set, then either it's a bug or it's a -// result of this issue. If it's a result of this issue, then in -// the offline constraint graph there should be a REF node inside -// some cycle that reaches this node, and in the NORM solution the -// pointer being dereferenced by that REF node should be the empty -// set. If that isn't true then this is a bug. If it is true, then -// you can further check that in the NORM solution the "extra" -// points-to info in the HVN solution does in fact come from that -// purported cycle (if it doesn't, then this is still a bug). If -// you're doing the further check then you'll need to do it for -// each "extra" points-to element in the HVN points-to set. -// -// There are probably ways to optimize these checks by taking -// advantage of graph properties. For example, extraneous points-to -// info will flow through the graph and end up in many -// nodes. Rather than checking every node with extra info, you -// could probably work out the "origin point" of the extra info and -// just check there. Note that the check in the first bullet is -// looking for soundness bugs, while the check in the second bullet -// is looking for precision bugs; depending on your needs, you may -// care more about one than the other. -// -// which we should evaluate. The cross-check is nonetheless invaluable -// for all but one of the programs in the pointer_test suite. - -import ( - "fmt" - "go/types" - "io" - "reflect" - - "golang.org/x/tools/container/intsets" -) - -// A peLabel is a pointer-equivalence label: two nodes with the same -// peLabel have identical points-to solutions. -// -// The numbers are allocated consecutively like so: -// 0 not a pointer -// 1..N-1 addrConstraints (equals the constraint's .src field, hence sparse) -// ... offsetAddr constraints -// ... SCCs (with indirect nodes or multiple inputs) -// -// Each PE label denotes a set of pointers containing a single addr, a -// single offsetAddr, or some set of other PE labels. -// -type peLabel int - -type hvn struct { - a *analysis - N int // len(a.nodes) immediately after constraint generation - log io.Writer // (optional) log of HVN lemmas - onodes []*onode // nodes of the offline graph - label peLabel // the next available PE label - hvnLabel map[string]peLabel // hash-value numbering (PE label) for each set of onodeids - stack []onodeid // DFS stack - index int32 // next onode.index, from Tarjan's SCC algorithm - - // For each distinct offsetAddrConstraint (src, offset) pair, - // offsetAddrLabels records a unique PE label >= N. - offsetAddrLabels map[offsetAddr]peLabel -} - -// The index of an node in the offline graph. -// (Currently the first N align with the main nodes, -// but this may change with HRU.) -type onodeid uint32 - -// An onode is a node in the offline constraint graph. -// (Where ambiguous, members of analysis.nodes are referred to as -// "main graph" nodes.) -// -// Edges in the offline constraint graph (edges and implicit) point to -// the source, i.e. against the flow of values: they are dependencies. -// Implicit edges are used for SCC computation, but not for gathering -// incoming labels. -// -type onode struct { - rep onodeid // index of representative of SCC in offline constraint graph - - edges intsets.Sparse // constraint edges X-->Y (this onode is X) - implicit intsets.Sparse // implicit edges *X-->*Y (this onode is X) - peLabels intsets.Sparse // set of peLabels are pointer-equivalent to this one - indirect bool // node has points-to relations not represented in graph - - // Tarjan's SCC algorithm - index, lowlink int32 // Tarjan numbering - scc int32 // -ve => on stack; 0 => unvisited; +ve => node is root of a found SCC -} - -type offsetAddr struct { - ptr nodeid - offset uint32 -} - -// nextLabel issues the next unused pointer-equivalence label. -func (h *hvn) nextLabel() peLabel { - h.label++ - return h.label -} - -// ref(X) returns the index of the onode for *X. -func (h *hvn) ref(id onodeid) onodeid { - return id + onodeid(len(h.a.nodes)) -} - -// hvn computes pointer-equivalence labels (peLabels) using the Hash-based -// Value Numbering (HVN) algorithm described in Hardekopf & Lin, SAS'07. -// -func (a *analysis) hvn() { - start("HVN") - - if a.log != nil { - fmt.Fprintf(a.log, "\n\n==== Pointer equivalence optimization\n\n") - } - - h := hvn{ - a: a, - N: len(a.nodes), - log: a.log, - hvnLabel: make(map[string]peLabel), - offsetAddrLabels: make(map[offsetAddr]peLabel), - } - - if h.log != nil { - fmt.Fprintf(h.log, "\nCreating offline graph nodes...\n") - } - - // Create offline nodes. The first N nodes correspond to main - // graph nodes; the next N are their corresponding ref() nodes. - h.onodes = make([]*onode, 2*h.N) - for id := range a.nodes { - id := onodeid(id) - h.onodes[id] = &onode{} - h.onodes[h.ref(id)] = &onode{indirect: true} - } - - // Each node initially represents just itself. - for id, o := range h.onodes { - o.rep = onodeid(id) - } - - h.markIndirectNodes() - - // Reserve the first N PE labels for addrConstraints. - h.label = peLabel(h.N) - - // Add offline constraint edges. - if h.log != nil { - fmt.Fprintf(h.log, "\nAdding offline graph edges...\n") - } - for _, c := range a.constraints { - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "; %s\n", c) - } - c.presolve(&h) - } - - // Find and collapse SCCs. - if h.log != nil { - fmt.Fprintf(h.log, "\nFinding SCCs...\n") - } - h.index = 1 - for id, o := range h.onodes { - if id > 0 && o.index == 0 { - // Start depth-first search at each unvisited node. - h.visit(onodeid(id)) - } - } - - // Dump the solution - // (NB: somewhat redundant with logging from simplify().) - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\nPointer equivalences:\n") - for id, o := range h.onodes { - if id == 0 { - continue - } - if id == int(h.N) { - fmt.Fprintf(h.log, "---\n") - } - fmt.Fprintf(h.log, "o%d\t", id) - if o.rep != onodeid(id) { - fmt.Fprintf(h.log, "rep=o%d", o.rep) - } else { - fmt.Fprintf(h.log, "p%d", o.peLabels.Min()) - if o.indirect { - fmt.Fprint(h.log, " indirect") - } - } - fmt.Fprintln(h.log) - } - } - - // Simplify the main constraint graph - h.simplify() - - a.showCounts() - - stop("HVN") -} - -// ---- constraint-specific rules ---- - -// dst := &src -func (c *addrConstraint) presolve(h *hvn) { - // Each object (src) is an initial PE label. - label := peLabel(c.src) // label < N - if debugHVNVerbose && h.log != nil { - // duplicate log messages are possible - fmt.Fprintf(h.log, "\tcreate p%d: {&n%d}\n", label, c.src) - } - odst := onodeid(c.dst) - osrc := onodeid(c.src) - - // Assign dst this label. - h.onodes[odst].peLabels.Insert(int(label)) - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\to%d has p%d\n", odst, label) - } - - h.addImplicitEdge(h.ref(odst), osrc) // *dst ~~> src. -} - -// dst = src -func (c *copyConstraint) presolve(h *hvn) { - odst := onodeid(c.dst) - osrc := onodeid(c.src) - h.addEdge(odst, osrc) // dst --> src - h.addImplicitEdge(h.ref(odst), h.ref(osrc)) // *dst ~~> *src -} - -// dst = *src + offset -func (c *loadConstraint) presolve(h *hvn) { - odst := onodeid(c.dst) - osrc := onodeid(c.src) - if c.offset == 0 { - h.addEdge(odst, h.ref(osrc)) // dst --> *src - } else { - // We don't interpret load-with-offset, e.g. results - // of map value lookup, R-block of dynamic call, slice - // copy/append, reflection. - h.markIndirect(odst, "load with offset") - } -} - -// *dst + offset = src -func (c *storeConstraint) presolve(h *hvn) { - odst := onodeid(c.dst) - osrc := onodeid(c.src) - if c.offset == 0 { - h.onodes[h.ref(odst)].edges.Insert(int(osrc)) // *dst --> src - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\to%d --> o%d\n", h.ref(odst), osrc) - } - } else { - // We don't interpret store-with-offset. - // See discussion of soundness at markIndirectNodes. - } -} - -// dst = &src.offset -func (c *offsetAddrConstraint) presolve(h *hvn) { - // Give each distinct (addr, offset) pair a fresh PE label. - // The cache performs CSE, effectively. - key := offsetAddr{c.src, c.offset} - label, ok := h.offsetAddrLabels[key] - if !ok { - label = h.nextLabel() - h.offsetAddrLabels[key] = label - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\tcreate p%d: {&n%d.#%d}\n", - label, c.src, c.offset) - } - } - - // Assign dst this label. - h.onodes[c.dst].peLabels.Insert(int(label)) - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\to%d has p%d\n", c.dst, label) - } -} - -// dst = src.(typ) where typ is an interface -func (c *typeFilterConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.dst), "typeFilter result") -} - -// dst = src.(typ) where typ is concrete -func (c *untagConstraint) presolve(h *hvn) { - odst := onodeid(c.dst) - for end := odst + onodeid(h.a.sizeof(c.typ)); odst < end; odst++ { - h.markIndirect(odst, "untag result") - } -} - -// dst = src.method(c.params...) -func (c *invokeConstraint) presolve(h *hvn) { - // All methods are address-taken functions, so - // their formal P-blocks were already marked indirect. - - // Mark the caller's targets node as indirect. - sig := c.method.Type().(*types.Signature) - id := c.params - h.markIndirect(onodeid(c.params), "invoke targets node") - id++ - - id += nodeid(h.a.sizeof(sig.Params())) - - // Mark the caller's R-block as indirect. - end := id + nodeid(h.a.sizeof(sig.Results())) - for id < end { - h.markIndirect(onodeid(id), "invoke R-block") - id++ - } -} - -// markIndirectNodes marks as indirect nodes whose points-to relations -// are not entirely captured by the offline graph, including: -// -// (a) All address-taken nodes (including the following nodes within -// the same object). This is described in the paper. -// -// The most subtle cause of indirect nodes is the generation of -// store-with-offset constraints since the offline graph doesn't -// represent them. A global audit of constraint generation reveals the -// following uses of store-with-offset: -// -// (b) genDynamicCall, for P-blocks of dynamically called functions, -// to which dynamic copy edges will be added to them during -// solving: from storeConstraint for standalone functions, -// and from invokeConstraint for methods. -// All such P-blocks must be marked indirect. -// (c) MakeUpdate, to update the value part of a map object. -// All MakeMap objects's value parts must be marked indirect. -// (d) copyElems, to update the destination array. -// All array elements must be marked indirect. -// -// Not all indirect marking happens here. ref() nodes are marked -// indirect at construction, and each constraint's presolve() method may -// mark additional nodes. -// -func (h *hvn) markIndirectNodes() { - // (a) all address-taken nodes, plus all nodes following them - // within the same object, since these may be indirectly - // stored or address-taken. - for _, c := range h.a.constraints { - if c, ok := c.(*addrConstraint); ok { - start := h.a.enclosingObj(c.src) - end := start + nodeid(h.a.nodes[start].obj.size) - for id := c.src; id < end; id++ { - h.markIndirect(onodeid(id), "A-T object") - } - } - } - - // (b) P-blocks of all address-taken functions. - for id := 0; id < h.N; id++ { - obj := h.a.nodes[id].obj - - // TODO(adonovan): opt: if obj.cgn.fn is a method and - // obj.cgn is not its shared contour, this is an - // "inlined" static method call. We needn't consider it - // address-taken since no invokeConstraint will affect it. - - if obj != nil && obj.flags&otFunction != 0 && h.a.atFuncs[obj.cgn.fn] { - // address-taken function - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "n%d is address-taken: %s\n", id, obj.cgn.fn) - } - h.markIndirect(onodeid(id), "A-T func identity") - id++ - sig := obj.cgn.fn.Signature - psize := h.a.sizeof(sig.Params()) - if sig.Recv() != nil { - psize += h.a.sizeof(sig.Recv().Type()) - } - for end := id + int(psize); id < end; id++ { - h.markIndirect(onodeid(id), "A-T func P-block") - } - id-- - continue - } - } - - // (c) all map objects' value fields. - for _, id := range h.a.mapValues { - h.markIndirect(onodeid(id), "makemap.value") - } - - // (d) all array element objects. - // TODO(adonovan): opt: can we do better? - for id := 0; id < h.N; id++ { - // Identity node for an object of array type? - if tArray, ok := h.a.nodes[id].typ.(*types.Array); ok { - // Mark the array element nodes indirect. - // (Skip past the identity field.) - for range h.a.flatten(tArray.Elem()) { - id++ - h.markIndirect(onodeid(id), "array elem") - } - } - } -} - -func (h *hvn) markIndirect(oid onodeid, comment string) { - h.onodes[oid].indirect = true - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\to%d is indirect: %s\n", oid, comment) - } -} - -// Adds an edge dst-->src. -// Note the unusual convention: edges are dependency (contraflow) edges. -func (h *hvn) addEdge(odst, osrc onodeid) { - h.onodes[odst].edges.Insert(int(osrc)) - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\to%d --> o%d\n", odst, osrc) - } -} - -func (h *hvn) addImplicitEdge(odst, osrc onodeid) { - h.onodes[odst].implicit.Insert(int(osrc)) - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\to%d ~~> o%d\n", odst, osrc) - } -} - -// visit implements the depth-first search of Tarjan's SCC algorithm. -// Precondition: x is canonical. -func (h *hvn) visit(x onodeid) { - h.checkCanonical(x) - xo := h.onodes[x] - xo.index = h.index - xo.lowlink = h.index - h.index++ - - h.stack = append(h.stack, x) // push - assert(xo.scc == 0, "node revisited") - xo.scc = -1 - - var deps []int - deps = xo.edges.AppendTo(deps) - deps = xo.implicit.AppendTo(deps) - - for _, y := range deps { - // Loop invariant: x is canonical. - - y := h.find(onodeid(y)) - - if x == y { - continue // nodes already coalesced - } - - xo := h.onodes[x] - yo := h.onodes[y] - - switch { - case yo.scc > 0: - // y is already a collapsed SCC - - case yo.scc < 0: - // y is on the stack, and thus in the current SCC. - if yo.index < xo.lowlink { - xo.lowlink = yo.index - } - - default: - // y is unvisited; visit it now. - h.visit(y) - // Note: x and y are now non-canonical. - - x = h.find(onodeid(x)) - - if yo.lowlink < xo.lowlink { - xo.lowlink = yo.lowlink - } - } - } - h.checkCanonical(x) - - // Is x the root of an SCC? - if xo.lowlink == xo.index { - // Coalesce all nodes in the SCC. - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "scc o%d\n", x) - } - for { - // Pop y from stack. - i := len(h.stack) - 1 - y := h.stack[i] - h.stack = h.stack[:i] - - h.checkCanonical(x) - xo := h.onodes[x] - h.checkCanonical(y) - yo := h.onodes[y] - - if xo == yo { - // SCC is complete. - xo.scc = 1 - h.labelSCC(x) - break - } - h.coalesce(x, y) - } - } -} - -// Precondition: x is canonical. -func (h *hvn) labelSCC(x onodeid) { - h.checkCanonical(x) - xo := h.onodes[x] - xpe := &xo.peLabels - - // All indirect nodes get new labels. - if xo.indirect { - label := h.nextLabel() - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\tcreate p%d: indirect SCC\n", label) - fmt.Fprintf(h.log, "\to%d has p%d\n", x, label) - } - - // Remove pre-labeling, in case a direct pre-labeled node was - // merged with an indirect one. - xpe.Clear() - xpe.Insert(int(label)) - - return - } - - // Invariant: all peLabels sets are non-empty. - // Those that are logically empty contain zero as their sole element. - // No other sets contains zero. - - // Find all labels coming in to the coalesced SCC node. - for _, y := range xo.edges.AppendTo(nil) { - y := h.find(onodeid(y)) - if y == x { - continue // already coalesced - } - ype := &h.onodes[y].peLabels - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\tedge from o%d = %s\n", y, ype) - } - - if ype.IsEmpty() { - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\tnode has no PE label\n") - } - } - assert(!ype.IsEmpty(), "incoming node has no PE label") - - if ype.Has(0) { - // {0} represents a non-pointer. - assert(ype.Len() == 1, "PE set contains {0, ...}") - } else { - xpe.UnionWith(ype) - } - } - - switch xpe.Len() { - case 0: - // SCC has no incoming non-zero PE labels: it is a non-pointer. - xpe.Insert(0) - - case 1: - // already a singleton - - default: - // SCC has multiple incoming non-zero PE labels. - // Find the canonical label representing this set. - // We use String() as a fingerprint consistent with Equals(). - key := xpe.String() - label, ok := h.hvnLabel[key] - if !ok { - label = h.nextLabel() - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\tcreate p%d: union %s\n", label, xpe.String()) - } - h.hvnLabel[key] = label - } - xpe.Clear() - xpe.Insert(int(label)) - } - - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\to%d has p%d\n", x, xpe.Min()) - } -} - -// coalesce combines two nodes in the offline constraint graph. -// Precondition: x and y are canonical. -func (h *hvn) coalesce(x, y onodeid) { - xo := h.onodes[x] - yo := h.onodes[y] - - // x becomes y's canonical representative. - yo.rep = x - - if debugHVNVerbose && h.log != nil { - fmt.Fprintf(h.log, "\tcoalesce o%d into o%d\n", y, x) - } - - // x accumulates y's edges. - xo.edges.UnionWith(&yo.edges) - yo.edges.Clear() - - // x accumulates y's implicit edges. - xo.implicit.UnionWith(&yo.implicit) - yo.implicit.Clear() - - // x accumulates y's pointer-equivalence labels. - xo.peLabels.UnionWith(&yo.peLabels) - yo.peLabels.Clear() - - // x accumulates y's indirect flag. - if yo.indirect { - xo.indirect = true - } -} - -// simplify computes a degenerate renumbering of nodeids from the PE -// labels assigned by the hvn, and uses it to simplify the main -// constraint graph, eliminating non-pointer nodes and duplicate -// constraints. -// -func (h *hvn) simplify() { - // canon maps each peLabel to its canonical main node. - canon := make([]nodeid, h.label) - for i := range canon { - canon[i] = nodeid(h.N) // indicates "unset" - } - - // mapping maps each main node index to the index of the canonical node. - mapping := make([]nodeid, len(h.a.nodes)) - - for id := range h.a.nodes { - id := nodeid(id) - if id == 0 { - canon[0] = 0 - mapping[0] = 0 - continue - } - oid := h.find(onodeid(id)) - peLabels := &h.onodes[oid].peLabels - assert(peLabels.Len() == 1, "PE class is not a singleton") - label := peLabel(peLabels.Min()) - - canonId := canon[label] - if canonId == nodeid(h.N) { - // id becomes the representative of the PE label. - canonId = id - canon[label] = canonId - - if h.a.log != nil { - fmt.Fprintf(h.a.log, "\tpts(n%d) is canonical : \t(%s)\n", - id, h.a.nodes[id].typ) - } - - } else { - // Link the solver states for the two nodes. - assert(h.a.nodes[canonId].solve != nil, "missing solver state") - h.a.nodes[id].solve = h.a.nodes[canonId].solve - - if h.a.log != nil { - // TODO(adonovan): debug: reorganize the log so it prints - // one line: - // pe y = x1, ..., xn - // for each canonical y. Requires allocation. - fmt.Fprintf(h.a.log, "\tpts(n%d) = pts(n%d) : %s\n", - id, canonId, h.a.nodes[id].typ) - } - } - - mapping[id] = canonId - } - - // Renumber the constraints, eliminate duplicates, and eliminate - // any containing non-pointers (n0). - addrs := make(map[addrConstraint]bool) - copys := make(map[copyConstraint]bool) - loads := make(map[loadConstraint]bool) - stores := make(map[storeConstraint]bool) - offsetAddrs := make(map[offsetAddrConstraint]bool) - untags := make(map[untagConstraint]bool) - typeFilters := make(map[typeFilterConstraint]bool) - invokes := make(map[invokeConstraint]bool) - - nbefore := len(h.a.constraints) - cc := h.a.constraints[:0] // in-situ compaction - for _, c := range h.a.constraints { - // Renumber. - switch c := c.(type) { - case *addrConstraint: - // Don't renumber c.src since it is the label of - // an addressable object and will appear in PT sets. - c.dst = mapping[c.dst] - default: - c.renumber(mapping) - } - - if c.ptr() == 0 { - continue // skip: constraint attached to non-pointer - } - - var dup bool - switch c := c.(type) { - case *addrConstraint: - _, dup = addrs[*c] - addrs[*c] = true - - case *copyConstraint: - if c.src == c.dst { - continue // skip degenerate copies - } - if c.src == 0 { - continue // skip copy from non-pointer - } - _, dup = copys[*c] - copys[*c] = true - - case *loadConstraint: - if c.src == 0 { - continue // skip load from non-pointer - } - _, dup = loads[*c] - loads[*c] = true - - case *storeConstraint: - if c.src == 0 { - continue // skip store from non-pointer - } - _, dup = stores[*c] - stores[*c] = true - - case *offsetAddrConstraint: - if c.src == 0 { - continue // skip offset from non-pointer - } - _, dup = offsetAddrs[*c] - offsetAddrs[*c] = true - - case *untagConstraint: - if c.src == 0 { - continue // skip untag of non-pointer - } - _, dup = untags[*c] - untags[*c] = true - - case *typeFilterConstraint: - if c.src == 0 { - continue // skip filter of non-pointer - } - _, dup = typeFilters[*c] - typeFilters[*c] = true - - case *invokeConstraint: - if c.params == 0 { - panic("non-pointer invoke.params") - } - if c.iface == 0 { - continue // skip invoke on non-pointer - } - _, dup = invokes[*c] - invokes[*c] = true - - default: - // We don't bother de-duping advanced constraints - // (e.g. reflection) since they are uncommon. - - // Eliminate constraints containing non-pointer nodeids. - // - // We use reflection to find the fields to avoid - // adding yet another method to constraint. - // - // TODO(adonovan): experiment with a constraint - // method that returns a slice of pointers to - // nodeids fields to enable uniform iteration; - // the renumber() method could be removed and - // implemented using the new one. - // - // TODO(adonovan): opt: this is unsound since - // some constraints still have an effect if one - // of the operands is zero: rVCall, rVMapIndex, - // rvSetMapIndex. Handle them specially. - rtNodeid := reflect.TypeOf(nodeid(0)) - x := reflect.ValueOf(c).Elem() - for i, nf := 0, x.NumField(); i < nf; i++ { - f := x.Field(i) - if f.Type() == rtNodeid { - if f.Uint() == 0 { - dup = true // skip it - break - } - } - } - } - if dup { - continue // skip duplicates - } - - cc = append(cc, c) - } - h.a.constraints = cc - - if h.log != nil { - fmt.Fprintf(h.log, "#constraints: was %d, now %d\n", nbefore, len(h.a.constraints)) - } -} - -// find returns the canonical onodeid for x. -// (The onodes form a disjoint set forest.) -func (h *hvn) find(x onodeid) onodeid { - // TODO(adonovan): opt: this is a CPU hotspot. Try "union by rank". - xo := h.onodes[x] - rep := xo.rep - if rep != x { - rep = h.find(rep) // simple path compression - xo.rep = rep - } - return rep -} - -func (h *hvn) checkCanonical(x onodeid) { - if debugHVN { - assert(x == h.find(x), "not canonical") - } -} - -func assert(p bool, msg string) { - if debugHVN && !p { - panic("assertion failed: " + msg) - } -} diff --git a/vendor/golang.org/x/tools/go/pointer/intrinsics.go b/vendor/golang.org/x/tools/go/pointer/intrinsics.go deleted file mode 100644 index b7e2b1403c4..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/intrinsics.go +++ /dev/null @@ -1,361 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -// This package defines the treatment of intrinsics, i.e. library -// functions requiring special analytical treatment. -// -// Most of these are C or assembly functions, but even some Go -// functions require may special treatment if the analysis completely -// replaces the implementation of an API such as reflection. - -// TODO(adonovan): support a means of writing analytic summaries in -// the target code, so that users can summarise the effects of their -// own C functions using a snippet of Go. - -import ( - "fmt" - "go/types" - - "golang.org/x/tools/go/ssa" -) - -// Instances of 'intrinsic' generate analysis constraints for calls to -// intrinsic functions. -// Implementations may exploit information from the calling site -// via cgn.callersite; for shared contours this is nil. -type intrinsic func(a *analysis, cgn *cgnode) - -// Initialized in explicit init() to defeat (spurious) initialization -// cycle error. -var intrinsicsByName = make(map[string]intrinsic) - -func init() { - // Key strings are from Function.String(). - // That little dot ۰ is an Arabic zero numeral (U+06F0), - // categories [Nd]. - for name, fn := range map[string]intrinsic{ - // Other packages. - "bytes.Equal": ext۰NoEffect, - "bytes.IndexByte": ext۰NoEffect, - "crypto/aes.decryptBlockAsm": ext۰NoEffect, - "crypto/aes.encryptBlockAsm": ext۰NoEffect, - "crypto/aes.expandKeyAsm": ext۰NoEffect, - "crypto/aes.hasAsm": ext۰NoEffect, - "crypto/md5.block": ext۰NoEffect, - "crypto/rc4.xorKeyStream": ext۰NoEffect, - "crypto/sha1.block": ext۰NoEffect, - "crypto/sha256.block": ext۰NoEffect, - "hash/crc32.castagnoliSSE42": ext۰NoEffect, - "hash/crc32.haveSSE42": ext۰NoEffect, - "math.Abs": ext۰NoEffect, - "math.Acos": ext۰NoEffect, - "math.Asin": ext۰NoEffect, - "math.Atan": ext۰NoEffect, - "math.Atan2": ext۰NoEffect, - "math.Ceil": ext۰NoEffect, - "math.Cos": ext۰NoEffect, - "math.Dim": ext۰NoEffect, - "math.Exp": ext۰NoEffect, - "math.Exp2": ext۰NoEffect, - "math.Expm1": ext۰NoEffect, - "math.Float32bits": ext۰NoEffect, - "math.Float32frombits": ext۰NoEffect, - "math.Float64bits": ext۰NoEffect, - "math.Float64frombits": ext۰NoEffect, - "math.Floor": ext۰NoEffect, - "math.Frexp": ext۰NoEffect, - "math.Hypot": ext۰NoEffect, - "math.Ldexp": ext۰NoEffect, - "math.Log": ext۰NoEffect, - "math.Log10": ext۰NoEffect, - "math.Log1p": ext۰NoEffect, - "math.Log2": ext۰NoEffect, - "math.Max": ext۰NoEffect, - "math.Min": ext۰NoEffect, - "math.Mod": ext۰NoEffect, - "math.Modf": ext۰NoEffect, - "math.Remainder": ext۰NoEffect, - "math.Sin": ext۰NoEffect, - "math.Sincos": ext۰NoEffect, - "math.Sqrt": ext۰NoEffect, - "math.Tan": ext۰NoEffect, - "math.Trunc": ext۰NoEffect, - "math/big.addMulVVW": ext۰NoEffect, - "math/big.addVV": ext۰NoEffect, - "math/big.addVW": ext۰NoEffect, - "math/big.bitLen": ext۰NoEffect, - "math/big.divWVW": ext۰NoEffect, - "math/big.divWW": ext۰NoEffect, - "math/big.mulAddVWW": ext۰NoEffect, - "math/big.mulWW": ext۰NoEffect, - "math/big.shlVU": ext۰NoEffect, - "math/big.shrVU": ext۰NoEffect, - "math/big.subVV": ext۰NoEffect, - "math/big.subVW": ext۰NoEffect, - "net.runtime_Semacquire": ext۰NoEffect, - "net.runtime_Semrelease": ext۰NoEffect, - "net.runtime_pollClose": ext۰NoEffect, - "net.runtime_pollOpen": ext۰NoEffect, - "net.runtime_pollReset": ext۰NoEffect, - "net.runtime_pollServerInit": ext۰NoEffect, - "net.runtime_pollSetDeadline": ext۰NoEffect, - "net.runtime_pollUnblock": ext۰NoEffect, - "net.runtime_pollWait": ext۰NoEffect, - "net.runtime_pollWaitCanceled": ext۰NoEffect, - "os.epipecheck": ext۰NoEffect, - // All other runtime functions are treated as NoEffect. - "runtime.SetFinalizer": ext۰runtime۰SetFinalizer, - "strings.IndexByte": ext۰NoEffect, - "sync.runtime_Semacquire": ext۰NoEffect, - "sync.runtime_Semrelease": ext۰NoEffect, - "sync.runtime_Syncsemacquire": ext۰NoEffect, - "sync.runtime_Syncsemcheck": ext۰NoEffect, - "sync.runtime_Syncsemrelease": ext۰NoEffect, - "sync.runtime_procPin": ext۰NoEffect, - "sync.runtime_procUnpin": ext۰NoEffect, - "sync.runtime_registerPool": ext۰NoEffect, - "sync/atomic.AddInt32": ext۰NoEffect, - "sync/atomic.AddInt64": ext۰NoEffect, - "sync/atomic.AddUint32": ext۰NoEffect, - "sync/atomic.AddUint64": ext۰NoEffect, - "sync/atomic.AddUintptr": ext۰NoEffect, - "sync/atomic.CompareAndSwapInt32": ext۰NoEffect, - "sync/atomic.CompareAndSwapUint32": ext۰NoEffect, - "sync/atomic.CompareAndSwapUint64": ext۰NoEffect, - "sync/atomic.CompareAndSwapUintptr": ext۰NoEffect, - "sync/atomic.LoadInt32": ext۰NoEffect, - "sync/atomic.LoadInt64": ext۰NoEffect, - "sync/atomic.LoadPointer": ext۰NoEffect, // ignore unsafe.Pointers - "sync/atomic.LoadUint32": ext۰NoEffect, - "sync/atomic.LoadUint64": ext۰NoEffect, - "sync/atomic.LoadUintptr": ext۰NoEffect, - "sync/atomic.StoreInt32": ext۰NoEffect, - "sync/atomic.StorePointer": ext۰NoEffect, // ignore unsafe.Pointers - "sync/atomic.StoreUint32": ext۰NoEffect, - "sync/atomic.StoreUintptr": ext۰NoEffect, - "syscall.Close": ext۰NoEffect, - "syscall.Exit": ext۰NoEffect, - "syscall.Getpid": ext۰NoEffect, - "syscall.Getwd": ext۰NoEffect, - "syscall.Kill": ext۰NoEffect, - "syscall.RawSyscall": ext۰NoEffect, - "syscall.RawSyscall6": ext۰NoEffect, - "syscall.Syscall": ext۰NoEffect, - "syscall.Syscall6": ext۰NoEffect, - "syscall.runtime_AfterFork": ext۰NoEffect, - "syscall.runtime_BeforeFork": ext۰NoEffect, - "syscall.setenv_c": ext۰NoEffect, - "time.Sleep": ext۰NoEffect, - "time.now": ext۰NoEffect, - "time.startTimer": ext۰time۰startTimer, - "time.stopTimer": ext۰NoEffect, - } { - intrinsicsByName[name] = fn - } -} - -// findIntrinsic returns the constraint generation function for an -// intrinsic function fn, or nil if the function should be handled normally. -// -func (a *analysis) findIntrinsic(fn *ssa.Function) intrinsic { - // Consult the *Function-keyed cache. - // A cached nil indicates a normal non-intrinsic function. - impl, ok := a.intrinsics[fn] - if !ok { - impl = intrinsicsByName[fn.String()] // may be nil - - if a.isReflect(fn) { - if !a.config.Reflection { - impl = ext۰NoEffect // reflection disabled - } else if impl == nil { - // Ensure all "reflect" code is treated intrinsically. - impl = ext۰NotYetImplemented - } - } else if impl == nil && fn.Pkg != nil && fn.Pkg.Pkg.Path() == "runtime" { - // Ignore "runtime" (except SetFinalizer): - // it has few interesting effects on aliasing - // and is full of unsafe code we can't analyze. - impl = ext۰NoEffect - } - - a.intrinsics[fn] = impl - } - return impl -} - -// isReflect reports whether fn belongs to the "reflect" package. -func (a *analysis) isReflect(fn *ssa.Function) bool { - if a.reflectValueObj == nil { - return false // "reflect" package not loaded - } - reflectPackage := a.reflectValueObj.Pkg() - if fn.Pkg != nil && fn.Pkg.Pkg == reflectPackage { - return true - } - // Synthetic wrappers have a nil Pkg, so they slip through the - // previous check. Check the receiver package. - // TODO(adonovan): should synthetic wrappers have a non-nil Pkg? - if recv := fn.Signature.Recv(); recv != nil { - if named, ok := deref(recv.Type()).(*types.Named); ok { - if named.Obj().Pkg() == reflectPackage { - return true // e.g. wrapper of (reflect.Value).f - } - } - } - return false -} - -// A trivial intrinsic suitable for any function that does not: -// 1) induce aliases between its arguments or any global variables; -// 2) call any functions; or -// 3) create any labels. -// -// Many intrinsics (such as CompareAndSwapInt32) have a fourth kind of -// effect: loading or storing through a pointer. Though these could -// be significant, we deliberately ignore them because they are -// generally not worth the effort. -// -// We sometimes violate condition #3 if the function creates only -// non-function labels, as the control-flow graph is still sound. -// -func ext۰NoEffect(a *analysis, cgn *cgnode) {} - -func ext۰NotYetImplemented(a *analysis, cgn *cgnode) { - fn := cgn.fn - a.warnf(fn.Pos(), "unsound: intrinsic treatment of %s not yet implemented", fn) -} - -// ---------- func runtime.SetFinalizer(x, f interface{}) ---------- - -// runtime.SetFinalizer(x, f) -type runtimeSetFinalizerConstraint struct { - targets nodeid // (indirect) - f nodeid // (ptr) - x nodeid -} - -func (c *runtimeSetFinalizerConstraint) ptr() nodeid { return c.f } -func (c *runtimeSetFinalizerConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.targets), "SetFinalizer.targets") -} -func (c *runtimeSetFinalizerConstraint) renumber(mapping []nodeid) { - c.targets = mapping[c.targets] - c.f = mapping[c.f] - c.x = mapping[c.x] -} - -func (c *runtimeSetFinalizerConstraint) String() string { - return fmt.Sprintf("runtime.SetFinalizer(n%d, n%d)", c.x, c.f) -} - -func (c *runtimeSetFinalizerConstraint) solve(a *analysis, delta *nodeset) { - for _, fObj := range delta.AppendTo(a.deltaSpace) { - tDyn, f, indirect := a.taggedValue(nodeid(fObj)) - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - tSig, ok := tDyn.Underlying().(*types.Signature) - if !ok { - continue // not a function - } - if tSig.Recv() != nil { - panic(tSig) - } - if tSig.Params().Len() != 1 { - continue // not a unary function - } - - // Extract x to tmp. - tx := tSig.Params().At(0).Type() - tmp := a.addNodes(tx, "SetFinalizer.tmp") - a.typeAssert(tx, tmp, c.x, false) - - // Call f(tmp). - a.store(f, tmp, 1, a.sizeof(tx)) - - // Add dynamic call target. - if a.onlineCopy(c.targets, f) { - a.addWork(c.targets) - } - } -} - -func ext۰runtime۰SetFinalizer(a *analysis, cgn *cgnode) { - // This is the shared contour, used for dynamic calls. - targets := a.addOneNode(tInvalid, "SetFinalizer.targets", nil) - cgn.sites = append(cgn.sites, &callsite{targets: targets}) - params := a.funcParams(cgn.obj) - a.addConstraint(&runtimeSetFinalizerConstraint{ - targets: targets, - x: params, - f: params + 1, - }) -} - -// ---------- func time.startTimer(t *runtimeTimer) ---------- - -// time.StartTimer(t) -type timeStartTimerConstraint struct { - targets nodeid // (indirect) - t nodeid // (ptr) -} - -func (c *timeStartTimerConstraint) ptr() nodeid { return c.t } -func (c *timeStartTimerConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.targets), "StartTimer.targets") -} -func (c *timeStartTimerConstraint) renumber(mapping []nodeid) { - c.targets = mapping[c.targets] - c.t = mapping[c.t] -} - -func (c *timeStartTimerConstraint) String() string { - return fmt.Sprintf("time.startTimer(n%d)", c.t) -} - -func (c *timeStartTimerConstraint) solve(a *analysis, delta *nodeset) { - for _, tObj := range delta.AppendTo(a.deltaSpace) { - t := nodeid(tObj) - - // We model startTimer as if it was defined thus: - // func startTimer(t *runtimeTimer) { t.f(t.arg) } - - // We hard-code the field offsets of time.runtimeTimer: - // type runtimeTimer struct { - // 0 __identity__ - // 1 i int32 - // 2 when int64 - // 3 period int64 - // 4 f func(int64, interface{}) - // 5 arg interface{} - // } - f := t + 4 - arg := t + 5 - - // store t.arg to t.f.params[0] - // (offset 1 => skip identity) - a.store(f, arg, 1, 1) - - // Add dynamic call target. - if a.onlineCopy(c.targets, f) { - a.addWork(c.targets) - } - } -} - -func ext۰time۰startTimer(a *analysis, cgn *cgnode) { - // This is the shared contour, used for dynamic calls. - targets := a.addOneNode(tInvalid, "startTimer.targets", nil) - cgn.sites = append(cgn.sites, &callsite{targets: targets}) - params := a.funcParams(cgn.obj) - a.addConstraint(&timeStartTimerConstraint{ - targets: targets, - t: params, - }) -} diff --git a/vendor/golang.org/x/tools/go/pointer/labels.go b/vendor/golang.org/x/tools/go/pointer/labels.go deleted file mode 100644 index 7d64ef6a480..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/labels.go +++ /dev/null @@ -1,152 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -import ( - "fmt" - "go/token" - "go/types" - "strings" - - "golang.org/x/tools/go/ssa" -) - -// A Label is an entity that may be pointed to by a pointer, map, -// channel, 'func', slice or interface. -// -// Labels include: -// - functions -// - globals -// - tagged objects, representing interfaces and reflect.Values -// - arrays created by conversions (e.g. []byte("foo"), []byte(s)) -// - stack- and heap-allocated variables (including composite literals) -// - channels, maps and arrays created by make() -// - intrinsic or reflective operations that allocate (e.g. append, reflect.New) -// - intrinsic objects, e.g. the initial array behind os.Args. -// - and their subelements, e.g. "alloc.y[*].z" -// -// Labels are so varied that they defy good generalizations; -// some have no value, no callgraph node, or no position. -// Many objects have types that are inexpressible in Go: -// maps, channels, functions, tagged objects. -// -// At most one of Value() or ReflectType() may return non-nil. -// -type Label struct { - obj *object // the addressable memory location containing this label - subelement *fieldInfo // subelement path within obj, e.g. ".a.b[*].c" -} - -// Value returns the ssa.Value that allocated this label's object, if any. -func (l Label) Value() ssa.Value { - val, _ := l.obj.data.(ssa.Value) - return val -} - -// ReflectType returns the type represented by this label if it is an -// reflect.rtype instance object or *reflect.rtype-tagged object. -// -func (l Label) ReflectType() types.Type { - rtype, _ := l.obj.data.(types.Type) - return rtype -} - -// Path returns the path to the subelement of the object containing -// this label. For example, ".x[*].y". -// -func (l Label) Path() string { - return l.subelement.path() -} - -// Pos returns the position of this label, if known, zero otherwise. -func (l Label) Pos() token.Pos { - switch data := l.obj.data.(type) { - case ssa.Value: - return data.Pos() - case types.Type: - if nt, ok := deref(data).(*types.Named); ok { - return nt.Obj().Pos() - } - } - if cgn := l.obj.cgn; cgn != nil { - return cgn.fn.Pos() - } - return token.NoPos -} - -// String returns the printed form of this label. -// -// Examples: Object type: -// x (a variable) -// (sync.Mutex).Lock (a function) -// convert (array created by conversion) -// makemap (map allocated via make) -// makechan (channel allocated via make) -// makeinterface (tagged object allocated by makeinterface) -// (allocation in instrinsic) -// sync.Mutex (a reflect.rtype instance) -// (an intrinsic object) -// -// Labels within compound objects have subelement paths: -// x.y[*].z (a struct variable, x) -// append.y[*].z (array allocated by append) -// makeslice.y[*].z (array allocated via make) -// -// TODO(adonovan): expose func LabelString(*types.Package, Label). -// -func (l Label) String() string { - var s string - switch v := l.obj.data.(type) { - case types.Type: - return v.String() - - case string: - s = v // an intrinsic object (e.g. os.Args[*]) - - case nil: - if l.obj.cgn != nil { - // allocation by intrinsic or reflective operation - s = fmt.Sprintf("", l.obj.cgn.fn) - } else { - s = "" // should be unreachable - } - - case *ssa.Function: - s = v.String() - - case *ssa.Global: - s = v.String() - - case *ssa.Const: - s = v.Name() - - case *ssa.Alloc: - s = v.Comment - if s == "" { - s = "alloc" - } - - case *ssa.Call: - // Currently only calls to append can allocate objects. - if v.Call.Value.(*ssa.Builtin).Object().Name() != "append" { - panic("unhandled *ssa.Call label: " + v.Name()) - } - s = "append" - - case *ssa.MakeMap, *ssa.MakeChan, *ssa.MakeSlice, *ssa.Convert: - s = strings.ToLower(strings.TrimPrefix(fmt.Sprintf("%T", v), "*ssa.")) - - case *ssa.MakeInterface: - // MakeInterface is usually implicit in Go source (so - // Pos()==0), and tagged objects may be allocated - // synthetically (so no *MakeInterface data). - s = "makeinterface:" + v.X.Type().String() - - default: - panic(fmt.Sprintf("unhandled object data type: %T", v)) - } - - return s + l.subelement.path() -} diff --git a/vendor/golang.org/x/tools/go/pointer/opt.go b/vendor/golang.org/x/tools/go/pointer/opt.go deleted file mode 100644 index 81f80aa4f5c..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/opt.go +++ /dev/null @@ -1,132 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -// This file implements renumbering, a pre-solver optimization to -// improve the efficiency of the solver's points-to set representation. -// -// TODO(adonovan): rename file "renumber.go" - -import "fmt" - -// renumber permutes a.nodes so that all nodes within an addressable -// object appear before all non-addressable nodes, maintaining the -// order of nodes within the same object (as required by offsetAddr). -// -// renumber must update every nodeid in the analysis (constraints, -// Pointers, callgraph, etc) to reflect the new ordering. -// -// This is an optimisation to increase the locality and efficiency of -// sparse representations of points-to sets. (Typically only about -// 20% of nodes are within an object.) -// -// NB: nodes added during solving (e.g. for reflection, SetFinalizer) -// will be appended to the end. -// -// Renumbering makes the PTA log inscrutable. To aid debugging, later -// phases (e.g. HVN) must not rely on it having occurred. -// -func (a *analysis) renumber() { - if a.log != nil { - fmt.Fprintf(a.log, "\n\n==== Renumbering\n\n") - } - - N := nodeid(len(a.nodes)) - newNodes := make([]*node, N, N) - renumbering := make([]nodeid, N, N) // maps old to new - - var i, j nodeid - - // The zero node is special. - newNodes[j] = a.nodes[i] - renumbering[i] = j - i++ - j++ - - // Pass 1: object nodes. - for i < N { - obj := a.nodes[i].obj - if obj == nil { - i++ - continue - } - - end := i + nodeid(obj.size) - for i < end { - newNodes[j] = a.nodes[i] - renumbering[i] = j - i++ - j++ - } - } - nobj := j - - // Pass 2: non-object nodes. - for i = 1; i < N; { - obj := a.nodes[i].obj - if obj != nil { - i += nodeid(obj.size) - continue - } - - newNodes[j] = a.nodes[i] - renumbering[i] = j - i++ - j++ - } - - if j != N { - panic(fmt.Sprintf("internal error: j=%d, N=%d", j, N)) - } - - // Log the remapping table. - if a.log != nil { - fmt.Fprintf(a.log, "Renumbering nodes to improve density:\n") - fmt.Fprintf(a.log, "(%d object nodes of %d total)\n", nobj, N) - for old, new := range renumbering { - fmt.Fprintf(a.log, "\tn%d -> n%d\n", old, new) - } - } - - // Now renumber all existing nodeids to use the new node permutation. - // It is critical that all reachable nodeids are accounted for! - - // Renumber nodeids in queried Pointers. - for v, ptr := range a.result.Queries { - ptr.n = renumbering[ptr.n] - a.result.Queries[v] = ptr - } - for v, ptr := range a.result.IndirectQueries { - ptr.n = renumbering[ptr.n] - a.result.IndirectQueries[v] = ptr - } - for _, queries := range a.config.extendedQueries { - for _, query := range queries { - if query.ptr != nil { - query.ptr.n = renumbering[query.ptr.n] - } - } - } - - // Renumber nodeids in global objects. - for v, id := range a.globalobj { - a.globalobj[v] = renumbering[id] - } - - // Renumber nodeids in constraints. - for _, c := range a.constraints { - c.renumber(renumbering) - } - - // Renumber nodeids in the call graph. - for _, cgn := range a.cgnodes { - cgn.obj = renumbering[cgn.obj] - for _, site := range cgn.sites { - site.targets = renumbering[site.targets] - } - } - - a.nodes = newNodes -} diff --git a/vendor/golang.org/x/tools/go/pointer/print.go b/vendor/golang.org/x/tools/go/pointer/print.go deleted file mode 100644 index 4f2f4c7ae12..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/print.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -import "fmt" - -func (c *addrConstraint) String() string { - return fmt.Sprintf("addr n%d <- {&n%d}", c.dst, c.src) -} - -func (c *copyConstraint) String() string { - return fmt.Sprintf("copy n%d <- n%d", c.dst, c.src) -} - -func (c *loadConstraint) String() string { - return fmt.Sprintf("load n%d <- n%d[%d]", c.dst, c.src, c.offset) -} - -func (c *storeConstraint) String() string { - return fmt.Sprintf("store n%d[%d] <- n%d", c.dst, c.offset, c.src) -} - -func (c *offsetAddrConstraint) String() string { - return fmt.Sprintf("offsetAddr n%d <- n%d.#%d", c.dst, c.src, c.offset) -} - -func (c *typeFilterConstraint) String() string { - return fmt.Sprintf("typeFilter n%d <- n%d.(%s)", c.dst, c.src, c.typ) -} - -func (c *untagConstraint) String() string { - return fmt.Sprintf("untag n%d <- n%d.(%s)", c.dst, c.src, c.typ) -} - -func (c *invokeConstraint) String() string { - return fmt.Sprintf("invoke n%d.%s(n%d ...)", c.iface, c.method.Name(), c.params) -} - -func (n nodeid) String() string { - return fmt.Sprintf("n%d", n) -} diff --git a/vendor/golang.org/x/tools/go/pointer/query.go b/vendor/golang.org/x/tools/go/pointer/query.go deleted file mode 100644 index b52cc9f3bc6..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/query.go +++ /dev/null @@ -1,221 +0,0 @@ -package pointer - -import ( - "errors" - "fmt" - "go/ast" - "go/parser" - "go/token" - "go/types" - "strconv" -) - -// An extendedQuery represents a sequence of destructuring operations -// applied to an ssa.Value (denoted by "x"). -type extendedQuery struct { - ops []interface{} - ptr *Pointer -} - -// indexValue returns the value of an integer literal used as an -// index. -func indexValue(expr ast.Expr) (int, error) { - lit, ok := expr.(*ast.BasicLit) - if !ok { - return 0, fmt.Errorf("non-integer index (%T)", expr) - } - if lit.Kind != token.INT { - return 0, fmt.Errorf("non-integer index %s", lit.Value) - } - return strconv.Atoi(lit.Value) -} - -// parseExtendedQuery parses and validates a destructuring Go -// expression and returns the sequence of destructuring operations. -// See parseDestructuringExpr for details. -func parseExtendedQuery(typ types.Type, query string) ([]interface{}, types.Type, error) { - expr, err := parser.ParseExpr(query) - if err != nil { - return nil, nil, err - } - ops, typ, err := destructuringOps(typ, expr) - if err != nil { - return nil, nil, err - } - if len(ops) == 0 { - return nil, nil, errors.New("invalid query: must not be empty") - } - if ops[0] != "x" { - return nil, nil, fmt.Errorf("invalid query: query operand must be named x") - } - if !CanPoint(typ) { - return nil, nil, fmt.Errorf("query does not describe a pointer-like value: %s", typ) - } - return ops, typ, nil -} - -// destructuringOps parses a Go expression consisting only of an -// identifier "x", field selections, indexing, channel receives, load -// operations and parens---for example: "<-(*x[i])[key]"--- and -// returns the sequence of destructuring operations on x. -func destructuringOps(typ types.Type, expr ast.Expr) ([]interface{}, types.Type, error) { - switch expr := expr.(type) { - case *ast.SelectorExpr: - out, typ, err := destructuringOps(typ, expr.X) - if err != nil { - return nil, nil, err - } - - var structT *types.Struct - switch typ := typ.Underlying().(type) { - case *types.Pointer: - var ok bool - structT, ok = typ.Elem().Underlying().(*types.Struct) - if !ok { - return nil, nil, fmt.Errorf("cannot access field %s of pointer to type %s", expr.Sel.Name, typ.Elem()) - } - - out = append(out, "load") - case *types.Struct: - structT = typ - default: - return nil, nil, fmt.Errorf("cannot access field %s of type %s", expr.Sel.Name, typ) - } - - for i := 0; i < structT.NumFields(); i++ { - field := structT.Field(i) - if field.Name() == expr.Sel.Name { - out = append(out, "field", i) - return out, field.Type().Underlying(), nil - } - } - // TODO(dh): supporting embedding would need something like - // types.LookupFieldOrMethod, but without taking package - // boundaries into account, because we may want to access - // unexported fields. If we were only interested in one level - // of unexported name, we could determine the appropriate - // package and run LookupFieldOrMethod with that. However, a - // single query may want to cross multiple package boundaries, - // and at this point it's not really worth the complexity. - return nil, nil, fmt.Errorf("no field %s in %s (embedded fields must be resolved manually)", expr.Sel.Name, structT) - case *ast.Ident: - return []interface{}{expr.Name}, typ, nil - case *ast.BasicLit: - return []interface{}{expr.Value}, nil, nil - case *ast.IndexExpr: - out, typ, err := destructuringOps(typ, expr.X) - if err != nil { - return nil, nil, err - } - switch typ := typ.Underlying().(type) { - case *types.Array: - out = append(out, "arrayelem") - return out, typ.Elem().Underlying(), nil - case *types.Slice: - out = append(out, "sliceelem") - return out, typ.Elem().Underlying(), nil - case *types.Map: - out = append(out, "mapelem") - return out, typ.Elem().Underlying(), nil - case *types.Tuple: - out = append(out, "index") - idx, err := indexValue(expr.Index) - if err != nil { - return nil, nil, err - } - out = append(out, idx) - if idx >= typ.Len() || idx < 0 { - return nil, nil, fmt.Errorf("tuple index %d out of bounds", idx) - } - return out, typ.At(idx).Type().Underlying(), nil - default: - return nil, nil, fmt.Errorf("cannot index type %s", typ) - } - - case *ast.UnaryExpr: - if expr.Op != token.ARROW { - return nil, nil, fmt.Errorf("unsupported unary operator %s", expr.Op) - } - out, typ, err := destructuringOps(typ, expr.X) - if err != nil { - return nil, nil, err - } - ch, ok := typ.(*types.Chan) - if !ok { - return nil, nil, fmt.Errorf("cannot receive from value of type %s", typ) - } - out = append(out, "recv") - return out, ch.Elem().Underlying(), err - case *ast.ParenExpr: - return destructuringOps(typ, expr.X) - case *ast.StarExpr: - out, typ, err := destructuringOps(typ, expr.X) - if err != nil { - return nil, nil, err - } - ptr, ok := typ.(*types.Pointer) - if !ok { - return nil, nil, fmt.Errorf("cannot dereference type %s", typ) - } - out = append(out, "load") - return out, ptr.Elem().Underlying(), err - default: - return nil, nil, fmt.Errorf("unsupported expression %T", expr) - } -} - -func (a *analysis) evalExtendedQuery(t types.Type, id nodeid, ops []interface{}) (types.Type, nodeid) { - pid := id - // TODO(dh): we're allocating intermediary nodes each time - // evalExtendedQuery is called. We should probably only generate - // them once per (v, ops) pair. - for i := 1; i < len(ops); i++ { - var nid nodeid - switch ops[i] { - case "recv": - t = t.(*types.Chan).Elem().Underlying() - nid = a.addNodes(t, "query.extended") - a.load(nid, pid, 0, a.sizeof(t)) - case "field": - i++ // fetch field index - tt := t.(*types.Struct) - idx := ops[i].(int) - offset := a.offsetOf(t, idx) - t = tt.Field(idx).Type().Underlying() - nid = a.addNodes(t, "query.extended") - a.copy(nid, pid+nodeid(offset), a.sizeof(t)) - case "arrayelem": - t = t.(*types.Array).Elem().Underlying() - nid = a.addNodes(t, "query.extended") - a.copy(nid, 1+pid, a.sizeof(t)) - case "sliceelem": - t = t.(*types.Slice).Elem().Underlying() - nid = a.addNodes(t, "query.extended") - a.load(nid, pid, 1, a.sizeof(t)) - case "mapelem": - tt := t.(*types.Map) - t = tt.Elem() - ksize := a.sizeof(tt.Key()) - vsize := a.sizeof(tt.Elem()) - nid = a.addNodes(t, "query.extended") - a.load(nid, pid, ksize, vsize) - case "index": - i++ // fetch index - tt := t.(*types.Tuple) - idx := ops[i].(int) - t = tt.At(idx).Type().Underlying() - nid = a.addNodes(t, "query.extended") - a.copy(nid, pid+nodeid(idx), a.sizeof(t)) - case "load": - t = t.(*types.Pointer).Elem().Underlying() - nid = a.addNodes(t, "query.extended") - a.load(nid, pid, 0, a.sizeof(t)) - default: - // shouldn't happen - panic(fmt.Sprintf("unknown op %q", ops[i])) - } - pid = nid - } - - return t, pid -} diff --git a/vendor/golang.org/x/tools/go/pointer/reflect.go b/vendor/golang.org/x/tools/go/pointer/reflect.go deleted file mode 100644 index 7aa1a9cb880..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/reflect.go +++ /dev/null @@ -1,1975 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -// This file implements the generation and resolution rules for -// constraints arising from the use of reflection in the target -// program. See doc.go for explanation of the representation. -// -// For consistency, the names of all parameters match those of the -// actual functions in the "reflect" package. -// -// To avoid proliferation of equivalent labels, intrinsics should -// memoize as much as possible, like TypeOf and Zero do for their -// tagged objects. -// -// TODO(adonovan): this file is rather subtle. Explain how we derive -// the implementation of each reflect operator from its spec, -// including the subtleties of reflect.flag{Addr,RO,Indir}. -// [Hint: our implementation is as if reflect.flagIndir was always -// true, i.e. reflect.Values are pointers to tagged objects, there is -// no inline allocation optimization; and indirect tagged objects (not -// yet implemented) correspond to reflect.Values with -// reflect.flagAddr.] -// A picture would help too. -// -// TODO(adonovan): try factoring up the common parts of the majority of -// these constraints that are single input, single output. - -import ( - "fmt" - "go/constant" - "go/types" - "reflect" - - "golang.org/x/tools/go/ssa" -) - -func init() { - for name, fn := range map[string]intrinsic{ - // reflect.Value methods. - "(reflect.Value).Addr": ext۰reflect۰Value۰Addr, - "(reflect.Value).Bool": ext۰NoEffect, - "(reflect.Value).Bytes": ext۰reflect۰Value۰Bytes, - "(reflect.Value).Call": ext۰reflect۰Value۰Call, - "(reflect.Value).CallSlice": ext۰reflect۰Value۰CallSlice, - "(reflect.Value).CanAddr": ext۰NoEffect, - "(reflect.Value).CanInterface": ext۰NoEffect, - "(reflect.Value).CanSet": ext۰NoEffect, - "(reflect.Value).Cap": ext۰NoEffect, - "(reflect.Value).Close": ext۰NoEffect, - "(reflect.Value).Complex": ext۰NoEffect, - "(reflect.Value).Convert": ext۰reflect۰Value۰Convert, - "(reflect.Value).Elem": ext۰reflect۰Value۰Elem, - "(reflect.Value).Field": ext۰reflect۰Value۰Field, - "(reflect.Value).FieldByIndex": ext۰reflect۰Value۰FieldByIndex, - "(reflect.Value).FieldByName": ext۰reflect۰Value۰FieldByName, - "(reflect.Value).FieldByNameFunc": ext۰reflect۰Value۰FieldByNameFunc, - "(reflect.Value).Float": ext۰NoEffect, - "(reflect.Value).Index": ext۰reflect۰Value۰Index, - "(reflect.Value).Int": ext۰NoEffect, - "(reflect.Value).Interface": ext۰reflect۰Value۰Interface, - "(reflect.Value).InterfaceData": ext۰NoEffect, - "(reflect.Value).IsNil": ext۰NoEffect, - "(reflect.Value).IsValid": ext۰NoEffect, - "(reflect.Value).Kind": ext۰NoEffect, - "(reflect.Value).Len": ext۰NoEffect, - "(reflect.Value).MapIndex": ext۰reflect۰Value۰MapIndex, - "(reflect.Value).MapKeys": ext۰reflect۰Value۰MapKeys, - "(reflect.Value).Method": ext۰reflect۰Value۰Method, - "(reflect.Value).MethodByName": ext۰reflect۰Value۰MethodByName, - "(reflect.Value).NumField": ext۰NoEffect, - "(reflect.Value).NumMethod": ext۰NoEffect, - "(reflect.Value).OverflowComplex": ext۰NoEffect, - "(reflect.Value).OverflowFloat": ext۰NoEffect, - "(reflect.Value).OverflowInt": ext۰NoEffect, - "(reflect.Value).OverflowUint": ext۰NoEffect, - "(reflect.Value).Pointer": ext۰NoEffect, - "(reflect.Value).Recv": ext۰reflect۰Value۰Recv, - "(reflect.Value).Send": ext۰reflect۰Value۰Send, - "(reflect.Value).Set": ext۰reflect۰Value۰Set, - "(reflect.Value).SetBool": ext۰NoEffect, - "(reflect.Value).SetBytes": ext۰reflect۰Value۰SetBytes, - "(reflect.Value).SetComplex": ext۰NoEffect, - "(reflect.Value).SetFloat": ext۰NoEffect, - "(reflect.Value).SetInt": ext۰NoEffect, - "(reflect.Value).SetLen": ext۰NoEffect, - "(reflect.Value).SetMapIndex": ext۰reflect۰Value۰SetMapIndex, - "(reflect.Value).SetPointer": ext۰reflect۰Value۰SetPointer, - "(reflect.Value).SetString": ext۰NoEffect, - "(reflect.Value).SetUint": ext۰NoEffect, - "(reflect.Value).Slice": ext۰reflect۰Value۰Slice, - "(reflect.Value).String": ext۰NoEffect, - "(reflect.Value).TryRecv": ext۰reflect۰Value۰Recv, - "(reflect.Value).TrySend": ext۰reflect۰Value۰Send, - "(reflect.Value).Type": ext۰NoEffect, - "(reflect.Value).Uint": ext۰NoEffect, - "(reflect.Value).UnsafeAddr": ext۰NoEffect, - - // Standalone reflect.* functions. - "reflect.Append": ext۰reflect۰Append, - "reflect.AppendSlice": ext۰reflect۰AppendSlice, - "reflect.Copy": ext۰reflect۰Copy, - "reflect.ChanOf": ext۰reflect۰ChanOf, - "reflect.DeepEqual": ext۰NoEffect, - "reflect.Indirect": ext۰reflect۰Indirect, - "reflect.MakeChan": ext۰reflect۰MakeChan, - "reflect.MakeFunc": ext۰reflect۰MakeFunc, - "reflect.MakeMap": ext۰reflect۰MakeMap, - "reflect.MakeSlice": ext۰reflect۰MakeSlice, - "reflect.MapOf": ext۰reflect۰MapOf, - "reflect.New": ext۰reflect۰New, - "reflect.NewAt": ext۰reflect۰NewAt, - "reflect.PtrTo": ext۰reflect۰PtrTo, - "reflect.Select": ext۰reflect۰Select, - "reflect.SliceOf": ext۰reflect۰SliceOf, - "reflect.TypeOf": ext۰reflect۰TypeOf, - "reflect.ValueOf": ext۰reflect۰ValueOf, - "reflect.Zero": ext۰reflect۰Zero, - "reflect.init": ext۰NoEffect, - - // *reflect.rtype methods - "(*reflect.rtype).Align": ext۰NoEffect, - "(*reflect.rtype).AssignableTo": ext۰NoEffect, - "(*reflect.rtype).Bits": ext۰NoEffect, - "(*reflect.rtype).ChanDir": ext۰NoEffect, - "(*reflect.rtype).ConvertibleTo": ext۰NoEffect, - "(*reflect.rtype).Elem": ext۰reflect۰rtype۰Elem, - "(*reflect.rtype).Field": ext۰reflect۰rtype۰Field, - "(*reflect.rtype).FieldAlign": ext۰NoEffect, - "(*reflect.rtype).FieldByIndex": ext۰reflect۰rtype۰FieldByIndex, - "(*reflect.rtype).FieldByName": ext۰reflect۰rtype۰FieldByName, - "(*reflect.rtype).FieldByNameFunc": ext۰reflect۰rtype۰FieldByNameFunc, - "(*reflect.rtype).Implements": ext۰NoEffect, - "(*reflect.rtype).In": ext۰reflect۰rtype۰In, - "(*reflect.rtype).IsVariadic": ext۰NoEffect, - "(*reflect.rtype).Key": ext۰reflect۰rtype۰Key, - "(*reflect.rtype).Kind": ext۰NoEffect, - "(*reflect.rtype).Len": ext۰NoEffect, - "(*reflect.rtype).Method": ext۰reflect۰rtype۰Method, - "(*reflect.rtype).MethodByName": ext۰reflect۰rtype۰MethodByName, - "(*reflect.rtype).Name": ext۰NoEffect, - "(*reflect.rtype).NumField": ext۰NoEffect, - "(*reflect.rtype).NumIn": ext۰NoEffect, - "(*reflect.rtype).NumMethod": ext۰NoEffect, - "(*reflect.rtype).NumOut": ext۰NoEffect, - "(*reflect.rtype).Out": ext۰reflect۰rtype۰Out, - "(*reflect.rtype).PkgPath": ext۰NoEffect, - "(*reflect.rtype).Size": ext۰NoEffect, - "(*reflect.rtype).String": ext۰NoEffect, - } { - intrinsicsByName[name] = fn - } -} - -// -------------------- (reflect.Value) -------------------- - -func ext۰reflect۰Value۰Addr(a *analysis, cgn *cgnode) {} // TODO(adonovan) - -// ---------- func (Value).Bytes() Value ---------- - -// result = v.Bytes() -type rVBytesConstraint struct { - v nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rVBytesConstraint) ptr() nodeid { return c.v } -func (c *rVBytesConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "rVBytes.result") -} -func (c *rVBytesConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.result = mapping[c.result] -} - -func (c *rVBytesConstraint) String() string { - return fmt.Sprintf("n%d = reflect n%d.Bytes()", c.result, c.v) -} - -func (c *rVBytesConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, slice, indirect := a.taggedValue(vObj) - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - tSlice, ok := tDyn.Underlying().(*types.Slice) - if ok && types.Identical(tSlice.Elem(), types.Typ[types.Uint8]) { - if a.onlineCopy(c.result, slice) { - changed = true - } - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰Value۰Bytes(a *analysis, cgn *cgnode) { - a.addConstraint(&rVBytesConstraint{ - v: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// ---------- func (Value).Call(in []Value) []Value ---------- - -// result = v.Call(in) -type rVCallConstraint struct { - cgn *cgnode - targets nodeid // (indirect) - v nodeid // (ptr) - arg nodeid // = in[*] - result nodeid // (indirect) - dotdotdot bool // interpret last arg as a "..." slice -} - -func (c *rVCallConstraint) ptr() nodeid { return c.v } -func (c *rVCallConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.targets), "rVCall.targets") - h.markIndirect(onodeid(c.result), "rVCall.result") -} -func (c *rVCallConstraint) renumber(mapping []nodeid) { - c.targets = mapping[c.targets] - c.v = mapping[c.v] - c.arg = mapping[c.arg] - c.result = mapping[c.result] -} - -func (c *rVCallConstraint) String() string { - return fmt.Sprintf("n%d = reflect n%d.Call(n%d)", c.result, c.v, c.arg) -} - -func (c *rVCallConstraint) solve(a *analysis, delta *nodeset) { - if c.targets == 0 { - panic("no targets") - } - - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, fn, indirect := a.taggedValue(vObj) - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - tSig, ok := tDyn.Underlying().(*types.Signature) - if !ok { - continue // not a function - } - if tSig.Recv() != nil { - panic(tSig) // TODO(adonovan): rethink when we implement Method() - } - - // Add dynamic call target. - if a.onlineCopy(c.targets, fn) { - a.addWork(c.targets) - // TODO(adonovan): is 'else continue' a sound optimisation here? - } - - // Allocate a P/R block. - tParams := tSig.Params() - tResults := tSig.Results() - params := a.addNodes(tParams, "rVCall.params") - results := a.addNodes(tResults, "rVCall.results") - - // Make a dynamic call to 'fn'. - a.store(fn, params, 1, a.sizeof(tParams)) - a.load(results, fn, 1+a.sizeof(tParams), a.sizeof(tResults)) - - // Populate P by type-asserting each actual arg (all merged in c.arg). - for i, n := 0, tParams.Len(); i < n; i++ { - T := tParams.At(i).Type() - a.typeAssert(T, params, c.arg, false) - params += nodeid(a.sizeof(T)) - } - - // Use R by tagging and copying each actual result to c.result. - for i, n := 0, tResults.Len(); i < n; i++ { - T := tResults.At(i).Type() - // Convert from an arbitrary type to a reflect.Value - // (like MakeInterface followed by reflect.ValueOf). - if isInterface(T) { - // (don't tag) - if a.onlineCopy(c.result, results) { - changed = true - } - } else { - obj := a.makeTagged(T, c.cgn, nil) - a.onlineCopyN(obj+1, results, a.sizeof(T)) - if a.addLabel(c.result, obj) { // (true) - changed = true - } - } - results += nodeid(a.sizeof(T)) - } - } - if changed { - a.addWork(c.result) - } -} - -// Common code for direct (inlined) and indirect calls to (reflect.Value).Call. -func reflectCallImpl(a *analysis, cgn *cgnode, site *callsite, recv, arg nodeid, dotdotdot bool) nodeid { - // Allocate []reflect.Value array for the result. - ret := a.nextNode() - a.addNodes(types.NewArray(a.reflectValueObj.Type(), 1), "rVCall.ret") - a.endObject(ret, cgn, nil) - - // pts(targets) will be the set of possible call targets. - site.targets = a.addOneNode(tInvalid, "rvCall.targets", nil) - - // All arguments are merged since they arrive in a slice. - argelts := a.addOneNode(a.reflectValueObj.Type(), "rVCall.args", nil) - a.load(argelts, arg, 1, 1) // slice elements - - a.addConstraint(&rVCallConstraint{ - cgn: cgn, - targets: site.targets, - v: recv, - arg: argelts, - result: ret + 1, // results go into elements of ret - dotdotdot: dotdotdot, - }) - return ret -} - -func reflectCall(a *analysis, cgn *cgnode, dotdotdot bool) { - // This is the shared contour implementation of (reflect.Value).Call - // and CallSlice, as used by indirect calls (rare). - // Direct calls are inlined in gen.go, eliding the - // intermediate cgnode for Call. - site := new(callsite) - cgn.sites = append(cgn.sites, site) - recv := a.funcParams(cgn.obj) - arg := recv + 1 - ret := reflectCallImpl(a, cgn, site, recv, arg, dotdotdot) - a.addressOf(cgn.fn.Signature.Results().At(0).Type(), a.funcResults(cgn.obj), ret) -} - -func ext۰reflect۰Value۰Call(a *analysis, cgn *cgnode) { - reflectCall(a, cgn, false) -} - -func ext۰reflect۰Value۰CallSlice(a *analysis, cgn *cgnode) { - // TODO(adonovan): implement. Also, inline direct calls in gen.go too. - if false { - reflectCall(a, cgn, true) - } -} - -func ext۰reflect۰Value۰Convert(a *analysis, cgn *cgnode) {} // TODO(adonovan) - -// ---------- func (Value).Elem() Value ---------- - -// result = v.Elem() -type rVElemConstraint struct { - cgn *cgnode - v nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rVElemConstraint) ptr() nodeid { return c.v } -func (c *rVElemConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "rVElem.result") -} -func (c *rVElemConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.result = mapping[c.result] -} - -func (c *rVElemConstraint) String() string { - return fmt.Sprintf("n%d = reflect n%d.Elem()", c.result, c.v) -} - -func (c *rVElemConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, payload, indirect := a.taggedValue(vObj) - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - switch t := tDyn.Underlying().(type) { - case *types.Interface: - if a.onlineCopy(c.result, payload) { - changed = true - } - - case *types.Pointer: - obj := a.makeTagged(t.Elem(), c.cgn, nil) - a.load(obj+1, payload, 0, a.sizeof(t.Elem())) - if a.addLabel(c.result, obj) { - changed = true - } - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰Value۰Elem(a *analysis, cgn *cgnode) { - a.addConstraint(&rVElemConstraint{ - cgn: cgn, - v: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -func ext۰reflect۰Value۰Field(a *analysis, cgn *cgnode) {} // TODO(adonovan) -func ext۰reflect۰Value۰FieldByIndex(a *analysis, cgn *cgnode) {} // TODO(adonovan) -func ext۰reflect۰Value۰FieldByName(a *analysis, cgn *cgnode) {} // TODO(adonovan) -func ext۰reflect۰Value۰FieldByNameFunc(a *analysis, cgn *cgnode) {} // TODO(adonovan) - -// ---------- func (Value).Index() Value ---------- - -// result = v.Index() -type rVIndexConstraint struct { - cgn *cgnode - v nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rVIndexConstraint) ptr() nodeid { return c.v } -func (c *rVIndexConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "rVIndex.result") -} -func (c *rVIndexConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.result = mapping[c.result] -} - -func (c *rVIndexConstraint) String() string { - return fmt.Sprintf("n%d = reflect n%d.Index()", c.result, c.v) -} - -func (c *rVIndexConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, payload, indirect := a.taggedValue(vObj) - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - var res nodeid - switch t := tDyn.Underlying().(type) { - case *types.Array: - res = a.makeTagged(t.Elem(), c.cgn, nil) - a.onlineCopyN(res+1, payload+1, a.sizeof(t.Elem())) - - case *types.Slice: - res = a.makeTagged(t.Elem(), c.cgn, nil) - a.load(res+1, payload, 1, a.sizeof(t.Elem())) - - case *types.Basic: - if t.Kind() == types.String { - res = a.makeTagged(types.Typ[types.Rune], c.cgn, nil) - } - } - if res != 0 && a.addLabel(c.result, res) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰Value۰Index(a *analysis, cgn *cgnode) { - a.addConstraint(&rVIndexConstraint{ - cgn: cgn, - v: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// ---------- func (Value).Interface() Value ---------- - -// result = v.Interface() -type rVInterfaceConstraint struct { - v nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rVInterfaceConstraint) ptr() nodeid { return c.v } -func (c *rVInterfaceConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "rVInterface.result") -} -func (c *rVInterfaceConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.result = mapping[c.result] -} - -func (c *rVInterfaceConstraint) String() string { - return fmt.Sprintf("n%d = reflect n%d.Interface()", c.result, c.v) -} - -func (c *rVInterfaceConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, payload, indirect := a.taggedValue(vObj) - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - if isInterface(tDyn) { - if a.onlineCopy(c.result, payload) { - a.addWork(c.result) - } - } else { - if a.addLabel(c.result, vObj) { - changed = true - } - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰Value۰Interface(a *analysis, cgn *cgnode) { - a.addConstraint(&rVInterfaceConstraint{ - v: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// ---------- func (Value).MapIndex(Value) Value ---------- - -// result = v.MapIndex(_) -type rVMapIndexConstraint struct { - cgn *cgnode - v nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rVMapIndexConstraint) ptr() nodeid { return c.v } -func (c *rVMapIndexConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "rVMapIndex.result") -} -func (c *rVMapIndexConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.result = mapping[c.result] -} - -func (c *rVMapIndexConstraint) String() string { - return fmt.Sprintf("n%d = reflect n%d.MapIndex(_)", c.result, c.v) -} - -func (c *rVMapIndexConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, m, indirect := a.taggedValue(vObj) - tMap, _ := tDyn.Underlying().(*types.Map) - if tMap == nil { - continue // not a map - } - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - obj := a.makeTagged(tMap.Elem(), c.cgn, nil) - a.load(obj+1, m, a.sizeof(tMap.Key()), a.sizeof(tMap.Elem())) - if a.addLabel(c.result, obj) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰Value۰MapIndex(a *analysis, cgn *cgnode) { - a.addConstraint(&rVMapIndexConstraint{ - cgn: cgn, - v: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// ---------- func (Value).MapKeys() []Value ---------- - -// result = v.MapKeys() -type rVMapKeysConstraint struct { - cgn *cgnode - v nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rVMapKeysConstraint) ptr() nodeid { return c.v } -func (c *rVMapKeysConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "rVMapKeys.result") -} -func (c *rVMapKeysConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.result = mapping[c.result] -} - -func (c *rVMapKeysConstraint) String() string { - return fmt.Sprintf("n%d = reflect n%d.MapKeys()", c.result, c.v) -} - -func (c *rVMapKeysConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, m, indirect := a.taggedValue(vObj) - tMap, _ := tDyn.Underlying().(*types.Map) - if tMap == nil { - continue // not a map - } - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - kObj := a.makeTagged(tMap.Key(), c.cgn, nil) - a.load(kObj+1, m, 0, a.sizeof(tMap.Key())) - if a.addLabel(c.result, kObj) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰Value۰MapKeys(a *analysis, cgn *cgnode) { - // Allocate an array for the result. - obj := a.nextNode() - T := types.NewSlice(a.reflectValueObj.Type()) - a.addNodes(sliceToArray(T), "reflect.MapKeys result") - a.endObject(obj, cgn, nil) - a.addressOf(T, a.funcResults(cgn.obj), obj) - - a.addConstraint(&rVMapKeysConstraint{ - cgn: cgn, - v: a.funcParams(cgn.obj), - result: obj + 1, // result is stored in array elems - }) -} - -func ext۰reflect۰Value۰Method(a *analysis, cgn *cgnode) {} // TODO(adonovan) -func ext۰reflect۰Value۰MethodByName(a *analysis, cgn *cgnode) {} // TODO(adonovan) - -// ---------- func (Value).Recv(Value) Value ---------- - -// result, _ = v.Recv() -type rVRecvConstraint struct { - cgn *cgnode - v nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rVRecvConstraint) ptr() nodeid { return c.v } -func (c *rVRecvConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "rVRecv.result") -} -func (c *rVRecvConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.result = mapping[c.result] -} - -func (c *rVRecvConstraint) String() string { - return fmt.Sprintf("n%d = reflect n%d.Recv()", c.result, c.v) -} - -func (c *rVRecvConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, ch, indirect := a.taggedValue(vObj) - tChan, _ := tDyn.Underlying().(*types.Chan) - if tChan == nil { - continue // not a channel - } - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - tElem := tChan.Elem() - elemObj := a.makeTagged(tElem, c.cgn, nil) - a.load(elemObj+1, ch, 0, a.sizeof(tElem)) - if a.addLabel(c.result, elemObj) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰Value۰Recv(a *analysis, cgn *cgnode) { - a.addConstraint(&rVRecvConstraint{ - cgn: cgn, - v: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// ---------- func (Value).Send(Value) ---------- - -// v.Send(x) -type rVSendConstraint struct { - cgn *cgnode - v nodeid // (ptr) - x nodeid -} - -func (c *rVSendConstraint) ptr() nodeid { return c.v } -func (c *rVSendConstraint) presolve(*hvn) {} -func (c *rVSendConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.x = mapping[c.x] -} - -func (c *rVSendConstraint) String() string { - return fmt.Sprintf("reflect n%d.Send(n%d)", c.v, c.x) -} - -func (c *rVSendConstraint) solve(a *analysis, delta *nodeset) { - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, ch, indirect := a.taggedValue(vObj) - tChan, _ := tDyn.Underlying().(*types.Chan) - if tChan == nil { - continue // not a channel - } - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - // Extract x's payload to xtmp, then store to channel. - tElem := tChan.Elem() - xtmp := a.addNodes(tElem, "Send.xtmp") - a.typeAssert(tElem, xtmp, c.x, false) - a.store(ch, xtmp, 0, a.sizeof(tElem)) - } -} - -func ext۰reflect۰Value۰Send(a *analysis, cgn *cgnode) { - params := a.funcParams(cgn.obj) - a.addConstraint(&rVSendConstraint{ - cgn: cgn, - v: params, - x: params + 1, - }) -} - -func ext۰reflect۰Value۰Set(a *analysis, cgn *cgnode) {} // TODO(adonovan) - -// ---------- func (Value).SetBytes(x []byte) ---------- - -// v.SetBytes(x) -type rVSetBytesConstraint struct { - cgn *cgnode - v nodeid // (ptr) - x nodeid -} - -func (c *rVSetBytesConstraint) ptr() nodeid { return c.v } -func (c *rVSetBytesConstraint) presolve(*hvn) {} -func (c *rVSetBytesConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.x = mapping[c.x] -} - -func (c *rVSetBytesConstraint) String() string { - return fmt.Sprintf("reflect n%d.SetBytes(n%d)", c.v, c.x) -} - -func (c *rVSetBytesConstraint) solve(a *analysis, delta *nodeset) { - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, slice, indirect := a.taggedValue(vObj) - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - tSlice, ok := tDyn.Underlying().(*types.Slice) - if ok && types.Identical(tSlice.Elem(), types.Typ[types.Uint8]) { - if a.onlineCopy(slice, c.x) { - a.addWork(slice) - } - } - } -} - -func ext۰reflect۰Value۰SetBytes(a *analysis, cgn *cgnode) { - params := a.funcParams(cgn.obj) - a.addConstraint(&rVSetBytesConstraint{ - cgn: cgn, - v: params, - x: params + 1, - }) -} - -// ---------- func (Value).SetMapIndex(k Value, v Value) ---------- - -// v.SetMapIndex(key, val) -type rVSetMapIndexConstraint struct { - cgn *cgnode - v nodeid // (ptr) - key nodeid - val nodeid -} - -func (c *rVSetMapIndexConstraint) ptr() nodeid { return c.v } -func (c *rVSetMapIndexConstraint) presolve(*hvn) {} -func (c *rVSetMapIndexConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.key = mapping[c.key] - c.val = mapping[c.val] -} - -func (c *rVSetMapIndexConstraint) String() string { - return fmt.Sprintf("reflect n%d.SetMapIndex(n%d, n%d)", c.v, c.key, c.val) -} - -func (c *rVSetMapIndexConstraint) solve(a *analysis, delta *nodeset) { - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, m, indirect := a.taggedValue(vObj) - tMap, _ := tDyn.Underlying().(*types.Map) - if tMap == nil { - continue // not a map - } - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - keysize := a.sizeof(tMap.Key()) - - // Extract key's payload to keytmp, then store to map key. - keytmp := a.addNodes(tMap.Key(), "SetMapIndex.keytmp") - a.typeAssert(tMap.Key(), keytmp, c.key, false) - a.store(m, keytmp, 0, keysize) - - // Extract val's payload to vtmp, then store to map value. - valtmp := a.addNodes(tMap.Elem(), "SetMapIndex.valtmp") - a.typeAssert(tMap.Elem(), valtmp, c.val, false) - a.store(m, valtmp, keysize, a.sizeof(tMap.Elem())) - } -} - -func ext۰reflect۰Value۰SetMapIndex(a *analysis, cgn *cgnode) { - params := a.funcParams(cgn.obj) - a.addConstraint(&rVSetMapIndexConstraint{ - cgn: cgn, - v: params, - key: params + 1, - val: params + 2, - }) -} - -func ext۰reflect۰Value۰SetPointer(a *analysis, cgn *cgnode) {} // TODO(adonovan) - -// ---------- func (Value).Slice(v Value, i, j int) Value ---------- - -// result = v.Slice(_, _) -type rVSliceConstraint struct { - cgn *cgnode - v nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rVSliceConstraint) ptr() nodeid { return c.v } -func (c *rVSliceConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "rVSlice.result") -} -func (c *rVSliceConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.result = mapping[c.result] -} - -func (c *rVSliceConstraint) String() string { - return fmt.Sprintf("n%d = reflect n%d.Slice(_, _)", c.result, c.v) -} - -func (c *rVSliceConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, payload, indirect := a.taggedValue(vObj) - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - var res nodeid - switch t := tDyn.Underlying().(type) { - case *types.Pointer: - if tArr, ok := t.Elem().Underlying().(*types.Array); ok { - // pointer to array - res = a.makeTagged(types.NewSlice(tArr.Elem()), c.cgn, nil) - if a.onlineCopy(res+1, payload) { - a.addWork(res + 1) - } - } - - case *types.Array: - // TODO(adonovan): implement addressable - // arrays when we do indirect tagged objects. - - case *types.Slice: - res = vObj - - case *types.Basic: - if t == types.Typ[types.String] { - res = vObj - } - } - - if res != 0 && a.addLabel(c.result, res) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰Value۰Slice(a *analysis, cgn *cgnode) { - a.addConstraint(&rVSliceConstraint{ - cgn: cgn, - v: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// -------------------- Standalone reflect functions -------------------- - -func ext۰reflect۰Append(a *analysis, cgn *cgnode) {} // TODO(adonovan) -func ext۰reflect۰AppendSlice(a *analysis, cgn *cgnode) {} // TODO(adonovan) -func ext۰reflect۰Copy(a *analysis, cgn *cgnode) {} // TODO(adonovan) - -// ---------- func ChanOf(ChanDir, Type) Type ---------- - -// result = ChanOf(dir, t) -type reflectChanOfConstraint struct { - cgn *cgnode - t nodeid // (ptr) - result nodeid // (indirect) - dirs []types.ChanDir -} - -func (c *reflectChanOfConstraint) ptr() nodeid { return c.t } -func (c *reflectChanOfConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "reflectChanOf.result") -} -func (c *reflectChanOfConstraint) renumber(mapping []nodeid) { - c.t = mapping[c.t] - c.result = mapping[c.result] -} - -func (c *reflectChanOfConstraint) String() string { - return fmt.Sprintf("n%d = reflect.ChanOf(n%d)", c.result, c.t) -} - -func (c *reflectChanOfConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - tObj := nodeid(x) - T := a.rtypeTaggedValue(tObj) - - if typeTooHigh(T) { - continue - } - - for _, dir := range c.dirs { - if a.addLabel(c.result, a.makeRtype(types.NewChan(dir, T))) { - changed = true - } - } - } - if changed { - a.addWork(c.result) - } -} - -// dirMap maps reflect.ChanDir to the set of channel types generated by ChanOf. -var dirMap = [...][]types.ChanDir{ - 0: {types.SendOnly, types.RecvOnly, types.SendRecv}, // unknown - reflect.RecvDir: {types.RecvOnly}, - reflect.SendDir: {types.SendOnly}, - reflect.BothDir: {types.SendRecv}, -} - -func ext۰reflect۰ChanOf(a *analysis, cgn *cgnode) { - // If we have access to the callsite, - // and the channel argument is a constant (as is usual), - // only generate the requested direction. - var dir reflect.ChanDir // unknown - if site := cgn.callersite; site != nil { - if c, ok := site.instr.Common().Args[0].(*ssa.Const); ok { - v, _ := constant.Int64Val(c.Value) - if 0 <= v && v <= int64(reflect.BothDir) { - dir = reflect.ChanDir(v) - } - } - } - - params := a.funcParams(cgn.obj) - a.addConstraint(&reflectChanOfConstraint{ - cgn: cgn, - t: params + 1, - result: a.funcResults(cgn.obj), - dirs: dirMap[dir], - }) -} - -// ---------- func Indirect(v Value) Value ---------- - -// result = Indirect(v) -type reflectIndirectConstraint struct { - cgn *cgnode - v nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *reflectIndirectConstraint) ptr() nodeid { return c.v } -func (c *reflectIndirectConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "reflectIndirect.result") -} -func (c *reflectIndirectConstraint) renumber(mapping []nodeid) { - c.v = mapping[c.v] - c.result = mapping[c.result] -} - -func (c *reflectIndirectConstraint) String() string { - return fmt.Sprintf("n%d = reflect.Indirect(n%d)", c.result, c.v) -} - -func (c *reflectIndirectConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - vObj := nodeid(x) - tDyn, _, _ := a.taggedValue(vObj) - var res nodeid - if tPtr, ok := tDyn.Underlying().(*types.Pointer); ok { - // load the payload of the pointer's tagged object - // into a new tagged object - res = a.makeTagged(tPtr.Elem(), c.cgn, nil) - a.load(res+1, vObj+1, 0, a.sizeof(tPtr.Elem())) - } else { - res = vObj - } - - if a.addLabel(c.result, res) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰Indirect(a *analysis, cgn *cgnode) { - a.addConstraint(&reflectIndirectConstraint{ - cgn: cgn, - v: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// ---------- func MakeChan(Type) Value ---------- - -// result = MakeChan(typ) -type reflectMakeChanConstraint struct { - cgn *cgnode - typ nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *reflectMakeChanConstraint) ptr() nodeid { return c.typ } -func (c *reflectMakeChanConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "reflectMakeChan.result") -} -func (c *reflectMakeChanConstraint) renumber(mapping []nodeid) { - c.typ = mapping[c.typ] - c.result = mapping[c.result] -} - -func (c *reflectMakeChanConstraint) String() string { - return fmt.Sprintf("n%d = reflect.MakeChan(n%d)", c.result, c.typ) -} - -func (c *reflectMakeChanConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - typObj := nodeid(x) - T := a.rtypeTaggedValue(typObj) - tChan, ok := T.Underlying().(*types.Chan) - if !ok || tChan.Dir() != types.SendRecv { - continue // not a bidirectional channel type - } - - obj := a.nextNode() - a.addNodes(tChan.Elem(), "reflect.MakeChan.value") - a.endObject(obj, c.cgn, nil) - - // put its address in a new T-tagged object - id := a.makeTagged(T, c.cgn, nil) - a.addLabel(id+1, obj) - - // flow the T-tagged object to the result - if a.addLabel(c.result, id) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰MakeChan(a *analysis, cgn *cgnode) { - a.addConstraint(&reflectMakeChanConstraint{ - cgn: cgn, - typ: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -func ext۰reflect۰MakeFunc(a *analysis, cgn *cgnode) {} // TODO(adonovan) - -// ---------- func MakeMap(Type) Value ---------- - -// result = MakeMap(typ) -type reflectMakeMapConstraint struct { - cgn *cgnode - typ nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *reflectMakeMapConstraint) ptr() nodeid { return c.typ } -func (c *reflectMakeMapConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "reflectMakeMap.result") -} -func (c *reflectMakeMapConstraint) renumber(mapping []nodeid) { - c.typ = mapping[c.typ] - c.result = mapping[c.result] -} - -func (c *reflectMakeMapConstraint) String() string { - return fmt.Sprintf("n%d = reflect.MakeMap(n%d)", c.result, c.typ) -} - -func (c *reflectMakeMapConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - typObj := nodeid(x) - T := a.rtypeTaggedValue(typObj) - tMap, ok := T.Underlying().(*types.Map) - if !ok { - continue // not a map type - } - - mapObj := a.nextNode() - a.addNodes(tMap.Key(), "reflect.MakeMap.key") - a.addNodes(tMap.Elem(), "reflect.MakeMap.value") - a.endObject(mapObj, c.cgn, nil) - - // put its address in a new T-tagged object - id := a.makeTagged(T, c.cgn, nil) - a.addLabel(id+1, mapObj) - - // flow the T-tagged object to the result - if a.addLabel(c.result, id) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰MakeMap(a *analysis, cgn *cgnode) { - a.addConstraint(&reflectMakeMapConstraint{ - cgn: cgn, - typ: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// ---------- func MakeSlice(Type) Value ---------- - -// result = MakeSlice(typ) -type reflectMakeSliceConstraint struct { - cgn *cgnode - typ nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *reflectMakeSliceConstraint) ptr() nodeid { return c.typ } -func (c *reflectMakeSliceConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "reflectMakeSlice.result") -} -func (c *reflectMakeSliceConstraint) renumber(mapping []nodeid) { - c.typ = mapping[c.typ] - c.result = mapping[c.result] -} - -func (c *reflectMakeSliceConstraint) String() string { - return fmt.Sprintf("n%d = reflect.MakeSlice(n%d)", c.result, c.typ) -} - -func (c *reflectMakeSliceConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - typObj := nodeid(x) - T := a.rtypeTaggedValue(typObj) - if _, ok := T.Underlying().(*types.Slice); !ok { - continue // not a slice type - } - - obj := a.nextNode() - a.addNodes(sliceToArray(T), "reflect.MakeSlice") - a.endObject(obj, c.cgn, nil) - - // put its address in a new T-tagged object - id := a.makeTagged(T, c.cgn, nil) - a.addLabel(id+1, obj) - - // flow the T-tagged object to the result - if a.addLabel(c.result, id) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰MakeSlice(a *analysis, cgn *cgnode) { - a.addConstraint(&reflectMakeSliceConstraint{ - cgn: cgn, - typ: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -func ext۰reflect۰MapOf(a *analysis, cgn *cgnode) {} // TODO(adonovan) - -// ---------- func New(Type) Value ---------- - -// result = New(typ) -type reflectNewConstraint struct { - cgn *cgnode - typ nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *reflectNewConstraint) ptr() nodeid { return c.typ } -func (c *reflectNewConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "reflectNew.result") -} -func (c *reflectNewConstraint) renumber(mapping []nodeid) { - c.typ = mapping[c.typ] - c.result = mapping[c.result] -} - -func (c *reflectNewConstraint) String() string { - return fmt.Sprintf("n%d = reflect.New(n%d)", c.result, c.typ) -} - -func (c *reflectNewConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - typObj := nodeid(x) - T := a.rtypeTaggedValue(typObj) - - // allocate new T object - newObj := a.nextNode() - a.addNodes(T, "reflect.New") - a.endObject(newObj, c.cgn, nil) - - // put its address in a new *T-tagged object - id := a.makeTagged(types.NewPointer(T), c.cgn, nil) - a.addLabel(id+1, newObj) - - // flow the pointer to the result - if a.addLabel(c.result, id) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰New(a *analysis, cgn *cgnode) { - a.addConstraint(&reflectNewConstraint{ - cgn: cgn, - typ: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -func ext۰reflect۰NewAt(a *analysis, cgn *cgnode) { - ext۰reflect۰New(a, cgn) - - // TODO(adonovan): also report dynamic calls to unsound intrinsics. - if site := cgn.callersite; site != nil { - a.warnf(site.pos(), "unsound: %s contains a reflect.NewAt() call", site.instr.Parent()) - } -} - -// ---------- func PtrTo(Type) Type ---------- - -// result = PtrTo(t) -type reflectPtrToConstraint struct { - cgn *cgnode - t nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *reflectPtrToConstraint) ptr() nodeid { return c.t } -func (c *reflectPtrToConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "reflectPtrTo.result") -} -func (c *reflectPtrToConstraint) renumber(mapping []nodeid) { - c.t = mapping[c.t] - c.result = mapping[c.result] -} - -func (c *reflectPtrToConstraint) String() string { - return fmt.Sprintf("n%d = reflect.PtrTo(n%d)", c.result, c.t) -} - -func (c *reflectPtrToConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - tObj := nodeid(x) - T := a.rtypeTaggedValue(tObj) - - if typeTooHigh(T) { - continue - } - - if a.addLabel(c.result, a.makeRtype(types.NewPointer(T))) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰PtrTo(a *analysis, cgn *cgnode) { - a.addConstraint(&reflectPtrToConstraint{ - cgn: cgn, - t: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -func ext۰reflect۰Select(a *analysis, cgn *cgnode) {} // TODO(adonovan) - -// ---------- func SliceOf(Type) Type ---------- - -// result = SliceOf(t) -type reflectSliceOfConstraint struct { - cgn *cgnode - t nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *reflectSliceOfConstraint) ptr() nodeid { return c.t } -func (c *reflectSliceOfConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "reflectSliceOf.result") -} -func (c *reflectSliceOfConstraint) renumber(mapping []nodeid) { - c.t = mapping[c.t] - c.result = mapping[c.result] -} - -func (c *reflectSliceOfConstraint) String() string { - return fmt.Sprintf("n%d = reflect.SliceOf(n%d)", c.result, c.t) -} - -func (c *reflectSliceOfConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - tObj := nodeid(x) - T := a.rtypeTaggedValue(tObj) - - if typeTooHigh(T) { - continue - } - - if a.addLabel(c.result, a.makeRtype(types.NewSlice(T))) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰SliceOf(a *analysis, cgn *cgnode) { - a.addConstraint(&reflectSliceOfConstraint{ - cgn: cgn, - t: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// ---------- func TypeOf(v Value) Type ---------- - -// result = TypeOf(i) -type reflectTypeOfConstraint struct { - cgn *cgnode - i nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *reflectTypeOfConstraint) ptr() nodeid { return c.i } -func (c *reflectTypeOfConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "reflectTypeOf.result") -} -func (c *reflectTypeOfConstraint) renumber(mapping []nodeid) { - c.i = mapping[c.i] - c.result = mapping[c.result] -} - -func (c *reflectTypeOfConstraint) String() string { - return fmt.Sprintf("n%d = reflect.TypeOf(n%d)", c.result, c.i) -} - -func (c *reflectTypeOfConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - iObj := nodeid(x) - tDyn, _, _ := a.taggedValue(iObj) - if a.addLabel(c.result, a.makeRtype(tDyn)) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰TypeOf(a *analysis, cgn *cgnode) { - a.addConstraint(&reflectTypeOfConstraint{ - cgn: cgn, - i: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// ---------- func ValueOf(interface{}) Value ---------- - -func ext۰reflect۰ValueOf(a *analysis, cgn *cgnode) { - // TODO(adonovan): when we start creating indirect tagged - // objects, we'll need to handle them specially here since - // they must never appear in the PTS of an interface{}. - a.copy(a.funcResults(cgn.obj), a.funcParams(cgn.obj), 1) -} - -// ---------- func Zero(Type) Value ---------- - -// result = Zero(typ) -type reflectZeroConstraint struct { - cgn *cgnode - typ nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *reflectZeroConstraint) ptr() nodeid { return c.typ } -func (c *reflectZeroConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "reflectZero.result") -} -func (c *reflectZeroConstraint) renumber(mapping []nodeid) { - c.typ = mapping[c.typ] - c.result = mapping[c.result] -} - -func (c *reflectZeroConstraint) String() string { - return fmt.Sprintf("n%d = reflect.Zero(n%d)", c.result, c.typ) -} - -func (c *reflectZeroConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - typObj := nodeid(x) - T := a.rtypeTaggedValue(typObj) - - // TODO(adonovan): if T is an interface type, we need - // to create an indirect tagged object containing - // new(T). To avoid updates of such shared values, - // we'll need another flag on indirect tagged objects - // that marks whether they are addressable or - // readonly, just like the reflect package does. - - // memoize using a.reflectZeros[T] - var id nodeid - if z := a.reflectZeros.At(T); false && z != nil { - id = z.(nodeid) - } else { - id = a.makeTagged(T, c.cgn, nil) - a.reflectZeros.Set(T, id) - } - if a.addLabel(c.result, id) { - changed = true - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰Zero(a *analysis, cgn *cgnode) { - a.addConstraint(&reflectZeroConstraint{ - cgn: cgn, - typ: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// -------------------- (*reflect.rtype) methods -------------------- - -// ---------- func (*rtype) Elem() Type ---------- - -// result = Elem(t) -type rtypeElemConstraint struct { - cgn *cgnode - t nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rtypeElemConstraint) ptr() nodeid { return c.t } -func (c *rtypeElemConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "rtypeElem.result") -} -func (c *rtypeElemConstraint) renumber(mapping []nodeid) { - c.t = mapping[c.t] - c.result = mapping[c.result] -} - -func (c *rtypeElemConstraint) String() string { - return fmt.Sprintf("n%d = (*reflect.rtype).Elem(n%d)", c.result, c.t) -} - -func (c *rtypeElemConstraint) solve(a *analysis, delta *nodeset) { - // Implemented by *types.{Map,Chan,Array,Slice,Pointer}. - type hasElem interface { - Elem() types.Type - } - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - tObj := nodeid(x) - T := a.nodes[tObj].obj.data.(types.Type) - if tHasElem, ok := T.Underlying().(hasElem); ok { - if a.addLabel(c.result, a.makeRtype(tHasElem.Elem())) { - changed = true - } - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰rtype۰Elem(a *analysis, cgn *cgnode) { - a.addConstraint(&rtypeElemConstraint{ - cgn: cgn, - t: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// ---------- func (*rtype) Field(int) StructField ---------- -// ---------- func (*rtype) FieldByName(string) (StructField, bool) ---------- - -// result = FieldByName(t, name) -// result = Field(t, _) -type rtypeFieldByNameConstraint struct { - cgn *cgnode - name string // name of field; "" for unknown - t nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rtypeFieldByNameConstraint) ptr() nodeid { return c.t } -func (c *rtypeFieldByNameConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result+3), "rtypeFieldByName.result.Type") -} -func (c *rtypeFieldByNameConstraint) renumber(mapping []nodeid) { - c.t = mapping[c.t] - c.result = mapping[c.result] -} - -func (c *rtypeFieldByNameConstraint) String() string { - return fmt.Sprintf("n%d = (*reflect.rtype).FieldByName(n%d, %q)", c.result, c.t, c.name) -} - -func (c *rtypeFieldByNameConstraint) solve(a *analysis, delta *nodeset) { - // type StructField struct { - // 0 __identity__ - // 1 Name string - // 2 PkgPath string - // 3 Type Type - // 4 Tag StructTag - // 5 Offset uintptr - // 6 Index []int - // 7 Anonymous bool - // } - - for _, x := range delta.AppendTo(a.deltaSpace) { - tObj := nodeid(x) - T := a.nodes[tObj].obj.data.(types.Type) - tStruct, ok := T.Underlying().(*types.Struct) - if !ok { - continue // not a struct type - } - - n := tStruct.NumFields() - for i := 0; i < n; i++ { - f := tStruct.Field(i) - if c.name == "" || c.name == f.Name() { - - // a.offsetOf(Type) is 3. - if id := c.result + 3; a.addLabel(id, a.makeRtype(f.Type())) { - a.addWork(id) - } - // TODO(adonovan): StructField.Index should be non-nil. - } - } - } -} - -func ext۰reflect۰rtype۰FieldByName(a *analysis, cgn *cgnode) { - // If we have access to the callsite, - // and the argument is a string constant, - // return only that field. - var name string - if site := cgn.callersite; site != nil { - if c, ok := site.instr.Common().Args[0].(*ssa.Const); ok { - name = constant.StringVal(c.Value) - } - } - - a.addConstraint(&rtypeFieldByNameConstraint{ - cgn: cgn, - name: name, - t: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -func ext۰reflect۰rtype۰Field(a *analysis, cgn *cgnode) { - // No-one ever calls Field with a constant argument, - // so we don't specialize that case. - a.addConstraint(&rtypeFieldByNameConstraint{ - cgn: cgn, - t: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -func ext۰reflect۰rtype۰FieldByIndex(a *analysis, cgn *cgnode) {} // TODO(adonovan) -func ext۰reflect۰rtype۰FieldByNameFunc(a *analysis, cgn *cgnode) {} // TODO(adonovan) - -// ---------- func (*rtype) In/Out(i int) Type ---------- - -// result = In/Out(t, i) -type rtypeInOutConstraint struct { - cgn *cgnode - t nodeid // (ptr) - result nodeid // (indirect) - out bool - i int // -ve if not a constant -} - -func (c *rtypeInOutConstraint) ptr() nodeid { return c.t } -func (c *rtypeInOutConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "rtypeInOut.result") -} -func (c *rtypeInOutConstraint) renumber(mapping []nodeid) { - c.t = mapping[c.t] - c.result = mapping[c.result] -} - -func (c *rtypeInOutConstraint) String() string { - return fmt.Sprintf("n%d = (*reflect.rtype).InOut(n%d, %d)", c.result, c.t, c.i) -} - -func (c *rtypeInOutConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - tObj := nodeid(x) - T := a.nodes[tObj].obj.data.(types.Type) - sig, ok := T.Underlying().(*types.Signature) - if !ok { - continue // not a func type - } - - tuple := sig.Params() - if c.out { - tuple = sig.Results() - } - for i, n := 0, tuple.Len(); i < n; i++ { - if c.i < 0 || c.i == i { - if a.addLabel(c.result, a.makeRtype(tuple.At(i).Type())) { - changed = true - } - } - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰rtype۰InOut(a *analysis, cgn *cgnode, out bool) { - // If we have access to the callsite, - // and the argument is an int constant, - // return only that parameter. - index := -1 - if site := cgn.callersite; site != nil { - if c, ok := site.instr.Common().Args[0].(*ssa.Const); ok { - v, _ := constant.Int64Val(c.Value) - index = int(v) - } - } - a.addConstraint(&rtypeInOutConstraint{ - cgn: cgn, - t: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - out: out, - i: index, - }) -} - -func ext۰reflect۰rtype۰In(a *analysis, cgn *cgnode) { - ext۰reflect۰rtype۰InOut(a, cgn, false) -} - -func ext۰reflect۰rtype۰Out(a *analysis, cgn *cgnode) { - ext۰reflect۰rtype۰InOut(a, cgn, true) -} - -// ---------- func (*rtype) Key() Type ---------- - -// result = Key(t) -type rtypeKeyConstraint struct { - cgn *cgnode - t nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rtypeKeyConstraint) ptr() nodeid { return c.t } -func (c *rtypeKeyConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result), "rtypeKey.result") -} -func (c *rtypeKeyConstraint) renumber(mapping []nodeid) { - c.t = mapping[c.t] - c.result = mapping[c.result] -} - -func (c *rtypeKeyConstraint) String() string { - return fmt.Sprintf("n%d = (*reflect.rtype).Key(n%d)", c.result, c.t) -} - -func (c *rtypeKeyConstraint) solve(a *analysis, delta *nodeset) { - changed := false - for _, x := range delta.AppendTo(a.deltaSpace) { - tObj := nodeid(x) - T := a.nodes[tObj].obj.data.(types.Type) - if tMap, ok := T.Underlying().(*types.Map); ok { - if a.addLabel(c.result, a.makeRtype(tMap.Key())) { - changed = true - } - } - } - if changed { - a.addWork(c.result) - } -} - -func ext۰reflect۰rtype۰Key(a *analysis, cgn *cgnode) { - a.addConstraint(&rtypeKeyConstraint{ - cgn: cgn, - t: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// ---------- func (*rtype) Method(int) (Method, bool) ---------- -// ---------- func (*rtype) MethodByName(string) (Method, bool) ---------- - -// result = MethodByName(t, name) -// result = Method(t, _) -type rtypeMethodByNameConstraint struct { - cgn *cgnode - name string // name of method; "" for unknown - t nodeid // (ptr) - result nodeid // (indirect) -} - -func (c *rtypeMethodByNameConstraint) ptr() nodeid { return c.t } -func (c *rtypeMethodByNameConstraint) presolve(h *hvn) { - h.markIndirect(onodeid(c.result+3), "rtypeMethodByName.result.Type") - h.markIndirect(onodeid(c.result+4), "rtypeMethodByName.result.Func") -} -func (c *rtypeMethodByNameConstraint) renumber(mapping []nodeid) { - c.t = mapping[c.t] - c.result = mapping[c.result] -} - -func (c *rtypeMethodByNameConstraint) String() string { - return fmt.Sprintf("n%d = (*reflect.rtype).MethodByName(n%d, %q)", c.result, c.t, c.name) -} - -// changeRecv returns sig with Recv prepended to Params(). -func changeRecv(sig *types.Signature) *types.Signature { - params := sig.Params() - n := params.Len() - p2 := make([]*types.Var, n+1) - p2[0] = sig.Recv() - for i := 0; i < n; i++ { - p2[i+1] = params.At(i) - } - return types.NewSignature(nil, types.NewTuple(p2...), sig.Results(), sig.Variadic()) -} - -func (c *rtypeMethodByNameConstraint) solve(a *analysis, delta *nodeset) { - for _, x := range delta.AppendTo(a.deltaSpace) { - tObj := nodeid(x) - T := a.nodes[tObj].obj.data.(types.Type) - - isIface := isInterface(T) - - // We don't use Lookup(c.name) when c.name != "" to avoid - // ambiguity: >1 unexported methods could match. - mset := a.prog.MethodSets.MethodSet(T) - for i, n := 0, mset.Len(); i < n; i++ { - sel := mset.At(i) - if c.name == "" || c.name == sel.Obj().Name() { - // type Method struct { - // 0 __identity__ - // 1 Name string - // 2 PkgPath string - // 3 Type Type - // 4 Func Value - // 5 Index int - // } - - var sig *types.Signature - var fn *ssa.Function - if isIface { - sig = sel.Type().(*types.Signature) - } else { - fn = a.prog.MethodValue(sel) - // move receiver to params[0] - sig = changeRecv(fn.Signature) - } - - // a.offsetOf(Type) is 3. - if id := c.result + 3; a.addLabel(id, a.makeRtype(sig)) { - a.addWork(id) - } - if fn != nil { - // a.offsetOf(Func) is 4. - if id := c.result + 4; a.addLabel(id, a.objectNode(nil, fn)) { - a.addWork(id) - } - } - } - } - } -} - -func ext۰reflect۰rtype۰MethodByName(a *analysis, cgn *cgnode) { - // If we have access to the callsite, - // and the argument is a string constant, - // return only that method. - var name string - if site := cgn.callersite; site != nil { - if c, ok := site.instr.Common().Args[0].(*ssa.Const); ok { - name = constant.StringVal(c.Value) - } - } - - a.addConstraint(&rtypeMethodByNameConstraint{ - cgn: cgn, - name: name, - t: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -func ext۰reflect۰rtype۰Method(a *analysis, cgn *cgnode) { - // No-one ever calls Method with a constant argument, - // so we don't specialize that case. - a.addConstraint(&rtypeMethodByNameConstraint{ - cgn: cgn, - t: a.funcParams(cgn.obj), - result: a.funcResults(cgn.obj), - }) -} - -// typeHeight returns the "height" of the type, which is roughly -// speaking the number of chan, map, pointer and slice type constructors -// at the root of T; these are the four type kinds that can be created -// via reflection. Chan and map constructors are counted as double the -// height of slice and pointer constructors since they are less often -// deeply nested. -// -// The solver rules for type constructors must somehow bound the set of -// types they create to ensure termination of the algorithm in cases -// where the output of a type constructor flows to its input, e.g. -// -// func f(t reflect.Type) { -// f(reflect.PtrTo(t)) -// } -// -// It does this by limiting the type height to k, but this still leaves -// a potentially exponential (4^k) number of of types that may be -// enumerated in pathological cases. -// -func typeHeight(T types.Type) int { - switch T := T.(type) { - case *types.Chan: - return 2 + typeHeight(T.Elem()) - case *types.Map: - k := typeHeight(T.Key()) - v := typeHeight(T.Elem()) - if v > k { - k = v // max(k, v) - } - return 2 + k - case *types.Slice: - return 1 + typeHeight(T.Elem()) - case *types.Pointer: - return 1 + typeHeight(T.Elem()) - } - return 0 -} - -func typeTooHigh(T types.Type) bool { - return typeHeight(T) > 3 -} diff --git a/vendor/golang.org/x/tools/go/pointer/solve.go b/vendor/golang.org/x/tools/go/pointer/solve.go deleted file mode 100644 index 0fdd098b012..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/solve.go +++ /dev/null @@ -1,370 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -// This file defines a naive Andersen-style solver for the inclusion -// constraint system. - -import ( - "fmt" - "go/types" -) - -type solverState struct { - complex []constraint // complex constraints attached to this node - copyTo nodeset // simple copy constraint edges - pts nodeset // points-to set of this node - prevPTS nodeset // pts(n) in previous iteration (for difference propagation) -} - -func (a *analysis) solve() { - start("Solving") - if a.log != nil { - fmt.Fprintf(a.log, "\n\n==== Solving constraints\n\n") - } - - // Solver main loop. - var delta nodeset - for { - // Add new constraints to the graph: - // static constraints from SSA on round 1, - // dynamic constraints from reflection thereafter. - a.processNewConstraints() - - var x int - if !a.work.TakeMin(&x) { - break // empty - } - id := nodeid(x) - if a.log != nil { - fmt.Fprintf(a.log, "\tnode n%d\n", id) - } - - n := a.nodes[id] - - // Difference propagation. - delta.Difference(&n.solve.pts.Sparse, &n.solve.prevPTS.Sparse) - if delta.IsEmpty() { - continue - } - if a.log != nil { - fmt.Fprintf(a.log, "\t\tpts(n%d : %s) = %s + %s\n", - id, n.typ, &delta, &n.solve.prevPTS) - } - n.solve.prevPTS.Copy(&n.solve.pts.Sparse) - - // Apply all resolution rules attached to n. - a.solveConstraints(n, &delta) - - if a.log != nil { - fmt.Fprintf(a.log, "\t\tpts(n%d) = %s\n", id, &n.solve.pts) - } - } - - if !a.nodes[0].solve.pts.IsEmpty() { - panic(fmt.Sprintf("pts(0) is nonempty: %s", &a.nodes[0].solve.pts)) - } - - // Release working state (but keep final PTS). - for _, n := range a.nodes { - n.solve.complex = nil - n.solve.copyTo.Clear() - n.solve.prevPTS.Clear() - } - - if a.log != nil { - fmt.Fprintf(a.log, "Solver done\n") - - // Dump solution. - for i, n := range a.nodes { - if !n.solve.pts.IsEmpty() { - fmt.Fprintf(a.log, "pts(n%d) = %s : %s\n", i, &n.solve.pts, n.typ) - } - } - } - stop("Solving") -} - -// processNewConstraints takes the new constraints from a.constraints -// and adds them to the graph, ensuring -// that new constraints are applied to pre-existing labels and -// that pre-existing constraints are applied to new labels. -// -func (a *analysis) processNewConstraints() { - // Take the slice of new constraints. - // (May grow during call to solveConstraints.) - constraints := a.constraints - a.constraints = nil - - // Initialize points-to sets from addr-of (base) constraints. - for _, c := range constraints { - if c, ok := c.(*addrConstraint); ok { - dst := a.nodes[c.dst] - dst.solve.pts.add(c.src) - - // Populate the worklist with nodes that point to - // something initially (due to addrConstraints) and - // have other constraints attached. - // (A no-op in round 1.) - if !dst.solve.copyTo.IsEmpty() || len(dst.solve.complex) > 0 { - a.addWork(c.dst) - } - } - } - - // Attach simple (copy) and complex constraints to nodes. - var stale nodeset - for _, c := range constraints { - var id nodeid - switch c := c.(type) { - case *addrConstraint: - // base constraints handled in previous loop - continue - case *copyConstraint: - // simple (copy) constraint - id = c.src - a.nodes[id].solve.copyTo.add(c.dst) - default: - // complex constraint - id = c.ptr() - solve := a.nodes[id].solve - solve.complex = append(solve.complex, c) - } - - if n := a.nodes[id]; !n.solve.pts.IsEmpty() { - if !n.solve.prevPTS.IsEmpty() { - stale.add(id) - } - a.addWork(id) - } - } - // Apply new constraints to pre-existing PTS labels. - var space [50]int - for _, id := range stale.AppendTo(space[:0]) { - n := a.nodes[nodeid(id)] - a.solveConstraints(n, &n.solve.prevPTS) - } -} - -// solveConstraints applies each resolution rule attached to node n to -// the set of labels delta. It may generate new constraints in -// a.constraints. -// -func (a *analysis) solveConstraints(n *node, delta *nodeset) { - if delta.IsEmpty() { - return - } - - // Process complex constraints dependent on n. - for _, c := range n.solve.complex { - if a.log != nil { - fmt.Fprintf(a.log, "\t\tconstraint %s\n", c) - } - c.solve(a, delta) - } - - // Process copy constraints. - var copySeen nodeset - for _, x := range n.solve.copyTo.AppendTo(a.deltaSpace) { - mid := nodeid(x) - if copySeen.add(mid) { - if a.nodes[mid].solve.pts.addAll(delta) { - a.addWork(mid) - } - } - } -} - -// addLabel adds label to the points-to set of ptr and reports whether the set grew. -func (a *analysis) addLabel(ptr, label nodeid) bool { - b := a.nodes[ptr].solve.pts.add(label) - if b && a.log != nil { - fmt.Fprintf(a.log, "\t\tpts(n%d) += n%d\n", ptr, label) - } - return b -} - -func (a *analysis) addWork(id nodeid) { - a.work.Insert(int(id)) - if a.log != nil { - fmt.Fprintf(a.log, "\t\twork: n%d\n", id) - } -} - -// onlineCopy adds a copy edge. It is called online, i.e. during -// solving, so it adds edges and pts members directly rather than by -// instantiating a 'constraint'. -// -// The size of the copy is implicitly 1. -// It returns true if pts(dst) changed. -// -func (a *analysis) onlineCopy(dst, src nodeid) bool { - if dst != src { - if nsrc := a.nodes[src]; nsrc.solve.copyTo.add(dst) { - if a.log != nil { - fmt.Fprintf(a.log, "\t\t\tdynamic copy n%d <- n%d\n", dst, src) - } - // TODO(adonovan): most calls to onlineCopy - // are followed by addWork, possibly batched - // via a 'changed' flag; see if there's a - // noticeable penalty to calling addWork here. - return a.nodes[dst].solve.pts.addAll(&nsrc.solve.pts) - } - } - return false -} - -// Returns sizeof. -// Implicitly adds nodes to worklist. -// -// TODO(adonovan): now that we support a.copy() during solving, we -// could eliminate onlineCopyN, but it's much slower. Investigate. -// -func (a *analysis) onlineCopyN(dst, src nodeid, sizeof uint32) uint32 { - for i := uint32(0); i < sizeof; i++ { - if a.onlineCopy(dst, src) { - a.addWork(dst) - } - src++ - dst++ - } - return sizeof -} - -func (c *loadConstraint) solve(a *analysis, delta *nodeset) { - var changed bool - for _, x := range delta.AppendTo(a.deltaSpace) { - k := nodeid(x) - koff := k + nodeid(c.offset) - if a.onlineCopy(c.dst, koff) { - changed = true - } - } - if changed { - a.addWork(c.dst) - } -} - -func (c *storeConstraint) solve(a *analysis, delta *nodeset) { - for _, x := range delta.AppendTo(a.deltaSpace) { - k := nodeid(x) - koff := k + nodeid(c.offset) - if a.onlineCopy(koff, c.src) { - a.addWork(koff) - } - } -} - -func (c *offsetAddrConstraint) solve(a *analysis, delta *nodeset) { - dst := a.nodes[c.dst] - for _, x := range delta.AppendTo(a.deltaSpace) { - k := nodeid(x) - if dst.solve.pts.add(k + nodeid(c.offset)) { - a.addWork(c.dst) - } - } -} - -func (c *typeFilterConstraint) solve(a *analysis, delta *nodeset) { - for _, x := range delta.AppendTo(a.deltaSpace) { - ifaceObj := nodeid(x) - tDyn, _, indirect := a.taggedValue(ifaceObj) - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - if types.AssignableTo(tDyn, c.typ) { - if a.addLabel(c.dst, ifaceObj) { - a.addWork(c.dst) - } - } - } -} - -func (c *untagConstraint) solve(a *analysis, delta *nodeset) { - predicate := types.AssignableTo - if c.exact { - predicate = types.Identical - } - for _, x := range delta.AppendTo(a.deltaSpace) { - ifaceObj := nodeid(x) - tDyn, v, indirect := a.taggedValue(ifaceObj) - if indirect { - // TODO(adonovan): we'll need to implement this - // when we start creating indirect tagged objects. - panic("indirect tagged object") - } - - if predicate(tDyn, c.typ) { - // Copy payload sans tag to dst. - // - // TODO(adonovan): opt: if tDyn is - // nonpointerlike we can skip this entire - // constraint, perhaps. We only care about - // pointers among the fields. - a.onlineCopyN(c.dst, v, a.sizeof(tDyn)) - } - } -} - -func (c *invokeConstraint) solve(a *analysis, delta *nodeset) { - for _, x := range delta.AppendTo(a.deltaSpace) { - ifaceObj := nodeid(x) - tDyn, v, indirect := a.taggedValue(ifaceObj) - if indirect { - // TODO(adonovan): we may need to implement this if - // we ever apply invokeConstraints to reflect.Value PTSs, - // e.g. for (reflect.Value).Call. - panic("indirect tagged object") - } - - // Look up the concrete method. - fn := a.prog.LookupMethod(tDyn, c.method.Pkg(), c.method.Name()) - if fn == nil { - panic(fmt.Sprintf("n%d: no ssa.Function for %s", c.iface, c.method)) - } - sig := fn.Signature - - fnObj := a.globalobj[fn] // dynamic calls use shared contour - if fnObj == 0 { - // a.objectNode(fn) was not called during gen phase. - panic(fmt.Sprintf("a.globalobj[%s]==nil", fn)) - } - - // Make callsite's fn variable point to identity of - // concrete method. (There's no need to add it to - // worklist since it never has attached constraints.) - a.addLabel(c.params, fnObj) - - // Extract value and connect to method's receiver. - // Copy payload to method's receiver param (arg0). - arg0 := a.funcParams(fnObj) - recvSize := a.sizeof(sig.Recv().Type()) - a.onlineCopyN(arg0, v, recvSize) - - src := c.params + 1 // skip past identity - dst := arg0 + nodeid(recvSize) - - // Copy caller's argument block to method formal parameters. - paramsSize := a.sizeof(sig.Params()) - a.onlineCopyN(dst, src, paramsSize) - src += nodeid(paramsSize) - dst += nodeid(paramsSize) - - // Copy method results to caller's result block. - resultsSize := a.sizeof(sig.Results()) - a.onlineCopyN(src, dst, resultsSize) - } -} - -func (c *addrConstraint) solve(a *analysis, delta *nodeset) { - panic("addr is not a complex constraint") -} - -func (c *copyConstraint) solve(a *analysis, delta *nodeset) { - panic("copy is not a complex constraint") -} diff --git a/vendor/golang.org/x/tools/go/pointer/util.go b/vendor/golang.org/x/tools/go/pointer/util.go deleted file mode 100644 index 683fddddcfd..00000000000 --- a/vendor/golang.org/x/tools/go/pointer/util.go +++ /dev/null @@ -1,313 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package pointer - -import ( - "bytes" - "fmt" - "go/types" - "log" - "os" - "os/exec" - "runtime" - "time" - - "golang.org/x/tools/container/intsets" -) - -// CanPoint reports whether the type T is pointerlike, -// for the purposes of this analysis. -func CanPoint(T types.Type) bool { - switch T := T.(type) { - case *types.Named: - if obj := T.Obj(); obj.Name() == "Value" && obj.Pkg().Path() == "reflect" { - return true // treat reflect.Value like interface{} - } - return CanPoint(T.Underlying()) - case *types.Pointer, *types.Interface, *types.Map, *types.Chan, *types.Signature, *types.Slice: - return true - } - - return false // array struct tuple builtin basic -} - -// CanHaveDynamicTypes reports whether the type T can "hold" dynamic types, -// i.e. is an interface (incl. reflect.Type) or a reflect.Value. -// -func CanHaveDynamicTypes(T types.Type) bool { - switch T := T.(type) { - case *types.Named: - if obj := T.Obj(); obj.Name() == "Value" && obj.Pkg().Path() == "reflect" { - return true // reflect.Value - } - return CanHaveDynamicTypes(T.Underlying()) - case *types.Interface: - return true - } - return false -} - -func isInterface(T types.Type) bool { return types.IsInterface(T) } - -// mustDeref returns the element type of its argument, which must be a -// pointer; panic ensues otherwise. -func mustDeref(typ types.Type) types.Type { - return typ.Underlying().(*types.Pointer).Elem() -} - -// deref returns a pointer's element type; otherwise it returns typ. -func deref(typ types.Type) types.Type { - if p, ok := typ.Underlying().(*types.Pointer); ok { - return p.Elem() - } - return typ -} - -// A fieldInfo describes one subelement (node) of the flattening-out -// of a type T: the subelement's type and its path from the root of T. -// -// For example, for this type: -// type line struct{ points []struct{x, y int} } -// flatten() of the inner struct yields the following []fieldInfo: -// struct{ x, y int } "" -// int ".x" -// int ".y" -// and flatten(line) yields: -// struct{ points []struct{x, y int} } "" -// struct{ x, y int } ".points[*]" -// int ".points[*].x -// int ".points[*].y" -// -type fieldInfo struct { - typ types.Type - - // op and tail describe the path to the element (e.g. ".a#2.b[*].c"). - op interface{} // *Array: true; *Tuple: int; *Struct: *types.Var; *Named: nil - tail *fieldInfo -} - -// path returns a user-friendly string describing the subelement path. -// -func (fi *fieldInfo) path() string { - var buf bytes.Buffer - for p := fi; p != nil; p = p.tail { - switch op := p.op.(type) { - case bool: - fmt.Fprintf(&buf, "[*]") - case int: - fmt.Fprintf(&buf, "#%d", op) - case *types.Var: - fmt.Fprintf(&buf, ".%s", op.Name()) - } - } - return buf.String() -} - -// flatten returns a list of directly contained fields in the preorder -// traversal of the type tree of t. The resulting elements are all -// scalars (basic types or pointerlike types), except for struct/array -// "identity" nodes, whose type is that of the aggregate. -// -// reflect.Value is considered pointerlike, similar to interface{}. -// -// Callers must not mutate the result. -// -func (a *analysis) flatten(t types.Type) []*fieldInfo { - fl, ok := a.flattenMemo[t] - if !ok { - switch t := t.(type) { - case *types.Named: - u := t.Underlying() - if isInterface(u) { - // Debuggability hack: don't remove - // the named type from interfaces as - // they're very verbose. - fl = append(fl, &fieldInfo{typ: t}) - } else { - fl = a.flatten(u) - } - - case *types.Basic, - *types.Signature, - *types.Chan, - *types.Map, - *types.Interface, - *types.Slice, - *types.Pointer: - fl = append(fl, &fieldInfo{typ: t}) - - case *types.Array: - fl = append(fl, &fieldInfo{typ: t}) // identity node - for _, fi := range a.flatten(t.Elem()) { - fl = append(fl, &fieldInfo{typ: fi.typ, op: true, tail: fi}) - } - - case *types.Struct: - fl = append(fl, &fieldInfo{typ: t}) // identity node - for i, n := 0, t.NumFields(); i < n; i++ { - f := t.Field(i) - for _, fi := range a.flatten(f.Type()) { - fl = append(fl, &fieldInfo{typ: fi.typ, op: f, tail: fi}) - } - } - - case *types.Tuple: - // No identity node: tuples are never address-taken. - n := t.Len() - if n == 1 { - // Don't add a fieldInfo link for singletons, - // e.g. in params/results. - fl = append(fl, a.flatten(t.At(0).Type())...) - } else { - for i := 0; i < n; i++ { - f := t.At(i) - for _, fi := range a.flatten(f.Type()) { - fl = append(fl, &fieldInfo{typ: fi.typ, op: i, tail: fi}) - } - } - } - - default: - panic(fmt.Sprintf("cannot flatten unsupported type %T", t)) - } - - a.flattenMemo[t] = fl - } - - return fl -} - -// sizeof returns the number of pointerlike abstractions (nodes) in the type t. -func (a *analysis) sizeof(t types.Type) uint32 { - return uint32(len(a.flatten(t))) -} - -// shouldTrack reports whether object type T contains (recursively) -// any fields whose addresses should be tracked. -func (a *analysis) shouldTrack(T types.Type) bool { - if a.track == trackAll { - return true // fast path - } - track, ok := a.trackTypes[T] - if !ok { - a.trackTypes[T] = true // break cycles conservatively - // NB: reflect.Value, reflect.Type are pre-populated to true. - for _, fi := range a.flatten(T) { - switch ft := fi.typ.Underlying().(type) { - case *types.Interface, *types.Signature: - track = true // needed for callgraph - case *types.Basic: - // no-op - case *types.Chan: - track = a.track&trackChan != 0 || a.shouldTrack(ft.Elem()) - case *types.Map: - track = a.track&trackMap != 0 || a.shouldTrack(ft.Key()) || a.shouldTrack(ft.Elem()) - case *types.Slice: - track = a.track&trackSlice != 0 || a.shouldTrack(ft.Elem()) - case *types.Pointer: - track = a.track&trackPtr != 0 || a.shouldTrack(ft.Elem()) - case *types.Array, *types.Struct: - // No need to look at field types since they will follow (flattened). - default: - // Includes *types.Tuple, which are never address-taken. - panic(ft) - } - if track { - break - } - } - a.trackTypes[T] = track - if !track && a.log != nil { - fmt.Fprintf(a.log, "\ttype not tracked: %s\n", T) - } - } - return track -} - -// offsetOf returns the (abstract) offset of field index within struct -// or tuple typ. -func (a *analysis) offsetOf(typ types.Type, index int) uint32 { - var offset uint32 - switch t := typ.Underlying().(type) { - case *types.Tuple: - for i := 0; i < index; i++ { - offset += a.sizeof(t.At(i).Type()) - } - case *types.Struct: - offset++ // the node for the struct itself - for i := 0; i < index; i++ { - offset += a.sizeof(t.Field(i).Type()) - } - default: - panic(fmt.Sprintf("offsetOf(%s : %T)", typ, typ)) - } - return offset -} - -// sliceToArray returns the type representing the arrays to which -// slice type slice points. -func sliceToArray(slice types.Type) *types.Array { - return types.NewArray(slice.Underlying().(*types.Slice).Elem(), 1) -} - -// Node set ------------------------------------------------------------------- - -type nodeset struct { - intsets.Sparse -} - -func (ns *nodeset) String() string { - var buf bytes.Buffer - buf.WriteRune('{') - var space [50]int - for i, n := range ns.AppendTo(space[:0]) { - if i > 0 { - buf.WriteString(", ") - } - buf.WriteRune('n') - fmt.Fprintf(&buf, "%d", n) - } - buf.WriteRune('}') - return buf.String() -} - -func (ns *nodeset) add(n nodeid) bool { - return ns.Sparse.Insert(int(n)) -} - -func (x *nodeset) addAll(y *nodeset) bool { - return x.UnionWith(&y.Sparse) -} - -// Profiling & debugging ------------------------------------------------------- - -var timers = make(map[string]time.Time) - -func start(name string) { - if debugTimers { - timers[name] = time.Now() - log.Printf("%s...\n", name) - } -} - -func stop(name string) { - if debugTimers { - log.Printf("%s took %s\n", name, time.Since(timers[name])) - } -} - -// diff runs the command "diff a b" and reports its success. -func diff(a, b string) bool { - var cmd *exec.Cmd - switch runtime.GOOS { - case "plan9": - cmd = exec.Command("/bin/diff", "-c", a, b) - default: - cmd = exec.Command("/usr/bin/diff", "-u", a, b) - } - cmd.Stdout = os.Stderr - cmd.Stderr = os.Stderr - return cmd.Run() == nil -} diff --git a/vendor/golang.org/x/tools/go/ssa/blockopt.go b/vendor/golang.org/x/tools/go/ssa/blockopt.go deleted file mode 100644 index e79260a21a2..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/blockopt.go +++ /dev/null @@ -1,187 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// Simple block optimizations to simplify the control flow graph. - -// TODO(adonovan): opt: instead of creating several "unreachable" blocks -// per function in the Builder, reuse a single one (e.g. at Blocks[1]) -// to reduce garbage. - -import ( - "fmt" - "os" -) - -// If true, perform sanity checking and show progress at each -// successive iteration of optimizeBlocks. Very verbose. -const debugBlockOpt = false - -// markReachable sets Index=-1 for all blocks reachable from b. -func markReachable(b *BasicBlock) { - b.Index = -1 - for _, succ := range b.Succs { - if succ.Index == 0 { - markReachable(succ) - } - } -} - -// deleteUnreachableBlocks marks all reachable blocks of f and -// eliminates (nils) all others, including possibly cyclic subgraphs. -// -func deleteUnreachableBlocks(f *Function) { - const white, black = 0, -1 - // We borrow b.Index temporarily as the mark bit. - for _, b := range f.Blocks { - b.Index = white - } - markReachable(f.Blocks[0]) - if f.Recover != nil { - markReachable(f.Recover) - } - for i, b := range f.Blocks { - if b.Index == white { - for _, c := range b.Succs { - if c.Index == black { - c.removePred(b) // delete white->black edge - } - } - if debugBlockOpt { - fmt.Fprintln(os.Stderr, "unreachable", b) - } - f.Blocks[i] = nil // delete b - } - } - f.removeNilBlocks() -} - -// jumpThreading attempts to apply simple jump-threading to block b, -// in which a->b->c become a->c if b is just a Jump. -// The result is true if the optimization was applied. -// -func jumpThreading(f *Function, b *BasicBlock) bool { - if b.Index == 0 { - return false // don't apply to entry block - } - if b.Instrs == nil { - return false - } - if _, ok := b.Instrs[0].(*Jump); !ok { - return false // not just a jump - } - c := b.Succs[0] - if c == b { - return false // don't apply to degenerate jump-to-self. - } - if c.hasPhi() { - return false // not sound without more effort - } - for j, a := range b.Preds { - a.replaceSucc(b, c) - - // If a now has two edges to c, replace its degenerate If by Jump. - if len(a.Succs) == 2 && a.Succs[0] == c && a.Succs[1] == c { - jump := new(Jump) - jump.setBlock(a) - a.Instrs[len(a.Instrs)-1] = jump - a.Succs = a.Succs[:1] - c.removePred(b) - } else { - if j == 0 { - c.replacePred(b, a) - } else { - c.Preds = append(c.Preds, a) - } - } - - if debugBlockOpt { - fmt.Fprintln(os.Stderr, "jumpThreading", a, b, c) - } - } - f.Blocks[b.Index] = nil // delete b - return true -} - -// fuseBlocks attempts to apply the block fusion optimization to block -// a, in which a->b becomes ab if len(a.Succs)==len(b.Preds)==1. -// The result is true if the optimization was applied. -// -func fuseBlocks(f *Function, a *BasicBlock) bool { - if len(a.Succs) != 1 { - return false - } - b := a.Succs[0] - if len(b.Preds) != 1 { - return false - } - - // Degenerate &&/|| ops may result in a straight-line CFG - // containing φ-nodes. (Ideally we'd replace such them with - // their sole operand but that requires Referrers, built later.) - if b.hasPhi() { - return false // not sound without further effort - } - - // Eliminate jump at end of A, then copy all of B across. - a.Instrs = append(a.Instrs[:len(a.Instrs)-1], b.Instrs...) - for _, instr := range b.Instrs { - instr.setBlock(a) - } - - // A inherits B's successors - a.Succs = append(a.succs2[:0], b.Succs...) - - // Fix up Preds links of all successors of B. - for _, c := range b.Succs { - c.replacePred(b, a) - } - - if debugBlockOpt { - fmt.Fprintln(os.Stderr, "fuseBlocks", a, b) - } - - f.Blocks[b.Index] = nil // delete b - return true -} - -// optimizeBlocks() performs some simple block optimizations on a -// completed function: dead block elimination, block fusion, jump -// threading. -// -func optimizeBlocks(f *Function) { - deleteUnreachableBlocks(f) - - // Loop until no further progress. - changed := true - for changed { - changed = false - - if debugBlockOpt { - f.WriteTo(os.Stderr) - mustSanityCheck(f, nil) - } - - for _, b := range f.Blocks { - // f.Blocks will temporarily contain nils to indicate - // deleted blocks; we remove them at the end. - if b == nil { - continue - } - - // Fuse blocks. b->c becomes bc. - if fuseBlocks(f, b) { - changed = true - } - - // a->b->c becomes a->c if b contains only a Jump. - if jumpThreading(f, b) { - changed = true - continue // (b was disconnected) - } - } - } - f.removeNilBlocks() -} diff --git a/vendor/golang.org/x/tools/go/ssa/builder.go b/vendor/golang.org/x/tools/go/ssa/builder.go deleted file mode 100644 index 36aebdf8250..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/builder.go +++ /dev/null @@ -1,2382 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file implements the BUILD phase of SSA construction. -// -// SSA construction has two phases, CREATE and BUILD. In the CREATE phase -// (create.go), all packages are constructed and type-checked and -// definitions of all package members are created, method-sets are -// computed, and wrapper methods are synthesized. -// ssa.Packages are created in arbitrary order. -// -// In the BUILD phase (builder.go), the builder traverses the AST of -// each Go source function and generates SSA instructions for the -// function body. Initializer expressions for package-level variables -// are emitted to the package's init() function in the order specified -// by go/types.Info.InitOrder, then code for each function in the -// package is generated in lexical order. -// The BUILD phases for distinct packages are independent and are -// executed in parallel. -// -// TODO(adonovan): indeed, building functions is now embarrassingly parallel. -// Audit for concurrency then benchmark using more goroutines. -// -// The builder's and Program's indices (maps) are populated and -// mutated during the CREATE phase, but during the BUILD phase they -// remain constant. The sole exception is Prog.methodSets and its -// related maps, which are protected by a dedicated mutex. - -import ( - "fmt" - "go/ast" - "go/constant" - "go/token" - "go/types" - "os" - "sync" -) - -type opaqueType struct { - types.Type - name string -} - -func (t *opaqueType) String() string { return t.name } - -var ( - varOk = newVar("ok", tBool) - varIndex = newVar("index", tInt) - - // Type constants. - tBool = types.Typ[types.Bool] - tByte = types.Typ[types.Byte] - tInt = types.Typ[types.Int] - tInvalid = types.Typ[types.Invalid] - tString = types.Typ[types.String] - tUntypedNil = types.Typ[types.UntypedNil] - tRangeIter = &opaqueType{nil, "iter"} // the type of all "range" iterators - tEface = types.NewInterface(nil, nil).Complete() - - // SSA Value constants. - vZero = intConst(0) - vOne = intConst(1) - vTrue = NewConst(constant.MakeBool(true), tBool) -) - -// builder holds state associated with the package currently being built. -// Its methods contain all the logic for AST-to-SSA conversion. -type builder struct{} - -// cond emits to fn code to evaluate boolean condition e and jump -// to t or f depending on its value, performing various simplifications. -// -// Postcondition: fn.currentBlock is nil. -// -func (b *builder) cond(fn *Function, e ast.Expr, t, f *BasicBlock) { - switch e := e.(type) { - case *ast.ParenExpr: - b.cond(fn, e.X, t, f) - return - - case *ast.BinaryExpr: - switch e.Op { - case token.LAND: - ltrue := fn.newBasicBlock("cond.true") - b.cond(fn, e.X, ltrue, f) - fn.currentBlock = ltrue - b.cond(fn, e.Y, t, f) - return - - case token.LOR: - lfalse := fn.newBasicBlock("cond.false") - b.cond(fn, e.X, t, lfalse) - fn.currentBlock = lfalse - b.cond(fn, e.Y, t, f) - return - } - - case *ast.UnaryExpr: - if e.Op == token.NOT { - b.cond(fn, e.X, f, t) - return - } - } - - // A traditional compiler would simplify "if false" (etc) here - // but we do not, for better fidelity to the source code. - // - // The value of a constant condition may be platform-specific, - // and may cause blocks that are reachable in some configuration - // to be hidden from subsequent analyses such as bug-finding tools. - emitIf(fn, b.expr(fn, e), t, f) -} - -// logicalBinop emits code to fn to evaluate e, a &&- or -// ||-expression whose reified boolean value is wanted. -// The value is returned. -// -func (b *builder) logicalBinop(fn *Function, e *ast.BinaryExpr) Value { - rhs := fn.newBasicBlock("binop.rhs") - done := fn.newBasicBlock("binop.done") - - // T(e) = T(e.X) = T(e.Y) after untyped constants have been - // eliminated. - // TODO(adonovan): not true; MyBool==MyBool yields UntypedBool. - t := fn.Pkg.typeOf(e) - - var short Value // value of the short-circuit path - switch e.Op { - case token.LAND: - b.cond(fn, e.X, rhs, done) - short = NewConst(constant.MakeBool(false), t) - - case token.LOR: - b.cond(fn, e.X, done, rhs) - short = NewConst(constant.MakeBool(true), t) - } - - // Is rhs unreachable? - if rhs.Preds == nil { - // Simplify false&&y to false, true||y to true. - fn.currentBlock = done - return short - } - - // Is done unreachable? - if done.Preds == nil { - // Simplify true&&y (or false||y) to y. - fn.currentBlock = rhs - return b.expr(fn, e.Y) - } - - // All edges from e.X to done carry the short-circuit value. - var edges []Value - for range done.Preds { - edges = append(edges, short) - } - - // The edge from e.Y to done carries the value of e.Y. - fn.currentBlock = rhs - edges = append(edges, b.expr(fn, e.Y)) - emitJump(fn, done) - fn.currentBlock = done - - phi := &Phi{Edges: edges, Comment: e.Op.String()} - phi.pos = e.OpPos - phi.typ = t - return done.emit(phi) -} - -// exprN lowers a multi-result expression e to SSA form, emitting code -// to fn and returning a single Value whose type is a *types.Tuple. -// The caller must access the components via Extract. -// -// Multi-result expressions include CallExprs in a multi-value -// assignment or return statement, and "value,ok" uses of -// TypeAssertExpr, IndexExpr (when X is a map), and UnaryExpr (when Op -// is token.ARROW). -// -func (b *builder) exprN(fn *Function, e ast.Expr) Value { - typ := fn.Pkg.typeOf(e).(*types.Tuple) - switch e := e.(type) { - case *ast.ParenExpr: - return b.exprN(fn, e.X) - - case *ast.CallExpr: - // Currently, no built-in function nor type conversion - // has multiple results, so we can avoid some of the - // cases for single-valued CallExpr. - var c Call - b.setCall(fn, e, &c.Call) - c.typ = typ - return fn.emit(&c) - - case *ast.IndexExpr: - mapt := fn.Pkg.typeOf(e.X).Underlying().(*types.Map) - lookup := &Lookup{ - X: b.expr(fn, e.X), - Index: emitConv(fn, b.expr(fn, e.Index), mapt.Key()), - CommaOk: true, - } - lookup.setType(typ) - lookup.setPos(e.Lbrack) - return fn.emit(lookup) - - case *ast.TypeAssertExpr: - return emitTypeTest(fn, b.expr(fn, e.X), typ.At(0).Type(), e.Lparen) - - case *ast.UnaryExpr: // must be receive <- - unop := &UnOp{ - Op: token.ARROW, - X: b.expr(fn, e.X), - CommaOk: true, - } - unop.setType(typ) - unop.setPos(e.OpPos) - return fn.emit(unop) - } - panic(fmt.Sprintf("exprN(%T) in %s", e, fn)) -} - -// builtin emits to fn SSA instructions to implement a call to the -// built-in function obj with the specified arguments -// and return type. It returns the value defined by the result. -// -// The result is nil if no special handling was required; in this case -// the caller should treat this like an ordinary library function -// call. -// -func (b *builder) builtin(fn *Function, obj *types.Builtin, args []ast.Expr, typ types.Type, pos token.Pos) Value { - switch obj.Name() { - case "make": - switch typ.Underlying().(type) { - case *types.Slice: - n := b.expr(fn, args[1]) - m := n - if len(args) == 3 { - m = b.expr(fn, args[2]) - } - if m, ok := m.(*Const); ok { - // treat make([]T, n, m) as new([m]T)[:n] - cap := m.Int64() - at := types.NewArray(typ.Underlying().(*types.Slice).Elem(), cap) - alloc := emitNew(fn, at, pos) - alloc.Comment = "makeslice" - v := &Slice{ - X: alloc, - High: n, - } - v.setPos(pos) - v.setType(typ) - return fn.emit(v) - } - v := &MakeSlice{ - Len: n, - Cap: m, - } - v.setPos(pos) - v.setType(typ) - return fn.emit(v) - - case *types.Map: - var res Value - if len(args) == 2 { - res = b.expr(fn, args[1]) - } - v := &MakeMap{Reserve: res} - v.setPos(pos) - v.setType(typ) - return fn.emit(v) - - case *types.Chan: - var sz Value = vZero - if len(args) == 2 { - sz = b.expr(fn, args[1]) - } - v := &MakeChan{Size: sz} - v.setPos(pos) - v.setType(typ) - return fn.emit(v) - } - - case "new": - alloc := emitNew(fn, deref(typ), pos) - alloc.Comment = "new" - return alloc - - case "len", "cap": - // Special case: len or cap of an array or *array is - // based on the type, not the value which may be nil. - // We must still evaluate the value, though. (If it - // was side-effect free, the whole call would have - // been constant-folded.) - t := deref(fn.Pkg.typeOf(args[0])).Underlying() - if at, ok := t.(*types.Array); ok { - b.expr(fn, args[0]) // for effects only - return intConst(at.Len()) - } - // Otherwise treat as normal. - - case "panic": - fn.emit(&Panic{ - X: emitConv(fn, b.expr(fn, args[0]), tEface), - pos: pos, - }) - fn.currentBlock = fn.newBasicBlock("unreachable") - return vTrue // any non-nil Value will do - } - return nil // treat all others as a regular function call -} - -// addr lowers a single-result addressable expression e to SSA form, -// emitting code to fn and returning the location (an lvalue) defined -// by the expression. -// -// If escaping is true, addr marks the base variable of the -// addressable expression e as being a potentially escaping pointer -// value. For example, in this code: -// -// a := A{ -// b: [1]B{B{c: 1}} -// } -// return &a.b[0].c -// -// the application of & causes a.b[0].c to have its address taken, -// which means that ultimately the local variable a must be -// heap-allocated. This is a simple but very conservative escape -// analysis. -// -// Operations forming potentially escaping pointers include: -// - &x, including when implicit in method call or composite literals. -// - a[:] iff a is an array (not *array) -// - references to variables in lexically enclosing functions. -// -func (b *builder) addr(fn *Function, e ast.Expr, escaping bool) lvalue { - switch e := e.(type) { - case *ast.Ident: - if isBlankIdent(e) { - return blank{} - } - obj := fn.Pkg.objectOf(e) - v := fn.Prog.packageLevelValue(obj) // var (address) - if v == nil { - v = fn.lookup(obj, escaping) - } - return &address{addr: v, pos: e.Pos(), expr: e} - - case *ast.CompositeLit: - t := deref(fn.Pkg.typeOf(e)) - var v *Alloc - if escaping { - v = emitNew(fn, t, e.Lbrace) - } else { - v = fn.addLocal(t, e.Lbrace) - } - v.Comment = "complit" - var sb storebuf - b.compLit(fn, v, e, true, &sb) - sb.emit(fn) - return &address{addr: v, pos: e.Lbrace, expr: e} - - case *ast.ParenExpr: - return b.addr(fn, e.X, escaping) - - case *ast.SelectorExpr: - sel, ok := fn.Pkg.info.Selections[e] - if !ok { - // qualified identifier - return b.addr(fn, e.Sel, escaping) - } - if sel.Kind() != types.FieldVal { - panic(sel) - } - wantAddr := true - v := b.receiver(fn, e.X, wantAddr, escaping, sel) - last := len(sel.Index()) - 1 - return &address{ - addr: emitFieldSelection(fn, v, sel.Index()[last], true, e.Sel), - pos: e.Sel.Pos(), - expr: e.Sel, - } - - case *ast.IndexExpr: - var x Value - var et types.Type - switch t := fn.Pkg.typeOf(e.X).Underlying().(type) { - case *types.Array: - x = b.addr(fn, e.X, escaping).address(fn) - et = types.NewPointer(t.Elem()) - case *types.Pointer: // *array - x = b.expr(fn, e.X) - et = types.NewPointer(t.Elem().Underlying().(*types.Array).Elem()) - case *types.Slice: - x = b.expr(fn, e.X) - et = types.NewPointer(t.Elem()) - case *types.Map: - return &element{ - m: b.expr(fn, e.X), - k: emitConv(fn, b.expr(fn, e.Index), t.Key()), - t: t.Elem(), - pos: e.Lbrack, - } - default: - panic("unexpected container type in IndexExpr: " + t.String()) - } - v := &IndexAddr{ - X: x, - Index: emitConv(fn, b.expr(fn, e.Index), tInt), - } - v.setPos(e.Lbrack) - v.setType(et) - return &address{addr: fn.emit(v), pos: e.Lbrack, expr: e} - - case *ast.StarExpr: - return &address{addr: b.expr(fn, e.X), pos: e.Star, expr: e} - } - - panic(fmt.Sprintf("unexpected address expression: %T", e)) -} - -type store struct { - lhs lvalue - rhs Value -} - -type storebuf struct{ stores []store } - -func (sb *storebuf) store(lhs lvalue, rhs Value) { - sb.stores = append(sb.stores, store{lhs, rhs}) -} - -func (sb *storebuf) emit(fn *Function) { - for _, s := range sb.stores { - s.lhs.store(fn, s.rhs) - } -} - -// assign emits to fn code to initialize the lvalue loc with the value -// of expression e. If isZero is true, assign assumes that loc holds -// the zero value for its type. -// -// This is equivalent to loc.store(fn, b.expr(fn, e)), but may generate -// better code in some cases, e.g., for composite literals in an -// addressable location. -// -// If sb is not nil, assign generates code to evaluate expression e, but -// not to update loc. Instead, the necessary stores are appended to the -// storebuf sb so that they can be executed later. This allows correct -// in-place update of existing variables when the RHS is a composite -// literal that may reference parts of the LHS. -// -func (b *builder) assign(fn *Function, loc lvalue, e ast.Expr, isZero bool, sb *storebuf) { - // Can we initialize it in place? - if e, ok := unparen(e).(*ast.CompositeLit); ok { - // A CompositeLit never evaluates to a pointer, - // so if the type of the location is a pointer, - // an &-operation is implied. - if _, ok := loc.(blank); !ok { // avoid calling blank.typ() - if isPointer(loc.typ()) { - ptr := b.addr(fn, e, true).address(fn) - // copy address - if sb != nil { - sb.store(loc, ptr) - } else { - loc.store(fn, ptr) - } - return - } - } - - if _, ok := loc.(*address); ok { - if isInterface(loc.typ()) { - // e.g. var x interface{} = T{...} - // Can't in-place initialize an interface value. - // Fall back to copying. - } else { - // x = T{...} or x := T{...} - addr := loc.address(fn) - if sb != nil { - b.compLit(fn, addr, e, isZero, sb) - } else { - var sb storebuf - b.compLit(fn, addr, e, isZero, &sb) - sb.emit(fn) - } - - // Subtle: emit debug ref for aggregate types only; - // slice and map are handled by store ops in compLit. - switch loc.typ().Underlying().(type) { - case *types.Struct, *types.Array: - emitDebugRef(fn, e, addr, true) - } - - return - } - } - } - - // simple case: just copy - rhs := b.expr(fn, e) - if sb != nil { - sb.store(loc, rhs) - } else { - loc.store(fn, rhs) - } -} - -// expr lowers a single-result expression e to SSA form, emitting code -// to fn and returning the Value defined by the expression. -// -func (b *builder) expr(fn *Function, e ast.Expr) Value { - e = unparen(e) - - tv := fn.Pkg.info.Types[e] - - // Is expression a constant? - if tv.Value != nil { - return NewConst(tv.Value, tv.Type) - } - - var v Value - if tv.Addressable() { - // Prefer pointer arithmetic ({Index,Field}Addr) followed - // by Load over subelement extraction (e.g. Index, Field), - // to avoid large copies. - v = b.addr(fn, e, false).load(fn) - } else { - v = b.expr0(fn, e, tv) - } - if fn.debugInfo() { - emitDebugRef(fn, e, v, false) - } - return v -} - -func (b *builder) expr0(fn *Function, e ast.Expr, tv types.TypeAndValue) Value { - switch e := e.(type) { - case *ast.BasicLit: - panic("non-constant BasicLit") // unreachable - - case *ast.FuncLit: - fn2 := &Function{ - name: fmt.Sprintf("%s$%d", fn.Name(), 1+len(fn.AnonFuncs)), - Signature: fn.Pkg.typeOf(e.Type).Underlying().(*types.Signature), - pos: e.Type.Func, - parent: fn, - Pkg: fn.Pkg, - Prog: fn.Prog, - syntax: e, - } - fn.AnonFuncs = append(fn.AnonFuncs, fn2) - b.buildFunction(fn2) - if fn2.FreeVars == nil { - return fn2 - } - v := &MakeClosure{Fn: fn2} - v.setType(tv.Type) - for _, fv := range fn2.FreeVars { - v.Bindings = append(v.Bindings, fv.outer) - fv.outer = nil - } - return fn.emit(v) - - case *ast.TypeAssertExpr: // single-result form only - return emitTypeAssert(fn, b.expr(fn, e.X), tv.Type, e.Lparen) - - case *ast.CallExpr: - if fn.Pkg.info.Types[e.Fun].IsType() { - // Explicit type conversion, e.g. string(x) or big.Int(x) - x := b.expr(fn, e.Args[0]) - y := emitConv(fn, x, tv.Type) - if y != x { - switch y := y.(type) { - case *Convert: - y.pos = e.Lparen - case *ChangeType: - y.pos = e.Lparen - case *MakeInterface: - y.pos = e.Lparen - } - } - return y - } - // Call to "intrinsic" built-ins, e.g. new, make, panic. - if id, ok := unparen(e.Fun).(*ast.Ident); ok { - if obj, ok := fn.Pkg.info.Uses[id].(*types.Builtin); ok { - if v := b.builtin(fn, obj, e.Args, tv.Type, e.Lparen); v != nil { - return v - } - } - } - // Regular function call. - var v Call - b.setCall(fn, e, &v.Call) - v.setType(tv.Type) - return fn.emit(&v) - - case *ast.UnaryExpr: - switch e.Op { - case token.AND: // &X --- potentially escaping. - addr := b.addr(fn, e.X, true) - if _, ok := unparen(e.X).(*ast.StarExpr); ok { - // &*p must panic if p is nil (http://golang.org/s/go12nil). - // For simplicity, we'll just (suboptimally) rely - // on the side effects of a load. - // TODO(adonovan): emit dedicated nilcheck. - addr.load(fn) - } - return addr.address(fn) - case token.ADD: - return b.expr(fn, e.X) - case token.NOT, token.ARROW, token.SUB, token.XOR: // ! <- - ^ - v := &UnOp{ - Op: e.Op, - X: b.expr(fn, e.X), - } - v.setPos(e.OpPos) - v.setType(tv.Type) - return fn.emit(v) - default: - panic(e.Op) - } - - case *ast.BinaryExpr: - switch e.Op { - case token.LAND, token.LOR: - return b.logicalBinop(fn, e) - case token.SHL, token.SHR: - fallthrough - case token.ADD, token.SUB, token.MUL, token.QUO, token.REM, token.AND, token.OR, token.XOR, token.AND_NOT: - return emitArith(fn, e.Op, b.expr(fn, e.X), b.expr(fn, e.Y), tv.Type, e.OpPos) - - case token.EQL, token.NEQ, token.GTR, token.LSS, token.LEQ, token.GEQ: - cmp := emitCompare(fn, e.Op, b.expr(fn, e.X), b.expr(fn, e.Y), e.OpPos) - // The type of x==y may be UntypedBool. - return emitConv(fn, cmp, DefaultType(tv.Type)) - default: - panic("illegal op in BinaryExpr: " + e.Op.String()) - } - - case *ast.SliceExpr: - var low, high, max Value - var x Value - switch fn.Pkg.typeOf(e.X).Underlying().(type) { - case *types.Array: - // Potentially escaping. - x = b.addr(fn, e.X, true).address(fn) - case *types.Basic, *types.Slice, *types.Pointer: // *array - x = b.expr(fn, e.X) - default: - panic("unreachable") - } - if e.High != nil { - high = b.expr(fn, e.High) - } - if e.Low != nil { - low = b.expr(fn, e.Low) - } - if e.Slice3 { - max = b.expr(fn, e.Max) - } - v := &Slice{ - X: x, - Low: low, - High: high, - Max: max, - } - v.setPos(e.Lbrack) - v.setType(tv.Type) - return fn.emit(v) - - case *ast.Ident: - obj := fn.Pkg.info.Uses[e] - // Universal built-in or nil? - switch obj := obj.(type) { - case *types.Builtin: - return &Builtin{name: obj.Name(), sig: tv.Type.(*types.Signature)} - case *types.Nil: - return nilConst(tv.Type) - } - // Package-level func or var? - if v := fn.Prog.packageLevelValue(obj); v != nil { - if _, ok := obj.(*types.Var); ok { - return emitLoad(fn, v) // var (address) - } - return v // (func) - } - // Local var. - return emitLoad(fn, fn.lookup(obj, false)) // var (address) - - case *ast.SelectorExpr: - sel, ok := fn.Pkg.info.Selections[e] - if !ok { - // qualified identifier - return b.expr(fn, e.Sel) - } - switch sel.Kind() { - case types.MethodExpr: - // (*T).f or T.f, the method f from the method-set of type T. - // The result is a "thunk". - return emitConv(fn, makeThunk(fn.Prog, sel), tv.Type) - - case types.MethodVal: - // e.f where e is an expression and f is a method. - // The result is a "bound". - obj := sel.Obj().(*types.Func) - rt := recvType(obj) - wantAddr := isPointer(rt) - escaping := true - v := b.receiver(fn, e.X, wantAddr, escaping, sel) - if isInterface(rt) { - // If v has interface type I, - // we must emit a check that v is non-nil. - // We use: typeassert v.(I). - emitTypeAssert(fn, v, rt, token.NoPos) - } - c := &MakeClosure{ - Fn: makeBound(fn.Prog, obj), - Bindings: []Value{v}, - } - c.setPos(e.Sel.Pos()) - c.setType(tv.Type) - return fn.emit(c) - - case types.FieldVal: - indices := sel.Index() - last := len(indices) - 1 - v := b.expr(fn, e.X) - v = emitImplicitSelections(fn, v, indices[:last]) - v = emitFieldSelection(fn, v, indices[last], false, e.Sel) - return v - } - - panic("unexpected expression-relative selector") - - case *ast.IndexExpr: - switch t := fn.Pkg.typeOf(e.X).Underlying().(type) { - case *types.Array: - // Non-addressable array (in a register). - v := &Index{ - X: b.expr(fn, e.X), - Index: emitConv(fn, b.expr(fn, e.Index), tInt), - } - v.setPos(e.Lbrack) - v.setType(t.Elem()) - return fn.emit(v) - - case *types.Map: - // Maps are not addressable. - mapt := fn.Pkg.typeOf(e.X).Underlying().(*types.Map) - v := &Lookup{ - X: b.expr(fn, e.X), - Index: emitConv(fn, b.expr(fn, e.Index), mapt.Key()), - } - v.setPos(e.Lbrack) - v.setType(mapt.Elem()) - return fn.emit(v) - - case *types.Basic: // => string - // Strings are not addressable. - v := &Lookup{ - X: b.expr(fn, e.X), - Index: b.expr(fn, e.Index), - } - v.setPos(e.Lbrack) - v.setType(tByte) - return fn.emit(v) - - case *types.Slice, *types.Pointer: // *array - // Addressable slice/array; use IndexAddr and Load. - return b.addr(fn, e, false).load(fn) - - default: - panic("unexpected container type in IndexExpr: " + t.String()) - } - - case *ast.CompositeLit, *ast.StarExpr: - // Addressable types (lvalues) - return b.addr(fn, e, false).load(fn) - } - - panic(fmt.Sprintf("unexpected expr: %T", e)) -} - -// stmtList emits to fn code for all statements in list. -func (b *builder) stmtList(fn *Function, list []ast.Stmt) { - for _, s := range list { - b.stmt(fn, s) - } -} - -// receiver emits to fn code for expression e in the "receiver" -// position of selection e.f (where f may be a field or a method) and -// returns the effective receiver after applying the implicit field -// selections of sel. -// -// wantAddr requests that the result is an an address. If -// !sel.Indirect(), this may require that e be built in addr() mode; it -// must thus be addressable. -// -// escaping is defined as per builder.addr(). -// -func (b *builder) receiver(fn *Function, e ast.Expr, wantAddr, escaping bool, sel *types.Selection) Value { - var v Value - if wantAddr && !sel.Indirect() && !isPointer(fn.Pkg.typeOf(e)) { - v = b.addr(fn, e, escaping).address(fn) - } else { - v = b.expr(fn, e) - } - - last := len(sel.Index()) - 1 - v = emitImplicitSelections(fn, v, sel.Index()[:last]) - if !wantAddr && isPointer(v.Type()) { - v = emitLoad(fn, v) - } - return v -} - -// setCallFunc populates the function parts of a CallCommon structure -// (Func, Method, Recv, Args[0]) based on the kind of invocation -// occurring in e. -// -func (b *builder) setCallFunc(fn *Function, e *ast.CallExpr, c *CallCommon) { - c.pos = e.Lparen - - // Is this a method call? - if selector, ok := unparen(e.Fun).(*ast.SelectorExpr); ok { - sel, ok := fn.Pkg.info.Selections[selector] - if ok && sel.Kind() == types.MethodVal { - obj := sel.Obj().(*types.Func) - recv := recvType(obj) - wantAddr := isPointer(recv) - escaping := true - v := b.receiver(fn, selector.X, wantAddr, escaping, sel) - if isInterface(recv) { - // Invoke-mode call. - c.Value = v - c.Method = obj - } else { - // "Call"-mode call. - c.Value = fn.Prog.declaredFunc(obj) - c.Args = append(c.Args, v) - } - return - } - - // sel.Kind()==MethodExpr indicates T.f() or (*T).f(): - // a statically dispatched call to the method f in the - // method-set of T or *T. T may be an interface. - // - // e.Fun would evaluate to a concrete method, interface - // wrapper function, or promotion wrapper. - // - // For now, we evaluate it in the usual way. - // - // TODO(adonovan): opt: inline expr() here, to make the - // call static and to avoid generation of wrappers. - // It's somewhat tricky as it may consume the first - // actual parameter if the call is "invoke" mode. - // - // Examples: - // type T struct{}; func (T) f() {} // "call" mode - // type T interface { f() } // "invoke" mode - // - // type S struct{ T } - // - // var s S - // S.f(s) - // (*S).f(&s) - // - // Suggested approach: - // - consume the first actual parameter expression - // and build it with b.expr(). - // - apply implicit field selections. - // - use MethodVal logic to populate fields of c. - } - - // Evaluate the function operand in the usual way. - c.Value = b.expr(fn, e.Fun) -} - -// emitCallArgs emits to f code for the actual parameters of call e to -// a (possibly built-in) function of effective type sig. -// The argument values are appended to args, which is then returned. -// -func (b *builder) emitCallArgs(fn *Function, sig *types.Signature, e *ast.CallExpr, args []Value) []Value { - // f(x, y, z...): pass slice z straight through. - if e.Ellipsis != 0 { - for i, arg := range e.Args { - v := emitConv(fn, b.expr(fn, arg), sig.Params().At(i).Type()) - args = append(args, v) - } - return args - } - - offset := len(args) // 1 if call has receiver, 0 otherwise - - // Evaluate actual parameter expressions. - // - // If this is a chained call of the form f(g()) where g has - // multiple return values (MRV), they are flattened out into - // args; a suffix of them may end up in a varargs slice. - for _, arg := range e.Args { - v := b.expr(fn, arg) - if ttuple, ok := v.Type().(*types.Tuple); ok { // MRV chain - for i, n := 0, ttuple.Len(); i < n; i++ { - args = append(args, emitExtract(fn, v, i)) - } - } else { - args = append(args, v) - } - } - - // Actual->formal assignability conversions for normal parameters. - np := sig.Params().Len() // number of normal parameters - if sig.Variadic() { - np-- - } - for i := 0; i < np; i++ { - args[offset+i] = emitConv(fn, args[offset+i], sig.Params().At(i).Type()) - } - - // Actual->formal assignability conversions for variadic parameter, - // and construction of slice. - if sig.Variadic() { - varargs := args[offset+np:] - st := sig.Params().At(np).Type().(*types.Slice) - vt := st.Elem() - if len(varargs) == 0 { - args = append(args, nilConst(st)) - } else { - // Replace a suffix of args with a slice containing it. - at := types.NewArray(vt, int64(len(varargs))) - a := emitNew(fn, at, token.NoPos) - a.setPos(e.Rparen) - a.Comment = "varargs" - for i, arg := range varargs { - iaddr := &IndexAddr{ - X: a, - Index: intConst(int64(i)), - } - iaddr.setType(types.NewPointer(vt)) - fn.emit(iaddr) - emitStore(fn, iaddr, arg, arg.Pos()) - } - s := &Slice{X: a} - s.setType(st) - args[offset+np] = fn.emit(s) - args = args[:offset+np+1] - } - } - return args -} - -// setCall emits to fn code to evaluate all the parameters of a function -// call e, and populates *c with those values. -// -func (b *builder) setCall(fn *Function, e *ast.CallExpr, c *CallCommon) { - // First deal with the f(...) part and optional receiver. - b.setCallFunc(fn, e, c) - - // Then append the other actual parameters. - sig, _ := fn.Pkg.typeOf(e.Fun).Underlying().(*types.Signature) - if sig == nil { - panic(fmt.Sprintf("no signature for call of %s", e.Fun)) - } - c.Args = b.emitCallArgs(fn, sig, e, c.Args) -} - -// assignOp emits to fn code to perform loc = val. -func (b *builder) assignOp(fn *Function, loc lvalue, val Value, op token.Token, pos token.Pos) { - oldv := loc.load(fn) - loc.store(fn, emitArith(fn, op, oldv, emitConv(fn, val, oldv.Type()), loc.typ(), pos)) -} - -// localValueSpec emits to fn code to define all of the vars in the -// function-local ValueSpec, spec. -// -func (b *builder) localValueSpec(fn *Function, spec *ast.ValueSpec) { - switch { - case len(spec.Values) == len(spec.Names): - // e.g. var x, y = 0, 1 - // 1:1 assignment - for i, id := range spec.Names { - if !isBlankIdent(id) { - fn.addLocalForIdent(id) - } - lval := b.addr(fn, id, false) // non-escaping - b.assign(fn, lval, spec.Values[i], true, nil) - } - - case len(spec.Values) == 0: - // e.g. var x, y int - // Locals are implicitly zero-initialized. - for _, id := range spec.Names { - if !isBlankIdent(id) { - lhs := fn.addLocalForIdent(id) - if fn.debugInfo() { - emitDebugRef(fn, id, lhs, true) - } - } - } - - default: - // e.g. var x, y = pos() - tuple := b.exprN(fn, spec.Values[0]) - for i, id := range spec.Names { - if !isBlankIdent(id) { - fn.addLocalForIdent(id) - lhs := b.addr(fn, id, false) // non-escaping - lhs.store(fn, emitExtract(fn, tuple, i)) - } - } - } -} - -// assignStmt emits code to fn for a parallel assignment of rhss to lhss. -// isDef is true if this is a short variable declaration (:=). -// -// Note the similarity with localValueSpec. -// -func (b *builder) assignStmt(fn *Function, lhss, rhss []ast.Expr, isDef bool) { - // Side effects of all LHSs and RHSs must occur in left-to-right order. - lvals := make([]lvalue, len(lhss)) - isZero := make([]bool, len(lhss)) - for i, lhs := range lhss { - var lval lvalue = blank{} - if !isBlankIdent(lhs) { - if isDef { - if obj := fn.Pkg.info.Defs[lhs.(*ast.Ident)]; obj != nil { - fn.addNamedLocal(obj) - isZero[i] = true - } - } - lval = b.addr(fn, lhs, false) // non-escaping - } - lvals[i] = lval - } - if len(lhss) == len(rhss) { - // Simple assignment: x = f() (!isDef) - // Parallel assignment: x, y = f(), g() (!isDef) - // or short var decl: x, y := f(), g() (isDef) - // - // In all cases, the RHSs may refer to the LHSs, - // so we need a storebuf. - var sb storebuf - for i := range rhss { - b.assign(fn, lvals[i], rhss[i], isZero[i], &sb) - } - sb.emit(fn) - } else { - // e.g. x, y = pos() - tuple := b.exprN(fn, rhss[0]) - emitDebugRef(fn, rhss[0], tuple, false) - for i, lval := range lvals { - lval.store(fn, emitExtract(fn, tuple, i)) - } - } -} - -// arrayLen returns the length of the array whose composite literal elements are elts. -func (b *builder) arrayLen(fn *Function, elts []ast.Expr) int64 { - var max int64 = -1 - var i int64 = -1 - for _, e := range elts { - if kv, ok := e.(*ast.KeyValueExpr); ok { - i = b.expr(fn, kv.Key).(*Const).Int64() - } else { - i++ - } - if i > max { - max = i - } - } - return max + 1 -} - -// compLit emits to fn code to initialize a composite literal e at -// address addr with type typ. -// -// Nested composite literals are recursively initialized in place -// where possible. If isZero is true, compLit assumes that addr -// holds the zero value for typ. -// -// Because the elements of a composite literal may refer to the -// variables being updated, as in the second line below, -// x := T{a: 1} -// x = T{a: x.a} -// all the reads must occur before all the writes. Thus all stores to -// loc are emitted to the storebuf sb for later execution. -// -// A CompositeLit may have pointer type only in the recursive (nested) -// case when the type name is implicit. e.g. in []*T{{}}, the inner -// literal has type *T behaves like &T{}. -// In that case, addr must hold a T, not a *T. -// -func (b *builder) compLit(fn *Function, addr Value, e *ast.CompositeLit, isZero bool, sb *storebuf) { - typ := deref(fn.Pkg.typeOf(e)) - switch t := typ.Underlying().(type) { - case *types.Struct: - if !isZero && len(e.Elts) != t.NumFields() { - // memclear - sb.store(&address{addr, e.Lbrace, nil}, - zeroValue(fn, deref(addr.Type()))) - isZero = true - } - for i, e := range e.Elts { - fieldIndex := i - pos := e.Pos() - if kv, ok := e.(*ast.KeyValueExpr); ok { - fname := kv.Key.(*ast.Ident).Name - for i, n := 0, t.NumFields(); i < n; i++ { - sf := t.Field(i) - if sf.Name() == fname { - fieldIndex = i - pos = kv.Colon - e = kv.Value - break - } - } - } - sf := t.Field(fieldIndex) - faddr := &FieldAddr{ - X: addr, - Field: fieldIndex, - } - faddr.setType(types.NewPointer(sf.Type())) - fn.emit(faddr) - b.assign(fn, &address{addr: faddr, pos: pos, expr: e}, e, isZero, sb) - } - - case *types.Array, *types.Slice: - var at *types.Array - var array Value - switch t := t.(type) { - case *types.Slice: - at = types.NewArray(t.Elem(), b.arrayLen(fn, e.Elts)) - alloc := emitNew(fn, at, e.Lbrace) - alloc.Comment = "slicelit" - array = alloc - case *types.Array: - at = t - array = addr - - if !isZero && int64(len(e.Elts)) != at.Len() { - // memclear - sb.store(&address{array, e.Lbrace, nil}, - zeroValue(fn, deref(array.Type()))) - } - } - - var idx *Const - for _, e := range e.Elts { - pos := e.Pos() - if kv, ok := e.(*ast.KeyValueExpr); ok { - idx = b.expr(fn, kv.Key).(*Const) - pos = kv.Colon - e = kv.Value - } else { - var idxval int64 - if idx != nil { - idxval = idx.Int64() + 1 - } - idx = intConst(idxval) - } - iaddr := &IndexAddr{ - X: array, - Index: idx, - } - iaddr.setType(types.NewPointer(at.Elem())) - fn.emit(iaddr) - if t != at { // slice - // backing array is unaliased => storebuf not needed. - b.assign(fn, &address{addr: iaddr, pos: pos, expr: e}, e, true, nil) - } else { - b.assign(fn, &address{addr: iaddr, pos: pos, expr: e}, e, true, sb) - } - } - - if t != at { // slice - s := &Slice{X: array} - s.setPos(e.Lbrace) - s.setType(typ) - sb.store(&address{addr: addr, pos: e.Lbrace, expr: e}, fn.emit(s)) - } - - case *types.Map: - m := &MakeMap{Reserve: intConst(int64(len(e.Elts)))} - m.setPos(e.Lbrace) - m.setType(typ) - fn.emit(m) - for _, e := range e.Elts { - e := e.(*ast.KeyValueExpr) - - // If a key expression in a map literal is itself a - // composite literal, the type may be omitted. - // For example: - // map[*struct{}]bool{{}: true} - // An &-operation may be implied: - // map[*struct{}]bool{&struct{}{}: true} - var key Value - if _, ok := unparen(e.Key).(*ast.CompositeLit); ok && isPointer(t.Key()) { - // A CompositeLit never evaluates to a pointer, - // so if the type of the location is a pointer, - // an &-operation is implied. - key = b.addr(fn, e.Key, true).address(fn) - } else { - key = b.expr(fn, e.Key) - } - - loc := element{ - m: m, - k: emitConv(fn, key, t.Key()), - t: t.Elem(), - pos: e.Colon, - } - - // We call assign() only because it takes care - // of any &-operation required in the recursive - // case, e.g., - // map[int]*struct{}{0: {}} implies &struct{}{}. - // In-place update is of course impossible, - // and no storebuf is needed. - b.assign(fn, &loc, e.Value, true, nil) - } - sb.store(&address{addr: addr, pos: e.Lbrace, expr: e}, m) - - default: - panic("unexpected CompositeLit type: " + t.String()) - } -} - -// switchStmt emits to fn code for the switch statement s, optionally -// labelled by label. -// -func (b *builder) switchStmt(fn *Function, s *ast.SwitchStmt, label *lblock) { - // We treat SwitchStmt like a sequential if-else chain. - // Multiway dispatch can be recovered later by ssautil.Switches() - // to those cases that are free of side effects. - if s.Init != nil { - b.stmt(fn, s.Init) - } - var tag Value = vTrue - if s.Tag != nil { - tag = b.expr(fn, s.Tag) - } - done := fn.newBasicBlock("switch.done") - if label != nil { - label._break = done - } - // We pull the default case (if present) down to the end. - // But each fallthrough label must point to the next - // body block in source order, so we preallocate a - // body block (fallthru) for the next case. - // Unfortunately this makes for a confusing block order. - var dfltBody *[]ast.Stmt - var dfltFallthrough *BasicBlock - var fallthru, dfltBlock *BasicBlock - ncases := len(s.Body.List) - for i, clause := range s.Body.List { - body := fallthru - if body == nil { - body = fn.newBasicBlock("switch.body") // first case only - } - - // Preallocate body block for the next case. - fallthru = done - if i+1 < ncases { - fallthru = fn.newBasicBlock("switch.body") - } - - cc := clause.(*ast.CaseClause) - if cc.List == nil { - // Default case. - dfltBody = &cc.Body - dfltFallthrough = fallthru - dfltBlock = body - continue - } - - var nextCond *BasicBlock - for _, cond := range cc.List { - nextCond = fn.newBasicBlock("switch.next") - // TODO(adonovan): opt: when tag==vTrue, we'd - // get better code if we use b.cond(cond) - // instead of BinOp(EQL, tag, b.expr(cond)) - // followed by If. Don't forget conversions - // though. - cond := emitCompare(fn, token.EQL, tag, b.expr(fn, cond), token.NoPos) - emitIf(fn, cond, body, nextCond) - fn.currentBlock = nextCond - } - fn.currentBlock = body - fn.targets = &targets{ - tail: fn.targets, - _break: done, - _fallthrough: fallthru, - } - b.stmtList(fn, cc.Body) - fn.targets = fn.targets.tail - emitJump(fn, done) - fn.currentBlock = nextCond - } - if dfltBlock != nil { - emitJump(fn, dfltBlock) - fn.currentBlock = dfltBlock - fn.targets = &targets{ - tail: fn.targets, - _break: done, - _fallthrough: dfltFallthrough, - } - b.stmtList(fn, *dfltBody) - fn.targets = fn.targets.tail - } - emitJump(fn, done) - fn.currentBlock = done -} - -// typeSwitchStmt emits to fn code for the type switch statement s, optionally -// labelled by label. -// -func (b *builder) typeSwitchStmt(fn *Function, s *ast.TypeSwitchStmt, label *lblock) { - // We treat TypeSwitchStmt like a sequential if-else chain. - // Multiway dispatch can be recovered later by ssautil.Switches(). - - // Typeswitch lowering: - // - // var x X - // switch y := x.(type) { - // case T1, T2: S1 // >1 (y := x) - // case nil: SN // nil (y := x) - // default: SD // 0 types (y := x) - // case T3: S3 // 1 type (y := x.(T3)) - // } - // - // ...s.Init... - // x := eval x - // .caseT1: - // t1, ok1 := typeswitch,ok x - // if ok1 then goto S1 else goto .caseT2 - // .caseT2: - // t2, ok2 := typeswitch,ok x - // if ok2 then goto S1 else goto .caseNil - // .S1: - // y := x - // ...S1... - // goto done - // .caseNil: - // if t2, ok2 := typeswitch,ok x - // if x == nil then goto SN else goto .caseT3 - // .SN: - // y := x - // ...SN... - // goto done - // .caseT3: - // t3, ok3 := typeswitch,ok x - // if ok3 then goto S3 else goto default - // .S3: - // y := t3 - // ...S3... - // goto done - // .default: - // y := x - // ...SD... - // goto done - // .done: - - if s.Init != nil { - b.stmt(fn, s.Init) - } - - var x Value - switch ass := s.Assign.(type) { - case *ast.ExprStmt: // x.(type) - x = b.expr(fn, unparen(ass.X).(*ast.TypeAssertExpr).X) - case *ast.AssignStmt: // y := x.(type) - x = b.expr(fn, unparen(ass.Rhs[0]).(*ast.TypeAssertExpr).X) - } - - done := fn.newBasicBlock("typeswitch.done") - if label != nil { - label._break = done - } - var default_ *ast.CaseClause - for _, clause := range s.Body.List { - cc := clause.(*ast.CaseClause) - if cc.List == nil { - default_ = cc - continue - } - body := fn.newBasicBlock("typeswitch.body") - var next *BasicBlock - var casetype types.Type - var ti Value // ti, ok := typeassert,ok x - for _, cond := range cc.List { - next = fn.newBasicBlock("typeswitch.next") - casetype = fn.Pkg.typeOf(cond) - var condv Value - if casetype == tUntypedNil { - condv = emitCompare(fn, token.EQL, x, nilConst(x.Type()), token.NoPos) - ti = x - } else { - yok := emitTypeTest(fn, x, casetype, cc.Case) - ti = emitExtract(fn, yok, 0) - condv = emitExtract(fn, yok, 1) - } - emitIf(fn, condv, body, next) - fn.currentBlock = next - } - if len(cc.List) != 1 { - ti = x - } - fn.currentBlock = body - b.typeCaseBody(fn, cc, ti, done) - fn.currentBlock = next - } - if default_ != nil { - b.typeCaseBody(fn, default_, x, done) - } else { - emitJump(fn, done) - } - fn.currentBlock = done -} - -func (b *builder) typeCaseBody(fn *Function, cc *ast.CaseClause, x Value, done *BasicBlock) { - if obj := fn.Pkg.info.Implicits[cc]; obj != nil { - // In a switch y := x.(type), each case clause - // implicitly declares a distinct object y. - // In a single-type case, y has that type. - // In multi-type cases, 'case nil' and default, - // y has the same type as the interface operand. - emitStore(fn, fn.addNamedLocal(obj), x, obj.Pos()) - } - fn.targets = &targets{ - tail: fn.targets, - _break: done, - } - b.stmtList(fn, cc.Body) - fn.targets = fn.targets.tail - emitJump(fn, done) -} - -// selectStmt emits to fn code for the select statement s, optionally -// labelled by label. -// -func (b *builder) selectStmt(fn *Function, s *ast.SelectStmt, label *lblock) { - // A blocking select of a single case degenerates to a - // simple send or receive. - // TODO(adonovan): opt: is this optimization worth its weight? - if len(s.Body.List) == 1 { - clause := s.Body.List[0].(*ast.CommClause) - if clause.Comm != nil { - b.stmt(fn, clause.Comm) - done := fn.newBasicBlock("select.done") - if label != nil { - label._break = done - } - fn.targets = &targets{ - tail: fn.targets, - _break: done, - } - b.stmtList(fn, clause.Body) - fn.targets = fn.targets.tail - emitJump(fn, done) - fn.currentBlock = done - return - } - } - - // First evaluate all channels in all cases, and find - // the directions of each state. - var states []*SelectState - blocking := true - debugInfo := fn.debugInfo() - for _, clause := range s.Body.List { - var st *SelectState - switch comm := clause.(*ast.CommClause).Comm.(type) { - case nil: // default case - blocking = false - continue - - case *ast.SendStmt: // ch<- i - ch := b.expr(fn, comm.Chan) - st = &SelectState{ - Dir: types.SendOnly, - Chan: ch, - Send: emitConv(fn, b.expr(fn, comm.Value), - ch.Type().Underlying().(*types.Chan).Elem()), - Pos: comm.Arrow, - } - if debugInfo { - st.DebugNode = comm - } - - case *ast.AssignStmt: // x := <-ch - recv := unparen(comm.Rhs[0]).(*ast.UnaryExpr) - st = &SelectState{ - Dir: types.RecvOnly, - Chan: b.expr(fn, recv.X), - Pos: recv.OpPos, - } - if debugInfo { - st.DebugNode = recv - } - - case *ast.ExprStmt: // <-ch - recv := unparen(comm.X).(*ast.UnaryExpr) - st = &SelectState{ - Dir: types.RecvOnly, - Chan: b.expr(fn, recv.X), - Pos: recv.OpPos, - } - if debugInfo { - st.DebugNode = recv - } - } - states = append(states, st) - } - - // We dispatch on the (fair) result of Select using a - // sequential if-else chain, in effect: - // - // idx, recvOk, r0...r_n-1 := select(...) - // if idx == 0 { // receive on channel 0 (first receive => r0) - // x, ok := r0, recvOk - // ...state0... - // } else if v == 1 { // send on channel 1 - // ...state1... - // } else { - // ...default... - // } - sel := &Select{ - States: states, - Blocking: blocking, - } - sel.setPos(s.Select) - var vars []*types.Var - vars = append(vars, varIndex, varOk) - for _, st := range states { - if st.Dir == types.RecvOnly { - tElem := st.Chan.Type().Underlying().(*types.Chan).Elem() - vars = append(vars, anonVar(tElem)) - } - } - sel.setType(types.NewTuple(vars...)) - - fn.emit(sel) - idx := emitExtract(fn, sel, 0) - - done := fn.newBasicBlock("select.done") - if label != nil { - label._break = done - } - - var defaultBody *[]ast.Stmt - state := 0 - r := 2 // index in 'sel' tuple of value; increments if st.Dir==RECV - for _, cc := range s.Body.List { - clause := cc.(*ast.CommClause) - if clause.Comm == nil { - defaultBody = &clause.Body - continue - } - body := fn.newBasicBlock("select.body") - next := fn.newBasicBlock("select.next") - emitIf(fn, emitCompare(fn, token.EQL, idx, intConst(int64(state)), token.NoPos), body, next) - fn.currentBlock = body - fn.targets = &targets{ - tail: fn.targets, - _break: done, - } - switch comm := clause.Comm.(type) { - case *ast.ExprStmt: // <-ch - if debugInfo { - v := emitExtract(fn, sel, r) - emitDebugRef(fn, states[state].DebugNode.(ast.Expr), v, false) - } - r++ - - case *ast.AssignStmt: // x := <-states[state].Chan - if comm.Tok == token.DEFINE { - fn.addLocalForIdent(comm.Lhs[0].(*ast.Ident)) - } - x := b.addr(fn, comm.Lhs[0], false) // non-escaping - v := emitExtract(fn, sel, r) - if debugInfo { - emitDebugRef(fn, states[state].DebugNode.(ast.Expr), v, false) - } - x.store(fn, v) - - if len(comm.Lhs) == 2 { // x, ok := ... - if comm.Tok == token.DEFINE { - fn.addLocalForIdent(comm.Lhs[1].(*ast.Ident)) - } - ok := b.addr(fn, comm.Lhs[1], false) // non-escaping - ok.store(fn, emitExtract(fn, sel, 1)) - } - r++ - } - b.stmtList(fn, clause.Body) - fn.targets = fn.targets.tail - emitJump(fn, done) - fn.currentBlock = next - state++ - } - if defaultBody != nil { - fn.targets = &targets{ - tail: fn.targets, - _break: done, - } - b.stmtList(fn, *defaultBody) - fn.targets = fn.targets.tail - } else { - // A blocking select must match some case. - // (This should really be a runtime.errorString, not a string.) - fn.emit(&Panic{ - X: emitConv(fn, stringConst("blocking select matched no case"), tEface), - }) - fn.currentBlock = fn.newBasicBlock("unreachable") - } - emitJump(fn, done) - fn.currentBlock = done -} - -// forStmt emits to fn code for the for statement s, optionally -// labelled by label. -// -func (b *builder) forStmt(fn *Function, s *ast.ForStmt, label *lblock) { - // ...init... - // jump loop - // loop: - // if cond goto body else done - // body: - // ...body... - // jump post - // post: (target of continue) - // ...post... - // jump loop - // done: (target of break) - if s.Init != nil { - b.stmt(fn, s.Init) - } - body := fn.newBasicBlock("for.body") - done := fn.newBasicBlock("for.done") // target of 'break' - loop := body // target of back-edge - if s.Cond != nil { - loop = fn.newBasicBlock("for.loop") - } - cont := loop // target of 'continue' - if s.Post != nil { - cont = fn.newBasicBlock("for.post") - } - if label != nil { - label._break = done - label._continue = cont - } - emitJump(fn, loop) - fn.currentBlock = loop - if loop != body { - b.cond(fn, s.Cond, body, done) - fn.currentBlock = body - } - fn.targets = &targets{ - tail: fn.targets, - _break: done, - _continue: cont, - } - b.stmt(fn, s.Body) - fn.targets = fn.targets.tail - emitJump(fn, cont) - - if s.Post != nil { - fn.currentBlock = cont - b.stmt(fn, s.Post) - emitJump(fn, loop) // back-edge - } - fn.currentBlock = done -} - -// rangeIndexed emits to fn the header for an integer-indexed loop -// over array, *array or slice value x. -// The v result is defined only if tv is non-nil. -// forPos is the position of the "for" token. -// -func (b *builder) rangeIndexed(fn *Function, x Value, tv types.Type, pos token.Pos) (k, v Value, loop, done *BasicBlock) { - // - // length = len(x) - // index = -1 - // loop: (target of continue) - // index++ - // if index < length goto body else done - // body: - // k = index - // v = x[index] - // ...body... - // jump loop - // done: (target of break) - - // Determine number of iterations. - var length Value - if arr, ok := deref(x.Type()).Underlying().(*types.Array); ok { - // For array or *array, the number of iterations is - // known statically thanks to the type. We avoid a - // data dependence upon x, permitting later dead-code - // elimination if x is pure, static unrolling, etc. - // Ranging over a nil *array may have >0 iterations. - // We still generate code for x, in case it has effects. - length = intConst(arr.Len()) - } else { - // length = len(x). - var c Call - c.Call.Value = makeLen(x.Type()) - c.Call.Args = []Value{x} - c.setType(tInt) - length = fn.emit(&c) - } - - index := fn.addLocal(tInt, token.NoPos) - emitStore(fn, index, intConst(-1), pos) - - loop = fn.newBasicBlock("rangeindex.loop") - emitJump(fn, loop) - fn.currentBlock = loop - - incr := &BinOp{ - Op: token.ADD, - X: emitLoad(fn, index), - Y: vOne, - } - incr.setType(tInt) - emitStore(fn, index, fn.emit(incr), pos) - - body := fn.newBasicBlock("rangeindex.body") - done = fn.newBasicBlock("rangeindex.done") - emitIf(fn, emitCompare(fn, token.LSS, incr, length, token.NoPos), body, done) - fn.currentBlock = body - - k = emitLoad(fn, index) - if tv != nil { - switch t := x.Type().Underlying().(type) { - case *types.Array: - instr := &Index{ - X: x, - Index: k, - } - instr.setType(t.Elem()) - instr.setPos(x.Pos()) - v = fn.emit(instr) - - case *types.Pointer: // *array - instr := &IndexAddr{ - X: x, - Index: k, - } - instr.setType(types.NewPointer(t.Elem().Underlying().(*types.Array).Elem())) - instr.setPos(x.Pos()) - v = emitLoad(fn, fn.emit(instr)) - - case *types.Slice: - instr := &IndexAddr{ - X: x, - Index: k, - } - instr.setType(types.NewPointer(t.Elem())) - instr.setPos(x.Pos()) - v = emitLoad(fn, fn.emit(instr)) - - default: - panic("rangeIndexed x:" + t.String()) - } - } - return -} - -// rangeIter emits to fn the header for a loop using -// Range/Next/Extract to iterate over map or string value x. -// tk and tv are the types of the key/value results k and v, or nil -// if the respective component is not wanted. -// -func (b *builder) rangeIter(fn *Function, x Value, tk, tv types.Type, pos token.Pos) (k, v Value, loop, done *BasicBlock) { - // - // it = range x - // loop: (target of continue) - // okv = next it (ok, key, value) - // ok = extract okv #0 - // if ok goto body else done - // body: - // k = extract okv #1 - // v = extract okv #2 - // ...body... - // jump loop - // done: (target of break) - // - - if tk == nil { - tk = tInvalid - } - if tv == nil { - tv = tInvalid - } - - rng := &Range{X: x} - rng.setPos(pos) - rng.setType(tRangeIter) - it := fn.emit(rng) - - loop = fn.newBasicBlock("rangeiter.loop") - emitJump(fn, loop) - fn.currentBlock = loop - - _, isString := x.Type().Underlying().(*types.Basic) - - okv := &Next{ - Iter: it, - IsString: isString, - } - okv.setType(types.NewTuple( - varOk, - newVar("k", tk), - newVar("v", tv), - )) - fn.emit(okv) - - body := fn.newBasicBlock("rangeiter.body") - done = fn.newBasicBlock("rangeiter.done") - emitIf(fn, emitExtract(fn, okv, 0), body, done) - fn.currentBlock = body - - if tk != tInvalid { - k = emitExtract(fn, okv, 1) - } - if tv != tInvalid { - v = emitExtract(fn, okv, 2) - } - return -} - -// rangeChan emits to fn the header for a loop that receives from -// channel x until it fails. -// tk is the channel's element type, or nil if the k result is -// not wanted -// pos is the position of the '=' or ':=' token. -// -func (b *builder) rangeChan(fn *Function, x Value, tk types.Type, pos token.Pos) (k Value, loop, done *BasicBlock) { - // - // loop: (target of continue) - // ko = <-x (key, ok) - // ok = extract ko #1 - // if ok goto body else done - // body: - // k = extract ko #0 - // ... - // goto loop - // done: (target of break) - - loop = fn.newBasicBlock("rangechan.loop") - emitJump(fn, loop) - fn.currentBlock = loop - recv := &UnOp{ - Op: token.ARROW, - X: x, - CommaOk: true, - } - recv.setPos(pos) - recv.setType(types.NewTuple( - newVar("k", x.Type().Underlying().(*types.Chan).Elem()), - varOk, - )) - ko := fn.emit(recv) - body := fn.newBasicBlock("rangechan.body") - done = fn.newBasicBlock("rangechan.done") - emitIf(fn, emitExtract(fn, ko, 1), body, done) - fn.currentBlock = body - if tk != nil { - k = emitExtract(fn, ko, 0) - } - return -} - -// rangeStmt emits to fn code for the range statement s, optionally -// labelled by label. -// -func (b *builder) rangeStmt(fn *Function, s *ast.RangeStmt, label *lblock) { - var tk, tv types.Type - if s.Key != nil && !isBlankIdent(s.Key) { - tk = fn.Pkg.typeOf(s.Key) - } - if s.Value != nil && !isBlankIdent(s.Value) { - tv = fn.Pkg.typeOf(s.Value) - } - - // If iteration variables are defined (:=), this - // occurs once outside the loop. - // - // Unlike a short variable declaration, a RangeStmt - // using := never redeclares an existing variable; it - // always creates a new one. - if s.Tok == token.DEFINE { - if tk != nil { - fn.addLocalForIdent(s.Key.(*ast.Ident)) - } - if tv != nil { - fn.addLocalForIdent(s.Value.(*ast.Ident)) - } - } - - x := b.expr(fn, s.X) - - var k, v Value - var loop, done *BasicBlock - switch rt := x.Type().Underlying().(type) { - case *types.Slice, *types.Array, *types.Pointer: // *array - k, v, loop, done = b.rangeIndexed(fn, x, tv, s.For) - - case *types.Chan: - k, loop, done = b.rangeChan(fn, x, tk, s.For) - - case *types.Map, *types.Basic: // string - k, v, loop, done = b.rangeIter(fn, x, tk, tv, s.For) - - default: - panic("Cannot range over: " + rt.String()) - } - - // Evaluate both LHS expressions before we update either. - var kl, vl lvalue - if tk != nil { - kl = b.addr(fn, s.Key, false) // non-escaping - } - if tv != nil { - vl = b.addr(fn, s.Value, false) // non-escaping - } - if tk != nil { - kl.store(fn, k) - } - if tv != nil { - vl.store(fn, v) - } - - if label != nil { - label._break = done - label._continue = loop - } - - fn.targets = &targets{ - tail: fn.targets, - _break: done, - _continue: loop, - } - b.stmt(fn, s.Body) - fn.targets = fn.targets.tail - emitJump(fn, loop) // back-edge - fn.currentBlock = done -} - -// stmt lowers statement s to SSA form, emitting code to fn. -func (b *builder) stmt(fn *Function, _s ast.Stmt) { - // The label of the current statement. If non-nil, its _goto - // target is always set; its _break and _continue are set only - // within the body of switch/typeswitch/select/for/range. - // It is effectively an additional default-nil parameter of stmt(). - var label *lblock -start: - switch s := _s.(type) { - case *ast.EmptyStmt: - // ignore. (Usually removed by gofmt.) - - case *ast.DeclStmt: // Con, Var or Typ - d := s.Decl.(*ast.GenDecl) - if d.Tok == token.VAR { - for _, spec := range d.Specs { - if vs, ok := spec.(*ast.ValueSpec); ok { - b.localValueSpec(fn, vs) - } - } - } - - case *ast.LabeledStmt: - label = fn.labelledBlock(s.Label) - emitJump(fn, label._goto) - fn.currentBlock = label._goto - _s = s.Stmt - goto start // effectively: tailcall stmt(fn, s.Stmt, label) - - case *ast.ExprStmt: - b.expr(fn, s.X) - - case *ast.SendStmt: - fn.emit(&Send{ - Chan: b.expr(fn, s.Chan), - X: emitConv(fn, b.expr(fn, s.Value), - fn.Pkg.typeOf(s.Chan).Underlying().(*types.Chan).Elem()), - pos: s.Arrow, - }) - - case *ast.IncDecStmt: - op := token.ADD - if s.Tok == token.DEC { - op = token.SUB - } - loc := b.addr(fn, s.X, false) - b.assignOp(fn, loc, NewConst(constant.MakeInt64(1), loc.typ()), op, s.Pos()) - - case *ast.AssignStmt: - switch s.Tok { - case token.ASSIGN, token.DEFINE: - b.assignStmt(fn, s.Lhs, s.Rhs, s.Tok == token.DEFINE) - - default: // +=, etc. - op := s.Tok + token.ADD - token.ADD_ASSIGN - b.assignOp(fn, b.addr(fn, s.Lhs[0], false), b.expr(fn, s.Rhs[0]), op, s.Pos()) - } - - case *ast.GoStmt: - // The "intrinsics" new/make/len/cap are forbidden here. - // panic is treated like an ordinary function call. - v := Go{pos: s.Go} - b.setCall(fn, s.Call, &v.Call) - fn.emit(&v) - - case *ast.DeferStmt: - // The "intrinsics" new/make/len/cap are forbidden here. - // panic is treated like an ordinary function call. - v := Defer{pos: s.Defer} - b.setCall(fn, s.Call, &v.Call) - fn.emit(&v) - - // A deferred call can cause recovery from panic, - // and control resumes at the Recover block. - createRecoverBlock(fn) - - case *ast.ReturnStmt: - var results []Value - if len(s.Results) == 1 && fn.Signature.Results().Len() > 1 { - // Return of one expression in a multi-valued function. - tuple := b.exprN(fn, s.Results[0]) - ttuple := tuple.Type().(*types.Tuple) - for i, n := 0, ttuple.Len(); i < n; i++ { - results = append(results, - emitConv(fn, emitExtract(fn, tuple, i), - fn.Signature.Results().At(i).Type())) - } - } else { - // 1:1 return, or no-arg return in non-void function. - for i, r := range s.Results { - v := emitConv(fn, b.expr(fn, r), fn.Signature.Results().At(i).Type()) - results = append(results, v) - } - } - if fn.namedResults != nil { - // Function has named result parameters (NRPs). - // Perform parallel assignment of return operands to NRPs. - for i, r := range results { - emitStore(fn, fn.namedResults[i], r, s.Return) - } - } - // Run function calls deferred in this - // function when explicitly returning from it. - fn.emit(new(RunDefers)) - if fn.namedResults != nil { - // Reload NRPs to form the result tuple. - results = results[:0] - for _, r := range fn.namedResults { - results = append(results, emitLoad(fn, r)) - } - } - fn.emit(&Return{Results: results, pos: s.Return}) - fn.currentBlock = fn.newBasicBlock("unreachable") - - case *ast.BranchStmt: - var block *BasicBlock - switch s.Tok { - case token.BREAK: - if s.Label != nil { - block = fn.labelledBlock(s.Label)._break - } else { - for t := fn.targets; t != nil && block == nil; t = t.tail { - block = t._break - } - } - - case token.CONTINUE: - if s.Label != nil { - block = fn.labelledBlock(s.Label)._continue - } else { - for t := fn.targets; t != nil && block == nil; t = t.tail { - block = t._continue - } - } - - case token.FALLTHROUGH: - for t := fn.targets; t != nil && block == nil; t = t.tail { - block = t._fallthrough - } - - case token.GOTO: - block = fn.labelledBlock(s.Label)._goto - } - emitJump(fn, block) - fn.currentBlock = fn.newBasicBlock("unreachable") - - case *ast.BlockStmt: - b.stmtList(fn, s.List) - - case *ast.IfStmt: - if s.Init != nil { - b.stmt(fn, s.Init) - } - then := fn.newBasicBlock("if.then") - done := fn.newBasicBlock("if.done") - els := done - if s.Else != nil { - els = fn.newBasicBlock("if.else") - } - b.cond(fn, s.Cond, then, els) - fn.currentBlock = then - b.stmt(fn, s.Body) - emitJump(fn, done) - - if s.Else != nil { - fn.currentBlock = els - b.stmt(fn, s.Else) - emitJump(fn, done) - } - - fn.currentBlock = done - - case *ast.SwitchStmt: - b.switchStmt(fn, s, label) - - case *ast.TypeSwitchStmt: - b.typeSwitchStmt(fn, s, label) - - case *ast.SelectStmt: - b.selectStmt(fn, s, label) - - case *ast.ForStmt: - b.forStmt(fn, s, label) - - case *ast.RangeStmt: - b.rangeStmt(fn, s, label) - - default: - panic(fmt.Sprintf("unexpected statement kind: %T", s)) - } -} - -// buildFunction builds SSA code for the body of function fn. Idempotent. -func (b *builder) buildFunction(fn *Function) { - if fn.Blocks != nil { - return // building already started - } - - var recvField *ast.FieldList - var body *ast.BlockStmt - var functype *ast.FuncType - switch n := fn.syntax.(type) { - case nil: - return // not a Go source function. (Synthetic, or from object file.) - case *ast.FuncDecl: - functype = n.Type - recvField = n.Recv - body = n.Body - case *ast.FuncLit: - functype = n.Type - body = n.Body - default: - panic(n) - } - - if body == nil { - // External function. - if fn.Params == nil { - // This condition ensures we add a non-empty - // params list once only, but we may attempt - // the degenerate empty case repeatedly. - // TODO(adonovan): opt: don't do that. - - // We set Function.Params even though there is no body - // code to reference them. This simplifies clients. - if recv := fn.Signature.Recv(); recv != nil { - fn.addParamObj(recv) - } - params := fn.Signature.Params() - for i, n := 0, params.Len(); i < n; i++ { - fn.addParamObj(params.At(i)) - } - } - return - } - if fn.Prog.mode&LogSource != 0 { - defer logStack("build function %s @ %s", fn, fn.Prog.Fset.Position(fn.pos))() - } - fn.startBody() - fn.createSyntacticParams(recvField, functype) - b.stmt(fn, body) - if cb := fn.currentBlock; cb != nil && (cb == fn.Blocks[0] || cb == fn.Recover || cb.Preds != nil) { - // Control fell off the end of the function's body block. - // - // Block optimizations eliminate the current block, if - // unreachable. It is a builder invariant that - // if this no-arg return is ill-typed for - // fn.Signature.Results, this block must be - // unreachable. The sanity checker checks this. - fn.emit(new(RunDefers)) - fn.emit(new(Return)) - } - fn.finishBody() -} - -// buildFuncDecl builds SSA code for the function or method declared -// by decl in package pkg. -// -func (b *builder) buildFuncDecl(pkg *Package, decl *ast.FuncDecl) { - id := decl.Name - if isBlankIdent(id) { - return // discard - } - fn := pkg.values[pkg.info.Defs[id]].(*Function) - if decl.Recv == nil && id.Name == "init" { - var v Call - v.Call.Value = fn - v.setType(types.NewTuple()) - pkg.init.emit(&v) - } - b.buildFunction(fn) -} - -// Build calls Package.Build for each package in prog. -// Building occurs in parallel unless the BuildSerially mode flag was set. -// -// Build is intended for whole-program analysis; a typical compiler -// need only build a single package. -// -// Build is idempotent and thread-safe. -// -func (prog *Program) Build() { - var wg sync.WaitGroup - for _, p := range prog.packages { - if prog.mode&BuildSerially != 0 { - p.Build() - } else { - wg.Add(1) - go func(p *Package) { - p.Build() - wg.Done() - }(p) - } - } - wg.Wait() -} - -// Build builds SSA code for all functions and vars in package p. -// -// Precondition: CreatePackage must have been called for all of p's -// direct imports (and hence its direct imports must have been -// error-free). -// -// Build is idempotent and thread-safe. -// -func (p *Package) Build() { p.buildOnce.Do(p.build) } - -func (p *Package) build() { - if p.info == nil { - return // synthetic package, e.g. "testmain" - } - - // Ensure we have runtime type info for all exported members. - // TODO(adonovan): ideally belongs in memberFromObject, but - // that would require package creation in topological order. - for name, mem := range p.Members { - if ast.IsExported(name) { - p.Prog.needMethodsOf(mem.Type()) - } - } - if p.Prog.mode&LogSource != 0 { - defer logStack("build %s", p)() - } - init := p.init - init.startBody() - - var done *BasicBlock - - if p.Prog.mode&BareInits == 0 { - // Make init() skip if package is already initialized. - initguard := p.Var("init$guard") - doinit := init.newBasicBlock("init.start") - done = init.newBasicBlock("init.done") - emitIf(init, emitLoad(init, initguard), done, doinit) - init.currentBlock = doinit - emitStore(init, initguard, vTrue, token.NoPos) - - // Call the init() function of each package we import. - for _, pkg := range p.Pkg.Imports() { - prereq := p.Prog.packages[pkg] - if prereq == nil { - panic(fmt.Sprintf("Package(%q).Build(): unsatisfied import: Program.CreatePackage(%q) was not called", p.Pkg.Path(), pkg.Path())) - } - var v Call - v.Call.Value = prereq.init - v.Call.pos = init.pos - v.setType(types.NewTuple()) - init.emit(&v) - } - } - - var b builder - - // Initialize package-level vars in correct order. - for _, varinit := range p.info.InitOrder { - if init.Prog.mode&LogSource != 0 { - fmt.Fprintf(os.Stderr, "build global initializer %v @ %s\n", - varinit.Lhs, p.Prog.Fset.Position(varinit.Rhs.Pos())) - } - if len(varinit.Lhs) == 1 { - // 1:1 initialization: var x, y = a(), b() - var lval lvalue - if v := varinit.Lhs[0]; v.Name() != "_" { - lval = &address{addr: p.values[v].(*Global), pos: v.Pos()} - } else { - lval = blank{} - } - b.assign(init, lval, varinit.Rhs, true, nil) - } else { - // n:1 initialization: var x, y := f() - tuple := b.exprN(init, varinit.Rhs) - for i, v := range varinit.Lhs { - if v.Name() == "_" { - continue - } - emitStore(init, p.values[v].(*Global), emitExtract(init, tuple, i), v.Pos()) - } - } - } - - // Build all package-level functions, init functions - // and methods, including unreachable/blank ones. - // We build them in source order, but it's not significant. - for _, file := range p.files { - for _, decl := range file.Decls { - if decl, ok := decl.(*ast.FuncDecl); ok { - b.buildFuncDecl(p, decl) - } - } - } - - // Finish up init(). - if p.Prog.mode&BareInits == 0 { - emitJump(init, done) - init.currentBlock = done - } - init.emit(new(Return)) - init.finishBody() - - p.info = nil // We no longer need ASTs or go/types deductions. - - if p.Prog.mode&SanityCheckFunctions != 0 { - sanityCheckPackage(p) - } -} - -// Like ObjectOf, but panics instead of returning nil. -// Only valid during p's create and build phases. -func (p *Package) objectOf(id *ast.Ident) types.Object { - if o := p.info.ObjectOf(id); o != nil { - return o - } - panic(fmt.Sprintf("no types.Object for ast.Ident %s @ %s", - id.Name, p.Prog.Fset.Position(id.Pos()))) -} - -// Like TypeOf, but panics instead of returning nil. -// Only valid during p's create and build phases. -func (p *Package) typeOf(e ast.Expr) types.Type { - if T := p.info.TypeOf(e); T != nil { - return T - } - panic(fmt.Sprintf("no type for %T @ %s", - e, p.Prog.Fset.Position(e.Pos()))) -} diff --git a/vendor/golang.org/x/tools/go/ssa/const.go b/vendor/golang.org/x/tools/go/ssa/const.go deleted file mode 100644 index f43792e7f37..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/const.go +++ /dev/null @@ -1,169 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file defines the Const SSA value type. - -import ( - "fmt" - "go/constant" - "go/token" - "go/types" - "strconv" -) - -// NewConst returns a new constant of the specified value and type. -// val must be valid according to the specification of Const.Value. -// -func NewConst(val constant.Value, typ types.Type) *Const { - return &Const{typ, val} -} - -// intConst returns an 'int' constant that evaluates to i. -// (i is an int64 in case the host is narrower than the target.) -func intConst(i int64) *Const { - return NewConst(constant.MakeInt64(i), tInt) -} - -// nilConst returns a nil constant of the specified type, which may -// be any reference type, including interfaces. -// -func nilConst(typ types.Type) *Const { - return NewConst(nil, typ) -} - -// stringConst returns a 'string' constant that evaluates to s. -func stringConst(s string) *Const { - return NewConst(constant.MakeString(s), tString) -} - -// zeroConst returns a new "zero" constant of the specified type, -// which must not be an array or struct type: the zero values of -// aggregates are well-defined but cannot be represented by Const. -// -func zeroConst(t types.Type) *Const { - switch t := t.(type) { - case *types.Basic: - switch { - case t.Info()&types.IsBoolean != 0: - return NewConst(constant.MakeBool(false), t) - case t.Info()&types.IsNumeric != 0: - return NewConst(constant.MakeInt64(0), t) - case t.Info()&types.IsString != 0: - return NewConst(constant.MakeString(""), t) - case t.Kind() == types.UnsafePointer: - fallthrough - case t.Kind() == types.UntypedNil: - return nilConst(t) - default: - panic(fmt.Sprint("zeroConst for unexpected type:", t)) - } - case *types.Pointer, *types.Slice, *types.Interface, *types.Chan, *types.Map, *types.Signature: - return nilConst(t) - case *types.Named: - return NewConst(zeroConst(t.Underlying()).Value, t) - case *types.Array, *types.Struct, *types.Tuple: - panic(fmt.Sprint("zeroConst applied to aggregate:", t)) - } - panic(fmt.Sprint("zeroConst: unexpected ", t)) -} - -func (c *Const) RelString(from *types.Package) string { - var s string - if c.Value == nil { - s = "nil" - } else if c.Value.Kind() == constant.String { - s = constant.StringVal(c.Value) - const max = 20 - // TODO(adonovan): don't cut a rune in half. - if len(s) > max { - s = s[:max-3] + "..." // abbreviate - } - s = strconv.Quote(s) - } else { - s = c.Value.String() - } - return s + ":" + relType(c.Type(), from) -} - -func (c *Const) Name() string { - return c.RelString(nil) -} - -func (c *Const) String() string { - return c.Name() -} - -func (c *Const) Type() types.Type { - return c.typ -} - -func (c *Const) Referrers() *[]Instruction { - return nil -} - -func (c *Const) Parent() *Function { return nil } - -func (c *Const) Pos() token.Pos { - return token.NoPos -} - -// IsNil returns true if this constant represents a typed or untyped nil value. -func (c *Const) IsNil() bool { - return c.Value == nil -} - -// TODO(adonovan): move everything below into golang.org/x/tools/go/ssa/interp. - -// Int64 returns the numeric value of this constant truncated to fit -// a signed 64-bit integer. -// -func (c *Const) Int64() int64 { - switch x := constant.ToInt(c.Value); x.Kind() { - case constant.Int: - if i, ok := constant.Int64Val(x); ok { - return i - } - return 0 - case constant.Float: - f, _ := constant.Float64Val(x) - return int64(f) - } - panic(fmt.Sprintf("unexpected constant value: %T", c.Value)) -} - -// Uint64 returns the numeric value of this constant truncated to fit -// an unsigned 64-bit integer. -// -func (c *Const) Uint64() uint64 { - switch x := constant.ToInt(c.Value); x.Kind() { - case constant.Int: - if u, ok := constant.Uint64Val(x); ok { - return u - } - return 0 - case constant.Float: - f, _ := constant.Float64Val(x) - return uint64(f) - } - panic(fmt.Sprintf("unexpected constant value: %T", c.Value)) -} - -// Float64 returns the numeric value of this constant truncated to fit -// a float64. -// -func (c *Const) Float64() float64 { - f, _ := constant.Float64Val(c.Value) - return f -} - -// Complex128 returns the complex value of this constant truncated to -// fit a complex128. -// -func (c *Const) Complex128() complex128 { - re, _ := constant.Float64Val(constant.Real(c.Value)) - im, _ := constant.Float64Val(constant.Imag(c.Value)) - return complex(re, im) -} diff --git a/vendor/golang.org/x/tools/go/ssa/create.go b/vendor/golang.org/x/tools/go/ssa/create.go deleted file mode 100644 index 85163a0c5a7..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/create.go +++ /dev/null @@ -1,270 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file implements the CREATE phase of SSA construction. -// See builder.go for explanation. - -import ( - "fmt" - "go/ast" - "go/token" - "go/types" - "os" - "sync" - - "golang.org/x/tools/go/types/typeutil" -) - -// NewProgram returns a new SSA Program. -// -// mode controls diagnostics and checking during SSA construction. -// -func NewProgram(fset *token.FileSet, mode BuilderMode) *Program { - prog := &Program{ - Fset: fset, - imported: make(map[string]*Package), - packages: make(map[*types.Package]*Package), - thunks: make(map[selectionKey]*Function), - bounds: make(map[*types.Func]*Function), - mode: mode, - } - - h := typeutil.MakeHasher() // protected by methodsMu, in effect - prog.methodSets.SetHasher(h) - prog.canon.SetHasher(h) - - return prog -} - -// memberFromObject populates package pkg with a member for the -// typechecker object obj. -// -// For objects from Go source code, syntax is the associated syntax -// tree (for funcs and vars only); it will be used during the build -// phase. -// -func memberFromObject(pkg *Package, obj types.Object, syntax ast.Node) { - name := obj.Name() - switch obj := obj.(type) { - case *types.Builtin: - if pkg.Pkg != types.Unsafe { - panic("unexpected builtin object: " + obj.String()) - } - - case *types.TypeName: - pkg.Members[name] = &Type{ - object: obj, - pkg: pkg, - } - - case *types.Const: - c := &NamedConst{ - object: obj, - Value: NewConst(obj.Val(), obj.Type()), - pkg: pkg, - } - pkg.values[obj] = c.Value - pkg.Members[name] = c - - case *types.Var: - g := &Global{ - Pkg: pkg, - name: name, - object: obj, - typ: types.NewPointer(obj.Type()), // address - pos: obj.Pos(), - } - pkg.values[obj] = g - pkg.Members[name] = g - - case *types.Func: - sig := obj.Type().(*types.Signature) - if sig.Recv() == nil && name == "init" { - pkg.ninit++ - name = fmt.Sprintf("init#%d", pkg.ninit) - } - fn := &Function{ - name: name, - object: obj, - Signature: sig, - syntax: syntax, - pos: obj.Pos(), - Pkg: pkg, - Prog: pkg.Prog, - } - if syntax == nil { - fn.Synthetic = "loaded from gc object file" - } - - pkg.values[obj] = fn - if sig.Recv() == nil { - pkg.Members[name] = fn // package-level function - } - - default: // (incl. *types.Package) - panic("unexpected Object type: " + obj.String()) - } -} - -// membersFromDecl populates package pkg with members for each -// typechecker object (var, func, const or type) associated with the -// specified decl. -// -func membersFromDecl(pkg *Package, decl ast.Decl) { - switch decl := decl.(type) { - case *ast.GenDecl: // import, const, type or var - switch decl.Tok { - case token.CONST: - for _, spec := range decl.Specs { - for _, id := range spec.(*ast.ValueSpec).Names { - if !isBlankIdent(id) { - memberFromObject(pkg, pkg.info.Defs[id], nil) - } - } - } - - case token.VAR: - for _, spec := range decl.Specs { - for _, id := range spec.(*ast.ValueSpec).Names { - if !isBlankIdent(id) { - memberFromObject(pkg, pkg.info.Defs[id], spec) - } - } - } - - case token.TYPE: - for _, spec := range decl.Specs { - id := spec.(*ast.TypeSpec).Name - if !isBlankIdent(id) { - memberFromObject(pkg, pkg.info.Defs[id], nil) - } - } - } - - case *ast.FuncDecl: - id := decl.Name - if !isBlankIdent(id) { - memberFromObject(pkg, pkg.info.Defs[id], decl) - } - } -} - -// CreatePackage constructs and returns an SSA Package from the -// specified type-checked, error-free file ASTs, and populates its -// Members mapping. -// -// importable determines whether this package should be returned by a -// subsequent call to ImportedPackage(pkg.Path()). -// -// The real work of building SSA form for each function is not done -// until a subsequent call to Package.Build(). -// -func (prog *Program) CreatePackage(pkg *types.Package, files []*ast.File, info *types.Info, importable bool) *Package { - p := &Package{ - Prog: prog, - Members: make(map[string]Member), - values: make(map[types.Object]Value), - Pkg: pkg, - info: info, // transient (CREATE and BUILD phases) - files: files, // transient (CREATE and BUILD phases) - } - - // Add init() function. - p.init = &Function{ - name: "init", - Signature: new(types.Signature), - Synthetic: "package initializer", - Pkg: p, - Prog: prog, - } - p.Members[p.init.name] = p.init - - // CREATE phase. - // Allocate all package members: vars, funcs, consts and types. - if len(files) > 0 { - // Go source package. - for _, file := range files { - for _, decl := range file.Decls { - membersFromDecl(p, decl) - } - } - } else { - // GC-compiled binary package (or "unsafe") - // No code. - // No position information. - scope := p.Pkg.Scope() - for _, name := range scope.Names() { - obj := scope.Lookup(name) - memberFromObject(p, obj, nil) - if obj, ok := obj.(*types.TypeName); ok { - if named, ok := obj.Type().(*types.Named); ok { - for i, n := 0, named.NumMethods(); i < n; i++ { - memberFromObject(p, named.Method(i), nil) - } - } - } - } - } - - if prog.mode&BareInits == 0 { - // Add initializer guard variable. - initguard := &Global{ - Pkg: p, - name: "init$guard", - typ: types.NewPointer(tBool), - } - p.Members[initguard.Name()] = initguard - } - - if prog.mode&GlobalDebug != 0 { - p.SetDebugMode(true) - } - - if prog.mode&PrintPackages != 0 { - printMu.Lock() - p.WriteTo(os.Stdout) - printMu.Unlock() - } - - if importable { - prog.imported[p.Pkg.Path()] = p - } - prog.packages[p.Pkg] = p - - return p -} - -// printMu serializes printing of Packages/Functions to stdout. -var printMu sync.Mutex - -// AllPackages returns a new slice containing all packages in the -// program prog in unspecified order. -// -func (prog *Program) AllPackages() []*Package { - pkgs := make([]*Package, 0, len(prog.packages)) - for _, pkg := range prog.packages { - pkgs = append(pkgs, pkg) - } - return pkgs -} - -// ImportedPackage returns the importable Package whose PkgPath -// is path, or nil if no such Package has been created. -// -// A parameter to CreatePackage determines whether a package should be -// considered importable. For example, no import declaration can resolve -// to the ad-hoc main package created by 'go build foo.go'. -// -// TODO(adonovan): rethink this function and the "importable" concept; -// most packages are importable. This function assumes that all -// types.Package.Path values are unique within the ssa.Program, which is -// false---yet this function remains very convenient. -// Clients should use (*Program).Package instead where possible. -// SSA doesn't really need a string-keyed map of packages. -// -func (prog *Program) ImportedPackage(path string) *Package { - return prog.imported[path] -} diff --git a/vendor/golang.org/x/tools/go/ssa/doc.go b/vendor/golang.org/x/tools/go/ssa/doc.go deleted file mode 100644 index 1a13640f9d5..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/doc.go +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package ssa defines a representation of the elements of Go programs -// (packages, types, functions, variables and constants) using a -// static single-assignment (SSA) form intermediate representation -// (IR) for the bodies of functions. -// -// THIS INTERFACE IS EXPERIMENTAL AND IS LIKELY TO CHANGE. -// -// For an introduction to SSA form, see -// http://en.wikipedia.org/wiki/Static_single_assignment_form. -// This page provides a broader reading list: -// http://www.dcs.gla.ac.uk/~jsinger/ssa.html. -// -// The level of abstraction of the SSA form is intentionally close to -// the source language to facilitate construction of source analysis -// tools. It is not intended for machine code generation. -// -// All looping, branching and switching constructs are replaced with -// unstructured control flow. Higher-level control flow constructs -// such as multi-way branch can be reconstructed as needed; see -// ssautil.Switches() for an example. -// -// The simplest way to create the SSA representation of a package is -// to load typed syntax trees using golang.org/x/tools/go/packages, then -// invoke the ssautil.Packages helper function. See ExampleLoadPackages -// and ExampleWholeProgram for examples. -// The resulting ssa.Program contains all the packages and their -// members, but SSA code is not created for function bodies until a -// subsequent call to (*Package).Build or (*Program).Build. -// -// The builder initially builds a naive SSA form in which all local -// variables are addresses of stack locations with explicit loads and -// stores. Registerisation of eligible locals and φ-node insertion -// using dominance and dataflow are then performed as a second pass -// called "lifting" to improve the accuracy and performance of -// subsequent analyses; this pass can be skipped by setting the -// NaiveForm builder flag. -// -// The primary interfaces of this package are: -// -// - Member: a named member of a Go package. -// - Value: an expression that yields a value. -// - Instruction: a statement that consumes values and performs computation. -// - Node: a Value or Instruction (emphasizing its membership in the SSA value graph) -// -// A computation that yields a result implements both the Value and -// Instruction interfaces. The following table shows for each -// concrete type which of these interfaces it implements. -// -// Value? Instruction? Member? -// *Alloc ✔ ✔ -// *BinOp ✔ ✔ -// *Builtin ✔ -// *Call ✔ ✔ -// *ChangeInterface ✔ ✔ -// *ChangeType ✔ ✔ -// *Const ✔ -// *Convert ✔ ✔ -// *DebugRef ✔ -// *Defer ✔ -// *Extract ✔ ✔ -// *Field ✔ ✔ -// *FieldAddr ✔ ✔ -// *FreeVar ✔ -// *Function ✔ ✔ (func) -// *Global ✔ ✔ (var) -// *Go ✔ -// *If ✔ -// *Index ✔ ✔ -// *IndexAddr ✔ ✔ -// *Jump ✔ -// *Lookup ✔ ✔ -// *MakeChan ✔ ✔ -// *MakeClosure ✔ ✔ -// *MakeInterface ✔ ✔ -// *MakeMap ✔ ✔ -// *MakeSlice ✔ ✔ -// *MapUpdate ✔ -// *NamedConst ✔ (const) -// *Next ✔ ✔ -// *Panic ✔ -// *Parameter ✔ -// *Phi ✔ ✔ -// *Range ✔ ✔ -// *Return ✔ -// *RunDefers ✔ -// *Select ✔ ✔ -// *Send ✔ -// *Slice ✔ ✔ -// *Store ✔ -// *Type ✔ (type) -// *TypeAssert ✔ ✔ -// *UnOp ✔ ✔ -// -// Other key types in this package include: Program, Package, Function -// and BasicBlock. -// -// The program representation constructed by this package is fully -// resolved internally, i.e. it does not rely on the names of Values, -// Packages, Functions, Types or BasicBlocks for the correct -// interpretation of the program. Only the identities of objects and -// the topology of the SSA and type graphs are semantically -// significant. (There is one exception: Ids, used to identify field -// and method names, contain strings.) Avoidance of name-based -// operations simplifies the implementation of subsequent passes and -// can make them very efficient. Many objects are nonetheless named -// to aid in debugging, but it is not essential that the names be -// either accurate or unambiguous. The public API exposes a number of -// name-based maps for client convenience. -// -// The ssa/ssautil package provides various utilities that depend only -// on the public API of this package. -// -// TODO(adonovan): Consider the exceptional control-flow implications -// of defer and recover(). -// -// TODO(adonovan): write a how-to document for all the various cases -// of trying to determine corresponding elements across the four -// domains of source locations, ast.Nodes, types.Objects, -// ssa.Values/Instructions. -// -package ssa // import "golang.org/x/tools/go/ssa" diff --git a/vendor/golang.org/x/tools/go/ssa/dom.go b/vendor/golang.org/x/tools/go/ssa/dom.go deleted file mode 100644 index 12ef4308f3c..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/dom.go +++ /dev/null @@ -1,341 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file defines algorithms related to dominance. - -// Dominator tree construction ---------------------------------------- -// -// We use the algorithm described in Lengauer & Tarjan. 1979. A fast -// algorithm for finding dominators in a flowgraph. -// http://doi.acm.org/10.1145/357062.357071 -// -// We also apply the optimizations to SLT described in Georgiadis et -// al, Finding Dominators in Practice, JGAA 2006, -// http://jgaa.info/accepted/2006/GeorgiadisTarjanWerneck2006.10.1.pdf -// to avoid the need for buckets of size > 1. - -import ( - "bytes" - "fmt" - "math/big" - "os" - "sort" -) - -// Idom returns the block that immediately dominates b: -// its parent in the dominator tree, if any. -// Neither the entry node (b.Index==0) nor recover node -// (b==b.Parent().Recover()) have a parent. -// -func (b *BasicBlock) Idom() *BasicBlock { return b.dom.idom } - -// Dominees returns the list of blocks that b immediately dominates: -// its children in the dominator tree. -// -func (b *BasicBlock) Dominees() []*BasicBlock { return b.dom.children } - -// Dominates reports whether b dominates c. -func (b *BasicBlock) Dominates(c *BasicBlock) bool { - return b.dom.pre <= c.dom.pre && c.dom.post <= b.dom.post -} - -type byDomPreorder []*BasicBlock - -func (a byDomPreorder) Len() int { return len(a) } -func (a byDomPreorder) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a byDomPreorder) Less(i, j int) bool { return a[i].dom.pre < a[j].dom.pre } - -// DomPreorder returns a new slice containing the blocks of f in -// dominator tree preorder. -// -func (f *Function) DomPreorder() []*BasicBlock { - n := len(f.Blocks) - order := make(byDomPreorder, n, n) - copy(order, f.Blocks) - sort.Sort(order) - return order -} - -// domInfo contains a BasicBlock's dominance information. -type domInfo struct { - idom *BasicBlock // immediate dominator (parent in domtree) - children []*BasicBlock // nodes immediately dominated by this one - pre, post int32 // pre- and post-order numbering within domtree -} - -// ltState holds the working state for Lengauer-Tarjan algorithm -// (during which domInfo.pre is repurposed for CFG DFS preorder number). -type ltState struct { - // Each slice is indexed by b.Index. - sdom []*BasicBlock // b's semidominator - parent []*BasicBlock // b's parent in DFS traversal of CFG - ancestor []*BasicBlock // b's ancestor with least sdom -} - -// dfs implements the depth-first search part of the LT algorithm. -func (lt *ltState) dfs(v *BasicBlock, i int32, preorder []*BasicBlock) int32 { - preorder[i] = v - v.dom.pre = i // For now: DFS preorder of spanning tree of CFG - i++ - lt.sdom[v.Index] = v - lt.link(nil, v) - for _, w := range v.Succs { - if lt.sdom[w.Index] == nil { - lt.parent[w.Index] = v - i = lt.dfs(w, i, preorder) - } - } - return i -} - -// eval implements the EVAL part of the LT algorithm. -func (lt *ltState) eval(v *BasicBlock) *BasicBlock { - // TODO(adonovan): opt: do path compression per simple LT. - u := v - for ; lt.ancestor[v.Index] != nil; v = lt.ancestor[v.Index] { - if lt.sdom[v.Index].dom.pre < lt.sdom[u.Index].dom.pre { - u = v - } - } - return u -} - -// link implements the LINK part of the LT algorithm. -func (lt *ltState) link(v, w *BasicBlock) { - lt.ancestor[w.Index] = v -} - -// buildDomTree computes the dominator tree of f using the LT algorithm. -// Precondition: all blocks are reachable (e.g. optimizeBlocks has been run). -// -func buildDomTree(f *Function) { - // The step numbers refer to the original LT paper; the - // reordering is due to Georgiadis. - - // Clear any previous domInfo. - for _, b := range f.Blocks { - b.dom = domInfo{} - } - - n := len(f.Blocks) - // Allocate space for 5 contiguous [n]*BasicBlock arrays: - // sdom, parent, ancestor, preorder, buckets. - space := make([]*BasicBlock, 5*n, 5*n) - lt := ltState{ - sdom: space[0:n], - parent: space[n : 2*n], - ancestor: space[2*n : 3*n], - } - - // Step 1. Number vertices by depth-first preorder. - preorder := space[3*n : 4*n] - root := f.Blocks[0] - prenum := lt.dfs(root, 0, preorder) - recover := f.Recover - if recover != nil { - lt.dfs(recover, prenum, preorder) - } - - buckets := space[4*n : 5*n] - copy(buckets, preorder) - - // In reverse preorder... - for i := int32(n) - 1; i > 0; i-- { - w := preorder[i] - - // Step 3. Implicitly define the immediate dominator of each node. - for v := buckets[i]; v != w; v = buckets[v.dom.pre] { - u := lt.eval(v) - if lt.sdom[u.Index].dom.pre < i { - v.dom.idom = u - } else { - v.dom.idom = w - } - } - - // Step 2. Compute the semidominators of all nodes. - lt.sdom[w.Index] = lt.parent[w.Index] - for _, v := range w.Preds { - u := lt.eval(v) - if lt.sdom[u.Index].dom.pre < lt.sdom[w.Index].dom.pre { - lt.sdom[w.Index] = lt.sdom[u.Index] - } - } - - lt.link(lt.parent[w.Index], w) - - if lt.parent[w.Index] == lt.sdom[w.Index] { - w.dom.idom = lt.parent[w.Index] - } else { - buckets[i] = buckets[lt.sdom[w.Index].dom.pre] - buckets[lt.sdom[w.Index].dom.pre] = w - } - } - - // The final 'Step 3' is now outside the loop. - for v := buckets[0]; v != root; v = buckets[v.dom.pre] { - v.dom.idom = root - } - - // Step 4. Explicitly define the immediate dominator of each - // node, in preorder. - for _, w := range preorder[1:] { - if w == root || w == recover { - w.dom.idom = nil - } else { - if w.dom.idom != lt.sdom[w.Index] { - w.dom.idom = w.dom.idom.dom.idom - } - // Calculate Children relation as inverse of Idom. - w.dom.idom.dom.children = append(w.dom.idom.dom.children, w) - } - } - - pre, post := numberDomTree(root, 0, 0) - if recover != nil { - numberDomTree(recover, pre, post) - } - - // printDomTreeDot(os.Stderr, f) // debugging - // printDomTreeText(os.Stderr, root, 0) // debugging - - if f.Prog.mode&SanityCheckFunctions != 0 { - sanityCheckDomTree(f) - } -} - -// numberDomTree sets the pre- and post-order numbers of a depth-first -// traversal of the dominator tree rooted at v. These are used to -// answer dominance queries in constant time. -// -func numberDomTree(v *BasicBlock, pre, post int32) (int32, int32) { - v.dom.pre = pre - pre++ - for _, child := range v.dom.children { - pre, post = numberDomTree(child, pre, post) - } - v.dom.post = post - post++ - return pre, post -} - -// Testing utilities ---------------------------------------- - -// sanityCheckDomTree checks the correctness of the dominator tree -// computed by the LT algorithm by comparing against the dominance -// relation computed by a naive Kildall-style forward dataflow -// analysis (Algorithm 10.16 from the "Dragon" book). -// -func sanityCheckDomTree(f *Function) { - n := len(f.Blocks) - - // D[i] is the set of blocks that dominate f.Blocks[i], - // represented as a bit-set of block indices. - D := make([]big.Int, n) - - one := big.NewInt(1) - - // all is the set of all blocks; constant. - var all big.Int - all.Set(one).Lsh(&all, uint(n)).Sub(&all, one) - - // Initialization. - for i, b := range f.Blocks { - if i == 0 || b == f.Recover { - // A root is dominated only by itself. - D[i].SetBit(&D[0], 0, 1) - } else { - // All other blocks are (initially) dominated - // by every block. - D[i].Set(&all) - } - } - - // Iteration until fixed point. - for changed := true; changed; { - changed = false - for i, b := range f.Blocks { - if i == 0 || b == f.Recover { - continue - } - // Compute intersection across predecessors. - var x big.Int - x.Set(&all) - for _, pred := range b.Preds { - x.And(&x, &D[pred.Index]) - } - x.SetBit(&x, i, 1) // a block always dominates itself. - if D[i].Cmp(&x) != 0 { - D[i].Set(&x) - changed = true - } - } - } - - // Check the entire relation. O(n^2). - // The Recover block (if any) must be treated specially so we skip it. - ok := true - for i := 0; i < n; i++ { - for j := 0; j < n; j++ { - b, c := f.Blocks[i], f.Blocks[j] - if c == f.Recover { - continue - } - actual := b.Dominates(c) - expected := D[j].Bit(i) == 1 - if actual != expected { - fmt.Fprintf(os.Stderr, "dominates(%s, %s)==%t, want %t\n", b, c, actual, expected) - ok = false - } - } - } - - preorder := f.DomPreorder() - for _, b := range f.Blocks { - if got := preorder[b.dom.pre]; got != b { - fmt.Fprintf(os.Stderr, "preorder[%d]==%s, want %s\n", b.dom.pre, got, b) - ok = false - } - } - - if !ok { - panic("sanityCheckDomTree failed for " + f.String()) - } - -} - -// Printing functions ---------------------------------------- - -// printDomTree prints the dominator tree as text, using indentation. -func printDomTreeText(buf *bytes.Buffer, v *BasicBlock, indent int) { - fmt.Fprintf(buf, "%*s%s\n", 4*indent, "", v) - for _, child := range v.dom.children { - printDomTreeText(buf, child, indent+1) - } -} - -// printDomTreeDot prints the dominator tree of f in AT&T GraphViz -// (.dot) format. -func printDomTreeDot(buf *bytes.Buffer, f *Function) { - fmt.Fprintln(buf, "//", f) - fmt.Fprintln(buf, "digraph domtree {") - for i, b := range f.Blocks { - v := b.dom - fmt.Fprintf(buf, "\tn%d [label=\"%s (%d, %d)\",shape=\"rectangle\"];\n", v.pre, b, v.pre, v.post) - // TODO(adonovan): improve appearance of edges - // belonging to both dominator tree and CFG. - - // Dominator tree edge. - if i != 0 { - fmt.Fprintf(buf, "\tn%d -> n%d [style=\"solid\",weight=100];\n", v.idom.dom.pre, v.pre) - } - // CFG edges. - for _, pred := range b.Preds { - fmt.Fprintf(buf, "\tn%d -> n%d [style=\"dotted\",weight=0];\n", pred.dom.pre, v.pre) - } - } - fmt.Fprintln(buf, "}") -} diff --git a/vendor/golang.org/x/tools/go/ssa/emit.go b/vendor/golang.org/x/tools/go/ssa/emit.go deleted file mode 100644 index 1036988adcb..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/emit.go +++ /dev/null @@ -1,468 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// Helpers for emitting SSA instructions. - -import ( - "fmt" - "go/ast" - "go/token" - "go/types" -) - -// emitNew emits to f a new (heap Alloc) instruction allocating an -// object of type typ. pos is the optional source location. -// -func emitNew(f *Function, typ types.Type, pos token.Pos) *Alloc { - v := &Alloc{Heap: true} - v.setType(types.NewPointer(typ)) - v.setPos(pos) - f.emit(v) - return v -} - -// emitLoad emits to f an instruction to load the address addr into a -// new temporary, and returns the value so defined. -// -func emitLoad(f *Function, addr Value) *UnOp { - v := &UnOp{Op: token.MUL, X: addr} - v.setType(deref(addr.Type())) - f.emit(v) - return v -} - -// emitDebugRef emits to f a DebugRef pseudo-instruction associating -// expression e with value v. -// -func emitDebugRef(f *Function, e ast.Expr, v Value, isAddr bool) { - if !f.debugInfo() { - return // debugging not enabled - } - if v == nil || e == nil { - panic("nil") - } - var obj types.Object - e = unparen(e) - if id, ok := e.(*ast.Ident); ok { - if isBlankIdent(id) { - return - } - obj = f.Pkg.objectOf(id) - switch obj.(type) { - case *types.Nil, *types.Const, *types.Builtin: - return - } - } - f.emit(&DebugRef{ - X: v, - Expr: e, - IsAddr: isAddr, - object: obj, - }) -} - -// emitArith emits to f code to compute the binary operation op(x, y) -// where op is an eager shift, logical or arithmetic operation. -// (Use emitCompare() for comparisons and Builder.logicalBinop() for -// non-eager operations.) -// -func emitArith(f *Function, op token.Token, x, y Value, t types.Type, pos token.Pos) Value { - switch op { - case token.SHL, token.SHR: - x = emitConv(f, x, t) - // y may be signed or an 'untyped' constant. - // TODO(adonovan): whence signed values? - if b, ok := y.Type().Underlying().(*types.Basic); ok && b.Info()&types.IsUnsigned == 0 { - y = emitConv(f, y, types.Typ[types.Uint64]) - } - - case token.ADD, token.SUB, token.MUL, token.QUO, token.REM, token.AND, token.OR, token.XOR, token.AND_NOT: - x = emitConv(f, x, t) - y = emitConv(f, y, t) - - default: - panic("illegal op in emitArith: " + op.String()) - - } - v := &BinOp{ - Op: op, - X: x, - Y: y, - } - v.setPos(pos) - v.setType(t) - return f.emit(v) -} - -// emitCompare emits to f code compute the boolean result of -// comparison comparison 'x op y'. -// -func emitCompare(f *Function, op token.Token, x, y Value, pos token.Pos) Value { - xt := x.Type().Underlying() - yt := y.Type().Underlying() - - // Special case to optimise a tagless SwitchStmt so that - // these are equivalent - // switch { case e: ...} - // switch true { case e: ... } - // if e==true { ... } - // even in the case when e's type is an interface. - // TODO(adonovan): opt: generalise to x==true, false!=y, etc. - if x == vTrue && op == token.EQL { - if yt, ok := yt.(*types.Basic); ok && yt.Info()&types.IsBoolean != 0 { - return y - } - } - - if types.Identical(xt, yt) { - // no conversion necessary - } else if _, ok := xt.(*types.Interface); ok { - y = emitConv(f, y, x.Type()) - } else if _, ok := yt.(*types.Interface); ok { - x = emitConv(f, x, y.Type()) - } else if _, ok := x.(*Const); ok { - x = emitConv(f, x, y.Type()) - } else if _, ok := y.(*Const); ok { - y = emitConv(f, y, x.Type()) - } else { - // other cases, e.g. channels. No-op. - } - - v := &BinOp{ - Op: op, - X: x, - Y: y, - } - v.setPos(pos) - v.setType(tBool) - return f.emit(v) -} - -// isValuePreserving returns true if a conversion from ut_src to -// ut_dst is value-preserving, i.e. just a change of type. -// Precondition: neither argument is a named type. -// -func isValuePreserving(ut_src, ut_dst types.Type) bool { - // Identical underlying types? - if structTypesIdentical(ut_dst, ut_src) { - return true - } - - switch ut_dst.(type) { - case *types.Chan: - // Conversion between channel types? - _, ok := ut_src.(*types.Chan) - return ok - - case *types.Pointer: - // Conversion between pointers with identical base types? - _, ok := ut_src.(*types.Pointer) - return ok - } - return false -} - -// emitConv emits to f code to convert Value val to exactly type typ, -// and returns the converted value. Implicit conversions are required -// by language assignability rules in assignments, parameter passing, -// etc. Conversions cannot fail dynamically. -// -func emitConv(f *Function, val Value, typ types.Type) Value { - t_src := val.Type() - - // Identical types? Conversion is a no-op. - if types.Identical(t_src, typ) { - return val - } - - ut_dst := typ.Underlying() - ut_src := t_src.Underlying() - - // Just a change of type, but not value or representation? - if isValuePreserving(ut_src, ut_dst) { - c := &ChangeType{X: val} - c.setType(typ) - return f.emit(c) - } - - // Conversion to, or construction of a value of, an interface type? - if _, ok := ut_dst.(*types.Interface); ok { - // Assignment from one interface type to another? - if _, ok := ut_src.(*types.Interface); ok { - c := &ChangeInterface{X: val} - c.setType(typ) - return f.emit(c) - } - - // Untyped nil constant? Return interface-typed nil constant. - if ut_src == tUntypedNil { - return nilConst(typ) - } - - // Convert (non-nil) "untyped" literals to their default type. - if t, ok := ut_src.(*types.Basic); ok && t.Info()&types.IsUntyped != 0 { - val = emitConv(f, val, DefaultType(ut_src)) - } - - f.Pkg.Prog.needMethodsOf(val.Type()) - mi := &MakeInterface{X: val} - mi.setType(typ) - return f.emit(mi) - } - - // Conversion of a compile-time constant value? - if c, ok := val.(*Const); ok { - if _, ok := ut_dst.(*types.Basic); ok || c.IsNil() { - // Conversion of a compile-time constant to - // another constant type results in a new - // constant of the destination type and - // (initially) the same abstract value. - // We don't truncate the value yet. - return NewConst(c.Value, typ) - } - - // We're converting from constant to non-constant type, - // e.g. string -> []byte/[]rune. - } - - // A representation-changing conversion? - // At least one of {ut_src,ut_dst} must be *Basic. - // (The other may be []byte or []rune.) - _, ok1 := ut_src.(*types.Basic) - _, ok2 := ut_dst.(*types.Basic) - if ok1 || ok2 { - c := &Convert{X: val} - c.setType(typ) - return f.emit(c) - } - - panic(fmt.Sprintf("in %s: cannot convert %s (%s) to %s", f, val, val.Type(), typ)) -} - -// emitStore emits to f an instruction to store value val at location -// addr, applying implicit conversions as required by assignability rules. -// -func emitStore(f *Function, addr, val Value, pos token.Pos) *Store { - s := &Store{ - Addr: addr, - Val: emitConv(f, val, deref(addr.Type())), - pos: pos, - } - f.emit(s) - return s -} - -// emitJump emits to f a jump to target, and updates the control-flow graph. -// Postcondition: f.currentBlock is nil. -// -func emitJump(f *Function, target *BasicBlock) { - b := f.currentBlock - b.emit(new(Jump)) - addEdge(b, target) - f.currentBlock = nil -} - -// emitIf emits to f a conditional jump to tblock or fblock based on -// cond, and updates the control-flow graph. -// Postcondition: f.currentBlock is nil. -// -func emitIf(f *Function, cond Value, tblock, fblock *BasicBlock) { - b := f.currentBlock - b.emit(&If{Cond: cond}) - addEdge(b, tblock) - addEdge(b, fblock) - f.currentBlock = nil -} - -// emitExtract emits to f an instruction to extract the index'th -// component of tuple. It returns the extracted value. -// -func emitExtract(f *Function, tuple Value, index int) Value { - e := &Extract{Tuple: tuple, Index: index} - e.setType(tuple.Type().(*types.Tuple).At(index).Type()) - return f.emit(e) -} - -// emitTypeAssert emits to f a type assertion value := x.(t) and -// returns the value. x.Type() must be an interface. -// -func emitTypeAssert(f *Function, x Value, t types.Type, pos token.Pos) Value { - a := &TypeAssert{X: x, AssertedType: t} - a.setPos(pos) - a.setType(t) - return f.emit(a) -} - -// emitTypeTest emits to f a type test value,ok := x.(t) and returns -// a (value, ok) tuple. x.Type() must be an interface. -// -func emitTypeTest(f *Function, x Value, t types.Type, pos token.Pos) Value { - a := &TypeAssert{ - X: x, - AssertedType: t, - CommaOk: true, - } - a.setPos(pos) - a.setType(types.NewTuple( - newVar("value", t), - varOk, - )) - return f.emit(a) -} - -// emitTailCall emits to f a function call in tail position. The -// caller is responsible for all fields of 'call' except its type. -// Intended for wrapper methods. -// Precondition: f does/will not use deferred procedure calls. -// Postcondition: f.currentBlock is nil. -// -func emitTailCall(f *Function, call *Call) { - tresults := f.Signature.Results() - nr := tresults.Len() - if nr == 1 { - call.typ = tresults.At(0).Type() - } else { - call.typ = tresults - } - tuple := f.emit(call) - var ret Return - switch nr { - case 0: - // no-op - case 1: - ret.Results = []Value{tuple} - default: - for i := 0; i < nr; i++ { - v := emitExtract(f, tuple, i) - // TODO(adonovan): in principle, this is required: - // v = emitConv(f, o.Type, f.Signature.Results[i].Type) - // but in practice emitTailCall is only used when - // the types exactly match. - ret.Results = append(ret.Results, v) - } - } - f.emit(&ret) - f.currentBlock = nil -} - -// emitImplicitSelections emits to f code to apply the sequence of -// implicit field selections specified by indices to base value v, and -// returns the selected value. -// -// If v is the address of a struct, the result will be the address of -// a field; if it is the value of a struct, the result will be the -// value of a field. -// -func emitImplicitSelections(f *Function, v Value, indices []int) Value { - for _, index := range indices { - fld := deref(v.Type()).Underlying().(*types.Struct).Field(index) - - if isPointer(v.Type()) { - instr := &FieldAddr{ - X: v, - Field: index, - } - instr.setType(types.NewPointer(fld.Type())) - v = f.emit(instr) - // Load the field's value iff indirectly embedded. - if isPointer(fld.Type()) { - v = emitLoad(f, v) - } - } else { - instr := &Field{ - X: v, - Field: index, - } - instr.setType(fld.Type()) - v = f.emit(instr) - } - } - return v -} - -// emitFieldSelection emits to f code to select the index'th field of v. -// -// If wantAddr, the input must be a pointer-to-struct and the result -// will be the field's address; otherwise the result will be the -// field's value. -// Ident id is used for position and debug info. -// -func emitFieldSelection(f *Function, v Value, index int, wantAddr bool, id *ast.Ident) Value { - fld := deref(v.Type()).Underlying().(*types.Struct).Field(index) - if isPointer(v.Type()) { - instr := &FieldAddr{ - X: v, - Field: index, - } - instr.setPos(id.Pos()) - instr.setType(types.NewPointer(fld.Type())) - v = f.emit(instr) - // Load the field's value iff we don't want its address. - if !wantAddr { - v = emitLoad(f, v) - } - } else { - instr := &Field{ - X: v, - Field: index, - } - instr.setPos(id.Pos()) - instr.setType(fld.Type()) - v = f.emit(instr) - } - emitDebugRef(f, id, v, wantAddr) - return v -} - -// zeroValue emits to f code to produce a zero value of type t, -// and returns it. -// -func zeroValue(f *Function, t types.Type) Value { - switch t.Underlying().(type) { - case *types.Struct, *types.Array: - return emitLoad(f, f.addLocal(t, token.NoPos)) - default: - return zeroConst(t) - } -} - -// createRecoverBlock emits to f a block of code to return after a -// recovered panic, and sets f.Recover to it. -// -// If f's result parameters are named, the code loads and returns -// their current values, otherwise it returns the zero values of their -// type. -// -// Idempotent. -// -func createRecoverBlock(f *Function) { - if f.Recover != nil { - return // already created - } - saved := f.currentBlock - - f.Recover = f.newBasicBlock("recover") - f.currentBlock = f.Recover - - var results []Value - if f.namedResults != nil { - // Reload NRPs to form value tuple. - for _, r := range f.namedResults { - results = append(results, emitLoad(f, r)) - } - } else { - R := f.Signature.Results() - for i, n := 0, R.Len(); i < n; i++ { - T := R.At(i).Type() - - // Return zero value of each result type. - results = append(results, zeroValue(f, T)) - } - } - f.emit(&Return{Results: results}) - - f.currentBlock = saved -} diff --git a/vendor/golang.org/x/tools/go/ssa/func.go b/vendor/golang.org/x/tools/go/ssa/func.go deleted file mode 100644 index 0b99bc9ba16..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/func.go +++ /dev/null @@ -1,691 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file implements the Function and BasicBlock types. - -import ( - "bytes" - "fmt" - "go/ast" - "go/token" - "go/types" - "io" - "os" - "strings" -) - -// addEdge adds a control-flow graph edge from from to to. -func addEdge(from, to *BasicBlock) { - from.Succs = append(from.Succs, to) - to.Preds = append(to.Preds, from) -} - -// Parent returns the function that contains block b. -func (b *BasicBlock) Parent() *Function { return b.parent } - -// String returns a human-readable label of this block. -// It is not guaranteed unique within the function. -// -func (b *BasicBlock) String() string { - return fmt.Sprintf("%d", b.Index) -} - -// emit appends an instruction to the current basic block. -// If the instruction defines a Value, it is returned. -// -func (b *BasicBlock) emit(i Instruction) Value { - i.setBlock(b) - b.Instrs = append(b.Instrs, i) - v, _ := i.(Value) - return v -} - -// predIndex returns the i such that b.Preds[i] == c or panics if -// there is none. -func (b *BasicBlock) predIndex(c *BasicBlock) int { - for i, pred := range b.Preds { - if pred == c { - return i - } - } - panic(fmt.Sprintf("no edge %s -> %s", c, b)) -} - -// hasPhi returns true if b.Instrs contains φ-nodes. -func (b *BasicBlock) hasPhi() bool { - _, ok := b.Instrs[0].(*Phi) - return ok -} - -// phis returns the prefix of b.Instrs containing all the block's φ-nodes. -func (b *BasicBlock) phis() []Instruction { - for i, instr := range b.Instrs { - if _, ok := instr.(*Phi); !ok { - return b.Instrs[:i] - } - } - return nil // unreachable in well-formed blocks -} - -// replacePred replaces all occurrences of p in b's predecessor list with q. -// Ordinarily there should be at most one. -// -func (b *BasicBlock) replacePred(p, q *BasicBlock) { - for i, pred := range b.Preds { - if pred == p { - b.Preds[i] = q - } - } -} - -// replaceSucc replaces all occurrences of p in b's successor list with q. -// Ordinarily there should be at most one. -// -func (b *BasicBlock) replaceSucc(p, q *BasicBlock) { - for i, succ := range b.Succs { - if succ == p { - b.Succs[i] = q - } - } -} - -// removePred removes all occurrences of p in b's -// predecessor list and φ-nodes. -// Ordinarily there should be at most one. -// -func (b *BasicBlock) removePred(p *BasicBlock) { - phis := b.phis() - - // We must preserve edge order for φ-nodes. - j := 0 - for i, pred := range b.Preds { - if pred != p { - b.Preds[j] = b.Preds[i] - // Strike out φ-edge too. - for _, instr := range phis { - phi := instr.(*Phi) - phi.Edges[j] = phi.Edges[i] - } - j++ - } - } - // Nil out b.Preds[j:] and φ-edges[j:] to aid GC. - for i := j; i < len(b.Preds); i++ { - b.Preds[i] = nil - for _, instr := range phis { - instr.(*Phi).Edges[i] = nil - } - } - b.Preds = b.Preds[:j] - for _, instr := range phis { - phi := instr.(*Phi) - phi.Edges = phi.Edges[:j] - } -} - -// Destinations associated with unlabelled for/switch/select stmts. -// We push/pop one of these as we enter/leave each construct and for -// each BranchStmt we scan for the innermost target of the right type. -// -type targets struct { - tail *targets // rest of stack - _break *BasicBlock - _continue *BasicBlock - _fallthrough *BasicBlock -} - -// Destinations associated with a labelled block. -// We populate these as labels are encountered in forward gotos or -// labelled statements. -// -type lblock struct { - _goto *BasicBlock - _break *BasicBlock - _continue *BasicBlock -} - -// labelledBlock returns the branch target associated with the -// specified label, creating it if needed. -// -func (f *Function) labelledBlock(label *ast.Ident) *lblock { - lb := f.lblocks[label.Obj] - if lb == nil { - lb = &lblock{_goto: f.newBasicBlock(label.Name)} - if f.lblocks == nil { - f.lblocks = make(map[*ast.Object]*lblock) - } - f.lblocks[label.Obj] = lb - } - return lb -} - -// addParam adds a (non-escaping) parameter to f.Params of the -// specified name, type and source position. -// -func (f *Function) addParam(name string, typ types.Type, pos token.Pos) *Parameter { - v := &Parameter{ - name: name, - typ: typ, - pos: pos, - parent: f, - } - f.Params = append(f.Params, v) - return v -} - -func (f *Function) addParamObj(obj types.Object) *Parameter { - name := obj.Name() - if name == "" { - name = fmt.Sprintf("arg%d", len(f.Params)) - } - param := f.addParam(name, obj.Type(), obj.Pos()) - param.object = obj - return param -} - -// addSpilledParam declares a parameter that is pre-spilled to the -// stack; the function body will load/store the spilled location. -// Subsequent lifting will eliminate spills where possible. -// -func (f *Function) addSpilledParam(obj types.Object) { - param := f.addParamObj(obj) - spill := &Alloc{Comment: obj.Name()} - spill.setType(types.NewPointer(obj.Type())) - spill.setPos(obj.Pos()) - f.objects[obj] = spill - f.Locals = append(f.Locals, spill) - f.emit(spill) - f.emit(&Store{Addr: spill, Val: param}) -} - -// startBody initializes the function prior to generating SSA code for its body. -// Precondition: f.Type() already set. -// -func (f *Function) startBody() { - f.currentBlock = f.newBasicBlock("entry") - f.objects = make(map[types.Object]Value) // needed for some synthetics, e.g. init -} - -// createSyntacticParams populates f.Params and generates code (spills -// and named result locals) for all the parameters declared in the -// syntax. In addition it populates the f.objects mapping. -// -// Preconditions: -// f.startBody() was called. -// Postcondition: -// len(f.Params) == len(f.Signature.Params) + (f.Signature.Recv() ? 1 : 0) -// -func (f *Function) createSyntacticParams(recv *ast.FieldList, functype *ast.FuncType) { - // Receiver (at most one inner iteration). - if recv != nil { - for _, field := range recv.List { - for _, n := range field.Names { - f.addSpilledParam(f.Pkg.info.Defs[n]) - } - // Anonymous receiver? No need to spill. - if field.Names == nil { - f.addParamObj(f.Signature.Recv()) - } - } - } - - // Parameters. - if functype.Params != nil { - n := len(f.Params) // 1 if has recv, 0 otherwise - for _, field := range functype.Params.List { - for _, n := range field.Names { - f.addSpilledParam(f.Pkg.info.Defs[n]) - } - // Anonymous parameter? No need to spill. - if field.Names == nil { - f.addParamObj(f.Signature.Params().At(len(f.Params) - n)) - } - } - } - - // Named results. - if functype.Results != nil { - for _, field := range functype.Results.List { - // Implicit "var" decl of locals for named results. - for _, n := range field.Names { - f.namedResults = append(f.namedResults, f.addLocalForIdent(n)) - } - } - } -} - -type setNumable interface { - setNum(int) -} - -// numberRegisters assigns numbers to all SSA registers -// (value-defining Instructions) in f, to aid debugging. -// (Non-Instruction Values are named at construction.) -// -func numberRegisters(f *Function) { - v := 0 - for _, b := range f.Blocks { - for _, instr := range b.Instrs { - switch instr.(type) { - case Value: - instr.(setNumable).setNum(v) - v++ - } - } - } -} - -// buildReferrers populates the def/use information in all non-nil -// Value.Referrers slice. -// Precondition: all such slices are initially empty. -func buildReferrers(f *Function) { - var rands []*Value - for _, b := range f.Blocks { - for _, instr := range b.Instrs { - rands = instr.Operands(rands[:0]) // recycle storage - for _, rand := range rands { - if r := *rand; r != nil { - if ref := r.Referrers(); ref != nil { - *ref = append(*ref, instr) - } - } - } - } - } -} - -// finishBody() finalizes the function after SSA code generation of its body. -func (f *Function) finishBody() { - f.objects = nil - f.currentBlock = nil - f.lblocks = nil - - // Don't pin the AST in memory (except in debug mode). - if n := f.syntax; n != nil && !f.debugInfo() { - f.syntax = extentNode{n.Pos(), n.End()} - } - - // Remove from f.Locals any Allocs that escape to the heap. - j := 0 - for _, l := range f.Locals { - if !l.Heap { - f.Locals[j] = l - j++ - } - } - // Nil out f.Locals[j:] to aid GC. - for i := j; i < len(f.Locals); i++ { - f.Locals[i] = nil - } - f.Locals = f.Locals[:j] - - optimizeBlocks(f) - - buildReferrers(f) - - buildDomTree(f) - - if f.Prog.mode&NaiveForm == 0 { - // For debugging pre-state of lifting pass: - // numberRegisters(f) - // f.WriteTo(os.Stderr) - lift(f) - } - - f.namedResults = nil // (used by lifting) - - numberRegisters(f) - - if f.Prog.mode&PrintFunctions != 0 { - printMu.Lock() - f.WriteTo(os.Stdout) - printMu.Unlock() - } - - if f.Prog.mode&SanityCheckFunctions != 0 { - mustSanityCheck(f, nil) - } -} - -// removeNilBlocks eliminates nils from f.Blocks and updates each -// BasicBlock.Index. Use this after any pass that may delete blocks. -// -func (f *Function) removeNilBlocks() { - j := 0 - for _, b := range f.Blocks { - if b != nil { - b.Index = j - f.Blocks[j] = b - j++ - } - } - // Nil out f.Blocks[j:] to aid GC. - for i := j; i < len(f.Blocks); i++ { - f.Blocks[i] = nil - } - f.Blocks = f.Blocks[:j] -} - -// SetDebugMode sets the debug mode for package pkg. If true, all its -// functions will include full debug info. This greatly increases the -// size of the instruction stream, and causes Functions to depend upon -// the ASTs, potentially keeping them live in memory for longer. -// -func (pkg *Package) SetDebugMode(debug bool) { - // TODO(adonovan): do we want ast.File granularity? - pkg.debug = debug -} - -// debugInfo reports whether debug info is wanted for this function. -func (f *Function) debugInfo() bool { - return f.Pkg != nil && f.Pkg.debug -} - -// addNamedLocal creates a local variable, adds it to function f and -// returns it. Its name and type are taken from obj. Subsequent -// calls to f.lookup(obj) will return the same local. -// -func (f *Function) addNamedLocal(obj types.Object) *Alloc { - l := f.addLocal(obj.Type(), obj.Pos()) - l.Comment = obj.Name() - f.objects[obj] = l - return l -} - -func (f *Function) addLocalForIdent(id *ast.Ident) *Alloc { - return f.addNamedLocal(f.Pkg.info.Defs[id]) -} - -// addLocal creates an anonymous local variable of type typ, adds it -// to function f and returns it. pos is the optional source location. -// -func (f *Function) addLocal(typ types.Type, pos token.Pos) *Alloc { - v := &Alloc{} - v.setType(types.NewPointer(typ)) - v.setPos(pos) - f.Locals = append(f.Locals, v) - f.emit(v) - return v -} - -// lookup returns the address of the named variable identified by obj -// that is local to function f or one of its enclosing functions. -// If escaping, the reference comes from a potentially escaping pointer -// expression and the referent must be heap-allocated. -// -func (f *Function) lookup(obj types.Object, escaping bool) Value { - if v, ok := f.objects[obj]; ok { - if alloc, ok := v.(*Alloc); ok && escaping { - alloc.Heap = true - } - return v // function-local var (address) - } - - // Definition must be in an enclosing function; - // plumb it through intervening closures. - if f.parent == nil { - panic("no ssa.Value for " + obj.String()) - } - outer := f.parent.lookup(obj, true) // escaping - v := &FreeVar{ - name: obj.Name(), - typ: outer.Type(), - pos: outer.Pos(), - outer: outer, - parent: f, - } - f.objects[obj] = v - f.FreeVars = append(f.FreeVars, v) - return v -} - -// emit emits the specified instruction to function f. -func (f *Function) emit(instr Instruction) Value { - return f.currentBlock.emit(instr) -} - -// RelString returns the full name of this function, qualified by -// package name, receiver type, etc. -// -// The specific formatting rules are not guaranteed and may change. -// -// Examples: -// "math.IsNaN" // a package-level function -// "(*bytes.Buffer).Bytes" // a declared method or a wrapper -// "(*bytes.Buffer).Bytes$thunk" // thunk (func wrapping method; receiver is param 0) -// "(*bytes.Buffer).Bytes$bound" // bound (func wrapping method; receiver supplied by closure) -// "main.main$1" // an anonymous function in main -// "main.init#1" // a declared init function -// "main.init" // the synthesized package initializer -// -// When these functions are referred to from within the same package -// (i.e. from == f.Pkg.Object), they are rendered without the package path. -// For example: "IsNaN", "(*Buffer).Bytes", etc. -// -// All non-synthetic functions have distinct package-qualified names. -// (But two methods may have the same name "(T).f" if one is a synthetic -// wrapper promoting a non-exported method "f" from another package; in -// that case, the strings are equal but the identifiers "f" are distinct.) -// -func (f *Function) RelString(from *types.Package) string { - // Anonymous? - if f.parent != nil { - // An anonymous function's Name() looks like "parentName$1", - // but its String() should include the type/package/etc. - parent := f.parent.RelString(from) - for i, anon := range f.parent.AnonFuncs { - if anon == f { - return fmt.Sprintf("%s$%d", parent, 1+i) - } - } - - return f.name // should never happen - } - - // Method (declared or wrapper)? - if recv := f.Signature.Recv(); recv != nil { - return f.relMethod(from, recv.Type()) - } - - // Thunk? - if f.method != nil { - return f.relMethod(from, f.method.Recv()) - } - - // Bound? - if len(f.FreeVars) == 1 && strings.HasSuffix(f.name, "$bound") { - return f.relMethod(from, f.FreeVars[0].Type()) - } - - // Package-level function? - // Prefix with package name for cross-package references only. - if p := f.pkg(); p != nil && p != from { - return fmt.Sprintf("%s.%s", p.Path(), f.name) - } - - // Unknown. - return f.name -} - -func (f *Function) relMethod(from *types.Package, recv types.Type) string { - return fmt.Sprintf("(%s).%s", relType(recv, from), f.name) -} - -// writeSignature writes to buf the signature sig in declaration syntax. -func writeSignature(buf *bytes.Buffer, from *types.Package, name string, sig *types.Signature, params []*Parameter) { - buf.WriteString("func ") - if recv := sig.Recv(); recv != nil { - buf.WriteString("(") - if n := params[0].Name(); n != "" { - buf.WriteString(n) - buf.WriteString(" ") - } - types.WriteType(buf, params[0].Type(), types.RelativeTo(from)) - buf.WriteString(") ") - } - buf.WriteString(name) - types.WriteSignature(buf, sig, types.RelativeTo(from)) -} - -func (f *Function) pkg() *types.Package { - if f.Pkg != nil { - return f.Pkg.Pkg - } - return nil -} - -var _ io.WriterTo = (*Function)(nil) // *Function implements io.Writer - -func (f *Function) WriteTo(w io.Writer) (int64, error) { - var buf bytes.Buffer - WriteFunction(&buf, f) - n, err := w.Write(buf.Bytes()) - return int64(n), err -} - -// WriteFunction writes to buf a human-readable "disassembly" of f. -func WriteFunction(buf *bytes.Buffer, f *Function) { - fmt.Fprintf(buf, "# Name: %s\n", f.String()) - if f.Pkg != nil { - fmt.Fprintf(buf, "# Package: %s\n", f.Pkg.Pkg.Path()) - } - if syn := f.Synthetic; syn != "" { - fmt.Fprintln(buf, "# Synthetic:", syn) - } - if pos := f.Pos(); pos.IsValid() { - fmt.Fprintf(buf, "# Location: %s\n", f.Prog.Fset.Position(pos)) - } - - if f.parent != nil { - fmt.Fprintf(buf, "# Parent: %s\n", f.parent.Name()) - } - - if f.Recover != nil { - fmt.Fprintf(buf, "# Recover: %s\n", f.Recover) - } - - from := f.pkg() - - if f.FreeVars != nil { - buf.WriteString("# Free variables:\n") - for i, fv := range f.FreeVars { - fmt.Fprintf(buf, "# % 3d:\t%s %s\n", i, fv.Name(), relType(fv.Type(), from)) - } - } - - if len(f.Locals) > 0 { - buf.WriteString("# Locals:\n") - for i, l := range f.Locals { - fmt.Fprintf(buf, "# % 3d:\t%s %s\n", i, l.Name(), relType(deref(l.Type()), from)) - } - } - writeSignature(buf, from, f.Name(), f.Signature, f.Params) - buf.WriteString(":\n") - - if f.Blocks == nil { - buf.WriteString("\t(external)\n") - } - - // NB. column calculations are confused by non-ASCII - // characters and assume 8-space tabs. - const punchcard = 80 // for old time's sake. - const tabwidth = 8 - for _, b := range f.Blocks { - if b == nil { - // Corrupt CFG. - fmt.Fprintf(buf, ".nil:\n") - continue - } - n, _ := fmt.Fprintf(buf, "%d:", b.Index) - bmsg := fmt.Sprintf("%s P:%d S:%d", b.Comment, len(b.Preds), len(b.Succs)) - fmt.Fprintf(buf, "%*s%s\n", punchcard-1-n-len(bmsg), "", bmsg) - - if false { // CFG debugging - fmt.Fprintf(buf, "\t# CFG: %s --> %s --> %s\n", b.Preds, b, b.Succs) - } - for _, instr := range b.Instrs { - buf.WriteString("\t") - switch v := instr.(type) { - case Value: - l := punchcard - tabwidth - // Left-align the instruction. - if name := v.Name(); name != "" { - n, _ := fmt.Fprintf(buf, "%s = ", name) - l -= n - } - n, _ := buf.WriteString(instr.String()) - l -= n - // Right-align the type if there's space. - if t := v.Type(); t != nil { - buf.WriteByte(' ') - ts := relType(t, from) - l -= len(ts) + len(" ") // (spaces before and after type) - if l > 0 { - fmt.Fprintf(buf, "%*s", l, "") - } - buf.WriteString(ts) - } - case nil: - // Be robust against bad transforms. - buf.WriteString("") - default: - buf.WriteString(instr.String()) - } - buf.WriteString("\n") - } - } - fmt.Fprintf(buf, "\n") -} - -// newBasicBlock adds to f a new basic block and returns it. It does -// not automatically become the current block for subsequent calls to emit. -// comment is an optional string for more readable debugging output. -// -func (f *Function) newBasicBlock(comment string) *BasicBlock { - b := &BasicBlock{ - Index: len(f.Blocks), - Comment: comment, - parent: f, - } - b.Succs = b.succs2[:0] - f.Blocks = append(f.Blocks, b) - return b -} - -// NewFunction returns a new synthetic Function instance belonging to -// prog, with its name and signature fields set as specified. -// -// The caller is responsible for initializing the remaining fields of -// the function object, e.g. Pkg, Params, Blocks. -// -// It is practically impossible for clients to construct well-formed -// SSA functions/packages/programs directly, so we assume this is the -// job of the Builder alone. NewFunction exists to provide clients a -// little flexibility. For example, analysis tools may wish to -// construct fake Functions for the root of the callgraph, a fake -// "reflect" package, etc. -// -// TODO(adonovan): think harder about the API here. -// -func (prog *Program) NewFunction(name string, sig *types.Signature, provenance string) *Function { - return &Function{Prog: prog, name: name, Signature: sig, Synthetic: provenance} -} - -type extentNode [2]token.Pos - -func (n extentNode) Pos() token.Pos { return n[0] } -func (n extentNode) End() token.Pos { return n[1] } - -// Syntax returns an ast.Node whose Pos/End methods provide the -// lexical extent of the function if it was defined by Go source code -// (f.Synthetic==""), or nil otherwise. -// -// If f was built with debug information (see Package.SetDebugRef), -// the result is the *ast.FuncDecl or *ast.FuncLit that declared the -// function. Otherwise, it is an opaque Node providing only position -// information; this avoids pinning the AST in memory. -// -func (f *Function) Syntax() ast.Node { return f.syntax } diff --git a/vendor/golang.org/x/tools/go/ssa/identical.go b/vendor/golang.org/x/tools/go/ssa/identical.go deleted file mode 100644 index 53cbee107b6..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/identical.go +++ /dev/null @@ -1,7 +0,0 @@ -// +build go1.8 - -package ssa - -import "go/types" - -var structTypesIdentical = types.IdenticalIgnoreTags diff --git a/vendor/golang.org/x/tools/go/ssa/identical_17.go b/vendor/golang.org/x/tools/go/ssa/identical_17.go deleted file mode 100644 index da89d3339a5..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/identical_17.go +++ /dev/null @@ -1,7 +0,0 @@ -// +build !go1.8 - -package ssa - -import "go/types" - -var structTypesIdentical = types.Identical diff --git a/vendor/golang.org/x/tools/go/ssa/lift.go b/vendor/golang.org/x/tools/go/ssa/lift.go deleted file mode 100644 index 048e9b03260..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/lift.go +++ /dev/null @@ -1,653 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file defines the lifting pass which tries to "lift" Alloc -// cells (new/local variables) into SSA registers, replacing loads -// with the dominating stored value, eliminating loads and stores, and -// inserting φ-nodes as needed. - -// Cited papers and resources: -// -// Ron Cytron et al. 1991. Efficiently computing SSA form... -// http://doi.acm.org/10.1145/115372.115320 -// -// Cooper, Harvey, Kennedy. 2001. A Simple, Fast Dominance Algorithm. -// Software Practice and Experience 2001, 4:1-10. -// http://www.hipersoft.rice.edu/grads/publications/dom14.pdf -// -// Daniel Berlin, llvmdev mailing list, 2012. -// http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-January/046638.html -// (Be sure to expand the whole thread.) - -// TODO(adonovan): opt: there are many optimizations worth evaluating, and -// the conventional wisdom for SSA construction is that a simple -// algorithm well engineered often beats those of better asymptotic -// complexity on all but the most egregious inputs. -// -// Danny Berlin suggests that the Cooper et al. algorithm for -// computing the dominance frontier is superior to Cytron et al. -// Furthermore he recommends that rather than computing the DF for the -// whole function then renaming all alloc cells, it may be cheaper to -// compute the DF for each alloc cell separately and throw it away. -// -// Consider exploiting liveness information to avoid creating dead -// φ-nodes which we then immediately remove. -// -// Also see many other "TODO: opt" suggestions in the code. - -import ( - "fmt" - "go/token" - "go/types" - "math/big" - "os" -) - -// If true, show diagnostic information at each step of lifting. -// Very verbose. -const debugLifting = false - -// domFrontier maps each block to the set of blocks in its dominance -// frontier. The outer slice is conceptually a map keyed by -// Block.Index. The inner slice is conceptually a set, possibly -// containing duplicates. -// -// TODO(adonovan): opt: measure impact of dups; consider a packed bit -// representation, e.g. big.Int, and bitwise parallel operations for -// the union step in the Children loop. -// -// domFrontier's methods mutate the slice's elements but not its -// length, so their receivers needn't be pointers. -// -type domFrontier [][]*BasicBlock - -func (df domFrontier) add(u, v *BasicBlock) { - p := &df[u.Index] - *p = append(*p, v) -} - -// build builds the dominance frontier df for the dominator (sub)tree -// rooted at u, using the Cytron et al. algorithm. -// -// TODO(adonovan): opt: consider Berlin approach, computing pruned SSA -// by pruning the entire IDF computation, rather than merely pruning -// the DF -> IDF step. -func (df domFrontier) build(u *BasicBlock) { - // Encounter each node u in postorder of dom tree. - for _, child := range u.dom.children { - df.build(child) - } - for _, vb := range u.Succs { - if v := vb.dom; v.idom != u { - df.add(u, vb) - } - } - for _, w := range u.dom.children { - for _, vb := range df[w.Index] { - // TODO(adonovan): opt: use word-parallel bitwise union. - if v := vb.dom; v.idom != u { - df.add(u, vb) - } - } - } -} - -func buildDomFrontier(fn *Function) domFrontier { - df := make(domFrontier, len(fn.Blocks)) - df.build(fn.Blocks[0]) - if fn.Recover != nil { - df.build(fn.Recover) - } - return df -} - -func removeInstr(refs []Instruction, instr Instruction) []Instruction { - i := 0 - for _, ref := range refs { - if ref == instr { - continue - } - refs[i] = ref - i++ - } - for j := i; j != len(refs); j++ { - refs[j] = nil // aid GC - } - return refs[:i] -} - -// lift replaces local and new Allocs accessed only with -// load/store by SSA registers, inserting φ-nodes where necessary. -// The result is a program in classical pruned SSA form. -// -// Preconditions: -// - fn has no dead blocks (blockopt has run). -// - Def/use info (Operands and Referrers) is up-to-date. -// - The dominator tree is up-to-date. -// -func lift(fn *Function) { - // TODO(adonovan): opt: lots of little optimizations may be - // worthwhile here, especially if they cause us to avoid - // buildDomFrontier. For example: - // - // - Alloc never loaded? Eliminate. - // - Alloc never stored? Replace all loads with a zero constant. - // - Alloc stored once? Replace loads with dominating store; - // don't forget that an Alloc is itself an effective store - // of zero. - // - Alloc used only within a single block? - // Use degenerate algorithm avoiding φ-nodes. - // - Consider synergy with scalar replacement of aggregates (SRA). - // e.g. *(&x.f) where x is an Alloc. - // Perhaps we'd get better results if we generated this as x.f - // i.e. Field(x, .f) instead of Load(FieldIndex(x, .f)). - // Unclear. - // - // But we will start with the simplest correct code. - df := buildDomFrontier(fn) - - if debugLifting { - title := false - for i, blocks := range df { - if blocks != nil { - if !title { - fmt.Fprintf(os.Stderr, "Dominance frontier of %s:\n", fn) - title = true - } - fmt.Fprintf(os.Stderr, "\t%s: %s\n", fn.Blocks[i], blocks) - } - } - } - - newPhis := make(newPhiMap) - - // During this pass we will replace some BasicBlock.Instrs - // (allocs, loads and stores) with nil, keeping a count in - // BasicBlock.gaps. At the end we will reset Instrs to the - // concatenation of all non-dead newPhis and non-nil Instrs - // for the block, reusing the original array if space permits. - - // While we're here, we also eliminate 'rundefers' - // instructions in functions that contain no 'defer' - // instructions. - usesDefer := false - - // A counter used to generate ~unique ids for Phi nodes, as an - // aid to debugging. We use large numbers to make them highly - // visible. All nodes are renumbered later. - fresh := 1000 - - // Determine which allocs we can lift and number them densely. - // The renaming phase uses this numbering for compact maps. - numAllocs := 0 - for _, b := range fn.Blocks { - b.gaps = 0 - b.rundefers = 0 - for _, instr := range b.Instrs { - switch instr := instr.(type) { - case *Alloc: - index := -1 - if liftAlloc(df, instr, newPhis, &fresh) { - index = numAllocs - numAllocs++ - } - instr.index = index - case *Defer: - usesDefer = true - case *RunDefers: - b.rundefers++ - } - } - } - - // renaming maps an alloc (keyed by index) to its replacement - // value. Initially the renaming contains nil, signifying the - // zero constant of the appropriate type; we construct the - // Const lazily at most once on each path through the domtree. - // TODO(adonovan): opt: cache per-function not per subtree. - renaming := make([]Value, numAllocs) - - // Renaming. - rename(fn.Blocks[0], renaming, newPhis) - - // Eliminate dead φ-nodes. - removeDeadPhis(fn.Blocks, newPhis) - - // Prepend remaining live φ-nodes to each block. - for _, b := range fn.Blocks { - nps := newPhis[b] - j := len(nps) - - rundefersToKill := b.rundefers - if usesDefer { - rundefersToKill = 0 - } - - if j+b.gaps+rundefersToKill == 0 { - continue // fast path: no new phis or gaps - } - - // Compact nps + non-nil Instrs into a new slice. - // TODO(adonovan): opt: compact in situ (rightwards) - // if Instrs has sufficient space or slack. - dst := make([]Instruction, len(b.Instrs)+j-b.gaps-rundefersToKill) - for i, np := range nps { - dst[i] = np.phi - } - for _, instr := range b.Instrs { - if instr == nil { - continue - } - if !usesDefer { - if _, ok := instr.(*RunDefers); ok { - continue - } - } - dst[j] = instr - j++ - } - b.Instrs = dst - } - - // Remove any fn.Locals that were lifted. - j := 0 - for _, l := range fn.Locals { - if l.index < 0 { - fn.Locals[j] = l - j++ - } - } - // Nil out fn.Locals[j:] to aid GC. - for i := j; i < len(fn.Locals); i++ { - fn.Locals[i] = nil - } - fn.Locals = fn.Locals[:j] -} - -// removeDeadPhis removes φ-nodes not transitively needed by a -// non-Phi, non-DebugRef instruction. -func removeDeadPhis(blocks []*BasicBlock, newPhis newPhiMap) { - // First pass: find the set of "live" φ-nodes: those reachable - // from some non-Phi instruction. - // - // We compute reachability in reverse, starting from each φ, - // rather than forwards, starting from each live non-Phi - // instruction, because this way visits much less of the - // Value graph. - livePhis := make(map[*Phi]bool) - for _, npList := range newPhis { - for _, np := range npList { - phi := np.phi - if !livePhis[phi] && phiHasDirectReferrer(phi) { - markLivePhi(livePhis, phi) - } - } - } - - // Existing φ-nodes due to && and || operators - // are all considered live (see Go issue 19622). - for _, b := range blocks { - for _, phi := range b.phis() { - markLivePhi(livePhis, phi.(*Phi)) - } - } - - // Second pass: eliminate unused phis from newPhis. - for block, npList := range newPhis { - j := 0 - for _, np := range npList { - if livePhis[np.phi] { - npList[j] = np - j++ - } else { - // discard it, first removing it from referrers - for _, val := range np.phi.Edges { - if refs := val.Referrers(); refs != nil { - *refs = removeInstr(*refs, np.phi) - } - } - np.phi.block = nil - } - } - newPhis[block] = npList[:j] - } -} - -// markLivePhi marks phi, and all φ-nodes transitively reachable via -// its Operands, live. -func markLivePhi(livePhis map[*Phi]bool, phi *Phi) { - livePhis[phi] = true - for _, rand := range phi.Operands(nil) { - if q, ok := (*rand).(*Phi); ok { - if !livePhis[q] { - markLivePhi(livePhis, q) - } - } - } -} - -// phiHasDirectReferrer reports whether phi is directly referred to by -// a non-Phi instruction. Such instructions are the -// roots of the liveness traversal. -func phiHasDirectReferrer(phi *Phi) bool { - for _, instr := range *phi.Referrers() { - if _, ok := instr.(*Phi); !ok { - return true - } - } - return false -} - -type blockSet struct{ big.Int } // (inherit methods from Int) - -// add adds b to the set and returns true if the set changed. -func (s *blockSet) add(b *BasicBlock) bool { - i := b.Index - if s.Bit(i) != 0 { - return false - } - s.SetBit(&s.Int, i, 1) - return true -} - -// take removes an arbitrary element from a set s and -// returns its index, or returns -1 if empty. -func (s *blockSet) take() int { - l := s.BitLen() - for i := 0; i < l; i++ { - if s.Bit(i) == 1 { - s.SetBit(&s.Int, i, 0) - return i - } - } - return -1 -} - -// newPhi is a pair of a newly introduced φ-node and the lifted Alloc -// it replaces. -type newPhi struct { - phi *Phi - alloc *Alloc -} - -// newPhiMap records for each basic block, the set of newPhis that -// must be prepended to the block. -type newPhiMap map[*BasicBlock][]newPhi - -// liftAlloc determines whether alloc can be lifted into registers, -// and if so, it populates newPhis with all the φ-nodes it may require -// and returns true. -// -// fresh is a source of fresh ids for phi nodes. -// -func liftAlloc(df domFrontier, alloc *Alloc, newPhis newPhiMap, fresh *int) bool { - // Don't lift aggregates into registers, because we don't have - // a way to express their zero-constants. - switch deref(alloc.Type()).Underlying().(type) { - case *types.Array, *types.Struct: - return false - } - - // Don't lift named return values in functions that defer - // calls that may recover from panic. - if fn := alloc.Parent(); fn.Recover != nil { - for _, nr := range fn.namedResults { - if nr == alloc { - return false - } - } - } - - // Compute defblocks, the set of blocks containing a - // definition of the alloc cell. - var defblocks blockSet - for _, instr := range *alloc.Referrers() { - // Bail out if we discover the alloc is not liftable; - // the only operations permitted to use the alloc are - // loads/stores into the cell, and DebugRef. - switch instr := instr.(type) { - case *Store: - if instr.Val == alloc { - return false // address used as value - } - if instr.Addr != alloc { - panic("Alloc.Referrers is inconsistent") - } - defblocks.add(instr.Block()) - case *UnOp: - if instr.Op != token.MUL { - return false // not a load - } - if instr.X != alloc { - panic("Alloc.Referrers is inconsistent") - } - case *DebugRef: - // ok - default: - return false // some other instruction - } - } - // The Alloc itself counts as a (zero) definition of the cell. - defblocks.add(alloc.Block()) - - if debugLifting { - fmt.Fprintln(os.Stderr, "\tlifting ", alloc, alloc.Name()) - } - - fn := alloc.Parent() - - // Φ-insertion. - // - // What follows is the body of the main loop of the insert-φ - // function described by Cytron et al, but instead of using - // counter tricks, we just reset the 'hasAlready' and 'work' - // sets each iteration. These are bitmaps so it's pretty cheap. - // - // TODO(adonovan): opt: recycle slice storage for W, - // hasAlready, defBlocks across liftAlloc calls. - var hasAlready blockSet - - // Initialize W and work to defblocks. - var work blockSet = defblocks // blocks seen - var W blockSet // blocks to do - W.Set(&defblocks.Int) - - // Traverse iterated dominance frontier, inserting φ-nodes. - for i := W.take(); i != -1; i = W.take() { - u := fn.Blocks[i] - for _, v := range df[u.Index] { - if hasAlready.add(v) { - // Create φ-node. - // It will be prepended to v.Instrs later, if needed. - phi := &Phi{ - Edges: make([]Value, len(v.Preds)), - Comment: alloc.Comment, - } - // This is merely a debugging aid: - phi.setNum(*fresh) - *fresh++ - - phi.pos = alloc.Pos() - phi.setType(deref(alloc.Type())) - phi.block = v - if debugLifting { - fmt.Fprintf(os.Stderr, "\tplace %s = %s at block %s\n", phi.Name(), phi, v) - } - newPhis[v] = append(newPhis[v], newPhi{phi, alloc}) - - if work.add(v) { - W.add(v) - } - } - } - } - - return true -} - -// replaceAll replaces all intraprocedural uses of x with y, -// updating x.Referrers and y.Referrers. -// Precondition: x.Referrers() != nil, i.e. x must be local to some function. -// -func replaceAll(x, y Value) { - var rands []*Value - pxrefs := x.Referrers() - pyrefs := y.Referrers() - for _, instr := range *pxrefs { - rands = instr.Operands(rands[:0]) // recycle storage - for _, rand := range rands { - if *rand != nil { - if *rand == x { - *rand = y - } - } - } - if pyrefs != nil { - *pyrefs = append(*pyrefs, instr) // dups ok - } - } - *pxrefs = nil // x is now unreferenced -} - -// renamed returns the value to which alloc is being renamed, -// constructing it lazily if it's the implicit zero initialization. -// -func renamed(renaming []Value, alloc *Alloc) Value { - v := renaming[alloc.index] - if v == nil { - v = zeroConst(deref(alloc.Type())) - renaming[alloc.index] = v - } - return v -} - -// rename implements the (Cytron et al) SSA renaming algorithm, a -// preorder traversal of the dominator tree replacing all loads of -// Alloc cells with the value stored to that cell by the dominating -// store instruction. For lifting, we need only consider loads, -// stores and φ-nodes. -// -// renaming is a map from *Alloc (keyed by index number) to its -// dominating stored value; newPhis[x] is the set of new φ-nodes to be -// prepended to block x. -// -func rename(u *BasicBlock, renaming []Value, newPhis newPhiMap) { - // Each φ-node becomes the new name for its associated Alloc. - for _, np := range newPhis[u] { - phi := np.phi - alloc := np.alloc - renaming[alloc.index] = phi - } - - // Rename loads and stores of allocs. - for i, instr := range u.Instrs { - switch instr := instr.(type) { - case *Alloc: - if instr.index >= 0 { // store of zero to Alloc cell - // Replace dominated loads by the zero value. - renaming[instr.index] = nil - if debugLifting { - fmt.Fprintf(os.Stderr, "\tkill alloc %s\n", instr) - } - // Delete the Alloc. - u.Instrs[i] = nil - u.gaps++ - } - - case *Store: - if alloc, ok := instr.Addr.(*Alloc); ok && alloc.index >= 0 { // store to Alloc cell - // Replace dominated loads by the stored value. - renaming[alloc.index] = instr.Val - if debugLifting { - fmt.Fprintf(os.Stderr, "\tkill store %s; new value: %s\n", - instr, instr.Val.Name()) - } - // Remove the store from the referrer list of the stored value. - if refs := instr.Val.Referrers(); refs != nil { - *refs = removeInstr(*refs, instr) - } - // Delete the Store. - u.Instrs[i] = nil - u.gaps++ - } - - case *UnOp: - if instr.Op == token.MUL { - if alloc, ok := instr.X.(*Alloc); ok && alloc.index >= 0 { // load of Alloc cell - newval := renamed(renaming, alloc) - if debugLifting { - fmt.Fprintf(os.Stderr, "\tupdate load %s = %s with %s\n", - instr.Name(), instr, newval.Name()) - } - // Replace all references to - // the loaded value by the - // dominating stored value. - replaceAll(instr, newval) - // Delete the Load. - u.Instrs[i] = nil - u.gaps++ - } - } - - case *DebugRef: - if alloc, ok := instr.X.(*Alloc); ok && alloc.index >= 0 { // ref of Alloc cell - if instr.IsAddr { - instr.X = renamed(renaming, alloc) - instr.IsAddr = false - - // Add DebugRef to instr.X's referrers. - if refs := instr.X.Referrers(); refs != nil { - *refs = append(*refs, instr) - } - } else { - // A source expression denotes the address - // of an Alloc that was optimized away. - instr.X = nil - - // Delete the DebugRef. - u.Instrs[i] = nil - u.gaps++ - } - } - } - } - - // For each φ-node in a CFG successor, rename the edge. - for _, v := range u.Succs { - phis := newPhis[v] - if len(phis) == 0 { - continue - } - i := v.predIndex(u) - for _, np := range phis { - phi := np.phi - alloc := np.alloc - newval := renamed(renaming, alloc) - if debugLifting { - fmt.Fprintf(os.Stderr, "\tsetphi %s edge %s -> %s (#%d) (alloc=%s) := %s\n", - phi.Name(), u, v, i, alloc.Name(), newval.Name()) - } - phi.Edges[i] = newval - if prefs := newval.Referrers(); prefs != nil { - *prefs = append(*prefs, phi) - } - } - } - - // Continue depth-first recursion over domtree, pushing a - // fresh copy of the renaming map for each subtree. - for i, v := range u.dom.children { - r := renaming - if i < len(u.dom.children)-1 { - // On all but the final iteration, we must make - // a copy to avoid destructive update. - r = make([]Value, len(renaming)) - copy(r, renaming) - } - rename(v, r, newPhis) - } - -} diff --git a/vendor/golang.org/x/tools/go/ssa/lvalue.go b/vendor/golang.org/x/tools/go/ssa/lvalue.go deleted file mode 100644 index 4d85be3ec78..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/lvalue.go +++ /dev/null @@ -1,120 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// lvalues are the union of addressable expressions and map-index -// expressions. - -import ( - "go/ast" - "go/token" - "go/types" -) - -// An lvalue represents an assignable location that may appear on the -// left-hand side of an assignment. This is a generalization of a -// pointer to permit updates to elements of maps. -// -type lvalue interface { - store(fn *Function, v Value) // stores v into the location - load(fn *Function) Value // loads the contents of the location - address(fn *Function) Value // address of the location - typ() types.Type // returns the type of the location -} - -// An address is an lvalue represented by a true pointer. -type address struct { - addr Value - pos token.Pos // source position - expr ast.Expr // source syntax of the value (not address) [debug mode] -} - -func (a *address) load(fn *Function) Value { - load := emitLoad(fn, a.addr) - load.pos = a.pos - return load -} - -func (a *address) store(fn *Function, v Value) { - store := emitStore(fn, a.addr, v, a.pos) - if a.expr != nil { - // store.Val is v, converted for assignability. - emitDebugRef(fn, a.expr, store.Val, false) - } -} - -func (a *address) address(fn *Function) Value { - if a.expr != nil { - emitDebugRef(fn, a.expr, a.addr, true) - } - return a.addr -} - -func (a *address) typ() types.Type { - return deref(a.addr.Type()) -} - -// An element is an lvalue represented by m[k], the location of an -// element of a map or string. These locations are not addressable -// since pointers cannot be formed from them, but they do support -// load(), and in the case of maps, store(). -// -type element struct { - m, k Value // map or string - t types.Type // map element type or string byte type - pos token.Pos // source position of colon ({k:v}) or lbrack (m[k]=v) -} - -func (e *element) load(fn *Function) Value { - l := &Lookup{ - X: e.m, - Index: e.k, - } - l.setPos(e.pos) - l.setType(e.t) - return fn.emit(l) -} - -func (e *element) store(fn *Function, v Value) { - up := &MapUpdate{ - Map: e.m, - Key: e.k, - Value: emitConv(fn, v, e.t), - } - up.pos = e.pos - fn.emit(up) -} - -func (e *element) address(fn *Function) Value { - panic("map/string elements are not addressable") -} - -func (e *element) typ() types.Type { - return e.t -} - -// A blank is a dummy variable whose name is "_". -// It is not reified: loads are illegal and stores are ignored. -// -type blank struct{} - -func (bl blank) load(fn *Function) Value { - panic("blank.load is illegal") -} - -func (bl blank) store(fn *Function, v Value) { - // no-op -} - -func (bl blank) address(fn *Function) Value { - panic("blank var is not addressable") -} - -func (bl blank) typ() types.Type { - // This should be the type of the blank Ident; the typechecker - // doesn't provide this yet, but fortunately, we don't need it - // yet either. - panic("blank.typ is unimplemented") -} diff --git a/vendor/golang.org/x/tools/go/ssa/methods.go b/vendor/golang.org/x/tools/go/ssa/methods.go deleted file mode 100644 index 9cf383916bb..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/methods.go +++ /dev/null @@ -1,239 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file defines utilities for population of method sets. - -import ( - "fmt" - "go/types" -) - -// MethodValue returns the Function implementing method sel, building -// wrapper methods on demand. It returns nil if sel denotes an -// abstract (interface) method. -// -// Precondition: sel.Kind() == MethodVal. -// -// Thread-safe. -// -// EXCLUSIVE_LOCKS_ACQUIRED(prog.methodsMu) -// -func (prog *Program) MethodValue(sel *types.Selection) *Function { - if sel.Kind() != types.MethodVal { - panic(fmt.Sprintf("MethodValue(%s) kind != MethodVal", sel)) - } - T := sel.Recv() - if isInterface(T) { - return nil // abstract method - } - if prog.mode&LogSource != 0 { - defer logStack("MethodValue %s %v", T, sel)() - } - - prog.methodsMu.Lock() - defer prog.methodsMu.Unlock() - - return prog.addMethod(prog.createMethodSet(T), sel) -} - -// LookupMethod returns the implementation of the method of type T -// identified by (pkg, name). It returns nil if the method exists but -// is abstract, and panics if T has no such method. -// -func (prog *Program) LookupMethod(T types.Type, pkg *types.Package, name string) *Function { - sel := prog.MethodSets.MethodSet(T).Lookup(pkg, name) - if sel == nil { - panic(fmt.Sprintf("%s has no method %s", T, types.Id(pkg, name))) - } - return prog.MethodValue(sel) -} - -// methodSet contains the (concrete) methods of a non-interface type. -type methodSet struct { - mapping map[string]*Function // populated lazily - complete bool // mapping contains all methods -} - -// Precondition: !isInterface(T). -// EXCLUSIVE_LOCKS_REQUIRED(prog.methodsMu) -func (prog *Program) createMethodSet(T types.Type) *methodSet { - mset, ok := prog.methodSets.At(T).(*methodSet) - if !ok { - mset = &methodSet{mapping: make(map[string]*Function)} - prog.methodSets.Set(T, mset) - } - return mset -} - -// EXCLUSIVE_LOCKS_REQUIRED(prog.methodsMu) -func (prog *Program) addMethod(mset *methodSet, sel *types.Selection) *Function { - if sel.Kind() == types.MethodExpr { - panic(sel) - } - id := sel.Obj().Id() - fn := mset.mapping[id] - if fn == nil { - obj := sel.Obj().(*types.Func) - - needsPromotion := len(sel.Index()) > 1 - needsIndirection := !isPointer(recvType(obj)) && isPointer(sel.Recv()) - if needsPromotion || needsIndirection { - fn = makeWrapper(prog, sel) - } else { - fn = prog.declaredFunc(obj) - } - if fn.Signature.Recv() == nil { - panic(fn) // missing receiver - } - mset.mapping[id] = fn - } - return fn -} - -// RuntimeTypes returns a new unordered slice containing all -// concrete types in the program for which a complete (non-empty) -// method set is required at run-time. -// -// Thread-safe. -// -// EXCLUSIVE_LOCKS_ACQUIRED(prog.methodsMu) -// -func (prog *Program) RuntimeTypes() []types.Type { - prog.methodsMu.Lock() - defer prog.methodsMu.Unlock() - - var res []types.Type - prog.methodSets.Iterate(func(T types.Type, v interface{}) { - if v.(*methodSet).complete { - res = append(res, T) - } - }) - return res -} - -// declaredFunc returns the concrete function/method denoted by obj. -// Panic ensues if there is none. -// -func (prog *Program) declaredFunc(obj *types.Func) *Function { - if v := prog.packageLevelValue(obj); v != nil { - return v.(*Function) - } - panic("no concrete method: " + obj.String()) -} - -// needMethodsOf ensures that runtime type information (including the -// complete method set) is available for the specified type T and all -// its subcomponents. -// -// needMethodsOf must be called for at least every type that is an -// operand of some MakeInterface instruction, and for the type of -// every exported package member. -// -// Precondition: T is not a method signature (*Signature with Recv()!=nil). -// -// Thread-safe. (Called via emitConv from multiple builder goroutines.) -// -// TODO(adonovan): make this faster. It accounts for 20% of SSA build time. -// -// EXCLUSIVE_LOCKS_ACQUIRED(prog.methodsMu) -// -func (prog *Program) needMethodsOf(T types.Type) { - prog.methodsMu.Lock() - prog.needMethods(T, false) - prog.methodsMu.Unlock() -} - -// Precondition: T is not a method signature (*Signature with Recv()!=nil). -// Recursive case: skip => don't create methods for T. -// -// EXCLUSIVE_LOCKS_REQUIRED(prog.methodsMu) -// -func (prog *Program) needMethods(T types.Type, skip bool) { - // Each package maintains its own set of types it has visited. - if prevSkip, ok := prog.runtimeTypes.At(T).(bool); ok { - // needMethods(T) was previously called - if !prevSkip || skip { - return // already seen, with same or false 'skip' value - } - } - prog.runtimeTypes.Set(T, skip) - - tmset := prog.MethodSets.MethodSet(T) - - if !skip && !isInterface(T) && tmset.Len() > 0 { - // Create methods of T. - mset := prog.createMethodSet(T) - if !mset.complete { - mset.complete = true - n := tmset.Len() - for i := 0; i < n; i++ { - prog.addMethod(mset, tmset.At(i)) - } - } - } - - // Recursion over signatures of each method. - for i := 0; i < tmset.Len(); i++ { - sig := tmset.At(i).Type().(*types.Signature) - prog.needMethods(sig.Params(), false) - prog.needMethods(sig.Results(), false) - } - - switch t := T.(type) { - case *types.Basic: - // nop - - case *types.Interface: - // nop---handled by recursion over method set. - - case *types.Pointer: - prog.needMethods(t.Elem(), false) - - case *types.Slice: - prog.needMethods(t.Elem(), false) - - case *types.Chan: - prog.needMethods(t.Elem(), false) - - case *types.Map: - prog.needMethods(t.Key(), false) - prog.needMethods(t.Elem(), false) - - case *types.Signature: - if t.Recv() != nil { - panic(fmt.Sprintf("Signature %s has Recv %s", t, t.Recv())) - } - prog.needMethods(t.Params(), false) - prog.needMethods(t.Results(), false) - - case *types.Named: - // A pointer-to-named type can be derived from a named - // type via reflection. It may have methods too. - prog.needMethods(types.NewPointer(T), false) - - // Consider 'type T struct{S}' where S has methods. - // Reflection provides no way to get from T to struct{S}, - // only to S, so the method set of struct{S} is unwanted, - // so set 'skip' flag during recursion. - prog.needMethods(t.Underlying(), true) - - case *types.Array: - prog.needMethods(t.Elem(), false) - - case *types.Struct: - for i, n := 0, t.NumFields(); i < n; i++ { - prog.needMethods(t.Field(i).Type(), false) - } - - case *types.Tuple: - for i, n := 0, t.Len(); i < n; i++ { - prog.needMethods(t.At(i).Type(), false) - } - - default: - panic(T) - } -} diff --git a/vendor/golang.org/x/tools/go/ssa/mode.go b/vendor/golang.org/x/tools/go/ssa/mode.go deleted file mode 100644 index d2a269893a7..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/mode.go +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file defines the BuilderMode type and its command-line flag. - -import ( - "bytes" - "fmt" -) - -// BuilderMode is a bitmask of options for diagnostics and checking. -// -// *BuilderMode satisfies the flag.Value interface. Example: -// -// var mode = ssa.BuilderMode(0) -// func init() { flag.Var(&mode, "build", ssa.BuilderModeDoc) } -// -type BuilderMode uint - -const ( - PrintPackages BuilderMode = 1 << iota // Print package inventory to stdout - PrintFunctions // Print function SSA code to stdout - LogSource // Log source locations as SSA builder progresses - SanityCheckFunctions // Perform sanity checking of function bodies - NaiveForm // Build naïve SSA form: don't replace local loads/stores with registers - BuildSerially // Build packages serially, not in parallel. - GlobalDebug // Enable debug info for all packages - BareInits // Build init functions without guards or calls to dependent inits -) - -const BuilderModeDoc = `Options controlling the SSA builder. -The value is a sequence of zero or more of these letters: -C perform sanity [C]hecking of the SSA form. -D include [D]ebug info for every function. -P print [P]ackage inventory. -F print [F]unction SSA code. -S log [S]ource locations as SSA builder progresses. -L build distinct packages seria[L]ly instead of in parallel. -N build [N]aive SSA form: don't replace local loads/stores with registers. -I build bare [I]nit functions: no init guards or calls to dependent inits. -` - -func (m BuilderMode) String() string { - var buf bytes.Buffer - if m&GlobalDebug != 0 { - buf.WriteByte('D') - } - if m&PrintPackages != 0 { - buf.WriteByte('P') - } - if m&PrintFunctions != 0 { - buf.WriteByte('F') - } - if m&LogSource != 0 { - buf.WriteByte('S') - } - if m&SanityCheckFunctions != 0 { - buf.WriteByte('C') - } - if m&NaiveForm != 0 { - buf.WriteByte('N') - } - if m&BuildSerially != 0 { - buf.WriteByte('L') - } - return buf.String() -} - -// Set parses the flag characters in s and updates *m. -func (m *BuilderMode) Set(s string) error { - var mode BuilderMode - for _, c := range s { - switch c { - case 'D': - mode |= GlobalDebug - case 'P': - mode |= PrintPackages - case 'F': - mode |= PrintFunctions - case 'S': - mode |= LogSource | BuildSerially - case 'C': - mode |= SanityCheckFunctions - case 'N': - mode |= NaiveForm - case 'L': - mode |= BuildSerially - default: - return fmt.Errorf("unknown BuilderMode option: %q", c) - } - } - *m = mode - return nil -} - -// Get returns m. -func (m BuilderMode) Get() interface{} { return m } diff --git a/vendor/golang.org/x/tools/go/ssa/print.go b/vendor/golang.org/x/tools/go/ssa/print.go deleted file mode 100644 index 3333ba41a00..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/print.go +++ /dev/null @@ -1,431 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file implements the String() methods for all Value and -// Instruction types. - -import ( - "bytes" - "fmt" - "go/types" - "io" - "reflect" - "sort" - - "golang.org/x/tools/go/types/typeutil" -) - -// relName returns the name of v relative to i. -// In most cases, this is identical to v.Name(), but references to -// Functions (including methods) and Globals use RelString and -// all types are displayed with relType, so that only cross-package -// references are package-qualified. -// -func relName(v Value, i Instruction) string { - var from *types.Package - if i != nil { - from = i.Parent().pkg() - } - switch v := v.(type) { - case Member: // *Function or *Global - return v.RelString(from) - case *Const: - return v.RelString(from) - } - return v.Name() -} - -func relType(t types.Type, from *types.Package) string { - return types.TypeString(t, types.RelativeTo(from)) -} - -func relString(m Member, from *types.Package) string { - // NB: not all globals have an Object (e.g. init$guard), - // so use Package().Object not Object.Package(). - if pkg := m.Package().Pkg; pkg != nil && pkg != from { - return fmt.Sprintf("%s.%s", pkg.Path(), m.Name()) - } - return m.Name() -} - -// Value.String() -// -// This method is provided only for debugging. -// It never appears in disassembly, which uses Value.Name(). - -func (v *Parameter) String() string { - from := v.Parent().pkg() - return fmt.Sprintf("parameter %s : %s", v.Name(), relType(v.Type(), from)) -} - -func (v *FreeVar) String() string { - from := v.Parent().pkg() - return fmt.Sprintf("freevar %s : %s", v.Name(), relType(v.Type(), from)) -} - -func (v *Builtin) String() string { - return fmt.Sprintf("builtin %s", v.Name()) -} - -// Instruction.String() - -func (v *Alloc) String() string { - op := "local" - if v.Heap { - op = "new" - } - from := v.Parent().pkg() - return fmt.Sprintf("%s %s (%s)", op, relType(deref(v.Type()), from), v.Comment) -} - -func (v *Phi) String() string { - var b bytes.Buffer - b.WriteString("phi [") - for i, edge := range v.Edges { - if i > 0 { - b.WriteString(", ") - } - // Be robust against malformed CFG. - if v.block == nil { - b.WriteString("??") - continue - } - block := -1 - if i < len(v.block.Preds) { - block = v.block.Preds[i].Index - } - fmt.Fprintf(&b, "%d: ", block) - edgeVal := "" // be robust - if edge != nil { - edgeVal = relName(edge, v) - } - b.WriteString(edgeVal) - } - b.WriteString("]") - if v.Comment != "" { - b.WriteString(" #") - b.WriteString(v.Comment) - } - return b.String() -} - -func printCall(v *CallCommon, prefix string, instr Instruction) string { - var b bytes.Buffer - b.WriteString(prefix) - if !v.IsInvoke() { - b.WriteString(relName(v.Value, instr)) - } else { - fmt.Fprintf(&b, "invoke %s.%s", relName(v.Value, instr), v.Method.Name()) - } - b.WriteString("(") - for i, arg := range v.Args { - if i > 0 { - b.WriteString(", ") - } - b.WriteString(relName(arg, instr)) - } - if v.Signature().Variadic() { - b.WriteString("...") - } - b.WriteString(")") - return b.String() -} - -func (c *CallCommon) String() string { - return printCall(c, "", nil) -} - -func (v *Call) String() string { - return printCall(&v.Call, "", v) -} - -func (v *BinOp) String() string { - return fmt.Sprintf("%s %s %s", relName(v.X, v), v.Op.String(), relName(v.Y, v)) -} - -func (v *UnOp) String() string { - return fmt.Sprintf("%s%s%s", v.Op, relName(v.X, v), commaOk(v.CommaOk)) -} - -func printConv(prefix string, v, x Value) string { - from := v.Parent().pkg() - return fmt.Sprintf("%s %s <- %s (%s)", - prefix, - relType(v.Type(), from), - relType(x.Type(), from), - relName(x, v.(Instruction))) -} - -func (v *ChangeType) String() string { return printConv("changetype", v, v.X) } -func (v *Convert) String() string { return printConv("convert", v, v.X) } -func (v *ChangeInterface) String() string { return printConv("change interface", v, v.X) } -func (v *MakeInterface) String() string { return printConv("make", v, v.X) } - -func (v *MakeClosure) String() string { - var b bytes.Buffer - fmt.Fprintf(&b, "make closure %s", relName(v.Fn, v)) - if v.Bindings != nil { - b.WriteString(" [") - for i, c := range v.Bindings { - if i > 0 { - b.WriteString(", ") - } - b.WriteString(relName(c, v)) - } - b.WriteString("]") - } - return b.String() -} - -func (v *MakeSlice) String() string { - from := v.Parent().pkg() - return fmt.Sprintf("make %s %s %s", - relType(v.Type(), from), - relName(v.Len, v), - relName(v.Cap, v)) -} - -func (v *Slice) String() string { - var b bytes.Buffer - b.WriteString("slice ") - b.WriteString(relName(v.X, v)) - b.WriteString("[") - if v.Low != nil { - b.WriteString(relName(v.Low, v)) - } - b.WriteString(":") - if v.High != nil { - b.WriteString(relName(v.High, v)) - } - if v.Max != nil { - b.WriteString(":") - b.WriteString(relName(v.Max, v)) - } - b.WriteString("]") - return b.String() -} - -func (v *MakeMap) String() string { - res := "" - if v.Reserve != nil { - res = relName(v.Reserve, v) - } - from := v.Parent().pkg() - return fmt.Sprintf("make %s %s", relType(v.Type(), from), res) -} - -func (v *MakeChan) String() string { - from := v.Parent().pkg() - return fmt.Sprintf("make %s %s", relType(v.Type(), from), relName(v.Size, v)) -} - -func (v *FieldAddr) String() string { - st := deref(v.X.Type()).Underlying().(*types.Struct) - // Be robust against a bad index. - name := "?" - if 0 <= v.Field && v.Field < st.NumFields() { - name = st.Field(v.Field).Name() - } - return fmt.Sprintf("&%s.%s [#%d]", relName(v.X, v), name, v.Field) -} - -func (v *Field) String() string { - st := v.X.Type().Underlying().(*types.Struct) - // Be robust against a bad index. - name := "?" - if 0 <= v.Field && v.Field < st.NumFields() { - name = st.Field(v.Field).Name() - } - return fmt.Sprintf("%s.%s [#%d]", relName(v.X, v), name, v.Field) -} - -func (v *IndexAddr) String() string { - return fmt.Sprintf("&%s[%s]", relName(v.X, v), relName(v.Index, v)) -} - -func (v *Index) String() string { - return fmt.Sprintf("%s[%s]", relName(v.X, v), relName(v.Index, v)) -} - -func (v *Lookup) String() string { - return fmt.Sprintf("%s[%s]%s", relName(v.X, v), relName(v.Index, v), commaOk(v.CommaOk)) -} - -func (v *Range) String() string { - return "range " + relName(v.X, v) -} - -func (v *Next) String() string { - return "next " + relName(v.Iter, v) -} - -func (v *TypeAssert) String() string { - from := v.Parent().pkg() - return fmt.Sprintf("typeassert%s %s.(%s)", commaOk(v.CommaOk), relName(v.X, v), relType(v.AssertedType, from)) -} - -func (v *Extract) String() string { - return fmt.Sprintf("extract %s #%d", relName(v.Tuple, v), v.Index) -} - -func (s *Jump) String() string { - // Be robust against malformed CFG. - block := -1 - if s.block != nil && len(s.block.Succs) == 1 { - block = s.block.Succs[0].Index - } - return fmt.Sprintf("jump %d", block) -} - -func (s *If) String() string { - // Be robust against malformed CFG. - tblock, fblock := -1, -1 - if s.block != nil && len(s.block.Succs) == 2 { - tblock = s.block.Succs[0].Index - fblock = s.block.Succs[1].Index - } - return fmt.Sprintf("if %s goto %d else %d", relName(s.Cond, s), tblock, fblock) -} - -func (s *Go) String() string { - return printCall(&s.Call, "go ", s) -} - -func (s *Panic) String() string { - return "panic " + relName(s.X, s) -} - -func (s *Return) String() string { - var b bytes.Buffer - b.WriteString("return") - for i, r := range s.Results { - if i == 0 { - b.WriteString(" ") - } else { - b.WriteString(", ") - } - b.WriteString(relName(r, s)) - } - return b.String() -} - -func (*RunDefers) String() string { - return "rundefers" -} - -func (s *Send) String() string { - return fmt.Sprintf("send %s <- %s", relName(s.Chan, s), relName(s.X, s)) -} - -func (s *Defer) String() string { - return printCall(&s.Call, "defer ", s) -} - -func (s *Select) String() string { - var b bytes.Buffer - for i, st := range s.States { - if i > 0 { - b.WriteString(", ") - } - if st.Dir == types.RecvOnly { - b.WriteString("<-") - b.WriteString(relName(st.Chan, s)) - } else { - b.WriteString(relName(st.Chan, s)) - b.WriteString("<-") - b.WriteString(relName(st.Send, s)) - } - } - non := "" - if !s.Blocking { - non = "non" - } - return fmt.Sprintf("select %sblocking [%s]", non, b.String()) -} - -func (s *Store) String() string { - return fmt.Sprintf("*%s = %s", relName(s.Addr, s), relName(s.Val, s)) -} - -func (s *MapUpdate) String() string { - return fmt.Sprintf("%s[%s] = %s", relName(s.Map, s), relName(s.Key, s), relName(s.Value, s)) -} - -func (s *DebugRef) String() string { - p := s.Parent().Prog.Fset.Position(s.Pos()) - var descr interface{} - if s.object != nil { - descr = s.object // e.g. "var x int" - } else { - descr = reflect.TypeOf(s.Expr) // e.g. "*ast.CallExpr" - } - var addr string - if s.IsAddr { - addr = "address of " - } - return fmt.Sprintf("; %s%s @ %d:%d is %s", addr, descr, p.Line, p.Column, s.X.Name()) -} - -func (p *Package) String() string { - return "package " + p.Pkg.Path() -} - -var _ io.WriterTo = (*Package)(nil) // *Package implements io.Writer - -func (p *Package) WriteTo(w io.Writer) (int64, error) { - var buf bytes.Buffer - WritePackage(&buf, p) - n, err := w.Write(buf.Bytes()) - return int64(n), err -} - -// WritePackage writes to buf a human-readable summary of p. -func WritePackage(buf *bytes.Buffer, p *Package) { - fmt.Fprintf(buf, "%s:\n", p) - - var names []string - maxname := 0 - for name := range p.Members { - if l := len(name); l > maxname { - maxname = l - } - names = append(names, name) - } - - from := p.Pkg - sort.Strings(names) - for _, name := range names { - switch mem := p.Members[name].(type) { - case *NamedConst: - fmt.Fprintf(buf, " const %-*s %s = %s\n", - maxname, name, mem.Name(), mem.Value.RelString(from)) - - case *Function: - fmt.Fprintf(buf, " func %-*s %s\n", - maxname, name, relType(mem.Type(), from)) - - case *Type: - fmt.Fprintf(buf, " type %-*s %s\n", - maxname, name, relType(mem.Type().Underlying(), from)) - for _, meth := range typeutil.IntuitiveMethodSet(mem.Type(), &p.Prog.MethodSets) { - fmt.Fprintf(buf, " %s\n", types.SelectionString(meth, types.RelativeTo(from))) - } - - case *Global: - fmt.Fprintf(buf, " var %-*s %s\n", - maxname, name, relType(mem.Type().(*types.Pointer).Elem(), from)) - } - } - - fmt.Fprintf(buf, "\n") -} - -func commaOk(x bool) string { - if x { - return ",ok" - } - return "" -} diff --git a/vendor/golang.org/x/tools/go/ssa/sanity.go b/vendor/golang.org/x/tools/go/ssa/sanity.go deleted file mode 100644 index 0a7abc5e98f..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/sanity.go +++ /dev/null @@ -1,532 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// An optional pass for sanity-checking invariants of the SSA representation. -// Currently it checks CFG invariants but little at the instruction level. - -import ( - "fmt" - "go/types" - "io" - "os" - "strings" -) - -type sanity struct { - reporter io.Writer - fn *Function - block *BasicBlock - instrs map[Instruction]struct{} - insane bool -} - -// sanityCheck performs integrity checking of the SSA representation -// of the function fn and returns true if it was valid. Diagnostics -// are written to reporter if non-nil, os.Stderr otherwise. Some -// diagnostics are only warnings and do not imply a negative result. -// -// Sanity-checking is intended to facilitate the debugging of code -// transformation passes. -// -func sanityCheck(fn *Function, reporter io.Writer) bool { - if reporter == nil { - reporter = os.Stderr - } - return (&sanity{reporter: reporter}).checkFunction(fn) -} - -// mustSanityCheck is like sanityCheck but panics instead of returning -// a negative result. -// -func mustSanityCheck(fn *Function, reporter io.Writer) { - if !sanityCheck(fn, reporter) { - fn.WriteTo(os.Stderr) - panic("SanityCheck failed") - } -} - -func (s *sanity) diagnostic(prefix, format string, args ...interface{}) { - fmt.Fprintf(s.reporter, "%s: function %s", prefix, s.fn) - if s.block != nil { - fmt.Fprintf(s.reporter, ", block %s", s.block) - } - io.WriteString(s.reporter, ": ") - fmt.Fprintf(s.reporter, format, args...) - io.WriteString(s.reporter, "\n") -} - -func (s *sanity) errorf(format string, args ...interface{}) { - s.insane = true - s.diagnostic("Error", format, args...) -} - -func (s *sanity) warnf(format string, args ...interface{}) { - s.diagnostic("Warning", format, args...) -} - -// findDuplicate returns an arbitrary basic block that appeared more -// than once in blocks, or nil if all were unique. -func findDuplicate(blocks []*BasicBlock) *BasicBlock { - if len(blocks) < 2 { - return nil - } - if blocks[0] == blocks[1] { - return blocks[0] - } - // Slow path: - m := make(map[*BasicBlock]bool) - for _, b := range blocks { - if m[b] { - return b - } - m[b] = true - } - return nil -} - -func (s *sanity) checkInstr(idx int, instr Instruction) { - switch instr := instr.(type) { - case *If, *Jump, *Return, *Panic: - s.errorf("control flow instruction not at end of block") - case *Phi: - if idx == 0 { - // It suffices to apply this check to just the first phi node. - if dup := findDuplicate(s.block.Preds); dup != nil { - s.errorf("phi node in block with duplicate predecessor %s", dup) - } - } else { - prev := s.block.Instrs[idx-1] - if _, ok := prev.(*Phi); !ok { - s.errorf("Phi instruction follows a non-Phi: %T", prev) - } - } - if ne, np := len(instr.Edges), len(s.block.Preds); ne != np { - s.errorf("phi node has %d edges but %d predecessors", ne, np) - - } else { - for i, e := range instr.Edges { - if e == nil { - s.errorf("phi node '%s' has no value for edge #%d from %s", instr.Comment, i, s.block.Preds[i]) - } - } - } - - case *Alloc: - if !instr.Heap { - found := false - for _, l := range s.fn.Locals { - if l == instr { - found = true - break - } - } - if !found { - s.errorf("local alloc %s = %s does not appear in Function.Locals", instr.Name(), instr) - } - } - - case *BinOp: - case *Call: - case *ChangeInterface: - case *ChangeType: - case *Convert: - if _, ok := instr.X.Type().Underlying().(*types.Basic); !ok { - if _, ok := instr.Type().Underlying().(*types.Basic); !ok { - s.errorf("convert %s -> %s: at least one type must be basic", instr.X.Type(), instr.Type()) - } - } - - case *Defer: - case *Extract: - case *Field: - case *FieldAddr: - case *Go: - case *Index: - case *IndexAddr: - case *Lookup: - case *MakeChan: - case *MakeClosure: - numFree := len(instr.Fn.(*Function).FreeVars) - numBind := len(instr.Bindings) - if numFree != numBind { - s.errorf("MakeClosure has %d Bindings for function %s with %d free vars", - numBind, instr.Fn, numFree) - - } - if recv := instr.Type().(*types.Signature).Recv(); recv != nil { - s.errorf("MakeClosure's type includes receiver %s", recv.Type()) - } - - case *MakeInterface: - case *MakeMap: - case *MakeSlice: - case *MapUpdate: - case *Next: - case *Range: - case *RunDefers: - case *Select: - case *Send: - case *Slice: - case *Store: - case *TypeAssert: - case *UnOp: - case *DebugRef: - // TODO(adonovan): implement checks. - default: - panic(fmt.Sprintf("Unknown instruction type: %T", instr)) - } - - if call, ok := instr.(CallInstruction); ok { - if call.Common().Signature() == nil { - s.errorf("nil signature: %s", call) - } - } - - // Check that value-defining instructions have valid types - // and a valid referrer list. - if v, ok := instr.(Value); ok { - t := v.Type() - if t == nil { - s.errorf("no type: %s = %s", v.Name(), v) - } else if t == tRangeIter { - // not a proper type; ignore. - } else if b, ok := t.Underlying().(*types.Basic); ok && b.Info()&types.IsUntyped != 0 { - s.errorf("instruction has 'untyped' result: %s = %s : %s", v.Name(), v, t) - } - s.checkReferrerList(v) - } - - // Untyped constants are legal as instruction Operands(), - // for example: - // _ = "foo"[0] - // or: - // if wordsize==64 {...} - - // All other non-Instruction Values can be found via their - // enclosing Function or Package. -} - -func (s *sanity) checkFinalInstr(instr Instruction) { - switch instr := instr.(type) { - case *If: - if nsuccs := len(s.block.Succs); nsuccs != 2 { - s.errorf("If-terminated block has %d successors; expected 2", nsuccs) - return - } - if s.block.Succs[0] == s.block.Succs[1] { - s.errorf("If-instruction has same True, False target blocks: %s", s.block.Succs[0]) - return - } - - case *Jump: - if nsuccs := len(s.block.Succs); nsuccs != 1 { - s.errorf("Jump-terminated block has %d successors; expected 1", nsuccs) - return - } - - case *Return: - if nsuccs := len(s.block.Succs); nsuccs != 0 { - s.errorf("Return-terminated block has %d successors; expected none", nsuccs) - return - } - if na, nf := len(instr.Results), s.fn.Signature.Results().Len(); nf != na { - s.errorf("%d-ary return in %d-ary function", na, nf) - } - - case *Panic: - if nsuccs := len(s.block.Succs); nsuccs != 0 { - s.errorf("Panic-terminated block has %d successors; expected none", nsuccs) - return - } - - default: - s.errorf("non-control flow instruction at end of block") - } -} - -func (s *sanity) checkBlock(b *BasicBlock, index int) { - s.block = b - - if b.Index != index { - s.errorf("block has incorrect Index %d", b.Index) - } - if b.parent != s.fn { - s.errorf("block has incorrect parent %s", b.parent) - } - - // Check all blocks are reachable. - // (The entry block is always implicitly reachable, - // as is the Recover block, if any.) - if (index > 0 && b != b.parent.Recover) && len(b.Preds) == 0 { - s.warnf("unreachable block") - if b.Instrs == nil { - // Since this block is about to be pruned, - // tolerating transient problems in it - // simplifies other optimizations. - return - } - } - - // Check predecessor and successor relations are dual, - // and that all blocks in CFG belong to same function. - for _, a := range b.Preds { - found := false - for _, bb := range a.Succs { - if bb == b { - found = true - break - } - } - if !found { - s.errorf("expected successor edge in predecessor %s; found only: %s", a, a.Succs) - } - if a.parent != s.fn { - s.errorf("predecessor %s belongs to different function %s", a, a.parent) - } - } - for _, c := range b.Succs { - found := false - for _, bb := range c.Preds { - if bb == b { - found = true - break - } - } - if !found { - s.errorf("expected predecessor edge in successor %s; found only: %s", c, c.Preds) - } - if c.parent != s.fn { - s.errorf("successor %s belongs to different function %s", c, c.parent) - } - } - - // Check each instruction is sane. - n := len(b.Instrs) - if n == 0 { - s.errorf("basic block contains no instructions") - } - var rands [10]*Value // reuse storage - for j, instr := range b.Instrs { - if instr == nil { - s.errorf("nil instruction at index %d", j) - continue - } - if b2 := instr.Block(); b2 == nil { - s.errorf("nil Block() for instruction at index %d", j) - continue - } else if b2 != b { - s.errorf("wrong Block() (%s) for instruction at index %d ", b2, j) - continue - } - if j < n-1 { - s.checkInstr(j, instr) - } else { - s.checkFinalInstr(instr) - } - - // Check Instruction.Operands. - operands: - for i, op := range instr.Operands(rands[:0]) { - if op == nil { - s.errorf("nil operand pointer %d of %s", i, instr) - continue - } - val := *op - if val == nil { - continue // a nil operand is ok - } - - // Check that "untyped" types only appear on constant operands. - if _, ok := (*op).(*Const); !ok { - if basic, ok := (*op).Type().(*types.Basic); ok { - if basic.Info()&types.IsUntyped != 0 { - s.errorf("operand #%d of %s is untyped: %s", i, instr, basic) - } - } - } - - // Check that Operands that are also Instructions belong to same function. - // TODO(adonovan): also check their block dominates block b. - if val, ok := val.(Instruction); ok { - if val.Block() == nil { - s.errorf("operand %d of %s is an instruction (%s) that belongs to no block", i, instr, val) - } else if val.Parent() != s.fn { - s.errorf("operand %d of %s is an instruction (%s) from function %s", i, instr, val, val.Parent()) - } - } - - // Check that each function-local operand of - // instr refers back to instr. (NB: quadratic) - switch val := val.(type) { - case *Const, *Global, *Builtin: - continue // not local - case *Function: - if val.parent == nil { - continue // only anon functions are local - } - } - - // TODO(adonovan): check val.Parent() != nil <=> val.Referrers() is defined. - - if refs := val.Referrers(); refs != nil { - for _, ref := range *refs { - if ref == instr { - continue operands - } - } - s.errorf("operand %d of %s (%s) does not refer to us", i, instr, val) - } else { - s.errorf("operand %d of %s (%s) has no referrers", i, instr, val) - } - } - } -} - -func (s *sanity) checkReferrerList(v Value) { - refs := v.Referrers() - if refs == nil { - s.errorf("%s has missing referrer list", v.Name()) - return - } - for i, ref := range *refs { - if _, ok := s.instrs[ref]; !ok { - s.errorf("%s.Referrers()[%d] = %s is not an instruction belonging to this function", v.Name(), i, ref) - } - } -} - -func (s *sanity) checkFunction(fn *Function) bool { - // TODO(adonovan): check Function invariants: - // - check params match signature - // - check transient fields are nil - // - warn if any fn.Locals do not appear among block instructions. - s.fn = fn - if fn.Prog == nil { - s.errorf("nil Prog") - } - - _ = fn.String() // must not crash - _ = fn.RelString(fn.pkg()) // must not crash - - // All functions have a package, except delegates (which are - // shared across packages, or duplicated as weak symbols in a - // separate-compilation model), and error.Error. - if fn.Pkg == nil { - if strings.HasPrefix(fn.Synthetic, "wrapper ") || - strings.HasPrefix(fn.Synthetic, "bound ") || - strings.HasPrefix(fn.Synthetic, "thunk ") || - strings.HasSuffix(fn.name, "Error") { - // ok - } else { - s.errorf("nil Pkg") - } - } - if src, syn := fn.Synthetic == "", fn.Syntax() != nil; src != syn { - s.errorf("got fromSource=%t, hasSyntax=%t; want same values", src, syn) - } - for i, l := range fn.Locals { - if l.Parent() != fn { - s.errorf("Local %s at index %d has wrong parent", l.Name(), i) - } - if l.Heap { - s.errorf("Local %s at index %d has Heap flag set", l.Name(), i) - } - } - // Build the set of valid referrers. - s.instrs = make(map[Instruction]struct{}) - for _, b := range fn.Blocks { - for _, instr := range b.Instrs { - s.instrs[instr] = struct{}{} - } - } - for i, p := range fn.Params { - if p.Parent() != fn { - s.errorf("Param %s at index %d has wrong parent", p.Name(), i) - } - // Check common suffix of Signature and Params match type. - if sig := fn.Signature; sig != nil { - j := i - len(fn.Params) + sig.Params().Len() // index within sig.Params - if j < 0 { - continue - } - if !types.Identical(p.Type(), sig.Params().At(j).Type()) { - s.errorf("Param %s at index %d has wrong type (%s, versus %s in Signature)", p.Name(), i, p.Type(), sig.Params().At(j).Type()) - - } - } - s.checkReferrerList(p) - } - for i, fv := range fn.FreeVars { - if fv.Parent() != fn { - s.errorf("FreeVar %s at index %d has wrong parent", fv.Name(), i) - } - s.checkReferrerList(fv) - } - - if fn.Blocks != nil && len(fn.Blocks) == 0 { - // Function _had_ blocks (so it's not external) but - // they were "optimized" away, even the entry block. - s.errorf("Blocks slice is non-nil but empty") - } - for i, b := range fn.Blocks { - if b == nil { - s.warnf("nil *BasicBlock at f.Blocks[%d]", i) - continue - } - s.checkBlock(b, i) - } - if fn.Recover != nil && fn.Blocks[fn.Recover.Index] != fn.Recover { - s.errorf("Recover block is not in Blocks slice") - } - - s.block = nil - for i, anon := range fn.AnonFuncs { - if anon.Parent() != fn { - s.errorf("AnonFuncs[%d]=%s but %s.Parent()=%s", i, anon, anon, anon.Parent()) - } - } - s.fn = nil - return !s.insane -} - -// sanityCheckPackage checks invariants of packages upon creation. -// It does not require that the package is built. -// Unlike sanityCheck (for functions), it just panics at the first error. -func sanityCheckPackage(pkg *Package) { - if pkg.Pkg == nil { - panic(fmt.Sprintf("Package %s has no Object", pkg)) - } - _ = pkg.String() // must not crash - - for name, mem := range pkg.Members { - if name != mem.Name() { - panic(fmt.Sprintf("%s: %T.Name() = %s, want %s", - pkg.Pkg.Path(), mem, mem.Name(), name)) - } - obj := mem.Object() - if obj == nil { - // This check is sound because fields - // {Global,Function}.object have type - // types.Object. (If they were declared as - // *types.{Var,Func}, we'd have a non-empty - // interface containing a nil pointer.) - - continue // not all members have typechecker objects - } - if obj.Name() != name { - if obj.Name() == "init" && strings.HasPrefix(mem.Name(), "init#") { - // Ok. The name of a declared init function varies between - // its types.Func ("init") and its ssa.Function ("init#%d"). - } else { - panic(fmt.Sprintf("%s: %T.Object().Name() = %s, want %s", - pkg.Pkg.Path(), mem, obj.Name(), name)) - } - } - if obj.Pos() != mem.Pos() { - panic(fmt.Sprintf("%s Pos=%d obj.Pos=%d", mem, mem.Pos(), obj.Pos())) - } - } -} diff --git a/vendor/golang.org/x/tools/go/ssa/source.go b/vendor/golang.org/x/tools/go/ssa/source.go deleted file mode 100644 index 8d9cca17039..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/source.go +++ /dev/null @@ -1,293 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file defines utilities for working with source positions -// or source-level named entities ("objects"). - -// TODO(adonovan): test that {Value,Instruction}.Pos() positions match -// the originating syntax, as specified. - -import ( - "go/ast" - "go/token" - "go/types" -) - -// EnclosingFunction returns the function that contains the syntax -// node denoted by path. -// -// Syntax associated with package-level variable specifications is -// enclosed by the package's init() function. -// -// Returns nil if not found; reasons might include: -// - the node is not enclosed by any function. -// - the node is within an anonymous function (FuncLit) and -// its SSA function has not been created yet -// (pkg.Build() has not yet been called). -// -func EnclosingFunction(pkg *Package, path []ast.Node) *Function { - // Start with package-level function... - fn := findEnclosingPackageLevelFunction(pkg, path) - if fn == nil { - return nil // not in any function - } - - // ...then walk down the nested anonymous functions. - n := len(path) -outer: - for i := range path { - if lit, ok := path[n-1-i].(*ast.FuncLit); ok { - for _, anon := range fn.AnonFuncs { - if anon.Pos() == lit.Type.Func { - fn = anon - continue outer - } - } - // SSA function not found: - // - package not yet built, or maybe - // - builder skipped FuncLit in dead block - // (in principle; but currently the Builder - // generates even dead FuncLits). - return nil - } - } - return fn -} - -// HasEnclosingFunction returns true if the AST node denoted by path -// is contained within the declaration of some function or -// package-level variable. -// -// Unlike EnclosingFunction, the behaviour of this function does not -// depend on whether SSA code for pkg has been built, so it can be -// used to quickly reject check inputs that will cause -// EnclosingFunction to fail, prior to SSA building. -// -func HasEnclosingFunction(pkg *Package, path []ast.Node) bool { - return findEnclosingPackageLevelFunction(pkg, path) != nil -} - -// findEnclosingPackageLevelFunction returns the Function -// corresponding to the package-level function enclosing path. -// -func findEnclosingPackageLevelFunction(pkg *Package, path []ast.Node) *Function { - if n := len(path); n >= 2 { // [... {Gen,Func}Decl File] - switch decl := path[n-2].(type) { - case *ast.GenDecl: - if decl.Tok == token.VAR && n >= 3 { - // Package-level 'var' initializer. - return pkg.init - } - - case *ast.FuncDecl: - if decl.Recv == nil && decl.Name.Name == "init" { - // Explicit init() function. - for _, b := range pkg.init.Blocks { - for _, instr := range b.Instrs { - if instr, ok := instr.(*Call); ok { - if callee, ok := instr.Call.Value.(*Function); ok && callee.Pkg == pkg && callee.Pos() == decl.Name.NamePos { - return callee - } - } - } - } - // Hack: return non-nil when SSA is not yet - // built so that HasEnclosingFunction works. - return pkg.init - } - // Declared function/method. - return findNamedFunc(pkg, decl.Name.NamePos) - } - } - return nil // not in any function -} - -// findNamedFunc returns the named function whose FuncDecl.Ident is at -// position pos. -// -func findNamedFunc(pkg *Package, pos token.Pos) *Function { - // Look at all package members and method sets of named types. - // Not very efficient. - for _, mem := range pkg.Members { - switch mem := mem.(type) { - case *Function: - if mem.Pos() == pos { - return mem - } - case *Type: - mset := pkg.Prog.MethodSets.MethodSet(types.NewPointer(mem.Type())) - for i, n := 0, mset.Len(); i < n; i++ { - // Don't call Program.Method: avoid creating wrappers. - obj := mset.At(i).Obj().(*types.Func) - if obj.Pos() == pos { - return pkg.values[obj].(*Function) - } - } - } - } - return nil -} - -// ValueForExpr returns the SSA Value that corresponds to non-constant -// expression e. -// -// It returns nil if no value was found, e.g. -// - the expression is not lexically contained within f; -// - f was not built with debug information; or -// - e is a constant expression. (For efficiency, no debug -// information is stored for constants. Use -// go/types.Info.Types[e].Value instead.) -// - e is a reference to nil or a built-in function. -// - the value was optimised away. -// -// If e is an addressable expression used in an lvalue context, -// value is the address denoted by e, and isAddr is true. -// -// The types of e (or &e, if isAddr) and the result are equal -// (modulo "untyped" bools resulting from comparisons). -// -// (Tip: to find the ssa.Value given a source position, use -// astutil.PathEnclosingInterval to locate the ast.Node, then -// EnclosingFunction to locate the Function, then ValueForExpr to find -// the ssa.Value.) -// -func (f *Function) ValueForExpr(e ast.Expr) (value Value, isAddr bool) { - if f.debugInfo() { // (opt) - e = unparen(e) - for _, b := range f.Blocks { - for _, instr := range b.Instrs { - if ref, ok := instr.(*DebugRef); ok { - if ref.Expr == e { - return ref.X, ref.IsAddr - } - } - } - } - } - return -} - -// --- Lookup functions for source-level named entities (types.Objects) --- - -// Package returns the SSA Package corresponding to the specified -// type-checker package object. -// It returns nil if no such SSA package has been created. -// -func (prog *Program) Package(obj *types.Package) *Package { - return prog.packages[obj] -} - -// packageLevelValue returns the package-level value corresponding to -// the specified named object, which may be a package-level const -// (*Const), var (*Global) or func (*Function) of some package in -// prog. It returns nil if the object is not found. -// -func (prog *Program) packageLevelValue(obj types.Object) Value { - if pkg, ok := prog.packages[obj.Pkg()]; ok { - return pkg.values[obj] - } - return nil -} - -// FuncValue returns the concrete Function denoted by the source-level -// named function obj, or nil if obj denotes an interface method. -// -// TODO(adonovan): check the invariant that obj.Type() matches the -// result's Signature, both in the params/results and in the receiver. -// -func (prog *Program) FuncValue(obj *types.Func) *Function { - fn, _ := prog.packageLevelValue(obj).(*Function) - return fn -} - -// ConstValue returns the SSA Value denoted by the source-level named -// constant obj. -// -func (prog *Program) ConstValue(obj *types.Const) *Const { - // TODO(adonovan): opt: share (don't reallocate) - // Consts for const objects and constant ast.Exprs. - - // Universal constant? {true,false,nil} - if obj.Parent() == types.Universe { - return NewConst(obj.Val(), obj.Type()) - } - // Package-level named constant? - if v := prog.packageLevelValue(obj); v != nil { - return v.(*Const) - } - return NewConst(obj.Val(), obj.Type()) -} - -// VarValue returns the SSA Value that corresponds to a specific -// identifier denoting the source-level named variable obj. -// -// VarValue returns nil if a local variable was not found, perhaps -// because its package was not built, the debug information was not -// requested during SSA construction, or the value was optimized away. -// -// ref is the path to an ast.Ident (e.g. from PathEnclosingInterval), -// and that ident must resolve to obj. -// -// pkg is the package enclosing the reference. (A reference to a var -// always occurs within a function, so we need to know where to find it.) -// -// If the identifier is a field selector and its base expression is -// non-addressable, then VarValue returns the value of that field. -// For example: -// func f() struct {x int} -// f().x // VarValue(x) returns a *Field instruction of type int -// -// All other identifiers denote addressable locations (variables). -// For them, VarValue may return either the variable's address or its -// value, even when the expression is evaluated only for its value; the -// situation is reported by isAddr, the second component of the result. -// -// If !isAddr, the returned value is the one associated with the -// specific identifier. For example, -// var x int // VarValue(x) returns Const 0 here -// x = 1 // VarValue(x) returns Const 1 here -// -// It is not specified whether the value or the address is returned in -// any particular case, as it may depend upon optimizations performed -// during SSA code generation, such as registerization, constant -// folding, avoidance of materialization of subexpressions, etc. -// -func (prog *Program) VarValue(obj *types.Var, pkg *Package, ref []ast.Node) (value Value, isAddr bool) { - // All references to a var are local to some function, possibly init. - fn := EnclosingFunction(pkg, ref) - if fn == nil { - return // e.g. def of struct field; SSA not built? - } - - id := ref[0].(*ast.Ident) - - // Defining ident of a parameter? - if id.Pos() == obj.Pos() { - for _, param := range fn.Params { - if param.Object() == obj { - return param, false - } - } - } - - // Other ident? - for _, b := range fn.Blocks { - for _, instr := range b.Instrs { - if dr, ok := instr.(*DebugRef); ok { - if dr.Pos() == id.Pos() { - return dr.X, dr.IsAddr - } - } - } - } - - // Defining ident of package-level var? - if v := prog.packageLevelValue(obj); v != nil { - return v.(*Global), true - } - - return // e.g. debug info not requested, or var optimized away -} diff --git a/vendor/golang.org/x/tools/go/ssa/ssa.go b/vendor/golang.org/x/tools/go/ssa/ssa.go deleted file mode 100644 index 78272c546a1..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/ssa.go +++ /dev/null @@ -1,1695 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This package defines a high-level intermediate representation for -// Go programs using static single-assignment (SSA) form. - -import ( - "fmt" - "go/ast" - "go/constant" - "go/token" - "go/types" - "sync" - - "golang.org/x/tools/go/types/typeutil" -) - -// A Program is a partial or complete Go program converted to SSA form. -type Program struct { - Fset *token.FileSet // position information for the files of this Program - imported map[string]*Package // all importable Packages, keyed by import path - packages map[*types.Package]*Package // all loaded Packages, keyed by object - mode BuilderMode // set of mode bits for SSA construction - MethodSets typeutil.MethodSetCache // cache of type-checker's method-sets - - methodsMu sync.Mutex // guards the following maps: - methodSets typeutil.Map // maps type to its concrete methodSet - runtimeTypes typeutil.Map // types for which rtypes are needed - canon typeutil.Map // type canonicalization map - bounds map[*types.Func]*Function // bounds for curried x.Method closures - thunks map[selectionKey]*Function // thunks for T.Method expressions -} - -// A Package is a single analyzed Go package containing Members for -// all package-level functions, variables, constants and types it -// declares. These may be accessed directly via Members, or via the -// type-specific accessor methods Func, Type, Var and Const. -// -// Members also contains entries for "init" (the synthetic package -// initializer) and "init#%d", the nth declared init function, -// and unspecified other things too. -// -type Package struct { - Prog *Program // the owning program - Pkg *types.Package // the corresponding go/types.Package - Members map[string]Member // all package members keyed by name (incl. init and init#%d) - values map[types.Object]Value // package members (incl. types and methods), keyed by object - init *Function // Func("init"); the package's init function - debug bool // include full debug info in this package - - // The following fields are set transiently, then cleared - // after building. - buildOnce sync.Once // ensures package building occurs once - ninit int32 // number of init functions - info *types.Info // package type information - files []*ast.File // package ASTs -} - -// A Member is a member of a Go package, implemented by *NamedConst, -// *Global, *Function, or *Type; they are created by package-level -// const, var, func and type declarations respectively. -// -type Member interface { - Name() string // declared name of the package member - String() string // package-qualified name of the package member - RelString(*types.Package) string // like String, but relative refs are unqualified - Object() types.Object // typechecker's object for this member, if any - Pos() token.Pos // position of member's declaration, if known - Type() types.Type // type of the package member - Token() token.Token // token.{VAR,FUNC,CONST,TYPE} - Package() *Package // the containing package -} - -// A Type is a Member of a Package representing a package-level named type. -type Type struct { - object *types.TypeName - pkg *Package -} - -// A NamedConst is a Member of a Package representing a package-level -// named constant. -// -// Pos() returns the position of the declaring ast.ValueSpec.Names[*] -// identifier. -// -// NB: a NamedConst is not a Value; it contains a constant Value, which -// it augments with the name and position of its 'const' declaration. -// -type NamedConst struct { - object *types.Const - Value *Const - pkg *Package -} - -// A Value is an SSA value that can be referenced by an instruction. -type Value interface { - // Name returns the name of this value, and determines how - // this Value appears when used as an operand of an - // Instruction. - // - // This is the same as the source name for Parameters, - // Builtins, Functions, FreeVars, Globals. - // For constants, it is a representation of the constant's value - // and type. For all other Values this is the name of the - // virtual register defined by the instruction. - // - // The name of an SSA Value is not semantically significant, - // and may not even be unique within a function. - Name() string - - // If this value is an Instruction, String returns its - // disassembled form; otherwise it returns unspecified - // human-readable information about the Value, such as its - // kind, name and type. - String() string - - // Type returns the type of this value. Many instructions - // (e.g. IndexAddr) change their behaviour depending on the - // types of their operands. - Type() types.Type - - // Parent returns the function to which this Value belongs. - // It returns nil for named Functions, Builtin, Const and Global. - Parent() *Function - - // Referrers returns the list of instructions that have this - // value as one of their operands; it may contain duplicates - // if an instruction has a repeated operand. - // - // Referrers actually returns a pointer through which the - // caller may perform mutations to the object's state. - // - // Referrers is currently only defined if Parent()!=nil, - // i.e. for the function-local values FreeVar, Parameter, - // Functions (iff anonymous) and all value-defining instructions. - // It returns nil for named Functions, Builtin, Const and Global. - // - // Instruction.Operands contains the inverse of this relation. - Referrers() *[]Instruction - - // Pos returns the location of the AST token most closely - // associated with the operation that gave rise to this value, - // or token.NoPos if it was not explicit in the source. - // - // For each ast.Node type, a particular token is designated as - // the closest location for the expression, e.g. the Lparen - // for an *ast.CallExpr. This permits a compact but - // approximate mapping from Values to source positions for use - // in diagnostic messages, for example. - // - // (Do not use this position to determine which Value - // corresponds to an ast.Expr; use Function.ValueForExpr - // instead. NB: it requires that the function was built with - // debug information.) - Pos() token.Pos -} - -// An Instruction is an SSA instruction that computes a new Value or -// has some effect. -// -// An Instruction that defines a value (e.g. BinOp) also implements -// the Value interface; an Instruction that only has an effect (e.g. Store) -// does not. -// -type Instruction interface { - // String returns the disassembled form of this value. - // - // Examples of Instructions that are Values: - // "x + y" (BinOp) - // "len([])" (Call) - // Note that the name of the Value is not printed. - // - // Examples of Instructions that are not Values: - // "return x" (Return) - // "*y = x" (Store) - // - // (The separation Value.Name() from Value.String() is useful - // for some analyses which distinguish the operation from the - // value it defines, e.g., 'y = local int' is both an allocation - // of memory 'local int' and a definition of a pointer y.) - String() string - - // Parent returns the function to which this instruction - // belongs. - Parent() *Function - - // Block returns the basic block to which this instruction - // belongs. - Block() *BasicBlock - - // setBlock sets the basic block to which this instruction belongs. - setBlock(*BasicBlock) - - // Operands returns the operands of this instruction: the - // set of Values it references. - // - // Specifically, it appends their addresses to rands, a - // user-provided slice, and returns the resulting slice, - // permitting avoidance of memory allocation. - // - // The operands are appended in undefined order, but the order - // is consistent for a given Instruction; the addresses are - // always non-nil but may point to a nil Value. Clients may - // store through the pointers, e.g. to effect a value - // renaming. - // - // Value.Referrers is a subset of the inverse of this - // relation. (Referrers are not tracked for all types of - // Values.) - Operands(rands []*Value) []*Value - - // Pos returns the location of the AST token most closely - // associated with the operation that gave rise to this - // instruction, or token.NoPos if it was not explicit in the - // source. - // - // For each ast.Node type, a particular token is designated as - // the closest location for the expression, e.g. the Go token - // for an *ast.GoStmt. This permits a compact but approximate - // mapping from Instructions to source positions for use in - // diagnostic messages, for example. - // - // (Do not use this position to determine which Instruction - // corresponds to an ast.Expr; see the notes for Value.Pos. - // This position may be used to determine which non-Value - // Instruction corresponds to some ast.Stmts, but not all: If - // and Jump instructions have no Pos(), for example.) - Pos() token.Pos -} - -// A Node is a node in the SSA value graph. Every concrete type that -// implements Node is also either a Value, an Instruction, or both. -// -// Node contains the methods common to Value and Instruction, plus the -// Operands and Referrers methods generalized to return nil for -// non-Instructions and non-Values, respectively. -// -// Node is provided to simplify SSA graph algorithms. Clients should -// use the more specific and informative Value or Instruction -// interfaces where appropriate. -// -type Node interface { - // Common methods: - String() string - Pos() token.Pos - Parent() *Function - - // Partial methods: - Operands(rands []*Value) []*Value // nil for non-Instructions - Referrers() *[]Instruction // nil for non-Values -} - -// Function represents the parameters, results, and code of a function -// or method. -// -// If Blocks is nil, this indicates an external function for which no -// Go source code is available. In this case, FreeVars and Locals -// are nil too. Clients performing whole-program analysis must -// handle external functions specially. -// -// Blocks contains the function's control-flow graph (CFG). -// Blocks[0] is the function entry point; block order is not otherwise -// semantically significant, though it may affect the readability of -// the disassembly. -// To iterate over the blocks in dominance order, use DomPreorder(). -// -// Recover is an optional second entry point to which control resumes -// after a recovered panic. The Recover block may contain only a return -// statement, preceded by a load of the function's named return -// parameters, if any. -// -// A nested function (Parent()!=nil) that refers to one or more -// lexically enclosing local variables ("free variables") has FreeVars. -// Such functions cannot be called directly but require a -// value created by MakeClosure which, via its Bindings, supplies -// values for these parameters. -// -// If the function is a method (Signature.Recv() != nil) then the first -// element of Params is the receiver parameter. -// -// A Go package may declare many functions called "init". -// For each one, Object().Name() returns "init" but Name() returns -// "init#1", etc, in declaration order. -// -// Pos() returns the declaring ast.FuncLit.Type.Func or the position -// of the ast.FuncDecl.Name, if the function was explicit in the -// source. Synthetic wrappers, for which Synthetic != "", may share -// the same position as the function they wrap. -// Syntax.Pos() always returns the position of the declaring "func" token. -// -// Type() returns the function's Signature. -// -type Function struct { - name string - object types.Object // a declared *types.Func or one of its wrappers - method *types.Selection // info about provenance of synthetic methods - Signature *types.Signature - pos token.Pos - - Synthetic string // provenance of synthetic function; "" for true source functions - syntax ast.Node // *ast.Func{Decl,Lit}; replaced with simple ast.Node after build, unless debug mode - parent *Function // enclosing function if anon; nil if global - Pkg *Package // enclosing package; nil for shared funcs (wrappers and error.Error) - Prog *Program // enclosing program - Params []*Parameter // function parameters; for methods, includes receiver - FreeVars []*FreeVar // free variables whose values must be supplied by closure - Locals []*Alloc // local variables of this function - Blocks []*BasicBlock // basic blocks of the function; nil => external - Recover *BasicBlock // optional; control transfers here after recovered panic - AnonFuncs []*Function // anonymous functions directly beneath this one - referrers []Instruction // referring instructions (iff Parent() != nil) - - // The following fields are set transiently during building, - // then cleared. - currentBlock *BasicBlock // where to emit code - objects map[types.Object]Value // addresses of local variables - namedResults []*Alloc // tuple of named results - targets *targets // linked stack of branch targets - lblocks map[*ast.Object]*lblock // labelled blocks -} - -// BasicBlock represents an SSA basic block. -// -// The final element of Instrs is always an explicit transfer of -// control (If, Jump, Return, or Panic). -// -// A block may contain no Instructions only if it is unreachable, -// i.e., Preds is nil. Empty blocks are typically pruned. -// -// BasicBlocks and their Preds/Succs relation form a (possibly cyclic) -// graph independent of the SSA Value graph: the control-flow graph or -// CFG. It is illegal for multiple edges to exist between the same -// pair of blocks. -// -// Each BasicBlock is also a node in the dominator tree of the CFG. -// The tree may be navigated using Idom()/Dominees() and queried using -// Dominates(). -// -// The order of Preds and Succs is significant (to Phi and If -// instructions, respectively). -// -type BasicBlock struct { - Index int // index of this block within Parent().Blocks - Comment string // optional label; no semantic significance - parent *Function // parent function - Instrs []Instruction // instructions in order - Preds, Succs []*BasicBlock // predecessors and successors - succs2 [2]*BasicBlock // initial space for Succs - dom domInfo // dominator tree info - gaps int // number of nil Instrs (transient) - rundefers int // number of rundefers (transient) -} - -// Pure values ---------------------------------------- - -// A FreeVar represents a free variable of the function to which it -// belongs. -// -// FreeVars are used to implement anonymous functions, whose free -// variables are lexically captured in a closure formed by -// MakeClosure. The value of such a free var is an Alloc or another -// FreeVar and is considered a potentially escaping heap address, with -// pointer type. -// -// FreeVars are also used to implement bound method closures. Such a -// free var represents the receiver value and may be of any type that -// has concrete methods. -// -// Pos() returns the position of the value that was captured, which -// belongs to an enclosing function. -// -type FreeVar struct { - name string - typ types.Type - pos token.Pos - parent *Function - referrers []Instruction - - // Transiently needed during building. - outer Value // the Value captured from the enclosing context. -} - -// A Parameter represents an input parameter of a function. -// -type Parameter struct { - name string - object types.Object // a *types.Var; nil for non-source locals - typ types.Type - pos token.Pos - parent *Function - referrers []Instruction -} - -// A Const represents the value of a constant expression. -// -// The underlying type of a constant may be any boolean, numeric, or -// string type. In addition, a Const may represent the nil value of -// any reference type---interface, map, channel, pointer, slice, or -// function---but not "untyped nil". -// -// All source-level constant expressions are represented by a Const -// of the same type and value. -// -// Value holds the value of the constant, independent of its Type(), -// using go/constant representation, or nil for a typed nil value. -// -// Pos() returns token.NoPos. -// -// Example printed form: -// 42:int -// "hello":untyped string -// 3+4i:MyComplex -// -type Const struct { - typ types.Type - Value constant.Value -} - -// A Global is a named Value holding the address of a package-level -// variable. -// -// Pos() returns the position of the ast.ValueSpec.Names[*] -// identifier. -// -type Global struct { - name string - object types.Object // a *types.Var; may be nil for synthetics e.g. init$guard - typ types.Type - pos token.Pos - - Pkg *Package -} - -// A Builtin represents a specific use of a built-in function, e.g. len. -// -// Builtins are immutable values. Builtins do not have addresses. -// Builtins can only appear in CallCommon.Func. -// -// Name() indicates the function: one of the built-in functions from the -// Go spec (excluding "make" and "new") or one of these ssa-defined -// intrinsics: -// -// // wrapnilchk returns ptr if non-nil, panics otherwise. -// // (For use in indirection wrappers.) -// func ssa:wrapnilchk(ptr *T, recvType, methodName string) *T -// -// Object() returns a *types.Builtin for built-ins defined by the spec, -// nil for others. -// -// Type() returns a *types.Signature representing the effective -// signature of the built-in for this call. -// -type Builtin struct { - name string - sig *types.Signature -} - -// Value-defining instructions ---------------------------------------- - -// The Alloc instruction reserves space for a variable of the given type, -// zero-initializes it, and yields its address. -// -// Alloc values are always addresses, and have pointer types, so the -// type of the allocated variable is actually -// Type().Underlying().(*types.Pointer).Elem(). -// -// If Heap is false, Alloc allocates space in the function's -// activation record (frame); we refer to an Alloc(Heap=false) as a -// "local" alloc. Each local Alloc returns the same address each time -// it is executed within the same activation; the space is -// re-initialized to zero. -// -// If Heap is true, Alloc allocates space in the heap; we -// refer to an Alloc(Heap=true) as a "new" alloc. Each new Alloc -// returns a different address each time it is executed. -// -// When Alloc is applied to a channel, map or slice type, it returns -// the address of an uninitialized (nil) reference of that kind; store -// the result of MakeSlice, MakeMap or MakeChan in that location to -// instantiate these types. -// -// Pos() returns the ast.CompositeLit.Lbrace for a composite literal, -// or the ast.CallExpr.Rparen for a call to new() or for a call that -// allocates a varargs slice. -// -// Example printed form: -// t0 = local int -// t1 = new int -// -type Alloc struct { - register - Comment string - Heap bool - index int // dense numbering; for lifting -} - -// The Phi instruction represents an SSA φ-node, which combines values -// that differ across incoming control-flow edges and yields a new -// value. Within a block, all φ-nodes must appear before all non-φ -// nodes. -// -// Pos() returns the position of the && or || for short-circuit -// control-flow joins, or that of the *Alloc for φ-nodes inserted -// during SSA renaming. -// -// Example printed form: -// t2 = phi [0: t0, 1: t1] -// -type Phi struct { - register - Comment string // a hint as to its purpose - Edges []Value // Edges[i] is value for Block().Preds[i] -} - -// The Call instruction represents a function or method call. -// -// The Call instruction yields the function result if there is exactly -// one. Otherwise it returns a tuple, the components of which are -// accessed via Extract. -// -// See CallCommon for generic function call documentation. -// -// Pos() returns the ast.CallExpr.Lparen, if explicit in the source. -// -// Example printed form: -// t2 = println(t0, t1) -// t4 = t3() -// t7 = invoke t5.Println(...t6) -// -type Call struct { - register - Call CallCommon -} - -// The BinOp instruction yields the result of binary operation X Op Y. -// -// Pos() returns the ast.BinaryExpr.OpPos, if explicit in the source. -// -// Example printed form: -// t1 = t0 + 1:int -// -type BinOp struct { - register - // One of: - // ADD SUB MUL QUO REM + - * / % - // AND OR XOR SHL SHR AND_NOT & | ^ << >> &^ - // EQL NEQ LSS LEQ GTR GEQ == != < <= < >= - Op token.Token - X, Y Value -} - -// The UnOp instruction yields the result of Op X. -// ARROW is channel receive. -// MUL is pointer indirection (load). -// XOR is bitwise complement. -// SUB is negation. -// NOT is logical negation. -// -// If CommaOk and Op=ARROW, the result is a 2-tuple of the value above -// and a boolean indicating the success of the receive. The -// components of the tuple are accessed using Extract. -// -// Pos() returns the ast.UnaryExpr.OpPos, if explicit in the source. -// For receive operations (ARROW) implicit in ranging over a channel, -// Pos() returns the ast.RangeStmt.For. -// For implicit memory loads (STAR), Pos() returns the position of the -// most closely associated source-level construct; the details are not -// specified. -// -// Example printed form: -// t0 = *x -// t2 = <-t1,ok -// -type UnOp struct { - register - Op token.Token // One of: NOT SUB ARROW MUL XOR ! - <- * ^ - X Value - CommaOk bool -} - -// The ChangeType instruction applies to X a value-preserving type -// change to Type(). -// -// Type changes are permitted: -// - between a named type and its underlying type. -// - between two named types of the same underlying type. -// - between (possibly named) pointers to identical base types. -// - from a bidirectional channel to a read- or write-channel, -// optionally adding/removing a name. -// -// This operation cannot fail dynamically. -// -// Pos() returns the ast.CallExpr.Lparen, if the instruction arose -// from an explicit conversion in the source. -// -// Example printed form: -// t1 = changetype *int <- IntPtr (t0) -// -type ChangeType struct { - register - X Value -} - -// The Convert instruction yields the conversion of value X to type -// Type(). One or both of those types is basic (but possibly named). -// -// A conversion may change the value and representation of its operand. -// Conversions are permitted: -// - between real numeric types. -// - between complex numeric types. -// - between string and []byte or []rune. -// - between pointers and unsafe.Pointer. -// - between unsafe.Pointer and uintptr. -// - from (Unicode) integer to (UTF-8) string. -// A conversion may imply a type name change also. -// -// This operation cannot fail dynamically. -// -// Conversions of untyped string/number/bool constants to a specific -// representation are eliminated during SSA construction. -// -// Pos() returns the ast.CallExpr.Lparen, if the instruction arose -// from an explicit conversion in the source. -// -// Example printed form: -// t1 = convert []byte <- string (t0) -// -type Convert struct { - register - X Value -} - -// ChangeInterface constructs a value of one interface type from a -// value of another interface type known to be assignable to it. -// This operation cannot fail. -// -// Pos() returns the ast.CallExpr.Lparen if the instruction arose from -// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the -// instruction arose from an explicit e.(T) operation; or token.NoPos -// otherwise. -// -// Example printed form: -// t1 = change interface interface{} <- I (t0) -// -type ChangeInterface struct { - register - X Value -} - -// MakeInterface constructs an instance of an interface type from a -// value of a concrete type. -// -// Use Program.MethodSets.MethodSet(X.Type()) to find the method-set -// of X, and Program.MethodValue(m) to find the implementation of a method. -// -// To construct the zero value of an interface type T, use: -// NewConst(constant.MakeNil(), T, pos) -// -// Pos() returns the ast.CallExpr.Lparen, if the instruction arose -// from an explicit conversion in the source. -// -// Example printed form: -// t1 = make interface{} <- int (42:int) -// t2 = make Stringer <- t0 -// -type MakeInterface struct { - register - X Value -} - -// The MakeClosure instruction yields a closure value whose code is -// Fn and whose free variables' values are supplied by Bindings. -// -// Type() returns a (possibly named) *types.Signature. -// -// Pos() returns the ast.FuncLit.Type.Func for a function literal -// closure or the ast.SelectorExpr.Sel for a bound method closure. -// -// Example printed form: -// t0 = make closure anon@1.2 [x y z] -// t1 = make closure bound$(main.I).add [i] -// -type MakeClosure struct { - register - Fn Value // always a *Function - Bindings []Value // values for each free variable in Fn.FreeVars -} - -// The MakeMap instruction creates a new hash-table-based map object -// and yields a value of kind map. -// -// Type() returns a (possibly named) *types.Map. -// -// Pos() returns the ast.CallExpr.Lparen, if created by make(map), or -// the ast.CompositeLit.Lbrack if created by a literal. -// -// Example printed form: -// t1 = make map[string]int t0 -// t1 = make StringIntMap t0 -// -type MakeMap struct { - register - Reserve Value // initial space reservation; nil => default -} - -// The MakeChan instruction creates a new channel object and yields a -// value of kind chan. -// -// Type() returns a (possibly named) *types.Chan. -// -// Pos() returns the ast.CallExpr.Lparen for the make(chan) that -// created it. -// -// Example printed form: -// t0 = make chan int 0 -// t0 = make IntChan 0 -// -type MakeChan struct { - register - Size Value // int; size of buffer; zero => synchronous. -} - -// The MakeSlice instruction yields a slice of length Len backed by a -// newly allocated array of length Cap. -// -// Both Len and Cap must be non-nil Values of integer type. -// -// (Alloc(types.Array) followed by Slice will not suffice because -// Alloc can only create arrays of constant length.) -// -// Type() returns a (possibly named) *types.Slice. -// -// Pos() returns the ast.CallExpr.Lparen for the make([]T) that -// created it. -// -// Example printed form: -// t1 = make []string 1:int t0 -// t1 = make StringSlice 1:int t0 -// -type MakeSlice struct { - register - Len Value - Cap Value -} - -// The Slice instruction yields a slice of an existing string, slice -// or *array X between optional integer bounds Low and High. -// -// Dynamically, this instruction panics if X evaluates to a nil *array -// pointer. -// -// Type() returns string if the type of X was string, otherwise a -// *types.Slice with the same element type as X. -// -// Pos() returns the ast.SliceExpr.Lbrack if created by a x[:] slice -// operation, the ast.CompositeLit.Lbrace if created by a literal, or -// NoPos if not explicit in the source (e.g. a variadic argument slice). -// -// Example printed form: -// t1 = slice t0[1:] -// -type Slice struct { - register - X Value // slice, string, or *array - Low, High, Max Value // each may be nil -} - -// The FieldAddr instruction yields the address of Field of *struct X. -// -// The field is identified by its index within the field list of the -// struct type of X. -// -// Dynamically, this instruction panics if X evaluates to a nil -// pointer. -// -// Type() returns a (possibly named) *types.Pointer. -// -// Pos() returns the position of the ast.SelectorExpr.Sel for the -// field, if explicit in the source. -// -// Example printed form: -// t1 = &t0.name [#1] -// -type FieldAddr struct { - register - X Value // *struct - Field int // field is X.Type().Underlying().(*types.Pointer).Elem().Underlying().(*types.Struct).Field(Field) -} - -// The Field instruction yields the Field of struct X. -// -// The field is identified by its index within the field list of the -// struct type of X; by using numeric indices we avoid ambiguity of -// package-local identifiers and permit compact representations. -// -// Pos() returns the position of the ast.SelectorExpr.Sel for the -// field, if explicit in the source. -// -// Example printed form: -// t1 = t0.name [#1] -// -type Field struct { - register - X Value // struct - Field int // index into X.Type().(*types.Struct).Fields -} - -// The IndexAddr instruction yields the address of the element at -// index Index of collection X. Index is an integer expression. -// -// The elements of maps and strings are not addressable; use Lookup or -// MapUpdate instead. -// -// Dynamically, this instruction panics if X evaluates to a nil *array -// pointer. -// -// Type() returns a (possibly named) *types.Pointer. -// -// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if -// explicit in the source. -// -// Example printed form: -// t2 = &t0[t1] -// -type IndexAddr struct { - register - X Value // slice or *array, - Index Value // numeric index -} - -// The Index instruction yields element Index of array X. -// -// Pos() returns the ast.IndexExpr.Lbrack for the index operation, if -// explicit in the source. -// -// Example printed form: -// t2 = t0[t1] -// -type Index struct { - register - X Value // array - Index Value // integer index -} - -// The Lookup instruction yields element Index of collection X, a map -// or string. Index is an integer expression if X is a string or the -// appropriate key type if X is a map. -// -// If CommaOk, the result is a 2-tuple of the value above and a -// boolean indicating the result of a map membership test for the key. -// The components of the tuple are accessed using Extract. -// -// Pos() returns the ast.IndexExpr.Lbrack, if explicit in the source. -// -// Example printed form: -// t2 = t0[t1] -// t5 = t3[t4],ok -// -type Lookup struct { - register - X Value // string or map - Index Value // numeric or key-typed index - CommaOk bool // return a value,ok pair -} - -// SelectState is a helper for Select. -// It represents one goal state and its corresponding communication. -// -type SelectState struct { - Dir types.ChanDir // direction of case (SendOnly or RecvOnly) - Chan Value // channel to use (for send or receive) - Send Value // value to send (for send) - Pos token.Pos // position of token.ARROW - DebugNode ast.Node // ast.SendStmt or ast.UnaryExpr(<-) [debug mode] -} - -// The Select instruction tests whether (or blocks until) one -// of the specified sent or received states is entered. -// -// Let n be the number of States for which Dir==RECV and T_i (0<=i string iterator; false => map iterator. -} - -// The TypeAssert instruction tests whether interface value X has type -// AssertedType. -// -// If !CommaOk, on success it returns v, the result of the conversion -// (defined below); on failure it panics. -// -// If CommaOk: on success it returns a pair (v, true) where v is the -// result of the conversion; on failure it returns (z, false) where z -// is AssertedType's zero value. The components of the pair must be -// accessed using the Extract instruction. -// -// If AssertedType is a concrete type, TypeAssert checks whether the -// dynamic type in interface X is equal to it, and if so, the result -// of the conversion is a copy of the value in the interface. -// -// If AssertedType is an interface, TypeAssert checks whether the -// dynamic type of the interface is assignable to it, and if so, the -// result of the conversion is a copy of the interface value X. -// If AssertedType is a superinterface of X.Type(), the operation will -// fail iff the operand is nil. (Contrast with ChangeInterface, which -// performs no nil-check.) -// -// Type() reflects the actual type of the result, possibly a -// 2-types.Tuple; AssertedType is the asserted type. -// -// Pos() returns the ast.CallExpr.Lparen if the instruction arose from -// an explicit T(e) conversion; the ast.TypeAssertExpr.Lparen if the -// instruction arose from an explicit e.(T) operation; or the -// ast.CaseClause.Case if the instruction arose from a case of a -// type-switch statement. -// -// Example printed form: -// t1 = typeassert t0.(int) -// t3 = typeassert,ok t2.(T) -// -type TypeAssert struct { - register - X Value - AssertedType types.Type - CommaOk bool -} - -// The Extract instruction yields component Index of Tuple. -// -// This is used to access the results of instructions with multiple -// return values, such as Call, TypeAssert, Next, UnOp(ARROW) and -// IndexExpr(Map). -// -// Example printed form: -// t1 = extract t0 #1 -// -type Extract struct { - register - Tuple Value - Index int -} - -// Instructions executed for effect. They do not yield a value. -------------------- - -// The Jump instruction transfers control to the sole successor of its -// owning block. -// -// A Jump must be the last instruction of its containing BasicBlock. -// -// Pos() returns NoPos. -// -// Example printed form: -// jump done -// -type Jump struct { - anInstruction -} - -// The If instruction transfers control to one of the two successors -// of its owning block, depending on the boolean Cond: the first if -// true, the second if false. -// -// An If instruction must be the last instruction of its containing -// BasicBlock. -// -// Pos() returns NoPos. -// -// Example printed form: -// if t0 goto done else body -// -type If struct { - anInstruction - Cond Value -} - -// The Return instruction returns values and control back to the calling -// function. -// -// len(Results) is always equal to the number of results in the -// function's signature. -// -// If len(Results) > 1, Return returns a tuple value with the specified -// components which the caller must access using Extract instructions. -// -// There is no instruction to return a ready-made tuple like those -// returned by a "value,ok"-mode TypeAssert, Lookup or UnOp(ARROW) or -// a tail-call to a function with multiple result parameters. -// -// Return must be the last instruction of its containing BasicBlock. -// Such a block has no successors. -// -// Pos() returns the ast.ReturnStmt.Return, if explicit in the source. -// -// Example printed form: -// return -// return nil:I, 2:int -// -type Return struct { - anInstruction - Results []Value - pos token.Pos -} - -// The RunDefers instruction pops and invokes the entire stack of -// procedure calls pushed by Defer instructions in this function. -// -// It is legal to encounter multiple 'rundefers' instructions in a -// single control-flow path through a function; this is useful in -// the combined init() function, for example. -// -// Pos() returns NoPos. -// -// Example printed form: -// rundefers -// -type RunDefers struct { - anInstruction -} - -// The Panic instruction initiates a panic with value X. -// -// A Panic instruction must be the last instruction of its containing -// BasicBlock, which must have no successors. -// -// NB: 'go panic(x)' and 'defer panic(x)' do not use this instruction; -// they are treated as calls to a built-in function. -// -// Pos() returns the ast.CallExpr.Lparen if this panic was explicit -// in the source. -// -// Example printed form: -// panic t0 -// -type Panic struct { - anInstruction - X Value // an interface{} - pos token.Pos -} - -// The Go instruction creates a new goroutine and calls the specified -// function within it. -// -// See CallCommon for generic function call documentation. -// -// Pos() returns the ast.GoStmt.Go. -// -// Example printed form: -// go println(t0, t1) -// go t3() -// go invoke t5.Println(...t6) -// -type Go struct { - anInstruction - Call CallCommon - pos token.Pos -} - -// The Defer instruction pushes the specified call onto a stack of -// functions to be called by a RunDefers instruction or by a panic. -// -// See CallCommon for generic function call documentation. -// -// Pos() returns the ast.DeferStmt.Defer. -// -// Example printed form: -// defer println(t0, t1) -// defer t3() -// defer invoke t5.Println(...t6) -// -type Defer struct { - anInstruction - Call CallCommon - pos token.Pos -} - -// The Send instruction sends X on channel Chan. -// -// Pos() returns the ast.SendStmt.Arrow, if explicit in the source. -// -// Example printed form: -// send t0 <- t1 -// -type Send struct { - anInstruction - Chan, X Value - pos token.Pos -} - -// The Store instruction stores Val at address Addr. -// Stores can be of arbitrary types. -// -// Pos() returns the position of the source-level construct most closely -// associated with the memory store operation. -// Since implicit memory stores are numerous and varied and depend upon -// implementation choices, the details are not specified. -// -// Example printed form: -// *x = y -// -type Store struct { - anInstruction - Addr Value - Val Value - pos token.Pos -} - -// The MapUpdate instruction updates the association of Map[Key] to -// Value. -// -// Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack, -// if explicit in the source. -// -// Example printed form: -// t0[t1] = t2 -// -type MapUpdate struct { - anInstruction - Map Value - Key Value - Value Value - pos token.Pos -} - -// A DebugRef instruction maps a source-level expression Expr to the -// SSA value X that represents the value (!IsAddr) or address (IsAddr) -// of that expression. -// -// DebugRef is a pseudo-instruction: it has no dynamic effect. -// -// Pos() returns Expr.Pos(), the start position of the source-level -// expression. This is not the same as the "designated" token as -// documented at Value.Pos(). e.g. CallExpr.Pos() does not return the -// position of the ("designated") Lparen token. -// -// If Expr is an *ast.Ident denoting a var or func, Object() returns -// the object; though this information can be obtained from the type -// checker, including it here greatly facilitates debugging. -// For non-Ident expressions, Object() returns nil. -// -// DebugRefs are generated only for functions built with debugging -// enabled; see Package.SetDebugMode() and the GlobalDebug builder -// mode flag. -// -// DebugRefs are not emitted for ast.Idents referring to constants or -// predeclared identifiers, since they are trivial and numerous. -// Nor are they emitted for ast.ParenExprs. -// -// (By representing these as instructions, rather than out-of-band, -// consistency is maintained during transformation passes by the -// ordinary SSA renaming machinery.) -// -// Example printed form: -// ; *ast.CallExpr @ 102:9 is t5 -// ; var x float64 @ 109:72 is x -// ; address of *ast.CompositeLit @ 216:10 is t0 -// -type DebugRef struct { - anInstruction - Expr ast.Expr // the referring expression (never *ast.ParenExpr) - object types.Object // the identity of the source var/func - IsAddr bool // Expr is addressable and X is the address it denotes - X Value // the value or address of Expr -} - -// Embeddable mix-ins and helpers for common parts of other structs. ----------- - -// register is a mix-in embedded by all SSA values that are also -// instructions, i.e. virtual registers, and provides a uniform -// implementation of most of the Value interface: Value.Name() is a -// numbered register (e.g. "t0"); the other methods are field accessors. -// -// Temporary names are automatically assigned to each register on -// completion of building a function in SSA form. -// -// Clients must not assume that the 'id' value (and the Name() derived -// from it) is unique within a function. As always in this API, -// semantics are determined only by identity; names exist only to -// facilitate debugging. -// -type register struct { - anInstruction - num int // "name" of virtual register, e.g. "t0". Not guaranteed unique. - typ types.Type // type of virtual register - pos token.Pos // position of source expression, or NoPos - referrers []Instruction -} - -// anInstruction is a mix-in embedded by all Instructions. -// It provides the implementations of the Block and setBlock methods. -type anInstruction struct { - block *BasicBlock // the basic block of this instruction -} - -// CallCommon is contained by Go, Defer and Call to hold the -// common parts of a function or method call. -// -// Each CallCommon exists in one of two modes, function call and -// interface method invocation, or "call" and "invoke" for short. -// -// 1. "call" mode: when Method is nil (!IsInvoke), a CallCommon -// represents an ordinary function call of the value in Value, -// which may be a *Builtin, a *Function or any other value of kind -// 'func'. -// -// Value may be one of: -// (a) a *Function, indicating a statically dispatched call -// to a package-level function, an anonymous function, or -// a method of a named type. -// (b) a *MakeClosure, indicating an immediately applied -// function literal with free variables. -// (c) a *Builtin, indicating a statically dispatched call -// to a built-in function. -// (d) any other value, indicating a dynamically dispatched -// function call. -// StaticCallee returns the identity of the callee in cases -// (a) and (b), nil otherwise. -// -// Args contains the arguments to the call. If Value is a method, -// Args[0] contains the receiver parameter. -// -// Example printed form: -// t2 = println(t0, t1) -// go t3() -// defer t5(...t6) -// -// 2. "invoke" mode: when Method is non-nil (IsInvoke), a CallCommon -// represents a dynamically dispatched call to an interface method. -// In this mode, Value is the interface value and Method is the -// interface's abstract method. Note: an abstract method may be -// shared by multiple interfaces due to embedding; Value.Type() -// provides the specific interface used for this call. -// -// Value is implicitly supplied to the concrete method implementation -// as the receiver parameter; in other words, Args[0] holds not the -// receiver but the first true argument. -// -// Example printed form: -// t1 = invoke t0.String() -// go invoke t3.Run(t2) -// defer invoke t4.Handle(...t5) -// -// For all calls to variadic functions (Signature().Variadic()), -// the last element of Args is a slice. -// -type CallCommon struct { - Value Value // receiver (invoke mode) or func value (call mode) - Method *types.Func // abstract method (invoke mode) - Args []Value // actual parameters (in static method call, includes receiver) - pos token.Pos // position of CallExpr.Lparen, iff explicit in source -} - -// IsInvoke returns true if this call has "invoke" (not "call") mode. -func (c *CallCommon) IsInvoke() bool { - return c.Method != nil -} - -func (c *CallCommon) Pos() token.Pos { return c.pos } - -// Signature returns the signature of the called function. -// -// For an "invoke"-mode call, the signature of the interface method is -// returned. -// -// In either "call" or "invoke" mode, if the callee is a method, its -// receiver is represented by sig.Recv, not sig.Params().At(0). -// -func (c *CallCommon) Signature() *types.Signature { - if c.Method != nil { - return c.Method.Type().(*types.Signature) - } - return c.Value.Type().Underlying().(*types.Signature) -} - -// StaticCallee returns the callee if this is a trivially static -// "call"-mode call to a function. -func (c *CallCommon) StaticCallee() *Function { - switch fn := c.Value.(type) { - case *Function: - return fn - case *MakeClosure: - return fn.Fn.(*Function) - } - return nil -} - -// Description returns a description of the mode of this call suitable -// for a user interface, e.g., "static method call". -func (c *CallCommon) Description() string { - switch fn := c.Value.(type) { - case *Builtin: - return "built-in function call" - case *MakeClosure: - return "static function closure call" - case *Function: - if fn.Signature.Recv() != nil { - return "static method call" - } - return "static function call" - } - if c.IsInvoke() { - return "dynamic method call" // ("invoke" mode) - } - return "dynamic function call" -} - -// The CallInstruction interface, implemented by *Go, *Defer and *Call, -// exposes the common parts of function-calling instructions, -// yet provides a way back to the Value defined by *Call alone. -// -type CallInstruction interface { - Instruction - Common() *CallCommon // returns the common parts of the call - Value() *Call // returns the result value of the call (*Call) or nil (*Go, *Defer) -} - -func (s *Call) Common() *CallCommon { return &s.Call } -func (s *Defer) Common() *CallCommon { return &s.Call } -func (s *Go) Common() *CallCommon { return &s.Call } - -func (s *Call) Value() *Call { return s } -func (s *Defer) Value() *Call { return nil } -func (s *Go) Value() *Call { return nil } - -func (v *Builtin) Type() types.Type { return v.sig } -func (v *Builtin) Name() string { return v.name } -func (*Builtin) Referrers() *[]Instruction { return nil } -func (v *Builtin) Pos() token.Pos { return token.NoPos } -func (v *Builtin) Object() types.Object { return types.Universe.Lookup(v.name) } -func (v *Builtin) Parent() *Function { return nil } - -func (v *FreeVar) Type() types.Type { return v.typ } -func (v *FreeVar) Name() string { return v.name } -func (v *FreeVar) Referrers() *[]Instruction { return &v.referrers } -func (v *FreeVar) Pos() token.Pos { return v.pos } -func (v *FreeVar) Parent() *Function { return v.parent } - -func (v *Global) Type() types.Type { return v.typ } -func (v *Global) Name() string { return v.name } -func (v *Global) Parent() *Function { return nil } -func (v *Global) Pos() token.Pos { return v.pos } -func (v *Global) Referrers() *[]Instruction { return nil } -func (v *Global) Token() token.Token { return token.VAR } -func (v *Global) Object() types.Object { return v.object } -func (v *Global) String() string { return v.RelString(nil) } -func (v *Global) Package() *Package { return v.Pkg } -func (v *Global) RelString(from *types.Package) string { return relString(v, from) } - -func (v *Function) Name() string { return v.name } -func (v *Function) Type() types.Type { return v.Signature } -func (v *Function) Pos() token.Pos { return v.pos } -func (v *Function) Token() token.Token { return token.FUNC } -func (v *Function) Object() types.Object { return v.object } -func (v *Function) String() string { return v.RelString(nil) } -func (v *Function) Package() *Package { return v.Pkg } -func (v *Function) Parent() *Function { return v.parent } -func (v *Function) Referrers() *[]Instruction { - if v.parent != nil { - return &v.referrers - } - return nil -} - -func (v *Parameter) Type() types.Type { return v.typ } -func (v *Parameter) Name() string { return v.name } -func (v *Parameter) Object() types.Object { return v.object } -func (v *Parameter) Referrers() *[]Instruction { return &v.referrers } -func (v *Parameter) Pos() token.Pos { return v.pos } -func (v *Parameter) Parent() *Function { return v.parent } - -func (v *Alloc) Type() types.Type { return v.typ } -func (v *Alloc) Referrers() *[]Instruction { return &v.referrers } -func (v *Alloc) Pos() token.Pos { return v.pos } - -func (v *register) Type() types.Type { return v.typ } -func (v *register) setType(typ types.Type) { v.typ = typ } -func (v *register) Name() string { return fmt.Sprintf("t%d", v.num) } -func (v *register) setNum(num int) { v.num = num } -func (v *register) Referrers() *[]Instruction { return &v.referrers } -func (v *register) Pos() token.Pos { return v.pos } -func (v *register) setPos(pos token.Pos) { v.pos = pos } - -func (v *anInstruction) Parent() *Function { return v.block.parent } -func (v *anInstruction) Block() *BasicBlock { return v.block } -func (v *anInstruction) setBlock(block *BasicBlock) { v.block = block } -func (v *anInstruction) Referrers() *[]Instruction { return nil } - -func (t *Type) Name() string { return t.object.Name() } -func (t *Type) Pos() token.Pos { return t.object.Pos() } -func (t *Type) Type() types.Type { return t.object.Type() } -func (t *Type) Token() token.Token { return token.TYPE } -func (t *Type) Object() types.Object { return t.object } -func (t *Type) String() string { return t.RelString(nil) } -func (t *Type) Package() *Package { return t.pkg } -func (t *Type) RelString(from *types.Package) string { return relString(t, from) } - -func (c *NamedConst) Name() string { return c.object.Name() } -func (c *NamedConst) Pos() token.Pos { return c.object.Pos() } -func (c *NamedConst) String() string { return c.RelString(nil) } -func (c *NamedConst) Type() types.Type { return c.object.Type() } -func (c *NamedConst) Token() token.Token { return token.CONST } -func (c *NamedConst) Object() types.Object { return c.object } -func (c *NamedConst) Package() *Package { return c.pkg } -func (c *NamedConst) RelString(from *types.Package) string { return relString(c, from) } - -// Func returns the package-level function of the specified name, -// or nil if not found. -// -func (p *Package) Func(name string) (f *Function) { - f, _ = p.Members[name].(*Function) - return -} - -// Var returns the package-level variable of the specified name, -// or nil if not found. -// -func (p *Package) Var(name string) (g *Global) { - g, _ = p.Members[name].(*Global) - return -} - -// Const returns the package-level constant of the specified name, -// or nil if not found. -// -func (p *Package) Const(name string) (c *NamedConst) { - c, _ = p.Members[name].(*NamedConst) - return -} - -// Type returns the package-level type of the specified name, -// or nil if not found. -// -func (p *Package) Type(name string) (t *Type) { - t, _ = p.Members[name].(*Type) - return -} - -func (v *Call) Pos() token.Pos { return v.Call.pos } -func (s *Defer) Pos() token.Pos { return s.pos } -func (s *Go) Pos() token.Pos { return s.pos } -func (s *MapUpdate) Pos() token.Pos { return s.pos } -func (s *Panic) Pos() token.Pos { return s.pos } -func (s *Return) Pos() token.Pos { return s.pos } -func (s *Send) Pos() token.Pos { return s.pos } -func (s *Store) Pos() token.Pos { return s.pos } -func (s *If) Pos() token.Pos { return token.NoPos } -func (s *Jump) Pos() token.Pos { return token.NoPos } -func (s *RunDefers) Pos() token.Pos { return token.NoPos } -func (s *DebugRef) Pos() token.Pos { return s.Expr.Pos() } - -// Operands. - -func (v *Alloc) Operands(rands []*Value) []*Value { - return rands -} - -func (v *BinOp) Operands(rands []*Value) []*Value { - return append(rands, &v.X, &v.Y) -} - -func (c *CallCommon) Operands(rands []*Value) []*Value { - rands = append(rands, &c.Value) - for i := range c.Args { - rands = append(rands, &c.Args[i]) - } - return rands -} - -func (s *Go) Operands(rands []*Value) []*Value { - return s.Call.Operands(rands) -} - -func (s *Call) Operands(rands []*Value) []*Value { - return s.Call.Operands(rands) -} - -func (s *Defer) Operands(rands []*Value) []*Value { - return s.Call.Operands(rands) -} - -func (v *ChangeInterface) Operands(rands []*Value) []*Value { - return append(rands, &v.X) -} - -func (v *ChangeType) Operands(rands []*Value) []*Value { - return append(rands, &v.X) -} - -func (v *Convert) Operands(rands []*Value) []*Value { - return append(rands, &v.X) -} - -func (s *DebugRef) Operands(rands []*Value) []*Value { - return append(rands, &s.X) -} - -func (v *Extract) Operands(rands []*Value) []*Value { - return append(rands, &v.Tuple) -} - -func (v *Field) Operands(rands []*Value) []*Value { - return append(rands, &v.X) -} - -func (v *FieldAddr) Operands(rands []*Value) []*Value { - return append(rands, &v.X) -} - -func (s *If) Operands(rands []*Value) []*Value { - return append(rands, &s.Cond) -} - -func (v *Index) Operands(rands []*Value) []*Value { - return append(rands, &v.X, &v.Index) -} - -func (v *IndexAddr) Operands(rands []*Value) []*Value { - return append(rands, &v.X, &v.Index) -} - -func (*Jump) Operands(rands []*Value) []*Value { - return rands -} - -func (v *Lookup) Operands(rands []*Value) []*Value { - return append(rands, &v.X, &v.Index) -} - -func (v *MakeChan) Operands(rands []*Value) []*Value { - return append(rands, &v.Size) -} - -func (v *MakeClosure) Operands(rands []*Value) []*Value { - rands = append(rands, &v.Fn) - for i := range v.Bindings { - rands = append(rands, &v.Bindings[i]) - } - return rands -} - -func (v *MakeInterface) Operands(rands []*Value) []*Value { - return append(rands, &v.X) -} - -func (v *MakeMap) Operands(rands []*Value) []*Value { - return append(rands, &v.Reserve) -} - -func (v *MakeSlice) Operands(rands []*Value) []*Value { - return append(rands, &v.Len, &v.Cap) -} - -func (v *MapUpdate) Operands(rands []*Value) []*Value { - return append(rands, &v.Map, &v.Key, &v.Value) -} - -func (v *Next) Operands(rands []*Value) []*Value { - return append(rands, &v.Iter) -} - -func (s *Panic) Operands(rands []*Value) []*Value { - return append(rands, &s.X) -} - -func (v *Phi) Operands(rands []*Value) []*Value { - for i := range v.Edges { - rands = append(rands, &v.Edges[i]) - } - return rands -} - -func (v *Range) Operands(rands []*Value) []*Value { - return append(rands, &v.X) -} - -func (s *Return) Operands(rands []*Value) []*Value { - for i := range s.Results { - rands = append(rands, &s.Results[i]) - } - return rands -} - -func (*RunDefers) Operands(rands []*Value) []*Value { - return rands -} - -func (v *Select) Operands(rands []*Value) []*Value { - for i := range v.States { - rands = append(rands, &v.States[i].Chan, &v.States[i].Send) - } - return rands -} - -func (s *Send) Operands(rands []*Value) []*Value { - return append(rands, &s.Chan, &s.X) -} - -func (v *Slice) Operands(rands []*Value) []*Value { - return append(rands, &v.X, &v.Low, &v.High, &v.Max) -} - -func (s *Store) Operands(rands []*Value) []*Value { - return append(rands, &s.Addr, &s.Val) -} - -func (v *TypeAssert) Operands(rands []*Value) []*Value { - return append(rands, &v.X) -} - -func (v *UnOp) Operands(rands []*Value) []*Value { - return append(rands, &v.X) -} - -// Non-Instruction Values: -func (v *Builtin) Operands(rands []*Value) []*Value { return rands } -func (v *FreeVar) Operands(rands []*Value) []*Value { return rands } -func (v *Const) Operands(rands []*Value) []*Value { return rands } -func (v *Function) Operands(rands []*Value) []*Value { return rands } -func (v *Global) Operands(rands []*Value) []*Value { return rands } -func (v *Parameter) Operands(rands []*Value) []*Value { return rands } diff --git a/vendor/golang.org/x/tools/go/ssa/ssautil/load.go b/vendor/golang.org/x/tools/go/ssa/ssautil/load.go deleted file mode 100644 index 6d65b78ef28..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/ssautil/load.go +++ /dev/null @@ -1,175 +0,0 @@ -// Copyright 2015 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssautil - -// This file defines utility functions for constructing programs in SSA form. - -import ( - "go/ast" - "go/token" - "go/types" - - "golang.org/x/tools/go/loader" - "golang.org/x/tools/go/packages" - "golang.org/x/tools/go/ssa" -) - -// Packages creates an SSA program for a set of packages. -// -// The packages must have been loaded from source syntax using the -// golang.org/x/tools/go/packages.Load function in LoadSyntax or -// LoadAllSyntax mode. -// -// Packages creates an SSA package for each well-typed package in the -// initial list, plus all their dependencies. The resulting list of -// packages corresponds to the list of initial packages, and may contain -// a nil if SSA code could not be constructed for the corresponding initial -// package due to type errors. -// -// Code for bodies of functions is not built until Build is called on -// the resulting Program. SSA code is constructed only for the initial -// packages with well-typed syntax trees. -// -// The mode parameter controls diagnostics and checking during SSA construction. -// -func Packages(initial []*packages.Package, mode ssa.BuilderMode) (*ssa.Program, []*ssa.Package) { - return doPackages(initial, mode, false) -} - -// AllPackages creates an SSA program for a set of packages plus all -// their dependencies. -// -// The packages must have been loaded from source syntax using the -// golang.org/x/tools/go/packages.Load function in LoadAllSyntax mode. -// -// AllPackages creates an SSA package for each well-typed package in the -// initial list, plus all their dependencies. The resulting list of -// packages corresponds to the list of initial packages, and may contain -// a nil if SSA code could not be constructed for the corresponding -// initial package due to type errors. -// -// Code for bodies of functions is not built until Build is called on -// the resulting Program. SSA code is constructed for all packages with -// well-typed syntax trees. -// -// The mode parameter controls diagnostics and checking during SSA construction. -// -func AllPackages(initial []*packages.Package, mode ssa.BuilderMode) (*ssa.Program, []*ssa.Package) { - return doPackages(initial, mode, true) -} - -func doPackages(initial []*packages.Package, mode ssa.BuilderMode, deps bool) (*ssa.Program, []*ssa.Package) { - - var fset *token.FileSet - if len(initial) > 0 { - fset = initial[0].Fset - } - - prog := ssa.NewProgram(fset, mode) - - isInitial := make(map[*packages.Package]bool, len(initial)) - for _, p := range initial { - isInitial[p] = true - } - - ssamap := make(map[*packages.Package]*ssa.Package) - packages.Visit(initial, nil, func(p *packages.Package) { - if p.Types != nil && !p.IllTyped { - var files []*ast.File - if deps || isInitial[p] { - files = p.Syntax - } - ssamap[p] = prog.CreatePackage(p.Types, files, p.TypesInfo, true) - } - }) - - var ssapkgs []*ssa.Package - for _, p := range initial { - ssapkgs = append(ssapkgs, ssamap[p]) // may be nil - } - return prog, ssapkgs -} - -// CreateProgram returns a new program in SSA form, given a program -// loaded from source. An SSA package is created for each transitively -// error-free package of lprog. -// -// Code for bodies of functions is not built until Build is called -// on the result. -// -// The mode parameter controls diagnostics and checking during SSA construction. -// -// Deprecated: Use golang.org/x/tools/go/packages and the Packages -// function instead; see ssa.ExampleLoadPackages. -// -func CreateProgram(lprog *loader.Program, mode ssa.BuilderMode) *ssa.Program { - prog := ssa.NewProgram(lprog.Fset, mode) - - for _, info := range lprog.AllPackages { - if info.TransitivelyErrorFree { - prog.CreatePackage(info.Pkg, info.Files, &info.Info, info.Importable) - } - } - - return prog -} - -// BuildPackage builds an SSA program with IR for a single package. -// -// It populates pkg by type-checking the specified file ASTs. All -// dependencies are loaded using the importer specified by tc, which -// typically loads compiler export data; SSA code cannot be built for -// those packages. BuildPackage then constructs an ssa.Program with all -// dependency packages created, and builds and returns the SSA package -// corresponding to pkg. -// -// The caller must have set pkg.Path() to the import path. -// -// The operation fails if there were any type-checking or import errors. -// -// See ../ssa/example_test.go for an example. -// -func BuildPackage(tc *types.Config, fset *token.FileSet, pkg *types.Package, files []*ast.File, mode ssa.BuilderMode) (*ssa.Package, *types.Info, error) { - if fset == nil { - panic("no token.FileSet") - } - if pkg.Path() == "" { - panic("package has no import path") - } - - info := &types.Info{ - Types: make(map[ast.Expr]types.TypeAndValue), - Defs: make(map[*ast.Ident]types.Object), - Uses: make(map[*ast.Ident]types.Object), - Implicits: make(map[ast.Node]types.Object), - Scopes: make(map[ast.Node]*types.Scope), - Selections: make(map[*ast.SelectorExpr]*types.Selection), - } - if err := types.NewChecker(tc, fset, pkg, info).Files(files); err != nil { - return nil, nil, err - } - - prog := ssa.NewProgram(fset, mode) - - // Create SSA packages for all imports. - // Order is not significant. - created := make(map[*types.Package]bool) - var createAll func(pkgs []*types.Package) - createAll = func(pkgs []*types.Package) { - for _, p := range pkgs { - if !created[p] { - created[p] = true - prog.CreatePackage(p, nil, nil, true) - createAll(p.Imports()) - } - } - } - createAll(pkg.Imports()) - - // Create and build the primary package. - ssapkg := prog.CreatePackage(pkg, files, info, false) - ssapkg.Build() - return ssapkg, info, nil -} diff --git a/vendor/golang.org/x/tools/go/ssa/ssautil/switch.go b/vendor/golang.org/x/tools/go/ssa/ssautil/switch.go deleted file mode 100644 index db03bf55590..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/ssautil/switch.go +++ /dev/null @@ -1,234 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssautil - -// This file implements discovery of switch and type-switch constructs -// from low-level control flow. -// -// Many techniques exist for compiling a high-level switch with -// constant cases to efficient machine code. The optimal choice will -// depend on the data type, the specific case values, the code in the -// body of each case, and the hardware. -// Some examples: -// - a lookup table (for a switch that maps constants to constants) -// - a computed goto -// - a binary tree -// - a perfect hash -// - a two-level switch (to partition constant strings by their first byte). - -import ( - "bytes" - "fmt" - "go/token" - "go/types" - - "golang.org/x/tools/go/ssa" -) - -// A ConstCase represents a single constant comparison. -// It is part of a Switch. -type ConstCase struct { - Block *ssa.BasicBlock // block performing the comparison - Body *ssa.BasicBlock // body of the case - Value *ssa.Const // case comparand -} - -// A TypeCase represents a single type assertion. -// It is part of a Switch. -type TypeCase struct { - Block *ssa.BasicBlock // block performing the type assert - Body *ssa.BasicBlock // body of the case - Type types.Type // case type - Binding ssa.Value // value bound by this case -} - -// A Switch is a logical high-level control flow operation -// (a multiway branch) discovered by analysis of a CFG containing -// only if/else chains. It is not part of the ssa.Instruction set. -// -// One of ConstCases and TypeCases has length >= 2; -// the other is nil. -// -// In a value switch, the list of cases may contain duplicate constants. -// A type switch may contain duplicate types, or types assignable -// to an interface type also in the list. -// TODO(adonovan): eliminate such duplicates. -// -type Switch struct { - Start *ssa.BasicBlock // block containing start of if/else chain - X ssa.Value // the switch operand - ConstCases []ConstCase // ordered list of constant comparisons - TypeCases []TypeCase // ordered list of type assertions - Default *ssa.BasicBlock // successor if all comparisons fail -} - -func (sw *Switch) String() string { - // We represent each block by the String() of its - // first Instruction, e.g. "print(42:int)". - var buf bytes.Buffer - if sw.ConstCases != nil { - fmt.Fprintf(&buf, "switch %s {\n", sw.X.Name()) - for _, c := range sw.ConstCases { - fmt.Fprintf(&buf, "case %s: %s\n", c.Value, c.Body.Instrs[0]) - } - } else { - fmt.Fprintf(&buf, "switch %s.(type) {\n", sw.X.Name()) - for _, c := range sw.TypeCases { - fmt.Fprintf(&buf, "case %s %s: %s\n", - c.Binding.Name(), c.Type, c.Body.Instrs[0]) - } - } - if sw.Default != nil { - fmt.Fprintf(&buf, "default: %s\n", sw.Default.Instrs[0]) - } - fmt.Fprintf(&buf, "}") - return buf.String() -} - -// Switches examines the control-flow graph of fn and returns the -// set of inferred value and type switches. A value switch tests an -// ssa.Value for equality against two or more compile-time constant -// values. Switches involving link-time constants (addresses) are -// ignored. A type switch type-asserts an ssa.Value against two or -// more types. -// -// The switches are returned in dominance order. -// -// The resulting switches do not necessarily correspond to uses of the -// 'switch' keyword in the source: for example, a single source-level -// switch statement with non-constant cases may result in zero, one or -// many Switches, one per plural sequence of constant cases. -// Switches may even be inferred from if/else- or goto-based control flow. -// (In general, the control flow constructs of the source program -// cannot be faithfully reproduced from the SSA representation.) -// -func Switches(fn *ssa.Function) []Switch { - // Traverse the CFG in dominance order, so we don't - // enter an if/else-chain in the middle. - var switches []Switch - seen := make(map[*ssa.BasicBlock]bool) // TODO(adonovan): opt: use ssa.blockSet - for _, b := range fn.DomPreorder() { - if x, k := isComparisonBlock(b); x != nil { - // Block b starts a switch. - sw := Switch{Start: b, X: x} - valueSwitch(&sw, k, seen) - if len(sw.ConstCases) > 1 { - switches = append(switches, sw) - } - } - - if y, x, T := isTypeAssertBlock(b); y != nil { - // Block b starts a type switch. - sw := Switch{Start: b, X: x} - typeSwitch(&sw, y, T, seen) - if len(sw.TypeCases) > 1 { - switches = append(switches, sw) - } - } - } - return switches -} - -func valueSwitch(sw *Switch, k *ssa.Const, seen map[*ssa.BasicBlock]bool) { - b := sw.Start - x := sw.X - for x == sw.X { - if seen[b] { - break - } - seen[b] = true - - sw.ConstCases = append(sw.ConstCases, ConstCase{ - Block: b, - Body: b.Succs[0], - Value: k, - }) - b = b.Succs[1] - if len(b.Instrs) > 2 { - // Block b contains not just 'if x == k', - // so it may have side effects that - // make it unsafe to elide. - break - } - if len(b.Preds) != 1 { - // Block b has multiple predecessors, - // so it cannot be treated as a case. - break - } - x, k = isComparisonBlock(b) - } - sw.Default = b -} - -func typeSwitch(sw *Switch, y ssa.Value, T types.Type, seen map[*ssa.BasicBlock]bool) { - b := sw.Start - x := sw.X - for x == sw.X { - if seen[b] { - break - } - seen[b] = true - - sw.TypeCases = append(sw.TypeCases, TypeCase{ - Block: b, - Body: b.Succs[0], - Type: T, - Binding: y, - }) - b = b.Succs[1] - if len(b.Instrs) > 4 { - // Block b contains not just - // {TypeAssert; Extract #0; Extract #1; If} - // so it may have side effects that - // make it unsafe to elide. - break - } - if len(b.Preds) != 1 { - // Block b has multiple predecessors, - // so it cannot be treated as a case. - break - } - y, x, T = isTypeAssertBlock(b) - } - sw.Default = b -} - -// isComparisonBlock returns the operands (v, k) if a block ends with -// a comparison v==k, where k is a compile-time constant. -// -func isComparisonBlock(b *ssa.BasicBlock) (v ssa.Value, k *ssa.Const) { - if n := len(b.Instrs); n >= 2 { - if i, ok := b.Instrs[n-1].(*ssa.If); ok { - if binop, ok := i.Cond.(*ssa.BinOp); ok && binop.Block() == b && binop.Op == token.EQL { - if k, ok := binop.Y.(*ssa.Const); ok { - return binop.X, k - } - if k, ok := binop.X.(*ssa.Const); ok { - return binop.Y, k - } - } - } - } - return -} - -// isTypeAssertBlock returns the operands (y, x, T) if a block ends with -// a type assertion "if y, ok := x.(T); ok {". -// -func isTypeAssertBlock(b *ssa.BasicBlock) (y, x ssa.Value, T types.Type) { - if n := len(b.Instrs); n >= 4 { - if i, ok := b.Instrs[n-1].(*ssa.If); ok { - if ext1, ok := i.Cond.(*ssa.Extract); ok && ext1.Block() == b && ext1.Index == 1 { - if ta, ok := ext1.Tuple.(*ssa.TypeAssert); ok && ta.Block() == b { - // hack: relies upon instruction ordering. - if ext0, ok := b.Instrs[n-3].(*ssa.Extract); ok { - return ext0, ta.X, ta.AssertedType - } - } - } - } - } - return -} diff --git a/vendor/golang.org/x/tools/go/ssa/ssautil/visit.go b/vendor/golang.org/x/tools/go/ssa/ssautil/visit.go deleted file mode 100644 index 3424e8a3086..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/ssautil/visit.go +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssautil // import "golang.org/x/tools/go/ssa/ssautil" - -import "golang.org/x/tools/go/ssa" - -// This file defines utilities for visiting the SSA representation of -// a Program. -// -// TODO(adonovan): test coverage. - -// AllFunctions finds and returns the set of functions potentially -// needed by program prog, as determined by a simple linker-style -// reachability algorithm starting from the members and method-sets of -// each package. The result may include anonymous functions and -// synthetic wrappers. -// -// Precondition: all packages are built. -// -func AllFunctions(prog *ssa.Program) map[*ssa.Function]bool { - visit := visitor{ - prog: prog, - seen: make(map[*ssa.Function]bool), - } - visit.program() - return visit.seen -} - -type visitor struct { - prog *ssa.Program - seen map[*ssa.Function]bool -} - -func (visit *visitor) program() { - for _, pkg := range visit.prog.AllPackages() { - for _, mem := range pkg.Members { - if fn, ok := mem.(*ssa.Function); ok { - visit.function(fn) - } - } - } - for _, T := range visit.prog.RuntimeTypes() { - mset := visit.prog.MethodSets.MethodSet(T) - for i, n := 0, mset.Len(); i < n; i++ { - visit.function(visit.prog.MethodValue(mset.At(i))) - } - } -} - -func (visit *visitor) function(fn *ssa.Function) { - if !visit.seen[fn] { - visit.seen[fn] = true - var buf [10]*ssa.Value // avoid alloc in common case - for _, b := range fn.Blocks { - for _, instr := range b.Instrs { - for _, op := range instr.Operands(buf[:0]) { - if fn, ok := (*op).(*ssa.Function); ok { - visit.function(fn) - } - } - } - } - } -} - -// MainPackages returns the subset of the specified packages -// named "main" that define a main function. -// The result may include synthetic "testmain" packages. -func MainPackages(pkgs []*ssa.Package) []*ssa.Package { - var mains []*ssa.Package - for _, pkg := range pkgs { - if pkg.Pkg.Name() == "main" && pkg.Func("main") != nil { - mains = append(mains, pkg) - } - } - return mains -} diff --git a/vendor/golang.org/x/tools/go/ssa/testmain.go b/vendor/golang.org/x/tools/go/ssa/testmain.go deleted file mode 100644 index 4bf8d98f3a6..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/testmain.go +++ /dev/null @@ -1,273 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// CreateTestMainPackage synthesizes a main package that runs all the -// tests of the supplied packages. -// It is closely coupled to $GOROOT/src/cmd/go/test.go and $GOROOT/src/testing. -// -// TODO(adonovan): throws this all away now that x/tools/go/packages -// provides access to the actual synthetic test main files. - -import ( - "bytes" - "fmt" - "go/ast" - "go/parser" - "go/types" - "log" - "os" - "strings" - "text/template" -) - -// FindTests returns the Test, Benchmark, and Example functions -// (as defined by "go test") defined in the specified package, -// and its TestMain function, if any. -// -// Deprecated: Use golang.org/x/tools/go/packages to access synthetic -// testmain packages. -func FindTests(pkg *Package) (tests, benchmarks, examples []*Function, main *Function) { - prog := pkg.Prog - - // The first two of these may be nil: if the program doesn't import "testing", - // it can't contain any tests, but it may yet contain Examples. - var testSig *types.Signature // func(*testing.T) - var benchmarkSig *types.Signature // func(*testing.B) - var exampleSig = types.NewSignature(nil, nil, nil, false) // func() - - // Obtain the types from the parameters of testing.MainStart. - if testingPkg := prog.ImportedPackage("testing"); testingPkg != nil { - mainStart := testingPkg.Func("MainStart") - params := mainStart.Signature.Params() - testSig = funcField(params.At(1).Type()) - benchmarkSig = funcField(params.At(2).Type()) - - // Does the package define this function? - // func TestMain(*testing.M) - if f := pkg.Func("TestMain"); f != nil { - sig := f.Type().(*types.Signature) - starM := mainStart.Signature.Results().At(0).Type() // *testing.M - if sig.Results().Len() == 0 && - sig.Params().Len() == 1 && - types.Identical(sig.Params().At(0).Type(), starM) { - main = f - } - } - } - - // TODO(adonovan): use a stable order, e.g. lexical. - for _, mem := range pkg.Members { - if f, ok := mem.(*Function); ok && - ast.IsExported(f.Name()) && - strings.HasSuffix(prog.Fset.Position(f.Pos()).Filename, "_test.go") { - - switch { - case testSig != nil && isTestSig(f, "Test", testSig): - tests = append(tests, f) - case benchmarkSig != nil && isTestSig(f, "Benchmark", benchmarkSig): - benchmarks = append(benchmarks, f) - case isTestSig(f, "Example", exampleSig): - examples = append(examples, f) - default: - continue - } - } - } - return -} - -// Like isTest, but checks the signature too. -func isTestSig(f *Function, prefix string, sig *types.Signature) bool { - return isTest(f.Name(), prefix) && types.Identical(f.Signature, sig) -} - -// Given the type of one of the three slice parameters of testing.Main, -// returns the function type. -func funcField(slice types.Type) *types.Signature { - return slice.(*types.Slice).Elem().Underlying().(*types.Struct).Field(1).Type().(*types.Signature) -} - -// isTest tells whether name looks like a test (or benchmark, according to prefix). -// It is a Test (say) if there is a character after Test that is not a lower-case letter. -// We don't want TesticularCancer. -// Plundered from $GOROOT/src/cmd/go/test.go -func isTest(name, prefix string) bool { - if !strings.HasPrefix(name, prefix) { - return false - } - if len(name) == len(prefix) { // "Test" is ok - return true - } - return ast.IsExported(name[len(prefix):]) -} - -// CreateTestMainPackage creates and returns a synthetic "testmain" -// package for the specified package if it defines tests, benchmarks or -// executable examples, or nil otherwise. The new package is named -// "main" and provides a function named "main" that runs the tests, -// similar to the one that would be created by the 'go test' tool. -// -// Subsequent calls to prog.AllPackages include the new package. -// The package pkg must belong to the program prog. -// -// Deprecated: Use golang.org/x/tools/go/packages to access synthetic -// testmain packages. -func (prog *Program) CreateTestMainPackage(pkg *Package) *Package { - if pkg.Prog != prog { - log.Fatal("Package does not belong to Program") - } - - // Template data - var data struct { - Pkg *Package - Tests, Benchmarks, Examples []*Function - Main *Function - Go18 bool - } - data.Pkg = pkg - - // Enumerate tests. - data.Tests, data.Benchmarks, data.Examples, data.Main = FindTests(pkg) - if data.Main == nil && - data.Tests == nil && data.Benchmarks == nil && data.Examples == nil { - return nil - } - - // Synthesize source for testmain package. - path := pkg.Pkg.Path() + "$testmain" - tmpl := testmainTmpl - if testingPkg := prog.ImportedPackage("testing"); testingPkg != nil { - // In Go 1.8, testing.MainStart's first argument is an interface, not a func. - data.Go18 = types.IsInterface(testingPkg.Func("MainStart").Signature.Params().At(0).Type()) - } else { - // The program does not import "testing", but FindTests - // returned non-nil, which must mean there were Examples - // but no Test, Benchmark, or TestMain functions. - - // We'll simply call them from testmain.main; this will - // ensure they don't panic, but will not check any - // "Output:" comments. - // (We should not execute an Example that has no - // "Output:" comment, but it's impossible to tell here.) - tmpl = examplesOnlyTmpl - } - var buf bytes.Buffer - if err := tmpl.Execute(&buf, data); err != nil { - log.Fatalf("internal error expanding template for %s: %v", path, err) - } - if false { // debugging - fmt.Fprintln(os.Stderr, buf.String()) - } - - // Parse and type-check the testmain package. - f, err := parser.ParseFile(prog.Fset, path+".go", &buf, parser.Mode(0)) - if err != nil { - log.Fatalf("internal error parsing %s: %v", path, err) - } - conf := types.Config{ - DisableUnusedImportCheck: true, - Importer: importer{pkg}, - } - files := []*ast.File{f} - info := &types.Info{ - Types: make(map[ast.Expr]types.TypeAndValue), - Defs: make(map[*ast.Ident]types.Object), - Uses: make(map[*ast.Ident]types.Object), - Implicits: make(map[ast.Node]types.Object), - Scopes: make(map[ast.Node]*types.Scope), - Selections: make(map[*ast.SelectorExpr]*types.Selection), - } - testmainPkg, err := conf.Check(path, prog.Fset, files, info) - if err != nil { - log.Fatalf("internal error type-checking %s: %v", path, err) - } - - // Create and build SSA code. - testmain := prog.CreatePackage(testmainPkg, files, info, false) - testmain.SetDebugMode(false) - testmain.Build() - testmain.Func("main").Synthetic = "test main function" - testmain.Func("init").Synthetic = "package initializer" - return testmain -} - -// An implementation of types.Importer for an already loaded SSA program. -type importer struct { - pkg *Package // package under test; may be non-importable -} - -func (imp importer) Import(path string) (*types.Package, error) { - if p := imp.pkg.Prog.ImportedPackage(path); p != nil { - return p.Pkg, nil - } - if path == imp.pkg.Pkg.Path() { - return imp.pkg.Pkg, nil - } - return nil, fmt.Errorf("not found") // can't happen -} - -var testmainTmpl = template.Must(template.New("testmain").Parse(` -package main - -import "io" -import "os" -import "testing" -import p {{printf "%q" .Pkg.Pkg.Path}} - -{{if .Go18}} -type deps struct{} - -func (deps) ImportPath() string { return "" } -func (deps) MatchString(pat, str string) (bool, error) { return true, nil } -func (deps) StartCPUProfile(io.Writer) error { return nil } -func (deps) StartTestLog(io.Writer) {} -func (deps) StopCPUProfile() {} -func (deps) StopTestLog() error { return nil } -func (deps) WriteHeapProfile(io.Writer) error { return nil } -func (deps) WriteProfileTo(string, io.Writer, int) error { return nil } - -var match deps -{{else}} -func match(_, _ string) (bool, error) { return true, nil } -{{end}} - -func main() { - tests := []testing.InternalTest{ -{{range .Tests}} - { {{printf "%q" .Name}}, p.{{.Name}} }, -{{end}} - } - benchmarks := []testing.InternalBenchmark{ -{{range .Benchmarks}} - { {{printf "%q" .Name}}, p.{{.Name}} }, -{{end}} - } - examples := []testing.InternalExample{ -{{range .Examples}} - {Name: {{printf "%q" .Name}}, F: p.{{.Name}}}, -{{end}} - } - m := testing.MainStart(match, tests, benchmarks, examples) -{{with .Main}} - p.{{.Name}}(m) -{{else}} - os.Exit(m.Run()) -{{end}} -} - -`)) - -var examplesOnlyTmpl = template.Must(template.New("examples").Parse(` -package main - -import p {{printf "%q" .Pkg.Pkg.Path}} - -func main() { -{{range .Examples}} - p.{{.Name}}() -{{end}} -} -`)) diff --git a/vendor/golang.org/x/tools/go/ssa/util.go b/vendor/golang.org/x/tools/go/ssa/util.go deleted file mode 100644 index ddb11846096..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/util.go +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file defines a number of miscellaneous utility functions. - -import ( - "fmt" - "go/ast" - "go/token" - "go/types" - "io" - "os" - - "golang.org/x/tools/go/ast/astutil" -) - -//// AST utilities - -func unparen(e ast.Expr) ast.Expr { return astutil.Unparen(e) } - -// isBlankIdent returns true iff e is an Ident with name "_". -// They have no associated types.Object, and thus no type. -// -func isBlankIdent(e ast.Expr) bool { - id, ok := e.(*ast.Ident) - return ok && id.Name == "_" -} - -//// Type utilities. Some of these belong in go/types. - -// isPointer returns true for types whose underlying type is a pointer. -func isPointer(typ types.Type) bool { - _, ok := typ.Underlying().(*types.Pointer) - return ok -} - -func isInterface(T types.Type) bool { return types.IsInterface(T) } - -// deref returns a pointer's element type; otherwise it returns typ. -func deref(typ types.Type) types.Type { - if p, ok := typ.Underlying().(*types.Pointer); ok { - return p.Elem() - } - return typ -} - -// recvType returns the receiver type of method obj. -func recvType(obj *types.Func) types.Type { - return obj.Type().(*types.Signature).Recv().Type() -} - -// DefaultType returns the default "typed" type for an "untyped" type; -// it returns the incoming type for all other types. The default type -// for untyped nil is untyped nil. -// -// Exported to ssa/interp. -// -// TODO(adonovan): use go/types.DefaultType after 1.8. -// -func DefaultType(typ types.Type) types.Type { - if t, ok := typ.(*types.Basic); ok { - k := t.Kind() - switch k { - case types.UntypedBool: - k = types.Bool - case types.UntypedInt: - k = types.Int - case types.UntypedRune: - k = types.Rune - case types.UntypedFloat: - k = types.Float64 - case types.UntypedComplex: - k = types.Complex128 - case types.UntypedString: - k = types.String - } - typ = types.Typ[k] - } - return typ -} - -// logStack prints the formatted "start" message to stderr and -// returns a closure that prints the corresponding "end" message. -// Call using 'defer logStack(...)()' to show builder stack on panic. -// Don't forget trailing parens! -// -func logStack(format string, args ...interface{}) func() { - msg := fmt.Sprintf(format, args...) - io.WriteString(os.Stderr, msg) - io.WriteString(os.Stderr, "\n") - return func() { - io.WriteString(os.Stderr, msg) - io.WriteString(os.Stderr, " end\n") - } -} - -// newVar creates a 'var' for use in a types.Tuple. -func newVar(name string, typ types.Type) *types.Var { - return types.NewParam(token.NoPos, nil, name, typ) -} - -// anonVar creates an anonymous 'var' for use in a types.Tuple. -func anonVar(typ types.Type) *types.Var { - return newVar("", typ) -} - -var lenResults = types.NewTuple(anonVar(tInt)) - -// makeLen returns the len builtin specialized to type func(T)int. -func makeLen(T types.Type) *Builtin { - lenParams := types.NewTuple(anonVar(T)) - return &Builtin{ - name: "len", - sig: types.NewSignature(nil, lenParams, lenResults, false), - } -} diff --git a/vendor/golang.org/x/tools/go/ssa/wrappers.go b/vendor/golang.org/x/tools/go/ssa/wrappers.go deleted file mode 100644 index a4ae71d8cfc..00000000000 --- a/vendor/golang.org/x/tools/go/ssa/wrappers.go +++ /dev/null @@ -1,290 +0,0 @@ -// Copyright 2013 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package ssa - -// This file defines synthesis of Functions that delegate to declared -// methods; they come in three kinds: -// -// (1) wrappers: methods that wrap declared methods, performing -// implicit pointer indirections and embedded field selections. -// -// (2) thunks: funcs that wrap declared methods. Like wrappers, -// thunks perform indirections and field selections. The thunk's -// first parameter is used as the receiver for the method call. -// -// (3) bounds: funcs that wrap declared methods. The bound's sole -// free variable, supplied by a closure, is used as the receiver -// for the method call. No indirections or field selections are -// performed since they can be done before the call. - -import ( - "fmt" - - "go/types" -) - -// -- wrappers ----------------------------------------------------------- - -// makeWrapper returns a synthetic method that delegates to the -// declared method denoted by meth.Obj(), first performing any -// necessary pointer indirections or field selections implied by meth. -// -// The resulting method's receiver type is meth.Recv(). -// -// This function is versatile but quite subtle! Consider the -// following axes of variation when making changes: -// - optional receiver indirection -// - optional implicit field selections -// - meth.Obj() may denote a concrete or an interface method -// - the result may be a thunk or a wrapper. -// -// EXCLUSIVE_LOCKS_REQUIRED(prog.methodsMu) -// -func makeWrapper(prog *Program, sel *types.Selection) *Function { - obj := sel.Obj().(*types.Func) // the declared function - sig := sel.Type().(*types.Signature) // type of this wrapper - - var recv *types.Var // wrapper's receiver or thunk's params[0] - name := obj.Name() - var description string - var start int // first regular param - if sel.Kind() == types.MethodExpr { - name += "$thunk" - description = "thunk" - recv = sig.Params().At(0) - start = 1 - } else { - description = "wrapper" - recv = sig.Recv() - } - - description = fmt.Sprintf("%s for %s", description, sel.Obj()) - if prog.mode&LogSource != 0 { - defer logStack("make %s to (%s)", description, recv.Type())() - } - fn := &Function{ - name: name, - method: sel, - object: obj, - Signature: sig, - Synthetic: description, - Prog: prog, - pos: obj.Pos(), - } - fn.startBody() - fn.addSpilledParam(recv) - createParams(fn, start) - - indices := sel.Index() - - var v Value = fn.Locals[0] // spilled receiver - if isPointer(sel.Recv()) { - v = emitLoad(fn, v) - - // For simple indirection wrappers, perform an informative nil-check: - // "value method (T).f called using nil *T pointer" - if len(indices) == 1 && !isPointer(recvType(obj)) { - var c Call - c.Call.Value = &Builtin{ - name: "ssa:wrapnilchk", - sig: types.NewSignature(nil, - types.NewTuple(anonVar(sel.Recv()), anonVar(tString), anonVar(tString)), - types.NewTuple(anonVar(sel.Recv())), false), - } - c.Call.Args = []Value{ - v, - stringConst(deref(sel.Recv()).String()), - stringConst(sel.Obj().Name()), - } - c.setType(v.Type()) - v = fn.emit(&c) - } - } - - // Invariant: v is a pointer, either - // value of *A receiver param, or - // address of A spilled receiver. - - // We use pointer arithmetic (FieldAddr possibly followed by - // Load) in preference to value extraction (Field possibly - // preceded by Load). - - v = emitImplicitSelections(fn, v, indices[:len(indices)-1]) - - // Invariant: v is a pointer, either - // value of implicit *C field, or - // address of implicit C field. - - var c Call - if r := recvType(obj); !isInterface(r) { // concrete method - if !isPointer(r) { - v = emitLoad(fn, v) - } - c.Call.Value = prog.declaredFunc(obj) - c.Call.Args = append(c.Call.Args, v) - } else { - c.Call.Method = obj - c.Call.Value = emitLoad(fn, v) - } - for _, arg := range fn.Params[1:] { - c.Call.Args = append(c.Call.Args, arg) - } - emitTailCall(fn, &c) - fn.finishBody() - return fn -} - -// createParams creates parameters for wrapper method fn based on its -// Signature.Params, which do not include the receiver. -// start is the index of the first regular parameter to use. -// -func createParams(fn *Function, start int) { - tparams := fn.Signature.Params() - for i, n := start, tparams.Len(); i < n; i++ { - fn.addParamObj(tparams.At(i)) - } -} - -// -- bounds ----------------------------------------------------------- - -// makeBound returns a bound method wrapper (or "bound"), a synthetic -// function that delegates to a concrete or interface method denoted -// by obj. The resulting function has no receiver, but has one free -// variable which will be used as the method's receiver in the -// tail-call. -// -// Use MakeClosure with such a wrapper to construct a bound method -// closure. e.g.: -// -// type T int or: type T interface { meth() } -// func (t T) meth() -// var t T -// f := t.meth -// f() // calls t.meth() -// -// f is a closure of a synthetic wrapper defined as if by: -// -// f := func() { return t.meth() } -// -// Unlike makeWrapper, makeBound need perform no indirection or field -// selections because that can be done before the closure is -// constructed. -// -// EXCLUSIVE_LOCKS_ACQUIRED(meth.Prog.methodsMu) -// -func makeBound(prog *Program, obj *types.Func) *Function { - prog.methodsMu.Lock() - defer prog.methodsMu.Unlock() - fn, ok := prog.bounds[obj] - if !ok { - description := fmt.Sprintf("bound method wrapper for %s", obj) - if prog.mode&LogSource != 0 { - defer logStack("%s", description)() - } - fn = &Function{ - name: obj.Name() + "$bound", - object: obj, - Signature: changeRecv(obj.Type().(*types.Signature), nil), // drop receiver - Synthetic: description, - Prog: prog, - pos: obj.Pos(), - } - - fv := &FreeVar{name: "recv", typ: recvType(obj), parent: fn} - fn.FreeVars = []*FreeVar{fv} - fn.startBody() - createParams(fn, 0) - var c Call - - if !isInterface(recvType(obj)) { // concrete - c.Call.Value = prog.declaredFunc(obj) - c.Call.Args = []Value{fv} - } else { - c.Call.Value = fv - c.Call.Method = obj - } - for _, arg := range fn.Params { - c.Call.Args = append(c.Call.Args, arg) - } - emitTailCall(fn, &c) - fn.finishBody() - - prog.bounds[obj] = fn - } - return fn -} - -// -- thunks ----------------------------------------------------------- - -// makeThunk returns a thunk, a synthetic function that delegates to a -// concrete or interface method denoted by sel.Obj(). The resulting -// function has no receiver, but has an additional (first) regular -// parameter. -// -// Precondition: sel.Kind() == types.MethodExpr. -// -// type T int or: type T interface { meth() } -// func (t T) meth() -// f := T.meth -// var t T -// f(t) // calls t.meth() -// -// f is a synthetic wrapper defined as if by: -// -// f := func(t T) { return t.meth() } -// -// TODO(adonovan): opt: currently the stub is created even when used -// directly in a function call: C.f(i, 0). This is less efficient -// than inlining the stub. -// -// EXCLUSIVE_LOCKS_ACQUIRED(meth.Prog.methodsMu) -// -func makeThunk(prog *Program, sel *types.Selection) *Function { - if sel.Kind() != types.MethodExpr { - panic(sel) - } - - key := selectionKey{ - kind: sel.Kind(), - recv: sel.Recv(), - obj: sel.Obj(), - index: fmt.Sprint(sel.Index()), - indirect: sel.Indirect(), - } - - prog.methodsMu.Lock() - defer prog.methodsMu.Unlock() - - // Canonicalize key.recv to avoid constructing duplicate thunks. - canonRecv, ok := prog.canon.At(key.recv).(types.Type) - if !ok { - canonRecv = key.recv - prog.canon.Set(key.recv, canonRecv) - } - key.recv = canonRecv - - fn, ok := prog.thunks[key] - if !ok { - fn = makeWrapper(prog, sel) - if fn.Signature.Recv() != nil { - panic(fn) // unexpected receiver - } - prog.thunks[key] = fn - } - return fn -} - -func changeRecv(s *types.Signature, recv *types.Var) *types.Signature { - return types.NewSignature(recv, s.Params(), s.Results(), s.Variadic()) -} - -// selectionKey is like types.Selection but a usable map key. -type selectionKey struct { - kind types.SelectionKind - recv types.Type // canonicalized via Program.canon - obj types.Object - index string - indirect bool -} diff --git a/vendor/golang.org/x/tools/go/types/typeutil/callee.go b/vendor/golang.org/x/tools/go/types/typeutil/callee.go deleted file mode 100644 index 38f596daf9e..00000000000 --- a/vendor/golang.org/x/tools/go/types/typeutil/callee.go +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2018 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package typeutil - -import ( - "go/ast" - "go/types" - - "golang.org/x/tools/go/ast/astutil" -) - -// Callee returns the named target of a function call, if any: -// a function, method, builtin, or variable. -func Callee(info *types.Info, call *ast.CallExpr) types.Object { - var obj types.Object - switch fun := astutil.Unparen(call.Fun).(type) { - case *ast.Ident: - obj = info.Uses[fun] // type, var, builtin, or declared func - case *ast.SelectorExpr: - if sel, ok := info.Selections[fun]; ok { - obj = sel.Obj() // method or field - } else { - obj = info.Uses[fun.Sel] // qualified identifier? - } - } - if _, ok := obj.(*types.TypeName); ok { - return nil // T(x) is a conversion, not a call - } - return obj -} - -// StaticCallee returns the target (function or method) of a static -// function call, if any. It returns nil for calls to builtins. -func StaticCallee(info *types.Info, call *ast.CallExpr) *types.Func { - if f, ok := Callee(info, call).(*types.Func); ok && !interfaceMethod(f) { - return f - } - return nil -} - -func interfaceMethod(f *types.Func) bool { - recv := f.Type().(*types.Signature).Recv() - return recv != nil && types.IsInterface(recv.Type()) -} diff --git a/vendor/golang.org/x/tools/go/types/typeutil/imports.go b/vendor/golang.org/x/tools/go/types/typeutil/imports.go deleted file mode 100644 index 9c441dba9c0..00000000000 --- a/vendor/golang.org/x/tools/go/types/typeutil/imports.go +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package typeutil - -import "go/types" - -// Dependencies returns all dependencies of the specified packages. -// -// Dependent packages appear in topological order: if package P imports -// package Q, Q appears earlier than P in the result. -// The algorithm follows import statements in the order they -// appear in the source code, so the result is a total order. -// -func Dependencies(pkgs ...*types.Package) []*types.Package { - var result []*types.Package - seen := make(map[*types.Package]bool) - var visit func(pkgs []*types.Package) - visit = func(pkgs []*types.Package) { - for _, p := range pkgs { - if !seen[p] { - seen[p] = true - visit(p.Imports()) - result = append(result, p) - } - } - } - visit(pkgs) - return result -} diff --git a/vendor/golang.org/x/tools/go/types/typeutil/map.go b/vendor/golang.org/x/tools/go/types/typeutil/map.go deleted file mode 100644 index c7f75450064..00000000000 --- a/vendor/golang.org/x/tools/go/types/typeutil/map.go +++ /dev/null @@ -1,313 +0,0 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package typeutil defines various utilities for types, such as Map, -// a mapping from types.Type to interface{} values. -package typeutil // import "golang.org/x/tools/go/types/typeutil" - -import ( - "bytes" - "fmt" - "go/types" - "reflect" -) - -// Map is a hash-table-based mapping from types (types.Type) to -// arbitrary interface{} values. The concrete types that implement -// the Type interface are pointers. Since they are not canonicalized, -// == cannot be used to check for equivalence, and thus we cannot -// simply use a Go map. -// -// Just as with map[K]V, a nil *Map is a valid empty map. -// -// Not thread-safe. -// -type Map struct { - hasher Hasher // shared by many Maps - table map[uint32][]entry // maps hash to bucket; entry.key==nil means unused - length int // number of map entries -} - -// entry is an entry (key/value association) in a hash bucket. -type entry struct { - key types.Type - value interface{} -} - -// SetHasher sets the hasher used by Map. -// -// All Hashers are functionally equivalent but contain internal state -// used to cache the results of hashing previously seen types. -// -// A single Hasher created by MakeHasher() may be shared among many -// Maps. This is recommended if the instances have many keys in -// common, as it will amortize the cost of hash computation. -// -// A Hasher may grow without bound as new types are seen. Even when a -// type is deleted from the map, the Hasher never shrinks, since other -// types in the map may reference the deleted type indirectly. -// -// Hashers are not thread-safe, and read-only operations such as -// Map.Lookup require updates to the hasher, so a full Mutex lock (not a -// read-lock) is require around all Map operations if a shared -// hasher is accessed from multiple threads. -// -// If SetHasher is not called, the Map will create a private hasher at -// the first call to Insert. -// -func (m *Map) SetHasher(hasher Hasher) { - m.hasher = hasher -} - -// Delete removes the entry with the given key, if any. -// It returns true if the entry was found. -// -func (m *Map) Delete(key types.Type) bool { - if m != nil && m.table != nil { - hash := m.hasher.Hash(key) - bucket := m.table[hash] - for i, e := range bucket { - if e.key != nil && types.Identical(key, e.key) { - // We can't compact the bucket as it - // would disturb iterators. - bucket[i] = entry{} - m.length-- - return true - } - } - } - return false -} - -// At returns the map entry for the given key. -// The result is nil if the entry is not present. -// -func (m *Map) At(key types.Type) interface{} { - if m != nil && m.table != nil { - for _, e := range m.table[m.hasher.Hash(key)] { - if e.key != nil && types.Identical(key, e.key) { - return e.value - } - } - } - return nil -} - -// Set sets the map entry for key to val, -// and returns the previous entry, if any. -func (m *Map) Set(key types.Type, value interface{}) (prev interface{}) { - if m.table != nil { - hash := m.hasher.Hash(key) - bucket := m.table[hash] - var hole *entry - for i, e := range bucket { - if e.key == nil { - hole = &bucket[i] - } else if types.Identical(key, e.key) { - prev = e.value - bucket[i].value = value - return - } - } - - if hole != nil { - *hole = entry{key, value} // overwrite deleted entry - } else { - m.table[hash] = append(bucket, entry{key, value}) - } - } else { - if m.hasher.memo == nil { - m.hasher = MakeHasher() - } - hash := m.hasher.Hash(key) - m.table = map[uint32][]entry{hash: {entry{key, value}}} - } - - m.length++ - return -} - -// Len returns the number of map entries. -func (m *Map) Len() int { - if m != nil { - return m.length - } - return 0 -} - -// Iterate calls function f on each entry in the map in unspecified order. -// -// If f should mutate the map, Iterate provides the same guarantees as -// Go maps: if f deletes a map entry that Iterate has not yet reached, -// f will not be invoked for it, but if f inserts a map entry that -// Iterate has not yet reached, whether or not f will be invoked for -// it is unspecified. -// -func (m *Map) Iterate(f func(key types.Type, value interface{})) { - if m != nil { - for _, bucket := range m.table { - for _, e := range bucket { - if e.key != nil { - f(e.key, e.value) - } - } - } - } -} - -// Keys returns a new slice containing the set of map keys. -// The order is unspecified. -func (m *Map) Keys() []types.Type { - keys := make([]types.Type, 0, m.Len()) - m.Iterate(func(key types.Type, _ interface{}) { - keys = append(keys, key) - }) - return keys -} - -func (m *Map) toString(values bool) string { - if m == nil { - return "{}" - } - var buf bytes.Buffer - fmt.Fprint(&buf, "{") - sep := "" - m.Iterate(func(key types.Type, value interface{}) { - fmt.Fprint(&buf, sep) - sep = ", " - fmt.Fprint(&buf, key) - if values { - fmt.Fprintf(&buf, ": %q", value) - } - }) - fmt.Fprint(&buf, "}") - return buf.String() -} - -// String returns a string representation of the map's entries. -// Values are printed using fmt.Sprintf("%v", v). -// Order is unspecified. -// -func (m *Map) String() string { - return m.toString(true) -} - -// KeysString returns a string representation of the map's key set. -// Order is unspecified. -// -func (m *Map) KeysString() string { - return m.toString(false) -} - -//////////////////////////////////////////////////////////////////////// -// Hasher - -// A Hasher maps each type to its hash value. -// For efficiency, a hasher uses memoization; thus its memory -// footprint grows monotonically over time. -// Hashers are not thread-safe. -// Hashers have reference semantics. -// Call MakeHasher to create a Hasher. -type Hasher struct { - memo map[types.Type]uint32 -} - -// MakeHasher returns a new Hasher instance. -func MakeHasher() Hasher { - return Hasher{make(map[types.Type]uint32)} -} - -// Hash computes a hash value for the given type t such that -// Identical(t, t') => Hash(t) == Hash(t'). -func (h Hasher) Hash(t types.Type) uint32 { - hash, ok := h.memo[t] - if !ok { - hash = h.hashFor(t) - h.memo[t] = hash - } - return hash -} - -// hashString computes the Fowler–Noll–Vo hash of s. -func hashString(s string) uint32 { - var h uint32 - for i := 0; i < len(s); i++ { - h ^= uint32(s[i]) - h *= 16777619 - } - return h -} - -// hashFor computes the hash of t. -func (h Hasher) hashFor(t types.Type) uint32 { - // See Identical for rationale. - switch t := t.(type) { - case *types.Basic: - return uint32(t.Kind()) - - case *types.Array: - return 9043 + 2*uint32(t.Len()) + 3*h.Hash(t.Elem()) - - case *types.Slice: - return 9049 + 2*h.Hash(t.Elem()) - - case *types.Struct: - var hash uint32 = 9059 - for i, n := 0, t.NumFields(); i < n; i++ { - f := t.Field(i) - if f.Anonymous() { - hash += 8861 - } - hash += hashString(t.Tag(i)) - hash += hashString(f.Name()) // (ignore f.Pkg) - hash += h.Hash(f.Type()) - } - return hash - - case *types.Pointer: - return 9067 + 2*h.Hash(t.Elem()) - - case *types.Signature: - var hash uint32 = 9091 - if t.Variadic() { - hash *= 8863 - } - return hash + 3*h.hashTuple(t.Params()) + 5*h.hashTuple(t.Results()) - - case *types.Interface: - var hash uint32 = 9103 - for i, n := 0, t.NumMethods(); i < n; i++ { - // See go/types.identicalMethods for rationale. - // Method order is not significant. - // Ignore m.Pkg(). - m := t.Method(i) - hash += 3*hashString(m.Name()) + 5*h.Hash(m.Type()) - } - return hash - - case *types.Map: - return 9109 + 2*h.Hash(t.Key()) + 3*h.Hash(t.Elem()) - - case *types.Chan: - return 9127 + 2*uint32(t.Dir()) + 3*h.Hash(t.Elem()) - - case *types.Named: - // Not safe with a copying GC; objects may move. - return uint32(reflect.ValueOf(t.Obj()).Pointer()) - - case *types.Tuple: - return h.hashTuple(t) - } - panic(t) -} - -func (h Hasher) hashTuple(tuple *types.Tuple) uint32 { - // See go/types.identicalTypes for rationale. - n := tuple.Len() - var hash uint32 = 9137 + 2*uint32(n) - for i := 0; i < n; i++ { - hash += 3 * h.Hash(tuple.At(i).Type()) - } - return hash -} diff --git a/vendor/golang.org/x/tools/go/types/typeutil/methodsetcache.go b/vendor/golang.org/x/tools/go/types/typeutil/methodsetcache.go deleted file mode 100644 index 32084610f49..00000000000 --- a/vendor/golang.org/x/tools/go/types/typeutil/methodsetcache.go +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// This file implements a cache of method sets. - -package typeutil - -import ( - "go/types" - "sync" -) - -// A MethodSetCache records the method set of each type T for which -// MethodSet(T) is called so that repeat queries are fast. -// The zero value is a ready-to-use cache instance. -type MethodSetCache struct { - mu sync.Mutex - named map[*types.Named]struct{ value, pointer *types.MethodSet } // method sets for named N and *N - others map[types.Type]*types.MethodSet // all other types -} - -// MethodSet returns the method set of type T. It is thread-safe. -// -// If cache is nil, this function is equivalent to types.NewMethodSet(T). -// Utility functions can thus expose an optional *MethodSetCache -// parameter to clients that care about performance. -// -func (cache *MethodSetCache) MethodSet(T types.Type) *types.MethodSet { - if cache == nil { - return types.NewMethodSet(T) - } - cache.mu.Lock() - defer cache.mu.Unlock() - - switch T := T.(type) { - case *types.Named: - return cache.lookupNamed(T).value - - case *types.Pointer: - if N, ok := T.Elem().(*types.Named); ok { - return cache.lookupNamed(N).pointer - } - } - - // all other types - // (The map uses pointer equivalence, not type identity.) - mset := cache.others[T] - if mset == nil { - mset = types.NewMethodSet(T) - if cache.others == nil { - cache.others = make(map[types.Type]*types.MethodSet) - } - cache.others[T] = mset - } - return mset -} - -func (cache *MethodSetCache) lookupNamed(named *types.Named) struct{ value, pointer *types.MethodSet } { - if cache.named == nil { - cache.named = make(map[*types.Named]struct{ value, pointer *types.MethodSet }) - } - // Avoid recomputing mset(*T) for each distinct Pointer - // instance whose underlying type is a named type. - msets, ok := cache.named[named] - if !ok { - msets.value = types.NewMethodSet(named) - msets.pointer = types.NewMethodSet(types.NewPointer(named)) - cache.named[named] = msets - } - return msets -} diff --git a/vendor/golang.org/x/tools/go/types/typeutil/ui.go b/vendor/golang.org/x/tools/go/types/typeutil/ui.go deleted file mode 100644 index 9849c24cef3..00000000000 --- a/vendor/golang.org/x/tools/go/types/typeutil/ui.go +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2014 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package typeutil - -// This file defines utilities for user interfaces that display types. - -import "go/types" - -// IntuitiveMethodSet returns the intuitive method set of a type T, -// which is the set of methods you can call on an addressable value of -// that type. -// -// The result always contains MethodSet(T), and is exactly MethodSet(T) -// for interface types and for pointer-to-concrete types. -// For all other concrete types T, the result additionally -// contains each method belonging to *T if there is no identically -// named method on T itself. -// -// This corresponds to user intuition about method sets; -// this function is intended only for user interfaces. -// -// The order of the result is as for types.MethodSet(T). -// -func IntuitiveMethodSet(T types.Type, msets *MethodSetCache) []*types.Selection { - isPointerToConcrete := func(T types.Type) bool { - ptr, ok := T.(*types.Pointer) - return ok && !types.IsInterface(ptr.Elem()) - } - - var result []*types.Selection - mset := msets.MethodSet(T) - if types.IsInterface(T) || isPointerToConcrete(T) { - for i, n := 0, mset.Len(); i < n; i++ { - result = append(result, mset.At(i)) - } - } else { - // T is some other concrete type. - // Report methods of T and *T, preferring those of T. - pmset := msets.MethodSet(types.NewPointer(T)) - for i, n := 0, pmset.Len(); i < n; i++ { - meth := pmset.At(i) - if m := mset.Lookup(meth.Obj().Pkg(), meth.Obj().Name()); m != nil { - meth = m - } - result = append(result, meth) - } - - } - return result -} diff --git a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk.go b/vendor/golang.org/x/tools/internal/fastwalk/fastwalk.go deleted file mode 100644 index 7219c8e9ff1..00000000000 --- a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk.go +++ /dev/null @@ -1,196 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package fastwalk provides a faster version of filepath.Walk for file system -// scanning tools. -package fastwalk - -import ( - "errors" - "os" - "path/filepath" - "runtime" - "sync" -) - -// TraverseLink is used as a return value from WalkFuncs to indicate that the -// symlink named in the call may be traversed. -var TraverseLink = errors.New("fastwalk: traverse symlink, assuming target is a directory") - -// SkipFiles is a used as a return value from WalkFuncs to indicate that the -// callback should not be called for any other files in the current directory. -// Child directories will still be traversed. -var SkipFiles = errors.New("fastwalk: skip remaining files in directory") - -// Walk is a faster implementation of filepath.Walk. -// -// filepath.Walk's design necessarily calls os.Lstat on each file, -// even if the caller needs less info. -// Many tools need only the type of each file. -// On some platforms, this information is provided directly by the readdir -// system call, avoiding the need to stat each file individually. -// fastwalk_unix.go contains a fork of the syscall routines. -// -// See golang.org/issue/16399 -// -// Walk walks the file tree rooted at root, calling walkFn for -// each file or directory in the tree, including root. -// -// If fastWalk returns filepath.SkipDir, the directory is skipped. -// -// Unlike filepath.Walk: -// * file stat calls must be done by the user. -// The only provided metadata is the file type, which does not include -// any permission bits. -// * multiple goroutines stat the filesystem concurrently. The provided -// walkFn must be safe for concurrent use. -// * fastWalk can follow symlinks if walkFn returns the TraverseLink -// sentinel error. It is the walkFn's responsibility to prevent -// fastWalk from going into symlink cycles. -func Walk(root string, walkFn func(path string, typ os.FileMode) error) error { - // TODO(bradfitz): make numWorkers configurable? We used a - // minimum of 4 to give the kernel more info about multiple - // things we want, in hopes its I/O scheduling can take - // advantage of that. Hopefully most are in cache. Maybe 4 is - // even too low of a minimum. Profile more. - numWorkers := 4 - if n := runtime.NumCPU(); n > numWorkers { - numWorkers = n - } - - // Make sure to wait for all workers to finish, otherwise - // walkFn could still be called after returning. This Wait call - // runs after close(e.donec) below. - var wg sync.WaitGroup - defer wg.Wait() - - w := &walker{ - fn: walkFn, - enqueuec: make(chan walkItem, numWorkers), // buffered for performance - workc: make(chan walkItem, numWorkers), // buffered for performance - donec: make(chan struct{}), - - // buffered for correctness & not leaking goroutines: - resc: make(chan error, numWorkers), - } - defer close(w.donec) - - for i := 0; i < numWorkers; i++ { - wg.Add(1) - go w.doWork(&wg) - } - todo := []walkItem{{dir: root}} - out := 0 - for { - workc := w.workc - var workItem walkItem - if len(todo) == 0 { - workc = nil - } else { - workItem = todo[len(todo)-1] - } - select { - case workc <- workItem: - todo = todo[:len(todo)-1] - out++ - case it := <-w.enqueuec: - todo = append(todo, it) - case err := <-w.resc: - out-- - if err != nil { - return err - } - if out == 0 && len(todo) == 0 { - // It's safe to quit here, as long as the buffered - // enqueue channel isn't also readable, which might - // happen if the worker sends both another unit of - // work and its result before the other select was - // scheduled and both w.resc and w.enqueuec were - // readable. - select { - case it := <-w.enqueuec: - todo = append(todo, it) - default: - return nil - } - } - } - } -} - -// doWork reads directories as instructed (via workc) and runs the -// user's callback function. -func (w *walker) doWork(wg *sync.WaitGroup) { - defer wg.Done() - for { - select { - case <-w.donec: - return - case it := <-w.workc: - select { - case <-w.donec: - return - case w.resc <- w.walk(it.dir, !it.callbackDone): - } - } - } -} - -type walker struct { - fn func(path string, typ os.FileMode) error - - donec chan struct{} // closed on fastWalk's return - workc chan walkItem // to workers - enqueuec chan walkItem // from workers - resc chan error // from workers -} - -type walkItem struct { - dir string - callbackDone bool // callback already called; don't do it again -} - -func (w *walker) enqueue(it walkItem) { - select { - case w.enqueuec <- it: - case <-w.donec: - } -} - -func (w *walker) onDirEnt(dirName, baseName string, typ os.FileMode) error { - joined := dirName + string(os.PathSeparator) + baseName - if typ == os.ModeDir { - w.enqueue(walkItem{dir: joined}) - return nil - } - - err := w.fn(joined, typ) - if typ == os.ModeSymlink { - if err == TraverseLink { - // Set callbackDone so we don't call it twice for both the - // symlink-as-symlink and the symlink-as-directory later: - w.enqueue(walkItem{dir: joined, callbackDone: true}) - return nil - } - if err == filepath.SkipDir { - // Permit SkipDir on symlinks too. - return nil - } - } - return err -} - -func (w *walker) walk(root string, runUserCallback bool) error { - if runUserCallback { - err := w.fn(root, os.ModeDir) - if err == filepath.SkipDir { - return nil - } - if err != nil { - return err - } - } - - return readDir(root, w.onDirEnt) -} diff --git a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_fileno.go b/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_fileno.go deleted file mode 100644 index ccffec5adc1..00000000000 --- a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_fileno.go +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build freebsd openbsd netbsd - -package fastwalk - -import "syscall" - -func direntInode(dirent *syscall.Dirent) uint64 { - return uint64(dirent.Fileno) -} diff --git a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_ino.go b/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_ino.go deleted file mode 100644 index ab7fbc0a9a3..00000000000 --- a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_ino.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build linux darwin -// +build !appengine - -package fastwalk - -import "syscall" - -func direntInode(dirent *syscall.Dirent) uint64 { - return uint64(dirent.Ino) -} diff --git a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_namlen_linux.go b/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_namlen_linux.go deleted file mode 100644 index e880d358b13..00000000000 --- a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_namlen_linux.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2018 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build linux -// +build !appengine - -package fastwalk - -import ( - "bytes" - "syscall" - "unsafe" -) - -func direntNamlen(dirent *syscall.Dirent) uint64 { - const fixedHdr = uint16(unsafe.Offsetof(syscall.Dirent{}.Name)) - nameBuf := (*[unsafe.Sizeof(dirent.Name)]byte)(unsafe.Pointer(&dirent.Name[0])) - const nameBufLen = uint16(len(nameBuf)) - limit := dirent.Reclen - fixedHdr - if limit > nameBufLen { - limit = nameBufLen - } - nameLen := bytes.IndexByte(nameBuf[:limit], 0) - if nameLen < 0 { - panic("failed to find terminating 0 byte in dirent") - } - return uint64(nameLen) -} diff --git a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_portable.go b/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_portable.go deleted file mode 100644 index a906b87595b..00000000000 --- a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_portable.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build appengine !linux,!darwin,!freebsd,!openbsd,!netbsd - -package fastwalk - -import ( - "io/ioutil" - "os" -) - -// readDir calls fn for each directory entry in dirName. -// It does not descend into directories or follow symlinks. -// If fn returns a non-nil error, readDir returns with that error -// immediately. -func readDir(dirName string, fn func(dirName, entName string, typ os.FileMode) error) error { - fis, err := ioutil.ReadDir(dirName) - if err != nil { - return err - } - skipFiles := false - for _, fi := range fis { - if fi.Mode().IsRegular() && skipFiles { - continue - } - if err := fn(dirName, fi.Name(), fi.Mode()&os.ModeType); err != nil { - if err == SkipFiles { - skipFiles = true - continue - } - return err - } - } - return nil -} diff --git a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_unix.go b/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_unix.go deleted file mode 100644 index 3369b1a0b2d..00000000000 --- a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_unix.go +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// +build linux darwin freebsd openbsd netbsd -// +build !appengine - -package fastwalk - -import ( - "fmt" - "os" - "syscall" - "unsafe" -) - -const blockSize = 8 << 10 - -// unknownFileMode is a sentinel (and bogus) os.FileMode -// value used to represent a syscall.DT_UNKNOWN Dirent.Type. -const unknownFileMode os.FileMode = os.ModeNamedPipe | os.ModeSocket | os.ModeDevice - -func readDir(dirName string, fn func(dirName, entName string, typ os.FileMode) error) error { - fd, err := syscall.Open(dirName, 0, 0) - if err != nil { - return &os.PathError{Op: "open", Path: dirName, Err: err} - } - defer syscall.Close(fd) - - // The buffer must be at least a block long. - buf := make([]byte, blockSize) // stack-allocated; doesn't escape - bufp := 0 // starting read position in buf - nbuf := 0 // end valid data in buf - skipFiles := false - for { - if bufp >= nbuf { - bufp = 0 - nbuf, err = syscall.ReadDirent(fd, buf) - if err != nil { - return os.NewSyscallError("readdirent", err) - } - if nbuf <= 0 { - return nil - } - } - consumed, name, typ := parseDirEnt(buf[bufp:nbuf]) - bufp += consumed - if name == "" || name == "." || name == ".." { - continue - } - // Fallback for filesystems (like old XFS) that don't - // support Dirent.Type and have DT_UNKNOWN (0) there - // instead. - if typ == unknownFileMode { - fi, err := os.Lstat(dirName + "/" + name) - if err != nil { - // It got deleted in the meantime. - if os.IsNotExist(err) { - continue - } - return err - } - typ = fi.Mode() & os.ModeType - } - if skipFiles && typ.IsRegular() { - continue - } - if err := fn(dirName, name, typ); err != nil { - if err == SkipFiles { - skipFiles = true - continue - } - return err - } - } -} - -func parseDirEnt(buf []byte) (consumed int, name string, typ os.FileMode) { - // golang.org/issue/15653 - dirent := (*syscall.Dirent)(unsafe.Pointer(&buf[0])) - if v := unsafe.Offsetof(dirent.Reclen) + unsafe.Sizeof(dirent.Reclen); uintptr(len(buf)) < v { - panic(fmt.Sprintf("buf size of %d smaller than dirent header size %d", len(buf), v)) - } - if len(buf) < int(dirent.Reclen) { - panic(fmt.Sprintf("buf size %d < record length %d", len(buf), dirent.Reclen)) - } - consumed = int(dirent.Reclen) - if direntInode(dirent) == 0 { // File absent in directory. - return - } - switch dirent.Type { - case syscall.DT_REG: - typ = 0 - case syscall.DT_DIR: - typ = os.ModeDir - case syscall.DT_LNK: - typ = os.ModeSymlink - case syscall.DT_BLK: - typ = os.ModeDevice - case syscall.DT_FIFO: - typ = os.ModeNamedPipe - case syscall.DT_SOCK: - typ = os.ModeSocket - case syscall.DT_UNKNOWN: - typ = unknownFileMode - default: - // Skip weird things. - // It's probably a DT_WHT (http://lwn.net/Articles/325369/) - // or something. Revisit if/when this package is moved outside - // of goimports. goimports only cares about regular files, - // symlinks, and directories. - return - } - - nameBuf := (*[unsafe.Sizeof(dirent.Name)]byte)(unsafe.Pointer(&dirent.Name[0])) - nameLen := direntNamlen(dirent) - - // Special cases for common things: - if nameLen == 1 && nameBuf[0] == '.' { - name = "." - } else if nameLen == 2 && nameBuf[0] == '.' && nameBuf[1] == '.' { - name = ".." - } else { - name = string(nameBuf[:nameLen]) - } - return -} diff --git a/vendor/golang.org/x/tools/internal/gocommand/invoke.go b/vendor/golang.org/x/tools/internal/gocommand/invoke.go new file mode 100644 index 00000000000..75d73e744fd --- /dev/null +++ b/vendor/golang.org/x/tools/internal/gocommand/invoke.go @@ -0,0 +1,121 @@ +// Package gocommand is a helper for calling the go command. +package gocommand + +import ( + "bytes" + "context" + "fmt" + "os" + "os/exec" + "strings" + "time" +) + +// An Invocation represents a call to the go command. +type Invocation struct { + Verb string + Args []string + BuildFlags []string + Env []string + WorkingDir string + Logf func(format string, args ...interface{}) +} + +// Run runs the invocation, returning its stdout and an error suitable for +// human consumption, including stderr. +func (i *Invocation) Run(ctx context.Context) (*bytes.Buffer, error) { + stdout, _, friendly, _ := i.RunRaw(ctx) + return stdout, friendly +} + +// RunRaw is like Run, but also returns the raw stderr and error for callers +// that want to do low-level error handling/recovery. +func (i *Invocation) RunRaw(ctx context.Context) (stdout *bytes.Buffer, stderr *bytes.Buffer, friendlyError error, rawError error) { + log := i.Logf + if log == nil { + log = func(string, ...interface{}) {} + } + + goArgs := []string{i.Verb} + switch i.Verb { + case "mod": + // mod needs the sub-verb before build flags. + goArgs = append(goArgs, i.Args[0]) + goArgs = append(goArgs, i.BuildFlags...) + goArgs = append(goArgs, i.Args[1:]...) + case "env": + // env doesn't take build flags. + goArgs = append(goArgs, i.Args...) + default: + goArgs = append(goArgs, i.BuildFlags...) + goArgs = append(goArgs, i.Args...) + } + cmd := exec.Command("go", goArgs...) + stdout = &bytes.Buffer{} + stderr = &bytes.Buffer{} + cmd.Stdout = stdout + cmd.Stderr = stderr + // On darwin the cwd gets resolved to the real path, which breaks anything that + // expects the working directory to keep the original path, including the + // go command when dealing with modules. + // The Go stdlib has a special feature where if the cwd and the PWD are the + // same node then it trusts the PWD, so by setting it in the env for the child + // process we fix up all the paths returned by the go command. + cmd.Env = append(append([]string{}, i.Env...), "PWD="+i.WorkingDir) + cmd.Dir = i.WorkingDir + + defer func(start time.Time) { log("%s for %v", time.Since(start), cmdDebugStr(cmd)) }(time.Now()) + + rawError = runCmdContext(ctx, cmd) + friendlyError = rawError + if rawError != nil { + // Check for 'go' executable not being found. + if ee, ok := rawError.(*exec.Error); ok && ee.Err == exec.ErrNotFound { + friendlyError = fmt.Errorf("go command required, not found: %v", ee) + } + if ctx.Err() != nil { + friendlyError = ctx.Err() + } + friendlyError = fmt.Errorf("err: %v: stderr: %s", rawError, stderr) + } + return +} + +// runCmdContext is like exec.CommandContext except it sends os.Interrupt +// before os.Kill. +func runCmdContext(ctx context.Context, cmd *exec.Cmd) error { + if err := cmd.Start(); err != nil { + return err + } + resChan := make(chan error, 1) + go func() { + resChan <- cmd.Wait() + }() + + select { + case err := <-resChan: + return err + case <-ctx.Done(): + } + // Cancelled. Interrupt and see if it ends voluntarily. + cmd.Process.Signal(os.Interrupt) + select { + case err := <-resChan: + return err + case <-time.After(time.Second): + } + // Didn't shut down in response to interrupt. Kill it hard. + cmd.Process.Kill() + return <-resChan +} + +func cmdDebugStr(cmd *exec.Cmd) string { + env := make(map[string]string) + for _, kv := range cmd.Env { + split := strings.Split(kv, "=") + k, v := split[0], split[1] + env[k] = v + } + + return fmt.Sprintf("GOROOT=%v GOPATH=%v GO111MODULE=%v GOPROXY=%v PWD=%v go %v", env["GOROOT"], env["GOPATH"], env["GO111MODULE"], env["GOPROXY"], env["PWD"], cmd.Args) +} diff --git a/vendor/golang.org/x/tools/internal/gopathwalk/walk.go b/vendor/golang.org/x/tools/internal/gopathwalk/walk.go deleted file mode 100644 index d0675622893..00000000000 --- a/vendor/golang.org/x/tools/internal/gopathwalk/walk.go +++ /dev/null @@ -1,273 +0,0 @@ -// Copyright 2018 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -// Package gopathwalk is like filepath.Walk but specialized for finding Go -// packages, particularly in $GOPATH and $GOROOT. -package gopathwalk - -import ( - "bufio" - "bytes" - "fmt" - "go/build" - "io/ioutil" - "log" - "os" - "path/filepath" - "strings" - "time" - - "golang.org/x/tools/internal/fastwalk" -) - -// Options controls the behavior of a Walk call. -type Options struct { - Debug bool // Enable debug logging - ModulesEnabled bool // Search module caches. Also disables legacy goimports ignore rules. -} - -// RootType indicates the type of a Root. -type RootType int - -const ( - RootUnknown RootType = iota - RootGOROOT - RootGOPATH - RootCurrentModule - RootModuleCache - RootOther -) - -// A Root is a starting point for a Walk. -type Root struct { - Path string - Type RootType -} - -// SrcDirsRoots returns the roots from build.Default.SrcDirs(). Not modules-compatible. -func SrcDirsRoots(ctx *build.Context) []Root { - var roots []Root - roots = append(roots, Root{filepath.Join(ctx.GOROOT, "src"), RootGOROOT}) - for _, p := range filepath.SplitList(ctx.GOPATH) { - roots = append(roots, Root{filepath.Join(p, "src"), RootGOPATH}) - } - return roots -} - -// Walk walks Go source directories ($GOROOT, $GOPATH, etc) to find packages. -// For each package found, add will be called (concurrently) with the absolute -// paths of the containing source directory and the package directory. -// add will be called concurrently. -func Walk(roots []Root, add func(root Root, dir string), opts Options) { - WalkSkip(roots, add, func(Root, string) bool { return false }, opts) -} - -// WalkSkip walks Go source directories ($GOROOT, $GOPATH, etc) to find packages. -// For each package found, add will be called (concurrently) with the absolute -// paths of the containing source directory and the package directory. -// For each directory that will be scanned, skip will be called (concurrently) -// with the absolute paths of the containing source directory and the directory. -// If skip returns false on a directory it will be processed. -// add will be called concurrently. -// skip will be called concurrently. -func WalkSkip(roots []Root, add func(root Root, dir string), skip func(root Root, dir string) bool, opts Options) { - for _, root := range roots { - walkDir(root, add, skip, opts) - } -} - -// walkDir creates a walker and starts fastwalk with this walker. -func walkDir(root Root, add func(Root, string), skip func(root Root, dir string) bool, opts Options) { - if _, err := os.Stat(root.Path); os.IsNotExist(err) { - if opts.Debug { - log.Printf("skipping nonexistent directory: %v", root.Path) - } - return - } - start := time.Now() - if opts.Debug { - log.Printf("gopathwalk: scanning %s", root.Path) - } - w := &walker{ - root: root, - add: add, - skip: skip, - opts: opts, - } - w.init() - if err := fastwalk.Walk(root.Path, w.walk); err != nil { - log.Printf("gopathwalk: scanning directory %v: %v", root.Path, err) - } - - if opts.Debug { - log.Printf("gopathwalk: scanned %s in %v", root.Path, time.Since(start)) - } -} - -// walker is the callback for fastwalk.Walk. -type walker struct { - root Root // The source directory to scan. - add func(Root, string) // The callback that will be invoked for every possible Go package dir. - skip func(Root, string) bool // The callback that will be invoked for every dir. dir is skipped if it returns true. - opts Options // Options passed to Walk by the user. - - ignoredDirs []os.FileInfo // The ignored directories, loaded from .goimportsignore files. -} - -// init initializes the walker based on its Options -func (w *walker) init() { - var ignoredPaths []string - if w.root.Type == RootModuleCache { - ignoredPaths = []string{"cache"} - } - if !w.opts.ModulesEnabled && w.root.Type == RootGOPATH { - ignoredPaths = w.getIgnoredDirs(w.root.Path) - ignoredPaths = append(ignoredPaths, "v", "mod") - } - - for _, p := range ignoredPaths { - full := filepath.Join(w.root.Path, p) - if fi, err := os.Stat(full); err == nil { - w.ignoredDirs = append(w.ignoredDirs, fi) - if w.opts.Debug { - log.Printf("Directory added to ignore list: %s", full) - } - } else if w.opts.Debug { - log.Printf("Error statting ignored directory: %v", err) - } - } -} - -// getIgnoredDirs reads an optional config file at /.goimportsignore -// of relative directories to ignore when scanning for go files. -// The provided path is one of the $GOPATH entries with "src" appended. -func (w *walker) getIgnoredDirs(path string) []string { - file := filepath.Join(path, ".goimportsignore") - slurp, err := ioutil.ReadFile(file) - if w.opts.Debug { - if err != nil { - log.Print(err) - } else { - log.Printf("Read %s", file) - } - } - if err != nil { - return nil - } - - var ignoredDirs []string - bs := bufio.NewScanner(bytes.NewReader(slurp)) - for bs.Scan() { - line := strings.TrimSpace(bs.Text()) - if line == "" || strings.HasPrefix(line, "#") { - continue - } - ignoredDirs = append(ignoredDirs, line) - } - return ignoredDirs -} - -// shouldSkipDir reports whether the file should be skipped or not. -func (w *walker) shouldSkipDir(fi os.FileInfo, dir string) bool { - for _, ignoredDir := range w.ignoredDirs { - if os.SameFile(fi, ignoredDir) { - return true - } - } - if w.skip != nil { - // Check with the user specified callback. - return w.skip(w.root, dir) - } - return false -} - -// walk walks through the given path. -func (w *walker) walk(path string, typ os.FileMode) error { - dir := filepath.Dir(path) - if typ.IsRegular() { - if dir == w.root.Path && (w.root.Type == RootGOROOT || w.root.Type == RootGOPATH) { - // Doesn't make sense to have regular files - // directly in your $GOPATH/src or $GOROOT/src. - return fastwalk.SkipFiles - } - if !strings.HasSuffix(path, ".go") { - return nil - } - - w.add(w.root, dir) - return fastwalk.SkipFiles - } - if typ == os.ModeDir { - base := filepath.Base(path) - if base == "" || base[0] == '.' || base[0] == '_' || - base == "testdata" || - (w.root.Type == RootGOROOT && w.opts.ModulesEnabled && base == "vendor") || - (!w.opts.ModulesEnabled && base == "node_modules") { - return filepath.SkipDir - } - fi, err := os.Lstat(path) - if err == nil && w.shouldSkipDir(fi, path) { - return filepath.SkipDir - } - return nil - } - if typ == os.ModeSymlink { - base := filepath.Base(path) - if strings.HasPrefix(base, ".#") { - // Emacs noise. - return nil - } - fi, err := os.Lstat(path) - if err != nil { - // Just ignore it. - return nil - } - if w.shouldTraverse(dir, fi) { - return fastwalk.TraverseLink - } - } - return nil -} - -// shouldTraverse reports whether the symlink fi, found in dir, -// should be followed. It makes sure symlinks were never visited -// before to avoid symlink loops. -func (w *walker) shouldTraverse(dir string, fi os.FileInfo) bool { - path := filepath.Join(dir, fi.Name()) - target, err := filepath.EvalSymlinks(path) - if err != nil { - return false - } - ts, err := os.Stat(target) - if err != nil { - fmt.Fprintln(os.Stderr, err) - return false - } - if !ts.IsDir() { - return false - } - if w.shouldSkipDir(ts, dir) { - return false - } - // Check for symlink loops by statting each directory component - // and seeing if any are the same file as ts. - for { - parent := filepath.Dir(path) - if parent == path { - // Made it to the root without seeing a cycle. - // Use this symlink. - return true - } - parentInfo, err := os.Stat(parent) - if err != nil { - return false - } - if os.SameFile(ts, parentInfo) { - // Cycle. Don't traverse. - return false - } - path = parent - } - -} diff --git a/vendor/golang.org/x/tools/internal/packagesinternal/packages.go b/vendor/golang.org/x/tools/internal/packagesinternal/packages.go new file mode 100644 index 00000000000..b13ce33a39f --- /dev/null +++ b/vendor/golang.org/x/tools/internal/packagesinternal/packages.go @@ -0,0 +1,27 @@ +// Package packagesinternal exposes internal-only fields from go/packages. +package packagesinternal + +import "time" + +// Fields must match go list; +type Module struct { + Path string // module path + Version string // module version + Versions []string // available module versions (with -versions) + Replace *Module // replaced by this module + Time *time.Time // time version was created + Update *Module // available update, if any (with -u) + Main bool // is this the main module? + Indirect bool // is this module only an indirect dependency of main module? + Dir string // directory holding files for this module, if any + GoMod string // path to go.mod file used when loading this module, if any + GoVersion string // go version used in module + Error *ModuleError // error loading module +} +type ModuleError struct { + Err string // the error itself +} + +var GetForTest = func(p interface{}) string { return "" } + +var GetModule = func(p interface{}) *Module { return nil } diff --git a/vendor/golang.org/x/tools/present/args.go b/vendor/golang.org/x/tools/present/args.go deleted file mode 100644 index d63196e028c..00000000000 --- a/vendor/golang.org/x/tools/present/args.go +++ /dev/null @@ -1,228 +0,0 @@ -// Copyright 2012 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package present - -import ( - "errors" - "regexp" - "strconv" - "unicode/utf8" -) - -// This file is stolen from go/src/cmd/godoc/codewalk.go. -// It's an evaluator for the file address syntax implemented by acme and sam, -// but using Go-native regular expressions. -// To keep things reasonably close, this version uses (?m:re) for all user-provided -// regular expressions. That is the only change to the code from codewalk.go. -// See http://9p.io/sys/doc/sam/sam.html Table II for details on the syntax. - -// addrToByte evaluates the given address starting at offset start in data. -// It returns the lo and hi byte offset of the matched region within data. -func addrToByteRange(addr string, start int, data []byte) (lo, hi int, err error) { - if addr == "" { - lo, hi = start, len(data) - return - } - var ( - dir byte - prevc byte - charOffset bool - ) - lo = start - hi = start - for addr != "" && err == nil { - c := addr[0] - switch c { - default: - err = errors.New("invalid address syntax near " + string(c)) - case ',': - if len(addr) == 1 { - hi = len(data) - } else { - _, hi, err = addrToByteRange(addr[1:], hi, data) - } - return - - case '+', '-': - if prevc == '+' || prevc == '-' { - lo, hi, err = addrNumber(data, lo, hi, prevc, 1, charOffset) - } - dir = c - - case '$': - lo = len(data) - hi = len(data) - if len(addr) > 1 { - dir = '+' - } - - case '#': - charOffset = true - - case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': - var i int - for i = 1; i < len(addr); i++ { - if addr[i] < '0' || addr[i] > '9' { - break - } - } - var n int - n, err = strconv.Atoi(addr[0:i]) - if err != nil { - break - } - lo, hi, err = addrNumber(data, lo, hi, dir, n, charOffset) - dir = 0 - charOffset = false - prevc = c - addr = addr[i:] - continue - - case '/': - var i, j int - Regexp: - for i = 1; i < len(addr); i++ { - switch addr[i] { - case '\\': - i++ - case '/': - j = i + 1 - break Regexp - } - } - if j == 0 { - j = i - } - pattern := addr[1:i] - lo, hi, err = addrRegexp(data, lo, hi, dir, pattern) - prevc = c - addr = addr[j:] - continue - } - prevc = c - addr = addr[1:] - } - - if err == nil && dir != 0 { - lo, hi, err = addrNumber(data, lo, hi, dir, 1, charOffset) - } - if err != nil { - return 0, 0, err - } - return lo, hi, nil -} - -// addrNumber applies the given dir, n, and charOffset to the address lo, hi. -// dir is '+' or '-', n is the count, and charOffset is true if the syntax -// used was #n. Applying +n (or +#n) means to advance n lines -// (or characters) after hi. Applying -n (or -#n) means to back up n lines -// (or characters) before lo. -// The return value is the new lo, hi. -func addrNumber(data []byte, lo, hi int, dir byte, n int, charOffset bool) (int, int, error) { - switch dir { - case 0: - lo = 0 - hi = 0 - fallthrough - - case '+': - if charOffset { - pos := hi - for ; n > 0 && pos < len(data); n-- { - _, size := utf8.DecodeRune(data[pos:]) - pos += size - } - if n == 0 { - return pos, pos, nil - } - break - } - // find next beginning of line - if hi > 0 { - for hi < len(data) && data[hi-1] != '\n' { - hi++ - } - } - lo = hi - if n == 0 { - return lo, hi, nil - } - for ; hi < len(data); hi++ { - if data[hi] != '\n' { - continue - } - switch n--; n { - case 1: - lo = hi + 1 - case 0: - return lo, hi + 1, nil - } - } - - case '-': - if charOffset { - // Scan backward for bytes that are not UTF-8 continuation bytes. - pos := lo - for ; pos > 0 && n > 0; pos-- { - if data[pos]&0xc0 != 0x80 { - n-- - } - } - if n == 0 { - return pos, pos, nil - } - break - } - // find earlier beginning of line - for lo > 0 && data[lo-1] != '\n' { - lo-- - } - hi = lo - if n == 0 { - return lo, hi, nil - } - for ; lo >= 0; lo-- { - if lo > 0 && data[lo-1] != '\n' { - continue - } - switch n--; n { - case 1: - hi = lo - case 0: - return lo, hi, nil - } - } - } - - return 0, 0, errors.New("address out of range") -} - -// addrRegexp searches for pattern in the given direction starting at lo, hi. -// The direction dir is '+' (search forward from hi) or '-' (search backward from lo). -// Backward searches are unimplemented. -func addrRegexp(data []byte, lo, hi int, dir byte, pattern string) (int, int, error) { - // We want ^ and $ to work as in sam/acme, so use ?m. - re, err := regexp.Compile("(?m:" + pattern + ")") - if err != nil { - return 0, 0, err - } - if dir == '-' { - // Could implement reverse search using binary search - // through file, but that seems like overkill. - return 0, 0, errors.New("reverse search not implemented") - } - m := re.FindIndex(data[hi:]) - if len(m) > 0 { - m[0] += hi - m[1] += hi - } else if hi > 0 { - // No match. Wrap to beginning of data. - m = re.FindIndex(data) - } - if len(m) == 0 { - return 0, 0, errors.New("no match for " + pattern) - } - return m[0], m[1], nil -} diff --git a/vendor/golang.org/x/tools/present/caption.go b/vendor/golang.org/x/tools/present/caption.go deleted file mode 100644 index 00e0b5d0586..00000000000 --- a/vendor/golang.org/x/tools/present/caption.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2012 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package present - -import "strings" - -func init() { - Register("caption", parseCaption) -} - -type Caption struct { - Text string -} - -func (c Caption) TemplateName() string { return "caption" } - -func parseCaption(_ *Context, _ string, _ int, text string) (Elem, error) { - text = strings.TrimSpace(strings.TrimPrefix(text, ".caption")) - return Caption{text}, nil -} diff --git a/vendor/golang.org/x/tools/present/code.go b/vendor/golang.org/x/tools/present/code.go deleted file mode 100644 index b47a72a5069..00000000000 --- a/vendor/golang.org/x/tools/present/code.go +++ /dev/null @@ -1,267 +0,0 @@ -// Copyright 2012 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package present - -import ( - "bufio" - "bytes" - "fmt" - "html/template" - "path/filepath" - "regexp" - "strconv" - "strings" -) - -// PlayEnabled specifies whether runnable playground snippets should be -// displayed in the present user interface. -var PlayEnabled = false - -// TODO(adg): replace the PlayEnabled flag with something less spaghetti-like. -// Instead this will probably be determined by a template execution Context -// value that contains various global metadata required when rendering -// templates. - -// NotesEnabled specifies whether presenter notes should be displayed in the -// present user interface. -var NotesEnabled = false - -func init() { - Register("code", parseCode) - Register("play", parseCode) -} - -type Code struct { - Text template.HTML - Play bool // runnable code - Edit bool // editable code - FileName string // file name - Ext string // file extension - Raw []byte // content of the file -} - -func (c Code) TemplateName() string { return "code" } - -// The input line is a .code or .play entry with a file name and an optional HLfoo marker on the end. -// Anything between the file and HL (if any) is an address expression, which we treat as a string here. -// We pick off the HL first, for easy parsing. -var ( - highlightRE = regexp.MustCompile(`\s+HL([a-zA-Z0-9_]+)?$`) - hlCommentRE = regexp.MustCompile(`(.+) // HL(.*)$`) - codeRE = regexp.MustCompile(`\.(code|play)\s+((?:(?:-edit|-numbers)\s+)*)([^\s]+)(?:\s+(.*))?$`) -) - -// parseCode parses a code present directive. Its syntax: -// .code [-numbers] [-edit] [address] [highlight] -// The directive may also be ".play" if the snippet is executable. -func parseCode(ctx *Context, sourceFile string, sourceLine int, cmd string) (Elem, error) { - cmd = strings.TrimSpace(cmd) - - // Pull off the HL, if any, from the end of the input line. - highlight := "" - if hl := highlightRE.FindStringSubmatchIndex(cmd); len(hl) == 4 { - if hl[2] < 0 || hl[3] < 0 { - return nil, fmt.Errorf("%s:%d invalid highlight syntax", sourceFile, sourceLine) - } - highlight = cmd[hl[2]:hl[3]] - cmd = cmd[:hl[2]-2] - } - - // Parse the remaining command line. - // Arguments: - // args[0]: whole match - // args[1]: .code/.play - // args[2]: flags ("-edit -numbers") - // args[3]: file name - // args[4]: optional address - args := codeRE.FindStringSubmatch(cmd) - if len(args) != 5 { - return nil, fmt.Errorf("%s:%d: syntax error for .code/.play invocation", sourceFile, sourceLine) - } - command, flags, file, addr := args[1], args[2], args[3], strings.TrimSpace(args[4]) - play := command == "play" && PlayEnabled - - // Read in code file and (optionally) match address. - filename := filepath.Join(filepath.Dir(sourceFile), file) - textBytes, err := ctx.ReadFile(filename) - if err != nil { - return nil, fmt.Errorf("%s:%d: %v", sourceFile, sourceLine, err) - } - lo, hi, err := addrToByteRange(addr, 0, textBytes) - if err != nil { - return nil, fmt.Errorf("%s:%d: %v", sourceFile, sourceLine, err) - } - if lo > hi { - // The search in addrToByteRange can wrap around so we might - // end up with the range ending before its starting point - hi, lo = lo, hi - } - - // Acme pattern matches can stop mid-line, - // so run to end of line in both directions if not at line start/end. - for lo > 0 && textBytes[lo-1] != '\n' { - lo-- - } - if hi > 0 { - for hi < len(textBytes) && textBytes[hi-1] != '\n' { - hi++ - } - } - - lines := codeLines(textBytes, lo, hi) - - data := &codeTemplateData{ - Lines: formatLines(lines, highlight), - Edit: strings.Contains(flags, "-edit"), - Numbers: strings.Contains(flags, "-numbers"), - } - - // Include before and after in a hidden span for playground code. - if play { - data.Prefix = textBytes[:lo] - data.Suffix = textBytes[hi:] - } - - var buf bytes.Buffer - if err := codeTemplate.Execute(&buf, data); err != nil { - return nil, err - } - return Code{ - Text: template.HTML(buf.String()), - Play: play, - Edit: data.Edit, - FileName: filepath.Base(filename), - Ext: filepath.Ext(filename), - Raw: rawCode(lines), - }, nil -} - -// formatLines returns a new slice of codeLine with the given lines -// replacing tabs with spaces and adding highlighting where needed. -func formatLines(lines []codeLine, highlight string) []codeLine { - formatted := make([]codeLine, len(lines)) - for i, line := range lines { - // Replace tabs with spaces, which work better in HTML. - line.L = strings.Replace(line.L, "\t", " ", -1) - - // Highlight lines that end with "// HL[highlight]" - // and strip the magic comment. - if m := hlCommentRE.FindStringSubmatch(line.L); m != nil { - line.L = m[1] - line.HL = m[2] == highlight - } - - formatted[i] = line - } - return formatted -} - -// rawCode returns the code represented by the given codeLines without any kind -// of formatting. -func rawCode(lines []codeLine) []byte { - b := new(bytes.Buffer) - for _, line := range lines { - b.WriteString(line.L) - b.WriteByte('\n') - } - return b.Bytes() -} - -type codeTemplateData struct { - Lines []codeLine - Prefix, Suffix []byte - Edit, Numbers bool -} - -var leadingSpaceRE = regexp.MustCompile(`^[ \t]*`) - -var codeTemplate = template.Must(template.New("code").Funcs(template.FuncMap{ - "trimSpace": strings.TrimSpace, - "leadingSpace": leadingSpaceRE.FindString, -}).Parse(codeTemplateHTML)) - -const codeTemplateHTML = ` -{{with .Prefix}}
{{printf "%s" .}}
{{end}} - -{{/* - */}}{{range .Lines}}{{/* - */}}{{if .HL}}{{leadingSpace .L}}{{trimSpace .L}}{{/* - */}}{{else}}{{.L}}{{end}}{{/* -*/}} -{{end}} - -{{with .Suffix}}
{{printf "%s" .}}
{{end}} -` - -// codeLine represents a line of code extracted from a source file. -type codeLine struct { - L string // The line of code. - N int // The line number from the source file. - HL bool // Whether the line should be highlighted. -} - -// codeLines takes a source file and returns the lines that -// span the byte range specified by start and end. -// It discards lines that end in "OMIT". -func codeLines(src []byte, start, end int) (lines []codeLine) { - startLine := 1 - for i, b := range src { - if i == start { - break - } - if b == '\n' { - startLine++ - } - } - s := bufio.NewScanner(bytes.NewReader(src[start:end])) - for n := startLine; s.Scan(); n++ { - l := s.Text() - if strings.HasSuffix(l, "OMIT") { - continue - } - lines = append(lines, codeLine{L: l, N: n}) - } - // Trim leading and trailing blank lines. - for len(lines) > 0 && len(lines[0].L) == 0 { - lines = lines[1:] - } - for len(lines) > 0 && len(lines[len(lines)-1].L) == 0 { - lines = lines[:len(lines)-1] - } - return -} - -func parseArgs(name string, line int, args []string) (res []interface{}, err error) { - res = make([]interface{}, len(args)) - for i, v := range args { - if len(v) == 0 { - return nil, fmt.Errorf("%s:%d bad code argument %q", name, line, v) - } - switch v[0] { - case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': - n, err := strconv.Atoi(v) - if err != nil { - return nil, fmt.Errorf("%s:%d bad code argument %q", name, line, v) - } - res[i] = n - case '/': - if len(v) < 2 || v[len(v)-1] != '/' { - return nil, fmt.Errorf("%s:%d bad code argument %q", name, line, v) - } - res[i] = v - case '$': - res[i] = "$" - case '_': - if len(v) == 1 { - // Do nothing; "_" indicates an intentionally empty parameter. - break - } - fallthrough - default: - return nil, fmt.Errorf("%s:%d bad code argument %q", name, line, v) - } - } - return -} diff --git a/vendor/golang.org/x/tools/present/doc.go b/vendor/golang.org/x/tools/present/doc.go deleted file mode 100644 index ff75d66c44c..00000000000 --- a/vendor/golang.org/x/tools/present/doc.go +++ /dev/null @@ -1,261 +0,0 @@ -// Copyright 2011 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -/* -The present file format - -Present files have the following format. The first non-blank non-comment -line is the title, so the header looks like - - Title of document - Subtitle of document - 15:04 2 Jan 2006 - Tags: foo, bar, baz - - Author Name - Job title, Company - joe@example.com - http://url/ - @twitter_name - -The subtitle, date, and tags lines are optional. - -The date line may be written without a time: - 2 Jan 2006 -In this case, the time will be interpreted as 10am UTC on that date. - -The tags line is a comma-separated list of tags that may be used to categorize -the document. - -The author section may contain a mixture of text, twitter names, and links. -For slide presentations, only the plain text lines will be displayed on the -first slide. - -Multiple presenters may be specified, separated by a blank line. - -After that come slides/sections, each after a blank line: - - * Title of slide or section (must have asterisk) - - Some Text - - ** Subsection - - - bullets - - more bullets - - a bullet with - - *** Sub-subsection - - Some More text - - Preformatted text - is indented (however you like) - - Further Text, including invocations like: - - .code x.go /^func main/,/^}/ - .play y.go - .image image.jpg - .background image.jpg - .iframe http://foo - .link http://foo label - .html file.html - .caption _Gopher_ by [[https://www.instagram.com/reneefrench/][Renée French]] - - Again, more text - -Blank lines are OK (not mandatory) after the title and after the -text. Text, bullets, and .code etc. are all optional; title is -not. - -Lines starting with # in column 1 are commentary. - -Fonts: - -Within the input for plain text or lists, text bracketed by font -markers will be presented in italic, bold, or program font. -Marker characters are _ (italic), * (bold) and ` (program font). -An opening marker must be preceded by a space or punctuation -character or else be at start of a line; similarly, a closing -marker must be followed by a space or punctuation character or -else be at the end of a line. Unmatched markers appear as plain text. -There must be no spaces between markers. Within marked text, -a single marker character becomes a space and a doubled single -marker quotes the marker character. - - _italic_ - *bold* - `program` - Markup—_especially_italic_text_—can easily be overused. - _Why_use_scoped__ptr_? Use plain ***ptr* instead. - -Inline links: - -Links can be included in any text with the form [[url][label]], or -[[url]] to use the URL itself as the label. - -Functions: - -A number of template functions are available through invocations -in the input text. Each such invocation contains a period as the -first character on the line, followed immediately by the name of -the function, followed by any arguments. A typical invocation might -be - .play demo.go /^func show/,/^}/ -(except that the ".play" must be at the beginning of the line and -not be indented like this.) - -Here follows a description of the functions: - -code: - -Injects program source into the output by extracting code from files -and injecting them as HTML-escaped
 blocks.  The argument is
-a file name followed by an optional address that specifies what
-section of the file to display. The address syntax is similar in
-its simplest form to that of ed, but comes from sam and is more
-general. See
-	https://plan9.io/sys/doc/sam/sam.html Table II
-for full details. The displayed block is always rounded out to a
-full line at both ends.
-
-If no pattern is present, the entire file is displayed.
-
-Any line in the program that ends with the four characters
-	OMIT
-is deleted from the source before inclusion, making it easy
-to write things like
-	.code test.go /START OMIT/,/END OMIT/
-to find snippets like this
-	tedious_code = boring_function()
-	// START OMIT
-	interesting_code = fascinating_function()
-	// END OMIT
-and see only this:
-	interesting_code = fascinating_function()
-
-Also, inside the displayed text a line that ends
-	// HL
-will be highlighted in the display. A highlighting mark may have a
-suffix word, such as
-	// HLxxx
-Such highlights are enabled only if the code invocation ends with
-"HL" followed by the word:
-	.code test.go /^type Foo/,/^}/ HLxxx
-
-The .code function may take one or more flags immediately preceding
-the filename. This command shows test.go in an editable text area:
-	.code -edit test.go
-This command shows test.go with line numbers:
-	.code -numbers test.go
-
-play:
-
-The function "play" is the same as "code" but puts a button
-on the displayed source so the program can be run from the browser.
-Although only the selected text is shown, all the source is included
-in the HTML output so it can be presented to the compiler.
-
-link:
-
-Create a hyperlink. The syntax is 1 or 2 space-separated arguments.
-The first argument is always the HTTP URL.  If there is a second
-argument, it is the text label to display for this link.
-
-	.link http://golang.org golang.org
-
-image:
-
-The template uses the function "image" to inject picture files.
-
-The syntax is simple: 1 or 3 space-separated arguments.
-The first argument is always the file name.
-If there are more arguments, they are the height and width;
-both must be present, or substituted with an underscore.
-Replacing a dimension argument with the underscore parameter
-preserves the aspect ratio of the image when scaling.
-
-	.image images/betsy.jpg 100 200
-
-	.image images/janet.jpg _ 300
-
-video:
-
-The template uses the function "video" to inject video files.
-
-The syntax is simple: 2 or 4 space-separated arguments.
-The first argument is always the file name.
-The second argument is always the file content-type.
-If there are more arguments, they are the height and width;
-both must be present, or substituted with an underscore.
-Replacing a dimension argument with the underscore parameter
-preserves the aspect ratio of the video when scaling.
-
-	.video videos/evangeline.mp4 video/mp4 400 600
-
-	.video videos/mabel.ogg video/ogg 500 _
-
-background:
-
-The template uses the function "background" to set the background image for
-a slide.  The only argument is the file name of the image.
-
-	.background images/susan.jpg
-
-caption:
-
-The template uses the function "caption" to inject figure captions.
-
-The text after ".caption" is embedded in a figcaption element after
-processing styling and links as in standard text lines.
-
-	.caption _Gopher_ by [[http://www.reneefrench.com][Renée French]]
-
-iframe:
-
-The function "iframe" injects iframes (pages inside pages).
-Its syntax is the same as that of image.
-
-html:
-
-The function html includes the contents of the specified file as
-unescaped HTML. This is useful for including custom HTML elements
-that cannot be created using only the slide format.
-It is your responsibility to make sure the included HTML is valid and safe.
-
-	.html file.html
-
-Presenter notes:
-
-Presenter notes may be enabled by appending the "-notes" flag when you run
-your "present" binary.
-
-This will allow you to open a second window by pressing 'N' from your browser
-displaying your slides. The second window is completely synced with your main
-window, except that presenter notes are only visible on the second window.
-
-Lines that begin with ": " are treated as presenter notes.
-
-	* Title of slide
-
-	Some Text
-
-	: Presenter notes (first paragraph)
-	: Presenter notes (subsequent paragraph(s))
-
-Notes may appear anywhere within the slide text. For example:
-
-	* Title of slide
-
-	: Presenter notes (first paragraph)
-
-	Some Text
-
-	: Presenter notes (subsequent paragraph(s))
-
-This has the same result as the example above.
-
-*/
-package present // import "golang.org/x/tools/present"
diff --git a/vendor/golang.org/x/tools/present/html.go b/vendor/golang.org/x/tools/present/html.go
deleted file mode 100644
index cca90ef4af1..00000000000
--- a/vendor/golang.org/x/tools/present/html.go
+++ /dev/null
@@ -1,31 +0,0 @@
-package present
-
-import (
-	"errors"
-	"html/template"
-	"path/filepath"
-	"strings"
-)
-
-func init() {
-	Register("html", parseHTML)
-}
-
-func parseHTML(ctx *Context, fileName string, lineno int, text string) (Elem, error) {
-	p := strings.Fields(text)
-	if len(p) != 2 {
-		return nil, errors.New("invalid .html args")
-	}
-	name := filepath.Join(filepath.Dir(fileName), p[1])
-	b, err := ctx.ReadFile(name)
-	if err != nil {
-		return nil, err
-	}
-	return HTML{template.HTML(b)}, nil
-}
-
-type HTML struct {
-	template.HTML
-}
-
-func (s HTML) TemplateName() string { return "html" }
diff --git a/vendor/golang.org/x/tools/present/iframe.go b/vendor/golang.org/x/tools/present/iframe.go
deleted file mode 100644
index 057d2295858..00000000000
--- a/vendor/golang.org/x/tools/present/iframe.go
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright 2013 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package present
-
-import (
-	"fmt"
-	"strings"
-)
-
-func init() {
-	Register("iframe", parseIframe)
-}
-
-type Iframe struct {
-	URL    string
-	Width  int
-	Height int
-}
-
-func (i Iframe) TemplateName() string { return "iframe" }
-
-func parseIframe(ctx *Context, fileName string, lineno int, text string) (Elem, error) {
-	args := strings.Fields(text)
-	if len(args) < 2 {
-		return nil, fmt.Errorf("incorrect iframe invocation: %q", text)
-	}
-	i := Iframe{URL: args[1]}
-	a, err := parseArgs(fileName, lineno, args[2:])
-	if err != nil {
-		return nil, err
-	}
-	switch len(a) {
-	case 0:
-		// no size parameters
-	case 2:
-		if v, ok := a[0].(int); ok {
-			i.Height = v
-		}
-		if v, ok := a[1].(int); ok {
-			i.Width = v
-		}
-	default:
-		return nil, fmt.Errorf("incorrect iframe invocation: %q", text)
-	}
-	return i, nil
-}
diff --git a/vendor/golang.org/x/tools/present/image.go b/vendor/golang.org/x/tools/present/image.go
deleted file mode 100644
index 84965cae544..00000000000
--- a/vendor/golang.org/x/tools/present/image.go
+++ /dev/null
@@ -1,53 +0,0 @@
-// Copyright 2012 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package present
-
-import (
-	"fmt"
-	"strings"
-)
-
-func init() {
-	Register("image", parseImage)
-}
-
-type Image struct {
-	URL    string
-	Width  int
-	Height int
-}
-
-func (i Image) TemplateName() string { return "image" }
-
-func parseImage(ctx *Context, fileName string, lineno int, text string) (Elem, error) {
-	args := strings.Fields(text)
-	if len(args) < 2 {
-		return nil, fmt.Errorf("incorrect image invocation: %q", text)
-	}
-	img := Image{URL: args[1]}
-	a, err := parseArgs(fileName, lineno, args[2:])
-	if err != nil {
-		return nil, err
-	}
-	switch len(a) {
-	case 0:
-		// no size parameters
-	case 2:
-		// If a parameter is empty (underscore) or invalid
-		// leave the field set to zero. The "image" action
-		// template will then omit that img tag attribute and
-		// the browser will calculate the value to preserve
-		// the aspect ratio.
-		if v, ok := a[0].(int); ok {
-			img.Height = v
-		}
-		if v, ok := a[1].(int); ok {
-			img.Width = v
-		}
-	default:
-		return nil, fmt.Errorf("incorrect image invocation: %q", text)
-	}
-	return img, nil
-}
diff --git a/vendor/golang.org/x/tools/present/link.go b/vendor/golang.org/x/tools/present/link.go
deleted file mode 100644
index 2aead352e9c..00000000000
--- a/vendor/golang.org/x/tools/present/link.go
+++ /dev/null
@@ -1,100 +0,0 @@
-// Copyright 2012 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package present
-
-import (
-	"fmt"
-	"log"
-	"net/url"
-	"strings"
-)
-
-func init() {
-	Register("link", parseLink)
-}
-
-type Link struct {
-	URL   *url.URL
-	Label string
-}
-
-func (l Link) TemplateName() string { return "link" }
-
-func parseLink(ctx *Context, fileName string, lineno int, text string) (Elem, error) {
-	args := strings.Fields(text)
-	if len(args) < 2 {
-		return nil, fmt.Errorf("link element must have at least 2 arguments")
-	}
-	url, err := url.Parse(args[1])
-	if err != nil {
-		return nil, err
-	}
-	label := ""
-	if len(args) > 2 {
-		label = strings.Join(args[2:], " ")
-	} else {
-		scheme := url.Scheme + "://"
-		if url.Scheme == "mailto" {
-			scheme = "mailto:"
-		}
-		label = strings.Replace(url.String(), scheme, "", 1)
-	}
-	return Link{url, label}, nil
-}
-
-func renderLink(href, text string) string {
-	text = font(text)
-	if text == "" {
-		text = href
-	}
-	// Open links in new window only when their url is absolute.
-	target := "_blank"
-	if u, err := url.Parse(href); err != nil {
-		log.Println("renderLink parsing url:", err)
-	} else if !u.IsAbs() || u.Scheme == "javascript" {
-		target = "_self"
-	}
-
-	return fmt.Sprintf(`%s`, href, target, text)
-}
-
-// parseInlineLink parses an inline link at the start of s, and returns
-// a rendered HTML link and the total length of the raw inline link.
-// If no inline link is present, it returns all zeroes.
-func parseInlineLink(s string) (link string, length int) {
-	if !strings.HasPrefix(s, "[[") {
-		return
-	}
-	end := strings.Index(s, "]]")
-	if end == -1 {
-		return
-	}
-	urlEnd := strings.Index(s, "]")
-	rawURL := s[2:urlEnd]
-	const badURLChars = `<>"{}|\^[] ` + "`" // per RFC2396 section 2.4.3
-	if strings.ContainsAny(rawURL, badURLChars) {
-		return
-	}
-	if urlEnd == end {
-		simpleUrl := ""
-		url, err := url.Parse(rawURL)
-		if err == nil {
-			// If the URL is http://foo.com, drop the http://
-			// In other words, render [[http://golang.org]] as:
-			//   golang.org
-			if strings.HasPrefix(rawURL, url.Scheme+"://") {
-				simpleUrl = strings.TrimPrefix(rawURL, url.Scheme+"://")
-			} else if strings.HasPrefix(rawURL, url.Scheme+":") {
-				simpleUrl = strings.TrimPrefix(rawURL, url.Scheme+":")
-			}
-		}
-		return renderLink(rawURL, simpleUrl), end + 2
-	}
-	if s[urlEnd:urlEnd+2] != "][" {
-		return
-	}
-	text := s[urlEnd+2 : end]
-	return renderLink(rawURL, text), end + 2
-}
diff --git a/vendor/golang.org/x/tools/present/parse.go b/vendor/golang.org/x/tools/present/parse.go
deleted file mode 100644
index dd0f00b27ba..00000000000
--- a/vendor/golang.org/x/tools/present/parse.go
+++ /dev/null
@@ -1,568 +0,0 @@
-// Copyright 2011 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package present
-
-import (
-	"bufio"
-	"bytes"
-	"errors"
-	"fmt"
-	"html/template"
-	"io"
-	"io/ioutil"
-	"log"
-	"net/url"
-	"regexp"
-	"strings"
-	"time"
-	"unicode"
-	"unicode/utf8"
-)
-
-var (
-	parsers = make(map[string]ParseFunc)
-	funcs   = template.FuncMap{}
-)
-
-// Template returns an empty template with the action functions in its FuncMap.
-func Template() *template.Template {
-	return template.New("").Funcs(funcs)
-}
-
-// Render renders the doc to the given writer using the provided template.
-func (d *Doc) Render(w io.Writer, t *template.Template) error {
-	data := struct {
-		*Doc
-		Template     *template.Template
-		PlayEnabled  bool
-		NotesEnabled bool
-	}{d, t, PlayEnabled, NotesEnabled}
-	return t.ExecuteTemplate(w, "root", data)
-}
-
-// Render renders the section to the given writer using the provided template.
-func (s *Section) Render(w io.Writer, t *template.Template) error {
-	data := struct {
-		*Section
-		Template    *template.Template
-		PlayEnabled bool
-	}{s, t, PlayEnabled}
-	return t.ExecuteTemplate(w, "section", data)
-}
-
-type ParseFunc func(ctx *Context, fileName string, lineNumber int, inputLine string) (Elem, error)
-
-// Register binds the named action, which does not begin with a period, to the
-// specified parser to be invoked when the name, with a period, appears in the
-// present input text.
-func Register(name string, parser ParseFunc) {
-	if len(name) == 0 || name[0] == ';' {
-		panic("bad name in Register: " + name)
-	}
-	parsers["."+name] = parser
-}
-
-// Doc represents an entire document.
-type Doc struct {
-	Title      string
-	Subtitle   string
-	Time       time.Time
-	Authors    []Author
-	TitleNotes []string
-	Sections   []Section
-	Tags       []string
-}
-
-// Author represents the person who wrote and/or is presenting the document.
-type Author struct {
-	Elem []Elem
-}
-
-// TextElem returns the first text elements of the author details.
-// This is used to display the author' name, job title, and company
-// without the contact details.
-func (p *Author) TextElem() (elems []Elem) {
-	for _, el := range p.Elem {
-		if _, ok := el.(Text); !ok {
-			break
-		}
-		elems = append(elems, el)
-	}
-	return
-}
-
-// Section represents a section of a document (such as a presentation slide)
-// comprising a title and a list of elements.
-type Section struct {
-	Number  []int
-	Title   string
-	Elem    []Elem
-	Notes   []string
-	Classes []string
-	Styles  []string
-}
-
-// HTMLAttributes for the section
-func (s Section) HTMLAttributes() template.HTMLAttr {
-	if len(s.Classes) == 0 && len(s.Styles) == 0 {
-		return ""
-	}
-
-	var class string
-	if len(s.Classes) > 0 {
-		class = fmt.Sprintf(`class=%q`, strings.Join(s.Classes, " "))
-	}
-	var style string
-	if len(s.Styles) > 0 {
-		style = fmt.Sprintf(`style=%q`, strings.Join(s.Styles, " "))
-	}
-	return template.HTMLAttr(strings.Join([]string{class, style}, " "))
-}
-
-// Sections contained within the section.
-func (s Section) Sections() (sections []Section) {
-	for _, e := range s.Elem {
-		if section, ok := e.(Section); ok {
-			sections = append(sections, section)
-		}
-	}
-	return
-}
-
-// Level returns the level of the given section.
-// The document title is level 1, main section 2, etc.
-func (s Section) Level() int {
-	return len(s.Number) + 1
-}
-
-// FormattedNumber returns a string containing the concatenation of the
-// numbers identifying a Section.
-func (s Section) FormattedNumber() string {
-	b := &bytes.Buffer{}
-	for _, n := range s.Number {
-		fmt.Fprintf(b, "%v.", n)
-	}
-	return b.String()
-}
-
-func (s Section) TemplateName() string { return "section" }
-
-// Elem defines the interface for a present element. That is, something that
-// can provide the name of the template used to render the element.
-type Elem interface {
-	TemplateName() string
-}
-
-// renderElem implements the elem template function, used to render
-// sub-templates.
-func renderElem(t *template.Template, e Elem) (template.HTML, error) {
-	var data interface{} = e
-	if s, ok := e.(Section); ok {
-		data = struct {
-			Section
-			Template *template.Template
-		}{s, t}
-	}
-	return execTemplate(t, e.TemplateName(), data)
-}
-
-// pageNum derives a page number from a section.
-func pageNum(s Section, offset int) int {
-	if len(s.Number) == 0 {
-		return offset
-	}
-	return s.Number[0] + offset
-}
-
-func init() {
-	funcs["elem"] = renderElem
-	funcs["pagenum"] = pageNum
-}
-
-// execTemplate is a helper to execute a template and return the output as a
-// template.HTML value.
-func execTemplate(t *template.Template, name string, data interface{}) (template.HTML, error) {
-	b := new(bytes.Buffer)
-	err := t.ExecuteTemplate(b, name, data)
-	if err != nil {
-		return "", err
-	}
-	return template.HTML(b.String()), nil
-}
-
-// Text represents an optionally preformatted paragraph.
-type Text struct {
-	Lines []string
-	Pre   bool
-}
-
-func (t Text) TemplateName() string { return "text" }
-
-// List represents a bulleted list.
-type List struct {
-	Bullet []string
-}
-
-func (l List) TemplateName() string { return "list" }
-
-// Lines is a helper for parsing line-based input.
-type Lines struct {
-	line int // 0 indexed, so has 1-indexed number of last line returned
-	text []string
-}
-
-func readLines(r io.Reader) (*Lines, error) {
-	var lines []string
-	s := bufio.NewScanner(r)
-	for s.Scan() {
-		lines = append(lines, s.Text())
-	}
-	if err := s.Err(); err != nil {
-		return nil, err
-	}
-	return &Lines{0, lines}, nil
-}
-
-func (l *Lines) next() (text string, ok bool) {
-	for {
-		current := l.line
-		l.line++
-		if current >= len(l.text) {
-			return "", false
-		}
-		text = l.text[current]
-		// Lines starting with # are comments.
-		if len(text) == 0 || text[0] != '#' {
-			ok = true
-			break
-		}
-	}
-	return
-}
-
-func (l *Lines) back() {
-	l.line--
-}
-
-func (l *Lines) nextNonEmpty() (text string, ok bool) {
-	for {
-		text, ok = l.next()
-		if !ok {
-			return
-		}
-		if len(text) > 0 {
-			break
-		}
-	}
-	return
-}
-
-// A Context specifies the supporting context for parsing a presentation.
-type Context struct {
-	// ReadFile reads the file named by filename and returns the contents.
-	ReadFile func(filename string) ([]byte, error)
-}
-
-// ParseMode represents flags for the Parse function.
-type ParseMode int
-
-const (
-	// If set, parse only the title and subtitle.
-	TitlesOnly ParseMode = 1
-)
-
-// Parse parses a document from r.
-func (ctx *Context) Parse(r io.Reader, name string, mode ParseMode) (*Doc, error) {
-	doc := new(Doc)
-	lines, err := readLines(r)
-	if err != nil {
-		return nil, err
-	}
-
-	for i := lines.line; i < len(lines.text); i++ {
-		if strings.HasPrefix(lines.text[i], "*") {
-			break
-		}
-
-		if isSpeakerNote(lines.text[i]) {
-			doc.TitleNotes = append(doc.TitleNotes, lines.text[i][2:])
-		}
-	}
-
-	err = parseHeader(doc, lines)
-	if err != nil {
-		return nil, err
-	}
-	if mode&TitlesOnly != 0 {
-		return doc, nil
-	}
-
-	// Authors
-	if doc.Authors, err = parseAuthors(lines); err != nil {
-		return nil, err
-	}
-	// Sections
-	if doc.Sections, err = parseSections(ctx, name, lines, []int{}); err != nil {
-		return nil, err
-	}
-	return doc, nil
-}
-
-// Parse parses a document from r. Parse reads assets used by the presentation
-// from the file system using ioutil.ReadFile.
-func Parse(r io.Reader, name string, mode ParseMode) (*Doc, error) {
-	ctx := Context{ReadFile: ioutil.ReadFile}
-	return ctx.Parse(r, name, mode)
-}
-
-// isHeading matches any section heading.
-var isHeading = regexp.MustCompile(`^\*+ `)
-
-// lesserHeading returns true if text is a heading of a lesser or equal level
-// than that denoted by prefix.
-func lesserHeading(text, prefix string) bool {
-	return isHeading.MatchString(text) && !strings.HasPrefix(text, prefix+"*")
-}
-
-// parseSections parses Sections from lines for the section level indicated by
-// number (a nil number indicates the top level).
-func parseSections(ctx *Context, name string, lines *Lines, number []int) ([]Section, error) {
-	var sections []Section
-	for i := 1; ; i++ {
-		// Next non-empty line is title.
-		text, ok := lines.nextNonEmpty()
-		for ok && text == "" {
-			text, ok = lines.next()
-		}
-		if !ok {
-			break
-		}
-		prefix := strings.Repeat("*", len(number)+1)
-		if !strings.HasPrefix(text, prefix+" ") {
-			lines.back()
-			break
-		}
-		section := Section{
-			Number: append(append([]int{}, number...), i),
-			Title:  text[len(prefix)+1:],
-		}
-		text, ok = lines.nextNonEmpty()
-		for ok && !lesserHeading(text, prefix) {
-			var e Elem
-			r, _ := utf8.DecodeRuneInString(text)
-			switch {
-			case unicode.IsSpace(r):
-				i := strings.IndexFunc(text, func(r rune) bool {
-					return !unicode.IsSpace(r)
-				})
-				if i < 0 {
-					break
-				}
-				indent := text[:i]
-				var s []string
-				for ok && (strings.HasPrefix(text, indent) || text == "") {
-					if text != "" {
-						text = text[i:]
-					}
-					s = append(s, text)
-					text, ok = lines.next()
-				}
-				lines.back()
-				pre := strings.Join(s, "\n")
-				pre = strings.Replace(pre, "\t", "    ", -1) // browsers treat tabs badly
-				pre = strings.TrimRightFunc(pre, unicode.IsSpace)
-				e = Text{Lines: []string{pre}, Pre: true}
-			case strings.HasPrefix(text, "- "):
-				var b []string
-				for ok && strings.HasPrefix(text, "- ") {
-					b = append(b, text[2:])
-					text, ok = lines.next()
-				}
-				lines.back()
-				e = List{Bullet: b}
-			case isSpeakerNote(text):
-				section.Notes = append(section.Notes, text[2:])
-			case strings.HasPrefix(text, prefix+"* "):
-				lines.back()
-				subsecs, err := parseSections(ctx, name, lines, section.Number)
-				if err != nil {
-					return nil, err
-				}
-				for _, ss := range subsecs {
-					section.Elem = append(section.Elem, ss)
-				}
-			case strings.HasPrefix(text, "."):
-				args := strings.Fields(text)
-				if args[0] == ".background" {
-					section.Classes = append(section.Classes, "background")
-					section.Styles = append(section.Styles, "background-image: url('"+args[1]+"')")
-					break
-				}
-				parser := parsers[args[0]]
-				if parser == nil {
-					return nil, fmt.Errorf("%s:%d: unknown command %q\n", name, lines.line, text)
-				}
-				t, err := parser(ctx, name, lines.line, text)
-				if err != nil {
-					return nil, err
-				}
-				e = t
-			default:
-				var l []string
-				for ok && strings.TrimSpace(text) != "" {
-					if text[0] == '.' { // Command breaks text block.
-						lines.back()
-						break
-					}
-					if strings.HasPrefix(text, `\.`) { // Backslash escapes initial period.
-						text = text[1:]
-					}
-					l = append(l, text)
-					text, ok = lines.next()
-				}
-				if len(l) > 0 {
-					e = Text{Lines: l}
-				}
-			}
-			if e != nil {
-				section.Elem = append(section.Elem, e)
-			}
-			text, ok = lines.nextNonEmpty()
-		}
-		if isHeading.MatchString(text) {
-			lines.back()
-		}
-		sections = append(sections, section)
-	}
-	return sections, nil
-}
-
-func parseHeader(doc *Doc, lines *Lines) error {
-	var ok bool
-	// First non-empty line starts header.
-	doc.Title, ok = lines.nextNonEmpty()
-	if !ok {
-		return errors.New("unexpected EOF; expected title")
-	}
-	for {
-		text, ok := lines.next()
-		if !ok {
-			return errors.New("unexpected EOF")
-		}
-		if text == "" {
-			break
-		}
-		if isSpeakerNote(text) {
-			continue
-		}
-		const tagPrefix = "Tags:"
-		if strings.HasPrefix(text, tagPrefix) {
-			tags := strings.Split(text[len(tagPrefix):], ",")
-			for i := range tags {
-				tags[i] = strings.TrimSpace(tags[i])
-			}
-			doc.Tags = append(doc.Tags, tags...)
-		} else if t, ok := parseTime(text); ok {
-			doc.Time = t
-		} else if doc.Subtitle == "" {
-			doc.Subtitle = text
-		} else {
-			return fmt.Errorf("unexpected header line: %q", text)
-		}
-	}
-	return nil
-}
-
-func parseAuthors(lines *Lines) (authors []Author, err error) {
-	// This grammar demarcates authors with blanks.
-
-	// Skip blank lines.
-	if _, ok := lines.nextNonEmpty(); !ok {
-		return nil, errors.New("unexpected EOF")
-	}
-	lines.back()
-
-	var a *Author
-	for {
-		text, ok := lines.next()
-		if !ok {
-			return nil, errors.New("unexpected EOF")
-		}
-
-		// If we find a section heading, we're done.
-		if strings.HasPrefix(text, "* ") {
-			lines.back()
-			break
-		}
-
-		if isSpeakerNote(text) {
-			continue
-		}
-
-		// If we encounter a blank we're done with this author.
-		if a != nil && len(text) == 0 {
-			authors = append(authors, *a)
-			a = nil
-			continue
-		}
-		if a == nil {
-			a = new(Author)
-		}
-
-		// Parse the line. Those that
-		// - begin with @ are twitter names,
-		// - contain slashes are links, or
-		// - contain an @ symbol are an email address.
-		// The rest is just text.
-		var el Elem
-		switch {
-		case strings.HasPrefix(text, "@"):
-			el = parseURL("http://twitter.com/" + text[1:])
-		case strings.Contains(text, ":"):
-			el = parseURL(text)
-		case strings.Contains(text, "@"):
-			el = parseURL("mailto:" + text)
-		}
-		if l, ok := el.(Link); ok {
-			l.Label = text
-			el = l
-		}
-		if el == nil {
-			el = Text{Lines: []string{text}}
-		}
-		a.Elem = append(a.Elem, el)
-	}
-	if a != nil {
-		authors = append(authors, *a)
-	}
-	return authors, nil
-}
-
-func parseURL(text string) Elem {
-	u, err := url.Parse(text)
-	if err != nil {
-		log.Printf("Parse(%q): %v", text, err)
-		return nil
-	}
-	return Link{URL: u}
-}
-
-func parseTime(text string) (t time.Time, ok bool) {
-	t, err := time.Parse("15:04 2 Jan 2006", text)
-	if err == nil {
-		return t, true
-	}
-	t, err = time.Parse("2 Jan 2006", text)
-	if err == nil {
-		// at 11am UTC it is the same date everywhere
-		t = t.Add(time.Hour * 11)
-		return t, true
-	}
-	return time.Time{}, false
-}
-
-func isSpeakerNote(s string) bool {
-	return strings.HasPrefix(s, ": ")
-}
diff --git a/vendor/golang.org/x/tools/present/style.go b/vendor/golang.org/x/tools/present/style.go
deleted file mode 100644
index e2c228e55ab..00000000000
--- a/vendor/golang.org/x/tools/present/style.go
+++ /dev/null
@@ -1,167 +0,0 @@
-// Copyright 2012 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package present
-
-import (
-	"bytes"
-	"html"
-	"html/template"
-	"strings"
-	"unicode"
-	"unicode/utf8"
-)
-
-/*
-	Fonts are demarcated by an initial and final char bracketing a
-	space-delimited word, plus possibly some terminal punctuation.
-	The chars are
-		_ for italic
-		* for bold
-		` (back quote) for fixed width.
-	Inner appearances of the char become spaces. For instance,
-		_this_is_italic_!
-	becomes
-		this is italic!
-*/
-
-func init() {
-	funcs["style"] = Style
-}
-
-// Style returns s with HTML entities escaped and font indicators turned into
-// HTML font tags.
-func Style(s string) template.HTML {
-	return template.HTML(font(html.EscapeString(s)))
-}
-
-// font returns s with font indicators turned into HTML font tags.
-func font(s string) string {
-	if !strings.ContainsAny(s, "[`_*") {
-		return s
-	}
-	words := split(s)
-	var b bytes.Buffer
-Word:
-	for w, word := range words {
-		if len(word) < 2 {
-			continue Word
-		}
-		if link, _ := parseInlineLink(word); link != "" {
-			words[w] = link
-			continue Word
-		}
-		const marker = "_*`"
-		// Initial punctuation is OK but must be peeled off.
-		first := strings.IndexAny(word, marker)
-		if first == -1 {
-			continue Word
-		}
-		// Opening marker must be at the beginning of the token or else preceded by punctuation.
-		if first != 0 {
-			r, _ := utf8.DecodeLastRuneInString(word[:first])
-			if !unicode.IsPunct(r) {
-				continue Word
-			}
-		}
-		open, word := word[:first], word[first:]
-		char := word[0] // ASCII is OK.
-		close := ""
-		switch char {
-		default:
-			continue Word
-		case '_':
-			open += ""
-			close = ""
-		case '*':
-			open += ""
-			close = ""
-		case '`':
-			open += ""
-			close = ""
-		}
-		// Closing marker must be at the end of the token or else followed by punctuation.
-		last := strings.LastIndex(word, word[:1])
-		if last == 0 {
-			continue Word
-		}
-		if last+1 != len(word) {
-			r, _ := utf8.DecodeRuneInString(word[last+1:])
-			if !unicode.IsPunct(r) {
-				continue Word
-			}
-		}
-		head, tail := word[:last+1], word[last+1:]
-		b.Reset()
-		b.WriteString(open)
-		var wid int
-		for i := 1; i < len(head)-1; i += wid {
-			var r rune
-			r, wid = utf8.DecodeRuneInString(head[i:])
-			if r != rune(char) {
-				// Ordinary character.
-				b.WriteRune(r)
-				continue
-			}
-			if head[i+1] != char {
-				// Inner char becomes space.
-				b.WriteRune(' ')
-				continue
-			}
-			// Doubled char becomes real char.
-			// Not worth worrying about "_x__".
-			b.WriteByte(char)
-			wid++ // Consumed two chars, both ASCII.
-		}
-		b.WriteString(close) // Write closing tag.
-		b.WriteString(tail)  // Restore trailing punctuation.
-		words[w] = b.String()
-	}
-	return strings.Join(words, "")
-}
-
-// split is like strings.Fields but also returns the runs of spaces
-// and treats inline links as distinct words.
-func split(s string) []string {
-	var (
-		words = make([]string, 0, 10)
-		start = 0
-	)
-
-	// appendWord appends the string s[start:end] to the words slice.
-	// If the word contains the beginning of a link, the non-link portion
-	// of the word and the entire link are appended as separate words,
-	// and the start index is advanced to the end of the link.
-	appendWord := func(end int) {
-		if j := strings.Index(s[start:end], "[["); j > -1 {
-			if _, l := parseInlineLink(s[start+j:]); l > 0 {
-				// Append portion before link, if any.
-				if j > 0 {
-					words = append(words, s[start:start+j])
-				}
-				// Append link itself.
-				words = append(words, s[start+j:start+j+l])
-				// Advance start index to end of link.
-				start = start + j + l
-				return
-			}
-		}
-		// No link; just add the word.
-		words = append(words, s[start:end])
-		start = end
-	}
-
-	wasSpace := false
-	for i, r := range s {
-		isSpace := unicode.IsSpace(r)
-		if i > start && isSpace != wasSpace {
-			appendWord(i)
-		}
-		wasSpace = isSpace
-	}
-	for start < len(s) {
-		appendWord(len(s))
-	}
-	return words
-}
diff --git a/vendor/golang.org/x/tools/present/video.go b/vendor/golang.org/x/tools/present/video.go
deleted file mode 100644
index 93d93502bf2..00000000000
--- a/vendor/golang.org/x/tools/present/video.go
+++ /dev/null
@@ -1,54 +0,0 @@
-// Copyright 2016 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package present
-
-import (
-	"fmt"
-	"strings"
-)
-
-func init() {
-	Register("video", parseVideo)
-}
-
-type Video struct {
-	URL        string
-	SourceType string
-	Width      int
-	Height     int
-}
-
-func (v Video) TemplateName() string { return "video" }
-
-func parseVideo(ctx *Context, fileName string, lineno int, text string) (Elem, error) {
-	args := strings.Fields(text)
-	if len(args) < 3 {
-		return nil, fmt.Errorf("incorrect video invocation: %q", text)
-	}
-	vid := Video{URL: args[1], SourceType: args[2]}
-	a, err := parseArgs(fileName, lineno, args[3:])
-	if err != nil {
-		return nil, err
-	}
-	switch len(a) {
-	case 0:
-		// no size parameters
-	case 2:
-		// If a parameter is empty (underscore) or invalid
-		// leave the field set to zero. The "video" action
-		// template will then omit that vid tag attribute and
-		// the browser will calculate the value to preserve
-		// the aspect ratio.
-		if v, ok := a[0].(int); ok {
-			vid.Height = v
-		}
-		if v, ok := a[1].(int); ok {
-			vid.Width = v
-		}
-	default:
-		return nil, fmt.Errorf("incorrect video invocation: %q", text)
-	}
-	return vid, nil
-}
diff --git a/vendor/golang.org/x/xerrors/LICENSE b/vendor/golang.org/x/xerrors/LICENSE
new file mode 100644
index 00000000000..e4a47e17f14
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/LICENSE
@@ -0,0 +1,27 @@
+Copyright (c) 2019 The Go Authors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+   * Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+   * Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the following disclaimer
+in the documentation and/or other materials provided with the
+distribution.
+   * Neither the name of Google Inc. nor the names of its
+contributors may be used to endorse or promote products derived from
+this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/vendor/golang.org/x/xerrors/PATENTS b/vendor/golang.org/x/xerrors/PATENTS
new file mode 100644
index 00000000000..733099041f8
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/PATENTS
@@ -0,0 +1,22 @@
+Additional IP Rights Grant (Patents)
+
+"This implementation" means the copyrightable works distributed by
+Google as part of the Go project.
+
+Google hereby grants to You a perpetual, worldwide, non-exclusive,
+no-charge, royalty-free, irrevocable (except as stated in this section)
+patent license to make, have made, use, offer to sell, sell, import,
+transfer and otherwise run, modify and propagate the contents of this
+implementation of Go, where such license applies only to those patent
+claims, both currently owned or controlled by Google and acquired in
+the future, licensable by Google that are necessarily infringed by this
+implementation of Go.  This grant does not include claims that would be
+infringed only as a consequence of further modification of this
+implementation.  If you or your agent or exclusive licensee institute or
+order or agree to the institution of patent litigation against any
+entity (including a cross-claim or counterclaim in a lawsuit) alleging
+that this implementation of Go or any code incorporated within this
+implementation of Go constitutes direct or contributory patent
+infringement, or inducement of patent infringement, then any patent
+rights granted to you under this License for this implementation of Go
+shall terminate as of the date such litigation is filed.
diff --git a/vendor/golang.org/x/xerrors/README b/vendor/golang.org/x/xerrors/README
new file mode 100644
index 00000000000..aac7867a560
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/README
@@ -0,0 +1,2 @@
+This repository holds the transition packages for the new Go 1.13 error values.
+See golang.org/design/29934-error-values.
diff --git a/vendor/golang.org/x/xerrors/adaptor.go b/vendor/golang.org/x/xerrors/adaptor.go
new file mode 100644
index 00000000000..4317f248331
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/adaptor.go
@@ -0,0 +1,193 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package xerrors
+
+import (
+	"bytes"
+	"fmt"
+	"io"
+	"reflect"
+	"strconv"
+)
+
+// FormatError calls the FormatError method of f with an errors.Printer
+// configured according to s and verb, and writes the result to s.
+func FormatError(f Formatter, s fmt.State, verb rune) {
+	// Assuming this function is only called from the Format method, and given
+	// that FormatError takes precedence over Format, it cannot be called from
+	// any package that supports errors.Formatter. It is therefore safe to
+	// disregard that State may be a specific printer implementation and use one
+	// of our choice instead.
+
+	// limitations: does not support printing error as Go struct.
+
+	var (
+		sep    = " " // separator before next error
+		p      = &state{State: s}
+		direct = true
+	)
+
+	var err error = f
+
+	switch verb {
+	// Note that this switch must match the preference order
+	// for ordinary string printing (%#v before %+v, and so on).
+
+	case 'v':
+		if s.Flag('#') {
+			if stringer, ok := err.(fmt.GoStringer); ok {
+				io.WriteString(&p.buf, stringer.GoString())
+				goto exit
+			}
+			// proceed as if it were %v
+		} else if s.Flag('+') {
+			p.printDetail = true
+			sep = "\n  - "
+		}
+	case 's':
+	case 'q', 'x', 'X':
+		// Use an intermediate buffer in the rare cases that precision,
+		// truncation, or one of the alternative verbs (q, x, and X) are
+		// specified.
+		direct = false
+
+	default:
+		p.buf.WriteString("%!")
+		p.buf.WriteRune(verb)
+		p.buf.WriteByte('(')
+		switch {
+		case err != nil:
+			p.buf.WriteString(reflect.TypeOf(f).String())
+		default:
+			p.buf.WriteString("")
+		}
+		p.buf.WriteByte(')')
+		io.Copy(s, &p.buf)
+		return
+	}
+
+loop:
+	for {
+		switch v := err.(type) {
+		case Formatter:
+			err = v.FormatError((*printer)(p))
+		case fmt.Formatter:
+			v.Format(p, 'v')
+			break loop
+		default:
+			io.WriteString(&p.buf, v.Error())
+			break loop
+		}
+		if err == nil {
+			break
+		}
+		if p.needColon || !p.printDetail {
+			p.buf.WriteByte(':')
+			p.needColon = false
+		}
+		p.buf.WriteString(sep)
+		p.inDetail = false
+		p.needNewline = false
+	}
+
+exit:
+	width, okW := s.Width()
+	prec, okP := s.Precision()
+
+	if !direct || (okW && width > 0) || okP {
+		// Construct format string from State s.
+		format := []byte{'%'}
+		if s.Flag('-') {
+			format = append(format, '-')
+		}
+		if s.Flag('+') {
+			format = append(format, '+')
+		}
+		if s.Flag(' ') {
+			format = append(format, ' ')
+		}
+		if okW {
+			format = strconv.AppendInt(format, int64(width), 10)
+		}
+		if okP {
+			format = append(format, '.')
+			format = strconv.AppendInt(format, int64(prec), 10)
+		}
+		format = append(format, string(verb)...)
+		fmt.Fprintf(s, string(format), p.buf.String())
+	} else {
+		io.Copy(s, &p.buf)
+	}
+}
+
+var detailSep = []byte("\n    ")
+
+// state tracks error printing state. It implements fmt.State.
+type state struct {
+	fmt.State
+	buf bytes.Buffer
+
+	printDetail bool
+	inDetail    bool
+	needColon   bool
+	needNewline bool
+}
+
+func (s *state) Write(b []byte) (n int, err error) {
+	if s.printDetail {
+		if len(b) == 0 {
+			return 0, nil
+		}
+		if s.inDetail && s.needColon {
+			s.needNewline = true
+			if b[0] == '\n' {
+				b = b[1:]
+			}
+		}
+		k := 0
+		for i, c := range b {
+			if s.needNewline {
+				if s.inDetail && s.needColon {
+					s.buf.WriteByte(':')
+					s.needColon = false
+				}
+				s.buf.Write(detailSep)
+				s.needNewline = false
+			}
+			if c == '\n' {
+				s.buf.Write(b[k:i])
+				k = i + 1
+				s.needNewline = true
+			}
+		}
+		s.buf.Write(b[k:])
+		if !s.inDetail {
+			s.needColon = true
+		}
+	} else if !s.inDetail {
+		s.buf.Write(b)
+	}
+	return len(b), nil
+}
+
+// printer wraps a state to implement an xerrors.Printer.
+type printer state
+
+func (s *printer) Print(args ...interface{}) {
+	if !s.inDetail || s.printDetail {
+		fmt.Fprint((*state)(s), args...)
+	}
+}
+
+func (s *printer) Printf(format string, args ...interface{}) {
+	if !s.inDetail || s.printDetail {
+		fmt.Fprintf((*state)(s), format, args...)
+	}
+}
+
+func (s *printer) Detail() bool {
+	s.inDetail = true
+	return s.printDetail
+}
diff --git a/vendor/golang.org/x/xerrors/codereview.cfg b/vendor/golang.org/x/xerrors/codereview.cfg
new file mode 100644
index 00000000000..3f8b14b64e8
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/codereview.cfg
@@ -0,0 +1 @@
+issuerepo: golang/go
diff --git a/vendor/golang.org/x/xerrors/doc.go b/vendor/golang.org/x/xerrors/doc.go
new file mode 100644
index 00000000000..eef99d9d54d
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/doc.go
@@ -0,0 +1,22 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package xerrors implements functions to manipulate errors.
+//
+// This package is based on the Go 2 proposal for error values:
+//   https://golang.org/design/29934-error-values
+//
+// These functions were incorporated into the standard library's errors package
+// in Go 1.13:
+// - Is
+// - As
+// - Unwrap
+//
+// Also, Errorf's %w verb was incorporated into fmt.Errorf.
+//
+// Use this package to get equivalent behavior in all supported Go versions.
+//
+// No other features of this package were included in Go 1.13, and at present
+// there are no plans to include any of them.
+package xerrors // import "golang.org/x/xerrors"
diff --git a/vendor/golang.org/x/xerrors/errors.go b/vendor/golang.org/x/xerrors/errors.go
new file mode 100644
index 00000000000..e88d3772d86
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/errors.go
@@ -0,0 +1,33 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package xerrors
+
+import "fmt"
+
+// errorString is a trivial implementation of error.
+type errorString struct {
+	s     string
+	frame Frame
+}
+
+// New returns an error that formats as the given text.
+//
+// The returned error contains a Frame set to the caller's location and
+// implements Formatter to show this information when printed with details.
+func New(text string) error {
+	return &errorString{text, Caller(1)}
+}
+
+func (e *errorString) Error() string {
+	return e.s
+}
+
+func (e *errorString) Format(s fmt.State, v rune) { FormatError(e, s, v) }
+
+func (e *errorString) FormatError(p Printer) (next error) {
+	p.Print(e.s)
+	e.frame.Format(p)
+	return nil
+}
diff --git a/vendor/golang.org/x/xerrors/fmt.go b/vendor/golang.org/x/xerrors/fmt.go
new file mode 100644
index 00000000000..74c1c93ec9c
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/fmt.go
@@ -0,0 +1,109 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package xerrors
+
+import (
+	"fmt"
+	"strings"
+
+	"golang.org/x/xerrors/internal"
+)
+
+// Errorf formats according to a format specifier and returns the string as a
+// value that satisfies error.
+//
+// The returned error includes the file and line number of the caller when
+// formatted with additional detail enabled. If the last argument is an error
+// the returned error's Format method will return it if the format string ends
+// with ": %s", ": %v", or ": %w". If the last argument is an error and the
+// format string ends with ": %w", the returned error implements Wrapper
+// with an Unwrap method returning it.
+func Errorf(format string, a ...interface{}) error {
+	err, wrap := lastError(format, a)
+	format = formatPlusW(format)
+	if err == nil {
+		return &noWrapError{fmt.Sprintf(format, a...), nil, Caller(1)}
+	}
+
+	// TODO: this is not entirely correct. The error value could be
+	// printed elsewhere in format if it mixes numbered with unnumbered
+	// substitutions. With relatively small changes to doPrintf we can
+	// have it optionally ignore extra arguments and pass the argument
+	// list in its entirety.
+	msg := fmt.Sprintf(format[:len(format)-len(": %s")], a[:len(a)-1]...)
+	frame := Frame{}
+	if internal.EnableTrace {
+		frame = Caller(1)
+	}
+	if wrap {
+		return &wrapError{msg, err, frame}
+	}
+	return &noWrapError{msg, err, frame}
+}
+
+// formatPlusW is used to avoid the vet check that will barf at %w.
+func formatPlusW(s string) string {
+	return s
+}
+
+func lastError(format string, a []interface{}) (err error, wrap bool) {
+	wrap = strings.HasSuffix(format, ": %w")
+	if !wrap &&
+		!strings.HasSuffix(format, ": %s") &&
+		!strings.HasSuffix(format, ": %v") {
+		return nil, false
+	}
+
+	if len(a) == 0 {
+		return nil, false
+	}
+
+	err, ok := a[len(a)-1].(error)
+	if !ok {
+		return nil, false
+	}
+
+	return err, wrap
+}
+
+type noWrapError struct {
+	msg   string
+	err   error
+	frame Frame
+}
+
+func (e *noWrapError) Error() string {
+	return fmt.Sprint(e)
+}
+
+func (e *noWrapError) Format(s fmt.State, v rune) { FormatError(e, s, v) }
+
+func (e *noWrapError) FormatError(p Printer) (next error) {
+	p.Print(e.msg)
+	e.frame.Format(p)
+	return e.err
+}
+
+type wrapError struct {
+	msg   string
+	err   error
+	frame Frame
+}
+
+func (e *wrapError) Error() string {
+	return fmt.Sprint(e)
+}
+
+func (e *wrapError) Format(s fmt.State, v rune) { FormatError(e, s, v) }
+
+func (e *wrapError) FormatError(p Printer) (next error) {
+	p.Print(e.msg)
+	e.frame.Format(p)
+	return e.err
+}
+
+func (e *wrapError) Unwrap() error {
+	return e.err
+}
diff --git a/vendor/golang.org/x/xerrors/format.go b/vendor/golang.org/x/xerrors/format.go
new file mode 100644
index 00000000000..1bc9c26b97f
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/format.go
@@ -0,0 +1,34 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package xerrors
+
+// A Formatter formats error messages.
+type Formatter interface {
+	error
+
+	// FormatError prints the receiver's first error and returns the next error in
+	// the error chain, if any.
+	FormatError(p Printer) (next error)
+}
+
+// A Printer formats error messages.
+//
+// The most common implementation of Printer is the one provided by package fmt
+// during Printf (as of Go 1.13). Localization packages such as golang.org/x/text/message
+// typically provide their own implementations.
+type Printer interface {
+	// Print appends args to the message output.
+	Print(args ...interface{})
+
+	// Printf writes a formatted string.
+	Printf(format string, args ...interface{})
+
+	// Detail reports whether error detail is requested.
+	// After the first call to Detail, all text written to the Printer
+	// is formatted as additional detail, or ignored when
+	// detail has not been requested.
+	// If Detail returns false, the caller can avoid printing the detail at all.
+	Detail() bool
+}
diff --git a/vendor/golang.org/x/xerrors/frame.go b/vendor/golang.org/x/xerrors/frame.go
new file mode 100644
index 00000000000..0de628ec501
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/frame.go
@@ -0,0 +1,56 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package xerrors
+
+import (
+	"runtime"
+)
+
+// A Frame contains part of a call stack.
+type Frame struct {
+	// Make room for three PCs: the one we were asked for, what it called,
+	// and possibly a PC for skipPleaseUseCallersFrames. See:
+	// https://go.googlesource.com/go/+/032678e0fb/src/runtime/extern.go#169
+	frames [3]uintptr
+}
+
+// Caller returns a Frame that describes a frame on the caller's stack.
+// The argument skip is the number of frames to skip over.
+// Caller(0) returns the frame for the caller of Caller.
+func Caller(skip int) Frame {
+	var s Frame
+	runtime.Callers(skip+1, s.frames[:])
+	return s
+}
+
+// location reports the file, line, and function of a frame.
+//
+// The returned function may be "" even if file and line are not.
+func (f Frame) location() (function, file string, line int) {
+	frames := runtime.CallersFrames(f.frames[:])
+	if _, ok := frames.Next(); !ok {
+		return "", "", 0
+	}
+	fr, ok := frames.Next()
+	if !ok {
+		return "", "", 0
+	}
+	return fr.Function, fr.File, fr.Line
+}
+
+// Format prints the stack as error detail.
+// It should be called from an error's Format implementation
+// after printing any other error detail.
+func (f Frame) Format(p Printer) {
+	if p.Detail() {
+		function, file, line := f.location()
+		if function != "" {
+			p.Printf("%s\n    ", function)
+		}
+		if file != "" {
+			p.Printf("%s:%d\n", file, line)
+		}
+	}
+}
diff --git a/vendor/golang.org/x/xerrors/go.mod b/vendor/golang.org/x/xerrors/go.mod
new file mode 100644
index 00000000000..870d4f612db
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/go.mod
@@ -0,0 +1,3 @@
+module golang.org/x/xerrors
+
+go 1.11
diff --git a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_namlen_bsd.go b/vendor/golang.org/x/xerrors/internal/internal.go
similarity index 50%
rename from vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_namlen_bsd.go
rename to vendor/golang.org/x/xerrors/internal/internal.go
index a3b26a7bae0..89f4eca5df7 100644
--- a/vendor/golang.org/x/tools/internal/fastwalk/fastwalk_dirent_namlen_bsd.go
+++ b/vendor/golang.org/x/xerrors/internal/internal.go
@@ -2,12 +2,7 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// +build darwin freebsd openbsd netbsd
+package internal
 
-package fastwalk
-
-import "syscall"
-
-func direntNamlen(dirent *syscall.Dirent) uint64 {
-	return uint64(dirent.Namlen)
-}
+// EnableTrace indicates whether stack information should be recorded in errors.
+var EnableTrace = true
diff --git a/vendor/golang.org/x/xerrors/wrap.go b/vendor/golang.org/x/xerrors/wrap.go
new file mode 100644
index 00000000000..9a3b510374e
--- /dev/null
+++ b/vendor/golang.org/x/xerrors/wrap.go
@@ -0,0 +1,106 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package xerrors
+
+import (
+	"reflect"
+)
+
+// A Wrapper provides context around another error.
+type Wrapper interface {
+	// Unwrap returns the next error in the error chain.
+	// If there is no next error, Unwrap returns nil.
+	Unwrap() error
+}
+
+// Opaque returns an error with the same error formatting as err
+// but that does not match err and cannot be unwrapped.
+func Opaque(err error) error {
+	return noWrapper{err}
+}
+
+type noWrapper struct {
+	error
+}
+
+func (e noWrapper) FormatError(p Printer) (next error) {
+	if f, ok := e.error.(Formatter); ok {
+		return f.FormatError(p)
+	}
+	p.Print(e.error)
+	return nil
+}
+
+// Unwrap returns the result of calling the Unwrap method on err, if err implements
+// Unwrap. Otherwise, Unwrap returns nil.
+func Unwrap(err error) error {
+	u, ok := err.(Wrapper)
+	if !ok {
+		return nil
+	}
+	return u.Unwrap()
+}
+
+// Is reports whether any error in err's chain matches target.
+//
+// An error is considered to match a target if it is equal to that target or if
+// it implements a method Is(error) bool such that Is(target) returns true.
+func Is(err, target error) bool {
+	if target == nil {
+		return err == target
+	}
+
+	isComparable := reflect.TypeOf(target).Comparable()
+	for {
+		if isComparable && err == target {
+			return true
+		}
+		if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
+			return true
+		}
+		// TODO: consider supporing target.Is(err). This would allow
+		// user-definable predicates, but also may allow for coping with sloppy
+		// APIs, thereby making it easier to get away with them.
+		if err = Unwrap(err); err == nil {
+			return false
+		}
+	}
+}
+
+// As finds the first error in err's chain that matches the type to which target
+// points, and if so, sets the target to its value and returns true. An error
+// matches a type if it is assignable to the target type, or if it has a method
+// As(interface{}) bool such that As(target) returns true. As will panic if target
+// is not a non-nil pointer to a type which implements error or is of interface type.
+//
+// The As method should set the target to its value and return true if err
+// matches the type to which target points.
+func As(err error, target interface{}) bool {
+	if target == nil {
+		panic("errors: target cannot be nil")
+	}
+	val := reflect.ValueOf(target)
+	typ := val.Type()
+	if typ.Kind() != reflect.Ptr || val.IsNil() {
+		panic("errors: target must be a non-nil pointer")
+	}
+	if e := typ.Elem(); e.Kind() != reflect.Interface && !e.Implements(errorType) {
+		panic("errors: *target must be interface or implement error")
+	}
+	targetType := typ.Elem()
+	for err != nil {
+		if reflect.TypeOf(err).AssignableTo(targetType) {
+			val.Elem().Set(reflect.ValueOf(err))
+			return true
+		}
+		if x, ok := err.(interface{ As(interface{}) bool }); ok && x.As(target) {
+			return true
+		}
+		err = Unwrap(err)
+	}
+	return false
+}
+
+var errorType = reflect.TypeOf((*error)(nil)).Elem()
diff --git a/vendor/modules.txt b/vendor/modules.txt
index 41a0659c2c3..1a15748a35f 100644
--- a/vendor/modules.txt
+++ b/vendor/modules.txt
@@ -1,24 +1,15 @@
-# golang.org/x/tools v0.0.0-20200109174759-ac4f524c1612
-golang.org/x/tools/benchmark/parse
-golang.org/x/tools/blog/atom
-golang.org/x/tools/container/intsets
-golang.org/x/tools/go/ast/astutil
-golang.org/x/tools/go/buildutil
-golang.org/x/tools/go/callgraph
-golang.org/x/tools/go/callgraph/cha
-golang.org/x/tools/go/callgraph/rta
-golang.org/x/tools/go/callgraph/static
+# golang.org/x/mod v0.2.0
+golang.org/x/mod/internal/lazyregexp
+golang.org/x/mod/modfile
+golang.org/x/mod/module
+golang.org/x/mod/semver
+# golang.org/x/tools v0.0.0-20200302225559-9b52d559c609
 golang.org/x/tools/go/gcexportdata
-golang.org/x/tools/go/internal/cgo
 golang.org/x/tools/go/internal/gcimporter
 golang.org/x/tools/go/internal/packagesdriver
-golang.org/x/tools/go/loader
 golang.org/x/tools/go/packages
-golang.org/x/tools/go/pointer
-golang.org/x/tools/go/ssa
-golang.org/x/tools/go/ssa/ssautil
-golang.org/x/tools/go/types/typeutil
-golang.org/x/tools/internal/fastwalk
-golang.org/x/tools/internal/gopathwalk
-golang.org/x/tools/internal/semver
-golang.org/x/tools/present
+golang.org/x/tools/internal/gocommand
+golang.org/x/tools/internal/packagesinternal
+# golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898
+golang.org/x/xerrors
+golang.org/x/xerrors/internal