[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 11/26] qapi: Make c_type() consistently convert qapi
From: |
Markus Armbruster |
Subject: |
[Qemu-devel] [PULL 11/26] qapi: Make c_type() consistently convert qapi names |
Date: |
Fri, 15 May 2015 10:24:37 +0200 |
From: Eric Blake <address@hidden>
Continuing the string of cleanups for supporting downstream names
containing '.', this patch focuses on ensuring c_type() can
handle a downstream name. This patch alone does not fix the
places where generator output should be calling this function
but was open-coding things instead, but it gets us a step closer.
In particular, the changes to c_list_type() and type_name() mean
that type_name(FOO) now handles the case when FOO contains '.',
'-', or is a ticklish identifier other than a builtin (builtins
are exempted because ['int'] must remain mapped to 'intList' and
not 'q_intList'). Meanwhile, ['unix'] now maps to 'q_unixList'
rather than 'unixList', to match the fact that 'unix' is ticklish;
however, our naming conventions state that complex types should
start with a capital, so no type name following conventions will
ever have the 'q_' prepended.
Likewise, changes to c_type() mean that c_type(FOO) properly
handles an enum or complex type FOO with '.' or '-' in the
name, or is a ticklish identifier (again, a ticklish identifier
as a type name violates conventions).
Signed-off-by: Eric Blake <address@hidden>
Signed-off-by: Markus Armbruster <address@hidden>
---
scripts/qapi.py | 33 +++++++++++++++++++++++++++++----
1 file changed, 29 insertions(+), 4 deletions(-)
diff --git a/scripts/qapi.py b/scripts/qapi.py
index ecab879..273afed 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -769,6 +769,15 @@ def c_enum_const(type_name, const_name):
c_name_trans = string.maketrans('.-', '__')
+# Map @name to a valid C identifier.
+# If @protect, avoid returning certain ticklish identifiers (like
+# C keywords) by prepending "q_".
+#
+# Used for converting 'name' from a 'name':'type' qapi definition
+# into a generated struct member, as well as converting type names
+# into substrings of a generated C function name.
+# '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
+# protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
def c_name(name, protect=True):
# ANSI X3J11/88-090, 3.1.1
c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
@@ -800,13 +809,25 @@ def c_name(name, protect=True):
return "q_" + name
return name.translate(c_name_trans)
+# Map type @name to the C typedef name for the list form.
+#
+# ['Name'] -> 'NameList', ['x-Foo'] -> 'x_FooList', ['int'] -> 'intList'
def c_list_type(name):
- return name + 'List'
+ return type_name(name) + 'List'
+# Map type @value to the C typedef form.
+#
+# Used for converting 'type' from a 'member':'type' qapi definition
+# into the alphanumeric portion of the type for a generated C parameter,
+# as well as generated C function names. See c_type() for the rest of
+# the conversion such as adding '*' on pointer types.
+# 'int' -> 'int', '[x-Foo]' -> 'x_FooList', '__a.b_c' -> '__a_b_c'
def type_name(value):
if type(value) == list:
return c_list_type(value[0])
- return value
+ if value in builtin_types.keys():
+ return value
+ return c_name(value)
def add_name(name, info, meta, implicit = False):
global all_names
@@ -865,6 +886,10 @@ def is_enum(name):
eatspace = '\033EATSPACE.'
pointer_suffix = ' *' + eatspace
+# Map type @name to its C type expression.
+# If @is_param, const-qualify the string type.
+#
+# This function is used for computing the full C type of 'member':'name'.
# A special suffix is added in c_type() for pointer types, and it's
# stripped in mcgen(). So please notice this when you check the return
# value of c_type() outside mcgen().
@@ -889,7 +914,7 @@ def c_type(value, is_param=False):
elif type(value) == list:
return c_list_type(value[0]) + pointer_suffix
elif is_enum(value):
- return value
+ return c_name(value)
elif value == None:
return 'void'
elif value in events:
@@ -897,7 +922,7 @@ def c_type(value, is_param=False):
else:
# complex type name
assert isinstance(value, str) and value != ""
- return value + pointer_suffix
+ return c_name(value) + pointer_suffix
def is_c_ptr(value):
return c_type(value).endswith(pointer_suffix)
--
1.9.3
- [Qemu-devel] [PULL 01/26] qmp: Add qom_path field to query-cpus command, (continued)
- [Qemu-devel] [PULL 01/26] qmp: Add qom_path field to query-cpus command, Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 07/26] qapi: Simplify c_enum_const(), Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 05/26] qapi: Rename _generate_enum_string() to camel_to_upper(), Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 06/26] qapi: Rename generate_enum_full_value() to c_enum_const(), Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 14/26] qapi: Support downstream simple unions, Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 03/26] qapi: Fix C identifiers generated for names containing '.', Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 18/26] tests: Add missing dependencies on $(qapi-py), Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 26/26] qapi: Inline gen_command_decl_prologue(), gen_command_def_prologue(), Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 25/26] qapi: Drop pointless flush() before close(), Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 20/26] qapi: qapi-commands.py option --type is unused, drop it, Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 11/26] qapi: Make c_type() consistently convert qapi names,
Markus Armbruster <=
- [Qemu-devel] [PULL 19/26] qapi: qapi-event.py option -b does nothing, drop it, Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 04/26] qapi: Rename identical c_fun()/c_var() into c_name(), Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 23/26] qapi: Turn generators' mandatory option -i into an argument, Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 24/26] qapi: Factor open_output(), close_output() out of generators, Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 13/26] qapi: Support downstream structs, Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 22/26] qapi: Fix generators to report command line errors decently, Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 15/26] qapi: Support downstream flat unions, Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 10/26] qapi: Tidy c_type() logic, Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 16/26] qapi: Support downstream alternates, Markus Armbruster, 2015/05/15
- [Qemu-devel] [PULL 21/26] qapi: Factor parse_command_line() out of the generators, Markus Armbruster, 2015/05/15