9 #include "adt/bitfiddle.h"
19 lexer_next_token(&lexer, &token);
22 print_token(stderr, &token);
23 fprintf(stderr, "\n");
28 void eat(token_type_t type)
30 assert(token.type == type);
34 void parser_print_error_prefix()
36 fputs(lexer.source_position.input_name, stderr);
38 fprintf(stderr, "%d", lexer.source_position.linenr);
39 fputs(": error: ", stderr);
43 void parse_error(const char *message)
45 parser_print_error_prefix();
46 fprintf(stderr, "parse error: %s\n", message);
49 #define expect(expected) \
50 if(UNLIKELY(token.type != (expected))) { \
51 /*parse_error_expected(NULL, (expected), 0);*/ \
52 /*eat_until_semi();*/ \
58 SPECIFIER_SIGNED = 1 << 0,
59 SPECIFIER_UNSIGNED = 1 << 1,
60 SPECIFIER_LONG = 1 << 2,
61 SPECIFIER_INT = 1 << 3,
62 SPECIFIER_DOUBLE = 1 << 4,
63 SPECIFIER_CHAR = 1 << 5,
64 SPECIFIER_SHORT = 1 << 6,
65 SPECIFIER_LONG_LONG = 1 << 7,
66 SPECIFIER_FLOAT = 1 << 8,
67 SPECIFIER_BOOL = 1 << 9,
68 SPECIFIER_VOID = 1 << 10,
69 #ifdef PROVIDE_COMPLEX
70 SPECIFIER_COMPLEX = 1 << 11,
72 #ifdef PROVIDE_IMAGINARY
73 SPECIFIER_IMAGINARY = 1 << 12,
78 TYPE_QUALIFIER_CONST = 1 << 0,
79 TYPE_QUALIFIER_RESTRICT = 1 << 1,
80 TYPE_QUALIFIER_VOLATILE = 1 << 2,
81 TYPE_QUALIFIER_INLINE = 1 << 3,
86 STORAGE_CLASS_TYPEDEF,
90 STORAGE_CLASS_REGISTER
93 typedef struct declaration_specifiers_t declaration_specifiers_t;
94 struct declaration_specifiers_t {
95 storage_class_t storage_class;
100 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
102 type_type_t type_type = TYPE_INVALID;
103 atomic_type_type_t atomic_type = ATOMIC_TYPE_INVALID;
104 unsigned type_specifiers = 0;
110 #define MATCH_STORAGE_CLASS(token, class) \
112 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
113 parse_error("multiple storage classes in declaration " \
116 specifiers->storage_class = class; \
120 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
121 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
122 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
123 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
124 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
126 /* type qualifiers */
127 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
129 specifiers->type_qualifiers |= qualifier; \
133 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
134 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
135 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
136 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
138 /* type specifiers */
139 #define MATCH_SPECIFIER(token, specifier, name) \
142 if(type_specifiers & specifier) { \
143 parse_error("multiple " name " type specifiers given"); \
145 type_specifiers |= specifier; \
149 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
150 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
151 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
152 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
153 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
154 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
155 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
156 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
157 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
158 #ifdef PROVIDE_COMPLEX
159 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
161 #ifdef PROVIDE_IMAGINARY
162 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
166 if(type_specifiers & SPECIFIER_LONG_LONG) {
167 parse_error("too many long type specifiers given");
168 } else if(type_specifiers & SPECIFIER_LONG) {
169 type_specifiers |= SPECIFIER_LONG_LONG;
171 type_specifiers |= SPECIFIER_LONG;
175 /* struct or union specifier */
179 /* function specifier */
185 if(type_type == TYPE_INVALID) {
186 /* match valid basic types */
187 switch(type_specifiers) {
189 atomic_type = ATOMIC_TYPE_VOID;
192 atomic_type = ATOMIC_TYPE_CHAR;
194 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
195 atomic_type = ATOMIC_TYPE_SCHAR;
197 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
198 atomic_type = ATOMIC_TYPE_UCHAR;
200 case SPECIFIER_SHORT:
201 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
202 case SPECIFIER_SHORT | SPECIFIER_INT:
203 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
204 atomic_type = ATOMIC_TYPE_SHORT;
206 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
207 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
208 atomic_type = ATOMIC_TYPE_USHORT;
211 case SPECIFIER_SIGNED:
212 case SPECIFIER_SIGNED | SPECIFIER_INT:
213 atomic_type = ATOMIC_TYPE_INT;
215 case SPECIFIER_UNSIGNED:
216 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
217 atomic_type = ATOMIC_TYPE_UINT;
220 case SPECIFIER_SIGNED | SPECIFIER_LONG:
221 case SPECIFIER_LONG | SPECIFIER_INT:
222 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
223 atomic_type = ATOMIC_TYPE_LONG;
225 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
226 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
227 atomic_type = ATOMIC_TYPE_ULONG;
229 case SPECIFIER_LONG_LONG:
230 case SPECIFIER_SIGNED | SPECIFIER_LONG_LONG:
231 case SPECIFIER_LONG_LONG | SPECIFIER_INT:
232 case SPECIFIER_SIGNED | SPECIFIER_LONG_LONG | SPECIFIER_INT:
233 atomic_type = ATOMIC_TYPE_LONGLONG;
235 case SPECIFIER_UNSIGNED | SPECIFIER_LONG_LONG:
236 case SPECIFIER_UNSIGNED | SPECIFIER_LONG_LONG | SPECIFIER_INT:
237 atomic_type = ATOMIC_TYPE_ULONGLONG;
239 case SPECIFIER_FLOAT:
240 atomic_type = ATOMIC_TYPE_FLOAT;
242 case SPECIFIER_DOUBLE:
243 atomic_type = ATOMIC_TYPE_DOUBLE;
245 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
246 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
249 atomic_type = ATOMIC_TYPE_BOOL;
251 #ifdef PROVIDE_COMPLEX
252 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
253 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
255 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
256 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
258 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
259 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
262 #ifdef PROVIDE_IMAGINARY
263 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
264 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
266 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
267 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
269 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
270 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
274 /* invalid specifier combination, give an error message */
275 if(type_specifiers == 0) {
276 parse_error("no type specifiers given in declaration");
277 } else if((type_specifiers & SPECIFIER_SIGNED) &&
278 (type_specifiers & SPECIFIER_UNSIGNED)) {
279 parse_error("signed and unsigned specifiers gives");
280 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
281 parse_error("only integer types can be signed or unsigned");
283 parse_error("multiple datatypes in declaration");
287 if(type_specifiers != 0) {
288 parse_error("multiple datatypes in declaration");
293 typedef struct declarator_t declarator_t;
294 struct declarator_t {
295 /* pointer stuff... */
301 declarator_t *parse_declarator()
303 while(token.type == '*') {
306 //parse_type_qualifiers();
309 declarator_t *declarator;
313 declarator = allocate_ast(sizeof(declarator[0]));
314 memset(declarator, 0, sizeof(declarator[0]));
315 declarator->symbol = token.v.symbol;
319 declarator = parse_declarator();
323 parse_error("problem while parsing declarator");
326 if(token.type == '(') {
329 /* parse parameter-type-list or identifier-list */
332 } else if(token.type == '[') {
335 /* multiple type qualifiers, and static */
337 /* assignment_expression or '*' or nothing */
345 declarator_t *parse_init_declarator()
347 declarator_t *declarator = parse_declarator();
348 if(token.type == '=') {
350 //parse_initialize();
356 typedef struct declaration_t declaration_t;
357 struct declaration_t {
358 declaration_specifiers_t specifiers;
359 declaration_t *declarators;
362 void parse_declaration()
364 declaration_specifiers_t specifiers;
365 memset(&specifiers, 0, sizeof(specifiers));
366 parse_declaration_specifiers(&specifiers);
370 namespace_t *parse(FILE *in, const char *input_name)
372 namespace_t *namespace = parse_namespace();