qapi.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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, allow_metas = []):
  299. global all_names
  300. orig_value = value
  301. if value is None:
  302. return
  303. if value == '**':
  304. return
  305. # Check if array type for value is okay
  306. if isinstance(value, list):
  307. if not allow_array:
  308. raise QAPIExprError(expr_info,
  309. "%s cannot be an array" % source)
  310. if len(value) != 1 or not isinstance(value[0], str):
  311. raise QAPIExprError(expr_info,
  312. "%s: array type must contain single type name"
  313. % source)
  314. value = value[0]
  315. orig_value = "array of %s" %value
  316. # Check if type name for value is okay
  317. if isinstance(value, str):
  318. if not value in all_names:
  319. raise QAPIExprError(expr_info,
  320. "%s uses unknown type '%s'"
  321. % (source, orig_value))
  322. if not all_names[value] in allow_metas:
  323. raise QAPIExprError(expr_info,
  324. "%s cannot use %s type '%s'"
  325. % (source, all_names[value], orig_value))
  326. return
  327. # value is a dictionary, check that each member is okay
  328. if not isinstance(value, OrderedDict):
  329. raise QAPIExprError(expr_info,
  330. "%s should be a dictionary" % source)
  331. if not allow_dict:
  332. raise QAPIExprError(expr_info,
  333. "%s should be a type name" % source)
  334. for (key, arg) in value.items():
  335. check_name(expr_info, "Member of %s" % source, key,
  336. allow_optional=allow_optional)
  337. check_type(expr_info, "Member '%s' of %s" % (key, source), arg,
  338. allow_array=True, allow_dict=True, allow_optional=True,
  339. allow_metas=['built-in', 'union', 'alternate', 'struct',
  340. 'enum'])
  341. def check_command(expr, expr_info):
  342. name = expr['command']
  343. check_type(expr_info, "'data' for command '%s'" % name,
  344. expr.get('data'), allow_dict=True, allow_optional=True,
  345. allow_metas=['union', 'struct'])
  346. returns_meta = ['union', 'struct']
  347. if name in returns_whitelist:
  348. returns_meta += ['built-in', 'alternate', 'enum']
  349. check_type(expr_info, "'returns' for command '%s'" % name,
  350. expr.get('returns'), allow_array=True, allow_dict=True,
  351. allow_optional=True, allow_metas=returns_meta)
  352. def check_event(expr, expr_info):
  353. global events
  354. name = expr['event']
  355. params = expr.get('data')
  356. if name.upper() == 'MAX':
  357. raise QAPIExprError(expr_info, "Event name 'MAX' cannot be created")
  358. events.append(name)
  359. check_type(expr_info, "'data' for event '%s'" % name,
  360. expr.get('data'), allow_dict=True, allow_optional=True,
  361. allow_metas=['union', 'struct'])
  362. if params:
  363. for argname, argentry, optional, structured in parse_args(params):
  364. if structured:
  365. raise QAPIExprError(expr_info,
  366. "Nested structure define in event is not "
  367. "supported, event '%s', argname '%s'"
  368. % (expr['event'], argname))
  369. def check_union(expr, expr_info):
  370. name = expr['union']
  371. base = expr.get('base')
  372. discriminator = expr.get('discriminator')
  373. members = expr['data']
  374. values = { 'MAX': '(automatic)' }
  375. # If the object has a member 'base', its value must name a complex type,
  376. # and there must be a discriminator.
  377. if base is not None:
  378. if discriminator is None:
  379. raise QAPIExprError(expr_info,
  380. "Union '%s' requires a discriminator to go "
  381. "along with base" %name)
  382. # Two types of unions, determined by discriminator.
  383. # With no discriminator it is a simple union.
  384. if discriminator is None:
  385. enum_define = None
  386. allow_metas=['built-in', 'union', 'alternate', 'struct', 'enum']
  387. if base is not None:
  388. raise QAPIExprError(expr_info,
  389. "Simple union '%s' must not have a base"
  390. % name)
  391. # Else, it's a flat union.
  392. else:
  393. # The object must have a string member 'base'.
  394. if not isinstance(base, str):
  395. raise QAPIExprError(expr_info,
  396. "Flat union '%s' must have a string base field"
  397. % name)
  398. base_fields = find_base_fields(base)
  399. if not base_fields:
  400. raise QAPIExprError(expr_info,
  401. "Base '%s' is not a valid type"
  402. % base)
  403. # The value of member 'discriminator' must name a non-optional
  404. # member of the base type.
  405. check_name(expr_info, "Discriminator of flat union '%s'" % name,
  406. discriminator)
  407. discriminator_type = base_fields.get(discriminator)
  408. if not discriminator_type:
  409. raise QAPIExprError(expr_info,
  410. "Discriminator '%s' is not a member of base "
  411. "type '%s'"
  412. % (discriminator, base))
  413. enum_define = find_enum(discriminator_type)
  414. allow_metas=['struct']
  415. # Do not allow string discriminator
  416. if not enum_define:
  417. raise QAPIExprError(expr_info,
  418. "Discriminator '%s' must be of enumeration "
  419. "type" % discriminator)
  420. # Check every branch
  421. for (key, value) in members.items():
  422. check_name(expr_info, "Member of union '%s'" % name, key)
  423. # Each value must name a known type; furthermore, in flat unions,
  424. # branches must be a struct
  425. check_type(expr_info, "Member '%s' of union '%s'" % (key, name),
  426. value, allow_array=True, allow_metas=allow_metas)
  427. # If the discriminator names an enum type, then all members
  428. # of 'data' must also be members of the enum type.
  429. if enum_define:
  430. if not key in enum_define['enum_values']:
  431. raise QAPIExprError(expr_info,
  432. "Discriminator value '%s' is not found in "
  433. "enum '%s'" %
  434. (key, enum_define["enum_name"]))
  435. # Otherwise, check for conflicts in the generated enum
  436. else:
  437. c_key = _generate_enum_string(key)
  438. if c_key in values:
  439. raise QAPIExprError(expr_info,
  440. "Union '%s' member '%s' clashes with '%s'"
  441. % (name, key, values[c_key]))
  442. values[c_key] = key
  443. def check_alternate(expr, expr_info):
  444. name = expr['alternate']
  445. members = expr['data']
  446. values = { 'MAX': '(automatic)' }
  447. types_seen = {}
  448. # Check every branch
  449. for (key, value) in members.items():
  450. check_name(expr_info, "Member of alternate '%s'" % name, key)
  451. # Check for conflicts in the generated enum
  452. c_key = _generate_enum_string(key)
  453. if c_key in values:
  454. raise QAPIExprError(expr_info,
  455. "Alternate '%s' member '%s' clashes with '%s'"
  456. % (name, key, values[c_key]))
  457. values[c_key] = key
  458. # Ensure alternates have no type conflicts.
  459. check_type(expr_info, "Member '%s' of alternate '%s'" % (key, name),
  460. value,
  461. allow_metas=['built-in', 'union', 'struct', 'enum'])
  462. qtype = find_alternate_member_qtype(value)
  463. assert qtype
  464. if qtype in types_seen:
  465. raise QAPIExprError(expr_info,
  466. "Alternate '%s' member '%s' can't "
  467. "be distinguished from member '%s'"
  468. % (name, key, types_seen[qtype]))
  469. types_seen[qtype] = key
  470. def check_enum(expr, expr_info):
  471. name = expr['enum']
  472. members = expr.get('data')
  473. values = { 'MAX': '(automatic)' }
  474. if not isinstance(members, list):
  475. raise QAPIExprError(expr_info,
  476. "Enum '%s' requires an array for 'data'" % name)
  477. for member in members:
  478. check_name(expr_info, "Member of enum '%s'" %name, member,
  479. enum_member=True)
  480. key = _generate_enum_string(member)
  481. if key in values:
  482. raise QAPIExprError(expr_info,
  483. "Enum '%s' member '%s' clashes with '%s'"
  484. % (name, member, values[key]))
  485. values[key] = member
  486. def check_struct(expr, expr_info):
  487. name = expr['type']
  488. members = expr['data']
  489. check_type(expr_info, "'data' for type '%s'" % name, members,
  490. allow_dict=True, allow_optional=True)
  491. check_type(expr_info, "'base' for type '%s'" % name, expr.get('base'),
  492. allow_metas=['struct'])
  493. def check_exprs(schema):
  494. for expr_elem in schema.exprs:
  495. expr = expr_elem['expr']
  496. info = expr_elem['info']
  497. if expr.has_key('enum'):
  498. check_enum(expr, info)
  499. elif expr.has_key('union'):
  500. check_union(expr, info)
  501. elif expr.has_key('alternate'):
  502. check_alternate(expr, info)
  503. elif expr.has_key('type'):
  504. check_struct(expr, info)
  505. elif expr.has_key('command'):
  506. check_command(expr, info)
  507. elif expr.has_key('event'):
  508. check_event(expr, info)
  509. else:
  510. assert False, 'unexpected meta type'
  511. def check_keys(expr_elem, meta, required, optional=[]):
  512. expr = expr_elem['expr']
  513. info = expr_elem['info']
  514. name = expr[meta]
  515. if not isinstance(name, str):
  516. raise QAPIExprError(info,
  517. "'%s' key must have a string value" % meta)
  518. required = required + [ meta ]
  519. for (key, value) in expr.items():
  520. if not key in required and not key in optional:
  521. raise QAPIExprError(info,
  522. "Unknown key '%s' in %s '%s'"
  523. % (key, meta, name))
  524. for key in required:
  525. if not expr.has_key(key):
  526. raise QAPIExprError(info,
  527. "Key '%s' is missing from %s '%s'"
  528. % (key, meta, name))
  529. def parse_schema(input_file):
  530. global all_names
  531. exprs = []
  532. # First pass: read entire file into memory
  533. try:
  534. schema = QAPISchema(open(input_file, "r"))
  535. except (QAPISchemaError, QAPIExprError), e:
  536. print >>sys.stderr, e
  537. exit(1)
  538. try:
  539. # Next pass: learn the types and check for valid expression keys. At
  540. # this point, top-level 'include' has already been flattened.
  541. for builtin in builtin_types.keys():
  542. all_names[builtin] = 'built-in'
  543. for expr_elem in schema.exprs:
  544. expr = expr_elem['expr']
  545. info = expr_elem['info']
  546. if expr.has_key('enum'):
  547. check_keys(expr_elem, 'enum', ['data'])
  548. add_enum(expr['enum'], info, expr['data'])
  549. elif expr.has_key('union'):
  550. check_keys(expr_elem, 'union', ['data'],
  551. ['base', 'discriminator'])
  552. add_union(expr, info)
  553. elif expr.has_key('alternate'):
  554. check_keys(expr_elem, 'alternate', ['data'])
  555. add_name(expr['alternate'], info, 'alternate')
  556. elif expr.has_key('type'):
  557. check_keys(expr_elem, 'type', ['data'], ['base'])
  558. add_struct(expr, info)
  559. elif expr.has_key('command'):
  560. check_keys(expr_elem, 'command', [],
  561. ['data', 'returns', 'gen', 'success-response'])
  562. add_name(expr['command'], info, 'command')
  563. elif expr.has_key('event'):
  564. check_keys(expr_elem, 'event', [], ['data'])
  565. add_name(expr['event'], info, 'event')
  566. else:
  567. raise QAPIExprError(expr_elem['info'],
  568. "Expression is missing metatype")
  569. exprs.append(expr)
  570. # Try again for hidden UnionKind enum
  571. for expr_elem in schema.exprs:
  572. expr = expr_elem['expr']
  573. if expr.has_key('union'):
  574. if not discriminator_find_enum_define(expr):
  575. add_enum('%sKind' % expr['union'], expr_elem['info'],
  576. implicit=True)
  577. elif expr.has_key('alternate'):
  578. add_enum('%sKind' % expr['alternate'], expr_elem['info'],
  579. implicit=True)
  580. # Final pass - validate that exprs make sense
  581. check_exprs(schema)
  582. except QAPIExprError, e:
  583. print >>sys.stderr, e
  584. exit(1)
  585. return exprs
  586. def parse_args(typeinfo):
  587. if isinstance(typeinfo, str):
  588. struct = find_struct(typeinfo)
  589. assert struct != None
  590. typeinfo = struct['data']
  591. for member in typeinfo:
  592. argname = member
  593. argentry = typeinfo[member]
  594. optional = False
  595. structured = False
  596. if member.startswith('*'):
  597. argname = member[1:]
  598. optional = True
  599. if isinstance(argentry, OrderedDict):
  600. structured = True
  601. yield (argname, argentry, optional, structured)
  602. def de_camel_case(name):
  603. new_name = ''
  604. for ch in name:
  605. if ch.isupper() and new_name:
  606. new_name += '_'
  607. if ch == '-':
  608. new_name += '_'
  609. else:
  610. new_name += ch.lower()
  611. return new_name
  612. def camel_case(name):
  613. new_name = ''
  614. first = True
  615. for ch in name:
  616. if ch in ['_', '-']:
  617. first = True
  618. elif first:
  619. new_name += ch.upper()
  620. first = False
  621. else:
  622. new_name += ch.lower()
  623. return new_name
  624. def c_var(name, protect=True):
  625. # ANSI X3J11/88-090, 3.1.1
  626. c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
  627. 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
  628. 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
  629. 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
  630. 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
  631. # ISO/IEC 9899:1999, 6.4.1
  632. c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
  633. # ISO/IEC 9899:2011, 6.4.1
  634. c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
  635. '_Static_assert', '_Thread_local'])
  636. # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
  637. # excluding _.*
  638. gcc_words = set(['asm', 'typeof'])
  639. # C++ ISO/IEC 14882:2003 2.11
  640. cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete',
  641. 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
  642. 'namespace', 'new', 'operator', 'private', 'protected',
  643. 'public', 'reinterpret_cast', 'static_cast', 'template',
  644. 'this', 'throw', 'true', 'try', 'typeid', 'typename',
  645. 'using', 'virtual', 'wchar_t',
  646. # alternative representations
  647. 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
  648. 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
  649. # namespace pollution:
  650. polluted_words = set(['unix', 'errno'])
  651. if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
  652. return "q_" + name
  653. return name.replace('-', '_').lstrip("*")
  654. def c_fun(name, protect=True):
  655. return c_var(name, protect).replace('.', '_')
  656. def c_list_type(name):
  657. return '%sList' % name
  658. def type_name(name):
  659. if type(name) == list:
  660. return c_list_type(name[0])
  661. return name
  662. def add_name(name, info, meta, implicit = False, source = None):
  663. global all_names
  664. if not source:
  665. source = "'%s'" % meta
  666. check_name(info, source, name)
  667. if name in all_names:
  668. raise QAPIExprError(info,
  669. "%s '%s' is already defined"
  670. % (all_names[name], name))
  671. if not implicit and name[-4:] == 'Kind':
  672. raise QAPIExprError(info,
  673. "%s '%s' should not end in 'Kind'"
  674. % (meta, name))
  675. all_names[name] = meta
  676. def add_struct(definition, info):
  677. global struct_types
  678. name = definition['type']
  679. add_name(name, info, 'struct', source="'type'")
  680. struct_types.append(definition)
  681. def find_struct(name):
  682. global struct_types
  683. for struct in struct_types:
  684. if struct['type'] == name:
  685. return struct
  686. return None
  687. def add_union(definition, info):
  688. global union_types
  689. name = definition['union']
  690. add_name(name, info, 'union')
  691. union_types.append(definition)
  692. def find_union(name):
  693. global union_types
  694. for union in union_types:
  695. if union['union'] == name:
  696. return union
  697. return None
  698. def add_enum(name, info, enum_values = None, implicit = False):
  699. global enum_types
  700. add_name(name, info, 'enum', implicit)
  701. enum_types.append({"enum_name": name, "enum_values": enum_values})
  702. def find_enum(name):
  703. global enum_types
  704. for enum in enum_types:
  705. if enum['enum_name'] == name:
  706. return enum
  707. return None
  708. def is_enum(name):
  709. return find_enum(name) != None
  710. eatspace = '\033EATSPACE.'
  711. # A special suffix is added in c_type() for pointer types, and it's
  712. # stripped in mcgen(). So please notice this when you check the return
  713. # value of c_type() outside mcgen().
  714. def c_type(name, is_param=False):
  715. if name == 'str':
  716. if is_param:
  717. return 'const char *' + eatspace
  718. return 'char *' + eatspace
  719. elif name == 'int':
  720. return 'int64_t'
  721. elif (name == 'int8' or name == 'int16' or name == 'int32' or
  722. name == 'int64' or name == 'uint8' or name == 'uint16' or
  723. name == 'uint32' or name == 'uint64'):
  724. return name + '_t'
  725. elif name == 'size':
  726. return 'uint64_t'
  727. elif name == 'bool':
  728. return 'bool'
  729. elif name == 'number':
  730. return 'double'
  731. elif type(name) == list:
  732. return '%s *%s' % (c_list_type(name[0]), eatspace)
  733. elif is_enum(name):
  734. return name
  735. elif name == None or len(name) == 0:
  736. return 'void'
  737. elif name in events:
  738. return '%sEvent *%s' % (camel_case(name), eatspace)
  739. else:
  740. return '%s *%s' % (name, eatspace)
  741. def is_c_ptr(name):
  742. suffix = "*" + eatspace
  743. return c_type(name).endswith(suffix)
  744. def genindent(count):
  745. ret = ""
  746. for i in range(count):
  747. ret += " "
  748. return ret
  749. indent_level = 0
  750. def push_indent(indent_amount=4):
  751. global indent_level
  752. indent_level += indent_amount
  753. def pop_indent(indent_amount=4):
  754. global indent_level
  755. indent_level -= indent_amount
  756. def cgen(code, **kwds):
  757. indent = genindent(indent_level)
  758. lines = code.split('\n')
  759. lines = map(lambda x: indent + x, lines)
  760. return '\n'.join(lines) % kwds + '\n'
  761. def mcgen(code, **kwds):
  762. raw = cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
  763. return re.sub(re.escape(eatspace) + ' *', '', raw)
  764. def basename(filename):
  765. return filename.split("/")[-1]
  766. def guardname(filename):
  767. guard = basename(filename).rsplit(".", 1)[0]
  768. for substr in [".", " ", "-"]:
  769. guard = guard.replace(substr, "_")
  770. return guard.upper() + '_H'
  771. def guardstart(name):
  772. return mcgen('''
  773. #ifndef %(name)s
  774. #define %(name)s
  775. ''',
  776. name=guardname(name))
  777. def guardend(name):
  778. return mcgen('''
  779. #endif /* %(name)s */
  780. ''',
  781. name=guardname(name))
  782. # ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
  783. # ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
  784. # ENUM24_Name -> ENUM24_NAME
  785. def _generate_enum_string(value):
  786. c_fun_str = c_fun(value, False)
  787. if value.isupper():
  788. return c_fun_str
  789. new_name = ''
  790. l = len(c_fun_str)
  791. for i in range(l):
  792. c = c_fun_str[i]
  793. # When c is upper and no "_" appears before, do more checks
  794. if c.isupper() and (i > 0) and c_fun_str[i - 1] != "_":
  795. # Case 1: next string is lower
  796. # Case 2: previous string is digit
  797. if (i < (l - 1) and c_fun_str[i + 1].islower()) or \
  798. c_fun_str[i - 1].isdigit():
  799. new_name += '_'
  800. new_name += c
  801. return new_name.lstrip('_').upper()
  802. def generate_enum_full_value(enum_name, enum_value):
  803. abbrev_string = _generate_enum_string(enum_name)
  804. value_string = _generate_enum_string(enum_value)
  805. return "%s_%s" % (abbrev_string, value_string)