Skip to content

Commit

Permalink
wire-gen: initial start on subtypes
Browse files Browse the repository at this point in the history
first pass at adding subtype structs
  • Loading branch information
niftynei authored and rustyrussell committed Apr 10, 2019
1 parent e4658c2 commit 86a099a
Showing 1 changed file with 37 additions and 15 deletions.
52 changes: 37 additions & 15 deletions tools/generate-wire.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ def __init__(self, message, name, size, comments, prevname, includes):

# Bolts use just a number: Guess type based on size.
if options.bolt:
if size == 'var_int':
if size == '$': # this is a subtype
self.fieldtype = FieldType('struct {}'.format(name))
elif size == 'var_int':
base_size = 8
self.fieldtype = FieldType(size)
else:
Expand Down Expand Up @@ -853,6 +855,14 @@ def _inner_print_struct(struct_name, fields):
fields=str(fmt_fields))


class Subtype(Message):
def __init__(self, name, comments):
super().__init__(name, None, comments, False)

def print_struct(self):
return TlvMessage._inner_print_struct(self.name, self.fields)


tlv_message_towire_stub = """static void towire_{tlv_name}_{name}(u8 **p, struct tlv_msg_{name} *{name}) {{
{field_decls}
{subcalls}
Expand Down Expand Up @@ -1071,6 +1081,7 @@ def find_message_with_option(messages, optional_messages, name, option):
# Maps message names to messages
messages = []
messages_with_option = []
subtypes = []
comments = []
includes = []
tlv_fields = {}
Expand All @@ -1096,28 +1107,31 @@ def find_message_with_option(messages, optional_messages, name, option):
is_tlv_msg = len(parts) == 3
if len(parts) == 2 or is_tlv_msg:
# eg: commit_sig,132,(_tlv)
if is_tlv_msg:
message = TlvMessage(parts[0],
Enumtype("WIRE_" + parts[0].upper(), parts[1]),
comments)
if parts[1] == '$': # this is a subtype
subtypes.append(Subtype(parts[0], comments))
else:
message = Message(parts[0],
Enumtype("WIRE_" + parts[0].upper(), parts[1]),
comments)
if is_tlv_msg:
message = TlvMessage(parts[0],
Enumtype("WIRE_" + parts[0].upper(), parts[1]),
comments)
else:
message = Message(parts[0],
Enumtype("WIRE_" + parts[0].upper(), parts[1]),
comments)

messages.append(message)
if is_tlv_msg:
tlv_fields[parts[2]].append(message)
messages.append(message)
if is_tlv_msg:
tlv_fields[parts[2]].append(message)

comments = []
prevfield = None
else:
if len(parts) == 4:
# eg commit_sig,0,channel-id,8 OR
# commit_sig,0,channel-id,u64
m = find_message(messages, parts[0])
m = find_message(messages + subtypes, parts[0])
if m is None:
raise ValueError('Unknown message {}'.format(parts[0]))
raise ValueError('Unknown message or subtype {}'.format(parts[0]))
elif len(parts) == 5:
# eg.
# channel_reestablish,48,your_last_per_commitment_secret,32,option209
Expand Down Expand Up @@ -1187,6 +1201,13 @@ def build_tlv_structs(tlv_fields):
return structs


def build_subtype_structs(subtypes):
structs = ""
for subtype in subtypes:
structs += subtype.print_struct()
return structs


enum_header_template = """enum {enumname} {{
{enums}
}};
Expand Down Expand Up @@ -1214,7 +1235,7 @@ def build_tlv_structs(tlv_fields):
#include <ccan/tal/tal.h>
#include <wire/wire.h>
{includes}
{formatted_hdr_enums}{tlv_structs}
{formatted_hdr_enums}{gen_structs}
{func_decls}
#endif /* LIGHTNING_{idem} */
"""
Expand Down Expand Up @@ -1304,6 +1325,7 @@ def build_tlv_structs(tlv_fields):
built_impl_enums = build_impl_enums(options.enumname, toplevel_messages, tlv_fields)
tlv_structs = build_tlv_structs(tlv_fields)
tlv_structs += build_tlv_type_structs(tlv_fields)
subtype_structs = build_subtype_structs(subtypes)
includes = '\n'.join(includes)
printcases = ['case {enum.name}: printf("{enum.name}:\\n"); printwire_{name}("{name}", msg); return;'.format(enum=m.enum, name=m.name) for m in toplevel_messages]

Expand Down Expand Up @@ -1339,5 +1361,5 @@ def build_tlv_structs(tlv_fields):
enumname=options.enumname,
formatted_hdr_enums=built_hdr_enums,
formatted_impl_enums=built_impl_enums,
tlv_structs=tlv_structs,
gen_structs=tlv_structs + subtype_structs,
func_decls='\n'.join(decls)))

0 comments on commit 86a099a

Please sign in to comment.