Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_value_assignment_target.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Interface afw_value Implementation for assignment target
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
9 
15 #include "afw_internal.h"
16 
17 #define impl_afw_value_optional_release NULL
18 #define impl_afw_value_get_reference NULL
19 
20 #define impl_afw_value_get_evaluated_metas \
21  afw_value_internal_get_evaluated_metas_default
22 
23 /* Declares and rti/inf defines for interface afw_value */
24 #define AFW_IMPLEMENTATION_ID "assignment_target"
25 #define AFW_IMPLEMENTATION_INF_SPECIFIER AFW_DEFINE_CONST_DATA
26 #define AFW_IMPLEMENTATION_INF_LABEL afw_value_assignment_target_inf
28 
29 
30 typedef struct {
31  const afw_utf8_t id;
32  const afw_utf8_t description;
34 
35 
36 /* Target types */
37 static impl_type_entry_t
38 impl_assignment_types[] = {
39 #define XX(id, description) \
40 { AFW_UTF8_LITERAL(AFW_STRINGIFY(id)), \
41  AFW_UTF8_LITERAL(description) \
42 },
44 #undef XX
45  { { NULL } }
46 };
47 
48 
49 /* Create function for assignment_target value. */
50 AFW_DEFINE(const afw_value_t *)
52  const afw_compile_value_contextual_t *contextual,
53  const afw_compile_assignment_target_t *assignment_target,
54  const afw_pool_t *p,
55  afw_xctx_t *xctx)
56 {
58 
60  result->inf = &afw_value_assignment_target_inf;
61  result->contextual = contextual;
62  result->assignment_target = assignment_target;
63 
64  return (const afw_value_t *)result;
65 }
66 
67 
68 
69 /*
70  * Implementation of method optional_evaluate for interface afw_value.
71  */
72 const afw_value_t *
74  const afw_value_t * instance,
75  const afw_pool_t * p,
76  afw_xctx_t *xctx)
77 {
78  const afw_value_assignment_target_t *self =
79  (const afw_value_assignment_target_t *)instance;
80 
81  if (self->assignment_target->target_type ==
82  afw_compile_assignment_target_type_variable_reference)
83  {
84  return (const afw_value_t *)self->assignment_target->variable_reference;
85  }
86 
87  return instance;
88 }
89 
90 /*
91  * Implementation of method get_data_type for interface afw_value.
92  */
93 const afw_data_type_t *
94 impl_afw_value_get_data_type(
95  const afw_value_t * instance,
96  afw_xctx_t *xctx)
97 {
98  return NULL;
99 }
100 
101 /*
102  * Implementation of method get_evaluated_data_type for interface afw_value.
103  */
104 const afw_data_type_t *
105 impl_afw_value_get_evaluated_data_type(
106  const afw_value_t * instance,
107  afw_xctx_t *xctx)
108 {
109  return NULL;
110 }
111 
112 /*
113  * Implementation of method get_evaluated_data_type_parameter for interface
114  * afw_value.
115  */
116 const afw_utf8_t *
117 impl_afw_value_get_evaluated_data_type_parameter(
118  const afw_value_t * instance,
119  afw_xctx_t *xctx)
120 {
121  return NULL;
122 }
123 
124 /*
125  * Implementation of method get_evaluated_meta for interface afw_value.
126  */
127 const afw_value_t *
129  const afw_value_t *instance,
130  const afw_pool_t *p,
131  afw_xctx_t *xctx)
132 {
134  AFW_THROW_ERROR_Z(general, "Method not implemented.", xctx);
135 
136 }
137 
138 
139 static void
140 impl_assignment_element_produce_compiler_listing(
142  const afw_writer_t *writer,
143  afw_xctx_t *xctx)
144 {
145  afw_value_compiler_listing_begin_value(writer, NULL, NULL, xctx);
146  afw_writer_write_z(writer, "assignment_element: [", xctx);
147  afw_writer_write_eol(writer, xctx);
148  afw_writer_increment_indent(writer, xctx);
149 
150  if (self->assignment_target) {
151  afw_value_produce_compiler_listing(self->assignment_target, writer, xctx);
152  }
153  else {
154  afw_writer_write_z(writer, "<skipped>", xctx);
155  afw_writer_write_eol(writer, xctx);
156  }
157 
158  if (self->default_value) {
159  afw_writer_write_z(writer, "default: ", xctx);
160  afw_value_produce_compiler_listing(self->default_value, writer, xctx);
161  }
162 
163  afw_writer_decrement_indent(writer, xctx);
164  afw_writer_write_z(writer, "]", xctx);
165  afw_writer_write_eol(writer, xctx);
166 }
167 
168 
169 static void
170 impl_assignment_property_produce_compiler_listing(
172  const afw_writer_t *writer,
173  afw_xctx_t *xctx)
174 {
175  afw_value_compiler_listing_begin_value(writer, NULL, NULL, xctx);
176  if (self->is_rename) {
177  afw_writer_write_z(writer, "assignment_property rename: [", xctx);
178  afw_writer_write_eol(writer, xctx);
179  afw_writer_increment_indent(writer, xctx);
180 
181  afw_writer_write_z(writer, "property_name: ", xctx);
182  afw_writer_write_utf8(writer, self->property_name, xctx);
183  afw_writer_write_eol(writer, xctx);
184 
185  impl_assignment_element_produce_compiler_listing(
186  self->assignment_element, writer, xctx);
187  }
188  else {
189  afw_writer_write_z(writer, "assignment_property reference: [", xctx);
190  afw_writer_write_eol(writer, xctx);
191  afw_writer_increment_indent(writer, xctx);
192 
194  (const afw_value_t *)self->variable_reference, writer, xctx);
195 
196  if (self->default_value) {
197  afw_writer_write_z(writer, "default: ", xctx);
198  afw_value_produce_compiler_listing(self->default_value, writer, xctx);
199  }
200  }
201 
202  afw_writer_decrement_indent(writer, xctx);
203  afw_writer_write_z(writer, "]", xctx);
204  afw_writer_write_eol(writer, xctx);
205 }
206 
207 
208 static void
209 impl_list_destructure_produce_compiler_listing(
210  const afw_compile_list_destructure_t *self ,
211  const afw_writer_t *writer,
212  afw_xctx_t *xctx)
213 {
215 
216  afw_value_compiler_listing_begin_value(writer, NULL, NULL, xctx);
217  afw_writer_write_z(writer, "list_destructure: [", xctx);
218  afw_writer_write_eol(writer, xctx);
219  afw_writer_increment_indent(writer, xctx);
220 
221  for (ae = self->assignment_element; ae; ae = ae->next) {
222  impl_assignment_element_produce_compiler_listing(ae, writer, xctx);
223  }
224 
225  if (self->rest) {
226  afw_writer_write_z(writer, "rest: ", xctx);
227  afw_value_produce_compiler_listing(self->rest, writer, xctx);
228  }
229 
230  afw_writer_decrement_indent(writer, xctx);
231  afw_writer_write_z(writer, "]", xctx);
232  afw_writer_write_eol(writer, xctx);
233 }
234 
235 
236 static void
237 impl_object_destructure_produce_compiler_listing(
239  const afw_writer_t *writer,
240  afw_xctx_t *xctx)
241 {
243 
244  afw_value_compiler_listing_begin_value(writer, NULL, NULL, xctx);
245  afw_writer_write_z(writer, "object_destructure: [", xctx);
246  afw_writer_write_eol(writer, xctx);
247  afw_writer_increment_indent(writer, xctx);
248 
249  for (ap = self->assignment_property; ap; ap = ap->next) {
250  impl_assignment_property_produce_compiler_listing(ap, writer, xctx);
251  }
252 
253  if (self->rest) {
254  afw_writer_write_z(writer, "rest: ", xctx);
255  afw_value_produce_compiler_listing(self->rest, writer, xctx);
256  }
257 
258  afw_writer_decrement_indent(writer, xctx);
259  afw_writer_write_z(writer, "]", xctx);
260  afw_writer_write_eol(writer, xctx);
261 }
262 
263 
264 /*
265  * Implementation of method compiler_listing for interface afw_value.
266  */
267 void
268 impl_afw_value_produce_compiler_listing(
269  const afw_value_t *instance,
270  const afw_writer_t *writer,
271  afw_xctx_t *xctx)
272 {
273  const afw_value_assignment_target_t *self =
274  (const afw_value_assignment_target_t *)instance;
275 
276  afw_value_compiler_listing_begin_value(writer, instance,
277  self->contextual, xctx);
278  afw_writer_write_z(writer, " ", xctx);
279  afw_writer_write_utf8(writer,
280  &impl_assignment_types[self->assignment_target->assignment_type].id,
281  xctx);
282  afw_writer_write_z(writer, ": [", xctx);
283  afw_writer_write_eol(writer, xctx);
284  afw_writer_increment_indent(writer, xctx);
285 
286  switch (self->assignment_target->target_type) {
287  case afw_compile_assignment_target_type_list_destructure:
288  impl_list_destructure_produce_compiler_listing(
289  self->assignment_target->list_destructure, writer, xctx);
290  break;
291 
292  case afw_compile_assignment_target_type_object_destructure:
293  impl_object_destructure_produce_compiler_listing(
294  self->assignment_target->object_destructure, writer, xctx);
295  break;
296 
297  case afw_compile_assignment_target_type_variable_reference:
299  (const afw_value_t *)self->assignment_target->variable_reference,
300  writer, xctx);
301  break;
302 
303  case afw_compile_assignment_target_type_max_type:
304  default:
305  AFW_THROW_ERROR_Z(code, "Invalid case", xctx);
306  }
307 
308  afw_writer_decrement_indent(writer, xctx);
309  afw_writer_write_z(writer, "]", xctx);
310  afw_writer_write_eol(writer, xctx);
311 }
312 
313 /*
314  * Implementation of method decompile for interface afw_value.
315  */
316 void
317 impl_afw_value_decompile(
318  const afw_value_t * instance,
319  const afw_writer_t * writer,
320  afw_xctx_t *xctx)
321 {
322  AFW_THROW_ERROR_Z(general,
323  "Decompile is not supported for assignment target.",
324  xctx);
325 }
326 
327 /*
328  * Implementation of method get_info for interface afw_value.
329  */
330 void
331 impl_afw_value_get_info(
332  const afw_value_t *instance,
333  afw_value_info_t *info,
334  const afw_pool_t *p,
335  afw_xctx_t *xctx)
336 {
337  const afw_value_assignment_target_t *self =
338  (const afw_value_assignment_target_t *)instance;
339 
340  afw_memory_clear(info);
341  info->value_inf_id = &instance->inf->rti.implementation_id;
342  info->contextual = self->contextual;
343 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
Interface afw_interface implementation declares.
#define AFW_COMPILE_INTERNAL_ASSIGNMENT_TYPE_MAP(XX)
Assignment type Map.
#define AFW_THROW_ERROR_Z(code, message_z, xctx)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:283
#define afw_memory_clear(to)
Clear preallocated memory for sizeof(*(to)).
Definition: afw_memory.h:47
#define afw_pool_calloc_type(instance, type, xctx)
Macro to allocate cleared memory to hold type in pool.
Definition: afw_pool.h:167
const afw_value_t * impl_afw_value_get_evaluated_meta(const afw_value_t *instance, const afw_pool_t *p, afw_xctx_t *xctx)
const afw_value_t * impl_afw_value_optional_evaluate(const afw_value_t *instance, const afw_pool_t *p, afw_xctx_t *xctx)
#define afw_value_produce_compiler_listing(instance, writer, xctx)
Call method produce_compiler_listing of interface afw_value.
afw_value_assignment_target_inf
Value annotated inf.
Definition: afw_value.h:225
afw_value_assignment_target_create(const afw_compile_value_contextual_t *contextual, const afw_compile_assignment_target_t *assignment_target, const afw_pool_t *p, afw_xctx_t *xctx)
Create assignment target value.
#define afw_writer_increment_indent(instance, xctx)
Call method increment_indent of interface afw_writer.
#define afw_writer_write_eol(instance, xctx)
Call method write_eol of interface afw_writer.
#define afw_writer_decrement_indent(instance, xctx)
Call method decrement_indent of interface afw_writer.
#define afw_writer_write_z(writer, s_z, xctx)
Call afw_writer_write() with zero terminated string.
Definition: afw_writer.h:35
#define afw_writer_write_utf8(writer, S, xctx)
Call afw_writer_write() with a afw_utf8_t string.
Definition: afw_writer.h:45
Contextual information provided in some values.
Interface afw_data_type public struct.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Struct for assignment target value.
Filled in by afw_value get_info method.
Definition: afw_value.h:49
Interface afw_value public struct.
Interface afw_writer public struct.
Interface afw_xctx public struct.