2 * rpcgen - Generate XDR bindings automatically.
3 * Copyright (C) 2008 Red Hat Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include "rpcgen_int.h"
29 /* If we're debugging the code generator itself, then #line directives
30 * aren't useful - we want to see the error in the generated code
31 * instead. Setting this disables #line directives in the output.
33 #define DEBUG_CODEGEN 0
35 static void gen_line (void);
36 static void gen_decl (int indent, const struct decl *);
37 static void gen_decl_xdr_call (int indent, const struct decl *, const char *);
38 static void gen_type (const struct type *);
45 for (i = 0; i < n; ++i)
49 /* This generates a #line directive referring back to the
50 * original source file.
57 fprintf (yyout, "#line %d \"%s\"\n", yylineno, input_filename);
64 const char *p = strrchr (output_filename, '/') ? : output_filename;
65 const char *q = strrchr (output_filename, '.');
67 while (*p && p != q) {
74 write_basename_caps (void)
76 const char *p = strrchr (output_filename, '/') ? : output_filename;
77 const char *q = strrchr (output_filename, '.');
79 while (*p && p != q) {
81 fputc (toupper (*p), yyout);
89 gen_prologue (const char *filename)
92 "/* This file was generated by PortableXDR rpcgen %s\n"
93 " * ANY CHANGES YOU MAKE TO THIS FILE MAY BE LOST!\n"
94 " * The input file was %s\n"
97 PACKAGE_VERSION, filename);
102 fprintf (yyout, "#include \"");
104 fprintf (yyout, ".h\"\n\n");
108 fprintf (yyout, "#ifndef RPCGEN_HEADER_");
109 write_basename_caps ();
112 "#define RPCGEN_HEADER_");
113 write_basename_caps ();
117 "#ifdef __cplusplus\n"
121 "#include <stdint.h>\n"
122 "#include <rpc/rpc.h>\n"
124 "/* Use the following symbol in your code to detect whether\n"
125 " * PortableXDR's rpcgen was used to compile the source file.\n"
127 "#define PORTABLE_RPCGEN_");
128 write_basename_caps ();
149 "#ifdef __cplusplus\n"
153 "#endif /* RPCGEN_HEADER_");
154 write_basename_caps ();
155 fprintf (yyout, "_H */\n");
159 fprintf (yyout, "\n/* EOF */\n");
163 gen_const (const char *name, const char *value)
165 if (output_mode == output_h) {
168 fprintf (yyout, "#define %s %s\n", name, value);
173 gen_enum (const char *name, const struct cons *enum_values)
180 fprintf (yyout, "enum %s {\n", name);
181 while (enum_values) {
182 struct enum_value *enum_value = (struct enum_value *) enum_values->ptr;
183 if (enum_value->value)
184 fprintf (yyout, " %s = %s,\n",
185 enum_value->ident, enum_value->value);
187 fprintf (yyout, " %s,\n", enum_value->ident);
188 enum_values = enum_values->next;
192 "typedef enum %s %s;\n"
193 "extern bool_t xdr_%s (XDR *, %s *);\n"
195 name, name, name, name);
201 "xdr_%s (XDR *xdrs, %s *objp)\n"
203 " if (!xdr_enum (xdrs, (enum_t *) objp))\n"
213 /* The Sun rpcgen seems to do some sort of inlining optimization based
214 * on {size of struct|number of elements}(?) We don't do any such
215 * optimization. Instead we rely on gcc doing the correct level of
216 * optimization based on inlined functions found in the header files.
219 gen_struct (const char *name, const struct cons *decls)
226 fprintf (yyout, "struct %s {\n", name);
228 gen_decl (2, (struct decl *) decls->ptr);
233 "typedef struct %s %s;\n"
234 "extern bool_t xdr_%s (XDR *, %s *);\n"
236 name, name, name, name);
242 "xdr_%s (XDR *xdrs, %s *objp)\n"
246 gen_decl_xdr_call (2, (struct decl *) decls->ptr, "&objp->");
258 gen_union (const char *name, const struct decl *discrim,
259 const struct cons *union_cases)
269 fprintf (yyout, "struct %s {\n", name);
270 gen_decl (2, discrim);
271 fprintf (yyout, " union {\n");
273 while (union_cases) {
274 struct union_case *uc = (struct union_case *) union_cases->ptr;
275 if (uc->decl) gen_decl (4, uc->decl);
276 union_cases = union_cases->next;
281 "typedef struct %s %s;\n"
282 "extern bool_t xdr_%s (XDR *, %s *);\n"
284 name, name, name, name, name);
290 "xdr_%s (XDR *xdrs, %s *objp)\n"
293 gen_decl_xdr_call (2, discrim, "&objp->");
295 " switch (objp->%s) {\n",
298 len = strlen (name) + 11;
300 snprintf (str, len, "&objp->%s_u.", name);
302 while (union_cases) {
303 struct union_case *uc = (struct union_case *) union_cases->ptr;
307 gen_decl_xdr_call (4, uc->decl, str);
310 union_cases = union_cases->next;
323 gen_typedef (const struct decl *decl)
330 fputs ("typedef ", yyout);
334 "extern bool_t xdr_%s (XDR *, %s *);\n"
336 decl->ident, decl->ident);
342 fprintf (yyout, "xdr_%s (XDR *xdrs, %s *objp)\n",
343 decl->ident, decl->ident);
344 fprintf (yyout, "{\n");
345 gen_decl_xdr_call (2, decl, "&objp->");
355 gen_decl (int indent, const struct decl *decl)
359 switch (decl->decl_type)
361 case decl_type_string:
362 fprintf (yyout, "char *%s;\n", decl->ident);
365 case decl_type_opaque_fixed:
366 fprintf (yyout, "char %s[%s];\n", decl->ident, decl->len);
369 case decl_type_opaque_variable:
370 fprintf (yyout, "struct {\n");
372 fprintf (yyout, "uint32_t %s_len;\n", decl->ident);
374 fprintf (yyout, "char *%s_val;\n", decl->ident);
376 fprintf (yyout, "} %s;\n", decl->ident);
379 case decl_type_simple:
380 gen_type (decl->type);
381 fprintf (yyout, " %s;\n", decl->ident);
384 case decl_type_fixed_array:
385 gen_type (decl->type);
386 fprintf (yyout, " %s[%s];\n", decl->ident, decl->len);
389 case decl_type_variable_array:
390 fprintf (yyout, "struct {\n");
392 fprintf (yyout, "uint32_t %s_len;\n", decl->ident);
394 gen_type (decl->type);
395 fprintf (yyout, " *%s_val;\n", decl->ident);
397 fprintf (yyout, "} %s;\n", decl->ident);
400 case decl_type_pointer:
401 gen_type (decl->type);
402 fprintf (yyout, " *%s;\n", decl->ident);
408 xdr_func_of_simple_type (const struct type *type)
412 /* The caller supplies the "xdr_" prefix. */
413 switch (type->type) {
415 if (type->sgn) r = "char"; else r = "u_char";
418 if (type->sgn) r = "short"; else r = "u_short";
421 if (type->sgn) r = "int"; else r = "u_int";
424 if (type->sgn) r = "quad_t"; else r = "u_quad_t";
435 default: abort (); /* Avoid GCC warning. */
440 /* Caller must free the result. */
442 sizeof_simple_type (const struct type *type)
447 switch (type->type) {
449 r = strdup ("1"); /* Note: fixed by the XDR RFC. */
467 len = strlen (type->ident) + 10;
469 snprintf (str, len, "sizeof (%s)", type->ident);
472 default: abort (); /* Avoid GCC warning. */
479 gen_decl_xdr_call (int indent, const struct decl *decl, const char *struct_name)
486 switch (decl->decl_type)
488 case decl_type_string:
490 fprintf (yyout, "if (!xdr_string (xdrs, objp, %s))\n", decl->len);
492 fprintf (yyout, "if (!xdr_string (xdrs, objp, ~0))\n");
495 case decl_type_opaque_fixed:
496 fprintf (yyout, "if (!xdr_opaque (xdrs, &objp, %s))\n", decl->len);
499 case decl_type_opaque_variable:
500 len_str = decl->len ? : "~0";
502 "if (!xdr_bytes (xdrs, %s%s.%s_val, %s%s.%s_len, %s))\n",
503 struct_name, decl->ident, decl->ident,
504 struct_name, decl->ident, decl->ident, len_str);
507 case decl_type_simple:
508 fprintf (yyout, "if (!xdr_%s (xdrs, %s%s))\n",
509 xdr_func_of_simple_type (decl->type), struct_name, decl->ident);
512 case decl_type_fixed_array:
513 str = sizeof_simple_type (decl->type);
515 "if (!xdr_vector (xdrs, %s%s, %s, %s, (xdrproc_t) xdr_%s))\n",
516 struct_name, decl->ident, decl->len,
517 str, xdr_func_of_simple_type (decl->type));
521 case decl_type_variable_array:
522 str = sizeof_simple_type (decl->type);
523 len_str = decl->len ? : "~0";
525 "if (!xdr_array (xdrs, %s%s.%s_val, %s%s.%s_len, %s, %s, (xdrproc_t) xdr_%s))\n",
526 struct_name, decl->ident, decl->ident,
527 struct_name, decl->ident, decl->ident,
529 str, xdr_func_of_simple_type (decl->type));
533 case decl_type_pointer:
534 str = sizeof_simple_type (decl->type);
535 fprintf (yyout, "if (!xdr_pointer (xdrs, objp, %s, (xdrproc_t) xdr_%s))\n",
536 str, xdr_func_of_simple_type (decl->type));
542 fprintf (yyout, "return FALSE;\n");
546 gen_type (const struct type *type)
551 if (type->sgn) fputs ("int8_t", yyout);
552 else fputs ("uint8_t", yyout);
556 if (type->sgn) fputs ("int16_t", yyout);
557 else fputs ("uint16_t", yyout);
561 if (type->sgn) fputs ("int32_t", yyout);
562 else fputs ("uint32_t", yyout);
566 if (type->sgn) fputs ("int64_t", yyout);
567 else fputs ("uint64_t", yyout);
571 fputs ("double", yyout);
575 fputs ("bool_t", yyout);
579 fputs (type->ident, yyout);