about summary refs log tree commit diff
path: root/vendor/modernc.org/gc/v3/gc.go
blob: 1723bd5807af0cac4d4d029599cbd95a14e25975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
// Copyright 2022 The Gc Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:generate stringer -output stringer.go -linecomment -type=Kind,ScopeKind,ChanDir,TypeCheck

package gc // modernc.org/gc/v3

import (
	"fmt"
	"go/build"
	"go/build/constraint"
	"go/token"
	"io"
	"io/fs"
	"os"
	"path/filepath"
	"runtime"
	"sort"
	"strconv"
	"strings"
	"sync"
	"unicode"

	"github.com/hashicorp/golang-lru/v2"
)

var (
	trcErrors bool
)

type FileFilter func(cfg *Config, importPath string, matchedFSPaths []string, withTestFiles bool) (pkgFiles []string, err error)

type TypeCheck int

const (
	TypeCheckNone TypeCheck = iota
	TypeCheckAll
)

type cacheKey struct {
	buildTagsKey string
	cfg          *Config
	fsPath       string
	goarch       string
	goos         string
	gopathKey    string
	goroot       string
	importPath   string
	typeCheck    TypeCheck

	withTestFiles bool
}

type cacheItem struct {
	pkg *Package
	ch  chan struct{}
}

func newCacheItem() *cacheItem { return &cacheItem{ch: make(chan struct{})} }

func (c *cacheItem) set(pkg *Package) {
	c.pkg = pkg
	close(c.ch)
}

func (c *cacheItem) wait() *Package {
	<-c.ch
	return c.pkg
}

type Cache struct {
	sync.Mutex
	lru *lru.TwoQueueCache[cacheKey, *cacheItem]
}

func NewCache(size int) (*Cache, error) {
	c, err := lru.New2Q[cacheKey, *cacheItem](size)
	if err != nil {
		return nil, err
	}

	return &Cache{lru: c}, nil
}

func MustNewCache(size int) *Cache {
	c, err := NewCache(size)
	if err != nil {
		panic(todo("", err))
	}

	return c
}

type ConfigOption func(*Config) error

// Config configures NewPackage
//
// Config instances can be shared, they are not mutated once created and
// configured.
type Config struct {
	abi           *ABI
	buildTagMap   map[string]bool
	buildTags     []string
	buildTagsKey  string // Zero byte separated
	builtin       *Package
	cache         *Cache
	cmp           *Package // Go 1.21
	env           map[string]string
	fs            fs.FS
	goarch        string
	gocompiler    string // "gc", "gccgo"
	goos          string
	gopath        string
	gopathKey     string // Zero byte separated
	goroot        string
	goversion     string
	lookup        func(rel, importPath, version string) (fsPath string, err error)
	parallel      *parallel
	searchGoPaths []string
	searchGoroot  []string

	int  Type // Set by NewConfig
	uint Type // Set by NewConfig

	arch32bit  bool
	configured bool
}

