[be] Handle 'break' and 'continue' inside loops.
[smatch.git] / expression.h
blob935d23acec8910905ae619a678bec47f5054f7c6
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_BINOP,
21 EXPR_ASSIGNMENT,
22 EXPR_LOGICAL,
23 EXPR_DEREF,
24 EXPR_PREOP,
25 EXPR_POSTOP,
26 EXPR_CAST,
27 EXPR_SIZEOF,
28 EXPR_CONDITIONAL,
29 EXPR_STATEMENT,
30 EXPR_CALL,
31 EXPR_COMMA,
32 EXPR_COMPARE,
33 EXPR_BITFIELD,
34 EXPR_LABEL,
35 EXPR_INITIALIZER, // initializer list
36 EXPR_IDENTIFIER, // identifier in initializer
37 EXPR_INDEX, // index in initializer
38 EXPR_POS, // position in initializer
41 struct expression {
42 enum expression_type type;
43 int op;
44 struct position pos;
45 struct symbol *ctype;
46 union {
47 // EXPR_VALUE
48 unsigned long long value;
50 // EXPR_STRING
51 struct string *string;
53 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
54 struct expression *unop;
56 // EXPR_SYMBOL
57 struct /* symbol_arg */ {
58 struct symbol *symbol;
59 struct ident *symbol_name;
62 // EXPR_STATEMENT
63 struct statement *statement;
65 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
66 struct /* binop_arg */ {
67 struct expression *left, *right;
69 // EXPR_DEREF
70 struct /* deref_arg */ {
71 struct expression *deref;
72 struct ident *member;
74 // EXPR_CAST and EXPR_SIZEOF
75 struct /* cast_arg */ {
76 struct symbol *cast_type;
77 struct expression *cast_expression;
79 // EXPR_CONDITIONAL
80 struct /* conditional_expr */ {
81 struct expression *conditional, *cond_true, *cond_false;
83 // EXPR_CALL
84 struct /* call_expr */ {
85 struct expression *fn;
86 struct expression_list *args;
88 // EXPR_BITFIELD
89 struct /* bitfield_expr */ {
90 unsigned char bitpos, nrbits;
91 struct expression *address;
93 // EXPR_LABEL
94 struct /* label_expr */ {
95 struct symbol *label_symbol;
97 // EXPR_INITIALIZER
98 struct expression_list *expr_list;
99 // EXPR_IDENTIFIER
100 struct ident *expr_ident;
101 // EXPR_INDEX
102 struct /* index_expr */ {
103 unsigned int idx_from, idx_to;
105 // EXPR_POS
106 struct /* initpos_expr */ {
107 unsigned int init_offset;
108 struct symbol *init_sym;
109 struct expression *init_expr;
114 /* Constant expression values */
115 long long get_expression_value(struct expression *);
117 /* Expression parsing */
118 struct token *parse_expression(struct token *token, struct expression **tree);
119 struct token *conditional_expression(struct token *token, struct expression **tree);
120 struct token *primary_expression(struct token *token, struct expression **tree);
121 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
122 struct token *assignment_expression(struct token *token, struct expression **tree);
124 extern void check_duplicates(struct symbol *sym);
125 extern struct symbol *evaluate_symbol(struct symbol *sym);
126 extern struct symbol *evaluate_statement(struct statement *stmt);
127 extern struct symbol *evaluate_expression(struct expression *);
129 extern void expand_symbol(struct symbol *);
131 static inline struct expression *alloc_expression(struct position pos, int type)
133 struct expression *expr = __alloc_expression(0);
134 expr->type = type;
135 expr->pos = pos;
136 return expr;
139 static inline struct expression *alloc_const_expression(struct position pos, int value)
141 struct expression *expr = __alloc_expression(0);
142 expr->type = EXPR_VALUE;
143 expr->pos = pos;
144 expr->value = value;
145 expr->ctype = &int_ctype;
146 return expr;
149 /* Type name parsing */
150 struct token *typename(struct token *, struct symbol **);
152 static inline int lookup_type(struct token *token)
154 if (token->pos.type == TOKEN_IDENT)
155 return lookup_symbol(token->ident, NS_TYPEDEF) != NULL;
156 return 0;
159 /* Statement parsing */
160 struct statement *alloc_statement(struct position pos, int type);
161 struct token *initializer(struct expression **tree, struct token *token);
162 struct token *compound_statement(struct token *, struct statement *);
164 /* The preprocessor calls this 'constant_expression()' */
165 #define constant_expression(token,tree) conditional_expression(token, tree)
167 /* Cast folding of constant values.. */
168 void cast_value(struct expression *expr, struct symbol *newtype,
169 struct expression *old, struct symbol *oldtype);
171 #endif