Cthulhu  0.2.10
Cthulhu compiler collection
resolve.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-3.0-only
2 
4 
5 #include "core/macros.h"
6 #include "cthulhu/tree/query.h"
7 #include "cthulhu/util/types.h"
8 #include "cthulhu/util/util.h"
9 
10 #include "base/panic.h"
11 
12 ctu_t *begin_resolve(tree_t *sema, tree_t *self, void *user, ctu_kind_t kind)
13 {
14  ctu_t *decl = user;
15  CTASSERTF(decl->kind == kind, "decl %s is not a %d", decl->name, kind);
16 
17  CT_UNUSED(sema);
18  CT_UNUSED(self);
19 
20  return decl;
21 }
22 
23 static bool is_array(const tree_t *type)
24 {
25  return tree_is(tree_follow_type(type), eTreeTypeArray);
26 }
27 
28 static bool is_pointer(const tree_t *type)
29 {
30  return tree_is(tree_follow_type(type), eTreeTypePointer);
31 }
32 
33 static bool is_opaque(const tree_t *type)
34 {
35  return tree_is(tree_follow_type(type), eTreeTypeOpaque);
36 }
37 
38 tree_t *ctu_cast_type(tree_t *sema, tree_t *expr, const tree_t *dst)
39 {
40  CTASSERT(sema != NULL);
41  CTASSERT(expr != NULL);
42  CTASSERT(dst != NULL);
43 
44  const tree_t *inner = tree_follow_type(tree_get_type(expr));
45 
46  // TODO: deduplicate casting logic
47 
48  // if we're casting to a pointer we should preserve the length information
49  if (is_array(inner) && is_pointer(dst))
50  {
51  if (util_types_equal(inner->ptr, dst->ptr))
52  {
53  const tree_t *elem = tree_ty_load_type(inner);
54  tree_t *ptr = tree_type_pointer(tree_get_node(expr), tree_get_name(dst), elem, inner->length);
55  return tree_expr_cast(expr->node, ptr, expr, eCastBit);
56  }
57  }
58 
59  if (is_array(dst) && is_pointer(inner))
60  {
61  if (util_types_equal(inner->ptr, dst->ptr))
62  {
63  return tree_expr_cast(expr->node, dst, expr, eCastBit);
64  }
65  }
66 
67  if (is_array(dst) && is_array(inner))
68  {
69  if (dst->length >= inner->length)
70  {
71  if (util_types_equal(dst->ptr, inner->ptr))
72  {
73  return tree_expr_cast(expr->node, dst, expr, eCastBit);
74  }
75  }
76  }
77 
78  if (is_pointer(dst) && is_opaque(inner))
79  {
80  return tree_expr_cast(expr->node, dst, expr, eCastBit);
81  }
82 
83  if (util_type_is_digit(inner) && util_type_is_digit(dst))
84  {
85  return tree_expr_cast(expr->node, dst, expr, eCastSignExtend);
86  }
87 
88  // TODO: deal with other casts
89  return expr;
90 }
CT_PUREFN CT_TREE_API const node_t * tree_get_node(const tree_t *tree)
Definition: context.c:94
CT_PUREFN CT_TREE_API const char * tree_get_name(const tree_t *tree)
Definition: context.c:102
CT_PUREFN CT_TREE_API const tree_t * tree_get_type(const tree_t *tree)
Definition: context.c:132
CT_PUREFN CT_TREE_API bool tree_is(const tree_t *self, tree_kind_t kind)
Definition: query.c:91
CT_PUREFN CT_TREE_API const tree_t * tree_ty_load_type(const tree_t *self)
get the type of a type after it has been loaded
Definition: query.c:267
ctu_kind_t
Definition: ast.h:21
#define CT_UNUSED(x)
mark a variable as unused
Definition: macros.h:46
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
#define CTASSERTF(expr,...)
assert a condition with a message and optional format arguments
Definition: panic.h:116
CT_BEGIN_API CT_UTIL_API bool util_types_equal(const tree_t *lhs, const tree_t *rhs)
compare two types for strict equality compares two types for exact equality, does not follow typedefs
Definition: util.c:33
CT_UTIL_API bool util_type_is_digit(const tree_t *type)
Definition: util.c:353
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_type_pointer(const node_t *node, const char *name, const tree_t *pointer, size_t length)
create a pointer type
Definition: tree.c:165
CT_TREE_API const tree_t * tree_follow_type(const tree_t *type)
Definition: decl.c:274
tree_t * ctu_cast_type(tree_t *sema, tree_t *expr, const tree_t *dst)
Definition: resolve.c:38
ctu_t * begin_resolve(tree_t *sema, tree_t *self, void *user, ctu_kind_t kind)
Definition: resolve.c:12
Definition: ast.h:86
ctu_kind_t kind
Definition: ast.h:87
char * name
Definition: ast.h:92
Definition: tree.h:67
size_t length
Definition: tree.h:174
const tree_t * ptr
Definition: tree.h:172
const node_t * node
Definition: tree.h:69