glib-genmarshal.c 34.9 KB
Newer Older
1
/* GLIB-GenMarshal - Marshaller generator for GObject library
2
 * Copyright (C) 2000-2001 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
19

20
#include "config.h"
21

22 23 24 25 26 27
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
28

29
#include <glib/gstdio.h>
30

31 32 33 34 35
#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "GLib-Genmarshal"
#include <glib.h>
#include <glib/gprintf.h>

36 37 38
#ifdef G_OS_UNIX
#include <unistd.h>
#endif
39 40 41 42
#ifdef G_OS_WIN32
#include <io.h>
#endif

43 44 45 46 47 48 49
/* --- defines --- */
#define	PRG_NAME	"glib-genmarshal"
#define	PKG_NAME	"GLib"
#define PKG_HTTP_HOME	"http://www.gtk.org"


/* --- typedefs & structures --- */
50
typedef struct
51
{
52 53 54
  gchar	      *keyword;		/* marhaller list keyword [MY_STRING] */
  const gchar *sig_name;	/* signature name [STRING] */
  const gchar *ctype;		/* C type name [gchar*] */
55
  const gchar *promoted_ctype;	/* promoted C type name [gchar*] */
56
  const gchar *getter;		/* value getter function [g_value_get_string] */
57 58 59 60
  const gchar *box;		/* value box function [g_strdup] */
  const gchar *unbox;		/* value unbox function [g_free] */
  gboolean     box_ignores_static;  /* Wether the box/unbox functions ignore the static_scope */
  gboolean     box_takes_type;  /* Wether the box/unbox functions take a type arg */
61 62
} InArgument;
typedef struct
63
{
64 65 66 67 68 69 70 71 72 73 74
  gchar	      *keyword;		/* marhaller list keyword [MY_STRING] */
  const gchar *sig_name;	/* signature name [STRING] */
  const gchar *ctype;		/* C type name [gchar*] */
  const gchar *setter;		/* value setter function [g_value_set_string] */
} OutArgument;
typedef struct
{
  gchar       *ploc;
  OutArgument *rarg;
  GList       *args;	/* of type InArgument* */
} Signature;
75 76 77 78 79 80 81 82 83 84


/* --- prototypes --- */
static void	parse_args	(gint	   	*argc_p,
				 gchar	     ***argv_p);
static void	print_blurb	(FILE	       	*bout,
				 gboolean	 print_help);


/* --- variables --- */
85
static const GScannerConfig scanner_config_template =
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
{
  (
   " \t\r"		/* "\n" is statement delimiter */
   )                    /* cset_skip_characters */,
  (
   G_CSET_a_2_z
   "_"
   G_CSET_A_2_Z
   )                    /* cset_identifier_first */,
  (
   G_CSET_a_2_z
   "_0123456789"
   G_CSET_A_2_Z
   )                    /* cset_identifier_nth */,
  ( "#\n" )             /* cpair_comment_single */,

  FALSE                 /* case_sensitive */,

  TRUE                  /* skip_comment_multi */,
  TRUE                  /* skip_comment_single */,
  TRUE                  /* scan_comment_multi */,
  TRUE                  /* scan_identifier */,
  FALSE                 /* scan_identifier_1char */,
  FALSE                 /* scan_identifier_NULL */,
  TRUE                  /* scan_symbols */,
  FALSE                 /* scan_binary */,
  TRUE                  /* scan_octal */,
  TRUE                  /* scan_float */,
  TRUE                  /* scan_hex */,
  FALSE                 /* scan_hex_dollar */,
  TRUE                  /* scan_string_sq */,
  TRUE                  /* scan_string_dq */,
  TRUE                  /* numbers_2_int */,
  FALSE                 /* int_2_float */,
  FALSE                 /* identifier_2_string */,
  TRUE                  /* char_2_token */,
  FALSE                 /* symbol_2_token */,
  FALSE                 /* scope_0_fallback */,
};
125
static gchar		* const std_marshaller_prefix = "g_cclosure_marshal";
126
static gchar		*marshaller_prefix = "g_cclosure_user_marshal";
127
static GHashTable	*marshallers = NULL;
128
static FILE             *fout = NULL;
129 130
static gboolean		 gen_cheader = FALSE;
static gboolean		 gen_cbody = FALSE;
131
static gboolean          gen_internal = FALSE;
132
static gboolean		 gen_valist = FALSE;
133
static gboolean		 skip_ploc = FALSE;
134
static gboolean		 std_includes = TRUE;
135
static gint              exit_status = 0;
136 137 138


