hw/arm/pxa2xx: Mark coprocessor registers as ARM_CP_IO
[qemu/ar7.git] / scripts / qapi.py
blobf96a7772e565017fc6550b86e1ff9b5821ef19b0
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 = {}
68 def error_path(parent):
69 res = ""
70 while parent:
71 res = ("In file included from %s:%d:\n" % (parent['file'],
72 parent['line'])) + res
73 parent = parent['parent']
74 return res
76 class QAPISchemaError(Exception):
77 def __init__(self, schema, msg):
78 self.input_file = schema.input_file
79 self.msg = msg
80 self.col = 1
81 self.line = schema.line
82 for ch in schema.src[schema.line_pos:schema.pos]:
83 if ch == '\t':
84 self.col = (self.col + 7) % 8 + 1
85 else:
86 self.col += 1
87 self.info = schema.parent_info
89 def __str__(self):
90 return error_path(self.info) + \
91 "%s:%d:%d: %s" % (self.input_file, self.line, self.col, self.msg)
93 class QAPIExprError(Exception):
94 def __init__(self, expr_info, msg):
95 self.info = expr_info
96 self.msg = msg
98 def __str__(self):
99 return error_path(self.info['parent']) + \
100 "%s:%d: %s" % (self.info['file'], self.info['line'], self.msg)
102 class QAPISchema:
104 def __init__(self, fp, input_relname=None, include_hist=[],
105 previously_included=[], parent_info=None):
106 """ include_hist is a stack used to detect inclusion cycles
107 previously_included is a global state used to avoid multiple
108 inclusions of the same file"""
109 input_fname = os.path.abspath(fp.name)
110 if input_relname is None:
111 input_relname = fp.name
112 self.input_dir = os.path.dirname(input_fname)
113 self.input_file = input_relname
114 self.include_hist = include_hist + [(input_relname, input_fname)]
115 previously_included.append(input_fname)
116 self.parent_info = parent_info
117 self.src = fp.read()
118 if self.src == '' or self.src[-1] != '\n':
119 self.src += '\n'
120 self.cursor = 0
121 self.line = 1
122 self.line_pos = 0
123 self.exprs = []
124 self.accept()
126 while self.tok != None:
127 expr_info = {'file': input_relname, 'line': self.line, 'parent': self.parent_info}
128 expr = self.get_expr(False)
129 if isinstance(expr, dict) and "include" in expr:
130 if len(expr) != 1:
131 raise QAPIExprError(expr_info, "Invalid 'include' directive")
132 include = expr["include"]
133 if not isinstance(include, str):
134 raise QAPIExprError(expr_info,
135 'Expected a file name (string), got: %s'
136 % include)
137 include_path = os.path.join(self.input_dir, include)
138 for elem in self.include_hist:
139 if include_path == elem[1]:
140 raise QAPIExprError(expr_info, "Inclusion loop for %s"
141 % include)
142 # skip multiple include of the same file
143 if include_path in previously_included:
144 continue
145 try:
146 fobj = open(include_path, 'r')
147 except IOError, e:
148 raise QAPIExprError(expr_info,
149 '%s: %s' % (e.strerror, include))
150 exprs_include = QAPISchema(fobj, include, self.include_hist,
151 previously_included, expr_info)
152 self.exprs.extend(exprs_include.exprs)
153 else:
154 expr_elem = {'expr': expr,
155 'info': expr_info}
156 self.exprs.append(expr_elem)
158 def accept(self):
159 while True:
160 self.tok = self.src[self.cursor]
161 self.pos = self.cursor
162 self.cursor += 1
163 self.val = None
165 if self.tok == '#':
166 self.cursor = self.src.find('\n', self.cursor)
167 elif self.tok in ['{', '}', ':', ',', '[', ']']:
168 return
169 elif self.tok == "'":
170 string = ''
171 esc = False
172 while True:
173 ch = self.src[self.cursor]
174 self.cursor += 1
175 if ch == '\n':
176 raise QAPISchemaError(self,
177 'Missing terminating "\'"')
178 if esc:
179 if ch == 'b':
180 string += '\b'
181 elif ch == 'f':
182 string += '\f'
183 elif ch == 'n':
184 string += '\n'
185 elif ch == 'r':
186 string += '\r'
187 elif ch == 't':
188 string += '\t'
189 elif ch == 'u':
190 value = 0
191 for x in range(0, 4):
192 ch = self.src[self.cursor]
193 self.cursor += 1
194 if ch not in "0123456789abcdefABCDEF":
195 raise QAPISchemaError(self,
196 '\\u escape needs 4 '
197 'hex digits')
198 value = (value << 4) + int(ch, 16)
199 # If Python 2 and 3 didn't disagree so much on
200 # how to handle Unicode, then we could allow
201 # Unicode string defaults. But most of QAPI is
202 # ASCII-only, so we aren't losing much for now.
203 if not value or value > 0x7f:
204 raise QAPISchemaError(self,
205 'For now, \\u escape '
206 'only supports non-zero '
207 'values up to \\u007f')
208 string += chr(value)
209 elif ch in "\\/'\"":
210 string += ch
211 else:
212 raise QAPISchemaError(self,
213 "Unknown escape \\%s" %ch)
214 esc = False
215 elif ch == "\\":
216 esc = True
217 elif ch == "'":
218 self.val = string
219 return
220 else:
221 string += ch
222 elif self.tok in "tfn":
223 val = self.src[self.cursor - 1:]
224 if val.startswith("true"):
225 self.val = True
226 self.cursor += 3
227 return
228 elif val.startswith("false"):
229 self.val = False
230 self.cursor += 4
231 return
232 elif val.startswith("null"):
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
303 def find_base_fields(base):
304 base_struct_define = find_struct(base)
305 if not base_struct_define:
306 return None
307 return base_struct_define['data']
309 # Return the qtype of an alternate branch, or None on error.
310 def find_alternate_member_qtype(qapi_type):
311 if builtin_types.has_key(qapi_type):
312 return builtin_types[qapi_type]
313 elif find_struct(qapi_type):
314 return "QTYPE_QDICT"
315 elif find_enum(qapi_type):
316 return "QTYPE_QSTRING"
317 elif find_union(qapi_type):
318 return "QTYPE_QDICT"
319 return None
321 # Return the discriminator enum define if discriminator is specified as an
322 # enum type, otherwise return None.
323 def discriminator_find_enum_define(expr):
324 base = expr.get('base')
325 discriminator = expr.get('discriminator')
327 if not (discriminator and base):
328 return None
330 base_fields = find_base_fields(base)
331 if not base_fields:
332 return None
334 discriminator_type = base_fields.get(discriminator)
335 if not discriminator_type:
336 return None
338 return find_enum(discriminator_type)
340 valid_name = re.compile('^[a-zA-Z_][a-zA-Z0-9_.-]*$')
341 def check_name(expr_info, source, name, allow_optional = False,
342 enum_member = False):
343 global valid_name
344 membername = name
346 if not isinstance(name, str):
347 raise QAPIExprError(expr_info,
348 "%s requires a string name" % source)
349 if name.startswith('*'):
350 membername = name[1:]
351 if not allow_optional:
352 raise QAPIExprError(expr_info,
353 "%s does not allow optional name '%s'"
354 % (source, name))
355 # Enum members can start with a digit, because the generated C
356 # code always prefixes it with the enum name
357 if enum_member:
358 membername = '_' + membername
359 if not valid_name.match(membername):
360 raise QAPIExprError(expr_info,
361 "%s uses invalid name '%s'" % (source, name))
363 def check_type(expr_info, source, value, allow_array = False,
364 allow_dict = False, allow_optional = False,
365 allow_star = False, allow_metas = []):
366 global all_names
367 orig_value = value
369 if value is None:
370 return
372 if allow_star and value == '**':
373 return
375 # Check if array type for value is okay
376 if isinstance(value, list):
377 if not allow_array:
378 raise QAPIExprError(expr_info,
379 "%s cannot be an array" % source)
380 if len(value) != 1 or not isinstance(value[0], str):
381 raise QAPIExprError(expr_info,
382 "%s: array type must contain single type name"
383 % source)
384 value = value[0]
385 orig_value = "array of %s" %value
387 # Check if type name for value is okay
388 if isinstance(value, str):
389 if value == '**':
390 raise QAPIExprError(expr_info,
391 "%s uses '**' but did not request 'gen':false"
392 % source)
393 if not value in all_names:
394 raise QAPIExprError(expr_info,
395 "%s uses unknown type '%s'"
396 % (source, orig_value))
397 if not all_names[value] in allow_metas:
398 raise QAPIExprError(expr_info,
399 "%s cannot use %s type '%s'"
400 % (source, all_names[value], orig_value))
401 return
403 # value is a dictionary, check that each member is okay
404 if not isinstance(value, OrderedDict):
405 raise QAPIExprError(expr_info,
406 "%s should be a dictionary" % source)
407 if not allow_dict:
408 raise QAPIExprError(expr_info,
409 "%s should be a type name" % source)
410 for (key, arg) in value.items():
411 check_name(expr_info, "Member of %s" % source, key,
412 allow_optional=allow_optional)
413 # Todo: allow dictionaries to represent default values of
414 # an optional argument.
415 check_type(expr_info, "Member '%s' of %s" % (key, source), arg,
416 allow_array=True, allow_star=allow_star,
417 allow_metas=['built-in', 'union', 'alternate', 'struct',
418 'enum'])
420 def check_member_clash(expr_info, base_name, data, source = ""):
421 base = find_struct(base_name)
422 assert base
423 base_members = base['data']
424 for key in data.keys():
425 if key.startswith('*'):
426 key = key[1:]
427 if key in base_members or "*" + key in base_members:
428 raise QAPIExprError(expr_info,
429 "Member name '%s'%s clashes with base '%s'"
430 % (key, source, base_name))
431 if base.get('base'):
432 check_member_clash(expr_info, base['base'], data, source)
434 def check_command(expr, expr_info):
435 name = expr['command']
436 allow_star = expr.has_key('gen')
438 check_type(expr_info, "'data' for command '%s'" % name,
439 expr.get('data'), allow_dict=True, allow_optional=True,
440 allow_metas=['union', 'struct'], allow_star=allow_star)
441 returns_meta = ['union', 'struct']
442 if name in returns_whitelist:
443 returns_meta += ['built-in', 'alternate', 'enum']
444 check_type(expr_info, "'returns' for command '%s'" % name,
445 expr.get('returns'), allow_array=True, allow_dict=True,
446 allow_optional=True, allow_metas=returns_meta,
447 allow_star=allow_star)
449 def check_event(expr, expr_info):
450 global events
451 name = expr['event']
452 params = expr.get('data')
454 if name.upper() == 'MAX':
455 raise QAPIExprError(expr_info, "Event name 'MAX' cannot be created")
456 events.append(name)
457 check_type(expr_info, "'data' for event '%s'" % name,
458 expr.get('data'), allow_dict=True, allow_optional=True,
459 allow_metas=['union', 'struct'])
461 def check_union(expr, expr_info):
462 name = expr['union']
463 base = expr.get('base')
464 discriminator = expr.get('discriminator')
465 members = expr['data']
466 values = { 'MAX': '(automatic)' }
468 # If the object has a member 'base', its value must name a struct,
469 # and there must be a discriminator.
470 if base is not None:
471 if discriminator is None:
472 raise QAPIExprError(expr_info,
473 "Union '%s' requires a discriminator to go "
474 "along with base" %name)
476 # Two types of unions, determined by discriminator.
478 # With no discriminator it is a simple union.
479 if discriminator is None:
480 enum_define = None
481 allow_metas=['built-in', 'union', 'alternate', 'struct', 'enum']
482 if base is not None:
483 raise QAPIExprError(expr_info,
484 "Simple union '%s' must not have a base"
485 % name)
487 # Else, it's a flat union.
488 else:
489 # The object must have a string member 'base'.
490 if not isinstance(base, str):
491 raise QAPIExprError(expr_info,
492 "Flat union '%s' must have a string base field"
493 % name)
494 base_fields = find_base_fields(base)
495 if not base_fields:
496 raise QAPIExprError(expr_info,
497 "Base '%s' is not a valid struct"
498 % base)
500 # The value of member 'discriminator' must name a non-optional
501 # member of the base struct.
502 check_name(expr_info, "Discriminator of flat union '%s'" % name,
503 discriminator)
504 discriminator_type = base_fields.get(discriminator)
505 if not discriminator_type:
506 raise QAPIExprError(expr_info,
507 "Discriminator '%s' is not a member of base "
508 "struct '%s'"
509 % (discriminator, base))
510 enum_define = find_enum(discriminator_type)
511 allow_metas=['struct']
512 # Do not allow string discriminator
513 if not enum_define:
514 raise QAPIExprError(expr_info,
515 "Discriminator '%s' must be of enumeration "
516 "type" % discriminator)
518 # Check every branch
519 for (key, value) in members.items():
520 check_name(expr_info, "Member of union '%s'" % name, key)
522 # Each value must name a known type; furthermore, in flat unions,
523 # branches must be a struct with no overlapping member names
524 check_type(expr_info, "Member '%s' of union '%s'" % (key, name),
525 value, allow_array=True, allow_metas=allow_metas)
526 if base:
527 branch_struct = find_struct(value)
528 assert branch_struct
529 check_member_clash(expr_info, base, branch_struct['data'],
530 " of branch '%s'" % key)
532 # If the discriminator names an enum type, then all members
533 # of 'data' must also be members of the enum type.
534 if enum_define:
535 if not key in enum_define['enum_values']:
536 raise QAPIExprError(expr_info,
537 "Discriminator value '%s' is not found in "
538 "enum '%s'" %
539 (key, enum_define["enum_name"]))
541 # Otherwise, check for conflicts in the generated enum
542 else:
543 c_key = camel_to_upper(key)
544 if c_key in values:
545 raise QAPIExprError(expr_info,
546 "Union '%s' member '%s' clashes with '%s'"
547 % (name, key, values[c_key]))
548 values[c_key] = key
550 def check_alternate(expr, expr_info):
551 name = expr['alternate']
552 members = expr['data']
553 values = { 'MAX': '(automatic)' }
554 types_seen = {}
556 # Check every branch
557 for (key, value) in members.items():
558 check_name(expr_info, "Member of alternate '%s'" % name, key)
560 # Check for conflicts in the generated enum
561 c_key = camel_to_upper(key)
562 if c_key in values:
563 raise QAPIExprError(expr_info,
564 "Alternate '%s' member '%s' clashes with '%s'"
565 % (name, key, values[c_key]))
566 values[c_key] = key
568 # Ensure alternates have no type conflicts.
569 check_type(expr_info, "Member '%s' of alternate '%s'" % (key, name),
570 value,
571 allow_metas=['built-in', 'union', 'struct', 'enum'])
572 qtype = find_alternate_member_qtype(value)
573 assert qtype
574 if qtype in types_seen:
575 raise QAPIExprError(expr_info,
576 "Alternate '%s' member '%s' can't "
577 "be distinguished from member '%s'"
578 % (name, key, types_seen[qtype]))
579 types_seen[qtype] = key
581 def check_enum(expr, expr_info):
582 name = expr['enum']
583 members = expr.get('data')
584 values = { 'MAX': '(automatic)' }
586 if not isinstance(members, list):
587 raise QAPIExprError(expr_info,
588 "Enum '%s' requires an array for 'data'" % name)
589 for member in members:
590 check_name(expr_info, "Member of enum '%s'" %name, member,
591 enum_member=True)
592 key = camel_to_upper(member)
593 if key in values:
594 raise QAPIExprError(expr_info,
595 "Enum '%s' member '%s' clashes with '%s'"
596 % (name, member, values[key]))
597 values[key] = member
599 def check_struct(expr, expr_info):
600 name = expr['struct']
601 members = expr['data']
603 check_type(expr_info, "'data' for struct '%s'" % name, members,
604 allow_dict=True, allow_optional=True)
605 check_type(expr_info, "'base' for struct '%s'" % name, expr.get('base'),
606 allow_metas=['struct'])
607 if expr.get('base'):
608 check_member_clash(expr_info, expr['base'], expr['data'])
610 def check_exprs(schema):
611 for expr_elem in schema.exprs:
612 expr = expr_elem['expr']
613 info = expr_elem['info']
615 if expr.has_key('enum'):
616 check_enum(expr, info)
617 elif expr.has_key('union'):
618 check_union(expr, info)
619 elif expr.has_key('alternate'):
620 check_alternate(expr, info)
621 elif expr.has_key('struct'):
622 check_struct(expr, info)
623 elif expr.has_key('command'):
624 check_command(expr, info)
625 elif expr.has_key('event'):
626 check_event(expr, info)
627 else:
628 assert False, 'unexpected meta type'
630 def check_keys(expr_elem, meta, required, optional=[]):
631 expr = expr_elem['expr']
632 info = expr_elem['info']
633 name = expr[meta]
634 if not isinstance(name, str):
635 raise QAPIExprError(info,
636 "'%s' key must have a string value" % meta)
637 required = required + [ meta ]
638 for (key, value) in expr.items():
639 if not key in required and not key in optional:
640 raise QAPIExprError(info,
641 "Unknown key '%s' in %s '%s'"
642 % (key, meta, name))
643 if (key == 'gen' or key == 'success-response') and value != False:
644 raise QAPIExprError(info,
645 "'%s' of %s '%s' should only use false value"
646 % (key, meta, name))
647 for key in required:
648 if not expr.has_key(key):
649 raise QAPIExprError(info,
650 "Key '%s' is missing from %s '%s'"
651 % (key, meta, name))
654 def parse_schema(input_file):
655 global all_names
656 exprs = []
658 # First pass: read entire file into memory
659 try:
660 schema = QAPISchema(open(input_file, "r"))
661 except (QAPISchemaError, QAPIExprError), e:
662 print >>sys.stderr, e
663 exit(1)
665 try:
666 # Next pass: learn the types and check for valid expression keys. At
667 # this point, top-level 'include' has already been flattened.
668 for builtin in builtin_types.keys():
669 all_names[builtin] = 'built-in'
670 for expr_elem in schema.exprs:
671 expr = expr_elem['expr']
672 info = expr_elem['info']
673 if expr.has_key('enum'):
674 check_keys(expr_elem, 'enum', ['data'])
675 add_enum(expr['enum'], info, expr['data'])
676 elif expr.has_key('union'):
677 check_keys(expr_elem, 'union', ['data'],
678 ['base', 'discriminator'])
679 add_union(expr, info)
680 elif expr.has_key('alternate'):
681 check_keys(expr_elem, 'alternate', ['data'])
682 add_name(expr['alternate'], info, 'alternate')
683 elif expr.has_key('struct'):
684 check_keys(expr_elem, 'struct', ['data'], ['base'])
685 add_struct(expr, info)
686 elif expr.has_key('command'):
687 check_keys(expr_elem, 'command', [],
688 ['data', 'returns', 'gen', 'success-response'])
689 add_name(expr['command'], info, 'command')
690 elif expr.has_key('event'):
691 check_keys(expr_elem, 'event', [], ['data'])
692 add_name(expr['event'], info, 'event')
693 else:
694 raise QAPIExprError(expr_elem['info'],
695 "Expression is missing metatype")
696 exprs.append(expr)
698 # Try again for hidden UnionKind enum
699 for expr_elem in schema.exprs:
700 expr = expr_elem['expr']
701 if expr.has_key('union'):
702 if not discriminator_find_enum_define(expr):
703 add_enum('%sKind' % expr['union'], expr_elem['info'],
704 implicit=True)
705 elif expr.has_key('alternate'):
706 add_enum('%sKind' % expr['alternate'], expr_elem['info'],
707 implicit=True)
709 # Final pass - validate that exprs make sense
710 check_exprs(schema)
711 except QAPIExprError, e:
712 print >>sys.stderr, e
713 exit(1)
715 return exprs
717 def parse_args(typeinfo):
718 if isinstance(typeinfo, str):
719 struct = find_struct(typeinfo)
720 assert struct != None
721 typeinfo = struct['data']
723 for member in typeinfo:
724 argname = member
725 argentry = typeinfo[member]
726 optional = False
727 if member.startswith('*'):
728 argname = member[1:]
729 optional = True
730 # Todo: allow argentry to be OrderedDict, for providing the
731 # value of an optional argument.
732 yield (argname, argentry, optional)
734 def camel_case(name):
735 new_name = ''
736 first = True
737 for ch in name:
738 if ch in ['_', '-']:
739 first = True
740 elif first:
741 new_name += ch.upper()
742 first = False
743 else:
744 new_name += ch.lower()
745 return new_name
747 # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
748 # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
749 # ENUM24_Name -> ENUM24_NAME
750 def camel_to_upper(value):
751 c_fun_str = c_name(value, False)
752 if value.isupper():
753 return c_fun_str
755 new_name = ''
756 l = len(c_fun_str)
757 for i in range(l):
758 c = c_fun_str[i]
759 # When c is upper and no "_" appears before, do more checks
760 if c.isupper() and (i > 0) and c_fun_str[i - 1] != "_":
761 # Case 1: next string is lower
762 # Case 2: previous string is digit
763 if (i < (l - 1) and c_fun_str[i + 1].islower()) or \
764 c_fun_str[i - 1].isdigit():
765 new_name += '_'
766 new_name += c
767 return new_name.lstrip('_').upper()
769 def c_enum_const(type_name, const_name):
770 return camel_to_upper(type_name + '_' + const_name)
772 c_name_trans = string.maketrans('.-', '__')
774 # Map @name to a valid C identifier.
775 # If @protect, avoid returning certain ticklish identifiers (like
776 # C keywords) by prepending "q_".
778 # Used for converting 'name' from a 'name':'type' qapi definition
779 # into a generated struct member, as well as converting type names
780 # into substrings of a generated C function name.
781 # '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
782 # protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
783 def c_name(name, protect=True):
784 # ANSI X3J11/88-090, 3.1.1
785 c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
786 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
787 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
788 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
789 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
790 # ISO/IEC 9899:1999, 6.4.1
791 c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
792 # ISO/IEC 9899:2011, 6.4.1
793 c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
794 '_Static_assert', '_Thread_local'])
795 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
796 # excluding _.*
797 gcc_words = set(['asm', 'typeof'])
798 # C++ ISO/IEC 14882:2003 2.11
799 cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete',
800 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
801 'namespace', 'new', 'operator', 'private', 'protected',
802 'public', 'reinterpret_cast', 'static_cast', 'template',
803 'this', 'throw', 'true', 'try', 'typeid', 'typename',
804 'using', 'virtual', 'wchar_t',
805 # alternative representations
806 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
807 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
808 # namespace pollution:
809 polluted_words = set(['unix', 'errno'])
810 if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
811 return "q_" + name
812 return name.translate(c_name_trans)
814 # Map type @name to the C typedef name for the list form.
816 # ['Name'] -> 'NameList', ['x-Foo'] -> 'x_FooList', ['int'] -> 'intList'
817 def c_list_type(name):
818 return type_name(name) + 'List'
820 # Map type @value to the C typedef form.
822 # Used for converting 'type' from a 'member':'type' qapi definition
823 # into the alphanumeric portion of the type for a generated C parameter,
824 # as well as generated C function names. See c_type() for the rest of
825 # the conversion such as adding '*' on pointer types.
826 # 'int' -> 'int', '[x-Foo]' -> 'x_FooList', '__a.b_c' -> '__a_b_c'
827 def type_name(value):
828 if type(value) == list:
829 return c_list_type(value[0])
830 if value in builtin_types.keys():
831 return value
832 return c_name(value)
834 def add_name(name, info, meta, implicit = False):
835 global all_names
836 check_name(info, "'%s'" % meta, name)
837 if name in all_names:
838 raise QAPIExprError(info,
839 "%s '%s' is already defined"
840 % (all_names[name], name))
841 if not implicit and name[-4:] == 'Kind':
842 raise QAPIExprError(info,
843 "%s '%s' should not end in 'Kind'"
844 % (meta, name))
845 all_names[name] = meta
847 def add_struct(definition, info):
848 global struct_types
849 name = definition['struct']
850 add_name(name, info, 'struct')
851 struct_types.append(definition)
853 def find_struct(name):
854 global struct_types
855 for struct in struct_types:
856 if struct['struct'] == name:
857 return struct
858 return None
860 def add_union(definition, info):
861 global union_types
862 name = definition['union']
863 add_name(name, info, 'union')
864 union_types.append(definition)
866 def find_union(name):
867 global union_types
868 for union in union_types:
869 if union['union'] == name:
870 return union
871 return None
873 def add_enum(name, info, enum_values = None, implicit = False):
874 global enum_types
875 add_name(name, info, 'enum', implicit)
876 enum_types.append({"enum_name": name, "enum_values": enum_values})
878 def find_enum(name):
879 global enum_types
880 for enum in enum_types:
881 if enum['enum_name'] == name:
882 return enum
883 return None
885 def is_enum(name):
886 return find_enum(name) != None
888 eatspace = '\033EATSPACE.'
889 pointer_suffix = ' *' + eatspace
891 # Map type @name to its C type expression.
892 # If @is_param, const-qualify the string type.
894 # This function is used for computing the full C type of 'member':'name'.
895 # A special suffix is added in c_type() for pointer types, and it's
896 # stripped in mcgen(). So please notice this when you check the return
897 # value of c_type() outside mcgen().
898 def c_type(value, is_param=False):
899 if value == 'str':
900 if is_param:
901 return 'const char' + pointer_suffix
902 return 'char' + pointer_suffix
904 elif value == 'int':
905 return 'int64_t'
906 elif (value == 'int8' or value == 'int16' or value == 'int32' or
907 value == 'int64' or value == 'uint8' or value == 'uint16' or
908 value == 'uint32' or value == 'uint64'):
909 return value + '_t'
910 elif value == 'size':
911 return 'uint64_t'
912 elif value == 'bool':
913 return 'bool'
914 elif value == 'number':
915 return 'double'
916 elif type(value) == list:
917 return c_list_type(value[0]) + pointer_suffix
918 elif is_enum(value):
919 return c_name(value)
920 elif value == None:
921 return 'void'
922 elif value in events:
923 return camel_case(value) + 'Event' + pointer_suffix
924 else:
925 # complex type name
926 assert isinstance(value, str) and value != ""
927 return c_name(value) + pointer_suffix
929 def is_c_ptr(value):
930 return c_type(value).endswith(pointer_suffix)
932 def genindent(count):
933 ret = ""
934 for i in range(count):
935 ret += " "
936 return ret
938 indent_level = 0
940 def push_indent(indent_amount=4):
941 global indent_level
942 indent_level += indent_amount
944 def pop_indent(indent_amount=4):
945 global indent_level
946 indent_level -= indent_amount
948 def cgen(code, **kwds):
949 indent = genindent(indent_level)
950 lines = code.split('\n')
951 lines = map(lambda x: indent + x, lines)
952 return '\n'.join(lines) % kwds + '\n'
954 def mcgen(code, **kwds):
955 raw = cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
956 return re.sub(re.escape(eatspace) + ' *', '', raw)
958 def basename(filename):
959 return filename.split("/")[-1]
961 def guardname(filename):
962 guard = basename(filename).rsplit(".", 1)[0]
963 for substr in [".", " ", "-"]:
964 guard = guard.replace(substr, "_")
965 return guard.upper() + '_H'
967 def guardstart(name):
968 return mcgen('''
970 #ifndef %(name)s
971 #define %(name)s
973 ''',
974 name=guardname(name))
976 def guardend(name):
977 return mcgen('''
979 #endif /* %(name)s */
981 ''',
982 name=guardname(name))
984 def parse_command_line(extra_options = "", extra_long_options = []):
986 try:
987 opts, args = getopt.gnu_getopt(sys.argv[1:],
988 "chp:o:" + extra_options,
989 ["source", "header", "prefix=",
990 "output-dir="] + extra_long_options)
991 except getopt.GetoptError, err:
992 print >>sys.stderr, "%s: %s" % (sys.argv[0], str(err))
993 sys.exit(1)
995 output_dir = ""
996 prefix = ""
997 do_c = False
998 do_h = False
999 extra_opts = []
1001 for oa in opts:
1002 o, a = oa
1003 if o in ("-p", "--prefix"):
1004 prefix = a
1005 elif o in ("-o", "--output-dir"):
1006 output_dir = a + "/"
1007 elif o in ("-c", "--source"):
1008 do_c = True
1009 elif o in ("-h", "--header"):
1010 do_h = True
1011 else:
1012 extra_opts.append(oa)
1014 if not do_c and not do_h:
1015 do_c = True
1016 do_h = True
1018 if len(args) != 1:
1019 print >>sys.stderr, "%s: need exactly one argument" % sys.argv[0]
1020 sys.exit(1)
1021 input_file = args[0]
1023 return (input_file, output_dir, do_c, do_h, prefix, extra_opts)
1025 def open_output(output_dir, do_c, do_h, prefix, c_file, h_file,
1026 c_comment, h_comment):
1027 c_file = output_dir + prefix + c_file
1028 h_file = output_dir + prefix + h_file
1030 try:
1031 os.makedirs(output_dir)
1032 except os.error, e:
1033 if e.errno != errno.EEXIST:
1034 raise
1036 def maybe_open(really, name, opt):
1037 if really:
1038 return open(name, opt)
1039 else:
1040 import StringIO
1041 return StringIO.StringIO()
1043 fdef = maybe_open(do_c, c_file, 'w')
1044 fdecl = maybe_open(do_h, h_file, 'w')
1046 fdef.write(mcgen('''
1047 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
1048 %(comment)s
1049 ''',
1050 comment = c_comment))
1052 fdecl.write(mcgen('''
1053 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
1054 %(comment)s
1055 #ifndef %(guard)s
1056 #define %(guard)s
1058 ''',
1059 comment = h_comment, guard = guardname(h_file)))
1061 return (fdef, fdecl)
1063 def close_output(fdef, fdecl):
1064 fdecl.write('''
1065 #endif
1066 ''')
1067 fdecl.close()
1068 fdef.close()