commit 2be84a5d9a548d102eb37f48d6aaa485f146e206
parent 0ef535b1fb46ac588b778c494f4c28e36218147f
Author: Drew DeVault <sir@cmpwn.com>
Date: Tue, 29 Dec 2020 09:08:14 -0500
check: implement string literals
Diffstat:
5 files changed, 31 insertions(+), 11 deletions(-)
diff --git a/include/expr.h b/include/expr.h
@@ -105,15 +105,15 @@ struct array_constant {
};
union expression_constant {
- struct {
- char *sval;
- size_t ssz;
- };
bool bval;
double fval;
intmax_t ival;
uintmax_t uval;
uint32_t rune;
+ struct {
+ size_t len;
+ char *value;
+ } string;
struct array_constant *array;
// TODO: Struct constants
};
diff --git a/include/types.h b/include/types.h
@@ -143,7 +143,9 @@ extern const struct type
builtin_type_const_uintptr,
builtin_type_const_rune,
builtin_type_const_size,
- // Aggregate
- builtin_type_const_ptr_char;
+ // etc
+ builtin_type_const_ptr_char,
+ builtin_type_str,
+ builtin_type_const_str;
#endif
diff --git a/src/check.c b/src/check.c
@@ -2,6 +2,7 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "ast.h"
#include "check.h"
#include "expr.h"
@@ -330,9 +331,14 @@ check_expr_constant(struct context *ctx,
case TYPE_STORAGE_ARRAY:
check_expr_array(ctx, aexpr, expr);
break;
+ case TYPE_STORAGE_STRING:
+ expr->constant.string.len = aexpr->constant.string.len;
+ expr->constant.string.value = xcalloc(1, aexpr->constant.string.len);
+ memcpy(expr->constant.string.value, aexpr->constant.string.value,
+ aexpr->constant.string.len);
+ break;
case TYPE_STORAGE_F32:
case TYPE_STORAGE_F64:
- case TYPE_STORAGE_STRING:
case TYPE_STORAGE_STRUCT:
assert(0); // TODO
case TYPE_STORAGE_CHAR:
diff --git a/src/type_store.c b/src/type_store.c
@@ -152,12 +152,13 @@ builtin_type_for_storage(enum type_storage storage, bool is_const)
return &builtin_type_void; // const void and void are the same type
case TYPE_STORAGE_NULL:
return &builtin_type_null; // const null and null are the same type
+ case TYPE_STORAGE_STRING:
+ return is_const ? &builtin_type_const_str : &builtin_type_str;
case TYPE_STORAGE_ALIAS:
case TYPE_STORAGE_ARRAY:
case TYPE_STORAGE_FUNCTION:
case TYPE_STORAGE_POINTER:
case TYPE_STORAGE_SLICE:
- case TYPE_STORAGE_STRING:
case TYPE_STORAGE_STRUCT:
case TYPE_STORAGE_TAGGED_UNION:
case TYPE_STORAGE_UNION:
@@ -194,6 +195,7 @@ atype_hash(struct type_store *store, const struct ast_type *type)
case TYPE_STORAGE_UINT:
case TYPE_STORAGE_UINTPTR:
case TYPE_STORAGE_VOID:
+ case TYPE_STORAGE_STRING:
break; // built-ins
case TYPE_STORAGE_ALIAS:
assert(0); // TODO
@@ -217,7 +219,6 @@ atype_hash(struct type_store *store, const struct ast_type *type)
hash = atype_hash(store, type->pointer.referent);
break;
case TYPE_STORAGE_SLICE:
- case TYPE_STORAGE_STRING:
case TYPE_STORAGE_STRUCT:
case TYPE_STORAGE_TAGGED_UNION:
case TYPE_STORAGE_UNION:
@@ -252,6 +253,7 @@ type_hash(struct type_store *store, const struct type *type)
case TYPE_STORAGE_UINT:
case TYPE_STORAGE_UINTPTR:
case TYPE_STORAGE_VOID:
+ case TYPE_STORAGE_STRING:
break; // built-ins
case TYPE_STORAGE_ALIAS:
assert(0); // TODO
@@ -275,7 +277,6 @@ type_hash(struct type_store *store, const struct type *type)
hash = type_hash(store, type->pointer.referent);
break;
case TYPE_STORAGE_SLICE:
- case TYPE_STORAGE_STRING:
case TYPE_STORAGE_STRUCT:
case TYPE_STORAGE_TAGGED_UNION:
case TYPE_STORAGE_UNION:
diff --git a/src/types.c b/src/types.c
@@ -400,7 +400,7 @@ builtin_type_const_size = {
.align = 8,
};
-// Selected aggregate type singletons
+// Others
const struct type builtin_type_const_ptr_char = {
.storage = TYPE_STORAGE_POINTER,
.flags = TYPE_CONST,
@@ -409,4 +409,15 @@ const struct type builtin_type_const_ptr_char = {
.pointer = {
.referent = &builtin_type_char,
},
+},
+builtin_type_str = {
+ .storage = TYPE_STORAGE_STRING,
+ .size = 24, // XXX: ARCH
+ .align = 8,
+},
+builtin_type_const_str = {
+ .storage = TYPE_STORAGE_STRING,
+ .flags = TYPE_CONST,
+ .size = 24, // XXX: ARCH
+ .align = 8,
};