shithub: mc

Download patch

ref: adc89d37fecdce58d5af63a39c055369d624f089
parent: f32109a79fd8602de04e1dca83ae3aaf44cadf03
author: Ori Bernstein <[email protected]>
date: Sun Feb 2 11:21:41 EST 2014

Remove 'trait' node.

    This should go in the types table, not in it's own AST node.

--- a/myrtypes/myrtypes.c
+++ b/myrtypes/myrtypes.c
@@ -109,7 +109,7 @@
             printf("Body:\n");
             dumptypes(n->func.body, indent + 1);
             break;
-        case Ntrait: case Nimpl:
+        case Nimpl:
             die("Ntrait/Nimpl not yet supported!");
             break;
         case Nname:
--- a/parse/dump.c
+++ b/parse/dump.c
@@ -226,9 +226,6 @@
             fprintf(fd, "%s", n->name.name);
             fprintf(fd, ")\n");
             break;
-        case Ntrait:
-            die("Trait definition");
-            break;
         case Nimpl:
             die("Impl definition");
             break;
--- a/parse/gram.y
+++ b/parse/gram.y
@@ -134,7 +134,7 @@
 %type <tok> asnop cmpop addop mulop shiftop optident
 
 %type <tydef> tydef typeid
-%type <node> traitdef
+%type <trait> traitdef
 
 %type <node> exprln retexpr goto continue break expr atomicexpr 
 %type <node> littok literal asnexpr lorexpr landexpr borexpr
@@ -179,6 +179,7 @@
         Type **params;
         size_t nparams;
     } tydef;
+    Trait *trait;
     Node *node;
     Tok  *tok;
     Type *ty;
@@ -206,7 +207,7 @@
             {puttype(file->file.globls, mkname($1.line, $1.name), $1.type);
              installucons(file->file.globls, $1.type);}
         | traitdef
-            {lappend(&file->file.stmts, &file->file.nstmts, $1);}
+            {puttrait(file->file.globls, $1->name, $1);}
         | implstmt
             {lappend(&file->file.stmts, &file->file.nstmts, $1);}
         | /* empty */
@@ -313,7 +314,7 @@
         ;
 
 traitdef: Ttrait Tident generictype Tasn traitbody Tendblk
-            {$$ = mktraitdef($1->line, mkname($2->line, $2->str), $5.nl, $5.nn, NULL, 0);}
+            {$$ = mktrait($1->line, mkname($2->line, $2->str), NULL, 0, $5.nl, $5.nn);}
         ;
 
 traitbody
@@ -813,7 +814,7 @@
     size_t i;
 
     for (i = 0; i < ntraits; i++) {
-        if (!strcmp(traittab[i]->name, str)) {
+        if (!strcmp(namestr(traittab[i]->name), str)) {
             settrait(t, traittab[i]);
             return;
         }
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -1422,9 +1422,6 @@
             inferfunc(st, n);
             popstab();
             break;
-        case Ntrait:
-            die("Trait inference not yet implemented\n");
-            break;
         case Nimpl:
             die("Impl not yet implemented\n");
             break;
@@ -1742,9 +1739,6 @@
                 default:        break;
             }
             break;
-        case Ntrait:
-            die("Trait inference not yet implemented\n");
-            break;
         case Nimpl:
             die("Trait inference not yet implemented\n");
             break;
@@ -1855,9 +1849,6 @@
             for (i = 0; i < n->func.nargs; i++)
                 nodetag(st, n->func.args[i], ingeneric);
             nodetag(st, n->func.body, ingeneric);
-            break;
-        case Ntrait:
-            die("Trait inference not yet implemented\n");
             break;
         case Nimpl:
             die("Impl not yet implemented\n");
--- a/parse/node.c
+++ b/parse/node.c
@@ -183,19 +183,6 @@
     return n;
 }
 
-Node *mktraitdef(int line, Node *name, Node **funcs, size_t nfuncs, Node **membs, size_t nmembs)
-{
-    Node *n;
-
-    n = mknode(line, Ntrait);
-    n->trait.name = name;
-    n->trait.funcs = funcs;
-    n->trait.nfuncs = nfuncs;
-    n->trait.membs = membs;
-    n->trait.nmembs = nmembs;
-    return n;
-}
-
 Node *mkimplstmt(int line, Node *name, Type *t)
 {
     Node *n;
--- a/parse/nodes.def
+++ b/parse/nodes.def
@@ -12,5 +12,4 @@
 N(Nname)
 N(Ndecl)
 N(Nfunc)
-N(Ntrait)
 N(Nimpl)
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -147,7 +147,7 @@
 
 struct Trait {
     int cid;    /* unique id */
-    char *name;
+    Node *name;
     Node **memb;        /* type must have these members */
     size_t nmemb;
     Node **funcs;       /* and declare these funcs */
@@ -254,7 +254,6 @@
             char  isglobl;
             char  isconst;
             char  isgeneric;
-            char  istrait;
             char  isextern;
             char  ishidden;
         } decl;
@@ -400,7 +399,7 @@
 Type *mktyfunc(int line, Node **args, size_t nargs, Type *ret);
 Type *mktystruct(int line, Node **decls, size_t ndecls);
 Type *mktyunion(int line, Ucon **decls, size_t ndecls);
-Trait *mktrait(int line, char *name, Node **memb, size_t nmemb, Node **funcs, size_t nfuncs);
+Trait *mktrait(int line, Node *name, Node **memb, size_t nmemb, Node **funcs, size_t nfuncs);
 Type *mktylike(int line, Ty ty); /* constrains tyvar t like it was builtin ty */
 int   istysigned(Type *t);
 int   istyfloat(Type *t);
@@ -432,7 +431,6 @@
 Node *mkmatchstmt(int line, Node *val, Node **matches, size_t nmatches);
 Node *mkmatch(int line, Node *pat, Node *body);
 Node *mkblock(int line, Stab *scope);
-Node *mktraitdef(int line, Node *name, Node **funcs, size_t nfuncs, Node **membs, size_t nmembs);
 Node *mkimplstmt(int line, Node *name, Type *type);
 Node *mkintlit(int line, uvlong val);
 Node *mkidxinit(int line, Node *idx, Node *init);
--- a/parse/specialize.c
+++ b/parse/specialize.c
@@ -224,7 +224,7 @@
             break;
         case Nnone: case Nname:
             break;
-        case Ntrait: case Nimpl:
+        case Nimpl:
             die("trait/impl not implemented");
             break;
     }
