Statistics
| Branch: | Revision:

root / scripts / qapi-visit.py @ 8d3bc517

History | View | Annotate | Download (6.5 kB)

1
#
2
# QAPI visitor generator
3
#
4
# Copyright IBM, Corp. 2011
5
#
6
# Authors:
7
#  Anthony Liguori <aliguori@us.ibm.com>
8
#  Michael Roth    <mdroth@linux.vnet.ibm.com>
9
#
10
# This work is licensed under the terms of the GNU GPLv2.
11
# See the COPYING.LIB file in the top-level directory.
12

    
13
from ordereddict import OrderedDict
14
from qapi import *
15
import sys
16
import os
17
import getopt
18
import errno
19

    
20
def generate_visit_struct_body(field_prefix, members):
21
    ret = ""
22
    if len(field_prefix):
23
        field_prefix = field_prefix + "."
24
    for argname, argentry, optional, structured in parse_args(members):
25
        if optional:
26
            ret += mcgen('''
27
visit_start_optional(m, (obj && *obj) ? &(*obj)->%(c_prefix)shas_%(c_name)s : NULL, "%(name)s", errp);
28
if ((*obj)->%(prefix)shas_%(c_name)s) {
29
''',
30
                         c_prefix=c_var(field_prefix), prefix=field_prefix,
31
                         c_name=c_var(argname), name=argname)
32
            push_indent()
33

    
34
        if structured:
35
            ret += mcgen('''
36
visit_start_struct(m, NULL, "", "%(name)s", 0, errp);
37
''',
38
                         name=argname)
39
            ret += generate_visit_struct_body(field_prefix + argname, argentry)
40
            ret += mcgen('''
41
visit_end_struct(m, errp);
42
''')
43
        else:
44
            ret += mcgen('''
45
visit_type_%(type)s(m, (obj && *obj) ? &(*obj)->%(c_prefix)s%(c_name)s : NULL, "%(name)s", errp);
46
''',
47
                         c_prefix=c_var(field_prefix), prefix=field_prefix,
48
                         type=type_name(argentry), c_name=c_var(argname),
49
                         name=argname)
50

    
51
        if optional:
52
            pop_indent()
53
            ret += mcgen('''
54
}
55
visit_end_optional(m, errp);
56
''')
57
    return ret
58

    
59
def generate_visit_struct(name, members):
60
    ret = mcgen('''
61

62
void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
63
{
64
    visit_start_struct(m, (void **)obj, "%(name)s", name, sizeof(%(name)s), errp);
65
''',
66
                name=name)
67
    push_indent()
68
    ret += generate_visit_struct_body("", members)
69
    pop_indent()
70

    
71
    ret += mcgen('''
72
    visit_end_struct(m, errp);
73
}
74
''')
75
    return ret
76

    
77
def generate_visit_list(name, members):
78
    return mcgen('''
79

80
void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp)
81
{
82
    GenericList *i, **head = (GenericList **)obj;
83

84
    visit_start_list(m, name, errp);
85

86
    for (*head = i = visit_next_list(m, head, errp); i; i = visit_next_list(m, &i, errp)) {
87
        %(name)sList *native_i = (%(name)sList *)i;
88
        visit_type_%(name)s(m, &native_i->value, NULL, errp);
89
    }
90

91
    visit_end_list(m, errp);
92
}
93
''',
94
                name=name)
95

    
96
def generate_visit_enum(name, members):
97
    return mcgen('''
98

99
void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp)
100
{
101
    visit_type_enum(m, (int *)obj, %(name)s_lookup, "%(name)s", name, errp);
102
}
103
''',
104
                 name=name)
105

    
106
def generate_visit_union(name, members):
107
    ret = generate_visit_enum('%sKind' % name, members.keys())
108

    
109
    ret += mcgen('''
110

111
void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
112
{
113
}
114
''',
115
                 name=name)
116

    
117
    return ret
118

    
119
def generate_declaration(name, members, genlist=True):
120
    ret = mcgen('''
121

122
void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp);
123
''',
124
                name=name)
125

    
126
    if genlist:
127
        ret += mcgen('''
128
void visit_type_%(name)sList(Visitor *m, %(name)sList ** obj, const char *name, Error **errp);
129
''',
130
                 name=name)
