aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMustafa Quraish <[email protected]>2022-02-04 01:31:55 -0500
committerMustafa Quraish <[email protected]>2022-02-04 01:31:55 -0500
commitcaf131c26deaf363b2fce95a7fda676729eb2e65 (patch)
tree75d6aa001969645fdbbe9a1890b85f63ce361f9f
parentMinor fixes, rearranging, whitespace trimming. No functional changes. (diff)
downloadcup-caf131c26deaf363b2fce95a7fda676729eb2e65.tar.xz
cup-caf131c26deaf363b2fce95a7fda676729eb2e65.zip
Some minor bug fixes
(1) Return correct size for unions (2) Make sure function name identifier doesn't exist (3) Assign `<anonymous>` as display name for nested compound types
-rw-r--r--src/parser.c8
-rw-r--r--src/types.c8
2 files changed, 12 insertions, 4 deletions
diff --git a/src/parser.c b/src/parser.c
index 30d080b..e917564 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -873,10 +873,14 @@ Node *parse_func(Lexer *lexer)
Token token;
token = assert_token(Lexer_next(lexer), TOKEN_FN);
+ token = assert_token(Lexer_next(lexer), TOKEN_IDENTIFIER);
+ if (identifier_exists(&token))
+ die_location(token.loc, "Function name already exists as an identifier");
+
Node *func = Node_new(AST_FUNC);
push_new_function(func);
- token = assert_token(Lexer_next(lexer), TOKEN_IDENTIFIER);
+
func->func.name = token.value.as_string;
parse_func_args(lexer, func);
@@ -922,6 +926,8 @@ Type *parse_struct_union_declaration(Lexer *lexer, bool is_global) {
struct_type->struct_name = token.value.as_string;
push_struct_definition(struct_type);
Lexer_next(lexer);
+ } else {
+ struct_type->struct_name = "<anonymous>";
}
assert_token(Lexer_next(lexer), TOKEN_OPEN_BRACE);
diff --git a/src/types.c b/src/types.c
index d004634..8ac4c77 100644
--- a/src/types.c
+++ b/src/types.c
@@ -37,6 +37,7 @@ i64 size_for_type(Type *type)
case TYPE_CHAR: return 1;
case TYPE_ARRAY: return type->array_size * size_for_type(type->ptr);
case TYPE_STRUCT: return type->fields.size;
+ case TYPE_UNION: return type->fields.size;
default: {
printf("Unknown type: %d\n", type->type);
assert(false && "Unreachable type");
@@ -89,10 +90,11 @@ bool is_int_type(Type *type)
bool is_struct_or_struct_ptr(Type *type)
{
- if (type->type == TYPE_STRUCT)
- return true;
- if (type->type == TYPE_PTR && type->ptr->type == TYPE_STRUCT)
+ if (type->type == TYPE_STRUCT || type->type == TYPE_UNION)
return true;
+ if (type->type == TYPE_PTR)
+ if (type->ptr->type == TYPE_STRUCT || type->ptr->type == TYPE_UNION)
+ return true;
return false;
}