arm: Rename virt machine class to use MACHINE_TYPE_NAME
[qemu/ar7.git] / scripts / qapi.py
blobc4423b740c952f54458cdac31bc05f4e19dc0d3d
2 # QAPI helper library
4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2015 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
15 from ordereddict import OrderedDict
16 import errno
17 import getopt
18 import os
19 import sys
20 import string
22 builtin_types = {
23 'str': 'QTYPE_QSTRING',
24 'int': 'QTYPE_QINT',
25 'number': 'QTYPE_QFLOAT',
26 'bool': 'QTYPE_QBOOL',
27 'int8': 'QTYPE_QINT',
28 'int16': 'QTYPE_QINT',
29 'int32': 'QTYPE_QINT',
30 'int64': 'QTYPE_QINT',
31 'uint8': 'QTYPE_QINT',
32 'uint16': 'QTYPE_QINT',
33 'uint32': 'QTYPE_QINT',
34 'uint64': 'QTYPE_QINT',
35 'size': 'QTYPE_QINT',
38 # Whitelist of commands allowed to return a non-dictionary
39 returns_whitelist = [
40 # From QMP:
41 'human-monitor-command',
42 'query-migrate-cache-size',
43 'query-tpm-models',
44 'query-tpm-types',
45 'ringbuf-read',
47 # From QGA:
48 'guest-file-open',
49 'guest-fsfreeze-freeze',
50 'guest-fsfreeze-freeze-list',
51 'guest-fsfreeze-status',
52 'guest-fsfreeze-thaw',
53 'guest-get-time',
54 'guest-set-vcpus',
55 'guest-sync',
56 'guest-sync-delimited',
58 # From qapi-schema-test:
59 'user_def_cmd3',
62 enum_types = []
63 struct_types = []
64 union_types = []
65 events = []
66 all_names = {}
69 # Parsing the schema into expressions
72 def error_path(parent):
73 res = ""
74 while parent:
75 res = ("In file included from %s:%d:\n" % (parent['file'],
76 parent['line'])) + res
77 parent = parent['parent']
78 return res
80 class QAPISchemaError(Exception):
81 def __init__(self, schema, msg):
82 self.fname = schema.fname
83 self.msg = msg
84 self.col = 1
85 self.line = schema.line
86 for ch in schema.src[schema.line_pos:schema.pos]:
87 if ch == '\t':
88 self.col = (self.col + 7) % 8 + 1
89 else:
90 self.col += 1
91 self.info = schema.incl_info
93 def __str__(self):
94 return error_path(self.info) + \
95 "%s:%d:%d: %s" % (self.fname, self.line, self.col, self.msg)
97 class QAPIExprError(Exception):
98 def __init__(self, expr_info, msg):
99 self.info = expr_info
100 self.msg = msg
102 def __str__(self):
103 return error_path(self.info['parent']) + \
104 "%s:%d: %s" % (self.info['file'], self.info['line'], self.msg)
106 class QAPISchema:
108 def __init__(self, fp, previously_included = [], incl_info = None):
109 abs_fname = os.path.abspath(fp.name)
110 fname = fp.name
111 self.fname = fname
112 previously_included.append(abs_fname)
113 self.incl_info = incl_info
114 self.src = fp.read()
115 if self.src == '' or self.src[-1] != '\n':
116 self.src += '\n'
117 self.cursor = 0
118 self.line = 1
119 self.line_pos = 0
120 self.exprs = []
121 self.accept()
123 while self.tok != None:
124 expr_info = {'file': fname, 'line': self.line,
125 'parent': self.incl_info}
126 expr = self.get_expr(False)
127 if isinstance(expr, dict) and "include" in expr:
128 if len(expr) != 1:
129 raise QAPIExprError(expr_info, "Invalid 'include' directive")
130 include = expr["include"]
131 if not isinstance(include, str):
132 raise QAPIExprError(expr_info,
133 'Expected a file name (string), got: %s'
134 % include)
135 incl_abs_fname = os.path.join(os.path.dirname(abs_fname),
136 include)
137 # catch inclusion cycle
138 inf = expr_info
139 while inf:
140 if incl_abs_fname == os.path.abspath(inf['file']):
141 raise QAPIExprError(expr_info, "Inclusion loop for %s"
142 % include)
143 inf = inf['parent']
144 # skip multiple include of the same file
145 if incl_abs_fname in previously_included:
146 continue
147 try:
148 fobj = open(incl_abs_fname, 'r')
149 except IOError, e:
150 raise QAPIExprError(expr_info,
151 '%s: %s' % (e.strerror, include))
152 exprs_include = QAPISchema(fobj, previously_included,
153 expr_info)
154 self.exprs.extend(exprs_include.exprs)
155 else:
156 expr_elem = {'expr': expr,
157 'info': expr_info}
158 self.exprs.append(expr_elem)
160 def accept(self):
161 while True:
162 self.tok = self.src[self.cursor]
163 self.pos = self.cursor
164 self.cursor += 1
165 self.val = None
167 if self.tok == '#':
168 self.cursor = self.src.find('\n', self.cursor)
169 elif self.tok in ['{', '}', ':', ',', '[', ']']:
170 return
171 elif self.tok == "'":
172 string = ''
173 esc = False
174 while True:
175 ch = self.src[self.cursor]
176 self.cursor += 1
177 if ch == '\n':
178 raise QAPISchemaError(self,
179 'Missing terminating "\'"')
180 if esc:
181 if ch == 'b':
182 string += '\b'
183 elif ch == 'f':
184 string += '\f'
185 elif ch == 'n':
186 string += '\n'
187 elif ch == 'r':
188 string += '\r'
189 elif ch == 't':
190 string += '\t'
191 elif ch == 'u':
192 value = 0
193 for x in range(0, 4):
194 ch = self.src[self.cursor]
195 self.cursor += 1
196 if ch not in "0123456789abcdefABCDEF":
197 raise QAPISchemaError(self,
198 '\\u escape needs 4 '
199 'hex digits')
200 value = (value << 4) + int(ch, 16)
201 # If Python 2 and 3 didn't disagree so much on
202 # how to handle Unicode, then we could allow
203 # Unicode string defaults. But most of QAPI is
204 # ASCII-only, so we aren't losing much for now.
205 if not value or value > 0x7f:
206 raise QAPISchemaError(self,
207 'For now, \\u escape '
208 'only supports non-zero '
209 'values up to \\u007f')
210 string += chr(value)
211 elif ch in "\\/'\"":
212 string += ch
213 else:
214 raise QAPISchemaError(self,
215 "Unknown escape \\%s" %ch)
216 esc = False
217 elif ch == "\\":
218 esc = True
219 elif ch == "'":
220 self.val = string
221 return
222 else:
223 string += ch
224 elif self.src.startswith("true", self.pos):
225 self.val = True
226 self.cursor += 3
227 return
228 elif self.src.startswith("false", self.pos):
229 self.val = False
230 self.cursor += 4
231 return
232 elif self.src.startswith("null", self.pos):
233 self.val = None
234 self.cursor += 3
235 return
236 elif self.tok == '\n':
237 if self.cursor == len(self.src):
238 self.tok = None
239 return
240 self.line += 1
241 self.line_pos = self.cursor
242 elif not self.tok.isspace():
243 raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
245 def get_members(self):
246 expr = OrderedDict()
247 if self.tok == '}':
248 self.accept()
249 return expr
250 if self.tok != "'":
251 raise QAPISchemaError(self, 'Expected string or "}"')
252 while True:
253 key = self.val
254 self.accept()
255 if self.tok != ':':
256 raise QAPISchemaError(self, 'Expected ":"')
257 self.accept()
258 if key in expr:
259 raise QAPISchemaError(self, 'Duplicate key "%s"' % key)
260 expr[key] = self.get_expr(True)
261 if self.tok == '}':
262 self.accept()
263 return expr
264 if self.tok != ',':
265 raise QAPISchemaError(self, 'Expected "," or "}"')
266 self.accept()
267 if self.tok != "'":
268 raise QAPISchemaError(self, 'Expected string')
270 def get_values(self):
271 expr = []
272 if self.tok == ']':
273 self.accept()
274 return expr
275 if not self.tok in "{['tfn":
276 raise QAPISchemaError(self, 'Expected "{", "[", "]", string, '
277 'boolean or "null"')
278 while True:
279 expr.append(self.get_expr(True))
280 if self.tok == ']':
281 self.accept()
282 return expr
283 if self.tok != ',':
284 raise QAPISchemaError(self, 'Expected "," or "]"')
285 self.accept()
287 def get_expr(self, nested):
288 if self.tok != '{' and not nested:
289 raise QAPISchemaError(self, 'Expected "{"')
290 if self.tok == '{':
291 self.accept()
292 expr = self.get_members()
293 elif self.tok == '[':
294 self.accept()
295 expr = self.get_values()
296 elif self.tok in "'tfn":
297 expr = self.val
298 self.accept()
299 else:
300 raise QAPISchemaError(self, 'Expected "{", "[" or string')
301 return expr
304 # Semantic analysis of schema expressions
307 def find_base_fields(base):
308 base_struct_define = find_struct(base)
309 if not base_struct_define:
310 return None
311 return base_struct_define['data']
313 # Return the qtype of an alternate branch, or None on error.
314 def find_alternate_member_qtype(qapi_type):
315 if builtin_types.has_key(qapi_type):
316 return builtin_types[qapi_type]
317 elif find_struct(qapi_type):
318 return "QTYPE_QDICT"
319 elif find_enum(qapi_type):
320 return "QTYPE_QSTRING"
321 elif find_union(qapi_type):
322 return "QTYPE_QDICT"
323 return None
325 # Return the discriminator enum define if discriminator is specified as an
326 # enum type, otherwise return None.
327 def discriminator_find_enum_define(expr):
328 base = expr.get('base')
329 discriminator = expr.get('discriminator')
331 if not (discriminator and base):
332 return None
334 base_fields = find_base_fields(base)
335 if not base_fields:
336 return None
338 discriminator_type = base_fields.get(discriminator)
339 if not discriminator_type:
340 return None
342 return find_enum(discriminator_type)
344 # FIXME should enforce "other than downstream extensions [...], all
345 # names should begin with a letter".
346 valid_name = re.compile('^[a-zA-Z_][a-zA-Z0-9_.-]*$')
347 def check_name(expr_info, source, name, allow_optional = False,
348 enum_member = False):
349 global valid_name
350 membername = name
352 if not isinstance(name, str):
353 raise QAPIExprError(expr_info,
354 "%s requires a string name" % source)
355 if name.startswith('*'):
356 membername = name[1:]
357 if not allow_optional:
358 raise QAPIExprError(expr_info,
359 "%s does not allow optional name '%s'"
360 % (source, name))
361 # Enum members can start with a digit, because the generated C
362 # code always prefixes it with the enum name
363 if enum_member:
364 membername = '_' + membername
365 if not valid_name.match(membername):
366 raise QAPIExprError(expr_info,
367 "%s uses invalid name '%s'" % (source, name))
369 def add_name(name, info, meta, implicit = False):
370 global all_names
371 check_name(info, "'%s'" % meta, name)
372 # FIXME should reject names that differ only in '_' vs. '.'
373 # vs. '-', because they're liable to clash in generated C.
374 if name in all_names:
375 raise QAPIExprError(info,
376 "%s '%s' is already defined"
377 % (all_names[name], name))
378 if not implicit and name[-4:] == 'Kind':
379 raise QAPIExprError(info,
380 "%s '%s' should not end in 'Kind'"
381 % (meta, name))
382 all_names[name] = meta
384 def add_struct(definition, info):
385 global struct_types
386 name = definition['struct']
387 add_name(name, info, 'struct')
388 struct_types.append(definition)
390 def find_struct(name):
391 global struct_types
392 for struct in struct_types:
393 if struct['struct'] == name:
394 return struct
395 return None
397 def add_union(definition, info):
398 global union_types
399 name = definition['union']
400 add_name(name, info, 'union')
401 union_types.append(definition)
403 def find_union(name):
404 global union_types
405 for union in union_types:
406 if union['union'] == name:
407 return union
408 return None
410 def add_enum(name, info, enum_values = None, implicit = False):
411 global enum_types
412 add_name(name, info, 'enum', implicit)
413 enum_types.append({"enum_name": name, "enum_values": enum_values})
415 def find_enum(name):
416 global enum_types
417 for enum in enum_types:
418 if enum['enum_name'] == name:
419 return enum
420 return None
422 def is_enum(name):
423 return find_enum(name) != None
425 def check_type(expr_info, source, value, allow_array = False,
426 allow_dict = False, allow_optional = False,
427 allow_star = False, allow_metas = []):
428 global all_names
430 if value is None:
431 return
433 if allow_star and value == '**':
434 return
436 # Check if array type for value is okay
437 if isinstance(value, list):
438 if not allow_array:
439 raise QAPIExprError(expr_info,
440 "%s cannot be an array" % source)
441 if len(value) != 1 or not isinstance(value[0], str):
442 raise QAPIExprError(expr_info,
443 "%s: array type must contain single type name"
444 % source)
445 value = value[0]
447 # Check if type name for value is okay
448 if isinstance(value, str):
449 if value == '**':
450 raise QAPIExprError(expr_info,
451 "%s uses '**' but did not request 'gen':false"
452 % source)
453 if not value in all_names:
454 raise QAPIExprError(expr_info,
455 "%s uses unknown type '%s'"
456 % (source, value))
457 if not all_names[value] in allow_metas:
458 raise QAPIExprError(expr_info,
459 "%s cannot use %s type '%s'"
460 % (source, all_names[value], value))
461 return
463 if not allow_dict:
464 raise QAPIExprError(expr_info,
465 "%s should be a type name" % source)
467 if not isinstance(value, OrderedDict):
468 raise QAPIExprError(expr_info,
469 "%s should be a dictionary or type name" % source)
471 # value is a dictionary, check that each member is okay
472 for (key, arg) in value.items():
473 check_name(expr_info, "Member of %s" % source, key,
474 allow_optional=allow_optional)
475 # Todo: allow dictionaries to represent default values of
476 # an optional argument.
477 check_type(expr_info, "Member '%s' of %s" % (key, source), arg,
478 allow_array=True, allow_star=allow_star,
479 allow_metas=['built-in', 'union', 'alternate', 'struct',
480 'enum'])
482 def check_member_clash(expr_info, base_name, data, source = ""):
483 base = find_struct(base_name)
484 assert base
485 base_members = base['data']
486 for key in data.keys():
487 if key.startswith('*'):
488 key = key[1:]
489 if key in base_members or "*" + key in base_members:
490 raise QAPIExprError(expr_info,
491 "Member name '%s'%s clashes with base '%s'"
492 % (key, source, base_name))
493 if base.get('base'):
494 check_member_clash(expr_info, base['base'], data, source)
496 def check_command(expr, expr_info):
497 name = expr['command']
498 allow_star = expr.has_key('gen')
500 check_type(expr_info, "'data' for command '%s'" % name,
501 expr.get('data'), allow_dict=True, allow_optional=True,
502 allow_metas=['struct'], allow_star=allow_star)
503 returns_meta = ['union', 'struct']
504 if name in returns_whitelist:
505 returns_meta += ['built-in', 'alternate', 'enum']
506 check_type(expr_info, "'returns' for command '%s'" % name,
507 expr.get('returns'), allow_array=True,
508 allow_optional=True, allow_metas=returns_meta,
509 allow_star=allow_star)
511 def check_event(expr, expr_info):
512 global events
513 name = expr['event']
515 if name.upper() == 'MAX':
516 raise QAPIExprError(expr_info, "Event name 'MAX' cannot be created")
517 events.append(name)
518 check_type(expr_info, "'data' for event '%s'" % name,
519 expr.get('data'), allow_dict=True, allow_optional=True,
520 allow_metas=['struct'])
522 def check_union(expr, expr_info):
523 name = expr['union']
524 base = expr.get('base')
525 discriminator = expr.get('discriminator')
526 members = expr['data']
527 values = { 'MAX': '(automatic)' }
529 # Two types of unions, determined by discriminator.
531 # With no discriminator it is a simple union.
532 if discriminator is None:
533 enum_define = None
534 allow_metas=['built-in', 'union', 'alternate', 'struct', 'enum']
535 if base is not None:
536 raise QAPIExprError(expr_info,
537 "Simple union '%s' must not have a base"
538 % name)
540 # Else, it's a flat union.
541 else:
542 # The object must have a string member 'base'.
543 if not isinstance(base, str):
544 raise QAPIExprError(expr_info,
545 "Flat union '%s' must have a string base field"
546 % name)
547 base_fields = find_base_fields(base)
548 if not base_fields:
549 raise QAPIExprError(expr_info,
550 "Base '%s' is not a valid struct"
551 % base)
553 # The value of member 'discriminator' must name a non-optional
554 # member of the base struct.
555 check_name(expr_info, "Discriminator of flat union '%s'" % name,
556 discriminator)
557 discriminator_type = base_fields.get(discriminator)
558 if not discriminator_type:
559 raise QAPIExprError(expr_info,
560 "Discriminator '%s' is not a member of base "
561 "struct '%s'"
562 % (discriminator, base))
563 enum_define = find_enum(discriminator_type)
564 allow_metas=['struct']
565 # Do not allow string discriminator
566 if not enum_define:
567 raise QAPIExprError(expr_info,
568 "Discriminator '%s' must be of enumeration "
569 "type" % discriminator)
571 # Check every branch
572 for (key, value) in members.items():
573 check_name(expr_info, "Member of union '%s'" % name, key)
575 # Each value must name a known type; furthermore, in flat unions,
576 # branches must be a struct with no overlapping member names
577 check_type(expr_info, "Member '%s' of union '%s'" % (key, name),
578 value, allow_array=not base, allow_metas=allow_metas)
579 if base:
580 branch_struct = find_struct(value)
581 assert branch_struct
582 check_member_clash(expr_info, base, branch_struct['data'],
583 " of branch '%s'" % key)
585 # If the discriminator names an enum type, then all members
586 # of 'data' must also be members of the enum type.
587 if enum_define:
588 if not key in enum_define['enum_values']:
589 raise QAPIExprError(expr_info,
590 "Discriminator value '%s' is not found in "
591 "enum '%s'" %
592 (key, enum_define["enum_name"]))
594 # Otherwise, check for conflicts in the generated enum
595 else:
596 c_key = camel_to_upper(key)
597 if c_key in values:
598 raise QAPIExprError(expr_info,
599 "Union '%s' member '%s' clashes with '%s'"
600 % (name, key, values[c_key]))
601 values[c_key] = key
603 def check_alternate(expr, expr_info):
604 name = expr['alternate']
605 members = expr['data']
606 values = { 'MAX': '(automatic)' }
607 types_seen = {}
609 # Check every branch
610 for (key, value) in members.items():
611 check_name(expr_info, "Member of alternate '%s'" % name, key)
613 # Check for conflicts in the generated enum
614 c_key = camel_to_upper(key)
615 if c_key in values:
616 raise QAPIExprError(expr_info,
617 "Alternate '%s' member '%s' clashes with '%s'"
618 % (name, key, values[c_key]))
619 values[c_key] = key
621 # Ensure alternates have no type conflicts.
622 check_type(expr_info, "Member '%s' of alternate '%s'" % (key, name),
623 value,
624 allow_metas=['built-in', 'union', 'struct', 'enum'])
625 qtype = find_alternate_member_qtype(value)
626 assert qtype
627 if qtype in types_seen:
628 raise QAPIExprError(expr_info,
629 "Alternate '%s' member '%s' can't "
630 "be distinguished from member '%s'"
631 % (name, key, types_seen[qtype]))
632 types_seen[qtype] = key
634 def check_enum(expr, expr_info):
635 name = expr['enum']
636 members = expr.get('data')
637 prefix = expr.get('prefix')
638 values = { 'MAX': '(automatic)' }
640 if not isinstance(members, list):
641 raise QAPIExprError(expr_info,
642 "Enum '%s' requires an array for 'data'" % name)
643 if prefix is not None and not isinstance(prefix, str):
644 raise QAPIExprError(expr_info,
645 "Enum '%s' requires a string for 'prefix'" % name)
646 for member in members:
647 check_name(expr_info, "Member of enum '%s'" %name, member,
648 enum_member=True)
649 key = camel_to_upper(member)
650 if key in values:
651 raise QAPIExprError(expr_info,
652 "Enum '%s' member '%s' clashes with '%s'"
653 % (name, member, values[key]))
654 values[key] = member
656 def check_struct(expr, expr_info):
657 name = expr['struct']
658 members = expr['data']
660 check_type(expr_info, "'data' for struct '%s'" % name, members,
661 allow_dict=True, allow_optional=True)
662 check_type(expr_info, "'base' for struct '%s'" % name, expr.get('base'),
663 allow_metas=['struct'])
664 if expr.get('base'):
665 check_member_clash(expr_info, expr['base'], expr['data'])
667 def check_keys(expr_elem, meta, required, optional=[]):
668 expr = expr_elem['expr']
669 info = expr_elem['info']
670 name = expr[meta]
671 if not isinstance(name, str):
672 raise QAPIExprError(info,
673 "'%s' key must have a string value" % meta)
674 required = required + [ meta ]
675 for (key, value) in expr.items():
676 if not key in required and not key in optional:
677 raise QAPIExprError(info,
678 "Unknown key '%s' in %s '%s'"
679 % (key, meta, name))
680 if (key == 'gen' or key == 'success-response') and value != False:
681 raise QAPIExprError(info,
682 "'%s' of %s '%s' should only use false value"
683 % (key, meta, name))
684 for key in required:
685 if not expr.has_key(key):
686 raise QAPIExprError(info,
687 "Key '%s' is missing from %s '%s'"
688 % (key, meta, name))
690 def check_exprs(exprs):
691 global all_names
693 # Learn the types and check for valid expression keys
694 for builtin in builtin_types.keys():
695 all_names[builtin] = 'built-in'
696 for expr_elem in exprs:
697 expr = expr_elem['expr']
698 info = expr_elem['info']
699 if expr.has_key('enum'):
700 check_keys(expr_elem, 'enum', ['data'], ['prefix'])
701 add_enum(expr['enum'], info, expr['data'])
702 elif expr.has_key('union'):
703 check_keys(expr_elem, 'union', ['data'],
704 ['base', 'discriminator'])
705 add_union(expr, info)
706 elif expr.has_key('alternate'):
707 check_keys(expr_elem, 'alternate', ['data'])
708 add_name(expr['alternate'], info, 'alternate')
709 elif expr.has_key('struct'):
710 check_keys(expr_elem, 'struct', ['data'], ['base'])
711 add_struct(expr, info)
712 elif expr.has_key('command'):
713 check_keys(expr_elem, 'command', [],
714 ['data', 'returns', 'gen', 'success-response'])
715 add_name(expr['command'], info, 'command')
716 elif expr.has_key('event'):
717 check_keys(expr_elem, 'event', [], ['data'])
718 add_name(expr['event'], info, 'event')
719 else:
720 raise QAPIExprError(expr_elem['info'],
721 "Expression is missing metatype")
723 # Try again for hidden UnionKind enum
724 for expr_elem in exprs:
725 expr = expr_elem['expr']
726 if expr.has_key('union'):
727 if not discriminator_find_enum_define(expr):
728 add_enum('%sKind' % expr['union'], expr_elem['info'],
729 implicit=True)
730 elif expr.has_key('alternate'):
731 add_enum('%sKind' % expr['alternate'], expr_elem['info'],
732 implicit=True)
734 # Validate that exprs make sense
735 for expr_elem in exprs:
736 expr = expr_elem['expr']
737 info = expr_elem['info']
739 if expr.has_key('enum'):
740 check_enum(expr, info)
741 elif expr.has_key('union'):
742 check_union(expr, info)
743 elif expr.has_key('alternate'):
744 check_alternate(expr, info)
745 elif expr.has_key('struct'):
746 check_struct(expr, info)
747 elif expr.has_key('command'):
748 check_command(expr, info)
749 elif expr.has_key('event'):
750 check_event(expr, info)
751 else:
752 assert False, 'unexpected meta type'
754 return map(lambda expr_elem: expr_elem['expr'], exprs)
756 def parse_schema(fname):
757 try:
758 schema = QAPISchema(open(fname, "r"))
759 return check_exprs(schema.exprs)
760 except (QAPISchemaError, QAPIExprError), e:
761 print >>sys.stderr, e
762 exit(1)
765 # Code generation helpers
768 def parse_args(typeinfo):
769 if isinstance(typeinfo, str):
770 struct = find_struct(typeinfo)
771 assert struct != None
772 typeinfo = struct['data']
774 for member in typeinfo:
775 argname = member
776 argentry = typeinfo[member]
777 optional = False
778 if member.startswith('*'):
779 argname = member[1:]
780 optional = True
781 # Todo: allow argentry to be OrderedDict, for providing the
782 # value of an optional argument.
783 yield (argname, argentry, optional)
785 def camel_case(name):
786 new_name = ''
787 first = True
788 for ch in name:
789 if ch in ['_', '-']:
790 first = True
791 elif first:
792 new_name += ch.upper()
793 first = False
794 else:
795 new_name += ch.lower()
796 return new_name
798 # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
799 # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
800 # ENUM24_Name -> ENUM24_NAME
801 def camel_to_upper(value):
802 c_fun_str = c_name(value, False)
803 if value.isupper():
804 return c_fun_str
806 new_name = ''
807 l = len(c_fun_str)
808 for i in range(l):
809 c = c_fun_str[i]
810 # When c is upper and no "_" appears before, do more checks
811 if c.isupper() and (i > 0) and c_fun_str[i - 1] != "_":
812 # Case 1: next string is lower
813 # Case 2: previous string is digit
814 if (i < (l - 1) and c_fun_str[i + 1].islower()) or \
815 c_fun_str[i - 1].isdigit():
816 new_name += '_'
817 new_name += c
818 return new_name.lstrip('_').upper()
820 def c_enum_const(type_name, const_name, prefix=None):
821 if prefix is not None:
822 type_name = prefix
823 return camel_to_upper(type_name + '_' + const_name)
825 c_name_trans = string.maketrans('.-', '__')
827 # Map @name to a valid C identifier.
828 # If @protect, avoid returning certain ticklish identifiers (like
829 # C keywords) by prepending "q_".
831 # Used for converting 'name' from a 'name':'type' qapi definition
832 # into a generated struct member, as well as converting type names
833 # into substrings of a generated C function name.
834 # '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
835 # protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
836 def c_name(name, protect=True):
837 # ANSI X3J11/88-090, 3.1.1
838 c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
839 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
840 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
841 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
842 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
843 # ISO/IEC 9899:1999, 6.4.1
844 c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
845 # ISO/IEC 9899:2011, 6.4.1
846 c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
847 '_Static_assert', '_Thread_local'])
848 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
849 # excluding _.*
850 gcc_words = set(['asm', 'typeof'])
851 # C++ ISO/IEC 14882:2003 2.11
852 cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete',
853 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
854 'namespace', 'new', 'operator', 'private', 'protected',
855 'public', 'reinterpret_cast', 'static_cast', 'template',
856 'this', 'throw', 'true', 'try', 'typeid', 'typename',
857 'using', 'virtual', 'wchar_t',
858 # alternative representations
859 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
860 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
861 # namespace pollution:
862 polluted_words = set(['unix', 'errno'])
863 if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
864 return "q_" + name
865 return name.translate(c_name_trans)
867 # Map type @name to the C typedef name for the list form.
869 # ['Name'] -> 'NameList', ['x-Foo'] -> 'x_FooList', ['int'] -> 'intList'
870 def c_list_type(name):
871 return type_name(name) + 'List'
873 # Map type @value to the C typedef form.
875 # Used for converting 'type' from a 'member':'type' qapi definition
876 # into the alphanumeric portion of the type for a generated C parameter,
877 # as well as generated C function names. See c_type() for the rest of
878 # the conversion such as adding '*' on pointer types.
879 # 'int' -> 'int', '[x-Foo]' -> 'x_FooList', '__a.b_c' -> '__a_b_c'
880 def type_name(value):
881 if type(value) == list:
882 return c_list_type(value[0])
883 if value in builtin_types.keys():
884 return value
885 return c_name(value)
887 eatspace = '\033EATSPACE.'
888 pointer_suffix = ' *' + eatspace
890 # Map type @name to its C type expression.
891 # If @is_param, const-qualify the string type.
893 # This function is used for computing the full C type of 'member':'name'.
894 # A special suffix is added in c_type() for pointer types, and it's
895 # stripped in mcgen(). So please notice this when you check the return
896 # value of c_type() outside mcgen().
897 def c_type(value, is_param=False):
898 if value == 'str':
899 if is_param:
900 return 'const char' + pointer_suffix
901 return 'char' + pointer_suffix
903 elif value == 'int':
904 return 'int64_t'
905 elif (value == 'int8' or value == 'int16' or value == 'int32' or
906 value == 'int64' or value == 'uint8' or value == 'uint16' or
907 value == 'uint32' or value == 'uint64'):
908 return value + '_t'
909 elif value == 'size':
910 return 'uint64_t'
911 elif value == 'bool':
912 return 'bool'
913 elif value == 'number':
914 return 'double'
915 elif type(value) == list:
916 return c_list_type(value[0]) + pointer_suffix
917 elif is_enum(value):
918 return c_name(value)
919 elif value == None:
920 return 'void'
921 elif value in events:
922 return camel_case(value) + 'Event' + pointer_suffix
923 else:
924 # complex type name
925 assert isinstance(value, str) and value != ""
926 return c_name(value) + pointer_suffix
928 def is_c_ptr(value):
929 return c_type(value).endswith(pointer_suffix)
931 def genindent(count):
932 ret = ""
933 for i in range(count):
934 ret += " "
935 return ret
937 indent_level = 0
939 def push_indent(indent_amount=4):
940 global indent_level
941 indent_level += indent_amount
943 def pop_indent(indent_amount=4):
944 global indent_level
945 indent_level -= indent_amount
947 # Generate @code with @kwds interpolated.
948 # Obey indent_level, and strip eatspace.
949 def cgen(code, **kwds):
950 raw = code % kwds
951 if indent_level:
952 indent = genindent(indent_level)
953 # re.subn() lacks flags support before Python 2.7, use re.compile()
954 raw = re.subn(re.compile("^.", re.MULTILINE),
955 indent + r'\g<0>', raw)
956 raw = raw[0]
957 return re.sub(re.escape(eatspace) + ' *', '', raw)
959 def mcgen(code, **kwds):
960 if code[0] == '\n':
961 code = code[1:]
962 return cgen(code, **kwds)
965 def guardname(filename):
966 return c_name(filename, protect=False).upper()
968 def guardstart(name):
969 return mcgen('''
971 #ifndef %(name)s
972 #define %(name)s
974 ''',
975 name=guardname(name))
977 def guardend(name):
978 return mcgen('''
980 #endif /* %(name)s */
982 ''',
983 name=guardname(name))
986 # Common command line parsing
989 def parse_command_line(extra_options = "", extra_long_options = []):
991 try:
992 opts, args = getopt.gnu_getopt(sys.argv[1:],
993 "chp:o:" + extra_options,
994 ["source", "header", "prefix=",
995 "output-dir="] + extra_long_options)
996 except getopt.GetoptError, err:
997 print >>sys.stderr, "%s: %s" % (sys.argv[0], str(err))
998 sys.exit(1)
1000 output_dir = ""
1001 prefix = ""
1002 do_c = False
1003 do_h = False
1004 extra_opts = []
1006 for oa in opts:
1007 o, a = oa
1008 if o in ("-p", "--prefix"):
1009 match = re.match('([A-Za-z_.-][A-Za-z0-9_.-]*)?', a)
1010 if match.end() != len(a):
1011 print >>sys.stderr, \
1012 "%s: 'funny character '%s' in argument of --prefix" \
1013 % (sys.argv[0], a[match.end()])
1014 sys.exit(1)
1015 prefix = a
1016 elif o in ("-o", "--output-dir"):
1017 output_dir = a + "/"
1018 elif o in ("-c", "--source"):
1019 do_c = True
1020 elif o in ("-h", "--header"):
1021 do_h = True
1022 else:
1023 extra_opts.append(oa)
1025 if not do_c and not do_h:
1026 do_c = True
1027 do_h = True
1029 if len(args) != 1:
1030 print >>sys.stderr, "%s: need exactly one argument" % sys.argv[0]
1031 sys.exit(1)
1032 fname = args[0]
1034 return (fname, output_dir, do_c, do_h, prefix, extra_opts)
1037 # Generate output files with boilerplate
1040 def open_output(output_dir, do_c, do_h, prefix, c_file, h_file,
1041 c_comment, h_comment):
1042 guard = guardname(prefix + h_file)
1043 c_file = output_dir + prefix + c_file
1044 h_file = output_dir + prefix + h_file
1046 if output_dir:
1047 try:
1048 os.makedirs(output_dir)
1049 except os.error, e:
1050 if e.errno != errno.EEXIST:
1051 raise
1053 def maybe_open(really, name, opt):
1054 if really:
1055 return open(name, opt)
1056 else:
1057 import StringIO
1058 return StringIO.StringIO()
1060 fdef = maybe_open(do_c, c_file, 'w')
1061 fdecl = maybe_open(do_h, h_file, 'w')
1063 fdef.write(mcgen('''
1064 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
1065 %(comment)s
1066 ''',
1067 comment = c_comment))
1069 fdecl.write(mcgen('''
1070 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
1071 %(comment)s
1072 #ifndef %(guard)s
1073 #define %(guard)s
1075 ''',
1076 comment = h_comment, guard = guard))
1078 return (fdef, fdecl)
1080 def close_output(fdef, fdecl):
1081 fdecl.write('''
1082 #endif
1083 ''')
1084 fdecl.close()
1085 fdef.close()