qapi: add const prefix to 'char *' insider c_type()
[qemu.git] / scripts / qapi.py
1 #
2 # QAPI helper library
3 #
4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013 Red Hat Inc.
6 #
7 # Authors:
8 # Anthony Liguori <aliguori@us.ibm.com>
9 # Markus Armbruster <armbru@redhat.com>
10 #
11 # This work is licensed under the terms of the GNU GPL, version 2.
12 # See the COPYING file in the top-level directory.
13
14 import re
15 from ordereddict import OrderedDict
16 import os
17 import sys
18
19 builtin_types = [
20 'str', 'int', 'number', 'bool',
21 'int8', 'int16', 'int32', 'int64',
22 'uint8', 'uint16', 'uint32', 'uint64'
23 ]
24
25 builtin_type_qtypes = {
26 'str': 'QTYPE_QSTRING',
27 'int': 'QTYPE_QINT',
28 'number': 'QTYPE_QFLOAT',
29 'bool': 'QTYPE_QBOOL',
30 'int8': 'QTYPE_QINT',
31 'int16': 'QTYPE_QINT',
32 'int32': 'QTYPE_QINT',
33 'int64': 'QTYPE_QINT',
34 'uint8': 'QTYPE_QINT',
35 'uint16': 'QTYPE_QINT',
36 'uint32': 'QTYPE_QINT',
37 'uint64': 'QTYPE_QINT',
38 }
39
40 def error_path(parent):
41 res = ""
42 while parent:
43 res = ("In file included from %s:%d:\n" % (parent['file'],
44 parent['line'])) + res
45 parent = parent['parent']
46 return res
47
48 class QAPISchemaError(Exception):
49 def __init__(self, schema, msg):
50 self.input_file = schema.input_file
51 self.msg = msg
52 self.col = 1
53 self.line = schema.line
54 for ch in schema.src[schema.line_pos:schema.pos]:
55 if ch == '\t':
56 self.col = (self.col + 7) % 8 + 1
57 else:
58 self.col += 1
59 self.info = schema.parent_info
60
61 def __str__(self):
62 return error_path(self.info) + \
63 "%s:%d:%d: %s" % (self.input_file, self.line, self.col, self.msg)
64
65 class QAPIExprError(Exception):
66 def __init__(self, expr_info, msg):
67 self.info = expr_info
68 self.msg = msg
69
70 def __str__(self):
71 return error_path(self.info['parent']) + \
72 "%s:%d: %s" % (self.info['file'], self.info['line'], self.msg)
73
74 class QAPISchema:
75
76 def __init__(self, fp, input_relname=None, include_hist=[],
77 previously_included=[], parent_info=None):
78 """ include_hist is a stack used to detect inclusion cycles
79 previously_included is a global state used to avoid multiple
80 inclusions of the same file"""
81 input_fname = os.path.abspath(fp.name)
82 if input_relname is None:
83 input_relname = fp.name
84 self.input_dir = os.path.dirname(input_fname)
85 self.input_file = input_relname
86 self.include_hist = include_hist + [(input_relname, input_fname)]
87 previously_included.append(input_fname)
88 self.parent_info = parent_info
89 self.src = fp.read()
90 if self.src == '' or self.src[-1] != '\n':
91 self.src += '\n'
92 self.cursor = 0
93 self.line = 1
94 self.line_pos = 0
95 self.exprs = []
96 self.accept()
97
98 while self.tok != None:
99 expr_info = {'file': input_relname, 'line': self.line, 'parent': self.parent_info}
100 expr = self.get_expr(False)
101 if isinstance(expr, dict) and "include" in expr:
102 if len(expr) != 1:
103 raise QAPIExprError(expr_info, "Invalid 'include' directive")
104 include = expr["include"]
105 if not isinstance(include, str):
106 raise QAPIExprError(expr_info,
107 'Expected a file name (string), got: %s'
108 % include)
109 include_path = os.path.join(self.input_dir, include)
110 if any(include_path == elem[1]
111 for elem in self.include_hist):
112 raise QAPIExprError(expr_info, "Inclusion loop for %s"
113 % include)
114 # skip multiple include of the same file
115 if include_path in previously_included:
116 continue
117 try:
118 fobj = open(include_path, 'r')
119 except IOError, e:
120 raise QAPIExprError(expr_info,
121 '%s: %s' % (e.strerror, include))
122 exprs_include = QAPISchema(fobj, include, self.include_hist,
123 previously_included, expr_info)
124 self.exprs.extend(exprs_include.exprs)
125 else:
126 expr_elem = {'expr': expr,
127 'info': expr_info}
128 self.exprs.append(expr_elem)
129
130 def accept(self):
131 while True:
132 self.tok = self.src[self.cursor]
133 self.pos = self.cursor
134 self.cursor += 1
135 self.val = None
136
137 if self.tok == '#':
138 self.cursor = self.src.find('\n', self.cursor)
139 elif self.tok in ['{', '}', ':', ',', '[', ']']:
140 return
141 elif self.tok == "'":
142 string = ''
143 esc = False
144 while True:
145 ch = self.src[self.cursor]
146 self.cursor += 1
147 if ch == '\n':
148 raise QAPISchemaError(self,
149 'Missing terminating "\'"')
150 if esc:
151 string += ch
152 esc = False
153 elif ch == "\\":
154 esc = True
155 elif ch == "'":
156 self.val = string
157 return
158 else:
159 string += ch
160 elif self.tok == '\n':
161 if self.cursor == len(self.src):
162 self.tok = None
163 return
164 self.line += 1
165 self.line_pos = self.cursor
166 elif not self.tok.isspace():
167 raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
168
169 def get_members(self):
170 expr = OrderedDict()
171 if self.tok == '}':
172 self.accept()
173 return expr
174 if self.tok != "'":
175 raise QAPISchemaError(self, 'Expected string or "}"')
176 while True:
177 key = self.val
178 self.accept()
179 if self.tok != ':':
180 raise QAPISchemaError(self, 'Expected ":"')
181 self.accept()
182 if key in expr:
183 raise QAPISchemaError(self, 'Duplicate key "%s"' % key)
184 expr[key] = self.get_expr(True)
185 if self.tok == '}':
186 self.accept()
187 return expr
188 if self.tok != ',':
189 raise QAPISchemaError(self, 'Expected "," or "}"')
190 self.accept()
191 if self.tok != "'":
192 raise QAPISchemaError(self, 'Expected string')
193
194 def get_values(self):
195 expr = []
196 if self.tok == ']':
197 self.accept()
198 return expr
199 if not self.tok in [ '{', '[', "'" ]:
200 raise QAPISchemaError(self, 'Expected "{", "[", "]" or string')
201 while True:
202 expr.append(self.get_expr(True))
203 if self.tok == ']':
204 self.accept()
205 return expr
206 if self.tok != ',':
207 raise QAPISchemaError(self, 'Expected "," or "]"')
208 self.accept()
209
210 def get_expr(self, nested):
211 if self.tok != '{' and not nested:
212 raise QAPISchemaError(self, 'Expected "{"')
213 if self.tok == '{':
214 self.accept()
215 expr = self.get_members()
216 elif self.tok == '[':
217 self.accept()
218 expr = self.get_values()
219 elif self.tok == "'":
220 expr = self.val
221 self.accept()
222 else:
223 raise QAPISchemaError(self, 'Expected "{", "[" or string')
224 return expr
225
226 def find_base_fields(base):
227 base_struct_define = find_struct(base)
228 if not base_struct_define:
229 return None
230 return base_struct_define['data']
231
232 # Return the discriminator enum define if discriminator is specified as an
233 # enum type, otherwise return None.
234 def discriminator_find_enum_define(expr):
235 base = expr.get('base')
236 discriminator = expr.get('discriminator')
237
238 if not (discriminator and base):
239 return None
240
241 base_fields = find_base_fields(base)
242 if not base_fields:
243 return None
244
245 discriminator_type = base_fields.get(discriminator)
246 if not discriminator_type:
247 return None
248
249 return find_enum(discriminator_type)
250
251 def check_union(expr, expr_info):
252 name = expr['union']
253 base = expr.get('base')
254 discriminator = expr.get('discriminator')
255 members = expr['data']
256
257 # If the object has a member 'base', its value must name a complex type.
258 if base:
259 base_fields = find_base_fields(base)
260 if not base_fields:
261 raise QAPIExprError(expr_info,
262 "Base '%s' is not a valid type"
263 % base)
264
265 # If the union object has no member 'discriminator', it's an
266 # ordinary union.
267 if not discriminator:
268 enum_define = None
269
270 # Else if the value of member 'discriminator' is {}, it's an
271 # anonymous union.
272 elif discriminator == {}:
273 enum_define = None
274
275 # Else, it's a flat union.
276 else:
277 # The object must have a member 'base'.
278 if not base:
279 raise QAPIExprError(expr_info,
280 "Flat union '%s' must have a base field"
281 % name)
282 # The value of member 'discriminator' must name a member of the
283 # base type.
284 discriminator_type = base_fields.get(discriminator)
285 if not discriminator_type:
286 raise QAPIExprError(expr_info,
287 "Discriminator '%s' is not a member of base "
288 "type '%s'"
289 % (discriminator, base))
290 enum_define = find_enum(discriminator_type)
291 # Do not allow string discriminator
292 if not enum_define:
293 raise QAPIExprError(expr_info,
294 "Discriminator '%s' must be of enumeration "
295 "type" % discriminator)
296
297 # Check every branch
298 for (key, value) in members.items():
299 # If this named member's value names an enum type, then all members
300 # of 'data' must also be members of the enum type.
301 if enum_define and not key in enum_define['enum_values']:
302 raise QAPIExprError(expr_info,
303 "Discriminator value '%s' is not found in "
304 "enum '%s'" %
305 (key, enum_define["enum_name"]))
306 # Todo: add checking for values. Key is checked as above, value can be
307 # also checked here, but we need more functions to handle array case.
308
309 def check_exprs(schema):
310 for expr_elem in schema.exprs:
311 expr = expr_elem['expr']
312 if expr.has_key('union'):
313 check_union(expr, expr_elem['info'])
314
315 def parse_schema(input_file):
316 try:
317 schema = QAPISchema(open(input_file, "r"))
318 except (QAPISchemaError, QAPIExprError), e:
319 print >>sys.stderr, e
320 exit(1)
321
322 exprs = []
323
324 for expr_elem in schema.exprs:
325 expr = expr_elem['expr']
326 if expr.has_key('enum'):
327 add_enum(expr['enum'], expr['data'])
328 elif expr.has_key('union'):
329 add_union(expr)
330 elif expr.has_key('type'):
331 add_struct(expr)
332 exprs.append(expr)
333
334 # Try again for hidden UnionKind enum
335 for expr_elem in schema.exprs:
336 expr = expr_elem['expr']
337 if expr.has_key('union'):
338 if not discriminator_find_enum_define(expr):
339 add_enum('%sKind' % expr['union'])
340
341 try:
342 check_exprs(schema)
343 except QAPIExprError, e:
344 print >>sys.stderr, e
345 exit(1)
346
347 return exprs
348
349 def parse_args(typeinfo):
350 if isinstance(typeinfo, basestring):
351 struct = find_struct(typeinfo)
352 assert struct != None
353 typeinfo = struct['data']
354
355 for member in typeinfo:
356 argname = member
357 argentry = typeinfo[member]
358 optional = False
359 structured = False
360 if member.startswith('*'):
361 argname = member[1:]
362 optional = True
363 if isinstance(argentry, OrderedDict):
364 structured = True
365 yield (argname, argentry, optional, structured)
366
367 def de_camel_case(name):
368 new_name = ''
369 for ch in name:
370 if ch.isupper() and new_name:
371 new_name += '_'
372 if ch == '-':
373 new_name += '_'
374 else:
375 new_name += ch.lower()
376 return new_name
377
378 def camel_case(name):
379 new_name = ''
380 first = True
381 for ch in name:
382 if ch in ['_', '-']:
383 first = True
384 elif first:
385 new_name += ch.upper()
386 first = False
387 else:
388 new_name += ch.lower()
389 return new_name
390
391 def c_var(name, protect=True):
392 # ANSI X3J11/88-090, 3.1.1
393 c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
394 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
395 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
396 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
397 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
398 # ISO/IEC 9899:1999, 6.4.1
399 c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
400 # ISO/IEC 9899:2011, 6.4.1
401 c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
402 '_Static_assert', '_Thread_local'])
403 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
404 # excluding _.*
405 gcc_words = set(['asm', 'typeof'])
406 # C++ ISO/IEC 14882:2003 2.11
407 cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete',
408 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
409 'namespace', 'new', 'operator', 'private', 'protected',
410 'public', 'reinterpret_cast', 'static_cast', 'template',
411 'this', 'throw', 'true', 'try', 'typeid', 'typename',
412 'using', 'virtual', 'wchar_t',
413 # alternative representations
414 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
415 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
416 # namespace pollution:
417 polluted_words = set(['unix', 'errno'])
418 if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
419 return "q_" + name
420 return name.replace('-', '_').lstrip("*")
421
422 def c_fun(name, protect=True):
423 return c_var(name, protect).replace('.', '_')
424
425 def c_list_type(name):
426 return '%sList' % name
427
428 def type_name(name):
429 if type(name) == list:
430 return c_list_type(name[0])
431 return name
432
433 enum_types = []
434 struct_types = []
435 union_types = []
436
437 def add_struct(definition):
438 global struct_types
439 struct_types.append(definition)
440
441 def find_struct(name):
442 global struct_types
443 for struct in struct_types:
444 if struct['type'] == name:
445 return struct
446 return None
447
448 def add_union(definition):
449 global union_types
450 union_types.append(definition)
451
452 def find_union(name):
453 global union_types
454 for union in union_types:
455 if union['union'] == name:
456 return union
457 return None
458
459 def add_enum(name, enum_values = None):
460 global enum_types
461 enum_types.append({"enum_name": name, "enum_values": enum_values})
462
463 def find_enum(name):
464 global enum_types
465 for enum in enum_types:
466 if enum['enum_name'] == name:
467 return enum
468 return None
469
470 def is_enum(name):
471 return find_enum(name) != None
472
473 def c_type(name, is_param=False):
474 if name == 'str':
475 if is_param:
476 return 'const char *'
477 return 'char *'
478 elif name == 'int':
479 return 'int64_t'
480 elif (name == 'int8' or name == 'int16' or name == 'int32' or
481 name == 'int64' or name == 'uint8' or name == 'uint16' or
482 name == 'uint32' or name == 'uint64'):
483 return name + '_t'
484 elif name == 'size':
485 return 'uint64_t'
486 elif name == 'bool':
487 return 'bool'
488 elif name == 'number':
489 return 'double'
490 elif type(name) == list:
491 return '%s *' % c_list_type(name[0])
492 elif is_enum(name):
493 return name
494 elif name == None or len(name) == 0:
495 return 'void'
496 elif name == name.upper():
497 return '%sEvent *' % camel_case(name)
498 else:
499 return '%s *' % name
500
501 def genindent(count):
502 ret = ""
503 for i in range(count):
504 ret += " "
505 return ret
506
507 indent_level = 0
508
509 def push_indent(indent_amount=4):
510 global indent_level
511 indent_level += indent_amount
512
513 def pop_indent(indent_amount=4):
514 global indent_level
515 indent_level -= indent_amount
516
517 def cgen(code, **kwds):
518 indent = genindent(indent_level)
519 lines = code.split('\n')
520 lines = map(lambda x: indent + x, lines)
521 return '\n'.join(lines) % kwds + '\n'
522
523 def mcgen(code, **kwds):
524 return cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
525
526 def basename(filename):
527 return filename.split("/")[-1]
528
529 def guardname(filename):
530 guard = basename(filename).rsplit(".", 1)[0]
531 for substr in [".", " ", "-"]:
532 guard = guard.replace(substr, "_")
533 return guard.upper() + '_H'
534
535 def guardstart(name):
536 return mcgen('''
537
538 #ifndef %(name)s
539 #define %(name)s
540
541 ''',
542 name=guardname(name))
543
544 def guardend(name):
545 return mcgen('''
546
547 #endif /* %(name)s */
548
549 ''',
550 name=guardname(name))
551
552 # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
553 # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
554 # ENUM24_Name -> ENUM24_NAME
555 def _generate_enum_string(value):
556 c_fun_str = c_fun(value, False)
557 if value.isupper():
558 return c_fun_str
559
560 new_name = ''
561 l = len(c_fun_str)
562 for i in range(l):
563 c = c_fun_str[i]
564 # When c is upper and no "_" appears before, do more checks
565 if c.isupper() and (i > 0) and c_fun_str[i - 1] != "_":
566 # Case 1: next string is lower
567 # Case 2: previous string is digit
568 if (i < (l - 1) and c_fun_str[i + 1].islower()) or \
569 c_fun_str[i - 1].isdigit():
570 new_name += '_'
571 new_name += c
572 return new_name.lstrip('_').upper()
573
574 def generate_enum_full_value(enum_name, enum_value):
575 abbrev_string = _generate_enum_string(enum_name)
576 value_string = _generate_enum_string(enum_value)
577 return "%s_%s" % (abbrev_string, value_string)