shithub: mc

Download patch

ref: fb324f09c66a57b4566c134232d835ac5ef42796
parent: a63c8ce234719b9ae18f1d7423c817abf71a040b
author: Ori Bernstein <orib@google.com>
date: Thu Dec 15 11:10:29 EST 2011

More stubbing in of type stuff.

--- a/parse/infer.c
+++ b/parse/infer.c
@@ -39,8 +39,43 @@
     return e->expr.op;
 }
 
-static void inferexpr(Node *n)
+static Type *type(Node *n)
 {
+    die("Unimplemented type()");
+    return NULL;
+}
+
+static Type *littype(Node *lit)
+{
+    return NULL;
+}
+
+static Type *unify(Type *a, Type *b)
+{
+    die("Unimplemented unify");
+    return NULL;
+}
+
+static Type *tyfind(Type *t)
+{
+    die("Unimplemented tyfind");
+    return t;
+}
+
+static void unifycall(Node *n)
+{
+}
+
+static void inferexpr(Node *n, Type *ret)
+{
+    Node **args;
+    int nargs;
+    Type *t;
+    int i;
+
+    assert(n->type == Nexpr);
+    args = n->expr.args;
+    nargs = n->expr.nargs;
     switch (exprop(n)) {
         /* all operands are same type */
         case Oadd:      /* @a + @a -> @a */
@@ -59,17 +94,6 @@
         case Opredec:   /* --@a -> @a */
         case Opostinc:  /* @a++ -> @a */
         case Opostdec:  /* @a-- -> @a */
-        case Oaddr:     /* &@a -> @a* */
-        case Oderef:    /* *@a* ->  @a */
-        case Olor:      /* @a || @b -> bool */
-        case Oland:     /* @a && @b -> bool */
-        case Olnot:     /* !@a -> bool */
-        case Oeq:       /* @a == @a -> bool */
-        case One:       /* @a != @a -> bool */
-        case Ogt:       /* @a > @a -> bool */
-        case Oge:       /* @a >= @a -> bool */
-        case Olt:       /* @a < @a -> bool */
-        case Ole:       /* @a <= @b -> bool */
         case Oasn:      /* @a = @a -> @a */
         case Oaddeq:    /* @a += @a -> @a */
         case Osubeq:    /* @a -= @a -> @a */
@@ -81,26 +105,75 @@
         case Obxoreq:   /* @a ^= @a -> @a */
         case Obsleq:    /* @a <<= @a -> @a */
         case Obsreq:    /* @a >>= @a -> @a */
+            t = type(args[0]);
+            for (i = 1; i < nargs; i++)
+                t = unify(t, type(args[i]));
+            settype(n, tyfind(t));
+            break;
+
+        /* operands same type, returning bool */
+        case Olor:      /* @a || @b -> bool */
+        case Oland:     /* @a && @b -> bool */
+        case Olnot:     /* !@a -> bool */
+        case Oeq:       /* @a == @a -> bool */
+        case One:       /* @a != @a -> bool */
+        case Ogt:       /* @a > @a -> bool */
+        case Oge:       /* @a >= @a -> bool */
+        case Olt:       /* @a < @a -> bool */
+        case Ole:       /* @a <= @b -> bool */
+            t = type(args[0]);
+            for (i = 1; i < nargs; i++)
+                unify(t, type(args[i]));
+            settype(n, mkty(-1, Tybool));
+            break;
+
+        /* reach into a type and pull out subtypes */
+        case Oaddr:     /* &@a -> @a* */
+            settype(n, mktyptr(n->line, type(args[0])));
+            break;
+        case Oderef:    /* *@a* ->  @a */
+            t = unify(type(args[0]), mktyptr(n->line, mktyvar(n->line)));
+            settype(n, t);
+            break;
         case Oidx:      /* @a[@b::tcint] -> @a */
+            die("inference of indexes not done yet");
+            break;
         case Oslice:    /* @a[@b::tcint,@b::tcint] -> @a[,] */
+            die("inference of slices not done yet");
+            break;
+
+        /* special cases */
         case Omemb:     /* @a.Ident -> @b, verify type(@a.Ident)==@b later */
+            die("members not done yet");
+            break;
         case Osize:     /* sizeof @a -> size */
+            die("inference of sizes not done yet");
+            break;
         case Ocall:     /* (@a, @b, @c, ... -> @r)(@a,@b,@c, ... -> @r) -> @r */
+            unifycall(n);
+            break;
         case Ocast:     /* cast(@a, @b) -> @b */
+            die("casts not implemented");
+            break;
         case Oret:      /* -> @a -> void */
+            settype(n, mkty(-1, Tyvoid));
+            break;
         case Ogoto:     /* goto void* -> void */
+            settype(n, mkty(-1, Tyvoid));
+            break;
         case Ovar:      /* a:@a -> @a */
+            settype(n, decltype(n));
+            break;
         case Olit:      /* <lit>:@a::tyclass -> @a */
+            settype(n, littype(n));
+            break;
         case Olbl:      /* :lbl -> void* */
+            settype(n, mktyptr(n->line, mkty(-1, Tyvoid)));
         case Obad:      /* error! */
             break;
     }
 }
 
