github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/src/go/doc/reader.go (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package doc 6 7 import ( 8 "go/ast" 9 "go/token" 10 "regexp" 11 "sort" 12 "strconv" 13 ) 14 15 // ---------------------------------------------------------------------------- 16 // function/method sets 17 // 18 // Internally, we treat functions like methods and collect them in method sets. 19 20 // A methodSet describes a set of methods. Entries where Decl == nil are conflict 21 // entries (more than one method with the same name at the same embedding level). 22 // 23 type methodSet map[string]*Func 24 25 // recvString returns a string representation of recv of the 26 // form "T", "*T", or "BADRECV" (if not a proper receiver type). 27 // 28 func recvString(recv ast.Expr) string { 29 switch t := recv.(type) { 30 case *ast.Ident: 31 return t.Name 32 case *ast.StarExpr: 33 return "*" + recvString(t.X) 34 } 35 return "BADRECV" 36 } 37 38 // set creates the corresponding Func for f and adds it to mset. 39 // If there are multiple f's with the same name, set keeps the first 40 // one with documentation; conflicts are ignored. 41 // 42 func (mset methodSet) set(f *ast.FuncDecl) { 43 name := f.Name.Name 44 if g := mset[name]; g != nil && g.Doc != "" { 45 // A function with the same name has already been registered; 46 // since it has documentation, assume f is simply another 47 // implementation and ignore it. This does not happen if the 48 // caller is using go/build.ScanDir to determine the list of 49 // files implementing a package. 50 return 51 } 52 // function doesn't exist or has no documentation; use f 53 recv := "" 54 if f.Recv != nil { 55 var typ ast.Expr 56 // be careful in case of incorrect ASTs 57 if list := f.Recv.List; len(list) == 1 { 58 typ = list[0].Type 59 } 60 recv = recvString(typ) 61 } 62 mset[name] = &Func{ 63 Doc: f.Doc.Text(), 64 Name: name, 65 Decl: f, 66 Recv: recv, 67 Orig: recv, 68 } 69 f.Doc = nil // doc consumed - remove from AST 70 } 71 72 // add adds method m to the method set; m is ignored if the method set 73 // already contains a method with the same name at the same or a higher 74 // level than m. 75 // 76 func (mset methodSet) add(m *Func) { 77 old := mset[m.Name] 78 if old == nil || m.Level < old.Level { 79 mset[m.Name] = m 80 return 81 } 82 if old != nil && m.Level == old.Level { 83 // conflict - mark it using a method with nil Decl 84 mset[m.Name] = &Func{ 85 Name: m.Name, 86 Level: m.Level, 87 } 88 } 89 } 90 91 // ---------------------------------------------------------------------------- 92 // Named types 93 94 // baseTypeName returns the name of the base type of x (or "") 95 // and whether the type is imported or not. 96 // 97 func baseTypeName(x ast.Expr) (name string, imported bool) { 98 switch t := x.(type) { 99 case *ast.Ident: 100 return t.Name, false 101 case *ast.SelectorExpr: 102 if _, ok := t.X.(*ast.Ident); ok { 103 // only possible for qualified type names; 104 // assume type is imported 105 return t.Sel.Name, true 106 } 107 case *ast.StarExpr: 108 return baseTypeName(t.X) 109 } 110 return 111 } 112 113 // An embeddedSet describes a set of embedded types. 114 type embeddedSet map[*namedType]bool 115 116 // A namedType represents a named unqualified (package local, or possibly 117 // predeclared) type. The namedType for a type name is always found via 118 // reader.lookupType. 119 // 120 type namedType struct { 121 doc string // doc comment for type 122 name string // type name 123 decl *ast.GenDecl // nil if declaration hasn't been seen yet 124 125 isEmbedded bool // true if this type is embedded 126 isStruct bool // true if this type is a struct 127 embedded embeddedSet // true if the embedded type is a pointer 128 129 // associated declarations 130 values []*Value // consts and vars 131 funcs methodSet 132 methods methodSet 133 } 134 135 // ---------------------------------------------------------------------------- 136 // AST reader 137 138 // reader accumulates documentation for a single package. 139 // It modifies the AST: Comments (declaration documentation) 140 // that have been collected by the reader are set to nil 141 // in the respective AST nodes so that they are not printed 142 // twice (once when printing the documentation and once when 143 // printing the corresponding AST node). 144 // 145 type reader struct { 146 mode Mode 147 148 // package properties 149 doc string // package documentation, if any 150 filenames []string 151 notes map[string][]*Note 152 153 // declarations 154 imports map[string]int 155 hasDotImp bool // if set, package contains a dot import 156 values []*Value // consts and vars 157 types map[string]*namedType 158 funcs methodSet 159 160 // support for package-local error type declarations 161 errorDecl bool // if set, type "error" was declared locally 162 fixlist []*ast.InterfaceType // list of interfaces containing anonymous field "error" 163 } 164 165 func (r *reader) isVisible(name string) bool { 166 return r.mode&AllDecls != 0 || ast.IsExported(name) 167 } 168 169 // lookupType returns the base type with the given name. 170 // If the base type has not been encountered yet, a new 171 // type with the given name but no associated declaration 172 // is added to the type map. 173 // 174 func (r *reader) lookupType(name string) *namedType { 175 if name == "" || name == "_" { 176 return nil // no type docs for anonymous types 177 } 178 if typ, found := r.types[name]; found { 179 return typ 180 } 181 // type not found - add one without declaration 182 typ := &namedType{ 183 name: name, 184 embedded: make(embeddedSet), 185 funcs: make(methodSet), 186 methods: make(methodSet), 187 } 188 r.types[name] = typ 189 return typ 190 } 191 192 // recordAnonymousField registers fieldType as the type of an 193 // anonymous field in the parent type. If the field is imported 194 // (qualified name) or the parent is nil, the field is ignored. 195 // The function returns the field name. 196 // 197 func (r *reader) recordAnonymousField(parent *namedType, fieldType ast.Expr) (fname string) { 198 fname, imp := baseTypeName(fieldType) 199 if parent == nil || imp { 200 return 201 } 202 if ftype := r.lookupType(fname); ftype != nil { 203 ftype.isEmbedded = true 204 _, ptr := fieldType.(*ast.StarExpr) 205 parent.embedded[ftype] = ptr 206 } 207 return 208 } 209 210 func (r *reader) readDoc(comment *ast.CommentGroup) { 211 // By convention there should be only one package comment 212 // but collect all of them if there are more than one. 213 text := comment.Text() 214 if r.doc == "" { 215 r.doc = text 216 return 217 } 218 r.doc += "\n" + text 219 } 220 221 func (r *reader) remember(typ *ast.InterfaceType) { 222 r.fixlist = append(r.fixlist, typ) 223 } 224 225 func specNames(specs []ast.Spec) []string { 226 names := make([]string, 0, len(specs)) // reasonable estimate 227 for _, s := range specs { 228 // s guaranteed to be an *ast.ValueSpec by readValue 229 for _, ident := range s.(*ast.ValueSpec).Names { 230 names = append(names, ident.Name) 231 } 232 } 233 return names 234 } 235 236 // readValue processes a const or var declaration. 237 // 238 func (r *reader) readValue(decl *ast.GenDecl) { 239 // determine if decl should be associated with a type 240 // Heuristic: For each typed entry, determine the type name, if any. 241 // If there is exactly one type name that is sufficiently 242 // frequent, associate the decl with the respective type. 243 domName := "" 244 domFreq := 0 245 prev := "" 246 n := 0 247 for _, spec := range decl.Specs { 248 s, ok := spec.(*ast.ValueSpec) 249 if !ok { 250 continue // should not happen, but be conservative 251 } 252 name := "" 253 switch { 254 case s.Type != nil: 255 // a type is present; determine its name 256 if n, imp := baseTypeName(s.Type); !imp { 257 name = n 258 } 259 case decl.Tok == token.CONST: 260 // no type is present but we have a constant declaration; 261 // use the previous type name (w/o more type information 262 // we cannot handle the case of unnamed variables with 263 // initializer expressions except for some trivial cases) 264 name = prev 265 } 266 if name != "" { 267 // entry has a named type 268 if domName != "" && domName != name { 269 // more than one type name - do not associate 270 // with any type 271 domName = "" 272 break 273 } 274 domName = name 275 domFreq++ 276 } 277 prev = name 278 n++ 279 } 280 281 // nothing to do w/o a legal declaration 282 if n == 0 { 283 return 284 } 285 286 // determine values list with which to associate the Value for this decl 287 values := &r.values 288 const threshold = 0.75 289 if domName != "" && r.isVisible(domName) && domFreq >= int(float64(len(decl.Specs))*threshold) { 290 // typed entries are sufficiently frequent 291 if typ := r.lookupType(domName); typ != nil { 292 values = &typ.values // associate with that type 293 } 294 } 295 296 *values = append(*values, &Value{ 297 Doc: decl.Doc.Text(), 298 Names: specNames(decl.Specs), 299 Decl: decl, 300 order: len(*values), 301 }) 302 decl.Doc = nil // doc consumed - remove from AST 303 } 304 305 // fields returns a struct's fields or an interface's methods. 306 // 307 func fields(typ ast.Expr) (list []*ast.Field, isStruct bool) { 308 var fields *ast.FieldList 309 switch t := typ.(type) { 310 case *ast.StructType: 311 fields = t.Fields 312 isStruct = true 313 case *ast.InterfaceType: 314 fields = t.Methods 315 } 316 if fields != nil { 317 list = fields.List 318 } 319 return 320 } 321 322 // readType processes a type declaration. 323 // 324 func (r *reader) readType(decl *ast.GenDecl, spec *ast.TypeSpec) { 325 typ := r.lookupType(spec.Name.Name) 326 if typ == nil { 327 return // no name or blank name - ignore the type 328 } 329 330 // A type should be added at most once, so typ.decl 331 // should be nil - if it is not, simply overwrite it. 332 typ.decl = decl 333 334 // compute documentation 335 doc := spec.Doc 336 spec.Doc = nil // doc consumed - remove from AST 337 if doc == nil { 338 // no doc associated with the spec, use the declaration doc, if any 339 doc = decl.Doc 340 } 341 decl.Doc = nil // doc consumed - remove from AST 342 typ.doc = doc.Text() 343 344 // record anonymous fields (they may contribute methods) 345 // (some fields may have been recorded already when filtering 346 // exports, but that's ok) 347 var list []*ast.Field 348 list, typ.isStruct = fields(spec.Type) 349 for _, field := range list { 350 if len(field.Names) == 0 { 351 r.recordAnonymousField(typ, field.Type) 352 } 353 } 354 } 355 356 // readFunc processes a func or method declaration. 357 // 358 func (r *reader) readFunc(fun *ast.FuncDecl) { 359 // strip function body 360 fun.Body = nil 361 362 // associate methods with the receiver type, if any 363 if fun.Recv != nil { 364 // method 365 if len(fun.Recv.List) == 0 { 366 // should not happen (incorrect AST); (See issue 17788) 367 // don't show this method 368 return 369 } 370 recvTypeName, imp := baseTypeName(fun.Recv.List[0].Type) 371 if imp { 372 // should not happen (incorrect AST); 373 // don't show this method 374 return 375 } 376 if typ := r.lookupType(recvTypeName); typ != nil { 377 typ.methods.set(fun) 378 } 379 // otherwise ignore the method 380 // TODO(gri): There may be exported methods of non-exported types 381 // that can be called because of exported values (consts, vars, or 382 // function results) of that type. Could determine if that is the 383 // case and then show those methods in an appropriate section. 384 return 385 } 386 387 // associate factory functions with the first visible result type, if any 388 if fun.Type.Results.NumFields() >= 1 { 389 res := fun.Type.Results.List[0] 390 if len(res.Names) <= 1 { 391 // exactly one (named or anonymous) result associated 392 // with the first type in result signature (there may 393 // be more than one result) 394 factoryType := res.Type 395 if t, ok := factoryType.(*ast.ArrayType); ok && t.Len == nil { 396 // We consider functions that return slices of type T (or 397 // pointers to T) as factory functions of T. 398 factoryType = t.Elt 399 } 400 if n, imp := baseTypeName(factoryType); !imp && r.isVisible(n) { 401 if typ := r.lookupType(n); typ != nil { 402 // associate function with typ 403 typ.funcs.set(fun) 404 return 405 } 406 } 407 } 408 } 409 410 // just an ordinary function 411 r.funcs.set(fun) 412 } 413 414 var ( 415 noteMarker = `([A-Z][A-Z]+)\(([^)]+)\):?` // MARKER(uid), MARKER at least 2 chars, uid at least 1 char 416 noteMarkerRx = regexp.MustCompile(`^[ \t]*` + noteMarker) // MARKER(uid) at text start 417 noteCommentRx = regexp.MustCompile(`^/[/*][ \t]*` + noteMarker) // MARKER(uid) at comment start 418 ) 419 420 // readNote collects a single note from a sequence of comments. 421 // 422 func (r *reader) readNote(list []*ast.Comment) { 423 text := (&ast.CommentGroup{List: list}).Text() 424 if m := noteMarkerRx.FindStringSubmatchIndex(text); m != nil { 425 // The note body starts after the marker. 426 // We remove any formatting so that we don't 427 // get spurious line breaks/indentation when 428 // showing the TODO body. 429 body := clean(text[m[1]:], keepNL) 430 if body != "" { 431 marker := text[m[2]:m[3]] 432 r.notes[marker] = append(r.notes[marker], &Note{ 433 Pos: list[0].Pos(), 434 End: list[len(list)-1].End(), 435 UID: text[m[4]:m[5]], 436 Body: body, 437 }) 438 } 439 } 440 } 441 442 // readNotes extracts notes from comments. 443 // A note must start at the beginning of a comment with "MARKER(uid):" 444 // and is followed by the note body (e.g., "// BUG(gri): fix this"). 445 // The note ends at the end of the comment group or at the start of 446 // another note in the same comment group, whichever comes first. 447 // 448 func (r *reader) readNotes(comments []*ast.CommentGroup) { 449 for _, group := range comments { 450 i := -1 // comment index of most recent note start, valid if >= 0 451 list := group.List 452 for j, c := range list { 453 if noteCommentRx.MatchString(c.Text) { 454 if i >= 0 { 455 r.readNote(list[i:j]) 456 } 457 i = j 458 } 459 } 460 if i >= 0 { 461 r.readNote(list[i:]) 462 } 463 } 464 } 465 466 // readFile adds the AST for a source file to the reader. 467 // 468 func (r *reader) readFile(src *ast.File) { 469 // add package documentation 470 if src.Doc != nil { 471 r.readDoc(src.Doc) 472 src.Doc = nil // doc consumed - remove from AST 473 } 474 475 // add all declarations 476 for _, decl := range src.Decls { 477 switch d := decl.(type) { 478 case *ast.GenDecl: 479 switch d.Tok { 480 case token.IMPORT: 481 // imports are handled individually 482 for _, spec := range d.Specs { 483 if s, ok := spec.(*ast.ImportSpec); ok { 484 if import_, err := strconv.Unquote(s.Path.Value); err == nil { 485 r.imports[import_] = 1 486 if s.Name != nil && s.Name.Name == "." { 487 r.hasDotImp = true 488 } 489 } 490 } 491 } 492 case token.CONST, token.VAR: 493 // constants and variables are always handled as a group 494 r.readValue(d) 495 case token.TYPE: 496 // types are handled individually 497 if len(d.Specs) == 1 && !d.Lparen.IsValid() { 498 // common case: single declaration w/o parentheses 499 // (if a single declaration is parenthesized, 500 // create a new fake declaration below, so that 501 // go/doc type declarations always appear w/o 502 // parentheses) 503 if s, ok := d.Specs[0].(*ast.TypeSpec); ok { 504 r.readType(d, s) 505 } 506 break 507 } 508 for _, spec := range d.Specs { 509 if s, ok := spec.(*ast.TypeSpec); ok { 510 // use an individual (possibly fake) declaration 511 // for each type; this also ensures that each type 512 // gets to (re-)use the declaration documentation 513 // if there's none associated with the spec itself 514 fake := &ast.GenDecl{ 515 Doc: d.Doc, 516 // don't use the existing TokPos because it 517 // will lead to the wrong selection range for 518 // the fake declaration if there are more 519 // than one type in the group (this affects 520 // src/cmd/godoc/godoc.go's posLink_urlFunc) 521 TokPos: s.Pos(), 522 Tok: token.TYPE, 523 Specs: []ast.Spec{s}, 524 } 525 r.readType(fake, s) 526 } 527 } 528 } 529 case *ast.FuncDecl: 530 r.readFunc(d) 531 } 532 } 533 534 // collect MARKER(...): annotations 535 r.readNotes(src.Comments) 536 src.Comments = nil // consumed unassociated comments - remove from AST 537 } 538 539 func (r *reader) readPackage(pkg *ast.Package, mode Mode) { 540 // initialize reader 541 r.filenames = make([]string, len(pkg.Files)) 542 r.imports = make(map[string]int) 543 r.mode = mode 544 r.types = make(map[string]*namedType) 545 r.funcs = make(methodSet) 546 r.notes = make(map[string][]*Note) 547 548 // sort package files before reading them so that the 549 // result does not depend on map iteration order 550 i := 0 551 for filename := range pkg.Files { 552 r.filenames[i] = filename 553 i++ 554 } 555 sort.Strings(r.filenames) 556 557 // process files in sorted order 558 for _, filename := range r.filenames { 559 f := pkg.Files[filename] 560 if mode&AllDecls == 0 { 561 r.fileExports(f) 562 } 563 r.readFile(f) 564 } 565 } 566 567 // ---------------------------------------------------------------------------- 568 // Types 569 570 func customizeRecv(f *Func, recvTypeName string, embeddedIsPtr bool, level int) *Func { 571 if f == nil || f.Decl == nil || f.Decl.Recv == nil || len(f.Decl.Recv.List) != 1 { 572 return f // shouldn't happen, but be safe 573 } 574 575 // copy existing receiver field and set new type 576 newField := *f.Decl.Recv.List[0] 577 origPos := newField.Type.Pos() 578 _, origRecvIsPtr := newField.Type.(*ast.StarExpr) 579 newIdent := &ast.Ident{NamePos: origPos, Name: recvTypeName} 580 var typ ast.Expr = newIdent 581 if !embeddedIsPtr && origRecvIsPtr { 582 newIdent.NamePos++ // '*' is one character 583 typ = &ast.StarExpr{Star: origPos, X: newIdent} 584 } 585 newField.Type = typ 586 587 // copy existing receiver field list and set new receiver field 588 newFieldList := *f.Decl.Recv 589 newFieldList.List = []*ast.Field{&newField} 590 591 // copy existing function declaration and set new receiver field list 592 newFuncDecl := *f.Decl 593 newFuncDecl.Recv = &newFieldList 594 595 // copy existing function documentation and set new declaration 596 newF := *f 597 newF.Decl = &newFuncDecl 598 newF.Recv = recvString(typ) 599 // the Orig field never changes 600 newF.Level = level 601 602 return &newF 603 } 604 605 // collectEmbeddedMethods collects the embedded methods of typ in mset. 606 // 607 func (r *reader) collectEmbeddedMethods(mset methodSet, typ *namedType, recvTypeName string, embeddedIsPtr bool, level int, visited embeddedSet) { 608 visited[typ] = true 609 for embedded, isPtr := range typ.embedded { 610 // Once an embedded type is embedded as a pointer type 611 // all embedded types in those types are treated like 612 // pointer types for the purpose of the receiver type 613 // computation; i.e., embeddedIsPtr is sticky for this 614 // embedding hierarchy. 615 thisEmbeddedIsPtr := embeddedIsPtr || isPtr 616 for _, m := range embedded.methods { 617 // only top-level methods are embedded 618 if m.Level == 0 { 619 mset.add(customizeRecv(m, recvTypeName, thisEmbeddedIsPtr, level)) 620 } 621 } 622 if !visited[embedded] { 623 r.collectEmbeddedMethods(mset, embedded, recvTypeName, thisEmbeddedIsPtr, level+1, visited) 624 } 625 } 626 delete(visited, typ) 627 } 628 629 // computeMethodSets determines the actual method sets for each type encountered. 630 // 631 func (r *reader) computeMethodSets() { 632 for _, t := range r.types { 633 // collect embedded methods for t 634 if t.isStruct { 635 // struct 636 r.collectEmbeddedMethods(t.methods, t, t.name, false, 1, make(embeddedSet)) 637 } else { 638 // interface 639 // TODO(gri) fix this 640 } 641 } 642 643 // if error was declared locally, don't treat it as exported field anymore 644 if r.errorDecl { 645 for _, ityp := range r.fixlist { 646 removeErrorField(ityp) 647 } 648 } 649 } 650 651 // cleanupTypes removes the association of functions and methods with 652 // types that have no declaration. Instead, these functions and methods 653 // are shown at the package level. It also removes types with missing 654 // declarations or which are not visible. 655 // 656 func (r *reader) cleanupTypes() { 657 for _, t := range r.types { 658 visible := r.isVisible(t.name) 659 predeclared := predeclaredTypes[t.name] 660 661 if t.decl == nil && (predeclared || visible && (t.isEmbedded || r.hasDotImp)) { 662 // t.name is a predeclared type (and was not redeclared in this package), 663 // or it was embedded somewhere but its declaration is missing (because 664 // the AST is incomplete), or we have a dot-import (and all bets are off): 665 // move any associated values, funcs, and methods back to the top-level so 666 // that they are not lost. 667 // 1) move values 668 r.values = append(r.values, t.values...) 669 // 2) move factory functions 670 for name, f := range t.funcs { 671 // in a correct AST, package-level function names 672 // are all different - no need to check for conflicts 673 r.funcs[name] = f 674 } 675 // 3) move methods 676 if !predeclared { 677 for name, m := range t.methods { 678 // don't overwrite functions with the same name - drop them 679 if _, found := r.funcs[name]; !found { 680 r.funcs[name] = m 681 } 682 } 683 } 684 } 685 // remove types w/o declaration or which are not visible 686 if t.decl == nil || !visible { 687 delete(r.types, t.name) 688 } 689 } 690 } 691 692 // ---------------------------------------------------------------------------- 693 // Sorting 694 695 type data struct { 696 n int 697 swap func(i, j int) 698 less func(i, j int) bool 699 } 700 701 func (d *data) Len() int { return d.n } 702 func (d *data) Swap(i, j int) { d.swap(i, j) } 703 func (d *data) Less(i, j int) bool { return d.less(i, j) } 704 705 // sortBy is a helper function for sorting 706 func sortBy(less func(i, j int) bool, swap func(i, j int), n int) { 707 sort.Sort(&data{n, swap, less}) 708 } 709 710 func sortedKeys(m map[string]int) []string { 711 list := make([]string, len(m)) 712 i := 0 713 for key := range m { 714 list[i] = key 715 i++ 716 } 717 sort.Strings(list) 718 return list 719 } 720 721 // sortingName returns the name to use when sorting d into place. 722 // 723 func sortingName(d *ast.GenDecl) string { 724 if len(d.Specs) == 1 { 725 if s, ok := d.Specs[0].(*ast.ValueSpec); ok { 726 return s.Names[0].Name 727 } 728 } 729 return "" 730 } 731 732 func sortedValues(m []*Value, tok token.Token) []*Value { 733 list := make([]*Value, len(m)) // big enough in any case 734 i := 0 735 for _, val := range m { 736 if val.Decl.Tok == tok { 737 list[i] = val 738 i++ 739 } 740 } 741 list = list[0:i] 742 743 sortBy( 744 func(i, j int) bool { 745 if ni, nj := sortingName(list[i].Decl), sortingName(list[j].Decl); ni != nj { 746 return ni < nj 747 } 748 return list[i].order < list[j].order 749 }, 750 func(i, j int) { list[i], list[j] = list[j], list[i] }, 751 len(list), 752 ) 753 754 return list 755 } 756 757 func sortedTypes(m map[string]*namedType, allMethods bool) []*Type { 758 list := make([]*Type, len(m)) 759 i := 0 760 for _, t := range m { 761 list[i] = &Type{ 762 Doc: t.doc, 763 Name: t.name, 764 Decl: t.decl, 765 Consts: sortedValues(t.values, token.CONST), 766 Vars: sortedValues(t.values, token.VAR), 767 Funcs: sortedFuncs(t.funcs, true), 768 Methods: sortedFuncs(t.methods, allMethods), 769 } 770 i++ 771 } 772 773 sortBy( 774 func(i, j int) bool { return list[i].Name < list[j].Name }, 775 func(i, j int) { list[i], list[j] = list[j], list[i] }, 776 len(list), 777 ) 778 779 return list 780 } 781 782 func removeStar(s string) string { 783 if len(s) > 0 && s[0] == '*' { 784 return s[1:] 785 } 786 return s 787 } 788 789 func sortedFuncs(m methodSet, allMethods bool) []*Func { 790 list := make([]*Func, len(m)) 791 i := 0 792 for _, m := range m { 793 // determine which methods to include 794 switch { 795 case m.Decl == nil: 796 // exclude conflict entry 797 case allMethods, m.Level == 0, !ast.IsExported(removeStar(m.Orig)): 798 // forced inclusion, method not embedded, or method 799 // embedded but original receiver type not exported 800 list[i] = m 801 i++ 802 } 803 } 804 list = list[0:i] 805 sortBy( 806 func(i, j int) bool { return list[i].Name < list[j].Name }, 807 func(i, j int) { list[i], list[j] = list[j], list[i] }, 808 len(list), 809 ) 810 return list 811 } 812 813 // noteBodies returns a list of note body strings given a list of notes. 814 // This is only used to populate the deprecated Package.Bugs field. 815 // 816 func noteBodies(notes []*Note) []string { 817 var list []string 818 for _, n := range notes { 819 list = append(list, n.Body) 820 } 821 return list 822 } 823 824 // ---------------------------------------------------------------------------- 825 // Predeclared identifiers 826 827 // IsPredeclared reports whether s is a predeclared identifier. 828 func IsPredeclared(s string) bool { 829 return predeclaredTypes[s] || predeclaredFuncs[s] || predeclaredConstants[s] 830 } 831 832 var predeclaredTypes = map[string]bool{ 833 "bool": true, 834 "byte": true, 835 "complex64": true, 836 "complex128": true, 837 "error": true, 838 "float32": true, 839 "float64": true, 840 "int": true, 841 "int8": true, 842 "int16": true, 843 "int32": true, 844 "int64": true, 845 "rune": true, 846 "string": true, 847 "uint": true, 848 "uint8": true, 849 "uint16": true, 850 "uint32": true, 851 "uint64": true, 852 "uintptr": true, 853 } 854 855 var predeclaredFuncs = map[string]bool{ 856 "append": true, 857 "cap": true, 858 "close": true, 859 "complex": true, 860 "copy": true, 861 "delete": true, 862 "imag": true, 863 "len": true, 864 "make": true, 865 "new": true, 866 "panic": true, 867 "print": true, 868 "println": true, 869 "real": true, 870 "recover": true, 871 } 872 873 var predeclaredConstants = map[string]bool{ 874 "false": true, 875 "iota": true, 876 "nil": true, 877 "true": true, 878 }