paul@100 | 1 | /* |
paul@100 | 2 | * Server code generation. |
paul@100 | 3 | * |
paul@214 | 4 | * Copyright (C) 2019, 2020, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@100 | 5 | * |
paul@100 | 6 | * This program is free software; you can redistribute it and/or |
paul@100 | 7 | * modify it under the terms of the GNU General Public License as |
paul@100 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@100 | 9 | * the License, or (at your option) any later version. |
paul@100 | 10 | * |
paul@100 | 11 | * This program is distributed in the hope that it will be useful, |
paul@100 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@100 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@100 | 14 | * GNU General Public License for more details. |
paul@100 | 15 | * |
paul@100 | 16 | * You should have received a copy of the GNU General Public License |
paul@100 | 17 | * along with this program; if not, write to the Free Software |
paul@100 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@100 | 19 | * Boston, MA 02110-1301, USA |
paul@100 | 20 | */ |
paul@100 | 21 | |
paul@100 | 22 | #include <stdlib.h> |
paul@100 | 23 | #include "common.h" |
paul@126 | 24 | #include "config.h" |
paul@100 | 25 | #include "declaration.h" |
paul@100 | 26 | #include "message.h" |
paul@100 | 27 | #include "server.h" |
paul@100 | 28 | #include "templates.h" |
paul@100 | 29 | |
paul@100 | 30 | |
paul@100 | 31 | |
paul@125 | 32 | /* Generic signature generation. */ |
paul@125 | 33 | |
paul@125 | 34 | static void _write_server_signature(struct signature *sig, |
paul@125 | 35 | enum signature_role role, |
paul@125 | 36 | FILE *fp, struct interface *iface, |
paul@125 | 37 | const char *prefix) |
paul@125 | 38 | { |
paul@125 | 39 | char *opname = get_operation_name(iface, sig); |
paul@125 | 40 | |
paul@125 | 41 | fprintf(fp, server_function_signature, prefix, opname, iface->name); |
paul@125 | 42 | fputs(get_signature_terminator(role), fp); |
paul@125 | 43 | |
paul@125 | 44 | free(opname); |
paul@125 | 45 | } |
paul@125 | 46 | |
paul@214 | 47 | /* Generate a signature for the invocation of an operation. */ |
paul@125 | 48 | |
paul@214 | 49 | static void write_server_wrapper_signature(struct signature *sig, enum signature_role role, |
paul@125 | 50 | FILE *fp, struct interface *iface) |
paul@125 | 51 | { |
paul@125 | 52 | _write_server_signature(sig, role, fp, iface, "wrap"); |
paul@125 | 53 | } |
paul@125 | 54 | |
paul@214 | 55 | /* Generate a signature for the initiation of an operation. */ |
paul@214 | 56 | |
paul@214 | 57 | static void write_server_initiation_signature(struct signature *sig, |
paul@125 | 58 | enum signature_role role, |
paul@125 | 59 | FILE *fp, struct interface *iface) |
paul@125 | 60 | { |
paul@125 | 61 | _write_server_signature(sig, role, fp, iface, "initiate"); |
paul@125 | 62 | } |
paul@125 | 63 | |
paul@214 | 64 | /* Generate a signature featuring an initiator reference (for genuine decoupled |
paul@214 | 65 | completions) and only "out" and "inout" parameters. */ |
paul@214 | 66 | |
paul@214 | 67 | static void write_server_completion_signature(struct signature *sig, |
paul@125 | 68 | enum signature_role role, |
paul@125 | 69 | FILE *fp, struct interface *iface) |
paul@125 | 70 | { |
paul@125 | 71 | char *opname = get_operation_name(iface, sig); |
paul@180 | 72 | int completion = have_attribute(sig->attributes, "completion"); |
paul@125 | 73 | |
paul@180 | 74 | char *prologue = completion ? |
paul@180 | 75 | server_completion_function_signature_prologue : |
paul@180 | 76 | server_completion_optional_function_signature_prologue; |
paul@180 | 77 | |
paul@180 | 78 | fprintf(fp, prologue, opname, iface->name); |
paul@180 | 79 | |
paul@180 | 80 | /* Continue from the initial endpoint parameter if a genuine completion. */ |
paul@129 | 81 | |
paul@129 | 82 | write_parameters(sig->parameters, fp, SIGNATURE_ROLE, COMPLETION_ROLE, |
paul@180 | 83 | OUT_PARAMETER, completion); |
paul@129 | 84 | |
paul@125 | 85 | fputs(")", fp); |
paul@125 | 86 | fputs(get_signature_terminator(role), fp); |
paul@125 | 87 | |
paul@125 | 88 | free(opname); |
paul@125 | 89 | } |
paul@125 | 90 | |
paul@214 | 91 | /* Generate a cast for superfluous message structures. */ |
paul@214 | 92 | |
paul@214 | 93 | static void write_superfluous_message_cast(FILE *fp, int input_words, int input_items, |
paul@214 | 94 | int output_words, int output_items) |
paul@214 | 95 | { |
paul@214 | 96 | if (!input_words && !input_items && !output_words && !output_items) |
paul@214 | 97 | fputs(server_function_body_unused_message, fp); |
paul@214 | 98 | } |
paul@214 | 99 | |
paul@214 | 100 | /* Generate the initialisation of input words and items. */ |
paul@100 | 101 | |
paul@214 | 102 | static void write_input_initialisation(struct parameter *param, FILE *fp, |
paul@214 | 103 | const char *opname, int input_words, |
paul@214 | 104 | int input_items) |
paul@214 | 105 | { |
paul@214 | 106 | if (input_words) |
paul@214 | 107 | { |
paul@214 | 108 | write_accessor_initialisation(IN_PARAMETER, SERVER_ROLE, GENERAL_FUNCTION_ROLE, |
paul@214 | 109 | opname, fp); |
paul@214 | 110 | write_message_access(param, SERVER_ROLE, GENERAL_FUNCTION_ROLE, IN_PARAMETER, |
paul@214 | 111 | WORD_CLASS, fp); |
paul@214 | 112 | } |
paul@214 | 113 | |
paul@214 | 114 | if (input_items) |
paul@214 | 115 | { |
paul@214 | 116 | fputs("\n", fp); |
paul@214 | 117 | write_message_access(param, SERVER_ROLE, GENERAL_FUNCTION_ROLE, IN_PARAMETER, |
paul@214 | 118 | ITEM_CLASS, fp); |
paul@214 | 119 | } |
paul@214 | 120 | } |
paul@214 | 121 | |
paul@214 | 122 | /* Generate the initialisation of output words and items. */ |
paul@214 | 123 | |
paul@214 | 124 | static void write_output_initialisation(struct parameter *param, FILE *fp, |
paul@214 | 125 | const char *opname, int output_words, |
paul@214 | 126 | int output_items, enum function_role function) |
paul@100 | 127 | { |
paul@214 | 128 | if (output_words) |
paul@214 | 129 | { |
paul@214 | 130 | write_accessor_initialisation(OUT_PARAMETER, SERVER_ROLE, function, |
paul@214 | 131 | opname, fp); |
paul@214 | 132 | write_message_access(param, SERVER_ROLE, function, OUT_PARAMETER, |
paul@214 | 133 | WORD_CLASS, fp); |
paul@214 | 134 | } |
paul@214 | 135 | |
paul@214 | 136 | if (output_items) |
paul@125 | 137 | { |
paul@214 | 138 | fputs("\n", fp); |
paul@214 | 139 | write_message_access(param, SERVER_ROLE, function, OUT_PARAMETER, |
paul@214 | 140 | ITEM_CLASS, fp); |
paul@125 | 141 | } |
paul@214 | 142 | } |
paul@214 | 143 | |
paul@214 | 144 | /* Generate an invocation of the actual server operation. */ |
paul@214 | 145 | |
paul@214 | 146 | static void write_server_function_call(struct parameter *param, FILE *fp, |
paul@214 | 147 | struct interface *iface, |
paul@214 | 148 | struct signature *sig, |
paul@214 | 149 | enum specifier specifier) |
paul@214 | 150 | { |
paul@214 | 151 | int continuing = 0; |
paul@214 | 152 | char *name, *addr, |
paul@214 | 153 | *opname = get_signature_operation_name(iface, sig, SERVER_ROLE, |
paul@214 | 154 | ACCESS_ROLE, conf.language); |
paul@214 | 155 | |
paul@214 | 156 | fputs("\n err = ", fp); |
paul@214 | 157 | |
paul@214 | 158 | /* Access a method when emitting C++. */ |
paul@214 | 159 | |
paul@214 | 160 | if (conf.language == CPP_LANGUAGE) |
paul@214 | 161 | fprintf(fp, "_self->%s(", opname); |
paul@214 | 162 | |
paul@214 | 163 | /* Employ a function pointer via the object type otherwise. */ |
paul@214 | 164 | |
paul@125 | 165 | else |
paul@125 | 166 | { |
paul@214 | 167 | fprintf(fp, "_self->iface->%s(_self->ref", opname); |
paul@214 | 168 | continuing = 1; |
paul@125 | 169 | } |
paul@180 | 170 | |
paul@214 | 171 | /* Generate the parameter list, employing addresses for output parameters. */ |
paul@214 | 172 | |
paul@214 | 173 | write_parameters(param, fp, INVOCATION_ROLE, GENERAL_FUNCTION_ROLE, specifier, |
paul@214 | 174 | continuing); |
paul@214 | 175 | fputs(");\n", fp); |
paul@180 | 176 | |
paul@214 | 177 | /* Emit post-invocation details. */ |
paul@214 | 178 | |
paul@214 | 179 | fputs(server_function_body_call, fp); |
paul@214 | 180 | |
paul@214 | 181 | /* Free allocated strings. */ |
paul@214 | 182 | |
paul@214 | 183 | free(opname); |
paul@100 | 184 | } |
paul@100 | 185 | |
paul@100 | 186 | /* Generate a function body corresponding to an operation for server use. */ |
paul@100 | 187 | |
paul@214 | 188 | static void write_server_wrapper_function_body(struct parameter *param, FILE *fp, |
paul@100 | 189 | struct interface *iface, struct signature *sig) |
paul@100 | 190 | { |
paul@100 | 191 | int input_words, input_items, output_words, output_items; |
paul@100 | 192 | char *opname = get_operation_name(iface, sig); |
paul@100 | 193 | |
paul@100 | 194 | /* Generate the prologue. */ |
paul@100 | 195 | |
paul@100 | 196 | fputs(server_function_body_prologue, fp); |
paul@100 | 197 | |
paul@100 | 198 | /* Count the number of input words, items and output words. */ |
paul@100 | 199 | |
paul@100 | 200 | count_parameters(param, &input_words, &input_items, &output_words, &output_items); |
paul@100 | 201 | |
paul@100 | 202 | /* Generate variable declarations. */ |
paul@100 | 203 | |
paul@100 | 204 | write_declarations(param, ANY_PARAMETER, ANY_CLASS, fp); |
paul@100 | 205 | |
paul@100 | 206 | /* Generate types parameterised with the qualified operation name. */ |
paul@100 | 207 | |
paul@100 | 208 | write_accessor_declaration(input_words, output_words, opname, fp); |
paul@100 | 209 | |
paul@146 | 210 | /* Handle superfluous message structures. */ |
paul@146 | 211 | |
paul@146 | 212 | write_superfluous_message_cast(fp, input_words, input_items, output_words, output_items); |
paul@146 | 213 | |
paul@100 | 214 | /* Unpack each word and item from the message into variables. */ |
paul@100 | 215 | |
paul@125 | 216 | write_input_initialisation(param, fp, opname, input_words, input_items); |
paul@125 | 217 | |
paul@125 | 218 | /* Invoke the actual operation using the variables. */ |
paul@125 | 219 | |
paul@125 | 220 | write_server_function_call(param, fp, iface, sig, ANY_PARAMETER); |
paul@125 | 221 | |
paul@125 | 222 | /* Pack the outputs into the message and perform any other housekeeping. */ |
paul@125 | 223 | |
paul@129 | 224 | write_output_initialisation(param, fp, opname, output_words, output_items, GENERAL_FUNCTION_ROLE); |
paul@125 | 225 | |
paul@127 | 226 | /* Return the success of the reply operation. */ |
paul@125 | 227 | |
paul@127 | 228 | fputs(server_function_body_epilogue, fp); |
paul@125 | 229 | |
paul@125 | 230 | /* Free allocated strings. */ |
paul@125 | 231 | |
paul@125 | 232 | free(opname); |
paul@125 | 233 | } |
paul@125 | 234 | |
paul@125 | 235 | /* Generate a function body corresponding to the initiation of an operation. */ |
paul@125 | 236 | |
paul@214 | 237 | static void write_server_initiation_function_body(struct parameter *param, FILE *fp, |
paul@125 | 238 | struct interface *iface, |
paul@125 | 239 | struct signature *sig) |
paul@125 | 240 | { |
paul@125 | 241 | int input_words, input_items, output_words, output_items; |
paul@125 | 242 | char *opname = get_operation_name(iface, sig); |
paul@125 | 243 | |
paul@125 | 244 | /* Generate the prologue. */ |
paul@125 | 245 | |
paul@125 | 246 | fputs(server_function_body_prologue, fp); |
paul@125 | 247 | |
paul@125 | 248 | /* Count the number of input words, items and output words. */ |
paul@125 | 249 | |
paul@125 | 250 | count_parameters(param, &input_words, &input_items, &output_words, &output_items); |
paul@125 | 251 | |
paul@125 | 252 | /* Generate variable declarations for "in" and "inout" parameters. */ |
paul@125 | 253 | |
paul@125 | 254 | write_declarations(param, IN_PARAMETER, ANY_CLASS, fp); |
paul@125 | 255 | |
paul@125 | 256 | /* Generate types parameterised with the qualified operation name. */ |
paul@125 | 257 | |
paul@125 | 258 | write_accessor_declaration(input_words, 0, opname, fp); |
paul@125 | 259 | |
paul@146 | 260 | /* Handle superfluous message structures. */ |
paul@146 | 261 | |
paul@146 | 262 | write_superfluous_message_cast(fp, input_words, input_items, 0, 0); |
paul@146 | 263 | |
paul@125 | 264 | /* Unpack each word and item from the message into variables. */ |
paul@125 | 265 | |
paul@125 | 266 | write_input_initialisation(param, fp, opname, input_words, input_items); |
paul@125 | 267 | |
paul@125 | 268 | /* Invoke the actual operation using the variables. */ |
paul@125 | 269 | |
paul@125 | 270 | write_server_function_call(param, fp, iface, sig, IN_PARAMETER); |
paul@125 | 271 | |
paul@127 | 272 | /* Return the success of the operation restoring buffer registers. */ |
paul@125 | 273 | |
paul@127 | 274 | fputs(server_initiation_function_body_epilogue, fp); |
paul@125 | 275 | |
paul@125 | 276 | /* Free allocated strings. */ |
paul@125 | 277 | |
paul@125 | 278 | free(opname); |
paul@125 | 279 | } |
paul@125 | 280 | |
paul@125 | 281 | /* Generate a function body corresponding to the completion of an operation. */ |
paul@125 | 282 | |
paul@214 | 283 | static void write_server_completion_function_body(struct parameter *param, FILE *fp, |
paul@125 | 284 | struct interface *iface, |
paul@125 | 285 | struct signature *sig) |
paul@125 | 286 | { |
paul@125 | 287 | int input_words, input_items, output_words, output_items; |
paul@125 | 288 | char *opname = get_operation_name(iface, sig); |
paul@125 | 289 | |
paul@125 | 290 | /* Generate the prologue. */ |
paul@125 | 291 | |
paul@125 | 292 | fputs(server_completion_function_body_prologue, fp); |
paul@125 | 293 | |
paul@125 | 294 | /* Count the number of input words, items and output words. */ |
paul@125 | 295 | |
paul@125 | 296 | count_parameters(param, &input_words, &input_items, &output_words, &output_items); |
paul@125 | 297 | |
paul@125 | 298 | /* Generate types parameterised with the qualified operation name. */ |
paul@125 | 299 | |
paul@125 | 300 | write_accessor_declaration(0, output_words, opname, fp); |
paul@125 | 301 | |
paul@125 | 302 | /* Initialise the message. */ |
paul@125 | 303 | |
paul@125 | 304 | fprintf(fp, "\n ipc_message_new(&msg);\n"); |
paul@125 | 305 | |
paul@125 | 306 | /* Pack the outputs into the message and perform any other housekeeping. */ |
paul@125 | 307 | |
paul@129 | 308 | write_output_initialisation(param, fp, opname, output_words, output_items, COMPLETION_ROLE); |
paul@125 | 309 | |
paul@180 | 310 | /* Send a response as a new message to the given endpoint. |
paul@125 | 311 | NOTE: The label 0 is employed but an error condition could be communicated. */ |
paul@125 | 312 | |
paul@180 | 313 | if (have_attribute(sig->attributes, "completion")) |
paul@180 | 314 | fprintf(fp, server_completion_function_body_epilogue, "0", "_endp"); |
paul@180 | 315 | |
paul@180 | 316 | /* Send a plain reply for optional/coupled completions. */ |
paul@180 | 317 | |
paul@180 | 318 | else |
paul@180 | 319 | fputs(server_completion_optional_function_body_epilogue, fp); |
paul@125 | 320 | |
paul@125 | 321 | /* Free allocated strings. */ |
paul@125 | 322 | |
paul@125 | 323 | free(opname); |
paul@125 | 324 | } |
paul@125 | 325 | |
paul@214 | 326 | |
paul@214 | 327 | |
paul@214 | 328 | /* Public functions. */ |
paul@214 | 329 | |
paul@214 | 330 | /* Generate each component operation signature or "function prototype". */ |
paul@146 | 331 | |
paul@214 | 332 | void write_server_interface_signature(struct signature *sig, |
paul@214 | 333 | enum signature_role role, FILE *fp, |
paul@214 | 334 | struct interface *iface) |
paul@146 | 335 | { |
paul@214 | 336 | write_interface_signature(sig, role, SERVER_ROLE, |
paul@214 | 337 | get_server_function_role(sig), fp, iface); |
paul@146 | 338 | } |
paul@146 | 339 | |
paul@214 | 340 | /* Generate signature declarations for each operation. */ |
paul@125 | 341 | |
paul@214 | 342 | void write_server_signature(struct signature *sig, enum signature_role role, |
paul@214 | 343 | FILE *fp, struct interface *iface) |
paul@125 | 344 | { |
paul@214 | 345 | if (have_attribute(sig->attributes, "completion")) |
paul@214 | 346 | write_server_initiation_signature(sig, role, fp, iface); |
paul@214 | 347 | else |
paul@214 | 348 | write_server_wrapper_signature(sig, role, fp, iface); |
paul@100 | 349 | |
paul@214 | 350 | /* Write the appropriate completion signature even for use by conventional |
paul@214 | 351 | operations. */ |
paul@214 | 352 | |
paul@214 | 353 | write_server_completion_signature(sig, role, fp, iface); |
paul@125 | 354 | } |
paul@100 | 355 | |
paul@214 | 356 | /* Generate function source code for each operation, with the generated function |
paul@214 | 357 | unpacking a message, calling the actual operation and repacking the |
paul@214 | 358 | results. */ |
paul@100 | 359 | |
paul@214 | 360 | void write_server_function(struct signature *sig, FILE *fp, struct interface *iface) |
paul@125 | 361 | { |
paul@214 | 362 | if (have_attribute(sig->attributes, "completion")) |
paul@100 | 363 | { |
paul@214 | 364 | write_server_initiation_signature(sig, DEFINITION_ROLE, fp, iface); |
paul@214 | 365 | write_server_initiation_function_body(sig->parameters, fp, iface, sig); |
paul@214 | 366 | fputs(END_FUNCTION, fp); |
paul@214 | 367 | } |
paul@214 | 368 | else |
paul@214 | 369 | { |
paul@214 | 370 | write_server_wrapper_signature(sig, DEFINITION_ROLE, fp, iface); |
paul@214 | 371 | write_server_wrapper_function_body(sig->parameters, fp, iface, sig); |
paul@214 | 372 | fputs(END_FUNCTION, fp); |
paul@100 | 373 | } |
paul@100 | 374 | |
paul@214 | 375 | /* Write the appropriate completion function even for use by conventional |
paul@214 | 376 | operations. */ |
paul@100 | 377 | |
paul@214 | 378 | write_server_completion_signature(sig, DEFINITION_ROLE, fp, iface); |
paul@214 | 379 | write_server_completion_function_body(sig->parameters, fp, iface, sig); |
paul@214 | 380 | fputs(END_FUNCTION, fp); |
paul@100 | 381 | } |