shithub: mc

Download patch

ref: a61cdcb7496036b21256a5008465ad03583bc8d4
parent: 5f46a84bd478727391b1f8819e276fce68eb8958
author: Ori Bernstein <[email protected]>
date: Mon Sep 10 07:04:31 EDT 2012

Start tracking generic types.

--- a/parse/gram.y
+++ b/parse/gram.y
@@ -260,7 +260,7 @@
 
 tydef   : Ttype typeid Tasn type
             {$$ = $2;
-             $$.type = mktyname($2.line, mkname($2.line, $2.name), $4);}
+             $$.type = mktytmpl($2.line, mkname($2.line, $2.name), $2.params, $2.nparams, $4);}
         | Ttype typeid
             {$$ = $2;}
         ;
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -200,6 +200,7 @@
     if (t->resolved)
         return;
     t->resolved = 1;
+    /* if this is a generic type, bind the params. */
     /* Walk through aggregate type members */
     if (t->type == Tystruct) {
         for (i = 0; i < t->nmemb; i++)
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -98,22 +98,25 @@
     int tid;
     int line;
 
-    int resolved;     /* Have we resolved the subtypes? Prevents infinite recursion. */
-    int fixed;        /* Have we fixed the subtypes? Prevents infinite recursion. */
+    int resolved;       /* Have we resolved the subtypes? Prevents infinite recursion. */
+    int fixed;          /* Have we fixed the subtypes? Prevents infinite recursion. */
 
-    Bitset *cstrs;    /* the type constraints matched on this type */
-    Node **cstrlist;  /* The names of the constraints on the type. Used to fill the bitset */
-    size_t ncstrlist; /* The length of the constraint list above */
+    Bitset *cstrs;      /* the type constraints matched on this type */
+    Node **cstrlist;    /* The names of the constraints on the type. Used to fill the bitset */
+    size_t ncstrlist;   /* The length of the constraint list above */
 
-    Type **sub;       /* sub-types; shared by all composite types */
-    size_t nsub;      /* For compound types */
-    size_t nmemb;     /* for aggregate types (struct, union) */
+    Type **params;      /* Tyname: the type parameters captured */
+    size_t nparams;     /* Tyname: the number of type parameters */
+
+    Type **sub;         /* sub-types; shared by all composite types */
+    size_t nsub;        /* For compound types */
+    size_t nmemb;       /* for aggregate types (struct, union) */
     union {
-        Node *name;    /* Tyname: unresolved name. Tyalias: alias name */
-        Node *asize;   /* array size */
-        char *pname;   /* Typaram: name of type parameter */
-        Node **sdecls; /* Tystruct: decls in struct */
-        Ucon **udecls; /* Tyunion: decls in union */
+        Node *name;     /* Tyname: unresolved name. Tyalias: alias name */
+        Node *asize;    /* array size */
+        char *pname;    /* Typaram: name of type parameter */
+        Node **sdecls;  /* Tystruct: decls in struct */
+        Ucon **udecls;  /* Tyunion: decls in union */
     };
 };
 
@@ -339,6 +342,7 @@
 Type *mktyvar(int line);
 Type *mktyparam(int line, char *name);
 Type *mktyname(int line, Node *name, Type *base);
+Type *mktytmpl(int line, Node *name, Type **params, size_t nparams, Type *base);
 Type *mktyunres(int line, Node *name);
 Type *mktyarray(int line, Type *base, Node *sz);
 Type *mktyslice(int line, Type *base);
--- a/parse/type.c
+++ b/parse/type.c
@@ -128,7 +128,7 @@
     return t;
 }
 
-Type *mktyname(int line, Node *name, Type *base)
+Type *mktytmpl(int line, Node *name, Type **params, size_t nparams, Type *base)
 {
     Type *t;
 
@@ -138,9 +138,16 @@
     t->cstrs = bsdup(base->cstrs);
     t->sub = xalloc(sizeof(Type*));
     t->sub[0] = base;
+    t->params = params;
+    t->nparams = nparams;
     return t;
 }
 
+Type *mktyname(int line, Node *name, Type *base)
+{
+    return mktytmpl(line, name, NULL, 0, base);
+}
+
 Type *mktyarray(int line, Type *base, Node *sz)
 {
     Type *t;
@@ -432,6 +439,15 @@
             break;
         case Tyname:  
             p += snprintf(p, end - p, "%s", namestr(t->name));
+            if (t->nparams) {
+                p += snprintf(p, end - p, "(");
+                for (i = 0; i < t->nparams; i++)  {
+                    p += snprintf(p, end - p, "%s", sep);
+                    p += tybfmt(p, end - p, t->params[i]);
+                    sep = ", ";
+                }
+                p += snprintf(p, end - p, ")");
+            }
             break;
         case Tystruct:  p += fmtstruct(p, end - p, t);  break;
         case Tyunion:   p += fmtunion(p, end - p, t);   break;