sd/milkymist-memcard: Fix format string
[qemu/armbru.git] / scripts / qapi / common.py
blobba35abea478e1647da077a2699bbf7455cead2f9
2 # QAPI helper library
4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2018 Red Hat Inc.
7 # Authors:
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.
14 import re
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)
22 if value.isupper():
23 return c_fun_str
25 new_name = ''
26 length = len(c_fun_str)
27 for i in range(length):
28 c = c_fun_str[i]
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():
32 new_name += '_'
33 elif c_fun_str[i - 1].isdigit():
34 new_name += '_'
35 new_name += c
36 return new_name.lstrip('_').upper()
39 def c_enum_const(type_name, const_name, prefix=None):
40 if prefix is not None:
41 type_name = prefix
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
71 # excluding _.*
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):
88 return 'q_' + name
89 return name
92 eatspace = '\033EATSPACE.'
93 pointer_suffix = ' *' + eatspace
96 def genindent(count):
97 ret = ''
98 for _ in range(count):
99 ret += ' '
100 return ret
103 indent_level = 0
106 def push_indent(indent_amount=4):
107 global indent_level
108 indent_level += indent_amount
111 def pop_indent(indent_amount=4):
112 global indent_level
113 indent_level -= indent_amount
116 # Generate @code with @kwds interpolated.
117 # Obey indent_level, and strip eatspace.
118 def cgen(code, **kwds):
119 raw = code % kwds
120 if indent_level:
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),
124 indent, raw)
125 raw = raw[0]
126 return re.sub(re.escape(eatspace) + r' *', '', raw)
129 def mcgen(code, **kwds):
130 if code[0] == '\n':
131 code = code[1:]
132 return cgen(code, **kwds)
135 def c_fname(filename):
136 return re.sub(r'[^A-Za-z0-9_]', '_', filename)
139 def guardstart(name):
140 return mcgen('''
141 #ifndef %(name)s
142 #define %(name)s
144 ''',
145 name=c_fname(name).upper())
148 def guardend(name):
149 return mcgen('''
151 #endif /* %(name)s */
152 ''',
153 name=c_fname(name).upper())
156 def gen_if(ifcond):
157 ret = ''
158 for ifc in ifcond:
159 ret += mcgen('''
160 #if %(cond)s
161 ''', cond=ifc)
162 return ret
165 def gen_endif(ifcond):
166 ret = ''
167 for ifc in reversed(ifcond):
168 ret += mcgen('''
169 #endif /* %(cond)s */
170 ''', cond=ifc)
171 return ret
174 def build_params(arg_type, boxed, extra=None):
175 ret = ''
176 sep = ''
177 if boxed:
178 assert arg_type
179 ret += '%s arg' % arg_type.c_param_type()
180 sep = ', '
181 elif arg_type:
182 assert not arg_type.variants
183 for memb in arg_type.members:
184 ret += sep
185 sep = ', '
186 if memb.optional:
187 ret += 'bool has_%s, ' % c_name(memb.name)
188 ret += '%s %s' % (memb.type.c_param_type(),
189 c_name(memb.name))
190 if extra:
191 ret += sep + extra
192 return ret if ret else 'void'