aboutsummaryrefslogtreecommitdiff
path: root/scope.c
diff options
context:
space:
mode:
authorMichael Forney <mforney@mforney.org>2019-02-11 18:43:18 -0800
committerMichael Forney <mforney@mforney.org>2019-02-12 01:55:14 -0800
commiteddc4693e49f70cd214b7645cb9fce54a89fbb6c (patch)
treefa1b640f49cde25e323aa0629aed64c064da930e /scope.c
Initial import
Diffstat (limited to 'scope.c')
-rw-r--r--scope.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/scope.c b/scope.c
new file mode 100644
index 0000000..6c1d5fd
--- /dev/null
+++ b/scope.c
@@ -0,0 +1,91 @@
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include "util.h"
+#include "htab.h"
+#include "scope.h"
+
+struct scope filescope;
+
+struct scope *
+mkscope(struct scope *parent)
+{
+ struct scope *s;
+
+ s = xmalloc(sizeof(*s));
+ s->decls = NULL;
+ s->tags = NULL;
+ s->breaklabel = parent->breaklabel;
+ s->continuelabel = parent->continuelabel;
+ s->switchcases = parent->switchcases;
+ s->parent = parent;
+
+ return s;
+}
+
+struct scope *
+delscope(struct scope *s)
+{
+ struct scope *parent = s->parent;
+
+ if (s->decls)
+ delhtab(s->decls, NULL);
+ if (s->tags)
+ delhtab(s->tags, NULL);
+ free(s);
+
+ return parent;
+}
+
+struct declaration *
+scopegetdecl(struct scope *s, const char *name, bool recurse)
+{
+ struct declaration *d;
+ struct hashtablekey k;
+
+ htabstrkey(&k, name);
+ do {
+ d = s->decls ? htabget(s->decls, &k) : NULL;
+ s = s->parent;
+ } while (!d && s && recurse);
+
+ return d;
+}
+
+struct type *
+scopegettag(struct scope *s, const char *name, bool recurse)
+{
+ struct type *t;
+ struct hashtablekey k;
+
+ htabstrkey(&k, name);
+ do {
+ t = s->tags ? htabget(s->tags, &k) : NULL;
+ s = s->parent;
+ } while (!t && s && recurse);
+
+ return t;
+}
+
+void
+scopeputdecl(struct scope *s, const char *name, struct declaration *d)
+{
+ struct hashtablekey k;
+
+ if (!s->decls)
+ s->decls = mkhtab(32);
+ htabstrkey(&k, name);
+ *htabput(s->decls, &k) = d;
+}
+
+void
+scopeputtag(struct scope *s, const char *name, struct type *t)
+{
+ struct hashtablekey k;
+
+ if (!s->tags)
+ s->tags = mkhtab(32);
+ htabstrkey(&k, name);
+ *htabput(s->tags, &k) = t;
+}