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