qapi-visit.py 11.6 KB
Newer Older
1 2 3 4
#
# QAPI visitor generator
#
# Copyright IBM, Corp. 2011
E
Eric Blake 已提交
5
# Copyright (C) 2014-2015 Red Hat, Inc.
6 7 8 9
#
# Authors:
#  Anthony Liguori <aliguori@us.ibm.com>
#  Michael Roth    <mdroth@linux.vnet.ibm.com>
10
#  Markus Armbruster <armbru@redhat.com>
11
#
12 13
# This work is licensed under the terms of the GNU GPL, version 2.
# See the COPYING file in the top-level directory.
14 15

from qapi import *
16
import re
17

18 19
# visit_type_FOO_implicit() is emitted as needed; track if it has already
# been output.
20
implicit_structs_seen = set()
21 22 23

# visit_type_FOO_fields() is always emitted; track if a forward declaration
# or implementation has already been output.
24
struct_fields_seen = set()
25

26

27 28 29 30 31
def gen_visit_decl(name, scalar=False):
    c_type = c_name(name) + ' *'
    if not scalar:
        c_type += '*'
    return mcgen('''
32
void visit_type_%(c_name)s(Visitor *v, %(c_type)sobj, const char *name, Error **errp);
33 34 35 36
''',
                 c_name=c_name(name), c_type=c_type)


37
def gen_visit_fields_decl(typ):
38
    ret = ''
39
    if typ.name not in struct_fields_seen:
40 41
        ret += mcgen('''

42
static void visit_type_%(c_type)s_fields(Visitor *v, %(c_type)s **obj, Error **errp);
43
''',
44
                     c_type=typ.c_name())
45 46 47 48 49 50 51 52 53 54
        struct_fields_seen.add(typ.name)
    return ret


def gen_visit_implicit_struct(typ):
    if typ in implicit_structs_seen:
        return ''
    implicit_structs_seen.add(typ)

    ret = gen_visit_fields_decl(typ)
55 56

    ret += mcgen('''
57

58
static void visit_type_implicit_%(c_type)s(Visitor *v, %(c_type)s **obj, Error **errp)
59 60 61
{
    Error *err = NULL;

62
    visit_start_implicit_struct(v, (void **)obj, sizeof(%(c_type)s), &err);
63
    if (!err) {
64 65
        visit_type_%(c_type)s_fields(v, obj, errp);
        visit_end_implicit_struct(v, &err);
66 67 68 69
    }
    error_propagate(errp, err);
}
''',
70
                 c_type=typ.c_name())
71
    return ret
72

73 74

def gen_visit_struct_fields(name, base, members):
75 76
    struct_fields_seen.add(name)

77
    ret = ''
78

79
    if base:
80
        ret += gen_visit_implicit_struct(base)
81

82 83
    ret += mcgen('''

84
static void visit_type_%(c_name)s_fields(Visitor *v, %(c_name)s **obj, Error **errp)
85 86
{
    Error *err = NULL;
M
Markus Armbruster 已提交
87

88
''',
89
                 c_name=c_name(name))
P
Paolo Bonzini 已提交
90

91 92
    if base:
        ret += mcgen('''
93
    visit_type_implicit_%(c_type)s(v, &(*obj)->%(c_name)s, &err);
94
''',
95
                     c_type=base.c_name(), c_name=c_name('base'))
E
Eric Blake 已提交
96
        ret += gen_err_check()
97

E
Eric Blake 已提交
98
    ret += gen_visit_fields(members, prefix='(*obj)->')
P
Paolo Bonzini 已提交
99

100 101
    # 'goto out' produced for base, and by gen_visit_fields() for each member
    if base or members:
102
        ret += mcgen('''
103

104 105 106
out:
''')
    ret += mcgen('''
107 108 109
    error_propagate(errp, err);
}
''')
110 111 112
    return ret


113 114 115
def gen_visit_struct(name, base, members):
    ret = gen_visit_struct_fields(name, base, members)

116 117 118 119
    # FIXME: if *obj is NULL on entry, and visit_start_struct() assigns to
    # *obj, but then visit_type_FOO_fields() fails, we should clean up *obj
    # rather than leaving it non-NULL. As currently written, the caller must
    # call qapi_free_FOO() to avoid a memory leak of the partial FOO.
120 121
    ret += mcgen('''

122
void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
123
{
124 125
    Error *err = NULL;

126
    visit_start_struct(v, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
127 128
    if (!err) {
        if (*obj) {
129
            visit_type_%(c_name)s_fields(v, obj, errp);
130
        }
131
        visit_end_struct(v, &err);
132
    }
133
    error_propagate(errp, err);
134
}
135
''',
136
                 name=name, c_name=c_name(name))
137 138 139

    return ret

140

