handle assignments like: foo = x < 1;
[smatch.git] / expression.h
blob631224fd1225fdff5482602603b10f89e9b0eaba
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 #include "allocate.h"
15 #include "lib.h"
16 #include "symbol.h"
18 struct expression_list;
20 enum expression_type {
21 EXPR_VALUE = 1,
22 EXPR_STRING,
23 EXPR_SYMBOL,
24 EXPR_TYPE,
25 EXPR_BINOP,
26 EXPR_ASSIGNMENT,
27 EXPR_LOGICAL,
28 EXPR_DEREF,
29 EXPR_PREOP,
30 EXPR_POSTOP,
31 EXPR_CAST,
32 EXPR_FORCE_CAST,
33 EXPR_IMPLIED_CAST,
34 EXPR_SIZEOF,
35 EXPR_ALIGNOF,
36 EXPR_PTRSIZEOF,
37 EXPR_CONDITIONAL,
38 EXPR_SELECT, // a "safe" conditional expression
39 EXPR_STATEMENT,
40 EXPR_CALL,
41 EXPR_COMMA,
42 EXPR_COMPARE,
43 EXPR_LABEL,
44 EXPR_INITIALIZER, // initializer list
45 EXPR_IDENTIFIER, // identifier in initializer
46 EXPR_INDEX, // index in initializer
47 EXPR_POS, // position in initializer
48 EXPR_FVALUE,
49 EXPR_SLICE,
50 EXPR_OFFSETOF,
53 enum {
54 Int_const_expr = 1,
55 Float_literal = 2,
56 }; /* for expr->flags */
58 enum {
59 Taint_comma = 1,
60 }; /* for expr->taint */
62 struct expression {
63 enum expression_type type:8;
64 unsigned flags:8;
65 int op;
66 struct position pos;
67 struct symbol *ctype;
68 union {
69 // EXPR_VALUE
70 struct {
71 unsigned long long value;
72 unsigned taint;
75 // EXPR_FVALUE
76 long double fvalue;
78 // EXPR_STRING
79 struct string *string;
81 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
82 struct /* unop */ {
83 struct expression *unop;
84 unsigned long op_value;
87 // EXPR_SYMBOL, EXPR_TYPE
88 struct /* symbol_arg */ {
89 struct symbol *symbol;
90 struct ident *symbol_name;
93 // EXPR_STATEMENT
94 struct statement *statement;
96 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
97 struct /* binop_arg */ {
98 struct expression *left, *right;
100 // EXPR_DEREF
101 struct /* deref_arg */ {
102 struct expression *deref;
103 struct ident *member;
105 // EXPR_SLICE
106 struct /* slice */ {
107 struct expression *base;
108 unsigned r_bitpos, r_nrbits;
110 // EXPR_CAST and EXPR_SIZEOF
111 struct /* cast_arg */ {
112 struct symbol *cast_type;
113 struct expression *cast_expression;
115 // EXPR_CONDITIONAL
116 // EXPR_SELECT
117 struct /* conditional_expr */ {
118 struct expression *conditional, *cond_true, *cond_false;
120 // EXPR_CALL
121 struct /* call_expr */ {
122 struct expression *fn;
123 struct expression_list *args;
125 // EXPR_LABEL
126 struct /* label_expr */ {
127 struct symbol *label_symbol;
129 // EXPR_INITIALIZER
130 struct expression_list *expr_list;
131 // EXPR_IDENTIFIER
132 struct /* ident_expr */ {
133 struct ident *expr_ident;
134 struct symbol *field;
135 struct expression *ident_expression;
137 // EXPR_INDEX
138 struct /* index_expr */ {
139 unsigned int idx_from, idx_to;
140 struct expression *idx_expression;
142 // EXPR_POS
143 struct /* initpos_expr */ {
144 unsigned int init_offset, init_nr;
145 struct expression *init_expr;
147 // EXPR_OFFSETOF
148 struct {
149 struct symbol *in;
150 struct expression *down;
151 union {
152 struct ident *ident;
153 struct expression *index;
159 /* Constant expression values */
160 int is_zero_constant(struct expression *);
161 long long get_expression_value(struct expression *);
162 long long const_expression_value(struct expression *);
164 /* Expression parsing */
165 struct token *parse_expression(struct token *token, struct expression **tree);
166 struct token *conditional_expression(struct token *token, struct expression **tree);
167 struct token *primary_expression(struct token *token, struct expression **tree);
168 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
169 struct token *assignment_expression(struct token *token, struct expression **tree);
171 extern void evaluate_symbol_list(struct symbol_list *list);
172 extern struct symbol *evaluate_statement(struct statement *stmt);
173 extern struct symbol *evaluate_expression(struct expression *);
175 extern int expand_symbol(struct symbol *);
177 static inline struct expression *alloc_expression(struct position pos, int type)
179 struct expression *expr = __alloc_expression(0);
180 expr->type = type;
181 expr->pos = pos;
182 return expr;
185 static inline struct expression *alloc_const_expression(struct position pos, int value)
187 struct expression *expr = __alloc_expression(0);
188 expr->type = EXPR_VALUE;
189 expr->pos = pos;
190 expr->value = value;
191 expr->ctype = &int_ctype;
192 return expr;
195 /* Type name parsing */
196 struct token *typename(struct token *, struct symbol **, int *);
198 static inline int lookup_type(struct token *token)
200 if (token->pos.type == TOKEN_IDENT) {
201 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
202 return sym && (sym->namespace & NS_TYPEDEF);
204 return 0;
207 /* Statement parsing */
208 struct statement *alloc_statement(struct position pos, int type);
209 struct token *initializer(struct expression **tree, struct token *token);
210 struct token *compound_statement(struct token *, struct statement *);
212 /* The preprocessor calls this 'constant_expression()' */
213 #define constant_expression(token,tree) conditional_expression(token, tree)
215 /* Cast folding of constant values.. */
216 void cast_value(struct expression *expr, struct symbol *newtype,
217 struct expression *old, struct symbol *oldtype);
219 #endif