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);
147 c_cbuf = p = cbuf->str;
148 while(p && *p && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
155 node = new_method(scope, method, type, oid, gtktypes, flags,
156 id, funcargs, onerror, defreturn, c_cbuf, line_no,
161 /*only free segment if we haven't passed it
170 class_nodes = g_list_append(class_nodes, node);
174 free_all_global_state(void)
181 g_list_foreach(gtktypes, (GFunc)g_free, NULL);
182 g_list_free(gtktypes);
185 free_node_list(funcargs);
190 push_funcarg(char *name, char *postfix)
193 Type *type = pop_type();
195 type->postfix = postfix;
197 node = new_funcarg(type, name, checks);
200 funcargs = g_list_append(funcargs, node);
204 push_init_arg(char *name, int is_class)
211 tn = g_strconcat(((Class *)class)->otype,":Class",NULL);
213 tn = g_strdup(((Class *)class)->otype);
215 type = new_type(1,tn,NULL);
216 node = new_funcarg((Type *)type,name,NULL);
217 funcargs = g_list_prepend(funcargs, node);
226 type = new_type(1,g_strdup(((Class *)class)->otype),NULL);
227 ch = g_list_append(ch,new_check(NULL_CHECK,NULL));
228 ch = g_list_append(ch,new_check(TYPE_CHECK,NULL));
229 node = new_funcarg((Type *)type,id,ch);
230 funcargs = g_list_prepend(funcargs, node);
234 find_var_or_die(const char *id, int line)
239 for(li = class_nodes; li != NULL; li = li->next) {
241 Node *node = li->data;
242 if(node->type != VARIABLE_NODE)
245 if(strcmp(var->id, id)==0)
249 s = g_strdup_printf(_("Variable %s not defined here"), id);
250 print_error(FALSE, s, line);
252 g_assert_not_reached();
257 set_return_value(char *type, char *val)
259 if(strcmp(type, "onerror")==0) {
265 } else if(strcmp(type, "defreturn")==0) {
286 %token CONST VOID STRUCT UNION ENUM THREEDOTS
287 %token SIGNED UNSIGNED LONG SHORT INT FLOAT DOUBLE CHAR
289 %token <id> TOKEN NUMBER TYPETOKEN ARRAY_DIM
290 %token <cbuf> CCODE HTCODE PHCODE HCODE ACODE ATCODE
291 %token <line> PUBLIC PRIVATE PROTECTED ARGUMENT VIRTUAL SIGNAL OVERRIDE
295 prog: ccodes class ccodes { ; }
302 Node *node = new_ccode(C_CCODE,($<cbuf>1)->str,
304 nodes = g_list_append(nodes,node);
305 g_string_free($<cbuf>1,FALSE);
308 Node *node = new_ccode(H_CCODE,($<cbuf>1)->str,
310 nodes = g_list_append(nodes,node);
311 g_string_free($<cbuf>1,FALSE);
314 Node *node = new_ccode(HT_CCODE,($<cbuf>1)->str,
316 nodes = g_list_append(nodes,node);
317 g_string_free($<cbuf>1,FALSE);
320 Node *node = new_ccode(PH_CCODE,($<cbuf>1)->str,
322 nodes = g_list_append(nodes,node);
323 g_string_free($<cbuf>1,FALSE);
326 Node *node = new_ccode(A_CCODE,($<cbuf>1)->str,
328 nodes = g_list_append(nodes,node);
329 g_string_free($<cbuf>1,FALSE);
332 Node *node = new_ccode(AT_CCODE,($<cbuf>1)->str,
334 nodes = g_list_append(nodes,node);
335 g_string_free($<cbuf>1,FALSE);
339 ccodes: ccodes ccode { ; }
343 class: classdec '{' classcode '}' {
344 ((Class *)class)->nodes = class_nodes;
346 nodes = g_list_append(nodes,class);
349 ((Class *)class)->nodes = NULL;
351 nodes = g_list_append(nodes,class);
355 classdec: CLASS TYPETOKEN FROM TYPETOKEN {
356 class = new_class($<id>2,$<id>4,NULL);
360 classcode: classcode thing { ; }
370 scope: PUBLIC { the_scope = PUBLIC_SCOPE; }
371 | PRIVATE { the_scope = PRIVATE_SCOPE; }
372 | PROTECTED { the_scope = PROTECTED_SCOPE; }
375 destructor: TOKEN TOKEN {
376 if(strcmp($<id>1, "destroywith")==0) {
379 destructor_line = line_no;
380 destructor_simple = TRUE;
384 yyerror(_("parse error"));
389 if(strcmp($<id>1, "destroy")==0) {
391 destructor = ($<cbuf>3)->str;
392 g_string_free($<cbuf>3, FALSE);
393 destructor_line = ccode_line;
394 destructor_simple = FALSE;
397 g_string_free($<cbuf>3, TRUE);
398 yyerror(_("parse error"));
404 initializer: '=' numtok {
405 initializer = $<id>2;
406 initializer_line = ccode_line;
409 initializer = ($<cbuf>3)->str;
410 initializer_line = ccode_line;
411 g_string_free($<cbuf>3, FALSE);
416 varoptions: destructor initializer { ; }
417 | initializer destructor { ; }
418 | initializer { destructor = NULL; }
419 | destructor { initializer = NULL; }
426 variable: scope type TOKEN varoptions ';' {
427 push_variable($<id>3, the_scope,$<line>1, NULL);
429 | scope type TOKEN ARRAY_DIM varoptions ';' {
430 push_variable($<id>3, the_scope, $<line>1, $<id>4);
433 argument: ARGUMENT flags argtype TOKEN TOKEN '{' CCODE TOKEN '{' CCODE ';' {
434 if(strcmp($<id>5,"get")==0 &&
435 strcmp($<id>8,"set")==0) {
437 Type *type = pop_type();
438 g_free($<id>5); g_free($<id>8);
439 node = new_argument($<id>3,type,$<list>2,$<id>4,
440 ($<cbuf>7)->str,$<line>6,
441 ($<cbuf>10)->str,$<line>9,
443 g_string_free($<cbuf>7,FALSE);
444 g_string_free($<cbuf>10,FALSE);
445 class_nodes = g_list_append(class_nodes,node);
446 } else if(strcmp($<id>5,"set")==0 &&
447 strcmp($<id>8,"get")==0) {
449 Type *type = pop_type();
450 g_free($<id>5); g_free($<id>8);
451 node = new_argument($<id>3,type,$<list>2,$<id>4,
452 ($<cbuf>10)->str,$<line>9,
453 ($<cbuf>7)->str,$<line>6,
455 g_string_free($<cbuf>10,FALSE);
456 g_string_free($<cbuf>7,FALSE);
457 class_nodes = g_list_append(class_nodes,node);
459 g_free($<id>3); g_free($<id>4);
460 g_free($<id>5); g_free($<id>8);
461 g_list_foreach($<list>2,(GFunc)g_free,NULL);
462 g_string_free($<cbuf>10,TRUE);
463 g_string_free($<cbuf>7,TRUE);
464 yyerror(_("parse error"));
468 | ARGUMENT flags argtype TOKEN TOKEN '{' CCODE ';' {
469 if(strcmp($<id>5,"get")==0) {
471 Type *type = pop_type();
473 node = new_argument($<id>3,type,$<list>2,$<id>4,
474 ($<cbuf>7)->str,$<line>6,
476 g_string_free($<cbuf>7,FALSE);
477 class_nodes = g_list_append(class_nodes,node);
478 } else if(strcmp($<id>5,"set")==0) {
480 Type *type = pop_type();
482 node = new_argument($<id>3,type,$<list>2,$<id>4,
483 NULL,0,($<cbuf>7)->str,
485 g_string_free($<cbuf>7,FALSE);
486 class_nodes = g_list_append(class_nodes,node);
488 g_free($<id>5); g_free($<id>3);
490 g_list_foreach($<list>2,(GFunc)g_free,NULL);
491 g_string_free($<cbuf>7,TRUE);
492 yyerror(_("parse error"));
496 | ARGUMENT flags argtype TOKEN TOKEN {
498 char *get, *set = NULL;
503 if(strcmp($<id>5,"link")!=0 &&
504 strcmp($<id>5,"stringlink")!=0 &&
505 strcmp($<id>5,"objectlink")!=0) {
506 g_free($<id>5); g_free($<id>3);
508 g_list_foreach($<list>2,(GFunc)g_free,NULL);
509 yyerror(_("parse error"));
515 var = find_var_or_die($<id>4, $<line>1);
516 if(var->scope == PRIVATE_SCOPE)
517 root = "self->_priv";
521 if(strcmp($<id>5,"link")==0) {
522 set = g_strdup_printf("%s->%s = ARG;",
524 } else if(strcmp($<id>5,"stringlink")==0) {
525 set = g_strdup_printf("g_free(%s->%s); "
526 "%s->%s = g_strdup(ARG);",
529 } else if(strcmp($<id>5,"objectlink")==0) {
530 set = g_strdup_printf(
532 "gtk_object_unref(GTK_OBJECT(%s->%s)); "
535 "gtk_object_ref(GTK_OBJECT(%s->%s)); ",
542 g_assert_not_reached();
545 /* get is the same for everything */
546 get = g_strdup_printf("ARG = %s->%s;", root, $<id>4);
552 type = copy_type(var->vtype);
554 node = new_argument($<id>3, type, $<list>2,
555 $<id>4, get, $<line>1,
556 set, $<line>1, $<line>1);
557 class_nodes = g_list_append(class_nodes,node);
561 argtype: TOKEN '(' TOKEN type ')' {
562 if(strcmp($<id>3,"type")!=0) {
565 yyerror(_("parse error"));
572 typestack = g_list_prepend(typestack,NULL);
576 flags: '(' flaglist ')' { $<list>$ = $<list>2; }
577 | { $<list>$ = NULL; }
580 flaglist: TOKEN '|' flaglist {
581 $<list>$ = g_list_append($<list>3,$<id>1);
584 $<list>$ = g_list_append(NULL,$<id>1);
591 Type *type = typestack->data;
592 char *oldname = type->name;
593 type->name = g_strconcat("const ",oldname,NULL);
599 Node *node = new_type(0,$<id>1,NULL);
600 typestack = g_list_prepend(typestack,node);
603 Node *node = new_type(stars,$<id>1,NULL);
605 typestack = g_list_prepend(typestack,node);
609 type2: UNSIGNED integer {
610 $<id>$ = g_strconcat("unsigned ",$<id>2,NULL);
613 $<id>$ = g_strconcat("signed ",$<id>2,NULL);
616 $<id>$ = g_strdup($<id>1);
619 $<id>$ = g_strdup("unsigned char");
622 $<id>$ = g_strdup("signed char");
625 $<id>$ = g_strdup("char");
628 $<id>$ = g_strdup("double");
631 $<id>$ = g_strdup("float");
637 $<id>$ = g_strconcat($<id>1,$<id>2,NULL);
644 $<id>$ = g_strdup("void");
655 $<id>$ = "short int";
676 stars: '*' stars { stars++; }
680 /* this never sets the_scope */
681 simplesigtype: TOKEN sigtype {
682 if(strcmp($<id>1, "first")==0)
683 $<sigtype>$ = SIGNAL_FIRST_METHOD;
684 else if(strcmp($<id>1, "last")==0)
685 $<sigtype>$ = SIGNAL_LAST_METHOD;
687 yyerror(_("signal must be 'first' or 'last'"));
694 $<sigtype>$ = SIGNAL_LAST_METHOD;
698 /* this always sets the_scope */
699 fullsigtype: scope TOKEN sigtype {
700 if(strcmp($<id>2,"first")==0)
701 $<sigtype>$ = SIGNAL_FIRST_METHOD;
702 else if(strcmp($<id>2,"last")==0)
703 $<sigtype>$ = SIGNAL_LAST_METHOD;
705 yyerror(_("signal must be 'first' or 'last'"));
711 | TOKEN scope sigtype {
712 if(strcmp($<id>1,"first")==0)
713 $<sigtype>$ = SIGNAL_FIRST_METHOD;
714 else if(strcmp($<id>1,"last")==0)
715 $<sigtype>$ = SIGNAL_LAST_METHOD;
717 yyerror(_("signal must be 'first' or 'last'"));
724 $<sigtype>$ = SIGNAL_LAST_METHOD;
727 /* the_scope was default thus public */
728 the_scope = PUBLIC_SCOPE;
732 sigtype: TOKEN '(' tokenlist ')' {
733 gtktypes = g_list_prepend(gtktypes, $<id>1);
737 tokenlist: tokenlist ',' TOKEN {
738 gtktypes = g_list_append(gtktypes, $<id>3);
741 gtktypes = g_list_append(gtktypes, $<id>1);
745 codenocode: '{' CCODE { $<cbuf>$ = $<cbuf>2; }
746 | ';' { $<cbuf>$ = NULL; }
749 /*here CCODE will include the ending '}' */
750 method: SIGNAL flags fullsigtype type TOKEN '(' funcargs ')' returnvals codenocode {
752 yyerror(_("signal without 'self' as "
754 free_all_global_state();
757 push_function(the_scope, $<sigtype>3,NULL,
758 $<id>5, $<cbuf>10,$<line>1,
759 ccode_line, vararg, $<list>2);
761 | scope SIGNAL flags simplesigtype type TOKEN '(' funcargs ')' returnvals codenocode {
763 yyerror(_("signal without 'self' as "
765 free_all_global_state();
768 push_function(the_scope, $<sigtype>4, NULL,
769 $<id>6, $<cbuf>11, $<line>2,
770 ccode_line, vararg, $<list>3);
772 | VIRTUAL scope type TOKEN '(' funcargs ')' returnvals codenocode {
774 yyerror(_("virtual method without 'self' as "
776 free_all_global_state();
779 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
781 ccode_line, vararg, NULL);
783 | scope VIRTUAL 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 | VIRTUAL type TOKEN '(' funcargs ')' returnvals codenocode {
796 yyerror(_("virtual method without 'self' as "
798 free_all_global_state();
801 push_function(PUBLIC_SCOPE, VIRTUAL_METHOD, NULL,
802 $<id>3, $<cbuf>8, $<line>1,
803 ccode_line, vararg, NULL);
805 | OVERRIDE '(' TYPETOKEN ')' type TOKEN '(' funcargs ')' returnvals codenocode {
806 push_function(NO_SCOPE, OVERRIDE_METHOD, $<id>3,
808 $<line>1, ccode_line,
811 | scope type TOKEN '(' funcargs ')' returnvals codenocode {
812 push_function(the_scope, REGULAR_METHOD, NULL, $<id>3,
813 $<cbuf>8, $<line>1, ccode_line,
816 | TOKEN '(' TOKEN ')' codenocode {
817 if(strcmp($<id>1, "init")==0) {
818 push_init_arg($<id>3,FALSE);
819 push_function(NO_SCOPE, INIT_METHOD, NULL,
820 $<id>1, $<cbuf>5, $<line>2,
821 ccode_line, FALSE, NULL);
822 } else if(strcmp($<id>1, "class_init")==0) {
823 push_init_arg($<id>3,TRUE);
824 push_function(NO_SCOPE, CLASS_INIT_METHOD, NULL,
825 $<id>1, $<cbuf>5, $<line>2,
826 ccode_line, FALSE, NULL);
830 g_string_free($<cbuf>3,TRUE);
831 yyerror(_("parse error"));
837 returnvals: TOKEN retcode {
838 g_free(onerror); onerror = NULL;
839 g_free(defreturn); defreturn = NULL;
840 if(!set_return_value($<id>1, $<id>2)) {
843 yyerror(_("parse error"));
848 | TOKEN retcode TOKEN retcode {
849 g_free(onerror); onerror = NULL;
850 g_free(defreturn); defreturn = NULL;
851 if(!set_return_value($<id>1, $<id>2)) {
852 g_free($<id>1); g_free($<id>2);
853 g_free($<id>3); g_free($<id>4);
854 yyerror(_("parse error"));
857 if(!set_return_value($<id>3, $<id>4)) {
858 onerror = defreturn = NULL;
859 g_free($<id>1); g_free($<id>2);
860 g_free($<id>3); g_free($<id>4);
861 yyerror(_("parse error"));
868 g_free(onerror); onerror = NULL;
869 g_free(defreturn); defreturn = NULL;
873 retcode: numtok { $<id>$ = $<id>1; }
875 $<id>$ = ($<cbuf>3)->str;
876 g_string_free($<cbuf>3, FALSE);
880 funcargs: VOID { vararg = FALSE; has_self = FALSE; }
884 if(strcmp($<id>1,"self")==0)
888 yyerror(_("parse error"));
892 | TOKEN ',' arglist {
894 if(strcmp($<id>1,"self")==0)
898 yyerror(_("parse error"));
902 | arglist { has_self = FALSE; }
905 arglist: arglist1 ',' THREEDOTS { vararg = TRUE; }
906 | arglist1 { vararg = FALSE; }
909 arglist1: arglist1 ',' arg { ; }
914 push_funcarg($<id>2,NULL);
916 | type TOKEN ARRAY_DIM {
917 push_funcarg($<id>2,$<id>3);
919 | type TOKEN '(' TOKEN checklist ')' {
920 if(strcmp($<id>4,"check")!=0) {
921 yyerror(_("parse error"));
925 push_funcarg($<id>2,NULL);
927 | type TOKEN ARRAY_DIM '(' TOKEN checklist ')' {
928 if(strcmp($<id>5,"check")!=0) {
929 yyerror(_("parse error"));
933 push_funcarg($<id>2,$<id>3);
937 checklist: checklist check { ; }
942 if(strcmp($<id>1,"type")==0) {
943 Node *node = new_check(TYPE_CHECK,NULL);
944 checks = g_list_append(checks,node);
945 } else if(strcmp($<id>1,"null")==0) {
946 Node *node = new_check(NULL_CHECK,NULL);
947 checks = g_list_append(checks,node);
949 yyerror(_("parse error"));
955 Node *node = new_check(GT_CHECK,$<id>2);
956 checks = g_list_append(checks,node);
959 Node *node = new_check(LT_CHECK,$<id>2);
960 checks = g_list_append(checks,node);
963 Node *node = new_check(GE_CHECK,$<id>3);
964 checks = g_list_append(checks,node);
967 Node *node = new_check(LE_CHECK,$<id>3);
968 checks = g_list_append(checks,node);
971 Node *node = new_check(EQ_CHECK,$<id>3);
972 checks = g_list_append(checks,node);
975 Node *node = new_check(NE_CHECK,$<id>3);
976 checks = g_list_append(checks,node);
980 numtok: NUMBER { $<id>$ = $<id>1; }
982 $<id>$ = g_strconcat("-",$<id>2,NULL);
985 | TOKEN { $<id>$ = $<id>1; }