fix handling of pointers in ?:
[smatch.git] / expression.h
blobfa5039abbaf810fe2f0620b5df8ebd2918111c69
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"
16 struct expression_list;
18 enum expression_type {
19 EXPR_VALUE = 1,
20 EXPR_STRING,
21 EXPR_SYMBOL,
22 EXPR_TYPE,
23 EXPR_BINOP,
24 EXPR_ASSIGNMENT,
25 EXPR_LOGICAL,
26 EXPR_DEREF,
27 EXPR_PREOP,
28 EXPR_POSTOP,
29 EXPR_CAST,
30 EXPR_FORCE_CAST,
31 EXPR_IMPLIED_CAST,
32 EXPR_SIZEOF,
33 EXPR_ALIGNOF,
34 EXPR_PTRSIZEOF,
35 EXPR_CONDITIONAL,
36 EXPR_SELECT, // a "safe" conditional expression
37 EXPR_STATEMENT,
38 EXPR_CALL,
39 EXPR_COMMA,
40 EXPR_COMPARE,
41 EXPR_LABEL,
42 EXPR_INITIALIZER, // initializer list
43 EXPR_IDENTIFIER, // identifier in initializer
44 EXPR_INDEX, // index in initializer
45 EXPR_POS, // position in initializer
46 EXPR_FVALUE,
47 EXPR_SLICE,
48 EXPR_OFFSETOF,
51 enum {
52 Int_const_expr = 1,
53 Float_literal = 2,
54 }; /* for expr->flags */
56 enum {
57 Taint_comma = 1,
60 struct expression {
61 enum expression_type type:8;
62 unsigned flags:8;
63 int op;
64 struct position pos;
65 struct symbol *ctype;
66 union {
67 // EXPR_VALUE
68 struct {
69 unsigned long long value;
70 unsigned taint;
73 // EXPR_FVALUE
74 long double fvalue;
76 // EXPR_STRING
77 struct string *string;
79 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
80 struct /* unop */ {
81 struct expression *unop;
82 unsigned long op_value;
85 // EXPR_SYMBOL, EXPR_TYPE
86 struct /* symbol_arg */ {
87 struct symbol *symbol;
88 struct ident *symbol_name;
91 // EXPR_STATEMENT
92 struct statement *statement;
94 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
95 struct /* binop_arg */ {
96 struct expression *left, *right;
98 // EXPR_DEREF
99 struct /* deref_arg */ {
100 struct expression *deref;
101 struct ident *member;
103 // EXPR_SLICE
104 struct /* slice */ {
105 struct expression *base;
106 unsigned r_bitpos, r_nrbits;
108 // EXPR_CAST and EXPR_SIZEOF
109 struct /* cast_arg */ {
110 struct symbol *cast_type;
111 struct expression *cast_expression;
113 // EXPR_CONDITIONAL
114 // EXPR_SELECT
115 struct /* conditional_expr */ {
116 struct expression *conditional, *cond_true, *cond_false;
118 // EXPR_CALL
119 struct /* call_expr */ {
120 struct expression *fn;
121 struct expression_list *args;
123 // EXPR_LABEL
124 struct /* label_expr */ {
125 struct symbol *label_symbol;
127 // EXPR_INITIALIZER
128 struct expression_list *expr_list;
129 // EXPR_IDENTIFIER
130 struct /* ident_expr */ {
131 struct ident *expr_ident;
132 struct symbol *field;
133 struct expression *ident_expression;
135 // EXPR_INDEX
136 struct /* index_expr */ {
137 unsigned int idx_from, idx_to;
138 struct expression *idx_expression;
140 // EXPR_POS
141 struct /* initpos_expr */ {
142 unsigned int init_offset, init_nr;
143 struct expression *init_expr;
145 // EXPR_OFFSETOF
146 struct {
147 struct symbol *in;
148 struct expression *down;
149 union {
150 struct ident *ident;
151 struct expression *index;
157 /* Constant expression values */
158 int is_zero_constant(struct expression *);
159 long long get_expression_value(struct expression *);
160 long long const_expression_value(struct expression *);
162 /* Expression parsing */
163 struct token *parse_expression(struct token *token, struct expression **tree);
164 struct token *conditional_expression(struct token *token, struct expression **tree);
165 struct token *primary_expression(struct token *token, struct expression **tree);
166 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
167 struct token *assignment_expression(struct token *token, struct expression **tree);
169 extern void evaluate_symbol_list(struct symbol_list *list);
170 extern struct symbol *evaluate_statement(struct statement *stmt);
171 extern struct symbol *evaluate_expression(struct expression *);
173 extern int expand_symbol(struct symbol *);
175 static inline struct expression *alloc_expression(struct position pos, int type)
177 struct expression *expr = __alloc_expression(0);
178 expr->type = type;
179 expr->pos = pos;
180 return expr;
183 static inline struct expression *alloc_const_expression(struct position pos, int value)
185 struct expression *expr = __alloc_expression(0);
186 expr->type = EXPR_VALUE;
187 expr->pos = pos;
188 expr->value = value;
189 expr->ctype = &int_ctype;
190 return expr;
193 /* Type name parsing */
194 struct token *typename(struct token *, struct symbol **, int);
196 static inline int lookup_type(struct token *token)
198 if (token->pos.type == TOKEN_IDENT) {
199 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
200 return sym && sym->namespace == NS_TYPEDEF;
202 return 0;
205 /* Statement parsing */
206 struct statement *alloc_statement(struct position pos, int type);
207 struct token *initializer(struct expression **tree, struct token *token);
208 struct token *compound_statement(struct token *, struct statement *);
210 /* The preprocessor calls this 'constant_expression()' */
211 #define constant_expression(token,tree) conditional_expression(token, tree)
213 /* Cast folding of constant values.. */
214 void cast_value(struct expression *expr, struct symbol *newtype,
215 struct expression *old, struct symbol *oldtype);
217 #endif