paul@44 | 1 | /* |
paul@44 | 2 | * Common routines. |
paul@44 | 3 | * |
paul@44 | 4 | * Copyright (C) 2019 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@44 | 37 | /* Obtain a new basename string. */ |
paul@44 | 38 | |
paul@44 | 39 | char *make_basename(const char *path) |
paul@44 | 40 | { |
paul@44 | 41 | char s[strlen(path) + 1]; |
paul@44 | 42 | |
paul@44 | 43 | strcpy(s, path); |
paul@44 | 44 | return strdup(basename(s)); |
paul@44 | 45 | } |
paul@44 | 46 | |
paul@44 | 47 | /* Obtain a new directory name string. */ |
paul@44 | 48 | |
paul@44 | 49 | char *make_dirname(const char *path) |
paul@44 | 50 | { |
paul@44 | 51 | char s[strlen(path) + 1]; |
paul@44 | 52 | |
paul@44 | 53 | strcpy(s, path); |
paul@44 | 54 | return strdup(dirname(s)); |
paul@44 | 55 | } |
paul@44 | 56 | |
paul@44 | 57 | /* Obtain a file pointer using a parameterised filename. */ |
paul@44 | 58 | |
paul@44 | 59 | FILE *get_output_file(const char *filename_template, const char *dir, const char *name) |
paul@44 | 60 | { |
paul@44 | 61 | FILE *fp; |
paul@44 | 62 | char *filename; |
paul@44 | 63 | |
paul@44 | 64 | if (asprintf(&filename, filename_template, dir, name) == -1) |
paul@44 | 65 | fp = NULL; |
paul@44 | 66 | else |
paul@44 | 67 | { |
paul@44 | 68 | fp = fopen(filename, "w"); |
paul@44 | 69 | free(filename); |
paul@44 | 70 | } |
paul@44 | 71 | |
paul@44 | 72 | /* Write an error message upon failure. */ |
paul@44 | 73 | |
paul@44 | 74 | if (fp == NULL) |
paul@44 | 75 | open_error(dir, name); |
paul@44 | 76 | |
paul@44 | 77 | return fp; |
paul@44 | 78 | } |
paul@44 | 79 | |
paul@44 | 80 | /* Find an attribute value. */ |
paul@44 | 81 | |
paul@51 | 82 | char *get_attribute_value(struct attribute *attr, const char *name) |
paul@44 | 83 | { |
paul@51 | 84 | if (attr == NULL) |
paul@44 | 85 | return NULL; |
paul@44 | 86 | |
paul@51 | 87 | if (!strcmp(attr->attribute, name) && (attr->identifiers != NULL)) |
paul@51 | 88 | return attr->identifiers->identifier; |
paul@44 | 89 | |
paul@51 | 90 | return get_attribute_value(attr->tail, name); |
paul@44 | 91 | } |
paul@44 | 92 | |
paul@162 | 93 | /* Return any conversion to a different object type in a dispatcher. */ |
paul@162 | 94 | |
paul@162 | 95 | char *get_object_conversion(struct interface *iface, int compound) |
paul@162 | 96 | { |
paul@162 | 97 | char *conversion; |
paul@162 | 98 | |
paul@162 | 99 | if (!compound) |
paul@162 | 100 | return strdup("_self"); |
paul@162 | 101 | |
paul@162 | 102 | if (asprintf(&conversion, "convert_to_%s(_self)", iface->name) == -1) |
paul@162 | 103 | conversion = NULL; |
paul@162 | 104 | |
paul@162 | 105 | return conversion; |
paul@162 | 106 | } |
paul@162 | 107 | |
paul@44 | 108 | /* Return any opcode attribute value. */ |
paul@44 | 109 | |
paul@51 | 110 | char *get_opcode(struct attribute *attr) |
paul@44 | 111 | { |
paul@51 | 112 | return get_attribute_value(attr, "opcode"); |
paul@44 | 113 | } |
paul@44 | 114 | |
paul@44 | 115 | /* Return any protocol attribute value. */ |
paul@44 | 116 | |
paul@51 | 117 | char *get_protocol(struct attribute *attr) |
paul@44 | 118 | { |
paul@51 | 119 | return get_attribute_value(attr, "protocol"); |
paul@44 | 120 | } |
paul@44 | 121 | |
paul@125 | 122 | /* Return the wrapper function prefix. */ |
paul@125 | 123 | |
paul@125 | 124 | char *get_operation_wrapper_prefix(struct attribute *attr) |
paul@125 | 125 | { |
paul@125 | 126 | if (have_attribute(attr, "completion")) |
paul@125 | 127 | return "initiate"; |
paul@125 | 128 | else |
paul@125 | 129 | return "wrap"; |
paul@125 | 130 | } |
paul@125 | 131 | |
paul@125 | 132 | int have_attribute(struct attribute *attr, const char *name) |
paul@125 | 133 | { |
paul@125 | 134 | if (attr == NULL) |
paul@125 | 135 | return 0; |
paul@125 | 136 | |
paul@125 | 137 | if (!strcmp(attr->attribute, name)) |
paul@125 | 138 | return 1; |
paul@125 | 139 | |
paul@125 | 140 | return have_attribute(attr->tail, name); |
paul@125 | 141 | } |
paul@125 | 142 | |
paul@132 | 143 | int have_attribute_value(struct attribute *attr, const char *name, |
paul@132 | 144 | const char *value) |
paul@132 | 145 | { |
paul@132 | 146 | if (attr == NULL) |
paul@132 | 147 | return 0; |
paul@132 | 148 | |
paul@132 | 149 | if (!strcmp(attr->attribute, name) && have_identifier(attr->identifiers, value)) |
paul@132 | 150 | return 1; |
paul@132 | 151 | |
paul@132 | 152 | return have_attribute_value(attr->tail, name, value); |
paul@132 | 153 | } |
paul@132 | 154 | |
paul@132 | 155 | int have_identifier(struct identifier *ident, const char *value) |
paul@132 | 156 | { |
paul@132 | 157 | if (ident == NULL) |
paul@132 | 158 | return 0; |
paul@132 | 159 | |
paul@132 | 160 | if (!strcmp(ident->identifier, value)) |
paul@132 | 161 | return 1; |
paul@132 | 162 | |
paul@132 | 163 | return have_identifier(ident->tail, value); |
paul@132 | 164 | } |
paul@132 | 165 | |
paul@115 | 166 | /* Obtain a suitable interface class name depending on the component role. */ |
paul@115 | 167 | |
paul@115 | 168 | char *get_interface_class_name(struct interface *iface, |
paul@115 | 169 | enum component_role component) |
paul@115 | 170 | { |
paul@115 | 171 | char *name; |
paul@115 | 172 | |
paul@115 | 173 | switch (component) |
paul@115 | 174 | { |
paul@115 | 175 | case CLIENT_ROLE: |
paul@117 | 176 | if (asprintf(&name, "client_%s", iface->name) == -1) |
paul@115 | 177 | name = NULL; |
paul@115 | 178 | break; |
paul@115 | 179 | |
paul@115 | 180 | case SERVER_ROLE: |
paul@115 | 181 | name = strdup(iface->name); |
paul@115 | 182 | break; |
paul@115 | 183 | |
paul@115 | 184 | default: |
paul@115 | 185 | name = NULL; |
paul@117 | 186 | break; |
paul@115 | 187 | } |
paul@115 | 188 | |
paul@115 | 189 | return name; |
paul@115 | 190 | } |
paul@115 | 191 | |
paul@110 | 192 | /* Generate a qualified operation name for declarations and definitions. */ |
paul@72 | 193 | |
paul@110 | 194 | char *get_signature_operation_name(struct interface *iface, |
paul@110 | 195 | struct signature *sig, |
paul@115 | 196 | enum component_role component, |
paul@110 | 197 | enum signature_role role, enum language lang) |
paul@72 | 198 | { |
paul@115 | 199 | char *opname, *class_name; |
paul@72 | 200 | |
paul@72 | 201 | switch (lang) |
paul@72 | 202 | { |
paul@117 | 203 | /* In C, interface members use the bare names. */ |
paul@117 | 204 | |
paul@72 | 205 | case C_LANGUAGE: |
paul@117 | 206 | if ((role == MEMBER_DECLARATION_ROLE) || (role == ACCESS_ROLE)) |
paul@117 | 207 | opname = strdup(sig->operation); |
paul@117 | 208 | else |
paul@117 | 209 | opname = get_operation_name(iface, sig); |
paul@117 | 210 | break; |
paul@117 | 211 | |
paul@117 | 212 | /* In C++, method definitions are qualified using class names. */ |
paul@72 | 213 | |
paul@72 | 214 | case CPP_LANGUAGE: |
paul@110 | 215 | if (role == DEFINITION_ROLE) |
paul@110 | 216 | { |
paul@115 | 217 | class_name = get_interface_class_name(iface, component); |
paul@115 | 218 | |
paul@115 | 219 | if (asprintf(&opname, "%s::%s", class_name, sig->operation) == -1) |
paul@110 | 220 | opname = NULL; |
paul@115 | 221 | |
paul@115 | 222 | free(class_name); |
paul@110 | 223 | } |
paul@110 | 224 | else |
paul@110 | 225 | opname = strdup(sig->operation); |
paul@72 | 226 | break; |
paul@72 | 227 | |
paul@72 | 228 | default: |
paul@72 | 229 | opname = NULL; |
paul@72 | 230 | break; |
paul@72 | 231 | } |
paul@72 | 232 | |
paul@72 | 233 | return opname; |
paul@72 | 234 | } |
paul@72 | 235 | |
paul@117 | 236 | /* Generate a qualified operation name for structure and function naming. */ |
paul@44 | 237 | |
paul@44 | 238 | char *get_operation_name(struct interface *iface, struct signature *sig) |
paul@44 | 239 | { |
paul@44 | 240 | char *opname; |
paul@44 | 241 | |
paul@44 | 242 | if (asprintf(&opname, "%s_%s", iface->name, sig->operation) == -1) |
paul@72 | 243 | opname = NULL; |
paul@44 | 244 | |
paul@44 | 245 | return opname; |
paul@44 | 246 | } |
paul@44 | 247 | |
paul@44 | 248 | /* Return the invocation opcode given protocol and operation name details. */ |
paul@44 | 249 | |
paul@44 | 250 | char *get_opcode_identifier(char *protocol, char *opname) |
paul@44 | 251 | { |
paul@44 | 252 | char *opcode; |
paul@44 | 253 | |
paul@44 | 254 | if (protocol != NULL) |
paul@44 | 255 | return protocol; |
paul@44 | 256 | |
paul@44 | 257 | if (asprintf(&opcode, "opcode_%s", opname) == -1) |
paul@44 | 258 | return NULL; |
paul@44 | 259 | |
paul@44 | 260 | return opcode; |
paul@44 | 261 | } |
paul@44 | 262 | |
paul@129 | 263 | /* Return the appropriate parameter decoration. Generally, output parameters |
paul@129 | 264 | involve addresses. However, completion functions accept output parameters as |
paul@129 | 265 | values. */ |
paul@125 | 266 | |
paul@125 | 267 | const char *get_parameter_decoration(struct parameter *param, |
paul@129 | 268 | enum parameter_role role, |
paul@129 | 269 | enum function_role function) |
paul@125 | 270 | { |
paul@129 | 271 | if ((param->specifier & OUT_PARAMETER) && (function != COMPLETION_ROLE)) |
paul@125 | 272 | { |
paul@125 | 273 | switch (role) |
paul@125 | 274 | { |
paul@125 | 275 | case SIGNATURE_ROLE: return "*"; |
paul@125 | 276 | case INVOCATION_ROLE: return "&"; |
paul@125 | 277 | default: break; |
paul@125 | 278 | } |
paul@125 | 279 | } |
paul@125 | 280 | |
paul@125 | 281 | return ""; |
paul@125 | 282 | } |
paul@125 | 283 | |
paul@44 | 284 | /* Return the parameter name, being the final identifier in a list. */ |
paul@44 | 285 | |
paul@51 | 286 | char *get_parameter_name(struct identifier *ident) |
paul@44 | 287 | { |
paul@44 | 288 | if (ident->tail != NULL) |
paul@44 | 289 | return get_parameter_name(ident->tail); |
paul@44 | 290 | else |
paul@44 | 291 | return ident->identifier; |
paul@44 | 292 | } |
paul@44 | 293 | |
paul@110 | 294 | /* Get the appropriate terminating text after a signature. */ |
paul@110 | 295 | |
paul@110 | 296 | char *get_signature_terminator(enum signature_role role) |
paul@110 | 297 | { |
paul@117 | 298 | /* Signatures in declarations or member initialisation. */ |
paul@117 | 299 | |
paul@117 | 300 | if (role & DECLARATION_ROLE) |
paul@117 | 301 | return COMPLETE_SIGNATURE; |
paul@117 | 302 | |
paul@117 | 303 | /* Signatures at the start of functions. */ |
paul@117 | 304 | |
paul@117 | 305 | if (role & DEFINITION_ROLE) |
paul@117 | 306 | return BEGIN_FUNCTION; |
paul@117 | 307 | |
paul@117 | 308 | return ""; |
paul@110 | 309 | } |
paul@110 | 310 | |
paul@44 | 311 | /* Count the different kinds of parameters in terms of inputs, outputs, words |
paul@44 | 312 | and items. */ |
paul@44 | 313 | |
paul@51 | 314 | void count_parameters(struct parameter *param, int *input_words, |
paul@57 | 315 | int *input_items, int *output_words, int *output_items) |
paul@44 | 316 | { |
paul@51 | 317 | for (*input_words = 0, *input_items = 0, *output_words = 0, *output_items = 0; |
paul@51 | 318 | param != NULL; |
paul@51 | 319 | param = param->tail) |
paul@44 | 320 | { |
paul@44 | 321 | if (param->specifier & IN_PARAMETER) |
paul@44 | 322 | { |
paul@57 | 323 | if (param->cls & ITEM_CLASS) (*input_items)++; |
paul@44 | 324 | else (*input_words)++; |
paul@44 | 325 | } |
paul@129 | 326 | |
paul@129 | 327 | if (param->specifier & OUT_PARAMETER) |
paul@44 | 328 | { |
paul@57 | 329 | if (param->cls & ITEM_CLASS) (*output_items)++; |
paul@44 | 330 | else (*output_words)++; |
paul@44 | 331 | } |
paul@44 | 332 | } |
paul@44 | 333 | } |
paul@61 | 334 | |
paul@70 | 335 | /* Return the maximum number of input items accepted by any one of the given |
paul@70 | 336 | signatures. */ |
paul@70 | 337 | |
paul@70 | 338 | int get_max_input_items(struct signature *sig) |
paul@70 | 339 | { |
paul@70 | 340 | int input_words, input_items, output_words, output_items; |
paul@70 | 341 | int max_input_items = 0; |
paul@70 | 342 | |
paul@70 | 343 | for (; sig != NULL; sig = sig->tail) |
paul@70 | 344 | { |
paul@70 | 345 | count_parameters(sig->parameters, &input_words, &input_items, &output_words, &output_items); |
paul@70 | 346 | |
paul@70 | 347 | if (input_items > max_input_items) |
paul@70 | 348 | max_input_items = input_items; |
paul@70 | 349 | } |
paul@70 | 350 | |
paul@70 | 351 | return max_input_items; |
paul@70 | 352 | } |
paul@70 | 353 | |
paul@61 | 354 | /* Return any operator needed to access the given parameter as a local name. */ |
paul@61 | 355 | |
paul@61 | 356 | char *access_name(struct parameter *param, enum component_role component) |
paul@61 | 357 | { |
paul@61 | 358 | /* In the client, employ the dereferencing operator for "inout" and "out" |
paul@61 | 359 | word parameters since the parameters will employ pointer types. */ |
paul@61 | 360 | |
paul@61 | 361 | if ((component == CLIENT_ROLE) && (param->specifier & OUT_PARAMETER) && |
paul@61 | 362 | (param->cls & WORD_CLASS)) |
paul@61 | 363 | return "*"; |
paul@61 | 364 | |
paul@61 | 365 | /* In the server, when importing items from the message, the address of the |
paul@61 | 366 | variable to hold the capability value must be used. */ |
paul@61 | 367 | |
paul@61 | 368 | if ((component == SERVER_ROLE) && (param->specifier & IN_PARAMETER) && |
paul@61 | 369 | (param->cls & ITEM_CLASS)) |
paul@61 | 370 | return "&"; |
paul@61 | 371 | |
paul@61 | 372 | return ""; |
paul@61 | 373 | } |
paul@61 | 374 | |
paul@61 | 375 | /* Return the item type name for the given parameter. */ |
paul@61 | 376 | |
paul@61 | 377 | char *item_type_name(struct parameter *param) |
paul@61 | 378 | { |
paul@76 | 379 | return param->cls == FPAGE_ITEM ? "fpage" : "capability"; |
paul@61 | 380 | } |
paul@61 | 381 | |
paul@129 | 382 | /* When copying output items from the message (in the client) or copying into |
paul@129 | 383 | the message (in the server, but only with a new message), the message address |
paul@129 | 384 | must be obtained using the appropriate operator. */ |
paul@61 | 385 | |
paul@129 | 386 | char *reference_message(enum component_role component, |
paul@129 | 387 | enum function_role function) |
paul@61 | 388 | { |
paul@129 | 389 | return (component == CLIENT_ROLE) || (function == COMPLETION_ROLE) ? "&" : ""; |
paul@61 | 390 | } |
paul@61 | 391 | |
paul@61 | 392 | /* Return the naming prefix for structure access to a message. */ |
paul@61 | 393 | |
paul@61 | 394 | char *structure_prefix(enum specifier direction) |
paul@61 | 395 | { |
paul@61 | 396 | return direction == IN_PARAMETER ? "in" : "out"; |
paul@61 | 397 | } |
paul@61 | 398 | |
paul@61 | 399 | /* Return whether message access for the given component role and specifier (or |
paul@61 | 400 | direction) involves writing to (or populating) the message. */ |
paul@61 | 401 | |
paul@61 | 402 | int writing_to_message(enum component_role component, enum specifier direction) |
paul@61 | 403 | { |
paul@61 | 404 | return (component == CLIENT_ROLE) && (direction & IN_PARAMETER) || |
paul@61 | 405 | (component == SERVER_ROLE) && (direction & OUT_PARAMETER); |
paul@61 | 406 | } |