4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2018 Red Hat Inc.
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Markus Armbruster <armbru@redhat.com>
11 # This work is licensed under the terms of the GNU GPL, version 2.
12 # See the COPYING file in the top-level directory.
17 # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
18 # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
19 # ENUM24_Name -> ENUM24_NAME
20 def camel_to_upper(value
):
21 c_fun_str
= c_name(value
, False)
26 length
= len(c_fun_str
)
27 for i
in range(length
):
29 # When c is upper and no '_' appears before, do more checks
30 if c
.isupper() and (i
> 0) and c_fun_str
[i
- 1] != '_':
31 if i
< length
- 1 and c_fun_str
[i
+ 1].islower():
33 elif c_fun_str
[i
- 1].isdigit():
36 return new_name
.lstrip('_').upper()
39 def c_enum_const(type_name
, const_name
, prefix
=None):
40 if prefix
is not None:
42 return camel_to_upper(type_name
) + '_' + c_name(const_name
, False).upper()
45 c_name_trans
= str.maketrans('.-', '__')
48 # Map @name to a valid C identifier.
49 # If @protect, avoid returning certain ticklish identifiers (like
50 # C keywords) by prepending 'q_'.
52 # Used for converting 'name' from a 'name':'type' qapi definition
53 # into a generated struct member, as well as converting type names
54 # into substrings of a generated C function name.
55 # '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
56 # protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
57 def c_name(name
, protect
=True):
58 # ANSI X3J11/88-090, 3.1.1
59 c89_words
= set(['auto', 'break', 'case', 'char', 'const', 'continue',
60 'default', 'do', 'double', 'else', 'enum', 'extern',
61 'float', 'for', 'goto', 'if', 'int', 'long', 'register',
62 'return', 'short', 'signed', 'sizeof', 'static',
63 'struct', 'switch', 'typedef', 'union', 'unsigned',
64 'void', 'volatile', 'while'])
65 # ISO/IEC 9899:1999, 6.4.1
66 c99_words
= set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
67 # ISO/IEC 9899:2011, 6.4.1
68 c11_words
= set(['_Alignas', '_Alignof', '_Atomic', '_Generic',
69 '_Noreturn', '_Static_assert', '_Thread_local'])
70 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
72 gcc_words
= set(['asm', 'typeof'])
73 # C++ ISO/IEC 14882:2003 2.11
74 cpp_words
= set(['bool', 'catch', 'class', 'const_cast', 'delete',
75 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
76 'namespace', 'new', 'operator', 'private', 'protected',
77 'public', 'reinterpret_cast', 'static_cast', 'template',
78 'this', 'throw', 'true', 'try', 'typeid', 'typename',
79 'using', 'virtual', 'wchar_t',
80 # alternative representations
81 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
82 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
83 # namespace pollution:
84 polluted_words
= set(['unix', 'errno', 'mips', 'sparc', 'i386'])
85 name
= name
.translate(c_name_trans
)
86 if protect
and (name
in c89_words | c99_words | c11_words | gcc_words
87 | cpp_words | polluted_words
):
92 eatspace
= '\033EATSPACE.'
93 pointer_suffix
= ' *' + eatspace
98 for _
in range(count
):
106 def push_indent(indent_amount
=4):
108 indent_level
+= indent_amount
111 def pop_indent(indent_amount
=4):
113 indent_level
-= indent_amount
116 # Generate @code with @kwds interpolated.
117 # Obey indent_level, and strip eatspace.
118 def cgen(code
, **kwds
):
121 indent
= genindent(indent_level
)
122 # re.subn() lacks flags support before Python 2.7, use re.compile()
123 raw
= re
.subn(re
.compile(r
'^(?!(#|$))', re
.MULTILINE
),
126 return re
.sub(re
.escape(eatspace
) + r
' *', '', raw
)
129 def mcgen(code
, **kwds
):
132 return cgen(code
, **kwds
)
135 def c_fname(filename
):
136 return re
.sub(r
'[^A-Za-z0-9_]', '_', filename
)
139 def guardstart(name
):
145 name
=c_fname(name
).upper())
151 #endif /* %(name)s */
153 name
=c_fname(name
).upper())
165 def gen_endif(ifcond
):
167 for ifc
in reversed(ifcond
):
169 #endif /* %(cond)s */
174 def build_params(arg_type
, boxed
, extra
=None):
179 ret
+= '%s arg' % arg_type
.c_param_type()
182 assert not arg_type
.variants
183 for memb
in arg_type
.members
:
187 ret
+= 'bool has_%s, ' % c_name(memb
.name
)
188 ret
+= '%s %s' % (memb
.type.c_param_type(),
192 return ret
if ret
else 'void'