qapi-types.py 10.1 KB
Newer Older
1 2 3 4 5 6 7 8
#
# QAPI types generator
#
# Copyright IBM, Corp. 2011
#
# Authors:
#  Anthony Liguori <aliguori@us.ibm.com>
#
9 10
# This work is licensed under the terms of the GNU GPL, version 2.
# See the COPYING file in the top-level directory.
11 12 13 14

from ordereddict import OrderedDict
from qapi import *

15 16
def generate_fwd_builtin(name):
    return mcgen('''
17 18 19

typedef struct %(name)sList
{
20 21 22 23
    union {
        %(type)s value;
        uint64_t padding;
    };
24 25 26
    struct %(name)sList *next;
} %(name)sList;
''',
27 28
                 type=c_type(name),
                 name=name)
29

30
def generate_fwd_struct(name):
31
    return mcgen('''
32

33 34 35 36
typedef struct %(name)s %(name)s;

typedef struct %(name)sList
{
37 38 39 40
    union {
        %(name)s *value;
        uint64_t padding;
    };
41 42 43
    struct %(name)sList *next;
} %(name)sList;
''',
E
Eric Blake 已提交
44
                 name=c_name(name))
45

46
def generate_fwd_enum_struct(name):
47 48 49
    return mcgen('''
typedef struct %(name)sList
{
50 51 52 53
    union {
        %(name)s value;
        uint64_t padding;
    };
54 55 56
    struct %(name)sList *next;
} %(name)sList;
''',
E
Eric Blake 已提交
57
                 name=c_name(name))
58

59 60
def generate_struct_fields(members):
    ret = ''
61

62
    for argname, argentry, optional in parse_args(members):
63 64 65 66
        if optional:
            ret += mcgen('''
    bool has_%(c_name)s;
''',
67
                         c_name=c_name(argname))
68
        ret += mcgen('''
69 70
    %(c_type)s %(c_name)s;
''',
71
                     c_type=c_type(argentry), c_name=c_name(argname))
72

73 74
    return ret

75 76
def generate_struct(expr):

77
    structname = expr.get('struct', "")
78
    members = expr['data']
79
    base = expr.get('base')
80

81 82 83 84
    ret = mcgen('''
struct %(name)s
{
''',
E
Eric Blake 已提交
85
          name=c_name(structname))
86

87 88 89
    if base:
        ret += generate_struct_fields({'base': base})

90 91
    ret += generate_struct_fields(members)

92 93 94 95 96 97 98 99
    # Make sure that all structs have at least one field; this avoids
    # potential issues with attempting to malloc space for zero-length structs
    # in C, and also incompatibility with C++ (where an empty struct is size 1).
    if not base and not members:
            ret += mcgen('''
    char qapi_dummy_field_for_empty_struct;
''')

100
    ret += mcgen('''
101 102
};
''')
103 104 105 106 107

    return ret

def generate_enum_lookup(name, values):
    ret = mcgen('''
108
const char * const %(name)s_lookup[] = {
109
''',
E
Eric Blake 已提交
110
                name=c_name(name))
111 112
    i = 0
    for value in values:
113
        index = c_enum_const(name, value)
114
        ret += mcgen('''
115
    [%(index)s] = "%(value)s",
116
''',
117
                     index = index, value = value)
118

119
    max_index = c_enum_const(name, 'MAX')
120
    ret += mcgen('''
121
    [%(max_index)s] = NULL,
122 123
};

124 125
''',
        max_index=max_index)
126 127 128
    return ret

def generate_enum(name, values):
E
Eric Blake 已提交
129
    name = c_name(name)
130
    lookup_decl = mcgen('''
131
extern const char * const %(name)s_lookup[];
132 133 134 135 136 137 138 139 140
''',
                name=name)

    enum_decl = mcgen('''
typedef enum %(name)s
{
''',
                name=name)

141 142 143
    # append automatically generated _MAX value
    enum_values = values + [ 'MAX' ]

144
    i = 0
145
    for value in enum_values:
146
        enum_full_value = c_enum_const(name, value)
147
        enum_decl += mcgen('''
148
    %(enum_full_value)s = %(i)d,
149
''',
150
                     enum_full_value = enum_full_value,
151 152 153 154 155 156 157 158 159 160
                     i=i)
        i += 1

    enum_decl += mcgen('''
} %(name)s;
''',
                 name=name)

    return lookup_decl + enum_decl

