sparse, llvm: Fix assert() in sparse code
[smatch.git] / expression.h
blob9778de8840e0240960c999491b714cddc526ad0a
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 {
80 int wide;
81 struct string *string;
84 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
85 struct /* unop */ {
86 struct expression *unop;
87 unsigned long op_value;
90 // EXPR_SYMBOL, EXPR_TYPE
91 struct /* symbol_arg */ {
92 struct symbol *symbol;
93 struct ident *symbol_name;
96 // EXPR_STATEMENT
97 struct statement *statement;
99 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
100 struct /* binop_arg */ {
101 struct expression *left, *right;
103 // EXPR_DEREF
104 struct /* deref_arg */ {
105 struct expression *deref;
106 struct ident *member;
108 // EXPR_SLICE
109 struct /* slice */ {
110 struct expression *base;
111 unsigned r_bitpos, r_nrbits;
113 // EXPR_CAST and EXPR_SIZEOF
114 struct /* cast_arg */ {
115 struct symbol *cast_type;
116 struct expression *cast_expression;
118 // EXPR_CONDITIONAL
119 // EXPR_SELECT
120 struct /* conditional_expr */ {
121 struct expression *conditional, *cond_true, *cond_false;
123 // EXPR_CALL
124 struct /* call_expr */ {
125 struct expression *fn;
126 struct expression_list *args;
128 // EXPR_LABEL
129 struct /* label_expr */ {
130 struct symbol *label_symbol;
132 // EXPR_INITIALIZER
133 struct expression_list *expr_list;
134 // EXPR_IDENTIFIER
135 struct /* ident_expr */ {
136 struct ident *expr_ident;
137 struct symbol *field;
138 struct expression *ident_expression;
140 // EXPR_INDEX
141 struct /* index_expr */ {
142 unsigned int idx_from, idx_to;
143 struct expression *idx_expression;
145 // EXPR_POS
146 struct /* initpos_expr */ {
147 unsigned int init_offset, init_nr;
148 struct expression *init_expr;
150 // EXPR_OFFSETOF
151 struct {
152 struct symbol *in;
153 struct expression *down;
154 union {
155 struct ident *ident;
156 struct expression *index;
162 /* Constant expression values */
163 int is_zero_constant(struct expression *);
164 long long get_expression_value(struct expression *);
165 long long const_expression_value(struct expression *);
167 /* Expression parsing */
168 struct token *parse_expression(struct token *token, struct expression **tree);
169 struct token *conditional_expression(struct token *token, struct expression **tree);
170 struct token *primary_expression(struct token *token, struct expression **tree);
171 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
172 struct token *assignment_expression(struct token *token, struct expression **tree);
174 extern void evaluate_symbol_list(struct symbol_list *list);
175 extern struct symbol *evaluate_statement(struct statement *stmt);
176 extern struct symbol *evaluate_expression(struct expression *);
178 extern int expand_symbol(struct symbol *);
180 static inline struct expression *alloc_expression(struct position pos, int type)
182 struct expression *expr = __alloc_expression(0);
183 expr->type = type;
184 expr->pos = pos;
185 return expr;
188 static inline struct expression *alloc_const_expression(struct position pos, int value)
190 struct expression *expr = __alloc_expression(0);
191 expr->type = EXPR_VALUE;
192 expr->pos = pos;
193 expr->value = value;
194 expr->ctype = &int_ctype;
195 return expr;
198 /* Type name parsing */
199 struct token *typename(struct token *, struct symbol **, int *);
201 static inline int lookup_type(struct token *token)
203 if (token->pos.type == TOKEN_IDENT) {
204 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
205 return sym && (sym->namespace & NS_TYPEDEF);
207 return 0;
210 /* Statement parsing */
211 struct statement *alloc_statement(struct position pos, int type);
212 struct token *initializer(struct expression **tree, struct token *token);
213 struct token *compound_statement(struct token *, struct statement *);
215 /* The preprocessor calls this 'constant_expression()' */
216 #define constant_expression(token,tree) conditional_expression(token, tree)
218 /* Cast folding of constant values.. */
219 void cast_value(struct expression *expr, struct symbol *newtype,
220 struct expression *old, struct symbol *oldtype);
222 #endif