1#
2# QAPI helper library
3#
4# Copyright IBM, Corp. 2011
5# Copyright (c) 2013 Red Hat Inc.
6#
7# Authors:
8#  Anthony Liguori <aliguori@us.ibm.com>
9#  Markus Armbruster <armbru@redhat.com>
10#
11# This work is licensed under the terms of the GNU GPL, version 2.
12# See the COPYING file in the top-level directory.
13
14import re
15from ordereddict import OrderedDict
16import os
17import sys
18
19try:
20    basestring
21except NameError:
22    basestring = str
23
24builtin_types = [
25    'str', 'int', 'number', 'bool',
26    'int8', 'int16', 'int32', 'int64',
27    'uint8', 'uint16', 'uint32', 'uint64'
28]
29
30builtin_type_qtypes = {
31    'str':      'QTYPE_QSTRING',
32    'int':      'QTYPE_QINT',
33    'number':   'QTYPE_QFLOAT',
34    'bool':     'QTYPE_QBOOL',
35    'int8':     'QTYPE_QINT',
36    'int16':    'QTYPE_QINT',
37    'int32':    'QTYPE_QINT',
38    'int64':    'QTYPE_QINT',
39    'uint8':    'QTYPE_QINT',
40    'uint16':   'QTYPE_QINT',
41    'uint32':   'QTYPE_QINT',
42    'uint64':   'QTYPE_QINT',
43}
44
45def error_path(parent):
46    res = ""
47    while parent:
48        res = ("In file included from %s:%d:\n" % (parent['file'],
49                                                   parent['line'])) + res
50        parent = parent['parent']
51    return res
52
53class QAPISchemaError(Exception):
54    def __init__(self, schema, msg):
55        self.input_file = schema.input_file
56        self.msg = msg
57        self.col = 1
58        self.line = schema.line
59        for ch in schema.src[schema.line_pos:schema.pos]:
60            if ch == '\t':
61                self.col = (self.col + 7) % 8 + 1
62            else:
63                self.col += 1
64        self.info = schema.parent_info
65
66    def __str__(self):
67        return error_path(self.info) + \
68            "%s:%d:%d: %s" % (self.input_file, self.line, self.col, self.msg)
69
70class QAPIExprError(Exception):
71    def __init__(self, expr_info, msg):
72        self.info = expr_info
73        self.msg = msg
74
75    def __str__(self):
76        return error_path(self.info['parent']) + \
77            "%s:%d: %s" % (self.info['file'], self.info['line'], self.msg)
78
79class QAPISchema:
80
81    def __init__(self, fp, input_relname=None, include_hist=[],
82                 previously_included=[], parent_info=None):
83        """ include_hist is a stack used to detect inclusion cycles
84            previously_included is a global state used to avoid multiple
85                                inclusions of the same file"""
86        input_fname = os.path.abspath(fp.name)
87        if input_relname is None:
88            input_relname = fp.name
89        self.input_dir = os.path.dirname(input_fname)
90        self.input_file = input_relname
91        self.include_hist = include_hist + [(input_relname, input_fname)]
92        previously_included.append(input_fname)
93        self.parent_info = parent_info
94        self.src = fp.read()
95        if self.src == '' or self.src[-1] != '\n':
96            self.src += '\n'
97        self.cursor = 0
98        self.line = 1
99        self.line_pos = 0
100        self.exprs = []
101        self.accept()
102
103        while self.tok != None:
104            expr_info = {'file': input_relname, 'line': self.line, 'parent': self.parent_info}
105            expr = self.get_expr(False)
106            if isinstance(expr, dict) and "include" in expr:
107                if len(expr) != 1:
108                    raise QAPIExprError(expr_info, "Invalid 'include' directive")
109                include = expr["include"]
110                if not isinstance(include, str):
111                    raise QAPIExprError(expr_info,
112                                        'Expected a file name (string), got: %s'
113                                        % include)
114                include_path = os.path.join(self.input_dir, include)
115                for elem in self.include_hist:
116                    if include_path == elem[1]:
117                        raise QAPIExprError(expr_info, "Inclusion loop for %s"
118                                            % include)
119                # skip multiple include of the same file
120                if include_path in previously_included:
121                    continue
122                try:
123                    fobj = open(include_path, 'r')
124                except IOError as e:
125                    raise QAPIExprError(expr_info,
126                                        '%s: %s' % (e.strerror, include))
127                exprs_include = QAPISchema(fobj, include, self.include_hist,
128                                           previously_included, expr_info)
129                self.exprs.extend(exprs_include.exprs)
130            else:
131                expr_elem = {'expr': expr,
132                             'info': expr_info}
133                self.exprs.append(expr_elem)
134
135    def accept(self):
136        while True:
137            self.tok = self.src[self.cursor]
138            self.pos = self.cursor
139            self.cursor += 1
140            self.val = None
141
142            if self.tok == '#':
143                self.cursor = self.src.find('\n', self.cursor)
144            elif self.tok in ['{', '}', ':', ',', '[', ']']:
145                return
146            elif self.tok == "'":
147                string = ''
148                esc = False
149                while True:
150                    ch = self.src[self.cursor]
151                    self.cursor += 1
152                    if ch == '\n':
153                        raise QAPISchemaError(self,
154                                              'Missing terminating "\'"')
155                    if esc:
156                        string += ch
157                        esc = False
158                    elif ch == "\\":
159                        esc = True
160                    elif ch == "'":
161                        self.val = string
162                        return
163                    else:
164                        string += ch
165            elif self.tok == '\n':
166                if self.cursor == len(self.src):
167                    self.tok = None
168                    return
169                self.line += 1
170                self.line_pos = self.cursor
171            elif not self.tok.isspace():
172                raise QAPISchemaError(self, 'Stray "%s"' % self.tok)
173
174    def get_members(self):
175        expr = OrderedDict()
176        if self.tok == '}':
177            self.accept()
178            return expr
179        if self.tok != "'":
180            raise QAPISchemaError(self, 'Expected string or "}"')
181        while True:
182            key = self.val
183            self.accept()
184            if self.tok != ':':
185                raise QAPISchemaError(self, 'Expected ":"')
186            self.accept()
187            if key in expr:
188                raise QAPISchemaError(self, 'Duplicate key "%s"' % key)
189            expr[key] = self.get_expr(True)
190            if self.tok == '}':
191                self.accept()
192                return expr
193            if self.tok != ',':
194                raise QAPISchemaError(self, 'Expected "," or "}"')
195            self.accept()
196            if self.tok != "'":
197                raise QAPISchemaError(self, 'Expected string')
198
199    def get_values(self):
200        expr = []
201        if self.tok == ']':
202            self.accept()
203            return expr
204        if not self.tok in [ '{', '[', "'" ]:
205            raise QAPISchemaError(self, 'Expected "{", "[", "]" or string')
206        while True:
207            expr.append(self.get_expr(True))
208            if self.tok == ']':
209                self.accept()
210                return expr
211            if self.tok != ',':
212                raise QAPISchemaError(self, 'Expected "," or "]"')
213            self.accept()
214
215    def get_expr(self, nested):
216        if self.tok != '{' and not nested:
217            raise QAPISchemaError(self, 'Expected "{"')
218        if self.tok == '{':
219            self.accept()
220            expr = self.get_members()
221        elif self.tok == '[':
222            self.accept()
223            expr = self.get_values()
224        elif self.tok == "'":
225            expr = self.val
226            self.accept()
227        else:
228            raise QAPISchemaError(self, 'Expected "{", "[" or string')
229        return expr
230
231def find_base_fields(base):
232    base_struct_define = find_struct(base)
233    if not base_struct_define:
234        return None
235    return base_struct_define['data']
236
237# Return the discriminator enum define if discriminator is specified as an
238# enum type, otherwise return None.
239def discriminator_find_enum_define(expr):
240    base = expr.get('base')
241    discriminator = expr.get('discriminator')
242
243    if not (discriminator and base):
244        return None
245
246    base_fields = find_base_fields(base)
247    if not base_fields:
248        return None
249
250    discriminator_type = base_fields.get(discriminator)
251    if not discriminator_type:
252        return None
253
254    return find_enum(discriminator_type)
255
256def check_event(expr, expr_info):
257    params = expr.get('data')
258    if params:
259        for argname, argentry, optional, structured in parse_args(params):
260            if structured:
261                raise QAPIExprError(expr_info,
262                                    "Nested structure define in event is not "
263                                    "supported, event '%s', argname '%s'"
264                                    % (expr['event'], argname))
265
266def check_union(expr, expr_info):
267    name = expr['union']
268    base = expr.get('base')
269    discriminator = expr.get('discriminator')
270    members = expr['data']
271
272    # If the object has a member 'base', its value must name a complex type.
273    if base:
274        base_fields = find_base_fields(base)
275        if not base_fields:
276            raise QAPIExprError(expr_info,
277                                "Base '%s' is not a valid type"
278                                % base)
279
280    # If the union object has no member 'discriminator', it's an
281    # ordinary union.
282    if not discriminator:
283        enum_define = None
284
285    # Else if the value of member 'discriminator' is {}, it's an
286    # anonymous union.
287    elif discriminator == {}:
288        enum_define = None
289
290    # Else, it's a flat union.
291    else:
292        # The object must have a member 'base'.
293        if not base:
294            raise QAPIExprError(expr_info,
295                                "Flat union '%s' must have a base field"
296                                % name)
297        # The value of member 'discriminator' must name a member of the
298        # base type.
299        discriminator_type = base_fields.get(discriminator)
300        if not discriminator_type:
301            raise QAPIExprError(expr_info,
302                                "Discriminator '%s' is not a member of base "
303                                "type '%s'"
304                                % (discriminator, base))
305        enum_define = find_enum(discriminator_type)
306        # Do not allow string discriminator
307        if not enum_define:
308            raise QAPIExprError(expr_info,
309                                "Discriminator '%s' must be of enumeration "
310                                "type" % discriminator)
311
312    # Check every branch
313    for (key, value) in members.items():
314        # If this named member's value names an enum type, then all members
315        # of 'data' must also be members of the enum type.
316        if enum_define and not key in enum_define['enum_values']:
317            raise QAPIExprError(expr_info,
318                                "Discriminator value '%s' is not found in "
319                                "enum '%s'" %
320                                (key, enum_define["enum_name"]))
321        # Todo: add checking for values. Key is checked as above, value can be
322        # also checked here, but we need more functions to handle array case.
323
324def check_exprs(schema):
325    for expr_elem in schema.exprs:
326        expr = expr_elem['expr']
327        if 'union' in expr:
328            check_union(expr, expr_elem['info'])
329        if 'event' in expr:
330            check_event(expr, expr_elem['info'])
331
332def parse_schema(input_file):
333    try:
334        schema = QAPISchema(open(input_file, "r"))
335    except (QAPISchemaError, QAPIExprError) as e:
336        print >>sys.stderr, e
337        exit(1)
338
339    exprs = []
340
341    for expr_elem in schema.exprs:
342        expr = expr_elem['expr']
343        if 'enum' in expr:
344            add_enum(expr['enum'], expr['data'])
345        elif 'union' in expr:
346            add_union(expr)
347        elif 'type' in expr:
348            add_struct(expr)
349        exprs.append(expr)
350
351    # Try again for hidden UnionKind enum
352    for expr_elem in schema.exprs:
353        expr = expr_elem['expr']
354        if 'union' in expr:
355            if not discriminator_find_enum_define(expr):
356                add_enum('%sKind' % expr['union'])
357
358    try:
359        check_exprs(schema)
360    except QAPIExprError as e:
361        print >>sys.stderr, e
362        exit(1)
363
364    return exprs
365
366def parse_args(typeinfo):
367    if isinstance(typeinfo, basestring):
368        struct = find_struct(typeinfo)
369        assert struct != None
370        typeinfo = struct['data']
371
372    for member in typeinfo:
373        argname = member
374        argentry = typeinfo[member]
375        optional = False
376        structured = False
377        if member.startswith('*'):
378            argname = member[1:]
379            optional = True
380        if isinstance(argentry, OrderedDict):
381            structured = True
382        yield (argname, argentry, optional, structured)
383
384def de_camel_case(name):
385    new_name = ''
386    for ch in name:
387        if ch.isupper() and new_name:
388            new_name += '_'
389        if ch == '-':
390            new_name += '_'
391        else:
392            new_name += ch.lower()
393    return new_name
394
395def camel_case(name):
396    new_name = ''
397    first = True
398    for ch in name:
399        if ch in ['_', '-']:
400            first = True
401        elif first:
402            new_name += ch.upper()
403            first = False
404        else:
405            new_name += ch.lower()
406    return new_name
407
408def c_var(name, protect=True):
409    # ANSI X3J11/88-090, 3.1.1
410    c89_words = set(['auto', 'break', 'case', 'char', 'const', 'continue',
411                     'default', 'do', 'double', 'else', 'enum', 'extern', 'float',
412                     'for', 'goto', 'if', 'int', 'long', 'register', 'return',
413                     'short', 'signed', 'sizeof', 'static', 'struct', 'switch',
414                     'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'])
415    # ISO/IEC 9899:1999, 6.4.1
416    c99_words = set(['inline', 'restrict', '_Bool', '_Complex', '_Imaginary'])
417    # ISO/IEC 9899:2011, 6.4.1
418    c11_words = set(['_Alignas', '_Alignof', '_Atomic', '_Generic', '_Noreturn',
419                     '_Static_assert', '_Thread_local'])
420    # GCC http://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/C-Extensions.html
421    # excluding _.*
422    gcc_words = set(['asm', 'typeof'])
423    # C++ ISO/IEC 14882:2003 2.11
424    cpp_words = set(['bool', 'catch', 'class', 'const_cast', 'delete',
425                     'dynamic_cast', 'explicit', 'false', 'friend', 'mutable',
426                     'namespace', 'new', 'operator', 'private', 'protected',
427                     'public', 'reinterpret_cast', 'static_cast', 'template',
428                     'this', 'throw', 'true', 'try', 'typeid', 'typename',
429                     'using', 'virtual', 'wchar_t',
430                     # alternative representations
431                     'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not',
432                     'not_eq', 'or', 'or_eq', 'xor', 'xor_eq'])
433    # namespace pollution:
434    polluted_words = set(['unix', 'errno'])
435    if protect and (name in c89_words | c99_words | c11_words | gcc_words | cpp_words | polluted_words):
436        return "q_" + name
437    return name.replace('-', '_').lstrip("*")
438
439def c_fun(name, protect=True):
440    return c_var(name, protect).replace('.', '_')
441
442def c_list_type(name):
443    return '%sList' % name
444
445def type_name(name):
446    if type(name) == list:
447        return c_list_type(name[0])
448    return name
449
450enum_types = []
451struct_types = []
452union_types = []
453
454def add_struct(definition):
455    global struct_types
456    struct_types.append(definition)
457
458def find_struct(name):
459    global struct_types
460    for struct in struct_types:
461        if struct['type'] == name:
462            return struct
463    return None
464
465def add_union(definition):
466    global union_types
467    union_types.append(definition)
468
469def find_union(name):
470    global union_types
471    for union in union_types:
472        if union['union'] == name:
473            return union
474    return None
475
476def add_enum(name, enum_values = None):
477    global enum_types
478    enum_types.append({"enum_name": name, "enum_values": enum_values})
479
480def find_enum(name):
481    global enum_types
482    for enum in enum_types:
483        if enum['enum_name'] == name:
484            return enum
485    return None
486
487def is_enum(name):
488    return find_enum(name) != None
489
490eatspace = '\033EATSPACE.'
491
492# A special suffix is added in c_type() for pointer types, and it's
493# stripped in mcgen(). So please notice this when you check the return
494# value of c_type() outside mcgen().
495def c_type(name, is_param=False):
496    if name == 'str':
497        if is_param:
498            return 'const char *' + eatspace
499        return 'char *' + eatspace
500
501    elif name == 'int':
502        return 'int64_t'
503    elif (name == 'int8' or name == 'int16' or name == 'int32' or
504          name == 'int64' or name == 'uint8' or name == 'uint16' or
505          name == 'uint32' or name == 'uint64'):
506        return name + '_t'
507    elif name == 'size':
508        return 'uint64_t'
509    elif name == 'bool':
510        return 'bool'
511    elif name == 'number':
512        return 'double'
513    elif type(name) == list:
514        return '%s *%s' % (c_list_type(name[0]), eatspace)
515    elif is_enum(name):
516        return name
517    elif name == None or len(name) == 0:
518        return 'void'
519    elif name == name.upper():
520        return '%sEvent *%s' % (camel_case(name), eatspace)
521    else:
522        return '%s *%s' % (name, eatspace)
523
524def is_c_ptr(name):
525    suffix = "*" + eatspace
526    return c_type(name).endswith(suffix)
527
528def genindent(count):
529    ret = ""
530    for i in range(count):
531        ret += " "
532    return ret
533
534indent_level = 0
535
536def push_indent(indent_amount=4):
537    global indent_level
538    indent_level += indent_amount
539
540def pop_indent(indent_amount=4):
541    global indent_level
542    indent_level -= indent_amount
543
544def cgen(code, **kwds):
545    indent = genindent(indent_level)
546    lines = code.split('\n')
547    lines = map(lambda x: indent + x, lines)
548    return '\n'.join(lines) % kwds + '\n'
549
550def mcgen(code, **kwds):
551    raw = cgen('\n'.join(code.split('\n')[1:-1]), **kwds)
552    return re.sub(re.escape(eatspace) + ' *', '', raw)
553
554def basename(filename):
555    return filename.split("/")[-1]
556
557def guardname(filename):
558    guard = basename(filename).rsplit(".", 1)[0]
559    for substr in [".", " ", "-"]:
560        guard = guard.replace(substr, "_")
561    return guard.upper() + '_H'
562
563def guardstart(name):
564    return mcgen('''
565
566#ifndef %(name)s
567#define %(name)s
568
569''',
570                 name=guardname(name))
571
572def guardend(name):
573    return mcgen('''
574
575#endif /* %(name)s */
576
577''',
578                 name=guardname(name))
579
580# ENUMName -> ENUM_NAME, EnumName1 -> ENUM_NAME1
581# ENUM_NAME -> ENUM_NAME, ENUM_NAME1 -> ENUM_NAME1, ENUM_Name2 -> ENUM_NAME2
582# ENUM24_Name -> ENUM24_NAME
583def _generate_enum_string(value):
584    c_fun_str = c_fun(value, False)
585    if value.isupper():
586        return c_fun_str
587
588    new_name = ''
589    l = len(c_fun_str)
590    for i in range(l):
591        c = c_fun_str[i]
592        # When c is upper and no "_" appears before, do more checks
593        if c.isupper() and (i > 0) and c_fun_str[i - 1] != "_":
594            # Case 1: next string is lower
595            # Case 2: previous string is digit
596            if (i < (l - 1) and c_fun_str[i + 1].islower()) or \
597            c_fun_str[i - 1].isdigit():
598                new_name += '_'
599        new_name += c
600    return new_name.lstrip('_').upper()
601
602def generate_enum_full_value(enum_name, enum_value):
603    abbrev_string = _generate_enum_string(enum_name)
604    value_string = _generate_enum_string(enum_value)
605    return "%s_%s" % (abbrev_string, value_string)
606