@@ -343,7 +343,7 @@
             r->func.body = specializenode(n->func.body, tsmap);
             popstab();
             break;
-        case Ntrait: case Nimpl:
+        case Nimpl:
             die("trait/impl not implemented");
         case Nnone:
             die("Nnone should not be seen as node type!");
--- a/parse/stab.c
+++ b/parse/stab.c
@@ -12,7 +12,7 @@
 
 #include "parse.h"
 
-/* Allows us to look up types/cstrs by name nodes */
+/* Allows us to look up types/traits by name nodes */
 typedef struct Tydefn Tydefn;
 typedef struct Traitdefn Traitdefn;
 struct Tydefn {
@@ -24,7 +24,7 @@
 struct Traitdefn {
     int line;
     Node *name;
-    Trait *cstr;
+    Trait *trait;
 };
 
 #define Maxstabdepth 128
@@ -140,13 +140,13 @@
     return NULL;
 }
 
-Trait *getcstr(Stab *st, Node *n)
+Trait *gettrait(Stab *st, Node *n)
 {
     Traitdefn *c;
 
     do {
         if ((c = htget(st->ty, n)))
-            return c->cstr;
+            return c->trait;
         st = st->super;
     } while (st);
     return NULL;
@@ -220,17 +220,17 @@
     htput(st->uc, uc->name, uc);
 }
 
-void putcstr(Stab *st, Node *n, Trait *c)
+void puttrait(Stab *st, Node *n, Trait *c)
 {
-    Traitdefn *cd;
+    Traitdefn *td;
 
     if (gettype(st, n))
         fatal(n->line, "Type %s already defined", namestr(n));
-    cd = xalloc(sizeof(Tydefn));
-    cd->line = n->line;
-    cd->name = n;
-    cd->cstr = c;
-    htput(st->ty, cd->name, cd);
+    td = xalloc(sizeof(Tydefn));
+    td->line = n->line;
+    td->name = n;
+    td->trait = c;
+    htput(st->ty, td->name, td);
 }
 
 void putns(Stab *st, Stab *scope)
--- a/parse/type.c
+++ b/parse/type.c
@@ -100,12 +100,12 @@
 }
 
 /* steals memb, funcs */
-Trait *mktrait(int line, char *name, Node **memb, size_t nmemb, Node **funcs, size_t nfuncs)
+Trait *mktrait(int line, Node *name, Node **memb, size_t nmemb, Node **funcs, size_t nfuncs)
 {
     Trait *c;
 
     c = zalloc(sizeof(Trait));
-    c->name = strdup(name);
+    c->name = name;
     c->memb = memb;
     c->nmemb = nmemb;
     c->funcs = funcs;
@@ -363,7 +363,7 @@
     sep = "";
     for (i = 0; i < ntraits; i++) {
         if (bshas(t->traits, i)) {
-            p += snprintf(p, end - p, "%s%s", sep, traittab[i]->name);
+            p += snprintf(p, end - p, "%s%s", sep, namestr(traittab[i]->name));
             sep = ",";
         }
     }
@@ -631,7 +631,7 @@
     Type *ty;
 
 #define Tc(c, n) \
-    mktrait(-1, n, NULL, 0, NULL, 0);
+    mktrait(-1, mkname(-1, n), NULL, 0, NULL, 0);
 #include "trait.def"
 #undef Tc
 
--- a/parse/use.c
+++ b/parse/use.c
@@ -461,7 +461,7 @@
                 pickle(n->func.args[i], fd);
             pickle(n->func.body, fd);
             break;
-        case Ntrait: case Nimpl:
+        case Nimpl:
             die("Ntrait/Nimpl not yet supported!");
             break;
         case Nnone:
@@ -596,7 +596,7 @@
             n->func.body = unpickle(fd);
             popstab();
             break;
-        case Ntrait: case Nimpl:
+        case Nimpl:
             die("Ntrait/Nimpl not yet supported!");
             break;
         case Nnone: