parse.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package parser
  14. import (
  15. "fmt"
  16. "go/ast"
  17. "go/build"
  18. "go/parser"
  19. "go/token"
  20. tc "go/types"
  21. "io/ioutil"
  22. "os"
  23. "os/exec"
  24. "path"
  25. "path/filepath"
  26. "sort"
  27. "strings"
  28. "k8s.io/gengo/types"
  29. "k8s.io/klog"
  30. )
  31. // This clarifies when a pkg path has been canonicalized.
  32. type importPathString string
  33. // Builder lets you add all the go files in all the packages that you care
  34. // about, then constructs the type source data.
  35. type Builder struct {
  36. context *build.Context
  37. // Map of package names to more canonical information about the package.
  38. // This might hold the same value for multiple names, e.g. if someone
  39. // referenced ./pkg/name or in the case of vendoring, which canonicalizes
  40. // differently that what humans would type.
  41. buildPackages map[string]*build.Package
  42. fset *token.FileSet
  43. // map of package path to list of parsed files
  44. parsed map[importPathString][]parsedFile
  45. // map of package path to absolute path (to prevent overlap)
  46. absPaths map[importPathString]string
  47. // Set by typeCheckPackage(), used by importPackage() and friends.
  48. typeCheckedPackages map[importPathString]*tc.Package
  49. // Map of package path to whether the user requested it or it was from
  50. // an import.
  51. userRequested map[importPathString]bool
  52. // All comments from everywhere in every parsed file.
  53. endLineToCommentGroup map[fileLine]*ast.CommentGroup
  54. // map of package to list of packages it imports.
  55. importGraph map[importPathString]map[string]struct{}
  56. }
  57. // parsedFile is for tracking files with name
  58. type parsedFile struct {
  59. name string
  60. file *ast.File
  61. }
  62. // key type for finding comments.
  63. type fileLine struct {
  64. file string
  65. line int
  66. }
  67. // New constructs a new builder.
  68. func New() *Builder {
  69. c := build.Default
  70. if c.GOROOT == "" {
  71. if p, err := exec.Command("which", "go").CombinedOutput(); err == nil {
  72. // The returned string will have some/path/bin/go, so remove the last two elements.
  73. c.GOROOT = filepath.Dir(filepath.Dir(strings.Trim(string(p), "\n")))
  74. } else {
  75. klog.Warningf("Warning: $GOROOT not set, and unable to run `which go` to find it: %v\n", err)
  76. }
  77. }
  78. // Force this to off, since we don't properly parse CGo. All symbols must
  79. // have non-CGo equivalents.
  80. c.CgoEnabled = false
  81. return &Builder{
  82. context: &c,
  83. buildPackages: map[string]*build.Package{},
  84. typeCheckedPackages: map[importPathString]*tc.Package{},
  85. fset: token.NewFileSet(),
  86. parsed: map[importPathString][]parsedFile{},
  87. absPaths: map[importPathString]string{},
  88. userRequested: map[importPathString]bool{},
  89. endLineToCommentGroup: map[fileLine]*ast.CommentGroup{},
  90. importGraph: map[importPathString]map[string]struct{}{},
  91. }
  92. }
  93. // AddBuildTags adds the specified build tags to the parse context.
  94. func (b *Builder) AddBuildTags(tags ...string) {
  95. b.context.BuildTags = append(b.context.BuildTags, tags...)
  96. }
  97. // Get package information from the go/build package. Automatically excludes
  98. // e.g. test files and files for other platforms-- there is quite a bit of
  99. // logic of that nature in the build package.
  100. func (b *Builder) importBuildPackage(dir string) (*build.Package, error) {
  101. if buildPkg, ok := b.buildPackages[dir]; ok {
  102. return buildPkg, nil
  103. }
  104. // This validates the `package foo // github.com/bar/foo` comments.
  105. buildPkg, err := b.importWithMode(dir, build.ImportComment)
  106. if err != nil {
  107. if _, ok := err.(*build.NoGoError); !ok {
  108. return nil, fmt.Errorf("unable to import %q: %v", dir, err)
  109. }
  110. }
  111. if buildPkg == nil {
  112. // Might be an empty directory. Try to just find the dir.
  113. buildPkg, err = b.importWithMode(dir, build.FindOnly)
  114. if err != nil {
  115. return nil, err
  116. }
  117. }
  118. // Remember it under the user-provided name.
  119. klog.V(5).Infof("saving buildPackage %s", dir)
  120. b.buildPackages[dir] = buildPkg
  121. canonicalPackage := canonicalizeImportPath(buildPkg.ImportPath)
  122. if dir != string(canonicalPackage) {
  123. // Since `dir` is not the canonical name, see if we knew it under another name.
  124. if buildPkg, ok := b.buildPackages[string(canonicalPackage)]; ok {
  125. return buildPkg, nil
  126. }
  127. // Must be new, save it under the canonical name, too.
  128. klog.V(5).Infof("saving buildPackage %s", canonicalPackage)
  129. b.buildPackages[string(canonicalPackage)] = buildPkg
  130. }
  131. return buildPkg, nil
  132. }
  133. // AddFileForTest adds a file to the set, without verifying that the provided
  134. // pkg actually exists on disk. The pkg must be of the form "canonical/pkg/path"
  135. // and the path must be the absolute path to the file. Because this bypasses
  136. // the normal recursive finding of package dependencies (on disk), test should
  137. // sort their test files topologically first, so all deps are resolved by the
  138. // time we need them.
  139. func (b *Builder) AddFileForTest(pkg string, path string, src []byte) error {
  140. if err := b.addFile(importPathString(pkg), path, src, true); err != nil {
  141. return err
  142. }
  143. if _, err := b.typeCheckPackage(importPathString(pkg)); err != nil {
  144. return err
  145. }
  146. return nil
  147. }
  148. // addFile adds a file to the set. The pkgPath must be of the form
  149. // "canonical/pkg/path" and the path must be the absolute path to the file. A
  150. // flag indicates whether this file was user-requested or just from following
  151. // the import graph.
  152. func (b *Builder) addFile(pkgPath importPathString, path string, src []byte, userRequested bool) error {
  153. for _, p := range b.parsed[pkgPath] {
  154. if path == p.name {
  155. klog.V(5).Infof("addFile %s %s already parsed, skipping", pkgPath, path)
  156. return nil
  157. }
  158. }
  159. klog.V(6).Infof("addFile %s %s", pkgPath, path)
  160. p, err := parser.ParseFile(b.fset, path, src, parser.DeclarationErrors|parser.ParseComments)
  161. if err != nil {
  162. return err
  163. }
  164. // This is redundant with addDir, but some tests call AddFileForTest, which
  165. // call into here without calling addDir.
  166. b.userRequested[pkgPath] = userRequested || b.userRequested[pkgPath]
  167. b.parsed[pkgPath] = append(b.parsed[pkgPath], parsedFile{path, p})
  168. for _, c := range p.Comments {
  169. position := b.fset.Position(c.End())
  170. b.endLineToCommentGroup[fileLine{position.Filename, position.Line}] = c
  171. }
  172. // We have to get the packages from this specific file, in case the
  173. // user added individual files instead of entire directories.
  174. if b.importGraph[pkgPath] == nil {
  175. b.importGraph[pkgPath] = map[string]struct{}{}
  176. }
  177. for _, im := range p.Imports {
  178. importedPath := strings.Trim(im.Path.Value, `"`)
  179. b.importGraph[pkgPath][importedPath] = struct{}{}
  180. }
  181. return nil
  182. }
  183. // AddDir adds an entire directory, scanning it for go files. 'dir' should have
  184. // a single go package in it. GOPATH, GOROOT, and the location of your go
  185. // binary (`which go`) will all be searched if dir doesn't literally resolve.
  186. func (b *Builder) AddDir(dir string) error {
  187. _, err := b.importPackage(dir, true)
  188. return err
  189. }
  190. // AddDirRecursive is just like AddDir, but it also recursively adds
  191. // subdirectories; it returns an error only if the path couldn't be resolved;
  192. // any directories recursed into without go source are ignored.
  193. func (b *Builder) AddDirRecursive(dir string) error {
  194. // Add the root.
  195. if _, err := b.importPackage(dir, true); err != nil {
  196. klog.Warningf("Ignoring directory %v: %v", dir, err)
  197. }
  198. // filepath.Walk includes the root dir, but we already did that, so we'll
  199. // remove that prefix and rebuild a package import path.
  200. prefix := b.buildPackages[dir].Dir
  201. fn := func(filePath string, info os.FileInfo, err error) error {
  202. if info != nil && info.IsDir() {
  203. rel := filepath.ToSlash(strings.TrimPrefix(filePath, prefix))
  204. if rel != "" {
  205. // Make a pkg path.
  206. pkg := path.Join(string(canonicalizeImportPath(b.buildPackages[dir].ImportPath)), rel)
  207. // Add it.
  208. if _, err := b.importPackage(pkg, true); err != nil {
  209. klog.Warningf("Ignoring child directory %v: %v", pkg, err)
  210. }
  211. }
  212. }
  213. return nil
  214. }
  215. if err := filepath.Walk(b.buildPackages[dir].Dir, fn); err != nil {
  216. return err
  217. }
  218. return nil
  219. }
  220. // AddDirTo adds an entire directory to a given Universe. Unlike AddDir, this
  221. // processes the package immediately, which makes it safe to use from within a
  222. // generator (rather than just at init time. 'dir' must be a single go package.
  223. // GOPATH, GOROOT, and the location of your go binary (`which go`) will all be
  224. // searched if dir doesn't literally resolve.
  225. // Deprecated. Please use AddDirectoryTo.
  226. func (b *Builder) AddDirTo(dir string, u *types.Universe) error {
  227. // We want all types from this package, as if they were directly added
  228. // by the user. They WERE added by the user, in effect.
  229. if _, err := b.importPackage(dir, true); err != nil {
  230. return err
  231. }
  232. return b.findTypesIn(canonicalizeImportPath(b.buildPackages[dir].ImportPath), u)
  233. }
  234. // AddDirectoryTo adds an entire directory to a given Universe. Unlike AddDir,
  235. // this processes the package immediately, which makes it safe to use from
  236. // within a generator (rather than just at init time. 'dir' must be a single go
  237. // package. GOPATH, GOROOT, and the location of your go binary (`which go`)
  238. // will all be searched if dir doesn't literally resolve.
  239. func (b *Builder) AddDirectoryTo(dir string, u *types.Universe) (*types.Package, error) {
  240. // We want all types from this package, as if they were directly added
  241. // by the user. They WERE added by the user, in effect.
  242. if _, err := b.importPackage(dir, true); err != nil {
  243. return nil, err
  244. }
  245. path := canonicalizeImportPath(b.buildPackages[dir].ImportPath)
  246. if err := b.findTypesIn(path, u); err != nil {
  247. return nil, err
  248. }
  249. return u.Package(string(path)), nil
  250. }
  251. // The implementation of AddDir. A flag indicates whether this directory was
  252. // user-requested or just from following the import graph.
  253. func (b *Builder) addDir(dir string, userRequested bool) error {
  254. klog.V(5).Infof("addDir %s", dir)
  255. buildPkg, err := b.importBuildPackage(dir)
  256. if err != nil {
  257. return err
  258. }
  259. canonicalPackage := canonicalizeImportPath(buildPkg.ImportPath)
  260. pkgPath := canonicalPackage
  261. if dir != string(canonicalPackage) {
  262. klog.V(5).Infof("addDir %s, canonical path is %s", dir, pkgPath)
  263. }
  264. // Sanity check the pkg dir has not changed.
  265. if prev, found := b.absPaths[pkgPath]; found {
  266. if buildPkg.Dir != prev {
  267. return fmt.Errorf("package %q (%s) previously resolved to %s", pkgPath, buildPkg.Dir, prev)
  268. }
  269. } else {
  270. b.absPaths[pkgPath] = buildPkg.Dir
  271. }
  272. for _, n := range buildPkg.GoFiles {
  273. if !strings.HasSuffix(n, ".go") {
  274. continue
  275. }
  276. absPath := filepath.Join(buildPkg.Dir, n)
  277. data, err := ioutil.ReadFile(absPath)
  278. if err != nil {
  279. return fmt.Errorf("while loading %q: %v", absPath, err)
  280. }
  281. err = b.addFile(pkgPath, absPath, data, userRequested)
  282. if err != nil {
  283. return fmt.Errorf("while parsing %q: %v", absPath, err)
  284. }
  285. }
  286. return nil
  287. }
  288. // importPackage is a function that will be called by the type check package when it
  289. // needs to import a go package. 'path' is the import path.
  290. func (b *Builder) importPackage(dir string, userRequested bool) (*tc.Package, error) {
  291. klog.V(5).Infof("importPackage %s", dir)
  292. var pkgPath = importPathString(dir)
  293. // Get the canonical path if we can.
  294. if buildPkg := b.buildPackages[dir]; buildPkg != nil {
  295. canonicalPackage := canonicalizeImportPath(buildPkg.ImportPath)
  296. klog.V(5).Infof("importPackage %s, canonical path is %s", dir, canonicalPackage)
  297. pkgPath = canonicalPackage
  298. }
  299. // If we have not seen this before, process it now.
  300. ignoreError := false
  301. if _, found := b.parsed[pkgPath]; !found {
  302. // Ignore errors in paths that we're importing solely because
  303. // they're referenced by other packages.
  304. ignoreError = true
  305. // Add it.
  306. if err := b.addDir(dir, userRequested); err != nil {
  307. return nil, err
  308. }
  309. // Get the canonical path now that it has been added.
  310. if buildPkg := b.buildPackages[dir]; buildPkg != nil {
  311. canonicalPackage := canonicalizeImportPath(buildPkg.ImportPath)
  312. klog.V(5).Infof("importPackage %s, canonical path is %s", dir, canonicalPackage)
  313. pkgPath = canonicalPackage
  314. }
  315. }
  316. // If it was previously known, just check that the user-requestedness hasn't
  317. // changed.
  318. b.userRequested[pkgPath] = userRequested || b.userRequested[pkgPath]
  319. // Run the type checker. We may end up doing this to pkgs that are already
  320. // done, or are in the queue to be done later, but it will short-circuit,
  321. // and we can't miss pkgs that are only depended on.
  322. pkg, err := b.typeCheckPackage(pkgPath)
  323. if err != nil {
  324. switch {
  325. case ignoreError && pkg != nil:
  326. klog.V(2).Infof("type checking encountered some issues in %q, but ignoring.\n", pkgPath)
  327. case !ignoreError && pkg != nil:
  328. klog.V(2).Infof("type checking encountered some errors in %q\n", pkgPath)
  329. return nil, err
  330. default:
  331. return nil, err
  332. }
  333. }
  334. return pkg, nil
  335. }
  336. type importAdapter struct {
  337. b *Builder
  338. }
  339. func (a importAdapter) Import(path string) (*tc.Package, error) {
  340. return a.b.importPackage(path, false)
  341. }
  342. // typeCheckPackage will attempt to return the package even if there are some
  343. // errors, so you may check whether the package is nil or not even if you get
  344. // an error.
  345. func (b *Builder) typeCheckPackage(pkgPath importPathString) (*tc.Package, error) {
  346. klog.V(5).Infof("typeCheckPackage %s", pkgPath)
  347. if pkg, ok := b.typeCheckedPackages[pkgPath]; ok {
  348. if pkg != nil {
  349. klog.V(6).Infof("typeCheckPackage %s already done", pkgPath)
  350. return pkg, nil
  351. }
  352. // We store a nil right before starting work on a package. So
  353. // if we get here and it's present and nil, that means there's
  354. // another invocation of this function on the call stack
  355. // already processing this package.
  356. return nil, fmt.Errorf("circular dependency for %q", pkgPath)
  357. }
  358. parsedFiles, ok := b.parsed[pkgPath]
  359. if !ok {
  360. return nil, fmt.Errorf("No files for pkg %q", pkgPath)
  361. }
  362. files := make([]*ast.File, len(parsedFiles))
  363. for i := range parsedFiles {
  364. files[i] = parsedFiles[i].file
  365. }
  366. b.typeCheckedPackages[pkgPath] = nil
  367. c := tc.Config{
  368. IgnoreFuncBodies: true,
  369. // Note that importAdapter can call b.importPackage which calls this
  370. // method. So there can't be cycles in the import graph.
  371. Importer: importAdapter{b},
  372. Error: func(err error) {
  373. klog.V(2).Infof("type checker: %v\n", err)
  374. },
  375. }
  376. pkg, err := c.Check(string(pkgPath), b.fset, files, nil)
  377. b.typeCheckedPackages[pkgPath] = pkg // record the result whether or not there was an error
  378. return pkg, err
  379. }
  380. // FindPackages fetches a list of the user-imported packages.
  381. // Note that you need to call b.FindTypes() first.
  382. func (b *Builder) FindPackages() []string {
  383. // Iterate packages in a predictable order.
  384. pkgPaths := []string{}
  385. for k := range b.typeCheckedPackages {
  386. pkgPaths = append(pkgPaths, string(k))
  387. }
  388. sort.Strings(pkgPaths)
  389. result := []string{}
  390. for _, pkgPath := range pkgPaths {
  391. if b.userRequested[importPathString(pkgPath)] {
  392. // Since walkType is recursive, all types that are in packages that
  393. // were directly mentioned will be included. We don't need to
  394. // include all types in all transitive packages, though.
  395. result = append(result, pkgPath)
  396. }
  397. }
  398. return result
  399. }
  400. // FindTypes finalizes the package imports, and searches through all the
  401. // packages for types.
  402. func (b *Builder) FindTypes() (types.Universe, error) {
  403. // Take a snapshot of pkgs to iterate, since this will recursively mutate
  404. // b.parsed. Iterate in a predictable order.
  405. pkgPaths := []string{}
  406. for pkgPath := range b.parsed {
  407. pkgPaths = append(pkgPaths, string(pkgPath))
  408. }
  409. sort.Strings(pkgPaths)
  410. u := types.Universe{}
  411. for _, pkgPath := range pkgPaths {
  412. if err := b.findTypesIn(importPathString(pkgPath), &u); err != nil {
  413. return nil, err
  414. }
  415. }
  416. return u, nil
  417. }
  418. // findTypesIn finalizes the package import and searches through the package
  419. // for types.
  420. func (b *Builder) findTypesIn(pkgPath importPathString, u *types.Universe) error {
  421. klog.V(5).Infof("findTypesIn %s", pkgPath)
  422. pkg := b.typeCheckedPackages[pkgPath]
  423. if pkg == nil {
  424. return fmt.Errorf("findTypesIn(%s): package is not known", pkgPath)
  425. }
  426. if !b.userRequested[pkgPath] {
  427. // Since walkType is recursive, all types that the
  428. // packages they asked for depend on will be included.
  429. // But we don't need to include all types in all
  430. // *packages* they depend on.
  431. klog.V(5).Infof("findTypesIn %s: package is not user requested", pkgPath)
  432. return nil
  433. }
  434. // We're keeping this package. This call will create the record.
  435. u.Package(string(pkgPath)).Name = pkg.Name()
  436. u.Package(string(pkgPath)).Path = pkg.Path()
  437. u.Package(string(pkgPath)).SourcePath = b.absPaths[pkgPath]
  438. for _, f := range b.parsed[pkgPath] {
  439. if _, fileName := filepath.Split(f.name); fileName == "doc.go" {
  440. tp := u.Package(string(pkgPath))
  441. // findTypesIn might be called multiple times. Clean up tp.Comments
  442. // to avoid repeatedly fill same comments to it.
  443. tp.Comments = []string{}
  444. for i := range f.file.Comments {
  445. tp.Comments = append(tp.Comments, splitLines(f.file.Comments[i].Text())...)
  446. }
  447. if f.file.Doc != nil {
  448. tp.DocComments = splitLines(f.file.Doc.Text())
  449. }
  450. }
  451. }
  452. s := pkg.Scope()
  453. for _, n := range s.Names() {
  454. obj := s.Lookup(n)
  455. tn, ok := obj.(*tc.TypeName)
  456. if ok {
  457. t := b.walkType(*u, nil, tn.Type())
  458. c1 := b.priorCommentLines(obj.Pos(), 1)
  459. // c1.Text() is safe if c1 is nil
  460. t.CommentLines = splitLines(c1.Text())
  461. if c1 == nil {
  462. t.SecondClosestCommentLines = splitLines(b.priorCommentLines(obj.Pos(), 2).Text())
  463. } else {
  464. t.SecondClosestCommentLines = splitLines(b.priorCommentLines(c1.List[0].Slash, 2).Text())
  465. }
  466. }
  467. tf, ok := obj.(*tc.Func)
  468. // We only care about functions, not concrete/abstract methods.
  469. if ok && tf.Type() != nil && tf.Type().(*tc.Signature).Recv() == nil {
  470. t := b.addFunction(*u, nil, tf)
  471. c1 := b.priorCommentLines(obj.Pos(), 1)
  472. // c1.Text() is safe if c1 is nil
  473. t.CommentLines = splitLines(c1.Text())
  474. if c1 == nil {
  475. t.SecondClosestCommentLines = splitLines(b.priorCommentLines(obj.Pos(), 2).Text())
  476. } else {
  477. t.SecondClosestCommentLines = splitLines(b.priorCommentLines(c1.List[0].Slash, 2).Text())
  478. }
  479. }
  480. tv, ok := obj.(*tc.Var)
  481. if ok && !tv.IsField() {
  482. b.addVariable(*u, nil, tv)
  483. }
  484. }
  485. importedPkgs := []string{}
  486. for k := range b.importGraph[pkgPath] {
  487. importedPkgs = append(importedPkgs, string(k))
  488. }
  489. sort.Strings(importedPkgs)
  490. for _, p := range importedPkgs {
  491. u.AddImports(string(pkgPath), p)
  492. }
  493. return nil
  494. }
  495. func (b *Builder) importWithMode(dir string, mode build.ImportMode) (*build.Package, error) {
  496. // This is a bit of a hack. The srcDir argument to Import() should
  497. // properly be the dir of the file which depends on the package to be
  498. // imported, so that vendoring can work properly and local paths can
  499. // resolve. We assume that there is only one level of vendoring, and that
  500. // the CWD is inside the GOPATH, so this should be safe. Nobody should be
  501. // using local (relative) paths except on the CLI, so CWD is also
  502. // sufficient.
  503. cwd, err := os.Getwd()
  504. if err != nil {
  505. return nil, fmt.Errorf("unable to get current directory: %v", err)
  506. }
  507. buildPkg, err := b.context.Import(dir, cwd, mode)
  508. if err != nil {
  509. return nil, err
  510. }
  511. return buildPkg, nil
  512. }
  513. // if there's a comment on the line `lines` before pos, return its text, otherwise "".
  514. func (b *Builder) priorCommentLines(pos token.Pos, lines int) *ast.CommentGroup {
  515. position := b.fset.Position(pos)
  516. key := fileLine{position.Filename, position.Line - lines}
  517. return b.endLineToCommentGroup[key]
  518. }
  519. func splitLines(str string) []string {
  520. return strings.Split(strings.TrimRight(str, "\n"), "\n")
  521. }
  522. func tcFuncNameToName(in string) types.Name {
  523. name := strings.TrimPrefix(in, "func ")
  524. nameParts := strings.Split(name, "(")
  525. return tcNameToName(nameParts[0])
  526. }
  527. func tcVarNameToName(in string) types.Name {
  528. nameParts := strings.Split(in, " ")
  529. // nameParts[0] is "var".
  530. // nameParts[2:] is the type of the variable, we ignore it for now.
  531. return tcNameToName(nameParts[1])
  532. }
  533. func tcNameToName(in string) types.Name {
  534. // Detect anonymous type names. (These may have '.' characters because
  535. // embedded types may have packages, so we detect them specially.)
  536. if strings.HasPrefix(in, "struct{") ||
  537. strings.HasPrefix(in, "<-chan") ||
  538. strings.HasPrefix(in, "chan<-") ||
  539. strings.HasPrefix(in, "chan ") ||
  540. strings.HasPrefix(in, "func(") ||
  541. strings.HasPrefix(in, "*") ||
  542. strings.HasPrefix(in, "map[") ||
  543. strings.HasPrefix(in, "[") {
  544. return types.Name{Name: in}
  545. }
  546. // Otherwise, if there are '.' characters present, the name has a
  547. // package path in front.
  548. nameParts := strings.Split(in, ".")
  549. name := types.Name{Name: in}
  550. if n := len(nameParts); n >= 2 {
  551. // The final "." is the name of the type--previous ones must
  552. // have been in the package path.
  553. name.Package, name.Name = strings.Join(nameParts[:n-1], "."), nameParts[n-1]
  554. }
  555. return name
  556. }
  557. func (b *Builder) convertSignature(u types.Universe, t *tc.Signature) *types.Signature {
  558. signature := &types.Signature{}
  559. for i := 0; i < t.Params().Len(); i++ {
  560. signature.Parameters = append(signature.Parameters, b.walkType(u, nil, t.Params().At(i).Type()))
  561. }
  562. for i := 0; i < t.Results().Len(); i++ {
  563. signature.Results = append(signature.Results, b.walkType(u, nil, t.Results().At(i).Type()))
  564. }
  565. if r := t.Recv(); r != nil {
  566. signature.Receiver = b.walkType(u, nil, r.Type())
  567. }
  568. signature.Variadic = t.Variadic()
  569. return signature
  570. }
  571. // walkType adds the type, and any necessary child types.
  572. func (b *Builder) walkType(u types.Universe, useName *types.Name, in tc.Type) *types.Type {
  573. // Most of the cases are underlying types of the named type.
  574. name := tcNameToName(in.String())
  575. if useName != nil {
  576. name = *useName
  577. }
  578. switch t := in.(type) {
  579. case *tc.Struct:
  580. out := u.Type(name)
  581. if out.Kind != types.Unknown {
  582. return out
  583. }
  584. out.Kind = types.Struct
  585. for i := 0; i < t.NumFields(); i++ {
  586. f := t.Field(i)
  587. m := types.Member{
  588. Name: f.Name(),
  589. Embedded: f.Anonymous(),
  590. Tags: t.Tag(i),
  591. Type: b.walkType(u, nil, f.Type()),
  592. CommentLines: splitLines(b.priorCommentLines(f.Pos(), 1).Text()),
  593. }
  594. out.Members = append(out.Members, m)
  595. }
  596. return out
  597. case *tc.Map:
  598. out := u.Type(name)
  599. if out.Kind != types.Unknown {
  600. return out
  601. }
  602. out.Kind = types.Map
  603. out.Elem = b.walkType(u, nil, t.Elem())
  604. out.Key = b.walkType(u, nil, t.Key())
  605. return out
  606. case *tc.Pointer:
  607. out := u.Type(name)
  608. if out.Kind != types.Unknown {
  609. return out
  610. }
  611. out.Kind = types.Pointer
  612. out.Elem = b.walkType(u, nil, t.Elem())
  613. return out
  614. case *tc.Slice:
  615. out := u.Type(name)
  616. if out.Kind != types.Unknown {
  617. return out
  618. }
  619. out.Kind = types.Slice
  620. out.Elem = b.walkType(u, nil, t.Elem())
  621. return out
  622. case *tc.Array:
  623. out := u.Type(name)
  624. if out.Kind != types.Unknown {
  625. return out
  626. }
  627. out.Kind = types.Array
  628. out.Elem = b.walkType(u, nil, t.Elem())
  629. // TODO: need to store array length, otherwise raw type name
  630. // cannot be properly written.
  631. return out
  632. case *tc.Chan:
  633. out := u.Type(name)
  634. if out.Kind != types.Unknown {
  635. return out
  636. }
  637. out.Kind = types.Chan
  638. out.Elem = b.walkType(u, nil, t.Elem())
  639. // TODO: need to store direction, otherwise raw type name
  640. // cannot be properly written.
  641. return out
  642. case *tc.Basic:
  643. out := u.Type(types.Name{
  644. Package: "",
  645. Name: t.Name(),
  646. })
  647. if out.Kind != types.Unknown {
  648. return out
  649. }
  650. out.Kind = types.Unsupported
  651. return out
  652. case *tc.Signature:
  653. out := u.Type(name)
  654. if out.Kind != types.Unknown {
  655. return out
  656. }
  657. out.Kind = types.Func
  658. out.Signature = b.convertSignature(u, t)
  659. return out
  660. case *tc.Interface:
  661. out := u.Type(name)
  662. if out.Kind != types.Unknown {
  663. return out
  664. }
  665. out.Kind = types.Interface
  666. t.Complete()
  667. for i := 0; i < t.NumMethods(); i++ {
  668. if out.Methods == nil {
  669. out.Methods = map[string]*types.Type{}
  670. }
  671. out.Methods[t.Method(i).Name()] = b.walkType(u, nil, t.Method(i).Type())
  672. }
  673. return out
  674. case *tc.Named:
  675. var out *types.Type
  676. switch t.Underlying().(type) {
  677. case *tc.Named, *tc.Basic, *tc.Map, *tc.Slice:
  678. name := tcNameToName(t.String())
  679. out = u.Type(name)
  680. if out.Kind != types.Unknown {
  681. return out
  682. }
  683. out.Kind = types.Alias
  684. out.Underlying = b.walkType(u, nil, t.Underlying())
  685. default:
  686. // tc package makes everything "named" with an
  687. // underlying anonymous type--we remove that annoying
  688. // "feature" for users. This flattens those types
  689. // together.
  690. name := tcNameToName(t.String())
  691. if out := u.Type(name); out.Kind != types.Unknown {
  692. return out // short circuit if we've already made this.
  693. }
  694. out = b.walkType(u, &name, t.Underlying())
  695. }
  696. // If the underlying type didn't already add methods, add them.
  697. // (Interface types will have already added methods.)
  698. if len(out.Methods) == 0 {
  699. for i := 0; i < t.NumMethods(); i++ {
  700. if out.Methods == nil {
  701. out.Methods = map[string]*types.Type{}
  702. }
  703. out.Methods[t.Method(i).Name()] = b.walkType(u, nil, t.Method(i).Type())
  704. }
  705. }
  706. return out
  707. default:
  708. out := u.Type(name)
  709. if out.Kind != types.Unknown {
  710. return out
  711. }
  712. out.Kind = types.Unsupported
  713. klog.Warningf("Making unsupported type entry %q for: %#v\n", out, t)
  714. return out
  715. }
  716. }
  717. func (b *Builder) addFunction(u types.Universe, useName *types.Name, in *tc.Func) *types.Type {
  718. name := tcFuncNameToName(in.String())
  719. if useName != nil {
  720. name = *useName
  721. }
  722. out := u.Function(name)
  723. out.Kind = types.DeclarationOf
  724. out.Underlying = b.walkType(u, nil, in.Type())
  725. return out
  726. }
  727. func (b *Builder) addVariable(u types.Universe, useName *types.Name, in *tc.Var) *types.Type {
  728. name := tcVarNameToName(in.String())
  729. if useName != nil {
  730. name = *useName
  731. }
  732. out := u.Variable(name)
  733. out.Kind = types.DeclarationOf
  734. out.Underlying = b.walkType(u, nil, in.Type())
  735. return out
  736. }
  737. // canonicalizeImportPath takes an import path and returns the actual package.
  738. // It doesn't support nested vendoring.
  739. func canonicalizeImportPath(importPath string) importPathString {
  740. if !strings.Contains(importPath, "/vendor/") {
  741. return importPathString(importPath)
  742. }
  743. return importPathString(importPath[strings.Index(importPath, "/vendor/")+len("/vendor/"):])
  744. }