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
23 'str': 'QTYPE_QSTRING',
25 'number': 'QTYPE_QFLOAT',
26 'bool': 'QTYPE_QBOOL',
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',
38 # Whitelist of commands allowed to return a non-dictionary
41 'human-monitor-command',
42 'query-migrate-cache-size',
49 'guest-fsfreeze-freeze',
50 'guest-fsfreeze-freeze-list',
51 'guest-fsfreeze-status',
52 'guest-fsfreeze-thaw',
56 'guest-sync-delimited',
58 # From qapi-schema-test:
68 def error_path(parent
):
71 res
= ("In file included from %s:%d:\n" % (parent
['file'],
72 parent
['line'])) + res
73 parent
= parent
['parent']
76 class QAPISchemaError(Exception):
77 def __init__(self
, schema
, msg
):
78 self
.input_file
= schema
.input_file
81 self
.line
= schema
.line
82 for ch
in schema
.src
[schema
.line_pos
:schema
.pos
]:
84 self
.col
= (self
.col
+ 7) % 8 + 1
87 self
.info
= schema
.parent_info
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
):
99 return error_path(self
.info
['parent']) + \
100 "%s:%d: %s" % (self
.info
['file'], self
.info
['line'], self
.msg
)
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
118 if self
.src
== '' or self
.src
[-1] != '\n':
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
:
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'
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"
142 # skip multiple include of the same file
143 if include_path
in previously_included
:
146 fobj
= open(include_path
, 'r')
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
)
154 expr_elem
= {'expr': expr
,
156 self
.exprs
.append(expr_elem
)
160 self
.tok
= self
.src
[self
.cursor
]
161 self
.pos
= self
.cursor
166 self
.cursor
= self
.src
.find('\n', self
.cursor
)
167 elif self
.tok
in ['{', '}', ':', ',', '[', ']']:
169 elif self
.tok
== "'":
173 ch
= self
.src
[self
.cursor
]
176 raise QAPISchemaError(self
,
177 'Missing terminating "\'"')
191 for x
in range(0, 4):
192 ch
= self
.src
[self
.cursor
]
194 if ch
not in "0123456789abcdefABCDEF":
195 raise QAPISchemaError(self
,
196 '\\u escape needs 4 '
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')
212 raise QAPISchemaError(self
,
213 "Unknown escape \\%s" %ch
)
222 elif self
.tok
in "tfn":
223 val
= self
.src
[self
.cursor
- 1:]
224 if val
.startswith("true"):
228 elif val
.startswith("false"):
232 elif val
.startswith("null"):
236 elif self
.tok
== '\n':
237 if self
.cursor
== len(self
.src
):
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
):
251 raise QAPISchemaError(self
, 'Expected string or "}"')
256 raise QAPISchemaError(self
, 'Expected ":"')
259 raise QAPISchemaError(self
, 'Duplicate key "%s"' % key
)
260 expr
[key
] = self
.get_expr(True)
265 raise QAPISchemaError(self
, 'Expected "," or "}"')
268 raise QAPISchemaError(self
, 'Expected string')
270 def get_values(self
):
275 if not self
.tok
in "{['tfn":
276 raise QAPISchemaError(self
, 'Expected "{", "[", "]", string, '
279 expr
.append(self
.get_expr(True))
284 raise QAPISchemaError(self
, 'Expected "," or "]"')
287 def get_expr(self
, nested
):
288 if self
.tok
!= '{' and not nested
:
289 raise QAPISchemaError(self
, 'Expected "{"')
292 expr
= self
.get_members()
293 elif self
.tok
== '[':
295 expr
= self
.get_values()
296 elif self
.tok
in "'tfn":
300 raise QAPISchemaError(self
, 'Expected "{", "[" or string')
303 def find_base_fields(base
):
304 base_struct_define
= find_struct(base
)
305 if not base_struct_define
:
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
):
315 elif find_enum(qapi_type
):
316 return "QTYPE_QSTRING"
317 elif find_union(qapi_type
):
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
):
330 base_fields
= find_base_fields(base
)
334 discriminator_type
= base_fields
.get(discriminator
)
335 if not discriminator_type
:
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):
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'"
355 # Enum members can start with a digit, because the generated C
356 # code always prefixes it with the enum name
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
= []):
372 if allow_star
and value
== '**':
375 # Check if array type for value is okay
376 if isinstance(value
, list):
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"
385 orig_value
= "array of %s" %value
387 # Check if type name for value is okay
388 if isinstance(value
, str):
390 raise QAPIExprError(expr_info
,
391 "%s uses '**' but did not request 'gen':false"
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
))
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
)
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',
420 def check_member_clash(expr_info
, base_name
, data
, source
= ""):
421 base
= find_struct(base_name
)
423 base_members
= base
['data']
424 for key
in data
.keys():
425 if key
.startswith('*'):
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
))
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
):
452 params
= expr
.get('data')
454 if name
.upper() == 'MAX':
455 raise QAPIExprError(expr_info
, "Event name 'MAX' cannot be created")
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
):
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.
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:
481 allow_metas
=['built-in', 'union', 'alternate', 'struct', 'enum']
483 raise QAPIExprError(expr_info
,
484 "Simple union '%s' must not have a base"
487 # Else, it's a flat union.
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"
494 base_fields
= find_base_fields(base
)
496 raise QAPIExprError(expr_info
,
497 "Base '%s' is not a valid struct"
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
,
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 "
509 % (discriminator
, base
))
510 enum_define
= find_enum(discriminator_type
)
511 allow_metas
=['struct']
512 # Do not allow string discriminator
514 raise QAPIExprError(expr_info
,
515 "Discriminator '%s' must be of enumeration "
516 "type" % discriminator
)
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
)
527 branch_struct
= find_struct(value
)
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.
535 if not key
in enum_define
['enum_values']:
536 raise QAPIExprError(expr_info
,
537 "Discriminator value '%s' is not found in "
539 (key
, enum_define
["enum_name"]))
541 # Otherwise, check for conflicts in the generated enum
543 c_key
= camel_to_upper(key
)
545 raise QAPIExprError(expr_info
,
546 "Union '%s' member '%s' clashes with '%s'"
547 % (name
, key
, values
[c_key
]))
550 def check_alternate(expr
, expr_info
):
551 name
= expr
['alternate']
552 members
= expr
['data']
553 values
= { 'MAX': '(automatic)' }
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
)
563 raise QAPIExprError(expr_info
,
564 "Alternate '%s' member '%s' clashes with '%s'"
565 % (name
, key
, values
[c_key
]))
568 # Ensure alternates have no type conflicts.
569 check_type(expr_info
, "Member '%s' of alternate '%s'" % (key
, name
),
571 allow_metas
=['built-in', 'union', 'struct', 'enum'])
572 qtype
= find_alternate_member_qtype(value
)
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
):
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
,
592 key
= camel_to_upper(member
)
594 raise QAPIExprError(expr_info
,
595 "Enum '%s' member '%s' clashes with '%s'"
596 % (name
, member
, values
[key
]))
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'])
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
)
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']
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'"
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"
648 if not expr
.has_key(key
):
649 raise QAPIExprError(info
,
650 "Key '%s' is missing from %s '%s'"
654 def parse_schema(input_file
):
658 # First pass: read entire file into memory
660 schema
= QAPISchema(open(input_file
, "r"))
661 except (QAPISchemaError
, QAPIExprError
), e
:
662 print >>sys
.stderr
, e
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')
694 raise QAPIExprError(expr_elem
['info'],
695 "Expression is missing metatype")
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'],
705 elif expr
.has_key('alternate'):
706 add_enum('%sKind' % expr
['alternate'], expr_elem
['info'],
709 # Final pass - validate that exprs make sense
711 except QAPIExprError
, e
:
712 print >>sys
.stderr
, e
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
:
725 argentry
= typeinfo
[member
]
727 if member
.startswith('*'):
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
):
741 new_name
+= ch
.upper()
744 new_name
+= ch
.lower()
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)
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():
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
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
):
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():
834 def add_name(name
, info
, meta
, implicit
= False):
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'"
845 all_names
[name
] = meta
847 def add_struct(definition
, info
):
849 name
= definition
['struct']
850 add_name(name
, info
, 'struct')
851 struct_types
.append(definition
)
853 def find_struct(name
):
855 for struct
in struct_types
:
856 if struct
['struct'] == name
:
860 def add_union(definition
, info
):
862 name
= definition
['union']
863 add_name(name
, info
, 'union')
864 union_types
.append(definition
)
866 def find_union(name
):
868 for union
in union_types
:
869 if union
['union'] == name
:
873 def add_enum(name
, info
, enum_values
= None, implicit
= False):
875 add_name(name
, info
, 'enum', implicit
)
876 enum_types
.append({"enum_name": name
, "enum_values": enum_values
})
880 for enum
in enum_types
:
881 if enum
['enum_name'] == 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):
901 return 'const char' + pointer_suffix
902 return 'char' + pointer_suffix
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'):
910 elif value
== 'size':
912 elif value
== 'bool':
914 elif value
== 'number':
916 elif type(value
) == list:
917 return c_list_type(value
[0]) + pointer_suffix
922 elif value
in events
:
923 return camel_case(value
) + 'Event' + pointer_suffix
926 assert isinstance(value
, str) and value
!= ""
927 return c_name(value
) + pointer_suffix
930 return c_type(value
).endswith(pointer_suffix
)
932 def genindent(count
):
934 for i
in range(count
):
940 def push_indent(indent_amount
=4):
942 indent_level
+= indent_amount
944 def pop_indent(indent_amount
=4):
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
):
974 name
=guardname(name
))
979 #endif /* %(name)s */
982 name
=guardname(name
))
984 def parse_command_line(extra_options
= "", extra_long_options
= []):
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
))
1003 if o
in ("-p", "--prefix"):
1005 elif o
in ("-o", "--output-dir"):
1006 output_dir
= a
+ "/"
1007 elif o
in ("-c", "--source"):
1009 elif o
in ("-h", "--header"):
1012 extra_opts
.append(oa
)
1014 if not do_c
and not do_h
:
1019 print >>sys
.stderr
, "%s: need exactly one argument" % sys
.argv
[0]
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
1031 os
.makedirs(output_dir
)
1033 if e
.errno
!= errno
.EEXIST
:
1036 def maybe_open(really
, name
, opt
):
1038 return open(name
, opt
)
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 */
1050 comment
= c_comment
))
1052 fdecl
.write(mcgen('''
1053 /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
1059 comment
= h_comment
, guard
= guardname(h_file
)))
1061 return (fdef
, fdecl
)
1063 def close_output(fdef
, fdecl
):