/* --- functions --- */
139 140 141 142 143 144
static void
put_marshal_value_getters (void)
{
  fputs ("\n", fout);
  fputs ("#ifdef G_ENABLE_DEBUG\n", fout);
  fputs ("#define g_marshal_value_peek_boolean(v)  g_value_get_boolean (v)\n", fout);
145
  fputs ("#define g_marshal_value_peek_char(v)     g_value_get_schar (v)\n", fout);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  fputs ("#define g_marshal_value_peek_uchar(v)    g_value_get_uchar (v)\n", fout);
  fputs ("#define g_marshal_value_peek_int(v)      g_value_get_int (v)\n", fout);
  fputs ("#define g_marshal_value_peek_uint(v)     g_value_get_uint (v)\n", fout);
  fputs ("#define g_marshal_value_peek_long(v)     g_value_get_long (v)\n", fout);
  fputs ("#define g_marshal_value_peek_ulong(v)    g_value_get_ulong (v)\n", fout);
  fputs ("#define g_marshal_value_peek_int64(v)    g_value_get_int64 (v)\n", fout);
  fputs ("#define g_marshal_value_peek_uint64(v)   g_value_get_uint64 (v)\n", fout);
  fputs ("#define g_marshal_value_peek_enum(v)     g_value_get_enum (v)\n", fout);
  fputs ("#define g_marshal_value_peek_flags(v)    g_value_get_flags (v)\n", fout);
  fputs ("#define g_marshal_value_peek_float(v)    g_value_get_float (v)\n", fout);
  fputs ("#define g_marshal_value_peek_double(v)   g_value_get_double (v)\n", fout);
  fputs ("#define g_marshal_value_peek_string(v)   (char*) g_value_get_string (v)\n", fout);
  fputs ("#define g_marshal_value_peek_param(v)    g_value_get_param (v)\n", fout);
  fputs ("#define g_marshal_value_peek_boxed(v)    g_value_get_boxed (v)\n", fout);
  fputs ("#define g_marshal_value_peek_pointer(v)  g_value_get_pointer (v)\n", fout);
  fputs ("#define g_marshal_value_peek_object(v)   g_value_get_object (v)\n", fout);
162
  fputs ("#define g_marshal_value_peek_variant(v)  g_value_get_variant (v)\n", fout);
163 164 165 166 167 168 169 170 171 172 173 174 175 176
  fputs ("#else /* !G_ENABLE_DEBUG */\n", fout);
  fputs ("/* WARNING: This code accesses GValues directly, which is UNSUPPORTED API.\n", fout);
  fputs (" *          Do not access GValues directly in your code. Instead, use the\n", fout);
  fputs (" *          g_value_get_*() functions\n", fout);
  fputs (" */\n", fout);
  fputs ("#define g_marshal_value_peek_boolean(v)  (v)->data[0].v_int\n", fout);
  fputs ("#define g_marshal_value_peek_char(v)     (v)->data[0].v_int\n", fout);
  fputs ("#define g_marshal_value_peek_uchar(v)    (v)->data[0].v_uint\n", fout);
  fputs ("#define g_marshal_value_peek_int(v)      (v)->data[0].v_int\n", fout);
  fputs ("#define g_marshal_value_peek_uint(v)     (v)->data[0].v_uint\n", fout);
  fputs ("#define g_marshal_value_peek_long(v)     (v)->data[0].v_long\n", fout);
  fputs ("#define g_marshal_value_peek_ulong(v)    (v)->data[0].v_ulong\n", fout);
  fputs ("#define g_marshal_value_peek_int64(v)    (v)->data[0].v_int64\n", fout);
  fputs ("#define g_marshal_value_peek_uint64(v)   (v)->data[0].v_uint64\n", fout);
177 178
  fputs ("#define g_marshal_value_peek_enum(v)     (v)->data[0].v_long\n", fout);
  fputs ("#define g_marshal_value_peek_flags(v)    (v)->data[0].v_ulong\n", fout);
179 180 181 182 183 184 185
  fputs ("#define g_marshal_value_peek_float(v)    (v)->data[0].v_float\n", fout);
  fputs ("#define g_marshal_value_peek_double(v)   (v)->data[0].v_double\n", fout);
  fputs ("#define g_marshal_value_peek_string(v)   (v)->data[0].v_pointer\n", fout);
  fputs ("#define g_marshal_value_peek_param(v)    (v)->data[0].v_pointer\n", fout);
  fputs ("#define g_marshal_value_peek_boxed(v)    (v)->data[0].v_pointer\n", fout);
  fputs ("#define g_marshal_value_peek_pointer(v)  (v)->data[0].v_pointer\n", fout);
  fputs ("#define g_marshal_value_peek_object(v)   (v)->data[0].v_pointer\n", fout);
186
  fputs ("#define g_marshal_value_peek_variant(v)  (v)->data[0].v_pointer\n", fout);
187 188 189 190
  fputs ("#endif /* !G_ENABLE_DEBUG */\n", fout);
  fputs ("\n", fout);
}

191
static gboolean
192
complete_in_arg (InArgument *iarg)
193
{
194
  static const InArgument args[] = {
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    /* keyword		sig_name	ctype		promoted        getter			*/
    { "VOID",		"VOID",		"void",		"void",		NULL,			},
    { "BOOLEAN",	"BOOLEAN",	"gboolean",	"gboolean",	"g_marshal_value_peek_boolean",	},
    { "CHAR",		"CHAR",		"gchar",	"gint",		"g_marshal_value_peek_char",	},
    { "UCHAR",		"UCHAR",	"guchar",	"guint",	"g_marshal_value_peek_uchar",	},
    { "INT",		"INT",		"gint",		"gint",		"g_marshal_value_peek_int",	},
    { "UINT",		"UINT",		"guint",	"guint",	"g_marshal_value_peek_uint",	},
    { "LONG",		"LONG",		"glong",	"glong",	"g_marshal_value_peek_long",	},
    { "ULONG",		"ULONG",	"gulong",	"gulong",	"g_marshal_value_peek_ulong",	},
    { "INT64",		"INT64",	"gint64",       "gint64",	"g_marshal_value_peek_int64",	},
    { "UINT64",		"UINT64",	"guint64",	"guint64",	"g_marshal_value_peek_uint64",	},
    { "ENUM",		"ENUM",		"gint",		"gint",		"g_marshal_value_peek_enum",	},
    { "FLAGS",		"FLAGS",	"guint",	"guint",	"g_marshal_value_peek_flags",	},
    { "FLOAT",		"FLOAT",	"gfloat",	"gdouble",	"g_marshal_value_peek_float",	},
    { "DOUBLE",		"DOUBLE",	"gdouble",	"gdouble",	"g_marshal_value_peek_double",	},
    { "STRING",		"STRING",	"gpointer",	"gpointer",	"g_marshal_value_peek_string",	"g_strdup", "g_free"},
    { "PARAM",		"PARAM",	"gpointer",	"gpointer",	"g_marshal_value_peek_param",	"g_param_spec_ref", "g_param_spec_unref"},
    { "BOXED",		"BOXED",	"gpointer",	"gpointer",	"g_marshal_value_peek_boxed",	"g_boxed_copy", "g_boxed_free", FALSE, TRUE},
    { "POINTER",	"POINTER",	"gpointer",	"gpointer",	"g_marshal_value_peek_pointer",	},
    { "OBJECT",		"OBJECT",	"gpointer",	"gpointer",	"g_marshal_value_peek_object",	"g_object_ref", "g_object_unref", TRUE},
    { "VARIANT",	"VARIANT",	"gpointer",	"gpointer",	"g_marshal_value_peek_variant",	"g_variant_ref_sink", "g_variant_unref"},
216
    /* deprecated: */
217 218
    { "NONE",		"VOID",		"void",		"void",		NULL,			},
    { "BOOL",		"BOOLEAN",	"gboolean",	"gboolean",	"g_marshal_value_peek_boolean",	},
219
  };
220 221 222 223
  guint i;

  g_return_val_if_fail (iarg != NULL, FALSE);

224
  for (i = 0; i < G_N_ELEMENTS (args); i++)
225
    if (strcmp (args[i].keyword, iarg->keyword) == 0)
226
      {
227 228
	iarg->sig_name = args[i].sig_name;
	iarg->ctype = args[i].ctype;
229
	iarg->promoted_ctype = args[i].promoted_ctype;
230
	iarg->getter = args[i].getter;
231 232 233 234
	iarg->box = args[i].box;
	iarg->unbox = args[i].unbox;
	iarg->box_ignores_static = args[i].box_ignores_static;
	iarg->box_takes_type = args[i].box_takes_type;
235 236 237

	return TRUE;
      }
238 239 240 241 242 243 244
  return FALSE;
}

