Markus Armbruster | 5ddeec8 | 2018-02-11 10:35:41 +0100 | [diff] [blame] | 1 | """ |
| 2 | QAPI command marshaller generator |
| 3 | |
| 4 | Copyright IBM, Corp. 2011 |
| 5 | Copyright (C) 2014-2018 Red Hat, Inc. |
| 6 | |
| 7 | Authors: |
| 8 | Anthony Liguori <aliguori@us.ibm.com> |
| 9 | Michael Roth <mdroth@linux.vnet.ibm.com> |
| 10 | Markus Armbruster <armbru@redhat.com> |
| 11 | |
| 12 | This work is licensed under the terms of the GNU GPL, version 2. |
| 13 | See the COPYING file in the top-level directory. |
| 14 | """ |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 15 | |
Markus Armbruster | fb0bc83 | 2018-02-26 13:48:58 -0600 | [diff] [blame] | 16 | from qapi.common import * |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 17 | |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 18 | |
Eric Blake | 48825ca | 2016-07-13 21:50:19 -0600 | [diff] [blame] | 19 | def gen_command_decl(name, arg_type, boxed, ret_type): |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 20 | return mcgen(''' |
Markus Armbruster | 03b4367 | 2015-09-16 13:06:20 +0200 | [diff] [blame] | 21 | %(c_type)s qmp_%(c_name)s(%(params)s); |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 22 | ''', |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 23 | c_type=(ret_type and ret_type.c_type()) or 'void', |
| 24 | c_name=c_name(name), |
Marc-André Lureau | 086ee7a | 2017-06-01 16:41:41 +0400 | [diff] [blame] | 25 | params=build_params(arg_type, boxed, 'Error **errp')) |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 26 | |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 27 | |
Eric Blake | 48825ca | 2016-07-13 21:50:19 -0600 | [diff] [blame] | 28 | def gen_call(name, arg_type, boxed, ret_type): |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 29 | ret = '' |
| 30 | |
| 31 | argstr = '' |
Eric Blake | 48825ca | 2016-07-13 21:50:19 -0600 | [diff] [blame] | 32 | if boxed: |
Eric Blake | c818408 | 2016-07-13 21:50:20 -0600 | [diff] [blame] | 33 | assert arg_type and not arg_type.is_empty() |
| 34 | argstr = '&arg, ' |
Eric Blake | 48825ca | 2016-07-13 21:50:19 -0600 | [diff] [blame] | 35 | elif arg_type: |
Eric Blake | 29f6bd1 | 2016-03-17 16:48:26 -0600 | [diff] [blame] | 36 | assert not arg_type.variants |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 37 | for memb in arg_type.members: |
Markus Armbruster | ee44602 | 2015-09-16 13:06:11 +0200 | [diff] [blame] | 38 | if memb.optional: |
Eric Blake | 386230a | 2016-03-17 16:48:33 -0600 | [diff] [blame] | 39 | argstr += 'arg.has_%s, ' % c_name(memb.name) |
| 40 | argstr += 'arg.%s, ' % c_name(memb.name) |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 41 | |
| 42 | lhs = '' |
| 43 | if ret_type: |
| 44 | lhs = 'retval = ' |
| 45 | |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 46 | ret = mcgen(''' |
Markus Armbruster | f153801 | 2015-09-16 13:06:18 +0200 | [diff] [blame] | 47 | |
Eric Blake | 05372f7 | 2015-09-29 16:21:12 -0600 | [diff] [blame] | 48 | %(lhs)sqmp_%(c_name)s(%(args)s&err); |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 49 | ''', |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 50 | c_name=c_name(name), args=argstr, lhs=lhs) |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 51 | if ret_type: |
Markus Armbruster | e02bca2 | 2015-06-27 17:21:12 +0200 | [diff] [blame] | 52 | ret += mcgen(''' |
Eric Blake | fa274ed | 2016-07-13 21:50:17 -0600 | [diff] [blame] | 53 | if (err) { |
| 54 | goto out; |
| 55 | } |
Markus Armbruster | e02bca2 | 2015-06-27 17:21:12 +0200 | [diff] [blame] | 56 | |
Eric Blake | 05372f7 | 2015-09-29 16:21:12 -0600 | [diff] [blame] | 57 | qmp_marshal_output_%(c_name)s(retval, ret, &err); |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 58 | ''', |
Markus Armbruster | 56d92b0 | 2015-09-16 13:06:21 +0200 | [diff] [blame] | 59 | c_name=ret_type.c_name()) |
Markus Armbruster | 1f9a7a1 | 2015-06-27 17:49:34 +0200 | [diff] [blame] | 60 | return ret |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 61 | |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 62 | |
Markus Armbruster | 56d92b0 | 2015-09-16 13:06:21 +0200 | [diff] [blame] | 63 | def gen_marshal_output(ret_type): |
Markus Armbruster | f153801 | 2015-09-16 13:06:18 +0200 | [diff] [blame] | 64 | return mcgen(''' |
Markus Armbruster | ee44602 | 2015-09-16 13:06:11 +0200 | [diff] [blame] | 65 | |
Markus Armbruster | 56d92b0 | 2015-09-16 13:06:21 +0200 | [diff] [blame] | 66 | static void qmp_marshal_output_%(c_name)s(%(c_type)s ret_in, QObject **ret_out, Error **errp) |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 67 | { |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 68 | Error *err = NULL; |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 69 | Visitor *v; |
| 70 | |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 71 | v = qobject_output_visitor_new(ret_out); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 72 | visit_type_%(c_name)s(v, "unused", &ret_in, &err); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 73 | if (!err) { |
| 74 | visit_complete(v, ret_out); |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 75 | } |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 76 | error_propagate(errp, err); |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 77 | visit_free(v); |
| 78 | v = qapi_dealloc_visitor_new(); |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 79 | visit_type_%(c_name)s(v, "unused", &ret_in, NULL); |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 80 | visit_free(v); |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 81 | } |
| 82 | ''', |
Markus Armbruster | 56d92b0 | 2015-09-16 13:06:21 +0200 | [diff] [blame] | 83 | c_type=ret_type.c_type(), c_name=ret_type.c_name()) |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 84 | |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 85 | |
Marc-André Lureau | 086ee7a | 2017-06-01 16:41:41 +0400 | [diff] [blame] | 86 | def build_marshal_proto(name): |
Markus Armbruster | c261394 | 2017-03-15 13:57:35 +0100 | [diff] [blame] | 87 | return ('void qmp_marshal_%s(QDict *args, QObject **ret, Error **errp)' |
| 88 | % c_name(name)) |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 89 | |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 90 | |
Markus Armbruster | f153801 | 2015-09-16 13:06:18 +0200 | [diff] [blame] | 91 | def gen_marshal_decl(name): |
| 92 | return mcgen(''' |
| 93 | %(proto)s; |
| 94 | ''', |
Marc-André Lureau | 086ee7a | 2017-06-01 16:41:41 +0400 | [diff] [blame] | 95 | proto=build_marshal_proto(name)) |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 96 | |
Markus Armbruster | f153801 | 2015-09-16 13:06:18 +0200 | [diff] [blame] | 97 | |
Eric Blake | 48825ca | 2016-07-13 21:50:19 -0600 | [diff] [blame] | 98 | def gen_marshal(name, arg_type, boxed, ret_type): |
Marc-André Lureau | a0067da | 2016-09-12 13:19:08 +0400 | [diff] [blame] | 99 | have_args = arg_type and not arg_type.is_empty() |
| 100 | |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 101 | ret = mcgen(''' |
Markus Armbruster | ee44602 | 2015-09-16 13:06:11 +0200 | [diff] [blame] | 102 | |
Markus Armbruster | f153801 | 2015-09-16 13:06:18 +0200 | [diff] [blame] | 103 | %(proto)s |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 104 | { |
Eric Blake | c1ff0e6 | 2016-03-17 16:48:34 -0600 | [diff] [blame] | 105 | Error *err = NULL; |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 106 | ''', |
Marc-André Lureau | 086ee7a | 2017-06-01 16:41:41 +0400 | [diff] [blame] | 107 | proto=build_marshal_proto(name)) |
Anthony Liguori | 776574d | 2011-09-02 12:34:46 -0500 | [diff] [blame] | 108 | |
Eric Blake | c1ff0e6 | 2016-03-17 16:48:34 -0600 | [diff] [blame] | 109 | if ret_type: |
| 110 | ret += mcgen(''' |
| 111 | %(c_type)s retval; |
| 112 | ''', |
| 113 | c_type=ret_type.c_type()) |
| 114 | |
Marc-André Lureau | a0067da | 2016-09-12 13:19:08 +0400 | [diff] [blame] | 115 | if have_args: |
| 116 | visit_members = ('visit_type_%s_members(v, &arg, &err);' |
| 117 | % arg_type.c_name()) |
Eric Blake | c1ff0e6 | 2016-03-17 16:48:34 -0600 | [diff] [blame] | 118 | ret += mcgen(''' |
Eric Blake | c1ff0e6 | 2016-03-17 16:48:34 -0600 | [diff] [blame] | 119 | Visitor *v; |
| 120 | %(c_name)s arg = {0}; |
| 121 | |
Marc-André Lureau | a0067da | 2016-09-12 13:19:08 +0400 | [diff] [blame] | 122 | ''', |
| 123 | c_name=arg_type.c_name()) |
| 124 | else: |
| 125 | visit_members = '' |
| 126 | ret += mcgen(''' |
| 127 | Visitor *v = NULL; |
| 128 | |
| 129 | if (args) { |
| 130 | ''') |
| 131 | push_indent() |
| 132 | |
| 133 | ret += mcgen(''' |
Markus Armbruster | 048abb7 | 2017-03-03 13:32:39 +0100 | [diff] [blame] | 134 | v = qobject_input_visitor_new(QOBJECT(args)); |
Eric Blake | ed84153 | 2016-04-28 15:45:16 -0600 | [diff] [blame] | 135 | visit_start_struct(v, NULL, NULL, 0, &err); |
| 136 | if (err) { |
| 137 | goto out; |
| 138 | } |
Marc-André Lureau | a0067da | 2016-09-12 13:19:08 +0400 | [diff] [blame] | 139 | %(visit_members)s |
Eric Blake | 15c2f66 | 2016-04-28 15:45:27 -0600 | [diff] [blame] | 140 | if (!err) { |
| 141 | visit_check_struct(v, &err); |
| 142 | } |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 143 | visit_end_struct(v, NULL); |
Eric Blake | c1ff0e6 | 2016-03-17 16:48:34 -0600 | [diff] [blame] | 144 | if (err) { |
| 145 | goto out; |
| 146 | } |
| 147 | ''', |
Marc-André Lureau | a0067da | 2016-09-12 13:19:08 +0400 | [diff] [blame] | 148 | visit_members=visit_members) |
Eric Blake | c1ff0e6 | 2016-03-17 16:48:34 -0600 | [diff] [blame] | 149 | |
Marc-André Lureau | a0067da | 2016-09-12 13:19:08 +0400 | [diff] [blame] | 150 | if not have_args: |
| 151 | pop_indent() |
Eric Blake | c1ff0e6 | 2016-03-17 16:48:34 -0600 | [diff] [blame] | 152 | ret += mcgen(''' |
Marc-André Lureau | a0067da | 2016-09-12 13:19:08 +0400 | [diff] [blame] | 153 | } |
Eric Blake | c1ff0e6 | 2016-03-17 16:48:34 -0600 | [diff] [blame] | 154 | ''') |
| 155 | |
Eric Blake | 48825ca | 2016-07-13 21:50:19 -0600 | [diff] [blame] | 156 | ret += gen_call(name, arg_type, boxed, ret_type) |
Markus Armbruster | 1f9a7a1 | 2015-06-27 17:49:34 +0200 | [diff] [blame] | 157 | |
Marc-André Lureau | a0067da | 2016-09-12 13:19:08 +0400 | [diff] [blame] | 158 | ret += mcgen(''' |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 159 | |
| 160 | out: |
Eric Blake | 2a0f50e | 2015-09-29 16:21:08 -0600 | [diff] [blame] | 161 | error_propagate(errp, err); |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 162 | visit_free(v); |
Marc-André Lureau | a0067da | 2016-09-12 13:19:08 +0400 | [diff] [blame] | 163 | ''') |
| 164 | |
| 165 | if have_args: |
| 166 | visit_members = ('visit_type_%s_members(v, &arg, NULL);' |
| 167 | % arg_type.c_name()) |
| 168 | else: |
| 169 | visit_members = '' |
| 170 | ret += mcgen(''' |
| 171 | if (args) { |
| 172 | ''') |
| 173 | push_indent() |
| 174 | |
| 175 | ret += mcgen(''' |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 176 | v = qapi_dealloc_visitor_new(); |
Eric Blake | ed84153 | 2016-04-28 15:45:16 -0600 | [diff] [blame] | 177 | visit_start_struct(v, NULL, NULL, 0, NULL); |
Marc-André Lureau | a0067da | 2016-09-12 13:19:08 +0400 | [diff] [blame] | 178 | %(visit_members)s |
Eric Blake | 1158bb2 | 2016-06-09 10:48:34 -0600 | [diff] [blame] | 179 | visit_end_struct(v, NULL); |
Eric Blake | 2c0ef9f | 2016-06-09 10:48:35 -0600 | [diff] [blame] | 180 | visit_free(v); |
Eric Blake | c1ff0e6 | 2016-03-17 16:48:34 -0600 | [diff] [blame] | 181 | ''', |
Marc-André Lureau | a0067da | 2016-09-12 13:19:08 +0400 | [diff] [blame] | 182 | visit_members=visit_members) |
| 183 | |
| 184 | if not have_args: |
| 185 | pop_indent() |
| 186 | ret += mcgen(''' |
| 187 | } |
| 188 | ''') |
Eric Blake | c1ff0e6 | 2016-03-17 16:48:34 -0600 | [diff] [blame] | 189 | |
Markus Armbruster | 1f9a7a1 | 2015-06-27 17:49:34 +0200 | [diff] [blame] | 190 | ret += mcgen(''' |
Markus Armbruster | 485febc | 2015-03-13 17:25:50 +0100 | [diff] [blame] | 191 | } |
Markus Armbruster | 1f9a7a1 | 2015-06-27 17:49:34 +0200 | [diff] [blame] | 192 | ''') |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 193 | return ret |
| 194 | |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 195 | |
Igor Mammedov | d6fe3d0 | 2018-05-11 18:51:43 +0200 | [diff] [blame] | 196 | def gen_register_command(name, success_response, allow_oob, allow_preconfig): |
Peter Xu | 876c675 | 2018-03-09 17:00:00 +0800 | [diff] [blame] | 197 | options = [] |
| 198 | |
Markus Armbruster | ee44602 | 2015-09-16 13:06:11 +0200 | [diff] [blame] | 199 | if not success_response: |
Peter Xu | 876c675 | 2018-03-09 17:00:00 +0800 | [diff] [blame] | 200 | options += ['QCO_NO_SUCCESS_RESP'] |
| 201 | if allow_oob: |
| 202 | options += ['QCO_ALLOW_OOB'] |
Igor Mammedov | d6fe3d0 | 2018-05-11 18:51:43 +0200 | [diff] [blame] | 203 | if allow_preconfig: |
| 204 | options += ['QCO_ALLOW_PRECONFIG'] |
Peter Xu | 876c675 | 2018-03-09 17:00:00 +0800 | [diff] [blame] | 205 | |
| 206 | if not options: |
| 207 | options = ['QCO_NO_OPTIONS'] |
| 208 | |
| 209 | options = " | ".join(options) |
Luiz Capitulino | d34b867 | 2012-05-08 14:24:44 -0300 | [diff] [blame] | 210 | |
Markus Armbruster | ee44602 | 2015-09-16 13:06:11 +0200 | [diff] [blame] | 211 | ret = mcgen(''' |
Markus Armbruster | c261394 | 2017-03-15 13:57:35 +0100 | [diff] [blame] | 212 | qmp_register_command(cmds, "%(name)s", |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 213 | qmp_marshal_%(c_name)s, %(opts)s); |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 214 | ''', |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 215 | name=name, c_name=c_name(name), |
| 216 | opts=options) |
Markus Armbruster | ee44602 | 2015-09-16 13:06:11 +0200 | [diff] [blame] | 217 | return ret |
| 218 | |
Markus Armbruster | e98859a | 2015-09-16 13:06:16 +0200 | [diff] [blame] | 219 | |
Markus Armbruster | 93b564c | 2018-02-11 10:35:44 +0100 | [diff] [blame] | 220 | def gen_registry(registry, prefix): |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 221 | ret = mcgen(''' |
Markus Armbruster | ee44602 | 2015-09-16 13:06:11 +0200 | [diff] [blame] | 222 | |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 223 | void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds) |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 224 | { |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 225 | QTAILQ_INIT(cmds); |
| 226 | |
| 227 | ''', |
| 228 | c_prefix=c_name(prefix, protect=False)) |
Markus Armbruster | 1f9a7a1 | 2015-06-27 17:49:34 +0200 | [diff] [blame] | 229 | ret += registry |
| 230 | ret += mcgen(''' |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 231 | } |
Markus Armbruster | 1f9a7a1 | 2015-06-27 17:49:34 +0200 | [diff] [blame] | 232 | ''') |
Michael Roth | c17d990 | 2011-07-19 14:50:42 -0500 | [diff] [blame] | 233 | return ret |
| 234 | |
Markus Armbruster | ee44602 | 2015-09-16 13:06:11 +0200 | [diff] [blame] | 235 | |
Markus Armbruster | 252dc31 | 2018-02-11 10:36:00 +0100 | [diff] [blame] | 236 | class QAPISchemaGenCommandVisitor(QAPISchemaModularCVisitor): |
Markus Armbruster | ee44602 | 2015-09-16 13:06:11 +0200 | [diff] [blame] | 237 | |
Markus Armbruster | 71b3f04 | 2018-02-26 13:50:08 -0600 | [diff] [blame] | 238 | def __init__(self, prefix): |
Markus Armbruster | 252dc31 | 2018-02-11 10:36:00 +0100 | [diff] [blame] | 239 | QAPISchemaModularCVisitor.__init__( |
| 240 | self, prefix, 'qapi-commands', |
Markus Armbruster | 71b3f04 | 2018-02-26 13:50:08 -0600 | [diff] [blame] | 241 | ' * Schema-defined QAPI/QMP commands', __doc__) |
Markus Armbruster | dddee4d | 2019-03-01 16:40:47 +0100 | [diff] [blame] | 242 | self._regy = QAPIGenCCode(None) |
Markus Armbruster | 252dc31 | 2018-02-11 10:36:00 +0100 | [diff] [blame] | 243 | self._visited_ret_types = {} |
| 244 | |
Markus Armbruster | dcac647 | 2019-02-14 16:22:36 +0100 | [diff] [blame] | 245 | def _begin_user_module(self, name): |
Markus Armbruster | 252dc31 | 2018-02-11 10:36:00 +0100 | [diff] [blame] | 246 | self._visited_ret_types[self._genc] = set() |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 247 | commands = self._module_basename('qapi-commands', name) |
| 248 | types = self._module_basename('qapi-types', name) |
| 249 | visit = self._module_basename('qapi-visit', name) |
Markus Armbruster | 71b3f04 | 2018-02-26 13:50:08 -0600 | [diff] [blame] | 250 | self._genc.add(mcgen(''' |
Eric Blake | 9167ebd | 2016-02-08 08:36:46 -0700 | [diff] [blame] | 251 | #include "qemu/osdep.h" |
Markus Armbruster | 4180978 | 2015-04-02 14:52:55 +0200 | [diff] [blame] | 252 | #include "qemu-common.h" |
| 253 | #include "qemu/module.h" |
Markus Armbruster | 4180978 | 2015-04-02 14:52:55 +0200 | [diff] [blame] | 254 | #include "qapi/visitor.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 255 | #include "qapi/qmp/qdict.h" |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 256 | #include "qapi/qobject-output-visitor.h" |
| 257 | #include "qapi/qobject-input-visitor.h" |
Markus Armbruster | 4180978 | 2015-04-02 14:52:55 +0200 | [diff] [blame] | 258 | #include "qapi/dealloc-visitor.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 259 | #include "qapi/error.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 260 | #include "%(visit)s.h" |
| 261 | #include "%(commands)s.h" |
Markus Armbruster | 4180978 | 2015-04-02 14:52:55 +0200 | [diff] [blame] | 262 | |
| 263 | ''', |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 264 | commands=commands, visit=visit)) |
Markus Armbruster | 71b3f04 | 2018-02-26 13:50:08 -0600 | [diff] [blame] | 265 | self._genh.add(mcgen(''' |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 266 | #include "%(types)s.h" |
Markus Armbruster | 1527bad | 2017-03-03 13:32:25 +0100 | [diff] [blame] | 267 | #include "qapi/qmp/dispatch.h" |
Markus Armbruster | 4180978 | 2015-04-02 14:52:55 +0200 | [diff] [blame] | 268 | |
| 269 | ''', |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 270 | types=types)) |
Markus Armbruster | 72aaa73 | 2015-04-02 11:41:22 +0200 | [diff] [blame] | 271 | |
Markus Armbruster | 71b3f04 | 2018-02-26 13:50:08 -0600 | [diff] [blame] | 272 | def visit_end(self): |
Markus Armbruster | 252dc31 | 2018-02-11 10:36:00 +0100 | [diff] [blame] | 273 | (genc, genh) = self._module[self._main_module] |
| 274 | genh.add(mcgen(''' |
| 275 | void %(c_prefix)sqmp_init_marshal(QmpCommandList *cmds); |
| 276 | ''', |
| 277 | c_prefix=c_name(self._prefix, protect=False))) |
Marc-André Lureau | 1f7b9f3 | 2018-07-03 17:56:43 +0200 | [diff] [blame] | 278 | genc.add(gen_registry(self._regy.get_content(), self._prefix)) |
Markus Armbruster | 71b3f04 | 2018-02-26 13:50:08 -0600 | [diff] [blame] | 279 | |
Marc-André Lureau | fbf09a2 | 2018-07-03 17:56:38 +0200 | [diff] [blame] | 280 | def visit_command(self, name, info, ifcond, arg_type, ret_type, gen, |
Igor Mammedov | d6fe3d0 | 2018-05-11 18:51:43 +0200 | [diff] [blame] | 281 | success_response, boxed, allow_oob, allow_preconfig): |
Markus Armbruster | 71b3f04 | 2018-02-26 13:50:08 -0600 | [diff] [blame] | 282 | if not gen: |
| 283 | return |
Marc-André Lureau | 1f7b9f3 | 2018-07-03 17:56:43 +0200 | [diff] [blame] | 284 | # FIXME: If T is a user-defined type, the user is responsible |
| 285 | # for making this work, i.e. to make T's condition the |
| 286 | # conjunction of the T-returning commands' conditions. If T |
| 287 | # is a built-in type, this isn't possible: the |
| 288 | # qmp_marshal_output_T() will be generated unconditionally. |
Markus Armbruster | 252dc31 | 2018-02-11 10:36:00 +0100 | [diff] [blame] | 289 | if ret_type and ret_type not in self._visited_ret_types[self._genc]: |
| 290 | self._visited_ret_types[self._genc].add(ret_type) |
Marc-André Lureau | 1f7b9f3 | 2018-07-03 17:56:43 +0200 | [diff] [blame] | 291 | with ifcontext(ret_type.ifcond, |
| 292 | self._genh, self._genc, self._regy): |
| 293 | self._genc.add(gen_marshal_output(ret_type)) |
| 294 | with ifcontext(ifcond, self._genh, self._genc, self._regy): |
| 295 | self._genh.add(gen_command_decl(name, arg_type, boxed, ret_type)) |
| 296 | self._genh.add(gen_marshal_decl(name)) |
| 297 | self._genc.add(gen_marshal(name, arg_type, boxed, ret_type)) |
| 298 | self._regy.add(gen_register_command(name, success_response, |
| 299 | allow_oob, allow_preconfig)) |
Markus Armbruster | 71b3f04 | 2018-02-26 13:50:08 -0600 | [diff] [blame] | 300 | |
| 301 | |
| 302 | def gen_commands(schema, output_dir, prefix): |
Markus Armbruster | 26df4e7 | 2018-02-26 13:39:37 -0600 | [diff] [blame] | 303 | vis = QAPISchemaGenCommandVisitor(prefix) |
| 304 | schema.visit(vis) |
Markus Armbruster | 71b3f04 | 2018-02-26 13:50:08 -0600 | [diff] [blame] | 305 | vis.write(output_dir) |