mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-19 05:44:12 +01:00
tlv: consolidate basetype parsing
clean up basetype parsing code a bit
This commit is contained in:
parent
6f2e70a6ac
commit
9a23a354fd
@ -53,6 +53,14 @@ class FieldType(object):
|
|||||||
def has_array_helper(self):
|
def has_array_helper(self):
|
||||||
return self.name in ['u8']
|
return self.name in ['u8']
|
||||||
|
|
||||||
|
def base(self):
|
||||||
|
basetype = self.name
|
||||||
|
if basetype.startswith('struct '):
|
||||||
|
basetype = basetype[7:]
|
||||||
|
elif basetype.startswith('enum '):
|
||||||
|
basetype = basetype[5:]
|
||||||
|
return basetype
|
||||||
|
|
||||||
# Returns base size
|
# Returns base size
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _typesize(typename):
|
def _typesize(typename):
|
||||||
@ -171,14 +179,6 @@ class Field(object):
|
|||||||
# Real typename.
|
# Real typename.
|
||||||
self.fieldtype = FieldType(size)
|
self.fieldtype = FieldType(size)
|
||||||
|
|
||||||
def basetype(self):
|
|
||||||
base = self.fieldtype.name
|
|
||||||
if base.startswith('struct '):
|
|
||||||
base = base[7:]
|
|
||||||
elif base.startswith('enum '):
|
|
||||||
base = base[5:]
|
|
||||||
return base
|
|
||||||
|
|
||||||
def is_padding(self):
|
def is_padding(self):
|
||||||
return self.name.startswith('pad')
|
return self.name.startswith('pad')
|
||||||
|
|
||||||
@ -378,7 +378,7 @@ class Message(object):
|
|||||||
if field.is_variable_size():
|
if field.is_variable_size():
|
||||||
self.checkLenField(field)
|
self.checkLenField(field)
|
||||||
self.has_variable_fields = True
|
self.has_variable_fields = True
|
||||||
elif field.basetype() in varlen_structs or field.optional:
|
elif field.fieldtype.base() in varlen_structs or field.optional:
|
||||||
self.has_variable_fields = True
|
self.has_variable_fields = True
|
||||||
self.fields.append(field)
|
self.fields.append(field)
|
||||||
|
|
||||||
@ -409,7 +409,7 @@ class Message(object):
|
|||||||
fields = ['\t{} {};\n'.format(f.fieldtype.name, f.name) for f in self.fields if f.is_len_var]
|
fields = ['\t{} {};\n'.format(f.fieldtype.name, f.name) for f in self.fields if f.is_len_var]
|
||||||
subcalls = CCode()
|
subcalls = CCode()
|
||||||
for f in self.fields:
|
for f in self.fields:
|
||||||
basetype = f.basetype()
|
basetype = f.fieldtype.base()
|
||||||
if f.is_tlv:
|
if f.is_tlv:
|
||||||
raise TypeError('Nested TLVs arent allowed!!')
|
raise TypeError('Nested TLVs arent allowed!!')
|
||||||
elif f.optional:
|
elif f.optional:
|
||||||
@ -482,7 +482,7 @@ class Message(object):
|
|||||||
if f.needs_ptr_to_ptr():
|
if f.needs_ptr_to_ptr():
|
||||||
ptrs += '*'
|
ptrs += '*'
|
||||||
# If each type is a variable length, we need a ptr to that.
|
# If each type is a variable length, we need a ptr to that.
|
||||||
if f.basetype() in varlen_structs:
|
if f.fieldtype.base() in varlen_structs:
|
||||||
ptrs += '*'
|
ptrs += '*'
|
||||||
|
|
||||||
args.append(', {} {}{}'.format(f.fieldtype.name, ptrs, f.name))
|
args.append(', {} {}{}'.format(f.fieldtype.name, ptrs, f.name))
|
||||||
@ -492,7 +492,7 @@ class Message(object):
|
|||||||
|
|
||||||
subcalls = CCode()
|
subcalls = CCode()
|
||||||
for f in self.fields:
|
for f in self.fields:
|
||||||
basetype = f.basetype()
|
basetype = f.fieldtype.base()
|
||||||
|
|
||||||
for c in f.comments:
|
for c in f.comments:
|
||||||
subcalls.append('/*{} */'.format(c))
|
subcalls.append('/*{} */'.format(c))
|
||||||
@ -591,12 +591,7 @@ class Message(object):
|
|||||||
|
|
||||||
subcalls = CCode()
|
subcalls = CCode()
|
||||||
for f in self.fields:
|
for f in self.fields:
|
||||||
basetype = f.fieldtype.name
|
basetype = f.fieldtype.base()
|
||||||
if basetype.startswith('struct '):
|
|
||||||
basetype = basetype[7:]
|
|
||||||
elif basetype.startswith('enum '):
|
|
||||||
basetype = basetype[5:]
|
|
||||||
|
|
||||||
for c in f.comments:
|
for c in f.comments:
|
||||||
subcalls.append('/*{} */'.format(c))
|
subcalls.append('/*{} */'.format(c))
|
||||||
|
|
||||||
@ -632,7 +627,7 @@ class Message(object):
|
|||||||
args.append(', const struct _{} *{}'.format(f.name, f.name))
|
args.append(', const struct _{} *{}'.format(f.name, f.name))
|
||||||
elif f.is_assignable():
|
elif f.is_assignable():
|
||||||
args.append(', {} {}'.format(f.fieldtype.name, f.name))
|
args.append(', {} {}'.format(f.fieldtype.name, f.name))
|
||||||
elif f.is_variable_size() and f.basetype() in varlen_structs:
|
elif f.is_variable_size() and f.fieldtype.base() in varlen_structs:
|
||||||
args.append(', const {} **{}'.format(f.fieldtype.name, f.name))
|
args.append(', const {} **{}'.format(f.fieldtype.name, f.name))
|
||||||
else:
|
else:
|
||||||
args.append(', const {} *{}'.format(f.fieldtype.name, f.name))
|
args.append(', const {} *{}'.format(f.fieldtype.name, f.name))
|
||||||
@ -651,11 +646,7 @@ class Message(object):
|
|||||||
|
|
||||||
subcalls = CCode()
|
subcalls = CCode()
|
||||||
for f in self.fields:
|
for f in self.fields:
|
||||||
basetype = f.fieldtype.name
|
basetype = f.fieldtype.base()
|
||||||
if basetype.startswith('struct '):
|
|
||||||
basetype = basetype[7:]
|
|
||||||
elif basetype.startswith('enum '):
|
|
||||||
basetype = basetype[5:]
|
|
||||||
|
|
||||||
for c in f.comments:
|
for c in f.comments:
|
||||||
subcalls.append('/*{} */'.format(c))
|
subcalls.append('/*{} */'.format(c))
|
||||||
@ -735,7 +726,7 @@ class Message(object):
|
|||||||
|
|
||||||
subcalls = CCode()
|
subcalls = CCode()
|
||||||
for f in self.fields:
|
for f in self.fields:
|
||||||
basetype = f.basetype()
|
basetype = f.fieldtype.base()
|
||||||
|
|
||||||
for c in f.comments:
|
for c in f.comments:
|
||||||
subcalls.append('/*{} */'.format(c))
|
subcalls.append('/*{} */'.format(c))
|
||||||
|
Loading…
Reference in New Issue
Block a user