+CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
+ get_unexpr_arithmetic_type)
+CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
+ get_unexpr_arithmetic_type)
+
+static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
+{
+ /* TODO: handle complex + imaginary types */
+
+ /* § 6.3.1.8 Usual arithmetic conversions */
+ if(type_left == type_long_double || type_right == type_long_double) {
+ return type_long_double;
+ } else if(type_left == type_double || type_right == type_double) {
+ return type_double;
+ } else if(type_left == type_float || type_right == type_float) {
+ return type_float;
+ }
+
+ type_right = promote_integer(type_right);
+ type_left = promote_integer(type_left);
+
+ if(type_left == type_right)
+ return type_left;
+
+ bool signed_left = is_type_signed(type_left);
+ bool signed_right = is_type_signed(type_right);
+ if(get_rank(type_left) < get_rank(type_right)) {
+ if(signed_left == signed_right || !signed_right) {
+ return type_right;
+ } else {
+ return type_left;
+ }
+ } else {
+ if(signed_left == signed_right || !signed_left) {
+ return type_left;
+ } else {
+ return type_right;
+ }
+ }
+}
+
+static void semantic_binexpr_arithmetic(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ expression_t *right = expression->right;
+ type_t *type_left = skip_typeref(left->datatype);
+ type_t *type_right = skip_typeref(right->datatype);
+
+ if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
+ /* TODO: improve error message */
+ parser_print_error_prefix();
+ fprintf(stderr, "operation needs arithmetic types\n");
+ return;
+ }
+
+ type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
+ expression->left = create_implicit_cast(left, arithmetic_type);
+ expression->right = create_implicit_cast(right, arithmetic_type);
+ expression->expression.datatype = arithmetic_type;
+}
+
+static void semantic_add(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ expression_t *right = expression->right;
+ type_t *orig_type_left = left->datatype;
+ type_t *orig_type_right = right->datatype;
+ type_t *type_left = skip_typeref(orig_type_left);
+ type_t *type_right = skip_typeref(orig_type_right);
+
+ /* § 5.6.5 */
+ if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
+ type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
+ expression->left = create_implicit_cast(left, arithmetic_type);
+ expression->right = create_implicit_cast(right, arithmetic_type);
+ expression->expression.datatype = arithmetic_type;
+ return;
+ } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
+ expression->expression.datatype = type_left;
+ } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
+ expression->expression.datatype = type_right;
+ } else {
+ parser_print_error_prefix();
+ fprintf(stderr, "invalid operands to binary + (");
+ print_type(orig_type_left);
+ fprintf(stderr, ", ");
+ print_type(orig_type_right);
+ fprintf(stderr, ")\n");
+ }
+}
+
+static void semantic_sub(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ expression_t *right = expression->right;
+ type_t *orig_type_left = left->datatype;
+ type_t *orig_type_right = right->datatype;
+ type_t *type_left = skip_typeref(orig_type_left);
+ type_t *type_right = skip_typeref(orig_type_right);
+
+ /* § 5.6.5 */
+ if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
+ type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
+ expression->left = create_implicit_cast(left, arithmetic_type);
+ expression->right = create_implicit_cast(right, arithmetic_type);
+ expression->expression.datatype = arithmetic_type;
+ return;
+ } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
+ expression->expression.datatype = type_left;
+ } else if(type_left->type == TYPE_POINTER &&
+ type_right->type == TYPE_POINTER) {
+ if(!pointers_compatible(type_left, type_right)) {
+ parser_print_error_prefix();
+ fprintf(stderr, "pointers to incompatible objects to binary - (");
+ print_type(orig_type_left);
+ fprintf(stderr, ", ");
+ print_type(orig_type_right);
+ fprintf(stderr, ")\n");
+ } else {
+ expression->expression.datatype = type_ptrdiff_t;
+ }
+ } else {
+ parser_print_error_prefix();
+ fprintf(stderr, "invalid operands to binary - (");
+ print_type(orig_type_left);
+ fprintf(stderr, ", ");
+ print_type(orig_type_right);
+ fprintf(stderr, ")\n");
+ }
+}
+
+static void semantic_comparison(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ expression_t *right = expression->right;
+ type_t *type_left = left->datatype;
+ type_t *type_right = right->datatype;
+
+ /* TODO non-arithmetic types */
+ if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
+ type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
+ expression->left = create_implicit_cast(left, arithmetic_type);
+ expression->right = create_implicit_cast(right, arithmetic_type);
+ expression->expression.datatype = arithmetic_type;
+ }
+ expression->expression.datatype = type_int;
+}
+
+static void semantic_arithmetic_assign(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ expression_t *right = expression->right;
+ type_t *type_left = left->datatype;
+ type_t *type_right = right->datatype;
+
+ if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
+ /* TODO: improve error message */
+ parser_print_error_prefix();
+ fprintf(stderr, "operation needs arithmetic types\n");
+ return;
+ }
+
+ /* combined instructions are tricky. We can't create an implicit cast on
+ * the left side, because we need the uncasted form for the store.
+ * The ast2firm pass has to know that left_type must be right_type
+ * for the arithmeitc operation and create a cast by itself */
+ type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
+ expression->right = create_implicit_cast(right, arithmetic_type);
+ expression->expression.datatype = type_left;
+}
+
+static void semantic_logical_op(binary_expression_t *expression)
+{
+ /* TODO */
+ expression->expression.datatype = type_int;
+}
+
+static void semantic_binexpr_assign(binary_expression_t *expression)
+{
+ expression_t *left = expression->left;
+ type_t *type_left = left->datatype;
+
+ semantic_assign(type_left, &expression->right, "assignment");
+
+ expression->expression.datatype = type_left;
+}
+
+static void semantic_comma(binary_expression_t *expression)
+{
+ expression->expression.datatype = expression->right->datatype;
+}
+
+#define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
+static expression_t *parse_##binexpression_type(unsigned precedence, \
+ expression_t *left) \
+{ \
+ eat(token_type); \
+ \
+ expression_t *right = parse_sub_expression(precedence + lr); \
+ \
+ binary_expression_t *binexpr \
+ = allocate_ast_zero(sizeof(binexpr[0])); \
+ binexpr->expression.type = EXPR_BINARY; \
+ binexpr->type = binexpression_type; \
+ binexpr->left = left; \
+ binexpr->right = right; \
+ sfunc(binexpr); \
+ \
+ return (expression_t*) binexpr; \
+}
+
+CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
+CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
+CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
+CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
+CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
+CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
+CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
+CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
+ semantic_comparison, 1)
+CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
+CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
+ semantic_comparison, 1)
+CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
+CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
+/* TODO shift has a bit special semantic */
+CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
+ semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
+ semantic_binexpr_arithmetic, 1)
+CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
+ semantic_arithmetic_assign, 0)
+CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
+ semantic_arithmetic_assign, 0)
+
+static expression_t *parse_sub_expression(unsigned precedence)