1
2
3
4
5 package types
6
7 import (
8 "fmt"
9 "internal/goversion"
10 "log"
11 "regexp"
12 "strconv"
13
14 "cmd/compile/internal/base"
15 )
16
17
18 type lang struct {
19 major, minor int
20 }
21
22
23
24
25 var langWant lang
26
27
28
29
30
31 func AllowsGoVersion(pkg *Pkg, major, minor int) bool {
32 if pkg == nil {
33
34 pkg = LocalPkg
35 }
36 if pkg != LocalPkg {
37
38 return true
39 }
40 if langWant.major == 0 && langWant.minor == 0 {
41 return true
42 }
43 return langWant.major > major || (langWant.major == major && langWant.minor >= minor)
44 }
45
46
47
48 func ParseLangFlag() {
49 if base.Flag.Lang == "" {
50 return
51 }
52
53 var err error
54 langWant, err = parseLang(base.Flag.Lang)
55 if err != nil {
56 log.Fatalf("invalid value %q for -lang: %v", base.Flag.Lang, err)
57 }
58
59 if def := currentLang(); base.Flag.Lang != def {
60 defVers, err := parseLang(def)
61 if err != nil {
62 log.Fatalf("internal error parsing default lang %q: %v", def, err)
63 }
64 if langWant.major > defVers.major || (langWant.major == defVers.major && langWant.minor > defVers.minor) {
65 log.Fatalf("invalid value %q for -lang: max known version is %q", base.Flag.Lang, def)
66 }
67 }
68 }
69
70
71 func parseLang(s string) (lang, error) {
72 matches := goVersionRE.FindStringSubmatch(s)
73 if matches == nil {
74 return lang{}, fmt.Errorf(`should be something like "go1.12"`)
75 }
76 major, err := strconv.Atoi(matches[1])
77 if err != nil {
78 return lang{}, err
79 }
80 minor, err := strconv.Atoi(matches[2])
81 if err != nil {
82 return lang{}, err
83 }
84 return lang{major: major, minor: minor}, nil
85 }
86
87
88 func currentLang() string {
89 return fmt.Sprintf("go1.%d", goversion.Version)
90 }
91
92
93
94 var goVersionRE = regexp.MustCompile(`^go([1-9][0-9]*)\.(0|[1-9][0-9]*)$`)
95
View as plain text