summaryrefslogtreecommitdiff
path: root/parse
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-08-07 23:17:14 -0700
committerOri Bernstein <ori@eigenstate.org>2017-08-08 20:56:59 -0700
commit890ecfa3dc504d7f302faab58722cc196dd0979f (patch)
tree315caf735d222fef152e887a6849997ceae21553 /parse
parentb53309d883b5facd81d37606c49b432833f32b72 (diff)
downloadmc-890ecfa3dc504d7f302faab58722cc196dd0979f.tar.gz
Bring back enforced namespaces.
It turns out that people actually like being forced to type the full names. Go figure. It does have the benefit of adding pressure to keep package names short. Eventually, we'll probably grow the ability to alias a package at import. We'll see how that goes. This reverts commit c5245eaa80064b77186fa286f8e5e8bca73309ab.
Diffstat (limited to 'parse')
-rw-r--r--parse/infer.c2
-rw-r--r--parse/parse.h20
-rw-r--r--parse/stab.c124
3 files changed, 48 insertions, 98 deletions
diff --git a/parse/infer.c b/parse/infer.c
index 837535e..04f7fc6 100644
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -1383,7 +1383,7 @@ inferpat(Node **np, Node *val, Node ***bind, size_t *nbind)
ns = curstab();
if (args[0]->name.ns)
ns = getns(file, args[0]->name.ns);
- s = getnsdcl(ns, args[0]);
+ s = getdcl(ns, args[0]);
if (s && !s->decl.ishidden) {
if (s->decl.isgeneric)
t = tysubst(s->decl.type, s->decl.type);
diff --git a/parse/parse.h b/parse/parse.h
index 74abae4..1fc3ac9 100644
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -97,14 +97,13 @@ struct Stab {
char *name;
char isfunc;
- /* symbols */
- Htab *dcl; /* decls */
+ /* Contents of stab.
+ * types and values are in separate namespaces. */
+ Htab *dcl;
+ Htab *env; /* the syms we close over, if we're a function */
Htab *ty; /* types */
Htab *tr; /* traits */
Htab *uc; /* union constructors */
-
- /* not quite symbols */
- Htab *env; /* the syms closed over, if this is a function stab */
Htab *lbl; /* labels */
Htab *impl; /* trait implementations: really a set of implemented traits. */
@@ -410,16 +409,15 @@ void forcedcl(Stab *st, Node *dcl);
void putucon(Stab *st, Ucon *uc);
void putlbl(Stab *st, char *name, Node *lbl);
-void *getnsdcl(Stab *st, Node *n);
-Node *getdcl(Stab *st, Node *n);
-Type *gettype(Stab *st, Node *n);
-Trait *gettrait(Stab *st, Node *n);
-Ucon *getucon(Stab *st, Node *n);
-
Stab *getns(Node *file, char *n);
+Node *getdcl(Stab *st, Node *n);
Node *getclosed(Stab *st, Node *n);
Node **getclosure(Stab *st, size_t *n);
+Type *gettype_l(Stab *st, Node *n);
+Type *gettype(Stab *st, Node *n);
Node *getimpl(Stab *st, Node *impl);
+Trait *gettrait(Stab *st, Node *n);
+Ucon *getucon(Stab *st, Node *n);
Node *getlbl(Stab *st, Srcloc loc, char *name);
Stab *curstab(void);
diff --git a/parse/stab.c b/parse/stab.c
index 7e7e968..7c031f9 100644
--- a/parse/stab.c
+++ b/parse/stab.c
@@ -16,7 +16,6 @@
/* Allows us to look up types/traits by name nodes */
typedef struct Tydefn Tydefn;
typedef struct Traitdefn Traitdefn;
-
struct Tydefn {
Srcloc loc;
Node *name;
@@ -226,6 +225,27 @@ getclosure(Stab *st, size_t *n)
* not in the current scope, it is recorded
* in the scope's closure.
*/
+Node *
+getdcl(Stab *st, Node *n)
+{
+ Node *s;
+ Stab *fn;
+
+ fn = NULL;
+ do {
+ s = htget(st->dcl, n);
+ if (s) {
+ /* record that this is in the closure of this scope */
+ if (fn && !s->decl.isglobl && !s->decl.isgeneric)
+ htput(fn->env, s->decl.name, s);
+ return s;
+ }
+ if (!fn && st->env)
+ fn = st;
+ st = st->super;
+ } while (st);
+ return NULL;
+}
void
putlbl(Stab *st, char *name, Node *lbl)
@@ -246,50 +266,18 @@ getlbl(Stab *st, Srcloc loc, char *name)
return htget(st->lbl, name);
}
-int
-hastype(Stab *st, Node *n)
-{
- do {
- if (hthas(st->ty, n))
- return 1;
- st = st->super;
- } while (st);
- return 0;
-}
-
-/* because of function casting rules, it's cleaner to do this as a macro */
-static void*
-getunique(void *(*fn)(Stab *, Node*), char *name, Stab *st, Node *n)
+Type *
+gettype_l(Stab *st, Node *n)
{
- size_t i, nk;
- void *p, *sym;
- char *foundns;
- void **k;
- Stab *s;
-
- p = fn(st, n);
- /* file can be null early on, when initializing the type tables */
- if (p || n->name.ns || !file)
- return p;
+ Tydefn *t;
- /* if a name is globally unique, we can refer to it without a namespace */
- sym = NULL;
- k = htkeys(file->file.ns, &nk);
- for (i = 0; i < nk; i++) {
- s = htget(file->file.ns, k[i]);
- p = fn(s, n);
- if (p) {
- if (sym)
- fatal(n, "ambiguous %s %s, defined in %s and %s\n", name, namestr(n), k[i], foundns);
- foundns = k[i];
- sym = p;
- }
- }
- return sym;
+ if ((t = htget(st->ty, n)))
+ return t->type;
+ return NULL;
}
-static void*
-getnstype(Stab *st, Node *n)
+Type *
+gettype(Stab *st, Node *n)
{
Tydefn *t;
@@ -301,43 +289,19 @@ getnstype(Stab *st, Node *n)
return NULL;
}
-Type*
-gettype(Stab *st, Node *n)
-{
- return getunique(getnstype, "type", st, n);
-}
-
-void*
-getnsdcl(Stab *st, Node *n)
+int
+hastype(Stab *st, Node *n)
{
- Node *s;
- Stab *fn;
-
- fn = NULL;
do {
- s = htget(st->dcl, n);
- if (s) {
- /* record that this is in the closure of this scope */
- if (fn && !s->decl.isglobl && !s->decl.isgeneric)
- htput(fn->env, s->decl.name, s);
- return s;
- }
- if (!fn && st->env)
- fn = st;
+ if (hthas(st->ty, n))
+ return 1;
st = st->super;
} while (st);
- return NULL;
-}
-
-
-Node*
-getdcl(Stab *st, Node *n)
-{
- return getunique(getnsdcl, "decl", st, n);
+ return 0;
}
-static void*
-getnsucon(Stab *st, Node *n)
+Ucon *
+getucon(Stab *st, Node *n)
{
Ucon *uc;
@@ -349,14 +313,8 @@ getnsucon(Stab *st, Node *n)
return NULL;
}
-Ucon*
-getucon(Stab *st, Node *n)
-{
- return getunique(getnsucon, "union constructor", st, n);
-}
-
-static void*
-getnstrait(Stab *st, Node *n)
+Trait *
+gettrait(Stab *st, Node *n)
{
Traitdefn *c;
@@ -370,12 +328,6 @@ getnstrait(Stab *st, Node *n)
return NULL;
}
-Trait*
-gettrait(Stab *st, Node *n)
-{
- return getunique(getnstrait, "trait", st, n);
-}
-
Stab *
getns(Node *file, char *name) {
return htget(file->file.ns, name);