Skip to content

Commit 0c336db

Browse files
committed
Fix some problems with boolean types
1 parent 84255f3 commit 0c336db

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

expr.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,12 +2769,24 @@ WidestType(AST *left, AST *right)
27692769
right = RemoveTypeModifiers(right);
27702770
lsize = TypeSize(left);
27712771
rsize = TypeSize(right);
2772+
if (IsBoolType(left)) {
2773+
lsize = 0; // bool gets demoted
2774+
}
2775+
if (IsBoolType(right)) {
2776+
rsize = 0; // bool gets demoted
2777+
}
27722778
if (!left && rsize >= LONG_SIZE) return right;
27732779
if (!right && lsize >= LONG_SIZE) return left;
27742780
if (left && right && left->kind != right->kind) {
27752781
if (lsize > LONG_SIZE || rsize > LONG_SIZE) {
2782+
if (IsUnsignedType(left) && IsUnsignedType(right)) {
2783+
return ast_type_unsigned_long64;
2784+
}
27762785
return ast_type_long64;
27772786
}
2787+
if (IsUnsignedType(left) && IsUnsignedType(right)) {
2788+
return ast_type_unsigned_long;
2789+
}
27782790
return ast_type_long;
27792791
}
27802792
if (lsize < rsize) return right;

frontends/types.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,10 @@ AST *CoerceOperatorTypes(AST *ast, AST *lefttype, AST *righttype)
966966
ast->right = FunctionAddress(ast->right);
967967
righttype = FunctionPointerType(righttype);
968968
}
969+
if (IsBoolType(rettype)) {
970+
// initially assume we will not be returning a long
971+
rettype = lefttype = ast_type_long;
972+
}
969973
//assert(ast->kind == AST_OPERATOR)
970974
if (!ast->left) {
971975
rettype = righttype;
@@ -1087,10 +1091,6 @@ AST *CoerceOperatorTypes(AST *ast, AST *lefttype, AST *righttype)
10871091
case K_NE:
10881092
case K_GE:
10891093
case '>':
1090-
case K_LTU:
1091-
case K_LEU:
1092-
case K_GTU:
1093-
case K_GEU:
10941094
CompileComparison(ast->d.ival, ast, lefttype, righttype);
10951095
return CurBoolType();
10961096
case K_NEGATE:
@@ -1290,13 +1290,27 @@ AST *CoerceAssignTypes(AST *line, int kind, AST **astptr, AST *desttype, AST *sr
12901290
}
12911291
}
12921292
srctype = NewAST(AST_REFTYPE, srctype, NULL);
1293-
}
1293+
}
1294+
if (desttype && IsBoolType(desttype) && expr) {
1295+
if (srctype && IsBoolType(srctype)) {
1296+
AstReportDone(&saveinfo);
1297+
return desttype;
1298+
}
1299+
if (expr->kind != AST_OPERATOR || expr->d.ival != K_NE) {
1300+
AST *left = DupAST(expr);
1301+
AST *check = AstOperator(K_NE, left, AstInteger(0));
1302+
*astptr = check;
1303+
AstReportDone(&saveinfo);
1304+
return desttype;
1305+
}
1306+
}
12941307
if (!desttype || !srctype) {
12951308
AstReportDone(&saveinfo);
12961309
return desttype;
12971310
}
12981311

12991312
if (IsRefType(srctype) && !IsRefType(desttype) && CompatibleTypes(desttype, srctype->left)) {
1313+
AstReportDone(&saveinfo);
13001314
return desttype;
13011315
}
13021316
if (IsFloatType(desttype)) {

0 commit comments

Comments
 (0)