X-Git-Url: http://git.draconx.ca/gitweb/gob-dx.git/blobdiff_plain/1656cfade4879af0cb003e3847e58264caa5781f..3b10bbd3a88d6e16146414d91d06bb2f36347bfc:/src/main.c diff --git a/src/main.c b/src/main.c index d5d299e..226e821 100644 --- a/src/main.c +++ b/src/main.c @@ -63,6 +63,8 @@ static int arguments = 0; /* number of named arguments */ static int overrides = 0; /* number of override methods */ static int privates = 0; /* number of private data members */ static int protecteds = 0; /* number of protected methods */ +static int destructors = 0; /* number of variable destructors */ +static int initializers = 0; /* number of variable initializers */ static gboolean made_aliases = FALSE; /* if we made any shorthand aliases and need the REALLY UGLY HACK to @@ -71,6 +73,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 +84,26 @@ gboolean got_error = FALSE; gboolean always_private_header = FALSE; gboolean no_private_header = FALSE; gboolean no_extern_c = FALSE; +gboolean no_write = FALSE; +gboolean no_lines = 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 +135,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 +169,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 +184,58 @@ 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); +} + +static gboolean +any_method_to_alias(Class *c) +{ + GList *li; + + for(li=c->nodes;li;li=g_list_next(li)) { + Node *node = li->data; + if(node->type == METHOD_NODE) { + Method *m = (Method *)node; + + if(m->method == INIT_METHOD || + m->method == CLASS_INIT_METHOD || + m->method == OVERRIDE_METHOD) + continue; + + return TRUE; + } + } + return FALSE; } @@ -247,7 +273,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 +284,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 +492,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 +519,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 +534,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,24 +691,25 @@ 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) out_printf(out, "static guint object_signals[LAST_SIGNAL] = {0};\n\n"); + out_printf(out, "/* pointer to the class of our parent */\n"); out_printf(out, "static %sClass *parent_class = NULL;\n\n",ptypebase); } @@ -753,6 +779,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 +839,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\t(GtkSignalRunType)(%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; @@ -917,9 +995,9 @@ make_arguments(Class *c) "\t\tGTK_TYPE_%s,\n" "\t\t%s,\n" "\t\tARG_%s);\n", - typebase,a->name,a->gtktype,flags->str,s); + typebase, a->name, a->gtktype, flags->str, s); g_free(s); - g_string_free(flags,TRUE); + g_string_free(flags, TRUE); } out_printf(out, @@ -927,6 +1005,32 @@ make_arguments(Class *c) "\tgtk_object_class->get_arg = ___object_get_arg;\n"); } +static void +print_initializer(Method *m, Variable *v) +{ + char *root; + + if(v->initializer == NULL) + return; + + if(v->scope == PRIVATE_SCOPE) + root = g_strconcat(((FuncArg *)m->args->data)->name, + "->_priv", NULL); + else + root = g_strdup(((FuncArg *)m->args->data)->name); + + if(v->initializer_line > 0) + out_addline_infile(out, v->initializer_line); + + out_printf(out, "\t%s->%s = %s;\n", + root, v->id, v->initializer); + + if(v->initializer_line > 0) + out_addline_outfile(out); + + g_free(root); +} + static void add_inits(Class *c) { @@ -938,25 +1042,36 @@ add_inits(Class *c) continue; m = (Method *)n; if(m->method == INIT_METHOD) { - if(m->line_no>0) + if(m->line_no > 0) out_addline_infile(out,m->line_no); print_method(out, "static ", "\n", "", " ", "\n", m, FALSE, FALSE); - if(m->line_no>0) + if(m->line_no > 0) out_addline_outfile(out); out_printf(out,"{\n"); - if(privates>0) { + if(privates > 0) { out_printf(out,"\t%s->_priv = " - "g_new0 (%sPrivate,1);\n", + "g_new0 (%sPrivate, 1);\n", ((FuncArg *)m->args->data)->name, typebase); } + if(initializers > 0) { + GList *li; + for(li = ((Class *)class)->nodes; + li != NULL; + li = li->next) { + Node *n = li->data; + if(n->type != VARIABLE_NODE) + continue; + print_initializer(m, (Variable *)n); + } + } } else if(m->method == CLASS_INIT_METHOD) { - if(m->line_no>0) + if(m->line_no > 0) out_addline_infile(out, m->line_no); print_method(out, "static ", "\n", "", " ", "\n", m, FALSE, FALSE); - if(m->line_no>0) + if(m->line_no > 0) out_addline_outfile(out); out_printf(out,"{\n"); if(signals>0 || @@ -1003,7 +1118,7 @@ add_inits(Class *c) } static void -add_getset_arg(Class *c, int is_set) +add_getset_arg(Class *c, gboolean is_set) { GList *li; out_printf(out,"\nstatic void\n" @@ -1036,14 +1151,39 @@ add_getset_arg(Class *c, int is_set) continue; s = g_strdup(a->name); g_strup(s); - out_printf(out,"\tcase ARG_%s:\n" - "#define ARG (GTK_VALUE_%s(*arg))\n" - "\t\t{\n", - s,a->gtktype); + if(is_set && a->atype) { + char *cast = get_type(a->atype, TRUE); + out_printf(out, "\tcase ARG_%s:\n", s); + if(no_gnu || for_cpp) { + out_printf(out, "#define ARG " + "((%s)GTK_VALUE_%s(*arg))\n", + cast, a->gtktype); + } else { + out_printf(out, "#ifdef __GNUC__\n"); + out_printf(out, "#define ARG " + "({%s foo = GTK_VALUE_%s(*arg); " + "foo; })\n", + cast, a->gtktype); + out_printf(out,"#else /* __GNUC__ */\n"); + out_printf(out, "#define ARG " + "((%s)GTK_VALUE_%s(*arg))\n", + cast, a->gtktype); + out_printf(out,"#endif /* __GNUC__ */\n\n"); + } + out_printf(out, "\t\t{\n"); + g_free(cast); + } else { + out_printf(out, "\tcase ARG_%s:\n" + "#define ARG (GTK_VALUE_%s(*arg))\n" + "\t\t{\n", + s, a->gtktype); + } g_free(s); - out_addline_infile(out,line_no); + if(line_no > 0) + out_addline_infile(out, line_no); out_printf(out,"%s\n",cbuf); - out_addline_outfile(out); + if(line_no > 0) + out_addline_outfile(out); out_printf(out,"\t\t}\n\t\tbreak;\n" "#undef ARG\n"); } @@ -1063,7 +1203,7 @@ print_checks(Method *m, FuncArg *fa) Check *ch = li->data; char *s; /* point to the method prot in .gob for failed checks */ - if(m->line_no>0) + if(m->line_no > 0) out_addline_infile(out,m->line_no); if(is_void) out_printf(out,"\tg_return_if_fail ("); @@ -1104,11 +1244,11 @@ print_checks(Method *m, FuncArg *fa) break; } if(is_void) - out_printf(out,");\n"); + out_printf(out, ");\n"); else { - out_printf(out,", ("); - print_type(out,m->mtype,TRUE); - out_printf(out,")%s);\n", + out_printf(out, ", ("); + print_type(out, m->mtype, TRUE); + out_printf(out, ")%s);\n", m->onerror?m->onerror:"0"); } } @@ -1128,20 +1268,76 @@ print_preconditions(Method *m) out_addline_outfile(out); } +static void +print_destructor(char *self_id, Variable *v) +{ + char *root; + + if(v->destructor == NULL) + return; + + if(v->scope == PRIVATE_SCOPE) + root = g_strconcat(self_id, "->_priv", NULL); + else + root = g_strdup(self_id); + + if(v->destructor_simple) { + if(v->destructor_line > 0) + out_addline_infile(out, v->destructor_line); + + out_printf(out, "\tif(%s->%s) " + "((*(void (*)(void *))%s)) (%s->%s);\n", + root, v->id, v->destructor, root, v->id); + + if(v->destructor_line > 0) + out_addline_outfile(out); + } else { + out_printf(out, "#define VAR (%s->%s)\n", root, v->id); + out_printf(out, "\t{\n\t%s *self G_GNUC_UNUSED = %s;\n", + typebase, self_id); + if(v->destructor_line > 0) + out_addline_infile(out, v->destructor_line); + + out_printf(out, "\t%s}\n", v->destructor); + + if(v->destructor_line > 0) + out_addline_outfile(out); + out_printf(out, "#undef VAR\n"); + } + + g_free(root); +} + /* put in code if it's needed */ static void put_in_gen_code(Method *m) { - /* now we only have the freeing of the private structure */ - if(privates>0 && - m->method == OVERRIDE_METHOD && - strcmp(m->id,"finalize")==0) { - out_printf(out,"\tg_free (%s (%s)->_priv);\n" - "\t%s (%s)->_priv = NULL;\n", - macrobase, - ((FuncArg *)m->args->data)->name, - macrobase, - ((FuncArg *)m->args->data)->name); + if(m->method == OVERRIDE_METHOD && + strcmp(m->id, "finalize")==0) { + if(privates > 0 || destructors > 0) { + out_printf(out,"\t%s *___self = %s (%s);\n", + typebase, macrobase, + ((FuncArg *)m->args->data)->name); + } + if(destructors > 0) { + GList *li; + for(li = ((Class *)class)->nodes; + li != NULL; + li = li->next) { + Node *n = li->data; + if(n->type == VARIABLE_NODE) + print_destructor("___self", + (Variable *)n); + } + } + if(privates > 0) { + out_printf(out,"\tg_free (___self->_priv);\n" + "\t___self->_priv = NULL;\n", + macrobase, + ((FuncArg *)m->args->data)->name, + macrobase, + ((FuncArg *)m->args->data)->name); + } } } @@ -1160,13 +1356,13 @@ print_method_body(Method *m, int pre) ugly in the .c file, but that is not supposed to be human readable anyway. */ if(m->cbuf) { - out_printf(out,"{\n"); + out_printf(out, "{\n"); if(m->ccode_line>0) - out_addline_infile(out,m->ccode_line); - out_printf(out,"\t%s}",m->cbuf); + out_addline_infile(out, m->ccode_line); + out_printf(out, "\t%s}", m->cbuf); } - out_printf(out,"}\n"); + out_printf(out, "}\n"); if(m->cbuf) out_addline_outfile(out); @@ -1196,7 +1392,7 @@ get_arg_names_for_macro(Method *m) { char *p; GList *li; - GString *gs = g_string_new(""); + GString *gs = g_string_new(NULL); p = ""; for(li=m->args;li;li=g_list_next(li)) { FuncArg *arg = li->data; @@ -1204,7 +1400,7 @@ get_arg_names_for_macro(Method *m) p = ","; } p = gs->str; - g_string_free(gs,FALSE); + g_string_free(gs, FALSE); return p; } @@ -1231,7 +1427,7 @@ put_method(Method *m) else /* PUBLIC, PROTECTED */ print_method(out, "", "\n", "", " ", "\n", m, FALSE, FALSE); - print_method_body(m,TRUE); + print_method_body(m, TRUE); break; case SIGNAL_FIRST_METHOD: case SIGNAL_LAST_METHOD: @@ -1382,19 +1578,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); + } + } } } @@ -1416,9 +1623,9 @@ put_argument_nongnu_wrappers(Class *c) s = g_strdup(a->name); g_strup(s); if(a->atype) - cast = get_type(a->atype,TRUE); + cast = get_type(a->atype, TRUE); else - cast = g_strdup(get_cast(a->gtktype,TRUE)); + cast = g_strdup(get_cast(a->gtktype, TRUE)); if(cast) { if(a->set) @@ -1501,6 +1708,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 +1741,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 +1752,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 +1779,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 +1803,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) { @@ -1702,18 +1947,22 @@ print_class_block(Class *c) add_get_type(); - if(no_gnu) - make_method_nongnu_aliases(c); - else { - out_printf(out,"\n#ifdef __GNUC__\n"); - make_method_gnu_aliases(c); - out_printf(out,"#else /* __GNUC__ */\n"); - make_method_nongnu_aliases(c); - out_printf(out,"#endif /* __GNUC__ */\n\n"); + if(any_method_to_alias(c)) { + if(no_gnu) + make_method_nongnu_aliases(c); + else { + out_printf(out,"\n#ifdef __GNUC__\n"); + make_method_gnu_aliases(c); + out_printf(out,"#else /* __GNUC__ */\n"); + make_method_nongnu_aliases(c); + out_printf(out,"#endif /* __GNUC__ */\n\n"); + } } - out_printf(out,"#define GET_NEW (gtk_type_new(%s_get_type()))\n", - funcbase); + out_printf(out,"/* a macro for creating a new object of our type */\n"); + out_printf(out, + "#define GET_NEW ((%s *)gtk_type_new(%s_get_type()))\n\n", + typebase, funcbase); add_inits(c); @@ -1729,8 +1978,6 @@ print_class_block(Class *c) } } - out_printf(out,"#undef GET_NEW\n"); - add_bad_hack_to_avoid_unused_warnings(c); } @@ -1740,9 +1987,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 @@ -1750,13 +1997,14 @@ print_file_comments(void) { time_t curtime; time(&curtime); - out_printf(outh,"/* Generated by GOB (v%s)" - " (do not edit directly) */\n\n",VERSION); + out_printf(outh, "/* Generated by GOB (v%s)" + " (do not edit directly) */\n\n", VERSION); if(outph) out_printf(outph,"/* Generated by GOB (v%s)" - " (do not edit directly) */\n\n",VERSION); + " (do not edit directly) */\n\n", VERSION); out_printf(out,"/* Generated by GOB (v%s) on %s" - " (do not edit directly) */\n\n",VERSION,ctime(&curtime)); + " (do not edit directly) */\n\n", + VERSION, ctime(&curtime)); } static void @@ -1765,17 +2013,17 @@ print_includes(void) gboolean found_header; char *p; - p = g_strconcat(filebase,".h",NULL); + p = g_strconcat(filebase, ".h", NULL); found_header = TRUE; - if(!g_list_find_custom(include_files,p,(GCompareFunc)strcmp)) { - out_printf(out,"#include \"%s.h\"\n\n",filebase); + if(!g_list_find_custom(include_files, p, (GCompareFunc)strcmp)) { + out_printf(out, "#include \"%s.h\"\n\n", filebase); found_header = FALSE; } g_free(p); /* if we are creating a private header see if it was included */ if(outph) { - p = g_strconcat(filebase,"-private.h",NULL); + p = g_strconcat(filebase, "-private.h", NULL); if(!g_list_find_custom(include_files,p,(GCompareFunc)strcmp)) { out_printf(out,"#include \"%s-private.h\"\n\n", filebase); @@ -1799,21 +2047,21 @@ print_header_prefixes(void) { char *p; - p = replace_sep(((Class *)class)->otype,'_'); + p = replace_sep(((Class *)class)->otype, '_'); g_strup(p); - out_printf(outh,"#ifndef __%s_H__\n#define __%s_H__\n\n",p,p); + out_printf(outh, "#ifndef __%s_H__\n#define __%s_H__\n\n", p, p); if(outph) - out_printf(outph,"#ifndef __%s_PRIVATE_H__\n" + out_printf(outph, "#ifndef __%s_PRIVATE_H__\n" "#define __%s_PRIVATE_H__\n\n" - "#include \"%s.h\"\n\n",p,p,filebase); + "#include \"%s.h\"\n\n", p, p, filebase); g_free(p); if(!no_extern_c) { - out_printf(outh,"#ifdef __cplusplus\n" + out_printf(outh, "#ifdef __cplusplus\n" "extern \"C\" {\n" "#endif /* __cplusplus */\n\n"); if(outph) - out_printf(outph,"#ifdef __cplusplus\n" + out_printf(outph, "#ifdef __cplusplus\n" "extern \"C\" {\n" "#endif /* __cplusplus */\n\n"); } @@ -1836,6 +2084,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 +2126,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 +2172,10 @@ 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" + "\t--no-lines Don't print '#line' to output\n"); } static void @@ -1922,7 +2191,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 +2201,57 @@ 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], "--no-lines")==0) { + no_lines = 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 +2264,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 +2281,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 +2292,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 +2308,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; @@ -2041,6 +2318,8 @@ main(int argc, char *argv[]) overrides = count_overrides((Class *)class); privates = count_privates((Class *)class); protecteds = count_protecteds((Class *)class); + destructors = count_destructors((Class *)class); + initializers = count_initializers((Class *)class); make_bases(); make_inits((Class *)class); @@ -2065,10 +2344,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;