pkg/compiler: allow strings as template arguments

Needed for netfilter descriptions.
This commit is contained in:
Dmitry Vyukov 2018-01-23 10:44:01 +01:00
parent 228e3d951c
commit de3e24c4b6
3 changed files with 3 additions and 7 deletions

View File

@ -670,12 +670,6 @@ func (comp *compiler) replaceTypedef(ctx *checkCtx, t *ast.Type, desc *typeDesc,
ctx.instantiationStack = append(ctx.instantiationStack, fullTypeName)
nargs := len(typedef.Args)
args := t.Args
for _, arg := range args {
if arg.String != "" {
comp.error(arg.Pos, "template arguments can't be strings (%q)", arg.String)
return
}
}
if nargs != len(t.Args) {
if nargs == 0 {
comp.error(t.Pos, "type %v is not a template", typedefName)
@ -734,6 +728,7 @@ func (comp *compiler) instantiate(templ ast.Node, params []*ast.Ident, args []*a
// TODO(dvyukov): somewhat hacky, but required for int8[0:CONST_ARG]
// Need more checks here. E.g. that CONST_ARG does not have subargs.
// And if CONST_ARG is a value, then use concreteArg.Value.
// Also need to error if CONST_ARG is a string.
if concreteArg := argMap[templArg.Ident2]; concreteArg != nil {
templArg.Ident2 = concreteArg.Ident
templArg.Pos2 = concreteArg.Pos

View File

@ -86,6 +86,8 @@ bitfield0 {
type type0 int8
type templ0[A, B] const[A, B]
type templ2[STR] string[STR]
type type3 templ2["a"]
type templ_struct0[A, B] {
len len[parent, int16]

View File

@ -266,5 +266,4 @@ foo$202(a templ0) ### template templ0 needs 2 arguments instead of 0
foo$203(a type0[42]) ### type type0 is not a template
foo$204(a ptr[in, templ_struct0[42, int8]])
foo$205(a ptr[in, templ_struct0[int8, int8]])
foo$206(a ptr[in, templ_struct1["foo"]]) ### template arguments can't be strings ("foo")
foo$207(a ptr[in, templ_struct2[1]])