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