static gboolean
complete_out_arg (OutArgument *oarg)
{
  static const OutArgument args[] = {
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    /* keyword		sig_name	ctype		setter			*/
    { "VOID",		"VOID",		"void",		NULL,					     },
    { "BOOLEAN",	"BOOLEAN",	"gboolean",	"g_value_set_boolean",			     },
    { "CHAR",		"CHAR",		"gchar",	"g_value_set_char",			     },
    { "UCHAR",		"UCHAR",	"guchar",	"g_value_set_uchar",			     },
    { "INT",		"INT",		"gint",		"g_value_set_int",			     },
    { "UINT",		"UINT",		"guint",	"g_value_set_uint",			     },
    { "LONG",		"LONG",		"glong",	"g_value_set_long",			     },
    { "ULONG",		"ULONG",	"gulong",	"g_value_set_ulong",			     },
    { "INT64",		"INT64",	"gint64",	"g_value_set_int64",			     },
    { "UINT64",		"UINT64",	"guint64",	"g_value_set_uint64",			     },
    { "ENUM",		"ENUM",		"gint",		"g_value_set_enum",			     },
    { "FLAGS",		"FLAGS",	"guint",	"g_value_set_flags",			     },
    { "FLOAT",		"FLOAT",	"gfloat",	"g_value_set_float",			     },
    { "DOUBLE",		"DOUBLE",	"gdouble",	"g_value_set_double",			     },
260 261 262
    { "STRING",		"STRING",	"gchar*",	"g_value_take_string",			     },
    { "PARAM",		"PARAM",	"GParamSpec*",	"g_value_take_param",			     },
    { "BOXED",		"BOXED",	"gpointer",	"g_value_take_boxed",			     },
263
    { "POINTER",	"POINTER",	"gpointer",	"g_value_set_pointer",			     },
264
    { "OBJECT",		"OBJECT",	"GObject*",	"g_value_take_object",			     },
265
    { "VARIANT",	"VARIANT",	"GVariant*",	"g_value_take_variant",			     },
266
    /* deprecated: */
267 268
    { "NONE",		"VOID",		"void",		NULL,					     },
    { "BOOL",		"BOOLEAN",	"gboolean",	"g_value_set_boolean",			     },
269 270 271 272 273
  };
  guint i;

  g_return_val_if_fail (oarg != NULL, FALSE);

274
  for (i = 0; i < G_N_ELEMENTS (args); i++)
275
    if (strcmp (args[i].keyword, oarg->keyword) == 0)
276
      {
277 278 279
	oarg->sig_name = args[i].sig_name;
	oarg->ctype = args[i].ctype;
	oarg->setter = args[i].setter;
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302

	return TRUE;
      }
  return FALSE;
}

static const gchar*
pad (const gchar *string)
{
#define PAD_LENGTH	12
  static gchar *buffer = NULL;
  gint i;

  g_return_val_if_fail (string != NULL, NULL);

  if (!buffer)
    buffer = g_new (gchar, PAD_LENGTH + 1);

  /* paranoid check */
  if (strlen (string) >= PAD_LENGTH)
    {
      g_free (buffer);
      buffer = g_strdup_printf ("%s ", string);
303 304
      g_warning ("overfull string (%u bytes) for padspace",
                 (guint) strlen (string));
305
      exit_status |= 2;
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

      return buffer;
    }

  for (i = 0; i < PAD_LENGTH; i++)
    {
      gboolean done = *string == 0;

      buffer[i] = done ? ' ' : *string++;
    }
  buffer[i] = 0;

  return buffer;
}

static const gchar*
indent (guint n_spaces)
{
324
  static gchar *buffer = NULL;
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  static guint blength = 0;

  if (blength <= n_spaces)
    {
      blength = n_spaces + 1;
      g_free (buffer);
      buffer = g_new (gchar, blength);
    }
  memset (buffer, ' ', n_spaces);
  buffer[n_spaces] = 0;

  return buffer;
}

