parse.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // Copyright 2012 Google Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. // This file contains the model construction by parsing source files.
  16. import (
  17. "flag"
  18. "fmt"
  19. "go/ast"
  20. "go/build"
  21. "go/parser"
  22. "go/token"
  23. "log"
  24. "path"
  25. "path/filepath"
  26. "strconv"
  27. "strings"
  28. "github.com/golang/mock/mockgen/model"
  29. )
  30. var (
  31. imports = flag.String("imports", "", "(source mode) Comma-separated name=path pairs of explicit imports to use.")
  32. auxFiles = flag.String("aux_files", "", "(source mode) Comma-separated pkg=path pairs of auxiliary Go source files.")
  33. )
  34. // TODO: simplify error reporting
  35. func ParseFile(source string) (*model.Package, error) {
  36. srcDir, err := filepath.Abs(filepath.Dir(source))
  37. if err != nil {
  38. return nil, fmt.Errorf("failed getting source directory: %v", err)
  39. }
  40. var packageImport string
  41. if p, err := build.ImportDir(srcDir, 0); err == nil {
  42. packageImport = p.ImportPath
  43. } // TODO: should we fail if this returns an error?
  44. fs := token.NewFileSet()
  45. file, err := parser.ParseFile(fs, source, nil, 0)
  46. if err != nil {
  47. return nil, fmt.Errorf("failed parsing source file %v: %v", source, err)
  48. }
  49. p := &fileParser{
  50. fileSet: fs,
  51. imports: make(map[string]string),
  52. importedInterfaces: make(map[string]map[string]*ast.InterfaceType),
  53. auxInterfaces: make(map[string]map[string]*ast.InterfaceType),
  54. srcDir: srcDir,
  55. }
  56. // Handle -imports.
  57. dotImports := make(map[string]bool)
  58. if *imports != "" {
  59. for _, kv := range strings.Split(*imports, ",") {
  60. eq := strings.Index(kv, "=")
  61. k, v := kv[:eq], kv[eq+1:]
  62. if k == "." {
  63. // TODO: Catch dupes?
  64. dotImports[v] = true
  65. } else {
  66. // TODO: Catch dupes?
  67. p.imports[k] = v
  68. }
  69. }
  70. }
  71. // Handle -aux_files.
  72. if err := p.parseAuxFiles(*auxFiles); err != nil {
  73. return nil, err
  74. }
  75. p.addAuxInterfacesFromFile(packageImport, file) // this file
  76. pkg, err := p.parseFile(packageImport, file)
  77. if err != nil {
  78. return nil, err
  79. }
  80. pkg.DotImports = make([]string, 0, len(dotImports))
  81. for path := range dotImports {
  82. pkg.DotImports = append(pkg.DotImports, path)
  83. }
  84. return pkg, nil
  85. }
  86. type fileParser struct {
  87. fileSet *token.FileSet
  88. imports map[string]string // package name => import path
  89. importedInterfaces map[string]map[string]*ast.InterfaceType // package (or "") => name => interface
  90. auxFiles []*ast.File
  91. auxInterfaces map[string]map[string]*ast.InterfaceType // package (or "") => name => interface
  92. srcDir string
  93. }
  94. func (p *fileParser) errorf(pos token.Pos, format string, args ...interface{}) error {
  95. ps := p.fileSet.Position(pos)
  96. format = "%s:%d:%d: " + format
  97. args = append([]interface{}{ps.Filename, ps.Line, ps.Column}, args...)
  98. return fmt.Errorf(format, args...)
  99. }
  100. func (p *fileParser) parseAuxFiles(auxFiles string) error {
  101. auxFiles = strings.TrimSpace(auxFiles)
  102. if auxFiles == "" {
  103. return nil
  104. }
  105. for _, kv := range strings.Split(auxFiles, ",") {
  106. parts := strings.SplitN(kv, "=", 2)
  107. if len(parts) != 2 {
  108. return fmt.Errorf("bad aux file spec: %v", kv)
  109. }
  110. pkg, fpath := parts[0], parts[1]
  111. file, err := parser.ParseFile(p.fileSet, fpath, nil, 0)
  112. if err != nil {
  113. return err
  114. }
  115. p.auxFiles = append(p.auxFiles, file)
  116. p.addAuxInterfacesFromFile(pkg, file)
  117. }
  118. return nil
  119. }
  120. func (p *fileParser) addAuxInterfacesFromFile(pkg string, file *ast.File) {
  121. if _, ok := p.auxInterfaces[pkg]; !ok {
  122. p.auxInterfaces[pkg] = make(map[string]*ast.InterfaceType)
  123. }
  124. for ni := range iterInterfaces(file) {
  125. p.auxInterfaces[pkg][ni.name.Name] = ni.it
  126. }
  127. }
  128. // parseFile loads all file imports and auxiliary files import into the
  129. // fileParser, parses all file interfaces and returns package model.
  130. func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Package, error) {
  131. allImports := importsOfFile(file)
  132. // Don't stomp imports provided by -imports. Those should take precedence.
  133. for pkg, path := range allImports {
  134. if _, ok := p.imports[pkg]; !ok {
  135. p.imports[pkg] = path
  136. }
  137. }
  138. // Add imports from auxiliary files, which might be needed for embedded interfaces.
  139. // Don't stomp any other imports.
  140. for _, f := range p.auxFiles {
  141. for pkg, path := range importsOfFile(f) {
  142. if _, ok := p.imports[pkg]; !ok {
  143. p.imports[pkg] = path
  144. }
  145. }
  146. }
  147. var is []*model.Interface
  148. for ni := range iterInterfaces(file) {
  149. i, err := p.parseInterface(ni.name.String(), importPath, ni.it)
  150. if err != nil {
  151. return nil, err
  152. }
  153. is = append(is, i)
  154. }
  155. return &model.Package{
  156. Name: file.Name.String(),
  157. Interfaces: is,
  158. }, nil
  159. }
  160. // parsePackage loads package specified by path, parses it and populates
  161. // corresponding imports and importedInterfaces into the fileParser.
  162. func (p *fileParser) parsePackage(path string) error {
  163. var pkgs map[string]*ast.Package
  164. if imp, err := build.Import(path, p.srcDir, build.FindOnly); err != nil {
  165. return err
  166. } else if pkgs, err = parser.ParseDir(p.fileSet, imp.Dir, nil, 0); err != nil {
  167. return err
  168. }
  169. for _, pkg := range pkgs {
  170. file := ast.MergePackageFiles(pkg, ast.FilterFuncDuplicates|ast.FilterUnassociatedComments|ast.FilterImportDuplicates)
  171. if _, ok := p.importedInterfaces[path]; !ok {
  172. p.importedInterfaces[path] = make(map[string]*ast.InterfaceType)
  173. }
  174. for ni := range iterInterfaces(file) {
  175. p.importedInterfaces[path][ni.name.Name] = ni.it
  176. }
  177. for pkgName, pkgPath := range importsOfFile(file) {
  178. if _, ok := p.imports[pkgName]; !ok {
  179. p.imports[pkgName] = pkgPath
  180. }
  181. }
  182. }
  183. return nil
  184. }
  185. func (p *fileParser) parseInterface(name, pkg string, it *ast.InterfaceType) (*model.Interface, error) {
  186. intf := &model.Interface{Name: name}
  187. for _, field := range it.Methods.List {
  188. switch v := field.Type.(type) {
  189. case *ast.FuncType:
  190. if nn := len(field.Names); nn != 1 {
  191. return nil, fmt.Errorf("expected one name for interface %v, got %d", intf.Name, nn)
  192. }
  193. m := &model.Method{
  194. Name: field.Names[0].String(),
  195. }
  196. var err error
  197. m.In, m.Variadic, m.Out, err = p.parseFunc(pkg, v)
  198. if err != nil {
  199. return nil, err
  200. }
  201. intf.Methods = append(intf.Methods, m)
  202. case *ast.Ident:
  203. // Embedded interface in this package.
  204. ei := p.auxInterfaces[pkg][v.String()]
  205. if ei == nil {
  206. if ei = p.importedInterfaces[pkg][v.String()]; ei == nil {
  207. return nil, p.errorf(v.Pos(), "unknown embedded interface %s", v.String())
  208. }
  209. }
  210. eintf, err := p.parseInterface(v.String(), pkg, ei)
  211. if err != nil {
  212. return nil, err
  213. }
  214. // Copy the methods.
  215. // TODO: apply shadowing rules.
  216. for _, m := range eintf.Methods {
  217. intf.Methods = append(intf.Methods, m)
  218. }
  219. case *ast.SelectorExpr:
  220. // Embedded interface in another package.
  221. fpkg, sel := v.X.(*ast.Ident).String(), v.Sel.String()
  222. epkg, ok := p.imports[fpkg]
  223. if !ok {
  224. return nil, p.errorf(v.X.Pos(), "unknown package %s", fpkg)
  225. }
  226. ei := p.auxInterfaces[fpkg][sel]
  227. if ei == nil {
  228. fpkg = epkg
  229. if _, ok = p.importedInterfaces[epkg]; !ok {
  230. if err := p.parsePackage(epkg); err != nil {
  231. return nil, p.errorf(v.Pos(), "could not parse package %s: %v", fpkg, err)
  232. }
  233. }
  234. if ei = p.importedInterfaces[epkg][sel]; ei == nil {
  235. return nil, p.errorf(v.Pos(), "unknown embedded interface %s.%s", fpkg, sel)
  236. }
  237. }
  238. eintf, err := p.parseInterface(sel, fpkg, ei)
  239. if err != nil {
  240. return nil, err
  241. }
  242. // Copy the methods.
  243. // TODO: apply shadowing rules.
  244. for _, m := range eintf.Methods {
  245. intf.Methods = append(intf.Methods, m)
  246. }
  247. default:
  248. return nil, fmt.Errorf("don't know how to mock method of type %T", field.Type)
  249. }
  250. }
  251. return intf, nil
  252. }
  253. func (p *fileParser) parseFunc(pkg string, f *ast.FuncType) (in []*model.Parameter, variadic *model.Parameter, out []*model.Parameter, err error) {
  254. if f.Params != nil {
  255. regParams := f.Params.List
  256. if isVariadic(f) {
  257. n := len(regParams)
  258. varParams := regParams[n-1:]
  259. regParams = regParams[:n-1]
  260. vp, err := p.parseFieldList(pkg, varParams)
  261. if err != nil {
  262. return nil, nil, nil, p.errorf(varParams[0].Pos(), "failed parsing variadic argument: %v", err)
  263. }
  264. variadic = vp[0]
  265. }
  266. in, err = p.parseFieldList(pkg, regParams)
  267. if err != nil {
  268. return nil, nil, nil, p.errorf(f.Pos(), "failed parsing arguments: %v", err)
  269. }
  270. }
  271. if f.Results != nil {
  272. out, err = p.parseFieldList(pkg, f.Results.List)
  273. if err != nil {
  274. return nil, nil, nil, p.errorf(f.Pos(), "failed parsing returns: %v", err)
  275. }
  276. }
  277. return
  278. }
  279. func (p *fileParser) parseFieldList(pkg string, fields []*ast.Field) ([]*model.Parameter, error) {
  280. nf := 0
  281. for _, f := range fields {
  282. nn := len(f.Names)
  283. if nn == 0 {
  284. nn = 1 // anonymous parameter
  285. }
  286. nf += nn
  287. }
  288. if nf == 0 {
  289. return nil, nil
  290. }
  291. ps := make([]*model.Parameter, nf)
  292. i := 0 // destination index
  293. for _, f := range fields {
  294. t, err := p.parseType(pkg, f.Type)
  295. if err != nil {
  296. return nil, err
  297. }
  298. if len(f.Names) == 0 {
  299. // anonymous arg
  300. ps[i] = &model.Parameter{Type: t}
  301. i++
  302. continue
  303. }
  304. for _, name := range f.Names {
  305. ps[i] = &model.Parameter{Name: name.Name, Type: t}
  306. i++
  307. }
  308. }
  309. return ps, nil
  310. }
  311. func (p *fileParser) parseType(pkg string, typ ast.Expr) (model.Type, error) {
  312. switch v := typ.(type) {
  313. case *ast.ArrayType:
  314. ln := -1
  315. if v.Len != nil {
  316. x, err := strconv.Atoi(v.Len.(*ast.BasicLit).Value)
  317. if err != nil {
  318. return nil, p.errorf(v.Len.Pos(), "bad array size: %v", err)
  319. }
  320. ln = x
  321. }
  322. t, err := p.parseType(pkg, v.Elt)
  323. if err != nil {
  324. return nil, err
  325. }
  326. return &model.ArrayType{Len: ln, Type: t}, nil
  327. case *ast.ChanType:
  328. t, err := p.parseType(pkg, v.Value)
  329. if err != nil {
  330. return nil, err
  331. }
  332. var dir model.ChanDir
  333. if v.Dir == ast.SEND {
  334. dir = model.SendDir
  335. }
  336. if v.Dir == ast.RECV {
  337. dir = model.RecvDir
  338. }
  339. return &model.ChanType{Dir: dir, Type: t}, nil
  340. case *ast.Ellipsis:
  341. // assume we're parsing a variadic argument
  342. return p.parseType(pkg, v.Elt)
  343. case *ast.FuncType:
  344. in, variadic, out, err := p.parseFunc(pkg, v)
  345. if err != nil {
  346. return nil, err
  347. }
  348. return &model.FuncType{In: in, Out: out, Variadic: variadic}, nil
  349. case *ast.Ident:
  350. if v.IsExported() {
  351. // `pkg` may be an aliased imported pkg
  352. // if so, patch the import w/ the fully qualified import
  353. maybeImportedPkg, ok := p.imports[pkg]
  354. if ok {
  355. pkg = maybeImportedPkg
  356. }
  357. // assume type in this package
  358. return &model.NamedType{Package: pkg, Type: v.Name}, nil
  359. } else {
  360. // assume predeclared type
  361. return model.PredeclaredType(v.Name), nil
  362. }
  363. case *ast.InterfaceType:
  364. if v.Methods != nil && len(v.Methods.List) > 0 {
  365. return nil, p.errorf(v.Pos(), "can't handle non-empty unnamed interface types")
  366. }
  367. return model.PredeclaredType("interface{}"), nil
  368. case *ast.MapType:
  369. key, err := p.parseType(pkg, v.Key)
  370. if err != nil {
  371. return nil, err
  372. }
  373. value, err := p.parseType(pkg, v.Value)
  374. if err != nil {
  375. return nil, err
  376. }
  377. return &model.MapType{Key: key, Value: value}, nil
  378. case *ast.SelectorExpr:
  379. pkgName := v.X.(*ast.Ident).String()
  380. pkg, ok := p.imports[pkgName]
  381. if !ok {
  382. return nil, p.errorf(v.Pos(), "unknown package %q", pkgName)
  383. }
  384. return &model.NamedType{Package: pkg, Type: v.Sel.String()}, nil
  385. case *ast.StarExpr:
  386. t, err := p.parseType(pkg, v.X)
  387. if err != nil {
  388. return nil, err
  389. }
  390. return &model.PointerType{Type: t}, nil
  391. case *ast.StructType:
  392. if v.Fields != nil && len(v.Fields.List) > 0 {
  393. return nil, p.errorf(v.Pos(), "can't handle non-empty unnamed struct types")
  394. }
  395. return model.PredeclaredType("struct{}"), nil
  396. }
  397. return nil, fmt.Errorf("don't know how to parse type %T", typ)
  398. }
  399. // importsOfFile returns a map of package name to import path
  400. // of the imports in file.
  401. func importsOfFile(file *ast.File) map[string]string {
  402. m := make(map[string]string)
  403. for _, is := range file.Imports {
  404. var pkgName string
  405. importPath := is.Path.Value[1 : len(is.Path.Value)-1] // remove quotes
  406. if is.Name != nil {
  407. // Named imports are always certain.
  408. if is.Name.Name == "_" {
  409. continue
  410. }
  411. pkgName = removeDot(is.Name.Name)
  412. } else {
  413. pkg, err := build.Import(importPath, "", 0)
  414. if err != nil {
  415. // Fallback to import path suffix. Note that this is uncertain.
  416. _, last := path.Split(importPath)
  417. // If the last path component has dots, the first dot-delimited
  418. // field is used as the name.
  419. pkgName = strings.SplitN(last, ".", 2)[0]
  420. } else {
  421. pkgName = pkg.Name
  422. }
  423. }
  424. if _, ok := m[pkgName]; ok {
  425. log.Fatalf("imported package collision: %q imported twice", pkgName)
  426. }
  427. m[pkgName] = importPath
  428. }
  429. return m
  430. }
  431. type namedInterface struct {
  432. name *ast.Ident
  433. it *ast.InterfaceType
  434. }
  435. // Create an iterator over all interfaces in file.
  436. func iterInterfaces(file *ast.File) <-chan namedInterface {
  437. ch := make(chan namedInterface)
  438. go func() {
  439. for _, decl := range file.Decls {
  440. gd, ok := decl.(*ast.GenDecl)
  441. if !ok || gd.Tok != token.TYPE {
  442. continue
  443. }
  444. for _, spec := range gd.Specs {
  445. ts, ok := spec.(*ast.TypeSpec)
  446. if !ok {
  447. continue
  448. }
  449. it, ok := ts.Type.(*ast.InterfaceType)
  450. if !ok {
  451. continue
  452. }
  453. ch <- namedInterface{ts.Name, it}
  454. }
  455. }
  456. close(ch)
  457. }()
  458. return ch
  459. }
  460. // isVariadic returns whether the function is variadic.
  461. func isVariadic(f *ast.FuncType) bool {
  462. nargs := len(f.Params.List)
  463. if nargs == 0 {
  464. return false
  465. }
  466. _, ok := f.Params.List[nargs-1].Type.(*ast.Ellipsis)
  467. return ok
  468. }