shithub: mc

Download patch

ref: ce384085a46a728d096389e3df59d1ca25d51aa2
parent: ecf8ce9396333eecb45500ae306f73405285806d
author: Ori Bernstein <[email protected]>
date: Tue Jun 5 17:01:57 EDT 2012

Relax restrictions on args of mktyfunc()

    We used to only be able to take Ndecl nodes, but it turns out to
    be useful to take the type of any node, since we want to make types
    from arguments to the function. This comes up during type inference.

--- a/parse/node.c
+++ b/parse/node.c
@@ -221,7 +221,24 @@
 Type *decltype(Node *n)
 {
     assert(n->type == Ndecl);
-    return n->decl.sym->type;
+    return nodetype(n);
+}
+
+Type *exprtype(Node *n)
+{
+    assert(n->type == Ndecl);
+    return nodetype(n);
+}
+
+Type *nodetype(Node *n)
+{
+    switch (n->type) {
+        case Ndecl:     return n->decl.sym->type;       break;
+        case Nexpr:     return n->expr.type;            break;
+        case Nlit:      return n->lit.type;             break;
+        default:        die("Node %s has no type", nodestr(n->type)); break;
+    }
+    return NULL;
 }
 
 void setns(Node *n, char *name)
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -323,6 +323,7 @@
 /* node util functions */
 char *declname(Node *n);
 Type *decltype(Node *n);
+Type *nodetype(Node *n);
 void addstmt(Node *file, Node *stmt);
 void setns(Node *n, char *name);
 Op exprop(Node *n);
--- a/parse/type.c
+++ b/parse/type.c
@@ -176,7 +176,7 @@
     t->sub = xalloc((1 + nargs)*sizeof(Type));
     t->sub[0] = ret;
     for (i = 0; i < nargs; i++)
-        t->sub[i + 1] = decltype(args[i]);
+        t->sub[i + 1] = nodetype(args[i]);
     return t;
 }