2 * Copyright (C) 1999 the Free Software Foundation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
34 char *filename = "stdin";
44 static char *funcbase;
45 static char *pfuncbase;
46 static char *macrobase;
48 static char *typebase;
49 static char *ptypebase;
51 static int signals = 0;
52 static int arguments = 0;
53 static int overrides = 0;
58 int exit_on_warn = FALSE;
59 int exit_on_error = TRUE;
60 int got_error = FALSE;
63 print_error(int is_warn, char *error,int line)
73 fprintf(stderr,"%s:%d: %s %s\n",filename,line,w,error);
75 fprintf(stderr,"%s: %s %s\n",filename,w,error);
76 if((!is_warn || exit_on_warn) && exit_on_error)
81 remove_sep(char *base)
84 char *s = g_strdup(base);
85 while((p=strchr(s,':')))
91 replace_sep(char *base, char r)
94 char *s = g_strdup(base);
95 while((p=strchr(s,':')))
105 /*separate the namespace part and then replace rest of
108 separns_replace_sep(char *base, char **ns, char **name, char r)
111 char *s = g_strdup(base);
113 if((p=strchr(s,':')) && p!=s) {
120 while((p=strchr(s,':')))
123 *name = g_strdup(s+1);
130 make_is_macro(char *base)
135 separns_replace_sep(base,&s1,&s2,'_');
137 s = g_strconcat(s1,"_IS_",s2,NULL);
139 s = g_strconcat("IS_",s2,NULL);
152 filebase = replace_sep(((Class *)class)->otype,'-');
155 funcbase = replace_sep(((Class *)class)->otype,'_');
158 pfuncbase = replace_sep(((Class *)class)->ptype,'_');
159 g_strdown(pfuncbase);
161 macrobase = replace_sep(((Class *)class)->otype,'_');
164 macrois = make_is_macro(((Class *)class)->otype);
166 typebase = remove_sep(((Class *)class)->otype);
168 ptypebase = remove_sep(((Class *)class)->ptype);
172 def_methods(Class *c)
176 out_printf(out,"\n");
177 for(li=c->nodes;li;li=g_list_next(li)) {
178 Node *node = li->data;
179 if(node->type == METHOD_NODE) {
180 Method *m = (Method *)node;
182 if(m->scope == INIT_METHOD ||
183 m->scope == CLASS_INIT_METHOD ||
184 m->scope == OVERRIDE_METHOD)
187 out_printf(out,"#define %s %s_%s\n",m->id,funcbase,m->id);
190 out_printf(out,"\n");
194 undef_methods(Class *c)
198 out_printf(out,"\n");
199 for(li=c->nodes;li;li=g_list_next(li)) {
200 Node *node = li->data;
201 if(node->type == METHOD_NODE) {
202 Method *m = (Method *)node;
204 if(m->scope == INIT_METHOD ||
205 m->scope == CLASS_INIT_METHOD ||
206 m->scope == OVERRIDE_METHOD)
209 out_printf(out,"#undef %s\n",m->id);
212 out_printf(out,"\n");
216 print_type(FILE *fp, Type *t)
220 s = remove_sep(t->name);
221 out_printf(fp,"%s ",s);
224 for(i=0;i<t->stars;i++)
229 put_variable(Variable *v)
231 out_printf(outh,"\t");
232 if(v->scope == PRIVATE_SCOPE)
233 out_printf(outh,"/* private */ ");
234 print_type(outh,v->vtype);
236 out_printf(outh,"%s;\n",v->id);
240 print_method(FILE *fp, char *typeprefix, char *nameprefix,
241 char *namepostfix,char *postfix, Method *m)
245 out_printf(fp,"%s",typeprefix);
246 print_type(fp,m->mtype);
247 out_printf(fp,"%s%s_%s%s(",
248 nameprefix,funcbase,m->id,namepostfix);
251 for(li=m->args;li;li=g_list_next(li)) {
252 FuncArg *arg = li->data;
253 print_type(fp,arg->atype);
255 out_printf(fp,"%s, ",arg->name);
257 out_printf(fp,"%s",arg->name);
261 out_printf(fp,", ...");
263 out_printf(fp,"void");
265 out_printf(fp,")%s\n",postfix);
269 put_vs_method(Method *m)
271 if(m->scope != SIGNAL_LAST_METHOD &&
272 m->scope != SIGNAL_FIRST_METHOD &&
273 m->scope != VIRTUAL_METHOD)
276 print_method(outh,"\t","(* ",") ",";",m);
280 put_pub_method(Method *m)
282 if(m->scope == PRIVATE_SCOPE ||
283 m->scope == OVERRIDE_METHOD ||
284 m->scope == INIT_METHOD ||
285 m->scope == CLASS_INIT_METHOD)
288 print_method(outh,"","\t","\t",";",m);
292 put_priv_method_prot(Method *m)
294 if(m->scope == PUBLIC_SCOPE)
297 if(m->scope == SIGNAL_LAST_METHOD ||
298 m->scope == SIGNAL_FIRST_METHOD ||
299 m->scope == VIRTUAL_METHOD) {
302 print_method(out,"static ","_real_"," ",";",m);
304 print_method(out,"static ",""," ",";",m);
309 make_init_args(Class *cl, char *name, int is_class)
316 tn = g_strconcat(cl->otype,":Class",NULL);
318 tn = g_strdup(cl->otype);
320 type = new_type(1,tn);
321 node = new_funcarg((Type *)type,name,NULL);
322 return g_list_prepend(NULL, node);
326 make_inits(Class *cl)
328 int got_class_init = FALSE;
329 int got_init = FALSE;
332 for(li=cl->nodes;li;li=g_list_next(li)) {
334 if(n->type == METHOD_NODE) {
335 Method *m = (Method *)n;
336 if(m->scope == INIT_METHOD) {
338 print_error(FALSE,"init defined more then once",m->line_no);
340 } else if(m->scope == CLASS_INIT_METHOD) {
342 print_error(FALSE,"class_init defined more then once",m->line_no);
343 got_class_init = TRUE;
347 if(!got_class_init) {
348 node = new_method(CLASS_INIT_METHOD,
349 (Type *)new_type(0,g_strdup("void")),
350 NULL,NULL,g_strdup("class_init"),
351 make_init_args(cl,g_strdup("c"),TRUE),
352 NULL, NULL,0,0,FALSE);
353 cl->nodes = g_list_prepend(cl->nodes,node);
356 node = new_method(INIT_METHOD,
357 (Type *)new_type(0,g_strdup("void")),
358 NULL,NULL,g_strdup("init"),
359 make_init_args(cl,g_strdup("o"),FALSE),
360 NULL, NULL,0,0,FALSE);
361 cl->nodes = g_list_prepend(cl->nodes,node);
365 static GHashTable *marsh = NULL;
368 add_signal_prots(Method *m)
374 if(m->scope != SIGNAL_LAST_METHOD &&
375 m->scope != SIGNAL_FIRST_METHOD)
379 marsh = g_hash_table_new(NULL,NULL);
381 if(strcmp(m->gtktypes->data,"NONE")==0 &&
382 strcmp(m->gtktypes->next->data,"NONE")==0)
385 s = g_strdup_printf("__Sig%d",sig++);
387 g_hash_table_insert(marsh,m,s);
389 out_printf(out,"\ntypedef ");
390 print_type(out,m->mtype);
392 out_printf(out,"(*%s) (",s);
394 for(li=m->args;li;li=g_list_next(li)) {
395 FuncArg *arg = li->data;
396 print_type(out,arg->atype);
397 out_printf(out,", ");
399 out_printf(out,"gpointer);\n");
401 out_printf(out,"\nstatic void\n"
402 "marshal_%s (GtkObject * object,\n"
403 "\tGtkSignalFunc func,\n"
404 "\tgpointer func_data,\n"
408 if(strcmp(m->gtktypes->data,"NONE")==0) {
410 out_printf(out, "\t%s rfunc;\n\n"
411 "\trfunc = (%s)func;\n\n"
412 "\t(*rfunc)((%s *)object",s,s,typebase);
413 if(strcmp(m->gtktypes->next->data,"NONE")!=0) {
414 for(i=0,li=m->gtktypes->next;li;
415 i++,li=g_list_next(li)) {
416 out_printf(out, ",\n\t\tGTK_VALUE_%s(args[%d])",
420 out_printf(out, ",\n\t\tfunc_data);\n}\n\n");
423 out_printf(out, "\t%s rfunc;\n\t",s);
424 print_type(out,m->mtype);
425 out_printf(out, " *retval;\n\n"
426 "\trfunc = (%s)func;\n\n"
427 "\tretval = GTK_RETLOC_%s(args[%d]);\n\n"
428 "\t*retval = (*rfunc)((%s *)object",
429 s,(char *)m->gtktypes->data,
430 g_list_length(m->gtktypes)-1,typebase);
431 if(strcmp(m->gtktypes->next->data,"NONE")!=0) {
432 for(i=0,li=m->gtktypes->next;li;
433 i++,li=g_list_next(li)) {
434 out_printf(out, ",\n\t\tGTK_VALUE_%s(args[%d])",
438 out_printf(out, ",\n\t\tfunc_data);\n}\n\n");
447 out_printf(out,"\n");
449 out_printf(out,"enum {\n");
450 for(li=c->nodes;li;li=g_list_next(li)) {
452 if(n->type == METHOD_NODE) {
453 Method *m = (Method *)n;
454 if(m->scope == SIGNAL_LAST_METHOD ||
455 m->scope == SIGNAL_FIRST_METHOD) {
456 char *s = g_strdup(m->id);
458 out_printf(out,"\t%s_SIGNAL,\n",s);
463 out_printf(out,"\tLAST_SIGNAL\n};\n\n");
466 out_printf(out,"enum {\n\tARG_0,\n");
467 for(li=c->nodes;li;li=g_list_next(li)) {
469 if(n->type == ARGUMENT_NODE) {
470 Argument *a = (Argument *)n;
471 char *s = g_strdup(a->name);
473 out_printf(out,"\tARG_%s,\n",s);
477 out_printf(out, "};\n\n");
482 "static guint object_signals[LAST_SIGNAL] = {0};\n\n");
484 out_printf(out, "static %sClass *parent_class = NULL;\n\n",ptypebase);
490 out_printf(out, "guint\n"
491 "%s_get_type (void)\n"
493 "\tstatic guint type = 0;\n\n"
495 "\t\tstatic const GtkTypeInfo info = {\n"
497 "\t\t\tsizeof (%s),\n"
498 "\t\t\tsizeof (%sClass),\n"
499 "\t\t\t(GtkClassInitFunc) %s_class_init,\n"
500 "\t\t\t(GtkObjectInitFunc) %s_init,\n"
501 "\t\t\t/* reserved_1 */ NULL,\n"
502 "\t\t\t/* reserved_2 */ NULL,\n"
503 "\t\t\t(GtkClassInitFunc) NULL,\n"
505 "\t\ttype = gtk_type_unique (%s_get_type(), &info);\n"
509 funcbase,typebase,typebase,typebase,
510 funcbase,funcbase,pfuncbase);
514 add_overrides(Class *c, char *oname)
520 done = g_hash_table_new(g_str_hash,g_str_equal);
521 s = g_strdup("GtkObject"); /* This was already done */
522 g_hash_table_insert(done,s,s);
523 for(li=c->nodes;li;li=g_list_next(li)) {
527 if(n->type != METHOD_NODE ||
528 ((Method *)n)->scope != OVERRIDE_METHOD)
532 s = remove_sep(m->otype);
534 if(g_hash_table_lookup(done,s)) {
538 g_hash_table_insert(done,s,s);
540 f = replace_sep(m->otype,'_');
543 out_printf(out,"\t%sClass *%s_class = (%sClass *)%s;\n",
548 g_hash_table_foreach(done,(GHFunc)g_free,NULL);
549 g_hash_table_destroy(done);
553 add_signals(Class *c)
557 out_printf(out,"\n");
558 for(li=c->nodes;li;li=g_list_next(li)) {
564 if(n->type != METHOD_NODE ||
565 (((Method *)n)->scope != SIGNAL_FIRST_METHOD &&
566 ((Method *)n)->scope != SIGNAL_LAST_METHOD))
571 if(g_hash_table_lookup(marsh,m))
572 mar = g_strconcat("marshal_",
573 (char *)g_hash_table_lookup(marsh,m),
576 mar = g_strdup("gtk_signal_default_marshaller");
578 is_none = (strcmp(m->gtktypes->next->data,"NONE")==0);
580 sig = g_strdup(m->id);
582 out_printf(out,"\tobject_signals[%s_SIGNAL] =\n"
583 "\t\tgtk_signal_new (\"%s\",\n"
584 "\t\t\tGTK_RUN_%s,\n"
585 "\t\t\tgtk_object_class->type,\n"
586 "\t\t\tGTK_SIGNAL_OFFSET (%sClass, %s),\n"
588 "\t\t\tGTK_TYPE_%s, %d",
590 m->scope==SIGNAL_LAST_METHOD?"LAST":"FIRST",
591 typebase,m->id,mar,(char *)m->gtktypes->data,
592 is_none?0:g_list_length(m->gtktypes->next));
598 for(l=m->gtktypes->next;l;l=g_list_next(l))
599 out_printf(out,",\n\t\t\tGTK_TYPE_%s",
603 out_printf(out,");\n");
605 out_printf(out,"\tgtk_object_class_add_signals (gtk_object_class,\n"
606 "\t\tobject_signals, LAST_SIGNAL);\n\n");
610 set_def_handlers(Class *c, char *oname)
614 out_printf(out,"\n");
615 for(li=c->nodes;li;li=g_list_next(li)) {
618 if(n->type != METHOD_NODE ||
619 (((Method *)n)->scope != SIGNAL_FIRST_METHOD &&
620 ((Method *)n)->scope != SIGNAL_LAST_METHOD &&
621 ((Method *)n)->scope != VIRTUAL_METHOD &&
622 ((Method *)n)->scope != OVERRIDE_METHOD))
627 if(m->scope == OVERRIDE_METHOD) {
629 s = replace_sep(m->otype,'_');
631 out_printf(out,"\t%s_class->%s = %s_%s;\n",
632 s,m->id,funcbase,m->id);
635 out_printf(out,"\t%s->%s = _real_%s_%s;\n",
636 oname,m->id,funcbase,m->id);
638 out_printf(out,"\t%s->%s = NULL;\n",
645 make_arguments(Class *c)
649 out_printf(out,"\n");
650 for(li=c->nodes;li;li=g_list_next(li)) {
656 if(n->type != ARGUMENT_NODE)
662 flags = g_string_new("GTK_ARG_READWRITE");
664 flags = g_string_new("GTK_ARG_READABLE");
666 flags = g_string_new("GTK_ARG_WRITABLE");
668 for(l=a->flags;l;l=g_list_next(l))
669 g_string_sprintfa(flags," | GTK_ARG_%s",(char *)l->data);
671 s = g_strdup(a->name);
673 out_printf(out,"\tgtk_object_add_arg_type(\"%s::%s\",\n"
677 typebase,a->name,a->gtktype,flags->str,s);
679 g_string_free(flags,TRUE);
683 "\n\tgtk_object_class->set_arg = __object_set_arg;\n"
684 "\tgtk_object_class->get_arg = __object_get_arg;\n");
691 for(li=c->nodes;li;li=g_list_next(li)) {
694 if(n->type != METHOD_NODE)
697 if(m->scope == INIT_METHOD) {
699 out_addline_infile(out,m->line_no);
700 print_method(out,"static ","\n"," ","",m);
702 out_addline_outfile(out);
703 out_printf(out,"{\n");
704 } else if(m->scope == CLASS_INIT_METHOD) {
706 out_addline_infile(out,m->line_no);
707 print_method(out,"static ","\n"," ","",m);
709 out_addline_outfile(out);
710 out_printf(out,"{\n");
716 "gtk_object_class = "
717 "(GtkObjectClass*) %s;\n",
718 ((FuncArg *)m->args->data)->name);
722 ((FuncArg *)m->args->data)->name);
724 out_printf(out,"\n\tparent_class = "
725 "gtk_type_class (%s_get_type ());\n",
731 set_def_handlers(c, ((FuncArg *)m->args->data)->name);
739 out_printf(out," {\n");
740 out_addline_infile(out,m->ccode_line);
741 out_printf(out,"%s\n",m->cbuf->str);
742 out_addline_outfile(out);
743 out_printf(out," }\n",m->cbuf->str);
745 out_printf(out,"return;\n");
747 out_printf(out,"}\n");
752 add_getset_arg(Class *c, int is_set)
755 out_printf(out,"\nstatic void\n"
756 "__object_%s_arg (GtkObject *object,\n"
760 "\t%s *self, *this;\n\n"
761 "\tself = this = %s (object);\n\n"
762 "\tswitch (arg_id) {\n",
763 is_set?"set":"get",typebase,macrobase);
765 for(li=c->nodes;li;li=g_list_next(li)) {
771 if(n->type != ARGUMENT_NODE)
776 line_no = a->set_line;
779 line_no = a->get_line;
783 s = g_strdup(a->name);
785 out_printf(out,"\tcase ARG_%s:\n"
786 "#define ARG (GTK_VALUE_%s(*arg))\n"
790 out_addline_infile(out,line_no);
791 out_printf(out,"%s\n",cbuf->str);
792 out_addline_outfile(out);
793 out_printf(out,"\t\t}\n\t\tbreak;\n"
796 out_printf(out,"\tdefault:\n\t\tbreak;\n\t}\n}\n");
800 print_checks(Method *m, FuncArg *fa)
804 is_void = (strcmp(m->mtype->name,"void")==0 &&
805 m->mtype->stars == 0);
807 for(li=fa->checks;li;li=g_list_next(li)) {
808 Check *ch = li->data;
811 out_printf(out,"\tg_return_if_fail (");
813 out_printf(out,"\tg_return_val_if_fail (");
816 out_printf(out,"%s != NULL",fa->name);
819 s = make_is_macro(fa->atype->name);
820 out_printf(out,"%s (%s)",s,fa->name);
824 out_printf(out,"%s < %s",fa->name,ch->number);
827 out_printf(out,"%s > %s",fa->name,ch->number);
830 out_printf(out,"%s <= %s",fa->name,ch->number);
833 out_printf(out,"%s >= %s",fa->name,ch->number);
836 out_printf(out,"%s == %s",fa->name,ch->number);
839 out_printf(out,"%s != %s",fa->name,ch->number);
843 out_printf(out,");\n");
845 out_printf(out,", (");
846 print_type(out,m->mtype);
847 out_printf(out,")%s);\n",
848 m->onerror?m->onerror:"0");
854 print_preconditions(Method *m)
858 for(li=m->args;li;li=g_list_next(li)) {
859 FuncArg *fa = li->data;
866 print_method_body(Method *m, int pre)
868 out_printf(out,"{\n");
870 print_preconditions(m);
871 out_printf(out,"\t{\n");
874 out_addline_infile(out,m->ccode_line);
875 out_printf(out,"\t\t%s\n",m->cbuf->str);
876 out_addline_outfile(out);
879 out_printf(out,"\t}\n");
880 out_printf(out,"}\n");
884 put_method(Method *m)
887 out_printf(out,"\n");
890 out_addline_infile(out,m->line_no);
891 print_method(out,"","\n"," ","",m);
892 print_method_body(m,TRUE);
895 out_addline_infile(out,m->line_no);
896 print_method(out,"static ","\n"," ","",m);
897 print_method_body(m,TRUE);
899 case SIGNAL_FIRST_METHOD:
900 case SIGNAL_LAST_METHOD:
901 out_addline_infile(out,m->line_no);
902 print_method(out,"","\n"," ","",m);
903 out_addline_outfile(out);
904 out_printf(out,"{\n");
907 if(strcmp(m->mtype->name,"void")==0 &&
908 m->mtype->stars==0) {
910 print_preconditions(m);
911 if(((FuncArg *)m->args->data)->name)
912 out_printf(out,"\tgtk_signal_emit (GTK_OBJECT (%s),\n"
913 "\t\tobject_signals[%s_SIGNAL]",
914 ((FuncArg *)m->args->data)->name,s);
915 for(li=m->args->next;li;li=g_list_next(li)) {
916 FuncArg *fa = li->data;
917 out_printf(out,",\n\t\t%s",fa->name);
919 out_printf(out,");\n}\n");
922 out_printf(out,"\t");
923 print_type(out,m->mtype);
924 out_printf(out,"return_val;\n");
925 print_preconditions(m);
926 out_printf(out,"\tgtk_signal_emit (GTK_OBJECT (%s),\n"
927 "\t\tobject_signals[%s_SIGNAL]",
928 ((FuncArg *)m->args->data)->name,s);
929 for(li=m->args->next;li;li=g_list_next(li)) {
930 FuncArg *fa = li->data;
931 out_printf(out,",\n\t\t%s",fa->name);
933 out_printf(out,",\n\t\t&return_val);\n"
934 "\treturn return_val;\n}\n");
939 out_addline_infile(out,m->line_no);
940 print_method(out,"static ","\n_real_"," ","",m);
941 print_method_body(m,FALSE);
944 out_addline_infile(out,m->line_no);
945 print_method(out,"","\n"," ","",m);
946 out_addline_outfile(out);
948 "\t%sClass *class;\n",typebase);
949 print_preconditions(m);
950 out_printf(out,"\tclass = %s_CLASS(GTK_OBJECT(%s)->klass);\n\n"
952 macrobase, ((FuncArg *)m->args->data)->name, m->id);
953 if(strcmp(m->mtype->name,"void")==0 &&
954 m->mtype->stars==0) {
956 out_printf(out,"\t\t(*class->%s)(%s",m->id,
957 ((FuncArg *)m->args->data)->name);
958 for(li=m->args->next;li;li=g_list_next(li)) {
959 FuncArg *fa = li->data;
960 out_printf(out,",%s",fa->name);
962 out_printf(out,");\n}\n");
965 out_printf(out,"\t\treturn (*class->%s)(%s",m->id,
966 ((FuncArg *)m->args->data)->name);
967 for(li=m->args->next;li;li=g_list_next(li)) {
968 FuncArg *fa = li->data;
969 out_printf(out,",%s",fa->name);
971 out_printf(out,");\n"
974 print_type(out,m->mtype);
975 out_printf(out,")(%s);\n}\n",
976 m->onerror?m->onerror:"0");
981 out_addline_infile(out,m->line_no);
982 print_method(out,"static ","\n_real_"," ","",m);
983 print_method_body(m,FALSE);
985 case OVERRIDE_METHOD:
986 out_addline_infile(out,m->line_no);
987 print_method(out,"static ","\n"," ","",m);
988 print_method_body(m,TRUE);
996 check_duplicate(Class *c,Node *node,char *id, int line_no)
999 for(l=c->nodes;l;l=g_list_next(l)) {
1004 if(n->type == METHOD_NODE) {
1005 Method *m = (Method *)n;
1007 nline_no = m->line_no;
1008 } else if(n->type == VARIABLE_NODE) {
1009 Variable *v = (Variable *)n;
1011 nline_no = v->line_no;
1015 line_no>=nline_no ||
1018 s = g_strdup_printf("symbol '%s' redefined, "
1019 "first defined on line %d",
1021 print_error(FALSE,s,nline_no);
1026 check_duplicate_symbols(Class *c)
1029 for(l=c->nodes;l;l=g_list_next(l)) {
1031 if(n->type == METHOD_NODE) {
1032 Method *m = (Method *)n;
1033 check_duplicate(c,n,m->id,m->line_no);
1034 } else if(n->type == VARIABLE_NODE) {
1035 Variable *v = (Variable *)n;
1036 check_duplicate(c,n,v->id,v->line_no);
1042 check_duplicate_named(Class *c,Node *node,char *id, int line_no)
1045 for(l=c->nodes;l;l=g_list_next(l)) {
1050 if(n->type == METHOD_NODE) {
1051 Method *m = (Method *)n;
1052 if(m->scope == SIGNAL_LAST_METHOD ||
1053 m->scope == SIGNAL_FIRST_METHOD) {
1055 nline_no = m->line_no;
1058 } else if(n->type == ARGUMENT_NODE) {
1059 Argument *a = (Argument *)n;
1061 nline_no = a->line_no;
1065 line_no>=nline_no ||
1068 s = g_strdup_printf("named symbol (argument or signal) '%s' "
1069 "redefined, first defined on line %d",
1071 print_error(FALSE,s,nline_no);
1076 check_duplicate_signals_args(Class *c)
1079 for(l=c->nodes;l;l=g_list_next(l)) {
1081 if(n->type == METHOD_NODE) {
1082 Method *m = (Method *)n;
1083 if(m->scope == SIGNAL_LAST_METHOD ||
1084 m->scope == SIGNAL_FIRST_METHOD)
1085 check_duplicate_named(c,n,m->id,m->line_no);
1086 } else if(n->type == ARGUMENT_NODE) {
1087 Argument *a = (Argument *)n;
1088 check_duplicate_named(c,n,a->name,a->line_no);
1094 check_public_new(Class *c)
1097 for(l=c->nodes;l;l=g_list_next(l)) {
1099 if(n->type == METHOD_NODE) {
1100 Method *m = (Method *)n;
1101 if(m->scope!=PUBLIC_SCOPE &&
1102 strcmp(m->id,"new")==0)
1104 "'new' should be a public method",
1111 check_vararg(Class *c)
1114 for(l=c->nodes;l;l=g_list_next(l)) {
1116 if(n->type == METHOD_NODE) {
1117 Method *m = (Method *)n;
1120 if(m->scope == OVERRIDE_METHOD ||
1121 m->scope == SIGNAL_LAST_METHOD ||
1122 m->scope == SIGNAL_FIRST_METHOD ||
1123 m->scope == VIRTUAL_METHOD) {
1125 "signals, overrides and virtuals, "
1126 "can't have variable argument "
1135 count_signals(Class *c)
1139 for(l=c->nodes;l;l=g_list_next(l)) {
1141 if(n->type == METHOD_NODE) {
1142 Method *m = (Method *)n;
1143 if(m->scope == SIGNAL_LAST_METHOD ||
1144 m->scope == SIGNAL_FIRST_METHOD)
1152 count_arguments(Class *c)
1157 for(li=c->nodes;li;li=g_list_next(li)) {
1159 if(n->type == ARGUMENT_NODE)
1166 count_overrides(Class *c)
1170 for(l=c->nodes;l;l=g_list_next(l)) {
1172 if(n->type == METHOD_NODE) {
1173 Method *m = (Method *)n;
1174 if(m->scope == OVERRIDE_METHOD)
1185 char *outfile,*outfileh;
1187 outfile = g_strconcat(filebase,".c",NULL);
1188 outfileh = g_strconcat(filebase,".h",NULL);
1190 out = fopen(outfile,"w");
1192 g_error("Cannot open outfile: %s",outfile);
1194 outh = fopen(outfileh,"w");
1196 g_error("Cannot open outfile: %s",outfileh);
1201 generate_outfiles(void)
1208 out_printf(outh,"/* Generated by GOB (v%s) on %s"
1209 " (do not edit directly) */\n\n",VERSION,ctime(&curtime));
1210 out_printf(out,"/* Generated by GOB (v%s) on %s"
1211 " (do not edit directly) */\n\n",VERSION,ctime(&curtime));
1213 p = replace_sep(((Class *)class)->otype,'_');
1215 out_printf(outh,"#ifndef __%s_H__\n#define __%s_H__\n\n"
1216 "#include <gtk/gtk.h>\n\n",p,p);
1219 out_printf(outh,"#ifdef __cplusplus\n"
1221 "#endif /* __cplusplus */\n\n");
1223 out_printf(out,"#include \"%s.h\"\n\n",filebase);
1225 for(li=nodes;li;li=g_list_next(li)) {
1226 Node *node = li->data;
1227 if(node->type == CCODE_NODE) {
1228 CCode *cc = (CCode *)node;
1234 out_addline_infile(fp,cc->line_no);
1236 out_printf(fp,"\n%s\n",cc->cbuf->str);
1238 out_addline_outfile(fp);
1239 } else if(node->type == CLASS_NODE) {
1241 Class *c = (Class *)class;
1244 signals = count_signals(c);
1245 arguments = count_arguments(c);
1246 overrides = count_overrides(c);
1248 out_printf(outh,"\n#define %s(obj)\t"
1249 "GTK_CHECK_CAST((obj),%s_get_type(),%s)\n",
1250 macrobase,funcbase,typebase);
1251 out_printf(outh,"#define %s_CLASS(klass)\t"
1252 "GTK_CHECK_CLASS_CAST((klass),%s_get_type(),%sClass)\n",
1253 macrobase,funcbase,typebase);
1254 out_printf(outh,"#define %s(obj)\t"
1255 "GTK_CHECK_TYPE((obj), %s_get_type ())\n\n",
1258 otype = remove_sep(c->otype);
1259 ptype = remove_sep(c->ptype);
1260 out_printf(outh,"\ntypedef struct _%s %s;\n",otype,otype);
1261 out_printf(outh,"struct _%s {\n\t%s __parent__;\n",
1263 for(l=c->nodes;l;l=g_list_next(l)) {
1265 if(n->type == VARIABLE_NODE)
1266 put_variable((Variable *)n);
1268 out_printf(outh,"};\n");
1270 out_printf(outh,"\ntypedef struct _%sClass %sClass;\n",
1273 "struct _%sClass {\n\t%sClass __parent__;\n",
1275 for(l=c->nodes;l;l=g_list_next(l)) {
1277 if(n->type == METHOD_NODE)
1278 put_vs_method((Method *)n);
1280 out_printf(outh,"};\n\n");
1282 out_printf(outh,"guint\t%s_get_type\t(void);\n",funcbase);
1285 out_printf(out,"static void __object_set_arg "
1286 "(GtkObject *object, GtkArg *arg, "
1288 "static void __object_get_arg "
1289 "(GtkObject *object, GtkArg *arg, "
1290 "guint arg_id);\n");
1293 for(l=c->nodes;l;l=g_list_next(l)) {
1295 if(n->type == METHOD_NODE) {
1296 put_pub_method((Method *)n);
1297 put_priv_method_prot((Method *)n);
1302 for(l=c->nodes;l;l=g_list_next(l)) {
1304 if(n->type == METHOD_NODE) {
1305 add_signal_prots((Method *)n);
1316 out_printf(out,"#define GET_NEW (gtk_type_new(%s_get_type()))\n",
1322 add_getset_arg(c, TRUE);
1323 add_getset_arg(c, FALSE);
1326 for(l=c->nodes;l;l=g_list_next(l)) {
1328 if(n->type == METHOD_NODE) {
1329 put_method((Method *)n);
1333 out_printf(out,"#undef GET_NEW\n");
1340 g_assert_not_reached();
1343 out_printf(outh,"\n#ifdef __cplusplus\n"
1345 "#endif /* __cplusplus */\n\n"
1350 usage(poptContext optCon, int exitcode, char *error, char *addl)
1352 poptPrintUsage(optCon, stderr, 0);
1353 if (error) fprintf(stderr, "%s: %s", error, addl);
1359 main(int argc, char *argv[])
1364 struct poptOption optionsTable[] = {
1365 { "exit-on-warn", 'w', 0, &exit_on_warn, 0,
1366 "exit on warnings" },
1368 { NULL, 0, 0, NULL, 0 }
1371 optCon = poptGetContext(NULL, argc, argv, optionsTable, 0);
1372 poptSetOtherOptionHelp(optCon, "[OPTIONS]* [filename]");
1374 while ((c = poptGetNextOpt(optCon)) >= 0)
1377 filename = poptGetArg(optCon);
1378 if(!(poptPeekArg(optCon) == NULL))
1379 usage(optCon, 1, "Specify only one file",
1380 ".e.g., filename.gob");
1383 /* an error occurred during option processing */
1384 fprintf(stderr, "%s: %s\n",
1385 poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
1391 yyin = fopen(filename,"r");
1393 fprintf(stderr,"Error: can't open file '%s'\n",
1402 g_error("Parsing errors, quitting");
1404 print_error(FALSE," no class defined",0);
1408 exit_on_error = FALSE;
1409 make_inits((Class *)class);
1410 check_duplicate_symbols((Class *)class);
1411 check_duplicate_signals_args((Class *)class);
1412 check_public_new((Class *)class);
1413 check_vararg((Class *)class);
1414 exit_on_error = TRUE;
1421 generate_outfiles();
1426 poptFreeContext(optCon);