// NewConfig returns a newly created config or an error, if any.
func NewConfig(opts ...ConfigOption) (r *Config, err error) {
	r = &Config{
		buildTagMap: map[string]bool{},
		env:         map[string]string{},
		parallel:    newParallel(),
	}

	defer func() {
		if r != nil {
			r.configured = true
		}
	}()

	r.lookup = r.DefaultLookup
	ctx := build.Default
	r.goos = r.getenv("GOOS", ctx.GOOS)
	r.goarch = r.getenv("GOARCH", ctx.GOARCH)
	r.goroot = r.getenv("GOROOT", ctx.GOROOT)
	r.gopath = r.getenv("GOPATH", ctx.GOPATH)
	r.buildTags = append(r.buildTags, r.goos, r.goarch)
	r.gocompiler = runtime.Compiler
	for _, opt := range opts {
		if err := opt(r); err != nil {
			return nil, err
		}
	}
	if r.abi, err = NewABI(r.goos, r.goarch); err != nil {
		return nil, err
	}
	switch r.goarch {
	case "386", "arm":
		r.arch32bit = true
	}

	//  During a particular build, the following build tags are satisfied:
	//
	//  the target operating system, as spelled by runtime.GOOS, set with the GOOS environment variable.
	//  the target architecture, as spelled by runtime.GOARCH, set with the GOARCH environment variable.
	//  "unix", if GOOS is a Unix or Unix-like system.
	//  the compiler being used, either "gc" or "gccgo"
	//  "cgo", if the cgo command is supported (see CGO_ENABLED in 'go help environment').
	//  a term for each Go major release, through the current version: "go1.1" from Go version 1.1 onward, "go1.12" from Go 1.12, and so on.
	//  any additional tags given by the -tags flag (see 'go help build').
	//  There are no separate build tags for beta or minor releases.
	if r.goversion == "" {
		r.goversion = runtime.Version()
	}
	if !strings.HasPrefix(r.goversion, "go") || !strings.Contains(r.goversion, ".") {
		return nil, fmt.Errorf("cannot parse Go version: %s", r.goversion)
	}

	ver := strings.SplitN(r.goversion[len("go"):], ".", 2)
	verMajor, err := strconv.Atoi(ver[0])
	if err != nil {
		return nil, fmt.Errorf("cannot parse Go version %s: %v", r.goversion, err)
	}

	if verMajor != 1 {
		return nil, fmt.Errorf("unsupported Go version: %s", r.goversion)
	}

	switch x, x2 := strings.IndexByte(ver[1], '.'), strings.Index(ver[1], "rc"); {
	case x >= 0:
		ver[1] = ver[1][:x]
	case x2 >= 0:
		ver[1] = ver[1][:x2]
	}
	verMinor, err := strconv.Atoi(ver[1])
	if err != nil {
		return nil, fmt.Errorf("cannot parse Go version %s: %v", r.goversion, err)
	}

	for i := 1; i <= verMinor; i++ {
		r.buildTags = append(r.buildTags, fmt.Sprintf("go%d.%d", verMajor, i))
	}
	r.buildTags = append(r.buildTags, r.gocompiler)
	r.buildTags = append(r.buildTags, extraTags(verMajor, verMinor, r.goos, r.goarch)...)
	if r.getenv("CGO_ENABLED", "1") == "1" {
		r.buildTags = append(r.buildTags, "cgo")
	}
	for i, v := range r.buildTags {
		tag := strings.TrimSpace(v)
		r.buildTags[i] = tag
		r.buildTagMap[tag] = true
	}
	sort.Strings(r.buildTags)
	r.buildTagsKey = strings.Join(r.buildTags, "\x00")
	r.searchGoroot = []string{filepath.Join(r.goroot, "src")}
	r.searchGoPaths = filepath.SplitList(r.gopath)
	r.gopathKey = strings.Join(r.searchGoPaths, "\x00")
	for i, v := range r.searchGoPaths {
		r.searchGoPaths[i] = filepath.Join(v, "src")
	}

	switch r.cmp, err = r.NewPackage("", "cmp", "", nil, false, TypeCheckNone); {
	case err != nil:
		r.cmp = nil
	default:
		//TODO r.cmp.Scope.kind = UniverseScope
	}
	if r.builtin, err = r.NewPackage("", "builtin", "", nil, false, TypeCheckNone); err != nil {
		return nil, err
	}

	r.builtin.Scope.kind = UniverseScope
	if err := r.builtin.check(newCtx(r)); err != nil {
		return nil, err
	}

	return r, nil
}

func (c *Config) universe() *Scope {
	if c.builtin != nil {
		return c.builtin.Scope
	}

	return nil
}

func (c *Config) stat(name string) (fs.FileInfo, error) {
	if c.fs == nil {
		return os.Stat(name)
	}

	name = filepath.ToSlash(name)
	if x, ok := c.fs.(fs.StatFS); ok {
		return x.Stat(name)
	}

	f, err := c.fs.Open(name)
	if err != nil {
		return nil, err
	}

	defer f.Close()

	return f.Stat()
}

func (c *Config) open(name string) (fs.File, error) {
	if c.fs == nil {
		return os.Open(name)
	}

	name = filepath.ToSlash(name)
	return c.fs.Open(name)
}

func (c *Config) glob(pattern string) (matches []string, err error) {
	if c.fs == nil {
		return filepath.Glob(pattern)
	}

	pattern = filepath.ToSlash(pattern)
	return fs.Glob(c.fs, pattern)
}

