Make our "__builtin_va_arg()" thing a bit closer to real.
[smatch.git] / expression.h
blob7f4fd5a441b39df81c69a47c97334c5d2f01b678
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 check_duplicates(struct symbol *sym);
138 extern struct symbol *evaluate_symbol(struct symbol *sym);
139 extern struct symbol *evaluate_statement(struct statement *stmt);
140 extern struct symbol *evaluate_expression(struct expression *);
142 extern void expand_symbol(struct symbol *);
144 static inline struct expression *alloc_expression(struct position pos, int type)
146 struct expression *expr = __alloc_expression(0);
147 expr->type = type;
148 expr->pos = pos;
149 return expr;
152 static inline struct expression *alloc_const_expression(struct position pos, int value)
154 struct expression *expr = __alloc_expression(0);
155 expr->type = EXPR_VALUE;
156 expr->pos = pos;
157 expr->value = value;
158 expr->ctype = &int_ctype;
159 return expr;
162 /* Type name parsing */
163 struct token *typename(struct token *, struct symbol **);
165 static inline int lookup_type(struct token *token)
167 if (token->pos.type == TOKEN_IDENT) {
168 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
169 return sym && sym->namespace == NS_TYPEDEF;
171 return 0;
174 /* Statement parsing */
175 struct statement *alloc_statement(struct position pos, int type);
176 struct token *initializer(struct expression **tree, struct token *token);
177 struct token *compound_statement(struct token *, struct statement *);
179 /* The preprocessor calls this 'constant_expression()' */
180 #define constant_expression(token,tree) conditional_expression(token, tree)
182 /* Cast folding of constant values.. */
183 void cast_value(struct expression *expr, struct symbol *newtype,
184 struct expression *old, struct symbol *oldtype);
186 #endif