shithub: mc

Download patch

ref: 890ecfa3dc504d7f302faab58722cc196dd0979f
parent: b53309d883b5facd81d37606c49b432833f32b72
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Aug 7 19:17:14 EDT 2017

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.

--- a/mk/c.mk
+++ b/mk/c.mk
@@ -9,7 +9,7 @@
 _LIBPATHS=$(addprefix -l, $(patsubst lib%.a,%,$(notdir $(DEPS)))) $(_PCLIBS)
 
 # yeah, I should probably remove -Werror, but it's nice for developing alone.
-CFLAGS += -Wall -Werror -Wno-unused-parameter -Wno-missing-field-initializers -Wno-sign-compare -Wno-char-subscripts -g -O0
+CFLAGS += -Wall -Werror -Wno-unused-parameter -Wno-missing-field-initializers -Wno-sign-compare -Wno-char-subscripts -g -O3
 CFLAGS += -MMD -MP -MF .deps/$(subst /,-,$*).d
 
 LIB ?= $(INSTLIB)
--- a/parse/infer.c
+++ b/parse/infer.c
@@ -1383,7 +1383,7 @@
 		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);
--- a/parse/parse.h
+++ b/parse/parse.h
@@ -97,14 +97,13 @@
 	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 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);
--- 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,7 +225,28 @@
  * 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 @@
 	return htget(st->lbl, name);
 }
 
-int
-hastype(Stab *st, Node *n)
+Type *
+gettype_l(Stab *st, Node *n)
 {
-	do {
-		if (hthas(st->ty, n))
-			return 1;
-		st = st->super;
-	} while (st);
-	return 0;
-}
+	Tydefn *t;
 
-/* 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)
-{
-	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;
-
-	/* 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,44 +289,20 @@
 	return NULL;
 }
 
-Type*
-gettype(Stab *st, Node *n)
+int
+hastype(Stab *st, Node *n)
 {
-	return getunique(getnstype, "type", st, n);
-}
-
-void*
-getnsdcl(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;
+	return 0;
 }
 
-
-Node*
-getdcl(Stab *st, Node *n)
+Ucon *
+getucon(Stab *st, Node *n)
 {
-	return getunique(getnsdcl, "decl", st, n);
-}
-
-static void*
-getnsucon(Stab *st, Node *n)
-{
 	Ucon *uc;
 
 	do {
@@ -349,15 +313,9 @@
 	return NULL;
 }
 
-Ucon*
-getucon(Stab *st, Node *n)
+Trait *
+gettrait(Stab *st, Node *n)
 {
-	return getunique(getnsucon, "union constructor", st, n);
-}
-
-static void*
-getnstrait(Stab *st, Node *n)
-{
 	Traitdefn *c;
 
 	if (n->name.ns)
@@ -368,12 +326,6 @@
 		st = st->super;
 	} while (st);
 	return NULL;
-}
-
-Trait*
-gettrait(Stab *st, Node *n)
-{
-	return getunique(getnstrait, "trait", st, n);
 }
 
 Stab *