161
def generate_alternate_qtypes(expr):
K
Kevin Wolf 已提交
162

163
    name = expr['alternate']
K
Kevin Wolf 已提交
164 165 166 167 168
    members = expr['data']

    ret = mcgen('''
const int %(name)s_qtypes[QTYPE_MAX] = {
''',
E
Eric Blake 已提交
169
                name=c_name(name))
K
Kevin Wolf 已提交
170 171

    for key in members:
172
        qtype = find_alternate_member_qtype(members[key])
173
        assert qtype, "Invalid alternate member"
K
Kevin Wolf 已提交
174 175

        ret += mcgen('''
E
Eric Blake 已提交
176
    [%(qtype)s] = %(enum_const)s,
K
Kevin Wolf 已提交
177
''',
E
Eric Blake 已提交
178 179
                     qtype = qtype,
                     enum_const = c_enum_const(name + 'Kind', key))
K
Kevin Wolf 已提交
180 181 182 183 184 185 186

    ret += mcgen('''
};
''')
    return ret


187
def generate_union(expr, meta):
188

189
    name = c_name(expr[meta])
190
    typeinfo = expr['data']
191

192
    base = expr.get('base')
193
    discriminator = expr.get('discriminator')
194

195 196 197 198 199 200
    enum_define = discriminator_find_enum_define(expr)
    if enum_define:
        discriminator_type_name = enum_define['enum_name']
    else:
        discriminator_type_name = '%sKind' % (name)

201 202 203
    ret = mcgen('''
struct %(name)s
{
204
    %(discriminator_type_name)s kind;
205
    union {
206
        void *data;
207
''',
208
                name=name,
209
                discriminator_type_name=c_name(discriminator_type_name))
210 211 212 213 214 215

    for key in typeinfo:
        ret += mcgen('''
        %(c_type)s %(c_name)s;
''',
                     c_type=c_type(typeinfo[key]),
216
                     c_name=c_name(key))
217 218 219

    ret += mcgen('''
    };
220 221 222
''')

    if base:
223 224 225
        assert discriminator
        base_fields = find_struct(base)['data'].copy()
        del base_fields[discriminator]
226 227 228
        ret += generate_struct_fields(base_fields)
    else:
        assert not discriminator
229 230

    ret += mcgen('''
231 232
};
''')
233
    if meta == 'alternate':
K
Kevin Wolf 已提交
234 235 236 237 238
        ret += mcgen('''
extern const int %(name)s_qtypes[];
''',
            name=name)

239 240 241 242 243

    return ret

def generate_type_cleanup_decl(name):
    ret = mcgen('''
E
Eric Blake 已提交
244
void qapi_free_%(name)s(%(c_type)s obj);
245
''',
E
Eric Blake 已提交
246
                c_type=c_type(name), name=c_name(name))
247 248 249 250
    return ret

def generate_type_cleanup(name):
    ret = mcgen('''
251

E
Eric Blake 已提交
252
void qapi_free_%(name)s(%(c_type)s obj)
253 254 255 256 257 258 259 260 261 262
{
    QapiDeallocVisitor *md;
    Visitor *v;

    if (!obj) {
        return;
    }

    md = qapi_dealloc_visitor_new();
    v = qapi_dealloc_get_visitor(md);
E
Eric Blake 已提交
263
    visit_type_%(name)s(v, &obj, NULL, NULL);
264 265 266
    qapi_dealloc_visitor_cleanup(md);
}
''',
E
Eric Blake 已提交
267
                c_type=c_type(name), name=c_name(name))
268 269
    return ret

270
do_builtins = False
271

272 273 274
(input_file, output_dir, do_c, do_h, prefix, opts) = \
    parse_command_line("b", ["builtins"])

275
for o, a in opts:
276
    if o in ("-b", "--builtins"):
277
        do_builtins = True
278

279
c_comment = '''
280 281 282 283 284 285 286 287 288 289 290 291 292
/*
 * deallocation functions for schema-defined QAPI types
 *
 * Copyright IBM, Corp. 2011
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *  Michael Roth      <mdroth@linux.vnet.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.
 *
 */
293 294
'''
h_comment = '''
295 296 297 298 299 300 301 302 303 304 305 306
/*
 * schema-defined QAPI types
 *
 * 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.
 *
 */
307
'''
308

309 310 311
(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
                            'qapi-types.c', 'qapi-types.h',
                            c_comment, h_comment)
