diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2018-03-02 11:49:19 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2018-03-05 12:10:27 +0100 |
| commit | 5110ff445ddb5a09a13e17b187c06d2dc3a7d52a (patch) | |
| tree | 4a482d23c3e284e996539a1677dee246e9e7b0a5 /pkg/compiler | |
| parent | db01d57e9144125b368d14815d08e897ff496604 (diff) | |
pkg/compiler: switch attributes from Ident to Type
This allows parametrized attributes like size[10].
But this is not used for now.
Diffstat (limited to 'pkg/compiler')
| -rw-r--r-- | pkg/compiler/check.go | 6 | ||||
| -rw-r--r-- | pkg/compiler/compiler.go | 22 | ||||
| -rw-r--r-- | pkg/compiler/testdata/errors.txt | 12 |
3 files changed, 32 insertions, 8 deletions
diff --git a/pkg/compiler/check.go b/pkg/compiler/check.go index 57ef5b995..58738fa3e 100644 --- a/pkg/compiler/check.go +++ b/pkg/compiler/check.go @@ -535,6 +535,12 @@ func (comp *compiler) checkStruct(ctx checkCtx, n *ast.Struct) { for _, f := range n.Fields { comp.checkType(ctx, f.Type, flags) } + for _, attr := range n.Attrs { + if attr.Ident == "" || attr.HasColon { + comp.error(attr.Pos, "bad struct/union attribute") + return + } + } if n.IsUnion { comp.parseUnionAttrs(n) } else { diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 918d25136..8e20a4e67 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -138,12 +138,15 @@ func (comp *compiler) warning(pos ast.Pos, msg string, args ...interface{}) { func (comp *compiler) parseUnionAttrs(n *ast.Struct) (varlen bool) { for _, attr := range n.Attrs { - switch attr.Name { + switch attr.Ident { case "varlen": varlen = true default: comp.error(attr.Pos, "unknown union %v attribute %v", - n.Name.Name, attr.Name) + n.Name.Name, attr.Ident) + } + if len(attr.Args) != 0 { + comp.error(attr.Pos, "%v attribute had args", attr.Ident) } } return @@ -152,15 +155,15 @@ func (comp *compiler) parseUnionAttrs(n *ast.Struct) (varlen bool) { func (comp *compiler) parseStructAttrs(n *ast.Struct) (packed bool, align uint64) { for _, attr := range n.Attrs { switch { - case attr.Name == "packed": + case attr.Ident == "packed": packed = true - case attr.Name == "align_ptr": + case attr.Ident == "align_ptr": align = comp.ptrSize - case strings.HasPrefix(attr.Name, "align_"): - a, err := strconv.ParseUint(attr.Name[6:], 10, 64) + case strings.HasPrefix(attr.Ident, "align_"): + a, err := strconv.ParseUint(attr.Ident[6:], 10, 64) if err != nil { comp.error(attr.Pos, "bad struct %v alignment %v", - n.Name.Name, attr.Name[6:]) + n.Name.Name, attr.Ident[6:]) continue } if a&(a-1) != 0 || a == 0 || a > 1<<30 { @@ -170,7 +173,10 @@ func (comp *compiler) parseStructAttrs(n *ast.Struct) (packed bool, align uint64 align = a default: comp.error(attr.Pos, "unknown struct %v attribute %v", - n.Name.Name, attr.Name) + n.Name.Name, attr.Ident) + } + if len(attr.Args) != 0 { + comp.error(attr.Pos, "%v attribute had args", attr.Ident) } } return diff --git a/pkg/compiler/testdata/errors.txt b/pkg/compiler/testdata/errors.txt index 8ea03bc7b..2b3a7539f 100644 --- a/pkg/compiler/testdata/errors.txt +++ b/pkg/compiler/testdata/errors.txt @@ -160,6 +160,18 @@ s7 { f1 ptr64[in, int32] } +s8 { + f1 int8 +} [unknown] ### unknown struct s8 attribute unknown + +s9 { + f1 int8 +} ["foo"[0]] ### bad struct/union attribute + +s10 { + f1 int8 +} [packed[0]] ### packed attribute had args + u3 [ f1 int8 f2 int32 |
