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.
25 #: Magic string that gets removed along with all space to its right.
26 EATSPACE
= '\033EATSPACE.'
27 POINTER_SUFFIX
= ' *' + EATSPACE
30 def camel_to_upper(value
: str) -> str:
32 Converts CamelCase to CAMEL_CASE.
37 EnumName1 -> ENUM_NAME1
38 ENUM_NAME -> ENUM_NAME
39 ENUM_NAME1 -> ENUM_NAME1
40 ENUM_Name2 -> ENUM_NAME2
41 ENUM24_Name -> ENUM24_NAME
44 upc
= value
[0].isupper()
46 # Copy remainder of ``value`` to ``ret`` with '_' inserted
48 if ch
.isupper() == upc
:
51 # ``ret`` ends in upper case, next char isn't: insert '_'
52 # before the last upper case char unless there is one
53 # already, or it's at the beginning
54 if len(ret
) > 2 and ret
[-2].isalnum():
55 ret
= ret
[:-1] + '_' + ret
[-1]
57 # ``ret`` doesn't end in upper case, next char is: insert
64 return c_name(ret
.upper()).lstrip('_')
67 def c_enum_const(type_name
: str,
69 prefix
: Optional
[str] = None) -> str:
71 Generate a C enumeration constant name.
73 :param type_name: The name of the enumeration.
74 :param const_name: The name of this constant.
75 :param prefix: Optional, prefix that overrides the type_name.
78 prefix
= camel_to_upper(type_name
)
79 return prefix
+ '_' + c_name(const_name
, False).upper()
82 def c_name(name
: str, protect
: bool = True) -> str:
84 Map ``name`` to a valid C identifier.
86 Used for converting 'name' from a 'name':'type' qapi definition
87 into a generated struct member, as well as converting type names
88 into substrings of a generated C function name.
90 '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
91 protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
93 :param name: The name to map.
94 :param protect: If true, avoid returning certain ticklish identifiers
95 (like C keywords) by prepending ``q_``.
97 # ANSI X3J11/88-090, 3.1.1
98 c89_words
= set(['auto', 'break', 'case', 'char', 'const', 'continue',
99 'default', 'do', 'double', 'else', 'enum', 'extern',
100 'float', 'for', 'goto', 'if', 'int', 'long', 'register',
101 'return', 'short', 'signed', 'sizeof', 'static',
102 'struct', 'switch', 'typedef', 'union', 'unsigned',
103 'void', 'volatile', 'while'])
104 # ISO/IEC 9899:1999, 6.4.1
105 c99_words
= set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
106 # ISO/IEC 9899:2011, 6.4.1
107 c11_words
= set(['_Alignas', '_Alignof', '_Atomic', '_Generic',
108 '_Noreturn', '_Static_assert', '_Thread_local'])
109 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
111 gcc_words
= set(['asm', 'typeof'])
112 # C++ ISO/IEC 14882:2003 2.11
113 cpp_words
= set(['bool', 'catch', 'class', 'const_cast', 'delete',
114 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
115 'namespace', 'new', 'operator', 'private', 'protected',
116 'public', 'reinterpret_cast', 'static_cast', 'template',
117 'this', 'throw', 'true', 'try', 'typeid', 'typename',
118 'using', 'virtual', 'wchar_t',
119 # alternative representations
120 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
121 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
122 # namespace pollution:
123 polluted_words
= set(['unix', 'errno', 'mips', 'sparc', 'i386', 'linux'])
124 name
= re
.sub(r
'[^A-Za-z0-9_]', '_', name
)
125 if protect
and (name
in (c89_words | c99_words | c11_words | gcc_words
126 | cpp_words | polluted_words
)
127 or name
[0].isdigit()):
134 Indentation level management.
136 :param initial: Initial number of spaces, default 0.
138 def __init__(self
, initial
: int = 0) -> None:
139 self
._level
= initial
141 def __repr__(self
) -> str:
142 return "{}({:d})".format(type(self
).__name
__, self
._level
)
144 def __str__(self
) -> str:
145 """Return the current indentation as a string of spaces."""
146 return ' ' * self
._level
148 def increase(self
, amount
: int = 4) -> None:
149 """Increase the indentation level by ``amount``, default 4."""
150 self
._level
+= amount
152 def decrease(self
, amount
: int = 4) -> None:
153 """Decrease the indentation level by ``amount``, default 4."""
154 assert amount
<= self
._level
155 self
._level
-= amount
158 #: Global, current indent level for code generation.
159 indent
= Indentation()
162 def cgen(code
: str, **kwds
: object) -> str:
164 Generate ``code`` with ``kwds`` interpolated.
166 Obey `indent`, and strip `EATSPACE`.
171 raw
= re
.sub(r
'^(?!(#|$))', pfx
, raw
, flags
=re
.MULTILINE
)
172 return re
.sub(re
.escape(EATSPACE
) + r
' *', '', raw
)
175 def mcgen(code
: str, **kwds
: object) -> str:
178 return cgen(code
, **kwds
)
181 def c_fname(filename
: str) -> str:
182 return re
.sub(r
'[^A-Za-z0-9_]', '_', filename
)
185 def guardstart(name
: str) -> str:
191 name
=c_fname(name
).upper())
194 def guardend(name
: str) -> str:
197 #endif /* %(name)s */
199 name
=c_fname(name
).upper())
202 def gen_ifcond(ifcond
: Optional
[Union
[str, Dict
[str, Any
]]],
203 cond_fmt
: str, not_fmt
: str,
204 all_operator
: str, any_operator
: str) -> str:
206 def do_gen(ifcond
: Union
[str, Dict
[str, Any
]],
207 need_parens
: bool) -> str:
208 if isinstance(ifcond
, str):
209 return cond_fmt
% ifcond
210 assert isinstance(ifcond
, dict) and len(ifcond
) == 1
212 return not_fmt
% do_gen(ifcond
['not'], True)
214 gen
= gen_infix(all_operator
, ifcond
['all'])
216 gen
= gen_infix(any_operator
, ifcond
['any'])
218 gen
= '(' + gen
+ ')'
221 def gen_infix(operator
: str, operands
: Sequence
[Any
]) -> str:
222 return operator
.join([do_gen(o
, True) for o
in operands
])
226 return do_gen(ifcond
, False)
229 def cgen_ifcond(ifcond
: Optional
[Union
[str, Dict
[str, Any
]]]) -> str:
230 return gen_ifcond(ifcond
, 'defined(%s)', '!%s', ' && ', ' || ')
233 def docgen_ifcond(ifcond
: Optional
[Union
[str, Dict
[str, Any
]]]) -> str:
234 # TODO Doc generated for conditions needs polish
235 return gen_ifcond(ifcond
, '%s', 'not %s', ' and ', ' or ')
238 def gen_if(cond
: str) -> str:
246 def gen_endif(cond
: str) -> str:
250 #endif /* %(cond)s */
254 def must_match(pattern
: str, string
: str) -> Match
[str]:
255 match
= re
.match(pattern
, string
)
256 assert match
is not None