func (c *Config) checkConstraints(pos token.Position, sep string) (r bool) {
	if !strings.Contains(sep, "//go:build") && !strings.Contains(sep, "+build") {
		return true
	}

	// defer func() { trc("", r) }()

	lines := strings.Split(sep, "\n")
	var build, plusBuild []string
	for i, line := range lines {
		if constraint.IsGoBuild(line) && i < len(lines)-1 && lines[i+1] == "" {
			build = append(build, line)
		}
		if constraint.IsPlusBuild(line) {
			plusBuild = append(plusBuild, line)
		}
	}
	switch len(build) {
	case 0:
		// ok
	case 1:
		expr, err := constraint.Parse(build[0])
		if err != nil {
			return true
		}

		return expr.Eval(func(tag string) (r bool) {
			// defer func() { trc("%q: %v", tag, r) }()
			switch tag {
			case "unix":
				return unixOS[c.goos]
			default:
				return c.buildTagMap[tag]
			}
		})
	default:
		panic(todo("%v: %q", pos, build))
	}

	for _, line := range plusBuild {
		expr, err := constraint.Parse(line)
		if err != nil {
			return true
		}

		if !expr.Eval(func(tag string) (r bool) {
			// defer func() { trc("%q: %v", tag, r) }()
			switch tag {
			case "unix":
				return unixOS[c.goos]
			default:
				return c.buildTagMap[tag]
			}
		}) {
			return false
		}
	}
	return true
}

// Default lookup translates import paths, possibly relative to rel, to file system paths.
func (c *Config) DefaultLookup(rel, importPath, version string) (fsPath string, err error) {
	if importPath == "" {
		return "", fmt.Errorf("import path cannot be emtpy")
	}

	// Implementation restriction: A compiler may restrict ImportPaths to non-empty
	// strings using only characters belonging to Unicode's L, M, N, P, and S
	// general categories (the Graphic characters without spaces) and may also
	// exclude the characters !"#$%&'()*,:;<=>?[\]^`{|} and the Unicode replacement
	// character U+FFFD.
	if strings.ContainsAny(importPath, "!\"#$%&'()*,:;<=>?[\\]^`{|}\ufffd") {
		return "", fmt.Errorf("invalid import path: %s", importPath)
	}

	for _, r := range importPath {
		if !unicode.Is(unicode.L, r) &&
			!unicode.Is(unicode.M, r) &&
			!unicode.Is(unicode.N, r) &&
			!unicode.Is(unicode.P, r) &&
			!unicode.Is(unicode.S, r) {
			return "", fmt.Errorf("invalid import path: %s", importPath)
		}
	}
	var search []string
	ip0 := importPath
	switch slash := strings.IndexByte(importPath, '/'); {
	case strings.HasPrefix(importPath, "./"):
		if rel != "" {
			panic(todo(""))
		}

		return "", fmt.Errorf("invalid import path: %s", importPath)
	case strings.HasPrefix(importPath, "/"):
		return importPath, nil
	case slash > 0:
		ip0 = importPath[:slash]
	default:
		ip0 = importPath
	}
	if ip0 != "" {
		switch {
		case strings.Contains(ip0, "."):
			search = c.searchGoPaths
		default:
			search = c.searchGoroot
		}
	}
	for _, v := range search {
		fsPath = filepath.Join(v, importPath)
		dir, err := c.open(fsPath)
		if err != nil {
			continue
		}

		fi, err := dir.Stat()
		dir.Close()
		if err != nil {
			continue
		}

		if fi.IsDir() {
			return fsPath, nil
		}
	}

	return "", fmt.Errorf("cannot find package %s, searched %v", importPath, search)
}

func (c *Config) getenv(nm, deflt string) (r string) {
	if r = c.env[nm]; r != "" {
		return r
	}

	if r = os.Getenv(nm); r != "" {
		return r
	}

	return deflt
}

func DefaultFileFilter(cfg *Config, importPath string, matchedFSPaths []string, withTestFiles bool) (pkgFiles []string, err error) {
	w := 0
	for _, v := range matchedFSPaths {
		base := filepath.Base(v)
		base = base[:len(base)-len(filepath.Ext(base))]
		const testSuffix = "_test"
		if strings.HasSuffix(base, testSuffix) {
			if !withTestFiles {
				continue
			}

			base = base[:len(base)-len(testSuffix)]
		}
		if x := strings.LastIndexByte(base, '_'); x > 0 {
			last := base[x+1:]
			base = base[:x]
			var prevLast string
			if x := strings.LastIndexByte(base, '_'); x > 0 {
				prevLast = base[x+1:]
			}
			if last != "" && prevLast != "" {
				//  *_GOOS_GOARCH
				if knownOS[prevLast] && prevLast != cfg.goos {
					continue
				}

				if knownArch[last] && last != cfg.goarch {
					continue
				}
			}

			if last != "" {
				// *_GOOS or *_GOARCH
				if knownOS[last] && last != cfg.goos {
					continue
				}

				if knownArch[last] && last != cfg.goarch {
					continue
				}
			}
		}

		matchedFSPaths[w] = v
		w++
	}
	return matchedFSPaths[:w], nil
}

