events.py (7533B)
1""" 2QAPI event generator 3 4Copyright (c) 2014 Wenchao Xia 5Copyright (c) 2015-2018 Red Hat Inc. 6 7Authors: 8 Wenchao Xia <wenchaoqemu@gmail.com> 9 Markus Armbruster <armbru@redhat.com> 10 11This work is licensed under the terms of the GNU GPL, version 2. 12See the COPYING file in the top-level directory. 13""" 14 15from typing import List, Optional 16 17from .common import c_enum_const, c_name, mcgen 18from .gen import QAPISchemaModularCVisitor, build_params, ifcontext 19from .schema import ( 20 QAPISchema, 21 QAPISchemaEnumMember, 22 QAPISchemaFeature, 23 QAPISchemaIfCond, 24 QAPISchemaObjectType, 25) 26from .source import QAPISourceInfo 27from .types import gen_enum, gen_enum_lookup 28 29 30def build_event_send_proto(name: str, 31 arg_type: Optional[QAPISchemaObjectType], 32 boxed: bool) -> str: 33 return 'void qapi_event_send_%(c_name)s(%(param)s)' % { 34 'c_name': c_name(name.lower()), 35 'param': build_params(arg_type, boxed)} 36 37 38def gen_event_send_decl(name: str, 39 arg_type: Optional[QAPISchemaObjectType], 40 boxed: bool) -> str: 41 return mcgen(''' 42 43%(proto)s; 44''', 45 proto=build_event_send_proto(name, arg_type, boxed)) 46 47 48def gen_param_var(typ: QAPISchemaObjectType) -> str: 49 """ 50 Generate a struct variable holding the event parameters. 51 52 Initialize it with the function arguments defined in `gen_event_send`. 53 """ 54 assert not typ.variants 55 ret = mcgen(''' 56 %(c_name)s param = { 57''', 58 c_name=typ.c_name()) 59 sep = ' ' 60 for memb in typ.members: 61 ret += sep 62 sep = ', ' 63 if memb.optional: 64 ret += 'has_' + c_name(memb.name) + sep 65 if memb.type.name == 'str': 66 # Cast away const added in build_params() 67 ret += '(char *)' 68 ret += c_name(memb.name) 69 ret += mcgen(''' 70 71 }; 72''') 73 if not typ.is_implicit(): 74 ret += mcgen(''' 75 %(c_name)s *arg = ¶m; 76''', 77 c_name=typ.c_name()) 78 return ret 79 80 81def gen_event_send(name: str, 82 arg_type: Optional[QAPISchemaObjectType], 83 features: List[QAPISchemaFeature], 84 boxed: bool, 85 event_enum_name: str, 86 event_emit: str) -> str: 87 # FIXME: Our declaration of local variables (and of 'errp' in the 88 # parameter list) can collide with exploded members of the event's 89 # data type passed in as parameters. If this collision ever hits in 90 # practice, we can rename our local variables with a leading _ prefix, 91 # or split the code into a wrapper function that creates a boxed 92 # 'param' object then calls another to do the real work. 93 have_args = boxed or (arg_type and not arg_type.is_empty()) 94 95 ret = mcgen(''' 96 97%(proto)s 98{ 99 QDict *qmp; 100''', 101 proto=build_event_send_proto(name, arg_type, boxed)) 102 103 if have_args: 104 assert arg_type is not None 105 ret += mcgen(''' 106 QObject *obj; 107 Visitor *v; 108''') 109 if not boxed: 110 ret += gen_param_var(arg_type) 111 112 if 'deprecated' in [f.name for f in features]: 113 ret += mcgen(''' 114 115 if (compat_policy.deprecated_output == COMPAT_POLICY_OUTPUT_HIDE) { 116 return; 117 } 118''') 119 120 ret += mcgen(''' 121 122 qmp = qmp_event_build_dict("%(name)s"); 123 124''', 125 name=name) 126 127 if have_args: 128 assert arg_type is not None 129 ret += mcgen(''' 130 v = qobject_output_visitor_new_qmp(&obj); 131''') 132 if not arg_type.is_implicit(): 133 ret += mcgen(''' 134 visit_type_%(c_name)s(v, "%(name)s", &arg, &error_abort); 135''', 136 name=name, c_name=arg_type.c_name()) 137 else: 138 ret += mcgen(''' 139 140 visit_start_struct(v, "%(name)s", NULL, 0, &error_abort); 141 visit_type_%(c_name)s_members(v, ¶m, &error_abort); 142 visit_check_struct(v, &error_abort); 143 visit_end_struct(v, NULL); 144''', 145 name=name, c_name=arg_type.c_name()) 146 ret += mcgen(''' 147 148 visit_complete(v, &obj); 149 if (qdict_size(qobject_to(QDict, obj))) { 150 qdict_put_obj(qmp, "data", obj); 151 } else { 152 qobject_unref(obj); 153 } 154''') 155 156 ret += mcgen(''' 157 %(event_emit)s(%(c_enum)s, qmp); 158 159''', 160 event_emit=event_emit, 161 c_enum=c_enum_const(event_enum_name, name)) 162 163 if have_args: 164 ret += mcgen(''' 165 visit_free(v); 166''') 167 ret += mcgen(''' 168 qobject_unref(qmp); 169} 170''') 171 return ret 172 173 174class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor): 175 176 def __init__(self, prefix: str): 177 super().__init__( 178 prefix, 'qapi-events', 179 ' * Schema-defined QAPI/QMP events', None, __doc__) 180 self._event_enum_name = c_name(prefix + 'QAPIEvent', protect=False) 181 self._event_enum_members: List[QAPISchemaEnumMember] = [] 182 self._event_emit_name = c_name(prefix + 'qapi_event_emit') 183 184 def _begin_user_module(self, name: str) -> None: 185 events = self._module_basename('qapi-events', name) 186 types = self._module_basename('qapi-types', name) 187 visit = self._module_basename('qapi-visit', name) 188 self._genc.add(mcgen(''' 189#include "qemu/osdep.h" 190#include "%(prefix)sqapi-emit-events.h" 191#include "%(events)s.h" 192#include "%(visit)s.h" 193#include "qapi/compat-policy.h" 194#include "qapi/error.h" 195#include "qapi/qmp/qdict.h" 196#include "qapi/qmp-event.h" 197 198''', 199 events=events, visit=visit, 200 prefix=self._prefix)) 201 self._genh.add(mcgen(''' 202#include "qapi/util.h" 203#include "%(types)s.h" 204''', 205 types=types)) 206 207 def visit_end(self) -> None: 208 self._add_module('./emit', ' * QAPI Events emission') 209 self._genc.preamble_add(mcgen(''' 210#include "qemu/osdep.h" 211#include "%(prefix)sqapi-emit-events.h" 212''', 213 prefix=self._prefix)) 214 self._genh.preamble_add(mcgen(''' 215#include "qapi/util.h" 216''')) 217 self._genh.add(gen_enum(self._event_enum_name, 218 self._event_enum_members)) 219 self._genc.add(gen_enum_lookup(self._event_enum_name, 220 self._event_enum_members)) 221 self._genh.add(mcgen(''' 222 223void %(event_emit)s(%(event_enum)s event, QDict *qdict); 224''', 225 event_emit=self._event_emit_name, 226 event_enum=self._event_enum_name)) 227 228 def visit_event(self, 229 name: str, 230 info: Optional[QAPISourceInfo], 231 ifcond: QAPISchemaIfCond, 232 features: List[QAPISchemaFeature], 233 arg_type: Optional[QAPISchemaObjectType], 234 boxed: bool) -> None: 235 with ifcontext(ifcond, self._genh, self._genc): 236 self._genh.add(gen_event_send_decl(name, arg_type, boxed)) 237 self._genc.add(gen_event_send(name, arg_type, features, boxed, 238 self._event_enum_name, 239 self._event_emit_name)) 240 # Note: we generate the enum member regardless of @ifcond, to 241 # keep the enumeration usable in target-independent code. 242 self._event_enum_members.append(QAPISchemaEnumMember(name, None)) 243 244 245def gen_events(schema: QAPISchema, 246 output_dir: str, 247 prefix: str) -> None: 248 vis = QAPISchemaGenEventVisitor(prefix) 249 schema.visit(vis) 250 vis.write(output_dir)