[PATCH] better recovery from type errors in EXPR_COMMA
[smatch.git] / expression.h
blob4c10db8e91940b7f08a537972e8b530bfbd1aee7
1 #ifndef EXPRESSION_H
2 #define EXPRESSION_H
3 /*
4 * sparse/expression.h
6 * Copyright (C) 2003 Transmeta Corp.
7 * 2003 Linus Torvalds
9 * Licensed under the Open Software License version 1.1
11 * Declarations and helper functions for expression parsing.
14 struct expression_list;
16 enum expression_type {
17 EXPR_VALUE,
18 EXPR_STRING,
19 EXPR_SYMBOL,
20 EXPR_TYPE,
21 EXPR_BINOP,
22 EXPR_ASSIGNMENT,
23 EXPR_LOGICAL,
24 EXPR_DEREF,
25 EXPR_PREOP,
26 EXPR_POSTOP,
27 EXPR_CAST,
28 EXPR_SIZEOF,
29 EXPR_ALIGNOF,
30 EXPR_CONDITIONAL,
31 EXPR_SELECT, // a "safe" conditional expression
32 EXPR_STATEMENT,
33 EXPR_CALL,
34 EXPR_COMMA,
35 EXPR_COMPARE,
36 EXPR_BITFIELD,
37 EXPR_LABEL,
38 EXPR_INITIALIZER, // initializer list
39 EXPR_IDENTIFIER, // identifier in initializer
40 EXPR_INDEX, // index in initializer
41 EXPR_POS, // position in initializer
42 EXPR_FVALUE,
43 EXPR_SLICE,
46 struct expression {
47 enum expression_type type;
48 int op;
49 struct position pos;
50 struct symbol *ctype;
51 union {
52 // EXPR_VALUE
53 unsigned long long value;
55 // EXPR_FVALUE
56 long double fvalue;
58 // EXPR_STRING
59 struct string *string;
61 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
62 struct expression *unop;
64 // EXPR_SYMBOL, EXPR_TYPE
65 struct /* symbol_arg */ {
66 struct symbol *symbol;
67 struct ident *symbol_name;
70 // EXPR_STATEMENT
71 struct statement *statement;
73 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
74 struct /* binop_arg */ {
75 struct expression *left, *right;
77 // EXPR_DEREF
78 struct /* deref_arg */ {
79 struct expression *deref;
80 struct ident *member;
82 // EXPR_SLICE
83 struct /* slice */ {
84 struct expression *base;
85 unsigned r_bitpos, r_nrbits;
87 // EXPR_CAST and EXPR_SIZEOF
88 struct /* cast_arg */ {
89 struct symbol *cast_type;
90 struct expression *cast_expression;
92 // EXPR_CONDITIONAL
93 // EXPR_SELECT
94 struct /* conditional_expr */ {
95 struct expression *conditional, *cond_true, *cond_false;
97 // EXPR_CALL
98 struct /* call_expr */ {
99 struct expression *fn;
100 struct expression_list *args;
102 // EXPR_BITFIELD
103 struct /* bitfield_expr */ {
104 unsigned char bitpos, nrbits;
105 struct expression *address;
107 // EXPR_LABEL
108 struct /* label_expr */ {
109 struct symbol *label_symbol;
111 // EXPR_INITIALIZER
112 struct expression_list *expr_list;
113 // EXPR_IDENTIFIER
114 struct ident *expr_ident;
115 // EXPR_INDEX
116 struct /* index_expr */ {
117 unsigned int idx_from, idx_to;
119 // EXPR_POS
120 struct /* initpos_expr */ {
121 unsigned int init_offset;
122 struct symbol *init_sym;
123 struct expression *init_expr;
128 /* Constant expression values */
129 long long get_expression_value(struct expression *);
131 /* Expression parsing */
132 struct token *parse_expression(struct token *token, struct expression **tree);
133 struct token *conditional_expression(struct token *token, struct expression **tree);
134 struct token *primary_expression(struct token *token, struct expression **tree);
135 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
136 struct token *assignment_expression(struct token *token, struct expression **tree);
138 extern void check_duplicates(struct symbol *sym);
139 extern struct symbol *evaluate_symbol(struct symbol *sym);
140 extern struct symbol *evaluate_statement(struct statement *stmt);
141 extern struct symbol *evaluate_expression(struct expression *);
143 extern void expand_symbol(struct symbol *);
145 static inline struct expression *alloc_expression(struct position pos, int type)
147 struct expression *expr = __alloc_expression(0);
148 expr->type = type;
149 expr->pos = pos;
150 return expr;
153 static inline struct expression *alloc_const_expression(struct position pos, int value)
155 struct expression *expr = __alloc_expression(0);
156 expr->type = EXPR_VALUE;
157 expr->pos = pos;
158 expr->value = value;
159 expr->ctype = &int_ctype;
160 return expr;
163 /* Type name parsing */
164 struct token *typename(struct token *, struct symbol **);
166 static inline int lookup_type(struct token *token)
168 if (token->pos.type == TOKEN_IDENT) {
169 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
170 return sym && sym->namespace == NS_TYPEDEF;
172 return 0;
175 /* Statement parsing */
176 struct statement *alloc_statement(struct position pos, int type);
177 struct token *initializer(struct expression **tree, struct token *token);
178 struct token *compound_statement(struct token *, struct statement *);
180 /* The preprocessor calls this 'constant_expression()' */
181 #define constant_expression(token,tree) conditional_expression(token, tree)
183 /* Cast folding of constant values.. */
184 void cast_value(struct expression *expr, struct symbol *newtype,
185 struct expression *old, struct symbol *oldtype);
187 #endif