Cthulhu  0.2.10
Cthulhu compiler collection
default.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 
3 #include "ctu/sema/default.h"
4 
5 #include "base/panic.h"
6 #include "cthulhu/tree/query.h"
7 #include "cthulhu/tree/tree.h"
8 #include "ctu/sema/sema.h"
9 
10 static tree_t *get_zero(const node_t *node, const tree_t *type)
11 {
12  tree_t *value = tree_expr_digit_int(node, type, 0);
13  return tree_expr_cast(node, type, value, eCastBit);
14 }
15 
16 static tree_t *get_null(const node_t *node, const tree_t *type)
17 {
18  tree_t *digit = ctu_get_int_type(eDigitPtr, eSignUnsigned);
19  tree_t *value = get_zero(node, digit);
20  return tree_expr_cast(node, type, value, eCastBit);
21 }
22 
23 const tree_t *ctu_get_default_value(const node_t *node, const tree_t *type)
24 {
25  type = tree_follow_type(type);
26  tree_kind_t kind = tree_get_kind(type);
27  switch (kind)
28  {
29  case eTreeTypeOpaque:
30  case eTreeTypePointer:
31  return get_null(node, type);
32  case eTreeTypeDigit:
33  return get_zero(node, type);
34  case eTreeTypeBool:
35  return tree_expr_bool(node, type, false);
36 
37  case eTreeTypeEmpty:
38  case eTreeTypeUnit:
39  CT_NEVER("cannot get default value for empty or unit type");
40 
41  default:
42  CT_NEVER("unimplemented default value for type %s", tree_to_string(type));
43  }
44 }
CT_PUREFN CT_TREE_API tree_kind_t tree_get_kind(const tree_t *tree)
Definition: context.c:143
CT_TREE_API const char * tree_to_string(const tree_t *self)
Definition: query.c:40
tree_t * ctu_get_int_type(digit_t digit, sign_t sign)
Definition: sema.c:202
const tree_t * ctu_get_default_value(const node_t *node, const tree_t *type)
Definition: default.c:23
#define CT_NEVER(...)
assert that a code path is never reached
Definition: panic.h:136
CT_TREE_API tree_t * tree_expr_cast(const node_t *node, const tree_t *type, const tree_t *expr, tree_cast_t cast)
create a cast expression
Definition: tree.c:251
CT_TREE_API tree_t * tree_expr_digit_int(const node_t *node, const tree_t *type, int value)
Definition: tree.c:226
CT_TREE_API tree_t * tree_expr_bool(const node_t *node, const tree_t *type, bool value)
Definition: tree.c:208
tree_kind_t
all tree node types
Definition: ops.h:17
CT_TREE_API const tree_t * tree_follow_type(const tree_t *type)
Definition: decl.c:274
a position in a source file
Definition: node.h:23
Definition: tree.h:67