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,
28 #include "treefuncs.h"
36 static GList *class_nodes = NULL;
39 static GList *typestack = NULL;
41 static GList *funcargs = NULL;
42 static GList *checks = NULL;
43 static int has_self = FALSE;
44 static int vararg = FALSE;
46 /* destructor and initializer for variables */
47 static char *destructor = NULL;
48 static int destructor_line = 0;
49 static gboolean destructor_simple = TRUE;
50 static char *initializer = NULL;
51 static int initializer_line = 0;
53 static char *onerror = NULL;
54 static char *defreturn = NULL;
56 static GList *gtktypes = NULL;
58 /* this can be a global as we will only do one function at a time
60 static int the_scope = NO_SCOPE;
65 extern int ccode_line;
76 if(strcmp(yytext,"\n")==0) {
77 out=g_strconcat("Error: ",str," before newline",NULL);
78 } else if(yytext[0]=='\0') {
79 out=g_strconcat("Error: ", str, " at end of input", NULL);
81 char *tmp = g_strdup(yytext);
82 while((p=strchr(tmp, '\n')))
85 out=g_strconcat("Error: ", str, " before '", tmp, "'", NULL);
89 fprintf(stderr, "%s:%d: %s\n", filename, line_no, out);
98 Type *type = typestack->data;
99 typestack = g_list_remove(typestack,typestack->data);
104 push_variable(char *name, int scope, int line_no, char *postfix)
107 Type *type = pop_type();
109 type->postfix = postfix;
111 var = new_variable(scope, type, name, line_no,
112 destructor, destructor_line,
114 initializer, initializer_line);
115 class_nodes = g_list_append(class_nodes, var);
119 push_function(int scope, int method, char *oid, char *id,
120 GString *cbuf, int line_no, int ccode_line,
121 gboolean vararg, GList *flags)
127 if(method!=INIT_METHOD && method!=CLASS_INIT_METHOD) {
130 type = (Type *)new_type(0,g_strdup("void"),NULL);
133 /* a complicated and ugly test to figure out if we have
134 the wrong number of types for a signal */
135 if((method == SIGNAL_FIRST_METHOD ||
136 method == SIGNAL_LAST_METHOD) &&
137 g_list_length(gtktypes) != g_list_length(funcargs) &&
138 !(g_list_length(funcargs) == 1 &&
139 g_list_length(gtktypes) == 2 &&
140 strcmp(gtktypes->next->data, "NONE")==0)) {
141 print_error(TRUE, _("The number of GTK arguments and "
142 "function arguments for a signal "
143 "don't seem to match"), line_no);
145 if(g_list_length(gtktypes) > 2) {
147 for(li = gtktypes->next; li; li = li->next) {
148 if(strcmp(li->data, "NONE")==0) {
150 _("NONE can only appear in an "
151 "argument list by itself"),
158 c_cbuf = p = cbuf->str;
159 while(p && *p && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
166 node = new_method(scope, method, type, oid, gtktypes, flags,
167 id, funcargs, onerror, defreturn, c_cbuf, line_no,
172 /*only free segment if we haven't passed it
181 class_nodes = g_list_append(class_nodes, node);
185 free_all_global_state(void)
192 g_list_foreach(gtktypes, (GFunc)g_free, NULL);
193 g_list_free(gtktypes);
196 free_node_list(funcargs);
201 push_funcarg(char *name, char *postfix)
204 Type *type = pop_type();
206 type->postfix = postfix;
208 node = new_funcarg(type, name, checks);
211 funcargs = g_list_append(funcargs, node);
215 push_init_arg(char *name, int is_class)
222 tn = g_strconcat(((Class *)class)->otype,":Class",NULL);
224 tn = g_strdup(((Class *)class)->otype);
226 type = new_type(1,tn,NULL);
227 node = new_funcarg((Type *)type,name,NULL);
228 funcargs = g_list_prepend(funcargs, node);
237 type = new_type(1,g_strdup(((Class *)class)->otype),NULL);
238 ch = g_list_append(ch,new_check(NULL_CHECK,NULL));
239 ch = g_list_append(ch,new_check(TYPE_CHECK,NULL));
240 node = new_funcarg((Type *)type,id,ch);
241 funcargs = g_list_prepend(funcargs, node);
245 find_var_or_die(const char *id, int line)
250 for(li = class_nodes; li != NULL; li = li->next) {
252 Node *node = li->data;
253 if(node->type != VARIABLE_NODE)
256 if(strcmp(var->id, id)==0)
260 s = g_strdup_printf(_("Variable %s not defined here"), id);
261 print_error(FALSE, s, line);
263 g_assert_not_reached();
268 set_return_value(char *type, char *val)
270 if(strcmp(type, "onerror")==0) {
276 } else if(strcmp(type, "defreturn")==0) {
297 %token CONST VOID STRUCT UNION ENUM THREEDOTS
298 %token SIGNED UNSIGNED LONG SHORT INT FLOAT DOUBLE CHAR
300 %token <id> TOKEN NUMBER TYPETOKEN ARRAY_DIM
301 %token <cbuf> CCODE HTCODE PHCODE HCODE ACODE ATCODE
302 %token <line> PUBLIC PRIVATE PROTECTED ARGUMENT VIRTUAL SIGNAL OVERRIDE
306 prog: ccodes class ccodes { ; }
313 Node *node = new_ccode(C_CCODE,($<cbuf>1)->str,
315 nodes = g_list_append(nodes,node);
316 g_string_free($<cbuf>1,FALSE);
319 Node *node = new_ccode(H_CCODE,($<cbuf>1)->str,
321 nodes = g_list_append(nodes,node);
322 g_string_free($<cbuf>1,FALSE);
325 Node *node = new_ccode(HT_CCODE,($<cbuf>1)->str,
327 nodes = g_list_append(nodes,node);
328 g_string_free($<cbuf>1,FALSE);
331 Node *node = new_ccode(PH_CCODE,($<cbuf>1)->str,
333 nodes = g_list_append(nodes,node);
334 g_string_free($<cbuf>1,FALSE);
337 Node *node = new_ccode(A_CCODE,($<cbuf>1)->str,
339 nodes = g_list_append(nodes,node);
340 g_string_free($<cbuf>1,FALSE);
343 Node *node = new_ccode(AT_CCODE,($<cbuf>1)->str,
345 nodes = g_list_append(nodes,node);
346 g_string_free($<cbuf>1,FALSE);
350 ccodes: ccodes ccode { ; }
354 class: classdec '{' classcode '}' {
355 ((Class *)class)->nodes = class_nodes;
357 nodes = g_list_append(nodes,class);
360 ((Class *)class)->nodes = NULL;
362 nodes = g_list_append(nodes,class);
366 classdec: CLASS TYPETOKEN FROM TYPETOKEN {
367 class = new_class($<id>2,$<id>4,NULL);
371 classcode: classcode thing { ; }
381 scope: PUBLIC { the_scope = PUBLIC_SCOPE; }
382 | PRIVATE { the_scope = PRIVATE_SCOPE; }
383 | PROTECTED { the_scope = PROTECTED_SCOPE; }
386 destructor: TOKEN TOKEN {
387 if(strcmp($<id>1, "destroywith")==0) {
390 destructor_line = line_no;
391 destructor_simple = TRUE;
395 yyerror(_("parse error"));
400 if(strcmp($<id>1, "destroy")==0) {
402 destructor = ($<cbuf>3)->str;
403 g_string_free($<cbuf>3, FALSE);
404 destructor_line = ccode_line;
405 destructor_simple = FALSE;
408 g_string_free($<cbuf>3, TRUE);
409 yyerror(_("parse error"));
415 initializer: '=' numtok {
416 initializer = $<id>2;
417 initializer_line = ccode_line;
420 initializer = ($<cbuf>3)->str;
421 initializer_line = ccode_line;
422 g_string_free($<cbuf>3, FALSE);
427 varoptions: destructor initializer { ; }
428 | initializer destructor { ; }
429 | initializer { destructor = NULL; }
430 | destructor { initializer = NULL; }
437 variable: scope type TOKEN varoptions ';' {
438 push_variable($<id>3, the_scope,$<line>1, NULL);
440 | scope type TOKEN ARRAY_DIM varoptions ';' {
441 push_variable($<id>3, the_scope, $<line>1, $<id>4);
444 argument: ARGUMENT flags argtype TOKEN TOKEN '{' CCODE TOKEN '{' CCODE ';' {
445 if(strcmp($<id>5,"get")==0 &&
446 strcmp($<id>8,"set")==0) {
448 Type *type = pop_type();
449 g_free($<id>5); g_free($<id>8);
450 node = new_argument($<id>3,type,$<list>2,$<id>4,
451 ($<cbuf>7)->str,$<line>6,
452 ($<cbuf>10)->str,$<line>9,
454 g_string_free($<cbuf>7,FALSE);
455 g_string_free($<cbuf>10,FALSE);
456 class_nodes = g_list_append(class_nodes,node);
457 } else if(strcmp($<id>5,"set")==0 &&
458 strcmp($<id>8,"get")==0) {
460 Type *type = pop_type();
461 g_free($<id>5); g_free($<id>8);
462 node = new_argument($<id>3,type,$<list>2,$<id>4,
463 ($<cbuf>10)->str,$<line>9,
464 ($<cbuf>7)->str,$<line>6,
466 g_string_free($<cbuf>10,FALSE);
467 g_string_free($<cbuf>7,FALSE);
468 class_nodes = g_list_append(class_nodes,node);
470 g_free($<id>3); g_free($<id>4);
471 g_free($<id>5); g_free($<id>8);
472 g_list_foreach($<list>2,(GFunc)g_free,NULL);
473 g_string_free($<cbuf>10,TRUE);
474 g_string_free($<cbuf>7,TRUE);
475 yyerror(_("parse error"));
479 | ARGUMENT flags argtype TOKEN TOKEN '{' CCODE ';' {
480 if(strcmp($<id>5,"get")==0) {
482 Type *type = pop_type();
484 node = new_argument($<id>3,type,$<list>2,$<id>4,
485 ($<cbuf>7)->str,$<line>6,
487 g_string_free($<cbuf>7,FALSE);
488 class_nodes = g_list_append(class_nodes,node);
489 } else if(strcmp($<id>5,"set")==0) {
491 Type *type = pop_type();
493 node = new_argument($<id>3,type,$<list>2,$<id>4,
494 NULL,0,($<cbuf>7)->str,
496 g_string_free($<cbuf>7,FALSE);
497 class_nodes = g_list_append(class_nodes,node);
499 g_free($<id>5); g_free($<id>3);
501 g_list_foreach($<list>2,(GFunc)g_free,NULL);
502 g_string_free($<cbuf>7,TRUE);
503 yyerror(_("parse error"));
507 | ARGUMENT flags argtype TOKEN TOKEN {
509 char *get, *set = NULL;
514 if(strcmp($<id>5,"link")!=0 &&
515 strcmp($<id>5,"stringlink")!=0 &&
516 strcmp($<id>5,"objectlink")!=0) {
517 g_free($<id>5); g_free($<id>3);
519 g_list_foreach($<list>2,(GFunc)g_free,NULL);
520 yyerror(_("parse error"));
526 var = find_var_or_die($<id>4, $<line>1);
527 if(var->scope == PRIVATE_SCOPE)
528 root = "self->_priv";
532 if(strcmp($<id>5,"link")==0) {
533 set = g_strdup_printf("%s->%s = ARG;",
535 } else if(strcmp($<id>5,"stringlink")==0) {
536 set = g_strdup_printf("g_free(%s->%s); "
537 "%s->%s = g_strdup(ARG);",
540 } else if(strcmp($<id>5,"objectlink")==0) {
541 set = g_strdup_printf(
543 "gtk_object_unref(GTK_OBJECT(%s->%s)); "
546 "gtk_object_ref(GTK_OBJECT(%s->%s)); ",
553 g_assert_not_reached();
556 /* get is the same for everything */
557 get = g_strdup_printf("ARG = %s->%s;", root, $<id>4);
563 type = copy_type(var->vtype);
565 node = new_argument($<id>3, type, $<list>2,
566 $<id>4, get, $<line>1,
567 set, $<line>1, $<line>1);
568 class_nodes = g_list_append(class_nodes,node);
572 argtype: TOKEN '(' TOKEN type ')' {
573 if(strcmp($<id>3,"type")!=0) {
576 yyerror(_("parse error"));
583 typestack = g_list_prepend(typestack,NULL);
587 flags: '(' flaglist ')' { $<list>$ = $<list>2; }
588 | { $<list>$ = NULL; }
591 flaglist: TOKEN '|' flaglist {
592 $<list>$ = g_list_append($<list>3,$<id>1);
595 $<list>$ = g_list_append(NULL,$<id>1);
602 Type *type = typestack->data;
603 char *oldname = type->name;
604 type->name = g_strconcat("const ",oldname,NULL);
610 Node *node = new_type(0,$<id>1,NULL);
611 typestack = g_list_prepend(typestack,node);
614 Node *node = new_type(stars,$<id>1,NULL);
616 typestack = g_list_prepend(typestack,node);
620 type2: UNSIGNED integer {
621 $<id>$ = g_strconcat("unsigned ",$<id>2,NULL);
624 $<id>$ = g_strconcat("signed ",$<id>2,NULL);
627 $<id>$ = g_strdup($<id>1);
630 $<id>$ = g_strdup("unsigned char");
633 $<id>$ = g_strdup("signed char");
636 $<id>$ = g_strdup("char");
639 $<id>$ = g_strdup("double");
642 $<id>$ = g_strdup("float");
648 $<id>$ = g_strconcat($<id>1,$<id>2,NULL);
655 $<id>$ = g_strdup("void");
666 $<id>$ = "short int";
687 stars: '*' stars { stars++; }
691 /* this never sets the_scope */
692 simplesigtype: TOKEN sigtype {
693 if(strcmp($<id>1, "first")==0)
694 $<sigtype>$ = SIGNAL_FIRST_METHOD;
695 else if(strcmp($<id>1, "last")==0)
696 $<sigtype>$ = SIGNAL_LAST_METHOD;
698 yyerror(_("signal must be 'first' or 'last'"));
705 $<sigtype>$ = SIGNAL_LAST_METHOD;
709 /* this always sets the_scope */
710 fullsigtype: scope TOKEN sigtype {
711 if(strcmp($<id>2,"first")==0)
712 $<sigtype>$ = SIGNAL_FIRST_METHOD;
713 else if(strcmp($<id>2,"last")==0)
714 $<sigtype>$ = SIGNAL_LAST_METHOD;
716 yyerror(_("signal must be 'first' or 'last'"));
722 | TOKEN scope sigtype {
723 if(strcmp($<id>1,"first")==0)
724 $<sigtype>$ = SIGNAL_FIRST_METHOD;
725 else if(strcmp($<id>1,"last")==0)
726 $<sigtype>$ = SIGNAL_LAST_METHOD;
728 yyerror(_("signal must be 'first' or 'last'"));
735 $<sigtype>$ = SIGNAL_LAST_METHOD;
738 /* the_scope was default thus public */
739 the_scope = PUBLIC_SCOPE;
743 sigtype: TOKEN '(' tokenlist ')' {
744 gtktypes = g_list_prepend(gtktypes, $<id>1);
748 tokenlist: tokenlist ',' TOKEN {
749 gtktypes = g_list_append(gtktypes, $<id>3);
752 gtktypes = g_list_append(gtktypes, $<id>1);
756 codenocode: '{' CCODE { $<cbuf>$ = $<cbuf>2; }
757 | ';' { $<cbuf>$ = NULL; }
760 /*here CCODE will include the ending '}' */
761 method: SIGNAL flags fullsigtype type TOKEN '(' funcargs ')' returnvals codenocode {
763 yyerror(_("signal without 'self' as "
765 free_all_global_state();
768 push_function(the_scope, $<sigtype>3,NULL,
769 $<id>5, $<cbuf>10,$<line>1,
770 ccode_line, vararg, $<list>2);
772 | scope SIGNAL flags simplesigtype type TOKEN '(' funcargs ')' returnvals codenocode {
774 yyerror(_("signal without 'self' as "
776 free_all_global_state();
779 push_function(the_scope, $<sigtype>4, NULL,
780 $<id>6, $<cbuf>11, $<line>2,
781 ccode_line, vararg, $<list>3);
783 | VIRTUAL scope type TOKEN '(' funcargs ')' returnvals codenocode {
785 yyerror(_("virtual method without 'self' as "
787 free_all_global_state();
790 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
792 ccode_line, vararg, NULL);
794 | scope VIRTUAL type TOKEN '(' funcargs ')' returnvals codenocode {
796 yyerror(_("virtual method without 'self' as "
798 free_all_global_state();
801 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
803 ccode_line, vararg, NULL);
805 | VIRTUAL type TOKEN '(' funcargs ')' returnvals codenocode {
807 yyerror(_("virtual method without 'self' as "
809 free_all_global_state();
812 push_function(PUBLIC_SCOPE, VIRTUAL_METHOD, NULL,
813 $<id>3, $<cbuf>8, $<line>1,
814 ccode_line, vararg, NULL);
816 | OVERRIDE '(' TYPETOKEN ')' type TOKEN '(' funcargs ')' returnvals codenocode {
817 push_function(NO_SCOPE, OVERRIDE_METHOD, $<id>3,
819 $<line>1, ccode_line,
822 | scope type TOKEN '(' funcargs ')' returnvals codenocode {
823 push_function(the_scope, REGULAR_METHOD, NULL, $<id>3,
824 $<cbuf>8, $<line>1, ccode_line,
827 | TOKEN '(' TOKEN ')' codenocode {
828 if(strcmp($<id>1, "init")==0) {
829 push_init_arg($<id>3,FALSE);
830 push_function(NO_SCOPE, INIT_METHOD, NULL,
831 $<id>1, $<cbuf>5, $<line>2,
832 ccode_line, FALSE, NULL);
833 } else if(strcmp($<id>1, "class_init")==0) {
834 push_init_arg($<id>3,TRUE);
835 push_function(NO_SCOPE, CLASS_INIT_METHOD, NULL,
836 $<id>1, $<cbuf>5, $<line>2,
837 ccode_line, FALSE, NULL);
841 g_string_free($<cbuf>3,TRUE);
842 yyerror(_("parse error"));
848 returnvals: TOKEN retcode {
849 g_free(onerror); onerror = NULL;
850 g_free(defreturn); defreturn = NULL;
851 if(!set_return_value($<id>1, $<id>2)) {
854 yyerror(_("parse error"));
859 | TOKEN retcode TOKEN retcode {
860 g_free(onerror); onerror = NULL;
861 g_free(defreturn); defreturn = NULL;
862 if(!set_return_value($<id>1, $<id>2)) {
863 g_free($<id>1); g_free($<id>2);
864 g_free($<id>3); g_free($<id>4);
865 yyerror(_("parse error"));
868 if(!set_return_value($<id>3, $<id>4)) {
869 onerror = defreturn = NULL;
870 g_free($<id>1); g_free($<id>2);
871 g_free($<id>3); g_free($<id>4);
872 yyerror(_("parse error"));
879 g_free(onerror); onerror = NULL;
880 g_free(defreturn); defreturn = NULL;
884 retcode: numtok { $<id>$ = $<id>1; }
886 $<id>$ = ($<cbuf>3)->str;
887 g_string_free($<cbuf>3, FALSE);
891 funcargs: VOID { vararg = FALSE; has_self = FALSE; }
895 if(strcmp($<id>1,"self")==0)
899 yyerror(_("parse error"));
903 | TOKEN ',' arglist {
905 if(strcmp($<id>1,"self")==0)
909 yyerror(_("parse error"));
913 | arglist { has_self = FALSE; }
916 arglist: arglist1 ',' THREEDOTS { vararg = TRUE; }
917 | arglist1 { vararg = FALSE; }
920 arglist1: arglist1 ',' arg { ; }
925 push_funcarg($<id>2,NULL);
927 | type TOKEN ARRAY_DIM {
928 push_funcarg($<id>2,$<id>3);
930 | type TOKEN '(' TOKEN checklist ')' {
931 if(strcmp($<id>4,"check")!=0) {
932 yyerror(_("parse error"));
936 push_funcarg($<id>2,NULL);
938 | type TOKEN ARRAY_DIM '(' TOKEN checklist ')' {
939 if(strcmp($<id>5,"check")!=0) {
940 yyerror(_("parse error"));
944 push_funcarg($<id>2,$<id>3);
948 checklist: checklist check { ; }
953 if(strcmp($<id>1,"type")==0) {
954 Node *node = new_check(TYPE_CHECK,NULL);
955 checks = g_list_append(checks,node);
956 } else if(strcmp($<id>1,"null")==0) {
957 Node *node = new_check(NULL_CHECK,NULL);
958 checks = g_list_append(checks,node);
960 yyerror(_("parse error"));
966 Node *node = new_check(GT_CHECK,$<id>2);
967 checks = g_list_append(checks,node);
970 Node *node = new_check(LT_CHECK,$<id>2);
971 checks = g_list_append(checks,node);
974 Node *node = new_check(GE_CHECK,$<id>3);
975 checks = g_list_append(checks,node);
978 Node *node = new_check(LE_CHECK,$<id>3);
979 checks = g_list_append(checks,node);
982 Node *node = new_check(EQ_CHECK,$<id>3);
983 checks = g_list_append(checks,node);
986 Node *node = new_check(NE_CHECK,$<id>3);
987 checks = g_list_append(checks,node);
991 numtok: NUMBER { $<id>$ = $<id>1; }
993 $<id>$ = g_strconcat("-",$<id>2,NULL);
996 | TOKEN { $<id>$ = $<id>1; }