From 07e46fbc2bd7ff8782c975596672e4e3d3891865 Mon Sep 17 00:00:00 2001 From: Florent Revest Date: Thu, 28 Nov 2024 01:49:47 +0100 Subject: pkg/compiler: handle string syscall attributes --- pkg/compiler/compiler.go | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'pkg/compiler/compiler.go') diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index 978255219..fa23ce6bf 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -210,34 +210,36 @@ func (comp *compiler) structIsVarlen(name string) bool { func (comp *compiler) parseIntAttrs(descs map[string]*attrDesc, parent ast.Node, attrs []*ast.Type) map[*attrDesc]uint64 { - intAttrs, _ := comp.parseAttrs(descs, parent, attrs) + intAttrs, _, _ := comp.parseAttrs(descs, parent, attrs) return intAttrs } func (comp *compiler) parseAttrs(descs map[string]*attrDesc, parent ast.Node, attrs []*ast.Type) ( - map[*attrDesc]uint64, map[*attrDesc]prog.Expression) { + map[*attrDesc]uint64, map[*attrDesc]prog.Expression, map[*attrDesc]string) { _, parentType, parentName := parent.Info() resInt := make(map[*attrDesc]uint64) resExpr := make(map[*attrDesc]prog.Expression) + resString := make(map[*attrDesc]string) for _, attr := range attrs { if unexpected, _, ok := checkTypeKind(attr, kindIdent); !ok { comp.error(attr.Pos, "unexpected %v, expect attribute", unexpected) - return resInt, resExpr + return resInt, resExpr, resString } if len(attr.Colon) != 0 { comp.error(attr.Colon[0].Pos, "unexpected ':'") - return resInt, resExpr + return resInt, resExpr, resString } desc := descs[attr.Ident] if desc == nil { comp.error(attr.Pos, "unknown %v %v attribute %v", parentType, parentName, attr.Ident) - return resInt, resExpr + return resInt, resExpr, resString } _, dupInt := resInt[desc] _, dupExpr := resExpr[desc] - if dupInt || dupExpr { + _, dupString := resString[desc] + if dupInt || dupExpr || dupString { comp.error(attr.Pos, "duplicate %v %v attribute %v", parentType, parentName, attr.Ident) - return resInt, resExpr + return resInt, resExpr, resString } switch desc.Type { @@ -245,18 +247,20 @@ func (comp *compiler) parseAttrs(descs map[string]*attrDesc, parent ast.Node, at resInt[desc] = 1 if len(attr.Args) != 0 { comp.error(attr.Pos, "%v attribute has args", attr.Ident) - return nil, nil + return nil, nil, nil } case intAttr: resInt[desc] = comp.parseAttrIntArg(attr) case exprAttr: resExpr[desc] = comp.parseAttrExprArg(attr) + case stringAttr: + resString[desc] = comp.parseAttrStringArg(attr) default: comp.error(attr.Pos, "attribute %v has unknown type", attr.Ident) - return nil, nil + return nil, nil, nil } } - return resInt, resExpr + return resInt, resExpr, resString } func (comp *compiler) parseAttrExprArg(attr *ast.Type) prog.Expression { @@ -289,6 +293,19 @@ func (comp *compiler) parseAttrIntArg(attr *ast.Type) uint64 { return sz.Value } +func (comp *compiler) parseAttrStringArg(attr *ast.Type) string { + if len(attr.Args) != 1 { + comp.error(attr.Pos, "%v attribute is expected to have 1 argument", attr.Ident) + return "" + } + arg := attr.Args[0] + if !arg.HasString { + comp.error(attr.Pos, "%v argument must be a string", attr.Ident) + return "" + } + return arg.String +} + func (comp *compiler) getTypeDesc(t *ast.Type) *typeDesc { if desc := builtinTypes[t.Ident]; desc != nil { return desc -- cgit mrf-deployment