312

313 314 315 316 317 318 319 320 321
fdef.write(mcgen('''
#include "qapi/dealloc-visitor.h"
#include "%(prefix)sqapi-types.h"
#include "%(prefix)sqapi-visit.h"

''',
                 prefix=prefix))

fdecl.write(mcgen('''
322 323
#include <stdbool.h>
#include <stdint.h>
324

325
'''))
326

327
exprs = parse_schema(input_file)
328

329
fdecl.write(guardstart("QAPI_TYPES_BUILTIN_STRUCT_DECL"))
330
for typename in builtin_types.keys():
331
    fdecl.write(generate_fwd_builtin(typename))
332 333
fdecl.write(guardend("QAPI_TYPES_BUILTIN_STRUCT_DECL"))

334 335
for expr in exprs:
    ret = "\n"
336
    if expr.has_key('struct'):
337
        ret += generate_fwd_struct(expr['struct'])
338
    elif expr.has_key('enum'):
339
        ret += generate_enum(expr['enum'], expr['data']) + "\n"
340
        ret += generate_fwd_enum_struct(expr['enum'])
341 342
        fdef.write(generate_enum_lookup(expr['enum'], expr['data']))
    elif expr.has_key('union'):
343
        ret += generate_fwd_struct(expr['union']) + "\n"
344 345 346 347 348
        enum_define = discriminator_find_enum_define(expr)
        if not enum_define:
            ret += generate_enum('%sKind' % expr['union'], expr['data'].keys())
            fdef.write(generate_enum_lookup('%sKind' % expr['union'],
                                            expr['data'].keys()))
349
    elif expr.has_key('alternate'):
350
        ret += generate_fwd_struct(expr['alternate']) + "\n"
351 352 353 354
        ret += generate_enum('%sKind' % expr['alternate'], expr['data'].keys())
        fdef.write(generate_enum_lookup('%sKind' % expr['alternate'],
                                        expr['data'].keys()))
        fdef.write(generate_alternate_qtypes(expr))
355 356 357 358
    else:
        continue
    fdecl.write(ret)

359 360 361
# to avoid header dependency hell, we always generate declarations
# for built-in types in our header files and simply guard them
fdecl.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))
362
for typename in builtin_types.keys():
363 364 365 366 367 368 369 370
    fdecl.write(generate_type_cleanup_decl(typename + "List"))
fdecl.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DECL"))

# ...this doesn't work for cases where we link in multiple objects that
# have the functions defined, so we use -b option to provide control
# over these cases
if do_builtins:
    fdef.write(guardstart("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))
371
    for typename in builtin_types.keys():
372 373 374
        fdef.write(generate_type_cleanup(typename + "List"))
    fdef.write(guardend("QAPI_TYPES_BUILTIN_CLEANUP_DEF"))

375 376
for expr in exprs:
    ret = "\n"
377
    if expr.has_key('struct'):
378
        ret += generate_struct(expr) + "\n"
379 380 381 382
        ret += generate_type_cleanup_decl(expr['struct'] + "List")
        fdef.write(generate_type_cleanup(expr['struct'] + "List") + "\n")
        ret += generate_type_cleanup_decl(expr['struct'])
        fdef.write(generate_type_cleanup(expr['struct']) + "\n")
383
    elif expr.has_key('union'):
384
        ret += generate_union(expr, 'union')
385 386 387 388
        ret += generate_type_cleanup_decl(expr['union'] + "List")
        fdef.write(generate_type_cleanup(expr['union'] + "List") + "\n")
        ret += generate_type_cleanup_decl(expr['union'])
        fdef.write(generate_type_cleanup(expr['union']) + "\n")
389 390 391 392 393 394
    elif expr.has_key('alternate'):
        ret += generate_union(expr, 'alternate')
        ret += generate_type_cleanup_decl(expr['alternate'] + "List")
        fdef.write(generate_type_cleanup(expr['alternate'] + "List") + "\n")
        ret += generate_type_cleanup_decl(expr['alternate'])
        fdef.write(generate_type_cleanup(expr['alternate']) + "\n")
395 396 397
    elif expr.has_key('enum'):
        ret += generate_type_cleanup_decl(expr['enum'] + "List")
        fdef.write(generate_type_cleanup(expr['enum'] + "List") + "\n")
398 399 400 401
    else:
        continue
    fdecl.write(ret)

402
close_output(fdef, fdecl)