[PATCH] speed up (and fix corner case in) tokenizer
[smatch.git] / expression.h
blob5845e0f404b41dd0dfcfffa23fa70eb71199cf72
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
43 struct expression {
44 enum expression_type type;
45 int op;
46 struct position pos;
47 struct symbol *ctype;
48 union {
49 // EXPR_VALUE
50 unsigned long long value;
52 // EXPR_STRING
53 struct string *string;
55 // EXPR_UNOP, EXPR_PREOP and EXPR_POSTOP
56 struct expression *unop;
58 // EXPR_SYMBOL, EXPR_TYPE
59 struct /* symbol_arg */ {
60 struct symbol *symbol;
61 struct ident *symbol_name;
64 // EXPR_STATEMENT
65 struct statement *statement;
67 // EXPR_BINOP, EXPR_COMMA, EXPR_COMPARE, EXPR_LOGICAL and EXPR_ASSIGNMENT
68 struct /* binop_arg */ {
69 struct expression *left, *right;
71 // EXPR_DEREF
72 struct /* deref_arg */ {
73 struct expression *deref;
74 struct ident *member;
76 // EXPR_CAST and EXPR_SIZEOF
77 struct /* cast_arg */ {
78 struct symbol *cast_type;
79 struct expression *cast_expression;
81 // EXPR_CONDITIONAL
82 struct /* conditional_expr */ {
83 struct expression *conditional, *cond_true, *cond_false;
85 // EXPR_CALL
86 struct /* call_expr */ {
87 struct expression *fn;
88 struct expression_list *args;
90 // EXPR_BITFIELD
91 struct /* bitfield_expr */ {
92 unsigned char bitpos, nrbits;
93 struct expression *address;
95 // EXPR_LABEL
96 struct /* label_expr */ {
97 struct symbol *label_symbol;
99 // EXPR_INITIALIZER
100 struct expression_list *expr_list;
101 // EXPR_IDENTIFIER
102 struct ident *expr_ident;
103 // EXPR_INDEX
104 struct /* index_expr */ {
105 unsigned int idx_from, idx_to;
107 // EXPR_POS
108 struct /* initpos_expr */ {
109 unsigned int init_offset;
110 struct symbol *init_sym;
111 struct expression *init_expr;
116 /* Constant expression values */
117 long long get_expression_value(struct expression *);
119 /* Expression parsing */
120 struct token *parse_expression(struct token *token, struct expression **tree);
121 struct token *conditional_expression(struct token *token, struct expression **tree);
122 struct token *primary_expression(struct token *token, struct expression **tree);
123 struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
124 struct token *assignment_expression(struct token *token, struct expression **tree);
126 extern void check_duplicates(struct symbol *sym);
127 extern struct symbol *evaluate_symbol(struct symbol *sym);
128 extern struct symbol *evaluate_statement(struct statement *stmt);
129 extern struct symbol *evaluate_expression(struct expression *);
131 extern void expand_symbol(struct symbol *);
133 static inline struct expression *alloc_expression(struct position pos, int type)
135 struct expression *expr = __alloc_expression(0);
136 expr->type = type;
137 expr->pos = pos;
138 return expr;
141 static inline struct expression *alloc_const_expression(struct position pos, int value)
143 struct expression *expr = __alloc_expression(0);
144 expr->type = EXPR_VALUE;
145 expr->pos = pos;
146 expr->value = value;
147 expr->ctype = &int_ctype;
148 return expr;
151 /* Type name parsing */
152 struct token *typename(struct token *, struct symbol **);
154 static inline int lookup_type(struct token *token)
156 if (token->pos.type == TOKEN_IDENT) {
157 struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
158 return sym && sym->namespace == NS_TYPEDEF;
160 return 0;
163 /* Statement parsing */
164 struct statement *alloc_statement(struct position pos, int type);
165 struct token *initializer(struct expression **tree, struct token *token);
166 struct token *compound_statement(struct token *, struct statement *);
168 /* The preprocessor calls this 'constant_expression()' */
169 #define constant_expression(token,tree) conditional_expression(token, tree)
171 /* Cast folding of constant values.. */
172 void cast_value(struct expression *expr, struct symbol *newtype,
173 struct expression *old, struct symbol *oldtype);
175 #endif