qapi.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. #
  2. # QAPI helper library
  3. #
  4. # Copyright IBM, Corp. 2011
  5. # Copyright (c) 2013-2015 Red Hat Inc.
  6. #
  7. # Authors:
  8. # Anthony Liguori <aliguori@us.ibm.com>
  9. # Markus Armbruster <armbru@redhat.com>
  10. #
  11. # This work is licensed under the terms of the GNU GPL, version 2.
  12. # See the COPYING file in the top-level directory.
  13. import re
  14. from ordereddict import OrderedDict
  15. import os
  16. import sys
  17. builtin_types = {
  18. 'str': 'QTYPE_QSTRING',
  19. 'int': 'QTYPE_QINT',
  20. 'number': 'QTYPE_QFLOAT',
  21. 'bool': 'QTYPE_QBOOL',
  22. 'int8': 'QTYPE_QINT',
  23. 'int16': 'QTYPE_QINT',
  24. 'int32': 'QTYPE_QINT',
  25. 'int64': 'QTYPE_QINT',
  26. 'uint8': 'QTYPE_QINT',
  27. 'uint16': 'QTYPE_QINT',
  28. 'uint32': 'QTYPE_QINT',
  29. 'uint64': 'QTYPE_QINT',
  30. 'size': 'QTYPE_QINT',
  31. }
  32. # Whitelist of commands allowed to return a non-dictionary
  33. returns_whitelist = [
  34. # From QMP:
  35. 'human-monitor-command',
  36. 'query-migrate-cache-size',
  37. 'query-tpm-models',
  38. 'query-tpm-types',
  39. 'ringbuf-read',
  40. # From QGA:
  41. 'guest-file-open',
  42. 'guest-fsfreeze-freeze',
  43. 'guest-fsfreeze-freeze-list',
  44. 'guest-fsfreeze-status',
  45. 'guest-fsfreeze-thaw',
  46. 'guest-get-time',
  47. 'guest-set-vcpus',
  48. 'guest-sync',
  49. 'guest-sync-delimited',
  50. # From qapi-schema-test:
  51. 'user_def_cmd3',
  52. ]
  53. enum_types = []
  54. struct_types = []
  55. union_types = []
  56. events = []
  57. all_names = {}
  58. def error_path(parent):
  59. res = ""
  60. while parent:
  61. res = ("In file included from %s:%d:\n" % (parent['file'],
  62. parent['line'])) + res
  63. parent = parent['parent']
  64. return res
  65. class QAPISchemaError(Exception):
  66. def __init__(self, schema, msg):
  67. self.input_file = schema.input_file
  68. self.msg = msg
  69. self.col = 1
  70. self.line = schema.line
  71. for ch in schema.src[schema.line_pos:schema.pos]:
  72. if ch == '\t':
  73. self.col = (self.col + 7) % 8 + 1
  74. else:
  75. self.col += 1
  76. self.info = schema.parent_info
  77. def __str__(self):
  78. return error_path(self.info) + \
  79. "%s:%d:%d: %s" % (self.input_file, self.line, self.col, self.msg)
  80. class QAPIExprError(Exception):
  81. def __init__(self, expr_info, msg):
  82. self.info = expr_info
  83. self.msg = msg
  84. def __str__(self):
  85. return error_path(self.info['parent']) + \
  86. "%s:%d: %s" % (self.info['file'], self.info['line'], self.msg)
  87. class QAPISchema:
  88. def __init__(self, fp, input_relname=None, include_hist=[],
  89. previously_included=[], parent_info=None):
  90. """ include_hist is a stack used to detect inclusion cycles
  91. previously_included is a global state used to avoid multiple
  92. inclusions of the same file"""
  93. input_fname = os.path.abspath(fp.name)
  94. if input_relname is None:
  95. input_relname = fp.name
  96. self.input_dir = os.path.dirname(input_fname)
  97. self.input_file = input_relname
  98. self.include_hist = include_hist + [(input_relname, input_fname)]
  99. previously_included.append(input_fname)
  100. self.parent_info = parent_info
  101. self.src = fp.read()
  102. if self.src == '' or self.src[-1] != '\n':
  103. self.src += '\n'
  104. self.cursor = 0
  105. self.line = 1
  106. self.line_pos = 0
  107. self.exprs = []
  108. self.accept()
  109. while self.tok != None:
  110. expr_info = {'file': input_relname, 'line': self.line, 'parent': self.parent_info}
  111. expr = self.get_expr(False)
  112. if isinstance(expr, dict) and "include" in expr:
  113. if len(expr) != 1:
  114. raise QAPIExprError(expr_info, "Invalid 'include' directive")
  115. include = expr["include"]
  116. if not isinstance(include, str):
  117. raise QAPIExprError(expr_info,
  118. 'Expected a file name (string), got: %s'
  119. % include)
  120. include_path = os.path.join(self.input_dir, include)
  121. for elem in self.include_hist:
  122. if include_path == elem[1]:
  123. raise QAPIExprError(expr_info, "Inclusion loop for %s"
  124. % include)
  125. # skip multiple include of the same file
  126. if include_path in previously_included:
  127. continue
  128. try:
  129. fobj = open(include_path, 'r')
  130. except IOError, e:
  131. raise QAPIExprError(expr_info,
  132. '%s: %s' % (e.strerror, include))
  133. exprs_include = QAPISchema(fobj, include, self.include_hist,
  134. previously_included, expr_info)
  135. self.exprs.extend(exprs_include.exprs)
  136. else:
  137. expr_elem = {'expr': expr,
  138. 'info': expr_info}
  139. self.exprs.append(expr_elem)
  140. def accept(self):
  141. while True:
  142. self.tok = self.src[self.cursor]
  143. self.pos = self.cursor
  144. self.cursor += 1
  145. self.val = None
  146. if self.tok == '#':
  147. self.cursor = self.src.find('\n', self.cursor)
  148. elif self.tok in ['{', '}', ':', ',', '[', ']']:
  149. return
  150. elif self.tok == "'":
  151. string = ''
  152. esc = False
  153. while True:
  154. ch = self.src[self.cursor]
  155. self.cursor += 1
  156. if ch == '\n':
  157. raise QAPISchemaError(self,
  158. 'Missing terminating "\'"')
  159. if esc:
  160. string += ch
  161. esc = False
  162. elif ch == "\\":
  163. esc = True
  164. elif ch == "'":
  165. self.val = string
  166. return
  167. else:
  168. string += ch
  169. elif self.tok in "tfn":
  170. val = self.src[self.cursor - 1:]
  171. if val.startswith("true"):
  172. self.val = True
  173. self.cursor += 3
  174. return
  175. elif val.startswith("false"):
  176. self.val = False
  177. self.cursor += 4
  178. return
  179. elif val.startswith("null"):
  180. self.val = None
  181. self.cursor += 3
  182. return
  183. elif self.tok == '\n':
  184. if self.cursor == len(self.src):
  185. self.tok = None
  186. return
  187. self.line += 1
  188. self.line_pos = self.cursor
  189. elif not self.tok.isspace():
  190. raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
  191. def get_members(self):
  192. expr = OrderedDict()
  193. if self.tok == '}':
  194. self.accept()
  195. return expr
  196. if self.tok != "'":
  197. raise QAPISchemaError(self, 'Expected string or "}"')
  198. while True:
  199. key = self.val
  200. self.accept()
  201. if self.tok != ':':
  202. raise QAPISchemaError(self, 'Expected ":"')
  203. self.accept()
  204. if key in expr:
  205. raise QAPISchemaError(self, 'Duplicate key "%s"' % key)
  206. expr[key] = self.get_expr(True)
  207. if self.tok == '}':
  208. self.accept()
  209. return expr
  210. if self.tok != ',':
  211. raise QAPISchemaError(self, 'Expected "," or "}"')
  212. self.accept()
  213. if self.tok != "'":
  214. raise QAPISchemaError(self, 'Expected string')
  215. def get_values(self):
  216. expr = []
  217. if self.tok == ']':
  218. self.accept()
  219. return expr
  220. if not self.tok in "{['tfn":
  221. raise QAPISchemaError(self, 'Expected "{", "[", "]", string, '
  222. 'boolean or "null"')
  223. while True:
  224. expr.append(self.get_expr(True))
  225. if self.tok == ']':
  226. self.accept()
  227. return expr
  228. if self.tok != ',':
  229. raise QAPISchemaError(self, 'Expected "," or "]"')
  230. self.accept()
  231. def get_expr(self, nested):
  232. if self.tok != '{' and not nested:
  233. raise QAPISchemaError(self, 'Expected "{"')
  234. if self.tok == '{':
  235. self.accept()
  236. expr = self.get_members()
  237. elif self.tok == '[':
  238. self.accept()
  239. expr = self.get_values()
  240. elif self.tok in "'tfn":
  241. expr = self.val
  242. self.accept()
  243. else:
  244. raise QAPISchemaError(self, 'Expected "{", "[" or string')
  245. return expr
  246. def find_base_fields(base):
  247. base_struct_define = find_struct(base)
  248. if not base_struct_define:
  249. return None
  250. return base_struct_define['data']
  251. # Return the qtype of an alternate branch, or None on error.
  252. def find_alternate_member_qtype(qapi_type):
  253. if builtin_types.has_key(qapi_type):
  254. return builtin_types[qapi_type]
  255. elif find_struct(qapi_type):
  256. return "QTYPE_QDICT"
  257. elif find_enum(qapi_type):
  258. return "QTYPE_QSTRING"
  259. elif find_union(qapi_type):
  260. return "QTYPE_QDICT"
  261. return None
  262. # Return the discriminator enum define if discriminator is specified as an
  263. # enum type, otherwise return None.
  264. def discriminator_find_enum_define(expr):
  265. base = expr.get('base')
  266. discriminator = expr.get('discriminator')
  267. if not (discriminator and base):
  268. return None
  269. base_fields = find_base_fields(base)
  270. if not base_fields:
  271. return None
  272. discriminator_type = base_fields.get(discriminator)
  273. if not discriminator_type:
  274. return None
  275. return find_enum(discriminator_type)
  276. valid_name = re.compile('^[a-zA-Z_][a-zA-Z0-9_.-]*$')
  277. def check_name(expr_info, source, name, allow_optional = False,
  278. enum_member = False):
  279. global valid_name
  280. membername = name
  281. if not isinstance(name, str):
  282. raise QAPIExprError(expr_info,
  283. "%s requires a string name" % source)
  284. if name.startswith('*'):
  285. membername = name[1:]
  286. if not allow_optional:
  287. raise QAPIExprError(expr_info,
  288. "%s does not allow optional name '%s'"
  289. % (source, name))
  290. # Enum members can start with a digit, because the generated C
  291. # code always prefixes it with the enum name
  292. if enum_member:
  293. membername = '_' + membername
  294. if not valid_name.match(membername):
  295. raise QAPIExprError(expr_info,
  296. "%s uses invalid name '%s'" % (source, name))
  297. def check_type(expr_info, source, value, allow_array = False,
  298. allow_dict = False, allow_optional = False,
  299. allow_star = False, allow_metas = []):
  300. global all_names
  301. orig_value = value
  302. if value is None:
  303. return
  304. if allow_star and value == '**':
  305. return
  306. # Check if array type for value is okay
  307. if isinstance(value, list):
  308. if not allow_array:
  309. raise QAPIExprError(expr_info,
  310. "%s cannot be an array" % source)
  311. if len(value) != 1 or not isinstance(value[0], str):
  312. raise QAPIExprError(expr_info,
  313. "%s: array type must contain single type name"
  314. % source)
  315. value = value[0]
  316. orig_value = "array of %s" %value
  317. # Check if type name for value is okay
  318. if isinstance(value, str):
  319. if value == '**':
  320. raise QAPIExprError(expr_info,
  321. "%s uses '**' but did not request 'gen':false"
  322. % source)
  323. if not value in all_names:
  324. raise QAPIExprError(expr_info,
  325. "%s uses unknown type '%s'"
  326. % (source, orig_value))
  327. if not all_names[value] in allow_metas:
  328. raise QAPIExprError(expr_info,
  329. "%s cannot use %s type '%s'"
  330. % (source, all_names[value], orig_value))
  331. return
  332. # value is a dictionary, check that each member is okay
  333. if not isinstance(value, OrderedDict):
  334. raise QAPIExprError(expr_info,
  335. "%s should be a dictionary" % source)
  336. if not allow_dict:
  337. raise QAPIExprError(expr_info,
  338. "%s should be a type name" % source)
  339. for (key, arg) in value.items():
  340. check_name(expr_info, "Member of %s" % source, key,
  341. allow_optional=allow_optional)
  342. check_type(expr_info, "Member '%s' of %s" % (key, source), arg,
  343. allow_array=True, allow_dict=True, allow_optional=True,
  344. allow_metas=['built-in', 'union', 'alternate', 'struct',
  345. 'enum'], allow_star=allow_star)
  346. def check_command(expr, expr_info):
  347. name = expr['command']
  348. allow_star = expr.has_key('gen')
  349. check_type(expr_info, "'data' for command '%s'" % name,
  350. expr.get('data'), allow_dict=True, allow_optional=True,
  351. allow_metas=['union', 'struct'], allow_star=allow_star)
  352. returns_meta = ['union', 'struct']
  353. if name in returns_whitelist:
  354. returns_meta += ['built-in', 'alternate', 'enum']
  355. check_type(expr_info, "'returns' for command '%s'" % name,
  356. expr.get('returns'), allow_array=True, allow_dict=True,
  357. allow_optional=True, allow_metas=returns_meta,
  358. allow_star=allow_star)
  359. def check_event(expr, expr_info):
  360. global events
  361. name = expr['event']
  362. params = expr.get('data')
  363. if name.upper() == 'MAX':
  364. raise QAPIExprError(expr_info, "Event name 'MAX' cannot be created")
  365. events.append(name)
  366. check_type(expr_info, "'data' for event '%s'" % name,
  367. expr.get('data'), allow_dict=True, allow_optional=True,
  368. allow_metas=['union', 'struct'])
  369. if params:
  370. for argname, argentry, optional, structured in parse_args(params):
  371. if structured:
  372. raise QAPIExprError(expr_info,
  373. "Nested structure define in event is not "
  374. "supported, event '%s', argname '%s'"
  375. % (expr['event'], argname))
  376. def check_union(expr, expr_info):
  377. name = expr['union']
  378. base = expr.get('base')
  379. discriminator = expr.get('discriminator')
  380. members = expr['data']
  381. values = { 'MAX': '(automatic)' }
  382. # If the object has a member 'base', its value must name a complex type,
  383. # and there must be a discriminator.
  384. if base is not None:
  385. if discriminator is None:
  386. raise QAPIExprError(expr_info,
  387. "Union '%s' requires a discriminator to go "
  388. "along with base" %name)
  389. # Two types of unions, determined by discriminator.
  390. # With no discriminator it is a simple union.
  391. if discriminator is None:
  392. enum_define = None
  393. allow_metas=['built-in', 'union', 'alternate', 'struct', 'enum']
  394. if base is not None:
  395. raise QAPIExprError(expr_info,
  396. "Simple union '%s' must not have a base"
  397. % name)
  398. # Else, it's a flat union.
  399. else:
  400. # The object must have a string member 'base'.
  401. if not isinstance(base, str):
  402. raise QAPIExprError(expr_info,
  403. "Flat union '%s' must have a string base field"
  404. % name)
  405. base_fields = find_base_fields(base)
  406. if not base_fields:
  407. raise QAPIExprError(expr_info,
  408. "Base '%s' is not a valid type"
  409. % base)
  410. # The value of member 'discriminator' must name a non-optional
  411. # member of the base type.
  412. check_name(expr_info, "Discriminator of flat union '%s'" % name,
  413. discriminator)
  414. discriminator_type = base_fields.get(discriminator)
  415. if not discriminator_type:
  416. raise QAPIExprError(expr_info,
  417. "Discriminator '%s' is not a member of base "
  418. "type '%s'"
  419. % (discriminator, base))
  420. enum_define = find_enum(discriminator_type)
  421. allow_metas=['struct']
  422. # Do not allow string discriminator
  423. if not enum_define:
  424. raise QAPIExprError(expr_info,
  425. "Discriminator '%s' must be of enumeration "
  426. "type" % discriminator)
  427. # Check every branch
  428. for (key, value) in members.items():
  429. check_name(expr_info, "Member of union '%s'" % name, key)
  430. # Each value must name a known type; furthermore, in flat unions,
  431. # branches must be a struct
  432. check_type(expr_info, "Member '%s' of union '%s'" % (key, name),
  433. value, allow_array=True, allow_metas=allow_metas)
  434. # If the discriminator names an enum type, then all members
  435. # of 'data' must also be members of the enum type.
  436. if enum_define:
  437. if not key in enum_define['enum_values']:
  438. raise QAPIExprError(expr_info,
  439. "Discriminator value '%s' is not found in "
  440. "enum '%s'" %
  441. (key, enum_define["enum_name"]))
  442. # Otherwise, check for conflicts in the generated enum
  443. else:
  444. c_key = _generate_enum_string(key)
  445. if c_key in values:
  446. raise QAPIExprError(expr_info,
  447. "Union '%s' member '%s' clashes with '%s'"
  448. % (name, key, values[c_key]))
  449. values[c_key] = key
  450. def check_alternate(expr, expr_info):
  451. name = expr['alternate']
  452. members = expr['data']
  453. values = { 'MAX': '(automatic)' }
  454. types_seen = {}
  455. # Check every branch
  456. for (key, value) in members.items():
  457. check_name(expr_info, "Member of alternate '%s'" % name, key)
  458. # Check for conflicts in the generated enum
  459. c_key = _generate_enum_string(key)
  460. if c_key in values:
  461. raise QAPIExprError(expr_info,
  462. "Alternate '%s' member '%s' clashes with '%s'"
  463. % (name, key, values[c_key]))
  464. values[c_key] = key
  465. # Ensure alternates have no type conflicts.
  466. check_type(expr_info, "Member '%s' of alternate '%s'" % (key, name),
  467. value,
  468. allow_metas=['built-in', 'union', 'struct', 'enum'])
  469. qtype = find_alternate_member_qtype(value)
  470. assert qtype
  471. if qtype in types_seen:
  472. raise QAPIExprError(expr_info,
  473. "Alternate '%s' member '%s' can't "
  474. "be distinguished from member '%s'"
  475. % (name, key, types_seen[qtype]))
  476. types_seen[qtype] = key
  477. def check_enum(expr, expr_info):
  478. name = expr['enum']
  479. members = expr.get('data')
  480. values = { 'MAX': '(automatic)' }
  481. if not isinstance(members, list):
  482. raise QAPIExprError(expr_info,
  483. "Enum '%s' requires an array for 'data'" % name)
  484. for member in members:
  485. check_name(expr_info, "Member of enum '%s'" %name, member,
  486. enum_member=True)
  487. key = _generate_enum_string(member)
  488. if key in values:
  489. raise QAPIExprError(expr_info,
  490. "Enum '%s' member '%s' clashes with '%s'"
  491. % (name, member, values[key]))
  492. values[key] = member
  493. def check_struct(expr, expr_info):
  494. name = expr['type']
  495. members = expr['data']
  496. check_type(expr_info, "'data' for type '%s'" % name, members,
  497. allow_dict=True, allow_optional=True)
  498. check_type(expr_info, "'base' for type '%s'" % name, expr.get('base'),
  499. allow_metas=['struct'])
  500. def check_exprs(schema):
  501. for expr_elem in schema.exprs:
  502. expr = expr_elem['expr']
  503. info = expr_elem['info']
  504. if expr.has_key('enum'):
  505. check_enum(expr, info)
  506. elif expr.has_key('union'):
  507. check_union(expr, info)
  508. elif expr.has_key('alternate'):
  509. check_alternate(expr, info)
  510. elif expr.has_key('type'):
  511. check_struct(expr, info)
  512. elif expr.has_key('command'):
  513. check_command(expr, info)
  514. elif expr.has_key('event'):
  515. check_event(expr, info)
  516. else:
  517. assert False, 'unexpected meta type'
  518. def check_keys(expr_elem, meta, required, optional=[]):
  519. expr = expr_elem['expr']
  520. info = expr_elem['info']
  521. name = expr[meta]
  522. if not isinstance(name, str):
  523. raise QAPIExprError(info,
  524. "'%s' key must have a string value" % meta)
  525. required = required + [ meta ]
  526. for (key, value) in expr.items():
  527. if not key in required and not key in optional:
  528. raise QAPIExprError(info,
  529. "Unknown key '%s' in %s '%s'"
  530. % (key, meta, name))
  531. if (key == 'gen' or key == 'success-response') and value != False:
  532. raise QAPIExprError(info,
  533. "'%s' of %s '%s' should only use false value"
  534. % (key, meta, name))
  535. for key in required:
  536. if not expr.has_key(key):
  537. raise QAPIExprError(info,
  538. "Key '%s' is missing from %s '%s'"
  539. % (key, meta, name))
  540. def parse_schema(input_file):
  541. global all_names
  542. exprs = []
  543. # First pass: read entire file into memory
  544. try:
  545. schema = QAPISchema(open(input_file, "r"))
  546. except (QAPISchemaError, QAPIExprError), e:
  547. print >>sys.stderr, e
  548. exit(1)
  549. try:
  550. # Next pass: learn the types and check for valid expression keys. At
  551. # this point, top-level 'include' has already been flattened.
  552. for builtin in builtin_types.keys():
  553. all_names[builtin] = 'built-in'
  554. for expr_elem in schema.exprs:
  555. expr = expr_elem['expr']
  556. info = expr_elem['info']
  557. if expr.has_key('enum'):
  558. check_keys(expr_elem, 'enum', ['data'])
  559. add_enum(expr['enum'], info, expr['data'])
  560. elif expr.has_key('union'):
  561. check_keys(expr_elem, 'union', ['data'],
  562. ['base', 'discriminator'])
  563. add_union(expr, info)
  564. elif expr.has_key('alternate'):
  565. check_keys(expr_elem, 'alternate', ['data'])
  566. add_name(expr['alternate'], info, 'alternate')
  567. elif expr.has_key('type'):
  568. check_keys(expr_elem, 'type', ['data'], ['base'])
  569. add_struct(expr, info)
  570. elif expr.has_key('command'):
  571. check_keys(expr_elem, 'command', [],
  572. ['data', 'returns', 'gen', 'success-response'])
  573. add_name(expr['command'], info, 'command')
  574. elif expr.has_key('event'):
  575. check_keys(expr_elem, 'event', [], ['data'])
  576. add_name(expr['event'], info, 'event')
  577. else:
  578. raise QAPIExprError(expr_elem['info'],
  579. "Expression is missing metatype")
  580. exprs.append(expr)
  581. # Try again for hidden UnionKind enum
  582. for expr_elem in schema.exprs:
  583. expr = expr_elem['expr']
  584. if expr.has_key('union'):
  585. if not discriminator_find_enum_define(expr):
  586. add_enum('%sKind' % expr['union'], expr_elem['info'],
  587. implicit=True)
  588. elif expr.has_key('alternate'):
  589. add_enum('%sKind' % expr['alternate'], expr_elem['info'],
  590. implicit=True)
  591. # Final pass - validate that exprs make sense
  592. check_exprs(schema)
  593. except QAPIExprError, e:
  594. print >>sys.stderr, e
  595. exit(1)
  596. return exprs
  597. def parse_args(typeinfo):
  598. if isinstance(typeinfo, str):
  599. struct = find_struct(typeinfo)
  600. assert struct != None
  601. typeinfo = struct['data']
  602. for member in typeinfo:
  603. argname = member
  604. argentry = typeinfo[member]
  605. optional = False
  606. structured = False
  607. if member.startswith('*'):
  608. argname = member[1:]
  609. optional = True
  610. if isinstance(argentry, OrderedDict):
  611. structured = True
  612. yield (argname, argentry, optional, structured)
  613. def de_camel_case(name):
  614. new_name = ''
  615. for ch in name:
  616. if ch.isupper() and new_name:
  617. new_name += '_'
  618. if ch == '-':
  619. new_name += '_'
  620. else:
  621. new_name += ch.lower()
  622. return new_name
  623. def camel_case(name):
  624. new_name = ''
  625. first = True
  626. for ch in name:
  627. if ch in ['_', '-']:
  628. first = True
  629. elif first:
  630. new_name += ch.upper()
  631. first = False
  632. else:
  633. new_name += ch.lower()
  634. return new_name
  635. def c_var(name, protect=True):
  636. # ANSI X3J11/88-090, 3.1.1
  637. c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
  638. 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
  639. 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
  640. 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
  641. 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
  642. # ISO/IEC 9899:1999, 6.4.1
  643. c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
  644. # ISO/IEC 9899:2011, 6.4.1
  645. c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
  646. '_Static_assert', '_Thread_local'])
  647. # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
  648. # excluding _.*
  649. gcc_words = set(['asm', 'typeof'])
  650. # C++ ISO/IEC 14882:2003 2.11
  651. cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete',
  652. 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
  653. 'namespace', 'new', 'operator', 'private', 'protected',
  654. 'public', 'reinterpret_cast', 'static_cast', 'template',
  655. 'this', 'throw', 'true', 'try', 'typeid', 'typename',
  656. 'using', 'virtual', 'wchar_t',
  657. # alternative representations
  658. 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
  659. 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
  660. # namespace pollution:
  661. polluted_words = set(['unix', 'errno'])
  662. if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
  663. return "q_" + name
  664. return name.replace('-', '_').lstrip("*")
  665. def c_fun(name, protect=True):
  666. return c_var(name, protect).replace('.', '_')
  667. def c_list_type(name):
  668. return '%sList' % name
  669. def type_name(name):
  670. if type(name) == list:
  671. return c_list_type(name[0])
  672. return name
  673. def add_name(name, info, meta, implicit = False, source = None):
  674. global all_names
  675. if not source:
  676. source = "'%s'" % meta
  677. check_name(info, source, name)
  678. if name in all_names:
  679. raise QAPIExprError(info,
  680. "%s '%s' is already defined"
  681. % (all_names[name], name))
  682. if not implicit and name[-4:] == 'Kind':
  683. raise QAPIExprError(info,
  684. "%s '%s' should not end in 'Kind'"
  685. % (meta, name))
  686. all_names[name] = meta
  687. def add_struct(definition, info):
  688. global struct_types
  689. name = definition['type']
  690. add_name(name, info, 'struct', source="'type'")
  691. struct_types.append(definition)
  692. def find_struct(name):
  693. global struct_types
  694. for struct in struct_types:
  695. if struct['type'] == name:
  696. return struct
  697. return None
  698. def add_union(definition, info):
  699. global union_types
  700. name = definition['union']
  701. add_name(name, info, 'union')
  702. union_types.append(definition)
  703. def find_union(name):
  704. global union_types
  705. for union in union_types:
  706. if union['union'] == name:
  707. return union
  708. return None
  709. def add_enum(name, info, enum_values = None, implicit = False):
  710. global enum_types
  711. add_name(name, info, 'enum', implicit)
  712. enum_types.append({"enum_name": name, "enum_values": enum_values})
  713. def find_enum(name):
  714. global enum_types
  715. for enum in enum_types:
  716. if enum['enum_name'] == name:
  717. return enum
  718. return None
  719. def is_enum(name):
  720. return find_enum(name) != None
  721. eatspace = '\033EATSPACE.'
  722. # A special suffix is added in c_type() for pointer types, and it's
  723. # stripped in mcgen(). So please notice this when you check the return
  724. # value of c_type() outside mcgen().
  725. def c_type(name, is_param=False):
  726. if name == 'str':
  727. if is_param:
  728. return 'const char *' + eatspace
  729. return 'char *' + eatspace
  730. elif name == 'int':
  731. return 'int64_t'
  732. elif (name == 'int8' or name == 'int16' or name == 'int32' or
  733. name == 'int64' or name == 'uint8' or name == 'uint16' or
  734. name == 'uint32' or name == 'uint64'):
  735. return name + '_t'
  736. elif name == 'size':
  737. return 'uint64_t'
  738. elif name == 'bool':
  739. return 'bool'
  740. elif name == 'number':
  741. return 'double'
  742. elif type(name) == list:
  743. return '%s *%s' % (c_list_type(name[0]), eatspace)
  744. elif is_enum(name):
  745. return name
  746. elif name == None or len(name) == 0:
  747. return 'void'
  748. elif name in events:
  749. return '%sEvent *%s' % (camel_case(name), eatspace)
  750. else:
  751. return '%s *%s' % (name, eatspace)
  752. def is_c_ptr(name):
  753. suffix = "*" + eatspace
  754. return c_type(name).endswith(suffix)
  755. def genindent(count):
  756. ret = ""
  757. for i in range(count):
  758. ret += " "
  759. return ret
  760. indent_level = 0
  761. def push_indent(indent_amount=4):
  762. global indent_level
  763. indent_level += indent_amount
  764. def pop_indent(indent_amount=4):
  765. global indent_level
  766. indent_level -= indent_amount
  767. def cgen(code, **kwds):
  768. indent = genindent(indent_level)
  769. lines = code.split('\n')
  770. lines = map(lambda x: indent + x, lines)
  771. return '\n'.join(lines) % kwds + '\n'
  772. def mcgen(code, **kwds):
  773. raw = cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
  774. return re.sub(re.escape(eatspace) + ' *', '', raw)
  775. def basename(filename):
  776. return filename.split("/")[-1]
  777. def guardname(filename):
  778. guard = basename(filename).rsplit(".", 1)[0]
  779. for substr in [".", " ", "-"]:
  780. guard = guard.replace(substr, "_")
  781. return guard.upper() + '_H'
  782. def guardstart(name):
  783. return mcgen('''
  784. #ifndef %(name)s
  785. #define %(name)s
  786. ''',
  787. name=guardname(name))
  788. def guardend(name):
  789. return mcgen('''
  790. #endif /* %(name)s */
  791. ''',
  792. name=guardname(name))
  793. # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
  794. # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
  795. # ENUM24_Name -> ENUM24_NAME
  796. def _generate_enum_string(value):
  797. c_fun_str = c_fun(value, False)
  798. if value.isupper():
  799. return c_fun_str
  800. new_name = ''
  801. l = len(c_fun_str)
  802. for i in range(l):
  803. c = c_fun_str[i]
  804. # When c is upper and no "_" appears before, do more checks
  805. if c.isupper() and (i > 0) and c_fun_str[i - 1] != "_":
  806. # Case 1: next string is lower
  807. # Case 2: previous string is digit
  808. if (i < (l - 1) and c_fun_str[i + 1].islower()) or \
  809. c_fun_str[i - 1].isdigit():
  810. new_name += '_'
  811. new_name += c
  812. return new_name.lstrip('_').upper()
  813. def generate_enum_full_value(enum_name, enum_value):
  814. abbrev_string = _generate_enum_string(enum_name)
  815. value_string = _generate_enum_string(enum_value)
  816. return "%s_%s" % (abbrev_string, value_string)