math: handle logical expressions
[smatch.git] / expression.h
blobda6ccac9cda2ab88e7913e6174e6c66997adb4e5
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 Handled = 1,
60 }; /* for expr->flags */
62 enum {
63 Taint_comma = 1,
64 }; /* for expr->taint */
66 struct expression {
67 enum expression_type type:8;
68 unsigned flags:8;
69 unsigned smatch_flags:16;
70 int op;
71 struct position pos;
72 struct symbol *ctype;
73 union {
74 // EXPR_VALUE
75 struct {
76 unsigned long long value;
77 unsigned taint;
80 // EXPR_FVALUE
81 long double fvalue;
83 // EXPR_STRING
84 struct {
85 int wide;
86 struct string *string;
89 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
90 struct /* unop */ {
91 struct expression *unop;
92 unsigned long op_value;
95 // EXPR_SYMBOL, EXPR_TYPE
96 struct /* symbol_arg */ {
97 struct symbol *symbol;
98 struct ident *symbol_name;
101 // EXPR_STATEMENT
102 struct statement *statement;
104 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
105 struct /* binop_arg */ {
106 struct expression *left, *right;
108 // EXPR_DEREF
109 struct /* deref_arg */ {
110 struct expression *deref;
111 struct ident *member;
113 // EXPR_SLICE
114 struct /* slice */ {
115 struct expression *base;
116 unsigned r_bitpos, r_nrbits;
118 // EXPR_CAST and EXPR_SIZEOF
119 struct /* cast_arg */ {
120 struct symbol *cast_type;
121 struct expression *cast_expression;
123 // EXPR_CONDITIONAL
124 // EXPR_SELECT
125 struct /* conditional_expr */ {
126 struct expression *conditional, *cond_true, *cond_false;
128 // EXPR_CALL
129 struct /* call_expr */ {
130 struct expression *fn;
131 struct expression_list *args;
133 // EXPR_LABEL
134 struct /* label_expr */ {
135 struct symbol *label_symbol;
137 // EXPR_INITIALIZER
138 struct expression_list *expr_list;
139 // EXPR_IDENTIFIER
140 struct /* ident_expr */ {
141 struct ident *expr_ident;
142 struct symbol *field;
143 struct expression *ident_expression;
145 // EXPR_INDEX
146 struct /* index_expr */ {
147 unsigned int idx_from, idx_to;
148 struct expression *idx_expression;
150 // EXPR_POS
151 struct /* initpos_expr */ {
152 unsigned int init_offset, init_nr;
153 struct expression *init_expr;
155 // EXPR_OFFSETOF
156 struct {
157 struct symbol *in;
158 struct expression *down;
159 union {
160 struct ident *ident;
161 struct expression *index;
167 /* Constant expression values */
168 int is_zero_constant(struct expression *);
169 long long get_expression_value(struct expression *);
170 long long const_expression_value(struct expression *);
172 /* Expression parsing */
173 struct token *parse_expression(struct token *token, struct expression **tree);
174 struct token *conditional_expression(struct token *token, struct expression **tree);
175 struct token *primary_expression(struct token *token, struct expression **tree);
176 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
177 struct token *assignment_expression(struct token *token, struct expression **tree);
179 extern void evaluate_symbol_list(struct symbol_list *list);
180 extern struct symbol *evaluate_statement(struct statement *stmt);
181 extern struct symbol *evaluate_expression(struct expression *);
183 extern int expand_symbol(struct symbol *);
185 static inline struct expression *alloc_expression(struct position pos, int type)
187 struct expression *expr = __alloc_expression(0);
188 expr->type = type;
189 expr->pos = pos;
190 return expr;
193 static inline struct expression *alloc_const_expression(struct position pos, int value)
195 struct expression *expr = __alloc_expression(0);
196 expr->type = EXPR_VALUE;
197 expr->pos = pos;
198 expr->value = value;
199 expr->ctype = &int_ctype;
200 return expr;
203 /* Type name parsing */
204 struct token *typename(struct token *, struct symbol **, int *);
206 static inline int lookup_type(struct token *token)
208 if (token->pos.type == TOKEN_IDENT) {
209 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
210 return sym && (sym->namespace & NS_TYPEDEF);
212 return 0;
215 /* Statement parsing */
216 struct statement *alloc_statement(struct position pos, int type);
217 struct token *initializer(struct expression **tree, struct token *token);
218 struct token *compound_statement(struct token *, struct statement *);
220 /* The preprocessor calls this 'constant_expression()' */
221 #define constant_expression(token,tree) conditional_expression(token, tree)
223 /* Cast folding of constant values.. */
224 void cast_value(struct expression *expr, struct symbol *newtype,
225 struct expression *old, struct symbol *oldtype);
227 #endif