-static void inferlit(Node *n)
-{
-}
-
 static void inferfunc(Node *n)
 {
 }
@@ -140,12 +213,11 @@
             infernode(n->loopstmt.body);
             break;
         case Nexpr:
-            inferexpr(n);
-        case Nlit:
-            inferlit(n);
+            inferexpr(n, NULL);
         case Nfunc:
             inferfunc(n);
         case Nname:
+        case Nlit:
         case Nuse:
             break;
     }
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -212,6 +212,7 @@
 /* type creation */
 void tyinit(); /* sets up built in types */
 
+Type *mkty(int line, Ty ty);
 Type *mktyvar(int line);
 Type *mktyparam(int line, char *name);
 Type *mktynamed(int line, Node *name);
--- a/parse/type.c
+++ b/parse/type.c
@@ -40,7 +40,7 @@
 int ncstr;
 
 static int nexttid = 0;
-static Type *mktype(Ty ty)
+Type *mkty(int line, Ty ty)
 {
     Type *t;
 
@@ -54,7 +54,7 @@
 {
     Type *t;
 
-    t = mktype(Tyvar);
+    t = mkty(line, Tyvar);
     return t;
 }
 
@@ -62,7 +62,7 @@
 {
     Type *t;
 
-    t = mktype(Tyvar);
+    t = mkty(line, Tyvar);
     t->pname = strdup(name);
     return t;
 }
@@ -76,10 +76,10 @@
     if (name->name.nparts == 1)
         for (i = 0; typenames[i].name; i++)
             if (!strcmp(typenames[i].name, name->name.parts[0]))
-                return mktype(typenames[i].ty);
+                return mkty(line, typenames[i].ty);
 
     /* if not, resolve it in the type inference stage */
-    t = mktype(Tyname);
+    t = mkty(line, Tyname);
     t->name = name;
     return t;
 }
@@ -88,7 +88,7 @@
 {
     Type *t;
 
-    t = mktype(Tyarray);
+    t = mkty(line, Tyarray);
     t->abase = base;
     t->asize = sz;
 
@@ -99,7 +99,7 @@
 {
     Type *t;
 
-    t = mktype(Tyslice);
+    t = mkty(line, Tyslice);
     t->sbase = base;
     return t;
 }
@@ -108,7 +108,7 @@
 {
     Type *t;
 
-    t = mktype(Typtr);
+    t = mkty(line, Typtr);
     t->pbase = base;
     return t;
 }
@@ -118,7 +118,7 @@
     Type *t;
     int i;
 
-    t = mktype(Tyfunc);
+    t = mkty(line, Tyfunc);
     t->nsub = nargs + 1;
     t->fnsub = xalloc((1 + nargs)*sizeof(Type));
     t->fnsub[0] = ret;
@@ -131,7 +131,7 @@
 {
     Type *t;
 
-    t = mktype(Tystruct);
+    t = mkty(line, Tystruct);
     t->nsub = ndecls;
     t->sdecls = memdup(decls, ndecls*sizeof(Node *));
     return t;
@@ -141,7 +141,7 @@
 {
     Type *t;
 
-    t = mktype(Tyunion);
+    t = mkty(line, Tyunion);
     t->udecls = decls;
     return t;
 }
@@ -150,7 +150,7 @@
 {
     Type *t;
 
-    t = mktype(Tyenum);
+    t = mkty(line, Tyenum);
     t->edecls = decls;
     return t;
 }