static void
generate_marshal (const gchar *signame,
		  Signature   *sig)
{
  guint ind, a;
  GList *node;
345 346
  gchar *tmp = g_strconcat (marshaller_prefix, "_", signame, NULL);
  gboolean have_std_marshaller = FALSE;
347

348
  /* here we have to make sure a marshaller named <marshaller_prefix>_<signame>
349 350
   * exists. we might have put it out already, can revert to a standard
   * marshaller provided by glib, or need to generate one.
351 352 353 354 355 356 357 358
   */

  if (g_hash_table_lookup (marshallers, tmp))
    {
      /* done, marshaller already generated */
      g_free (tmp);
      return;
    }
359 360
  else
    {
361
      /* need to alias/generate marshaller, register name */
362 363
      g_hash_table_insert (marshallers, tmp, tmp);
    }
364 365 366 367 368 369 370 371 372

  /* can we revert to a standard marshaller? */
  if (std_includes)
    {
      tmp = g_strconcat (std_marshaller_prefix, "_", signame, NULL);
      have_std_marshaller = g_hash_table_lookup (marshallers, tmp) != NULL;
      g_free (tmp);
    }

373
  /* GValue marshaller */
374 375
  if (gen_cheader && have_std_marshaller)
    {
376
      g_fprintf (fout, "#define %s_%s\t%s_%s\n", marshaller_prefix, signame, std_marshaller_prefix, signame);
377 378
    }
  if (gen_cheader && !have_std_marshaller)
379
    {
380 381
      ind = g_fprintf (fout, gen_internal ? "G_GNUC_INTERNAL " : "extern ");
      ind += g_fprintf (fout, "void ");
382 383 384 385 386 387
      ind += g_fprintf (fout, "%s_%s (", marshaller_prefix, signame);
      g_fprintf (fout,   "GClosure     *closure,\n");
      g_fprintf (fout, "%sGValue       *return_value,\n", indent (ind));
      g_fprintf (fout, "%sguint         n_param_values,\n", indent (ind));
      g_fprintf (fout, "%sconst GValue *param_values,\n", indent (ind));
      g_fprintf (fout, "%sgpointer      invocation_hint,\n", indent (ind));
388 389
      g_fprintf (fout, "%sgpointer      marshal_data);\n",
                 indent (ind));
390
    }
391
  if (gen_cbody && !have_std_marshaller)
392
    {
393
      /* cfile marshal header */
394 395 396
      g_fprintf (fout, "void\n");
      ind = g_fprintf (fout, "%s_%s (", marshaller_prefix, signame);
      g_fprintf (fout,   "GClosure     *closure,\n");
Sven Herzberg's avatar
Sven Herzberg committed
397
      g_fprintf (fout, "%sGValue       *return_value G_GNUC_UNUSED,\n", indent (ind));
398 399
      g_fprintf (fout, "%sguint         n_param_values,\n", indent (ind));
      g_fprintf (fout, "%sconst GValue *param_values,\n", indent (ind));
Sven Herzberg's avatar
Sven Herzberg committed
400
      g_fprintf (fout, "%sgpointer      invocation_hint G_GNUC_UNUSED,\n", indent (ind));
401 402
      g_fprintf (fout, "%sgpointer      marshal_data)\n", indent (ind));
      g_fprintf (fout, "{\n");
403

404
      /* cfile GMarshalFunc typedef */
405 406
      ind = g_fprintf (fout, "  typedef %s (*GMarshalFunc_%s) (", sig->rarg->ctype, signame);
      g_fprintf (fout, "%s data1,\n", pad ("gpointer"));
407 408
      for (a = 1, node = sig->args; node; node = node->next)
	{
409
	  InArgument *iarg = node->data;
410

411
	  if (iarg->getter)
412
	    g_fprintf (fout, "%s%s arg_%d,\n", indent (ind), pad (iarg->ctype), a++);
413
	}
414
      g_fprintf (fout, "%s%s data2);\n", indent (ind), pad ("gpointer"));
415 416

      /* cfile marshal variables */
417 418 419
      g_fprintf (fout, "  register GMarshalFunc_%s callback;\n", signame);
      g_fprintf (fout, "  register GCClosure *cc = (GCClosure*) closure;\n");
      g_fprintf (fout, "  register gpointer data1, data2;\n");
420
      if (sig->rarg->setter)
421
	g_fprintf (fout, "  %s v_return;\n", sig->rarg->ctype);
422

423
      if (sig->args || sig->rarg->setter)
424
	{
425
	  g_fprintf (fout, "\n");
426

427
	  if (sig->rarg->setter)
428
	    g_fprintf (fout, "  g_return_if_fail (return_value != NULL);\n");
429 430 431 432
	  if (sig->args)
	    {
	      for (a = 0, node = sig->args; node; node = node->next)
		{
433
		  InArgument *iarg = node->data;
434

435
		  if (iarg->getter)
436 437
		    a++;
		}
438
	      g_fprintf (fout, "  g_return_if_fail (n_param_values == %u);\n", 1 + a);
439 440 441 442
	    }
	}

      /* cfile marshal data1, data2 and callback setup */
443 444 445 446 447 448 449 450 451
      g_fprintf (fout, "\n");
      g_fprintf (fout, "  if (G_CCLOSURE_SWAP_DATA (closure))\n    {\n");
      g_fprintf (fout, "      data1 = closure->data;\n");
      g_fprintf (fout, "      data2 = g_value_peek_pointer (param_values + 0);\n");
      g_fprintf (fout, "    }\n  else\n    {\n");
      g_fprintf (fout, "      data1 = g_value_peek_pointer (param_values + 0);\n");
      g_fprintf (fout, "      data2 = closure->data;\n");
      g_fprintf (fout, "    }\n");
      g_fprintf (fout, "  callback = (GMarshalFunc_%s) (marshal_data ? marshal_data : cc->callback);\n", signame);
452 453

      /* cfile marshal callback action */
454 455 456
      g_fprintf (fout, "\n");
      ind = g_fprintf (fout, " %s callback (", sig->rarg->setter ? " v_return =" : "");
      g_fprintf (fout, "data1,\n");
457 458
      for (a = 1, node = sig->args; node; node = node->next)
	{
459
	  InArgument *iarg = node->data;
460

461
	  if (iarg->getter)
462
	    g_fprintf (fout, "%s%s (param_values + %d),\n", indent (ind), iarg->getter, a++);
463
	}
464
      g_fprintf (fout, "%sdata2);\n", indent (ind));
465 466

      /* cfile marshal return value storage */
467
      if (sig->rarg->setter)
468
	{
469 470
	  g_fprintf (fout, "\n");
	  g_fprintf (fout, "  %s (return_value, v_return);\n", sig->rarg->setter);
471 472 473
	}

      /* cfile marshal footer */
474
      g_fprintf (fout, "}\n");
475
    }
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553


  /* vararg marshaller */
  if (gen_cheader && gen_valist && have_std_marshaller)
    {
      g_fprintf (fout, "#define %s_%sv\t%s_%sv\n", marshaller_prefix, signame, std_marshaller_prefix, signame);
    }
  if (gen_cheader && gen_valist && !have_std_marshaller)
    {
      ind = g_fprintf (fout, gen_internal ? "G_GNUC_INTERNAL " : "extern ");
      ind += g_fprintf (fout, "void ");
      ind += g_fprintf (fout, "%s_%sv (", marshaller_prefix, signame);
      g_fprintf (fout,   "GClosure     *closure,\n");
      g_fprintf (fout, "%sGValue       *return_value,\n", indent (ind));
      g_fprintf (fout, "%sgpointer      instance,\n", indent (ind));
      g_fprintf (fout, "%sva_list       args,\n", indent (ind));
      g_fprintf (fout, "%sgpointer      marshal_data,\n", indent (ind));
      g_fprintf (fout, "%sint           n_params,\n", indent (ind));
      g_fprintf (fout, "%sGType        *param_types);\n", indent (ind));
    }
  if (gen_cbody && gen_valist && !have_std_marshaller)
    {
      gint i;
      gboolean has_arg;

      g_fprintf (fout, "void\n");
      ind = g_fprintf (fout, "%s_%sv (", marshaller_prefix, signame);
      g_fprintf (fout,   "GClosure     *closure,\n");
      g_fprintf (fout, "%sGValue       *return_value,\n", indent (ind));
      g_fprintf (fout, "%sgpointer      instance,\n", indent (ind));
      g_fprintf (fout, "%sva_list       args,\n", indent (ind));
      g_fprintf (fout, "%sgpointer      marshal_data,\n", indent (ind));
      g_fprintf (fout, "%sint           n_params,\n", indent (ind));
      g_fprintf (fout, "%sGType        *param_types)\n", indent (ind));
      g_fprintf (fout, "{\n");

      ind = g_fprintf (fout, "  typedef %s (*GMarshalFunc_%s) (", sig->rarg->ctype, signame);
      g_fprintf (fout, "%s instance", pad ("gpointer"));
      for (a = 0, node = sig->args; node; node = node->next)
	{
	  InArgument *iarg = node->data;

	  if (iarg->getter)
	    g_fprintf (fout, ",\n%s%s arg_%d", indent (ind), pad (iarg->ctype), a++);
	}
      g_fprintf (fout, ",\n%s%s data);\n", indent (ind), pad ("gpointer"));
      g_fprintf (fout, "  GCClosure *cc = (GCClosure*) closure;\n");
      g_fprintf (fout, "  gpointer data1, data2;\n");
      g_fprintf (fout, "  GMarshalFunc_%s callback;\n", signame);
      has_arg = FALSE;

      i = 0;
      for (node = sig->args; node; node = node->next)
	{
	  InArgument *iarg = node->data;

	  if (iarg->getter)
	    {
	      g_fprintf (fout, "  %s arg%i;\n", iarg->ctype, i++);
	      has_arg = TRUE;
	    }
	}
      if (has_arg)
	g_fprintf (fout, "  va_list args_copy;\n");

      if (sig->rarg->setter)
	g_fprintf (fout, "  %s v_return;\n", sig->rarg->ctype);

      if (sig->rarg->setter)
        {
          g_fprintf (fout, "\n");
          g_fprintf (fout, "  g_return_if_fail (return_value != NULL);\n");
        }

      /* cfile marshal data1, data2 and callback setup */
      if (has_arg)
	{
	  g_fprintf (fout, "\n");
554
	  g_fprintf (fout, "  G_VA_COPY (args_copy, args);\n");
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	  i = 0;
	  for (node = sig->args; node; node = node->next)
	    {
	      InArgument *iarg = node->data;

	      if (iarg->getter)
		{
		  g_fprintf (fout, "  arg%i = (%s) va_arg (args_copy, %s);\n",
			     i, iarg->ctype, iarg->promoted_ctype);

		  if (iarg->box != NULL)
		    {
		      g_fprintf (fout, "  if (");
		      if (!iarg->box_ignores_static)
			g_fprintf (fout, "(param_types[%i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0 && ", i);
		      g_fprintf (fout, "arg%i != NULL)\n  ", i);
		      if (iarg->box_takes_type)
			g_fprintf (fout,
				   "  arg%i = %s (param_types[%i] & ~G_SIGNAL_TYPE_STATIC_SCOPE, arg%i);\n",
				   i, iarg->box, i, i);
		      else
			g_fprintf (fout,
				   "  arg%i = %s (arg%i);\n",
				   i, iarg->box, i);
		    }
		}
	      i++;
	    }
	  g_fprintf (fout, "  va_end (args_copy);\n");
	}

      g_fprintf (fout, "\n");
      /* cfile marshal data1, data2 and callback setup */
      g_fprintf (fout, "  if (G_CCLOSURE_SWAP_DATA (closure))\n    {\n");
      g_fprintf (fout, "      data1 = closure->data;\n");
      g_fprintf (fout, "      data2 = instance;\n");
      g_fprintf (fout, "    }\n  else\n    {\n");
      g_fprintf (fout, "      data1 = instance;\n");
      g_fprintf (fout, "      data2 = closure->data;\n");
      g_fprintf (fout, "    }\n");
      g_fprintf (fout, "  callback = (GMarshalFunc_%s) (marshal_data ? marshal_data : cc->callback);\n", signame);

      /* cfile marshal callback action */
      g_fprintf (fout, "\n");
      ind = g_fprintf (fout, " %s callback (", sig->rarg->setter ? " v_return =" : "");
      g_fprintf (fout, "data1");

      i = 0;
      for (node = sig->args; node; node = node->next)
	{
	  InArgument *iarg = node->data;

	  if (iarg->getter)
	    g_fprintf (fout, ",\n%sarg%i", indent (ind), i++);
	}
      g_fprintf (fout, ",\n%sdata2);\n", indent (ind));

      i = 0;
      for (node = sig->args; node; node = node->next)
	{
	  InArgument *iarg = node->data;

	  if (iarg->unbox)
	    {
	      g_fprintf (fout, "  if (");
	      if (!iarg->box_ignores_static)
		g_fprintf (fout, "(param_types[%i] & G_SIGNAL_TYPE_STATIC_SCOPE) == 0 && ", i);
	      g_fprintf (fout, "arg%i != NULL)\n  ", i);
	      if (iarg->box_takes_type)
		g_fprintf (fout,
			   "  %s (param_types[%i] & ~G_SIGNAL_TYPE_STATIC_SCOPE, arg%i);\n",
			   iarg->unbox, i, i);
	      else
		g_fprintf (fout,
			   "  %s (arg%i);\n",
			   iarg->unbox, i);
	    }
	  i++;
	}

      /* cfile marshal return value storage */
      if (sig->rarg->setter)
	{
	  g_fprintf (fout, "\n");
	  g_fprintf (fout, "  %s (return_value, v_return);\n", sig->rarg->setter);
	}

      g_fprintf (fout, "}\n\n");
    }
644 645 646 647 648
}

