paul@113 | 1 | /* |
paul@113 | 2 | * Generation of interface headers. |
paul@113 | 3 | * |
paul@113 | 4 | * Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> |
paul@113 | 5 | * |
paul@113 | 6 | * This program is free software; you can redistribute it and/or |
paul@113 | 7 | * modify it under the terms of the GNU General Public License as |
paul@113 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@113 | 9 | * the License, or (at your option) any later version. |
paul@113 | 10 | * |
paul@113 | 11 | * This program is distributed in the hope that it will be useful, |
paul@113 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@113 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@113 | 14 | * GNU General Public License for more details. |
paul@113 | 15 | * |
paul@113 | 16 | * You should have received a copy of the GNU General Public License |
paul@113 | 17 | * along with this program; if not, write to the Free Software |
paul@113 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@113 | 19 | * Boston, MA 02110-1301, USA |
paul@113 | 20 | */ |
paul@113 | 21 | |
paul@115 | 22 | #include <stdlib.h> |
paul@113 | 23 | #include "client.h" |
paul@113 | 24 | #include "common.h" |
paul@126 | 25 | #include "config.h" |
paul@113 | 26 | #include "declaration.h" |
paul@113 | 27 | #include "includes.h" |
paul@113 | 28 | #include "interface.h" |
paul@113 | 29 | #include "server.h" |
paul@120 | 30 | #include "structure.h" |
paul@113 | 31 | #include "templates.h" |
paul@113 | 32 | |
paul@113 | 33 | |
paul@113 | 34 | |
paul@113 | 35 | /* Write the definition of an interface to the given file for use by client or |
paul@113 | 36 | server components. Client headers employ such definitions as do server |
paul@113 | 37 | interface headers, whereas server wrapper headers employ other descriptions |
paul@113 | 38 | of their operations. */ |
paul@113 | 39 | |
paul@113 | 40 | void write_interface_definition(struct interface *iface, |
paul@113 | 41 | enum component_role component, FILE *fp) |
paul@113 | 42 | { |
paul@126 | 43 | int cpp = (conf.language == CPP_LANGUAGE); |
paul@114 | 44 | int client = (component == CLIENT_ROLE); |
paul@115 | 45 | int input_items = get_max_input_items(iface->signatures); |
paul@115 | 46 | char *class_name = get_interface_class_name(iface, component); |
paul@117 | 47 | char *name = iface->name; |
paul@113 | 48 | |
paul@113 | 49 | /* Emit include statements. */ |
paul@113 | 50 | |
paul@113 | 51 | write_includes_separator(iface->includes, fp); |
paul@113 | 52 | write_includes(iface->includes, fp); |
paul@113 | 53 | |
paul@120 | 54 | /* Generate interface abstractions. */ |
paul@120 | 55 | |
paul@117 | 56 | if (!cpp) |
paul@115 | 57 | { |
paul@117 | 58 | /* Define an object reference type for C. */ |
paul@115 | 59 | |
paul@117 | 60 | if (!client) |
paul@117 | 61 | fprintf(fp, ref_type_definition_c, L4_CAP_TYPE, name); |
paul@115 | 62 | |
paul@117 | 63 | /* Define C client signatures representing the client functions. */ |
paul@115 | 64 | |
paul@117 | 65 | else |
paul@117 | 66 | write_signatures(iface->signatures, DECLARATION_ROLE, |
paul@117 | 67 | fp, iface, write_client_signature); |
paul@115 | 68 | } |
paul@113 | 69 | |
paul@117 | 70 | /* Begin any namespace, defining the class or type. |
paul@117 | 71 | C++ code employs an abstract server interface and a concrete client |
paul@117 | 72 | interface class. |
paul@117 | 73 | C code only employs a server interface which is instantiated for client |
paul@117 | 74 | use elsewhere by employing the client functions. */ |
paul@117 | 75 | |
paul@117 | 76 | if (cpp || !client) |
paul@117 | 77 | { |
paul@117 | 78 | if (cpp) |
paul@117 | 79 | { |
paul@117 | 80 | fprintf(fp, interface_prologue_cpp, class_name); |
paul@116 | 81 | |
paul@117 | 82 | /* Inherit from the actual interface in the client. */ |
paul@116 | 83 | |
paul@117 | 84 | if (client) |
paul@117 | 85 | fprintf(fp, client_interface_prologue_cpp, name); |
paul@117 | 86 | } |
paul@117 | 87 | else |
paul@117 | 88 | fputs(interface_prologue_c, fp); |
paul@117 | 89 | |
paul@117 | 90 | /* Start the class or type body. */ |
paul@117 | 91 | |
paul@121 | 92 | fputs(interface_body_begin, fp); |
paul@117 | 93 | |
paul@117 | 94 | /* Define any state and initialisation details of a class. */ |
paul@113 | 95 | |
paul@117 | 96 | if (cpp) |
paul@117 | 97 | { |
paul@117 | 98 | if (client) |
paul@117 | 99 | fprintf(fp, client_interface_endpoint_declaration_cpp, L4_CAP_TYPE); |
paul@117 | 100 | |
paul@117 | 101 | fputs(interface_signatures_prologue_cpp, fp); |
paul@117 | 102 | |
paul@117 | 103 | /* Define a constructor. */ |
paul@117 | 104 | |
paul@117 | 105 | if (client) |
paul@117 | 106 | fprintf(fp, client_interface_constructor_cpp, class_name, L4_CAP_TYPE); |
paul@117 | 107 | } |
paul@114 | 108 | |
paul@117 | 109 | /* Emit signatures. */ |
paul@117 | 110 | |
paul@117 | 111 | write_signatures(iface->signatures, MEMBER_DECLARATION_ROLE, |
paul@117 | 112 | fp, iface, |
paul@117 | 113 | client ? write_client_signature |
paul@117 | 114 | : write_server_interface_signature); |
paul@113 | 115 | |
paul@117 | 116 | /* Terminate the class or type body. */ |
paul@117 | 117 | |
paul@117 | 118 | if (cpp) |
paul@117 | 119 | fputs(interface_epilogue_cpp, fp); |
paul@117 | 120 | else |
paul@117 | 121 | fprintf(fp, interface_epilogue_c, name); |
paul@117 | 122 | } |
paul@113 | 123 | |
paul@115 | 124 | /* Define the maximum number of expected items for the interface. */ |
paul@115 | 125 | |
paul@115 | 126 | if (!client) |
paul@117 | 127 | fprintf(fp, expected_items_definition, name, input_items); |
paul@117 | 128 | |
paul@117 | 129 | /* Define an object type for C and declare a client interface. */ |
paul@117 | 130 | |
paul@117 | 131 | if (!cpp && !client) |
paul@117 | 132 | fprintf(fp, object_type_definition_c, name, name, name); |
paul@117 | 133 | |
paul@117 | 134 | if (!cpp && client) |
paul@117 | 135 | fprintf(fp, client_interface_declaration_c, name, name); |
paul@115 | 136 | |
paul@120 | 137 | /* Emit opcodes and message access structures with the generic server |
paul@120 | 138 | interface, these being common to both client and server. */ |
paul@120 | 139 | |
paul@120 | 140 | if (!client) |
paul@120 | 141 | { |
paul@120 | 142 | write_opcode_definition(iface, fp); |
paul@120 | 143 | write_structures(iface->signatures, fp, iface); |
paul@120 | 144 | } |
paul@120 | 145 | |
paul@162 | 146 | /* Emit a conversion macro for compound interface dispatch. */ |
paul@162 | 147 | |
paul@169 | 148 | if (!client) |
paul@169 | 149 | { |
paul@169 | 150 | if (cpp) |
paul@169 | 151 | fprintf(fp, compound_interface_conversion_cpp, name); |
paul@169 | 152 | else |
paul@169 | 153 | fprintf(fp, compound_interface_conversion_c, name, name, name, name); |
paul@169 | 154 | } |
paul@162 | 155 | |
paul@115 | 156 | free(class_name); |
paul@113 | 157 | } |