ref: 62a1e11f24271590c45158432b60fcceefab7c4f
parent: 6f20a44d78daf844adf60d34e07a4a314556e7db
author: Ori Bernstein <[email protected]>
date: Mon Jul 14 18:41:42 EDT 2014
Indent trace information.
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -11,13 +11,6 @@
#include "parse.h"
-static void indent(FILE *fd, int depth)
-{
- int i;
- for (i = 0; i < depth; i++)
- fprintf(fd, " ");
-}
-
/* outputs a fully qualified name */
static void outname(Node *n, FILE *fd)
{
@@ -33,11 +26,10 @@
{
char buf[1024];
- indent(fd, depth);
if (s->decl.isconst)
- fprintf(fd, "const ");
+ findentf(fd, depth, "const ");
else
- fprintf(fd, "var ");
+ findentf(fd, depth, "var ");
outname(s->decl.name, fd);
fprintf(fd, " : %s\n", tyfmt(buf, 1024, s->decl.type));
}
@@ -63,8 +55,7 @@
char *ty;
Type *t;
- indent(fd, depth);
- fprintf(fd, "Stab %p (super = %p, name=\"%s\")\n", st, st->super, namestr(st->name));
+ findentf(fd, depth, "Stab %p (super = %p, name=\"%s\")\n", st, st->super, namestr(st->name));
if (!st)
return;
@@ -71,8 +62,7 @@
/* print types */
k = htkeys(st->ty, &n);
for (i = 0; i < n; i++) {
- indent(fd, depth + 1);
- fprintf(fd, "T ");
+ findentf(fd, depth, "T ");
/* already indented */
outname(k[i], fd);
t = gettype(st, k[i]);
@@ -85,8 +75,7 @@
/* dump declarations */
k = htkeys(st->dcl, &n);
for (i = 0; i < n; i++) {
- indent(fd, depth + 1);
- fprintf(fd, "S ");
+ findentf(fd, depth, "S ");
/* already indented */
outsym(getdcl(st, k[i]), fd, 0);
}
@@ -95,8 +84,7 @@
/* dump sub-namespaces */
k = htkeys(st->ns, &n);
for (i = 0; i < n; i++) {
- indent(fd, depth + 1);
- fprintf(fd, "N %s\n", (char*)k[i]);
+ findentf(fd, depth + 1, "N %s\n", (char*)k[i]);
outstab(getns_str(st, k[i]), fd, depth + 1);
}
free(k);
@@ -118,20 +106,17 @@
int tid;
char buf[1024];
- indent(fd, depth);
if (!n) {
- fprintf(fd, "Nil\n");
+ findentf(fd, depth, "Nil\n");
return;
}
- fprintf(fd, "%s", nodestr(n->type));
+ findentf(fd, depth, "%s", nodestr(n->type));
switch(n->type) {
case Nfile:
fprintf(fd, "(name = %s)\n", n->file.name);
- indent(fd, depth + 1);
- fprintf(fd, "Globls:\n");
+ findentf(fd, depth + 1, "Globls:\n");
outstab(n->file.globls, fd, depth + 2);
- indent(fd, depth + 1);
- fprintf(fd, "Exports:\n");
+ findentf(fd, depth + 1, "Exports:\n");
outstab(n->file.exports, fd, depth + 2);
for (i = 0; i < n->file.nuses; i++)
outnode(n->file.uses[i], fd, depth + 1);
@@ -219,8 +204,7 @@
fprintf(fd, " (args =\n");
for (i = 0; i < n->func.nargs; i++)
outnode(n->func.args[i], fd, depth+1);
- indent(fd, depth);
- fprintf(fd, ")\n");
+ findentf(fd, depth, ")\n");
outstab(n->func.scope, fd, depth + 1);
outnode(n->func.body, fd, depth+1);
break;
@@ -233,7 +217,7 @@
break;
case Nimpl:
fprintf(fd, "(name = %s, type = %s)\n", namestr(n->impl.traitname), tyfmt(buf, sizeof buf, n->impl.type));
- indent(fd, depth);
+ findentf(fd, depth, "");
outnode(n->impl.traitname, fd, depth + 1);
for (i = 0; i < n->impl.ndecls; i++)
outnode(n->impl.decls[i], fd, depth+1);
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -17,6 +17,7 @@
struct Inferstate {
int ingeneric;
int sawret;
+ int indentdepth;
Type *ret;
/* bound by patterns turn into decls in the action block */
@@ -226,22 +227,26 @@
static Type *tyfreshen(Inferstate *st, Type *t)
{
Htab *ht;
+ char *from, *to;
if (!needfreshen(st, t)) {
if (debugopt['u'])
- printf("%s isn't generic: skipping freshen\n", tystr(t));
+ indentf(st->indentdepth, "%s isn't generic: skipping freshen\n", tystr(t));
return t;
}
- if (debugopt['u'])
- printf("Freshen %s => ", tystr(t));
+ from = tystr(t);
tybind(st, t);
ht = mkht(tyhash, tyeq);
t = tyspecialize(t, ht);
htfree(ht);
tyunbind(st, t);
- if (debugopt['u'])
- printf("%s\n", tystr(t));
+ if (debugopt['u']) {
+ to = tystr(t);
+ indentf(st->indentdepth, "Freshen %s => %s\n", from, to);
+ free(from);
+ free(to);
+ }
return t;
}
@@ -443,7 +448,7 @@
if (debugopt['u']) {
s = tystr(t);
- printf("Bind %s\n", s);
+ indentf(st->indentdepth, "Bind %s\n", s);
free(s);
}
if (hthas(bt, t->pname))
@@ -465,7 +470,7 @@
return;
if (debugopt['u']) {
s = tystr(t);
- printf("Binding %s\n", s);
+ indentf(st->indentdepth, "Binding %s\n", s);
free(s);
}
bt = mkht(strhash, streq);
@@ -662,7 +667,7 @@
if (debugopt['u']) {
from = tystr(a);
to = tystr(b);
- printf("Unify %s => %s\n", from, to);
+ indentf(st->indentdepth, "Unify %s => %s\n", from, to);
free(from);
free(to);
}
@@ -751,7 +756,7 @@
if (debugopt['u']) {
ret = tystr(ft->sub[0]);
ctx = ctxstr(st, n->expr.args[0]);
- printf("Call of %s returns %s\n", ctx, ret);
+ indentf(st->indentdepth, "Call of %s returns %s\n", ctx, ret);
free(ctx);
free(ret);
}
@@ -1506,11 +1511,17 @@
popstab();
break;
case Ndecl:
+ if (debugopt['u'])
+ indentf(st->indentdepth, "--- infer %s ---\n", declname(n));
+ st->indentdepth++;
bind(st, n);
inferdecl(st, n);
if (type(st, n)->type == Typaram && !st->ingeneric)
fatal(n->line, "Generic type %s in non-generic near %s\n", tystr(type(st, n)), ctxstr(st, n));
unbind(st, n);
+ st->indentdepth--;
+ if (debugopt['u'])
+ indentf(st->indentdepth, "--- done ---\n");
break;
case Nblock:
setsuper(n->block.scope, curstab());
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -547,6 +547,11 @@
/* suffix replacement */
char *swapsuffix(char *buf, size_t sz, char *s, char *suf, char *swap);
+/* indented printf */
+void indentf(int depth, char *fmt, ...);
+void findentf(FILE *fd, int depth, char *fmt, ...);
+void vfindentf(FILE *fd, int depth, char *fmt, va_list ap);
+
/* Options to control the compilation */
extern int yydebug;
extern char debugopt[128];
--- a/parse/util.c
+++ b/parse/util.c
@@ -383,3 +383,28 @@
return (sz + a - 1) & ~(a - 1);
}
+void indentf(int depth, char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vfindentf(stdout, depth, fmt, ap);
+ va_end(ap);
+}
+
+void findentf(FILE *fd, int depth, char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ vfindentf(fd, depth, fmt, ap);
+ va_end(ap);
+}
+
+void vfindentf(FILE *fd, int depth, char *fmt, va_list ap)
+{
+ ssize_t i;
+
+ for (i = 0; i < depth; i++)
+ fprintf(fd, "\t");
+ vfprintf(fd, fmt, ap);
+}
+
binary files a/test/genericmake b/test/genericmake differ