A sparse "context" instruction has side effects. Don't allow
[smatch.git] / expression.h
blobc0b1292d8e317640ce02e4ba74046c8f6fa4b7d6
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_IMPLIED_CAST,
29 EXPR_SIZEOF,
30 EXPR_ALIGNOF,
31 EXPR_PTRSIZEOF,
32 EXPR_CONDITIONAL,
33 EXPR_SELECT, // a "safe" conditional expression
34 EXPR_STATEMENT,
35 EXPR_CALL,
36 EXPR_COMMA,
37 EXPR_COMPARE,
38 EXPR_LABEL,
39 EXPR_INITIALIZER, // initializer list
40 EXPR_IDENTIFIER, // identifier in initializer
41 EXPR_INDEX, // index in initializer
42 EXPR_POS, // position in initializer
43 EXPR_FVALUE,
44 EXPR_SLICE,
47 struct expression {
48 enum expression_type type;
49 int op;
50 struct position pos;
51 struct symbol *ctype;
52 union {
53 // EXPR_VALUE
54 unsigned long long value;
56 // EXPR_FVALUE
57 long double fvalue;
59 // EXPR_STRING
60 struct string *string;
62 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
63 struct /* unop */ {
64 struct expression *unop;
65 unsigned long op_value;
68 // EXPR_SYMBOL, EXPR_TYPE
69 struct /* symbol_arg */ {
70 struct symbol *symbol;
71 struct ident *symbol_name;
74 // EXPR_STATEMENT
75 struct statement *statement;
77 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
78 struct /* binop_arg */ {
79 struct expression *left, *right;
81 // EXPR_DEREF
82 struct /* deref_arg */ {
83 struct expression *deref;
84 struct ident *member;
86 // EXPR_SLICE
87 struct /* slice */ {
88 struct expression *base;
89 unsigned r_bitpos, r_nrbits;
91 // EXPR_CAST and EXPR_SIZEOF
92 struct /* cast_arg */ {
93 struct symbol *cast_type;
94 struct expression *cast_expression;
96 // EXPR_CONDITIONAL
97 // EXPR_SELECT
98 struct /* conditional_expr */ {
99 struct expression *conditional, *cond_true, *cond_false;
101 // EXPR_CALL
102 struct /* call_expr */ {
103 struct expression *fn;
104 struct expression_list *args;
106 // EXPR_LABEL
107 struct /* label_expr */ {
108 struct symbol *label_symbol;
110 // EXPR_INITIALIZER
111 struct expression_list *expr_list;
112 // EXPR_IDENTIFIER
113 struct /* ident_expr */ {
114 struct ident *expr_ident;
115 struct expression *ident_expression;
117 // EXPR_INDEX
118 struct /* index_expr */ {
119 unsigned int idx_from, idx_to;
120 struct expression *idx_expression;
122 // EXPR_POS
123 struct /* initpos_expr */ {
124 unsigned int init_offset, init_nr;
125 struct expression *init_expr;
130 /* Constant expression values */
131 long long get_expression_value(struct expression *);
133 /* Expression parsing */
134 struct token *parse_expression(struct token *token, struct expression **tree);
135 struct token *conditional_expression(struct token *token, struct expression **tree);
136 struct token *primary_expression(struct token *token, struct expression **tree);
137 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
138 struct token *assignment_expression(struct token *token, struct expression **tree);
140 extern void evaluate_symbol_list(struct symbol_list *list);
141 extern struct symbol *evaluate_statement(struct statement *stmt);
142 extern struct symbol *evaluate_expression(struct expression *);
144 extern void expand_symbol(struct symbol *);
146 static inline struct expression *alloc_expression(struct position pos, int type)
148 struct expression *expr = __alloc_expression(0);
149 expr->type = type;
150 expr->pos = pos;
151 return expr;
154 static inline struct expression *alloc_const_expression(struct position pos, int value)
156 struct expression *expr = __alloc_expression(0);
157 expr->type = EXPR_VALUE;
158 expr->pos = pos;
159 expr->value = value;
160 expr->ctype = &int_ctype;
161 return expr;
164 /* Type name parsing */
165 struct token *typename(struct token *, struct symbol **);
167 static inline int lookup_type(struct token *token)
169 if (token->pos.type == TOKEN_IDENT) {
170 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
171 return sym && sym->namespace == NS_TYPEDEF;
173 return 0;
176 /* Statement parsing */
177 struct statement *alloc_statement(struct position pos, int type);
178 struct token *initializer(struct expression **tree, struct token *token);
179 struct token *compound_statement(struct token *, struct statement *);
181 /* The preprocessor calls this 'constant_expression()' */
182 #define constant_expression(token,tree) conditional_expression(token, tree)
184 /* Cast folding of constant values.. */
185 void cast_value(struct expression *expr, struct symbol *newtype,
186 struct expression *old, struct symbol *oldtype);
188 #endif