141
def gen_visit_list(name, element_type):
142 143
    return mcgen('''

144
void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
145
{
P
Paolo Bonzini 已提交
146
    Error *err = NULL;
147
    GenericList *i, **prev;
148

149
    visit_start_list(v, name, &err);
150 151 152 153 154
    if (err) {
        goto out;
    }

    for (prev = (GenericList **)obj;
155
         !err && (i = visit_next_list(v, prev, &err)) != NULL;
156
         prev = &i) {
157
        %(c_name)s *native_i = (%(c_name)s *)i;
158
        visit_type_%(c_elt_type)s(v, &native_i->value, NULL, &err);
159
    }
160 161 162

    error_propagate(errp, err);
    err = NULL;
163
    visit_end_list(v, &err);
164 165
out:
    error_propagate(errp, err);
166 167
}
''',
168
                 c_name=c_name(name), c_elt_type=element_type.c_name())
169

170 171

def gen_visit_enum(name):
172 173
    return mcgen('''

174
void visit_type_%(c_name)s(Visitor *v, %(c_name)s *obj, const char *name, Error **errp)
175
{
176
    visit_type_enum(v, (int *)obj, %(c_name)s_lookup, "%(name)s", name, errp);
177 178
}
''',
179
                 c_name=c_name(name), name=name)
180

181

182
def gen_visit_alternate(name, variants):
K
Kevin Wolf 已提交
183 184
    ret = mcgen('''

185
void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
K
Kevin Wolf 已提交
186 187 188
{
    Error *err = NULL;

189
    visit_start_implicit_struct(v, (void**) obj, sizeof(%(c_name)s), &err);
190 191 192
    if (err) {
        goto out;
    }
193
    visit_get_next_type(v, (int*) &(*obj)->kind, %(c_name)s_qtypes, name, &err);
194
    if (err) {
195
        goto out_obj;
196 197
    }
    switch ((*obj)->kind) {
K
Kevin Wolf 已提交
198
''',
199
                c_name=c_name(name))
K
Kevin Wolf 已提交
200

201
    for var in variants.variants:
K
Kevin Wolf 已提交
202
        ret += mcgen('''
203
    case %(case)s:
204
        visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, name, &err);
205
        break;
K
Kevin Wolf 已提交
206
''',
207 208 209 210
                     case=c_enum_const(variants.tag_member.type.name,
                                       var.name),
                     c_type=var.type.c_name(),
                     c_name=c_name(var.name))
K
Kevin Wolf 已提交
211 212

    ret += mcgen('''
213 214
    default:
        abort();
K
Kevin Wolf 已提交
215
    }
216
out_obj:
217 218
    error_propagate(errp, err);
    err = NULL;
219
    visit_end_implicit_struct(v, &err);
220 221
out:
    error_propagate(errp, err);
K
Kevin Wolf 已提交
222 223 224 225 226
}
''')

    return ret

227

228 229
def gen_visit_union(name, base, variants):
    ret = ''
230

231
    if base:
232
        members = [m for m in base.members if m != variants.tag_member]
233
        ret += gen_visit_struct_fields(name, None, members)
234

235 236 237
    for var in variants.variants:
        # Ugly special case for simple union TODO get rid of it
        if not var.simple_union_type():
238
            ret += gen_visit_implicit_struct(var.type)
239

240 241
    ret += mcgen('''

242
void visit_type_%(c_name)s(Visitor *v, %(c_name)s **obj, const char *name, Error **errp)
243
{
244 245
    Error *err = NULL;

246
    visit_start_struct(v, (void **)obj, "%(name)s", name, sizeof(%(c_name)s), &err);
247 248 249
    if (err) {
        goto out;
    }
250 251 252
    if (!*obj) {
        goto out_obj;
    }
253
''',
254
                 c_name=c_name(name), name=name)
255

256
    if base:
257
        ret += mcgen('''
258
    visit_type_%(c_name)s_fields(v, obj, &err);
259
''',
260
                     c_name=c_name(name))
E
Eric Blake 已提交
261
        ret += gen_err_check(label='out_obj')
262

263
    tag_key = variants.tag_member.name
264 265
    if not variants.tag_name:
        # we pointlessly use a different key for simple unions
266
        tag_key = 'type'
267
    ret += mcgen('''
268 269 270 271 272 273 274 275
    visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, "%(name)s", &err);
    if (err) {
        goto out_obj;
    }
    if (!visit_start_union(v, !!(*obj)->data, &err) || err) {
        goto out_obj;
    }
    switch ((*obj)->%(c_name)s) {
276
''',
277
                 c_type=variants.tag_member.type.c_name(),
278 279 280 281
                 # TODO ugly special case for simple union
                 # Use same tag name in C as on the wire to get rid of
                 # it, then: c_name=c_name(variants.tag_member.name)
                 c_name=c_name(variants.tag_name or 'kind'),
282
                 name=tag_key)
283

284 285 286
    for var in variants.variants:
        # TODO ugly special case for simple union
        simple_union_type = var.simple_union_type()
