X-Git-Url: http://git.draconx.ca/gitweb/gob-dx.git/blobdiff_plain/1656cfade4879af0cb003e3847e58264caa5781f..b17287deb56775a49030d738d8c8c0e9cd15f9fe:/src/main.c diff --git a/src/main.c b/src/main.c index d5d299e..dedf432 100644 --- a/src/main.c +++ b/src/main.c @@ -71,6 +71,7 @@ static gboolean made_aliases = FALSE; /* if we made any shorthand aliases FILE *out = NULL; FILE *outh = NULL; FILE *outph = NULL; +FILE *devnull = NULL; gboolean no_touch_headers = FALSE; gboolean for_cpp = FALSE; @@ -81,24 +82,25 @@ gboolean got_error = FALSE; gboolean always_private_header = FALSE; gboolean no_private_header = FALSE; gboolean no_extern_c = FALSE; +gboolean no_write = FALSE; static void make_bases(void) { - filebase = replace_sep(((Class *)class)->otype,'-'); + filebase = replace_sep(((Class *)class)->otype, '-'); g_strdown(filebase); - funcbase = replace_sep(((Class *)class)->otype,'_'); + funcbase = replace_sep(((Class *)class)->otype, '_'); g_strdown(funcbase); - pfuncbase = replace_sep(((Class *)class)->ptype,'_'); + pfuncbase = replace_sep(((Class *)class)->ptype, '_'); g_strdown(pfuncbase); - macrobase = replace_sep(((Class *)class)->otype,'_'); + macrobase = replace_sep(((Class *)class)->otype, '_'); g_strup(macrobase); - macrois = make_pre_macro(((Class *)class)->otype,"IS"); - macrotype = make_pre_macro(((Class *)class)->otype,"TYPE"); + macrois = make_pre_macro(((Class *)class)->otype, "IS"); + macrotype = make_pre_macro(((Class *)class)->otype, "TYPE"); typebase = remove_sep(((Class *)class)->otype); @@ -130,11 +132,11 @@ get_type(Type *t, gboolean postfix_to_stars) } g_string_append_c(gs,' '); - for(i=0;i<(t->stars+extra);i++) - g_string_append_c(gs,'*'); + for(i=0; i<(t->stars+extra); i++) + g_string_append_c(gs, '*'); s = gs->str; - g_string_free(gs,FALSE); + g_string_free(gs, FALSE); return s; } @@ -164,8 +166,8 @@ print_type(FILE *fp, Type *t, gboolean postfix_to_stars) { char *s; - s = get_type(t,postfix_to_stars); - out_printf(fp,"%s",s); + s = get_type(t, postfix_to_stars); + out_printf(fp, "%s", s); g_free(s); } @@ -179,37 +181,37 @@ print_method(FILE *fp, char *typeprefix, char *nameprefix, { GList *li; - out_printf(fp,"%s",typeprefix); - print_type(fp,m->mtype,TRUE); + out_printf(fp, "%s", typeprefix); + print_type(fp, m->mtype, TRUE); if(no_funcbase) - out_printf(fp,"%s%s%s%s(", - nameprefix,subnameprefix,m->id,namepostfix); + out_printf(fp, "%s%s%s%s(", + nameprefix, subnameprefix, m->id, namepostfix); else out_printf(fp,"%s%s_%s%s%s(", - nameprefix,funcbase,subnameprefix,m->id, + nameprefix, funcbase, subnameprefix, m->id, namepostfix); if(m->args) { - for(li=m->args;li;li=g_list_next(li)) { + for(li=m->args; li; li=g_list_next(li)) { FuncArg *arg = li->data; - print_type(fp,arg->atype,FALSE); + print_type(fp, arg->atype, FALSE); if(li->next) - out_printf(fp,"%s%s,%s",arg->name, + out_printf(fp, "%s%s,%s", arg->name, arg->atype->postfix? arg->atype->postfix:"", one_arg_per_line?"\n\t\t\t\t\t":" "); else - out_printf(fp,"%s%s",arg->name, + out_printf(fp, "%s%s", arg->name, arg->atype->postfix? arg->atype->postfix:""); } if(m->vararg) - out_printf(fp,",%s...", + out_printf(fp, ",%s...", one_arg_per_line?"\n\t\t\t\t\t":" "); } else { - out_printf(fp,"void"); + out_printf(fp, "void"); } - out_printf(fp,")%s",postfix); + out_printf(fp, ")%s", postfix); } @@ -247,7 +249,7 @@ make_method_nongnu_aliases(Class *c) { GList *li; - for(li=c->nodes;li;li=g_list_next(li)) { + for(li=c->nodes; li; li=g_list_next(li)) { Node *node = li->data; if(node->type == METHOD_NODE) { Method *m = (Method *)node; @@ -258,11 +260,12 @@ make_method_nongnu_aliases(Class *c) continue; /* in C++ mode don't alias new */ - if(for_cpp && strcmp(m->id,"new")==0) + if(for_cpp && strcmp(m->id, "new")==0) continue; - print_method(out,"static ","(* ","",") ","",m,FALSE,TRUE); - out_printf(out," = %s_%s;\n",funcbase,m->id); + print_method(out, "static ", "(* ", "", ") ", "", + m, FALSE, TRUE); + out_printf(out, " = %s_%s;\n", funcbase, m->id); made_aliases = TRUE; } @@ -465,18 +468,18 @@ make_inits(Class *cl) } if(!got_class_init) { node = new_method(NO_SCOPE, CLASS_INIT_METHOD, - (Type *)new_type(0,g_strdup("void"),NULL), - NULL,NULL,g_strdup("class_init"), - make_func_arg(cl->otype,TRUE,g_strdup("c")), - NULL, NULL,0,0,FALSE); + (Type *)new_type(0, g_strdup("void"),NULL), + NULL, NULL, NULL, g_strdup("class_init"), + make_func_arg(cl->otype, TRUE, g_strdup("c")), + NULL, NULL, 0, 0, FALSE); cl->nodes = g_list_prepend(cl->nodes,node); } if(!got_init) { node = new_method(NO_SCOPE, INIT_METHOD, - (Type *)new_type(0,g_strdup("void"),NULL), - NULL,NULL,g_strdup("init"), - make_func_arg(cl->otype,FALSE,g_strdup("o")), - NULL, NULL,0,0,FALSE); + (Type *)new_type(0, g_strdup("void"),NULL), + NULL, NULL, NULL, g_strdup("init"), + make_func_arg(cl->otype, FALSE, g_strdup("o")), + NULL, NULL, 0, 0, FALSE); cl->nodes = g_list_prepend(cl->nodes,node); } } @@ -492,16 +495,14 @@ make_finalize(Class *cl) if(n->type == METHOD_NODE) { Method *m = (Method *)n; if(m->method == OVERRIDE_METHOD && - strcmp(m->id,"finalize")==0) { - if(strcmp(m->otype,"Gtk:Object")==0) { + strcmp(m->id, "finalize")==0) { + if(strcmp(m->otype, "Gtk:Object")==0) { got_finalize = TRUE; break; - } else { - print_error(FALSE,"finalize method override " - "of class other then Gtk:Object", - m->line_no); } - + print_error(FALSE,"finalize method override " + "of class other then Gtk:Object", + m->line_no); } } } @@ -509,7 +510,7 @@ make_finalize(Class *cl) node = new_method(NO_SCOPE, OVERRIDE_METHOD, (Type *)new_type(0,g_strdup("void"),NULL), g_strdup("Gtk:Object"), - NULL,g_strdup("finalize"), + NULL, NULL, g_strdup("finalize"), make_func_arg("Gtk:Object",FALSE,g_strdup("o")), NULL, g_strdup("PARENT_HANDLER (o);\n"), @@ -666,18 +667,18 @@ add_enums(Class *c) out_printf(out,"\tLAST_SIGNAL\n};\n\n"); } if(arguments>0) { - out_printf(out,"enum {\n\tARG_0,\n"); + out_printf(out,"enum {\n\tARG_0"); for(li=c->nodes;li;li=g_list_next(li)) { Node *n = li->data; if(n->type == ARGUMENT_NODE) { Argument *a = (Argument *)n; char *s = g_strdup(a->name); g_strup(s); - out_printf(out,"\tARG_%s,\n",s); + out_printf(out,",\n\tARG_%s",s); g_free(s); } } - out_printf(out, "};\n\n"); + out_printf(out, "\n};\n\n"); } if(signals>0) @@ -753,6 +754,58 @@ add_overrides(Class *c, char *oname, gboolean did_gtk_obj) g_hash_table_destroy(done); } +static char * +make_run_signal_flags(Method *m, gboolean last) +{ + GList *li; + GString *gs; + char *flags[] = { + "FIRST", + "LAST", + "BOTH", + "NO_RECURSE", + "ACTION", + "NO_HOOKS", + NULL + }; + + gs = g_string_new(NULL); + + if(last) + g_string_assign(gs, "GTK_RUN_LAST"); + else + g_string_assign(gs, "GTK_RUN_FIRST"); + + if(m->scope == PUBLIC_SCOPE) + g_string_append(gs, " | GTK_RUN_ACTION"); + + for(li = m->flags; li; li = li->next) { + char *flag = li->data; + int i; + for(i=0;flags[i];i++) { + if(strcmp(flags[i],flag)==0) + break; + } + /* if we haven't found it in our list */ + if(!flags[i]) { + char *s; + s = g_strdup_printf("Unknown flag '%s' used, " + "perhaps it was misspelled", + flag); + print_error(TRUE, s, m->line_no); + g_free(s); + } + g_string_sprintfa(gs, " | GTK_RUN_%s",flag); + } + + { + char *ret = gs->str; + g_string_free(gs, FALSE); + return ret; + } +} + + static void add_signals(Class *c) { @@ -761,46 +814,46 @@ add_signals(Class *c) out_printf(out,"\n"); for(li=c->nodes;li;li=g_list_next(li)) { Node *n = li->data; - char *mar; - char *sig; - int is_none; - int last = FALSE; + char *mar, *sig, *flags; + gboolean is_none, last = FALSE; Method *m = (Method *)n; + if(n->type != METHOD_NODE || (m->method != SIGNAL_FIRST_METHOD && m->method != SIGNAL_LAST_METHOD)) continue; - if(m->method == SIGNAL_FIRST_METHOD) last = FALSE; else last = TRUE; - if(g_hash_table_lookup(marsh,m)) + if(g_hash_table_lookup(marsh, m)) mar = g_strconcat("___marshal_", (char *)g_hash_table_lookup(marsh,m), NULL); else mar = g_strdup("gtk_signal_default_marshaller"); - is_none = (strcmp(m->gtktypes->next->data,"NONE")==0); + is_none = (strcmp(m->gtktypes->next->data, "NONE")==0); sig = g_strdup(m->id); g_strup(sig); + flags = make_run_signal_flags(m, last); out_printf(out,"\tobject_signals[%s_SIGNAL] =\n" "\t\tgtk_signal_new (\"%s\",\n" - "\t\t\tGTK_RUN_%s,\n" + "\t\t\%s,\n" "\t\t\tgtk_object_class->type,\n" "\t\t\tGTK_SIGNAL_OFFSET (%sClass, %s),\n" "\t\t\t%s,\n" "\t\t\tGTK_TYPE_%s, %d", sig,m->id, - last?"LAST":"FIRST", + flags, typebase,m->id,mar,(char *)m->gtktypes->data, is_none?0:g_list_length(m->gtktypes->next)); g_free(mar); g_free(sig); + g_free(flags); if(!is_none) { GList *l; @@ -1382,19 +1435,30 @@ open_files(void) outfileph = NULL; - out = fopen(outfile,"w"); - if(!out) { - g_error("Cannot open outfile: %s",outfile); - } - outh = fopen(outfileh,"w"); - if(!outh) { - g_error("Cannot open outfile: %s",outfileh); - } - if(outfileph) { - outph = fopen(outfileph,"w"); - if(!outph) { + if(no_write) { + devnull = fopen("/dev/null","w"); + if(!devnull) { + g_error("Cannot open null device",NULL); + } + out = devnull; + outh = devnull; + if(outfileph) + outph = devnull; + } else { + out = fopen(outfile,"w"); + if(!out) { + g_error("Cannot open outfile: %s",outfile); + } + outh = fopen(outfileh,"w"); + if(!outh) { g_error("Cannot open outfile: %s",outfileh); } + if(outfileph) { + outph = fopen(outfileph,"w"); + if(!outph) { + g_error("Cannot open outfile: %s",outfileh); + } + } } } @@ -1501,6 +1565,22 @@ print_ccode_block(CCode *cc) fp = outh; out_printf(fp,"\n"); break; + case AT_CCODE: + /* AT code is printed exactly like normal 'all' + code but is printed before */ + case A_CCODE: + if(outph) { + out_printf(outph,"\n"); + out_printf(outph,"%s\n",cc->cbuf); + out_addline_infile(outph,cc->line_no); + out_addline_outfile(outph); + } + out_printf(outh,"\n"); + out_printf(outh,"%s\n",cc->cbuf); + fp = out; + out_printf(fp,"\n"); + out_addline_infile(fp,cc->line_no); + break; default: case C_CCODE: fp = out; @@ -1518,6 +1598,8 @@ print_ccode_block(CCode *cc) } out_printf(fp,"%s\n",cc->cbuf); if(cc->cctype == C_CCODE || + cc->cctype == A_CCODE || + cc->cctype == AT_CCODE || cc->cctype == PH_CCODE) out_addline_outfile(fp); } @@ -1527,6 +1609,7 @@ print_class_block(Class *c) { GList *l; char *s; + gboolean printed_private = FALSE; out_printf(out,"/* utility types we may need */\n"); out_printf(out,"typedef struct { " @@ -1553,6 +1636,11 @@ print_class_block(Class *c) "GTK_CHECK_TYPE((obj), %s_get_type ())\n\n", macrois,funcbase); + out_printf(out, "\n/* self casting macros */\n"); + out_printf(out, "#define SELF(x) %s(x)\n", macrobase); + out_printf(out, "#define IS_SELF(x) %s(x)\n", macrois); + out_printf(out, "#define SELF_CLASS(x) %s_CLASS(x)\n\n", macrobase); + if(privates>0) { out_printf(outh, "\n/* Private structure type */\n"); out_printf(outh,"typedef struct _%sPrivate %sPrivate;\n", @@ -1572,22 +1660,36 @@ print_class_block(Class *c) out_printf(outh,"struct _%s {\n\t%s __parent__;\n", typebase,ptypebase); for(l=c->nodes;l;l=g_list_next(l)) { + static gboolean printed_public = FALSE; Node *n = l->data; Variable *v = (Variable *)n; if(n->type == VARIABLE_NODE && - v->scope == PUBLIC_SCOPE) + v->scope == PUBLIC_SCOPE) { + if(!printed_public) { + out_printf(outh,"\t/*< public >*/\n"); + printed_public = TRUE; + } put_variable((Variable *)n,outh); + } } /* put protecteds always AFTER publics */ for(l=c->nodes;l;l=g_list_next(l)) { Node *n = l->data; Variable *v = (Variable *)n; if(n->type == VARIABLE_NODE && - v->scope == PROTECTED_SCOPE) + v->scope == PROTECTED_SCOPE) { + if(!printed_private) { + out_printf(outh,"\t/*< private >*/\n"); + printed_private = TRUE; + } put_variable((Variable *)n,outh); + } } - if(privates>0) + if(privates>0) { + if(!printed_private) + out_printf(outh,"\t/*< private >*/\n"); out_printf(outh,"\t%sPrivate *_priv;\n",typebase); + } out_printf(outh,"};\n"); if(privates>0) { @@ -1740,9 +1842,9 @@ print_version_macros(void) int major=0,minor=0,pl=0; sscanf(VERSION,"%d.%d.%d",&major,&minor,&pl); - out_printf(out,"#define GOB_VERSION_MAJOR %d\n", major); - out_printf(out,"#define GOB_VERSION_MINOR %d\n", minor); - out_printf(out,"#define GOB_VERSION_PATCHLEVEL %d\n\n", pl); + out_printf(out, "#define GOB_VERSION_MAJOR %d\n", major); + out_printf(out, "#define GOB_VERSION_MINOR %d\n", minor); + out_printf(out, "#define GOB_VERSION_PATCHLEVEL %d\n\n", pl); } static void @@ -1836,6 +1938,22 @@ print_header_postfixes(void) } } +static void +print_all_top(void) +{ + GList *li; + + /* print the AT_CCODE blocks */ + for(li=nodes;li;li=g_list_next(li)) { + Node *node = li->data; + if(node->type == CCODE_NODE) { + CCode *cc = (CCode *)node; + if(cc->cctype==AT_CCODE) + print_ccode_block((CCode *)node); + } + } +} + static void print_header_top(void) { @@ -1862,12 +1980,14 @@ generate_outfiles(void) print_file_comments(); + print_all_top(); + print_header_top(); print_header_prefixes(); print_version_macros(); - + print_includes(); for(li=nodes;li;li=g_list_next(li)) { @@ -1906,7 +2026,9 @@ print_help(void) "\t--no-private-header Don't create a private header, " "put private\n" "\t structure and protected " - "prototypes inside c file\n"); + "prototypes inside c file\n" + "\t--no-write,-n Don't write output files, just " + "check syntax\n"); } static void @@ -1922,7 +2044,7 @@ parse_options(int argc, char *argv[]) if(no_opts || argv[i][0]!='-') { /*must be a file*/ if(got_file) { - fprintf(stderr,"Specify only one file!\n"); + fprintf(stderr, "Specify only one file!\n"); print_help(); exit(1); } @@ -1932,50 +2054,55 @@ parse_options(int argc, char *argv[]) print_help(); exit(0); } else if(strcmp(argv[i],"--version")==0) { - fprintf(stderr,"Gob version %s\n",VERSION); + fprintf(stderr, "Gob version %s\n", VERSION); exit(0); - } else if(strcmp(argv[i],"--exit-on-warn")==0) { + } else if(strcmp(argv[i], "--exit-on-warn")==0) { exit_on_warn = TRUE; - } else if(strcmp(argv[i],"--no-exit-on-warn")==0) { + } else if(strcmp(argv[i], "--no-exit-on-warn")==0) { exit_on_warn = FALSE; - } else if(strcmp(argv[i],"--for-cpp")==0) { + } else if(strcmp(argv[i], "--for-cpp")==0) { for_cpp = TRUE; - } else if(strcmp(argv[i],"--no-touch-headers")==0) { + } else if(strcmp(argv[i], "--no-touch-headers")==0) { no_touch_headers = TRUE; - } else if(strcmp(argv[i],"--always-private-header")==0) { + } else if(strcmp(argv[i], "--always-private-header")==0) { no_private_header = FALSE; always_private_header = TRUE; - } else if(strcmp(argv[i],"--no-private-header")==0) { + } else if(strcmp(argv[i], "--no-private-header")==0) { always_private_header = FALSE; no_private_header = TRUE; - } else if(strcmp(argv[i],"--no-gnu")==0) { + } else if(strcmp(argv[i], "--no-gnu")==0) { no_gnu = TRUE; - } else if(strcmp(argv[i],"--no-extern-c")==0) { + } else if(strcmp(argv[i], "--no-extern-c")==0) { no_extern_c = TRUE; - } else if(strcmp(argv[i],"--")==0) { + } else if(strcmp(argv[i], "--no-write")==0) { + no_write = TRUE; + } else if(strcmp(argv[i], "--")==0) { /*further arguments are files*/ no_opts = TRUE; - } else if(strncmp(argv[i],"--",2)==0) { + } else if(strncmp(argv[i], "--",2)==0) { /*unknown long option*/ - fprintf(stderr,"Unknown option '%s'!\n",argv[i]); + fprintf(stderr, "Unknown option '%s'!\n", argv[i]); print_help(); exit(1); } else { /*by now we know we have a string starting with - which is a short option string*/ char *p = argv[i]+1; - for(p=argv[i]+1;*p;p++) { + for(p=argv[i]+1; *p; p++) { switch(*p) { case 'w': exit_on_warn=TRUE; break; + case 'n': + no_write = TRUE; + break; case 'h': case '?': print_help(); exit(0); default: fprintf(stderr, - "Unknown option '%c'!\n",*p); + "Unknown option '%c'!\n", *p); print_help(); exit(1); } @@ -1988,15 +2115,16 @@ parse_options(int argc, char *argv[]) static void compare_and_move_header(void) { - char *hfnew = g_strconcat("#gob#",filebase,".h#gob#",NULL); - char *hf = g_strconcat(filebase,".h",NULL); + char *hfnew = g_strconcat("#gob#", filebase, ".h#gob#", NULL); + char *hf = g_strconcat(filebase, ".h", NULL); struct stat s; if(stat(hf,&s)==0) { char *s; - s = g_strdup_printf("cmp '%s' '%s' > /dev/null",hf,hfnew); + s = g_strdup_printf("cmp '%s' '%s' > /dev/null", hf, hfnew); if(system(s)==0) { if(unlink(hfnew)!=0) - print_error(FALSE,"Can't remove new header file",0); + print_error(FALSE, + "Can't remove new header file", 0); g_free(hfnew); g_free(hf); g_free(s); @@ -2004,10 +2132,10 @@ compare_and_move_header(void) } g_free(s); if(unlink(hf)!=0) - print_error(FALSE,"Can't remove old header file",0); + print_error(FALSE, "Can't remove old header file", 0); } if(rename(hfnew,hf)!=0) - print_error(FALSE,"Can't rename new header file",0); + print_error(FALSE, "Can't rename new header file", 0); g_free(hfnew); g_free(hf); } @@ -2015,12 +2143,12 @@ compare_and_move_header(void) int main(int argc, char *argv[]) { - parse_options(argc,argv); + parse_options(argc, argv); if(filename) { - yyin = fopen(filename,"r"); + yyin = fopen(filename, "r"); if(!yyin) { - fprintf(stderr,"Error: can't open file '%s'\n", + fprintf(stderr, "Error: can't open file '%s'\n", filename); exit(1); } @@ -2031,7 +2159,7 @@ main(int argc, char *argv[]) if(yyparse()!=0) g_error("Parsing errors, quitting"); if(!class) - print_error(FALSE," no class defined",0); + print_error(FALSE, " no class defined", 0); exit_on_error = FALSE; @@ -2065,10 +2193,16 @@ main(int argc, char *argv[]) generate_outfiles(); - fclose(out); - fclose(outh); + if(devnull) + fclose(devnull); + else { + fclose(out); + fclose(outh); + if(outph) + fclose(outph); + } - if(no_touch_headers) + if(no_touch_headers && !no_write) compare_and_move_header(); return 0;