added a bunch of gcc builtins
[smatch.git] / expression.h
blobea6ab3427f143de2e6d46cbdd95f1fa39791f712
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_IMPLIED_CAST,
31 EXPR_SIZEOF,
32 EXPR_ALIGNOF,
33 EXPR_PTRSIZEOF,
34 EXPR_CONDITIONAL,
35 EXPR_SELECT, // a "safe" conditional expression
36 EXPR_STATEMENT,
37 EXPR_CALL,
38 EXPR_COMMA,
39 EXPR_COMPARE,
40 EXPR_LABEL,
41 EXPR_INITIALIZER, // initializer list
42 EXPR_IDENTIFIER, // identifier in initializer
43 EXPR_INDEX, // index in initializer
44 EXPR_POS, // position in initializer
45 EXPR_FVALUE,
46 EXPR_SLICE,
49 struct expression {
50 enum expression_type type;
51 int op;
52 struct position pos;
53 struct symbol *ctype;
54 union {
55 // EXPR_VALUE
56 unsigned long long value;
58 // EXPR_FVALUE
59 long double fvalue;
61 // EXPR_STRING
62 struct string *string;
64 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
65 struct /* unop */ {
66 struct expression *unop;
67 unsigned long op_value;
70 // EXPR_SYMBOL, EXPR_TYPE
71 struct /* symbol_arg */ {
72 struct symbol *symbol;
73 struct ident *symbol_name;
76 // EXPR_STATEMENT
77 struct statement *statement;
79 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
80 struct /* binop_arg */ {
81 struct expression *left, *right;
83 // EXPR_DEREF
84 struct /* deref_arg */ {
85 struct expression *deref;
86 struct ident *member;
88 // EXPR_SLICE
89 struct /* slice */ {
90 struct expression *base;
91 unsigned r_bitpos, r_nrbits;
93 // EXPR_CAST and EXPR_SIZEOF
94 struct /* cast_arg */ {
95 struct symbol *cast_type;
96 struct expression *cast_expression;
98 // EXPR_CONDITIONAL
99 // EXPR_SELECT
100 struct /* conditional_expr */ {
101 struct expression *conditional, *cond_true, *cond_false;
103 // EXPR_CALL
104 struct /* call_expr */ {
105 struct expression *fn;
106 struct expression_list *args;
108 // EXPR_LABEL
109 struct /* label_expr */ {
110 struct symbol *label_symbol;
112 // EXPR_INITIALIZER
113 struct expression_list *expr_list;
114 // EXPR_IDENTIFIER
115 struct /* ident_expr */ {
116 struct ident *expr_ident;
117 struct expression *ident_expression;
119 // EXPR_INDEX
120 struct /* index_expr */ {
121 unsigned int idx_from, idx_to;
122 struct expression *idx_expression;
124 // EXPR_POS
125 struct /* initpos_expr */ {
126 unsigned int init_offset, init_nr;
127 struct expression *init_expr;
132 /* Constant expression values */
133 long long get_expression_value(struct expression *);
135 /* Expression parsing */
136 struct token *parse_expression(struct token *token, struct expression **tree);
137 struct token *conditional_expression(struct token *token, struct expression **tree);
138 struct token *primary_expression(struct token *token, struct expression **tree);
139 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
140 struct token *assignment_expression(struct token *token, struct expression **tree);
142 extern void evaluate_symbol_list(struct symbol_list *list);
143 extern struct symbol *evaluate_statement(struct statement *stmt);
144 extern struct symbol *evaluate_expression(struct expression *);
146 extern int expand_symbol(struct symbol *);
148 static inline struct expression *alloc_expression(struct position pos, int type)
150 struct expression *expr = __alloc_expression(0);
151 expr->type = type;
152 expr->pos = pos;
153 return expr;
156 static inline struct expression *alloc_const_expression(struct position pos, int value)
158 struct expression *expr = __alloc_expression(0);
159 expr->type = EXPR_VALUE;
160 expr->pos = pos;
161 expr->value = value;
162 expr->ctype = &int_ctype;
163 return expr;
166 /* Type name parsing */
167 struct token *typename(struct token *, struct symbol **);
169 static inline int lookup_type(struct token *token)
171 if (token->pos.type == TOKEN_IDENT) {
172 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
173 return sym && sym->namespace == NS_TYPEDEF;
175 return 0;
178 /* Statement parsing */
179 struct statement *alloc_statement(struct position pos, int type);
180 struct token *initializer(struct expression **tree, struct token *token);
181 struct token *compound_statement(struct token *, struct statement *);
183 /* The preprocessor calls this 'constant_expression()' */
184 #define constant_expression(token,tree) conditional_expression(token, tree)
186 /* Cast folding of constant values.. */
187 void cast_value(struct expression *expr, struct symbol *newtype,
188 struct expression *old, struct symbol *oldtype);
190 #endif