static void
process_signature (Signature *sig)
{
649
  gchar *pname, *sname, *tmp;
650 651 652
  GList *node;

  /* lookup and complete info on arguments */
653
  if (!complete_out_arg (sig->rarg))
654
    {
655
      g_warning ("unknown type: %s", sig->rarg->keyword);
656
      exit_status |= 1;
657 658 659 660
      return;
    }
  for (node = sig->args; node; node = node->next)
    {
661
      InArgument *iarg = node->data;
662

663
      if (!complete_in_arg (iarg))
664
	{
665
	  g_warning ("unknown type: %s", iarg->keyword);
666
          exit_status |= 1;
667 668 669 670 671
	  return;
	}
    }

  /* construct requested marshaller name and technical marshaller name */
672 673
  pname = g_strconcat (sig->rarg->keyword, "_", NULL);
  sname = g_strconcat (sig->rarg->sig_name, "_", NULL);
674 675
  for (node = sig->args; node; node = node->next)
    {
676
      InArgument *iarg = node->data;
677 678 679
      gchar *tmp;

      tmp = sname;
680
      sname = g_strconcat (tmp, "_", iarg->sig_name, NULL);
681 682
      g_free (tmp);
      tmp = pname;
683
      pname = g_strconcat (tmp, "_", iarg->keyword, NULL);
684 685 686 687
      g_free (tmp);
    }

  /* introductionary comment */
688
  g_fprintf (fout, "\n/* %s", sig->rarg->keyword);
689 690
  for (node = sig->args; node; node = node->next)
    {
691
      InArgument *iarg = node->data;
692

693
      g_fprintf (fout, "%c%s", node->prev ? ',' : ':', iarg->keyword);
694 695
    }
  if (!skip_ploc)
696 697
    g_fprintf (fout, " (%s)", sig->ploc);
  g_fprintf (fout, " */\n");
698

699
  /* ensure technical marshaller exists (<marshaller_prefix>_<sname>) */
700 701
  generate_marshal (sname, sig);

702 703 704
  /* put out marshaller alias for requested name if required (<marshaller_prefix>_<pname>) */
  tmp = g_strconcat (marshaller_prefix, "_", pname, NULL);
  if (gen_cheader && !g_hash_table_lookup (marshallers, tmp))
705
    {
706
      g_fprintf (fout, "#define %s_%s\t%s_%s\n", marshaller_prefix, pname, marshaller_prefix, sname);
707 708 709

      g_hash_table_insert (marshallers, tmp, tmp);
    }
710 711
  else
    g_free (tmp);
712 713 714 715 716

  g_free (pname);
  g_free (sname);
}

