aboutsummaryrefslogtreecommitdiff
path: root/scope.c
blob: 6c1d5fdba15a0bd7dd15130304918abbf2aff418 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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;
}