Skip to content

Commit

Permalink
fix bug loading string literal
Browse files Browse the repository at this point in the history
  • Loading branch information
apache-hb committed Aug 11, 2024
1 parent 10724cd commit 1953cc8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
27 changes: 23 additions & 4 deletions data/examples/config/config.ct
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import win32;
import cstdlib as libc;

export struct StringView {
front: *char;
back: *char;
front: *const(char);
back: *const(char);
}

export struct ConfigEntry {
Expand Down Expand Up @@ -60,17 +60,36 @@ def readUntil(current: *char, terminator: char): StringView {
};
}

const kGlobalSectionName = "global";

export def openConfigFile(path: str): ConfigFile {
var file = mapFileHandle(path);
if file.fileView == libc::null {
return .{ };
}

var sections: [*]ConfigSection = libc::malloc(__sizeof(ConfigSection) * 4);
var sections: [*]ConfigSection = libc::malloc(__sizeof(ConfigSection) * 8);
var entries: [*]ConfigEntry = libc::malloc(__sizeof(ConfigEntry) * 16);

var index: uint = 0;
file.sections = sections;
file.entries = entries;

const innerName = kGlobalSectionName;

const globalName: StringView = .{
front = kGlobalSectionName,
back = &kGlobalSectionName[6]
};

sections[0] = .{
name = globalName,
firstEntry = 0,
lastEntry = 0
};

file.sectionCount = 1;

var index: uint = 0;
while index < file.fileSize {
const letter = file.fileView[index];
index = index + 1;
Expand Down
33 changes: 24 additions & 9 deletions src/language/ctu/src/sema/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ static tree_t *sema_load(tree_t *sema, const ctu_t *expr, const tree_t *implicit

tree_t *name = sema_decl_name(sema, expr->node, expr->path, &needs_load);

// TODO: this feels like a bit of a hack
// works around loading a string literal which turns into a char
// but we need to check if its a function first to prevent
// and assert from getting the type of a possibly unresolved function
if (!tree_is(name, eTreeDeclFunction))
{
const tree_t *type = tree_get_type(name);
if (tree_is(type, eTreeTypeString))
{
needs_load = false;
}
}

if (needs_load)
{
name = tree_expr_load(expr->node, name);
Expand Down Expand Up @@ -614,22 +627,24 @@ static tree_t *sema_init(ctu_sema_t *sema, const ctu_t *expr, const tree_t *impl
size_t len = vector_len(expr->inits);
for (size_t i = 0; i < len; i++)
{
ctu_t *init = vector_get(expr->inits, i);
CTASSERTF(init->kind == eCtuFieldInit, "invalid init kind %d", init->kind);
ctu_t *element = vector_get(expr->inits, i);
CTASSERTF(element->kind == eCtuFieldInit, "invalid init kind %d", element->kind);

tree_t *field = tree_ty_get_field(implicit_type, init->field);
tree_t *field = tree_ty_get_field(implicit_type, element->field);
if (field == NULL)
{
msg_notify(reports, &kEvent_FieldNotFound, init->node,
"field `%s` not found in struct `%s`", init->field,
msg_notify(reports, &kEvent_FieldNotFound, element->node,
"field `%s` not found in struct `%s`", element->field,
tree_to_string(implicit_type));
continue;
}

tree_t *value = ctu_sema_rvalue(sema, init->expr, tree_get_type(field));
tree_t *ref_type = tree_type_reference(init->node, "", tree_get_type(field));
tree_t *dst = tree_expr_field(init->node, ref_type, local, field);
tree_t *assign = tree_stmt_assign(init->node, dst, value);
const tree_t *field_type = tree_get_type(field);

tree_t *value = ctu_sema_rvalue(sema, element->expr, field_type);
tree_t *ref_type = tree_type_reference(element->node, "", field_type);
tree_t *dst = tree_expr_field(element->node, ref_type, local, field);
tree_t *assign = tree_stmt_assign(element->node, dst, value);

vector_push(&sema->block, assign);
set_add(fields, field);
Expand Down

0 comments on commit 1953cc8

Please sign in to comment.