717 718 719 720 721 722 723 724 725 726 727 728
static InArgument*
new_in_arg (const gchar *pname)
{
  InArgument *iarg = g_new0 (InArgument, 1);

  iarg->keyword = g_strdup (pname);

  return iarg;
}

static OutArgument*
new_out_arg (const gchar *pname)
729
{
730
  OutArgument *oarg = g_new0 (OutArgument, 1);
731

732
  oarg->keyword = g_strdup (pname);
733

734
  return oarg;
735 736 737 738 739 740 741 742 743
}

static guint
parse_line (GScanner  *scanner,
	    Signature *sig)
{
  /* parse identifier for return value */
  if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER)
    return G_TOKEN_IDENTIFIER;
744
  sig->rarg = new_out_arg (scanner->value.v_identifier);
745 746 747 748 749 750 751 752 753 754 755

  /* keep a note on the location */
  sig->ploc = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);

  /* expect ':' */
  if (g_scanner_get_next_token (scanner) != ':')
    return ':';

  /* parse first argument */
  if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER)
    return G_TOKEN_IDENTIFIER;
756
  sig->args = g_list_append (sig->args, new_in_arg (scanner->value.v_identifier));
757 758 759 760 761 762 763 764 765 766

  /* parse rest of argument list */
  while (g_scanner_peek_next_token (scanner) == ',')
    {
      /* eat comma */
      g_scanner_get_next_token (scanner);

      /* parse arg identifier */
      if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER)
	return G_TOKEN_IDENTIFIER;
