Commit 52f099c4 authored by Matthew Dempsky's avatar Matthew Dempsky

cmd/compile: simplify lexinit and lexfini

Split the syms array into separate basicTypes and builtinFuncs arrays.

Also, in lexfini, instead of duplicating the code from lexinit to
declare the builtin identifiers in the user package, just import them
from builtinpkg like how importdot works.

Change-Id: Ic3b3b454627a46f7bd5f290d0e31443e659d431f
Reviewed-on: https://go-review.googlesource.com/19936
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 188e3d25
...@@ -2067,77 +2067,75 @@ func (l *lexer) hexchar(n int) uint32 { ...@@ -2067,77 +2067,75 @@ func (l *lexer) hexchar(n int) uint32 {
return x return x
} }
var syms = []struct { var basicTypes = [...]struct {
name string name string
etype EType etype EType
op Op
}{ }{
// basic types {"int8", TINT8},
{"int8", TINT8, OXXX}, {"int16", TINT16},
{"int16", TINT16, OXXX}, {"int32", TINT32},
{"int32", TINT32, OXXX}, {"int64", TINT64},
{"int64", TINT64, OXXX}, {"uint8", TUINT8},
{"uint8", TUINT8, OXXX}, {"uint16", TUINT16},
{"uint16", TUINT16, OXXX}, {"uint32", TUINT32},
{"uint32", TUINT32, OXXX}, {"uint64", TUINT64},
{"uint64", TUINT64, OXXX}, {"float32", TFLOAT32},
{"float32", TFLOAT32, OXXX}, {"float64", TFLOAT64},
{"float64", TFLOAT64, OXXX}, {"complex64", TCOMPLEX64},
{"complex64", TCOMPLEX64, OXXX}, {"complex128", TCOMPLEX128},
{"complex128", TCOMPLEX128, OXXX}, {"bool", TBOOL},
{"bool", TBOOL, OXXX}, {"string", TSTRING},
{"string", TSTRING, OXXX}, {"any", TANY},
{"any", TANY, OXXX}, }
// builtin funcs var builtinFuncs = [...]struct {
{"append", Txxx, OAPPEND}, name string
{"cap", Txxx, OCAP}, op Op
{"close", Txxx, OCLOSE}, }{
{"complex", Txxx, OCOMPLEX}, {"append", OAPPEND},
{"copy", Txxx, OCOPY}, {"cap", OCAP},
{"delete", Txxx, ODELETE}, {"close", OCLOSE},
{"imag", Txxx, OIMAG}, {"complex", OCOMPLEX},
{"len", Txxx, OLEN}, {"copy", OCOPY},
{"make", Txxx, OMAKE}, {"delete", ODELETE},
{"new", Txxx, ONEW}, {"imag", OIMAG},
{"panic", Txxx, OPANIC}, {"len", OLEN},
{"print", Txxx, OPRINT}, {"make", OMAKE},
{"println", Txxx, OPRINTN}, {"new", ONEW},
{"real", Txxx, OREAL}, {"panic", OPANIC},
{"recover", Txxx, ORECOVER}, {"print", OPRINT},
{"println", OPRINTN},
{"real", OREAL},
{"recover", ORECOVER},
} }
// 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 basicTypes {
if etype := s.etype; etype != Txxx { etype := s.etype
if int(etype) >= len(Types) { if int(etype) >= len(Types) {
Fatalf("lexinit: %s bad etype", s.name) Fatalf("lexinit: %s bad etype", s.name)
}
s2 := Pkglookup(s.name, builtinpkg)
t := Types[etype]
if t == nil {
t = typ(etype)
t.Sym = s2
if etype != TANY && etype != TSTRING {
dowidth(t)
} }
s2 := Pkglookup(s.name, builtinpkg) Types[etype] = t
t := Types[etype]
if t == nil {
t = typ(etype)
t.Sym = s2
if etype != TANY && etype != TSTRING {
dowidth(t)
}
Types[etype] = t
}
s2.Def = typenod(t)
s2.Def.Name = new(Name)
continue
} }
s2.Def = typenod(t)
s2.Def.Name = new(Name)
}
for _, s := range builtinFuncs {
// TODO(marvin): Fix Node.EType type union. // TODO(marvin): Fix Node.EType type union.
if etype := s.op; etype != OXXX { s2 := Pkglookup(s.name, builtinpkg)
s2 := Pkglookup(s.name, builtinpkg) s2.Def = Nod(ONAME, nil, nil)
s2.Def = Nod(ONAME, nil, nil) s2.Def.Sym = s2
s2.Def.Sym = s2 s2.Def.Etype = EType(s.op)
s2.Def.Etype = EType(etype)
}
} }
// logically, the type of a string literal. // logically, the type of a string literal.
...@@ -2183,6 +2181,11 @@ func lexinit() { ...@@ -2183,6 +2181,11 @@ func lexinit() {
s.Def = nodlit(v) s.Def = nodlit(v)
s.Def.Sym = s s.Def.Sym = s
s.Def.Name = new(Name) s.Def.Name = new(Name)
s = Pkglookup("iota", builtinpkg)
s.Def = Nod(OIOTA, nil, nil)
s.Def.Sym = s
s.Def.Name = new(Name)
} }
func lexinit1() { func lexinit1() {
...@@ -2230,86 +2233,28 @@ func lexinit1() { ...@@ -2230,86 +2233,28 @@ func lexinit1() {
runetype.Sym = s runetype.Sym = s
s.Def = typenod(runetype) s.Def = typenod(runetype)
s.Def.Name = new(Name) s.Def.Name = new(Name)
}
func lexfini() {
for i := range syms {
s := Lookup(syms[i].name)
etype := syms[i].etype
if etype != Txxx && (etype != TANY || Debug['A'] != 0) && s.Def == nil {
s.Def = typenod(Types[etype])
s.Def.Name = new(Name)
s.Origpkg = builtinpkg
}
// TODO(marvin): Fix Node.EType type union.
etype = EType(syms[i].op)
if etype != EType(OXXX) && s.Def == nil {
s.Def = Nod(ONAME, nil, nil)
s.Def.Sym = s
s.Def.Etype = etype
s.Origpkg = builtinpkg
}
}
// backend-specific builtin types (e.g. int). // backend-specific builtin types (e.g. int).
for i := range Thearch.Typedefs { for i := range Thearch.Typedefs {
s := Lookup(Thearch.Typedefs[i].Name) s := Pkglookup(Thearch.Typedefs[i].Name, builtinpkg)
if s.Def == nil { s.Def = typenod(Types[Thearch.Typedefs[i].Etype])
s.Def = typenod(Types[Thearch.Typedefs[i].Etype])
s.Def.Name = new(Name)
s.Origpkg = builtinpkg
}
}
// there's only so much table-driven we can handle.
// these are special cases.
if s := Lookup("byte"); s.Def == nil {
s.Def = typenod(bytetype)
s.Def.Name = new(Name) s.Def.Name = new(Name)
s.Origpkg = builtinpkg s.Origpkg = builtinpkg
} }
}
if s := Lookup("error"); s.Def == nil { func lexfini() {
s.Def = typenod(errortype) for _, s := range builtinpkg.Syms {
s.Def.Name = new(Name) if s.Def == nil {
s.Origpkg = builtinpkg continue
} }
s1 := Lookup(s.Name)
if s := Lookup("rune"); s.Def == nil { if s1.Def != nil {
s.Def = typenod(runetype) continue
s.Def.Name = new(Name) }
s.Origpkg = builtinpkg
}
if s := Lookup("nil"); s.Def == nil {
var v Val
v.U = new(NilVal)
s.Def = nodlit(v)
s.Def.Sym = s
s.Def.Name = new(Name)
s.Origpkg = builtinpkg
}
if s := Lookup("iota"); s.Def == nil {
s.Def = Nod(OIOTA, nil, nil)
s.Def.Sym = s
s.Origpkg = builtinpkg
}
if s := Lookup("true"); s.Def == nil {
s.Def = Nodbool(true)
s.Def.Sym = s
s.Def.Name = new(Name)
s.Origpkg = builtinpkg
}
if s := Lookup("false"); s.Def == nil { s1.Def = s.Def
s.Def = Nodbool(false) s1.Block = s.Block
s.Def.Sym = s
s.Def.Name = new(Name)
s.Origpkg = builtinpkg
} }
nodfp = Nod(ONAME, nil, nil) nodfp = Nod(ONAME, nil, nil)
......
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