Add "optimize" flag, and collect the flags into lib.c.
[smatch.git] / expression.h
blob4560b840bea5161c883839b51cb167d675adbed3
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 expression *unop;
65 // EXPR_SYMBOL, EXPR_TYPE
66 struct /* symbol_arg */ {
67 struct symbol *symbol;
68 struct ident *symbol_name;
71 // EXPR_STATEMENT
72 struct statement *statement;
74 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
75 struct /* binop_arg */ {
76 struct expression *left, *right;
78 // EXPR_DEREF
79 struct /* deref_arg */ {
80 struct expression *deref;
81 struct ident *member;
83 // EXPR_SLICE
84 struct /* slice */ {
85 struct expression *base;
86 unsigned r_bitpos, r_nrbits;
88 // EXPR_CAST and EXPR_SIZEOF
89 struct /* cast_arg */ {
90 struct symbol *cast_type;
91 struct expression *cast_expression;
93 // EXPR_CONDITIONAL
94 // EXPR_SELECT
95 struct /* conditional_expr */ {
96 struct expression *conditional, *cond_true, *cond_false;
98 // EXPR_CALL
99 struct /* call_expr */ {
100 struct expression *fn;
101 struct expression_list *args;
103 // EXPR_LABEL
104 struct /* label_expr */ {
105 struct symbol *label_symbol;
107 // EXPR_INITIALIZER
108 struct expression_list *expr_list;
109 // EXPR_IDENTIFIER
110 struct /* ident_expr */ {
111 struct ident *expr_ident;
112 struct expression *ident_expression;
114 // EXPR_INDEX
115 struct /* index_expr */ {
116 unsigned int idx_from, idx_to;
117 struct expression *idx_expression;
119 // EXPR_POS
120 struct /* initpos_expr */ {
121 unsigned int init_offset, init_nr;
122 struct expression *init_expr;
127 /* Constant expression values */
128 long long get_expression_value(struct expression *);
130 /* Expression parsing */
131 struct token *parse_expression(struct token *token, struct expression **tree);
132 struct token *conditional_expression(struct token *token, struct expression **tree);
133 struct token *primary_expression(struct token *token, struct expression **tree);
134 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
135 struct token *assignment_expression(struct token *token, struct expression **tree);
137 extern void evaluate_symbol_list(struct symbol_list *list);
138 extern struct symbol *evaluate_statement(struct statement *stmt);
139 extern struct symbol *evaluate_expression(struct expression *);
141 extern void expand_symbol(struct symbol *);
143 static inline struct expression *alloc_expression(struct position pos, int type)
145 struct expression *expr = __alloc_expression(0);
146 expr->type = type;
147 expr->pos = pos;
148 return expr;
151 static inline struct expression *alloc_const_expression(struct position pos, int value)
153 struct expression *expr = __alloc_expression(0);
154 expr->type = EXPR_VALUE;
155 expr->pos = pos;
156 expr->value = value;
157 expr->ctype = &int_ctype;
158 return expr;
161 /* Type name parsing */
162 struct token *typename(struct token *, struct symbol **);
164 static inline int lookup_type(struct token *token)
166 if (token->pos.type == TOKEN_IDENT) {
167 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
168 return sym && sym->namespace == NS_TYPEDEF;
170 return 0;
173 /* Statement parsing */
174 struct statement *alloc_statement(struct position pos, int type);
175 struct token *initializer(struct expression **tree, struct token *token);
176 struct token *compound_statement(struct token *, struct statement *);
178 /* The preprocessor calls this 'constant_expression()' */
179 #define constant_expression(token,tree) conditional_expression(token, tree)
181 /* Cast folding of constant values.. */
182 void cast_value(struct expression *expr, struct symbol *newtype,
183 struct expression *old, struct symbol *oldtype);
185 #endif