131

    
132
    return ret
133

    
134
def generate_decl_enum(name, members, genlist=True):
135
    return mcgen('''
136

137
void visit_type_%(name)s(Visitor *m, %(name)s * obj, const char *name, Error **errp);
138
''',
139
                name=name)
140

    
141
try:
142
    opts, args = getopt.gnu_getopt(sys.argv[1:], "chp:o:",
143
                                   ["source", "header", "prefix=", "output-dir="])
144
except getopt.GetoptError, err:
145
    print str(err)
146
    sys.exit(1)
147

    
148
output_dir = ""
149
prefix = ""
150
c_file = 'qapi-visit.c'
151
h_file = 'qapi-visit.h'
152

    
153
do_c = False
154
do_h = False
155

    
156
for o, a in opts:
157
    if o in ("-p", "--prefix"):
158
        prefix = a
159
    elif o in ("-o", "--output-dir"):
160
        output_dir = a + "/"
161
    elif o in ("-c", "--source"):
162
        do_h = True
163
    elif o in ("-h", "--header"):
164
        do_c = True
165

    
166
if not do_c and not do_h:
167
    do_c = True
168
    do_h = True
169

    
170
c_file = output_dir + prefix + c_file
171
h_file = output_dir + prefix + h_file
172

    
173
try:
174
    os.makedirs(output_dir)
175
except os.error, e:
176
    if e.errno != errno.EEXIST:
177
        raise
178

    
179
def maybe_open(really, name, opt):
180
    class Null(object):
181
        def write(self, str):
182
            pass
183
        def read(self):
184
            return ''
185
    if really:
186
        return open(name, opt)
187

    
188
fdef = maybe_open(do_c, c_file, 'w')
189
fdecl = maybe_open(do_h, h_file, 'w')
190

    
191
fdef.write(mcgen('''
192
/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
193

194
/*
195
 * schema-defined QAPI visitor functions
196
 *
197
 * Copyright IBM, Corp. 2011
198
 *
199
 * Authors:
200
 *  Anthony Liguori   <aliguori@us.ibm.com>
201
 *
202
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
203
 * See the COPYING.LIB file in the top-level directory.
204
 *
205
 */
206

207
#include "%(header)s"
208
''',
209
                 header=basename(h_file)))
210

    
211
fdecl.write(mcgen('''
212
/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
213

214
/*
215
 * schema-defined QAPI visitor function
216
 *
217
 * Copyright IBM, Corp. 2011
218
 *
219
 * Authors:
220
 *  Anthony Liguori   <aliguori@us.ibm.com>
221
 *
222
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
223
 * See the COPYING.LIB file in the top-level directory.
224
 *
225
 */
226

227
#ifndef %(guard)s
228
#define %(guard)s
229

230
#include "qapi/qapi-visit-core.h"
231
#include "%(prefix)sqapi-types.h"
232
''',
233
                  prefix=prefix, guard=guardname(h_file)))
234

    
235
exprs = parse_schema(sys.stdin)
236

    
237
for expr in exprs:
238
    if expr.has_key('type'):
239
        ret = generate_visit_struct(expr['type'], expr['data'])
240
        ret += generate_visit_list(expr['type'], expr['data'])
241
        fdef.write(ret)
242

    
243
        ret = generate_declaration(expr['type'], expr['data'])
244
        fdecl.write(ret)
245
    elif expr.has_key('union'):
246
        ret = generate_visit_union(expr['union'], expr['data'])
247
        fdef.write(ret)
248

    
249
        ret = generate_decl_enum('%sKind' % expr['union'], expr['data'].keys())
250
        ret += generate_declaration(expr['union'], expr['data'])
251
        fdecl.write(ret)
252
    elif expr.has_key('enum'):
253
        ret = generate_visit_enum(expr['enum'], expr['data'])
254
        fdef.write(ret)
255

    
256
        ret = generate_decl_enum(expr['enum'], expr['data'])
257
        fdecl.write(ret)
258

    
259
fdecl.write('''
260
#endif
261
''')
262

    
263
fdecl.flush()
264
fdecl.close()
265

    
266
fdef.flush()
267
fdef.close()