Skip to content

Commit 5da2468

Browse files
chqrliebvdberg
authored andcommitted
Compiler: make string literals const
* modify type of string literals to const arrays of char * update tests
1 parent 62fe62b commit 5da2468

13 files changed

+51
-47
lines changed

analyser/module_analyser_expr.c2

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,16 @@ fn QualType Analyser.analyseConditionalOperator(Analyser* ma, Expr** e_ptr) {
306306
}
307307

308308
fn bool Analyser.checkAssignment(Analyser* ma, Expr* assignee, QualType tleft, const char* msg, SrcLoc loc) {
309+
if (!assignee.isLValue()) {
310+
ma.errorRange(assignee.getLoc(), assignee.getRange(), "lvalue required as %s", msg);
311+
return false;
312+
}
313+
314+
if (tleft.isArray()) {
315+
ma.error(loc, "array type '%s' is not assignable", tleft.diagName());
316+
return false;
317+
}
318+
309319
if (tleft.isConst()) {
310320
//assignee.dump();
311321
if (assignee.isIdentifier()) {
@@ -342,16 +352,6 @@ fn bool Analyser.checkAssignment(Analyser* ma, Expr* assignee, QualType tleft, c
342352
return false;
343353
}
344354

345-
if (!assignee.isLValue()) {
346-
ma.errorRange(assignee.getLoc(), assignee.getRange(), "lvalue required as %s", msg);
347-
return false;
348-
}
349-
350-
if (tleft.isArray()) {
351-
ma.error(loc, "array type '%s' is not assignable", tleft.diagName());
352-
return false;
353-
}
354-
355355
return true;
356356
}
357357

ast/string_type_pool.c2

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ fn QualType StringTypePool.get(StringTypePool* p, u32 len) {
7070
// Note: re-use char[x] existing types
7171
for (u32 i=0; i<p.count; i++) {
7272
StringTypeSlot* s = &p.slots[i];
73-
if (s.len == len) return QualType.create(s.type_);
73+
if (s.len == len) {
74+
QualType qt = QualType.create(s.type_);
75+
qt.setConst();
76+
return qt;
77+
}
7478
}
7579
if (p.count == p.capacity) p.resize(p.capacity * 2);
7680

@@ -81,6 +85,7 @@ fn QualType StringTypePool.get(StringTypePool* p, u32 len) {
8185
p.count++;
8286
QualType qt = QualType.create(t);
8387
t.setCanonicalType(qt);
88+
qt.setConst();
8489
return qt;
8590
}
8691

test/c_generator/constants/string_escape_chars.c2t

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
// @file{file1}
66
module test;
77

8-
char* a = " ";
9-
char* b = "a\rb";
10-
char* c = "a\tb";
11-
char* d = "a\nb";
12-
char* e = "a\0b";
13-
char* f = "a'b";
14-
char* g = "a\"b";
15-
char* h = "a\\b";
8+
const char* a = " ";
9+
const char* b = "a\rb";
10+
const char* c = "a\tb";
11+
const char* d = "a\nb";
12+
const char* e = "a\0b";
13+
const char* f = "a'b";
14+
const char* g = "a\"b";
15+
const char* h = "a\\b";
1616

1717
// @expect{atleast, cgen/build.c}
1818

19-
static char* test_a = " ";
20-
static char* test_b = "a\rb";
21-
static char* test_c = "a\tb";
22-
static char* test_d = "a\nb";
23-
static char* test_e = "a\0b";
24-
static char* test_f = "a'b";
25-
static char* test_g = "a\"b";
26-
static char* test_h = "a\\b";
19+
static const char* test_a = " ";
20+
static const char* test_b = "a\rb";
21+
static const char* test_c = "a\tb";
22+
static const char* test_d = "a\nb";
23+
static const char* test_e = "a\0b";
24+
static const char* test_f = "a'b";
25+
static const char* test_g = "a\"b";
26+
static const char* test_h = "a\\b";
2727

test/c_generator/constants/string_literals.c2t

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ module test1;
88
// const with inits
99
public const char[] AAA = "aaa";
1010
public const char[4] BBB = "bbb";
11-
public const char* ccc = "ccc";
11+
//public const char* ccc = "ccc";
1212

1313
// non-const with inits
1414
public char[] ddd = "ddd";
1515
public char[4] eee = "eee";
16-
public char* fff = "fff";
16+
public const char* fff = "fff";
1717

18-
// 'const' without init
18+
// 'const' without init (actually non-const)
1919
public const char* ggg;
2020

2121
// non-const without init
@@ -32,10 +32,9 @@ static const char test1_AAA[4] = "aaa";
3232

3333
static const char test1_BBB[4] = "bbb";
3434

35-
static const char* test1_ccc = "ccc";
3635
static char test1_ddd[4] = "ddd";
3736
static char test1_eee[4] = "eee";
38-
static char* test1_fff = "fff";
37+
static const char* test1_fff = "fff";
3938
static const char* test1_ggg = NULL;
4039
static char test1_hhh[3] = { };
4140
static char* test1_iii = NULL;

test/expr/assign_to_non_assignable.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn void test1() {
66
}
77

88
fn void test2() {
9-
"foo" = "bar"; // @error{array type 'char[4]' is not assignable}
9+
"foo" = "bar"; // @error{array type 'const char[4]' is not assignable}
1010
}
1111

1212
fn void test3() {

test/stmt/for_const.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ module test;
22

33
public fn i32 main() {
44
for (const char *p = "abc"; *p; p++) {}
5-
for (volatile char *p = "abc"; *p; p++) {}
5+
for (const volatile char *p = "abc"; *p; p++) {}
66
return 0;
77
}

test/types/string_init.c2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @warnings{no-unused}
22
module test;
33

4-
char* a = "AAA";
5-
u8* b = "BBB";
6-
i8* c = "CCC"; // @error{invalid type conversion from 'char[4]' to 'i8*'}
7-
4+
const char* a = "AAA";
5+
const u8* b = "BBB";
6+
const i8* c = "CCC"; // @error{invalid type conversion from 'const char[4]' to 'const i8*'}
7+
char* d = "CCC"; // @error{pointer conversion discards const qualifier}

test/types/struct_array_init_ok.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
module test;
33

44
type S struct {
5-
u8* cp;
5+
const u8* cp;
66
i32 a;
77
}
88

test/types/symbol_single/init_global_invalid_type.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
module test;
33

44
fn void test1() {
5-
i32 a = "test"; // @error{invalid type conversion from 'char[5]' to 'i32'}
5+
i32 a = "test"; // @error{invalid type conversion from 'const char[5]' to 'i32'}
66
}
77

test/vars/array_large.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @warnings{no-unused}
22
module test;
33

4-
char*[] a = {
4+
const char*[] a = {
55
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
66
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
77
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29",

0 commit comments

Comments
 (0)