4 # Copyright IBM, Corp. 2011
5 # Copyright (c) 2013 Red Hat Inc.
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
20 'str', 'int', 'number', 'bool',
21 'int8', 'int16', 'int32', 'int64',
22 'uint8', 'uint16', 'uint32', 'uint64'
25 builtin_type_qtypes = {
26 'str': 'QTYPE_QSTRING',
28 'number': 'QTYPE_QFLOAT',
29 'bool': 'QTYPE_QBOOL',
31 'int16': 'QTYPE_QINT',
32 'int32': 'QTYPE_QINT',
33 'int64': 'QTYPE_QINT',
34 'uint8': 'QTYPE_QINT',
35 'uint16': 'QTYPE_QINT',
36 'uint32': 'QTYPE_QINT',
37 'uint64': 'QTYPE_QINT',
40 def error_path(parent):
43 res = ("In file included from %s:%d:\n" % (parent['file'],
44 parent['line'])) + res
45 parent = parent['parent']
48 class QAPISchemaError(Exception):
49 def __init__(self, schema, msg):
50 self.input_file = schema.input_file
53 self.line = schema.line
54 for ch in schema.src[schema.line_pos:schema.pos]:
56 self.col = (self.col + 7) % 8 + 1
59 self.info = schema.parent_info
62 return error_path(self.info) + \
63 "%s:%d:%d: %s" % (self.input_file, self.line, self.col, self.msg)
65 class QAPIExprError(Exception):
66 def __init__(self, expr_info, msg):
71 return error_path(self.info['parent']) + \
72 "%s:%d: %s" % (self.info['file'], self.info['line'], self.msg)
76 def __init__(self, fp, input_relname=None, include_hist=[],
77 previously_included=[], parent_info=None):
78 """ include_hist is a stack used to detect inclusion cycles
79 previously_included is a global state used to avoid multiple
80 inclusions of the same file"""
81 input_fname = os.path.abspath(fp.name)
82 if input_relname is None:
83 input_relname = fp.name
84 self.input_dir = os.path.dirname(input_fname)
85 self.input_file = input_relname
86 self.include_hist = include_hist + [(input_relname, input_fname)]
87 previously_included.append(input_fname)
88 self.parent_info = parent_info
90 if self.src == '' or self.src[-1] != '\n':
98 while self.tok != None:
99 expr_info = {'file': input_relname, 'line': self.line, 'parent': self.parent_info}
100 expr = self.get_expr(False)
101 if isinstance(expr, dict) and "include" in expr:
103 raise QAPIExprError(expr_info, "Invalid 'include' directive")
104 include = expr["include"]
105 if not isinstance(include, str):
106 raise QAPIExprError(expr_info,
107 'Expected a file name (string), got: %s'
109 include_path = os.path.join(self.input_dir, include)
110 for elem in self.include_hist:
111 if include_path == elem[1]:
112 raise QAPIExprError(expr_info, "Inclusion loop for %s"
114 # skip multiple include of the same file
115 if include_path in previously_included:
118 fobj = open(include_path, 'r')
120 raise QAPIExprError(expr_info,
121 '%s: %s' % (e.strerror, include))
122 exprs_include = QAPISchema(fobj, include, self.include_hist,
123 previously_included, expr_info)
124 self.exprs.extend(exprs_include.exprs)
126 expr_elem = {'expr': expr,
128 self.exprs.append(expr_elem)
132 self.tok = self.src[self.cursor]
133 self.pos = self.cursor
138 self.cursor = self.src.find('\n', self.cursor)
139 elif self.tok in ['{', '}', ':', ',', '[', ']']:
141 elif self.tok == "'":
145 ch = self.src[self.cursor]
148 raise QAPISchemaError(self,
149 'Missing terminating "\'"')
160 elif self.tok == '\n':
161 if self.cursor == len(self.src):
165 self.line_pos = self.cursor
166 elif not self.tok.isspace():
167 raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
169 def get_members(self):
175 raise QAPISchemaError(self, 'Expected string or "}"')
180 raise QAPISchemaError(self, 'Expected ":"')
183 raise QAPISchemaError(self, 'Duplicate key "%s"' % key)
184 expr[key] = self.get_expr(True)
189 raise QAPISchemaError(self, 'Expected "," or "}"')
192 raise QAPISchemaError(self, 'Expected string')
194 def get_values(self):
199 if not self.tok in [ '{', '[', "'" ]:
200 raise QAPISchemaError(self, 'Expected "{", "[", "]" or string')
202 expr.append(self.get_expr(True))
207 raise QAPISchemaError(self, 'Expected "," or "]"')
210 def get_expr(self, nested):
211 if self.tok != '{' and not nested:
212 raise QAPISchemaError(self, 'Expected "{"')
215 expr = self.get_members()
216 elif self.tok == '[':
218 expr = self.get_values()
219 elif self.tok == "'":
223 raise QAPISchemaError(self, 'Expected "{", "[" or string')
226 def find_base_fields(base):
227 base_struct_define = find_struct(base)
228 if not base_struct_define:
230 return base_struct_define['data']
232 # Return the discriminator enum define if discriminator is specified as an
233 # enum type, otherwise return None.
234 def discriminator_find_enum_define(expr):
235 base = expr.get('base')
236 discriminator = expr.get('discriminator')
238 if not (discriminator and base):
241 base_fields = find_base_fields(base)
245 discriminator_type = base_fields.get(discriminator)
246 if not discriminator_type:
249 return find_enum(discriminator_type)
251 def check_event(expr, expr_info):
252 params = expr.get('data')
254 for argname, argentry, optional, structured in parse_args(params):
256 raise QAPIExprError(expr_info,
257 "Nested structure define in event is not "
258 "supported, event '%s', argname '%s'"
259 % (expr['event'], argname))
261 def check_union(expr, expr_info):
263 base = expr.get('base')
264 discriminator = expr.get('discriminator')
265 members = expr['data']
267 # If the object has a member 'base', its value must name a complex type.
269 base_fields = find_base_fields(base)
271 raise QAPIExprError(expr_info,
272 "Base '%s' is not a valid type"
275 # If the union object has no member 'discriminator', it's an
277 if not discriminator:
280 # Else if the value of member 'discriminator' is {}, it's an
282 elif discriminator == {}:
285 # Else, it's a flat union.
287 # The object must have a member 'base'.
289 raise QAPIExprError(expr_info,
290 "Flat union '%s' must have a base field"
292 # The value of member 'discriminator' must name a member of the
294 discriminator_type = base_fields.get(discriminator)
295 if not discriminator_type:
296 raise QAPIExprError(expr_info,
297 "Discriminator '%s' is not a member of base "
299 % (discriminator, base))
300 enum_define = find_enum(discriminator_type)
301 # Do not allow string discriminator
303 raise QAPIExprError(expr_info,
304 "Discriminator '%s' must be of enumeration "
305 "type" % discriminator)
308 for (key, value) in members.items():
309 # If this named member's value names an enum type, then all members
310 # of 'data' must also be members of the enum type.
311 if enum_define and not key in enum_define['enum_values']:
312 raise QAPIExprError(expr_info,
313 "Discriminator value '%s' is not found in "
315 (key, enum_define["enum_name"]))
316 # Todo: add checking for values. Key is checked as above, value can be
317 # also checked here, but we need more functions to handle array case.
319 def check_exprs(schema):
320 for expr_elem in schema.exprs:
321 expr = expr_elem['expr']
322 if expr.has_key('union'):
323 check_union(expr, expr_elem['info'])
324 if expr.has_key('event'):
325 check_event(expr, expr_elem['info'])
327 def parse_schema(input_file):
329 schema = QAPISchema(open(input_file, "r"))
330 except (QAPISchemaError, QAPIExprError), e:
331 print >>sys.stderr, e
336 for expr_elem in schema.exprs:
337 expr = expr_elem['expr']
338 if expr.has_key('enum'):
339 add_enum(expr['enum'], expr['data'])
340 elif expr.has_key('union'):
342 elif expr.has_key('type'):
346 # Try again for hidden UnionKind enum
347 for expr_elem in schema.exprs:
348 expr = expr_elem['expr']
349 if expr.has_key('union'):
350 if not discriminator_find_enum_define(expr):
351 add_enum('%sKind' % expr['union'])
355 except QAPIExprError, e:
356 print >>sys.stderr, e
361 def parse_args(typeinfo):
362 if isinstance(typeinfo, basestring):
363 struct = find_struct(typeinfo)
364 assert struct != None
365 typeinfo = struct['data']
367 for member in typeinfo:
369 argentry = typeinfo[member]
372 if member.startswith('*'):
375 if isinstance(argentry, OrderedDict):
377 yield (argname, argentry, optional, structured)
379 def de_camel_case(name):
382 if ch.isupper() and new_name:
387 new_name += ch.lower()
390 def camel_case(name):
397 new_name += ch.upper()
400 new_name += ch.lower()
403 def c_var(name, protect=True):
404 # ANSI X3J11/88-090, 3.1.1
405 c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
406 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
407 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
408 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
409 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
410 # ISO/IEC 9899:1999, 6.4.1
411 c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
412 # ISO/IEC 9899:2011, 6.4.1
413 c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
414 '_Static_assert', '_Thread_local'])
415 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
417 gcc_words = set(['asm', 'typeof'])
418 # C++ ISO/IEC 14882:2003 2.11
419 cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete',
420 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
421 'namespace', 'new', 'operator', 'private', 'protected',
422 'public', 'reinterpret_cast', 'static_cast', 'template',
423 'this', 'throw', 'true', 'try', 'typeid', 'typename',
424 'using', 'virtual', 'wchar_t',
425 # alternative representations
426 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
427 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
428 # namespace pollution:
429 polluted_words = set(['unix', 'errno'])
430 if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
432 return name.replace('-', '_').lstrip("*")
434 def c_fun(name, protect=True):
435 return c_var(name, protect).replace('.', '_')
437 def c_list_type(name):
438 return '%sList' % name
441 if type(name) == list:
442 return c_list_type(name[0])
449 def add_struct(definition):
451 struct_types.append(definition)
453 def find_struct(name):
455 for struct in struct_types:
456 if struct['type'] == name:
460 def add_union(definition):
462 union_types.append(definition)
464 def find_union(name):
466 for union in union_types:
467 if union['union'] == name:
471 def add_enum(name, enum_values = None):
473 enum_types.append({"enum_name": name, "enum_values": enum_values})
477 for enum in enum_types:
478 if enum['enum_name'] == name:
483 return find_enum(name) != None
485 eatspace = '\033EATSPACE.'
487 # A special suffix is added in c_type() for pointer types, and it's
488 # stripped in mcgen(). So please notice this when you check the return
489 # value of c_type() outside mcgen().
490 def c_type(name, is_param=False):
493 return 'const char *' + eatspace
494 return 'char *' + eatspace
498 elif (name == 'int8' or name == 'int16' or name == 'int32' or
499 name == 'int64' or name == 'uint8' or name == 'uint16' or
500 name == 'uint32' or name == 'uint64'):
506 elif name == 'number':
508 elif type(name) == list:
509 return '%s *%s' % (c_list_type(name[0]), eatspace)
512 elif name == None or len(name) == 0:
514 elif name == name.upper():
515 return '%sEvent *%s' % (camel_case(name), eatspace)
517 return '%s *%s' % (name, eatspace)
520 suffix = "*" + eatspace
521 return c_type(name).endswith(suffix)
523 def genindent(count):
525 for i in range(count):
531 def push_indent(indent_amount=4):
533 indent_level += indent_amount
535 def pop_indent(indent_amount=4):
537 indent_level -= indent_amount
539 def cgen(code, **kwds):
540 indent = genindent(indent_level)
541 lines = code.split('\n')
542 lines = map(lambda x: indent + x, lines)
543 return '\n'.join(lines) % kwds + '\n'
545 def mcgen(code, **kwds):
546 raw = cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
547 return re.sub(re.escape(eatspace) + ' *', '', raw)
549 def basename(filename):
550 return filename.split("/")[-1]
552 def guardname(filename):
553 guard = basename(filename).rsplit(".", 1)[0]
554 for substr in [".", " ", "-"]:
555 guard = guard.replace(substr, "_")
556 return guard.upper() + '_H'
558 def guardstart(name):
565 name=guardname(name))
570 #endif /* %(name)s */
573 name=guardname(name))
575 # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
576 # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
577 # ENUM24_Name -> ENUM24_NAME
578 def _generate_enum_string(value):
579 c_fun_str = c_fun(value, False)
587 # When c is upper and no "_" appears before, do more checks
588 if c.isupper() and (i > 0) and c_fun_str[i - 1] != "_":
589 # Case 1: next string is lower
590 # Case 2: previous string is digit
591 if (i < (l - 1) and c_fun_str[i + 1].islower()) or \
592 c_fun_str[i - 1].isdigit():
595 return new_name.lstrip('_').upper()
597 def generate_enum_full_value(enum_name, enum_value):
598 abbrev_string = _generate_enum_string(enum_name)
599 value_string = _generate_enum_string(enum_value)
600 return "%s_%s" % (abbrev_string, value_string)