Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 1 | # |
| 2 | # QAPI helper library |
| 3 | # |
| 4 | # Copyright IBM, Corp. 2011 |
Markus Armbruster | 47a6ea9 | 2018-02-26 13:19:40 -0600 | [diff] [blame] | 5 | # Copyright (c) 2013-2018 Red Hat Inc. |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 6 | # |
| 7 | # Authors: |
| 8 | # Anthony Liguori <aliguori@us.ibm.com> |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 9 | # Markus Armbruster <armbru@redhat.com> |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 10 | # |
Markus Armbruster | 678e48a | 2014-03-01 08:40:34 +0100 | [diff] [blame] | 11 | # This work is licensed under the terms of the GNU GPL, version 2. |
| 12 | # See the COPYING file in the top-level directory. |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 13 | |
Markus Armbruster | c261394 | 2017-03-15 13:57:35 +0100 | [diff] [blame] | 14 | import re |
John Snow | d646b2a | 2020-10-09 12:15:37 -0400 | [diff] [blame] | 15 | from typing import Optional, Sequence |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 16 | |
Eric Blake | 437db25 | 2015-09-29 16:21:02 -0600 | [diff] [blame] | 17 | |
John Snow | 1cc7398 | 2020-10-09 12:15:38 -0400 | [diff] [blame] | 18 | #: Magic string that gets removed along with all space to its right. |
John Snow | a7aa64a | 2020-10-09 12:15:34 -0400 | [diff] [blame] | 19 | EATSPACE = '\033EATSPACE.' |
| 20 | POINTER_SUFFIX = ' *' + EATSPACE |
| 21 | _C_NAME_TRANS = str.maketrans('.-', '__') |
| 22 | |
| 23 | |
John Snow | d646b2a | 2020-10-09 12:15:37 -0400 | [diff] [blame] | 24 | def camel_to_upper(value: str) -> str: |
John Snow | 1cc7398 | 2020-10-09 12:15:38 -0400 | [diff] [blame] | 25 | """ |
| 26 | Converts CamelCase to CAMEL_CASE. |
| 27 | |
| 28 | Examples:: |
| 29 | |
| 30 | ENUMName -> ENUM_NAME |
| 31 | EnumName1 -> ENUM_NAME1 |
| 32 | ENUM_NAME -> ENUM_NAME |
| 33 | ENUM_NAME1 -> ENUM_NAME1 |
| 34 | ENUM_Name2 -> ENUM_NAME2 |
| 35 | ENUM24_Name -> ENUM24_NAME |
| 36 | """ |
Markus Armbruster | 849bc53 | 2015-05-14 06:50:53 -0600 | [diff] [blame] | 37 | c_fun_str = c_name(value, False) |
| 38 | if value.isupper(): |
| 39 | return c_fun_str |
| 40 | |
| 41 | new_name = '' |
Markus Armbruster | b736e25 | 2018-06-21 10:35:51 +0200 | [diff] [blame] | 42 | length = len(c_fun_str) |
| 43 | for i in range(length): |
John Snow | 7395171 | 2020-10-09 12:15:35 -0400 | [diff] [blame] | 44 | char = c_fun_str[i] |
| 45 | # When char is upper case and no '_' appears before, do more checks |
| 46 | if char.isupper() and (i > 0) and c_fun_str[i - 1] != '_': |
Markus Armbruster | b736e25 | 2018-06-21 10:35:51 +0200 | [diff] [blame] | 47 | if i < length - 1 and c_fun_str[i + 1].islower(): |
Eric Blake | 437db25 | 2015-09-29 16:21:02 -0600 | [diff] [blame] | 48 | new_name += '_' |
| 49 | elif c_fun_str[i - 1].isdigit(): |
Markus Armbruster | 849bc53 | 2015-05-14 06:50:53 -0600 | [diff] [blame] | 50 | new_name += '_' |
John Snow | 7395171 | 2020-10-09 12:15:35 -0400 | [diff] [blame] | 51 | new_name += char |
Markus Armbruster | 849bc53 | 2015-05-14 06:50:53 -0600 | [diff] [blame] | 52 | return new_name.lstrip('_').upper() |
| 53 | |
Eric Blake | 437db25 | 2015-09-29 16:21:02 -0600 | [diff] [blame] | 54 | |
John Snow | d646b2a | 2020-10-09 12:15:37 -0400 | [diff] [blame] | 55 | def c_enum_const(type_name: str, |
| 56 | const_name: str, |
| 57 | prefix: Optional[str] = None) -> str: |
John Snow | 1cc7398 | 2020-10-09 12:15:38 -0400 | [diff] [blame] | 58 | """ |
| 59 | Generate a C enumeration constant name. |
| 60 | |
| 61 | :param type_name: The name of the enumeration. |
| 62 | :param const_name: The name of this constant. |
| 63 | :param prefix: Optional, prefix that overrides the type_name. |
| 64 | """ |
Daniel P. Berrange | 351d36e | 2015-08-26 14:21:20 +0100 | [diff] [blame] | 65 | if prefix is not None: |
| 66 | type_name = prefix |
Eric Blake | d20a580 | 2015-11-18 01:53:01 -0700 | [diff] [blame] | 67 | return camel_to_upper(type_name) + '_' + c_name(const_name, False).upper() |
Markus Armbruster | 849bc53 | 2015-05-14 06:50:53 -0600 | [diff] [blame] | 68 | |
Markus Armbruster | b736e25 | 2018-06-21 10:35:51 +0200 | [diff] [blame] | 69 | |
John Snow | d646b2a | 2020-10-09 12:15:37 -0400 | [diff] [blame] | 70 | def c_name(name: str, protect: bool = True) -> str: |
John Snow | 1cc7398 | 2020-10-09 12:15:38 -0400 | [diff] [blame] | 71 | """ |
| 72 | Map ``name`` to a valid C identifier. |
| 73 | |
| 74 | Used for converting 'name' from a 'name':'type' qapi definition |
| 75 | into a generated struct member, as well as converting type names |
| 76 | into substrings of a generated C function name. |
| 77 | |
| 78 | '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo' |
| 79 | protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int' |
| 80 | |
| 81 | :param name: The name to map. |
| 82 | :param protect: If true, avoid returning certain ticklish identifiers |
| 83 | (like C keywords) by prepending ``q_``. |
| 84 | """ |
Blue Swirl | 427a1a2 | 2012-07-30 15:46:55 +0000 | [diff] [blame] | 85 | # ANSI X3J11/88-090, 3.1.1 |
| 86 | c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue', |
Eric Blake | 437db25 | 2015-09-29 16:21:02 -0600 | [diff] [blame] | 87 | 'default', 'do', 'double', 'else', 'enum', 'extern', |
| 88 | 'float', 'for', 'goto', 'if', 'int', 'long', 'register', |
| 89 | 'return', 'short', 'signed', 'sizeof', 'static', |
| 90 | 'struct', 'switch', 'typedef', 'union', 'unsigned', |
| 91 | 'void', 'volatile', 'while']) |
Blue Swirl | 427a1a2 | 2012-07-30 15:46:55 +0000 | [diff] [blame] | 92 | # ISO/IEC 9899:1999, 6.4.1 |
| 93 | c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary']) |
| 94 | # ISO/IEC 9899:2011, 6.4.1 |
Eric Blake | 437db25 | 2015-09-29 16:21:02 -0600 | [diff] [blame] | 95 | c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', |
| 96 | '_Noreturn', '_Static_assert', '_Thread_local']) |
Blue Swirl | 427a1a2 | 2012-07-30 15:46:55 +0000 | [diff] [blame] | 97 | # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html |
| 98 | # excluding _.* |
| 99 | gcc_words = set(['asm', 'typeof']) |
Tomoki Sekiyama | 6f88009 | 2013-08-07 11:39:43 -0400 | [diff] [blame] | 100 | # C++ ISO/IEC 14882:2003 2.11 |
| 101 | cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete', |
| 102 | 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable', |
| 103 | 'namespace', 'new', 'operator', 'private', 'protected', |
| 104 | 'public', 'reinterpret_cast', 'static_cast', 'template', |
| 105 | 'this', 'throw', 'true', 'try', 'typeid', 'typename', |
| 106 | 'using', 'virtual', 'wchar_t', |
| 107 | # alternative representations |
| 108 | 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not', |
| 109 | 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq']) |
Paolo Bonzini | 1057725 | 2012-09-19 16:31:07 +0200 | [diff] [blame] | 110 | # namespace pollution: |
Laszlo Ersek | 9a801c7 | 2018-04-27 21:28:49 +0200 | [diff] [blame] | 111 | polluted_words = set(['unix', 'errno', 'mips', 'sparc', 'i386']) |
John Snow | a7aa64a | 2020-10-09 12:15:34 -0400 | [diff] [blame] | 112 | name = name.translate(_C_NAME_TRANS) |
Eric Blake | 437db25 | 2015-09-29 16:21:02 -0600 | [diff] [blame] | 113 | if protect and (name in c89_words | c99_words | c11_words | gcc_words |
| 114 | | cpp_words | polluted_words): |
Markus Armbruster | ef801a9 | 2017-03-15 13:57:08 +0100 | [diff] [blame] | 115 | return 'q_' + name |
Eric Blake | c43567c | 2015-11-18 01:52:52 -0700 | [diff] [blame] | 116 | return name |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 117 | |
Markus Armbruster | b736e25 | 2018-06-21 10:35:51 +0200 | [diff] [blame] | 118 | |
John Snow | cbe8f87 | 2020-10-09 12:15:33 -0400 | [diff] [blame] | 119 | class Indentation: |
| 120 | """ |
| 121 | Indentation level management. |
| 122 | |
| 123 | :param initial: Initial number of spaces, default 0. |
| 124 | """ |
| 125 | def __init__(self, initial: int = 0) -> None: |
| 126 | self._level = initial |
| 127 | |
| 128 | def __int__(self) -> int: |
| 129 | return self._level |
| 130 | |
| 131 | def __repr__(self) -> str: |
| 132 | return "{}({:d})".format(type(self).__name__, self._level) |
| 133 | |
| 134 | def __str__(self) -> str: |
| 135 | """Return the current indentation as a string of spaces.""" |
| 136 | return ' ' * self._level |
| 137 | |
| 138 | def __bool__(self) -> bool: |
| 139 | """True when there is a non-zero indentation.""" |
| 140 | return bool(self._level) |
| 141 | |
| 142 | def increase(self, amount: int = 4) -> None: |
| 143 | """Increase the indentation level by ``amount``, default 4.""" |
| 144 | self._level += amount |
| 145 | |
| 146 | def decrease(self, amount: int = 4) -> None: |
| 147 | """Decrease the indentation level by ``amount``, default 4.""" |
| 148 | if self._level < amount: |
| 149 | raise ArithmeticError( |
| 150 | f"Can't remove {amount:d} spaces from {self!r}") |
| 151 | self._level -= amount |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 152 | |
Markus Armbruster | b736e25 | 2018-06-21 10:35:51 +0200 | [diff] [blame] | 153 | |
John Snow | 1cc7398 | 2020-10-09 12:15:38 -0400 | [diff] [blame] | 154 | #: Global, current indent level for code generation. |
John Snow | cbe8f87 | 2020-10-09 12:15:33 -0400 | [diff] [blame] | 155 | indent = Indentation() |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 156 | |
Eric Blake | 437db25 | 2015-09-29 16:21:02 -0600 | [diff] [blame] | 157 | |
John Snow | d646b2a | 2020-10-09 12:15:37 -0400 | [diff] [blame] | 158 | def cgen(code: str, **kwds: object) -> str: |
John Snow | 1cc7398 | 2020-10-09 12:15:38 -0400 | [diff] [blame] | 159 | """ |
| 160 | Generate ``code`` with ``kwds`` interpolated. |
| 161 | |
| 162 | Obey `indent`, and strip `EATSPACE`. |
| 163 | """ |
Markus Armbruster | 77e703b | 2015-06-24 19:27:32 +0200 | [diff] [blame] | 164 | raw = code % kwds |
John Snow | cbe8f87 | 2020-10-09 12:15:33 -0400 | [diff] [blame] | 165 | if indent: |
| 166 | raw = re.sub(r'^(?!(#|$))', str(indent), raw, flags=re.MULTILINE) |
John Snow | a7aa64a | 2020-10-09 12:15:34 -0400 | [diff] [blame] | 167 | return re.sub(re.escape(EATSPACE) + r' *', '', raw) |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 168 | |
Eric Blake | 437db25 | 2015-09-29 16:21:02 -0600 | [diff] [blame] | 169 | |
John Snow | d646b2a | 2020-10-09 12:15:37 -0400 | [diff] [blame] | 170 | def mcgen(code: str, **kwds: object) -> str: |
Markus Armbruster | 77e703b | 2015-06-24 19:27:32 +0200 | [diff] [blame] | 171 | if code[0] == '\n': |
| 172 | code = code[1:] |
| 173 | return cgen(code, **kwds) |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 174 | |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 175 | |
John Snow | d646b2a | 2020-10-09 12:15:37 -0400 | [diff] [blame] | 176 | def c_fname(filename: str) -> str: |
Markus Armbruster | 709395f | 2019-03-01 16:40:48 +0100 | [diff] [blame] | 177 | return re.sub(r'[^A-Za-z0-9_]', '_', filename) |
Michael Roth | c0afa9c | 2013-05-10 17:46:00 -0500 | [diff] [blame] | 178 | |
Eric Blake | 437db25 | 2015-09-29 16:21:02 -0600 | [diff] [blame] | 179 | |
John Snow | d646b2a | 2020-10-09 12:15:37 -0400 | [diff] [blame] | 180 | def guardstart(name: str) -> str: |
Michael Roth | c0afa9c | 2013-05-10 17:46:00 -0500 | [diff] [blame] | 181 | return mcgen(''' |
Michael Roth | c0afa9c | 2013-05-10 17:46:00 -0500 | [diff] [blame] | 182 | #ifndef %(name)s |
| 183 | #define %(name)s |
| 184 | |
| 185 | ''', |
Markus Armbruster | 709395f | 2019-03-01 16:40:48 +0100 | [diff] [blame] | 186 | name=c_fname(name).upper()) |
Michael Roth | c0afa9c | 2013-05-10 17:46:00 -0500 | [diff] [blame] | 187 | |
Eric Blake | 437db25 | 2015-09-29 16:21:02 -0600 | [diff] [blame] | 188 | |
John Snow | d646b2a | 2020-10-09 12:15:37 -0400 | [diff] [blame] | 189 | def guardend(name: str) -> str: |
Michael Roth | c0afa9c | 2013-05-10 17:46:00 -0500 | [diff] [blame] | 190 | return mcgen(''' |
| 191 | |
| 192 | #endif /* %(name)s */ |
Michael Roth | c0afa9c | 2013-05-10 17:46:00 -0500 | [diff] [blame] | 193 | ''', |
Markus Armbruster | 709395f | 2019-03-01 16:40:48 +0100 | [diff] [blame] | 194 | name=c_fname(name).upper()) |
Markus Armbruster | 2114f5a | 2015-04-02 13:12:21 +0200 | [diff] [blame] | 195 | |
Eric Blake | 437db25 | 2015-09-29 16:21:02 -0600 | [diff] [blame] | 196 | |
John Snow | d646b2a | 2020-10-09 12:15:37 -0400 | [diff] [blame] | 197 | def gen_if(ifcond: Sequence[str]) -> str: |
Marc-André Lureau | ded9fc2 | 2018-07-03 17:56:40 +0200 | [diff] [blame] | 198 | ret = '' |
| 199 | for ifc in ifcond: |
| 200 | ret += mcgen(''' |
| 201 | #if %(cond)s |
| 202 | ''', cond=ifc) |
| 203 | return ret |
| 204 | |
| 205 | |
John Snow | d646b2a | 2020-10-09 12:15:37 -0400 | [diff] [blame] | 206 | def gen_endif(ifcond: Sequence[str]) -> str: |
Marc-André Lureau | ded9fc2 | 2018-07-03 17:56:40 +0200 | [diff] [blame] | 207 | ret = '' |
| 208 | for ifc in reversed(ifcond): |
| 209 | ret += mcgen(''' |
| 210 | #endif /* %(cond)s */ |
| 211 | ''', cond=ifc) |
| 212 | return ret |