-
Notifications
You must be signed in to change notification settings - Fork 0
/
declare.c
186 lines (175 loc) · 4.33 KB
/
declare.c
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "ccc.h"
/*
* we are in a parse state where we want to process declarations.
* any names and types we declare go into the current scope
*/
struct name *
declare(struct type **btp)
{
struct name *nm, *arg;
struct type *t, *prefix, *suffix, *rt;
int i;
nm = 0;
/*
* this will be primitive, enum, struct/union
*/
t = getbasetype();
if (t && *btp) {
err(ER_T_DT);
t = 0;
}
if (t) {
*btp = t;
}
prefix = *btp;
while (cur.type == STAR) {
gettoken();
prefix = get_type(TF_POINTER, prefix, 0, 0);
}
// parenthesed type definition does precedence
if (cur.type == LPAR) {
gettoken();
rt = 0;
nm = declare(&rt); // recurse
need(RPAR, RPAR, ER_D_DP);
if (*btp && rt) {
err(ER_T_DT);
rt = 0;
}
if (rt && !nm) {
*btp = rt;
}
}
if (cur.type == RPAR) {
if (!nm) {
for (t = prefix; t && t->sub; t = t->sub) {
if (t) {
t->sub = *btp;
*btp = prefix;
}
}
}
return nm;
}
if (cur.type == SYM) { // symbol name
if (nm) {
err(ER_D_MV);
}
nm = new_name(strdup(strbuf), var, prefix, 0);
gettoken();
if (cur.type == COLON) {
gettoken();
if (cur.type != NUMBER) {
err(ER_D_BD);
} else if (cur.v.numeric > MAXBITS) {
err(ER_D_BM);
} else {
nm->flags |= V_BITFIELD;
nm->width = cur.v.numeric;
}
gettoken();
}
}
while (cur.type == LBRACK) { // array
gettoken();
if (cur.type == RBRACK) {
i = -1;
} else {
i = parse_const();
}
t = get_type(TF_ARRAY, t, 0, i);
need(RBRACK, RBRACK, ER_D_AD);
}
if (cur.type == LPAR) { // ( <func_arg>[,<func_arg>]*. )
gettoken();
if (suffix) {
err(ER_D_FA);
suffix = 0;
}
suffix = get_type(0, func, 0);
while (cur.type != RPAR) {
#ifdef notdef
freetype(t);
t = v;
#endif
a = declare(&t);
if (a) {
a->next = suffix->elem;
suffix->elem = a;
a->flags |= V_FUNARG | V_LOCAL;
}
if (cur.type == COMMA) {
gettoken();
continue;
}
if (cur.type != RPAR) {
err(ER_P_FA);
break;
}
}
gettoken();
#ifdef notdef
/*
* old style parameter declarartion:
* foo(a,b) int a; int b;
*/
if ((cur.type != BEGIN) && (cur.type != SEMI)) {
if (t) {
freetype(t);
}
t = 0;
while (cur.type != BEGIN) {
a = declare(&t);
if (!a) {
err(ER_P_FM);
break;
}
b = elementvar(a->name, suffix);
if (b->type) {
err(ER_P_FO);
}
b->type = a->type;
if (cur.type == COMMA) {
gettoken();
continue;
}
if (cur.type == SEMI) {
freetype(t);
t = 0;
gettoken();
continue;
}
err(ER_P_FM);
break;
}
}
assign_arg_off(suffix, 4);
#endif
} // if cur.type == LPAR
if ((cur.type != ASSIGN) && (cur.type != BEGIN) &&
(cur.type != COMMA) && (cur.type != SEMI)) {
printf("token: %d 0x%x '%c'\n", cur.type, cur.type, cur.type);
err(ER_D_UT);
nm = 0;
}
if (!v) {
return 0;
}
/*
* prepend the prefix and append the suffix
*/
t = v->type;
t = rt;
while (t && t->sub) {
t = t->sub;
}
t = suffix;
while (t && t->sub) {
t = t->sub;
}
t = prefix;
return v;
} // declare
/*
* vim: tabstop=4 shiftwidth=4 expandtab:
*/