767
      sig->args = g_list_append (sig->args, new_in_arg (scanner->value.v_identifier));
768
    }
769

770 771 772
  /* expect end of line, done */
  if (g_scanner_get_next_token (scanner) != '\n')
    return '\n';
773

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
  /* success */
  return G_TOKEN_NONE;
}

static gboolean
string_key_destroy (gpointer key,
		    gpointer value,
		    gpointer user_data)
{
  g_free (key);

  return TRUE;
}

int
main (int   argc,
      char *argv[])
{
792
  const gchar *gobject_marshallers[] = {
793 794
#include	"gmarshal.strings"
  };
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
  GScanner *scanner;
  GSList *slist, *files = NULL;
  gint i;

  /* parse args and do fast exits */
  parse_args (&argc, &argv);

  /* list input files */
  for (i = 1; i < argc; i++)
    files = g_slist_prepend (files, argv[i]);
  if (files)
    files = g_slist_reverse (files);
  else
    files = g_slist_prepend (files, "/dev/stdin");

Matthias Clasen's avatar
Matthias Clasen committed
810
  /* setup auxiliary structs */
811 812 813 814
  scanner = g_scanner_new (&scanner_config_template);
  fout = stdout;
  marshallers = g_hash_table_new (g_str_hash, g_str_equal);

815
  /* add standard marshallers of the GObject library */
816
  if (std_includes)
817
    for (i = 0; i < G_N_ELEMENTS (gobject_marshallers); i++)
818
      {
819
	gchar *tmp = g_strdup (gobject_marshallers[i]);
820

821 822 823 824
	g_hash_table_insert (marshallers, tmp, tmp);
      }

  /* put out initial heading */
825
  g_fprintf (fout, "\n");
826 827

  if (gen_cheader && std_includes)
828
    {
829 830
      g_fprintf (fout, "#ifndef __%s_MARSHAL_H__\n", marshaller_prefix);
      g_fprintf (fout, "#define __%s_MARSHAL_H__\n\n", marshaller_prefix);
831 832
    }

833
  if ((gen_cheader || gen_cbody) && std_includes)
834
    g_fprintf (fout, "#include\t<glib-object.h>\n\n");
835 836

  if (gen_cheader)
837
    g_fprintf (fout, "G_BEGIN_DECLS\n");
838

839 840 841 842
  /* generate necessary preprocessor directives */
  if (gen_cbody)
    put_marshal_value_getters ();

843 844 845 846
  /* process input files */
  for (slist = files; slist; slist = slist->next)
    {
      gchar *file = slist->data;
847 848 849
      gint fd;

      if (strcmp (file, "/dev/stdin") == 0)
850
	/* Mostly for Win32. This is equivalent to opening /dev/stdin */
851 852
	fd = dup (0);
      else
853
	fd = g_open (file, O_RDONLY, 0);
854 855 856 857

      if (fd < 0)
	{
	  g_warning ("failed to open \"%s\": %s", file, g_strerror (errno));
858
	  exit_status |= 1;
859 860 861 862 863 864 865 866
	  continue;
	}

      /* set file name for error reports */
      scanner->input_name = file;

      /* parse & process file */
      g_scanner_input_file (scanner, fd);
867

868
      /* scanning loop, we parse the input until its end is reached,
869 870 871 872 873 874
       * or our sub routine came across invalid syntax
       */
      do
	{
	  guint expected_token = G_TOKEN_NONE;

875
	  switch ((guint) g_scanner_peek_next_token (scanner))
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
	    {
	    case '\n':
	      /* eat newline and restart */
	      g_scanner_get_next_token (scanner);
	      continue;
	    case G_TOKEN_EOF:
	      /* done */
	      break;
	    default:
	      /* parse and process signatures */
	      {
		Signature signature = { NULL, NULL, NULL };
		GList *node;

		expected_token = parse_line (scanner, &signature);
891

892 893 894
		/* once we got a valid signature, process it */
		if (expected_token == G_TOKEN_NONE)
		  process_signature (&signature);
895

896 897 898
		/* clean up signature contents */
		g_free (signature.ploc);
		if (signature.rarg)
899
		  g_free (signature.rarg->keyword);
900 901 902
		g_free (signature.rarg);
		for (node = signature.args; node; node = node->next)
		  {
903
		    InArgument *iarg = node->data;
904

905 906
		    g_free (iarg->keyword);
		    g_free (iarg);
907 908 909 910 911 912 913 914 915 916
		  }
		g_list_free (signature.args);
	      }
	      break;
	    }

	  /* bail out on errors */
	  if (expected_token != G_TOKEN_NONE)
	    {
	      g_scanner_unexp_token (scanner, expected_token, "type name", NULL, NULL, NULL, TRUE);
917
	      exit_status |= 1;
918 919 920 921 922 923 924 925 926 927
	      break;
	    }

	  g_scanner_peek_next_token (scanner);
	}
      while (scanner->next_token != G_TOKEN_EOF);

      close (fd);
    }

928 929 930
  /* put out trailer */
  if (gen_cheader)
    {
931
      g_fprintf (fout, "\nG_END_DECLS\n");
932 933

      if (std_includes)
934
	g_fprintf (fout, "\n#endif /* __%s_MARSHAL_H__ */\n", marshaller_prefix);
935
    }
936
  g_fprintf (fout, "\n");
937

938 939 940 941 942 943
  /* clean up */
  g_slist_free (files);
  g_scanner_destroy (scanner);
  g_hash_table_foreach_remove (marshallers, string_key_destroy, NULL);
  g_hash_table_destroy (marshallers);

944
  return exit_status;
945 946 947 948 949 950 951 952 953
}

