4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013-2015 Red Hat Inc.
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.
15 from ordereddict
import OrderedDict
22 'str': 'QTYPE_QSTRING',
24 'number': 'QTYPE_QFLOAT',
25 'bool': 'QTYPE_QBOOL',
27 'int16': 'QTYPE_QINT',
28 'int32': 'QTYPE_QINT',
29 'int64': 'QTYPE_QINT',
30 'uint8': 'QTYPE_QINT',
31 'uint16': 'QTYPE_QINT',
32 'uint32': 'QTYPE_QINT',
33 'uint64': 'QTYPE_QINT',
37 # Whitelist of commands allowed to return a non-dictionary
40 'human-monitor-command',
41 'query-migrate-cache-size',
48 'guest-fsfreeze-freeze',
49 'guest-fsfreeze-freeze-list',
50 'guest-fsfreeze-status',
51 'guest-fsfreeze-thaw',
55 'guest-sync-delimited',
57 # From qapi-schema-test:
67 def error_path(parent
):
70 res
= ("In file included from %s:%d:\n" % (parent
['file'],
71 parent
['line'])) + res
72 parent
= parent
['parent']
75 class QAPISchemaError(Exception):
76 def __init__(self
, schema
, msg
):
77 self
.input_file
= schema
.input_file
80 self
.line
= schema
.line
81 for ch
in schema
.src
[schema
.line_pos
:schema
.pos
]:
83 self
.col
= (self
.col
+ 7) % 8 + 1
86 self
.info
= schema
.parent_info
89 return error_path(self
.info
) + \
90 "%s:%d:%d: %s" % (self
.input_file
, self
.line
, self
.col
, self
.msg
)
92 class QAPIExprError(Exception):
93 def __init__(self
, expr_info
, msg
):
98 return error_path(self
.info
['parent']) + \
99 "%s:%d: %s" % (self
.info
['file'], self
.info
['line'], self
.msg
)
103 def __init__(self
, fp
, input_relname
=None, include_hist
=[],
104 previously_included
=[], parent_info
=None):
105 """ include_hist is a stack used to detect inclusion cycles
106 previously_included is a global state used to avoid multiple
107 inclusions of the same file"""
108 input_fname
= os
.path
.abspath(fp
.name
)
109 if input_relname
is None:
110 input_relname
= fp
.name
111 self
.input_dir
= os
.path
.dirname(input_fname
)
112 self
.input_file
= input_relname
113 self
.include_hist
= include_hist
+ [(input_relname
, input_fname
)]
114 previously_included
.append(input_fname
)
115 self
.parent_info
= parent_info
117 if self
.src
== '' or self
.src
[-1] != '\n':
125 while self
.tok
!= None:
126 expr_info
= {'file': input_relname
, 'line': self
.line
, 'parent': self
.parent_info
}
127 expr
= self
.get_expr(False)
128 if isinstance(expr
, dict) and "include" in expr
:
130 raise QAPIExprError(expr_info
, "Invalid 'include' directive")
131 include
= expr
["include"]
132 if not isinstance(include
, str):
133 raise QAPIExprError(expr_info
,
134 'Expected a file name (string), got: %s'
136 include_path
= os
.path
.join(self
.input_dir
, include
)
137 for elem
in self
.include_hist
:
138 if include_path
== elem
[1]:
139 raise QAPIExprError(expr_info
, "Inclusion loop for %s"
141 # skip multiple include of the same file
142 if include_path
in previously_included
:
145 fobj
= open(include_path
, 'r')
147 raise QAPIExprError(expr_info
,
148 '%s: %s' % (e
.strerror
, include
))
149 exprs_include
= QAPISchema(fobj
, include
, self
.include_hist
,
150 previously_included
, expr_info
)
151 self
.exprs
.extend(exprs_include
.exprs
)
153 expr_elem
= {'expr': expr
,
155 self
.exprs
.append(expr_elem
)
159 self
.tok
= self
.src
[self
.cursor
]
160 self
.pos
= self
.cursor
165 self
.cursor
= self
.src
.find('\n', self
.cursor
)
166 elif self
.tok
in ['{', '}', ':', ',', '[', ']']:
168 elif self
.tok
== "'":
172 ch
= self
.src
[self
.cursor
]
175 raise QAPISchemaError(self
,
176 'Missing terminating "\'"')
190 for x
in range(0, 4):
191 ch
= self
.src
[self
.cursor
]
193 if ch
not in "0123456789abcdefABCDEF":
194 raise QAPISchemaError(self
,
195 '\\u escape needs 4 '
197 value
= (value
<< 4) + int(ch
, 16)
198 # If Python 2 and 3 didn't disagree so much on
199 # how to handle Unicode, then we could allow
200 # Unicode string defaults. But most of QAPI is
201 # ASCII-only, so we aren't losing much for now.
202 if not value
or value
> 0x7f:
203 raise QAPISchemaError(self
,
204 'For now, \\u escape '
205 'only supports non-zero '
206 'values up to \\u007f')
211 raise QAPISchemaError(self
,
212 "Unknown escape \\%s" %ch
)
221 elif self
.tok
in "tfn":
222 val
= self
.src
[self
.cursor
- 1:]
223 if val
.startswith("true"):
227 elif val
.startswith("false"):
231 elif val
.startswith("null"):
235 elif self
.tok
== '\n':
236 if self
.cursor
== len(self
.src
):
240 self
.line_pos
= self
.cursor
241 elif not self
.tok
.isspace():
242 raise QAPISchemaError(self
, 'Stray "%s"' % self
.tok
)
244 def get_members(self
):
250 raise QAPISchemaError(self
, 'Expected string or "}"')
255 raise QAPISchemaError(self
, 'Expected ":"')
258 raise QAPISchemaError(self
, 'Duplicate key "%s"' % key
)
259 expr
[key
] = self
.get_expr(True)
264 raise QAPISchemaError(self
, 'Expected "," or "}"')
267 raise QAPISchemaError(self
, 'Expected string')
269 def get_values(self
):
274 if not self
.tok
in "{['tfn":
275 raise QAPISchemaError(self
, 'Expected "{", "[", "]", string, '
278 expr
.append(self
.get_expr(True))
283 raise QAPISchemaError(self
, 'Expected "," or "]"')
286 def get_expr(self
, nested
):
287 if self
.tok
!= '{' and not nested
:
288 raise QAPISchemaError(self
, 'Expected "{"')
291 expr
= self
.get_members()
292 elif self
.tok
== '[':
294 expr
= self
.get_values()
295 elif self
.tok
in "'tfn":
299 raise QAPISchemaError(self
, 'Expected "{", "[" or string')
302 def find_base_fields(base
):
303 base_struct_define
= find_struct(base
)
304 if not base_struct_define
:
306 return base_struct_define
['data']
308 # Return the qtype of an alternate branch, or None on error.
309 def find_alternate_member_qtype(qapi_type
):
310 if builtin_types
.has_key(qapi_type
):
311 return builtin_types
[qapi_type
]
312 elif find_struct(qapi_type
):
314 elif find_enum(qapi_type
):
315 return "QTYPE_QSTRING"
316 elif find_union(qapi_type
):
320 # Return the discriminator enum define if discriminator is specified as an
321 # enum type, otherwise return None.
322 def discriminator_find_enum_define(expr
):
323 base
= expr
.get('base')
324 discriminator
= expr
.get('discriminator')
326 if not (discriminator
and base
):
329 base_fields
= find_base_fields(base
)
333 discriminator_type
= base_fields
.get(discriminator
)
334 if not discriminator_type
:
337 return find_enum(discriminator_type
)
339 valid_name
= re
.compile('^[a-zA-Z_][a-zA-Z0-9_.-]*$')
340 def check_name(expr_info
, source
, name
, allow_optional
= False,
341 enum_member
= False):
345 if not isinstance(name
, str):
346 raise QAPIExprError(expr_info
,
347 "%s requires a string name" % source
)
348 if name
.startswith('*'):
349 membername
= name
[1:]
350 if not allow_optional
:
351 raise QAPIExprError(expr_info
,
352 "%s does not allow optional name '%s'"
354 # Enum members can start with a digit, because the generated C
355 # code always prefixes it with the enum name
357 membername
= '_' + membername
358 if not valid_name
.match(membername
):
359 raise QAPIExprError(expr_info
,
360 "%s uses invalid name '%s'" % (source
, name
))
362 def check_type(expr_info
, source
, value
, allow_array
= False,
363 allow_dict
= False, allow_optional
= False,
364 allow_star
= False, allow_metas
= []):
371 if allow_star
and value
== '**':
374 # Check if array type for value is okay
375 if isinstance(value
, list):
377 raise QAPIExprError(expr_info
,
378 "%s cannot be an array" % source
)
379 if len(value
) != 1 or not isinstance(value
[0], str):
380 raise QAPIExprError(expr_info
,
381 "%s: array type must contain single type name"
384 orig_value
= "array of %s" %value
386 # Check if type name for value is okay
387 if isinstance(value
, str):
389 raise QAPIExprError(expr_info
,
390 "%s uses '**' but did not request 'gen':false"
392 if not value
in all_names
:
393 raise QAPIExprError(expr_info
,
394 "%s uses unknown type '%s'"
395 % (source
, orig_value
))
396 if not all_names
[value
] in allow_metas
:
397 raise QAPIExprError(expr_info
,
398 "%s cannot use %s type '%s'"
399 % (source
, all_names
[value
], orig_value
))
402 # value is a dictionary, check that each member is okay
403 if not isinstance(value
, OrderedDict
):
404 raise QAPIExprError(expr_info
,
405 "%s should be a dictionary" % source
)
407 raise QAPIExprError(expr_info
,
408 "%s should be a type name" % source
)
409 for (key
, arg
) in value
.items():
410 check_name(expr_info
, "Member of %s" % source
, key
,
411 allow_optional
=allow_optional
)
412 # Todo: allow dictionaries to represent default values of
413 # an optional argument.
414 check_type(expr_info
, "Member '%s' of %s" % (key
, source
), arg
,
415 allow_array
=True, allow_star
=allow_star
,
416 allow_metas
=['built-in', 'union', 'alternate', 'struct',
419 def check_member_clash(expr_info
, base_name
, data
, source
= ""):
420 base
= find_struct(base_name
)
422 base_members
= base
['data']
423 for key
in data
.keys():
424 if key
.startswith('*'):
426 if key
in base_members
or "*" + key
in base_members
:
427 raise QAPIExprError(expr_info
,
428 "Member name '%s'%s clashes with base '%s'"
429 % (key
, source
, base_name
))
431 check_member_clash(expr_info
, base
['base'], data
, source
)
433 def check_command(expr
, expr_info
):
434 name
= expr
['command']
435 allow_star
= expr
.has_key('gen')
437 check_type(expr_info
, "'data' for command '%s'" % name
,
438 expr
.get('data'), allow_dict
=True, allow_optional
=True,
439 allow_metas
=['union', 'struct'], allow_star
=allow_star
)
440 returns_meta
= ['union', 'struct']
441 if name
in returns_whitelist
:
442 returns_meta
+= ['built-in', 'alternate', 'enum']
443 check_type(expr_info
, "'returns' for command '%s'" % name
,
444 expr
.get('returns'), allow_array
=True, allow_dict
=True,
445 allow_optional
=True, allow_metas
=returns_meta
,
446 allow_star
=allow_star
)
448 def check_event(expr
, expr_info
):
451 params
= expr
.get('data')
453 if name
.upper() == 'MAX':
454 raise QAPIExprError(expr_info
, "Event name 'MAX' cannot be created")
456 check_type(expr_info
, "'data' for event '%s'" % name
,
457 expr
.get('data'), allow_dict
=True, allow_optional
=True,
458 allow_metas
=['union', 'struct'])
460 def check_union(expr
, expr_info
):
462 base
= expr
.get('base')
463 discriminator
= expr
.get('discriminator')
464 members
= expr
['data']
465 values
= { 'MAX': '(automatic)' }
467 # If the object has a member 'base', its value must name a struct,
468 # and there must be a discriminator.
470 if discriminator
is None:
471 raise QAPIExprError(expr_info
,
472 "Union '%s' requires a discriminator to go "
473 "along with base" %name
)
475 # Two types of unions, determined by discriminator.
477 # With no discriminator it is a simple union.
478 if discriminator
is None:
480 allow_metas
=['built-in', 'union', 'alternate', 'struct', 'enum']
482 raise QAPIExprError(expr_info
,
483 "Simple union '%s' must not have a base"
486 # Else, it's a flat union.
488 # The object must have a string member 'base'.
489 if not isinstance(base
, str):
490 raise QAPIExprError(expr_info
,
491 "Flat union '%s' must have a string base field"
493 base_fields
= find_base_fields(base
)
495 raise QAPIExprError(expr_info
,
496 "Base '%s' is not a valid struct"
499 # The value of member 'discriminator' must name a non-optional
500 # member of the base struct.
501 check_name(expr_info
, "Discriminator of flat union '%s'" % name
,
503 discriminator_type
= base_fields
.get(discriminator
)
504 if not discriminator_type
:
505 raise QAPIExprError(expr_info
,
506 "Discriminator '%s' is not a member of base "
508 % (discriminator
, base
))
509 enum_define
= find_enum(discriminator_type
)
510 allow_metas
=['struct']
511 # Do not allow string discriminator
513 raise QAPIExprError(expr_info
,
514 "Discriminator '%s' must be of enumeration "
515 "type" % discriminator
)
518 for (key
, value
) in members
.items():
519 check_name(expr_info
, "Member of union '%s'" % name
, key
)
521 # Each value must name a known type; furthermore, in flat unions,
522 # branches must be a struct with no overlapping member names
523 check_type(expr_info
, "Member '%s' of union '%s'" % (key
, name
),
524 value
, allow_array
=True, allow_metas
=allow_metas
)
526 branch_struct
= find_struct(value
)
528 check_member_clash(expr_info
, base
, branch_struct
['data'],
529 " of branch '%s'" % key
)
531 # If the discriminator names an enum type, then all members
532 # of 'data' must also be members of the enum type.
534 if not key
in enum_define
['enum_values']:
535 raise QAPIExprError(expr_info
,
536 "Discriminator value '%s' is not found in "
538 (key
, enum_define
["enum_name"]))
540 # Otherwise, check for conflicts in the generated enum
542 c_key
= camel_to_upper(key
)
544 raise QAPIExprError(expr_info
,
545 "Union '%s' member '%s' clashes with '%s'"
546 % (name
, key
, values
[c_key
]))
549 def check_alternate(expr
, expr_info
):
550 name
= expr
['alternate']
551 members
= expr
['data']
552 values
= { 'MAX': '(automatic)' }
556 for (key
, value
) in members
.items():
557 check_name(expr_info
, "Member of alternate '%s'" % name
, key
)
559 # Check for conflicts in the generated enum
560 c_key
= camel_to_upper(key
)
562 raise QAPIExprError(expr_info
,
563 "Alternate '%s' member '%s' clashes with '%s'"
564 % (name
, key
, values
[c_key
]))
567 # Ensure alternates have no type conflicts.
568 check_type(expr_info
, "Member '%s' of alternate '%s'" % (key
, name
),
570 allow_metas
=['built-in', 'union', 'struct', 'enum'])
571 qtype
= find_alternate_member_qtype(value
)
573 if qtype
in types_seen
:
574 raise QAPIExprError(expr_info
,
575 "Alternate '%s' member '%s' can't "
576 "be distinguished from member '%s'"
577 % (name
, key
, types_seen
[qtype
]))
578 types_seen
[qtype
] = key
580 def check_enum(expr
, expr_info
):
582 members
= expr
.get('data')
583 values
= { 'MAX': '(automatic)' }
585 if not isinstance(members
, list):
586 raise QAPIExprError(expr_info
,
587 "Enum '%s' requires an array for 'data'" % name
)
588 for member
in members
:
589 check_name(expr_info
, "Member of enum '%s'" %name
, member
,
591 key
= camel_to_upper(member
)
593 raise QAPIExprError(expr_info
,
594 "Enum '%s' member '%s' clashes with '%s'"
595 % (name
, member
, values
[key
]))
598 def check_struct(expr
, expr_info
):
599 name
= expr
['struct']
600 members
= expr
['data']
602 check_type(expr_info
, "'data' for struct '%s'" % name
, members
,
603 allow_dict
=True, allow_optional
=True)
604 check_type(expr_info
, "'base' for struct '%s'" % name
, expr
.get('base'),
605 allow_metas
=['struct'])
607 check_member_clash(expr_info
, expr
['base'], expr
['data'])
609 def check_exprs(schema
):
610 for expr_elem
in schema
.exprs
:
611 expr
= expr_elem
['expr']
612 info
= expr_elem
['info']
614 if expr
.has_key('enum'):
615 check_enum(expr
, info
)
616 elif expr
.has_key('union'):
617 check_union(expr
, info
)
618 elif expr
.has_key('alternate'):
619 check_alternate(expr
, info
)
620 elif expr
.has_key('struct'):
621 check_struct(expr
, info
)
622 elif expr
.has_key('command'):
623 check_command(expr
, info
)
624 elif expr
.has_key('event'):
625 check_event(expr
, info
)
627 assert False, 'unexpected meta type'
629 def check_keys(expr_elem
, meta
, required
, optional
=[]):
630 expr
= expr_elem
['expr']
631 info
= expr_elem
['info']
633 if not isinstance(name
, str):
634 raise QAPIExprError(info
,
635 "'%s' key must have a string value" % meta
)
636 required
= required
+ [ meta
]
637 for (key
, value
) in expr
.items():
638 if not key
in required
and not key
in optional
:
639 raise QAPIExprError(info
,
640 "Unknown key '%s' in %s '%s'"
642 if (key
== 'gen' or key
== 'success-response') and value
!= False:
643 raise QAPIExprError(info
,
644 "'%s' of %s '%s' should only use false value"
647 if not expr
.has_key(key
):
648 raise QAPIExprError(info
,
649 "Key '%s' is missing from %s '%s'"
653 def parse_schema(input_file
):
657 # First pass: read entire file into memory
659 schema
= QAPISchema(open(input_file
, "r"))
660 except (QAPISchemaError
, QAPIExprError
), e
:
661 print >>sys
.stderr
, e
665 # Next pass: learn the types and check for valid expression keys. At
666 # this point, top-level 'include' has already been flattened.
667 for builtin
in builtin_types
.keys():
668 all_names
[builtin
] = 'built-in'
669 for expr_elem
in schema
.exprs
:
670 expr
= expr_elem
['expr']
671 info
= expr_elem
['info']
672 if expr
.has_key('enum'):
673 check_keys(expr_elem
, 'enum', ['data'])
674 add_enum(expr
['enum'], info
, expr
['data'])
675 elif expr
.has_key('union'):
676 check_keys(expr_elem
, 'union', ['data'],
677 ['base', 'discriminator'])
678 add_union(expr
, info
)
679 elif expr
.has_key('alternate'):
680 check_keys(expr_elem
, 'alternate', ['data'])
681 add_name(expr
['alternate'], info
, 'alternate')
682 elif expr
.has_key('struct'):
683 check_keys(expr_elem
, 'struct', ['data'], ['base'])
684 add_struct(expr
, info
)
685 elif expr
.has_key('command'):
686 check_keys(expr_elem
, 'command', [],
687 ['data', 'returns', 'gen', 'success-response'])
688 add_name(expr
['command'], info
, 'command')
689 elif expr
.has_key('event'):
690 check_keys(expr_elem
, 'event', [], ['data'])
691 add_name(expr
['event'], info
, 'event')
693 raise QAPIExprError(expr_elem
['info'],
694 "Expression is missing metatype")
697 # Try again for hidden UnionKind enum
698 for expr_elem
in schema
.exprs
:
699 expr
= expr_elem
['expr']
700 if expr
.has_key('union'):
701 if not discriminator_find_enum_define(expr
):
702 add_enum('%sKind' % expr
['union'], expr_elem
['info'],
704 elif expr
.has_key('alternate'):
705 add_enum('%sKind' % expr
['alternate'], expr_elem
['info'],
708 # Final pass - validate that exprs make sense
710 except QAPIExprError
, e
:
711 print >>sys
.stderr
, e
716 def parse_args(typeinfo
):
717 if isinstance(typeinfo
, str):
718 struct
= find_struct(typeinfo
)
719 assert struct
!= None
720 typeinfo
= struct
['data']
722 for member
in typeinfo
:
724 argentry
= typeinfo
[member
]
726 if member
.startswith('*'):
729 # Todo: allow argentry to be OrderedDict, for providing the
730 # value of an optional argument.
731 yield (argname
, argentry
, optional
)
733 def camel_case(name
):
740 new_name
+= ch
.upper()
743 new_name
+= ch
.lower()
746 # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
747 # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
748 # ENUM24_Name -> ENUM24_NAME
749 def camel_to_upper(value
):
750 c_fun_str
= c_name(value
, False)
758 # When c is upper and no "_" appears before, do more checks
759 if c
.isupper() and (i
> 0) and c_fun_str
[i
- 1] != "_":
760 # Case 1: next string is lower
761 # Case 2: previous string is digit
762 if (i
< (l
- 1) and c_fun_str
[i
+ 1].islower()) or \
763 c_fun_str
[i
- 1].isdigit():
766 return new_name
.lstrip('_').upper()
768 def c_enum_const(type_name
, const_name
):
769 return camel_to_upper(type_name
+ '_' + const_name
)
771 c_name_trans
= string
.maketrans('.-', '__')
773 # Map @name to a valid C identifier.
774 # If @protect, avoid returning certain ticklish identifiers (like
775 # C keywords) by prepending "q_".
777 # Used for converting 'name' from a 'name':'type' qapi definition
778 # into a generated struct member, as well as converting type names
779 # into substrings of a generated C function name.
780 # '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
781 # protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
782 def c_name(name
, protect
=True):
783 # ANSI X3J11/88-090, 3.1.1
784 c89_words
= set(['auto', 'break', 'case', 'char', 'const', 'continue',
785 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
786 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
787 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
788 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
789 # ISO/IEC 9899:1999, 6.4.1
790 c99_words
= set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
791 # ISO/IEC 9899:2011, 6.4.1
792 c11_words
= set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
793 '_Static_assert', '_Thread_local'])
794 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
796 gcc_words
= set(['asm', 'typeof'])
797 # C++ ISO/IEC 14882:2003 2.11
798 cpp_words
= set(['bool', 'catch', 'class', 'const_cast', 'delete',
799 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
800 'namespace', 'new', 'operator', 'private', 'protected',
801 'public', 'reinterpret_cast', 'static_cast', 'template',
802 'this', 'throw', 'true', 'try', 'typeid', 'typename',
803 'using', 'virtual', 'wchar_t',
804 # alternative representations
805 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
806 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
807 # namespace pollution:
808 polluted_words
= set(['unix', 'errno'])
809 if protect
and (name
in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words
):
811 return name
.translate(c_name_trans
)
813 # Map type @name to the C typedef name for the list form.
815 # ['Name'] -> 'NameList', ['x-Foo'] -> 'x_FooList', ['int'] -> 'intList'
816 def c_list_type(name
):
817 return type_name(name
) + 'List'
819 # Map type @value to the C typedef form.
821 # Used for converting 'type' from a 'member':'type' qapi definition
822 # into the alphanumeric portion of the type for a generated C parameter,
823 # as well as generated C function names. See c_type() for the rest of
824 # the conversion such as adding '*' on pointer types.
825 # 'int' -> 'int', '[x-Foo]' -> 'x_FooList', '__a.b_c' -> '__a_b_c'
826 def type_name(value
):
827 if type(value
) == list:
828 return c_list_type(value
[0])
829 if value
in builtin_types
.keys():
833 def add_name(name
, info
, meta
, implicit
= False):
835 check_name(info
, "'%s'" % meta
, name
)
836 if name
in all_names
:
837 raise QAPIExprError(info
,
838 "%s '%s' is already defined"
839 % (all_names
[name
], name
))
840 if not implicit
and name
[-4:] == 'Kind':
841 raise QAPIExprError(info
,
842 "%s '%s' should not end in 'Kind'"
844 all_names
[name
] = meta
846 def add_struct(definition
, info
):
848 name
= definition
['struct']
849 add_name(name
, info
, 'struct')
850 struct_types
.append(definition
)
852 def find_struct(name
):
854 for struct
in struct_types
:
855 if struct
['struct'] == name
:
859 def add_union(definition
, info
):
861 name
= definition
['union']
862 add_name(name
, info
, 'union')
863 union_types
.append(definition
)
865 def find_union(name
):
867 for union
in union_types
:
868 if union
['union'] == name
:
872 def add_enum(name
, info
, enum_values
= None, implicit
= False):
874 add_name(name
, info
, 'enum', implicit
)
875 enum_types
.append({"enum_name": name
, "enum_values": enum_values
})
879 for enum
in enum_types
:
880 if enum
['enum_name'] == name
:
885 return find_enum(name
) != None
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):
900 return 'const char' + pointer_suffix
901 return 'char' + pointer_suffix
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'):
909 elif value
== 'size':
911 elif value
== 'bool':
913 elif value
== 'number':
915 elif type(value
) == list:
916 return c_list_type(value
[0]) + pointer_suffix
921 elif value
in events
:
922 return camel_case(value
) + 'Event' + pointer_suffix
925 assert isinstance(value
, str) and value
!= ""
926 return c_name(value
) + pointer_suffix
929 return c_type(value
).endswith(pointer_suffix
)
931 def genindent(count
):
933 for i
in range(count
):
939 def push_indent(indent_amount
=4):
941 indent_level
+= indent_amount
943 def pop_indent(indent_amount
=4):
945 indent_level
-= indent_amount
947 def cgen(code
, **kwds
):
948 indent
= genindent(indent_level
)
949 lines
= code
.split('\n')
950 lines
= map(lambda x
: indent
+ x
, lines
)
951 return '\n'.join(lines
) % kwds
+ '\n'
953 def mcgen(code
, **kwds
):
954 raw
= cgen('\n'.join(code
.split('\n')[1:-1]), **kwds
)
955 return re
.sub(re
.escape(eatspace
) + ' *', '', raw
)
957 def basename(filename
):
958 return filename
.split("/")[-1]
960 def guardname(filename
):
961 guard
= basename(filename
).rsplit(".", 1)[0]
962 for substr
in [".", " ", "-"]:
963 guard
= guard
.replace(substr
, "_")
964 return guard
.upper() + '_H'
966 def guardstart(name
):
973 name
=guardname(name
))
978 #endif /* %(name)s */
981 name
=guardname(name
))
983 def parse_command_line(extra_options
= "", extra_long_options
= []):
986 opts
, args
= getopt
.gnu_getopt(sys
.argv
[1:],
987 "chp:o:" + extra_options
,
988 ["source", "header", "prefix=",
989 "output-dir="] + extra_long_options
)
990 except getopt
.GetoptError
, err
:
991 print >>sys
.stderr
, "%s: %s" % (sys
.argv
[0], str(err
))
1002 if o
in ("-p", "--prefix"):
1004 elif o
in ("-o", "--output-dir"):
1005 output_dir
= a
+ "/"
1006 elif o
in ("-c", "--source"):
1008 elif o
in ("-h", "--header"):
1011 extra_opts
.append(oa
)
1013 if not do_c
and not do_h
:
1018 print >>sys
.stderr
, "%s: need exactly one argument" % sys
.argv
[0]
1020 input_file
= args
[0]
1022 return (input_file
, output_dir
, do_c
, do_h
, prefix
, extra_opts
)