#ifndef EXPRESSION_H
#define EXPRESSION_H
#include "allocate.h"
#include "lib.h"
#include "symbol.h"
struct expression_list;
enum expression_type {
EXPR_VALUE = 1,
EXPR_STRING,
EXPR_SYMBOL,
EXPR_TYPE,
EXPR_BINOP,
EXPR_ASSIGNMENT,
EXPR_LOGICAL,
EXPR_DEREF,
EXPR_PREOP,
EXPR_POSTOP,
EXPR_CAST,
EXPR_FORCE_CAST,
EXPR_IMPLIED_CAST,
EXPR_SIZEOF,
EXPR_ALIGNOF,
EXPR_PTRSIZEOF,
EXPR_CONDITIONAL,
EXPR_SELECT,
EXPR_STATEMENT,
EXPR_CALL,
EXPR_COMMA,
EXPR_COMPARE,
EXPR_LABEL,
EXPR_INITIALIZER,
EXPR_IDENTIFIER,
EXPR_INDEX,
EXPR_POS,
EXPR_FVALUE,
EXPR_SLICE,
EXPR_OFFSETOF,
EXPR_ASM_OPERAND,
};
enum constexpr_flag {
CEF_NONE = 0,
CEF_INT = (1 << 0),
CEF_FLOAT = (1 << 1),
CEF_ENUM = (1 << 2),
CEF_CHAR = (1 << 3),
CEF_ICE = (1 << 4),
CEF_ACE = (1 << 5),
CEF_ADDR = (1 << 6),
CEF_SET_ICE = (CEF_ICE | CEF_ACE),
CEF_SET_INT = (CEF_INT | CEF_SET_ICE),
CEF_SET_FLOAT = (CEF_FLOAT | CEF_ACE),
CEF_SET_ENUM = (CEF_ENUM | CEF_SET_ICE),
CEF_SET_CHAR = (CEF_CHAR | CEF_SET_ICE),
CEF_CONST_MASK = (CEF_INT | CEF_FLOAT | CEF_CHAR),
CEF_CLR_ICE = (CEF_ICE | CEF_INT | CEF_ENUM | CEF_CHAR),
};
enum {
Handled = 1 << 0,
Tmp = 1 << 1,
Fake = 1 << 2,
};
enum {
Taint_comma = 1,
};
struct expression {
enum expression_type type:8;
unsigned flags:8;
unsigned smatch_flags:16;
int op;
struct position pos;
struct symbol *ctype;
unsigned long parent;
union {
struct {
unsigned long long value;
unsigned taint;
};
long double fvalue;
struct {
int wide;
struct string *string;
};
struct {
struct expression *unop;
unsigned long op_value;
};
struct {
struct symbol *symbol;
struct ident *symbol_name;
};
struct statement *statement;
struct {
struct expression *left, *right;
};
struct {
struct expression *deref;
struct ident *member;
int member_offset;
};
struct {
struct expression *base;
unsigned r_bitpos, r_nrbits;
};
struct {
struct symbol *cast_type;
struct expression *cast_expression;
};
struct {
struct expression *conditional, *cond_true, *cond_false;
};
struct {
struct expression *fn;
struct expression_list *args;
};
struct {
struct symbol *label_symbol;
};
struct expression_list *expr_list;
struct {
int offset;
struct ident *expr_ident;
struct symbol *field;
struct expression *ident_expression;
};
struct {
unsigned int idx_from, idx_to;
struct expression *idx_expression;
};
struct {
unsigned int init_offset, init_nr;
struct expression *init_expr;
};
struct {
struct symbol *in;
struct expression *down;
union {
struct ident *ident;
struct expression *index;
};
};
struct {
struct ident *name;
struct expression *constraint;
struct expression *expr;
};
};
};
int is_zero_constant(struct expression *expr);
int expr_truth_value(struct expression *expr);
long long get_expression_value(struct expression *);
long long const_expression_value(struct expression *);
long long get_expression_value_silent(struct expression *expr);
struct token *parse_expression(struct token *token, struct expression **tree);
struct token *conditional_expression(struct token *token, struct expression **tree);
struct token *primary_expression(struct token *token, struct expression **tree);
struct token *parens_expression(struct token *token, struct expression **expr, const char *where);
struct token *assignment_expression(struct token *token, struct expression **tree);
extern void evaluate_symbol_list(struct symbol_list *list);
extern struct symbol *evaluate_statement(struct statement *stmt);
extern struct symbol *evaluate_expression(struct expression *);
struct symbol *find_identifier(struct ident *ident, struct symbol_list *_list, int *offset);
extern int expand_symbol(struct symbol *);
static inline struct expression *alloc_expression(struct position pos, int type)
{
struct expression *expr = __alloc_expression(0);
expr->type = type;
expr->pos = pos;
expr->flags = CEF_NONE;
return expr;
}
static inline struct expression *alloc_const_expression(struct position pos, int value)
{
struct expression *expr = __alloc_expression(0);
expr->type = EXPR_VALUE;
expr->pos = pos;
expr->value = value;
expr->ctype = &int_ctype;
expr->flags = CEF_SET_INT;
return expr;
}
struct token *typename(struct token *, struct symbol **, int *);
static inline int lookup_type(struct token *token)
{
if (token->pos.type == TOKEN_IDENT) {
struct symbol *sym = lookup_symbol(token->ident, NS_SYMBOL | NS_TYPEDEF);
return sym && (sym->namespace & NS_TYPEDEF);
}
return 0;
}
struct statement *alloc_statement(struct position pos, int type);
struct token *initializer(struct expression **tree, struct token *token);
struct token *compound_statement(struct token *, struct statement *);
#define constant_expression(token,tree) conditional_expression(token, tree)
void cast_value(struct expression *expr, struct symbol *newtype,
struct expression *old, struct symbol *oldtype);
#endif