blob: cc3b678e40b318ca97bd573f4b18b4e0314f1d2e [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_node.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema compilation of common nodes.
5 *
6 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Christian Hopps32874e12021-05-01 09:43:54 -040015#define _GNU_SOURCE /* asprintf, strdup */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020016
17#include "schema_compile_node.h"
18
19#include <assert.h>
20#include <ctype.h>
21#include <stddef.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "common.h"
28#include "compat.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020029#include "dict.h"
30#include "log.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020031#include "plugins.h"
Radek Krejci77114102021-03-10 15:21:57 +010032#include "plugins_exts_compile.h"
Radek Krejci04699f02021-03-22 21:50:29 +010033#include "plugins_internal.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020034#include "plugins_types.h"
35#include "schema_compile.h"
36#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010037#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "set.h"
39#include "tree.h"
40#include "tree_data.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020042#include "tree_schema.h"
43#include "tree_schema_internal.h"
44#include "xpath.h"
45
46static struct lysc_ext_instance *
47lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
48{
49 /* TODO - extensions, increase refcount */
50 (void) ctx;
51 (void) orig;
52 return NULL;
53}
54
55/**
56 * @brief Add/replace a leaf default value in unres.
57 * Can also be used for a single leaf-list default value.
58 *
59 * @param[in] ctx Compile context.
60 * @param[in] leaf Leaf with the default value.
61 * @param[in] dflt Default value to use.
62 * @return LY_ERR value.
63 */
64static LY_ERR
65lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
66{
67 struct lysc_unres_dflt *r = NULL;
68 uint32_t i;
69
Michal Vasko7c565922021-06-10 14:58:27 +020070 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010071 return LY_SUCCESS;
72 }
73
Michal Vasko405cc9e2020-12-01 12:01:27 +010074 for (i = 0; i < ctx->unres->dflts.count; ++i) {
75 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020076 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +010077 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020078 lysp_qname_free(ctx->ctx, r->dflt);
79 free(r->dflt);
80 break;
81 }
82 }
83 if (!r) {
84 /* add new unres item */
85 r = calloc(1, sizeof *r);
86 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
87 r->leaf = leaf;
88
Michal Vasko405cc9e2020-12-01 12:01:27 +010089 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020090 }
91
92 r->dflt = malloc(sizeof *r->dflt);
93 LY_CHECK_GOTO(!r->dflt, error);
94 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
95
96 return LY_SUCCESS;
97
98error:
99 free(r->dflt);
100 LOGMEM(ctx->ctx);
101 return LY_EMEM;
102}
103
104/**
105 * @brief Add/replace a leaf-list default value(s) in unres.
106 *
107 * @param[in] ctx Compile context.
108 * @param[in] llist Leaf-list with the default value.
109 * @param[in] dflts Sized array of the default values.
110 * @return LY_ERR value.
111 */
112static LY_ERR
113lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
114{
115 struct lysc_unres_dflt *r = NULL;
116 uint32_t i;
117
Michal Vasko7c565922021-06-10 14:58:27 +0200118 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100119 return LY_SUCCESS;
120 }
121
Michal Vasko405cc9e2020-12-01 12:01:27 +0100122 for (i = 0; i < ctx->unres->dflts.count; ++i) {
123 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200124 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100125 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200126 lysp_qname_free(ctx->ctx, r->dflt);
127 free(r->dflt);
128 r->dflt = NULL;
129 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
130 r->dflts = NULL;
131 break;
132 }
133 }
134 if (!r) {
135 r = calloc(1, sizeof *r);
136 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
137 r->llist = llist;
138
Michal Vasko405cc9e2020-12-01 12:01:27 +0100139 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200140 }
141
142 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
143
144 return LY_SUCCESS;
145}
146
147/**
148 * @brief Duplicate the compiled pattern structure.
149 *
150 * Instead of duplicating memory, the reference counter in the @p orig is increased.
151 *
152 * @param[in] orig The pattern structure to duplicate.
153 * @return The duplicated structure to use.
154 */
155static struct lysc_pattern *
156lysc_pattern_dup(struct lysc_pattern *orig)
157{
158 ++orig->refcount;
159 return orig;
160}
161
162/**
163 * @brief Duplicate the array of compiled patterns.
164 *
165 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
166 *
167 * @param[in] ctx Libyang context for logging.
168 * @param[in] orig The patterns sized array to duplicate.
169 * @return New sized array as a copy of @p orig.
170 * @return NULL in case of memory allocation error.
171 */
172static struct lysc_pattern **
173lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
174{
175 struct lysc_pattern **dup = NULL;
176 LY_ARRAY_COUNT_TYPE u;
177
178 assert(orig);
179
180 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
181 LY_ARRAY_FOR(orig, u) {
182 dup[u] = lysc_pattern_dup(orig[u]);
183 LY_ARRAY_INCREMENT(dup);
184 }
185 return dup;
186}
187
188/**
189 * @brief Duplicate compiled range structure.
190 *
191 * @param[in] ctx Libyang context for logging.
192 * @param[in] orig The range structure to be duplicated.
193 * @return New compiled range structure as a copy of @p orig.
194 * @return NULL in case of memory allocation error.
195 */
196static struct lysc_range *
197lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
198{
199 struct lysc_range *dup;
200 LY_ERR ret;
201
202 assert(orig);
203
204 dup = calloc(1, sizeof *dup);
205 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
206 if (orig->parts) {
207 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100208 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200209 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
210 }
211 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
212 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
213 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
214
215 return dup;
216cleanup:
217 free(dup);
218 (void) ret; /* set but not used due to the return type */
219 return NULL;
220}
221
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200222/**
223 * @brief Compile information from the when statement
224 *
225 * @param[in] ctx Compile context.
226 * @param[in] when_p Parsed when structure.
227 * @param[in] flags Flags of the parsed node with the when statement.
228 * @param[in] ctx_node Context node for the when statement.
229 * @param[out] when Pointer where to store pointer to the created compiled when structure.
230 * @return LY_ERR value.
231 */
232static LY_ERR
233lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
234 struct lysc_when **when)
235{
236 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200237 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200238
239 *when = calloc(1, sizeof **when);
240 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
241 (*when)->refcount = 1;
242 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200243 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci8df109d2021-04-23 12:19:08 +0200244 LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200245 (*when)->context = (struct lysc_node *)ctx_node;
246 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
247 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100248 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200249 (*when)->flags = flags & LYS_STATUS_MASK;
250
251done:
252 return ret;
253}
254
255LY_ERR
256lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, const struct lysc_node *ctx_node,
257 struct lysc_node *node, struct lysc_when **when_c)
258{
259 struct lysc_when **new_when, ***node_when;
260
261 assert(when_p);
262
263 /* get the when array */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100264 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200265
266 /* create new when pointer */
267 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
268 if (!when_c || !(*when_c)) {
269 /* compile when */
270 LY_CHECK_RET(lys_compile_when_(ctx, when_p, flags, ctx_node, new_when));
271
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200272 /* remember the compiled when for sharing */
273 if (when_c) {
274 *when_c = *new_when;
275 }
276 } else {
277 /* use the previously compiled when */
278 ++(*when_c)->refcount;
279 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100280 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200281
Michal Vasko7c565922021-06-10 14:58:27 +0200282 if (!(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100283 /* do not check "when" semantics in a grouping, but repeat the check for different node because
284 * of dummy node check */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100285 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200286 }
287
288 return LY_SUCCESS;
289}
290
291/**
292 * @brief Compile information from the must statement
293 * @param[in] ctx Compile context.
294 * @param[in] must_p The parsed must statement structure.
295 * @param[in,out] must Prepared (empty) compiled must structure to fill.
296 * @return LY_ERR value.
297 */
298static LY_ERR
299lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
300{
301 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200302 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200303
304 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200305 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, must_p->arg.str, strlen(must_p->arg.str),
Radek Krejci8df109d2021-04-23 12:19:08 +0200306 LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200307 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
308 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
309 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
310 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100311 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200312
313done:
314 return ret;
315}
316
317/**
318 * @brief Validate and normalize numeric value from a range definition.
319 * @param[in] ctx Compile context.
320 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
321 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
322 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
323 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
324 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
325 * @param[in] value String value of the range boundary.
326 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
327 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
328 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
329 */
330static LY_ERR
331range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
332 size_t *len, char **valcopy)
333{
334 size_t fraction = 0, size;
335
336 *len = 0;
337
338 assert(value);
339 /* parse value */
340 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
341 return LY_EVALID;
342 }
343
344 if ((value[*len] == '-') || (value[*len] == '+')) {
345 ++(*len);
346 }
347
348 while (isdigit(value[*len])) {
349 ++(*len);
350 }
351
352 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
353 if (basetype == LY_TYPE_DEC64) {
354 goto decimal;
355 } else {
356 *valcopy = strndup(value, *len);
357 return LY_SUCCESS;
358 }
359 }
360 fraction = *len;
361
362 ++(*len);
363 while (isdigit(value[*len])) {
364 ++(*len);
365 }
366
367 if (basetype == LY_TYPE_DEC64) {
368decimal:
369 assert(frdigits);
370 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100371 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200372 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100373 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200374 return LY_EINVAL;
375 }
376 if (fraction) {
377 size = (*len) + (frdigits - ((*len) - 1 - fraction));
378 } else {
379 size = (*len) + frdigits + 1;
380 }
381 *valcopy = malloc(size * sizeof **valcopy);
382 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
383
384 (*valcopy)[size - 1] = '\0';
385 if (fraction) {
386 memcpy(&(*valcopy)[0], &value[0], fraction);
387 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
388 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
389 } else {
390 memcpy(&(*valcopy)[0], &value[0], *len);
391 memset(&(*valcopy)[*len], '0', frdigits);
392 }
393 }
394 return LY_SUCCESS;
395}
396
397/**
398 * @brief Check that values in range are in ascendant order.
399 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
400 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
401 * max can be also equal.
402 * @param[in] value Current value to check.
403 * @param[in] prev_value The last seen value.
404 * @return LY_SUCCESS or LY_EEXIST for invalid order.
405 */
406static LY_ERR
407range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
408{
409 if (unsigned_value) {
410 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
411 return LY_EEXIST;
412 }
413 } else {
414 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
415 return LY_EEXIST;
416 }
417 }
418 return LY_SUCCESS;
419}
420
421/**
422 * @brief Set min/max value of the range part.
423 * @param[in] ctx Compile context.
424 * @param[in] part Range part structure to fill.
425 * @param[in] max Flag to distinguish if storing min or max value.
426 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
427 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
428 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
429 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
430 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
431 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
432 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
433 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
434 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
435 * frdigits value), LY_EMEM.
436 */
437static LY_ERR
438range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
439 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
440{
441 LY_ERR ret = LY_SUCCESS;
442 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100443 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200444
445 if (value) {
446 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
447 LY_CHECK_GOTO(ret, finalize);
448 }
449 if (!valcopy && base_range) {
450 if (max) {
451 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
452 } else {
453 part->min_64 = base_range->parts[0].min_64;
454 }
455 if (!first) {
456 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
457 }
458 goto finalize;
459 }
460
461 switch (basetype) {
462 case LY_TYPE_INT8: /* range */
463 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100464 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200465 } else if (max) {
466 part->max_64 = INT64_C(127);
467 } else {
468 part->min_64 = INT64_C(-128);
469 }
470 if (!ret && !first) {
471 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
472 }
473 break;
474 case LY_TYPE_INT16: /* range */
475 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100476 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
477 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200478 } else if (max) {
479 part->max_64 = INT64_C(32767);
480 } else {
481 part->min_64 = INT64_C(-32768);
482 }
483 if (!ret && !first) {
484 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
485 }
486 break;
487 case LY_TYPE_INT32: /* range */
488 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100489 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
490 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200491 } else if (max) {
492 part->max_64 = INT64_C(2147483647);
493 } else {
494 part->min_64 = INT64_C(-2147483648);
495 }
496 if (!ret && !first) {
497 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
498 }
499 break;
500 case LY_TYPE_INT64: /* range */
501 case LY_TYPE_DEC64: /* range */
502 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100503 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
504 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200505 } else if (max) {
506 part->max_64 = INT64_C(9223372036854775807);
507 } else {
508 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
509 }
510 if (!ret && !first) {
511 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
512 }
513 break;
514 case LY_TYPE_UINT8: /* range */
515 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100516 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200517 } else if (max) {
518 part->max_u64 = UINT64_C(255);
519 } else {
520 part->min_u64 = UINT64_C(0);
521 }
522 if (!ret && !first) {
523 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
524 }
525 break;
526 case LY_TYPE_UINT16: /* range */
527 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100528 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200529 } else if (max) {
530 part->max_u64 = UINT64_C(65535);
531 } else {
532 part->min_u64 = UINT64_C(0);
533 }
534 if (!ret && !first) {
535 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
536 }
537 break;
538 case LY_TYPE_UINT32: /* range */
539 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100540 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
541 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200542 } else if (max) {
543 part->max_u64 = UINT64_C(4294967295);
544 } else {
545 part->min_u64 = UINT64_C(0);
546 }
547 if (!ret && !first) {
548 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
549 }
550 break;
551 case LY_TYPE_UINT64: /* range */
552 case LY_TYPE_STRING: /* length */
553 case LY_TYPE_BINARY: /* length */
554 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100555 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
556 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200557 } else if (max) {
558 part->max_u64 = UINT64_C(18446744073709551615);
559 } else {
560 part->min_u64 = UINT64_C(0);
561 }
562 if (!ret && !first) {
563 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
564 }
565 break;
566 default:
567 LOGINT(ctx->ctx);
568 ret = LY_EINT;
569 }
570
571finalize:
572 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100573 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200574 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
575 length_restr ? "length" : "range", valcopy ? valcopy : *value);
576 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100577 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200578 "Invalid %s restriction - invalid value \"%s\".",
579 length_restr ? "length" : "range", valcopy ? valcopy : *value);
580 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100581 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200582 "Invalid %s restriction - values are not in ascending order (%s).",
583 length_restr ? "length" : "range",
584 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
585 } else if (!ret && value) {
586 *value = *value + len;
587 }
588 free(valcopy);
589 return ret;
590}
591
592/**
593 * @brief Compile the parsed range restriction.
594 * @param[in] ctx Compile context.
595 * @param[in] range_p Parsed range structure to compile.
596 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
597 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
598 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
599 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
600 * range restriction must be more restrictive than the base_range.
601 * @param[in,out] range Pointer to the created current range structure.
602 * @return LY_ERR value.
603 */
604static LY_ERR
605lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
606 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
607{
aPiecek1c4da362021-04-29 14:26:34 +0200608 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200609 const char *expr;
610 struct lysc_range_part *parts = NULL, *part;
611 ly_bool range_expected = 0, uns;
612 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
613
614 assert(range);
615 assert(range_p);
616
617 expr = range_p->arg.str;
618 while (1) {
619 if (isspace(*expr)) {
620 ++expr;
621 } else if (*expr == '\0') {
622 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100623 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200624 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
625 length_restr ? "length" : "range", range_p->arg);
aPiecek1c4da362021-04-29 14:26:34 +0200626 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200627 goto cleanup;
628 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100629 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200630 "Invalid %s restriction - unexpected end of the expression (%s).",
631 length_restr ? "length" : "range", range_p->arg);
aPiecek1c4da362021-04-29 14:26:34 +0200632 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200633 goto cleanup;
634 }
635 parts_done++;
636 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100637 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200638 if (parts) {
639 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100640 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200641 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100642 (int)(expr - range_p->arg.str), range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200643 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200644 goto cleanup;
645 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100646 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200647
648 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200649 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200650 part->max_64 = part->min_64;
651 } else if (*expr == '|') {
652 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100653 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200654 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200655 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200656 goto cleanup;
657 }
658 expr++;
659 parts_done++;
660 /* process next part of the expression */
661 } else if (!strncmp(expr, "..", 2)) {
662 expr += 2;
663 while (isspace(*expr)) {
664 expr++;
665 }
666 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100667 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200668 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
aPiecek1c4da362021-04-29 14:26:34 +0200669 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200670 goto cleanup;
671 }
672 /* continue expecting the upper boundary */
673 range_expected = 1;
674 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
675 /* number */
676 if (range_expected) {
677 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200678 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200679 range_expected = 0;
680 } else {
681 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200682 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200683 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
684 part->max_64 = part->min_64;
685 }
686
687 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100688 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
689 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200690 while (isspace(*expr)) {
691 expr++;
692 }
693 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100694 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200695 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200696 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200697 goto cleanup;
698 }
699 if (range_expected) {
700 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200701 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200702 range_expected = 0;
703 } else {
704 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200705 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200706 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
707 part->min_64 = part->max_64;
708 }
709 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100710 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200711 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200712 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200713 goto cleanup;
714 }
715 }
716
717 /* check with the previous range/length restriction */
718 if (base_range) {
719 switch (basetype) {
720 case LY_TYPE_BINARY:
721 case LY_TYPE_UINT8:
722 case LY_TYPE_UINT16:
723 case LY_TYPE_UINT32:
724 case LY_TYPE_UINT64:
725 case LY_TYPE_STRING:
726 uns = 1;
727 break;
728 case LY_TYPE_DEC64:
729 case LY_TYPE_INT8:
730 case LY_TYPE_INT16:
731 case LY_TYPE_INT32:
732 case LY_TYPE_INT64:
733 uns = 0;
734 break;
735 default:
736 LOGINT(ctx->ctx);
737 ret = LY_EINT;
738 goto cleanup;
739 }
740 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
741 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
742 goto baseerror;
743 }
744 /* current lower bound is not lower than the base */
745 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
746 /* base has single value */
747 if (base_range->parts[v].min_64 == parts[u].min_64) {
748 /* both lower bounds are the same */
749 if (parts[u].min_64 != parts[u].max_64) {
750 /* current continues with a range */
751 goto baseerror;
752 } else {
753 /* equal single values, move both forward */
754 ++v;
755 continue;
756 }
757 } else {
758 /* base is single value lower than current range, so the
759 * value from base range is removed in the current,
760 * move only base and repeat checking */
761 ++v;
762 --u;
763 continue;
764 }
765 } else {
766 /* base is the range */
767 if (parts[u].min_64 == parts[u].max_64) {
768 /* current is a single value */
769 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
770 /* current is behind the base range, so base range is omitted,
771 * move the base and keep the current for further check */
772 ++v;
773 --u;
774 } /* else it is within the base range, so move the current, but keep the base */
775 continue;
776 } else {
777 /* both are ranges - check the higher bound, the lower was already checked */
778 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
779 /* higher bound is higher than the current higher bound */
780 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
781 /* but the current lower bound is also higher, so the base range is omitted,
782 * continue with the same current, but move the base */
783 --u;
784 ++v;
785 continue;
786 }
787 /* current range starts within the base range but end behind it */
788 goto baseerror;
789 } else {
790 /* current range is smaller than the base,
791 * move current, but stay with the base */
792 continue;
793 }
794 }
795 }
796 }
797 if (u != parts_done) {
798baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100799 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200800 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
801 length_restr ? "length" : "range", range_p->arg);
aPiecek1c4da362021-04-29 14:26:34 +0200802 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200803 goto cleanup;
804 }
805 }
806
807 if (!(*range)) {
808 *range = calloc(1, sizeof **range);
809 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
810 }
811
812 /* we rewrite the following values as the types chain is being processed */
813 if (range_p->eapptag) {
814 lydict_remove(ctx->ctx, (*range)->eapptag);
815 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
816 }
817 if (range_p->emsg) {
818 lydict_remove(ctx->ctx, (*range)->emsg);
819 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
820 }
821 if (range_p->dsc) {
822 lydict_remove(ctx->ctx, (*range)->dsc);
823 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
824 }
825 if (range_p->ref) {
826 lydict_remove(ctx->ctx, (*range)->ref);
827 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
828 }
829 /* extensions are taken only from the last range by the caller */
830
831 (*range)->parts = parts;
832 parts = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200833cleanup:
834 LY_ARRAY_FREE(parts);
835
836 return ret;
837}
838
839LY_ERR
Radek Krejci2efc45b2020-12-22 16:25:44 +0100840lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200841{
842 size_t idx, idx2, start, end, size, brack;
843 char *perl_regex, *ptr;
Christian Hoppsdafed222021-03-22 13:02:42 -0400844 int err_code, compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200845 const char *orig_ptr;
846 PCRE2_SIZE err_offset;
847 pcre2_code *code_local;
848
849#define URANGE_LEN 19
850 char *ublock2urange[][2] = {
851 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
852 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
853 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
854 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
855 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
856 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
857 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
858 {"Greek", "[\\x{0370}-\\x{03FF}]"},
859 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
860 {"Armenian", "[\\x{0530}-\\x{058F}]"},
861 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
862 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
863 {"Syriac", "[\\x{0700}-\\x{074F}]"},
864 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
865 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
866 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
867 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
868 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
869 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
870 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
871 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
872 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
873 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
874 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
875 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
876 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
877 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
878 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
879 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
880 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
881 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
882 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
883 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
884 {"Ogham", "[\\x{1680}-\\x{169F}]"},
885 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
886 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
887 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
888 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
889 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
890 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
891 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
892 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
893 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
894 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
895 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
896 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
897 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
898 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
899 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
900 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
901 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
902 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
903 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
904 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
905 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
906 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
907 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
908 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
909 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
910 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
911 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
912 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
913 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
914 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
915 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
916 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
917 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
918 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
919 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
920 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
921 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
922 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
923 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
924 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
925 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
926 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
927 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
928 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
929 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
930 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
931 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
932 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
933 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +0100934 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200935 {NULL, NULL}
936 };
937
938 /* adjust the expression to a Perl equivalent
939 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
940
941 /* allocate space for the transformed pattern */
942 size = strlen(pattern) + 1;
Christian Hoppsdafed222021-03-22 13:02:42 -0400943 compile_opts = PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
944#ifdef PCRE2_ENDANCHORED
945 compile_opts |= PCRE2_ENDANCHORED;
946#else
947 /* add space for trailing $ anchor */
948 size++;
949#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200950 perl_regex = malloc(size);
951 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
952 perl_regex[0] = '\0';
953
954 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
955 brack = 0;
956 idx = 0;
957 orig_ptr = pattern;
958 while (orig_ptr[0]) {
959 switch (orig_ptr[0]) {
960 case '$':
961 case '^':
962 if (!brack) {
963 /* make space for the extra character */
964 ++size;
965 perl_regex = ly_realloc(perl_regex, size);
966 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
967
968 /* print escape slash */
969 perl_regex[idx] = '\\';
970 ++idx;
971 }
972 break;
973 case '[':
974 /* must not be escaped */
975 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
976 ++brack;
977 }
978 break;
979 case ']':
980 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
981 /* pattern was checked and compiled already */
982 assert(brack);
983 --brack;
984 }
985 break;
986 default:
987 break;
988 }
989
990 /* copy char */
991 perl_regex[idx] = orig_ptr[0];
992
993 ++idx;
994 ++orig_ptr;
995 }
Christian Hoppsdafed222021-03-22 13:02:42 -0400996#ifndef PCRE2_ENDANCHORED
997 /* anchor match to end of subject */
998 perl_regex[idx++] = '$';
999#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001000 perl_regex[idx] = '\0';
1001
1002 /* substitute Unicode Character Blocks with exact Character Ranges */
1003 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1004 start = ptr - perl_regex;
1005
1006 ptr = strchr(ptr, '}');
1007 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001008 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001009 free(perl_regex);
1010 return LY_EVALID;
1011 }
1012 end = (ptr - perl_regex) + 1;
1013
1014 /* need more space */
1015 if (end - start < URANGE_LEN) {
1016 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1017 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1018 }
1019
1020 /* find our range */
1021 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001022 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1023 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001024 break;
1025 }
1026 }
1027 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001028 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001029 free(perl_regex);
1030 return LY_EVALID;
1031 }
1032
1033 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1034 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1035 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1036 ++idx;
1037 }
1038 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1039 --idx;
1040 }
1041 }
1042 if (idx) {
1043 /* skip brackets */
1044 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1045 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1046 } else {
1047 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1048 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1049 }
1050 }
1051
1052 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001053 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001054 &err_code, &err_offset, NULL);
1055 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001056 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1057 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001058 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001059 free(perl_regex);
1060 return LY_EVALID;
1061 }
1062 free(perl_regex);
1063
1064 if (code) {
1065 *code = code_local;
1066 } else {
1067 free(code_local);
1068 }
1069
1070 return LY_SUCCESS;
1071
1072#undef URANGE_LEN
1073}
1074
1075/**
1076 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1077 * @param[in] ctx Compile context.
1078 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1079 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1080 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1081 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1082 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1083 */
1084static LY_ERR
1085lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1086 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1087{
1088 struct lysc_pattern **pattern;
1089 LY_ARRAY_COUNT_TYPE u;
1090 LY_ERR ret = LY_SUCCESS;
1091
1092 /* first, copy the patterns from the base type */
1093 if (base_patterns) {
1094 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1095 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1096 }
1097
1098 LY_ARRAY_FOR(patterns_p, u) {
1099 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1100 *pattern = calloc(1, sizeof **pattern);
1101 ++(*pattern)->refcount;
1102
Radek Krejci2efc45b2020-12-22 16:25:44 +01001103 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001104 LY_CHECK_RET(ret);
1105
Radek Krejcif13b87b2020-12-01 22:02:17 +01001106 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001107 (*pattern)->inverted = 1;
1108 }
1109 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1110 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1111 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1112 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1113 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001114 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001115 }
1116done:
1117 return ret;
1118}
1119
1120/**
1121 * @brief map of the possible restrictions combination for the specific built-in type.
1122 */
1123static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1124 0 /* LY_TYPE_UNKNOWN */,
1125 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1126 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1127 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1128 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1129 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1130 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1131 LYS_SET_BIT /* LY_TYPE_BITS */,
1132 0 /* LY_TYPE_BOOL */,
1133 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1134 0 /* LY_TYPE_EMPTY */,
1135 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1136 LYS_SET_BASE /* LY_TYPE_IDENT */,
1137 LYS_SET_REQINST /* LY_TYPE_INST */,
1138 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1139 LYS_SET_TYPE /* LY_TYPE_UNION */,
1140 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1141 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1142 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1143 LYS_SET_RANGE /* LY_TYPE_INT64 */
1144};
1145
1146/**
1147 * @brief stringification of the YANG built-in data types
1148 */
1149const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001150 LY_TYPE_UNKNOWN_STR,
1151 LY_TYPE_BINARY_STR,
1152 LY_TYPE_UINT8_STR,
1153 LY_TYPE_UINT16_STR,
1154 LY_TYPE_UINT32_STR,
1155 LY_TYPE_UINT64_STR,
1156 LY_TYPE_STRING_STR,
1157 LY_TYPE_BITS_STR,
1158 LY_TYPE_BOOL_STR,
1159 LY_TYPE_DEC64_STR,
1160 LY_TYPE_EMPTY_STR,
1161 LY_TYPE_ENUM_STR,
1162 LY_TYPE_IDENT_STR,
1163 LY_TYPE_INST_STR,
1164 LY_TYPE_LEAFREF_STR,
1165 LY_TYPE_UNION_STR,
1166 LY_TYPE_INT8_STR,
1167 LY_TYPE_INT16_STR,
1168 LY_TYPE_INT32_STR,
1169 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001170};
1171
1172/**
1173 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1174 * @param[in] ctx Compile context.
1175 * @param[in] enums_p Array of the parsed enum structures to compile.
1176 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
1177 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001178 * @param[out] bitenums Newly created array of the compiled bitenums information for the current type.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001179 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1180 */
1181static LY_ERR
1182lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001183 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001184{
1185 LY_ERR ret = LY_SUCCESS;
1186 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001187 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1188 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001189 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001190 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001191
1192 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001193 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001194 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1195 return LY_EVALID;
1196 }
1197
1198 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001199 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001200 if (base_enums) {
1201 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1202 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001203 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001204 break;
1205 }
1206 }
1207 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001208 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001209 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001210 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001211 return LY_EVALID;
1212 }
1213 match = v;
1214 }
1215
1216 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001217 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001218 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001219 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001220 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001221 LY_ARRAY_FOR(*bitenums, v) {
1222 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001223 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001224 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001225 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001226 return LY_EVALID;
1227 }
1228 }
1229 } else if (base_enums) {
1230 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001231 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001232 } else {
1233 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001234 if (u == 0) {
1235 cur_val = 0;
1236 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001237 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001238 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001239 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001240 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001241 } else {
1242 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001243 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001244 }
1245
1246 /* save highest value for auto assing */
1247 if (highest_value < cur_val) {
1248 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001249 }
1250 } else { /* LY_TYPE_BITS */
1251 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001252 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001253 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001254 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001255 LY_ARRAY_FOR(*bitenums, v) {
1256 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001257 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001258 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001259 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001260 return LY_EVALID;
1261 }
1262 }
1263 } else if (base_enums) {
1264 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001265 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001266 } else {
1267 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001268 if (u == 0) {
1269 cur_pos = 0;
1270 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001271 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001272 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001273 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001274 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001275 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001276 } else {
1277 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001278 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001279 }
1280
1281 /* save highest position for auto assing */
1282 if (highest_position < cur_pos) {
1283 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001284 }
1285 }
1286
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001287 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001288 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001289 if (basetype == LY_TYPE_ENUM) {
1290 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001291 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001292 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001293 enums_p[u].name, base_enums[match].value, cur_val);
1294 return LY_EVALID;
1295 }
1296 } else {
1297 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001298 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001299 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001300 enums_p[u].name, base_enums[match].position, cur_pos);
1301 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001302 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001303 }
1304 }
1305
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001306 /* evaluate if-ffeatures */
1307 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1308 if (!enabled) {
1309 continue;
1310 }
1311
1312 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001313 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001314 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1315 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1316 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001317 e->flags = (enums_p[u].flags & LYS_FLAGS_COMPILED_MASK) | (basetype == LY_TYPE_ENUM ? LYS_IS_ENUM : 0);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001318 if (basetype == LY_TYPE_ENUM) {
1319 e->value = cur_val;
1320 } else {
1321 e->position = cur_pos;
1322 }
Radek Krejciab430862021-03-02 20:13:40 +01001323 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001324
1325 if (basetype == LY_TYPE_BITS) {
1326 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001327 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001328 if (v != u) {
1329 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001330 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1331 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001332 }
1333 }
1334 }
1335
1336done:
1337 return ret;
1338}
1339
Radek Krejci6a205692020-12-09 13:58:22 +01001340static LY_ERR
1341lys_compile_type_union(struct lysc_ctx *ctx, struct lysp_type *ptypes, struct lysp_node *context_pnode, uint16_t context_flags,
Michal Vaskoa99b3572021-02-01 11:54:58 +01001342 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001343{
1344 LY_ERR ret = LY_SUCCESS;
1345 struct lysc_type **utypes = *utypes_p;
1346 struct lysc_type_union *un_aux = NULL;
1347
1348 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1349 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001350 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1351 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001352 LY_CHECK_GOTO(ret, error);
1353 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1354 /* add space for additional types from the union subtype */
1355 un_aux = (struct lysc_type_union *)utypes[u + additional];
1356 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1357 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1358
1359 /* copy subtypes of the subtype union */
1360 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1361 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1362 struct lysc_type_leafref *lref;
1363
1364 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1365 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1366 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1367 lref = (struct lysc_type_leafref *)utypes[u + additional];
1368
1369 lref->basetype = LY_TYPE_LEAFREF;
1370 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1371 LY_CHECK_GOTO(ret, error);
1372 lref->refcount = 1;
Radek Krejci6a205692020-12-09 13:58:22 +01001373 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001374 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001375 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001376 LY_CHECK_GOTO(ret, error);
1377 /* TODO extensions */
1378
1379 } else {
1380 utypes[u + additional] = un_aux->types[v];
1381 ++un_aux->types[v]->refcount;
1382 }
1383 ++additional;
1384 LY_ARRAY_INCREMENT(utypes);
1385 }
1386 /* compensate u increment in main loop */
1387 --additional;
1388
1389 /* free the replaced union subtype */
1390 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1391 un_aux = NULL;
1392 } else {
1393 LY_ARRAY_INCREMENT(utypes);
1394 }
1395 }
1396
1397 *utypes_p = utypes;
1398 return LY_SUCCESS;
1399
1400error:
1401 if (un_aux) {
1402 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1403 }
1404 *utypes_p = utypes;
1405 return ret;
1406}
1407
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001408/**
1409 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1410 * @param[in] ctx Compile context.
1411 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1412 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001413 * @param[in] context_name Name of the context node or referencing typedef for logging.
1414 * @param[in] type_p Parsed type to compile.
1415 * @param[in] basetype Base YANG built-in type of the type to compile.
1416 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1417 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1418 * @param[out] type Newly created type structure with the filled information about the type.
1419 * @return LY_ERR value.
1420 */
1421static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001422lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1423 struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001424{
1425 LY_ERR ret = LY_SUCCESS;
1426 struct lysc_type_bin *bin;
1427 struct lysc_type_num *num;
1428 struct lysc_type_str *str;
1429 struct lysc_type_bits *bits;
1430 struct lysc_type_enum *enumeration;
1431 struct lysc_type_dec *dec;
1432 struct lysc_type_identityref *idref;
1433 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001434 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001435
1436 switch (basetype) {
1437 case LY_TYPE_BINARY:
1438 bin = (struct lysc_type_bin *)(*type);
1439
1440 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1441 if (type_p->length) {
1442 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1443 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1444 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001445 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001446 }
1447 }
1448 break;
1449 case LY_TYPE_BITS:
1450 /* RFC 7950 9.7 - bits */
1451 bits = (struct lysc_type_bits *)(*type);
1452 if (type_p->bits) {
1453 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1454 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1455 (struct lysc_type_bitenum_item **)&bits->bits));
1456 }
1457
1458 if (!base && !type_p->flags) {
1459 /* type derived from bits built-in type must contain at least one bit */
1460 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001461 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001462 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001463 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001464 }
1465 return LY_EVALID;
1466 }
1467 break;
1468 case LY_TYPE_DEC64:
1469 dec = (struct lysc_type_dec *)(*type);
1470
1471 /* RFC 7950 9.3.4 - fraction-digits */
1472 if (!base) {
1473 if (!type_p->fraction_digits) {
1474 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001475 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001476 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001477 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001478 }
1479 return LY_EVALID;
1480 }
1481 dec->fraction_digits = type_p->fraction_digits;
1482 } else {
1483 if (type_p->fraction_digits) {
1484 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1485 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001486 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001487 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1488 tpdfname);
1489 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001490 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001491 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1492 }
1493 return LY_EVALID;
1494 }
1495 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1496 }
1497
1498 /* RFC 7950 9.2.4 - range */
1499 if (type_p->range) {
1500 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1501 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1502 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001503 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001504 }
1505 }
1506 break;
1507 case LY_TYPE_STRING:
1508 str = (struct lysc_type_str *)(*type);
1509
1510 /* RFC 7950 9.4.4 - length */
1511 if (type_p->length) {
1512 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1513 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1514 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001515 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001516 }
1517 } else if (base && ((struct lysc_type_str *)base)->length) {
1518 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1519 }
1520
1521 /* RFC 7950 9.4.5 - pattern */
1522 if (type_p->patterns) {
1523 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1524 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1525 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1526 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1527 }
1528 break;
1529 case LY_TYPE_ENUM:
1530 enumeration = (struct lysc_type_enum *)(*type);
1531
1532 /* RFC 7950 9.6 - enum */
1533 if (type_p->enums) {
1534 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1535 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1536 }
1537
1538 if (!base && !type_p->flags) {
1539 /* type derived from enumerations built-in type must contain at least one enum */
1540 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001541 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001542 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001543 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001544 }
1545 return LY_EVALID;
1546 }
1547 break;
1548 case LY_TYPE_INT8:
1549 case LY_TYPE_UINT8:
1550 case LY_TYPE_INT16:
1551 case LY_TYPE_UINT16:
1552 case LY_TYPE_INT32:
1553 case LY_TYPE_UINT32:
1554 case LY_TYPE_INT64:
1555 case LY_TYPE_UINT64:
1556 num = (struct lysc_type_num *)(*type);
1557
1558 /* RFC 6020 9.2.4 - range */
1559 if (type_p->range) {
1560 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1561 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1562 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001563 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001564 }
1565 }
1566 break;
1567 case LY_TYPE_IDENT:
1568 idref = (struct lysc_type_identityref *)(*type);
1569
1570 /* RFC 7950 9.10.2 - base */
1571 if (type_p->bases) {
1572 if (base) {
1573 /* only the directly derived identityrefs can contain base specification */
1574 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001575 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001576 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1577 tpdfname);
1578 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001579 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001580 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1581 }
1582 return LY_EVALID;
1583 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001584 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001585 }
1586
1587 if (!base && !type_p->flags) {
1588 /* type derived from identityref built-in type must contain at least one base */
1589 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001590 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001591 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001592 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001593 }
1594 return LY_EVALID;
1595 }
1596 break;
1597 case LY_TYPE_LEAFREF:
1598 lref = (struct lysc_type_leafref *)*type;
1599
1600 /* RFC 7950 9.9.3 - require-instance */
1601 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001602 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001603 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001604 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001605 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1606 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001607 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001608 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1609 }
1610 return LY_EVALID;
1611 }
1612 lref->require_instance = type_p->require_instance;
1613 } else if (base) {
1614 /* inherit */
1615 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1616 } else {
1617 /* default is true */
1618 lref->require_instance = 1;
1619 }
1620 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001621 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001622
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001623 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001624 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, type_p->path->expr, strlen(type_p->path->expr),
Radek Krejci8df109d2021-04-23 12:19:08 +02001625 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001626 } else if (base) {
1627 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Radek Krejci8df109d2021-04-23 12:19:08 +02001628 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001629 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001630 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001631 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001632 return LY_EVALID;
1633 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001634 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001635 return LY_EVALID;
1636 }
1637 break;
1638 case LY_TYPE_INST:
1639 /* RFC 7950 9.9.3 - require-instance */
1640 if (type_p->flags & LYS_SET_REQINST) {
1641 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1642 } else {
1643 /* default is true */
1644 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1645 }
1646 break;
1647 case LY_TYPE_UNION:
1648 un = (struct lysc_type_union *)(*type);
1649
1650 /* RFC 7950 7.4 - type */
1651 if (type_p->types) {
1652 if (base) {
1653 /* only the directly derived union can contain types specification */
1654 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001655 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001656 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1657 tpdfname);
1658 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001659 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001660 "Invalid type substatement for the type not directly derived from union built-in type.");
1661 }
1662 return LY_EVALID;
1663 }
1664 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001665 LY_CHECK_RET(lys_compile_type_union(ctx, type_p->types, context_pnode, context_flags, context_name, &un->types));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001666 }
1667
1668 if (!base && !type_p->flags) {
1669 /* type derived from union built-in type must contain at least one type */
1670 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001671 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001672 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001673 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001674 }
1675 return LY_EVALID;
1676 }
1677 break;
1678 case LY_TYPE_BOOL:
1679 case LY_TYPE_EMPTY:
1680 case LY_TYPE_UNKNOWN: /* just to complete switch */
1681 break;
1682 }
1683
1684 if (tpdfname) {
1685 switch (basetype) {
1686 case LY_TYPE_BINARY:
1687 type_p->compiled = *type;
1688 *type = calloc(1, sizeof(struct lysc_type_bin));
1689 break;
1690 case LY_TYPE_BITS:
1691 type_p->compiled = *type;
1692 *type = calloc(1, sizeof(struct lysc_type_bits));
1693 break;
1694 case LY_TYPE_DEC64:
1695 type_p->compiled = *type;
1696 *type = calloc(1, sizeof(struct lysc_type_dec));
1697 break;
1698 case LY_TYPE_STRING:
1699 type_p->compiled = *type;
1700 *type = calloc(1, sizeof(struct lysc_type_str));
1701 break;
1702 case LY_TYPE_ENUM:
1703 type_p->compiled = *type;
1704 *type = calloc(1, sizeof(struct lysc_type_enum));
1705 break;
1706 case LY_TYPE_INT8:
1707 case LY_TYPE_UINT8:
1708 case LY_TYPE_INT16:
1709 case LY_TYPE_UINT16:
1710 case LY_TYPE_INT32:
1711 case LY_TYPE_UINT32:
1712 case LY_TYPE_INT64:
1713 case LY_TYPE_UINT64:
1714 type_p->compiled = *type;
1715 *type = calloc(1, sizeof(struct lysc_type_num));
1716 break;
1717 case LY_TYPE_IDENT:
1718 type_p->compiled = *type;
1719 *type = calloc(1, sizeof(struct lysc_type_identityref));
1720 break;
1721 case LY_TYPE_LEAFREF:
1722 type_p->compiled = *type;
1723 *type = calloc(1, sizeof(struct lysc_type_leafref));
1724 break;
1725 case LY_TYPE_INST:
1726 type_p->compiled = *type;
1727 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1728 break;
1729 case LY_TYPE_UNION:
1730 type_p->compiled = *type;
1731 *type = calloc(1, sizeof(struct lysc_type_union));
1732 break;
1733 case LY_TYPE_BOOL:
1734 case LY_TYPE_EMPTY:
1735 case LY_TYPE_UNKNOWN: /* just to complete switch */
1736 break;
1737 }
1738 }
1739 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1740
1741cleanup:
1742 return ret;
1743}
1744
Radek Krejcibf940f92021-03-24 21:04:13 +01001745/**
1746 * @brief Find the correct plugin implementing the described type
1747 *
1748 * @param[in] mod Module where the type is defined
1749 * @param[in] name Name of the type (typedef)
1750 * @param[in] basetype Type's basetype (when the built-in base plugin is supposed to be used)
1751 * @return Pointer to the plugin implementing the described data type.
1752 */
1753static struct lyplg_type *
1754lys_compile_type_get_plugin(struct lys_module *mod, const char *name, LY_DATA_TYPE basetype)
1755{
1756 struct lyplg_type *p;
1757
1758 /* try to find loaded user type plugins */
1759 p = lyplg_find(LYPLG_TYPE, mod->name, mod->revision, name);
1760 if (!p) {
1761 /* use the internal built-in type implementation */
1762 p = lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
1763 }
1764
1765 return p;
1766}
1767
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001768LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001769lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1770 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001771{
1772 LY_ERR ret = LY_SUCCESS;
1773 ly_bool dummyloops = 0;
1774 struct type_context {
1775 const struct lysp_tpdf *tpdf;
1776 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001777 } *tctx, *tctx_prev = NULL, *tctx_iter;
1778 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1779 struct lysc_type *base = NULL, *prev_type;
1780 struct ly_set tpdf_chain = {0};
1781
1782 (*type) = NULL;
1783 if (dflt) {
1784 *dflt = NULL;
1785 }
1786
1787 tctx = calloc(1, sizeof *tctx);
1788 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001789 for (ret = lysp_type_find(type_p->name, context_pnode, type_p->pmod, &basetype, &tctx->tpdf, &tctx->node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001790 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001791 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1792 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001793 if (basetype) {
1794 break;
1795 }
1796
1797 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001798 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1799 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001800 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1801
1802 if (units && !*units) {
1803 /* inherit units */
1804 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1805 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1806 }
1807 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1808 /* inherit default */
1809 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1810 }
1811 if (dummyloops && (!units || *units) && dflt && *dflt) {
1812 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1813 break;
1814 }
1815
Radek Krejci720d2612021-03-03 19:44:22 +01001816 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
1817 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1818 * and we need now recompile the type again in the updated context. */
1819 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1820 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1821 }
1822
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001823 if (tctx->tpdf->type.compiled) {
1824 /* it is not necessary to continue, the rest of the chain was already compiled,
1825 * but we still may need to inherit default and units values, so start dummy loops */
1826 basetype = tctx->tpdf->type.compiled->basetype;
1827 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1828 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1829
1830 if ((units && !*units) || (dflt && !*dflt)) {
1831 dummyloops = 1;
1832 goto preparenext;
1833 } else {
1834 tctx = NULL;
1835 break;
1836 }
1837 }
1838
1839 /* circular typedef reference detection */
1840 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1841 /* local part */
1842 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001843 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001844 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001845 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1846 free(tctx);
1847 ret = LY_EVALID;
1848 goto cleanup;
1849 }
1850 }
1851 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1852 /* global part for unions corner case */
1853 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001854 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001855 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001856 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1857 free(tctx);
1858 ret = LY_EVALID;
1859 goto cleanup;
1860 }
1861 }
1862
1863 /* store information for the following processing */
1864 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1865 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1866
1867preparenext:
1868 /* prepare next loop */
1869 tctx_prev = tctx;
1870 tctx = calloc(1, sizeof *tctx);
1871 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1872 }
1873 free(tctx);
1874
1875 /* allocate type according to the basetype */
1876 switch (basetype) {
1877 case LY_TYPE_BINARY:
1878 *type = calloc(1, sizeof(struct lysc_type_bin));
1879 break;
1880 case LY_TYPE_BITS:
1881 *type = calloc(1, sizeof(struct lysc_type_bits));
1882 break;
1883 case LY_TYPE_BOOL:
1884 case LY_TYPE_EMPTY:
1885 *type = calloc(1, sizeof(struct lysc_type));
1886 break;
1887 case LY_TYPE_DEC64:
1888 *type = calloc(1, sizeof(struct lysc_type_dec));
1889 break;
1890 case LY_TYPE_ENUM:
1891 *type = calloc(1, sizeof(struct lysc_type_enum));
1892 break;
1893 case LY_TYPE_IDENT:
1894 *type = calloc(1, sizeof(struct lysc_type_identityref));
1895 break;
1896 case LY_TYPE_INST:
1897 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1898 break;
1899 case LY_TYPE_LEAFREF:
1900 *type = calloc(1, sizeof(struct lysc_type_leafref));
1901 break;
1902 case LY_TYPE_STRING:
1903 *type = calloc(1, sizeof(struct lysc_type_str));
1904 break;
1905 case LY_TYPE_UNION:
1906 *type = calloc(1, sizeof(struct lysc_type_union));
1907 break;
1908 case LY_TYPE_INT8:
1909 case LY_TYPE_UINT8:
1910 case LY_TYPE_INT16:
1911 case LY_TYPE_UINT16:
1912 case LY_TYPE_INT32:
1913 case LY_TYPE_UINT32:
1914 case LY_TYPE_INT64:
1915 case LY_TYPE_UINT64:
1916 *type = calloc(1, sizeof(struct lysc_type_num));
1917 break;
1918 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001919 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001920 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1921 ret = LY_EVALID;
1922 goto cleanup;
1923 }
1924 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1925 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001926 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001927 ly_data_type2str[basetype]);
1928 free(*type);
1929 (*type) = NULL;
1930 ret = LY_EVALID;
1931 goto cleanup;
1932 }
1933
1934 /* get restrictions from the referred typedefs */
1935 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1936 tctx = (struct type_context *)tpdf_chain.objs[u];
1937
1938 /* remember the typedef context for circular check */
1939 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1940 LY_CHECK_GOTO(ret, cleanup);
1941
1942 if (tctx->tpdf->type.compiled) {
1943 base = tctx->tpdf->type.compiled;
1944 continue;
1945 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
1946 /* no change, just use the type information from the base */
1947 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
1948 ++base->refcount;
1949 continue;
1950 }
1951
1952 ++(*type)->refcount;
1953 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001954 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001955 tctx->tpdf->name, ly_data_type2str[basetype]);
1956 ret = LY_EVALID;
1957 goto cleanup;
1958 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001959 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001960 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
1961 tctx->tpdf->name, tctx->tpdf->dflt.str);
1962 ret = LY_EVALID;
1963 goto cleanup;
1964 }
1965
1966 (*type)->basetype = basetype;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02001967 /* collect extensions */
1968 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
1969 /* get plugin for the type */
Radek Krejcibf940f92021-03-24 21:04:13 +01001970 (*type)->plugin = lys_compile_type_get_plugin(tctx->tpdf->type.pmod->mod, tctx->tpdf->name, basetype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001971 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001972 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001973 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
1974 LY_CHECK_GOTO(ret, cleanup);
1975 base = prev_type;
1976 }
1977 /* remove the processed typedef contexts from the stack for circular check */
1978 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
1979
1980 /* process the type definition in leaf */
1981 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
1982 /* get restrictions from the node itself */
1983 (*type)->basetype = basetype;
Radek Krejcibf940f92021-03-24 21:04:13 +01001984 (*type)->plugin = base ? base->plugin : lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001985 ++(*type)->refcount;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001986 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001987 LY_CHECK_GOTO(ret, cleanup);
1988 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
1989 /* no specific restriction in leaf's type definition, copy from the base */
1990 free(*type);
1991 (*type) = base;
1992 ++(*type)->refcount;
1993 }
1994
Radek Krejciab430862021-03-02 20:13:40 +01001995 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001996
1997cleanup:
1998 ly_set_erase(&tpdf_chain, free);
1999 return ret;
2000}
2001
2002/**
Radek Krejcif13b87b2020-12-01 22:02:17 +01002003 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
2004 */
2005#define LYS_STATUS_USES LYS_CONFIG_MASK
2006
2007/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002008 * @brief Compile status information of the given node.
2009 *
2010 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
2011 * has the status correctly set during the compilation.
2012 *
2013 * @param[in] ctx Compile context
2014 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
2015 * If the status was set explicitly on the node, it is already set in the flags value and we just check
2016 * the compatibility with the parent's status value.
2017 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
2018 * @return LY_ERR value.
2019 */
2020static LY_ERR
2021lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
2022{
2023 /* status - it is not inherited by specification, but it does not make sense to have
2024 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2025 if (!((*node_flags) & LYS_STATUS_MASK)) {
2026 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01002027 if ((parent_flags & LYS_STATUS_USES) != LYS_STATUS_USES) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002028 /* do not print the warning when inheriting status from uses - the uses_status value has a special
Radek Krejcif13b87b2020-12-01 22:02:17 +01002029 * combination of bits (LYS_STATUS_USES) which marks the uses_status value */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002030 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
2031 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2032 }
2033 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
2034 } else {
2035 (*node_flags) |= LYS_STATUS_CURR;
2036 }
2037 } else if (parent_flags & LYS_STATUS_MASK) {
2038 /* check status compatibility with the parent */
2039 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
2040 if ((*node_flags) & LYS_STATUS_CURR) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002041 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002042 "A \"current\" status is in conflict with the parent's \"%s\" status.",
2043 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
2044 } else { /* LYS_STATUS_DEPRC */
Radek Krejci2efc45b2020-12-22 16:25:44 +01002045 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002046 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
2047 }
2048 return LY_EVALID;
2049 }
2050 }
2051 return LY_SUCCESS;
2052}
2053
2054/**
2055 * @brief Check uniqness of the node/action/notification name.
2056 *
2057 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2058 * structures, but they share the namespace so we need to check their name collisions.
2059 *
2060 * @param[in] ctx Compile context.
2061 * @param[in] parent Parent of the nodes to check, can be NULL.
2062 * @param[in] name Name of the item to find in the given lists.
2063 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2064 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2065 */
2066static LY_ERR
2067lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2068 const struct lysc_node *exclude)
2069{
2070 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002071 const struct lysc_node_action *actions;
2072 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002073 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002074 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002075
2076#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2077
2078 if (exclude->nodetype == LYS_CASE) {
2079 /* check restricted only to all the cases */
2080 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002081 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002082 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002083 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002084 return LY_EEXIST;
2085 }
2086 }
2087
2088 return LY_SUCCESS;
2089 }
2090
2091 /* no reason for our parent to be choice anymore */
2092 assert(!parent || (parent->nodetype != LYS_CHOICE));
2093
2094 if (parent && (parent->nodetype == LYS_CASE)) {
2095 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002096
2097 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2098 iter = lysc_data_parent(parent);
2099 do {
2100 parent = parent->parent;
2101 if (parent && (parent->nodetype == LYS_CHOICE)) {
2102 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2103 }
2104 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002105 }
2106
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002107 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002108 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2109 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2110 if (exclude->flags & LYS_IS_OUTPUT) {
2111 getnext_flags |= LYS_GETNEXT_OUTPUT;
2112 parent = lysc_node_child(parent)->next;
2113 } else {
2114 parent = lysc_node_child(parent);
2115 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002116 }
2117
2118 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002119 if (!parent && ctx->ext) {
2120 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2121 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2122 goto error;
2123 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002124
Radek Krejci5fa32a32021-02-08 18:12:38 +01002125 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2126 if (iter->nodetype == LYS_CHOICE) {
2127 iter2 = NULL;
2128 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2129 if (CHECK_NODE(iter2, exclude, name)) {
2130 goto error;
2131 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002132 }
2133 }
2134 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002135 } else {
2136 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2137 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2138 goto error;
2139 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002140
Radek Krejci5fa32a32021-02-08 18:12:38 +01002141 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2142 if (iter->nodetype == LYS_CHOICE) {
2143 iter2 = NULL;
2144 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2145 if (CHECK_NODE(iter2, exclude, name)) {
2146 goto error;
2147 }
2148 }
2149 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002150 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002151
Radek Krejci5fa32a32021-02-08 18:12:38 +01002152 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2153 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2154 if (CHECK_NODE(iter, exclude, name)) {
2155 goto error;
2156 }
2157 }
2158
2159 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2160 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2161 if (CHECK_NODE(iter, exclude, name)) {
2162 goto error;
2163 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002164 }
2165 }
Radek Krejci078e4342021-02-12 18:17:26 +01002166 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002167 return LY_SUCCESS;
2168
2169error:
Radek Krejci078e4342021-02-12 18:17:26 +01002170 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002171 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002172 return LY_EEXIST;
2173
2174#undef CHECK_NODE
2175}
2176
Radek Krejci2a9fc652021-01-22 17:44:34 +01002177/**
2178 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2179 * keep specific order of augments targetting the same node.
2180 *
2181 * @param[in] ctx Compile context
2182 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2183 * the choice itself is expected instead of a specific case node.
2184 * @param[in] node Schema node to connect into the list.
2185 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2186 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2187 */
2188static LY_ERR
2189lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002190{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002191 struct lysc_node **children, *anchor = NULL;
2192 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002193
Radek Krejci2a9fc652021-01-22 17:44:34 +01002194 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002195
Radek Krejci2a9fc652021-01-22 17:44:34 +01002196 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002197 if (node->nodetype == LYS_INPUT) {
2198 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2199 /* input node is part of the action but link it with output */
2200 node->next = &((struct lysc_node_action *)parent)->output.node;
2201 node->prev = node->next;
2202 return LY_SUCCESS;
2203 } else if (node->nodetype == LYS_OUTPUT) {
2204 /* output node is part of the action but link it with input */
2205 node->next = NULL;
2206 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002207 return LY_SUCCESS;
2208 } else if (node->nodetype == LYS_ACTION) {
2209 children = (struct lysc_node **)lysc_node_actions_p(parent);
2210 } else if (node->nodetype == LYS_NOTIF) {
2211 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2212 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002213 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002214 }
2215 assert(children);
2216
2217 if (!(*children)) {
2218 /* first child */
2219 *children = node;
2220 } else if (node->flags & LYS_KEY) {
2221 /* special handling of adding keys */
2222 assert(node->module == parent->module);
2223 anchor = *children;
2224 if (anchor->flags & LYS_KEY) {
2225 while ((anchor->flags & LYS_KEY) && anchor->next) {
2226 anchor = anchor->next;
2227 }
2228 /* insert after the last key */
2229 insert_after = 1;
2230 } /* else insert before anchor (at the beginning) */
2231 } else if ((*children)->prev->module == node->module) {
2232 /* last child is from the same module, keep the order and insert at the end */
2233 anchor = (*children)->prev;
2234 insert_after = 1;
2235 } else if (parent->module == node->module) {
2236 /* adding module child after some augments were connected */
2237 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2238 } else {
2239 /* some augments are already connected and we are connecting new ones,
2240 * keep module name order and insert the node into the children list */
2241 anchor = *children;
2242 do {
2243 anchor = anchor->prev;
2244
2245 /* check that we have not found the last augment node from our module or
2246 * the first augment node from a "smaller" module or
2247 * the first node from a local module */
2248 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2249 (anchor->module == parent->module)) {
2250 /* insert after */
2251 insert_after = 1;
2252 break;
2253 }
2254
2255 /* we have traversed all the nodes, insert before anchor (as the first node) */
2256 } while (anchor->prev->next);
2257 }
2258
2259 /* insert */
2260 if (anchor) {
2261 if (insert_after) {
2262 node->next = anchor->next;
2263 node->prev = anchor;
2264 anchor->next = node;
2265 if (node->next) {
2266 /* middle node */
2267 node->next->prev = node;
2268 } else {
2269 /* last node */
2270 (*children)->prev = node;
2271 }
2272 } else {
2273 node->next = anchor;
2274 node->prev = anchor->prev;
2275 anchor->prev = node;
2276 if (anchor == *children) {
2277 /* first node */
2278 *children = node;
2279 } else {
2280 /* middle node */
2281 node->prev->next = node;
2282 }
2283 }
2284 }
2285
2286 /* check the name uniqueness (even for an only child, it may be in case) */
2287 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2288 return LY_EEXIST;
2289 }
2290 } else {
2291 /* top-level element */
2292 struct lysc_node **list;
2293
Radek Krejci6b88a462021-02-17 12:39:34 +01002294 if (ctx->ext) {
2295 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL);
2296 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002297 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2298 } else if (node->nodetype == LYS_NOTIF) {
2299 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2300 } else {
2301 list = &ctx->cur_mod->compiled->data;
2302 }
2303 if (!(*list)) {
2304 *list = node;
2305 } else {
2306 /* insert at the end of the module's top-level nodes list */
2307 (*list)->prev->next = node;
2308 node->prev = (*list)->prev;
2309 (*list)->prev = node;
2310 }
2311
2312 /* check the name uniqueness on top-level */
2313 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2314 return LY_EEXIST;
2315 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002316 }
2317
Radek Krejci2a9fc652021-01-22 17:44:34 +01002318 return LY_SUCCESS;
2319}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002320
Radek Krejci2a9fc652021-01-22 17:44:34 +01002321/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002322 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002323 *
2324 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002325 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002326 * @return LY_ERR value.
2327 */
2328static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002329lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002330{
2331 /* case never has any explicit config */
2332 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002333
Michal Vasko7c565922021-06-10 14:58:27 +02002334 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Radek Krejci91531e12021-02-08 19:57:54 +01002335 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002336 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002337 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2338 /* config not explicitly set, inherit it from parent */
Radek Krejci91531e12021-02-08 19:57:54 +01002339 if (node->parent) {
2340 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002341 } else {
2342 /* default is config true */
2343 node->flags |= LYS_CONFIG_W;
2344 }
2345 } else {
2346 /* config set explicitly */
2347 node->flags |= LYS_SET_CONFIG;
2348 }
2349
Radek Krejci91531e12021-02-08 19:57:54 +01002350 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002351 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002352 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002353 }
2354
Radek Krejci2a9fc652021-01-22 17:44:34 +01002355 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002356}
2357
Radek Krejci91531e12021-02-08 19:57:54 +01002358/**
2359 * @brief Set various flags of the compiled nodes
2360 *
2361 * @param[in] ctx Compile context.
2362 * @param[in] node Compiled node where the flags will be set.
2363 * @param[in] uses_status If the node is being placed instead of uses, here we have the uses's status value (as node's flags).
2364 * Zero means no uses, non-zero value with no status bit set mean the default status.
2365 */
2366static LY_ERR
2367lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2368{
2369 /* inherit config flags */
2370 LY_CHECK_RET(lys_compile_config(ctx, node));
2371
2372 /* status - it is not inherited by specification, but it does not make sense to have
2373 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
2374 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (node->parent ? node->parent->flags : 0)));
2375
2376 /* other flags */
Michal Vasko7c565922021-06-10 14:58:27 +02002377 if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002378 node->flags |= LYS_IS_INPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002379 } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002380 node->flags |= LYS_IS_OUTPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002381 } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002382 node->flags |= LYS_IS_NOTIF;
2383 }
2384
2385 return LY_SUCCESS;
2386}
2387
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002388LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002389lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2390 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2391 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002392{
2393 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002394 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002395 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002396 struct lysp_when *pwhen = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002397
Radek Krejci2a9fc652021-01-22 17:44:34 +01002398 node->nodetype = pnode->nodetype;
2399 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002400 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002401 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002402 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002403
Radek Krejci2a9fc652021-01-22 17:44:34 +01002404 /* compile any deviations for this node */
2405 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002406 if (not_supported) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002407 goto error;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002408 } else if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002409 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002410 }
2411
Radek Krejci2a9fc652021-01-22 17:44:34 +01002412 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002413
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002414 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002415 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002416 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002417 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002418 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002419 }
2420
Radek Krejci91531e12021-02-08 19:57:54 +01002421 /* config, status and other flags */
2422 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002423 LY_CHECK_GOTO(ret, error);
2424
2425 /* list ordering */
2426 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002427 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002428 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002429 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002430 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002431 node->flags &= ~LYS_ORDBY_MASK;
2432 node->flags |= LYS_ORDBY_SYSTEM;
2433 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2434 /* default ordering is system */
2435 node->flags |= LYS_ORDBY_SYSTEM;
2436 }
2437 }
2438
Radek Krejci2a9fc652021-01-22 17:44:34 +01002439 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2440 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2441 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
2442
2443 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2444 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2445
Radek Krejci9a3823e2021-01-27 20:26:46 +01002446 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002447 /* compile when */
Radek Krejci9a3823e2021-01-27 20:26:46 +01002448 ret = lys_compile_when(ctx, pwhen, pnode->flags, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002449 LY_CHECK_GOTO(ret, cleanup);
2450 }
2451
2452 /* connect any augments */
2453 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2454
2455 /* nodetype-specific part */
2456 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2457
2458 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002459 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002460 if (node->flags & LYS_MAND_TRUE) {
2461 /* inherit LYS_MAND_TRUE in parent containers */
2462 lys_compile_mandatory_parents(parent, 1);
2463 }
2464
2465 if (child_set) {
2466 /* add the new node into set */
2467 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2468 }
2469
2470 goto cleanup;
2471
2472error:
2473 lysc_node_free(ctx->ctx, node, 0);
2474cleanup:
2475 if (ret && dev_pnode) {
2476 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2477 }
2478 lysp_dev_node_free(ctx->ctx, dev_pnode);
2479 return ret;
2480}
2481
2482/**
2483 * @brief Compile parsed action's input/output node information.
2484 * @param[in] ctx Compile context
2485 * @param[in] pnode Parsed inout node.
2486 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2487 * is enriched with the inout-specific information.
2488 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2489 */
2490LY_ERR
2491lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2492{
2493 LY_ERR ret = LY_SUCCESS;
2494 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002495 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002496
2497 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2498 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2499
2500 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002501 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002502 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002503
Radek Krejci01180ac2021-01-27 08:48:22 +01002504 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002505 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2506 }
2507
Michal Vasko7c565922021-06-10 14:58:27 +02002508 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002509
2510done:
2511 return ret;
2512}
2513
2514/**
2515 * @brief Compile parsed action node information.
2516 * @param[in] ctx Compile context
2517 * @param[in] pnode Parsed action node.
2518 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2519 * is enriched with the action-specific information.
2520 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2521 */
2522LY_ERR
2523lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2524{
2525 LY_ERR ret;
2526 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2527 struct lysc_node_action *action = (struct lysc_node_action *)node;
2528 struct lysp_node_action_inout *input, implicit_input = {
2529 .nodetype = LYS_INPUT,
2530 .name = "input",
2531 .parent = pnode,
2532 };
2533 struct lysp_node_action_inout *output, implicit_output = {
2534 .nodetype = LYS_OUTPUT,
2535 .name = "output",
2536 .parent = pnode,
2537 };
2538
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002539 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002540 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002541 if (action_p->input.nodetype == LYS_UNKNOWN) {
2542 input = &implicit_input;
2543 } else {
2544 input = &action_p->input;
2545 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002546 ret = lys_compile_node_(ctx, &input->node, &action->node, 0, lys_compile_node_action_inout, &action->input.node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002547 lysc_update_path(ctx, NULL, NULL);
2548 LY_CHECK_GOTO(ret, done);
2549
2550 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002551 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002552 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002553 output = &implicit_output;
2554 } else {
2555 output = &action_p->output;
2556 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002557 ret = lys_compile_node_(ctx, &output->node, &action->node, 0, lys_compile_node_action_inout, &action->output.node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002558 lysc_update_path(ctx, NULL, NULL);
2559 LY_CHECK_GOTO(ret, done);
2560
2561 /* do not check "must" semantics in a grouping */
Michal Vasko7c565922021-06-10 14:58:27 +02002562 if ((action->input.musts || action->output.musts) && !(ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002563 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2564 LY_CHECK_GOTO(ret, done);
2565 }
2566
2567done:
2568 return ret;
2569}
2570
2571/**
2572 * @brief Compile parsed action node information.
2573 * @param[in] ctx Compile context
2574 * @param[in] pnode Parsed action node.
2575 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2576 * is enriched with the action-specific information.
2577 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2578 */
2579LY_ERR
2580lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2581{
2582 LY_ERR ret = LY_SUCCESS;
2583 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2584 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2585 struct lysp_node *child_p;
2586
2587 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002588 if (notif_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002589 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002590 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002591 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002592 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002593
Radek Krejci01180ac2021-01-27 08:48:22 +01002594 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002595 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002596 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002597 }
2598
Radek Krejci2a9fc652021-01-22 17:44:34 +01002599done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002600 return ret;
2601}
2602
2603/**
2604 * @brief Compile parsed container node information.
2605 * @param[in] ctx Compile context
2606 * @param[in] pnode Parsed container node.
2607 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2608 * is enriched with the container-specific information.
2609 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2610 */
2611static LY_ERR
2612lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2613{
2614 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2615 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2616 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002617 LY_ERR ret = LY_SUCCESS;
2618
2619 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002620 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002621 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002622 }
2623
2624 /* more cases when the container has meaning but is kept NP for convenience:
2625 * - when condition
2626 * - direct child action/notification
2627 */
2628
2629 LY_LIST_FOR(cont_p->child, child_p) {
2630 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2631 LY_CHECK_GOTO(ret, done);
2632 }
2633
Michal Vasko5347e3a2020-11-03 17:14:57 +01002634 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002635 if (cont_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002636 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002637 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002638 LY_CHECK_GOTO(ret, done);
2639 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002640
2641 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2642 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2643 LY_CHECK_GOTO(ret, done);
2644 }
2645 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2646 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2647 LY_CHECK_GOTO(ret, done);
2648 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002649
2650done:
2651 return ret;
2652}
2653
Michal Vasko72244882021-01-12 15:21:05 +01002654/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002655 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2656 * @param[in] ctx Compile context.
2657 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2658 * @param[in] type_p Parsed type to compile.
2659 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2660 * @return LY_ERR value.
2661 */
2662static LY_ERR
2663lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2664 struct lysc_node_leaf *leaf)
2665{
2666 struct lysp_qname *dflt;
2667
Michal Vaskoa99b3572021-02-01 11:54:58 +01002668 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002669 leaf->units ? NULL : &leaf->units, &dflt));
2670
2671 /* store default value, if any */
2672 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2673 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2674 }
2675
Michal Vasko7c565922021-06-10 14:58:27 +02002676 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002677 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002678 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko7c565922021-06-10 14:58:27 +02002679 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002680 LY_ARRAY_COUNT_TYPE u;
2681 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2682 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2683 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002684 LY_CHECK_RET(ly_set_add(&ctx->unres->leafrefs, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002685 }
2686 }
2687 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2688 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002689 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002690 return LY_EVALID;
2691 }
2692 }
2693
2694 return LY_SUCCESS;
2695}
2696
2697/**
2698 * @brief Compile parsed leaf node information.
2699 * @param[in] ctx Compile context
2700 * @param[in] pnode Parsed leaf node.
2701 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2702 * is enriched with the leaf-specific information.
2703 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2704 */
2705static LY_ERR
2706lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2707{
2708 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2709 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002710 LY_ERR ret = LY_SUCCESS;
2711
Michal Vasko5347e3a2020-11-03 17:14:57 +01002712 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002713 if (leaf_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002714 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002715 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002716 LY_CHECK_GOTO(ret, done);
2717 }
2718 if (leaf_p->units) {
2719 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2720 leaf->flags |= LYS_SET_UNITS;
2721 }
2722
2723 /* compile type */
2724 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2725 LY_CHECK_GOTO(ret, done);
2726
2727 /* store/update default value */
2728 if (leaf_p->dflt.str) {
2729 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2730 leaf->flags |= LYS_SET_DFLT;
2731 }
2732
2733 /* checks */
2734 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002735 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002736 "Invalid mandatory leaf with a default value.");
2737 return LY_EVALID;
2738 }
2739
2740done:
2741 return ret;
2742}
2743
2744/**
2745 * @brief Compile parsed leaf-list node information.
2746 * @param[in] ctx Compile context
2747 * @param[in] pnode Parsed leaf-list node.
2748 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2749 * is enriched with the leaf-list-specific information.
2750 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2751 */
2752static LY_ERR
2753lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2754{
2755 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2756 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002757 LY_ERR ret = LY_SUCCESS;
2758
Michal Vasko5347e3a2020-11-03 17:14:57 +01002759 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002760 if (llist_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002761 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002762 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002763 LY_CHECK_GOTO(ret, done);
2764 }
2765 if (llist_p->units) {
2766 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2767 llist->flags |= LYS_SET_UNITS;
2768 }
2769
2770 /* compile type */
2771 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2772 LY_CHECK_GOTO(ret, done);
2773
2774 /* store/update default values */
2775 if (llist_p->dflts) {
2776 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002777 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002778 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2779 return LY_EVALID;
2780 }
2781
2782 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2783 llist->flags |= LYS_SET_DFLT;
2784 }
2785
2786 llist->min = llist_p->min;
2787 if (llist->min) {
2788 llist->flags |= LYS_MAND_TRUE;
2789 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002790 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
2791
2792 if (llist->flags & LYS_CONFIG_R) {
2793 /* state leaf-list is always ordered-by user */
2794 llist->flags &= ~LYS_ORDBY_SYSTEM;
2795 llist->flags |= LYS_ORDBY_USER;
2796 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002797
2798 /* checks */
2799 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002800 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "The default statement is present on leaf-list with a nonzero min-elements.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002801 return LY_EVALID;
2802 }
2803
2804 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002805 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002806 llist->min, llist->max);
2807 return LY_EVALID;
2808 }
2809
2810done:
2811 return ret;
2812}
2813
2814LY_ERR
2815lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
Radek Krejci8df109d2021-04-23 12:19:08 +02002816 const struct lys_module *cur_mod, LY_VALUE_FORMAT format, void *prefix_data, uint16_t nodetype,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002817 const struct lysc_node **target, uint16_t *result_flag)
2818{
2819 LY_ERR ret = LY_EVALID;
2820 const char *name, *prefix, *id;
2821 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002822 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002823 const char *nodeid_type;
2824 uint32_t getnext_extra_flag = 0;
2825 uint16_t current_nodetype = 0;
2826
2827 assert(nodeid);
2828 assert(target);
2829 assert(result_flag);
2830 *target = NULL;
2831 *result_flag = 0;
2832
2833 id = nodeid;
2834
2835 if (ctx_node) {
2836 /* descendant-schema-nodeid */
2837 nodeid_type = "descendant";
2838
2839 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002840 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002841 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002842 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002843 return LY_EVALID;
2844 }
2845 } else {
2846 /* absolute-schema-nodeid */
2847 nodeid_type = "absolute";
2848
2849 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002850 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002851 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002852 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002853 return LY_EVALID;
2854 }
2855 ++id;
2856 }
2857
2858 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2859 if (prefix) {
2860 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2861 if (!mod) {
2862 /* module must always be found */
2863 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002864 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002865 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002866 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002867 return LY_ENOTFOUND;
2868 }
2869 } else {
2870 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02002871 case LY_VALUE_SCHEMA:
2872 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002873 /* use the current module */
2874 mod = cur_mod;
2875 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02002876 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02002877 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002878 if (!ctx_node) {
2879 LOGINT_RET(ctx->ctx);
2880 }
2881 /* inherit the module of the previous context node */
2882 mod = ctx_node->module;
2883 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02002884 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02002885 case LY_VALUE_XML:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002886 /* not really defined */
2887 LOGINT_RET(ctx->ctx);
2888 }
2889 }
2890
2891 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2892 /* move through input/output manually */
2893 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002894 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2895 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002896 return LY_ENOTFOUND;
2897 }
2898 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002899 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002900 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002901 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002902 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2903 } else {
2904 /* only input or output is valid */
2905 ctx_node = NULL;
2906 }
2907 } else {
2908 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002909 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002910 getnext_extra_flag = 0;
2911 }
2912 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002913 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2914 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002915 return LY_ENOTFOUND;
2916 }
2917 current_nodetype = ctx_node->nodetype;
2918
2919 if (current_nodetype == LYS_NOTIF) {
2920 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2921 } else if (current_nodetype == LYS_INPUT) {
2922 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2923 } else if (current_nodetype == LYS_OUTPUT) {
2924 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2925 }
2926
2927 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2928 break;
2929 }
2930 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002931 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002932 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002933 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002934 return LY_EVALID;
2935 }
2936 ++id;
2937 }
2938
2939 if (ret == LY_SUCCESS) {
2940 *target = ctx_node;
2941 if (nodetype && !(current_nodetype & nodetype)) {
2942 return LY_EDENIED;
2943 }
2944 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002945 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002946 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002947 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002948 }
2949
2950 return ret;
2951}
2952
2953/**
2954 * @brief Compile information about list's uniques.
2955 * @param[in] ctx Compile context.
2956 * @param[in] uniques Sized array list of unique statements.
2957 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2958 * @return LY_ERR value.
2959 */
2960static LY_ERR
2961lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2962{
2963 LY_ERR ret = LY_SUCCESS;
2964 struct lysc_node_leaf **key, ***unique;
2965 struct lysc_node *parent;
2966 const char *keystr, *delim;
2967 size_t len;
2968 LY_ARRAY_COUNT_TYPE v;
2969 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2970 uint16_t flags;
2971
2972 LY_ARRAY_FOR(uniques, v) {
2973 config = -1;
2974 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2975 keystr = uniques[v].str;
2976 while (keystr) {
2977 delim = strpbrk(keystr, " \t\n");
2978 if (delim) {
2979 len = delim - keystr;
2980 while (isspace(*delim)) {
2981 ++delim;
2982 }
2983 } else {
2984 len = strlen(keystr);
2985 }
2986
2987 /* unique node must be present */
2988 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002989 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02002990 LY_VALUE_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002991 if (ret != LY_SUCCESS) {
2992 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002993 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002994 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01002995 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002996 }
2997 return LY_EVALID;
2998 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002999 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003000 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003001 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003002 return LY_EVALID;
3003 }
3004
3005 /* all referenced leafs must be of the same config type */
3006 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3007 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003008 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003009 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3010 return LY_EVALID;
3011 } else if ((*key)->flags & LYS_CONFIG_W) {
3012 config = 1;
3013 } else { /* LYS_CONFIG_R */
3014 config = 0;
3015 }
3016
3017 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3018 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3019 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003020 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003021 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3022 return LY_EVALID;
3023 }
3024 }
3025
3026 /* check status */
3027 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3028 (*key)->flags, (*key)->module, (*key)->name));
3029
3030 /* mark leaf as unique */
3031 (*key)->flags |= LYS_UNIQUE;
3032
3033 /* next unique value in line */
3034 keystr = delim;
3035 }
3036 /* next unique definition */
3037 }
3038
3039 return LY_SUCCESS;
3040}
3041
3042/**
3043 * @brief Compile parsed list node information.
3044 * @param[in] ctx Compile context
3045 * @param[in] pnode Parsed list node.
3046 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3047 * is enriched with the list-specific information.
3048 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3049 */
3050static LY_ERR
3051lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3052{
3053 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3054 struct lysc_node_list *list = (struct lysc_node_list *)node;
3055 struct lysp_node *child_p;
3056 struct lysc_node_leaf *key, *prev_key = NULL;
3057 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003058 const char *keystr, *delim;
3059 LY_ERR ret = LY_SUCCESS;
3060
3061 list->min = list_p->min;
3062 if (list->min) {
3063 list->flags |= LYS_MAND_TRUE;
3064 }
3065 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3066
3067 LY_LIST_FOR(list_p->child, child_p) {
3068 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3069 }
3070
Michal Vasko5347e3a2020-11-03 17:14:57 +01003071 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02003072 if (list_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003073 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003074 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003075 }
3076
3077 /* keys */
3078 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003079 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003080 return LY_EVALID;
3081 }
3082
3083 /* find all the keys (must be direct children) */
3084 keystr = list_p->key;
3085 if (!keystr) {
3086 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003087 list->flags &= ~LYS_ORDBY_SYSTEM;
3088 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003089 }
3090 while (keystr) {
3091 delim = strpbrk(keystr, " \t\n");
3092 if (delim) {
3093 len = delim - keystr;
3094 while (isspace(*delim)) {
3095 ++delim;
3096 }
3097 } else {
3098 len = strlen(keystr);
3099 }
3100
3101 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003102 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3103 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003104 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003105 return LY_EVALID;
3106 }
3107 /* keys must be unique */
3108 if (key->flags & LYS_KEY) {
3109 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003110 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003111 return LY_EVALID;
3112 }
3113
Radek Krejcia6016992021-03-03 10:13:41 +01003114 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003115 /* key must have the same config flag as the list itself */
3116 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003117 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003118 return LY_EVALID;
3119 }
3120 if (ctx->pmod->version < LYS_VERSION_1_1) {
3121 /* YANG 1.0 denies key to be of empty type */
3122 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003123 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003124 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3125 return LY_EVALID;
3126 }
3127 } else {
3128 /* when and if-feature are illegal on list keys */
3129 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003130 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003131 "List's key must not have any \"when\" statement.");
3132 return LY_EVALID;
3133 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003134 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003135 }
3136
3137 /* check status */
3138 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3139 key->flags, key->module, key->name));
3140
3141 /* ignore default values of the key */
3142 if (key->dflt) {
3143 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3144 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3145 free(key->dflt);
3146 key->dflt = NULL;
3147 }
3148 /* mark leaf as key */
3149 key->flags |= LYS_KEY;
3150
3151 /* move it to the correct position */
3152 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3153 /* fix links in closest previous siblings of the key */
3154 if (key->next) {
3155 key->next->prev = key->prev;
3156 } else {
3157 /* last child */
3158 list->child->prev = key->prev;
3159 }
3160 if (key->prev->next) {
3161 key->prev->next = key->next;
3162 }
3163 /* fix links in the key */
3164 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003165 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003166 key->next = prev_key->next;
3167 } else {
3168 key->prev = list->child->prev;
3169 key->next = list->child;
3170 }
3171 /* fix links in closes future siblings of the key */
3172 if (prev_key) {
3173 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003174 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003175 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003176 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003177 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003178 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003179 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003180 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003181 }
3182 /* fix links in parent */
3183 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003184 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003185 }
3186 }
3187
3188 /* next key value */
3189 prev_key = key;
3190 keystr = delim;
3191 lysc_update_path(ctx, NULL, NULL);
3192 }
3193
3194 /* uniques */
3195 if (list_p->uniques) {
3196 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3197 }
3198
Radek Krejci2a9fc652021-01-22 17:44:34 +01003199 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3200 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3201 LY_CHECK_GOTO(ret, done);
3202 }
3203 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3204 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3205 LY_CHECK_GOTO(ret, done);
3206 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003207
3208 /* checks */
3209 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003210 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003211 list->min, list->max);
3212 return LY_EVALID;
3213 }
3214
3215done:
3216 return ret;
3217}
3218
3219/**
3220 * @brief Do some checks and set the default choice's case.
3221 *
3222 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3223 *
3224 * @param[in] ctx Compile context.
3225 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3226 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3227 * @return LY_ERR value.
3228 */
3229static LY_ERR
3230lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3231{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003232 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003233 const struct lys_module *mod;
3234 const char *prefix = NULL, *name;
3235 size_t prefix_len = 0;
3236
3237 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3238 name = strchr(dflt->str, ':');
3239 if (name) {
3240 prefix = dflt->str;
3241 prefix_len = name - prefix;
3242 ++name;
3243 } else {
3244 name = dflt->str;
3245 }
3246 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003247 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003248 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003249 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003250 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003251 return LY_EVALID;
3252 }
3253 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003254 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003255 }
3256
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003257 ch->dflt = (struct lysc_node_case *)lys_find_child(&ch->node, mod, name, 0, LYS_CASE, LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003258 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003259 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003260 "Default case \"%s\" not found.", dflt->str);
3261 return LY_EVALID;
3262 }
3263
3264 /* no mandatory nodes directly under the default case */
3265 LY_LIST_FOR(ch->dflt->child, iter) {
3266 if (iter->parent != (struct lysc_node *)ch->dflt) {
3267 break;
3268 }
3269 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003270 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003271 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3272 return LY_EVALID;
3273 }
3274 }
3275
3276 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003277 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003278 return LY_EVALID;
3279 }
3280
3281 ch->dflt->flags |= LYS_SET_DFLT;
3282 return LY_SUCCESS;
3283}
3284
3285LY_ERR
3286lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3287 struct ly_set *child_set)
3288{
3289 LY_ERR ret = LY_SUCCESS;
3290 struct lysp_node *child_p_next = child_p->next;
3291 struct lysp_node_case *cs_p;
aPiecekaa320c92021-05-21 07:34:24 +02003292 struct lysc_node_case *compiled_case;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003293
3294 if (child_p->nodetype == LYS_CASE) {
3295 /* standard case under choice */
3296 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3297 } else {
3298 /* we need the implicit case first, so create a fake parsed case */
3299 cs_p = calloc(1, sizeof *cs_p);
3300 cs_p->nodetype = LYS_CASE;
3301 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, free_fake_node);
3302 cs_p->child = child_p;
3303
3304 /* make the child the only case child */
3305 child_p->next = NULL;
3306
3307 /* compile it normally */
3308 ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set);
3309
3310free_fake_node:
3311 /* free the fake parsed node and correct pointers back */
aPiecek082c7dc2021-05-20 08:55:07 +02003312
aPiecekaa320c92021-05-21 07:34:24 +02003313 /* get last case node */
3314 compiled_case = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases->prev;
3315
aPiecek082c7dc2021-05-20 08:55:07 +02003316 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
aPiecekaa320c92021-05-21 07:34:24 +02003317 /* Compiled case node cannot point to his corresponding parsed node
3318 * because it exists temporarily. Therefore, it must be set to NULL.
3319 */
aPiecek082c7dc2021-05-20 08:55:07 +02003320 assert(compiled_case->priv == cs_p);
aPiecek082c7dc2021-05-20 08:55:07 +02003321 compiled_case->priv = NULL;
3322 }
aPiecekaa320c92021-05-21 07:34:24 +02003323
3324 /* The status is copied from his child and not from his parent as usual. */
3325 if (compiled_case->child) {
3326 compiled_case->flags &= ~LYS_STATUS_MASK;
3327 compiled_case->flags |= LYS_STATUS_MASK & compiled_case->child->flags;
3328 }
3329
3330 cs_p->child = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003331 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3332 child_p->next = child_p_next;
3333 }
3334
3335 return ret;
3336}
3337
3338/**
3339 * @brief Compile parsed choice node information.
3340 *
3341 * @param[in] ctx Compile context
3342 * @param[in] pnode Parsed choice node.
3343 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3344 * is enriched with the choice-specific information.
3345 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3346 */
3347static LY_ERR
3348lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3349{
3350 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3351 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3352 struct lysp_node *child_p;
3353 LY_ERR ret = LY_SUCCESS;
3354
3355 assert(node->nodetype == LYS_CHOICE);
3356
3357 LY_LIST_FOR(ch_p->child, child_p) {
3358 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3359 }
3360
3361 /* default branch */
3362 if (ch_p->dflt.str) {
3363 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3364 }
3365
3366 return ret;
3367}
3368
3369/**
3370 * @brief Compile parsed anydata or anyxml node information.
3371 * @param[in] ctx Compile context
3372 * @param[in] pnode Parsed anydata or anyxml node.
3373 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3374 * is enriched with the any-specific information.
3375 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3376 */
3377static LY_ERR
3378lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3379{
3380 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3381 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003382 LY_ERR ret = LY_SUCCESS;
3383
Michal Vasko5347e3a2020-11-03 17:14:57 +01003384 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02003385 if (any_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003386 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003387 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003388 LY_CHECK_GOTO(ret, done);
3389 }
3390
Radek Krejci91531e12021-02-08 19:57:54 +01003391 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003392 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003393 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3394 }
3395done:
3396 return ret;
3397}
3398
3399/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003400 * @brief Prepare the case structure in choice node for the new data node.
3401 *
3402 * It is able to handle implicit as well as explicit cases and the situation when the case has multiple data nodes and the case was already
3403 * created in the choice when the first child was processed.
3404 *
3405 * @param[in] ctx Compile context.
3406 * @param[in] pnode Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case,
3407 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3408 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3409 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3410 * @return The case structure where the child node belongs to, NULL in case of error. Note that the child is not connected into the siblings list,
3411 * it is linked from the case structure only in case it is its first child.
3412 */
3413static LY_ERR
3414lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3415{
3416 struct lysp_node *child_p;
3417 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3418
3419 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3420 /* we have to add an implicit case node into the parent choice */
3421 } else if (pnode->nodetype == LYS_CASE) {
3422 /* explicit parent case */
3423 LY_LIST_FOR(cs_p->child, child_p) {
3424 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3425 }
3426 } else {
3427 LOGINT_RET(ctx->ctx);
3428 }
3429
3430 return LY_SUCCESS;
3431}
3432
3433void
3434lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3435{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003436 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003437
3438 if (add) { /* set flag */
3439 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3440 parent = parent->parent) {
3441 parent->flags |= LYS_MAND_TRUE;
3442 }
3443 } else { /* unset flag */
3444 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003445 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003446 if (iter->flags & LYS_MAND_TRUE) {
3447 /* there is another mandatory node */
3448 return;
3449 }
3450 }
3451 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3452 parent->flags &= ~LYS_MAND_TRUE;
3453 }
3454 }
3455}
3456
3457/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003458 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003459 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003460 * @param[in] name Name of the grouping to find,
3461 * @return NULL when there is no grouping with the specified name
3462 * @return Pointer to the grouping of the specified @p name.
3463 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003464static struct lysp_node_grp *
3465match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003466{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003467 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003468
Radek Krejci2a9fc652021-01-22 17:44:34 +01003469 LY_LIST_FOR(grps, grp) {
3470 if (!strcmp(grp->name, name)) {
3471 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003472 }
3473 }
3474
3475 return NULL;
3476}
3477
3478/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003479 * @brief Find grouping for a uses.
3480 *
3481 * @param[in] ctx Compile context.
3482 * @param[in] uses_p Parsed uses node.
3483 * @param[out] gpr_p Found grouping on success.
3484 * @param[out] grp_pmod Module of @p grp_p on success.
3485 * @return LY_ERR value.
3486 */
3487static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003488lys_compile_uses_find_grouping(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysp_node_grp **grp_p,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003489 struct lysp_module **grp_pmod)
3490{
3491 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003492 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003493 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003494 const char *id, *name, *prefix, *local_pref;
3495 size_t prefix_len, name_len;
3496 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003497 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003498
3499 *grp_p = NULL;
3500 *grp_pmod = NULL;
3501
3502 /* search for the grouping definition */
3503 id = uses_p->name;
3504 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3505 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3506 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3507 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003508 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003509 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003510 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3511 found = ctx->pmod;
3512 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003513 }
3514 }
3515 } else {
3516 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003517 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003518 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003519 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003520 "Invalid prefix used for grouping reference.", uses_p->name);
3521 return LY_EVALID;
3522 }
3523 pmod = mod->parsed;
3524 }
3525
3526 if (!found) {
3527 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003528 if ((grp = match_grouping(pmod->groupings, name))) {
3529 found = pmod;
3530 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003531 /* ... and all the submodules */
3532 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003533 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3534 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003535 break;
3536 }
3537 }
3538 }
3539 }
3540 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003541 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003542 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3543 return LY_EVALID;
3544 }
3545
Michal Vasko7c565922021-06-10 14:58:27 +02003546 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003547 /* remember that the grouping is instantiated to avoid its standalone validation */
3548 grp->flags |= LYS_USED_GRP;
3549 }
3550
3551 *grp_p = grp;
3552 *grp_pmod = found;
3553 return LY_SUCCESS;
3554}
3555
3556/**
3557 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3558 * If present, also apply uses's modificators.
3559 *
3560 * @param[in] ctx Compile context
3561 * @param[in] uses_p Parsed uses schema node.
3562 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3563 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3564 * the compile context.
3565 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3566 */
3567static LY_ERR
3568lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3569{
3570 struct lysp_node *pnode;
3571 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003572 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003573 uint32_t i, grp_stack_count;
3574 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3575 LY_ERR ret = LY_SUCCESS;
3576 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003577 struct ly_set uses_child_set = {0};
3578
3579 /* find the referenced grouping */
3580 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3581
3582 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3583 grp_stack_count = ctx->groupings.count;
3584 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3585 if (grp_stack_count == ctx->groupings.count) {
3586 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003587 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003588 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3589 return LY_EVALID;
3590 }
3591
3592 /* check status */
3593 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3594 LY_CHECK_GOTO(ret, cleanup);
3595
3596 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3597 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3598 LY_CHECK_GOTO(ret, cleanup);
3599
3600 /* switch context's parsed module being processed */
3601 ctx->pmod = grp_mod;
3602
3603 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003604 LY_LIST_FOR(grp->child, pnode) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01003605 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3606 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003607 LY_CHECK_GOTO(ret, cleanup);
3608 }
3609
3610 if (child_set) {
3611 /* add these children to our compiled child_set as well since uses is a schema-only node */
3612 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3613 }
3614
3615 if (uses_p->when) {
3616 /* pass uses's when to all the data children */
3617 for (i = 0; i < uses_child_set.count; ++i) {
3618 child = uses_child_set.snodes[i];
3619
Michal Vasko72244882021-01-12 15:21:05 +01003620 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003621 LY_CHECK_GOTO(ret, cleanup);
3622 }
3623 }
3624
3625 /* compile actions */
3626 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003627 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003628 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3629 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003630 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003631 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3632 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003633 ret = LY_EVALID;
3634 goto cleanup;
3635 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003636 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
3637 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3638 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3639 LY_CHECK_GOTO(ret, cleanup);
3640 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003641
3642 if (uses_p->when) {
3643 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003644 LY_LIST_FOR((struct lysc_node *)*actions, child) {
3645 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003646 LY_CHECK_GOTO(ret, cleanup);
3647 }
3648 }
3649 }
3650
3651 /* compile notifications */
3652 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003653 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003654 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3655 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003656 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003657 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3658 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003659 ret = LY_EVALID;
3660 goto cleanup;
3661 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003662
3663 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
3664 /* LYS_STATUS_USES in uses_status is a special bits combination to be able to detect status flags from uses */
3665 ret = lys_compile_node(ctx, pnode, parent, (uses_p->flags & LYS_STATUS_MASK) | LYS_STATUS_USES, &uses_child_set);
3666 LY_CHECK_GOTO(ret, cleanup);
3667 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003668
3669 if (uses_p->when) {
3670 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003671 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
3672 ret = lys_compile_when(ctx, uses_p->when, uses_p->flags, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003673 LY_CHECK_GOTO(ret, cleanup);
3674 }
3675 }
3676 }
3677
3678 /* check that all augments were applied */
3679 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003680 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3681 /* augment of some parent uses, irrelevant now */
3682 continue;
3683 }
3684
3685 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003686 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3687 ret = LY_ENOTFOUND;
3688 }
3689 LY_CHECK_GOTO(ret, cleanup);
3690
3691 /* check that all refines were applied */
3692 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003693 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3694 /* refine of some paretn uses, irrelevant now */
3695 continue;
3696 }
3697
3698 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003699 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3700 ret = LY_ENOTFOUND;
3701 }
3702 LY_CHECK_GOTO(ret, cleanup);
3703
3704cleanup:
3705 /* reload previous context's parsed module being processed */
3706 ctx->pmod = mod_old;
3707
3708 /* remove the grouping from the stack for circular groupings dependency check */
3709 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3710 assert(ctx->groupings.count == grp_stack_count);
3711
3712 ly_set_erase(&uses_child_set, NULL);
3713 return ret;
3714}
3715
3716static int
3717lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3718{
3719 struct lysp_node *iter;
3720 int len = 0;
3721
3722 *path = NULL;
3723 for (iter = node; iter && len >= 0; iter = iter->parent) {
3724 char *s = *path;
3725 char *id;
3726
3727 switch (iter->nodetype) {
3728 case LYS_USES:
3729 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3730 break;
3731 case LYS_GROUPING:
3732 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3733 break;
3734 case LYS_AUGMENT:
3735 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3736 break;
3737 default:
3738 id = strdup(iter->name);
3739 break;
3740 }
3741
3742 if (!iter->parent) {
3743 /* print prefix */
3744 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3745 } else {
3746 /* prefix is the same as in parent */
3747 len = asprintf(path, "/%s%s", id, s ? s : "");
3748 }
3749 free(s);
3750 free(id);
3751 }
3752
3753 if (len < 0) {
3754 free(*path);
3755 *path = NULL;
3756 } else if (len == 0) {
3757 *path = strdup("/");
3758 len = 1;
3759 }
3760 return len;
3761}
3762
3763LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003764lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003765{
3766 LY_ERR ret;
3767 char *path;
3768 int len;
3769
3770 struct lysp_node_uses fake_uses = {
3771 .parent = pnode,
3772 .nodetype = LYS_USES,
3773 .flags = 0, .next = NULL,
3774 .name = grp->name,
3775 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3776 .refines = NULL, .augments = NULL
3777 };
3778 struct lysc_node_container fake_container = {
3779 .nodetype = LYS_CONTAINER,
3780 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3781 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003782 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003783 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003784 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003785 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003786 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3787 };
3788
3789 if (grp->parent) {
3790 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3791 }
3792
3793 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3794 if (len < 0) {
3795 LOGMEM(ctx->ctx);
3796 return LY_EMEM;
3797 }
3798 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3799 ctx->path_len = (uint32_t)len;
3800 free(path);
3801
3802 lysc_update_path(ctx, NULL, "{grouping}");
3803 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003804 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003805 lysc_update_path(ctx, NULL, NULL);
3806 lysc_update_path(ctx, NULL, NULL);
3807
3808 ctx->path_len = 1;
3809 ctx->path[1] = '\0';
3810
3811 /* cleanup */
3812 lysc_node_container_free(ctx->ctx, &fake_container);
3813
3814 return ret;
3815}
3816
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003817LY_ERR
3818lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3819 struct ly_set *child_set)
3820{
3821 LY_ERR ret = LY_SUCCESS;
3822 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02003823 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003824
3825 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3826
3827 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003828 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003829 } else {
3830 lysc_update_path(ctx, NULL, "{uses}");
3831 lysc_update_path(ctx, NULL, pnode->name);
3832 }
3833
3834 switch (pnode->nodetype) {
3835 case LYS_CONTAINER:
3836 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3837 node_compile_spec = lys_compile_node_container;
3838 break;
3839 case LYS_LEAF:
3840 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3841 node_compile_spec = lys_compile_node_leaf;
3842 break;
3843 case LYS_LIST:
3844 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3845 node_compile_spec = lys_compile_node_list;
3846 break;
3847 case LYS_LEAFLIST:
3848 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3849 node_compile_spec = lys_compile_node_leaflist;
3850 break;
3851 case LYS_CHOICE:
3852 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3853 node_compile_spec = lys_compile_node_choice;
3854 break;
3855 case LYS_CASE:
3856 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3857 node_compile_spec = lys_compile_node_case;
3858 break;
3859 case LYS_ANYXML:
3860 case LYS_ANYDATA:
3861 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3862 node_compile_spec = lys_compile_node_any;
3863 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003864 case LYS_RPC:
3865 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02003866 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003867 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3868 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02003869 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003870 return LY_EVALID;
3871 }
3872 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3873 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02003874 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003875 break;
3876 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02003877 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003878 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3879 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02003880 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003881 return LY_EVALID;
3882 }
3883 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3884 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02003885 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003886 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003887 case LYS_USES:
3888 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3889 lysc_update_path(ctx, NULL, NULL);
3890 lysc_update_path(ctx, NULL, NULL);
3891 return ret;
3892 default:
3893 LOGINT(ctx->ctx);
3894 return LY_EINT;
3895 }
3896 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3897
Radek Krejci2a9fc652021-01-22 17:44:34 +01003898 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003899
Michal Vasko7c565922021-06-10 14:58:27 +02003900 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003901 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003902 return ret;
3903}