[master] cebbb48 Make symbol->kind more OO

Poul-Henning Kamp phk at FreeBSD.org
Thu Feb 15 09:29:13 UTC 2018


commit cebbb48b042654e75fa17f2ed8ab7cc6ba072f42
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Thu Feb 15 09:15:43 2018 +0000

    Make symbol->kind more OO

diff --git a/include/tbl/symbol_kind.h b/include/tbl/symbol_kind.h
index 5095ae3..bfae2c1 100644
--- a/include/tbl/symbol_kind.h
+++ b/include/tbl/symbol_kind.h
@@ -29,20 +29,20 @@
 
 /*lint -save -e525 -e539 */
 
-VCC_SYMB(NONE,		none)
-VCC_SYMB(ACL,		acl)
-VCC_SYMB(ACTION,	action)
-VCC_SYMB(BACKEND,	backend)
-VCC_SYMB(FUNC,		func)
-VCC_SYMB(INSTANCE,	instance)
-VCC_SYMB(METHOD,	method)
-VCC_SYMB(OBJECT,	object)
-VCC_SYMB(PROBE,		probe)
-VCC_SYMB(STEVEDORE,	stevedore)
-VCC_SYMB(SUB,		sub)
-VCC_SYMB(VAR,		var)
-VCC_SYMB(VCL,		vcl)
-VCC_SYMB(VMOD,		vmod)
-#undef VCC_SYMB
+VCC_KIND(NONE,		none)
+VCC_KIND(ACL,		acl)
+VCC_KIND(ACTION,	action)
+VCC_KIND(BACKEND,	backend)
+VCC_KIND(FUNC,		func)
+VCC_KIND(INSTANCE,	instance)
+VCC_KIND(METHOD,	method)
+VCC_KIND(OBJECT,	object)
+VCC_KIND(PROBE,		probe)
+VCC_KIND(STEVEDORE,	stevedore)
+VCC_KIND(SUB,		sub)
+VCC_KIND(VAR,		var)
+VCC_KIND(VCL,		vcl)
+VCC_KIND(VMOD,		vmod)
+#undef VCC_KIND
 
 /*lint -restore */
diff --git a/lib/libvcc/vcc_compile.h b/lib/libvcc/vcc_compile.h
index 479731f..dd9a33a 100644
--- a/lib/libvcc/vcc_compile.h
+++ b/lib/libvcc/vcc_compile.h
@@ -84,6 +84,7 @@ struct token {
 	char			*dec;
 };
 
