1 /* GLIB-GenMarshal - Marshaller generator for GObject library
2 * Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include <glib-object.h>
31 #include <sys/types.h>
39 #define PRG_NAME "glib-genmarshal"
40 #define PKG_NAME "GLib"
41 #define PKG_HTTP_HOME "http://www.gtk.org"
44 /* --- typedefs & structures --- */
45 typedef struct _Argument Argument;
46 typedef struct _Signature Signature;
49 gchar *pname; /* parsed name */
50 const gchar *sname; /* signature name */
51 const gchar *func; /* functional extension */
52 const gchar *cname; /* C name */
58 GList *args; /* of type Argument* */
62 /* --- prototypes --- */
63 static void parse_args (gint *argc_p,
65 static void print_blurb (FILE *bout,
69 /* --- variables --- */
70 static FILE *fout = NULL;
71 static GScannerConfig scanner_config_template =
74 " \t\r" /* "\n" is statement delimiter */
75 ) /* cset_skip_characters */,
80 ) /* cset_identifier_first */,
85 ) /* cset_identifier_nth */,
86 ( "#\n" ) /* cpair_comment_single */,
88 FALSE /* case_sensitive */,
90 TRUE /* skip_comment_multi */,
91 TRUE /* skip_comment_single */,
92 TRUE /* scan_comment_multi */,
93 TRUE /* scan_identifier */,
94 FALSE /* scan_identifier_1char */,
95 FALSE /* scan_identifier_NULL */,
96 TRUE /* scan_symbols */,
97 FALSE /* scan_binary */,
98 TRUE /* scan_octal */,
99 TRUE /* scan_float */,
101 FALSE /* scan_hex_dollar */,
102 TRUE /* scan_string_sq */,
103 TRUE /* scan_string_dq */,
104 TRUE /* numbers_2_int */,
105 FALSE /* int_2_float */,
106 FALSE /* identifier_2_string */,
107 TRUE /* char_2_token */,
108 FALSE /* symbol_2_token */,
109 FALSE /* scope_0_fallback */,
111 static gchar *marshaller_prefix = "g_cclosure_marshal";
112 static GHashTable *marshallers = NULL;
113 static gboolean gen_cheader = FALSE;
114 static gboolean gen_cbody = FALSE;
115 static gboolean skip_ploc = FALSE;
118 /* --- functions --- */
120 complete_arg (Argument *arg,
123 static const Argument inout_arguments[] = {
124 /* pname, sname, func, cname */
125 { "VOID", "VOID", NULL, "void", },
126 { "BOOLEAN", "BOOLEAN", "boolean", "gboolean", },
127 { "CHAR", "CHAR", "char", "gchar", },
128 { "UCHAR", "UCHAR", "uchar", "guchar", },
129 { "INT", "INT", "int", "gint", },
130 { "UINT", "UINT", "uint", "guint", },
131 { "LONG", "LONG", "long", "glong", },
132 { "ULONG", "ULONG", "ulong", "gulong", },
133 { "ENUM", "ENUM", "enum", "gint", },
134 { "FLAGS", "FLAGS", "flags", "guint", },
135 { "FLOAT", "FLOAT", "float", "gfloat", },
136 { "DOUBLE", "DOUBLE", "double", "gdouble", },
138 { "NONE", "VOID", NULL, "void", },
139 { "BOOL", "BOOLEAN", "boolean", "gboolean", },
141 static const Argument in_arguments[] = {
142 { "STRING", "POINTER", "as_pointer", "gpointer", },
143 { "BOXED", "POINTER", "as_pointer", "gpointer", },
144 { "POINTER", "POINTER", "as_pointer", "gpointer", },
145 { "OBJECT", "POINTER", "as_pointer", "gpointer", },
147 static const Argument out_arguments[] = {
148 { "STRING", "STRING", "string", "gchar*", },
149 { "BOXED", "BOXED", "boxed", "gpointer", },
150 { "POINTER", "POINTER", "pointer", "gpointer", },
151 { "OBJECT", "OBJECT", "object", "GObject*", },
153 const guint n_inout_arguments = sizeof (inout_arguments) / sizeof (inout_arguments[0]);
154 const guint n_out_arguments = sizeof (out_arguments) / sizeof (out_arguments[0]);
155 const guint n_in_arguments = sizeof (in_arguments) / sizeof (in_arguments[0]);
156 const Argument *arguments;
157 guint i, n_arguments;
159 g_return_val_if_fail (arg != NULL, FALSE);
161 arguments = inout_arguments;
162 n_arguments = n_inout_arguments;
163 for (i = 0; i < n_arguments; i++)
164 if (strcmp (arguments[i].pname, arg->pname) == 0)
166 arg->sname = arguments[i].sname;
167 arg->func = arguments[i].func;
168 arg->cname = arguments[i].cname;
172 arguments = is_return ? out_arguments : in_arguments;
173 n_arguments = is_return ? n_out_arguments : n_in_arguments;
174 for (i = 0; i < n_arguments; i++)
175 if (strcmp (arguments[i].pname, arg->pname) == 0)
177 arg->sname = arguments[i].sname;
178 arg->func = arguments[i].func;
179 arg->cname = arguments[i].cname;
188 pad (const gchar *string)
190 #define PAD_LENGTH 12
191 static gchar *buffer = NULL;
194 g_return_val_if_fail (string != NULL, NULL);
197 buffer = g_new (gchar, PAD_LENGTH + 1);
200 if (strlen (string) >= PAD_LENGTH)
203 buffer = g_strdup_printf ("%s ", string);
204 g_warning ("overfull string (%u bytes) for padspace", strlen (string));
209 for (i = 0; i < PAD_LENGTH; i++)
211 gboolean done = *string == 0;
213 buffer[i] = done ? ' ' : *string++;
221 indent (guint n_spaces)
223 static gchar *buffer;
224 static guint blength = 0;
226 if (blength <= n_spaces)
228 blength = n_spaces + 1;
230 buffer = g_new (gchar, blength);
232 memset (buffer, ' ', n_spaces);
233 buffer[n_spaces] = 0;
239 generate_marshal (const gchar *signame,
245 if (g_hash_table_lookup (marshallers, signame))
249 gchar *tmp = g_strdup (signame);
251 g_hash_table_insert (marshallers, tmp, tmp);
256 ind = fprintf (fout, "extern void ");
257 ind += fprintf (fout, "%s_%s (", marshaller_prefix, signame);
258 fprintf (fout, "GClosure *closure,\n");
259 fprintf (fout, "%sGValue *return_value,\n", indent (ind));
260 fprintf (fout, "%sguint n_param_values,\n", indent (ind));
261 fprintf (fout, "%sconst GValue *param_values,\n", indent (ind));
262 fprintf (fout, "%sgpointer invocation_hint,\n", indent (ind));
263 fprintf (fout, "%sgpointer marshal_data);\n", indent (ind));
267 /* cfile marhsal header */
268 fprintf (fout, "void\n");
269 ind = fprintf (fout, "%s_%s (", marshaller_prefix, signame);
270 fprintf (fout, "GClosure *closure,\n");
271 fprintf (fout, "%sGValue *return_value,\n", indent (ind));
272 fprintf (fout, "%sguint n_param_values,\n", indent (ind));
273 fprintf (fout, "%sconst GValue *param_values,\n", indent (ind));
274 fprintf (fout, "%sgpointer invocation_hint,\n", indent (ind));
275 fprintf (fout, "%sgpointer marshal_data)\n", indent (ind));
276 fprintf (fout, "{\n");
278 /* cfile GSignalFunc typedef */
279 ind = fprintf (fout, " typedef %s (*GSignalFunc_%s) (", sig->rarg->cname, signame);
280 fprintf (fout, "%s data1,\n", pad ("gpointer"));
281 for (a = 1, node = sig->args; node; node = node->next)
283 Argument *arg = node->data;
286 fprintf (fout, "%s%s arg_%d,\n", indent (ind), pad (arg->cname), a++);
288 fprintf (fout, "%s%s data2);\n", indent (ind), pad ("gpointer"));
290 /* cfile marshal variables */
291 fprintf (fout, " register GSignalFunc_%s callback;\n", signame);
292 fprintf (fout, " register GCClosure *cc = (GCClosure*) closure;\n");
293 fprintf (fout, " register gpointer data1, data2;\n");
295 fprintf (fout, " %s v_return;\n", sig->rarg->cname);
297 if (sig->args || sig->rarg->func)
299 fprintf (fout, "\n");
302 fprintf (fout, " g_return_if_fail (return_value != NULL);\n");
305 for (a = 0, node = sig->args; node; node = node->next)
307 Argument *arg = node->data;
312 fprintf (fout, " g_return_if_fail (n_param_values >= %u);\n", 1 + a);
316 /* cfile marshal data1, data2 and callback setup */
317 fprintf (fout, "\n");
318 fprintf (fout, " if (G_CCLOSURE_SWAP_DATA (closure))\n {\n");
319 fprintf (fout, " data1 = closure->data;\n");
320 fprintf (fout, " data2 = g_value_get_as_pointer (param_values + 0);\n");
321 fprintf (fout, " }\n else\n {\n");
322 fprintf (fout, " data1 = g_value_get_as_pointer (param_values + 0);\n");
323 fprintf (fout, " data2 = closure->data;\n");
324 fprintf (fout, " }\n");
325 fprintf (fout, " callback = (GSignalFunc_%s) (marshal_data ? marshal_data : cc->callback);\n", signame);
327 /* cfile marshal callback action */
328 fprintf (fout, "\n");
329 ind = fprintf (fout, " %s callback (", sig->rarg->func ? " v_return =" : "");
330 fprintf (fout, "data1,\n");
331 for (a = 1, node = sig->args; node; node = node->next)
333 Argument *arg = node->data;
336 fprintf (fout, "%sg_value_get_%s (param_values + %d),\n", indent (ind), arg->func, a++);
338 fprintf (fout, "%sdata2);\n", indent (ind));
340 /* cfile marshal return value storage */
343 fprintf (fout, "\n");
344 fprintf (fout, " g_value_set_%s (return_value, v_return);\n", sig->rarg->func);
347 /* cfile marshal footer */
348 fprintf (fout, "}\n");
353 process_signature (Signature *sig)
355 gchar *pname, *sname;
358 /* lookup and complete info on arguments */
359 if (!complete_arg (sig->rarg, TRUE))
361 g_warning ("unknown type: %s", sig->rarg->pname);
364 for (node = sig->args; node; node = node->next)
366 Argument *arg = node->data;
368 if (!complete_arg (arg, FALSE))
370 g_warning ("unknown type: %s", arg->pname);
375 /* construct requested marshaller name and technical marshaller name */
376 pname = g_strconcat (sig->rarg->pname, "_", NULL);
377 sname = g_strconcat (sig->rarg->sname, "_", NULL);
378 for (node = sig->args; node; node = node->next)
380 Argument *arg = node->data;
384 sname = g_strconcat (tmp, "_", arg->sname, NULL);
387 pname = g_strconcat (tmp, "_", arg->pname, NULL);
391 /* introductionary comment */
392 fprintf (fout, "\n/* %s", sig->rarg->pname);
393 for (node = sig->args; node; node = node->next)
395 Argument *arg = node->data;
397 fprintf (fout, "%c%s", node->prev ? ',' : ':', arg->pname);
400 fprintf (fout, " (%s)", sig->ploc);
401 fprintf (fout, " */\n");
403 /* generate signature marshaller */
404 generate_marshal (sname, sig);
406 /* put out marshaler alias if required */
407 if (gen_cheader && !g_hash_table_lookup (marshallers, pname))
409 gchar *tmp = g_strdup (pname);
411 fprintf (fout, "#define %s_%s\t%s_%s\n", marshaller_prefix, pname, marshaller_prefix, sname);
413 g_hash_table_insert (marshallers, tmp, tmp);
421 new_arg (const gchar *pname)
423 Argument *arg = g_new0 (Argument, 1);
425 arg->pname = g_strdup (pname);
431 parse_line (GScanner *scanner,
434 /* parse identifier for return value */
435 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER)
436 return G_TOKEN_IDENTIFIER;
437 sig->rarg = new_arg (scanner->value.v_identifier);
439 /* keep a note on the location */
440 sig->ploc = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
443 if (g_scanner_get_next_token (scanner) != ':')
446 /* parse first argument */
447 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER)
448 return G_TOKEN_IDENTIFIER;
449 sig->args = g_list_append (sig->args, new_arg (scanner->value.v_identifier));
451 /* parse rest of argument list */
452 while (g_scanner_peek_next_token (scanner) == ',')
455 g_scanner_get_next_token (scanner);
457 /* parse arg identifier */
458 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER)
459 return G_TOKEN_IDENTIFIER;
460 sig->args = g_list_append (sig->args, new_arg (scanner->value.v_identifier));
463 /* expect end of line, done */
464 if (g_scanner_get_next_token (scanner) != '\n')
472 string_key_destroy (gpointer key,
486 GSList *slist, *files = NULL;
489 /* parse args and do fast exits */
490 parse_args (&argc, &argv);
492 /* list input files */
493 for (i = 1; i < argc; i++)
494 files = g_slist_prepend (files, argv[i]);
496 files = g_slist_reverse (files);
498 files = g_slist_prepend (files, "/dev/stdin");
500 /* setup auxillary structs */
501 scanner = g_scanner_new (&scanner_config_template);
503 marshallers = g_hash_table_new (g_str_hash, g_str_equal);
505 /* process input files */
506 for (slist = files; slist; slist = slist->next)
508 gchar *file = slist->data;
509 gint fd = open (file, O_RDONLY);
513 g_warning ("failed to open \"%s\": %s", file, g_strerror (errno));
517 /* set file name for error reports */
518 scanner->input_name = file;
520 /* parse & process file */
521 g_scanner_input_file (scanner, fd);
523 /* scanning loop, we parse the input untill it's end is reached,
524 * or our sub routine came across invalid syntax
528 guint expected_token = G_TOKEN_NONE;
530 switch (g_scanner_peek_next_token (scanner))
533 /* eat newline and restart */
534 g_scanner_get_next_token (scanner);
540 /* parse and process signatures */
542 Signature signature = { NULL, NULL, NULL };
545 expected_token = parse_line (scanner, &signature);
547 /* once we got a valid signature, process it */
548 if (expected_token == G_TOKEN_NONE)
549 process_signature (&signature);
551 /* clean up signature contents */
552 g_free (signature.ploc);
554 g_free (signature.rarg->pname);
555 g_free (signature.rarg);
556 for (node = signature.args; node; node = node->next)
558 Argument *arg = node->data;
563 g_list_free (signature.args);
568 /* bail out on errors */
569 if (expected_token != G_TOKEN_NONE)
571 g_scanner_unexp_token (scanner, expected_token, "type name", NULL, NULL, NULL, TRUE);
575 g_scanner_peek_next_token (scanner);
577 while (scanner->next_token != G_TOKEN_EOF);
583 g_slist_free (files);
584 g_scanner_destroy (scanner);
585 g_hash_table_foreach_remove (marshallers, string_key_destroy, NULL);
586 g_hash_table_destroy (marshallers);
592 parse_args (gint *argc_p,
595 guint argc = *argc_p;
596 gchar **argv = *argv_p;
599 for (i = 1; i < argc; i++)
601 if (strcmp ("--header", argv[i]) == 0)
606 else if (strcmp ("--body", argv[i]) == 0)
611 else if (strcmp ("--skip-source", argv[i]) == 0)
616 else if ((strcmp ("--prefix", argv[i]) == 0) ||
617 (strncmp ("--prefix=", argv[i], 9) == 0))
619 gchar *equal = argv[i] + 8;
622 marshaller_prefix = g_strdup (equal + 1);
623 else if (i + 1 < argc)
625 marshaller_prefix = g_strdup (argv[i + 1]);
631 else if (strcmp ("-h", argv[i]) == 0 ||
632 strcmp ("--help", argv[i]) == 0)
634 print_blurb (stderr, TRUE);
638 else if (strcmp ("-v", argv[i]) == 0 ||
639 strcmp ("--version", argv[i]) == 0)
641 print_blurb (stderr, FALSE);
645 else if (strcmp (argv[i], "--g-fatal-warnings") == 0)
647 GLogLevelFlags fatal_mask;
649 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
650 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
651 g_log_set_always_fatal (fatal_mask);
658 for (i = 1; i < argc; i++)
676 print_blurb (FILE *bout,
681 fprintf (bout, "%s version ", PRG_NAME);
682 fprintf (bout, "%u.%u.%u", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
683 fprintf (bout, "\n");
684 fprintf (bout, "%s comes with ABSOLUTELY NO WARRANTY.\n", PRG_NAME);
685 fprintf (bout, "You may redistribute copies of %s under the terms of\n", PRG_NAME);
686 fprintf (bout, "the GNU General Public License which can be found in the\n");
687 fprintf (bout, "%s source package. Sources, examples and contact\n", PKG_NAME);
688 fprintf (bout, "information are available at %s\n", PKG_HTTP_HOME);
692 fprintf (bout, "Usage: %s [options] [files...]\n", PRG_NAME);
693 fprintf (bout, " --header generate C headers\n");
694 fprintf (bout, " --body generate C code\n");
695 fprintf (bout, " --prefix=string specify marshaller prefix\n");
696 fprintf (bout, " --skip-source skip source location comments\n");
697 fprintf (bout, " -h, --help show this help message\n");
698 fprintf (bout, " -v, --version print version informations\n");
699 fprintf (bout, " --g-fatal-warnings make warnings fatal (abort)\n");