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=[], parent_info=None):
77 input_fname = os.path.abspath(fp.name)
78 if input_relname is None:
79 input_relname = fp.name
80 self.input_dir = os.path.dirname(input_fname)
81 self.input_file = input_relname
82 self.include_hist = include_hist + [(input_relname, input_fname)]
83 self.parent_info = parent_info
85 if self.src == '' or self.src[-1] != '\n':
93 while self.tok != None:
94 expr_info = {'file': input_relname, 'line': self.line, 'parent': self.parent_info}
95 expr = self.get_expr(False)
96 if isinstance(expr, dict) and "include" in expr:
98 raise QAPIExprError(expr_info, "Invalid 'include' directive")
99 include = expr["include"]
100 if not isinstance(include, str):
101 raise QAPIExprError(expr_info,
102 'Expected a file name (string), got: %s'
104 include_path = os.path.join(self.input_dir, include)
105 if any(include_path == elem[1]
106 for elem in self.include_hist):
107 raise QAPIExprError(expr_info, "Inclusion loop for %s"
110 fobj = open(include_path, 'r')
112 raise QAPIExprError(expr_info,
113 '%s: %s' % (e.strerror, include))
114 exprs_include = QAPISchema(fobj, include,
115 self.include_hist, expr_info)
116 self.exprs.extend(exprs_include.exprs)
118 expr_elem = {'expr': expr,
120 self.exprs.append(expr_elem)
124 self.tok = self.src[self.cursor]
125 self.pos = self.cursor
130 self.cursor = self.src.find('\n', self.cursor)
131 elif self.tok in ['{', '}', ':', ',', '[', ']']:
133 elif self.tok == "'":
137 ch = self.src[self.cursor]
140 raise QAPISchemaError(self,
141 'Missing terminating "\'"')
152 elif self.tok == '\n':
153 if self.cursor == len(self.src):
157 self.line_pos = self.cursor
158 elif not self.tok.isspace():
159 raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
161 def get_members(self):
167 raise QAPISchemaError(self, 'Expected string or "}"')
172 raise QAPISchemaError(self, 'Expected ":"')
175 raise QAPISchemaError(self, 'Duplicate key "%s"' % key)
176 expr[key] = self.get_expr(True)
181 raise QAPISchemaError(self, 'Expected "," or "}"')
184 raise QAPISchemaError(self, 'Expected string')
186 def get_values(self):
191 if not self.tok in [ '{', '[', "'" ]:
192 raise QAPISchemaError(self, 'Expected "{", "[", "]" or string')
194 expr.append(self.get_expr(True))
199 raise QAPISchemaError(self, 'Expected "," or "]"')
202 def get_expr(self, nested):
203 if self.tok != '{' and not nested:
204 raise QAPISchemaError(self, 'Expected "{"')
207 expr = self.get_members()
208 elif self.tok == '[':
210 expr = self.get_values()
211 elif self.tok == "'":
215 raise QAPISchemaError(self, 'Expected "{", "[" or string')
218 def find_base_fields(base):
219 base_struct_define = find_struct(base)
220 if not base_struct_define:
222 return base_struct_define['data']
224 # Return the discriminator enum define if discriminator is specified as an
225 # enum type, otherwise return None.
226 def discriminator_find_enum_define(expr):
227 base = expr.get('base')
228 discriminator = expr.get('discriminator')
230 if not (discriminator and base):
233 base_fields = find_base_fields(base)
237 discriminator_type = base_fields.get(discriminator)
238 if not discriminator_type:
241 return find_enum(discriminator_type)
243 def check_union(expr, expr_info):
245 base = expr.get('base')
246 discriminator = expr.get('discriminator')
247 members = expr['data']
249 # If the object has a member 'base', its value must name a complex type.
251 base_fields = find_base_fields(base)
253 raise QAPIExprError(expr_info,
254 "Base '%s' is not a valid type"
257 # If the union object has no member 'discriminator', it's an
259 if not discriminator:
262 # Else if the value of member 'discriminator' is {}, it's an
264 elif discriminator == {}:
267 # Else, it's a flat union.
269 # The object must have a member 'base'.
271 raise QAPIExprError(expr_info,
272 "Flat union '%s' must have a base field"
274 # The value of member 'discriminator' must name a member of the
276 discriminator_type = base_fields.get(discriminator)
277 if not discriminator_type:
278 raise QAPIExprError(expr_info,
279 "Discriminator '%s' is not a member of base "
281 % (discriminator, base))
282 enum_define = find_enum(discriminator_type)
283 # Do not allow string discriminator
285 raise QAPIExprError(expr_info,
286 "Discriminator '%s' must be of enumeration "
287 "type" % discriminator)
290 for (key, value) in members.items():
291 # If this named member's value names an enum type, then all members
292 # of 'data' must also be members of the enum type.
293 if enum_define and not key in enum_define['enum_values']:
294 raise QAPIExprError(expr_info,
295 "Discriminator value '%s' is not found in "
297 (key, enum_define["enum_name"]))
298 # Todo: add checking for values. Key is checked as above, value can be
299 # also checked here, but we need more functions to handle array case.
301 def check_exprs(schema):
302 for expr_elem in schema.exprs:
303 expr = expr_elem['expr']
304 if expr.has_key('union'):
305 check_union(expr, expr_elem['info'])
307 def parse_schema(input_file):
309 schema = QAPISchema(open(input_file, "r"))
310 except (QAPISchemaError, QAPIExprError), e:
311 print >>sys.stderr, e
316 for expr_elem in schema.exprs:
317 expr = expr_elem['expr']
318 if expr.has_key('enum'):
319 add_enum(expr['enum'], expr['data'])
320 elif expr.has_key('union'):
322 elif expr.has_key('type'):
326 # Try again for hidden UnionKind enum
327 for expr_elem in schema.exprs:
328 expr = expr_elem['expr']
329 if expr.has_key('union'):
330 if not discriminator_find_enum_define(expr):
331 add_enum('%sKind' % expr['union'])
335 except QAPIExprError, e:
336 print >>sys.stderr, e
341 def parse_args(typeinfo):
342 if isinstance(typeinfo, basestring):
343 struct = find_struct(typeinfo)
344 assert struct != None
345 typeinfo = struct['data']
347 for member in typeinfo:
349 argentry = typeinfo[member]
352 if member.startswith('*'):
355 if isinstance(argentry, OrderedDict):
357 yield (argname, argentry, optional, structured)
359 def de_camel_case(name):
362 if ch.isupper() and new_name:
367 new_name += ch.lower()
370 def camel_case(name):
377 new_name += ch.upper()
380 new_name += ch.lower()
383 def c_var(name, protect=True):
384 # ANSI X3J11/88-090, 3.1.1
385 c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
386 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
387 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
388 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
389 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
390 # ISO/IEC 9899:1999, 6.4.1
391 c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
392 # ISO/IEC 9899:2011, 6.4.1
393 c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
394 '_Static_assert', '_Thread_local'])
395 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
397 gcc_words = set(['asm', 'typeof'])
398 # C++ ISO/IEC 14882:2003 2.11
399 cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete',
400 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
401 'namespace', 'new', 'operator', 'private', 'protected',
402 'public', 'reinterpret_cast', 'static_cast', 'template',
403 'this', 'throw', 'true', 'try', 'typeid', 'typename',
404 'using', 'virtual', 'wchar_t',
405 # alternative representations
406 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
407 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
408 # namespace pollution:
409 polluted_words = set(['unix', 'errno'])
410 if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
412 return name.replace('-', '_').lstrip("*")
414 def c_fun(name, protect=True):
415 return c_var(name, protect).replace('.', '_')
417 def c_list_type(name):
418 return '%sList' % name
421 if type(name) == list:
422 return c_list_type(name[0])
429 def add_struct(definition):
431 struct_types.append(definition)
433 def find_struct(name):
435 for struct in struct_types:
436 if struct['type'] == name:
440 def add_union(definition):
442 union_types.append(definition)
444 def find_union(name):
446 for union in union_types:
447 if union['union'] == name:
451 def add_enum(name, enum_values = None):
453 enum_types.append({"enum_name": name, "enum_values": enum_values})
457 for enum in enum_types:
458 if enum['enum_name'] == name:
463 return find_enum(name) != None
470 elif (name == 'int8' or name == 'int16' or name == 'int32' or
471 name == 'int64' or name == 'uint8' or name == 'uint16' or
472 name == 'uint32' or name == 'uint64'):
478 elif name == 'number':
480 elif type(name) == list:
481 return '%s *' % c_list_type(name[0])
484 elif name == None or len(name) == 0:
486 elif name == name.upper():
487 return '%sEvent *' % camel_case(name)
491 def genindent(count):
493 for i in range(count):
499 def push_indent(indent_amount=4):
501 indent_level += indent_amount
503 def pop_indent(indent_amount=4):
505 indent_level -= indent_amount
507 def cgen(code, **kwds):
508 indent = genindent(indent_level)
509 lines = code.split('\n')
510 lines = map(lambda x: indent + x, lines)
511 return '\n'.join(lines) % kwds + '\n'
513 def mcgen(code, **kwds):
514 return cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
516 def basename(filename):
517 return filename.split("/")[-1]
519 def guardname(filename):
520 guard = basename(filename).rsplit(".", 1)[0]
521 for substr in [".", " ", "-"]:
522 guard = guard.replace(substr, "_")
523 return guard.upper() + '_H'
525 def guardstart(name):
532 name=guardname(name))
537 #endif /* %(name)s */
540 name=guardname(name))
542 # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
543 # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
544 # ENUM24_Name -> ENUM24_NAME
545 def _generate_enum_string(value):
546 c_fun_str = c_fun(value, False)
554 # When c is upper and no "_" appears before, do more checks
555 if c.isupper() and (i > 0) and c_fun_str[i - 1] != "_":
556 # Case 1: next string is lower
557 # Case 2: previous string is digit
558 if (i < (l - 1) and c_fun_str[i + 1].islower()) or \
559 c_fun_str[i - 1].isdigit():
562 return new_name.lstrip('_').upper()
564 def generate_enum_full_value(enum_name, enum_value):
565 abbrev_string = _generate_enum_string(enum_name)
566 value_string = _generate_enum_string(enum_value)
567 return "%s_%s" % (abbrev_string, value_string)