Skip to content

Commit 5e3a29a

Browse files
committed
fixes #390
1 parent af71642 commit 5e3a29a

File tree

4 files changed

+333
-106
lines changed

4 files changed

+333
-106
lines changed

src/expressions.c

Lines changed: 140 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -919,81 +919,168 @@ int convert_to_number(struct parser_ctx* ctx, struct expression* p_expression_no
919919
"integer literal is too large to be represented in any integer type");
920920
}
921921

922-
if (suffix[0] == 'U')
922+
//This code follows the table in the standard.
923+
924+
static_assert(NUMBER_OF_TARGETS == 6, "does your target follow the C rules? (MSVC is different)");
925+
const bool is_msvc = (target == TARGET_X86_MSVC || target == TARGET_X64_MSVC);
926+
927+
const bool is_decimal_constant = (token->type == TK_COMPILER_DECIMAL_CONSTANT);
928+
const bool suffix_none = (suffix[0] == '\0');
929+
const bool suffix_u = (suffix[0] == 'U' && suffix[1] == '\0');
930+
931+
const bool suffix_l = ((suffix[0] == 'L' && suffix[1] == '\0')) ||
932+
((suffix[0] == 'i' && suffix[1] == '3' && suffix[2] == '2' && suffix[3] == '\0'));
933+
934+
const bool suffix_ul = (suffix[0] == 'U' && suffix[1] == 'L' && suffix[2] == '\0');
935+
936+
const bool suffix_ll = (suffix[0] == 'L' && suffix[1] == 'L' && suffix[2] == '\0') ||
937+
(suffix[0] == 'i' && suffix[1] == '6' || suffix[2] == '4' && suffix[2] == '\0');
938+
939+
const bool suffix_ull = (suffix[0] == 'U' && suffix[1] == 'L' && suffix[2] == 'L' && suffix[3] == '\0');
940+
941+
object_destroy(&p_expression_node->object);
942+
943+
if (suffix_none)
923944
{
924-
/*fixing the type that fits the size*/
925-
if (value <= unsigned_int_max_value && suffix[1] != 'L')
945+
if (value <= signed_int_max_value)
946+
{
947+
p_expression_node->object = object_make_signed_int(ctx->options.target, value);
948+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_INT;
949+
}
950+
else if (value <= unsigned_int_max_value && !is_decimal_constant)
926951
{
927-
object_destroy(&p_expression_node->object);
928952
p_expression_node->object = object_make_unsigned_int(ctx->options.target, value);
929-
p_expression_node->type.type_specifier_flags = (TYPE_SPECIFIER_INT | TYPE_SPECIFIER_UNSIGNED);
953+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_INT;
954+
}
955+
else if (value <= signed_long_max_value)
956+
{
957+
p_expression_node->object = object_make_signed_long(ctx->options.target, value);
958+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_LONG;
930959
}
931-
else if (value <= unsigned_long_max_value && suffix[2] != 'L')
960+
else if (value <= unsigned_long_max_value && (!is_decimal_constant || is_msvc))
932961
{
933-
object_destroy(&p_expression_node->object);
934-
p_expression_node->object = object_make_unsigned_long(target, value);
935-
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_LONG | TYPE_SPECIFIER_UNSIGNED;
962+
p_expression_node->object = object_make_unsigned_long(ctx->options.target, value);
963+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_LONG;
936964
}
937-
else //if (value <= ULLONG_MAX)
965+
else if (value <= signed_long_long_max_value)
966+
{
967+
p_expression_node->object = object_make_signed_long_long(ctx->options.target, value);
968+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_LONG_LONG;
969+
}
970+
else if (value <= unsigned_long_long_max_value && !is_decimal_constant)
938971
{
939-
object_destroy(&p_expression_node->object);
972+
p_expression_node->object = object_make_unsigned_long_long(ctx->options.target, value);
973+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_LONG_LONG;
974+
}
975+
else
976+
{
977+
compiler_diagnostic(W_IMPLICITLY_UNSIGNED_LITERAL,
978+
ctx,
979+
token,
980+
NULL,
981+
"integer literal is too large to be represented in a signed integer type, interpreting as unsigned");
982+
940983
p_expression_node->object = object_make_unsigned_long_long(ctx->options.target, value);
941984
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_LONG_LONG | TYPE_SPECIFIER_UNSIGNED;
942985
}
943986
}
944-
else
987+
else if (suffix_u)
945988
{
946-
static_assert(NUMBER_OF_TARGETS == 6, "does your target follow the C rules? (MSVC is different)");
947-
948-
/*fixing the type that fits the size*/
949-
if (value <= signed_int_max_value && suffix[0] != 'L')
989+
if (value <= unsigned_int_max_value)
950990
{
951-
object_destroy(&p_expression_node->object);
952-
p_expression_node->object = object_make_signed_int(ctx->options.target, value);
953-
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_INT;
991+
p_expression_node->object = object_make_unsigned_int(ctx->options.target, value);
992+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_INT;
954993
}
955-
else if (value <= signed_int_max_value && suffix[1] != 'L')
994+
else if (value <= unsigned_long_max_value)
956995
{
957-
object_destroy(&p_expression_node->object);
958-
p_expression_node->object = object_make_signed_long(target, value);
959-
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_LONG;
996+
p_expression_node->object = object_make_unsigned_long(ctx->options.target, value);
997+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_LONG;
960998
}
961-
else if ((target == TARGET_X86_MSVC || target == TARGET_X64_MSVC) &&
962-
(value <= unsigned_long_max_value) &&
963-
suffix[1] != 'L' /*!= LL*/)
999+
else //if (value <= unsigned_long_long_max_value)
9641000
{
965-
// ONLY MSVC, NON STANDARD, uses unsigned long instead of next big signed int
966-
object_destroy(&p_expression_node->object);
967-
p_expression_node->object = object_make_unsigned_long(target, value);
968-
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_LONG;
1001+
p_expression_node->object = object_make_unsigned_long_long(ctx->options.target, value);
1002+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_LONG_LONG;
9691003
}
970-
else if (value <= signed_long_max_value && suffix[1] != 'L' /*!= LL*/)
1004+
}
1005+
else if (suffix_l)
1006+
{
1007+
if (value <= signed_long_max_value)
9711008
{
972-
object_destroy(&p_expression_node->object);
973-
p_expression_node->object = object_make_signed_long(target, value);
1009+
p_expression_node->object = object_make_signed_long(ctx->options.target, value);
9741010
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_LONG;
9751011
}
1012+
else if (value <= unsigned_long_max_value && !is_decimal_constant)
1013+
{
1014+
p_expression_node->object = object_make_unsigned_long(ctx->options.target, value);
1015+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_LONG;
1016+
}
9761017
else if (value <= signed_long_long_max_value)
9771018
{
978-
object_destroy(&p_expression_node->object);
9791019
p_expression_node->object = object_make_signed_long_long(ctx->options.target, value);
9801020
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_LONG_LONG;
9811021
}
1022+
else if (value <= unsigned_long_long_max_value && !is_decimal_constant)
1023+
{
1024+
p_expression_node->object = object_make_unsigned_long_long(ctx->options.target, value);
1025+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_LONG_LONG;
1026+
}
9821027
else
9831028
{
984-
compiler_diagnostic(
985-
W_IMPLICITLY_UNSIGNED_LITERAL,
986-
ctx,
987-
token,
988-
NULL,
989-
"integer literal is too large to be represented in a signed integer type, interpreting as unsigned");
1029+
compiler_diagnostic(W_IMPLICITLY_UNSIGNED_LITERAL,
1030+
ctx,
1031+
token,
1032+
NULL,
1033+
"integer literal is too large to be represented in a signed integer type, interpreting as unsigned");
9901034

991-
object_destroy(&p_expression_node->object);
9921035
p_expression_node->object = object_make_unsigned_long_long(ctx->options.target, value);
9931036
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_LONG_LONG | TYPE_SPECIFIER_UNSIGNED;
9941037
}
9951038
}
1039+
else if (suffix_ul)
1040+
{
1041+
if (value <= unsigned_long_max_value)
1042+
{
1043+
p_expression_node->object = object_make_unsigned_long(ctx->options.target, value);
1044+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_LONG;
1045+
}
1046+
else //if (value <= unsigned_long_long_max_value)
1047+
{
1048+
p_expression_node->object = object_make_unsigned_long_long(ctx->options.target, value);
1049+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_LONG_LONG;
1050+
}
1051+
}
1052+
else if (suffix_ll)
1053+
{
1054+
if (value <= signed_long_long_max_value)
1055+
{
1056+
p_expression_node->object = object_make_signed_long_long(ctx->options.target, value);
1057+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_LONG_LONG;
1058+
}
1059+
else if (value <= unsigned_long_long_max_value && !is_decimal_constant)
1060+
{
1061+
p_expression_node->object = object_make_unsigned_long_long(ctx->options.target, value);
1062+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_LONG_LONG;
1063+
}
1064+
else
1065+
{
1066+
compiler_diagnostic(W_IMPLICITLY_UNSIGNED_LITERAL,
1067+
ctx,
1068+
token,
1069+
NULL,
1070+
"integer literal is too large to be represented in a signed integer type, interpreting as unsigned");
9961071

1072+
p_expression_node->object = object_make_unsigned_long_long(ctx->options.target, value);
1073+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_LONG_LONG | TYPE_SPECIFIER_UNSIGNED;
1074+
}
1075+
}
1076+
else if (suffix_ull)
1077+
{
1078+
//if (value <= unsigned_long_long_max_value && !is_decimal_constant)
1079+
{
1080+
p_expression_node->object = object_make_unsigned_long_long(ctx->options.target, value);
1081+
p_expression_node->type.type_specifier_flags = TYPE_SPECIFIER_UNSIGNED | TYPE_SPECIFIER_LONG_LONG;
1082+
}
1083+
}
9971084
}
9981085
break;
9991086

