blob: 720486f06c90e68a25dc74bff61955411eb33e16 [file] [log] [blame]
Wenchao Xia21cd70d2014-06-18 08:43:28 +02001#
2# QAPI event generator
3#
4# Copyright (c) 2014 Wenchao Xia
Markus Armbruster05f43a92015-09-16 13:06:14 +02005# Copyright (c) 2015 Red Hat Inc.
Wenchao Xia21cd70d2014-06-18 08:43:28 +02006#
7# Authors:
8# Wenchao Xia <wenchaoqemu@gmail.com>
Markus Armbruster05f43a92015-09-16 13:06:14 +02009# Markus Armbruster <armbru@redhat.com>
Wenchao Xia21cd70d2014-06-18 08:43:28 +020010#
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
Wenchao Xia21cd70d2014-06-18 08:43:28 +020014from qapi import *
Wenchao Xia21cd70d2014-06-18 08:43:28 +020015
Markus Armbrustere98859a2015-09-16 13:06:16 +020016
17def gen_event_send_proto(name, arg_type):
Markus Armbruster03b43672015-09-16 13:06:20 +020018 return 'void qapi_event_send_%(c_name)s(%(param)s)' % {
19 'c_name': c_name(name.lower()),
20 'param': gen_params(arg_type, 'Error **errp')}
Wenchao Xia21cd70d2014-06-18 08:43:28 +020021
22
Markus Armbrustere98859a2015-09-16 13:06:16 +020023def gen_event_send_decl(name, arg_type):
Wenchao Xia21cd70d2014-06-18 08:43:28 +020024 return mcgen('''
25
Markus Armbrustere98859a2015-09-16 13:06:16 +020026%(proto)s;
Wenchao Xia21cd70d2014-06-18 08:43:28 +020027''',
Markus Armbrustere98859a2015-09-16 13:06:16 +020028 proto=gen_event_send_proto(name, arg_type))
Wenchao Xia21cd70d2014-06-18 08:43:28 +020029
Wenchao Xia21cd70d2014-06-18 08:43:28 +020030
Markus Armbrustere98859a2015-09-16 13:06:16 +020031def gen_event_send(name, arg_type):
32 ret = mcgen('''
33
34%(proto)s
Wenchao Xia21cd70d2014-06-18 08:43:28 +020035{
36 QDict *qmp;
Eric Blake2a0f50e2015-09-29 16:21:08 -060037 Error *err = NULL;
Wenchao Xia21cd70d2014-06-18 08:43:28 +020038 QMPEventFuncEmit emit;
Markus Armbrustere98859a2015-09-16 13:06:16 +020039''',
40 proto=gen_event_send_proto(name, arg_type))
Wenchao Xia21cd70d2014-06-18 08:43:28 +020041
Markus Armbrustere98859a2015-09-16 13:06:16 +020042 if arg_type and arg_type.members:
43 ret += mcgen('''
Wenchao Xia21cd70d2014-06-18 08:43:28 +020044 QmpOutputVisitor *qov;
45 Visitor *v;
46 QObject *obj;
47
Markus Armbrustere98859a2015-09-16 13:06:16 +020048''')
Wenchao Xia21cd70d2014-06-18 08:43:28 +020049
Markus Armbrustere98859a2015-09-16 13:06:16 +020050 ret += mcgen('''
Wenchao Xia21cd70d2014-06-18 08:43:28 +020051 emit = qmp_event_get_func_emit();
52 if (!emit) {
53 return;
54 }
55
Markus Armbrustere98859a2015-09-16 13:06:16 +020056 qmp = qmp_event_build_dict("%(name)s");
Wenchao Xia21cd70d2014-06-18 08:43:28 +020057
Markus Armbrustere98859a2015-09-16 13:06:16 +020058''',
59 name=name)
Wenchao Xia21cd70d2014-06-18 08:43:28 +020060
Markus Armbrustere98859a2015-09-16 13:06:16 +020061 if arg_type and arg_type.members:
62 ret += mcgen('''
Wenchao Xia21cd70d2014-06-18 08:43:28 +020063 qov = qmp_output_visitor_new();
64 g_assert(qov);
65
66 v = qmp_output_get_visitor(qov);
67 g_assert(v);
68
69 /* Fake visit, as if all members are under a structure */
Eric Blake2a0f50e2015-09-29 16:21:08 -060070 visit_start_struct(v, NULL, "", "%(name)s", 0, &err);
Markus Armbrustere98859a2015-09-16 13:06:16 +020071''',
72 name=name)
Eric Blake1f353342015-09-29 16:21:13 -060073 ret += gen_err_check()
Eric Blake82ca8e42015-09-29 16:21:14 -060074 ret += gen_visit_fields(arg_type.members, need_cast=True)
Markus Armbrustere98859a2015-09-16 13:06:16 +020075 ret += mcgen('''
Eric Blake2a0f50e2015-09-29 16:21:08 -060076 visit_end_struct(v, &err);
77 if (err) {
Eric Blakef7823992015-09-29 16:21:10 -060078 goto out;
Wenchao Xia21cd70d2014-06-18 08:43:28 +020079 }
80
81 obj = qmp_output_get_qobject(qov);
82 g_assert(obj != NULL);
83
84 qdict_put_obj(qmp, "data", obj);
Markus Armbrustere98859a2015-09-16 13:06:16 +020085''')
Wenchao Xia21cd70d2014-06-18 08:43:28 +020086
Markus Armbrustere98859a2015-09-16 13:06:16 +020087 ret += mcgen('''
Eric Blake2a0f50e2015-09-29 16:21:08 -060088 emit(%(c_enum)s, qmp, &err);
Wenchao Xia21cd70d2014-06-18 08:43:28 +020089
Markus Armbrustere98859a2015-09-16 13:06:16 +020090''',
91 c_enum=c_enum_const(event_enum_name, name))
Wenchao Xia21cd70d2014-06-18 08:43:28 +020092
Markus Armbrustere98859a2015-09-16 13:06:16 +020093 if arg_type and arg_type.members:
94 ret += mcgen('''
Eric Blakef7823992015-09-29 16:21:10 -060095out:
Wenchao Xia21cd70d2014-06-18 08:43:28 +020096 qmp_output_visitor_cleanup(qov);
Markus Armbrustere98859a2015-09-16 13:06:16 +020097''')
98 ret += mcgen('''
Eric Blake2a0f50e2015-09-29 16:21:08 -060099 error_propagate(errp, err);
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200100 QDECREF(qmp);
101}
Markus Armbrustere98859a2015-09-16 13:06:16 +0200102''')
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200103 return ret
104
Markus Armbruster05f43a92015-09-16 13:06:14 +0200105
106class QAPISchemaGenEventVisitor(QAPISchemaVisitor):
107 def __init__(self):
108 self.decl = None
109 self.defn = None
110 self._event_names = None
111
112 def visit_begin(self, schema):
113 self.decl = ''
114 self.defn = ''
115 self._event_names = []
116
117 def visit_end(self):
Markus Armbrustere98859a2015-09-16 13:06:16 +0200118 self.decl += gen_enum(event_enum_name, self._event_names)
119 self.defn += gen_enum_lookup(event_enum_name, self._event_names)
Markus Armbruster05f43a92015-09-16 13:06:14 +0200120 self._event_names = None
121
122 def visit_event(self, name, info, arg_type):
Markus Armbrustere98859a2015-09-16 13:06:16 +0200123 self.decl += gen_event_send_decl(name, arg_type)
124 self.defn += gen_event_send(name, arg_type)
Markus Armbruster05f43a92015-09-16 13:06:14 +0200125 self._event_names.append(name)
126
127
Markus Armbruster2114f5a2015-04-02 13:12:21 +0200128(input_file, output_dir, do_c, do_h, prefix, dummy) = parse_command_line()
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200129
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200130c_comment = '''
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200131/*
132 * schema-defined QAPI event functions
133 *
134 * Copyright (c) 2014 Wenchao Xia
135 *
136 * Authors:
137 * Wenchao Xia <wenchaoqemu@gmail.com>
138 *
139 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
140 * See the COPYING.LIB file in the top-level directory.
141 *
142 */
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200143'''
144h_comment = '''
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200145/*
146 * schema-defined QAPI event functions
147 *
148 * Copyright (c) 2014 Wenchao Xia
149 *
150 * Authors:
151 * Wenchao Xia <wenchaoqemu@gmail.com>
152 *
153 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
154 * See the COPYING.LIB file in the top-level directory.
155 *
156 */
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200157'''
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200158
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200159(fdef, fdecl) = open_output(output_dir, do_c, do_h, prefix,
160 'qapi-event.c', 'qapi-event.h',
161 c_comment, h_comment)
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200162
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200163fdef.write(mcgen('''
164#include "qemu-common.h"
165#include "%(prefix)sqapi-event.h"
166#include "%(prefix)sqapi-visit.h"
167#include "qapi/qmp-output-visitor.h"
168#include "qapi/qmp-event.h"
169
170''',
171 prefix=prefix))
172
173fdecl.write(mcgen('''
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200174#include "qapi/error.h"
175#include "qapi/qmp/qdict.h"
176#include "%(prefix)sqapi-types.h"
177
178''',
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200179 prefix=prefix))
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200180
Markus Armbruster016a3352015-07-01 12:59:40 +0200181event_enum_name = c_name(prefix + "QAPIEvent", protect=False)
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200182
Markus Armbruster05f43a92015-09-16 13:06:14 +0200183schema = QAPISchema(input_file)
184gen = QAPISchemaGenEventVisitor()
185schema.visit(gen)
186fdef.write(gen.defn)
187fdecl.write(gen.decl)
Wenchao Xia21cd70d2014-06-18 08:43:28 +0200188
Markus Armbruster12f8e1b2015-04-02 14:46:39 +0200189close_output(fdef, fdecl)