12 #define YYLTYPE struct location
20 /* memory management */
21 void *xmalloc(size_t n
);
22 void *xrealloc(void *ptr
, size_t n
);
23 void *xmalloc_noptr(size_t n
);
24 void *xrealloc_noptr(void *ptr
, size_t n
);
25 char *xstrdup(const char *s
);
26 char *xstrndup(const char *s
, size_t n
);
27 #define NEW(p) ((p) = xmalloc(sizeof *(p)))
30 extern void (*exiter
)(int); /* called by fatal(), normally = exit(3) */
31 void fatal(int errno_value
, const char *msg
, ...);
32 void error(int errno_value
, const char *msg
, ...);
35 struct dict
*dict_new(void);
36 int dict_add(struct dict
*d
, const char *key
, void *data
);
37 void *dict_get(struct dict
*d
, const char *key
);
39 /* scanner/parser stuff */
40 int commandline(int argc
, char **argv
);
41 struct external_declaration
*parse(const char *filename
);
44 int yyerror(const char *msg
);
46 extern int yy_flex_debug
;
48 extern YYLTYPE yylloc
;
49 void parser_init(FILE *fp
);
50 struct declarator
*lookup(const char *name
);
52 struct declarator
*numbertype(const struct expression
*e
);
53 /* return the type for a constant */
55 void inputerror(const struct location
*where
, const char *msg
, ...);
56 enum warning_category
{
57 warn_obsolete
, /* obsolete constructs */
58 warn_compat
/* gratuitously incompatible
61 void inputwarning(const struct location
*where
, enum warning_category cat
,
62 const char *msg
, ...);
63 void suppress_errors(void);
64 void restore_errors(void);
67 #ifndef DECLARE_EXTERN
68 # define DECLARE_EXTERN extern
70 DECLARE_EXTERN
int line
;
71 DECLARE_EXTERN
const char *path
;
72 DECLARE_EXTERN
struct external_declaration
*translation_unit
;
73 DECLARE_EXTERN
int errors
;
74 DECLARE_EXTERN
int constant_string_literals
;
75 extern const char *cpp
;
77 /* various parsed things */
79 enum external_declaration_type
{
81 ed_function_definition
84 struct external_declaration
{
85 struct external_declaration
*next
;
86 enum external_declaration_type type
;
88 struct declaration
*declaration
;
89 struct function_definition
*function_definition
;
93 struct function_definition
{
94 struct declaration
*declaration
; /* function name + type */
95 struct declaration
*args
; /* old-style args */
96 struct statement
*body
; /* function body */
100 struct enumerator
*next
;
102 struct expression
*value
; /* or a null pointer */
103 struct location where
;
104 struct declarator
*declarator
; /* saved declarator */
107 struct declaration_specifiers
{
108 unsigned long type_specifiers
;
109 #define TS_VOID 0x00000001
110 #define TS_BOOL 0x00000002
111 #define TS_CHAR 0x00000003
112 #define TS_INT 0x00000004
113 #define TS_ENUM 0x00000005
114 #define TS_TYPEDEF 0x00000006
115 #define TS_FLOAT 0x00000007
116 #define TS_DOUBLE 0x00000008
117 #define TS_STRUCT 0x00000009
118 #define TS_UNION 0x0000000A
119 #define TS_GCC_VA_LIST 0x0000000B
121 #define TS__BASIC 0x000000FF
123 #define TS_COMPLEX 0x00000100
124 #define TS_IMAGINARY 0x00000200
125 #define TS__CI 0x00000300
127 #define TS_LONG 0x00000400
128 #define TS_SHORT 0x00000800
129 #define TS_LONGLONG 0x00000C00
130 #define TS__LENGTH 0x00000C00
132 #define TS_SIGNED 0x00001000
133 #define TS_UNSIGNED 0x00002000
134 #define TS__SIGN 0x00003000
136 #define TS_DEFINITION 0x80000000
137 unsigned storage_class_specifiers
;
138 #define SCS_TYPEDEF 0x0001
139 #define SCS_EXTERN 0x0002
140 #define SCS_STATIC 0x0004
141 #define SCS_AUTO 0x0008
142 #define SCS_REGISTER 0x0010
143 unsigned type_qualifiers
;
144 #define TQ_CONST 0x0001
145 #define TQ_RESTRICT 0x0002
146 #define TQ_VOLATILE 0x0004
147 unsigned function_specifiers
;
148 #define FS_INLINE 0x0001
149 /* XXX name is scoped, really */
150 char *name
; /* of enum, struct, union or typedef */
151 struct declaration
*structure
; /* struct/union */
152 struct enumerator
*enumerators
; /* enum */
153 struct declarator
*type
; /* typedef */
154 unsigned long enum_compat_type
;
158 struct declarator
*next
; /* next declarator in list */
159 char *name
; /* name in middle of declarator */
160 struct expression
*bits
; /* for bitfields */
161 struct declarator_type
*declarator_type
; /* type of declarator */
162 struct declaration_specifiers
*declaration_specifiers
;
163 struct initializer
*initializer
;
164 struct location where
;
166 #define DF_PARAM 0x0001 /* is a parameter */
167 struct declarator
*resolved
; /* resolved for typedefs */
170 enum declarator_type_type
{
177 struct declarator_type
{
178 enum declarator_type_type type
;
179 unsigned type_qualifiers
; /* TQ_... */
180 unsigned storage_class_specifiers
; /* SCS_STATIC or 0 */
181 struct declarator_type
*next
; /* return type, element type, etc */
184 struct expression
*size
;
186 #define AF_STAR 0x0001
189 struct identifier_list
*args
; /* arguments (no types) */
192 struct declaration
*args
; /* arguments (one declarator per
194 int variadic
; /* true if ", ..." */
197 struct location where
;
200 struct identifier_list
{
201 struct identifier_list
*next
;
206 struct declaration
*next
;
207 struct declaration_specifiers
*declaration_specifiers
;
208 struct declarator
*declarator_list
;
209 struct location where
;
212 enum expression_type
{
227 ex_implicit_conversion
,
233 enum expression_type type
;
235 struct declarator
*valuetype
;
238 struct expression
*l
, *r
;
240 struct expression
*unary
;
244 struct expression
*fn
;
245 struct expression_list
*args
;
248 struct expression
*arg
;
249 struct declaration
*type
;
252 struct declaration
*type
;
253 struct initializer
*value
;
255 struct declaration
*type
;
256 struct expression
*cast
; /* target type is valuetype */
258 struct location where
;
261 struct expression_list
{
262 struct expression_list
*next
;
263 struct expression
*e
;
266 enum initializer_type
{
271 enum designator_syntax
{
278 struct initializer
*next
;
279 enum initializer_type type
;
280 struct designator
*designator
;
282 struct expression
*expr
;
283 struct initializer
*list
;
285 enum designator_syntax syntax
;
288 enum designator_type
{
294 enum designator_type type
;
296 struct expression
*expr
;
299 struct designator
*next
;
300 struct location where
;
303 enum statement_type
{
323 struct statement
*next
;
324 enum statement_type type
;
326 char *goto_
; /* goto */
329 struct statement
*body
;
332 struct expression
*value
;
333 struct statement
*body
;
335 struct statement
*default_
;
337 struct statement
*body
;
338 struct location endwhere
; /* closing } */
341 struct declaration
*declaration
;
342 struct expression
*expression
; /* or a null pointer; also return */
344 struct expression
*cond
;
345 struct statement
*true;
346 struct statement
*false;
349 struct expression
*cond
;
350 struct statement
*body
;
353 struct expression
*cond
;
354 struct statement
*body
;
355 } while_
; /* including do-while */
357 struct expression
*init
;
358 struct expression
*cond
;
359 struct expression
*iter
;
360 struct statement
*body
;
363 struct declaration
*init
;
364 struct expression
*cond
;
365 struct expression
*iter
;
366 struct statement
*body
;
369 struct location where
;
372 /* dump.c ********************************************************************/
374 typedef struct dump_state dump_state
;
376 dump_state
*dump_new(FILE *fp
);
378 int dump_declaration_specifiers(dump_state
*dump
,
379 const struct declaration_specifiers
*ds
,
381 int dump_declarator(dump_state
*dump
,
382 const struct declarator
*d
,
384 int dump_declaration(dump_state
*dump
,
385 const struct declaration
*d
,
387 int dump_translation_unit(dump_state
*dump
,
388 const struct external_declaration
*ed
,
390 int dump_expression(dump_state
*dump
,
391 const struct expression
*e
,
393 int dump_initializer(dump_state
*dump
,
394 const struct initializer
*i
,
396 int dump_function_definition(dump_state
*dump
,
397 const struct function_definition
*fd
,
399 int dump_statement(dump_state
*dump
,
400 const struct statement
*fd
,
402 void dump_locations(dump_state
*dump
, int flag
);
404 /* type.c ********************************************************************/
406 struct declaration_specifiers
*merge_declaration_specifiers
407 (struct declaration_specifiers
*a
,
408 struct declaration_specifiers
*b
);
409 /* combine two declaration_specifires structs that are bits of the same list of
410 * declaration specifiers, checking relevant constraints on the way */
412 int is_integral_type(const struct declarator
*d
);
413 /* return true if D has integral type. typedefs should have been resolved. */
415 int is_arithmetic_type(const struct declarator
*d
);
416 /* return true if D has arithmetic type. typedefs should have been
419 int is_pointer_type(const struct declarator
*d
);
420 /* return true if D has pointer type. typedefs should have been resolved. */
422 int is_scalar_type(const struct declarator
*d
);
423 /* return true if D has scalar type. typedefs should have been resolved. */
425 struct declarator
*resolve_typedefs(struct declarator
*d
);
426 /* resolve typedefs in value types */
428 struct declarator
*target_type(struct declarator
*d
);
429 /* return (as a declarator) the target type of an array or whatever. Usually
430 * only makes sense after typedefs have been resolved. */
432 size_t width(unsigned long ts
);
433 /* return the width of type ts */
435 /* expr.c ********************************************************************/
437 struct expression
*array_decay(struct expression
*e
);
438 /* array-to-pointer decay (C99 6.3.2.1) */
440 struct expression
*integer_promotions(struct expression
*e
);
441 /* integral promotions (C99 6.3.1.1)
442 * anything >= (unsigned) int stays as it is
443 * otherwise if int is good enough we use that
444 * otherwise we use unsigned
447 struct declarator
*usual_arithmetic_conversions(struct expression
**lp
,
448 struct expression
**rp
,
449 const struct location
*where
);
450 /* perform usual arithmetic conversions, return the shared value type */
452 struct expression
*expr(enum expression_type type
,
453 const struct location
*where
);
454 struct expression
*binary(int op
,
455 struct expression
*l
,
456 struct expression
*r
,
457 const struct location
*where
);
458 struct expression
*prefix(int op
, struct expression
*r
,
459 const struct location
*where
);
460 struct expression
*postfix(int op
, struct expression
*l
,
461 const struct location
*where
);
462 struct expression
*paren(struct expression
*content
,
463 const struct location
*where
);
464 struct expression
*stringexpr(enum expression_type type
,
466 const struct location
*where
);
467 struct expression
*fncallexpr(struct expression
*f
,
468 struct expression_list
*l
,
469 const struct location
*where
);
470 /* various expression constructors */
472 /* constraints.c *************************************************************/
474 void ucn_constraints(unsigned long u
);
476 enum declarator_context
{
477 dc_function_definition
,
478 dc_function_parameter
,
479 dc_function_definition_parameter
,
494 enum type_category
get_type_category(const struct declaration_specifiers
*ds
,
495 const struct declarator_type
*dt
);
496 int is_void_args(const struct declarator_type
*dt
);
497 void declarator_constraints(const struct declaration_specifiers
*ds
,
498 const struct declarator
*d
,
499 enum declarator_context context
,
501 void declaration_constraints(const struct declaration
*decl
,
502 enum declarator_context context
);
504 /* scope.c *******************************************************************/
507 struct scope
*parent
; /* containing scope */
508 struct dict
*declarations
; /* declarations in this scope */
511 void enter_scope(void);
512 /* enter a new scope */
514 void exit_scope(void);
515 /* exit the current scope */
517 void add_declaration(struct declarator
*d
);
518 /* add a declaration to the current scope. The declaration specifiers need not
519 * have been filled in yet but must get filled in eventually. */
521 struct declarator
*lookup(const char *name
);
522 /* look up a name, starting in the current scope and working back up to file
525 void scope_init(void);
526 /* initialize scope support before parsing a file. After calling this you are
529 /* gcc.c *********************************************************************/
531 void gcc_extensions(void);
532 /* enable various gcc-isms */
534 /*****************************************************************************/
536 #include "platform.h"
538 #endif /* CPARSE_H */