@@ -1338,7 +1425,7 @@ struct expression* _Owner _Opt primary_expression(struct parser_ctx* ctx, enum e
13381425
{
13391426
it = str_utf8_decode(it, &c);
13401427
if (it == NULL)
1341-
{
1428+
{
13421429
throw;
13431430
}
13441431
}
@@ -1381,7 +1468,7 @@ struct expression* _Owner _Opt primary_expression(struct parser_ctx* ctx, enum e
13811468
//u8"" also are char not (char8_t)
13821469
*p_new = object_make_char(ctx->options.target, value);
13831470
}
1384-
object_list_push(&p_expression_node->object.members, p_new);
1471+
object_list_push(&p_expression_node->object.members, p_new);
13851472
}
13861473

13871474
parser_match(ctx);
@@ -1550,7 +1637,7 @@ struct expression* _Owner _Opt primary_expression(struct parser_ctx* ctx, enum e
15501637
*/
15511638

15521639
p_expression_node->expression_type = PRIMARY_EXPRESSION_STATEMENT_EXPRESSION;
1553-
1640+
15541641
bool e = is_diagnostic_enabled(&ctx->options, W_EXPRESSION_RESULT_NOT_USED);
15551642
options_set_warning(&ctx->options, W_EXPRESSION_RESULT_NOT_USED, false);
15561643
p_expression_node->compound_statement = compound_statement(ctx);
@@ -1560,20 +1647,20 @@ struct expression* _Owner _Opt primary_expression(struct parser_ctx* ctx, enum e
15601647
{
15611648
throw;
15621649
}
1563-
1650+
15641651
/*
1565-
The last thing in the compound statement should be an expression followed
1566-
by a semicolon; the value of this subexpression serves as the value of
1567-
the entire construct. (If you use some other kind of statement last within
1568-
the braces, the construct has type void, and thus effectively no value.)
1652+
The last thing in the compound statement should be an expression followed
1653+
by a semicolon; the value of this subexpression serves as the value of
1654+
the entire construct. (If you use some other kind of statement last within
1655+
the braces, the construct has type void, and thus effectively no value.)
15691656
*/
15701657
struct expression* _Opt p_last_expression = NULL;
15711658
struct block_item* _Opt p = p_expression_node->compound_statement->block_item_list.head;
15721659
while (p)
15731660
{
1574-
if (p->next == NULL &&
1661+
if (p->next == NULL &&
15751662
p->unlabeled_statement &&
1576-
p->unlabeled_statement->expression_statement &&
1663+
p->unlabeled_statement->expression_statement &&
15771664
p->unlabeled_statement->expression_statement->expression_opt)
15781665
{
15791666
p_last_expression = p->unlabeled_statement->expression_statement->expression_opt;
@@ -1589,7 +1676,7 @@ struct expression* _Owner _Opt primary_expression(struct parser_ctx* ctx, enum e
15891676
else
15901677
{
15911678
p_expression_node->type = make_void_type();
1592-
}
1679+
}
15931680
}
15941681
else
15951682
{
@@ -5632,7 +5719,7 @@ void expression_delete(struct expression* _Owner _Opt p)
56325719
//explodindo
56335720
//object_destroy(&p->object);
56345721

5635-
5722+
56365723
free(p);
56375724
}
56385725
}

src/file.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
#include <ctype.h>
2-
3-
int main(void)
4-
{
5-
unsigned char c = '\xdf'; // German letter ß in ISO-8859-1
6-
7-
isalnum(c);
8-
9-
}
1+
#define TYPE(E, T) _Generic(typeof(E), T : 1, default: 0)
2+
3+
int main() {
4+
//static_assert(TYPE(0x80000000, unsigned int));
5+
static_assert(TYPE(2147483648, unsigned long));
6+
}

0 commit comments

Comments
 (0)