blob: f96a7772e565017fc6550b86e1ff9b5821ef19b0 [file] [log] [blame]
Michael Roth0f923be2011-07-19 14:50:39 -05001#
2# QAPI helper library
3#
4# Copyright IBM, Corp. 2011
Eric Blakefe2a9302015-05-04 09:05:02 -06005# Copyright (c) 2013-2015 Red Hat Inc.
Michael Roth0f923be2011-07-19 14:50:39 -05006#
7# Authors:
8# Anthony Liguori <aliguori@us.ibm.com>
Markus Armbrusterc7a3f252013-07-27 17:41:55 +02009# Markus Armbruster <armbru@redhat.com>
Michael Roth0f923be2011-07-19 14:50:39 -050010#
Markus Armbruster678e48a2014-03-01 08:40:34 +010011# This work is licensed under the terms of the GNU GPL, version 2.
12# See the COPYING file in the top-level directory.
Michael Roth0f923be2011-07-19 14:50:39 -050013
Lluís Vilanovaa719a272014-05-07 20:46:15 +020014import re
Michael Roth0f923be2011-07-19 14:50:39 -050015from ordereddict import OrderedDict
Markus Armbruster12f8e1b2015-04-02 14:46:39 +020016import errno
Markus Armbruster2114f5a2015-04-02 13:12:21 +020017import getopt
Lluís Vilanova33aaad52014-05-02 15:52:35 +020018import os
Markus Armbruster2caba362013-07-27 17:41:56 +020019import sys
Markus Armbruster47299262015-05-14 06:50:47 -060020import string
Michael Roth0f923be2011-07-19 14:50:39 -050021
Eric Blakeb52c4b92015-05-04 09:05:00 -060022builtin_types = {
Kevin Wolf69dd62d2013-07-08 16:14:21 +020023 'str': 'QTYPE_QSTRING',
24 'int': 'QTYPE_QINT',
25 'number': 'QTYPE_QFLOAT',
26 'bool': 'QTYPE_QBOOL',
27 'int8': 'QTYPE_QINT',
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',
Eric Blakecb17f792015-05-04 09:05:01 -060035 'size': 'QTYPE_QINT',
Kevin Wolf69dd62d2013-07-08 16:14:21 +020036}
37
Eric Blake10d4d992015-05-04 09:05:23 -060038# Whitelist of commands allowed to return a non-dictionary
39returns_whitelist = [
40 # From QMP:
41 'human-monitor-command',
42 'query-migrate-cache-size',
43 'query-tpm-models',
44 'query-tpm-types',
45 'ringbuf-read',
46
47 # From QGA:
48 'guest-file-open',
49 'guest-fsfreeze-freeze',
50 'guest-fsfreeze-freeze-list',
51 'guest-fsfreeze-status',
52 'guest-fsfreeze-thaw',
53 'guest-get-time',
54 'guest-set-vcpus',
55 'guest-sync',
56 'guest-sync-delimited',
57
58 # From qapi-schema-test:
59 'user_def_cmd3',
60]
61
Eric Blake4dc2e692015-05-04 09:05:17 -060062enum_types = []
63struct_types = []
64union_types = []
65events = []
66all_names = {}
67
Lluís Vilanovaa719a272014-05-07 20:46:15 +020068def error_path(parent):
69 res = ""
70 while parent:
71 res = ("In file included from %s:%d:\n" % (parent['file'],
72 parent['line'])) + res
73 parent = parent['parent']
74 return res
75
Markus Armbruster2caba362013-07-27 17:41:56 +020076class QAPISchemaError(Exception):
77 def __init__(self, schema, msg):
Lluís Vilanovaa719a272014-05-07 20:46:15 +020078 self.input_file = schema.input_file
Markus Armbruster2caba362013-07-27 17:41:56 +020079 self.msg = msg
Wenchao Xia515b9432014-03-04 18:44:33 -080080 self.col = 1
81 self.line = schema.line
82 for ch in schema.src[schema.line_pos:schema.pos]:
83 if ch == '\t':
Markus Armbruster2caba362013-07-27 17:41:56 +020084 self.col = (self.col + 7) % 8 + 1
85 else:
86 self.col += 1
Lluís Vilanovaa719a272014-05-07 20:46:15 +020087 self.info = schema.parent_info
Markus Armbruster2caba362013-07-27 17:41:56 +020088
89 def __str__(self):
Lluís Vilanovaa719a272014-05-07 20:46:15 +020090 return error_path(self.info) + \
91 "%s:%d:%d: %s" % (self.input_file, self.line, self.col, self.msg)
Markus Armbruster2caba362013-07-27 17:41:56 +020092
Wenchao Xiab86b05e2014-03-04 18:44:34 -080093class QAPIExprError(Exception):
94 def __init__(self, expr_info, msg):
Lluís Vilanovaa719a272014-05-07 20:46:15 +020095 self.info = expr_info
Wenchao Xiab86b05e2014-03-04 18:44:34 -080096 self.msg = msg
97
98 def __str__(self):
Lluís Vilanovaa719a272014-05-07 20:46:15 +020099 return error_path(self.info['parent']) + \
100 "%s:%d: %s" % (self.info['file'], self.info['line'], self.msg)
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800101
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200102class QAPISchema:
Michael Roth0f923be2011-07-19 14:50:39 -0500103
Benoît Canet24fd8482014-05-16 12:51:56 +0200104 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"""
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200109 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)]
Benoît Canet24fd8482014-05-16 12:51:56 +0200115 previously_included.append(input_fname)
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200116 self.parent_info = parent_info
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200117 self.src = fp.read()
118 if self.src == '' or self.src[-1] != '\n':
119 self.src += '\n'
120 self.cursor = 0
Wenchao Xia515b9432014-03-04 18:44:33 -0800121 self.line = 1
122 self.line_pos = 0
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200123 self.exprs = []
124 self.accept()
Michael Roth0f923be2011-07-19 14:50:39 -0500125
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200126 while self.tok != None:
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200127 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:
130 if len(expr) != 1:
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'
136 % include)
137 include_path = os.path.join(self.input_dir, include)
Stefan Hajnoczi7ac9a9d2014-08-27 12:08:51 +0100138 for elem in self.include_hist:
139 if include_path == elem[1]:
140 raise QAPIExprError(expr_info, "Inclusion loop for %s"
141 % include)
Benoît Canet24fd8482014-05-16 12:51:56 +0200142 # skip multiple include of the same file
143 if include_path in previously_included:
144 continue
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200145 try:
146 fobj = open(include_path, 'r')
Luiz Capitulino34788812014-05-20 13:50:19 -0400147 except IOError, e:
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200148 raise QAPIExprError(expr_info,
149 '%s: %s' % (e.strerror, include))
Benoît Canet24fd8482014-05-16 12:51:56 +0200150 exprs_include = QAPISchema(fobj, include, self.include_hist,
151 previously_included, expr_info)
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200152 self.exprs.extend(exprs_include.exprs)
153 else:
154 expr_elem = {'expr': expr,
155 'info': expr_info}
156 self.exprs.append(expr_elem)
Michael Roth0f923be2011-07-19 14:50:39 -0500157
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200158 def accept(self):
159 while True:
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200160 self.tok = self.src[self.cursor]
Markus Armbruster2caba362013-07-27 17:41:56 +0200161 self.pos = self.cursor
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200162 self.cursor += 1
163 self.val = None
Michael Roth0f923be2011-07-19 14:50:39 -0500164
Markus Armbrusterf1a145e2013-07-27 17:42:01 +0200165 if self.tok == '#':
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200166 self.cursor = self.src.find('\n', self.cursor)
167 elif self.tok in ['{', '}', ':', ',', '[', ']']:
168 return
169 elif self.tok == "'":
170 string = ''
171 esc = False
172 while True:
173 ch = self.src[self.cursor]
174 self.cursor += 1
175 if ch == '\n':
Markus Armbruster2caba362013-07-27 17:41:56 +0200176 raise QAPISchemaError(self,
177 'Missing terminating "\'"')
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200178 if esc:
Eric Blakea7f59662015-05-04 09:05:36 -0600179 if ch == 'b':
180 string += '\b'
181 elif ch == 'f':
182 string += '\f'
183 elif ch == 'n':
184 string += '\n'
185 elif ch == 'r':
186 string += '\r'
187 elif ch == 't':
188 string += '\t'
189 elif ch == 'u':
190 value = 0
191 for x in range(0, 4):
192 ch = self.src[self.cursor]
193 self.cursor += 1
194 if ch not in "0123456789abcdefABCDEF":
195 raise QAPISchemaError(self,
196 '\\u escape needs 4 '
197 'hex digits')
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')
208 string += chr(value)
209 elif ch in "\\/'\"":
210 string += ch
211 else:
212 raise QAPISchemaError(self,
213 "Unknown escape \\%s" %ch)
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200214 esc = False
215 elif ch == "\\":
216 esc = True
217 elif ch == "'":
218 self.val = string
219 return
220 else:
221 string += ch
Fam Zhenge53188a2015-05-04 09:05:18 -0600222 elif self.tok in "tfn":
223 val = self.src[self.cursor - 1:]
224 if val.startswith("true"):
225 self.val = True
226 self.cursor += 3
227 return
228 elif val.startswith("false"):
229 self.val = False
230 self.cursor += 4
231 return
232 elif val.startswith("null"):
233 self.val = None
234 self.cursor += 3
235 return
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200236 elif self.tok == '\n':
237 if self.cursor == len(self.src):
238 self.tok = None
239 return
Wenchao Xia515b9432014-03-04 18:44:33 -0800240 self.line += 1
241 self.line_pos = self.cursor
Markus Armbruster9213aa52013-07-27 17:41:57 +0200242 elif not self.tok.isspace():
243 raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
Michael Roth0f923be2011-07-19 14:50:39 -0500244
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200245 def get_members(self):
246 expr = OrderedDict()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200247 if self.tok == '}':
248 self.accept()
249 return expr
250 if self.tok != "'":
251 raise QAPISchemaError(self, 'Expected string or "}"')
252 while True:
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200253 key = self.val
254 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200255 if self.tok != ':':
256 raise QAPISchemaError(self, 'Expected ":"')
257 self.accept()
Wenchao Xia4b359912014-03-04 18:44:32 -0800258 if key in expr:
259 raise QAPISchemaError(self, 'Duplicate key "%s"' % key)
Markus Armbruster5f3cd2b2013-07-27 17:41:59 +0200260 expr[key] = self.get_expr(True)
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200261 if self.tok == '}':
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200262 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200263 return expr
264 if self.tok != ',':
265 raise QAPISchemaError(self, 'Expected "," or "}"')
266 self.accept()
267 if self.tok != "'":
268 raise QAPISchemaError(self, 'Expected string')
Michael Roth0f923be2011-07-19 14:50:39 -0500269
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200270 def get_values(self):
271 expr = []
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200272 if self.tok == ']':
273 self.accept()
274 return expr
Fam Zhenge53188a2015-05-04 09:05:18 -0600275 if not self.tok in "{['tfn":
276 raise QAPISchemaError(self, 'Expected "{", "[", "]", string, '
277 'boolean or "null"')
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200278 while True:
Markus Armbruster5f3cd2b2013-07-27 17:41:59 +0200279 expr.append(self.get_expr(True))
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200280 if self.tok == ']':
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200281 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200282 return expr
283 if self.tok != ',':
284 raise QAPISchemaError(self, 'Expected "," or "]"')
285 self.accept()
Michael Roth0f923be2011-07-19 14:50:39 -0500286
Markus Armbruster5f3cd2b2013-07-27 17:41:59 +0200287 def get_expr(self, nested):
288 if self.tok != '{' and not nested:
289 raise QAPISchemaError(self, 'Expected "{"')
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200290 if self.tok == '{':
291 self.accept()
292 expr = self.get_members()
293 elif self.tok == '[':
294 self.accept()
295 expr = self.get_values()
Fam Zhenge53188a2015-05-04 09:05:18 -0600296 elif self.tok in "'tfn":
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200297 expr = self.val
298 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200299 else:
300 raise QAPISchemaError(self, 'Expected "{", "[" or string')
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200301 return expr
Kevin Wolfbd9927f2013-07-01 16:31:50 +0200302
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800303def find_base_fields(base):
304 base_struct_define = find_struct(base)
305 if not base_struct_define:
306 return None
307 return base_struct_define['data']
308
Eric Blake811d04f2015-05-04 09:05:10 -0600309# Return the qtype of an alternate branch, or None on error.
310def find_alternate_member_qtype(qapi_type):
Eric Blake44bd1272015-05-04 09:05:08 -0600311 if builtin_types.has_key(qapi_type):
312 return builtin_types[qapi_type]
313 elif find_struct(qapi_type):
314 return "QTYPE_QDICT"
315 elif find_enum(qapi_type):
316 return "QTYPE_QSTRING"
Eric Blake811d04f2015-05-04 09:05:10 -0600317 elif find_union(qapi_type):
318 return "QTYPE_QDICT"
Eric Blake44bd1272015-05-04 09:05:08 -0600319 return None
320
Wenchao Xiabceae762014-03-06 17:08:56 -0800321# Return the discriminator enum define if discriminator is specified as an
322# enum type, otherwise return None.
323def discriminator_find_enum_define(expr):
324 base = expr.get('base')
325 discriminator = expr.get('discriminator')
326
327 if not (discriminator and base):
328 return None
329
330 base_fields = find_base_fields(base)
331 if not base_fields:
332 return None
333
334 discriminator_type = base_fields.get(discriminator)
335 if not discriminator_type:
336 return None
337
338 return find_enum(discriminator_type)
339
Eric Blakec9e0a792015-05-04 09:05:22 -0600340valid_name = re.compile('^[a-zA-Z_][a-zA-Z0-9_.-]*$')
341def check_name(expr_info, source, name, allow_optional = False,
342 enum_member = False):
343 global valid_name
344 membername = name
345
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'"
354 % (source, name))
355 # Enum members can start with a digit, because the generated C
356 # code always prefixes it with the enum name
357 if enum_member:
358 membername = '_' + membername
359 if not valid_name.match(membername):
360 raise QAPIExprError(expr_info,
361 "%s uses invalid name '%s'" % (source, name))
362
Eric Blakedd883c62015-05-04 09:05:21 -0600363def check_type(expr_info, source, value, allow_array = False,
Eric Blake2cbf0992015-05-04 09:05:24 -0600364 allow_dict = False, allow_optional = False,
365 allow_star = False, allow_metas = []):
Eric Blakedd883c62015-05-04 09:05:21 -0600366 global all_names
367 orig_value = value
368
369 if value is None:
370 return
371
Eric Blake2cbf0992015-05-04 09:05:24 -0600372 if allow_star and value == '**':
Eric Blakedd883c62015-05-04 09:05:21 -0600373 return
374
375 # Check if array type for value is okay
376 if isinstance(value, list):
377 if not allow_array:
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"
383 % source)
384 value = value[0]
385 orig_value = "array of %s" %value
386
387 # Check if type name for value is okay
388 if isinstance(value, str):
Eric Blake2cbf0992015-05-04 09:05:24 -0600389 if value == '**':
390 raise QAPIExprError(expr_info,
391 "%s uses '**' but did not request 'gen':false"
392 % source)
Eric Blakedd883c62015-05-04 09:05:21 -0600393 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))
401 return
402
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)
407 if not allow_dict:
408 raise QAPIExprError(expr_info,
409 "%s should be a type name" % source)
410 for (key, arg) in value.items():
Eric Blakec9e0a792015-05-04 09:05:22 -0600411 check_name(expr_info, "Member of %s" % source, key,
412 allow_optional=allow_optional)
Eric Blake6b5abc72015-05-04 09:05:33 -0600413 # Todo: allow dictionaries to represent default values of
414 # an optional argument.
Eric Blakedd883c62015-05-04 09:05:21 -0600415 check_type(expr_info, "Member '%s' of %s" % (key, source), arg,
Eric Blake6b5abc72015-05-04 09:05:33 -0600416 allow_array=True, allow_star=allow_star,
Eric Blakedd883c62015-05-04 09:05:21 -0600417 allow_metas=['built-in', 'union', 'alternate', 'struct',
Eric Blake6b5abc72015-05-04 09:05:33 -0600418 'enum'])
Eric Blakedd883c62015-05-04 09:05:21 -0600419
Eric Blakeff55d722015-05-04 09:05:37 -0600420def check_member_clash(expr_info, base_name, data, source = ""):
421 base = find_struct(base_name)
422 assert base
423 base_members = base['data']
424 for key in data.keys():
425 if key.startswith('*'):
426 key = key[1:]
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))
431 if base.get('base'):
432 check_member_clash(expr_info, base['base'], data, source)
433
Eric Blakedd883c62015-05-04 09:05:21 -0600434def check_command(expr, expr_info):
435 name = expr['command']
Eric Blake2cbf0992015-05-04 09:05:24 -0600436 allow_star = expr.has_key('gen')
437
Eric Blakedd883c62015-05-04 09:05:21 -0600438 check_type(expr_info, "'data' for command '%s'" % name,
Eric Blakec9e0a792015-05-04 09:05:22 -0600439 expr.get('data'), allow_dict=True, allow_optional=True,
Eric Blake2cbf0992015-05-04 09:05:24 -0600440 allow_metas=['union', 'struct'], allow_star=allow_star)
Eric Blake10d4d992015-05-04 09:05:23 -0600441 returns_meta = ['union', 'struct']
442 if name in returns_whitelist:
443 returns_meta += ['built-in', 'alternate', 'enum']
Eric Blakedd883c62015-05-04 09:05:21 -0600444 check_type(expr_info, "'returns' for command '%s'" % name,
445 expr.get('returns'), allow_array=True, allow_dict=True,
Eric Blake2cbf0992015-05-04 09:05:24 -0600446 allow_optional=True, allow_metas=returns_meta,
447 allow_star=allow_star)
Eric Blakedd883c62015-05-04 09:05:21 -0600448
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200449def check_event(expr, expr_info):
Eric Blake4dc2e692015-05-04 09:05:17 -0600450 global events
451 name = expr['event']
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200452 params = expr.get('data')
Eric Blake4dc2e692015-05-04 09:05:17 -0600453
454 if name.upper() == 'MAX':
455 raise QAPIExprError(expr_info, "Event name 'MAX' cannot be created")
456 events.append(name)
Eric Blakedd883c62015-05-04 09:05:21 -0600457 check_type(expr_info, "'data' for event '%s'" % name,
Eric Blakec9e0a792015-05-04 09:05:22 -0600458 expr.get('data'), allow_dict=True, allow_optional=True,
Eric Blakedd883c62015-05-04 09:05:21 -0600459 allow_metas=['union', 'struct'])
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200460
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800461def check_union(expr, expr_info):
462 name = expr['union']
463 base = expr.get('base')
464 discriminator = expr.get('discriminator')
465 members = expr['data']
Eric Blake44bd1272015-05-04 09:05:08 -0600466 values = { 'MAX': '(automatic)' }
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800467
Eric Blakefd41dd42015-05-04 09:05:25 -0600468 # If the object has a member 'base', its value must name a struct,
Eric Blakea8d4a2e2015-05-04 09:05:07 -0600469 # and there must be a discriminator.
470 if base is not None:
471 if discriminator is None:
472 raise QAPIExprError(expr_info,
473 "Union '%s' requires a discriminator to go "
474 "along with base" %name)
Eric Blake44bd1272015-05-04 09:05:08 -0600475
Eric Blake811d04f2015-05-04 09:05:10 -0600476 # Two types of unions, determined by discriminator.
Eric Blake811d04f2015-05-04 09:05:10 -0600477
478 # With no discriminator it is a simple union.
479 if discriminator is None:
Eric Blake44bd1272015-05-04 09:05:08 -0600480 enum_define = None
Eric Blakedd883c62015-05-04 09:05:21 -0600481 allow_metas=['built-in', 'union', 'alternate', 'struct', 'enum']
Eric Blake44bd1272015-05-04 09:05:08 -0600482 if base is not None:
483 raise QAPIExprError(expr_info,
Eric Blake811d04f2015-05-04 09:05:10 -0600484 "Simple union '%s' must not have a base"
Eric Blake44bd1272015-05-04 09:05:08 -0600485 % name)
486
487 # Else, it's a flat union.
488 else:
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"
493 % name)
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800494 base_fields = find_base_fields(base)
495 if not base_fields:
496 raise QAPIExprError(expr_info,
Eric Blakefd41dd42015-05-04 09:05:25 -0600497 "Base '%s' is not a valid struct"
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800498 % base)
499
Eric Blakec9e0a792015-05-04 09:05:22 -0600500 # The value of member 'discriminator' must name a non-optional
Eric Blakefd41dd42015-05-04 09:05:25 -0600501 # member of the base struct.
Eric Blakec9e0a792015-05-04 09:05:22 -0600502 check_name(expr_info, "Discriminator of flat union '%s'" % name,
503 discriminator)
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800504 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 "
Eric Blakefd41dd42015-05-04 09:05:25 -0600508 "struct '%s'"
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800509 % (discriminator, base))
510 enum_define = find_enum(discriminator_type)
Eric Blakedd883c62015-05-04 09:05:21 -0600511 allow_metas=['struct']
Wenchao Xia52230702014-03-04 18:44:39 -0800512 # Do not allow string discriminator
513 if not enum_define:
514 raise QAPIExprError(expr_info,
515 "Discriminator '%s' must be of enumeration "
516 "type" % discriminator)
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800517
518 # Check every branch
519 for (key, value) in members.items():
Eric Blakec9e0a792015-05-04 09:05:22 -0600520 check_name(expr_info, "Member of union '%s'" % name, key)
521
Eric Blakedd883c62015-05-04 09:05:21 -0600522 # Each value must name a known type; furthermore, in flat unions,
Eric Blakeff55d722015-05-04 09:05:37 -0600523 # branches must be a struct with no overlapping member names
Eric Blakedd883c62015-05-04 09:05:21 -0600524 check_type(expr_info, "Member '%s' of union '%s'" % (key, name),
525 value, allow_array=True, allow_metas=allow_metas)
Eric Blakeff55d722015-05-04 09:05:37 -0600526 if base:
527 branch_struct = find_struct(value)
528 assert branch_struct
529 check_member_clash(expr_info, base, branch_struct['data'],
530 " of branch '%s'" % key)
Eric Blakedd883c62015-05-04 09:05:21 -0600531
Eric Blake44bd1272015-05-04 09:05:08 -0600532 # If the discriminator names an enum type, then all members
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800533 # of 'data' must also be members of the enum type.
Eric Blake44bd1272015-05-04 09:05:08 -0600534 if enum_define:
535 if not key in enum_define['enum_values']:
536 raise QAPIExprError(expr_info,
537 "Discriminator value '%s' is not found in "
538 "enum '%s'" %
539 (key, enum_define["enum_name"]))
540
541 # Otherwise, check for conflicts in the generated enum
542 else:
Markus Armbrusterfa6068a2015-05-14 06:50:49 -0600543 c_key = camel_to_upper(key)
Eric Blake44bd1272015-05-04 09:05:08 -0600544 if c_key in values:
545 raise QAPIExprError(expr_info,
546 "Union '%s' member '%s' clashes with '%s'"
547 % (name, key, values[c_key]))
548 values[c_key] = key
549
Eric Blake811d04f2015-05-04 09:05:10 -0600550def check_alternate(expr, expr_info):
Eric Blakeab916fa2015-05-04 09:05:13 -0600551 name = expr['alternate']
Eric Blake811d04f2015-05-04 09:05:10 -0600552 members = expr['data']
553 values = { 'MAX': '(automatic)' }
554 types_seen = {}
Eric Blake44bd1272015-05-04 09:05:08 -0600555
Eric Blake811d04f2015-05-04 09:05:10 -0600556 # Check every branch
557 for (key, value) in members.items():
Eric Blakec9e0a792015-05-04 09:05:22 -0600558 check_name(expr_info, "Member of alternate '%s'" % name, key)
559
Eric Blake811d04f2015-05-04 09:05:10 -0600560 # Check for conflicts in the generated enum
Markus Armbrusterfa6068a2015-05-14 06:50:49 -0600561 c_key = camel_to_upper(key)
Eric Blake811d04f2015-05-04 09:05:10 -0600562 if c_key in values:
563 raise QAPIExprError(expr_info,
Eric Blakeab916fa2015-05-04 09:05:13 -0600564 "Alternate '%s' member '%s' clashes with '%s'"
565 % (name, key, values[c_key]))
Eric Blake811d04f2015-05-04 09:05:10 -0600566 values[c_key] = key
567
568 # Ensure alternates have no type conflicts.
Eric Blakedd883c62015-05-04 09:05:21 -0600569 check_type(expr_info, "Member '%s' of alternate '%s'" % (key, name),
570 value,
571 allow_metas=['built-in', 'union', 'struct', 'enum'])
Eric Blake811d04f2015-05-04 09:05:10 -0600572 qtype = find_alternate_member_qtype(value)
Eric Blakedd883c62015-05-04 09:05:21 -0600573 assert qtype
Eric Blake811d04f2015-05-04 09:05:10 -0600574 if qtype in types_seen:
575 raise QAPIExprError(expr_info,
Eric Blakeab916fa2015-05-04 09:05:13 -0600576 "Alternate '%s' member '%s' can't "
Eric Blake811d04f2015-05-04 09:05:10 -0600577 "be distinguished from member '%s'"
578 % (name, key, types_seen[qtype]))
579 types_seen[qtype] = key
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800580
Eric Blakecf393592015-05-04 09:05:04 -0600581def check_enum(expr, expr_info):
582 name = expr['enum']
583 members = expr.get('data')
584 values = { 'MAX': '(automatic)' }
585
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:
Eric Blakec9e0a792015-05-04 09:05:22 -0600590 check_name(expr_info, "Member of enum '%s'" %name, member,
591 enum_member=True)
Markus Armbrusterfa6068a2015-05-14 06:50:49 -0600592 key = camel_to_upper(member)
Eric Blakecf393592015-05-04 09:05:04 -0600593 if key in values:
594 raise QAPIExprError(expr_info,
595 "Enum '%s' member '%s' clashes with '%s'"
596 % (name, member, values[key]))
597 values[key] = member
598
Eric Blakedd883c62015-05-04 09:05:21 -0600599def check_struct(expr, expr_info):
Eric Blakefd41dd42015-05-04 09:05:25 -0600600 name = expr['struct']
Eric Blakedd883c62015-05-04 09:05:21 -0600601 members = expr['data']
602
Eric Blakefd41dd42015-05-04 09:05:25 -0600603 check_type(expr_info, "'data' for struct '%s'" % name, members,
Eric Blakec9e0a792015-05-04 09:05:22 -0600604 allow_dict=True, allow_optional=True)
Eric Blakefd41dd42015-05-04 09:05:25 -0600605 check_type(expr_info, "'base' for struct '%s'" % name, expr.get('base'),
Eric Blakedd883c62015-05-04 09:05:21 -0600606 allow_metas=['struct'])
Eric Blakeff55d722015-05-04 09:05:37 -0600607 if expr.get('base'):
608 check_member_clash(expr_info, expr['base'], expr['data'])
Eric Blakedd883c62015-05-04 09:05:21 -0600609
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800610def check_exprs(schema):
611 for expr_elem in schema.exprs:
612 expr = expr_elem['expr']
Eric Blakecf393592015-05-04 09:05:04 -0600613 info = expr_elem['info']
614
615 if expr.has_key('enum'):
616 check_enum(expr, info)
617 elif expr.has_key('union'):
Eric Blakeab916fa2015-05-04 09:05:13 -0600618 check_union(expr, info)
619 elif expr.has_key('alternate'):
620 check_alternate(expr, info)
Eric Blakefd41dd42015-05-04 09:05:25 -0600621 elif expr.has_key('struct'):
Eric Blakedd883c62015-05-04 09:05:21 -0600622 check_struct(expr, info)
623 elif expr.has_key('command'):
624 check_command(expr, info)
Eric Blakecf393592015-05-04 09:05:04 -0600625 elif expr.has_key('event'):
626 check_event(expr, info)
Eric Blakedd883c62015-05-04 09:05:21 -0600627 else:
628 assert False, 'unexpected meta type'
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800629
Eric Blake0545f6b2015-05-04 09:05:15 -0600630def check_keys(expr_elem, meta, required, optional=[]):
631 expr = expr_elem['expr']
632 info = expr_elem['info']
633 name = expr[meta]
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'"
642 % (key, meta, name))
Eric Blake2cbf0992015-05-04 09:05:24 -0600643 if (key == 'gen' or key == 'success-response') and value != False:
644 raise QAPIExprError(info,
645 "'%s' of %s '%s' should only use false value"
646 % (key, meta, name))
Eric Blake0545f6b2015-05-04 09:05:15 -0600647 for key in required:
648 if not expr.has_key(key):
649 raise QAPIExprError(info,
650 "Key '%s' is missing from %s '%s'"
651 % (key, meta, name))
652
653
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200654def parse_schema(input_file):
Eric Blake4dc2e692015-05-04 09:05:17 -0600655 global all_names
656 exprs = []
657
Eric Blake268a1c52015-05-04 09:05:09 -0600658 # First pass: read entire file into memory
Markus Armbruster2caba362013-07-27 17:41:56 +0200659 try:
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200660 schema = QAPISchema(open(input_file, "r"))
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200661 except (QAPISchemaError, QAPIExprError), e:
Markus Armbruster2caba362013-07-27 17:41:56 +0200662 print >>sys.stderr, e
663 exit(1)
664
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800665 try:
Eric Blake0545f6b2015-05-04 09:05:15 -0600666 # Next pass: learn the types and check for valid expression keys. At
667 # this point, top-level 'include' has already been flattened.
Eric Blake4dc2e692015-05-04 09:05:17 -0600668 for builtin in builtin_types.keys():
669 all_names[builtin] = 'built-in'
Eric Blake268a1c52015-05-04 09:05:09 -0600670 for expr_elem in schema.exprs:
671 expr = expr_elem['expr']
Eric Blake4dc2e692015-05-04 09:05:17 -0600672 info = expr_elem['info']
Eric Blake268a1c52015-05-04 09:05:09 -0600673 if expr.has_key('enum'):
Eric Blake0545f6b2015-05-04 09:05:15 -0600674 check_keys(expr_elem, 'enum', ['data'])
Eric Blake4dc2e692015-05-04 09:05:17 -0600675 add_enum(expr['enum'], info, expr['data'])
Eric Blake268a1c52015-05-04 09:05:09 -0600676 elif expr.has_key('union'):
Eric Blake0545f6b2015-05-04 09:05:15 -0600677 check_keys(expr_elem, 'union', ['data'],
678 ['base', 'discriminator'])
Eric Blake4dc2e692015-05-04 09:05:17 -0600679 add_union(expr, info)
Eric Blake0545f6b2015-05-04 09:05:15 -0600680 elif expr.has_key('alternate'):
681 check_keys(expr_elem, 'alternate', ['data'])
Eric Blake4dc2e692015-05-04 09:05:17 -0600682 add_name(expr['alternate'], info, 'alternate')
Eric Blakefd41dd42015-05-04 09:05:25 -0600683 elif expr.has_key('struct'):
684 check_keys(expr_elem, 'struct', ['data'], ['base'])
Eric Blake4dc2e692015-05-04 09:05:17 -0600685 add_struct(expr, info)
Eric Blake0545f6b2015-05-04 09:05:15 -0600686 elif expr.has_key('command'):
687 check_keys(expr_elem, 'command', [],
688 ['data', 'returns', 'gen', 'success-response'])
Eric Blake4dc2e692015-05-04 09:05:17 -0600689 add_name(expr['command'], info, 'command')
Eric Blake0545f6b2015-05-04 09:05:15 -0600690 elif expr.has_key('event'):
691 check_keys(expr_elem, 'event', [], ['data'])
Eric Blake4dc2e692015-05-04 09:05:17 -0600692 add_name(expr['event'], info, 'event')
Eric Blake0545f6b2015-05-04 09:05:15 -0600693 else:
694 raise QAPIExprError(expr_elem['info'],
695 "Expression is missing metatype")
Eric Blake268a1c52015-05-04 09:05:09 -0600696 exprs.append(expr)
697
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):
Eric Blake4dc2e692015-05-04 09:05:17 -0600703 add_enum('%sKind' % expr['union'], expr_elem['info'],
704 implicit=True)
Eric Blakeab916fa2015-05-04 09:05:13 -0600705 elif expr.has_key('alternate'):
Eric Blake4dc2e692015-05-04 09:05:17 -0600706 add_enum('%sKind' % expr['alternate'], expr_elem['info'],
707 implicit=True)
Eric Blake268a1c52015-05-04 09:05:09 -0600708
709 # Final pass - validate that exprs make sense
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800710 check_exprs(schema)
711 except QAPIExprError, e:
712 print >>sys.stderr, e
713 exit(1)
714
Michael Roth0f923be2011-07-19 14:50:39 -0500715 return exprs
716
717def parse_args(typeinfo):
Eric Blakefe2a9302015-05-04 09:05:02 -0600718 if isinstance(typeinfo, str):
Kevin Wolfb35284e2013-07-01 16:31:51 +0200719 struct = find_struct(typeinfo)
720 assert struct != None
721 typeinfo = struct['data']
722
Michael Roth0f923be2011-07-19 14:50:39 -0500723 for member in typeinfo:
724 argname = member
725 argentry = typeinfo[member]
726 optional = False
Michael Roth0f923be2011-07-19 14:50:39 -0500727 if member.startswith('*'):
728 argname = member[1:]
729 optional = True
Eric Blake6b5abc72015-05-04 09:05:33 -0600730 # Todo: allow argentry to be OrderedDict, for providing the
731 # value of an optional argument.
732 yield (argname, argentry, optional)
Michael Roth0f923be2011-07-19 14:50:39 -0500733
Michael Roth0f923be2011-07-19 14:50:39 -0500734def camel_case(name):
735 new_name = ''
736 first = True
737 for ch in name:
738 if ch in ['_', '-']:
739 first = True
740 elif first:
741 new_name += ch.upper()
742 first = False
743 else:
744 new_name += ch.lower()
745 return new_name
746
Markus Armbruster849bc532015-05-14 06:50:53 -0600747# ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
748# ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
749# ENUM24_Name -> ENUM24_NAME
750def camel_to_upper(value):
751 c_fun_str = c_name(value, False)
752 if value.isupper():
753 return c_fun_str
754
755 new_name = ''
756 l = len(c_fun_str)
757 for i in range(l):
758 c = c_fun_str[i]
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():
765 new_name += '_'
766 new_name += c
767 return new_name.lstrip('_').upper()
768
769def c_enum_const(type_name, const_name):
770 return camel_to_upper(type_name + '_' + const_name)
771
Eric Blake18df5152015-05-14 06:50:48 -0600772c_name_trans = string.maketrans('.-', '__')
Markus Armbruster47299262015-05-14 06:50:47 -0600773
Eric Blakec6405b52015-05-14 06:50:55 -0600774# Map @name to a valid C identifier.
775# If @protect, avoid returning certain ticklish identifiers (like
776# C keywords) by prepending "q_".
777#
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'
Eric Blake18df5152015-05-14 06:50:48 -0600783def c_name(name, protect=True):
Blue Swirl427a1a22012-07-30 15:46:55 +0000784 # 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
796 # excluding _.*
797 gcc_words = set(['asm', 'typeof'])
Tomoki Sekiyama6f880092013-08-07 11:39:43 -0400798 # 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'])
Paolo Bonzini10577252012-09-19 16:31:07 +0200808 # namespace pollution:
Max Reitz8592a542013-12-20 19:28:18 +0100809 polluted_words = set(['unix', 'errno'])
Tomoki Sekiyama6f880092013-08-07 11:39:43 -0400810 if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
Blue Swirl427a1a22012-07-30 15:46:55 +0000811 return "q_" + name
Eric Blake18df5152015-05-14 06:50:48 -0600812 return name.translate(c_name_trans)
Michael Roth0f923be2011-07-19 14:50:39 -0500813
Eric Blakec6405b52015-05-14 06:50:55 -0600814# Map type @name to the C typedef name for the list form.
815#
816# ['Name'] -> 'NameList', ['x-Foo'] -> 'x_FooList', ['int'] -> 'intList'
Michael Roth0f923be2011-07-19 14:50:39 -0500817def c_list_type(name):
Eric Blakec6405b52015-05-14 06:50:55 -0600818 return type_name(name) + 'List'
Michael Roth0f923be2011-07-19 14:50:39 -0500819
Eric Blakec6405b52015-05-14 06:50:55 -0600820# Map type @value to the C typedef form.
821#
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'
Eric Blaked5573442015-05-14 06:50:54 -0600827def type_name(value):
828 if type(value) == list:
829 return c_list_type(value[0])
Eric Blakec6405b52015-05-14 06:50:55 -0600830 if value in builtin_types.keys():
831 return value
832 return c_name(value)
Michael Roth0f923be2011-07-19 14:50:39 -0500833
Eric Blakefd41dd42015-05-04 09:05:25 -0600834def add_name(name, info, meta, implicit = False):
Eric Blake4dc2e692015-05-04 09:05:17 -0600835 global all_names
Eric Blakefd41dd42015-05-04 09:05:25 -0600836 check_name(info, "'%s'" % meta, name)
Eric Blake4dc2e692015-05-04 09:05:17 -0600837 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'"
844 % (meta, name))
845 all_names[name] = meta
Kevin Wolfb35284e2013-07-01 16:31:51 +0200846
Eric Blake4dc2e692015-05-04 09:05:17 -0600847def add_struct(definition, info):
Kevin Wolfb35284e2013-07-01 16:31:51 +0200848 global struct_types
Eric Blakefd41dd42015-05-04 09:05:25 -0600849 name = definition['struct']
850 add_name(name, info, 'struct')
Kevin Wolfb35284e2013-07-01 16:31:51 +0200851 struct_types.append(definition)
852
853def find_struct(name):
854 global struct_types
855 for struct in struct_types:
Eric Blakefd41dd42015-05-04 09:05:25 -0600856 if struct['struct'] == name:
Kevin Wolfb35284e2013-07-01 16:31:51 +0200857 return struct
858 return None
Michael Roth0f923be2011-07-19 14:50:39 -0500859
Eric Blake4dc2e692015-05-04 09:05:17 -0600860def add_union(definition, info):
Kevin Wolfea66c6d2013-07-16 10:49:41 +0200861 global union_types
Eric Blake4dc2e692015-05-04 09:05:17 -0600862 name = definition['union']
863 add_name(name, info, 'union')
Eric Blakeab916fa2015-05-04 09:05:13 -0600864 union_types.append(definition)
Kevin Wolfea66c6d2013-07-16 10:49:41 +0200865
866def find_union(name):
867 global union_types
868 for union in union_types:
869 if union['union'] == name:
870 return union
871 return None
872
Eric Blake4dc2e692015-05-04 09:05:17 -0600873def add_enum(name, info, enum_values = None, implicit = False):
Michael Roth0f923be2011-07-19 14:50:39 -0500874 global enum_types
Eric Blake4dc2e692015-05-04 09:05:17 -0600875 add_name(name, info, 'enum', implicit)
Wenchao Xiadad1fca2014-03-04 18:44:31 -0800876 enum_types.append({"enum_name": name, "enum_values": enum_values})
877
878def find_enum(name):
879 global enum_types
880 for enum in enum_types:
881 if enum['enum_name'] == name:
882 return enum
883 return None
Michael Roth0f923be2011-07-19 14:50:39 -0500884
885def is_enum(name):
Wenchao Xiadad1fca2014-03-04 18:44:31 -0800886 return find_enum(name) != None
Michael Roth0f923be2011-07-19 14:50:39 -0500887
Amos Kong05dfb262014-06-10 19:25:53 +0800888eatspace = '\033EATSPACE.'
Eric Blaked5573442015-05-14 06:50:54 -0600889pointer_suffix = ' *' + eatspace
Amos Kong05dfb262014-06-10 19:25:53 +0800890
Eric Blakec6405b52015-05-14 06:50:55 -0600891# Map type @name to its C type expression.
892# If @is_param, const-qualify the string type.
893#
894# This function is used for computing the full C type of 'member':'name'.
Amos Kong05dfb262014-06-10 19:25:53 +0800895# 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().
Eric Blaked5573442015-05-14 06:50:54 -0600898def c_type(value, is_param=False):
899 if value == 'str':
Amos Kong0d14eeb2014-06-10 19:25:52 +0800900 if is_param:
Eric Blaked5573442015-05-14 06:50:54 -0600901 return 'const char' + pointer_suffix
902 return 'char' + pointer_suffix
Amos Kong05dfb262014-06-10 19:25:53 +0800903
Eric Blaked5573442015-05-14 06:50:54 -0600904 elif value == 'int':
Michael Roth0f923be2011-07-19 14:50:39 -0500905 return 'int64_t'
Eric Blaked5573442015-05-14 06:50:54 -0600906 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'):
909 return value + '_t'
910 elif value == 'size':
Laszlo Ersek092705d2012-07-17 16:17:07 +0200911 return 'uint64_t'
Eric Blaked5573442015-05-14 06:50:54 -0600912 elif value == 'bool':
Michael Roth0f923be2011-07-19 14:50:39 -0500913 return 'bool'
Eric Blaked5573442015-05-14 06:50:54 -0600914 elif value == 'number':
Michael Roth0f923be2011-07-19 14:50:39 -0500915 return 'double'
Eric Blaked5573442015-05-14 06:50:54 -0600916 elif type(value) == list:
917 return c_list_type(value[0]) + pointer_suffix
918 elif is_enum(value):
Eric Blakec6405b52015-05-14 06:50:55 -0600919 return c_name(value)
Eric Blaked5573442015-05-14 06:50:54 -0600920 elif value == None:
Michael Roth0f923be2011-07-19 14:50:39 -0500921 return 'void'
Eric Blaked5573442015-05-14 06:50:54 -0600922 elif value in events:
923 return camel_case(value) + 'Event' + pointer_suffix
Michael Roth0f923be2011-07-19 14:50:39 -0500924 else:
Eric Blaked5573442015-05-14 06:50:54 -0600925 # complex type name
926 assert isinstance(value, str) and value != ""
Eric Blakec6405b52015-05-14 06:50:55 -0600927 return c_name(value) + pointer_suffix
Amos Kong05dfb262014-06-10 19:25:53 +0800928
Eric Blaked5573442015-05-14 06:50:54 -0600929def is_c_ptr(value):
930 return c_type(value).endswith(pointer_suffix)
Michael Roth0f923be2011-07-19 14:50:39 -0500931
932def genindent(count):
933 ret = ""
934 for i in range(count):
935 ret += " "
936 return ret
937
938indent_level = 0
939
940def push_indent(indent_amount=4):
941 global indent_level
942 indent_level += indent_amount
943
944def pop_indent(indent_amount=4):
945 global indent_level
946 indent_level -= indent_amount
947
948def 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'
953
954def mcgen(code, **kwds):
Amos Kong05dfb262014-06-10 19:25:53 +0800955 raw = cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
956 return re.sub(re.escape(eatspace) + ' *', '', raw)
Michael Roth0f923be2011-07-19 14:50:39 -0500957
958def basename(filename):
959 return filename.split("/")[-1]
960
961def guardname(filename):
Michael Rothd8e1f212011-11-29 16:47:48 -0600962 guard = basename(filename).rsplit(".", 1)[0]
963 for substr in [".", " ", "-"]:
964 guard = guard.replace(substr, "_")
965 return guard.upper() + '_H'
Michael Rothc0afa9c2013-05-10 17:46:00 -0500966
967def guardstart(name):
968 return mcgen('''
969
970#ifndef %(name)s
971#define %(name)s
972
973''',
974 name=guardname(name))
975
976def guardend(name):
977 return mcgen('''
978
979#endif /* %(name)s */
980
981''',
982 name=guardname(name))
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200983
984def parse_command_line(extra_options = "", extra_long_options = []):
985
986 try:
987 opts, args = getopt.gnu_getopt(sys.argv[1:],
Markus Armbruster16d80f62015-04-02 13:32:16 +0200988 "chp:o:" + extra_options,
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200989 ["source", "header", "prefix=",
Markus Armbruster16d80f62015-04-02 13:32:16 +0200990 "output-dir="] + extra_long_options)
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200991 except getopt.GetoptError, err:
Markus Armbrusterb4540962015-04-02 13:17:34 +0200992 print >>sys.stderr, "%s: %s" % (sys.argv[0], str(err))
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200993 sys.exit(1)
994
995 output_dir = ""
996 prefix = ""
997 do_c = False
998 do_h = False
999 extra_opts = []
1000
1001 for oa in opts:
1002 o, a = oa
1003 if o in ("-p", "--prefix"):
1004 prefix = a
Markus Armbruster2114f5a2015-04-02 13:12:21 +02001005 elif o in ("-o", "--output-dir"):
1006 output_dir = a + "/"
1007 elif o in ("-c", "--source"):
1008 do_c = True
1009 elif o in ("-h", "--header"):
1010 do_h = True
1011 else:
1012 extra_opts.append(oa)
1013
1014 if not do_c and not do_h:
1015 do_c = True
1016 do_h = True
1017
Markus Armbruster16d80f62015-04-02 13:32:16 +02001018 if len(args) != 1:
1019 print >>sys.stderr, "%s: need exactly one argument" % sys.argv[0]
Markus Armbrusterb4540962015-04-02 13:17:34 +02001020 sys.exit(1)
Markus Armbruster16d80f62015-04-02 13:32:16 +02001021 input_file = args[0]
Markus Armbrusterb4540962015-04-02 13:17:34 +02001022
Markus Armbruster2114f5a2015-04-02 13:12:21 +02001023 return (input_file, output_dir, do_c, do_h, prefix, extra_opts)
Markus Armbruster12f8e1b2015-04-02 14:46:39 +02001024
1025def 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
1029
1030 try:
1031 os.makedirs(output_dir)
1032 except os.error, e:
1033 if e.errno != errno.EEXIST:
1034 raise
1035
1036 def maybe_open(really, name, opt):
1037 if really:
1038 return open(name, opt)
1039 else:
1040 import StringIO
1041 return StringIO.StringIO()
1042
1043 fdef = maybe_open(do_c, c_file, 'w')
1044 fdecl = maybe_open(do_h, h_file, 'w')
1045
1046 fdef.write(mcgen('''
1047/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
1048%(comment)s
1049''',
1050 comment = c_comment))
1051
1052 fdecl.write(mcgen('''
1053/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
1054%(comment)s
1055#ifndef %(guard)s
1056#define %(guard)s
1057
1058''',
1059 comment = h_comment, guard = guardname(h_file)))
1060
1061 return (fdef, fdecl)
1062
1063def close_output(fdef, fdecl):
1064 fdecl.write('''
1065#endif
1066''')
Markus Armbruster12f8e1b2015-04-02 14:46:39 +02001067 fdecl.close()
Markus Armbruster12f8e1b2015-04-02 14:46:39 +02001068 fdef.close()