Commit 071e43a9 authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: stop representing keywords as Syms

Instead add a dedicated keywords map for use in lexer.ident and drop
Sym's Lexical field.

Change-Id: Ia668e65499035ff7167fabbbd0cd027102b21231
Reviewed-on: https://go-review.googlesource.com/19935Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent af558acc
...@@ -122,10 +122,9 @@ type Pkg struct { ...@@ -122,10 +122,9 @@ type Pkg struct {
} }
type Sym struct { type Sym struct {
Lexical uint16
Flags uint8 Flags uint8
Link *Sym
Uniqgen uint32 Uniqgen uint32
Link *Sym
Importdef *Pkg // where imported definition was found Importdef *Pkg // where imported definition was found
Linkname string // link name Linkname string // link name
......
...@@ -1265,16 +1265,64 @@ func (l *lexer) ident(c rune) { ...@@ -1265,16 +1265,64 @@ func (l *lexer) ident(c rune) {
cp = nil cp = nil
l.ungetr(c) l.ungetr(c)
s := LookupBytes(lexbuf.Bytes()) name := lexbuf.Bytes()
if len(name) >= 2 {
if tok, ok := keywords[string(name)]; ok {
if Debug['x'] != 0 {
fmt.Printf("lex: %s\n", lexname(tok))
}
switch tok {
case LBREAK, LCONTINUE, LFALL, LRETURN:
l.nlsemi = true
}
l.tok = tok
return
}
}
s := LookupBytes(name)
if Debug['x'] != 0 { if Debug['x'] != 0 {
fmt.Printf("lex: %s %s\n", s, lexname(rune(s.Lexical))) fmt.Printf("lex: ident %s\n", s)
} }
l.sym_ = s l.sym_ = s
switch s.Lexical { l.nlsemi = true
case LNAME, LRETURN, LBREAK, LCONTINUE, LFALL: l.tok = LNAME
l.nlsemi = true }
}
l.tok = int32(s.Lexical) var keywords = map[string]int32{
"break": LBREAK,
"case": LCASE,
"chan": LCHAN,
"const": LCONST,
"continue": LCONTINUE,
"default": LDEFAULT,
"defer": LDEFER,
"else": LELSE,
"fallthrough": LFALL,
"for": LFOR,
"func": LFUNC,
"go": LGO,
"goto": LGOTO,
"if": LIF,
"import": LIMPORT,
"interface": LINTERFACE,
"map": LMAP,
"package": LPACKAGE,
"range": LRANGE,
"return": LRETURN,
"select": LSELECT,
"struct": LSTRUCT,
"switch": LSWITCH,
"type": LTYPE,
"var": LVAR,
// 💩
"notwithstanding": LIGNORE,
"thetruthofthematter": LIGNORE,
"despiteallobjections": LIGNORE,
"whereas": LIGNORE,
"insofaras": LIGNORE,
} }
func (l *lexer) number(c rune) { func (l *lexer) number(c rune) {
...@@ -1992,81 +2040,48 @@ func (l *lexer) hexchar(n int) uint32 { ...@@ -1992,81 +2040,48 @@ func (l *lexer) hexchar(n int) uint32 {
} }
var syms = []struct { var syms = []struct {
name string name string
lexical int etype EType
etype EType op Op
op Op
}{ }{
// basic types // basic types
{"int8", LNAME, TINT8, OXXX}, {"int8", TINT8, OXXX},
{"int16", LNAME, TINT16, OXXX}, {"int16", TINT16, OXXX},
{"int32", LNAME, TINT32, OXXX}, {"int32", TINT32, OXXX},
{"int64", LNAME, TINT64, OXXX}, {"int64", TINT64, OXXX},
{"uint8", LNAME, TUINT8, OXXX}, {"uint8", TUINT8, OXXX},
{"uint16", LNAME, TUINT16, OXXX}, {"uint16", TUINT16, OXXX},
{"uint32", LNAME, TUINT32, OXXX}, {"uint32", TUINT32, OXXX},
{"uint64", LNAME, TUINT64, OXXX}, {"uint64", TUINT64, OXXX},
{"float32", LNAME, TFLOAT32, OXXX}, {"float32", TFLOAT32, OXXX},
{"float64", LNAME, TFLOAT64, OXXX}, {"float64", TFLOAT64, OXXX},
{"complex64", LNAME, TCOMPLEX64, OXXX}, {"complex64", TCOMPLEX64, OXXX},
{"complex128", LNAME, TCOMPLEX128, OXXX}, {"complex128", TCOMPLEX128, OXXX},
{"bool", LNAME, TBOOL, OXXX}, {"bool", TBOOL, OXXX},
{"string", LNAME, TSTRING, OXXX}, {"string", TSTRING, OXXX},
{"any", LNAME, TANY, OXXX}, {"any", TANY, OXXX},
{"break", LBREAK, Txxx, OXXX},
{"case", LCASE, Txxx, OXXX}, // builtin funcs
{"chan", LCHAN, Txxx, OXXX}, {"append", Txxx, OAPPEND},
{"const", LCONST, Txxx, OXXX}, {"cap", Txxx, OCAP},
{"continue", LCONTINUE, Txxx, OXXX}, {"close", Txxx, OCLOSE},
{"default", LDEFAULT, Txxx, OXXX}, {"complex", Txxx, OCOMPLEX},
{"else", LELSE, Txxx, OXXX}, {"copy", Txxx, OCOPY},
{"defer", LDEFER, Txxx, OXXX}, {"delete", Txxx, ODELETE},
{"fallthrough", LFALL, Txxx, OXXX}, {"imag", Txxx, OIMAG},
{"for", LFOR, Txxx, OXXX}, {"len", Txxx, OLEN},
{"func", LFUNC, Txxx, OXXX}, {"make", Txxx, OMAKE},
{"go", LGO, Txxx, OXXX}, {"new", Txxx, ONEW},
{"goto", LGOTO, Txxx, OXXX}, {"panic", Txxx, OPANIC},
{"if", LIF, Txxx, OXXX}, {"print", Txxx, OPRINT},
{"import", LIMPORT, Txxx, OXXX}, {"println", Txxx, OPRINTN},
{"interface", LINTERFACE, Txxx, OXXX}, {"real", Txxx, OREAL},
{"map", LMAP, Txxx, OXXX}, {"recover", Txxx, ORECOVER},
{"package", LPACKAGE, Txxx, OXXX},
{"range", LRANGE, Txxx, OXXX},
{"return", LRETURN, Txxx, OXXX},
{"select", LSELECT, Txxx, OXXX},
{"struct", LSTRUCT, Txxx, OXXX},
{"switch", LSWITCH, Txxx, OXXX},
{"type", LTYPE, Txxx, OXXX},
{"var", LVAR, Txxx, OXXX},
{"append", LNAME, Txxx, OAPPEND},
{"cap", LNAME, Txxx, OCAP},
{"close", LNAME, Txxx, OCLOSE},
{"complex", LNAME, Txxx, OCOMPLEX},
{"copy", LNAME, Txxx, OCOPY},
{"delete", LNAME, Txxx, ODELETE},
{"imag", LNAME, Txxx, OIMAG},
{"len", LNAME, Txxx, OLEN},
{"make", LNAME, Txxx, OMAKE},
{"new", LNAME, Txxx, ONEW},
{"panic", LNAME, Txxx, OPANIC},
{"print", LNAME, Txxx, OPRINT},
{"println", LNAME, Txxx, OPRINTN},
{"real", LNAME, Txxx, OREAL},
{"recover", LNAME, Txxx, ORECOVER},
{"notwithstanding", LIGNORE, Txxx, OXXX},
{"thetruthofthematter", LIGNORE, Txxx, OXXX},
{"despiteallobjections", LIGNORE, Txxx, OXXX},
{"whereas", LIGNORE, Txxx, OXXX},
{"insofaras", LIGNORE, Txxx, OXXX},
} }
// lexinit initializes known symbols and the basic types. // lexinit initializes known symbols and the basic types.
func lexinit() { func lexinit() {
for _, s := range syms { for _, s := range syms {
lex := s.lexical
s1 := Lookup(s.name)
s1.Lexical = uint16(lex)
if etype := s.etype; etype != Txxx { if etype := s.etype; etype != Txxx {
if int(etype) >= len(Types) { if int(etype) >= len(Types) {
Fatalf("lexinit: %s bad etype", s.name) Fatalf("lexinit: %s bad etype", s.name)
...@@ -2083,7 +2098,6 @@ func lexinit() { ...@@ -2083,7 +2098,6 @@ func lexinit() {
Types[etype] = t Types[etype] = t
} }
s2.Lexical = LNAME
s2.Def = typenod(t) s2.Def = typenod(t)
s2.Def.Name = new(Name) s2.Def.Name = new(Name)
continue continue
...@@ -2092,7 +2106,6 @@ func lexinit() { ...@@ -2092,7 +2106,6 @@ func lexinit() {
// TODO(marvin): Fix Node.EType type union. // TODO(marvin): Fix Node.EType type union.
if etype := s.op; etype != OXXX { if etype := s.op; etype != OXXX {
s2 := Pkglookup(s.name, builtinpkg) s2 := Pkglookup(s.name, builtinpkg)
s2.Lexical = LNAME
s2.Def = Nod(ONAME, nil, nil) s2.Def = Nod(ONAME, nil, nil)
s2.Def.Sym = s2 s2.Def.Sym = s2
s2.Def.Etype = EType(etype) s2.Def.Etype = EType(etype)
...@@ -2171,46 +2184,29 @@ func lexinit1() { ...@@ -2171,46 +2184,29 @@ func lexinit1() {
t.Type.Type = f t.Type.Type = f
// error type // error type
s := Lookup("error") s := Pkglookup("error", builtinpkg)
s.Lexical = LNAME
s1 := Pkglookup("error", builtinpkg)
errortype = t errortype = t
errortype.Sym = s1 errortype.Sym = s
s1.Lexical = LNAME s.Def = typenod(errortype)
s1.Def = typenod(errortype)
// byte alias // byte alias
s = Lookup("byte") s = Pkglookup("byte", builtinpkg)
s.Lexical = LNAME
s1 = Pkglookup("byte", builtinpkg)
bytetype = typ(TUINT8) bytetype = typ(TUINT8)
bytetype.Sym = s1 bytetype.Sym = s
s1.Lexical = LNAME s.Def = typenod(bytetype)
s1.Def = typenod(bytetype) s.Def.Name = new(Name)
s1.Def.Name = new(Name)
// rune alias // rune alias
s = Lookup("rune") s = Pkglookup("rune", builtinpkg)
s.Lexical = LNAME
s1 = Pkglookup("rune", builtinpkg)
runetype = typ(TINT32) runetype = typ(TINT32)
runetype.Sym = s1 runetype.Sym = s
s1.Lexical = LNAME s.Def = typenod(runetype)
s1.Def = typenod(runetype) s.Def.Name = new(Name)
s1.Def.Name = new(Name)
} }
func lexfini() { func lexfini() {
for i := range syms { for i := range syms {
lex := syms[i].lexical
if lex != LNAME {
continue
}
s := Lookup(syms[i].name) s := Lookup(syms[i].name)
s.Lexical = uint16(lex)
etype := syms[i].etype etype := syms[i].etype
if etype != Txxx && (etype != TANY || Debug['A'] != 0) && s.Def == nil { if etype != Txxx && (etype != TANY || Debug['A'] != 0) && s.Def == nil {
......
...@@ -238,9 +238,8 @@ func (pkg *Pkg) Lookup(name string) *Sym { ...@@ -238,9 +238,8 @@ func (pkg *Pkg) Lookup(name string) *Sym {
} }
s := &Sym{ s := &Sym{
Name: name, Name: name,
Pkg: pkg, Pkg: pkg,
Lexical: LNAME,
} }
if name == "init" { if name == "init" {
initSyms = append(initSyms, s) initSyms = append(initSyms, s)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment