Clean up for/while/do parsing by separating them out
[smatch.git] / expression.h
blob529578a3c0fea02e97f461ed88a4f45d1a426ae3
1 #ifndef EXPRESSION_H
2 #define EXPRESSION_H
3 /*
4 * sparse/expression.h
6 * Copyright (C) 2003 Transmeta Corp.
8 * Licensed under the Open Software License version 1.1
10 * Declarations and helper functions for expression parsing.
13 struct expression_list;
15 enum expression_type {
16 EXPR_VALUE,
17 EXPR_STRING,
18 EXPR_SYMBOL,
19 EXPR_BINOP,
20 EXPR_ASSIGNMENT,
21 EXPR_LOGICAL,
22 EXPR_DEREF,
23 EXPR_PREOP,
24 EXPR_POSTOP,
25 EXPR_CAST,
26 EXPR_SIZEOF,
27 EXPR_CONDITIONAL,
28 EXPR_STATEMENT,
29 EXPR_CALL,
30 EXPR_COMMA,
31 EXPR_COMPARE,
32 EXPR_BITFIELD,
33 EXPR_INITIALIZER, // initializer list
34 EXPR_IDENTIFIER, // identifier in initializer
35 EXPR_INDEX, // index in initializer
36 EXPR_POS, // position in initializer
39 struct expression {
40 enum expression_type type;
41 int op;
42 struct position pos;
43 struct symbol *ctype;
44 union {
45 // EXPR_VALUE
46 unsigned long long value;
48 // EXPR_STRING
49 struct string *string;
51 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
52 struct expression *unop;
54 // EXPR_SYMBOL
55 struct symbol_arg {
56 struct symbol *symbol;
57 struct ident *symbol_name;
60 // EXPR_STATEMENT
61 struct statement *statement;
63 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
64 struct binop_arg {
65 struct expression *left, *right;
67 // EXPR_DEREF
68 struct deref_arg {
69 struct expression *deref;
70 struct ident *member;
72 // EXPR_CAST and EXPR_SIZEOF
73 struct cast_arg {
74 struct symbol *cast_type;
75 struct expression *cast_expression;
77 // EXPR_CONDITIONAL
78 struct conditional_expr {
79 struct expression *conditional, *cond_true, *cond_false;
81 // EXPR_CALL
82 struct call_expr {
83 struct expression *fn;
84 struct expression_list *args;
86 // EXPR_BITFIELD
87 struct bitfield_expr {
88 unsigned char bitpos, nrbits;
89 struct expression *address;
91 // EXPR_INITIALIZER
92 struct expression_list *expr_list;
93 // EXPR_IDENTIFIER
94 struct ident *expr_ident;
95 // EXPR_INDEX
96 struct index_expr {
97 unsigned int idx_from, idx_to;
99 // EXPR_POS
100 struct initpos_expr {
101 unsigned int init_offset;
102 struct symbol *init_sym;
103 struct expression *init_expr;
108 /* Constant expression values */
109 long long get_expression_value(struct expression *);
111 /* Expression parsing */
112 struct token *parse_expression(struct token *token, struct expression **tree);
113 struct token *conditional_expression(struct token *token, struct expression **tree);
114 struct token *primary_expression(struct token *token, struct expression **tree);
115 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
116 struct token *assignment_expression(struct token *token, struct expression **tree);
118 extern void check_duplicates(struct symbol *sym);
119 extern struct symbol *evaluate_symbol(struct symbol *sym);
120 extern struct symbol *evaluate_statement(struct statement *stmt);
121 extern struct symbol *evaluate_expression(struct expression *);
123 static inline struct expression *alloc_expression(struct position pos, int type)
125 struct expression *expr = __alloc_expression(0);
126 expr->type = type;
127 expr->pos = pos;
128 return expr;
131 /* Type name parsing */
132 struct token *typename(struct token *, struct symbol **);
134 static inline int lookup_type(struct token *token)
136 if (token->pos.type == TOKEN_IDENT)
137 return lookup_symbol(token->ident, NS_TYPEDEF) != NULL;
138 return 0;
141 /* Statement parsing */
142 struct statement *alloc_statement(struct position pos, int type);
143 struct token *initializer(struct expression **tree, struct token *token);
144 struct token *compound_statement(struct token *, struct statement *);
146 /* The preprocessor calls this 'constant_expression()' */
147 #define constant_expression(token,tree) conditional_expression(token, tree)
149 #endif