summaryrefslogtreecommitdiffstats
path: root/src/vpp-api/vapi/vapi_c_gen.py
diff options
context:
space:
mode:
authorOle Troan <ot@cisco.com>2018-11-13 12:36:56 +0100
committerFlorin Coras <florin.coras@gmail.com>2018-11-29 07:39:22 +0000
commit53fffa1db7cb04982db8977acd61b808ef60d5a8 (patch)
tree7f8c8b25b51d722cc6353c028ddad4e0ad6fcd31 /src/vpp-api/vapi/vapi_c_gen.py
parent4f10db317382832068d67b5d19be4a696d80c19a (diff)
API: Add support for type aliases
Previously all types are compound. This adds support for aliases, so one can do things like: typedef u32 interface_index; or typedef u8 ip4_address[4]; Change-Id: I0455cad0123fc88acb491d2a3ea2725426bdb246 Signed-off-by: Ole Troan <ot@cisco.com> Signed-off-by: Klement Sekera <ksekera@cisco.com>
Diffstat (limited to 'src/vpp-api/vapi/vapi_c_gen.py')
-rwxr-xr-xsrc/vpp-api/vapi/vapi_c_gen.py43
1 files changed, 32 insertions, 11 deletions
diff --git a/src/vpp-api/vapi/vapi_c_gen.py b/src/vpp-api/vapi/vapi_c_gen.py
index eb1006d3a7e..9939bc0556c 100755
--- a/src/vpp-api/vapi/vapi_c_gen.py
+++ b/src/vpp-api/vapi/vapi_c_gen.py
@@ -5,15 +5,18 @@ import os
import sys
import logging
from vapi_json_parser import Field, Struct, Enum, Union, Message, JsonParser,\
- SimpleType, StructType
+ SimpleType, StructType, Alias
class CField(Field):
+ def get_c_name(self):
+ return self.name
+
def get_c_def(self):
if self.len is not None:
- return "%s %s[%d]" % (self.type.get_c_name(), self.name, self.len)
+ return "%s %s[%d];" % (self.type.get_c_name(), self.name, self.len)
else:
- return "%s %s" % (self.type.get_c_name(), self.name)
+ return "%s %s;" % (self.type.get_c_name(), self.name)
def get_swap_to_be_code(self, struct, var):
if self.len is not None:
@@ -95,12 +98,26 @@ class CField(Field):
return result
+class CAlias(CField):
+ def get_c_name(self):
+ return self.name
+
+ def get_c_def(self):
+ return "typedef %s" % super(CAlias, self).get_c_def()
+ # if self.len is not None:
+ # return "typedef %s %s[%d];" % (self.type.get_c_name(), self.name, self.len)
+ # else:
+ # return "typedef %s %s;" % (self.type.get_c_name(), self.name)
+
+ # def needs_byte_swap
+
+
class CStruct(Struct):
def get_c_def(self):
return "\n".join([
- "typedef struct __attribute__((__packed__)) {\n%s;" % (
- ";\n".join([" %s" % x.get_c_def()
- for x in self.fields])),
+ "typedef struct __attribute__((__packed__)) {\n%s" % (
+ "\n".join([" %s" % x.get_c_def()
+ for x in self.fields])),
"} %s;" % self.get_c_name()])
def get_vla_assign_code(self, prefix, path):
@@ -156,7 +173,7 @@ class CSimpleType (SimpleType):
try:
self.get_swap_to_host_func_name()
return True
- except:
+ except KeyError:
pass
return False
@@ -335,8 +352,8 @@ class CMessage (Message):
if self.has_payload():
return "\n".join([
"typedef struct __attribute__ ((__packed__)) {",
- "%s; " %
- ";\n".join(self.payload_members),
+ "%s " %
+ "\n".join(self.payload_members),
"} %s;" % self.get_payload_struct_name(),
"",
"typedef struct __attribute__ ((__packed__)) {",
@@ -609,7 +626,8 @@ def emit_definition(parser, json_file, emitted, o):
if (o not in parser.enums_by_json[json_file] and
o not in parser.types_by_json[json_file] and
o not in parser.unions_by_json[json_file] and
- o.name not in parser.messages_by_json[json_file]):
+ o.name not in parser.messages_by_json[json_file] and
+ o not in parser.aliases_by_json[json_file]):
return
guard = "defined_%s" % o.get_c_name()
print("#ifndef %s" % guard)
@@ -690,6 +708,8 @@ def gen_json_unified_header(parser, logger, j, io, name):
emitted = []
for e in parser.enums_by_json[j]:
emit_definition(parser, j, emitted, e)
+ for a in parser.aliases_by_json[j]:
+ emit_definition(parser, j, emitted, a)
for u in parser.unions_by_json[j]:
emit_definition(parser, j, emitted, u)
for t in parser.types_by_json[j]:
@@ -765,7 +785,8 @@ if __name__ == '__main__':
union_class=CUnion,
struct_type_class=CStructType,
field_class=CField,
- message_class=CMessage)
+ message_class=CMessage,
+ alias_class=CAlias)
# not using the model of having separate generated header and code files
# with generated symbols present in shared library (per discussion with