// ConfigBuildTags configures build tags.
func ConfigBuildTags(tags []string) ConfigOption {
	return func(cfg *Config) error {
		if cfg.configured {
			return fmt.Errorf("ConfigBuildTags: Config instance already configured")
		}

		cfg.buildTags = append(cfg.buildTags, tags...)
		return nil
	}
}

// ConfigEnviron configures environment variables.
func ConfigEnviron(env []string) ConfigOption {
	return func(cfg *Config) error {
		if cfg.configured {
			return fmt.Errorf("ConfigEnviron: Config instance already configured")
		}

		for _, v := range env {
			switch x := strings.IndexByte(v, '='); {
			case x < 0:
				cfg.env[v] = ""
			default:
				cfg.env[v[:x]] = v[x+1:]
			}
		}
		return nil
	}
}

// ConfigFS configures a file system used for opening Go source files. If not
// explicitly configured, a default os.DirFS("/") is used on Unix-like
// operating systems. On Windows it will be rooted on the volume where
// runtime.GOROOT() is.
func ConfigFS(fs fs.FS) ConfigOption {
	return func(cfg *Config) error {
		if cfg.configured {
			return fmt.Errorf("ConfigFS: Config instance already configured")
		}

		cfg.fs = fs
		return nil
	}
}

// ConfigLookup configures a lookup function.
func ConfigLookup(f func(dir, importPath, version string) (fsPath string, err error)) ConfigOption {
	return func(cfg *Config) error {
		if cfg.configured {
			return fmt.Errorf("ConfigLookup: Config instance already configured")
		}

		cfg.lookup = f
		return nil
	}
}

// ConfigCache configures a cache.
func ConfigCache(c *Cache) ConfigOption {
	return func(cfg *Config) error {
		if cfg.configured {
			return fmt.Errorf("ConfigCache: Config instance already configured")
		}

		cfg.cache = c
		return nil
	}
}

type importGuard struct {
	m     map[string]struct{}
	stack []string
}

func newImportGuard() *importGuard { return &importGuard{m: map[string]struct{}{}} }

// Package represents a Go package. The instance must not be mutated.
type Package struct {
	AST            map[string]*AST // AST maps fsPaths of individual files to their respective ASTs
	FSPath         string
	GoFiles        []fs.FileInfo
	ImportPath     string
	InvalidGoFiles map[string]error // errors for particular files, if any
	Name           Token
	Scope          *Scope // Package scope.
	Version        string
	cfg            *Config
	guard          *importGuard
	mu             sync.Mutex
	typeCheck      TypeCheck

	isUnsafe bool // ImportPath == "usnafe"
	// isChecked bool
}

// NewPackage returns a Package, possibly cached, for importPath@version or an
// error, if any. The fileFilter argument can be nil, in such case
// DefaultFileFilter is used, which ignores Files with suffix _test.go unless
// withTestFiles is true.
//
// NewPackage is safe for concurrent use by multiple goroutines.
func (c *Config) NewPackage(dir, importPath, version string, fileFilter FileFilter, withTestFiles bool, typeCheck TypeCheck) (pkg *Package, err error) {
	return c.newPackage(dir, importPath, version, fileFilter, withTestFiles, typeCheck, newImportGuard())
}

