paul@44 | 1 | /* |
paul@44 | 2 | * Common routines. |
paul@44 | 3 | * |
paul@202 | 4 | * Copyright (C) 2019, 2020, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@44 | 5 | * |
paul@44 | 6 | * This program is free software; you can redistribute it and/or |
paul@44 | 7 | * modify it under the terms of the GNU General Public License as |
paul@44 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@44 | 9 | * the License, or (at your option) any later version. |
paul@44 | 10 | * |
paul@44 | 11 | * This program is distributed in the hope that it will be useful, |
paul@44 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@44 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@44 | 14 | * GNU General Public License for more details. |
paul@44 | 15 | * |
paul@44 | 16 | * You should have received a copy of the GNU General Public License |
paul@44 | 17 | * along with this program; if not, write to the Free Software |
paul@44 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@44 | 19 | * Boston, MA 02110-1301, USA |
paul@44 | 20 | */ |
paul@44 | 21 | |
paul@44 | 22 | #include <libgen.h> |
paul@44 | 23 | #include <stdlib.h> |
paul@44 | 24 | #include <string.h> |
paul@44 | 25 | #include "common.h" |
paul@110 | 26 | #include "templates.h" |
paul@44 | 27 | |
paul@44 | 28 | |
paul@44 | 29 | |
paul@44 | 30 | /* Generate an error message for file opening failure. */ |
paul@44 | 31 | |
paul@44 | 32 | void open_error(const char *dir, const char *name) |
paul@44 | 33 | { |
paul@55 | 34 | fprintf(stderr, "Could not open source file for writing in directory %s (name %s).\n", dir, name); |
paul@44 | 35 | } |
paul@44 | 36 | |
paul@214 | 37 | /* Test for a basename. */ |
paul@214 | 38 | |
paul@214 | 39 | int is_basename(char *path) |
paul@214 | 40 | { |
paul@214 | 41 | return basename(path) == path; |
paul@214 | 42 | } |
paul@214 | 43 | |
paul@44 | 44 | /* Obtain a new basename string. */ |
paul@44 | 45 | |
paul@44 | 46 | char *make_basename(const char *path) |
paul@44 | 47 | { |
paul@44 | 48 | char s[strlen(path) + 1]; |
paul@44 | 49 | |
paul@44 | 50 | strcpy(s, path); |
paul@44 | 51 | return strdup(basename(s)); |
paul@44 | 52 | } |
paul@44 | 53 | |
paul@44 | 54 | /* Obtain a new directory name string. */ |
paul@44 | 55 | |
paul@44 | 56 | char *make_dirname(const char *path) |
paul@44 | 57 | { |
paul@44 | 58 | char s[strlen(path) + 1]; |
paul@44 | 59 | |
paul@44 | 60 | strcpy(s, path); |
paul@44 | 61 | return strdup(dirname(s)); |
paul@44 | 62 | } |
paul@44 | 63 | |
paul@44 | 64 | /* Obtain a file pointer using a parameterised filename. */ |
paul@44 | 65 | |
paul@44 | 66 | FILE *get_output_file(const char *filename_template, const char *dir, const char *name) |
paul@44 | 67 | { |
paul@44 | 68 | FILE *fp; |
paul@44 | 69 | char *filename; |
paul@44 | 70 | |
paul@44 | 71 | if (asprintf(&filename, filename_template, dir, name) == -1) |
paul@44 | 72 | fp = NULL; |
paul@44 | 73 | else |
paul@44 | 74 | { |
paul@44 | 75 | fp = fopen(filename, "w"); |
paul@44 | 76 | free(filename); |
paul@44 | 77 | } |
paul@44 | 78 | |
paul@44 | 79 | /* Write an error message upon failure. */ |
paul@44 | 80 | |
paul@44 | 81 | if (fp == NULL) |
paul@44 | 82 | open_error(dir, name); |
paul@44 | 83 | |
paul@44 | 84 | return fp; |
paul@44 | 85 | } |
paul@44 | 86 | |
paul@214 | 87 | /* Return whether an interface is a compound interface. */ |
paul@214 | 88 | |
paul@214 | 89 | int is_compound_interface(struct interface *iface) |
paul@214 | 90 | { |
paul@214 | 91 | return iface->bases != NULL; |
paul@214 | 92 | } |
paul@214 | 93 | |
paul@44 | 94 | /* Find an attribute value. */ |
paul@44 | 95 | |
paul@51 | 96 | char *get_attribute_value(struct attribute *attr, const char *name) |
paul@44 | 97 | { |
paul@51 | 98 | if (attr == NULL) |
paul@44 | 99 | return NULL; |
paul@44 | 100 | |
paul@51 | 101 | if (!strcmp(attr->attribute, name) && (attr->identifiers != NULL)) |
paul@51 | 102 | return attr->identifiers->identifier; |
paul@44 | 103 | |
paul@51 | 104 | return get_attribute_value(attr->tail, name); |
paul@44 | 105 | } |
paul@44 | 106 | |
paul@162 | 107 | /* Return any conversion to a different object type in a dispatcher. */ |
paul@162 | 108 | |
paul@162 | 109 | char *get_object_conversion(struct interface *iface, int compound) |
paul@162 | 110 | { |
paul@162 | 111 | char *conversion; |
paul@162 | 112 | |
paul@162 | 113 | if (!compound) |
paul@162 | 114 | return strdup("_self"); |
paul@162 | 115 | |
paul@162 | 116 | if (asprintf(&conversion, "convert_to_%s(_self)", iface->name) == -1) |
paul@162 | 117 | conversion = NULL; |
paul@162 | 118 | |
paul@162 | 119 | return conversion; |
paul@162 | 120 | } |
paul@162 | 121 | |
paul@44 | 122 | /* Return any opcode attribute value. */ |
paul@44 | 123 | |
paul@51 | 124 | char *get_opcode(struct attribute *attr) |
paul@44 | 125 | { |
paul@51 | 126 | return get_attribute_value(attr, "opcode"); |
paul@44 | 127 | } |
paul@44 | 128 | |
paul@208 | 129 | /* Return any opcode type attribute value. */ |
paul@208 | 130 | |
paul@208 | 131 | char *get_opcode_type(struct attribute *attr) |
paul@208 | 132 | { |
paul@208 | 133 | return get_attribute_value(attr, "opcode_type"); |
paul@208 | 134 | } |
paul@208 | 135 | |
paul@44 | 136 | /* Return any protocol attribute value. */ |
paul@44 | 137 | |
paul@51 | 138 | char *get_protocol(struct attribute *attr) |
paul@44 | 139 | { |
paul@51 | 140 | return get_attribute_value(attr, "protocol"); |
paul@44 | 141 | } |
paul@44 | 142 | |
paul@125 | 143 | /* Return the wrapper function prefix. */ |
paul@125 | 144 | |
paul@125 | 145 | char *get_operation_wrapper_prefix(struct attribute *attr) |
paul@125 | 146 | { |
paul@125 | 147 | if (have_attribute(attr, "completion")) |
paul@125 | 148 | return "initiate"; |
paul@125 | 149 | else |
paul@125 | 150 | return "wrap"; |
paul@125 | 151 | } |
paul@125 | 152 | |
paul@125 | 153 | int have_attribute(struct attribute *attr, const char *name) |
paul@125 | 154 | { |
paul@125 | 155 | if (attr == NULL) |
paul@125 | 156 | return 0; |
paul@125 | 157 | |
paul@125 | 158 | if (!strcmp(attr->attribute, name)) |
paul@125 | 159 | return 1; |
paul@125 | 160 | |
paul@125 | 161 | return have_attribute(attr->tail, name); |
paul@125 | 162 | } |
paul@125 | 163 | |
paul@132 | 164 | int have_attribute_value(struct attribute *attr, const char *name, |
paul@132 | 165 | const char *value) |
paul@132 | 166 | { |
paul@132 | 167 | if (attr == NULL) |
paul@132 | 168 | return 0; |
paul@132 | 169 | |
paul@132 | 170 | if (!strcmp(attr->attribute, name) && have_identifier(attr->identifiers, value)) |
paul@132 | 171 | return 1; |
paul@132 | 172 | |
paul@132 | 173 | return have_attribute_value(attr->tail, name, value); |
paul@132 | 174 | } |
paul@132 | 175 | |
paul@132 | 176 | int have_identifier(struct identifier *ident, const char *value) |
paul@132 | 177 | { |
paul@132 | 178 | if (ident == NULL) |
paul@132 | 179 | return 0; |
paul@132 | 180 | |
paul@132 | 181 | if (!strcmp(ident->identifier, value)) |
paul@132 | 182 | return 1; |
paul@132 | 183 | |
paul@132 | 184 | return have_identifier(ident->tail, value); |
paul@132 | 185 | } |
paul@132 | 186 | |
paul@186 | 187 | /* Return the function role of the given signature for server use. */ |
paul@186 | 188 | |
paul@186 | 189 | enum function_role get_server_function_role(struct signature *sig) |
paul@186 | 190 | { |
paul@186 | 191 | return have_attribute(sig->attributes, "completion") ? COMPLETION_ROLE |
paul@186 | 192 | : GENERAL_FUNCTION_ROLE; |
paul@186 | 193 | } |
paul@186 | 194 | |
paul@115 | 195 | /* Obtain a suitable interface class name depending on the component role. */ |
paul@115 | 196 | |
paul@115 | 197 | char *get_interface_class_name(struct interface *iface, |
paul@115 | 198 | enum component_role component) |
paul@115 | 199 | { |
paul@115 | 200 | char *name; |
paul@115 | 201 | |
paul@115 | 202 | switch (component) |
paul@115 | 203 | { |
paul@115 | 204 | case CLIENT_ROLE: |
paul@117 | 205 | if (asprintf(&name, "client_%s", iface->name) == -1) |
paul@115 | 206 | name = NULL; |
paul@115 | 207 | break; |
paul@115 | 208 | |
paul@115 | 209 | case SERVER_ROLE: |
paul@115 | 210 | name = strdup(iface->name); |
paul@115 | 211 | break; |
paul@115 | 212 | |
paul@115 | 213 | default: |
paul@115 | 214 | name = NULL; |
paul@117 | 215 | break; |
paul@115 | 216 | } |
paul@115 | 217 | |
paul@115 | 218 | return name; |
paul@115 | 219 | } |
paul@115 | 220 | |
paul@110 | 221 | /* Generate a qualified operation name for declarations and definitions. */ |
paul@72 | 222 | |
paul@110 | 223 | char *get_signature_operation_name(struct interface *iface, |
paul@110 | 224 | struct signature *sig, |
paul@115 | 225 | enum component_role component, |
paul@110 | 226 | enum signature_role role, enum language lang) |
paul@72 | 227 | { |
paul@115 | 228 | char *opname, *class_name; |
paul@72 | 229 | |
paul@72 | 230 | switch (lang) |
paul@72 | 231 | { |
paul@117 | 232 | /* In C, interface members use the bare names. */ |
paul@117 | 233 | |
paul@72 | 234 | case C_LANGUAGE: |
paul@117 | 235 | if ((role == MEMBER_DECLARATION_ROLE) || (role == ACCESS_ROLE)) |
paul@117 | 236 | opname = strdup(sig->operation); |
paul@117 | 237 | else |
paul@117 | 238 | opname = get_operation_name(iface, sig); |
paul@117 | 239 | break; |
paul@117 | 240 | |
paul@117 | 241 | /* In C++, method definitions are qualified using class names. */ |
paul@72 | 242 | |
paul@72 | 243 | case CPP_LANGUAGE: |
paul@110 | 244 | if (role == DEFINITION_ROLE) |
paul@110 | 245 | { |
paul@115 | 246 | class_name = get_interface_class_name(iface, component); |
paul@115 | 247 | |
paul@115 | 248 | if (asprintf(&opname, "%s::%s", class_name, sig->operation) == -1) |
paul@110 | 249 | opname = NULL; |
paul@115 | 250 | |
paul@115 | 251 | free(class_name); |
paul@110 | 252 | } |
paul@110 | 253 | else |
paul@110 | 254 | opname = strdup(sig->operation); |
paul@72 | 255 | break; |
paul@72 | 256 | |
paul@72 | 257 | default: |
paul@72 | 258 | opname = NULL; |
paul@72 | 259 | break; |
paul@72 | 260 | } |
paul@72 | 261 | |
paul@72 | 262 | return opname; |
paul@72 | 263 | } |
paul@72 | 264 | |
paul@117 | 265 | /* Generate a qualified operation name for structure and function naming. */ |
paul@44 | 266 | |
paul@44 | 267 | char *get_operation_name(struct interface *iface, struct signature *sig) |
paul@44 | 268 | { |
paul@44 | 269 | char *opname; |
paul@44 | 270 | |
paul@44 | 271 | if (asprintf(&opname, "%s_%s", iface->name, sig->operation) == -1) |
paul@72 | 272 | opname = NULL; |
paul@44 | 273 | |
paul@44 | 274 | return opname; |
paul@44 | 275 | } |
paul@44 | 276 | |
paul@44 | 277 | /* Return the invocation opcode given protocol and operation name details. */ |
paul@44 | 278 | |
paul@44 | 279 | char *get_opcode_identifier(char *protocol, char *opname) |
paul@44 | 280 | { |
paul@44 | 281 | char *opcode; |
paul@44 | 282 | |
paul@44 | 283 | if (protocol != NULL) |
paul@44 | 284 | return protocol; |
paul@44 | 285 | |
paul@44 | 286 | if (asprintf(&opcode, "opcode_%s", opname) == -1) |
paul@44 | 287 | return NULL; |
paul@44 | 288 | |
paul@44 | 289 | return opcode; |
paul@44 | 290 | } |
paul@44 | 291 | |
paul@129 | 292 | /* Return the appropriate parameter decoration. Generally, output parameters |
paul@129 | 293 | involve addresses. However, completion functions accept output parameters as |
paul@129 | 294 | values. */ |
paul@125 | 295 | |
paul@125 | 296 | const char *get_parameter_decoration(struct parameter *param, |
paul@129 | 297 | enum parameter_role role, |
paul@129 | 298 | enum function_role function) |
paul@125 | 299 | { |
paul@129 | 300 | if ((param->specifier & OUT_PARAMETER) && (function != COMPLETION_ROLE)) |
paul@125 | 301 | { |
paul@125 | 302 | switch (role) |
paul@125 | 303 | { |
paul@125 | 304 | case SIGNATURE_ROLE: return "*"; |
paul@125 | 305 | case INVOCATION_ROLE: return "&"; |
paul@125 | 306 | default: break; |
paul@125 | 307 | } |
paul@125 | 308 | } |
paul@125 | 309 | |
paul@125 | 310 | return ""; |
paul@125 | 311 | } |
paul@125 | 312 | |
paul@44 | 313 | /* Return the parameter name, being the final identifier in a list. */ |
paul@44 | 314 | |
paul@51 | 315 | char *get_parameter_name(struct identifier *ident) |
paul@44 | 316 | { |
paul@44 | 317 | if (ident->tail != NULL) |
paul@44 | 318 | return get_parameter_name(ident->tail); |
paul@44 | 319 | else |
paul@44 | 320 | return ident->identifier; |
paul@44 | 321 | } |
paul@44 | 322 | |
paul@110 | 323 | /* Get the appropriate terminating text after a signature. */ |
paul@110 | 324 | |
paul@110 | 325 | char *get_signature_terminator(enum signature_role role) |
paul@110 | 326 | { |
paul@117 | 327 | /* Signatures in declarations or member initialisation. */ |
paul@117 | 328 | |
paul@117 | 329 | if (role & DECLARATION_ROLE) |
paul@117 | 330 | return COMPLETE_SIGNATURE; |
paul@117 | 331 | |
paul@117 | 332 | /* Signatures at the start of functions. */ |
paul@117 | 333 | |
paul@117 | 334 | if (role & DEFINITION_ROLE) |
paul@117 | 335 | return BEGIN_FUNCTION; |
paul@117 | 336 | |
paul@117 | 337 | return ""; |
paul@110 | 338 | } |
paul@110 | 339 | |
paul@44 | 340 | /* Count the different kinds of parameters in terms of inputs, outputs, words |
paul@44 | 341 | and items. */ |
paul@44 | 342 | |
paul@51 | 343 | void count_parameters(struct parameter *param, int *input_words, |
paul@57 | 344 | int *input_items, int *output_words, int *output_items) |
paul@44 | 345 | { |
paul@51 | 346 | for (*input_words = 0, *input_items = 0, *output_words = 0, *output_items = 0; |
paul@51 | 347 | param != NULL; |
paul@51 | 348 | param = param->tail) |
paul@44 | 349 | { |
paul@204 | 350 | /* Special case: "out fpage". */ |
paul@204 | 351 | |
paul@204 | 352 | if ((param->specifier & IN_PARAMETER) || (param->cls == FPAGE_ITEM)) |
paul@44 | 353 | { |
paul@57 | 354 | if (param->cls & ITEM_CLASS) (*input_items)++; |
paul@44 | 355 | else (*input_words)++; |
paul@44 | 356 | } |
paul@129 | 357 | |
paul@129 | 358 | if (param->specifier & OUT_PARAMETER) |
paul@44 | 359 | { |
paul@57 | 360 | if (param->cls & ITEM_CLASS) (*output_items)++; |
paul@44 | 361 | else (*output_words)++; |
paul@44 | 362 | } |
paul@44 | 363 | } |
paul@44 | 364 | } |
paul@61 | 365 | |
paul@70 | 366 | /* Return the maximum number of input items accepted by any one of the given |
paul@70 | 367 | signatures. */ |
paul@70 | 368 | |
paul@214 | 369 | int get_max_input_items(struct interface *iface) |
paul@70 | 370 | { |
paul@70 | 371 | int input_words, input_items, output_words, output_items; |
paul@70 | 372 | int max_input_items = 0; |
paul@214 | 373 | struct interface_ref *base; |
paul@214 | 374 | struct signature *sig; |
paul@70 | 375 | |
paul@214 | 376 | /* Test any base interfaces. */ |
paul@214 | 377 | |
paul@214 | 378 | for (base = iface->bases; base != NULL; base = base->tail) |
paul@214 | 379 | { |
paul@214 | 380 | input_items = get_max_input_items(base->iface); |
paul@214 | 381 | |
paul@214 | 382 | if (input_items > max_input_items) |
paul@214 | 383 | max_input_items = input_items; |
paul@214 | 384 | } |
paul@214 | 385 | |
paul@214 | 386 | /* Test the signatures. */ |
paul@214 | 387 | |
paul@214 | 388 | for (sig = iface->signatures; sig != NULL; sig = sig->tail) |
paul@70 | 389 | { |
paul@70 | 390 | count_parameters(sig->parameters, &input_words, &input_items, &output_words, &output_items); |
paul@70 | 391 | |
paul@70 | 392 | if (input_items > max_input_items) |
paul@70 | 393 | max_input_items = input_items; |
paul@70 | 394 | } |
paul@70 | 395 | |
paul@70 | 396 | return max_input_items; |
paul@70 | 397 | } |
paul@70 | 398 | |
paul@61 | 399 | /* Return any operator needed to access the given parameter as a local name. */ |
paul@61 | 400 | |
paul@202 | 401 | char *access_name(struct parameter *param, enum component_role component, |
paul@202 | 402 | int writing) |
paul@61 | 403 | { |
paul@61 | 404 | /* In the client, employ the dereferencing operator for "inout" and "out" |
paul@61 | 405 | word parameters since the parameters will employ pointer types. */ |
paul@61 | 406 | |
paul@61 | 407 | if ((component == CLIENT_ROLE) && (param->specifier & OUT_PARAMETER) && |
paul@61 | 408 | (param->cls & WORD_CLASS)) |
paul@61 | 409 | return "*"; |
paul@61 | 410 | |
paul@202 | 411 | /* In the client, employ the dereferencing operator for written "inout" |
paul@202 | 412 | item parameters since the parameters will employ pointer types. */ |
paul@202 | 413 | |
paul@202 | 414 | if ((component == CLIENT_ROLE) && (param->specifier & OUT_PARAMETER) && |
paul@202 | 415 | (param->cls & ITEM_CLASS) && writing) |
paul@202 | 416 | return "*"; |
paul@202 | 417 | |
paul@61 | 418 | /* In the server, when importing items from the message, the address of the |
paul@202 | 419 | variable to hold the item must be used. */ |
paul@61 | 420 | |
paul@61 | 421 | if ((component == SERVER_ROLE) && (param->specifier & IN_PARAMETER) && |
paul@202 | 422 | (param->cls & ITEM_CLASS) && !writing) |
paul@61 | 423 | return "&"; |
paul@61 | 424 | |
paul@61 | 425 | return ""; |
paul@61 | 426 | } |
paul@61 | 427 | |
paul@61 | 428 | /* Return the item type name for the given parameter. */ |
paul@61 | 429 | |
paul@203 | 430 | char *item_type_name(struct parameter *param, enum component_role component, |
paul@203 | 431 | int writing) |
paul@61 | 432 | { |
paul@203 | 433 | if (param->cls == FPAGE_ITEM) |
paul@203 | 434 | { |
paul@204 | 435 | /* Permit the specification of receive window flexpage items. */ |
paul@204 | 436 | |
paul@204 | 437 | if (specify_receive_fpage(param, component, writing)) |
paul@203 | 438 | return "receive_fpage"; |
paul@203 | 439 | else |
paul@203 | 440 | return "fpage"; |
paul@203 | 441 | } |
paul@203 | 442 | else |
paul@203 | 443 | return "capability"; |
paul@61 | 444 | } |
paul@61 | 445 | |
paul@204 | 446 | /* Return whether the parameter involves a receive window flexpage. */ |
paul@204 | 447 | |
paul@204 | 448 | int specify_receive_fpage(struct parameter *param, |
paul@204 | 449 | enum component_role component, int writing) |
paul@204 | 450 | { |
paul@204 | 451 | return (param->cls == FPAGE_ITEM) && |
paul@204 | 452 | (component == CLIENT_ROLE) && |
paul@204 | 453 | !(param->specifier & IN_PARAMETER) && |
paul@204 | 454 | (param->specifier & OUT_PARAMETER) && |
paul@204 | 455 | writing; |
paul@204 | 456 | } |
paul@204 | 457 | |
paul@129 | 458 | /* When copying output items from the message (in the client) or copying into |
paul@129 | 459 | the message (in the server, but only with a new message), the message address |
paul@129 | 460 | must be obtained using the appropriate operator. */ |
paul@61 | 461 | |
paul@129 | 462 | char *reference_message(enum component_role component, |
paul@129 | 463 | enum function_role function) |
paul@61 | 464 | { |
paul@129 | 465 | return (component == CLIENT_ROLE) || (function == COMPLETION_ROLE) ? "&" : ""; |
paul@61 | 466 | } |
paul@61 | 467 | |
paul@61 | 468 | /* Return the naming prefix for structure access to a message. */ |
paul@61 | 469 | |
paul@61 | 470 | char *structure_prefix(enum specifier direction) |
paul@61 | 471 | { |
paul@61 | 472 | return direction == IN_PARAMETER ? "in" : "out"; |
paul@61 | 473 | } |
paul@61 | 474 | |
paul@61 | 475 | /* Return whether message access for the given component role and specifier (or |
paul@61 | 476 | direction) involves writing to (or populating) the message. */ |
paul@61 | 477 | |
paul@61 | 478 | int writing_to_message(enum component_role component, enum specifier direction) |
paul@61 | 479 | { |
paul@61 | 480 | return (component == CLIENT_ROLE) && (direction & IN_PARAMETER) || |
paul@61 | 481 | (component == SERVER_ROLE) && (direction & OUT_PARAMETER); |
paul@61 | 482 | } |