Don't die on unknown expressions at linearization time.
[smatch.git] / expression.h
blob5d5612de00d6fd54cb0ae59ce25f08229ce15dfe
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_SIZEOF,
29 EXPR_ALIGNOF,
30 EXPR_CONDITIONAL,
31 EXPR_STATEMENT,
32 EXPR_CALL,
33 EXPR_COMMA,
34 EXPR_COMPARE,
35 EXPR_BITFIELD,
36 EXPR_LABEL,
37 EXPR_INITIALIZER, // initializer list
38 EXPR_IDENTIFIER, // identifier in initializer
39 EXPR_INDEX, // index in initializer
40 EXPR_POS, // position in initializer
41 EXPR_FVALUE,
44 struct expression {
45 enum expression_type type;
46 int op;
47 struct position pos;
48 struct symbol *ctype;
49 union {
50 // EXPR_VALUE
51 unsigned long long value;
53 // EXPR_FVALUE
54 long double fvalue;
56 // EXPR_STRING
57 struct string *string;
59 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
60 struct expression *unop;
62 // EXPR_SYMBOL, EXPR_TYPE
63 struct /* symbol_arg */ {
64 struct symbol *symbol;
65 struct ident *symbol_name;
68 // EXPR_STATEMENT
69 struct statement *statement;
71 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
72 struct /* binop_arg */ {
73 struct expression *left, *right;
75 // EXPR_DEREF
76 struct /* deref_arg */ {
77 struct expression *deref;
78 struct ident *member;
80 // EXPR_CAST and EXPR_SIZEOF
81 struct /* cast_arg */ {
82 struct symbol *cast_type;
83 struct expression *cast_expression;
85 // EXPR_CONDITIONAL
86 struct /* conditional_expr */ {
87 struct expression *conditional, *cond_true, *cond_false;
89 // EXPR_CALL
90 struct /* call_expr */ {
91 struct expression *fn;
92 struct expression_list *args;
94 // EXPR_BITFIELD
95 struct /* bitfield_expr */ {
96 unsigned char bitpos, nrbits;
97 struct expression *address;
99 // EXPR_LABEL
100 struct /* label_expr */ {
101 struct symbol *label_symbol;
103 // EXPR_INITIALIZER
104 struct expression_list *expr_list;
105 // EXPR_IDENTIFIER
106 struct ident *expr_ident;
107 // EXPR_INDEX
108 struct /* index_expr */ {
109 unsigned int idx_from, idx_to;
111 // EXPR_POS
112 struct /* initpos_expr */ {
113 unsigned int init_offset;
114 struct symbol *init_sym;
115 struct expression *init_expr;
120 /* Constant expression values */
121 long long get_expression_value(struct expression *);
123 /* Expression parsing */
124 struct token *parse_expression(struct token *token, struct expression **tree);
125 struct token *conditional_expression(struct token *token, struct expression **tree);
126 struct token *primary_expression(struct token *token, struct expression **tree);
127 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
128 struct token *assignment_expression(struct token *token, struct expression **tree);
130 extern void check_duplicates(struct symbol *sym);
131 extern struct symbol *evaluate_symbol(struct symbol *sym);
132 extern struct symbol *evaluate_statement(struct statement *stmt);
133 extern struct symbol *evaluate_expression(struct expression *);
135 extern void expand_symbol(struct symbol *);
137 static inline struct expression *alloc_expression(struct position pos, int type)
139 struct expression *expr = __alloc_expression(0);
140 expr->type = type;
141 expr->pos = pos;
142 return expr;
145 static inline struct expression *alloc_const_expression(struct position pos, int value)
147 struct expression *expr = __alloc_expression(0);
148 expr->type = EXPR_VALUE;
149 expr->pos = pos;
150 expr->value = value;
151 expr->ctype = &int_ctype;
152 return expr;
155 /* Type name parsing */
156 struct token *typename(struct token *, struct symbol **);
158 static inline int lookup_type(struct token *token)
160 if (token->pos.type == TOKEN_IDENT) {
161 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
162 return sym && sym->namespace == NS_TYPEDEF;
164 return 0;
167 /* Statement parsing */
168 struct statement *alloc_statement(struct position pos, int type);
169 struct token *initializer(struct expression **tree, struct token *token);
170 struct token *compound_statement(struct token *, struct statement *);
172 /* The preprocessor calls this 'constant_expression()' */
173 #define constant_expression(token,tree) conditional_expression(token, tree)
175 /* Cast folding of constant values.. */
176 void cast_value(struct expression *expr, struct symbol *newtype,
177 struct expression *old, struct symbol *oldtype);
179 #endif