shithub: mc

Download patch

ref: 7f5c1395bc44fd7cec4576a0feeb275cdd7ffb76
parent: da5f858f60c9a8d52add2f60845483be2f4b416e
author: Ori Bernstein <[email protected]>
date: Fri Oct 11 09:02:40 EDT 2013

Factor out duplicated variable node initialization.

--- a/parse/infer.c
+++ b/parse/infer.c
@@ -787,6 +787,25 @@
     popstab();
 }
 
+static Type *initvar(Inferstate *st, Node *n, Node *s)
+{
+    Type *t;
+
+    if (s->decl.isgeneric)
+        t = tyfreshen(st, tf(st, s->decl.type));
+    else
+        t = s->decl.type;
+    settype(st, n, t);
+    n->expr.did = s->decl.did;
+    n->expr.isconst = s->decl.isconst;
+    if (s->decl.isgeneric && !st->ingeneric) {
+        lappend(&st->specializationscope, &st->nspecializationscope, curstab());
+        lappend(&st->specializations, &st->nspecializations, n);
+        lappend(&st->genericdecls, &st->ngenericdecls, s);
+    }
+    return t;
+}
+
 /* Finds out if the member reference is actually
  * referring to a namespaced name, instead of a struct
  * member. If it is, it transforms it into the variable
@@ -818,17 +837,7 @@
     if (!s)
         fatal(n->line, "Undeclared var %s.%s", nsname->name.ns, nsname->name.name);
     var = mkexpr(n->line, Ovar, nsname, NULL);
-    var->expr.did = s->decl.did;
-    var->expr.isconst = s->decl.isconst;
-    if (s->decl.isgeneric)
-        settype(st, var, tyfreshen(st, s->decl.type));
-    else
-        settype(st, var, s->decl.type);
-    if (s->decl.isgeneric && !st->ingeneric) {
-        lappend(&st->specializationscope, &st->nspecializationscope, curstab());
-        lappend(&st->specializations, &st->nspecializations, var);
-        lappend(&st->genericdecls, &st->ngenericdecls, s);
-    }
+    initvar(st, var, s);
     *ret = var;
 }
 
@@ -1108,20 +1117,7 @@
             s = getdcl(curstab(), args[0]);
             if (!s)
                 fatal(n->line, "Undeclared var %s", ctxstr(st, args[0]));
-
-            n->expr.isconst = s->decl.isconst;
-            if (s->decl.isgeneric)
-                t = tyfreshen(st, tf(st, s->decl.type));
-            else
-                t = s->decl.type;
-            n->expr.isconst = s->decl.isconst;
-            settype(st, n, t);
-            n->expr.did = s->decl.did;
-            if (s->decl.isgeneric && !st->ingeneric) {
-                lappend(&st->specializationscope, &st->nspecializationscope, curstab());
-                lappend(&st->specializations, &st->nspecializations, n);
-                lappend(&st->genericdecls, &st->ngenericdecls, s);
-            }
+            t = initvar(st, n, s);
             break;
         case Oucon:
             inferucon(st, n, &n->expr.isconst);