Skip to content

Commit e5af5a9

Browse files
author
thradams
committed
refactoring
1 parent 760f133 commit e5af5a9

File tree

3 files changed

+105
-71
lines changed

3 files changed

+105
-71
lines changed

src/lib.c

Lines changed: 65 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8313,10 +8313,16 @@ struct declaration_specifiers* declaration_specifiers(struct parser_ctx* ctx, st
83138313

83148314
struct static_assert_declaration
83158315
{
8316-
struct token* first;
8317-
struct token* last;
8318-
struct expression* p_conditional_expression;
8319-
struct token* text_opt;
8316+
/*
8317+
static_assert-declaration:
8318+
"static_assert" ( constant-expression , string-literal ) ;
8319+
"static_assert" ( constant-expression ) ;
8320+
*/
8321+
8322+
struct token* first_token;
8323+
struct token* last_token;
8324+
struct expression* constant_expression;
8325+
struct token* string_literal_opt;
83208326
};
83218327
struct static_assert_declaration* static_assert_declaration(struct parser_ctx* ctx, struct error* error);
83228328

@@ -8340,10 +8346,21 @@ struct attribute* attribute(struct parser_ctx* ctx, struct error* error);
83408346

83418347
struct storage_class_specifier
83428348
{
8349+
/*
8350+
storage-class-specifier:
8351+
"auto"
8352+
"constexpr"
8353+
"extern"
8354+
"register"
8355+
"static"
8356+
"thread_local"
8357+
"typedef"
8358+
*/
83438359
enum storage_class_specifier_flags flags;
8360+
83448361
struct token* token;
83458362
};
8346-
struct storage_class_specifier* storage_class_specifier(struct parser_ctx* ctx, struct error* error);
8363+
struct storage_class_specifier* storage_class_specifier(struct parser_ctx* ctx);
83478364

83488365
struct function_specifier
83498366
{
@@ -15634,7 +15651,7 @@ struct declaration_specifier* declaration_specifier(struct parser_ctx* ctx, stru
1563415651
struct declaration_specifier* pDeclaration_specifier = calloc(1, sizeof * pDeclaration_specifier);
1563515652
if (first_of_storage_class_specifier(ctx))
1563615653
{
15637-
pDeclaration_specifier->storage_class_specifier = storage_class_specifier(ctx, error);
15654+
pDeclaration_specifier->storage_class_specifier = storage_class_specifier(ctx);
1563815655
}
1563915656
else if (first_of_type_specifier_qualifier(ctx))
1564015657
{
@@ -15729,68 +15746,71 @@ struct init_declarator_list init_declarator_list(struct parser_ctx* ctx,
1572915746
return init_declarator_list;
1573015747
}
1573115748

15732-
struct storage_class_specifier* storage_class_specifier(struct parser_ctx* ctx, struct error* error)
15749+
struct storage_class_specifier* storage_class_specifier(struct parser_ctx* ctx)
1573315750
{
15734-
if (error->code)
15735-
return NULL;
1573615751
if (ctx->current == NULL)
1573715752
return NULL;
1573815753

15739-
struct storage_class_specifier* p_storage_class_specifier = calloc(1, sizeof(struct storage_class_specifier));
15754+
struct storage_class_specifier* new_storage_class_specifier = calloc(1, sizeof(struct storage_class_specifier));
15755+
if (new_storage_class_specifier == NULL)
15756+
return NULL;
1574015757

15741-
p_storage_class_specifier->token = ctx->current;
15758+
new_storage_class_specifier->token = ctx->current;
1574215759
switch (ctx->current->type)
1574315760
{
1574415761
case TK_KEYWORD_TYPEDEF:
15745-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_TYPEDEF;
15762+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_TYPEDEF;
1574615763
break;
1574715764
case TK_KEYWORD_EXTERN:
15748-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_EXTERN;
15765+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_EXTERN;
1574915766
break;
1575015767
case TK_KEYWORD_CONSTEXPR:
15751-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_CONSTEXPR;
15768+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_CONSTEXPR;
1575215769
break;
1575315770
case TK_KEYWORD_STATIC:
15754-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_STATIC;
15771+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_STATIC;
1575515772
break;
1575615773
case TK_KEYWORD__THREAD_LOCAL:
15757-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_THREAD_LOCAL;
15774+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_THREAD_LOCAL;
1575815775
break;
1575915776
case TK_KEYWORD_AUTO:
15760-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_AUTO;
15777+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_AUTO;
1576115778
break;
1576215779
case TK_KEYWORD_REGISTER:
15763-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_REGISTER;
15780+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_REGISTER;
1576415781
break;
1576515782
default:
1576615783
assert(false);
1576715784
}
15785+
15786+
/*
15787+
TODO
15788+
thread_local may appear with static or extern,
15789+
auto may appear with all the others except typedef138), and
15790+
constexpr may appear with auto, register, or static.
15791+
*/
1576815792

1576915793
parser_match(ctx);
15770-
//'typedef'
15771-
//'extern'
15772-
//'static'
15773-
//'_Thread_local'
15774-
//'auto'
15775-
//'register'
15776-
return p_storage_class_specifier;
15794+
return new_storage_class_specifier;
1577715795
}
1577815796

1577915797
struct typeof_specifier_argument* typeof_specifier_argument(struct parser_ctx* ctx, struct error* error)
1578015798
{
15781-
struct typeof_specifier_argument* p_typeof_specifier_argument = calloc(1, sizeof(struct typeof_specifier_argument));
15799+
struct typeof_specifier_argument* new_typeof_specifier_argument = calloc(1, sizeof(struct typeof_specifier_argument));
15800+
if (new_typeof_specifier_argument)
15801+
return NULL;
15802+
1578215803
if (first_of_type_name(ctx))
1578315804
{
15784-
15785-
p_typeof_specifier_argument->type_name = type_name(ctx, error);
15805+
new_typeof_specifier_argument->type_name = type_name(ctx, error);
1578615806
}
1578715807
else
1578815808
{
1578915809
struct expression_ctx ectx = { 0 };
15790-
p_typeof_specifier_argument->expression = expression(ctx, error, &ectx);
15810+
new_typeof_specifier_argument->expression = expression(ctx, error, &ectx);
1579115811
}
1579215812

15793-
return p_typeof_specifier_argument;
15813+
return new_typeof_specifier_argument;
1579415814
}
1579515815

1579615816
bool first_of_typeof_specifier(struct parser_ctx* ctx)
@@ -17461,33 +17481,33 @@ struct static_assert_declaration* static_assert_declaration(struct parser_ctx* c
1746117481
struct static_assert_declaration* p_static_assert_declaration = calloc(1, sizeof(struct static_assert_declaration));
1746217482
try
1746317483
{
17464-
p_static_assert_declaration->first = ctx->current;
17484+
p_static_assert_declaration->first_token = ctx->current;
1746517485
struct token* position = ctx->current;
1746617486
parser_match_tk(ctx, TK_KEYWORD__STATIC_ASSERT, error);
1746717487
parser_match_tk(ctx, '(', error);
1746817488
struct expression_ctx ectx = { .bConstantExpressionRequired = true };
17469-
p_static_assert_declaration->p_conditional_expression = constant_expression(ctx, error, &ectx);
17489+
p_static_assert_declaration->constant_expression = constant_expression(ctx, error, &ectx);
1747017490

1747117491
if (error->code != 0)
1747217492
throw;
1747317493

1747417494
if (ctx->current->type == ',')
1747517495
{
1747617496
parser_match(ctx);
17477-
p_static_assert_declaration->text_opt = ctx->current;
17497+
p_static_assert_declaration->string_literal_opt = ctx->current;
1747817498
parser_match_tk(ctx, TK_STRING_LITERAL, error);
1747917499
}
1748017500

1748117501
parser_match_tk(ctx, ')', error);
17482-
p_static_assert_declaration->last = ctx->current;
17502+
p_static_assert_declaration->last_token = ctx->current;
1748317503
parser_match_tk(ctx, ';', error);
1748417504

17485-
if (p_static_assert_declaration->p_conditional_expression->constant_value == 0)
17505+
if (p_static_assert_declaration->constant_expression->constant_value == 0)
1748617506
{
17487-
if (p_static_assert_declaration->text_opt)
17507+
if (p_static_assert_declaration->string_literal_opt)
1748817508
{
1748917509
parser_seterror_with_token(ctx, position, "_Static_assert failed %s\n",
17490-
p_static_assert_declaration->text_opt->lexeme);
17510+
p_static_assert_declaration->string_literal_opt->lexeme);
1749117511
}
1749217512
else
1749317513
{
@@ -20225,16 +20245,16 @@ static void visit_block_item_list(struct visit_ctx* ctx, struct block_item_list*
2022520245

2022620246
static void visit_static_assert_declaration(struct visit_ctx* ctx, struct static_assert_declaration* p_static_assert_declaration, struct error* error)
2022720247
{
20228-
visit_expression(ctx, p_static_assert_declaration->p_conditional_expression, error);
20248+
visit_expression(ctx, p_static_assert_declaration->constant_expression, error);
2022920249

2023020250
if (ctx->target < LANGUAGE_C11)
2023120251
{
2023220252
/*
2023320253
* Vamos apagar a parte do static assert. Não adianta so commentar
2023420254
* pq poderia ter um comentario dentro. (so se verificar que nao tem)
2023520255
*/
20236-
for (struct token* p = p_static_assert_declaration->first;
20237-
p != p_static_assert_declaration->last->next;
20256+
for (struct token* p = p_static_assert_declaration->first_token;
20257+
p != p_static_assert_declaration->last_token->next;
2023820258
p = p->next)
2023920259
{
2024020260
/*
@@ -20245,19 +20265,19 @@ static void visit_static_assert_declaration(struct visit_ctx* ctx, struct static
2024520265
}
2024620266
else if (ctx->target == LANGUAGE_C11)
2024720267
{
20248-
if (p_static_assert_declaration->text_opt == NULL)
20268+
if (p_static_assert_declaration->string_literal_opt == NULL)
2024920269
{
20250-
struct token* rp = previous_parser_token(p_static_assert_declaration->last);
20270+
struct token* rp = previous_parser_token(p_static_assert_declaration->last_token);
2025120271
rp = previous_parser_token(rp);
2025220272

2025320273
struct token_list list1 = tokenizer(", \"error\"", "", 0, TK_FLAG_NONE, error);
2025420274
token_list_insert_after(&ctx->ast.token_list, rp, &list1);
2025520275
}
20256-
if (strcmp(p_static_assert_declaration->first->lexeme, "static_assert") == 0)
20276+
if (strcmp(p_static_assert_declaration->first_token->lexeme, "static_assert") == 0)
2025720277
{
2025820278
/*C23 has static_assert but C11 _Static_assert*/
20259-
free(p_static_assert_declaration->first->lexeme);
20260-
p_static_assert_declaration->first->lexeme = strdup("_Static_assert");
20279+
free(p_static_assert_declaration->first_token->lexeme);
20280+
p_static_assert_declaration->first_token->lexeme = strdup("_Static_assert");
2026120281
}
2026220282
}
2026320283
}

src/parser.c

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,7 @@ struct declaration_specifier* declaration_specifier(struct parser_ctx* ctx, stru
17311731
struct declaration_specifier* pDeclaration_specifier = calloc(1, sizeof * pDeclaration_specifier);
17321732
if (first_of_storage_class_specifier(ctx))
17331733
{
1734-
pDeclaration_specifier->storage_class_specifier = storage_class_specifier(ctx, error);
1734+
pDeclaration_specifier->storage_class_specifier = storage_class_specifier(ctx);
17351735
}
17361736
else if (first_of_type_specifier_qualifier(ctx))
17371737
{
@@ -1826,68 +1826,71 @@ struct init_declarator_list init_declarator_list(struct parser_ctx* ctx,
18261826
return init_declarator_list;
18271827
}
18281828

1829-
struct storage_class_specifier* storage_class_specifier(struct parser_ctx* ctx, struct error* error)
1829+
struct storage_class_specifier* storage_class_specifier(struct parser_ctx* ctx)
18301830
{
1831-
if (error->code)
1832-
return NULL;
18331831
if (ctx->current == NULL)
18341832
return NULL;
18351833

1836-
struct storage_class_specifier* p_storage_class_specifier = calloc(1, sizeof(struct storage_class_specifier));
1834+
struct storage_class_specifier* new_storage_class_specifier = calloc(1, sizeof(struct storage_class_specifier));
1835+
if (new_storage_class_specifier == NULL)
1836+
return NULL;
18371837

1838-
p_storage_class_specifier->token = ctx->current;
1838+
new_storage_class_specifier->token = ctx->current;
18391839
switch (ctx->current->type)
18401840
{
18411841
case TK_KEYWORD_TYPEDEF:
1842-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_TYPEDEF;
1842+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_TYPEDEF;
18431843
break;
18441844
case TK_KEYWORD_EXTERN:
1845-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_EXTERN;
1845+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_EXTERN;
18461846
break;
18471847
case TK_KEYWORD_CONSTEXPR:
1848-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_CONSTEXPR;
1848+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_CONSTEXPR;
18491849
break;
18501850
case TK_KEYWORD_STATIC:
1851-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_STATIC;
1851+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_STATIC;
18521852
break;
18531853
case TK_KEYWORD__THREAD_LOCAL:
1854-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_THREAD_LOCAL;
1854+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_THREAD_LOCAL;
18551855
break;
18561856
case TK_KEYWORD_AUTO:
1857-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_AUTO;
1857+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_AUTO;
18581858
break;
18591859
case TK_KEYWORD_REGISTER:
1860-
p_storage_class_specifier->flags = STORAGE_SPECIFIER_REGISTER;
1860+
new_storage_class_specifier->flags = STORAGE_SPECIFIER_REGISTER;
18611861
break;
18621862
default:
18631863
assert(false);
18641864
}
1865+
1866+
/*
1867+
TODO
1868+
thread_local may appear with static or extern,
1869+
auto may appear with all the others except typedef138), and
1870+
constexpr may appear with auto, register, or static.
1871+
*/
18651872

18661873
parser_match(ctx);
1867-
//'typedef'
1868-
//'extern'
1869-
//'static'
1870-
//'_Thread_local'
1871-
//'auto'
1872-
//'register'
1873-
return p_storage_class_specifier;
1874+
return new_storage_class_specifier;
18741875
}
18751876

18761877
struct typeof_specifier_argument* typeof_specifier_argument(struct parser_ctx* ctx, struct error* error)
18771878
{
1878-
struct typeof_specifier_argument* p_typeof_specifier_argument = calloc(1, sizeof(struct typeof_specifier_argument));
1879+
struct typeof_specifier_argument* new_typeof_specifier_argument = calloc(1, sizeof(struct typeof_specifier_argument));
1880+
if (new_typeof_specifier_argument)
1881+
return NULL;
1882+
18791883
if (first_of_type_name(ctx))
18801884
{
1881-
1882-
p_typeof_specifier_argument->type_name = type_name(ctx, error);
1885+
new_typeof_specifier_argument->type_name = type_name(ctx, error);
18831886
}
18841887
else
18851888
{
18861889
struct expression_ctx ectx = { 0 };
1887-
p_typeof_specifier_argument->expression = expression(ctx, error, &ectx);
1890+
new_typeof_specifier_argument->expression = expression(ctx, error, &ectx);
18881891
}
18891892

1890-
return p_typeof_specifier_argument;
1893+
return new_typeof_specifier_argument;
18911894
}
18921895

18931896
bool first_of_typeof_specifier(struct parser_ctx* ctx)

src/parser.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,21 @@ struct attribute* attribute(struct parser_ctx* ctx, struct error* error);
185185

186186
struct storage_class_specifier
187187
{
188+
/*
189+
storage-class-specifier:
190+
"auto"
191+
"constexpr"
192+
"extern"
193+
"register"
194+
"static"
195+
"thread_local"
196+
"typedef"
197+
*/
188198
enum storage_class_specifier_flags flags;
199+
189200
struct token* token;
190201
};
191-
struct storage_class_specifier* storage_class_specifier(struct parser_ctx* ctx, struct error* error);
202+
struct storage_class_specifier* storage_class_specifier(struct parser_ctx* ctx);
192203

193204
struct function_specifier
194205
{

0 commit comments

Comments
 (0)