func (c *Config) newPackage(dir, importPath, version string, fileFilter FileFilter, withTestFiles bool, typeCheck TypeCheck, guard *importGuard) (pkg *Package, err error) {
	if _, ok := guard.m[importPath]; ok {
		return nil, fmt.Errorf("import cycle %v", guard.stack)
	}

	guard.stack = append(guard.stack, importPath)
	fsPath, err := c.lookup(dir, importPath, version)
	if err != nil {
		return nil, fmt.Errorf("lookup %s: %v", importPath, err)
	}

	pat := filepath.Join(fsPath, "*.go")
	matches, err := c.glob(pat)
	if err != nil {
		return nil, fmt.Errorf("glob %s: %v", pat, err)
	}

	if len(matches) == 0 {
		return nil, fmt.Errorf("no Go files in %s", fsPath)
	}

	if fileFilter == nil {
		fileFilter = DefaultFileFilter
	}
	if matches, err = fileFilter(c, importPath, matches, withTestFiles); err != nil {
		return nil, fmt.Errorf("matching Go files in %s: %v", fsPath, err)
	}

	var k cacheKey
	if c.cache != nil {
		k = cacheKey{
			buildTagsKey:  c.buildTagsKey,
			cfg:           c,
			fsPath:        fsPath,
			goarch:        c.goarch,
			goos:          c.goos,
			gopathKey:     c.gopathKey,
			goroot:        c.goroot,
			importPath:    importPath,
			typeCheck:     typeCheck,
			withTestFiles: withTestFiles,
		}

		c.cache.Lock() // ---------------------------------------- lock
		item, ok := c.cache.lru.Get(k)
		if ok {
			c.cache.Unlock() // ---------------------------- unlock
			if pkg = item.wait(); pkg != nil && pkg.matches(&k, matches) {
				return pkg, nil
			}
		}

		item = newCacheItem()
		c.cache.lru.Add(k, item)
		c.cache.Unlock() // ------------------------------------ unlock

		defer func() {
			if pkg != nil && err == nil {
				item.set(pkg)
			}
		}()
	}

	r := &Package{
		AST:        map[string]*AST{},
		FSPath:     fsPath,
		ImportPath: importPath,
		Scope:      newScope(c.universe(), PackageScope),
		Version:    version,
		cfg:        c,
		guard:      guard,
		isUnsafe:   importPath == "unsafe",
		typeCheck:  typeCheck,
	}

	defer func() { r.guard = nil }()

	sort.Strings(matches)

	defer func() {
		sort.Slice(r.GoFiles, func(i, j int) bool { return r.GoFiles[i].Name() < r.GoFiles[j].Name() })
		if err != nil || len(r.InvalidGoFiles) != 0 || typeCheck == TypeCheckNone {
			return
		}

		//TODO err = r.check(newCtx(c))
	}()

	c.parallel.throttle(func() {
		for _, path := range matches {
			if err = c.newPackageFile(r, path); err != nil {
				return
			}
		}
	})
	return r, err
}

func (c *Config) newPackageFile(pkg *Package, path string) (err error) {
	f, err := c.open(path)
	if err != nil {
		return fmt.Errorf("opening file %q: %v", path, err)
	}

	defer func() {
		f.Close()
		if err != nil {
			if pkg.InvalidGoFiles == nil {
				pkg.InvalidGoFiles = map[string]error{}
			}
			pkg.InvalidGoFiles[path] = err
		}
	}()

	var fi fs.FileInfo
	if fi, err = f.Stat(); err != nil {
		return fmt.Errorf("stat %s: %v", path, err)
	}

	if !fi.Mode().IsRegular() {
		return nil
	}

	var b []byte
	if b, err = io.ReadAll(f); err != nil {
		return fmt.Errorf("reading %s: %v", path, err)
	}

	p := newParser(pkg.Scope, path, b, false)
	if p.peek(0) == PACKAGE {
		tok := Token{p.s.source, p.s.toks[p.ix].ch, int32(p.ix)}
		if !c.checkConstraints(tok.Position(), tok.Sep()) {
			return nil
		}
	}

	pkg.GoFiles = append(pkg.GoFiles, fi)
	var ast *AST
	if ast, err = p.parse(); err != nil {
		return nil
	}

	pkg.AST[path] = ast
	return nil
}

func (p *Package) matches(k *cacheKey, matches []string) bool {
	matched := map[string]struct{}{}
	for _, match := range matches {
		matched[match] = struct{}{}
	}
	for _, cachedInfo := range p.GoFiles {
		name := cachedInfo.Name()
		path := filepath.Join(p.FSPath, name)
		if _, ok := matched[path]; !ok {
			return false
		}

		info, err := k.cfg.stat(path)
		if err != nil {
			return false
		}

		if info.IsDir() ||
			info.Size() != cachedInfo.Size() ||
			info.ModTime().After(cachedInfo.ModTime()) ||
			info.Mode() != cachedInfo.Mode() {
			return false
		}
	}
	return true
}

// ParseFile parses 'b', assuming it comes from 'path' and returns an AST or error, if any.
func ParseFile(path string, b []byte) (*AST, error) {
	return newParser(newScope(nil, PackageScope), path, b, false).parse()
}