bolt-gen: handle enums

Add parsing know-how for enum fields. This is necessary for
internally defined wire generators. Enums are denoted by prefixing
the field with an `e:`.

Ex:

   msgdata,msg_name,field_name,e:enum_type,
This commit is contained in:
lisa neigut 2019-07-23 15:53:44 -05:00 committed by Rusty Russell
parent cfd56d86ee
commit b30d7d26ea
5 changed files with 46 additions and 6 deletions

View File

@ -238,13 +238,16 @@ class Type(FieldSet):
def __init__(self, name):
FieldSet.__init__(self)
self.name = name
self.name, self.is_enum = self.parse_name(name)
self.depends_on = {}
# FIXME: internal msgs can be enums
self.is_enum = False
self.type_comments = []
self.tlv = False
def parse_name(self, name):
if name.startswith('e:'):
return name[2:], True
return name, False
def add_data_field(self, field_name, type_obj, count=1,
is_extension=[], comments=[], optional=False):
FieldSet.add_data_field(self, field_name, type_obj, count,

View File

@ -7,6 +7,7 @@
# and run a test case.
check-units: check-tools
TOOL_TEST_INCL_SRC := tools/test/enum.c
TOOL_GEN_SRC := tools/test/gen_test.c tools/test/gen_print.c
TOOL_GEN_HEADER := tools/test/gen_test.h tools/test/gen_print.h
TOOL_TEST_SRC := $(wildcard tools/test/run-*.c)
@ -14,7 +15,8 @@ TOOL_TEST_OBJS := $(TOOL_TEST_SRC:.c=.o)
TOOL_TEST_PROGRAMS := $(TOOL_TEST_OBJS:.o=)
TOOL_TEST_COMMON_OBJS := \
common/utils.o
common/utils.o \
tools/test/enum.o
TOOLS_WIRE_DEPS := $(BOLT_DEPS) tools/test/test_cases $(wildcard tools/gen/*_template)
@ -22,6 +24,7 @@ $(TOOL_TEST_SRC) $(TOOL_GEN_SRC): $(TOOL_GEN_HEADER)
$(TOOL_TEST_OBJS): $(TOOL_GEN_SRC)
$(TOOL_TEST_PROGRAMS): $(TOOL_TEST_COMMON_OBJS) $(TOOL_GEN_SRC:.c=.o)
$(TOOL_GEN_SRC) $(TOOL_GEN_HEADER): $(TOOLS_WIRE_DEPS)
$(TOOL_GEN_SRC:.c=.o): $(TOOL_TEST_INCL_SRC:.c=.o)
tools/test/gen_test.h:
tools/generate-bolts.py --page header $@ test_type < tools/test/test_cases > $@

18
tools/test/enum.c Normal file
View File

@ -0,0 +1,18 @@
#include "enum.h"
#include <stdio.h>
void towire_test_enum(u8 **pptr, const enum test_enum test_enum)
{
printf("this would have been the towire for enum %u\n", test_enum);
}
enum test_enum fromwire_test_enum(const u8 **cursor, size_t *max)
{
printf("fromwire_test_enum at %ld\n", *max);
return TEST_ONE;
}
void printwire_test_enum(const char *fieldname, const enum test_enum *test_enum)
{
printf("%u\n", *test_enum);
}

15
tools/test/enum.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef LIGHTNING_TOOLS_TEST_ENUM_H
#define LIGHTNING_TOOLS_TEST_ENUM_H
#include <ccan/short_types/short_types.h>
#include <stdlib.h>
enum test_enum {
TEST_ONE,
TEST_TWO,
};
void towire_test_enum(u8 **pptr, const enum test_enum test_enum);
enum test_enum fromwire_test_enum(const u8 **cursor, size_t *max);
void printwire_test_enum(const char *fieldname, const enum test_enum *test_enum);
#endif /* LIGHTNING_TOOLS_TEST_ENUM_H */

View File

@ -1,6 +1,5 @@
#include <ccan/short_types/short_types.h>
# TODO: add enums!
# TODO: msgs expansions
#include "enum.h"
# AUTOGENERATED MOCKS START
# AUTOGENERATED MOCKS END
@ -26,6 +25,8 @@ msgdata,test_msg,len_varsize_struct,u16,
msgdata,test_msg,test_varsize_struct_varlen,test_features,len_varsize_struct
# assignable
msgdata,test_msg,test_assignable,u16,
# enum
msgdata,test_msg,test_enum,e:test_enum,
# test struct
msgdata,test_msg,test_struct,test_short_id,
# test var-size struct