Cthulhu  0.2.10
Cthulhu compiler collection
context.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #include "common.h"
4 
5 #include "cthulhu/tree/context.h"
6 #include "cthulhu/tree/query.h"
7 #include "cthulhu/tree/tree.h"
8 
9 #include "base/panic.h"
10 #include <stdio.h>
11 
12 static const tree_tags_t kTreeTags[eTreeTotal] = {
13 #define TREE_KIND(ID, NAME, TAGS) [ID] = (TAGS),
14 #include "cthulhu/tree/tree.inc"
15 };
16 
18 {
19  CTASSERTF(kind < eTreeTotal, "invalid tree kind %d", kind);
20 
21  return kTreeTags[kind] & tags;
22 }
23 
24 bool tree_has_tag(const tree_t *tree, tree_tags_t tags)
25 {
26  CTASSERT(tree != NULL);
27  return kind_has_tag(tree->kind, tags);
28 }
29 
33 
34 #if CTU_ASSERTS
35 static const char *tree_kind_string(const tree_t *tree)
36 {
37  return tree_kind_to_string(tree_get_kind(tree));
38 }
39 #endif
40 
42 void tree_set_qualifiers(tree_t *tree, tree_quals_t qualifiers)
43 {
44  CTASSERTF(tree_has_tag(tree, eTagQual), "tree type %s does not have qualifiers", tree_kind_string(tree));
45 
46  tree->quals = qualifiers;
47 }
48 
51 {
52  CTASSERTF(tree_has_tag(tree, eTagQual), "tree type %s does not have qualifiers", tree_kind_string(tree));
53 
54  return tree->quals;
55 }
56 
59 {
60  CTASSERTF(tree_has_tag(tree, eTagStorage), "tree type %s does not have storage", tree_kind_string(tree));
61  if (storage.storage != NULL)
62  {
63  CTASSERTF(!tree_is(storage.storage, eTreeTypeReference), "storage cannot be a reference (%s)", tree_to_string(tree));
64  }
65 
66  tree->storage = storage;
67 }
68 
71 {
72  CTASSERTF(tree_has_tag(tree, eTagStorage), "tree type %s does not have storage", tree_kind_string(tree));
73 
74  return tree->storage;
75 }
76 
78 void tree_set_eval(tree_t *tree, eval_model_t model)
79 {
80  CTASSERTF(tree_has_tag(tree, eTagEval), "tree type %s does not have an evaluation model", tree_kind_string(tree));
81 
82  tree->eval_model = model;
83 }
84 
87 {
88  CTASSERTF(tree_has_tag(tree, eTagEval), "tree type %s does not have an evaluation model", tree_kind_string(tree));
89 
90  return tree->eval_model;
91 }
92 
94 const node_t *tree_get_node(const tree_t *tree)
95 {
96  CTASSERT(tree != NULL); // all trees have nodes
97 
98  return tree->node;
99 }
100 
101 STA_DECL
102 const char *tree_get_name(const tree_t *tree)
103 {
104  CTASSERTF(tree_has_tag(tree, eTagName), "tree type %s does not have a name", tree_kind_string(tree));
105 
106  if (tree_is(tree, eTreeError)) return tree->message;
107 
108  return tree->name;
109 }
110 
111 STA_DECL
112 const char *tree_get_user_name(const tree_t *tree)
113 {
114  CTASSERTF(tree_has_tag(tree, eTagName), "tree type %s does not have a symbol", tree_kind_string(tree));
115 
116  if (tree_is(tree, eTreeError)) return tree->message;
117 
118  return tree_is_symbol_anonymous(tree) ? "<anonymous>" : tree->name;
119 }
120 
121 STA_DECL
123 {
124  CTASSERTF(tree_has_tag(tree, eTagName), "tree type %s does not have a name", tree_kind_string(tree));
125 
126  if (tree_is(tree, eTreeError)) return false;
127 
128  return tree->name == NULL;
129 }
130 
131 STA_DECL
132 const tree_t *tree_get_type(const tree_t *tree)
133 {
134  CTASSERTF(tree_has_tag(tree, eTagHasType), "tree type %s does not have a type", tree_kind_string(tree));
135  CTASSERTF(tree->type != NULL, "tree type %s has not been set", tree_kind_string(tree));
136 
137  if (tree_is(tree, eTreeError)) return tree;
138 
139  return tree->type;
140 }
141 
142 STA_DECL
144 {
145  CTASSERT(tree != NULL);
146 
147  return tree->kind;
148 }
149 
150 void tree_set_type(tree_t *self, const tree_t *type)
151 {
152  CTASSERTF(tree_has_tag(self, eTagHasType), "tree type %s does not have a type", tree_kind_string(self));
153 
154  self->type = type;
155 }
CT_PUREFN CT_TREE_API bool tree_is(const tree_t *self, tree_kind_t kind)
Definition: query.c:91
CT_TREE_API const char * tree_to_string(const tree_t *self)
Definition: query.c:40
CT_BEGIN_API CT_TREE_API const char * tree_kind_to_string(tree_kind_t kind)
Definition: query.c:23
tree_tags_t
Definition: common.h:8
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
#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
tree_quals_t
all type qualifiers
Definition: ops.h:25
eval_model_t
tree evaluation model
Definition: ops.h:112
tree_kind_t
all tree node types
Definition: ops.h:17
void tree_set_type(tree_t *self, const tree_t *type)
Definition: context.c:150
@ eTreeTotal
Definition: ops.h:20
a position in a source file
Definition: node.h:23
storage for a value
Definition: context.h:23
const tree_t * storage
the underlying storage type
Definition: context.h:25
Definition: tree.h:67
tree_kind_t kind
Definition: tree.h:68
const char * message
Definition: tree.h:120
const char * name
the name of the declaration
Definition: tree.h:163
const tree_t * type
Definition: tree.h:70
tree_quals_t quals
Definition: tree.h:166
eval_model_t eval_model
Definition: tree.h:167
tree_storage_t storage
Definition: tree.h:216
const node_t * node
Definition: tree.h:69
STA_DECL tree_storage_t tree_get_storage(const tree_t *tree)
Definition: context.c:70
STA_DECL eval_model_t tree_get_eval(const tree_t *tree)
Definition: context.c:86
STA_DECL const tree_t * tree_get_type(const tree_t *tree)
Definition: context.c:132
STA_DECL const node_t * tree_get_node(const tree_t *tree)
Definition: context.c:94
STA_DECL const char * tree_get_name(const tree_t *tree)
Definition: context.c:102
STA_DECL tree_quals_t tree_get_qualifiers(const tree_t *tree)
Definition: context.c:50
STA_DECL void tree_set_storage(tree_t *tree, tree_storage_t storage)
Definition: context.c:58
STA_DECL void tree_set_qualifiers(tree_t *tree, tree_quals_t qualifiers)
Definition: context.c:42
STA_DECL void tree_set_eval(tree_t *tree, eval_model_t model)
Definition: context.c:78
STA_DECL bool tree_is_symbol_anonymous(const tree_t *tree)
Definition: context.c:122
STA_DECL const char * tree_get_user_name(const tree_t *tree)
Definition: context.c:112
bool kind_has_tag(tree_kind_t kind, tree_tags_t tags)
Definition: context.c:17
STA_DECL tree_kind_t tree_get_kind(const tree_t *tree)
Definition: context.c:143
bool tree_has_tag(const tree_t *tree, tree_tags_t tags)
Definition: context.c:24