blob: 06478bb269d81758543bd9c0d08d5a9afb637d7e [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',
Markus Armbruster28770e02015-09-16 13:06:24 +020036 'any': None, # any qtype_code possible, actually
Kevin Wolf69dd62d2013-07-08 16:14:21 +020037}
38
Eric Blake10d4d992015-05-04 09:05:23 -060039# Whitelist of commands allowed to return a non-dictionary
40returns_whitelist = [
41 # From QMP:
42 'human-monitor-command',
Markus Armbruster6eb39372015-09-16 13:06:25 +020043 'qom-get',
Eric Blake10d4d992015-05-04 09:05:23 -060044 'query-migrate-cache-size',
45 'query-tpm-models',
46 'query-tpm-types',
47 'ringbuf-read',
48
49 # From QGA:
50 'guest-file-open',
51 'guest-fsfreeze-freeze',
52 'guest-fsfreeze-freeze-list',
53 'guest-fsfreeze-status',
54 'guest-fsfreeze-thaw',
55 'guest-get-time',
56 'guest-set-vcpus',
57 'guest-sync',
58 'guest-sync-delimited',
59
60 # From qapi-schema-test:
61 'user_def_cmd3',
62]
63
Eric Blake4dc2e692015-05-04 09:05:17 -060064enum_types = []
65struct_types = []
66union_types = []
67events = []
68all_names = {}
69
Markus Armbruster00e4b282015-06-10 10:04:36 +020070#
71# Parsing the schema into expressions
72#
73
Lluís Vilanovaa719a272014-05-07 20:46:15 +020074def error_path(parent):
75 res = ""
76 while parent:
77 res = ("In file included from %s:%d:\n" % (parent['file'],
78 parent['line'])) + res
79 parent = parent['parent']
80 return res
81
Markus Armbruster2caba362013-07-27 17:41:56 +020082class QAPISchemaError(Exception):
83 def __init__(self, schema, msg):
Markus Armbruster54414042015-06-09 16:22:45 +020084 self.fname = schema.fname
Markus Armbruster2caba362013-07-27 17:41:56 +020085 self.msg = msg
Wenchao Xia515b9432014-03-04 18:44:33 -080086 self.col = 1
87 self.line = schema.line
88 for ch in schema.src[schema.line_pos:schema.pos]:
89 if ch == '\t':
Markus Armbruster2caba362013-07-27 17:41:56 +020090 self.col = (self.col + 7) % 8 + 1
91 else:
92 self.col += 1
Markus Armbruster54414042015-06-09 16:22:45 +020093 self.info = schema.incl_info
Markus Armbruster2caba362013-07-27 17:41:56 +020094
95 def __str__(self):
Lluís Vilanovaa719a272014-05-07 20:46:15 +020096 return error_path(self.info) + \
Markus Armbruster54414042015-06-09 16:22:45 +020097 "%s:%d:%d: %s" % (self.fname, self.line, self.col, self.msg)
Markus Armbruster2caba362013-07-27 17:41:56 +020098
Wenchao Xiab86b05e2014-03-04 18:44:34 -080099class QAPIExprError(Exception):
100 def __init__(self, expr_info, msg):
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200101 self.info = expr_info
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800102 self.msg = msg
103
104 def __str__(self):
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200105 return error_path(self.info['parent']) + \
106 "%s:%d: %s" % (self.info['file'], self.info['line'], self.msg)
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800107
Markus Armbrustera4bcb202015-09-16 13:06:04 +0200108class QAPISchemaParser(object):
Michael Roth0f923be2011-07-19 14:50:39 -0500109
Markus Armbrustera1366082015-06-09 16:54:09 +0200110 def __init__(self, fp, previously_included = [], incl_info = None):
Markus Armbruster54414042015-06-09 16:22:45 +0200111 abs_fname = os.path.abspath(fp.name)
Markus Armbruster8608d252015-06-09 18:32:29 +0200112 fname = fp.name
Markus Armbruster54414042015-06-09 16:22:45 +0200113 self.fname = fname
Markus Armbruster54414042015-06-09 16:22:45 +0200114 previously_included.append(abs_fname)
115 self.incl_info = incl_info
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200116 self.src = fp.read()
117 if self.src == '' or self.src[-1] != '\n':
118 self.src += '\n'
119 self.cursor = 0
Wenchao Xia515b9432014-03-04 18:44:33 -0800120 self.line = 1
121 self.line_pos = 0
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200122 self.exprs = []
123 self.accept()
Michael Roth0f923be2011-07-19 14:50:39 -0500124
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200125 while self.tok != None:
Markus Armbruster54414042015-06-09 16:22:45 +0200126 expr_info = {'file': fname, 'line': self.line,
127 'parent': self.incl_info}
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200128 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)
Markus Armbruster54414042015-06-09 16:22:45 +0200137 incl_abs_fname = os.path.join(os.path.dirname(abs_fname),
138 include)
Markus Armbrustera1366082015-06-09 16:54:09 +0200139 # catch inclusion cycle
140 inf = expr_info
141 while inf:
142 if incl_abs_fname == os.path.abspath(inf['file']):
Stefan Hajnoczi7ac9a9d2014-08-27 12:08:51 +0100143 raise QAPIExprError(expr_info, "Inclusion loop for %s"
144 % include)
Markus Armbrustera1366082015-06-09 16:54:09 +0200145 inf = inf['parent']
Benoît Canet24fd8482014-05-16 12:51:56 +0200146 # skip multiple include of the same file
Markus Armbruster54414042015-06-09 16:22:45 +0200147 if incl_abs_fname in previously_included:
Benoît Canet24fd8482014-05-16 12:51:56 +0200148 continue
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200149 try:
Markus Armbruster54414042015-06-09 16:22:45 +0200150 fobj = open(incl_abs_fname, 'r')
Luiz Capitulino34788812014-05-20 13:50:19 -0400151 except IOError, e:
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200152 raise QAPIExprError(expr_info,
153 '%s: %s' % (e.strerror, include))
Markus Armbrustera4bcb202015-09-16 13:06:04 +0200154 exprs_include = QAPISchemaParser(fobj, previously_included,
155 expr_info)
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200156 self.exprs.extend(exprs_include.exprs)
157 else:
158 expr_elem = {'expr': expr,
159 'info': expr_info}
160 self.exprs.append(expr_elem)
Michael Roth0f923be2011-07-19 14:50:39 -0500161
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200162 def accept(self):
163 while True:
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200164 self.tok = self.src[self.cursor]
Markus Armbruster2caba362013-07-27 17:41:56 +0200165 self.pos = self.cursor
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200166 self.cursor += 1
167 self.val = None
Michael Roth0f923be2011-07-19 14:50:39 -0500168
Markus Armbrusterf1a145e2013-07-27 17:42:01 +0200169 if self.tok == '#':
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200170 self.cursor = self.src.find('\n', self.cursor)
171 elif self.tok in ['{', '}', ':', ',', '[', ']']:
172 return
173 elif self.tok == "'":
174 string = ''
175 esc = False
176 while True:
177 ch = self.src[self.cursor]
178 self.cursor += 1
179 if ch == '\n':
Markus Armbruster2caba362013-07-27 17:41:56 +0200180 raise QAPISchemaError(self,
181 'Missing terminating "\'"')
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200182 if esc:
Eric Blakea7f59662015-05-04 09:05:36 -0600183 if ch == 'b':
184 string += '\b'
185 elif ch == 'f':
186 string += '\f'
187 elif ch == 'n':
188 string += '\n'
189 elif ch == 'r':
190 string += '\r'
191 elif ch == 't':
192 string += '\t'
193 elif ch == 'u':
194 value = 0
195 for x in range(0, 4):
196 ch = self.src[self.cursor]
197 self.cursor += 1
198 if ch not in "0123456789abcdefABCDEF":
199 raise QAPISchemaError(self,
200 '\\u escape needs 4 '
201 'hex digits')
202 value = (value << 4) + int(ch, 16)
203 # If Python 2 and 3 didn't disagree so much on
204 # how to handle Unicode, then we could allow
205 # Unicode string defaults. But most of QAPI is
206 # ASCII-only, so we aren't losing much for now.
207 if not value or value > 0x7f:
208 raise QAPISchemaError(self,
209 'For now, \\u escape '
210 'only supports non-zero '
211 'values up to \\u007f')
212 string += chr(value)
213 elif ch in "\\/'\"":
214 string += ch
215 else:
216 raise QAPISchemaError(self,
217 "Unknown escape \\%s" %ch)
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200218 esc = False
219 elif ch == "\\":
220 esc = True
221 elif ch == "'":
222 self.val = string
223 return
224 else:
225 string += ch
Markus Armbrustere565d932015-06-10 08:24:58 +0200226 elif self.src.startswith("true", self.pos):
227 self.val = True
228 self.cursor += 3
229 return
230 elif self.src.startswith("false", self.pos):
231 self.val = False
232 self.cursor += 4
233 return
234 elif self.src.startswith("null", self.pos):
235 self.val = None
236 self.cursor += 3
237 return
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200238 elif self.tok == '\n':
239 if self.cursor == len(self.src):
240 self.tok = None
241 return
Wenchao Xia515b9432014-03-04 18:44:33 -0800242 self.line += 1
243 self.line_pos = self.cursor
Markus Armbruster9213aa52013-07-27 17:41:57 +0200244 elif not self.tok.isspace():
245 raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
Michael Roth0f923be2011-07-19 14:50:39 -0500246
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200247 def get_members(self):
248 expr = OrderedDict()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200249 if self.tok == '}':
250 self.accept()
251 return expr
252 if self.tok != "'":
253 raise QAPISchemaError(self, 'Expected string or "}"')
254 while True:
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200255 key = self.val
256 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200257 if self.tok != ':':
258 raise QAPISchemaError(self, 'Expected ":"')
259 self.accept()
Wenchao Xia4b359912014-03-04 18:44:32 -0800260 if key in expr:
261 raise QAPISchemaError(self, 'Duplicate key "%s"' % key)
Markus Armbruster5f3cd2b2013-07-27 17:41:59 +0200262 expr[key] = self.get_expr(True)
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200263 if self.tok == '}':
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200264 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200265 return expr
266 if self.tok != ',':
267 raise QAPISchemaError(self, 'Expected "," or "}"')
268 self.accept()
269 if self.tok != "'":
270 raise QAPISchemaError(self, 'Expected string')
Michael Roth0f923be2011-07-19 14:50:39 -0500271
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200272 def get_values(self):
273 expr = []
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200274 if self.tok == ']':
275 self.accept()
276 return expr
Fam Zhenge53188a2015-05-04 09:05:18 -0600277 if not self.tok in "{['tfn":
278 raise QAPISchemaError(self, 'Expected "{", "[", "]", string, '
279 'boolean or "null"')
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200280 while True:
Markus Armbruster5f3cd2b2013-07-27 17:41:59 +0200281 expr.append(self.get_expr(True))
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200282 if self.tok == ']':
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200283 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200284 return expr
285 if self.tok != ',':
286 raise QAPISchemaError(self, 'Expected "," or "]"')
287 self.accept()
Michael Roth0f923be2011-07-19 14:50:39 -0500288
Markus Armbruster5f3cd2b2013-07-27 17:41:59 +0200289 def get_expr(self, nested):
290 if self.tok != '{' and not nested:
291 raise QAPISchemaError(self, 'Expected "{"')
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200292 if self.tok == '{':
293 self.accept()
294 expr = self.get_members()
295 elif self.tok == '[':
296 self.accept()
297 expr = self.get_values()
Fam Zhenge53188a2015-05-04 09:05:18 -0600298 elif self.tok in "'tfn":
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200299 expr = self.val
300 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200301 else:
302 raise QAPISchemaError(self, 'Expected "{", "[" or string')
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200303 return expr
Kevin Wolfbd9927f2013-07-01 16:31:50 +0200304
Markus Armbruster00e4b282015-06-10 10:04:36 +0200305#
306# Semantic analysis of schema expressions
Markus Armbrusterac882192015-09-16 13:06:05 +0200307# TODO fold into QAPISchema
308# TODO catching name collisions in generated code would be nice
Markus Armbruster00e4b282015-06-10 10:04:36 +0200309#
310
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800311def find_base_fields(base):
312 base_struct_define = find_struct(base)
313 if not base_struct_define:
314 return None
315 return base_struct_define['data']
316
Eric Blake811d04f2015-05-04 09:05:10 -0600317# Return the qtype of an alternate branch, or None on error.
318def find_alternate_member_qtype(qapi_type):
Eric Blake44bd1272015-05-04 09:05:08 -0600319 if builtin_types.has_key(qapi_type):
320 return builtin_types[qapi_type]
321 elif find_struct(qapi_type):
322 return "QTYPE_QDICT"
323 elif find_enum(qapi_type):
324 return "QTYPE_QSTRING"
Eric Blake811d04f2015-05-04 09:05:10 -0600325 elif find_union(qapi_type):
326 return "QTYPE_QDICT"
Eric Blake44bd1272015-05-04 09:05:08 -0600327 return None
328
Wenchao Xiabceae762014-03-06 17:08:56 -0800329# Return the discriminator enum define if discriminator is specified as an
330# enum type, otherwise return None.
331def discriminator_find_enum_define(expr):
332 base = expr.get('base')
333 discriminator = expr.get('discriminator')
334
335 if not (discriminator and base):
336 return None
337
338 base_fields = find_base_fields(base)
339 if not base_fields:
340 return None
341
342 discriminator_type = base_fields.get(discriminator)
343 if not discriminator_type:
344 return None
345
346 return find_enum(discriminator_type)
347
Markus Armbrusterd90675f2015-07-31 11:33:52 +0200348# FIXME should enforce "other than downstream extensions [...], all
349# names should begin with a letter".
Eric Blakec9e0a792015-05-04 09:05:22 -0600350valid_name = re.compile('^[a-zA-Z_][a-zA-Z0-9_.-]*$')
351def check_name(expr_info, source, name, allow_optional = False,
352 enum_member = False):
353 global valid_name
354 membername = name
355
356 if not isinstance(name, str):
357 raise QAPIExprError(expr_info,
358 "%s requires a string name" % source)
359 if name.startswith('*'):
360 membername = name[1:]
361 if not allow_optional:
362 raise QAPIExprError(expr_info,
363 "%s does not allow optional name '%s'"
364 % (source, name))
365 # Enum members can start with a digit, because the generated C
366 # code always prefixes it with the enum name
367 if enum_member:
368 membername = '_' + membername
369 if not valid_name.match(membername):
370 raise QAPIExprError(expr_info,
371 "%s uses invalid name '%s'" % (source, name))
372
Markus Armbruster00e4b282015-06-10 10:04:36 +0200373def add_name(name, info, meta, implicit = False):
374 global all_names
375 check_name(info, "'%s'" % meta, name)
Markus Armbrusterd90675f2015-07-31 11:33:52 +0200376 # FIXME should reject names that differ only in '_' vs. '.'
377 # vs. '-', because they're liable to clash in generated C.
Markus Armbruster00e4b282015-06-10 10:04:36 +0200378 if name in all_names:
379 raise QAPIExprError(info,
380 "%s '%s' is already defined"
381 % (all_names[name], name))
382 if not implicit and name[-4:] == 'Kind':
383 raise QAPIExprError(info,
384 "%s '%s' should not end in 'Kind'"
385 % (meta, name))
386 all_names[name] = meta
387
388def add_struct(definition, info):
389 global struct_types
390 name = definition['struct']
391 add_name(name, info, 'struct')
392 struct_types.append(definition)
393
394def find_struct(name):
395 global struct_types
396 for struct in struct_types:
397 if struct['struct'] == name:
398 return struct
399 return None
400
401def add_union(definition, info):
402 global union_types
403 name = definition['union']
404 add_name(name, info, 'union')
405 union_types.append(definition)
406
407def find_union(name):
408 global union_types
409 for union in union_types:
410 if union['union'] == name:
411 return union
412 return None
413
414def add_enum(name, info, enum_values = None, implicit = False):
415 global enum_types
416 add_name(name, info, 'enum', implicit)
417 enum_types.append({"enum_name": name, "enum_values": enum_values})
418
419def find_enum(name):
420 global enum_types
421 for enum in enum_types:
422 if enum['enum_name'] == name:
423 return enum
424 return None
425
426def is_enum(name):
427 return find_enum(name) != None
428
Eric Blakedd883c62015-05-04 09:05:21 -0600429def check_type(expr_info, source, value, allow_array = False,
Eric Blake2cbf0992015-05-04 09:05:24 -0600430 allow_dict = False, allow_optional = False,
Markus Armbruster2d212912015-09-16 13:06:27 +0200431 allow_metas = []):
Eric Blakedd883c62015-05-04 09:05:21 -0600432 global all_names
Eric Blakedd883c62015-05-04 09:05:21 -0600433
434 if value is None:
435 return
436
Eric Blakedd883c62015-05-04 09:05:21 -0600437 # Check if array type for value is okay
438 if isinstance(value, list):
439 if not allow_array:
440 raise QAPIExprError(expr_info,
441 "%s cannot be an array" % source)
442 if len(value) != 1 or not isinstance(value[0], str):
443 raise QAPIExprError(expr_info,
444 "%s: array type must contain single type name"
445 % source)
446 value = value[0]
Eric Blakedd883c62015-05-04 09:05:21 -0600447
448 # Check if type name for value is okay
449 if isinstance(value, str):
450 if not value in all_names:
451 raise QAPIExprError(expr_info,
452 "%s uses unknown type '%s'"
Markus Armbrustereddf8172015-08-31 13:54:39 +0200453 % (source, value))
Eric Blakedd883c62015-05-04 09:05:21 -0600454 if not all_names[value] in allow_metas:
455 raise QAPIExprError(expr_info,
456 "%s cannot use %s type '%s'"
Markus Armbrustereddf8172015-08-31 13:54:39 +0200457 % (source, all_names[value], value))
Eric Blakedd883c62015-05-04 09:05:21 -0600458 return
459
Eric Blakedd883c62015-05-04 09:05:21 -0600460 if not allow_dict:
461 raise QAPIExprError(expr_info,
462 "%s should be a type name" % source)
Markus Armbrusterc6b71e52015-08-31 17:28:52 +0200463
464 if not isinstance(value, OrderedDict):
465 raise QAPIExprError(expr_info,
466 "%s should be a dictionary or type name" % source)
467
468 # value is a dictionary, check that each member is okay
Eric Blakedd883c62015-05-04 09:05:21 -0600469 for (key, arg) in value.items():
Eric Blakec9e0a792015-05-04 09:05:22 -0600470 check_name(expr_info, "Member of %s" % source, key,
471 allow_optional=allow_optional)
Eric Blake6b5abc72015-05-04 09:05:33 -0600472 # Todo: allow dictionaries to represent default values of
473 # an optional argument.
Eric Blakedd883c62015-05-04 09:05:21 -0600474 check_type(expr_info, "Member '%s' of %s" % (key, source), arg,
Markus Armbruster2d212912015-09-16 13:06:27 +0200475 allow_array=True,
Eric Blakedd883c62015-05-04 09:05:21 -0600476 allow_metas=['built-in', 'union', 'alternate', 'struct',
Eric Blake6b5abc72015-05-04 09:05:33 -0600477 'enum'])
Eric Blakedd883c62015-05-04 09:05:21 -0600478
Eric Blakeff55d722015-05-04 09:05:37 -0600479def check_member_clash(expr_info, base_name, data, source = ""):
480 base = find_struct(base_name)
481 assert base
482 base_members = base['data']
483 for key in data.keys():
484 if key.startswith('*'):
485 key = key[1:]
486 if key in base_members or "*" + key in base_members:
487 raise QAPIExprError(expr_info,
488 "Member name '%s'%s clashes with base '%s'"
489 % (key, source, base_name))
490 if base.get('base'):
491 check_member_clash(expr_info, base['base'], data, source)
492
Eric Blakedd883c62015-05-04 09:05:21 -0600493def check_command(expr, expr_info):
494 name = expr['command']
Eric Blake2cbf0992015-05-04 09:05:24 -0600495
Eric Blakedd883c62015-05-04 09:05:21 -0600496 check_type(expr_info, "'data' for command '%s'" % name,
Eric Blakec9e0a792015-05-04 09:05:22 -0600497 expr.get('data'), allow_dict=True, allow_optional=True,
Markus Armbruster2d212912015-09-16 13:06:27 +0200498 allow_metas=['struct'])
Eric Blake10d4d992015-05-04 09:05:23 -0600499 returns_meta = ['union', 'struct']
500 if name in returns_whitelist:
501 returns_meta += ['built-in', 'alternate', 'enum']
Eric Blakedd883c62015-05-04 09:05:21 -0600502 check_type(expr_info, "'returns' for command '%s'" % name,
Markus Armbruster9b090d42015-07-31 17:59:38 +0200503 expr.get('returns'), allow_array=True,
Markus Armbruster2d212912015-09-16 13:06:27 +0200504 allow_optional=True, allow_metas=returns_meta)
Eric Blakedd883c62015-05-04 09:05:21 -0600505
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200506def check_event(expr, expr_info):
Eric Blake4dc2e692015-05-04 09:05:17 -0600507 global events
508 name = expr['event']
Eric Blake4dc2e692015-05-04 09:05:17 -0600509
510 if name.upper() == 'MAX':
511 raise QAPIExprError(expr_info, "Event name 'MAX' cannot be created")
512 events.append(name)
Eric Blakedd883c62015-05-04 09:05:21 -0600513 check_type(expr_info, "'data' for event '%s'" % name,
Eric Blakec9e0a792015-05-04 09:05:22 -0600514 expr.get('data'), allow_dict=True, allow_optional=True,
Markus Armbruster315932b2015-07-01 10:12:24 +0200515 allow_metas=['struct'])
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200516
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800517def check_union(expr, expr_info):
518 name = expr['union']
519 base = expr.get('base')
520 discriminator = expr.get('discriminator')
521 members = expr['data']
Eric Blake44bd1272015-05-04 09:05:08 -0600522 values = { 'MAX': '(automatic)' }
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800523
Eric Blake811d04f2015-05-04 09:05:10 -0600524 # Two types of unions, determined by discriminator.
Eric Blake811d04f2015-05-04 09:05:10 -0600525
526 # With no discriminator it is a simple union.
527 if discriminator is None:
Eric Blake44bd1272015-05-04 09:05:08 -0600528 enum_define = None
Eric Blakedd883c62015-05-04 09:05:21 -0600529 allow_metas=['built-in', 'union', 'alternate', 'struct', 'enum']
Eric Blake44bd1272015-05-04 09:05:08 -0600530 if base is not None:
531 raise QAPIExprError(expr_info,
Eric Blake811d04f2015-05-04 09:05:10 -0600532 "Simple union '%s' must not have a base"
Eric Blake44bd1272015-05-04 09:05:08 -0600533 % name)
534
535 # Else, it's a flat union.
536 else:
537 # The object must have a string member 'base'.
538 if not isinstance(base, str):
539 raise QAPIExprError(expr_info,
540 "Flat union '%s' must have a string base field"
541 % name)
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800542 base_fields = find_base_fields(base)
543 if not base_fields:
544 raise QAPIExprError(expr_info,
Eric Blakefd41dd42015-05-04 09:05:25 -0600545 "Base '%s' is not a valid struct"
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800546 % base)
547
Eric Blakec9e0a792015-05-04 09:05:22 -0600548 # The value of member 'discriminator' must name a non-optional
Eric Blakefd41dd42015-05-04 09:05:25 -0600549 # member of the base struct.
Eric Blakec9e0a792015-05-04 09:05:22 -0600550 check_name(expr_info, "Discriminator of flat union '%s'" % name,
551 discriminator)
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800552 discriminator_type = base_fields.get(discriminator)
553 if not discriminator_type:
554 raise QAPIExprError(expr_info,
555 "Discriminator '%s' is not a member of base "
Eric Blakefd41dd42015-05-04 09:05:25 -0600556 "struct '%s'"
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800557 % (discriminator, base))
558 enum_define = find_enum(discriminator_type)
Eric Blakedd883c62015-05-04 09:05:21 -0600559 allow_metas=['struct']
Wenchao Xia52230702014-03-04 18:44:39 -0800560 # Do not allow string discriminator
561 if not enum_define:
562 raise QAPIExprError(expr_info,
563 "Discriminator '%s' must be of enumeration "
564 "type" % discriminator)
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800565
566 # Check every branch
567 for (key, value) in members.items():
Eric Blakec9e0a792015-05-04 09:05:22 -0600568 check_name(expr_info, "Member of union '%s'" % name, key)
569
Eric Blakedd883c62015-05-04 09:05:21 -0600570 # Each value must name a known type; furthermore, in flat unions,
Eric Blakeff55d722015-05-04 09:05:37 -0600571 # branches must be a struct with no overlapping member names
Eric Blakedd883c62015-05-04 09:05:21 -0600572 check_type(expr_info, "Member '%s' of union '%s'" % (key, name),
Markus Armbrusterf9a14272015-06-10 13:07:43 +0200573 value, allow_array=not base, allow_metas=allow_metas)
Eric Blakeff55d722015-05-04 09:05:37 -0600574 if base:
575 branch_struct = find_struct(value)
576 assert branch_struct
577 check_member_clash(expr_info, base, branch_struct['data'],
578 " of branch '%s'" % key)
Eric Blakedd883c62015-05-04 09:05:21 -0600579
Eric Blake44bd1272015-05-04 09:05:08 -0600580 # If the discriminator names an enum type, then all members
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800581 # of 'data' must also be members of the enum type.
Eric Blake44bd1272015-05-04 09:05:08 -0600582 if enum_define:
583 if not key in enum_define['enum_values']:
584 raise QAPIExprError(expr_info,
585 "Discriminator value '%s' is not found in "
586 "enum '%s'" %
587 (key, enum_define["enum_name"]))
588
589 # Otherwise, check for conflicts in the generated enum
590 else:
Markus Armbrusterfa6068a2015-05-14 06:50:49 -0600591 c_key = camel_to_upper(key)
Eric Blake44bd1272015-05-04 09:05:08 -0600592 if c_key in values:
593 raise QAPIExprError(expr_info,
594 "Union '%s' member '%s' clashes with '%s'"
595 % (name, key, values[c_key]))
596 values[c_key] = key
597
Eric Blake811d04f2015-05-04 09:05:10 -0600598def check_alternate(expr, expr_info):
Eric Blakeab916fa2015-05-04 09:05:13 -0600599 name = expr['alternate']
Eric Blake811d04f2015-05-04 09:05:10 -0600600 members = expr['data']
601 values = { 'MAX': '(automatic)' }
602 types_seen = {}
Eric Blake44bd1272015-05-04 09:05:08 -0600603
Eric Blake811d04f2015-05-04 09:05:10 -0600604 # Check every branch
605 for (key, value) in members.items():
Eric Blakec9e0a792015-05-04 09:05:22 -0600606 check_name(expr_info, "Member of alternate '%s'" % name, key)
607
Eric Blake811d04f2015-05-04 09:05:10 -0600608 # Check for conflicts in the generated enum
Markus Armbrusterfa6068a2015-05-14 06:50:49 -0600609 c_key = camel_to_upper(key)
Eric Blake811d04f2015-05-04 09:05:10 -0600610 if c_key in values:
611 raise QAPIExprError(expr_info,
Eric Blakeab916fa2015-05-04 09:05:13 -0600612 "Alternate '%s' member '%s' clashes with '%s'"
613 % (name, key, values[c_key]))
Eric Blake811d04f2015-05-04 09:05:10 -0600614 values[c_key] = key
615
616 # Ensure alternates have no type conflicts.
Eric Blakedd883c62015-05-04 09:05:21 -0600617 check_type(expr_info, "Member '%s' of alternate '%s'" % (key, name),
618 value,
619 allow_metas=['built-in', 'union', 'struct', 'enum'])
Eric Blake811d04f2015-05-04 09:05:10 -0600620 qtype = find_alternate_member_qtype(value)
Eric Blakedd883c62015-05-04 09:05:21 -0600621 assert qtype
Eric Blake811d04f2015-05-04 09:05:10 -0600622 if qtype in types_seen:
623 raise QAPIExprError(expr_info,
Eric Blakeab916fa2015-05-04 09:05:13 -0600624 "Alternate '%s' member '%s' can't "
Eric Blake811d04f2015-05-04 09:05:10 -0600625 "be distinguished from member '%s'"
626 % (name, key, types_seen[qtype]))
627 types_seen[qtype] = key
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800628
Eric Blakecf393592015-05-04 09:05:04 -0600629def check_enum(expr, expr_info):
630 name = expr['enum']
631 members = expr.get('data')
Daniel P. Berrange351d36e2015-08-26 14:21:20 +0100632 prefix = expr.get('prefix')
Eric Blakecf393592015-05-04 09:05:04 -0600633 values = { 'MAX': '(automatic)' }
634
635 if not isinstance(members, list):
636 raise QAPIExprError(expr_info,
637 "Enum '%s' requires an array for 'data'" % name)
Daniel P. Berrange351d36e2015-08-26 14:21:20 +0100638 if prefix is not None and not isinstance(prefix, str):
639 raise QAPIExprError(expr_info,
640 "Enum '%s' requires a string for 'prefix'" % name)
Eric Blakecf393592015-05-04 09:05:04 -0600641 for member in members:
Eric Blakec9e0a792015-05-04 09:05:22 -0600642 check_name(expr_info, "Member of enum '%s'" %name, member,
643 enum_member=True)
Markus Armbrusterfa6068a2015-05-14 06:50:49 -0600644 key = camel_to_upper(member)
Eric Blakecf393592015-05-04 09:05:04 -0600645 if key in values:
646 raise QAPIExprError(expr_info,
647 "Enum '%s' member '%s' clashes with '%s'"
648 % (name, member, values[key]))
649 values[key] = member
650
Eric Blakedd883c62015-05-04 09:05:21 -0600651def check_struct(expr, expr_info):
Eric Blakefd41dd42015-05-04 09:05:25 -0600652 name = expr['struct']
Eric Blakedd883c62015-05-04 09:05:21 -0600653 members = expr['data']
654
Eric Blakefd41dd42015-05-04 09:05:25 -0600655 check_type(expr_info, "'data' for struct '%s'" % name, members,
Eric Blakec9e0a792015-05-04 09:05:22 -0600656 allow_dict=True, allow_optional=True)
Eric Blakefd41dd42015-05-04 09:05:25 -0600657 check_type(expr_info, "'base' for struct '%s'" % name, expr.get('base'),
Eric Blakedd883c62015-05-04 09:05:21 -0600658 allow_metas=['struct'])
Eric Blakeff55d722015-05-04 09:05:37 -0600659 if expr.get('base'):
660 check_member_clash(expr_info, expr['base'], expr['data'])
Eric Blakedd883c62015-05-04 09:05:21 -0600661
Eric Blake0545f6b2015-05-04 09:05:15 -0600662def check_keys(expr_elem, meta, required, optional=[]):
663 expr = expr_elem['expr']
664 info = expr_elem['info']
665 name = expr[meta]
666 if not isinstance(name, str):
667 raise QAPIExprError(info,
668 "'%s' key must have a string value" % meta)
669 required = required + [ meta ]
670 for (key, value) in expr.items():
671 if not key in required and not key in optional:
672 raise QAPIExprError(info,
673 "Unknown key '%s' in %s '%s'"
674 % (key, meta, name))
Eric Blake2cbf0992015-05-04 09:05:24 -0600675 if (key == 'gen' or key == 'success-response') and value != False:
676 raise QAPIExprError(info,
677 "'%s' of %s '%s' should only use false value"
678 % (key, meta, name))
Eric Blake0545f6b2015-05-04 09:05:15 -0600679 for key in required:
680 if not expr.has_key(key):
681 raise QAPIExprError(info,
682 "Key '%s' is missing from %s '%s'"
683 % (key, meta, name))
684
Markus Armbruster4d076d62015-06-10 08:55:21 +0200685def check_exprs(exprs):
686 global all_names
687
688 # Learn the types and check for valid expression keys
689 for builtin in builtin_types.keys():
690 all_names[builtin] = 'built-in'
691 for expr_elem in exprs:
692 expr = expr_elem['expr']
693 info = expr_elem['info']
694 if expr.has_key('enum'):
Daniel P. Berrange351d36e2015-08-26 14:21:20 +0100695 check_keys(expr_elem, 'enum', ['data'], ['prefix'])
Markus Armbruster4d076d62015-06-10 08:55:21 +0200696 add_enum(expr['enum'], info, expr['data'])
697 elif expr.has_key('union'):
698 check_keys(expr_elem, 'union', ['data'],
699 ['base', 'discriminator'])
700 add_union(expr, info)
701 elif expr.has_key('alternate'):
702 check_keys(expr_elem, 'alternate', ['data'])
703 add_name(expr['alternate'], info, 'alternate')
704 elif expr.has_key('struct'):
705 check_keys(expr_elem, 'struct', ['data'], ['base'])
706 add_struct(expr, info)
707 elif expr.has_key('command'):
708 check_keys(expr_elem, 'command', [],
709 ['data', 'returns', 'gen', 'success-response'])
710 add_name(expr['command'], info, 'command')
711 elif expr.has_key('event'):
712 check_keys(expr_elem, 'event', [], ['data'])
713 add_name(expr['event'], info, 'event')
714 else:
715 raise QAPIExprError(expr_elem['info'],
716 "Expression is missing metatype")
717
718 # Try again for hidden UnionKind enum
719 for expr_elem in exprs:
720 expr = expr_elem['expr']
721 if expr.has_key('union'):
722 if not discriminator_find_enum_define(expr):
723 add_enum('%sKind' % expr['union'], expr_elem['info'],
724 implicit=True)
725 elif expr.has_key('alternate'):
726 add_enum('%sKind' % expr['alternate'], expr_elem['info'],
727 implicit=True)
728
729 # Validate that exprs make sense
730 for expr_elem in exprs:
731 expr = expr_elem['expr']
732 info = expr_elem['info']
733
734 if expr.has_key('enum'):
735 check_enum(expr, info)
736 elif expr.has_key('union'):
737 check_union(expr, info)
738 elif expr.has_key('alternate'):
739 check_alternate(expr, info)
740 elif expr.has_key('struct'):
741 check_struct(expr, info)
742 elif expr.has_key('command'):
743 check_command(expr, info)
744 elif expr.has_key('event'):
745 check_event(expr, info)
746 else:
747 assert False, 'unexpected meta type'
748
Markus Armbrusterac882192015-09-16 13:06:05 +0200749 return exprs
Eric Blake0545f6b2015-05-04 09:05:15 -0600750
Markus Armbrusterac882192015-09-16 13:06:05 +0200751
752#
753# Schema compiler frontend
754#
755
756class QAPISchemaEntity(object):
757 def __init__(self, name, info):
758 assert isinstance(name, str)
759 self.name = name
760 self.info = info
761
Markus Armbrusterf51d8c32015-09-16 13:06:06 +0200762 def c_name(self):
763 return c_name(self.name)
764
Markus Armbrusterac882192015-09-16 13:06:05 +0200765 def check(self, schema):
766 pass
767
Markus Armbruster3f7dc212015-09-16 13:06:07 +0200768 def visit(self, visitor):
769 pass
770
771
772class QAPISchemaVisitor(object):
773 def visit_begin(self, schema):
774 pass
775
776 def visit_end(self):
777 pass
778
779 def visit_builtin_type(self, name, info, json_type):
780 pass
781
782 def visit_enum_type(self, name, info, values, prefix):
783 pass
784
785 def visit_array_type(self, name, info, element_type):
786 pass
787
788 def visit_object_type(self, name, info, base, members, variants):
789 pass
790
Markus Armbruster39a18152015-09-16 13:06:28 +0200791 def visit_object_type_flat(self, name, info, members, variants):
792 pass
793
Markus Armbruster3f7dc212015-09-16 13:06:07 +0200794 def visit_alternate_type(self, name, info, variants):
795 pass
796
797 def visit_command(self, name, info, arg_type, ret_type,
798 gen, success_response):
799 pass
800
801 def visit_event(self, name, info, arg_type):
802 pass
803
Markus Armbrusterac882192015-09-16 13:06:05 +0200804
805class QAPISchemaType(QAPISchemaEntity):
Markus Armbrusterf51d8c32015-09-16 13:06:06 +0200806 def c_type(self, is_param=False):
807 return c_name(self.name) + pointer_suffix
808
809 def c_null(self):
810 return 'NULL'
811
812 def json_type(self):
813 pass
814
815 def alternate_qtype(self):
816 json2qtype = {
817 'string': 'QTYPE_QSTRING',
818 'number': 'QTYPE_QFLOAT',
819 'int': 'QTYPE_QINT',
820 'boolean': 'QTYPE_QBOOL',
821 'object': 'QTYPE_QDICT'
822 }
823 return json2qtype.get(self.json_type())
Markus Armbrusterac882192015-09-16 13:06:05 +0200824
825
826class QAPISchemaBuiltinType(QAPISchemaType):
Markus Armbrusterf51d8c32015-09-16 13:06:06 +0200827 def __init__(self, name, json_type, c_type, c_null):
Markus Armbrusterac882192015-09-16 13:06:05 +0200828 QAPISchemaType.__init__(self, name, None)
Markus Armbrusterf51d8c32015-09-16 13:06:06 +0200829 assert not c_type or isinstance(c_type, str)
830 assert json_type in ('string', 'number', 'int', 'boolean', 'null',
831 'value')
832 self._json_type_name = json_type
833 self._c_type_name = c_type
834 self._c_null_val = c_null
835
836 def c_name(self):
837 return self.name
838
839 def c_type(self, is_param=False):
840 if is_param and self.name == 'str':
841 return 'const ' + self._c_type_name
842 return self._c_type_name
843
844 def c_null(self):
845 return self._c_null_val
846
847 def json_type(self):
848 return self._json_type_name
Markus Armbrusterac882192015-09-16 13:06:05 +0200849
Markus Armbruster3f7dc212015-09-16 13:06:07 +0200850 def visit(self, visitor):
851 visitor.visit_builtin_type(self.name, self.info, self.json_type())
852
Markus Armbrusterac882192015-09-16 13:06:05 +0200853
854class QAPISchemaEnumType(QAPISchemaType):
855 def __init__(self, name, info, values, prefix):
856 QAPISchemaType.__init__(self, name, info)
857 for v in values:
858 assert isinstance(v, str)
859 assert prefix is None or isinstance(prefix, str)
860 self.values = values
861 self.prefix = prefix
862
863 def check(self, schema):
864 assert len(set(self.values)) == len(self.values)
865
Markus Armbrusterf51d8c32015-09-16 13:06:06 +0200866 def c_type(self, is_param=False):
867 return c_name(self.name)
868
869 def c_null(self):
870 return c_enum_const(self.name, (self.values + ['MAX'])[0],
871 self.prefix)
872
873 def json_type(self):
874 return 'string'
875
Markus Armbruster3f7dc212015-09-16 13:06:07 +0200876 def visit(self, visitor):
877 visitor.visit_enum_type(self.name, self.info,
878 self.values, self.prefix)
879
Markus Armbrusterac882192015-09-16 13:06:05 +0200880
881class QAPISchemaArrayType(QAPISchemaType):
882 def __init__(self, name, info, element_type):
883 QAPISchemaType.__init__(self, name, info)
884 assert isinstance(element_type, str)
885 self._element_type_name = element_type
886 self.element_type = None
887
888 def check(self, schema):
889 self.element_type = schema.lookup_type(self._element_type_name)
890 assert self.element_type
891
Markus Armbrusterf51d8c32015-09-16 13:06:06 +0200892 def json_type(self):
893 return 'array'
894
Markus Armbruster3f7dc212015-09-16 13:06:07 +0200895 def visit(self, visitor):
896 visitor.visit_array_type(self.name, self.info, self.element_type)
897
Markus Armbrusterac882192015-09-16 13:06:05 +0200898
899class QAPISchemaObjectType(QAPISchemaType):
900 def __init__(self, name, info, base, local_members, variants):
901 QAPISchemaType.__init__(self, name, info)
902 assert base is None or isinstance(base, str)
903 for m in local_members:
904 assert isinstance(m, QAPISchemaObjectTypeMember)
905 assert (variants is None or
906 isinstance(variants, QAPISchemaObjectTypeVariants))
907 self._base_name = base
908 self.base = None
909 self.local_members = local_members
910 self.variants = variants
911 self.members = None
912
913 def check(self, schema):
914 assert self.members is not False # not running in cycles
915 if self.members:
916 return
917 self.members = False # mark as being checked
918 if self._base_name:
919 self.base = schema.lookup_type(self._base_name)
920 assert isinstance(self.base, QAPISchemaObjectType)
921 assert not self.base.variants # not implemented
922 self.base.check(schema)
923 members = list(self.base.members)
924 else:
925 members = []
926 seen = {}
927 for m in members:
928 seen[m.name] = m
929 for m in self.local_members:
930 m.check(schema, members, seen)
931 if self.variants:
932 self.variants.check(schema, members, seen)
933 self.members = members
934
Markus Armbrusterf51d8c32015-09-16 13:06:06 +0200935 def c_name(self):
936 assert self.info
937 return QAPISchemaType.c_name(self)
938
939 def c_type(self, is_param=False):
940 assert self.info
941 return QAPISchemaType.c_type(self)
942
943 def json_type(self):
944 return 'object'
945
Markus Armbruster3f7dc212015-09-16 13:06:07 +0200946 def visit(self, visitor):
947 visitor.visit_object_type(self.name, self.info,
948 self.base, self.local_members, self.variants)
Markus Armbruster39a18152015-09-16 13:06:28 +0200949 visitor.visit_object_type_flat(self.name, self.info,
950 self.members, self.variants)
Markus Armbruster3f7dc212015-09-16 13:06:07 +0200951
Markus Armbrusterac882192015-09-16 13:06:05 +0200952
953class QAPISchemaObjectTypeMember(object):
954 def __init__(self, name, typ, optional):
955 assert isinstance(name, str)
956 assert isinstance(typ, str)
957 assert isinstance(optional, bool)
958 self.name = name
959 self._type_name = typ
960 self.type = None
961 self.optional = optional
962
963 def check(self, schema, all_members, seen):
964 assert self.name not in seen
965 self.type = schema.lookup_type(self._type_name)
966 assert self.type
967 all_members.append(self)
968 seen[self.name] = self
969
970
971class QAPISchemaObjectTypeVariants(object):
972 def __init__(self, tag_name, tag_enum, variants):
973 assert tag_name is None or isinstance(tag_name, str)
974 assert tag_enum is None or isinstance(tag_enum, str)
975 for v in variants:
976 assert isinstance(v, QAPISchemaObjectTypeVariant)
977 self.tag_name = tag_name
978 if tag_name:
979 assert not tag_enum
980 self.tag_member = None
981 else:
982 self.tag_member = QAPISchemaObjectTypeMember('type', tag_enum,
983 False)
984 self.variants = variants
985
986 def check(self, schema, members, seen):
987 if self.tag_name:
988 self.tag_member = seen[self.tag_name]
989 else:
990 self.tag_member.check(schema, members, seen)
991 assert isinstance(self.tag_member.type, QAPISchemaEnumType)
992 for v in self.variants:
993 vseen = dict(seen)
994 v.check(schema, self.tag_member.type, vseen)
995
Markus Armbrusterac882192015-09-16 13:06:05 +0200996class QAPISchemaObjectTypeVariant(QAPISchemaObjectTypeMember):
997 def __init__(self, name, typ):
998 QAPISchemaObjectTypeMember.__init__(self, name, typ, False)
999
1000 def check(self, schema, tag_type, seen):
1001 QAPISchemaObjectTypeMember.check(self, schema, [], seen)
1002 assert self.name in tag_type.values
1003
Markus Armbruster2b162cc2015-09-16 13:06:09 +02001004 # This function exists to support ugly simple union special cases
1005 # TODO get rid of them, and drop the function
1006 def simple_union_type(self):
1007 if isinstance(self.type, QAPISchemaObjectType) and not self.type.info:
1008 assert len(self.type.members) == 1
1009 assert not self.type.variants
1010 return self.type.members[0].type
1011 return None
1012
Markus Armbrusterac882192015-09-16 13:06:05 +02001013
1014class QAPISchemaAlternateType(QAPISchemaType):
1015 def __init__(self, name, info, variants):
1016 QAPISchemaType.__init__(self, name, info)
1017 assert isinstance(variants, QAPISchemaObjectTypeVariants)
1018 assert not variants.tag_name
1019 self.variants = variants
1020
1021 def check(self, schema):
1022 self.variants.check(schema, [], {})
1023
Markus Armbrusterf51d8c32015-09-16 13:06:06 +02001024 def json_type(self):
1025 return 'value'
1026
Markus Armbruster3f7dc212015-09-16 13:06:07 +02001027 def visit(self, visitor):
1028 visitor.visit_alternate_type(self.name, self.info, self.variants)
1029
Markus Armbrusterac882192015-09-16 13:06:05 +02001030
1031class QAPISchemaCommand(QAPISchemaEntity):
1032 def __init__(self, name, info, arg_type, ret_type, gen, success_response):
1033 QAPISchemaEntity.__init__(self, name, info)
1034 assert not arg_type or isinstance(arg_type, str)
1035 assert not ret_type or isinstance(ret_type, str)
1036 self._arg_type_name = arg_type
1037 self.arg_type = None
1038 self._ret_type_name = ret_type
1039 self.ret_type = None
1040 self.gen = gen
1041 self.success_response = success_response
1042
1043 def check(self, schema):
1044 if self._arg_type_name:
1045 self.arg_type = schema.lookup_type(self._arg_type_name)
1046 assert isinstance(self.arg_type, QAPISchemaObjectType)
1047 assert not self.arg_type.variants # not implemented
1048 if self._ret_type_name:
1049 self.ret_type = schema.lookup_type(self._ret_type_name)
1050 assert isinstance(self.ret_type, QAPISchemaType)
1051
Markus Armbruster3f7dc212015-09-16 13:06:07 +02001052 def visit(self, visitor):
1053 visitor.visit_command(self.name, self.info,
1054 self.arg_type, self.ret_type,
1055 self.gen, self.success_response)
1056
Markus Armbrusterac882192015-09-16 13:06:05 +02001057
1058class QAPISchemaEvent(QAPISchemaEntity):
1059 def __init__(self, name, info, arg_type):
1060 QAPISchemaEntity.__init__(self, name, info)
1061 assert not arg_type or isinstance(arg_type, str)
1062 self._arg_type_name = arg_type
1063 self.arg_type = None
1064
1065 def check(self, schema):
1066 if self._arg_type_name:
1067 self.arg_type = schema.lookup_type(self._arg_type_name)
1068 assert isinstance(self.arg_type, QAPISchemaObjectType)
1069 assert not self.arg_type.variants # not implemented
1070
Markus Armbruster3f7dc212015-09-16 13:06:07 +02001071 def visit(self, visitor):
1072 visitor.visit_event(self.name, self.info, self.arg_type)
1073
Markus Armbrusterac882192015-09-16 13:06:05 +02001074
1075class QAPISchema(object):
1076 def __init__(self, fname):
1077 try:
1078 self.exprs = check_exprs(QAPISchemaParser(open(fname, "r")).exprs)
1079 except (QAPISchemaError, QAPIExprError), err:
1080 print >>sys.stderr, err
1081 exit(1)
1082 self._entity_dict = {}
1083 self._def_predefineds()
1084 self._def_exprs()
1085 self.check()
1086
Markus Armbrusterac882192015-09-16 13:06:05 +02001087 def _def_entity(self, ent):
1088 assert ent.name not in self._entity_dict
1089 self._entity_dict[ent.name] = ent
1090
1091 def lookup_entity(self, name, typ=None):
1092 ent = self._entity_dict.get(name)
1093 if typ and not isinstance(ent, typ):
1094 return None
1095 return ent
1096
1097 def lookup_type(self, name):
1098 return self.lookup_entity(name, QAPISchemaType)
1099
Markus Armbrusterf51d8c32015-09-16 13:06:06 +02001100 def _def_builtin_type(self, name, json_type, c_type, c_null):
1101 self._def_entity(QAPISchemaBuiltinType(name, json_type,
1102 c_type, c_null))
Markus Armbruster28770e02015-09-16 13:06:24 +02001103 self._make_array_type(name) # TODO really needed?
Markus Armbrusterac882192015-09-16 13:06:05 +02001104
1105 def _def_predefineds(self):
Markus Armbrusterf51d8c32015-09-16 13:06:06 +02001106 for t in [('str', 'string', 'char' + pointer_suffix, 'NULL'),
1107 ('number', 'number', 'double', '0'),
1108 ('int', 'int', 'int64_t', '0'),
1109 ('int8', 'int', 'int8_t', '0'),
1110 ('int16', 'int', 'int16_t', '0'),
1111 ('int32', 'int', 'int32_t', '0'),
1112 ('int64', 'int', 'int64_t', '0'),
1113 ('uint8', 'int', 'uint8_t', '0'),
1114 ('uint16', 'int', 'uint16_t', '0'),
1115 ('uint32', 'int', 'uint32_t', '0'),
1116 ('uint64', 'int', 'uint64_t', '0'),
1117 ('size', 'int', 'uint64_t', '0'),
1118 ('bool', 'boolean', 'bool', 'false'),
Markus Armbruster28770e02015-09-16 13:06:24 +02001119 ('any', 'value', 'QObject' + pointer_suffix, 'NULL')]:
Markus Armbrusterf51d8c32015-09-16 13:06:06 +02001120 self._def_builtin_type(*t)
Markus Armbruster39a18152015-09-16 13:06:28 +02001121 self.the_empty_object_type = QAPISchemaObjectType(':empty', None, None,
1122 [], None)
1123 self._def_entity(self.the_empty_object_type)
Markus Armbrusterac882192015-09-16 13:06:05 +02001124
1125 def _make_implicit_enum_type(self, name, values):
1126 name = name + 'Kind'
1127 self._def_entity(QAPISchemaEnumType(name, None, values, None))
1128 return name
1129
1130 def _make_array_type(self, element_type):
1131 name = element_type + 'List'
1132 if not self.lookup_type(name):
1133 self._def_entity(QAPISchemaArrayType(name, None, element_type))
1134 return name
1135
1136 def _make_implicit_object_type(self, name, role, members):
1137 if not members:
1138 return None
1139 name = ':obj-%s-%s' % (name, role)
1140 if not self.lookup_entity(name, QAPISchemaObjectType):
1141 self._def_entity(QAPISchemaObjectType(name, None, None,
1142 members, None))
1143 return name
1144
1145 def _def_enum_type(self, expr, info):
1146 name = expr['enum']
1147 data = expr['data']
1148 prefix = expr.get('prefix')
1149 self._def_entity(QAPISchemaEnumType(name, info, data, prefix))
1150 self._make_array_type(name) # TODO really needed?
1151
1152 def _make_member(self, name, typ):
1153 optional = False
1154 if name.startswith('*'):
1155 name = name[1:]
1156 optional = True
1157 if isinstance(typ, list):
1158 assert len(typ) == 1
1159 typ = self._make_array_type(typ[0])
1160 return QAPISchemaObjectTypeMember(name, typ, optional)
1161
1162 def _make_members(self, data):
1163 return [self._make_member(key, value)
1164 for (key, value) in data.iteritems()]
1165
1166 def _def_struct_type(self, expr, info):
1167 name = expr['struct']
1168 base = expr.get('base')
1169 data = expr['data']
1170 self._def_entity(QAPISchemaObjectType(name, info, base,
1171 self._make_members(data),
1172 None))
1173 self._make_array_type(name) # TODO really needed?
1174
1175 def _make_variant(self, case, typ):
1176 return QAPISchemaObjectTypeVariant(case, typ)
1177
1178 def _make_simple_variant(self, case, typ):
1179 if isinstance(typ, list):
1180 assert len(typ) == 1
1181 typ = self._make_array_type(typ[0])
1182 typ = self._make_implicit_object_type(typ, 'wrapper',
1183 [self._make_member('data', typ)])
1184 return QAPISchemaObjectTypeVariant(case, typ)
1185
1186 def _make_tag_enum(self, type_name, variants):
1187 return self._make_implicit_enum_type(type_name,
1188 [v.name for v in variants])
1189
1190 def _def_union_type(self, expr, info):
1191 name = expr['union']
1192 data = expr['data']
1193 base = expr.get('base')
1194 tag_name = expr.get('discriminator')
1195 tag_enum = None
1196 if tag_name:
1197 variants = [self._make_variant(key, value)
1198 for (key, value) in data.iteritems()]
1199 else:
1200 variants = [self._make_simple_variant(key, value)
1201 for (key, value) in data.iteritems()]
1202 tag_enum = self._make_tag_enum(name, variants)
1203 self._def_entity(
1204 QAPISchemaObjectType(name, info, base,
1205 self._make_members(OrderedDict()),
1206 QAPISchemaObjectTypeVariants(tag_name,
1207 tag_enum,
1208 variants)))
1209 self._make_array_type(name) # TODO really needed?
1210
1211 def _def_alternate_type(self, expr, info):
1212 name = expr['alternate']
1213 data = expr['data']
1214 variants = [self._make_variant(key, value)
1215 for (key, value) in data.iteritems()]
1216 tag_enum = self._make_tag_enum(name, variants)
1217 self._def_entity(
1218 QAPISchemaAlternateType(name, info,
1219 QAPISchemaObjectTypeVariants(None,
1220 tag_enum,
1221 variants)))
1222 self._make_array_type(name) # TODO really needed?
1223
1224 def _def_command(self, expr, info):
1225 name = expr['command']
1226 data = expr.get('data')
1227 rets = expr.get('returns')
1228 gen = expr.get('gen', True)
1229 success_response = expr.get('success-response', True)
1230 if isinstance(data, OrderedDict):
1231 data = self._make_implicit_object_type(name, 'arg',
1232 self._make_members(data))
1233 if isinstance(rets, list):
1234 assert len(rets) == 1
1235 rets = self._make_array_type(rets[0])
1236 self._def_entity(QAPISchemaCommand(name, info, data, rets, gen,
1237 success_response))
1238
1239 def _def_event(self, expr, info):
1240 name = expr['event']
1241 data = expr.get('data')
1242 if isinstance(data, OrderedDict):
1243 data = self._make_implicit_object_type(name, 'arg',
1244 self._make_members(data))
1245 self._def_entity(QAPISchemaEvent(name, info, data))
1246
1247 def _def_exprs(self):
1248 for expr_elem in self.exprs:
1249 expr = expr_elem['expr']
1250 info = expr_elem['info']
1251 if 'enum' in expr:
1252 self._def_enum_type(expr, info)
1253 elif 'struct' in expr:
1254 self._def_struct_type(expr, info)
1255 elif 'union' in expr:
1256 self._def_union_type(expr, info)
1257 elif 'alternate' in expr:
1258 self._def_alternate_type(expr, info)
1259 elif 'command' in expr:
1260 self._def_command(expr, info)
1261 elif 'event' in expr:
1262 self._def_event(expr, info)
1263 else:
1264 assert False
1265
1266 def check(self):
1267 for ent in self._entity_dict.values():
1268 ent.check(self)
1269
Markus Armbruster3f7dc212015-09-16 13:06:07 +02001270 def visit(self, visitor):
Markus Armbruster39a18152015-09-16 13:06:28 +02001271 ignore = visitor.visit_begin(self)
Markus Armbruster3f7dc212015-09-16 13:06:07 +02001272 for name in sorted(self._entity_dict.keys()):
Markus Armbruster39a18152015-09-16 13:06:28 +02001273 if not ignore or not isinstance(self._entity_dict[name], ignore):
1274 self._entity_dict[name].visit(visitor)
Markus Armbruster3f7dc212015-09-16 13:06:07 +02001275 visitor.visit_end()
1276
Markus Armbruster2caba362013-07-27 17:41:56 +02001277
Markus Armbruster00e4b282015-06-10 10:04:36 +02001278#
1279# Code generation helpers
1280#
1281
Michael Roth0f923be2011-07-19 14:50:39 -05001282def camel_case(name):
1283 new_name = ''
1284 first = True
1285 for ch in name:
1286 if ch in ['_', '-']:
1287 first = True
1288 elif first:
1289 new_name += ch.upper()
1290 first = False
1291 else:
1292 new_name += ch.lower()
1293 return new_name
1294
Markus Armbruster849bc532015-05-14 06:50:53 -06001295# ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
1296# ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
1297# ENUM24_Name -> ENUM24_NAME
1298def camel_to_upper(value):
1299 c_fun_str = c_name(value, False)
1300 if value.isupper():
1301 return c_fun_str
1302
1303 new_name = ''
1304 l = len(c_fun_str)
1305 for i in range(l):
1306 c = c_fun_str[i]
1307 # When c is upper and no "_" appears before, do more checks
1308 if c.isupper() and (i > 0) and c_fun_str[i - 1] != "_":
1309 # Case 1: next string is lower
1310 # Case 2: previous string is digit
1311 if (i < (l - 1) and c_fun_str[i + 1].islower()) or \
1312 c_fun_str[i - 1].isdigit():
1313 new_name += '_'
1314 new_name += c
1315 return new_name.lstrip('_').upper()
1316
Daniel P. Berrange351d36e2015-08-26 14:21:20 +01001317def c_enum_const(type_name, const_name, prefix=None):
1318 if prefix is not None:
1319 type_name = prefix
Markus Armbruster849bc532015-05-14 06:50:53 -06001320 return camel_to_upper(type_name + '_' + const_name)
1321
Eric Blake18df5152015-05-14 06:50:48 -06001322c_name_trans = string.maketrans('.-', '__')
Markus Armbruster47299262015-05-14 06:50:47 -06001323
Eric Blakec6405b52015-05-14 06:50:55 -06001324# Map @name to a valid C identifier.
1325# If @protect, avoid returning certain ticklish identifiers (like
1326# C keywords) by prepending "q_".
1327#
1328# Used for converting 'name' from a 'name':'type' qapi definition
1329# into a generated struct member, as well as converting type names
1330# into substrings of a generated C function name.
1331# '__a.b_c' -> '__a_b_c', 'x-foo' -> 'x_foo'
1332# protect=True: 'int' -> 'q_int'; protect=False: 'int' -> 'int'
Eric Blake18df5152015-05-14 06:50:48 -06001333def c_name(name, protect=True):
Blue Swirl427a1a22012-07-30 15:46:55 +00001334 # ANSI X3J11/88-090, 3.1.1
1335 c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
1336 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
1337 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
1338 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
1339 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
1340 # ISO/IEC 9899:1999, 6.4.1
1341 c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
1342 # ISO/IEC 9899:2011, 6.4.1
1343 c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
1344 '_Static_assert', '_Thread_local'])
1345 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
1346 # excluding _.*
1347 gcc_words = set(['asm', 'typeof'])
Tomoki Sekiyama6f880092013-08-07 11:39:43 -04001348 # C++ ISO/IEC 14882:2003 2.11
1349 cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete',
1350 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
1351 'namespace', 'new', 'operator', 'private', 'protected',
1352 'public', 'reinterpret_cast', 'static_cast', 'template',
1353 'this', 'throw', 'true', 'try', 'typeid', 'typename',
1354 'using', 'virtual', 'wchar_t',
1355 # alternative representations
1356 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
1357 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
Paolo Bonzini10577252012-09-19 16:31:07 +02001358 # namespace pollution:
Max Reitz8592a542013-12-20 19:28:18 +01001359 polluted_words = set(['unix', 'errno'])
Tomoki Sekiyama6f880092013-08-07 11:39:43 -04001360 if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
Blue Swirl427a1a22012-07-30 15:46:55 +00001361 return "q_" + name
Eric Blake18df5152015-05-14 06:50:48 -06001362 return name.translate(c_name_trans)
Michael Roth0f923be2011-07-19 14:50:39 -05001363
Amos Kong05dfb262014-06-10 19:25:53 +08001364eatspace = '\033EATSPACE.'
Eric Blaked5573442015-05-14 06:50:54 -06001365pointer_suffix = ' *' + eatspace
Amos Kong05dfb262014-06-10 19:25:53 +08001366
Michael Roth0f923be2011-07-19 14:50:39 -05001367def genindent(count):
1368 ret = ""
1369 for i in range(count):
1370 ret += " "
1371 return ret
1372
1373indent_level = 0
1374
1375def push_indent(indent_amount=4):
1376 global indent_level
1377 indent_level += indent_amount
1378
1379def pop_indent(indent_amount=4):
1380 global indent_level
1381 indent_level -= indent_amount
1382
Markus Armbruster77e703b2015-06-24 19:27:32 +02001383# Generate @code with @kwds interpolated.
1384# Obey indent_level, and strip eatspace.
Michael Roth0f923be2011-07-19 14:50:39 -05001385def cgen(code, **kwds):
Markus Armbruster77e703b2015-06-24 19:27:32 +02001386 raw = code % kwds
1387 if indent_level:
1388 indent = genindent(indent_level)
Markus Armbruster2752e5b2015-09-07 17:45:55 +02001389 # re.subn() lacks flags support before Python 2.7, use re.compile()
1390 raw = re.subn(re.compile("^.", re.MULTILINE),
1391 indent + r'\g<0>', raw)
Markus Armbruster77e703b2015-06-24 19:27:32 +02001392 raw = raw[0]
1393 return re.sub(re.escape(eatspace) + ' *', '', raw)
Michael Roth0f923be2011-07-19 14:50:39 -05001394
1395def mcgen(code, **kwds):
Markus Armbruster77e703b2015-06-24 19:27:32 +02001396 if code[0] == '\n':
1397 code = code[1:]
1398 return cgen(code, **kwds)
Michael Roth0f923be2011-07-19 14:50:39 -05001399
Michael Roth0f923be2011-07-19 14:50:39 -05001400
1401def guardname(filename):
Markus Armbruster00dfc3b2015-06-27 07:27:21 +02001402 return c_name(filename, protect=False).upper()
Michael Rothc0afa9c2013-05-10 17:46:00 -05001403
1404def guardstart(name):
1405 return mcgen('''
1406
1407#ifndef %(name)s
1408#define %(name)s
1409
1410''',
1411 name=guardname(name))
1412
1413def guardend(name):
1414 return mcgen('''
1415
1416#endif /* %(name)s */
1417
1418''',
1419 name=guardname(name))
Markus Armbruster2114f5a2015-04-02 13:12:21 +02001420
Markus Armbrustere98859a2015-09-16 13:06:16 +02001421def gen_enum_lookup(name, values, prefix=None):
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001422 ret = mcgen('''
1423
Markus Armbrustere98859a2015-09-16 13:06:16 +02001424const char *const %(c_name)s_lookup[] = {
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001425''',
Markus Armbrustere98859a2015-09-16 13:06:16 +02001426 c_name=c_name(name))
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001427 for value in values:
1428 index = c_enum_const(name, value, prefix)
1429 ret += mcgen('''
1430 [%(index)s] = "%(value)s",
1431''',
Markus Armbrustere98859a2015-09-16 13:06:16 +02001432 index=index, value=value)
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001433
1434 max_index = c_enum_const(name, 'MAX', prefix)
1435 ret += mcgen('''
1436 [%(max_index)s] = NULL,
1437};
1438''',
Markus Armbrustere98859a2015-09-16 13:06:16 +02001439 max_index=max_index)
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001440 return ret
1441
Markus Armbrustere98859a2015-09-16 13:06:16 +02001442def gen_enum(name, values, prefix=None):
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001443 # append automatically generated _MAX value
Markus Armbrustere98859a2015-09-16 13:06:16 +02001444 enum_values = values + ['MAX']
1445
1446 ret = mcgen('''
1447
1448typedef enum %(c_name)s {
1449''',
1450 c_name=c_name(name))
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001451
1452 i = 0
1453 for value in enum_values:
Markus Armbrustere98859a2015-09-16 13:06:16 +02001454 ret += mcgen('''
1455 %(c_enum)s = %(i)d,
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001456''',
Markus Armbrustere98859a2015-09-16 13:06:16 +02001457 c_enum=c_enum_const(name, value, prefix),
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001458 i=i)
1459 i += 1
1460
Markus Armbrustere98859a2015-09-16 13:06:16 +02001461 ret += mcgen('''
1462} %(c_name)s;
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001463''',
Markus Armbrustere98859a2015-09-16 13:06:16 +02001464 c_name=c_name(name))
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001465
Markus Armbrustere98859a2015-09-16 13:06:16 +02001466 ret += mcgen('''
1467
1468extern const char *const %(c_name)s_lookup[];
1469''',
1470 c_name=c_name(name))
1471 return ret
Markus Armbrusterefd2eaa2015-09-16 13:06:12 +02001472
Markus Armbruster03b43672015-09-16 13:06:20 +02001473def gen_params(arg_type, extra):
1474 if not arg_type:
1475 return extra
1476 assert not arg_type.variants
1477 ret = ''
1478 sep = ''
1479 for memb in arg_type.members:
1480 ret += sep
1481 sep = ', '
1482 if memb.optional:
1483 ret += 'bool has_%s, ' % c_name(memb.name)
1484 ret += '%s %s' % (memb.type.c_type(is_param=True), c_name(memb.name))
1485 if extra:
1486 ret += sep + extra
1487 return ret
1488
Markus Armbruster00e4b282015-06-10 10:04:36 +02001489#
1490# Common command line parsing
1491#
1492
Markus Armbruster2114f5a2015-04-02 13:12:21 +02001493def parse_command_line(extra_options = "", extra_long_options = []):
1494
1495 try:
1496 opts, args = getopt.gnu_getopt(sys.argv[1:],
Markus Armbruster16d80f62015-04-02 13:32:16 +02001497 "chp:o:" + extra_options,
Markus Armbruster2114f5a2015-04-02 13:12:21 +02001498 ["source", "header", "prefix=",
Markus Armbruster16d80f62015-04-02 13:32:16 +02001499 "output-dir="] + extra_long_options)
Markus Armbruster2114f5a2015-04-02 13:12:21 +02001500 except getopt.GetoptError, err:
Markus Armbrusterb4540962015-04-02 13:17:34 +02001501 print >>sys.stderr, "%s: %s" % (sys.argv[0], str(err))
Markus Armbruster2114f5a2015-04-02 13:12:21 +02001502 sys.exit(1)
1503
1504 output_dir = ""
1505 prefix = ""
1506 do_c = False
1507 do_h = False
1508 extra_opts = []
1509
1510 for oa in opts:
1511 o, a = oa
1512 if o in ("-p", "--prefix"):
Markus Armbruster1cf47a12015-07-01 13:13:54 +02001513 match = re.match('([A-Za-z_.-][A-Za-z0-9_.-]*)?', a)
1514 if match.end() != len(a):
1515 print >>sys.stderr, \
1516 "%s: 'funny character '%s' in argument of --prefix" \
1517 % (sys.argv[0], a[match.end()])
1518 sys.exit(1)
Markus Armbruster2114f5a2015-04-02 13:12:21 +02001519 prefix = a
Markus Armbruster2114f5a2015-04-02 13:12:21 +02001520 elif o in ("-o", "--output-dir"):
1521 output_dir = a + "/"
1522 elif o in ("-c", "--source"):
1523 do_c = True
1524 elif o in ("-h", "--header"):
1525 do_h = True
1526 else:
1527 extra_opts.append(oa)
1528
1529 if not do_c and not do_h:
1530 do_c = True
1531 do_h = True
1532
Markus Armbruster16d80f62015-04-02 13:32:16 +02001533 if len(args) != 1:
1534 print >>sys.stderr, "%s: need exactly one argument" % sys.argv[0]
Markus Armbrusterb4540962015-04-02 13:17:34 +02001535 sys.exit(1)
Markus Armbruster54414042015-06-09 16:22:45 +02001536 fname = args[0]
Markus Armbrusterb4540962015-04-02 13:17:34 +02001537
Markus Armbruster54414042015-06-09 16:22:45 +02001538 return (fname, output_dir, do_c, do_h, prefix, extra_opts)
Markus Armbruster12f8e1b2015-04-02 14:46:39 +02001539
Markus Armbruster00e4b282015-06-10 10:04:36 +02001540#
1541# Generate output files with boilerplate
1542#
1543
Markus Armbruster12f8e1b2015-04-02 14:46:39 +02001544def open_output(output_dir, do_c, do_h, prefix, c_file, h_file,
1545 c_comment, h_comment):
Markus Armbruster00dfc3b2015-06-27 07:27:21 +02001546 guard = guardname(prefix + h_file)
Markus Armbruster12f8e1b2015-04-02 14:46:39 +02001547 c_file = output_dir + prefix + c_file
1548 h_file = output_dir + prefix + h_file
1549
Markus Armbrusterc4f498f2015-09-03 10:24:25 +02001550 if output_dir:
1551 try:
1552 os.makedirs(output_dir)
1553 except os.error, e:
1554 if e.errno != errno.EEXIST:
1555 raise
Markus Armbruster12f8e1b2015-04-02 14:46:39 +02001556
1557 def maybe_open(really, name, opt):
1558 if really:
1559 return open(name, opt)
1560 else:
1561 import StringIO
1562 return StringIO.StringIO()
1563
1564 fdef = maybe_open(do_c, c_file, 'w')
1565 fdecl = maybe_open(do_h, h_file, 'w')
1566
1567 fdef.write(mcgen('''
1568/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
1569%(comment)s
1570''',
1571 comment = c_comment))
1572
1573 fdecl.write(mcgen('''
1574/* AUTOMATICALLY GENERATED, DO NOT MODIFY */
1575%(comment)s
1576#ifndef %(guard)s
1577#define %(guard)s
1578
1579''',
Markus Armbruster00dfc3b2015-06-27 07:27:21 +02001580 comment = h_comment, guard = guard))
Markus Armbruster12f8e1b2015-04-02 14:46:39 +02001581
1582 return (fdef, fdecl)
1583
1584def close_output(fdef, fdecl):
1585 fdecl.write('''
1586#endif
1587''')
Markus Armbruster12f8e1b2015-04-02 14:46:39 +02001588 fdecl.close()
Markus Armbruster12f8e1b2015-04-02 14:46:39 +02001589 fdef.close()