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(&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 qdict_put_obj(qmp, "data", obj); 149''') 150 151 ret += mcgen(''' 152 %(event_emit)s(%(c_enum)s, qmp); 153 154''', 155 event_emit=event_emit, 156 c_enum=c_enum_const(event_enum_name, name)) 157 158 if have_args: 159 ret += mcgen(''' 160 visit_free(v); 161''') 162 ret += mcgen(''' 163 qobject_unref(qmp); 164} 165''') 166 return ret 167 168 169class QAPISchemaGenEventVisitor(QAPISchemaModularCVisitor): 170 171 def __init__(self, prefix: str): 172 super().__init__( 173 prefix, 'qapi-events', 174 ' * Schema-defined QAPI/QMP events', None, __doc__) 175 self._event_enum_name = c_name(prefix + 'QAPIEvent', protect=False) 176 self._event_enum_members: List[QAPISchemaEnumMember] = [] 177 self._event_emit_name = c_name(prefix + 'qapi_event_emit') 178 179 def _begin_user_module(self, name: str) -> None: 180 events = self._module_basename('qapi-events', name) 181 types = self._module_basename('qapi-types', name) 182 visit = self._module_basename('qapi-visit', name) 183 self._genc.add(mcgen(''' 184#include "qemu/osdep.h" 185#include "%(prefix)sqapi-emit-events.h" 186#include "%(events)s.h" 187#include "%(visit)s.h" 188#include "qapi/compat-policy.h" 189#include "qapi/error.h" 190#include "qapi/qmp/qdict.h" 191#include "qapi/qobject-output-visitor.h" 192#include "qapi/qmp-event.h" 193 194''', 195 events=events, visit=visit, 196 prefix=self._prefix)) 197 self._genh.add(mcgen(''' 198#include "qapi/util.h" 199#include "%(types)s.h" 200''', 201 types=types)) 202 203 def visit_end(self) -> None: 204 self._add_module('./emit', ' * QAPI Events emission') 205 self._genc.preamble_add(mcgen(''' 206#include "qemu/osdep.h" 207#include "%(prefix)sqapi-emit-events.h" 208''', 209 prefix=self._prefix)) 210 self._genh.preamble_add(mcgen(''' 211#include "qapi/util.h" 212''')) 213 self._genh.add(gen_enum(self._event_enum_name, 214 self._event_enum_members)) 215 self._genc.add(gen_enum_lookup(self._event_enum_name, 216 self._event_enum_members)) 217 self._genh.add(mcgen(''' 218 219void %(event_emit)s(%(event_enum)s event, QDict *qdict); 220''', 221 event_emit=self._event_emit_name, 222 event_enum=self._event_enum_name)) 223 224 def visit_event(self, 225 name: str, 226 info: Optional[QAPISourceInfo], 227 ifcond: Sequence[str], 228 features: List[QAPISchemaFeature], 229 arg_type: Optional[QAPISchemaObjectType], 230 boxed: bool) -> None: 231 with ifcontext(ifcond, self._genh, self._genc): 232 self._genh.add(gen_event_send_decl(name, arg_type, boxed)) 233 self._genc.add(gen_event_send(name, arg_type, features, boxed, 234 self._event_enum_name, 235 self._event_emit_name)) 236 # Note: we generate the enum member regardless of @ifcond, to 237 # keep the enumeration usable in target-independent code. 238 self._event_enum_members.append(QAPISchemaEnumMember(name, None)) 239 240 241def gen_events(schema: QAPISchema, 242 output_dir: str, 243 prefix: str) -> None: 244 vis = QAPISchemaGenEventVisitor(prefix) 245 schema.visit(vis) 246 vis.write(output_dir) 247