blob: 77d46aa99593eeef901c31369285ee10f6850a2d [file] [log] [blame]
Michael Roth0f923be2011-07-19 14:50:39 -05001#
2# QAPI helper library
3#
4# Copyright IBM, Corp. 2011
Markus Armbrusterc7a3f252013-07-27 17:41:55 +02005# Copyright (c) 2013 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
Lluís Vilanova33aaad52014-05-02 15:52:35 +020016import os
Markus Armbruster2caba362013-07-27 17:41:56 +020017import sys
Michael Roth0f923be2011-07-19 14:50:39 -050018
Michael Rothc0afa9c2013-05-10 17:46:00 -050019builtin_types = [
20 'str', 'int', 'number', 'bool',
21 'int8', 'int16', 'int32', 'int64',
22 'uint8', 'uint16', 'uint32', 'uint64'
23]
24
Kevin Wolf69dd62d2013-07-08 16:14:21 +020025builtin_type_qtypes = {
26 'str': 'QTYPE_QSTRING',
27 'int': 'QTYPE_QINT',
28 'number': 'QTYPE_QFLOAT',
29 'bool': 'QTYPE_QBOOL',
30 'int8': 'QTYPE_QINT',
31 'int16': 'QTYPE_QINT',
32 'int32': 'QTYPE_QINT',
33 'int64': 'QTYPE_QINT',
34 'uint8': 'QTYPE_QINT',
35 'uint16': 'QTYPE_QINT',
36 'uint32': 'QTYPE_QINT',
37 'uint64': 'QTYPE_QINT',
38}
39
Lluís Vilanovaa719a272014-05-07 20:46:15 +020040def error_path(parent):
41 res = ""
42 while parent:
43 res = ("In file included from %s:%d:\n" % (parent['file'],
44 parent['line'])) + res
45 parent = parent['parent']
46 return res
47
Markus Armbruster2caba362013-07-27 17:41:56 +020048class QAPISchemaError(Exception):
49 def __init__(self, schema, msg):
Lluís Vilanovaa719a272014-05-07 20:46:15 +020050 self.input_file = schema.input_file
Markus Armbruster2caba362013-07-27 17:41:56 +020051 self.msg = msg
Wenchao Xia515b9432014-03-04 18:44:33 -080052 self.col = 1
53 self.line = schema.line
54 for ch in schema.src[schema.line_pos:schema.pos]:
55 if ch == '\t':
Markus Armbruster2caba362013-07-27 17:41:56 +020056 self.col = (self.col + 7) % 8 + 1
57 else:
58 self.col += 1
Lluís Vilanovaa719a272014-05-07 20:46:15 +020059 self.info = schema.parent_info
Markus Armbruster2caba362013-07-27 17:41:56 +020060
61 def __str__(self):
Lluís Vilanovaa719a272014-05-07 20:46:15 +020062 return error_path(self.info) + \
63 "%s:%d:%d: %s" % (self.input_file, self.line, self.col, self.msg)
Markus Armbruster2caba362013-07-27 17:41:56 +020064
Wenchao Xiab86b05e2014-03-04 18:44:34 -080065class QAPIExprError(Exception):
66 def __init__(self, expr_info, msg):
Lluís Vilanovaa719a272014-05-07 20:46:15 +020067 self.info = expr_info
Wenchao Xiab86b05e2014-03-04 18:44:34 -080068 self.msg = msg
69
70 def __str__(self):
Lluís Vilanovaa719a272014-05-07 20:46:15 +020071 return error_path(self.info['parent']) + \
72 "%s:%d: %s" % (self.info['file'], self.info['line'], self.msg)
Wenchao Xiab86b05e2014-03-04 18:44:34 -080073
Markus Armbrusterc7a3f252013-07-27 17:41:55 +020074class QAPISchema:
Michael Roth0f923be2011-07-19 14:50:39 -050075
Benoît Canet24fd8482014-05-16 12:51:56 +020076 def __init__(self, fp, input_relname=None, include_hist=[],
77 previously_included=[], parent_info=None):
78 """ include_hist is a stack used to detect inclusion cycles
79 previously_included is a global state used to avoid multiple
80 inclusions of the same file"""
Lluís Vilanovaa719a272014-05-07 20:46:15 +020081 input_fname = os.path.abspath(fp.name)
82 if input_relname is None:
83 input_relname = fp.name
84 self.input_dir = os.path.dirname(input_fname)
85 self.input_file = input_relname
86 self.include_hist = include_hist + [(input_relname, input_fname)]
Benoît Canet24fd8482014-05-16 12:51:56 +020087 previously_included.append(input_fname)
Lluís Vilanovaa719a272014-05-07 20:46:15 +020088 self.parent_info = parent_info
Markus Armbrusterc7a3f252013-07-27 17:41:55 +020089 self.src = fp.read()
90 if self.src == '' or self.src[-1] != '\n':
91 self.src += '\n'
92 self.cursor = 0
Wenchao Xia515b9432014-03-04 18:44:33 -080093 self.line = 1
94 self.line_pos = 0
Markus Armbrusterc7a3f252013-07-27 17:41:55 +020095 self.exprs = []
96 self.accept()
Michael Roth0f923be2011-07-19 14:50:39 -050097
Markus Armbrusterc7a3f252013-07-27 17:41:55 +020098 while self.tok != None:
Lluís Vilanovaa719a272014-05-07 20:46:15 +020099 expr_info = {'file': input_relname, 'line': self.line, 'parent': self.parent_info}
100 expr = self.get_expr(False)
101 if isinstance(expr, dict) and "include" in expr:
102 if len(expr) != 1:
103 raise QAPIExprError(expr_info, "Invalid 'include' directive")
104 include = expr["include"]
105 if not isinstance(include, str):
106 raise QAPIExprError(expr_info,
107 'Expected a file name (string), got: %s'
108 % include)
109 include_path = os.path.join(self.input_dir, include)
Stefan Hajnoczi7ac9a9d2014-08-27 12:08:51 +0100110 for elem in self.include_hist:
111 if include_path == elem[1]:
112 raise QAPIExprError(expr_info, "Inclusion loop for %s"
113 % include)
Benoît Canet24fd8482014-05-16 12:51:56 +0200114 # skip multiple include of the same file
115 if include_path in previously_included:
116 continue
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200117 try:
118 fobj = open(include_path, 'r')
Luiz Capitulino34788812014-05-20 13:50:19 -0400119 except IOError, e:
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200120 raise QAPIExprError(expr_info,
121 '%s: %s' % (e.strerror, include))
Benoît Canet24fd8482014-05-16 12:51:56 +0200122 exprs_include = QAPISchema(fobj, include, self.include_hist,
123 previously_included, expr_info)
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200124 self.exprs.extend(exprs_include.exprs)
125 else:
126 expr_elem = {'expr': expr,
127 'info': expr_info}
128 self.exprs.append(expr_elem)
Michael Roth0f923be2011-07-19 14:50:39 -0500129
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200130 def accept(self):
131 while True:
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200132 self.tok = self.src[self.cursor]
Markus Armbruster2caba362013-07-27 17:41:56 +0200133 self.pos = self.cursor
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200134 self.cursor += 1
135 self.val = None
Michael Roth0f923be2011-07-19 14:50:39 -0500136
Markus Armbrusterf1a145e2013-07-27 17:42:01 +0200137 if self.tok == '#':
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200138 self.cursor = self.src.find('\n', self.cursor)
139 elif self.tok in ['{', '}', ':', ',', '[', ']']:
140 return
141 elif self.tok == "'":
142 string = ''
143 esc = False
144 while True:
145 ch = self.src[self.cursor]
146 self.cursor += 1
147 if ch == '\n':
Markus Armbruster2caba362013-07-27 17:41:56 +0200148 raise QAPISchemaError(self,
149 'Missing terminating "\'"')
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200150 if esc:
151 string += ch
152 esc = False
153 elif ch == "\\":
154 esc = True
155 elif ch == "'":
156 self.val = string
157 return
158 else:
159 string += ch
160 elif self.tok == '\n':
161 if self.cursor == len(self.src):
162 self.tok = None
163 return
Wenchao Xia515b9432014-03-04 18:44:33 -0800164 self.line += 1
165 self.line_pos = self.cursor
Markus Armbruster9213aa52013-07-27 17:41:57 +0200166 elif not self.tok.isspace():
167 raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
Michael Roth0f923be2011-07-19 14:50:39 -0500168
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200169 def get_members(self):
170 expr = OrderedDict()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200171 if self.tok == '}':
172 self.accept()
173 return expr
174 if self.tok != "'":
175 raise QAPISchemaError(self, 'Expected string or "}"')
176 while True:
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200177 key = self.val
178 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200179 if self.tok != ':':
180 raise QAPISchemaError(self, 'Expected ":"')
181 self.accept()
Wenchao Xia4b359912014-03-04 18:44:32 -0800182 if key in expr:
183 raise QAPISchemaError(self, 'Duplicate key "%s"' % key)
Markus Armbruster5f3cd2b2013-07-27 17:41:59 +0200184 expr[key] = self.get_expr(True)
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200185 if self.tok == '}':
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200186 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200187 return expr
188 if self.tok != ',':
189 raise QAPISchemaError(self, 'Expected "," or "}"')
190 self.accept()
191 if self.tok != "'":
192 raise QAPISchemaError(self, 'Expected string')
Michael Roth0f923be2011-07-19 14:50:39 -0500193
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200194 def get_values(self):
195 expr = []
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200196 if self.tok == ']':
197 self.accept()
198 return expr
199 if not self.tok in [ '{', '[', "'" ]:
200 raise QAPISchemaError(self, 'Expected "{", "[", "]" or string')
201 while True:
Markus Armbruster5f3cd2b2013-07-27 17:41:59 +0200202 expr.append(self.get_expr(True))
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200203 if self.tok == ']':
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200204 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200205 return expr
206 if self.tok != ',':
207 raise QAPISchemaError(self, 'Expected "," or "]"')
208 self.accept()
Michael Roth0f923be2011-07-19 14:50:39 -0500209
Markus Armbruster5f3cd2b2013-07-27 17:41:59 +0200210 def get_expr(self, nested):
211 if self.tok != '{' and not nested:
212 raise QAPISchemaError(self, 'Expected "{"')
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200213 if self.tok == '{':
214 self.accept()
215 expr = self.get_members()
216 elif self.tok == '[':
217 self.accept()
218 expr = self.get_values()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200219 elif self.tok == "'":
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200220 expr = self.val
221 self.accept()
Markus Armbruster6974ccd2013-07-27 17:41:58 +0200222 else:
223 raise QAPISchemaError(self, 'Expected "{", "[" or string')
Markus Armbrusterc7a3f252013-07-27 17:41:55 +0200224 return expr
Kevin Wolfbd9927f2013-07-01 16:31:50 +0200225
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800226def find_base_fields(base):
227 base_struct_define = find_struct(base)
228 if not base_struct_define:
229 return None
230 return base_struct_define['data']
231
Wenchao Xiabceae762014-03-06 17:08:56 -0800232# Return the discriminator enum define if discriminator is specified as an
233# enum type, otherwise return None.
234def discriminator_find_enum_define(expr):
235 base = expr.get('base')
236 discriminator = expr.get('discriminator')
237
238 if not (discriminator and base):
239 return None
240
241 base_fields = find_base_fields(base)
242 if not base_fields:
243 return None
244
245 discriminator_type = base_fields.get(discriminator)
246 if not discriminator_type:
247 return None
248
249 return find_enum(discriminator_type)
250
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200251def check_event(expr, expr_info):
252 params = expr.get('data')
253 if params:
254 for argname, argentry, optional, structured in parse_args(params):
255 if structured:
256 raise QAPIExprError(expr_info,
257 "Nested structure define in event is not "
Wenchao Xiad6f9c822014-06-24 16:33:59 -0700258 "supported, event '%s', argname '%s'"
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200259 % (expr['event'], argname))
260
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800261def check_union(expr, expr_info):
262 name = expr['union']
263 base = expr.get('base')
264 discriminator = expr.get('discriminator')
265 members = expr['data']
266
267 # If the object has a member 'base', its value must name a complex type.
268 if base:
269 base_fields = find_base_fields(base)
270 if not base_fields:
271 raise QAPIExprError(expr_info,
272 "Base '%s' is not a valid type"
273 % base)
274
275 # If the union object has no member 'discriminator', it's an
276 # ordinary union.
277 if not discriminator:
278 enum_define = None
279
280 # Else if the value of member 'discriminator' is {}, it's an
281 # anonymous union.
282 elif discriminator == {}:
283 enum_define = None
284
285 # Else, it's a flat union.
286 else:
287 # The object must have a member 'base'.
288 if not base:
289 raise QAPIExprError(expr_info,
290 "Flat union '%s' must have a base field"
291 % name)
292 # The value of member 'discriminator' must name a member of the
293 # base type.
294 discriminator_type = base_fields.get(discriminator)
295 if not discriminator_type:
296 raise QAPIExprError(expr_info,
297 "Discriminator '%s' is not a member of base "
298 "type '%s'"
299 % (discriminator, base))
300 enum_define = find_enum(discriminator_type)
Wenchao Xia52230702014-03-04 18:44:39 -0800301 # Do not allow string discriminator
302 if not enum_define:
303 raise QAPIExprError(expr_info,
304 "Discriminator '%s' must be of enumeration "
305 "type" % discriminator)
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800306
307 # Check every branch
308 for (key, value) in members.items():
309 # If this named member's value names an enum type, then all members
310 # of 'data' must also be members of the enum type.
311 if enum_define and not key in enum_define['enum_values']:
312 raise QAPIExprError(expr_info,
313 "Discriminator value '%s' is not found in "
314 "enum '%s'" %
315 (key, enum_define["enum_name"]))
316 # Todo: add checking for values. Key is checked as above, value can be
317 # also checked here, but we need more functions to handle array case.
318
319def check_exprs(schema):
320 for expr_elem in schema.exprs:
321 expr = expr_elem['expr']
322 if expr.has_key('union'):
323 check_union(expr, expr_elem['info'])
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200324 if expr.has_key('event'):
325 check_event(expr, expr_elem['info'])
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800326
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200327def parse_schema(input_file):
Markus Armbruster2caba362013-07-27 17:41:56 +0200328 try:
Lluís Vilanova33aaad52014-05-02 15:52:35 +0200329 schema = QAPISchema(open(input_file, "r"))
Lluís Vilanovaa719a272014-05-07 20:46:15 +0200330 except (QAPISchemaError, QAPIExprError), e:
Markus Armbruster2caba362013-07-27 17:41:56 +0200331 print >>sys.stderr, e
332 exit(1)
333
Kevin Wolfbd9927f2013-07-01 16:31:50 +0200334 exprs = []
335
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800336 for expr_elem in schema.exprs:
337 expr = expr_elem['expr']
Markus Armbruster28b8bd42013-07-27 17:42:00 +0200338 if expr.has_key('enum'):
Wenchao Xiadad1fca2014-03-04 18:44:31 -0800339 add_enum(expr['enum'], expr['data'])
Markus Armbruster28b8bd42013-07-27 17:42:00 +0200340 elif expr.has_key('union'):
341 add_union(expr)
Markus Armbruster28b8bd42013-07-27 17:42:00 +0200342 elif expr.has_key('type'):
343 add_struct(expr)
344 exprs.append(expr)
Michael Roth0f923be2011-07-19 14:50:39 -0500345
Wenchao Xiabceae762014-03-06 17:08:56 -0800346 # Try again for hidden UnionKind enum
347 for expr_elem in schema.exprs:
348 expr = expr_elem['expr']
349 if expr.has_key('union'):
350 if not discriminator_find_enum_define(expr):
351 add_enum('%sKind' % expr['union'])
352
Wenchao Xiab86b05e2014-03-04 18:44:34 -0800353 try:
354 check_exprs(schema)
355 except QAPIExprError, e:
356 print >>sys.stderr, e
357 exit(1)
358
Michael Roth0f923be2011-07-19 14:50:39 -0500359 return exprs
360
361def parse_args(typeinfo):
Kevin Wolfb35284e2013-07-01 16:31:51 +0200362 if isinstance(typeinfo, basestring):
363 struct = find_struct(typeinfo)
364 assert struct != None
365 typeinfo = struct['data']
366
Michael Roth0f923be2011-07-19 14:50:39 -0500367 for member in typeinfo:
368 argname = member
369 argentry = typeinfo[member]
370 optional = False
371 structured = False
372 if member.startswith('*'):
373 argname = member[1:]
374 optional = True
375 if isinstance(argentry, OrderedDict):
376 structured = True
377 yield (argname, argentry, optional, structured)
378
379def de_camel_case(name):
380 new_name = ''
381 for ch in name:
382 if ch.isupper() and new_name:
383 new_name += '_'
384 if ch == '-':
385 new_name += '_'
386 else:
387 new_name += ch.lower()
388 return new_name
389
390def camel_case(name):
391 new_name = ''
392 first = True
393 for ch in name:
394 if ch in ['_', '-']:
395 first = True
396 elif first:
397 new_name += ch.upper()
398 first = False
399 else:
400 new_name += ch.lower()
401 return new_name
402
Paolo Bonzinieda50a62012-09-19 16:31:06 +0200403def c_var(name, protect=True):
Blue Swirl427a1a22012-07-30 15:46:55 +0000404 # ANSI X3J11/88-090, 3.1.1
405 c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
406 'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
407 'for', 'goto', 'if', 'int', 'long', 'register', 'return',
408 'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
409 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
410 # ISO/IEC 9899:1999, 6.4.1
411 c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
412 # ISO/IEC 9899:2011, 6.4.1
413 c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
414 '_Static_assert', '_Thread_local'])
415 # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
416 # excluding _.*
417 gcc_words = set(['asm', 'typeof'])
Tomoki Sekiyama6f880092013-08-07 11:39:43 -0400418 # C++ ISO/IEC 14882:2003 2.11
419 cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete',
420 'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
421 'namespace', 'new', 'operator', 'private', 'protected',
422 'public', 'reinterpret_cast', 'static_cast', 'template',
423 'this', 'throw', 'true', 'try', 'typeid', 'typename',
424 'using', 'virtual', 'wchar_t',
425 # alternative representations
426 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
427 'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
Paolo Bonzini10577252012-09-19 16:31:07 +0200428 # namespace pollution:
Max Reitz8592a542013-12-20 19:28:18 +0100429 polluted_words = set(['unix', 'errno'])
Tomoki Sekiyama6f880092013-08-07 11:39:43 -0400430 if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
Blue Swirl427a1a22012-07-30 15:46:55 +0000431 return "q_" + name
Federico Simoncellic9da2282012-03-20 13:54:35 +0000432 return name.replace('-', '_').lstrip("*")
433
Paolo Bonzinieda50a62012-09-19 16:31:06 +0200434def c_fun(name, protect=True):
435 return c_var(name, protect).replace('.', '_')
Michael Roth0f923be2011-07-19 14:50:39 -0500436
437def c_list_type(name):
438 return '%sList' % name
439
440def type_name(name):
441 if type(name) == list:
442 return c_list_type(name[0])
443 return name
444
445enum_types = []
Kevin Wolfb35284e2013-07-01 16:31:51 +0200446struct_types = []
Kevin Wolfea66c6d2013-07-16 10:49:41 +0200447union_types = []
Kevin Wolfb35284e2013-07-01 16:31:51 +0200448
449def add_struct(definition):
450 global struct_types
451 struct_types.append(definition)
452
453def find_struct(name):
454 global struct_types
455 for struct in struct_types:
456 if struct['type'] == name:
457 return struct
458 return None
Michael Roth0f923be2011-07-19 14:50:39 -0500459
Kevin Wolfea66c6d2013-07-16 10:49:41 +0200460def add_union(definition):
461 global union_types
462 union_types.append(definition)
463
464def find_union(name):
465 global union_types
466 for union in union_types:
467 if union['union'] == name:
468 return union
469 return None
470
Wenchao Xiadad1fca2014-03-04 18:44:31 -0800471def add_enum(name, enum_values = None):
Michael Roth0f923be2011-07-19 14:50:39 -0500472 global enum_types
Wenchao Xiadad1fca2014-03-04 18:44:31 -0800473 enum_types.append({"enum_name": name, "enum_values": enum_values})
474
475def find_enum(name):
476 global enum_types
477 for enum in enum_types:
478 if enum['enum_name'] == name:
479 return enum
480 return None
Michael Roth0f923be2011-07-19 14:50:39 -0500481
482def is_enum(name):
Wenchao Xiadad1fca2014-03-04 18:44:31 -0800483 return find_enum(name) != None
Michael Roth0f923be2011-07-19 14:50:39 -0500484
Amos Kong05dfb262014-06-10 19:25:53 +0800485eatspace = '\033EATSPACE.'
486
487# A special suffix is added in c_type() for pointer types, and it's
488# stripped in mcgen(). So please notice this when you check the return
489# value of c_type() outside mcgen().
Amos Kong0d14eeb2014-06-10 19:25:52 +0800490def c_type(name, is_param=False):
Michael Roth0f923be2011-07-19 14:50:39 -0500491 if name == 'str':
Amos Kong0d14eeb2014-06-10 19:25:52 +0800492 if is_param:
Amos Kong05dfb262014-06-10 19:25:53 +0800493 return 'const char *' + eatspace
494 return 'char *' + eatspace
495
Michael Roth0f923be2011-07-19 14:50:39 -0500496 elif name == 'int':
497 return 'int64_t'
Laszlo Ersekc46f18c2012-07-17 16:17:06 +0200498 elif (name == 'int8' or name == 'int16' or name == 'int32' or
499 name == 'int64' or name == 'uint8' or name == 'uint16' or
500 name == 'uint32' or name == 'uint64'):
501 return name + '_t'
Laszlo Ersek092705d2012-07-17 16:17:07 +0200502 elif name == 'size':
503 return 'uint64_t'
Michael Roth0f923be2011-07-19 14:50:39 -0500504 elif name == 'bool':
505 return 'bool'
506 elif name == 'number':
507 return 'double'
508 elif type(name) == list:
Amos Kong05dfb262014-06-10 19:25:53 +0800509 return '%s *%s' % (c_list_type(name[0]), eatspace)
Michael Roth0f923be2011-07-19 14:50:39 -0500510 elif is_enum(name):
511 return name
512 elif name == None or len(name) == 0:
513 return 'void'
514 elif name == name.upper():
Amos Kong05dfb262014-06-10 19:25:53 +0800515 return '%sEvent *%s' % (camel_case(name), eatspace)
Michael Roth0f923be2011-07-19 14:50:39 -0500516 else:
Amos Kong05dfb262014-06-10 19:25:53 +0800517 return '%s *%s' % (name, eatspace)
518
519def is_c_ptr(name):
520 suffix = "*" + eatspace
521 return c_type(name).endswith(suffix)
Michael Roth0f923be2011-07-19 14:50:39 -0500522
523def genindent(count):
524 ret = ""
525 for i in range(count):
526 ret += " "
527 return ret
528
529indent_level = 0
530
531def push_indent(indent_amount=4):
532 global indent_level
533 indent_level += indent_amount
534
535def pop_indent(indent_amount=4):
536 global indent_level
537 indent_level -= indent_amount
538
539def cgen(code, **kwds):
540 indent = genindent(indent_level)
541 lines = code.split('\n')
542 lines = map(lambda x: indent + x, lines)
543 return '\n'.join(lines) % kwds + '\n'
544
545def mcgen(code, **kwds):
Amos Kong05dfb262014-06-10 19:25:53 +0800546 raw = cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
547 return re.sub(re.escape(eatspace) + ' *', '', raw)
Michael Roth0f923be2011-07-19 14:50:39 -0500548
549def basename(filename):
550 return filename.split("/")[-1]
551
552def guardname(filename):
Michael Rothd8e1f212011-11-29 16:47:48 -0600553 guard = basename(filename).rsplit(".", 1)[0]
554 for substr in [".", " ", "-"]:
555 guard = guard.replace(substr, "_")
556 return guard.upper() + '_H'
Michael Rothc0afa9c2013-05-10 17:46:00 -0500557
558def guardstart(name):
559 return mcgen('''
560
561#ifndef %(name)s
562#define %(name)s
563
564''',
565 name=guardname(name))
566
567def guardend(name):
568 return mcgen('''
569
570#endif /* %(name)s */
571
572''',
573 name=guardname(name))
Wenchao Xia62996592014-03-04 18:44:35 -0800574
Wenchao Xia5d371f42014-03-04 18:44:40 -0800575# ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
576# ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
577# ENUM24_Name -> ENUM24_NAME
578def _generate_enum_string(value):
579 c_fun_str = c_fun(value, False)
Wenchao Xiab0b58192014-03-04 18:44:36 -0800580 if value.isupper():
Wenchao Xia5d371f42014-03-04 18:44:40 -0800581 return c_fun_str
582
Wenchao Xia62996592014-03-04 18:44:35 -0800583 new_name = ''
Wenchao Xia5d371f42014-03-04 18:44:40 -0800584 l = len(c_fun_str)
585 for i in range(l):
586 c = c_fun_str[i]
587 # When c is upper and no "_" appears before, do more checks
588 if c.isupper() and (i > 0) and c_fun_str[i - 1] != "_":
589 # Case 1: next string is lower
590 # Case 2: previous string is digit
591 if (i < (l - 1) and c_fun_str[i + 1].islower()) or \
592 c_fun_str[i - 1].isdigit():
593 new_name += '_'
Wenchao Xia62996592014-03-04 18:44:35 -0800594 new_name += c
595 return new_name.lstrip('_').upper()
Wenchao Xiab0b58192014-03-04 18:44:36 -0800596
597def generate_enum_full_value(enum_name, enum_value):
Wenchao Xia5d371f42014-03-04 18:44:40 -0800598 abbrev_string = _generate_enum_string(enum_name)
599 value_string = _generate_enum_string(enum_value)
Wenchao Xiab0b58192014-03-04 18:44:36 -0800600 return "%s_%s" % (abbrev_string, value_string)