aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/compiler
diff options
context:
space:
mode:
authorJoey Jiaojg <joeyjiaojg@gmail.com>2021-05-26 19:38:04 +0800
committerGitHub <noreply@github.com>2021-05-26 13:38:04 +0200
commit3da9017c17c7d2432c4b76345c4d2efbeedd2935 (patch)
tree47e9b10fc542a1f5a1c785027ec6d2fce1a7dfef /pkg/compiler
parent1f4e6ed95868861aae449b3630e6cfe5a2b96c93 (diff)
pkg/compiler: add glob type
* all: add new typename dirname The current way to check files under sysfs or proc is: - define a string to represent each file - open the file - pass the fd to write / read / close The issues above are: - Need to know what file present on target device - Need to write openat for each file With dirname added, which will open one file in the directory randomly and then pass the fd to write/read/close. * all: use typename glob to match filename Fixes #481
Diffstat (limited to 'pkg/compiler')
-rw-r--r--pkg/compiler/testdata/all.txt3
-rw-r--r--pkg/compiler/testdata/errors.txt7
-rw-r--r--pkg/compiler/types.go24
3 files changed, 32 insertions, 2 deletions
diff --git a/pkg/compiler/testdata/all.txt b/pkg/compiler/testdata/all.txt
index 17b7c838d..b1adf2321 100644
--- a/pkg/compiler/testdata/all.txt
+++ b/pkg/compiler/testdata/all.txt
@@ -252,6 +252,9 @@ foo_templ9(a ptr[in, templ_base3[int64]])
foo_templ10(a ptr[in, templ_base4[int8]])
foo_templ11(a ptr[in, templ_base5[42, int8]])
+foo_glob0(a ptr[in, glob["/sys/"]])
+foo_glob1(a ptr[in, glob["/sys/**/*"]])
+
# Structs.
s0 {
diff --git a/pkg/compiler/testdata/errors.txt b/pkg/compiler/testdata/errors.txt
index d3658d239..670cc0193 100644
--- a/pkg/compiler/testdata/errors.txt
+++ b/pkg/compiler/testdata/errors.txt
@@ -334,6 +334,11 @@ foo$210(a ptr[in, templ11[0, 1, int8]]) ### template templ11 needs 2 arguments
foo$211(a ptr[in, templ9]) ### template templ9 needs 1 arguments instead of 0
foo$212(a ptr[in, templ11[1]]) ### template templ11 needs 2 arguments instead of 1
+foo$glob001(a ptr[in, glob[1]]) ### unexpected int 1, string arg must be a string literal or string flags
+foo$glob002(a ptr[in, glob]) ### glob only accepts 1 arg, provided 0
+foo$glob003(a ptr[in, glob["/sys", 5]]) ### glob only accepts 1 arg, provided 2
+foo$glob004(a ptr[in, glob["/sys", 5, 2]]) ### wrong number of arguments for type glob, expect [literal or flags], [size], [opt]
+
# fmt
foo$fmt0(a ptr[in, fmt]) ### wrong number of arguments for type fmt, expect format, value
@@ -355,4 +360,4 @@ struct$perfielddir {
f4 int32 (in, inout) ### arg/field has multiple direction attributes
f5 int32 (out, inout) ### arg/field has multiple direction attributes
f6 int32 (in, out, inout) ### arg/field has multiple direction attributes
-} \ No newline at end of file
+}
diff --git a/pkg/compiler/types.go b/pkg/compiler/types.go
index 038a05f24..d62c8fd48 100644
--- a/pkg/compiler/types.go
+++ b/pkg/compiler/types.go
@@ -598,10 +598,11 @@ func genTextType(t *ast.Type) prog.TextKind {
const (
stringnoz = "stringnoz"
+ glob = "glob"
)
var typeString = &typeDesc{
- Names: []string{"string", stringnoz},
+ Names: []string{"string", glob, stringnoz},
CanBeTypedef: true,
OptArgs: 2,
Args: []namedArg{
@@ -612,6 +613,15 @@ var typeString = &typeDesc{
if t.Ident == stringnoz && len(args) > 1 {
comp.error(args[0].Pos, "fixed-size string can't be non-zero-terminated")
}
+ if t.Ident == glob {
+ pos := t.Pos
+ if len(args) > 0 {
+ pos = args[0].Pos
+ }
+ if len(args) != 1 {
+ comp.error(pos, "glob only accepts 1 arg, provided %v", len(args))
+ }
+ }
},
CheckConsts: func(comp *compiler, t *ast.Type, args []*ast.Type, base prog.IntTypeCommon) {
if len(args) > 1 {
@@ -626,6 +636,9 @@ var typeString = &typeDesc{
}
},
Varlen: func(comp *compiler, t *ast.Type, args []*ast.Type) bool {
+ if t.Ident == glob {
+ return true
+ }
return comp.stringSize(t, args) == varlenString
},
ZeroSize: func(comp *compiler, t *ast.Type, args []*ast.Type) bool {
@@ -645,6 +658,15 @@ var typeString = &typeDesc{
NoZ: t.Ident == stringnoz,
}
}
+ if len(args) > 0 && t.Ident == glob {
+ base.TypeSize = 0
+ return &prog.BufferType{
+ TypeCommon: base.TypeCommon,
+ Kind: prog.BufferGlob,
+ SubKind: args[0].String,
+ NoZ: false,
+ }
+ }
subkind := ""
if len(args) > 0 && args[0].Ident != "" {
subkind = args[0].Ident