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 g_assert(scope != CLASS_SCOPE);
129 if(method!=INIT_METHOD && method!=CLASS_INIT_METHOD) {
132 type = (Type *)new_type(0,g_strdup("void"),NULL);
135 /* a complicated and ugly test to figure out if we have
136 the wrong number of types for a signal */
137 if((method == SIGNAL_FIRST_METHOD ||
138 method == SIGNAL_LAST_METHOD) &&
139 g_list_length(gtktypes) != g_list_length(funcargs) &&
140 !(g_list_length(funcargs) == 1 &&
141 g_list_length(gtktypes) == 2 &&
142 strcmp(gtktypes->next->data, "NONE")==0)) {
143 print_error(TRUE, _("The number of GTK arguments and "
144 "function arguments for a signal "
145 "don't seem to match"), line_no);
147 if(g_list_length(gtktypes) > 2) {
149 for(li = gtktypes->next; li; li = li->next) {
150 if(strcmp(li->data, "NONE")==0) {
152 _("NONE can only appear in an "
153 "argument list by itself"),
160 c_cbuf = p = cbuf->str;
161 while(p && *p && (*p==' ' || *p=='\t' || *p=='\n' || *p=='\r'))
168 node = new_method(scope, method, type, oid, gtktypes, flags,
169 id, funcargs, onerror, defreturn, c_cbuf, line_no,
170 ccode_line, vararg, method_unique_id++);
174 /*only free segment if we haven't passed it
183 class_nodes = g_list_append(class_nodes, node);
187 free_all_global_state(void)
194 g_list_foreach(gtktypes, (GFunc)g_free, NULL);
195 g_list_free(gtktypes);
198 free_node_list(funcargs);
203 push_funcarg(char *name, char *postfix)
206 Type *type = pop_type();
208 type->postfix = postfix;
210 node = new_funcarg(type, name, checks);
213 funcargs = g_list_append(funcargs, node);
217 push_init_arg(char *name, int is_class)
224 tn = g_strconcat(((Class *)class)->otype,":Class",NULL);
226 tn = g_strdup(((Class *)class)->otype);
228 type = new_type(1,tn,NULL);
229 node = new_funcarg((Type *)type,name,NULL);
230 funcargs = g_list_prepend(funcargs, node);
239 type = new_type(1,g_strdup(((Class *)class)->otype),NULL);
240 ch = g_list_append(ch,new_check(NULL_CHECK,NULL));
241 ch = g_list_append(ch,new_check(TYPE_CHECK,NULL));
242 node = new_funcarg((Type *)type,id,ch);
243 funcargs = g_list_prepend(funcargs, node);
247 find_var_or_die(const char *id, int line)
252 for(li = class_nodes; li != NULL; li = li->next) {
254 Node *node = li->data;
255 if(node->type != VARIABLE_NODE)
258 if(strcmp(var->id, id)==0)
262 s = g_strdup_printf(_("Variable %s not defined here"), id);
263 print_error(FALSE, s, line);
265 g_assert_not_reached();
270 set_return_value(char *type, char *val)
272 if(strcmp(type, "onerror")==0) {
278 } else if(strcmp(type, "defreturn")==0) {
299 %token CONST VOID STRUCT UNION ENUM THREEDOTS
300 %token SIGNED UNSIGNED LONG SHORT INT FLOAT DOUBLE CHAR
302 %token <id> TOKEN NUMBER TYPETOKEN ARRAY_DIM
303 %token <cbuf> CCODE HTCODE PHCODE HCODE ACODE ATCODE
304 %token <line> PUBLIC PRIVATE PROTECTED CLASSWIDE ARGUMENT VIRTUAL SIGNAL OVERRIDE
308 prog: ccodes class ccodes { ; }
315 Node *node = new_ccode(C_CCODE,($<cbuf>1)->str,
317 nodes = g_list_append(nodes,node);
318 g_string_free($<cbuf>1,FALSE);
321 Node *node = new_ccode(H_CCODE,($<cbuf>1)->str,
323 nodes = g_list_append(nodes,node);
324 g_string_free($<cbuf>1,FALSE);
327 Node *node = new_ccode(HT_CCODE,($<cbuf>1)->str,
329 nodes = g_list_append(nodes,node);
330 g_string_free($<cbuf>1,FALSE);
333 Node *node = new_ccode(PH_CCODE,($<cbuf>1)->str,
335 nodes = g_list_append(nodes,node);
336 g_string_free($<cbuf>1,FALSE);
339 Node *node = new_ccode(A_CCODE,($<cbuf>1)->str,
341 nodes = g_list_append(nodes,node);
342 g_string_free($<cbuf>1,FALSE);
345 Node *node = new_ccode(AT_CCODE,($<cbuf>1)->str,
347 nodes = g_list_append(nodes,node);
348 g_string_free($<cbuf>1,FALSE);
352 ccodes: ccodes ccode { ; }
356 class: classdec '{' classcode '}' {
357 ((Class *)class)->nodes = class_nodes;
359 nodes = g_list_append(nodes,class);
362 ((Class *)class)->nodes = NULL;
364 nodes = g_list_append(nodes,class);
368 classdec: CLASS TYPETOKEN FROM TYPETOKEN {
369 class = new_class($<id>2,$<id>4,NULL);
373 classcode: classcode thing { ; }
383 scope: PUBLIC { the_scope = PUBLIC_SCOPE; }
384 | PRIVATE { the_scope = PRIVATE_SCOPE; }
385 | PROTECTED { the_scope = PROTECTED_SCOPE; }
386 | CLASSWIDE { the_scope = CLASS_SCOPE; }
389 destructor: TOKEN TOKEN {
390 if(strcmp($<id>1, "destroywith")==0) {
393 destructor_line = line_no;
394 destructor_simple = TRUE;
398 yyerror(_("parse error"));
403 if(strcmp($<id>1, "destroy")==0) {
405 destructor = ($<cbuf>3)->str;
406 g_string_free($<cbuf>3, FALSE);
407 destructor_line = ccode_line;
408 destructor_simple = FALSE;
411 g_string_free($<cbuf>3, TRUE);
412 yyerror(_("parse error"));
418 initializer: '=' numtok {
419 initializer = $<id>2;
420 initializer_line = ccode_line;
423 initializer = ($<cbuf>3)->str;
424 initializer_line = ccode_line;
425 g_string_free($<cbuf>3, FALSE);
430 varoptions: destructor initializer { ; }
431 | initializer destructor { ; }
432 | initializer { destructor = NULL; }
433 | destructor { initializer = NULL; }
440 variable: scope type TOKEN varoptions ';' {
441 push_variable($<id>3, the_scope,$<line>1, NULL);
443 | scope type TOKEN ARRAY_DIM varoptions ';' {
444 push_variable($<id>3, the_scope, $<line>1, $<id>4);
447 argument: ARGUMENT flags argtype TOKEN TOKEN '{' CCODE TOKEN '{' CCODE ';' {
448 if(strcmp($<id>5,"get")==0 &&
449 strcmp($<id>8,"set")==0) {
451 Type *type = pop_type();
452 g_free($<id>5); g_free($<id>8);
453 node = new_argument($<id>3,type,$<list>2,$<id>4,
454 ($<cbuf>7)->str,$<line>6,
455 ($<cbuf>10)->str,$<line>9,
457 g_string_free($<cbuf>7,FALSE);
458 g_string_free($<cbuf>10,FALSE);
459 class_nodes = g_list_append(class_nodes,node);
460 } else if(strcmp($<id>5,"set")==0 &&
461 strcmp($<id>8,"get")==0) {
463 Type *type = pop_type();
464 g_free($<id>5); g_free($<id>8);
465 node = new_argument($<id>3,type,$<list>2,$<id>4,
466 ($<cbuf>10)->str,$<line>9,
467 ($<cbuf>7)->str,$<line>6,
469 g_string_free($<cbuf>10,FALSE);
470 g_string_free($<cbuf>7,FALSE);
471 class_nodes = g_list_append(class_nodes,node);
473 g_free($<id>3); g_free($<id>4);
474 g_free($<id>5); g_free($<id>8);
475 g_list_foreach($<list>2,(GFunc)g_free,NULL);
476 g_string_free($<cbuf>10,TRUE);
477 g_string_free($<cbuf>7,TRUE);
478 yyerror(_("parse error"));
482 | ARGUMENT flags argtype TOKEN TOKEN '{' CCODE ';' {
483 if(strcmp($<id>5,"get")==0) {
485 Type *type = pop_type();
487 node = new_argument($<id>3,type,$<list>2,$<id>4,
488 ($<cbuf>7)->str,$<line>6,
490 g_string_free($<cbuf>7,FALSE);
491 class_nodes = g_list_append(class_nodes,node);
492 } else if(strcmp($<id>5,"set")==0) {
494 Type *type = pop_type();
496 node = new_argument($<id>3,type,$<list>2,$<id>4,
497 NULL,0,($<cbuf>7)->str,
499 g_string_free($<cbuf>7,FALSE);
500 class_nodes = g_list_append(class_nodes,node);
502 g_free($<id>5); g_free($<id>3);
504 g_list_foreach($<list>2,(GFunc)g_free,NULL);
505 g_string_free($<cbuf>7,TRUE);
506 yyerror(_("parse error"));
510 | ARGUMENT flags argtype TOKEN TOKEN {
512 char *get, *set = NULL;
517 if(strcmp($<id>5, "link")!=0 &&
518 strcmp($<id>5, "stringlink")!=0 &&
519 strcmp($<id>5, "objectlink")!=0) {
520 g_free($<id>5); g_free($<id>3);
522 g_list_foreach($<list>2,(GFunc)g_free,NULL);
523 yyerror(_("parse error"));
529 var = find_var_or_die($<id>4, $<line>1);
530 if(var->scope == PRIVATE_SCOPE)
531 root = "self->_priv";
532 else if(var->scope == CLASS_SCOPE)
533 root = "SELF_CLASS(GTK_OBJECT(self)->klass)";
537 if(strcmp($<id>5, "link")==0) {
538 set = g_strdup_printf("%s->%s = ARG;",
540 } else if(strcmp($<id>5, "stringlink")==0) {
541 set = g_strdup_printf("g_free(%s->%s); "
542 "%s->%s = g_strdup(ARG);",
545 } else if(strcmp($<id>5, "objectlink")==0) {
546 set = g_strdup_printf(
548 "gtk_object_unref(GTK_OBJECT(%s->%s)); "
551 "gtk_object_ref(GTK_OBJECT(%s->%s));",
561 g_assert_not_reached();
564 /* get is the same for everything */
565 get = g_strdup_printf("ARG = %s->%s;", root, $<id>4);
571 type = copy_type(var->vtype);
573 node = new_argument($<id>3, type, $<list>2,
574 $<id>4, get, $<line>1,
575 set, $<line>1, $<line>1);
576 class_nodes = g_list_append(class_nodes,node);
580 argtype: TOKEN '(' TOKEN type ')' {
581 if(strcmp($<id>3,"type")!=0) {
584 yyerror(_("parse error"));
591 typestack = g_list_prepend(typestack,NULL);
595 flags: '(' flaglist ')' { $<list>$ = $<list>2; }
596 | { $<list>$ = NULL; }
599 flaglist: TOKEN '|' flaglist {
600 $<list>$ = g_list_append($<list>3,$<id>1);
603 $<list>$ = g_list_append(NULL,$<id>1);
610 Type *type = typestack->data;
611 char *oldname = type->name;
612 type->name = g_strconcat("const ",oldname,NULL);
618 Node *node = new_type(0,$<id>1,NULL);
619 typestack = g_list_prepend(typestack,node);
622 Node *node = new_type(stars,$<id>1,NULL);
624 typestack = g_list_prepend(typestack,node);
628 type2: UNSIGNED integer {
629 $<id>$ = g_strconcat("unsigned ",$<id>2,NULL);
632 $<id>$ = g_strconcat("signed ",$<id>2,NULL);
635 $<id>$ = g_strdup($<id>1);
638 $<id>$ = g_strdup("unsigned char");
641 $<id>$ = g_strdup("signed char");
644 $<id>$ = g_strdup("char");
647 $<id>$ = g_strdup("double");
650 $<id>$ = g_strdup("float");
656 $<id>$ = g_strconcat($<id>1,$<id>2,NULL);
663 $<id>$ = g_strdup("void");
674 $<id>$ = "short int";
695 stars: '*' stars { stars++; }
699 /* this never sets the_scope */
700 simplesigtype: TOKEN sigtype {
701 if(strcmp($<id>1, "first")==0)
702 $<sigtype>$ = SIGNAL_FIRST_METHOD;
703 else if(strcmp($<id>1, "last")==0)
704 $<sigtype>$ = SIGNAL_LAST_METHOD;
706 yyerror(_("signal must be 'first' or 'last'"));
713 $<sigtype>$ = SIGNAL_LAST_METHOD;
717 /* this always sets the_scope */
718 fullsigtype: scope TOKEN sigtype {
719 if(strcmp($<id>2,"first")==0)
720 $<sigtype>$ = SIGNAL_FIRST_METHOD;
721 else if(strcmp($<id>2,"last")==0)
722 $<sigtype>$ = SIGNAL_LAST_METHOD;
724 yyerror(_("signal must be 'first' or 'last'"));
730 | TOKEN scope sigtype {
731 if(strcmp($<id>1,"first")==0)
732 $<sigtype>$ = SIGNAL_FIRST_METHOD;
733 else if(strcmp($<id>1,"last")==0)
734 $<sigtype>$ = SIGNAL_LAST_METHOD;
736 yyerror(_("signal must be 'first' or 'last'"));
743 $<sigtype>$ = SIGNAL_LAST_METHOD;
746 /* the_scope was default thus public */
747 the_scope = PUBLIC_SCOPE;
751 sigtype: TOKEN '(' tokenlist ')' {
752 gtktypes = g_list_prepend(gtktypes, $<id>1);
756 tokenlist: tokenlist ',' TOKEN {
757 gtktypes = g_list_append(gtktypes, $<id>3);
760 gtktypes = g_list_append(gtktypes, $<id>1);
764 codenocode: '{' CCODE { $<cbuf>$ = $<cbuf>2; }
765 | ';' { $<cbuf>$ = NULL; }
768 /*here CCODE will include the ending '}' */
769 method: SIGNAL flags fullsigtype type TOKEN '(' funcargs ')' returnvals codenocode {
771 yyerror(_("signal without 'self' as "
773 free_all_global_state();
776 if(the_scope == CLASS_SCOPE) {
777 yyerror(_("a method cannot be of class scope"));
778 free_all_global_state();
781 push_function(the_scope, $<sigtype>3,NULL,
782 $<id>5, $<cbuf>10,$<line>1,
783 ccode_line, vararg, $<list>2);
785 | scope SIGNAL flags simplesigtype type TOKEN '(' funcargs ')' returnvals codenocode {
787 yyerror(_("signal without 'self' as "
789 free_all_global_state();
792 if(the_scope == CLASS_SCOPE) {
793 yyerror(_("a method cannot be of class scope"));
794 free_all_global_state();
797 push_function(the_scope, $<sigtype>4, NULL,
798 $<id>6, $<cbuf>11, $<line>2,
799 ccode_line, vararg, $<list>3);
801 | VIRTUAL scope type TOKEN '(' funcargs ')' returnvals codenocode {
803 yyerror(_("virtual method without 'self' as "
805 free_all_global_state();
808 if(the_scope == CLASS_SCOPE) {
809 yyerror(_("a method cannot be of class scope"));
810 free_all_global_state();
813 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
815 ccode_line, vararg, NULL);
817 | scope VIRTUAL type TOKEN '(' funcargs ')' returnvals codenocode {
819 yyerror(_("virtual method without 'self' as "
821 free_all_global_state();
824 if(the_scope == CLASS_SCOPE) {
825 yyerror(_("a method cannot be of class scope"));
826 free_all_global_state();
829 push_function(the_scope, VIRTUAL_METHOD, NULL, $<id>4,
831 ccode_line, vararg, NULL);
833 | VIRTUAL type TOKEN '(' funcargs ')' returnvals codenocode {
835 yyerror(_("virtual method without 'self' as "
837 free_all_global_state();
840 push_function(PUBLIC_SCOPE, VIRTUAL_METHOD, NULL,
841 $<id>3, $<cbuf>8, $<line>1,
842 ccode_line, vararg, NULL);
844 | OVERRIDE '(' TYPETOKEN ')' type TOKEN '(' funcargs ')' returnvals codenocode {
845 push_function(NO_SCOPE, OVERRIDE_METHOD, $<id>3,
847 $<line>1, ccode_line,
850 | scope type TOKEN '(' funcargs ')' returnvals codenocode {
851 if(the_scope == CLASS_SCOPE) {
852 yyerror(_("a method cannot be of class scope"));
853 free_all_global_state();
856 push_function(the_scope, REGULAR_METHOD, NULL, $<id>3,
857 $<cbuf>8, $<line>1, ccode_line,
860 | TOKEN '(' TOKEN ')' codenocode {
861 if(strcmp($<id>1, "init")==0) {
862 push_init_arg($<id>3,FALSE);
863 push_function(NO_SCOPE, INIT_METHOD, NULL,
864 $<id>1, $<cbuf>5, $<line>2,
865 ccode_line, FALSE, NULL);
866 } else if(strcmp($<id>1, "class_init")==0) {
867 push_init_arg($<id>3,TRUE);
868 push_function(NO_SCOPE, CLASS_INIT_METHOD, NULL,
869 $<id>1, $<cbuf>5, $<line>2,
870 ccode_line, FALSE, NULL);
874 g_string_free($<cbuf>5,TRUE);
875 yyerror(_("parse error "
876 "(untyped blocks must be init or "
883 returnvals: TOKEN retcode {
884 g_free(onerror); onerror = NULL;
885 g_free(defreturn); defreturn = NULL;
886 if(!set_return_value($<id>1, $<id>2)) {
889 yyerror(_("parse error"));
894 | TOKEN retcode TOKEN retcode {
895 g_free(onerror); onerror = NULL;
896 g_free(defreturn); defreturn = NULL;
897 if(!set_return_value($<id>1, $<id>2)) {
898 g_free($<id>1); g_free($<id>2);
899 g_free($<id>3); g_free($<id>4);
900 yyerror(_("parse error"));
903 if(!set_return_value($<id>3, $<id>4)) {
904 onerror = defreturn = NULL;
905 g_free($<id>1); g_free($<id>2);
906 g_free($<id>3); g_free($<id>4);
907 yyerror(_("parse error"));
914 g_free(onerror); onerror = NULL;
915 g_free(defreturn); defreturn = NULL;
919 retcode: numtok { $<id>$ = $<id>1; }
921 $<id>$ = ($<cbuf>3)->str;
922 g_string_free($<cbuf>3, FALSE);
926 funcargs: VOID { vararg = FALSE; has_self = FALSE; }
930 if(strcmp($<id>1,"self")==0)
934 yyerror(_("parse error"));
938 | TOKEN ',' arglist {
940 if(strcmp($<id>1,"self")==0)
944 yyerror(_("parse error"));
948 | arglist { has_self = FALSE; }
951 arglist: arglist1 ',' THREEDOTS { vararg = TRUE; }
952 | arglist1 { vararg = FALSE; }
955 arglist1: arglist1 ',' arg { ; }
960 push_funcarg($<id>2,NULL);
962 | type TOKEN ARRAY_DIM {
963 push_funcarg($<id>2,$<id>3);
965 | type TOKEN '(' TOKEN checklist ')' {
966 if(strcmp($<id>4,"check")!=0) {
967 yyerror(_("parse error"));
971 push_funcarg($<id>2,NULL);
973 | type TOKEN ARRAY_DIM '(' TOKEN checklist ')' {
974 if(strcmp($<id>5,"check")!=0) {
975 yyerror(_("parse error"));
979 push_funcarg($<id>2,$<id>3);
983 checklist: checklist check { ; }
988 if(strcmp($<id>1,"type")==0) {
989 Node *node = new_check(TYPE_CHECK,NULL);
990 checks = g_list_append(checks,node);
991 } else if(strcmp($<id>1,"null")==0) {
992 Node *node = new_check(NULL_CHECK,NULL);
993 checks = g_list_append(checks,node);
995 yyerror(_("parse error"));
1001 Node *node = new_check(GT_CHECK,$<id>2);
1002 checks = g_list_append(checks,node);
1005 Node *node = new_check(LT_CHECK,$<id>2);
1006 checks = g_list_append(checks,node);
1009 Node *node = new_check(GE_CHECK,$<id>3);
1010 checks = g_list_append(checks,node);
1013 Node *node = new_check(LE_CHECK,$<id>3);
1014 checks = g_list_append(checks,node);
1017 Node *node = new_check(EQ_CHECK,$<id>3);
1018 checks = g_list_append(checks,node);
1021 Node *node = new_check(NE_CHECK,$<id>3);
1022 checks = g_list_append(checks,node);
1026 numtok: NUMBER { $<id>$ = $<id>1; }
1028 $<id>$ = g_strconcat("-",$<id>2,NULL);
1031 | TOKEN { $<id>$ = $<id>1; }