+/*---------------------------------------------------------------------*/
 typedef const struct type	*vcc_type_t;
 
 struct type {
@@ -98,12 +99,21 @@ struct type {
 #define VCC_TYPE(foo)		extern const struct type foo[1];
 #include "tbl/vcc_types.h"
 
+/*---------------------------------------------------------------------*/
+typedef const struct kind	*vcc_kind_t;
 
-enum symkind {
-#define VCC_SYMB(uu, ll)	SYM_##uu,
-#include "tbl/symbol_kind.h"
+struct kind {
+	unsigned		magic;
+#define KIND_MAGIC		0xfad72443
+
+	const char		*name;
 };
 
+#define VCC_KIND(U,l)	extern const struct kind SYM_##U[1];
+#include "tbl/symbol_kind.h"
+
+/*---------------------------------------------------------------------*/
+
 typedef void sym_expr_t(struct vcc *tl, struct expr **,
     struct token *, struct symbol *sym, vcc_type_t);
 typedef void sym_wildcard_t(struct vcc *, struct symbol *, struct symbol *);
@@ -122,7 +132,7 @@ struct symbol {
 	char				*name;
 	unsigned			nlen;
 	sym_wildcard_t			*wildcard;
-	enum symkind			kind;
+	vcc_kind_t			kind;
 
 	sym_act_f			*action;
 	unsigned			action_mask;
@@ -315,17 +325,16 @@ void vcc_stevedore(struct vcc *vcc, const char *stv_name);
 
 /* vcc_symb.c */
 void VCC_PrintCName(struct vsb *vsb, const char *b, const char *e);
-struct symbol *VCC_MkSym(struct vcc *tl, const char *b, enum symkind kind);
+struct symbol *VCC_MkSym(struct vcc *tl, const char *b, vcc_kind_t);
 extern const char XREF_NONE[];
 extern const char XREF_DEF[];
 extern const char XREF_REF[];
 extern const char SYMTAB_NOERR[];
 extern const char SYMTAB_CREATE[];
-struct symbol *VCC_SymbolGet(struct vcc *, enum symkind, const char *,
+struct symbol *VCC_SymbolGet(struct vcc *, vcc_kind_t, const char *,
     const char *);
-const char * VCC_SymKind(struct vcc *tl, const struct symbol *s);
 typedef void symwalk_f(struct vcc *tl, const struct symbol *s);
-void VCC_WalkSymbols(struct vcc *tl, symwalk_f *func, enum symkind kind);
+void VCC_WalkSymbols(struct vcc *tl, symwalk_f *func, vcc_kind_t);
 
 /* vcc_token.c */
 void vcc_Coord(const struct vcc *tl, struct vsb *vsb,
diff --git a/lib/libvcc/vcc_expr.c b/lib/libvcc/vcc_expr.c
index 1e55b83..520e6ed 100644
--- a/lib/libvcc/vcc_expr.c
+++ b/lib/libvcc/vcc_expr.c
@@ -683,7 +683,7 @@ vcc_expr4(struct vcc *tl, struct expr **e, vcc_type_t fmt)
 		}
 		VSB_printf(tl->sb,
 		    "Symbol type (%s) can not be used in expression.\n",
-		    VCC_SymKind(tl, sym));
+		    sym->kind->name);
 		vcc_ErrWhere(tl, tl->t);
 		if (sym->def_b != NULL) {
 			VSB_printf(tl->sb, "That symbol was defined here:\n");
diff --git a/lib/libvcc/vcc_symb.c b/lib/libvcc/vcc_symb.c
index 6f73b16..b5df60c 100644
--- a/lib/libvcc/vcc_symb.c
+++ b/lib/libvcc/vcc_symb.c
@@ -38,8 +38,11 @@
 #include "vct.h"
 
 /*--------------------------------------------------------------------*/
+#define VCC_KIND(U,l) const struct kind SYM_##U[1] = {{ KIND_MAGIC, #l}};
+#include "tbl/symbol_kind.h"
+/*--------------------------------------------------------------------*/
 
-static enum symkind
+static vcc_kind_t
 VCC_HandleKind(vcc_type_t fmt)
 {
 	if (fmt == ACL)		return(SYM_ACL);
@@ -51,19 +54,6 @@ VCC_HandleKind(vcc_type_t fmt)
 	return(SYM_NONE);
 }
 
-const char *
-VCC_SymKind(struct vcc *tl, const struct symbol *s)
-{
-	switch (s->kind) {
-#define VCC_SYMB(uu, ll)	case SYM_##uu: return(#ll);
-#include "tbl/symbol_kind.h"
-	default:
-		ErrInternal(tl);
-		VSB_printf(tl->sb, "Symbol Kind 0x%x\n", s->kind);
-		return("INTERNALERROR");
-	}
-}
-
 void
 VCC_PrintCName(struct vsb *vsb, const char *b, const char *e)
 {
@@ -101,13 +91,14 @@ vcc_new_symbol(struct vcc *tl, const char *b, const char *e)
 	sym->name[e - b] = '\0';
 	sym->nlen = e - b;
 	VTAILQ_INIT(&sym->children);
+	sym->kind = SYM_NONE;
 	sym->type = VOID;
 	return (sym);
 }
 
 static struct symbol *
 VCC_Symbol(struct vcc *tl, struct symbol *parent,
-    const char *b, const char *e, enum symkind kind, int create)
+    const char *b, const char *e, vcc_kind_t kind, int create)
 {
 	const char *q;
 	struct symbol *sym, *sym2 = NULL;
@@ -196,7 +187,7 @@ const char SYMTAB_NOERR[] = "sym_noerror";
 const char SYMTAB_CREATE[] = "sym_create";
 
 struct symbol *
-VCC_SymbolGet(struct vcc *tl, enum symkind kind, const char *e, const char *x)
+VCC_SymbolGet(struct vcc *tl, vcc_kind_t kind, const char *e, const char *x)
 {
 	struct symbol *sym;
 
@@ -229,7 +220,7 @@ VCC_SymbolGet(struct vcc *tl, enum symkind kind, const char *e, const char *x)
 			VSB_printf(tl->sb, " is a reserved word.");
 		else
 			VSB_printf(tl->sb, " has wrong type (%s): ",
-				VCC_SymKind(tl, sym));
+				sym->kind->name);
 		VSB_cat(tl->sb, "\nAt: ");
 		vcc_ErrWhere(tl, tl->t);
 		if (sym->def_b != NULL) {
@@ -259,7 +250,7 @@ VCC_SymbolGet(struct vcc *tl, enum symkind kind, const char *e, const char *x)
 }
 
 struct symbol *
-VCC_MkSym(struct vcc *tl, const char *b, enum symkind kind)
+VCC_MkSym(struct vcc *tl, const char *b, vcc_kind_t kind)
 {
 	struct symbol *sym;
 
@@ -271,7 +262,7 @@ VCC_MkSym(struct vcc *tl, const char *b, enum symkind kind)
 
 static void
 vcc_walksymbols(struct vcc *tl, const struct symbol *root,
-    symwalk_f *func, enum symkind kind)
+    symwalk_f *func, vcc_kind_t kind)
 {
 	struct symbol *sym;
 
@@ -284,7 +275,7 @@ vcc_walksymbols(struct vcc *tl, const struct symbol *root,
 }
 
 void
-VCC_WalkSymbols(struct vcc *tl, symwalk_f *func, enum symkind kind)
+VCC_WalkSymbols(struct vcc *tl, symwalk_f *func, vcc_kind_t kind)
 {
 
 	vcc_walksymbols(tl, tl->symbols, func, kind);
@@ -324,7 +315,7 @@ struct symbol *
 VCC_HandleSymbol(struct vcc *tl, vcc_type_t fmt, const char *pfx)
 {
 	struct symbol *sym;
-	enum symkind kind;
+	vcc_kind_t kind;
 	struct token *t;
 	const char *p;
 
@@ -334,7 +325,7 @@ VCC_HandleSymbol(struct vcc *tl, vcc_type_t fmt, const char *pfx)
 	t = tl->t;
 	sym = VCC_SymbolGet(tl, SYM_NONE, SYMTAB_NOERR, XREF_NONE);
 	if (sym != NULL && sym->def_b != NULL && kind == sym->kind) {
-		p = VCC_SymKind(tl, sym);
+		p = sym->kind->name;
 		VSB_printf(tl->sb, "%c%s '%.*s' redefined.\n",
 		    toupper(*p), p + 1, PF(t));
 		vcc_ErrWhere(tl, t);
@@ -352,7 +343,7 @@ VCC_HandleSymbol(struct vcc *tl, vcc_type_t fmt, const char *pfx)
 	} else if (sym != NULL && sym->kind != kind) {
 		VSB_printf(tl->sb,
 		    "Name %.*s must have type '%s'.\n",
-		    PF(t), VCC_SymKind(tl, sym));
+		    PF(t), sym->kind->name);
 		vcc_ErrWhere(tl, t);
 		return (sym);
 	}
diff --git a/lib/libvcc/vcc_xref.c b/lib/libvcc/vcc_xref.c
index ce77c2e..775becc 100644
--- a/lib/libvcc/vcc_xref.c
+++ b/lib/libvcc/vcc_xref.c
@@ -70,12 +70,12 @@ vcc_checkref(struct vcc *tl, const struct symbol *sym)
 	if (sym->ndef == 0 && sym->nref != 0) {
 		AN(sym->ref_b);
 		VSB_printf(tl->sb, "Undefined %s %.*s, first reference:\n",
-		    VCC_SymKind(tl, sym), PF(sym->ref_b));
+		    sym->kind->name, PF(sym->ref_b));
 		vcc_ErrWhere(tl, sym->ref_b);
 	} else if (sym->ndef != 0 && sym->nref == 0) {
 		AN(sym->def_b);
 		VSB_printf(tl->sb, "Unused %s %.*s, defined:\n",
-		    VCC_SymKind(tl, sym), PF(sym->def_b));
+		    sym->kind->name, PF(sym->def_b));
 		vcc_ErrWhere(tl, sym->def_b);
 		if (!tl->err_unref) {
 			VSB_printf(tl->sb, "(That was just a warning)\n");
@@ -335,7 +335,10 @@ static void v_matchproto_(symwalk_f)
 vcc_xreftable(struct vcc *tl, const struct symbol *sym)
 {
 
-	Fc(tl, 0, " * %-7s ", VCC_SymKind(tl, sym));
+	CHECK_OBJ_NOTNULL(sym, SYMBOL_MAGIC);
+	CHECK_OBJ_NOTNULL(sym->kind, KIND_MAGIC);
+	CHECK_OBJ_NOTNULL(sym->type, TYPE_MAGIC);
+	Fc(tl, 0, " * %-7s ", sym->kind->name);
 	Fc(tl, 0, " %-9s ", sym->type->name);
 	vcc_pnam(tl, sym);
 	if (sym->wildcard != NULL)


More information about the varnish-commit mailing list