shithub: mc

Download patch

ref: 3825a62a980ee98a8b9a1f451081fedccd082cc4
parent: 6eea4ad525d048ac253d7f06de673b68d0bb9add
author: Ori Bernstein <[email protected]>
date: Fri Jan 6 19:22:10 EST 2012

Some misc type inference fixes.

--- a/parse/gram.y
+++ b/parse/gram.y
@@ -527,7 +527,7 @@
 blockbody
         : stmt
             {
-                $$ = mkblock(line, mkstab(curstab()));
+                $$ = mkblock(line, mkstab());
                 nlappend(&$$->block.stmts, &$$->block.nstmts, $1);
             }
         | blockbody stmt
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -91,7 +91,7 @@
         case Lfunc:
             /* we figure out the return type to infer for the body in
              * infernode() */
-            infernode(n, NULL);
+            infernode(n->lit.fnval, NULL);
             return n->lit.fnval->func.type;
             break;
         case Larray:    return NULL; break;
@@ -289,7 +289,7 @@
         case Ovar:      /* a:@a -> @a */
             s = getdcl(curstab(), args[0]);
             if (!s)
-                fatal(n->line, "Undeclared var");
+                fatal(n->line, "Undeclared var %s", args[0]->name.parts[args[0]->name.nparts - 1]);
             else
                 settype(n, s->type);
             break;
@@ -339,8 +339,10 @@
             inferdecl(n);
             break;
         case Nblock:
+            pushstab(n->block.scope);
             for (i = 0; i < n->block.nstmts; i++)
                 infernode(n->block.stmts[i], ret);
+            popstab();
             break;
         case Nifstmt:
             infernode(n->ifstmt.cond, NULL);
@@ -354,11 +356,13 @@
             infernode(n->loopstmt.body, ret);
             break;
         case Nexpr:
-            inferexpr(n, NULL);
+            inferexpr(n, ret);
+            break;
         case Nfunc:
             pushstab(n->func.scope);
             inferfunc(n);
             popstab();
+            break;
         case Nname:
         case Nlit:
         case Nuse:
--- a/parse/main.c
+++ b/parse/main.c
@@ -49,8 +49,8 @@
         tyinit();
         tokinit(argv[i]);
         file = mkfile(argv[i]);
-        file->file.exports = mkstab(NULL);
-        file->file.globls = mkstab(NULL);
+        file->file.exports = mkstab();
+        file->file.globls = mkstab();
         yyparse();
 
         /* before we do anything to the parse */
--- a/parse/node.c
+++ b/parse/node.c
@@ -98,13 +98,17 @@
 {
     Node *n;
     Node *f;
+    int i;
 
     f = mknode(line, Nfunc);
     f->func.args = args;
     f->func.nargs = nargs;
     f->func.body = body;
-    f->func.scope = mkstab(curstab());
+    f->func.scope = mkstab();
     f->func.type = mktyfunc(line, args, nargs, ret);
+
+    for (i = 0; i < nargs; i++)
+        putdcl(f->func.scope, args[i]->decl.sym);
 
     n = mknode(line, Nlit);
     n->lit.littype = Lfunc;
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -215,7 +215,7 @@
 extern Type *littypes[]; /* literal type -> type map */
 
 /* data structures */
-Bitset *mkbs();
+Bitset *mkbs(void);
 Bitset *dupbs(Bitset *bs);
 void delbs(Bitset *bs);
 void bsput(Bitset *bs, uint elt);
@@ -251,7 +251,7 @@
 int yyparse(void);
 
 /* stab creation */
-Stab *mkstab(Stab *super);
+Stab *mkstab(void);
 
 void putns(Stab *st, Stab *scope);
 void puttype(Stab *st, Node *n, Type *ty);
@@ -268,7 +268,7 @@
 Sym *mksym(int line, Node *name, Type *ty);
 
 /* type creation */
-void tyinit(); /* sets up built in types */
+void tyinit(void); /* sets up built in types */
 
 Type *mkty(int line, Ty ty);
 Type *mktyvar(int line);
@@ -343,4 +343,4 @@
 void nlappend(Node ***nl, size_t *len, Node *n);
 
 /* backend functions */
-void gen();
+void gen(void);
--- a/parse/pickle.c
+++ b/parse/pickle.c
@@ -182,7 +182,7 @@
     int i;
 
     /* read dcls */
-    st = mkstab(NULL);
+    st = mkstab();
     st->name = unpickle(fd);
     n = rdint(fd);
     for (i = 0; i < n; i++)
@@ -260,7 +260,8 @@
             pickle(ty->asize, fd);
             break;
         case Tyvar:
-            fprintf(stderr, "WARNING: Attempting to pickle Tyvar. This will not read back reliably.");
+            fprintf(stderr, "WARNING: Attempting to pickle Tyvar. This will not read back reliably.\n");
+            break;
         default:
             for (i = 0; i < ty->nsub; i++)
                 wrtype(fd, ty->sub[i]);
--- a/parse/stab.c
+++ b/parse/stab.c
@@ -64,15 +64,15 @@
     return sym;
 }
 
-Stab *mkstab(Stab *super)
+Stab *mkstab()
 {
     Stab *st;
 
     st = zalloc(sizeof(Stab));
-    st->super = super;
     st->ns = mkht(namehash, nameeq);
     st->dcl = mkht(namehash, nameeq);
     st->ty = mkht(namehash, nameeq);
+    st->closure = mkht(namehash, nameeq);
     return st;
 }