2 #include "smatch_extra.h"
4 DECLARE_ALLOCATOR(sname
);
5 __ALLOCATOR(struct expression
, "temporary expr", tmp_expression
);
7 static struct position
get_cur_pos(void)
9 static struct position pos
;
10 static struct position none
;
11 struct expression
*expr
;
12 struct statement
*stmt
;
14 expr
= last_ptr_list((struct ptr_list
*)big_expression_stack
);
15 stmt
= last_ptr_list((struct ptr_list
*)big_statement_stack
);
25 struct expression
*alloc_tmp_expression(struct position pos
, int type
)
27 struct expression
*expr
;
29 expr
= __alloc_tmp_expression(0);
30 expr
->smatch_flags
|= Fake
;
36 void free_tmp_expressions(void)
38 clear_tmp_expression_alloc();
41 struct expression
*zero_expr(void)
43 struct expression
*zero
;
45 zero
= alloc_tmp_expression(get_cur_pos(), EXPR_VALUE
);
47 zero
->ctype
= &int_ctype
;
51 struct expression
*value_expr(long long val
)
53 struct expression
*expr
;
58 expr
= alloc_tmp_expression(get_cur_pos(), EXPR_VALUE
);
60 expr
->ctype
= &llong_ctype
;
64 struct expression
*member_expression(struct expression
*deref
, int op
, struct ident
*member
)
66 struct expression
*expr
;
68 expr
= alloc_tmp_expression(deref
->pos
, EXPR_DEREF
);
71 expr
->member
= member
;
72 expr
->member_offset
= -1;
76 struct expression
*preop_expression(struct expression
*expr
, int op
)
78 struct expression
*preop
;
80 preop
= alloc_tmp_expression(expr
->pos
, EXPR_PREOP
);
86 struct expression
*deref_expression(struct expression
*expr
)
88 return preop_expression(expr
, '*');
91 struct expression
*assign_expression(struct expression
*left
, int op
, struct expression
*right
)
93 struct expression
*expr
;
98 /* FIXME: make this a tmp expression. */
99 expr
= alloc_expression(right
->pos
, EXPR_ASSIGNMENT
);
106 struct expression
*binop_expression(struct expression
*left
, int op
, struct expression
*right
)
108 struct expression
*expr
;
110 expr
= alloc_tmp_expression(right
->pos
, EXPR_BINOP
);
117 struct expression
*array_element_expression(struct expression
*array
, struct expression
*offset
)
119 struct expression
*expr
;
121 expr
= binop_expression(array
, '+', offset
);
122 return deref_expression(expr
);
125 struct expression
*symbol_expression(struct symbol
*sym
)
127 struct expression
*expr
;
129 expr
= alloc_tmp_expression(sym
->pos
, EXPR_SYMBOL
);
131 expr
->symbol_name
= sym
->ident
;
135 struct expression
*compare_expression(struct expression
*left
, int op
, struct expression
*right
)
137 struct expression
*expr
;
139 expr
= alloc_tmp_expression(get_cur_pos(), EXPR_COMPARE
);
146 struct expression
*string_expression(char *str
)
148 struct expression
*ret
;
149 struct string
*string
;
152 len
= strlen(str
) + 1;
153 string
= (void *)__alloc_sname(4 + len
);
154 string
->length
= len
;
155 string
->immutable
= 0;
156 memcpy(string
->data
, str
, len
);
158 ret
= alloc_tmp_expression(get_cur_pos(), EXPR_STRING
);
160 ret
->string
= string
;
165 struct expression
*gen_expression_from_key(struct expression
*arg
, const char *key
)
167 struct expression
*ret
;
168 struct token
*token
, *prev
, *end
;
174 /* The idea is that we can parse either $0->foo or $->foo */
178 while (*p
>= '0' && *p
<= '9')
180 len
= snprintf(buf
, sizeof(buf
), "%s\n", p
);
181 alloc
= alloc_string(buf
);
183 token
= tokenize_buffer(alloc
, len
, &end
);
186 if (token_type(token
) != TOKEN_STREAMBEGIN
)
191 while (token_type(token
) == TOKEN_SPECIAL
&&
192 (token
->special
== SPECIAL_DEREFERENCE
|| token
->special
== '.')) {
195 if (token_type(token
) != TOKEN_IDENT
)
197 ret
= deref_expression(ret
);
198 ret
= member_expression(ret
,
199 (prev
->special
== SPECIAL_DEREFERENCE
) ? '*' : '.',
204 if (token_type(token
) != TOKEN_STREAMEND
)
210 void expr_set_parent_expr(struct expression
*expr
, struct expression
*parent
)
214 if (parent
->smatch_flags
& Fake
)
217 expr
->parent
= (unsigned long)parent
| 0x1UL
;
220 void expr_set_parent_stmt(struct expression
*expr
, struct statement
*parent
)
224 expr
->parent
= (unsigned long)parent
;
227 struct expression
*expr_get_parent_expr(struct expression
*expr
)
231 if (!(expr
->parent
& 0x1UL
))
233 return (struct expression
*)(expr
->parent
& ~0x1UL
);
236 struct statement
*expr_get_parent_stmt(struct expression
*expr
)
240 if (expr
->parent
& 0x1UL
)
242 return (struct statement
*)expr
->parent
;