static void
parse_args (gint    *argc_p,
	    gchar ***argv_p)
{
  guint argc = *argc_p;
  gchar **argv = *argv_p;
  guint i, e;
954

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
  for (i = 1; i < argc; i++)
    {
      if (strcmp ("--header", argv[i]) == 0)
	{
	  gen_cheader = TRUE;
	  argv[i] = NULL;
	}
      else if (strcmp ("--body", argv[i]) == 0)
	{
	  gen_cbody = TRUE;
	  argv[i] = NULL;
	}
      else if (strcmp ("--skip-source", argv[i]) == 0)
	{
	  skip_ploc = TRUE;
	  argv[i] = NULL;
	}
972 973 974 975 976 977 978 979 980 981
      else if (strcmp ("--nostdinc", argv[i]) == 0)
	{
	  std_includes = FALSE;
	  argv[i] = NULL;
	}
      else if (strcmp ("--stdinc", argv[i]) == 0)
	{
	  std_includes = TRUE;
	  argv[i] = NULL;
	}
982 983 984 985 986
      else if (strcmp ("--internal", argv[i]) == 0)
	{
	  gen_internal = TRUE;
	  argv[i] = NULL;
	}
987 988 989 990 991
      else if (strcmp ("--valist-marshallers", argv[i]) == 0)
	{
	  gen_valist = TRUE;
	  argv[i] = NULL;
	}
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
      else if ((strcmp ("--prefix", argv[i]) == 0) ||
	       (strncmp ("--prefix=", argv[i], 9) == 0))
	{
          gchar *equal = argv[i] + 8;

	  if (*equal == '=')
	    marshaller_prefix = g_strdup (equal + 1);
	  else if (i + 1 < argc)
	    {
	      marshaller_prefix = g_strdup (argv[i + 1]);
	      argv[i] = NULL;
	      i += 1;
	    }
	  argv[i] = NULL;
	}
      else if (strcmp ("-h", argv[i]) == 0 ||
Matthias Clasen's avatar
Matthias Clasen committed
1008
          strcmp ("-?", argv[i]) == 0 ||
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	  strcmp ("--help", argv[i]) == 0)
	{
	  print_blurb (stderr, TRUE);
	  argv[i] = NULL;
	  exit (0);
	}
      else if (strcmp ("-v", argv[i]) == 0 ||
	       strcmp ("--version", argv[i]) == 0)
	{
	  print_blurb (stderr, FALSE);
	  argv[i] = NULL;
	  exit (0);
	}
      else if (strcmp (argv[i], "--g-fatal-warnings") == 0)
	{
	  GLogLevelFlags fatal_mask;
1025

1026 1027 1028
	  fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
	  fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
	  g_log_set_always_fatal (fatal_mask);
1029

1030 1031 1032
	  argv[i] = NULL;
	}
    }
1033

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
  e = 0;
  for (i = 1; i < argc; i++)
    {
      if (e)
	{
	  if (argv[i])
	    {
	      argv[e++] = argv[i];
	      argv[i] = NULL;
	    }
	}
      else if (!argv[i])
	e = i;
    }
  if (e)
    *argc_p = e;
}

static void
print_blurb (FILE    *bout,
	     gboolean print_help)
{
  if (!print_help)
    {
1058 1059 1060 1061 1062 1063 1064 1065
      g_fprintf (bout, "%s version ", PRG_NAME);
      g_fprintf (bout, "%u.%u.%u", GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
      g_fprintf (bout, "\n");
      g_fprintf (bout, "%s comes with ABSOLUTELY NO WARRANTY.\n", PRG_NAME);
      g_fprintf (bout, "You may redistribute copies of %s under the terms of\n", PRG_NAME);
      g_fprintf (bout, "the GNU General Public License which can be found in the\n");
      g_fprintf (bout, "%s source package. Sources, examples and contact\n", PKG_NAME);
      g_fprintf (bout, "information are available at %s\n", PKG_HTTP_HOME);
1066 1067 1068
    }
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
      g_fprintf (bout, "Usage:\n");
      g_fprintf (bout, "  %s [OPTION...] [FILES...]\n\n", PRG_NAME);
      g_fprintf (bout, "Help Options:\n");
      g_fprintf (bout, "  -h, --help                 Show this help message\n\n");
      g_fprintf (bout, "Utility Options:\n");
      g_fprintf (bout, "  --header                   Generate C headers\n");
      g_fprintf (bout, "  --body                     Generate C code\n");
      g_fprintf (bout, "  --prefix=string            Specify marshaller prefix\n");
      g_fprintf (bout, "  --skip-source              Skip source location comments\n");
      g_fprintf (bout, "  --stdinc, --nostdinc       Include/use standard marshallers\n");
      g_fprintf (bout, "  --internal                 Mark generated functions as internal\n");
1080
      g_fprintf (bout, "  --valist-marshallers       Generate va_list marshallers\n");
Matthias Clasen's avatar
Matthias Clasen committed
1081 1082
      g_fprintf (bout, "  -v, --version              Print version informations\n");
      g_fprintf (bout, "  --g-fatal-warnings         Make warnings fatal (abort)\n");
1083 1084
    }
}