From 66288e0e0b7af2f82427cfcfad606ea21880a837 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Sun, 7 Jan 2018 21:38:54 +0100 Subject: pkg/compiler: add builtin bool type aliases This adds builtin: type bool8 int8[0:1] type bool16 int16[0:1] type bool32 int32[0:1] type bool64 int64[0:1] type boolptr intptr[0:1] We used to use just int's for bools. But bool types provide several advantages: - make true/false probability equal - improve description expressiveness - reduce search space (we will take advantage of this later) --- pkg/ast/testdata/all.txt | 4 ++-- pkg/compiler/check.go | 2 +- pkg/compiler/compiler.go | 3 +++ pkg/compiler/testdata/errors.txt | 17 +++++++++-------- pkg/compiler/types.go | 23 ++++++++++++++++++++++- 5 files changed, 37 insertions(+), 12 deletions(-) (limited to 'pkg') diff --git a/pkg/ast/testdata/all.txt b/pkg/ast/testdata/all.txt index fedcc51a2..268b49a47 100644 --- a/pkg/ast/testdata/all.txt +++ b/pkg/ast/testdata/all.txt @@ -46,7 +46,7 @@ s2 { } -type bool8 int8 +type mybool8 int8 type net_port proc[1, 2, int16be] -type bool16 ### unexpected '\n', expecting int, identifier, string +type mybool16 ### unexpected '\n', expecting int, identifier, string type type4:4 int32 ### unexpected ':', expecting int, identifier, string diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go index ff87ead79..e9ec872f5 100644 --- a/pkg/compiler/check.go +++ b/pkg/compiler/check.go @@ -40,7 +40,7 @@ func (comp *compiler) checkNames() { comp.error(pos, "%v uses reserved name %v", typ, name) continue } - if builtinTypes[name] != nil { + if builtinTypes[name] != nil || builtinTypedefs[name] != nil { comp.error(pos, "%v name %v conflicts with builtin type", typ, name) continue } diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 0be7f2817..3901747a6 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -62,6 +62,9 @@ func Compile(desc *ast.Description, consts map[string]uint64, target *targets.Ta structNodes: make(map[*prog.StructDesc]*ast.Struct), structVarlen: make(map[string]bool), } + for name, typedef := range builtinTypedefs { + comp.typedefs[name] = typedef + } comp.assignSyscallNumbers(consts) comp.patchConsts(consts) comp.check() diff --git a/pkg/compiler/testdata/errors.txt b/pkg/compiler/testdata/errors.txt index 237519aa3..0a9363924 100644 --- a/pkg/compiler/testdata/errors.txt +++ b/pkg/compiler/testdata/errors.txt @@ -181,17 +181,17 @@ define d3 1 # Type aliases. -type bool8 int8[0:1] -type bool16 int16[0:1] +type mybool8 int8[0:1] +type mybool16 int16[0:1] type net_port proc[100, 1, int16be] -resource typeres0[bool8] +resource typeres0[mybool8] typestruct { - f1 bool8 - f2 bool16 + f1 mybool8 + f2 mybool16 } typeunion [ - f1 bool8 - f2 bool16 + f1 mybool8 + f2 mybool16 ] type type0 int8 @@ -217,6 +217,7 @@ type type13 ptr[in, typestruct13] type type14 flags[type0, int32] type type15 const[0, type0] ### unexpected value type0 for base type argument of const type, expect [int8 int16 int32 int64 int16be int32be int64be intptr] type type16 ptr[in, type0] ### type aliases can't refer to other type aliases +type bool8 int8[0:1] ### type name bool8 conflicts with builtin type typestruct11 { f type11 ### unknown type type11 @@ -231,7 +232,7 @@ typestruct13 { f2 type12 } -foo$100(a bool8, b bool16) +foo$100(a mybool8, b mybool16) foo$101(a type5) ### unknown type type5 foo$102(a type2) ### unknown type type2 foo$103(a type0:4) ### type alias type0 with ':' diff --git a/pkg/compiler/types.go b/pkg/compiler/types.go index 6174bb193..ee6e4a559 100644 --- a/pkg/compiler/types.go +++ b/pkg/compiler/types.go @@ -638,7 +638,8 @@ var typeArgBase = namedArg{ } var ( - builtinTypes = make(map[string]*typeDesc) + builtinTypes = make(map[string]*typeDesc) + builtinTypedefs = make(map[string]*ast.TypeDef) // To avoid weird cases like ptr[in, in] and ptr[out, opt]. reservedName = map[string]bool{ @@ -649,6 +650,14 @@ var ( } ) +const builtinDefs = ` +type bool8 int8[0:1] +type bool16 int16[0:1] +type bool32 int32[0:1] +type bool64 int64[0:1] +type boolptr intptr[0:1] +` + func init() { builtins := []*typeDesc{ typeInt, @@ -674,4 +683,16 @@ func init() { builtinTypes[name] = desc } } + builtinDesc := ast.Parse([]byte(builtinDefs), "builtins", func(pos ast.Pos, msg string) { + panic(fmt.Sprintf("failed to parse builtins: %v: %v", pos, msg)) + }) + for _, decl := range builtinDesc.Nodes { + switch n := decl.(type) { + case *ast.TypeDef: + builtinTypedefs[n.Name.Name] = n + case *ast.NewLine: + default: + panic(fmt.Sprintf("unexpected node in builtins: %#v", n)) + } + } } -- cgit mrf-deployment