type: introduce type_to_str()
[smatch.git] / smatch_expressions.c
blob4073eedf98ef863c4a2da15c14a842398f024ff2
1 #include "smatch.h"
3 static struct position get_cur_pos(void)
5 static struct position pos;
6 static struct position none;
7 struct expression *expr;
8 struct statement *stmt;
10 expr = last_ptr_list((struct ptr_list *)big_expression_stack);
11 stmt = last_ptr_list((struct ptr_list *)big_statement_stack);
12 if (expr)
13 pos = expr->pos;
14 else if (stmt)
15 pos = stmt->pos;
16 else
17 pos = none;
18 return pos;
21 struct expression *zero_expr(void)
23 static struct expression *zero;
25 if (zero)
26 return zero;
28 zero = alloc_expression(get_cur_pos(), EXPR_VALUE);
29 zero->value = 0;
30 zero->ctype = &char_ctype;
31 return zero;
34 struct expression *value_expr(long long val)
36 struct expression *expr;
38 if (!val)
39 return zero_expr();
41 expr = alloc_expression(get_cur_pos(), EXPR_VALUE);
42 expr->value = val;
43 expr->ctype = &llong_ctype;
44 return expr;
47 struct expression *member_expression(struct expression *deref, int op, struct ident *member)
49 struct expression *expr;
51 expr = alloc_expression(deref->pos, EXPR_DEREF);
52 expr->op = op;
53 expr->deref = deref;
54 expr->member = member;
55 return expr;
58 struct expression *deref_expression(struct expression *expr)
60 struct expression *preop;
62 preop = alloc_expression(expr->pos, EXPR_PREOP);
63 preop->unop = expr;
64 preop->op = '*';
65 return preop;
68 struct expression *assign_expression(struct expression *left, struct expression *right)
70 struct expression *expr;
72 expr = alloc_expression(right->pos, EXPR_ASSIGNMENT);
73 expr->op = '=';
74 expr->left = left;
75 expr->right = right;
76 return expr;
79 struct expression *binop_expression(struct expression *left, int op, struct expression *right)
81 struct expression *expr;
83 expr = alloc_expression(right->pos, EXPR_BINOP);
84 expr->op = op;
85 expr->left = left;
86 expr->right = right;
87 return expr;
90 struct expression *array_element_expression(struct expression *array, struct expression *offset)
92 struct expression *expr;
94 expr = binop_expression(array, '+', offset);
95 return deref_expression(expr);
98 struct expression *symbol_expression(struct symbol *sym)
100 struct expression *expr;
102 expr = alloc_expression(sym->pos, EXPR_SYMBOL);
103 expr->symbol = sym;
104 expr->symbol_name = sym->ident;
105 return expr;