287
        ret += mcgen('''
288
    case %(case)s:
289 290 291
''',
                     case=c_enum_const(variants.tag_member.type.name,
                                       var.name))
292
        if simple_union_type:
293
            ret += mcgen('''
294
        visit_type_%(c_type)s(v, &(*obj)->%(c_name)s, "data", &err);
295 296 297
''',
                         c_type=simple_union_type.c_name(),
                         c_name=c_name(var.name))
298
        else:
299
            ret += mcgen('''
300
        visit_type_implicit_%(c_type)s(v, &(*obj)->%(c_name)s, &err);
301 302 303
''',
                         c_type=var.type.c_name(),
                         c_name=c_name(var.name))
304
        ret += mcgen('''
305
        break;
306
''')
307 308

    ret += mcgen('''
309 310
    default:
        abort();
311
    }
312 313 314
out_obj:
    error_propagate(errp, err);
    err = NULL;
315 316 317
    if (*obj) {
        visit_end_union(v, !!(*obj)->data, &err);
    }
318 319
    error_propagate(errp, err);
    err = NULL;
320
    visit_end_struct(v, &err);
321 322
out:
    error_propagate(errp, err);
323 324 325
}
''')

326 327
    return ret

328

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
class QAPISchemaGenVisitVisitor(QAPISchemaVisitor):
    def __init__(self):
        self.decl = None
        self.defn = None
        self._btin = None

    def visit_begin(self, schema):
        self.decl = ''
        self.defn = ''
        self._btin = guardstart('QAPI_VISIT_BUILTIN')

    def visit_end(self):
        # To avoid header dependency hell, we always generate
        # declarations for built-in types in our header files and
        # simply guard them.  See also do_builtins (command line
        # option -b).
        self._btin += guardend('QAPI_VISIT_BUILTIN')
        self.decl = self._btin + self.decl
        self._btin = None

349 350
    def visit_needed(self, entity):
        # Visit everything except implicit objects
351 352
        return not (entity.is_implicit() and
                    isinstance(entity, QAPISchemaObjectType))
353

354 355
    def visit_enum_type(self, name, info, values, prefix):
        self.decl += gen_visit_decl(name, scalar=True)
356
        self.defn += gen_visit_enum(name)
357 358 359 360 361 362 363 364 365 366 367 368 369

    def visit_array_type(self, name, info, element_type):
        decl = gen_visit_decl(name)
        defn = gen_visit_list(name, element_type)
        if isinstance(element_type, QAPISchemaBuiltinType):
            self._btin += decl
            if do_builtins:
                self.defn += defn
        else:
            self.decl += decl
            self.defn += defn

    def visit_object_type(self, name, info, base, members, variants):
370 371 372 373 374 375
        self.decl += gen_visit_decl(name)
        if variants:
            assert not members      # not implemented
            self.defn += gen_visit_union(name, base, variants)
        else:
            self.defn += gen_visit_struct(name, base, members)
376 377 378 379 380 381 382 383 384

    def visit_alternate_type(self, name, info, variants):
        self.decl += gen_visit_decl(name)
        self.defn += gen_visit_alternate(name, variants)

# If you link code generated from multiple schemata, you want only one
# instance of the code for built-in types.  Generate it only when
# do_builtins, enabled by command line option -b.  See also
# QAPISchemaGenVisitVisitor.visit_end().
385
do_builtins = False
386

387 388 389
(input_file, output_dir, do_c, do_h, prefix, opts) = \
    parse_command_line("b", ["builtins"])

390
for o, a in opts:
391
    if o in ("-b", "--builtins"):
392
        do_builtins = True
393

394
c_comment = '''
395 396 397 398 399 400 401 402 403 404 405 406
/*
 * schema-defined QAPI visitor functions
 *
 * Copyright IBM, Corp. 2011
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
 * See the COPYING.LIB file in the top-level directory.
 *
 */
407 408
'''
h_comment = '''
409
/*
410
 * schema-defined QAPI visitor functions
411 412 413 414 415 416 417 418 419 420
 *
 * Copyright IBM, Corp. 2011
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
 * See the COPYING.LIB file in the top-level directory.
 *
 */
421 422 423 424 425
'''

(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
                            'qapi-visit.c', 'qapi-visit.h',
                            c_comment, h_comment)
426

427 428 429 430
fdef.write(mcgen('''
#include "qemu-common.h"
#include "%(prefix)sqapi-visit.h"
''',
431
                 prefix=prefix))
432

433
fdecl.write(mcgen('''
434
#include "qapi/visitor.h"
435
#include "%(prefix)sqapi-types.h"
436

437
''',
438
                  prefix=prefix))
439

440 441 442 443 444
schema = QAPISchema(input_file)
gen = QAPISchemaGenVisitVisitor()
schema.visit(gen)
fdef.write(gen.defn)
fdecl.write(gen.decl)
445

446
close_output(fdef, fdecl)