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 | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 5 | # Copyright (c) 2013 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 | # |
| 11 | # This work is licensed under the terms of the GNU GPLv2. |
| 12 | # See the COPYING.LIB file in the top-level directory. |
| 13 | |
| 14 | from ordereddict import OrderedDict |
Markus Armbruster | 2caba36 | 2013-07-27 17:41:56 +0200 | [diff] [blame] | 15 | import sys |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 16 | |
Michael Roth | c0afa9c | 2013-05-10 17:46:00 -0500 | [diff] [blame] | 17 | builtin_types = [ |
| 18 | 'str', 'int', 'number', 'bool', |
| 19 | 'int8', 'int16', 'int32', 'int64', |
| 20 | 'uint8', 'uint16', 'uint32', 'uint64' |
| 21 | ] |
| 22 | |
Kevin Wolf | 69dd62d | 2013-07-08 16:14:21 +0200 | [diff] [blame] | 23 | builtin_type_qtypes = { |
| 24 | 'str': 'QTYPE_QSTRING', |
| 25 | 'int': 'QTYPE_QINT', |
| 26 | 'number': 'QTYPE_QFLOAT', |
| 27 | 'bool': 'QTYPE_QBOOL', |
| 28 | 'int8': 'QTYPE_QINT', |
| 29 | 'int16': 'QTYPE_QINT', |
| 30 | 'int32': 'QTYPE_QINT', |
| 31 | 'int64': 'QTYPE_QINT', |
| 32 | 'uint8': 'QTYPE_QINT', |
| 33 | 'uint16': 'QTYPE_QINT', |
| 34 | 'uint32': 'QTYPE_QINT', |
| 35 | 'uint64': 'QTYPE_QINT', |
| 36 | } |
| 37 | |
Markus Armbruster | 2caba36 | 2013-07-27 17:41:56 +0200 | [diff] [blame] | 38 | class QAPISchemaError(Exception): |
| 39 | def __init__(self, schema, msg): |
| 40 | self.fp = schema.fp |
| 41 | self.msg = msg |
| 42 | self.line = self.col = 1 |
| 43 | for ch in schema.src[0:schema.pos]: |
| 44 | if ch == '\n': |
| 45 | self.line += 1 |
| 46 | self.col = 1 |
| 47 | elif ch == '\t': |
| 48 | self.col = (self.col + 7) % 8 + 1 |
| 49 | else: |
| 50 | self.col += 1 |
| 51 | |
| 52 | def __str__(self): |
| 53 | return "%s:%s:%s: %s" % (self.fp.name, self.line, self.col, self.msg) |
| 54 | |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 55 | class QAPISchema: |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 56 | |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 57 | def __init__(self, fp): |
| 58 | self.fp = fp |
| 59 | self.src = fp.read() |
| 60 | if self.src == '' or self.src[-1] != '\n': |
| 61 | self.src += '\n' |
| 62 | self.cursor = 0 |
| 63 | self.exprs = [] |
| 64 | self.accept() |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 65 | |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 66 | while self.tok != None: |
Markus Armbruster | 5f3cd2b | 2013-07-27 17:41:59 +0200 | [diff] [blame^] | 67 | self.exprs.append(self.get_expr(False)) |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 68 | |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 69 | def accept(self): |
| 70 | while True: |
| 71 | bol = self.cursor == 0 or self.src[self.cursor-1] == '\n' |
| 72 | self.tok = self.src[self.cursor] |
Markus Armbruster | 2caba36 | 2013-07-27 17:41:56 +0200 | [diff] [blame] | 73 | self.pos = self.cursor |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 74 | self.cursor += 1 |
| 75 | self.val = None |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 76 | |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 77 | if self.tok == '#' and bol: |
| 78 | self.cursor = self.src.find('\n', self.cursor) |
| 79 | elif self.tok in ['{', '}', ':', ',', '[', ']']: |
| 80 | return |
| 81 | elif self.tok == "'": |
| 82 | string = '' |
| 83 | esc = False |
| 84 | while True: |
| 85 | ch = self.src[self.cursor] |
| 86 | self.cursor += 1 |
| 87 | if ch == '\n': |
Markus Armbruster | 2caba36 | 2013-07-27 17:41:56 +0200 | [diff] [blame] | 88 | raise QAPISchemaError(self, |
| 89 | 'Missing terminating "\'"') |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 90 | if esc: |
| 91 | string += ch |
| 92 | esc = False |
| 93 | elif ch == "\\": |
| 94 | esc = True |
| 95 | elif ch == "'": |
| 96 | self.val = string |
| 97 | return |
| 98 | else: |
| 99 | string += ch |
| 100 | elif self.tok == '\n': |
| 101 | if self.cursor == len(self.src): |
| 102 | self.tok = None |
| 103 | return |
Markus Armbruster | 9213aa5 | 2013-07-27 17:41:57 +0200 | [diff] [blame] | 104 | elif not self.tok.isspace(): |
| 105 | raise QAPISchemaError(self, 'Stray "%s"' % self.tok) |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 106 | |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 107 | def get_members(self): |
| 108 | expr = OrderedDict() |
Markus Armbruster | 6974ccd | 2013-07-27 17:41:58 +0200 | [diff] [blame] | 109 | if self.tok == '}': |
| 110 | self.accept() |
| 111 | return expr |
| 112 | if self.tok != "'": |
| 113 | raise QAPISchemaError(self, 'Expected string or "}"') |
| 114 | while True: |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 115 | key = self.val |
| 116 | self.accept() |
Markus Armbruster | 6974ccd | 2013-07-27 17:41:58 +0200 | [diff] [blame] | 117 | if self.tok != ':': |
| 118 | raise QAPISchemaError(self, 'Expected ":"') |
| 119 | self.accept() |
Markus Armbruster | 5f3cd2b | 2013-07-27 17:41:59 +0200 | [diff] [blame^] | 120 | expr[key] = self.get_expr(True) |
Markus Armbruster | 6974ccd | 2013-07-27 17:41:58 +0200 | [diff] [blame] | 121 | if self.tok == '}': |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 122 | self.accept() |
Markus Armbruster | 6974ccd | 2013-07-27 17:41:58 +0200 | [diff] [blame] | 123 | return expr |
| 124 | if self.tok != ',': |
| 125 | raise QAPISchemaError(self, 'Expected "," or "}"') |
| 126 | self.accept() |
| 127 | if self.tok != "'": |
| 128 | raise QAPISchemaError(self, 'Expected string') |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 129 | |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 130 | def get_values(self): |
| 131 | expr = [] |
Markus Armbruster | 6974ccd | 2013-07-27 17:41:58 +0200 | [diff] [blame] | 132 | if self.tok == ']': |
| 133 | self.accept() |
| 134 | return expr |
| 135 | if not self.tok in [ '{', '[', "'" ]: |
| 136 | raise QAPISchemaError(self, 'Expected "{", "[", "]" or string') |
| 137 | while True: |
Markus Armbruster | 5f3cd2b | 2013-07-27 17:41:59 +0200 | [diff] [blame^] | 138 | expr.append(self.get_expr(True)) |
Markus Armbruster | 6974ccd | 2013-07-27 17:41:58 +0200 | [diff] [blame] | 139 | if self.tok == ']': |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 140 | self.accept() |
Markus Armbruster | 6974ccd | 2013-07-27 17:41:58 +0200 | [diff] [blame] | 141 | return expr |
| 142 | if self.tok != ',': |
| 143 | raise QAPISchemaError(self, 'Expected "," or "]"') |
| 144 | self.accept() |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 145 | |
Markus Armbruster | 5f3cd2b | 2013-07-27 17:41:59 +0200 | [diff] [blame^] | 146 | def get_expr(self, nested): |
| 147 | if self.tok != '{' and not nested: |
| 148 | raise QAPISchemaError(self, 'Expected "{"') |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 149 | if self.tok == '{': |
| 150 | self.accept() |
| 151 | expr = self.get_members() |
| 152 | elif self.tok == '[': |
| 153 | self.accept() |
| 154 | expr = self.get_values() |
Markus Armbruster | 6974ccd | 2013-07-27 17:41:58 +0200 | [diff] [blame] | 155 | elif self.tok == "'": |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 156 | expr = self.val |
| 157 | self.accept() |
Markus Armbruster | 6974ccd | 2013-07-27 17:41:58 +0200 | [diff] [blame] | 158 | else: |
| 159 | raise QAPISchemaError(self, 'Expected "{", "[" or string') |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 160 | return expr |
Kevin Wolf | bd9927f | 2013-07-01 16:31:50 +0200 | [diff] [blame] | 161 | |
| 162 | def parse_schema(fp): |
Markus Armbruster | 2caba36 | 2013-07-27 17:41:56 +0200 | [diff] [blame] | 163 | try: |
| 164 | schema = QAPISchema(fp) |
| 165 | except QAPISchemaError as e: |
| 166 | print >>sys.stderr, e |
| 167 | exit(1) |
| 168 | |
Kevin Wolf | bd9927f | 2013-07-01 16:31:50 +0200 | [diff] [blame] | 169 | exprs = [] |
| 170 | |
Markus Armbruster | c7a3f25 | 2013-07-27 17:41:55 +0200 | [diff] [blame] | 171 | for expr_eval in schema.exprs: |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 172 | if expr_eval.has_key('enum'): |
| 173 | add_enum(expr_eval['enum']) |
| 174 | elif expr_eval.has_key('union'): |
Kevin Wolf | ea66c6d | 2013-07-16 10:49:41 +0200 | [diff] [blame] | 175 | add_union(expr_eval) |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 176 | add_enum('%sKind' % expr_eval['union']) |
Kevin Wolf | b35284e | 2013-07-01 16:31:51 +0200 | [diff] [blame] | 177 | elif expr_eval.has_key('type'): |
| 178 | add_struct(expr_eval) |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 179 | exprs.append(expr_eval) |
| 180 | |
| 181 | return exprs |
| 182 | |
| 183 | def parse_args(typeinfo): |
Kevin Wolf | b35284e | 2013-07-01 16:31:51 +0200 | [diff] [blame] | 184 | if isinstance(typeinfo, basestring): |
| 185 | struct = find_struct(typeinfo) |
| 186 | assert struct != None |
| 187 | typeinfo = struct['data'] |
| 188 | |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 189 | for member in typeinfo: |
| 190 | argname = member |
| 191 | argentry = typeinfo[member] |
| 192 | optional = False |
| 193 | structured = False |
| 194 | if member.startswith('*'): |
| 195 | argname = member[1:] |
| 196 | optional = True |
| 197 | if isinstance(argentry, OrderedDict): |
| 198 | structured = True |
| 199 | yield (argname, argentry, optional, structured) |
| 200 | |
| 201 | def de_camel_case(name): |
| 202 | new_name = '' |
| 203 | for ch in name: |
| 204 | if ch.isupper() and new_name: |
| 205 | new_name += '_' |
| 206 | if ch == '-': |
| 207 | new_name += '_' |
| 208 | else: |
| 209 | new_name += ch.lower() |
| 210 | return new_name |
| 211 | |
| 212 | def camel_case(name): |
| 213 | new_name = '' |
| 214 | first = True |
| 215 | for ch in name: |
| 216 | if ch in ['_', '-']: |
| 217 | first = True |
| 218 | elif first: |
| 219 | new_name += ch.upper() |
| 220 | first = False |
| 221 | else: |
| 222 | new_name += ch.lower() |
| 223 | return new_name |
| 224 | |
Paolo Bonzini | eda50a6 | 2012-09-19 16:31:06 +0200 | [diff] [blame] | 225 | def c_var(name, protect=True): |
Blue Swirl | 427a1a2 | 2012-07-30 15:46:55 +0000 | [diff] [blame] | 226 | # ANSI X3J11/88-090, 3.1.1 |
| 227 | c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue', |
| 228 | 'default', 'do', 'double', 'else', 'enum', 'extern', 'float', |
| 229 | 'for', 'goto', 'if', 'int', 'long', 'register', 'return', |
| 230 | 'short', 'signed', 'sizeof', 'static', 'struct', 'switch', |
| 231 | 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while']) |
| 232 | # ISO/IEC 9899:1999, 6.4.1 |
| 233 | c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary']) |
| 234 | # ISO/IEC 9899:2011, 6.4.1 |
| 235 | c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn', |
| 236 | '_Static_assert', '_Thread_local']) |
| 237 | # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html |
| 238 | # excluding _.* |
| 239 | gcc_words = set(['asm', 'typeof']) |
Paolo Bonzini | 1057725 | 2012-09-19 16:31:07 +0200 | [diff] [blame] | 240 | # namespace pollution: |
| 241 | polluted_words = set(['unix']) |
| 242 | if protect and (name in c89_words | c99_words | c11_words | gcc_words | polluted_words): |
Blue Swirl | 427a1a2 | 2012-07-30 15:46:55 +0000 | [diff] [blame] | 243 | return "q_" + name |
Federico Simoncelli | c9da228 | 2012-03-20 13:54:35 +0000 | [diff] [blame] | 244 | return name.replace('-', '_').lstrip("*") |
| 245 | |
Paolo Bonzini | eda50a6 | 2012-09-19 16:31:06 +0200 | [diff] [blame] | 246 | def c_fun(name, protect=True): |
| 247 | return c_var(name, protect).replace('.', '_') |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 248 | |
| 249 | def c_list_type(name): |
| 250 | return '%sList' % name |
| 251 | |
| 252 | def type_name(name): |
| 253 | if type(name) == list: |
| 254 | return c_list_type(name[0]) |
| 255 | return name |
| 256 | |
| 257 | enum_types = [] |
Kevin Wolf | b35284e | 2013-07-01 16:31:51 +0200 | [diff] [blame] | 258 | struct_types = [] |
Kevin Wolf | ea66c6d | 2013-07-16 10:49:41 +0200 | [diff] [blame] | 259 | union_types = [] |
Kevin Wolf | b35284e | 2013-07-01 16:31:51 +0200 | [diff] [blame] | 260 | |
| 261 | def add_struct(definition): |
| 262 | global struct_types |
| 263 | struct_types.append(definition) |
| 264 | |
| 265 | def find_struct(name): |
| 266 | global struct_types |
| 267 | for struct in struct_types: |
| 268 | if struct['type'] == name: |
| 269 | return struct |
| 270 | return None |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 271 | |
Kevin Wolf | ea66c6d | 2013-07-16 10:49:41 +0200 | [diff] [blame] | 272 | def add_union(definition): |
| 273 | global union_types |
| 274 | union_types.append(definition) |
| 275 | |
| 276 | def find_union(name): |
| 277 | global union_types |
| 278 | for union in union_types: |
| 279 | if union['union'] == name: |
| 280 | return union |
| 281 | return None |
| 282 | |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 283 | def add_enum(name): |
| 284 | global enum_types |
| 285 | enum_types.append(name) |
| 286 | |
| 287 | def is_enum(name): |
| 288 | global enum_types |
| 289 | return (name in enum_types) |
| 290 | |
| 291 | def c_type(name): |
| 292 | if name == 'str': |
| 293 | return 'char *' |
| 294 | elif name == 'int': |
| 295 | return 'int64_t' |
Laszlo Ersek | c46f18c | 2012-07-17 16:17:06 +0200 | [diff] [blame] | 296 | elif (name == 'int8' or name == 'int16' or name == 'int32' or |
| 297 | name == 'int64' or name == 'uint8' or name == 'uint16' or |
| 298 | name == 'uint32' or name == 'uint64'): |
| 299 | return name + '_t' |
Laszlo Ersek | 092705d | 2012-07-17 16:17:07 +0200 | [diff] [blame] | 300 | elif name == 'size': |
| 301 | return 'uint64_t' |
Michael Roth | 0f923be | 2011-07-19 14:50:39 -0500 | [diff] [blame] | 302 | elif name == 'bool': |
| 303 | return 'bool' |
| 304 | elif name == 'number': |
| 305 | return 'double' |
| 306 | elif type(name) == list: |
| 307 | return '%s *' % c_list_type(name[0]) |
| 308 | elif is_enum(name): |
| 309 | return name |
| 310 | elif name == None or len(name) == 0: |
| 311 | return 'void' |
| 312 | elif name == name.upper(): |
| 313 | return '%sEvent *' % camel_case(name) |
| 314 | else: |
| 315 | return '%s *' % name |
| 316 | |
| 317 | def genindent(count): |
| 318 | ret = "" |
| 319 | for i in range(count): |
| 320 | ret += " " |
| 321 | return ret |
| 322 | |
| 323 | indent_level = 0 |
| 324 | |
| 325 | def push_indent(indent_amount=4): |
| 326 | global indent_level |
| 327 | indent_level += indent_amount |
| 328 | |
| 329 | def pop_indent(indent_amount=4): |
| 330 | global indent_level |
| 331 | indent_level -= indent_amount |
| 332 | |
| 333 | def cgen(code, **kwds): |
| 334 | indent = genindent(indent_level) |
| 335 | lines = code.split('\n') |
| 336 | lines = map(lambda x: indent + x, lines) |
| 337 | return '\n'.join(lines) % kwds + '\n' |
| 338 | |
| 339 | def mcgen(code, **kwds): |
| 340 | return cgen('\n'.join(code.split('\n')[1:-1]), **kwds) |
| 341 | |
| 342 | def basename(filename): |
| 343 | return filename.split("/")[-1] |
| 344 | |
| 345 | def guardname(filename): |
Michael Roth | d8e1f21 | 2011-11-29 16:47:48 -0600 | [diff] [blame] | 346 | guard = basename(filename).rsplit(".", 1)[0] |
| 347 | for substr in [".", " ", "-"]: |
| 348 | guard = guard.replace(substr, "_") |
| 349 | return guard.upper() + '_H' |
Michael Roth | c0afa9c | 2013-05-10 17:46:00 -0500 | [diff] [blame] | 350 | |
| 351 | def guardstart(name): |
| 352 | return mcgen(''' |
| 353 | |
| 354 | #ifndef %(name)s |
| 355 | #define %(name)s |
| 356 | |
| 357 | ''', |
| 358 | name=guardname(name)) |
| 359 | |
| 360 | def guardend(name): |
| 361 | return mcgen(''' |
| 362 | |
| 363 | #endif /* %(name)s */ |
| 364 | |
| 365 | ''', |
| 366 | name=guardname(name)) |