blob: 9642efbd23a666d6c218192e22486d0d149ac316 [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/**
Michal Vaskodfd254d2021-06-24 09:24:59 +0200223 * @brief Compile status information of the given statement.
224 *
225 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
226 * has the status correctly set during the compilation.
227 *
228 * @param[in] ctx Compile context
229 * @param[in,out] stmt_flags Compiled flags to update. If the status was set explicitly, it is already set
230 * in the flags value and we just check the compatibility with the parent's status value.
231 * @param[in] stmt_name Statement name, for logging.
232 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
233 * @param[in] parent_name Name of the parent node, for logging.
234 * @param[in] inherit_log Whether to print inherit message.
235 * @return LY_ERR value.
236 */
237static LY_ERR
238lys_compile_status(struct lysc_ctx *ctx, uint16_t *stmt_flags, const char *stmt_name, uint16_t parent_flags,
239 const char *parent_name, ly_bool inherit_log)
240{
241 /* status - it is not inherited by specification, but it does not make sense to have
242 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
243 if (!(*stmt_flags & LYS_STATUS_MASK)) {
244 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
245 if (inherit_log) {
246 LOGVRB("Missing explicit \"%s\" status specified in parent \"%s\", inheriting for \"%s\".",
247 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name, stmt_name);
248 }
249 *stmt_flags |= parent_flags & LYS_STATUS_MASK;
250 } else {
251 *stmt_flags |= LYS_STATUS_CURR;
252 }
253 } else if (parent_flags & LYS_STATUS_MASK) {
254 /* check status compatibility with the parent */
255 if ((parent_flags & LYS_STATUS_MASK) > (*stmt_flags & LYS_STATUS_MASK)) {
256 if (*stmt_flags & LYS_STATUS_CURR) {
257 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
258 "Status \"current\" of \"%s\" is in conflict with the \"%s\" status of parent \"%s\".",
259 stmt_name, (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name);
260 } else { /* LYS_STATUS_DEPRC */
261 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
262 "Status \"deprecated\" of \"%s\" is in conflict with the \"obsolete\" status of parent \"%s\".",
263 stmt_name, parent_name);
264 }
265 return LY_EVALID;
266 }
267 }
268 return LY_SUCCESS;
269}
270
271/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200272 * @brief Compile information from the when statement
273 *
274 * @param[in] ctx Compile context.
275 * @param[in] when_p Parsed when structure.
Michal Vaskodfd254d2021-06-24 09:24:59 +0200276 * @param[in] parent_flags Flags of the parsed node with the when statement.
277 * @param[in] compiled_parent Closest compiled parent of the when statement.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200278 * @param[in] ctx_node Context node for the when statement.
279 * @param[out] when Pointer where to store pointer to the created compiled when structure.
280 * @return LY_ERR value.
281 */
282static LY_ERR
Michal Vaskodfd254d2021-06-24 09:24:59 +0200283lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t parent_flags,
284 const struct lysc_node *compiled_parent, const struct lysc_node *ctx_node, struct lysc_when **when)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200285{
286 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200287 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200288
289 *when = calloc(1, sizeof **when);
290 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
291 (*when)->refcount = 1;
292 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200293 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci8df109d2021-04-23 12:19:08 +0200294 LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200295 (*when)->context = (struct lysc_node *)ctx_node;
296 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
297 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100298 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vaskodfd254d2021-06-24 09:24:59 +0200299 (*when)->flags = (parent_flags & LYS_STATUS_MASK);
300 if (compiled_parent) {
301 LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", compiled_parent->flags, compiled_parent->name, 0));
302 } else {
303 LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", 0, NULL, 0));
304 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200305
306done:
307 return ret;
308}
309
310LY_ERR
Michal Vaskodfd254d2021-06-24 09:24:59 +0200311lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t parent_flags, const struct lysc_node *compiled_parent,
312 const struct lysc_node *ctx_node, struct lysc_node *node, struct lysc_when **when_c)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200313{
314 struct lysc_when **new_when, ***node_when;
315
316 assert(when_p);
317
318 /* get the when array */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100319 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200320
321 /* create new when pointer */
322 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
323 if (!when_c || !(*when_c)) {
324 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +0200325 LY_CHECK_RET(lys_compile_when_(ctx, when_p, parent_flags, compiled_parent, ctx_node, new_when));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200326
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200327 /* remember the compiled when for sharing */
328 if (when_c) {
329 *when_c = *new_when;
330 }
331 } else {
332 /* use the previously compiled when */
333 ++(*when_c)->refcount;
334 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100335 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200336
Michal Vasko6f08e962021-07-23 08:30:47 +0200337 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100338 /* do not check "when" semantics in a grouping, but repeat the check for different node because
339 * of dummy node check */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100340 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200341 }
342
343 return LY_SUCCESS;
344}
345
346/**
347 * @brief Compile information from the must statement
348 * @param[in] ctx Compile context.
349 * @param[in] must_p The parsed must statement structure.
350 * @param[in,out] must Prepared (empty) compiled must structure to fill.
351 * @return LY_ERR value.
352 */
353static LY_ERR
354lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
355{
356 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200357 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200358
359 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200360 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 +0200361 LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200362 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
363 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
364 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
365 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100366 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200367
368done:
369 return ret;
370}
371
372/**
373 * @brief Validate and normalize numeric value from a range definition.
374 * @param[in] ctx Compile context.
375 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
376 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
377 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
378 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
379 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
380 * @param[in] value String value of the range boundary.
381 * @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.
382 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
383 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
384 */
385static LY_ERR
386range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
387 size_t *len, char **valcopy)
388{
389 size_t fraction = 0, size;
390
391 *len = 0;
392
393 assert(value);
394 /* parse value */
395 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
396 return LY_EVALID;
397 }
398
399 if ((value[*len] == '-') || (value[*len] == '+')) {
400 ++(*len);
401 }
402
403 while (isdigit(value[*len])) {
404 ++(*len);
405 }
406
407 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
408 if (basetype == LY_TYPE_DEC64) {
409 goto decimal;
410 } else {
411 *valcopy = strndup(value, *len);
412 return LY_SUCCESS;
413 }
414 }
415 fraction = *len;
416
417 ++(*len);
418 while (isdigit(value[*len])) {
419 ++(*len);
420 }
421
422 if (basetype == LY_TYPE_DEC64) {
423decimal:
424 assert(frdigits);
425 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100426 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200427 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100428 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200429 return LY_EINVAL;
430 }
431 if (fraction) {
432 size = (*len) + (frdigits - ((*len) - 1 - fraction));
433 } else {
434 size = (*len) + frdigits + 1;
435 }
436 *valcopy = malloc(size * sizeof **valcopy);
437 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
438
439 (*valcopy)[size - 1] = '\0';
440 if (fraction) {
441 memcpy(&(*valcopy)[0], &value[0], fraction);
442 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
443 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
444 } else {
445 memcpy(&(*valcopy)[0], &value[0], *len);
446 memset(&(*valcopy)[*len], '0', frdigits);
447 }
448 }
449 return LY_SUCCESS;
450}
451
452/**
453 * @brief Check that values in range are in ascendant order.
454 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
455 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
456 * max can be also equal.
457 * @param[in] value Current value to check.
458 * @param[in] prev_value The last seen value.
459 * @return LY_SUCCESS or LY_EEXIST for invalid order.
460 */
461static LY_ERR
462range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
463{
464 if (unsigned_value) {
465 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
466 return LY_EEXIST;
467 }
468 } else {
469 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
470 return LY_EEXIST;
471 }
472 }
473 return LY_SUCCESS;
474}
475
476/**
477 * @brief Set min/max value of the range part.
478 * @param[in] ctx Compile context.
479 * @param[in] part Range part structure to fill.
480 * @param[in] max Flag to distinguish if storing min or max value.
481 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
482 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
483 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
484 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
485 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
486 * @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.
487 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
488 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
489 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
490 * frdigits value), LY_EMEM.
491 */
492static LY_ERR
493range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
494 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
495{
496 LY_ERR ret = LY_SUCCESS;
497 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100498 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200499
500 if (value) {
501 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
502 LY_CHECK_GOTO(ret, finalize);
503 }
504 if (!valcopy && base_range) {
505 if (max) {
506 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
507 } else {
508 part->min_64 = base_range->parts[0].min_64;
509 }
510 if (!first) {
511 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
512 }
513 goto finalize;
514 }
515
516 switch (basetype) {
517 case LY_TYPE_INT8: /* range */
518 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100519 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 +0200520 } else if (max) {
521 part->max_64 = INT64_C(127);
522 } else {
523 part->min_64 = INT64_C(-128);
524 }
525 if (!ret && !first) {
526 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
527 }
528 break;
529 case LY_TYPE_INT16: /* range */
530 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100531 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
532 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200533 } else if (max) {
534 part->max_64 = INT64_C(32767);
535 } else {
536 part->min_64 = INT64_C(-32768);
537 }
538 if (!ret && !first) {
539 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
540 }
541 break;
542 case LY_TYPE_INT32: /* range */
543 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100544 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
545 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200546 } else if (max) {
547 part->max_64 = INT64_C(2147483647);
548 } else {
549 part->min_64 = INT64_C(-2147483648);
550 }
551 if (!ret && !first) {
552 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
553 }
554 break;
555 case LY_TYPE_INT64: /* range */
556 case LY_TYPE_DEC64: /* range */
557 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100558 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
559 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200560 } else if (max) {
561 part->max_64 = INT64_C(9223372036854775807);
562 } else {
563 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
564 }
565 if (!ret && !first) {
566 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
567 }
568 break;
569 case LY_TYPE_UINT8: /* range */
570 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100571 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 +0200572 } else if (max) {
573 part->max_u64 = UINT64_C(255);
574 } else {
575 part->min_u64 = UINT64_C(0);
576 }
577 if (!ret && !first) {
578 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
579 }
580 break;
581 case LY_TYPE_UINT16: /* range */
582 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100583 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 +0200584 } else if (max) {
585 part->max_u64 = UINT64_C(65535);
586 } else {
587 part->min_u64 = UINT64_C(0);
588 }
589 if (!ret && !first) {
590 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
591 }
592 break;
593 case LY_TYPE_UINT32: /* range */
594 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100595 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
596 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200597 } else if (max) {
598 part->max_u64 = UINT64_C(4294967295);
599 } else {
600 part->min_u64 = UINT64_C(0);
601 }
602 if (!ret && !first) {
603 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
604 }
605 break;
606 case LY_TYPE_UINT64: /* range */
607 case LY_TYPE_STRING: /* length */
608 case LY_TYPE_BINARY: /* length */
609 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100610 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
611 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200612 } else if (max) {
613 part->max_u64 = UINT64_C(18446744073709551615);
614 } else {
615 part->min_u64 = UINT64_C(0);
616 }
617 if (!ret && !first) {
618 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
619 }
620 break;
621 default:
622 LOGINT(ctx->ctx);
623 ret = LY_EINT;
624 }
625
626finalize:
627 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100628 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200629 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
630 length_restr ? "length" : "range", valcopy ? valcopy : *value);
631 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100632 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200633 "Invalid %s restriction - invalid value \"%s\".",
634 length_restr ? "length" : "range", valcopy ? valcopy : *value);
635 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100636 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200637 "Invalid %s restriction - values are not in ascending order (%s).",
638 length_restr ? "length" : "range",
639 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
640 } else if (!ret && value) {
641 *value = *value + len;
642 }
643 free(valcopy);
644 return ret;
645}
646
647/**
648 * @brief Compile the parsed range restriction.
649 * @param[in] ctx Compile context.
650 * @param[in] range_p Parsed range structure to compile.
651 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
652 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
653 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
654 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
655 * range restriction must be more restrictive than the base_range.
656 * @param[in,out] range Pointer to the created current range structure.
657 * @return LY_ERR value.
658 */
659static LY_ERR
660lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
661 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
662{
aPiecek1c4da362021-04-29 14:26:34 +0200663 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200664 const char *expr;
665 struct lysc_range_part *parts = NULL, *part;
666 ly_bool range_expected = 0, uns;
667 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
668
669 assert(range);
670 assert(range_p);
671
672 expr = range_p->arg.str;
673 while (1) {
674 if (isspace(*expr)) {
675 ++expr;
676 } else if (*expr == '\0') {
677 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100678 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200679 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200680 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200681 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200682 goto cleanup;
683 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100684 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200685 "Invalid %s restriction - unexpected end of the expression (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200686 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200687 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200688 goto cleanup;
689 }
690 parts_done++;
691 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100692 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200693 if (parts) {
694 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100695 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200696 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100697 (int)(expr - range_p->arg.str), range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200698 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200699 goto cleanup;
700 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100701 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200702
703 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200704 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 +0200705 part->max_64 = part->min_64;
706 } else if (*expr == '|') {
707 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100708 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200709 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200710 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200711 goto cleanup;
712 }
713 expr++;
714 parts_done++;
715 /* process next part of the expression */
716 } else if (!strncmp(expr, "..", 2)) {
717 expr += 2;
718 while (isspace(*expr)) {
719 expr++;
720 }
721 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100722 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200723 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
aPiecek1c4da362021-04-29 14:26:34 +0200724 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200725 goto cleanup;
726 }
727 /* continue expecting the upper boundary */
728 range_expected = 1;
729 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
730 /* number */
731 if (range_expected) {
732 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200733 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 +0200734 range_expected = 0;
735 } else {
736 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200737 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 +0200738 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
739 part->max_64 = part->min_64;
740 }
741
742 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100743 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
744 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200745 while (isspace(*expr)) {
746 expr++;
747 }
748 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100749 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200750 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200751 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200752 goto cleanup;
753 }
754 if (range_expected) {
755 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200756 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 +0200757 range_expected = 0;
758 } else {
759 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200760 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 +0200761 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
762 part->min_64 = part->max_64;
763 }
764 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100765 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200766 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200767 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200768 goto cleanup;
769 }
770 }
771
772 /* check with the previous range/length restriction */
773 if (base_range) {
774 switch (basetype) {
775 case LY_TYPE_BINARY:
776 case LY_TYPE_UINT8:
777 case LY_TYPE_UINT16:
778 case LY_TYPE_UINT32:
779 case LY_TYPE_UINT64:
780 case LY_TYPE_STRING:
781 uns = 1;
782 break;
783 case LY_TYPE_DEC64:
784 case LY_TYPE_INT8:
785 case LY_TYPE_INT16:
786 case LY_TYPE_INT32:
787 case LY_TYPE_INT64:
788 uns = 0;
789 break;
790 default:
791 LOGINT(ctx->ctx);
792 ret = LY_EINT;
793 goto cleanup;
794 }
795 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
796 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
797 goto baseerror;
798 }
799 /* current lower bound is not lower than the base */
800 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
801 /* base has single value */
802 if (base_range->parts[v].min_64 == parts[u].min_64) {
803 /* both lower bounds are the same */
804 if (parts[u].min_64 != parts[u].max_64) {
805 /* current continues with a range */
806 goto baseerror;
807 } else {
808 /* equal single values, move both forward */
809 ++v;
810 continue;
811 }
812 } else {
813 /* base is single value lower than current range, so the
814 * value from base range is removed in the current,
815 * move only base and repeat checking */
816 ++v;
817 --u;
818 continue;
819 }
820 } else {
821 /* base is the range */
822 if (parts[u].min_64 == parts[u].max_64) {
823 /* current is a single value */
824 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
825 /* current is behind the base range, so base range is omitted,
826 * move the base and keep the current for further check */
827 ++v;
828 --u;
829 } /* else it is within the base range, so move the current, but keep the base */
830 continue;
831 } else {
832 /* both are ranges - check the higher bound, the lower was already checked */
833 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
834 /* higher bound is higher than the current higher bound */
835 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
836 /* but the current lower bound is also higher, so the base range is omitted,
837 * continue with the same current, but move the base */
838 --u;
839 ++v;
840 continue;
841 }
842 /* current range starts within the base range but end behind it */
843 goto baseerror;
844 } else {
845 /* current range is smaller than the base,
846 * move current, but stay with the base */
847 continue;
848 }
849 }
850 }
851 }
852 if (u != parts_done) {
853baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100854 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200855 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200856 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200857 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200858 goto cleanup;
859 }
860 }
861
862 if (!(*range)) {
863 *range = calloc(1, sizeof **range);
864 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
865 }
866
867 /* we rewrite the following values as the types chain is being processed */
868 if (range_p->eapptag) {
869 lydict_remove(ctx->ctx, (*range)->eapptag);
870 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
871 }
872 if (range_p->emsg) {
873 lydict_remove(ctx->ctx, (*range)->emsg);
874 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
875 }
876 if (range_p->dsc) {
877 lydict_remove(ctx->ctx, (*range)->dsc);
878 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
879 }
880 if (range_p->ref) {
881 lydict_remove(ctx->ctx, (*range)->ref);
882 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
883 }
884 /* extensions are taken only from the last range by the caller */
885
886 (*range)->parts = parts;
887 parts = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200888cleanup:
889 LY_ARRAY_FREE(parts);
890
891 return ret;
892}
893
894LY_ERR
Radek Krejci2efc45b2020-12-22 16:25:44 +0100895lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200896{
897 size_t idx, idx2, start, end, size, brack;
898 char *perl_regex, *ptr;
Christian Hoppsdafed222021-03-22 13:02:42 -0400899 int err_code, compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200900 const char *orig_ptr;
901 PCRE2_SIZE err_offset;
902 pcre2_code *code_local;
903
904#define URANGE_LEN 19
905 char *ublock2urange[][2] = {
906 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
907 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
908 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
909 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
910 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
911 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
912 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
913 {"Greek", "[\\x{0370}-\\x{03FF}]"},
914 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
915 {"Armenian", "[\\x{0530}-\\x{058F}]"},
916 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
917 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
918 {"Syriac", "[\\x{0700}-\\x{074F}]"},
919 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
920 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
921 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
922 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
923 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
924 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
925 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
926 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
927 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
928 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
929 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
930 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
931 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
932 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
933 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
934 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
935 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
936 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
937 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
938 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
939 {"Ogham", "[\\x{1680}-\\x{169F}]"},
940 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
941 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
942 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
943 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
944 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
945 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
946 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
947 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
948 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
949 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
950 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
951 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
952 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
953 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
954 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
955 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
956 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
957 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
958 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
959 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
960 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
961 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
962 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
963 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
964 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
965 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
966 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
967 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
968 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
969 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
970 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
971 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
972 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
973 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
974 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
975 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
976 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
977 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
978 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
979 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
980 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
981 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
982 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
983 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
984 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
985 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
986 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
987 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
988 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +0100989 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200990 {NULL, NULL}
991 };
992
993 /* adjust the expression to a Perl equivalent
994 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
995
996 /* allocate space for the transformed pattern */
997 size = strlen(pattern) + 1;
Christian Hoppsdafed222021-03-22 13:02:42 -0400998 compile_opts = PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
999#ifdef PCRE2_ENDANCHORED
1000 compile_opts |= PCRE2_ENDANCHORED;
1001#else
1002 /* add space for trailing $ anchor */
1003 size++;
1004#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001005 perl_regex = malloc(size);
1006 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1007 perl_regex[0] = '\0';
1008
1009 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1010 brack = 0;
1011 idx = 0;
1012 orig_ptr = pattern;
1013 while (orig_ptr[0]) {
1014 switch (orig_ptr[0]) {
1015 case '$':
1016 case '^':
1017 if (!brack) {
1018 /* make space for the extra character */
1019 ++size;
1020 perl_regex = ly_realloc(perl_regex, size);
1021 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1022
1023 /* print escape slash */
1024 perl_regex[idx] = '\\';
1025 ++idx;
1026 }
1027 break;
1028 case '[':
1029 /* must not be escaped */
1030 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1031 ++brack;
1032 }
1033 break;
1034 case ']':
1035 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1036 /* pattern was checked and compiled already */
1037 assert(brack);
1038 --brack;
1039 }
1040 break;
1041 default:
1042 break;
1043 }
1044
1045 /* copy char */
1046 perl_regex[idx] = orig_ptr[0];
1047
1048 ++idx;
1049 ++orig_ptr;
1050 }
Christian Hoppsdafed222021-03-22 13:02:42 -04001051#ifndef PCRE2_ENDANCHORED
1052 /* anchor match to end of subject */
1053 perl_regex[idx++] = '$';
1054#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001055 perl_regex[idx] = '\0';
1056
1057 /* substitute Unicode Character Blocks with exact Character Ranges */
1058 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1059 start = ptr - perl_regex;
1060
1061 ptr = strchr(ptr, '}');
1062 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001063 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001064 free(perl_regex);
1065 return LY_EVALID;
1066 }
1067 end = (ptr - perl_regex) + 1;
1068
1069 /* need more space */
1070 if (end - start < URANGE_LEN) {
1071 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1072 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1073 }
1074
1075 /* find our range */
1076 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001077 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1078 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001079 break;
1080 }
1081 }
1082 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001083 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001084 free(perl_regex);
1085 return LY_EVALID;
1086 }
1087
1088 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1089 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1090 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1091 ++idx;
1092 }
1093 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1094 --idx;
1095 }
1096 }
1097 if (idx) {
1098 /* skip brackets */
1099 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1100 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1101 } else {
1102 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1103 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1104 }
1105 }
1106
1107 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001108 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001109 &err_code, &err_offset, NULL);
1110 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001111 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1112 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001113 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001114 free(perl_regex);
1115 return LY_EVALID;
1116 }
1117 free(perl_regex);
1118
1119 if (code) {
1120 *code = code_local;
1121 } else {
1122 free(code_local);
1123 }
1124
1125 return LY_SUCCESS;
1126
1127#undef URANGE_LEN
1128}
1129
1130/**
1131 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1132 * @param[in] ctx Compile context.
1133 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1134 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1135 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1136 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1137 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1138 */
1139static LY_ERR
1140lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1141 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1142{
1143 struct lysc_pattern **pattern;
1144 LY_ARRAY_COUNT_TYPE u;
1145 LY_ERR ret = LY_SUCCESS;
1146
1147 /* first, copy the patterns from the base type */
1148 if (base_patterns) {
1149 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1150 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1151 }
1152
1153 LY_ARRAY_FOR(patterns_p, u) {
1154 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1155 *pattern = calloc(1, sizeof **pattern);
1156 ++(*pattern)->refcount;
1157
Radek Krejci2efc45b2020-12-22 16:25:44 +01001158 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001159 LY_CHECK_RET(ret);
1160
Radek Krejcif13b87b2020-12-01 22:02:17 +01001161 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001162 (*pattern)->inverted = 1;
1163 }
1164 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1165 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1166 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1167 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1168 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001169 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001170 }
1171done:
1172 return ret;
1173}
1174
1175/**
1176 * @brief map of the possible restrictions combination for the specific built-in type.
1177 */
1178static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1179 0 /* LY_TYPE_UNKNOWN */,
1180 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1181 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1182 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1183 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1184 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1185 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1186 LYS_SET_BIT /* LY_TYPE_BITS */,
1187 0 /* LY_TYPE_BOOL */,
1188 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1189 0 /* LY_TYPE_EMPTY */,
1190 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1191 LYS_SET_BASE /* LY_TYPE_IDENT */,
1192 LYS_SET_REQINST /* LY_TYPE_INST */,
1193 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1194 LYS_SET_TYPE /* LY_TYPE_UNION */,
1195 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1196 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1197 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1198 LYS_SET_RANGE /* LY_TYPE_INT64 */
1199};
1200
1201/**
1202 * @brief stringification of the YANG built-in data types
1203 */
1204const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001205 LY_TYPE_UNKNOWN_STR,
1206 LY_TYPE_BINARY_STR,
1207 LY_TYPE_UINT8_STR,
1208 LY_TYPE_UINT16_STR,
1209 LY_TYPE_UINT32_STR,
1210 LY_TYPE_UINT64_STR,
1211 LY_TYPE_STRING_STR,
1212 LY_TYPE_BITS_STR,
1213 LY_TYPE_BOOL_STR,
1214 LY_TYPE_DEC64_STR,
1215 LY_TYPE_EMPTY_STR,
1216 LY_TYPE_ENUM_STR,
1217 LY_TYPE_IDENT_STR,
1218 LY_TYPE_INST_STR,
1219 LY_TYPE_LEAFREF_STR,
1220 LY_TYPE_UNION_STR,
1221 LY_TYPE_INT8_STR,
1222 LY_TYPE_INT16_STR,
1223 LY_TYPE_INT32_STR,
1224 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001225};
1226
1227/**
1228 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1229 * @param[in] ctx Compile context.
1230 * @param[in] enums_p Array of the parsed enum structures to compile.
1231 * @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.
1232 * @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 +01001233 * @param[out] bitenums Newly created array of the compiled bitenums information for the current type.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001234 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1235 */
1236static LY_ERR
1237lys_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 +01001238 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001239{
1240 LY_ERR ret = LY_SUCCESS;
1241 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001242 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1243 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001244 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001245 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001246
1247 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001248 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001249 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1250 return LY_EVALID;
1251 }
1252
1253 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001254 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001255 if (base_enums) {
1256 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1257 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001258 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001259 break;
1260 }
1261 }
1262 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001263 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001264 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001265 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001266 return LY_EVALID;
1267 }
1268 match = v;
1269 }
1270
1271 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001272 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001273 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001274 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001275 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001276 LY_ARRAY_FOR(*bitenums, v) {
1277 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001278 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001279 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001280 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001281 return LY_EVALID;
1282 }
1283 }
1284 } else if (base_enums) {
1285 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001286 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001287 } else {
1288 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001289 if (u == 0) {
1290 cur_val = 0;
1291 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001292 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001293 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001294 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001295 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001296 } else {
1297 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001298 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001299 }
1300
1301 /* save highest value for auto assing */
1302 if (highest_value < cur_val) {
1303 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001304 }
1305 } else { /* LY_TYPE_BITS */
1306 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001307 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001308 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001309 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001310 LY_ARRAY_FOR(*bitenums, v) {
1311 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001312 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001313 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001314 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001315 return LY_EVALID;
1316 }
1317 }
1318 } else if (base_enums) {
1319 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001320 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001321 } else {
1322 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001323 if (u == 0) {
1324 cur_pos = 0;
1325 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001326 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001327 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001328 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001329 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001330 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001331 } else {
1332 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001333 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001334 }
1335
1336 /* save highest position for auto assing */
1337 if (highest_position < cur_pos) {
1338 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001339 }
1340 }
1341
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001342 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001343 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001344 if (basetype == LY_TYPE_ENUM) {
1345 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001346 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001347 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001348 enums_p[u].name, base_enums[match].value, cur_val);
1349 return LY_EVALID;
1350 }
1351 } else {
1352 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001353 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001354 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001355 enums_p[u].name, base_enums[match].position, cur_pos);
1356 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001357 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001358 }
1359 }
1360
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001361 /* evaluate if-ffeatures */
1362 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1363 if (!enabled) {
1364 continue;
1365 }
1366
1367 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001368 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001369 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1370 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1371 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001372 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 +01001373 if (basetype == LY_TYPE_ENUM) {
1374 e->value = cur_val;
1375 } else {
1376 e->position = cur_pos;
1377 }
Radek Krejciab430862021-03-02 20:13:40 +01001378 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001379
1380 if (basetype == LY_TYPE_BITS) {
1381 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001382 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001383 if (v != u) {
1384 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001385 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1386 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001387 }
1388 }
1389 }
1390
1391done:
1392 return ret;
1393}
1394
Radek Krejci6a205692020-12-09 13:58:22 +01001395static LY_ERR
1396lys_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 +01001397 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001398{
1399 LY_ERR ret = LY_SUCCESS;
1400 struct lysc_type **utypes = *utypes_p;
1401 struct lysc_type_union *un_aux = NULL;
1402
1403 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1404 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001405 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1406 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001407 LY_CHECK_GOTO(ret, error);
1408 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1409 /* add space for additional types from the union subtype */
1410 un_aux = (struct lysc_type_union *)utypes[u + additional];
1411 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1412 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1413
1414 /* copy subtypes of the subtype union */
1415 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1416 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1417 struct lysc_type_leafref *lref;
1418
1419 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1420 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1421 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1422 lref = (struct lysc_type_leafref *)utypes[u + additional];
1423
1424 lref->basetype = LY_TYPE_LEAFREF;
1425 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1426 LY_CHECK_GOTO(ret, error);
Michal Vasko080064b2021-08-31 15:20:44 +02001427 lref->refcount = 1;
Radek Krejci6a205692020-12-09 13:58:22 +01001428 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001429 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001430 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001431 LY_CHECK_GOTO(ret, error);
1432 /* TODO extensions */
1433
1434 } else {
1435 utypes[u + additional] = un_aux->types[v];
Michal Vasko04338d92021-09-01 07:58:14 +02001436 LY_ATOMIC_INC_BARRIER(un_aux->types[v]->refcount);
Radek Krejci6a205692020-12-09 13:58:22 +01001437 }
1438 ++additional;
1439 LY_ARRAY_INCREMENT(utypes);
1440 }
1441 /* compensate u increment in main loop */
1442 --additional;
1443
1444 /* free the replaced union subtype */
1445 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1446 un_aux = NULL;
1447 } else {
1448 LY_ARRAY_INCREMENT(utypes);
1449 }
1450 }
1451
1452 *utypes_p = utypes;
1453 return LY_SUCCESS;
1454
1455error:
1456 if (un_aux) {
1457 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1458 }
1459 *utypes_p = utypes;
1460 return ret;
1461}
1462
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001463/**
1464 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1465 * @param[in] ctx Compile context.
1466 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1467 * @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 +02001468 * @param[in] context_name Name of the context node or referencing typedef for logging.
1469 * @param[in] type_p Parsed type to compile.
1470 * @param[in] basetype Base YANG built-in type of the type to compile.
1471 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1472 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1473 * @param[out] type Newly created type structure with the filled information about the type.
1474 * @return LY_ERR value.
1475 */
1476static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001477lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1478 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 +02001479{
1480 LY_ERR ret = LY_SUCCESS;
1481 struct lysc_type_bin *bin;
1482 struct lysc_type_num *num;
1483 struct lysc_type_str *str;
1484 struct lysc_type_bits *bits;
1485 struct lysc_type_enum *enumeration;
1486 struct lysc_type_dec *dec;
1487 struct lysc_type_identityref *idref;
1488 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001489 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001490
1491 switch (basetype) {
1492 case LY_TYPE_BINARY:
1493 bin = (struct lysc_type_bin *)(*type);
1494
1495 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1496 if (type_p->length) {
1497 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1498 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1499 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001500 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001501 }
1502 }
1503 break;
1504 case LY_TYPE_BITS:
1505 /* RFC 7950 9.7 - bits */
1506 bits = (struct lysc_type_bits *)(*type);
1507 if (type_p->bits) {
1508 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1509 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1510 (struct lysc_type_bitenum_item **)&bits->bits));
1511 }
1512
1513 if (!base && !type_p->flags) {
1514 /* type derived from bits built-in type must contain at least one bit */
1515 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001516 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001517 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001518 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001519 }
1520 return LY_EVALID;
1521 }
1522 break;
1523 case LY_TYPE_DEC64:
1524 dec = (struct lysc_type_dec *)(*type);
1525
1526 /* RFC 7950 9.3.4 - fraction-digits */
1527 if (!base) {
1528 if (!type_p->fraction_digits) {
1529 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001530 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001531 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001532 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001533 }
1534 return LY_EVALID;
1535 }
1536 dec->fraction_digits = type_p->fraction_digits;
1537 } else {
1538 if (type_p->fraction_digits) {
1539 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1540 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001541 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001542 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1543 tpdfname);
1544 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001545 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001546 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1547 }
1548 return LY_EVALID;
1549 }
1550 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1551 }
1552
1553 /* RFC 7950 9.2.4 - range */
1554 if (type_p->range) {
1555 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1556 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1557 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001558 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001559 }
1560 }
1561 break;
1562 case LY_TYPE_STRING:
1563 str = (struct lysc_type_str *)(*type);
1564
1565 /* RFC 7950 9.4.4 - length */
1566 if (type_p->length) {
1567 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1568 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1569 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001570 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001571 }
1572 } else if (base && ((struct lysc_type_str *)base)->length) {
1573 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1574 }
1575
1576 /* RFC 7950 9.4.5 - pattern */
1577 if (type_p->patterns) {
1578 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1579 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1580 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1581 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1582 }
1583 break;
1584 case LY_TYPE_ENUM:
1585 enumeration = (struct lysc_type_enum *)(*type);
1586
1587 /* RFC 7950 9.6 - enum */
1588 if (type_p->enums) {
1589 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1590 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1591 }
1592
1593 if (!base && !type_p->flags) {
1594 /* type derived from enumerations built-in type must contain at least one enum */
1595 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001596 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001597 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001598 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001599 }
1600 return LY_EVALID;
1601 }
1602 break;
1603 case LY_TYPE_INT8:
1604 case LY_TYPE_UINT8:
1605 case LY_TYPE_INT16:
1606 case LY_TYPE_UINT16:
1607 case LY_TYPE_INT32:
1608 case LY_TYPE_UINT32:
1609 case LY_TYPE_INT64:
1610 case LY_TYPE_UINT64:
1611 num = (struct lysc_type_num *)(*type);
1612
1613 /* RFC 6020 9.2.4 - range */
1614 if (type_p->range) {
1615 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1616 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1617 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001618 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001619 }
1620 }
1621 break;
1622 case LY_TYPE_IDENT:
1623 idref = (struct lysc_type_identityref *)(*type);
1624
1625 /* RFC 7950 9.10.2 - base */
1626 if (type_p->bases) {
1627 if (base) {
1628 /* only the directly derived identityrefs can contain base specification */
1629 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001630 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001631 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1632 tpdfname);
1633 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001634 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001635 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1636 }
1637 return LY_EVALID;
1638 }
aPiecekf4a0a192021-08-03 15:14:17 +02001639 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001640 }
1641
1642 if (!base && !type_p->flags) {
1643 /* type derived from identityref built-in type must contain at least one base */
1644 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001645 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001646 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001647 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001648 }
1649 return LY_EVALID;
1650 }
1651 break;
1652 case LY_TYPE_LEAFREF:
1653 lref = (struct lysc_type_leafref *)*type;
1654
1655 /* RFC 7950 9.9.3 - require-instance */
1656 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001657 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001658 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001659 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001660 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1661 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001662 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001663 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1664 }
1665 return LY_EVALID;
1666 }
1667 lref->require_instance = type_p->require_instance;
1668 } else if (base) {
1669 /* inherit */
1670 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1671 } else {
1672 /* default is true */
1673 lref->require_instance = 1;
1674 }
1675 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001676 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001677
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001678 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001679 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 +02001680 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001681 } else if (base) {
1682 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Radek Krejci8df109d2021-04-23 12:19:08 +02001683 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001684 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001685 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001686 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001687 return LY_EVALID;
1688 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001689 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001690 return LY_EVALID;
1691 }
1692 break;
1693 case LY_TYPE_INST:
1694 /* RFC 7950 9.9.3 - require-instance */
1695 if (type_p->flags & LYS_SET_REQINST) {
1696 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1697 } else {
1698 /* default is true */
1699 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1700 }
1701 break;
1702 case LY_TYPE_UNION:
1703 un = (struct lysc_type_union *)(*type);
1704
1705 /* RFC 7950 7.4 - type */
1706 if (type_p->types) {
1707 if (base) {
1708 /* only the directly derived union can contain types specification */
1709 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001710 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001711 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1712 tpdfname);
1713 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001714 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001715 "Invalid type substatement for the type not directly derived from union built-in type.");
1716 }
1717 return LY_EVALID;
1718 }
1719 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001720 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 +02001721 }
1722
1723 if (!base && !type_p->flags) {
1724 /* type derived from union built-in type must contain at least one type */
1725 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001726 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001727 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001728 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001729 }
1730 return LY_EVALID;
1731 }
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 if (tpdfname) {
1740 switch (basetype) {
1741 case LY_TYPE_BINARY:
1742 type_p->compiled = *type;
1743 *type = calloc(1, sizeof(struct lysc_type_bin));
1744 break;
1745 case LY_TYPE_BITS:
1746 type_p->compiled = *type;
1747 *type = calloc(1, sizeof(struct lysc_type_bits));
1748 break;
1749 case LY_TYPE_DEC64:
1750 type_p->compiled = *type;
1751 *type = calloc(1, sizeof(struct lysc_type_dec));
1752 break;
1753 case LY_TYPE_STRING:
1754 type_p->compiled = *type;
1755 *type = calloc(1, sizeof(struct lysc_type_str));
1756 break;
1757 case LY_TYPE_ENUM:
1758 type_p->compiled = *type;
1759 *type = calloc(1, sizeof(struct lysc_type_enum));
1760 break;
1761 case LY_TYPE_INT8:
1762 case LY_TYPE_UINT8:
1763 case LY_TYPE_INT16:
1764 case LY_TYPE_UINT16:
1765 case LY_TYPE_INT32:
1766 case LY_TYPE_UINT32:
1767 case LY_TYPE_INT64:
1768 case LY_TYPE_UINT64:
1769 type_p->compiled = *type;
1770 *type = calloc(1, sizeof(struct lysc_type_num));
1771 break;
1772 case LY_TYPE_IDENT:
1773 type_p->compiled = *type;
1774 *type = calloc(1, sizeof(struct lysc_type_identityref));
1775 break;
1776 case LY_TYPE_LEAFREF:
1777 type_p->compiled = *type;
1778 *type = calloc(1, sizeof(struct lysc_type_leafref));
1779 break;
1780 case LY_TYPE_INST:
1781 type_p->compiled = *type;
1782 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1783 break;
1784 case LY_TYPE_UNION:
1785 type_p->compiled = *type;
1786 *type = calloc(1, sizeof(struct lysc_type_union));
1787 break;
1788 case LY_TYPE_BOOL:
1789 case LY_TYPE_EMPTY:
1790 case LY_TYPE_UNKNOWN: /* just to complete switch */
1791 break;
1792 }
1793 }
1794 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1795
1796cleanup:
1797 return ret;
1798}
1799
Radek Krejcibf940f92021-03-24 21:04:13 +01001800/**
1801 * @brief Find the correct plugin implementing the described type
1802 *
1803 * @param[in] mod Module where the type is defined
1804 * @param[in] name Name of the type (typedef)
1805 * @param[in] basetype Type's basetype (when the built-in base plugin is supposed to be used)
1806 * @return Pointer to the plugin implementing the described data type.
1807 */
1808static struct lyplg_type *
1809lys_compile_type_get_plugin(struct lys_module *mod, const char *name, LY_DATA_TYPE basetype)
1810{
1811 struct lyplg_type *p;
1812
1813 /* try to find loaded user type plugins */
1814 p = lyplg_find(LYPLG_TYPE, mod->name, mod->revision, name);
1815 if (!p) {
1816 /* use the internal built-in type implementation */
1817 p = lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
1818 }
1819
1820 return p;
1821}
1822
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001823LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001824lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1825 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001826{
1827 LY_ERR ret = LY_SUCCESS;
1828 ly_bool dummyloops = 0;
1829 struct type_context {
1830 const struct lysp_tpdf *tpdf;
1831 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001832 } *tctx, *tctx_prev = NULL, *tctx_iter;
1833 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1834 struct lysc_type *base = NULL, *prev_type;
1835 struct ly_set tpdf_chain = {0};
Michal Vasko388a6632021-08-06 11:27:43 +02001836 struct lyplg_type *plugin;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001837
1838 (*type) = NULL;
1839 if (dflt) {
1840 *dflt = NULL;
1841 }
1842
1843 tctx = calloc(1, sizeof *tctx);
1844 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001845 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 +02001846 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001847 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1848 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001849 if (basetype) {
1850 break;
1851 }
1852
1853 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001854 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1855 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001856 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1857
1858 if (units && !*units) {
1859 /* inherit units */
1860 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1861 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1862 }
1863 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1864 /* inherit default */
1865 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1866 }
1867 if (dummyloops && (!units || *units) && dflt && *dflt) {
1868 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1869 break;
1870 }
1871
Michal Vasko080064b2021-08-31 15:20:44 +02001872 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
Radek Krejci720d2612021-03-03 19:44:22 +01001873 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1874 * and we need now recompile the type again in the updated context. */
1875 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1876 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1877 }
1878
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001879 if (tctx->tpdf->type.compiled) {
1880 /* it is not necessary to continue, the rest of the chain was already compiled,
1881 * but we still may need to inherit default and units values, so start dummy loops */
1882 basetype = tctx->tpdf->type.compiled->basetype;
1883 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1884 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1885
1886 if ((units && !*units) || (dflt && !*dflt)) {
1887 dummyloops = 1;
1888 goto preparenext;
1889 } else {
1890 tctx = NULL;
1891 break;
1892 }
1893 }
1894
1895 /* circular typedef reference detection */
1896 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1897 /* local part */
1898 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001899 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001900 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001901 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1902 free(tctx);
1903 ret = LY_EVALID;
1904 goto cleanup;
1905 }
1906 }
1907 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1908 /* global part for unions corner case */
1909 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001910 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001911 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001912 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1913 free(tctx);
1914 ret = LY_EVALID;
1915 goto cleanup;
1916 }
1917 }
1918
1919 /* store information for the following processing */
1920 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1921 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1922
1923preparenext:
1924 /* prepare next loop */
1925 tctx_prev = tctx;
1926 tctx = calloc(1, sizeof *tctx);
1927 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1928 }
1929 free(tctx);
1930
1931 /* allocate type according to the basetype */
1932 switch (basetype) {
1933 case LY_TYPE_BINARY:
1934 *type = calloc(1, sizeof(struct lysc_type_bin));
1935 break;
1936 case LY_TYPE_BITS:
1937 *type = calloc(1, sizeof(struct lysc_type_bits));
1938 break;
1939 case LY_TYPE_BOOL:
1940 case LY_TYPE_EMPTY:
1941 *type = calloc(1, sizeof(struct lysc_type));
1942 break;
1943 case LY_TYPE_DEC64:
1944 *type = calloc(1, sizeof(struct lysc_type_dec));
1945 break;
1946 case LY_TYPE_ENUM:
1947 *type = calloc(1, sizeof(struct lysc_type_enum));
1948 break;
1949 case LY_TYPE_IDENT:
1950 *type = calloc(1, sizeof(struct lysc_type_identityref));
1951 break;
1952 case LY_TYPE_INST:
1953 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1954 break;
1955 case LY_TYPE_LEAFREF:
1956 *type = calloc(1, sizeof(struct lysc_type_leafref));
1957 break;
1958 case LY_TYPE_STRING:
1959 *type = calloc(1, sizeof(struct lysc_type_str));
1960 break;
1961 case LY_TYPE_UNION:
1962 *type = calloc(1, sizeof(struct lysc_type_union));
1963 break;
1964 case LY_TYPE_INT8:
1965 case LY_TYPE_UINT8:
1966 case LY_TYPE_INT16:
1967 case LY_TYPE_UINT16:
1968 case LY_TYPE_INT32:
1969 case LY_TYPE_UINT32:
1970 case LY_TYPE_INT64:
1971 case LY_TYPE_UINT64:
1972 *type = calloc(1, sizeof(struct lysc_type_num));
1973 break;
1974 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001975 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001976 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1977 ret = LY_EVALID;
1978 goto cleanup;
1979 }
1980 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1981 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001982 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001983 ly_data_type2str[basetype]);
1984 free(*type);
1985 (*type) = NULL;
1986 ret = LY_EVALID;
1987 goto cleanup;
1988 }
1989
1990 /* get restrictions from the referred typedefs */
1991 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1992 tctx = (struct type_context *)tpdf_chain.objs[u];
1993
1994 /* remember the typedef context for circular check */
1995 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1996 LY_CHECK_GOTO(ret, cleanup);
1997
1998 if (tctx->tpdf->type.compiled) {
Michal Vasko388a6632021-08-06 11:27:43 +02001999 /* already compiled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002000 base = tctx->tpdf->type.compiled;
2001 continue;
Michal Vasko388a6632021-08-06 11:27:43 +02002002 }
2003
2004 /* get plugin for the specific typedef */
2005 plugin = lys_compile_type_get_plugin(tctx->tpdf->type.pmod->mod, tctx->tpdf->name, basetype);
2006 assert(plugin);
2007
2008 if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags) &&
2009 (plugin == base->plugin)) {
2010 /* no change, reuse the compiled base */
2011 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002012 LY_ATOMIC_INC_BARRIER(base->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002013 continue;
2014 }
2015
Michal Vasko04338d92021-09-01 07:58:14 +02002016 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002017 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002018 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002019 tctx->tpdf->name, ly_data_type2str[basetype]);
2020 ret = LY_EVALID;
2021 goto cleanup;
2022 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002023 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002024 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2025 tctx->tpdf->name, tctx->tpdf->dflt.str);
2026 ret = LY_EVALID;
2027 goto cleanup;
2028 }
2029
2030 (*type)->basetype = basetype;
Michal Vasko388a6632021-08-06 11:27:43 +02002031 (*type)->plugin = plugin;
2032
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002033 /* collect extensions */
2034 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko388a6632021-08-06 11:27:43 +02002035
2036 /* compile the new typedef */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002037 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01002038 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002039 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
2040 LY_CHECK_GOTO(ret, cleanup);
2041 base = prev_type;
2042 }
2043 /* remove the processed typedef contexts from the stack for circular check */
2044 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
2045
2046 /* process the type definition in leaf */
2047 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
2048 /* get restrictions from the node itself */
2049 (*type)->basetype = basetype;
Radek Krejcibf940f92021-03-24 21:04:13 +01002050 (*type)->plugin = base ? base->plugin : lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
Michal Vasko04338d92021-09-01 07:58:14 +02002051 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vaskoa99b3572021-02-01 11:54:58 +01002052 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002053 LY_CHECK_GOTO(ret, cleanup);
2054 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
2055 /* no specific restriction in leaf's type definition, copy from the base */
2056 free(*type);
2057 (*type) = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002058 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002059 }
2060
Radek Krejciab430862021-03-02 20:13:40 +01002061 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002062
2063cleanup:
2064 ly_set_erase(&tpdf_chain, free);
2065 return ret;
2066}
2067
2068/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002069 * @brief Check uniqness of the node/action/notification name.
2070 *
2071 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2072 * structures, but they share the namespace so we need to check their name collisions.
2073 *
2074 * @param[in] ctx Compile context.
2075 * @param[in] parent Parent of the nodes to check, can be NULL.
2076 * @param[in] name Name of the item to find in the given lists.
2077 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2078 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2079 */
2080static LY_ERR
2081lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2082 const struct lysc_node *exclude)
2083{
2084 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002085 const struct lysc_node_action *actions;
2086 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002087 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002088 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002089
2090#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2091
2092 if (exclude->nodetype == LYS_CASE) {
2093 /* check restricted only to all the cases */
2094 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002095 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002096 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002097 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002098 return LY_EEXIST;
2099 }
2100 }
2101
2102 return LY_SUCCESS;
2103 }
2104
2105 /* no reason for our parent to be choice anymore */
2106 assert(!parent || (parent->nodetype != LYS_CHOICE));
2107
2108 if (parent && (parent->nodetype == LYS_CASE)) {
2109 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002110
2111 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2112 iter = lysc_data_parent(parent);
2113 do {
2114 parent = parent->parent;
2115 if (parent && (parent->nodetype == LYS_CHOICE)) {
2116 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2117 }
2118 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002119 }
2120
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002121 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002122 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2123 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2124 if (exclude->flags & LYS_IS_OUTPUT) {
2125 getnext_flags |= LYS_GETNEXT_OUTPUT;
2126 parent = lysc_node_child(parent)->next;
2127 } else {
2128 parent = lysc_node_child(parent);
2129 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002130 }
2131
2132 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002133 if (!parent && ctx->ext) {
2134 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2135 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2136 goto error;
2137 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002138
Radek Krejci5fa32a32021-02-08 18:12:38 +01002139 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2140 if (iter->nodetype == LYS_CHOICE) {
2141 iter2 = NULL;
2142 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2143 if (CHECK_NODE(iter2, exclude, name)) {
2144 goto error;
2145 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002146 }
2147 }
2148 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002149 } else {
2150 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2151 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2152 goto error;
2153 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002154
Radek Krejci5fa32a32021-02-08 18:12:38 +01002155 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2156 if (iter->nodetype == LYS_CHOICE) {
2157 iter2 = NULL;
2158 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2159 if (CHECK_NODE(iter2, exclude, name)) {
2160 goto error;
2161 }
2162 }
2163 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002164 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002165
Radek Krejci5fa32a32021-02-08 18:12:38 +01002166 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2167 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2168 if (CHECK_NODE(iter, exclude, name)) {
2169 goto error;
2170 }
2171 }
2172
2173 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2174 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2175 if (CHECK_NODE(iter, exclude, name)) {
2176 goto error;
2177 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002178 }
2179 }
Radek Krejci078e4342021-02-12 18:17:26 +01002180 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002181 return LY_SUCCESS;
2182
2183error:
Radek Krejci078e4342021-02-12 18:17:26 +01002184 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002185 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002186 return LY_EEXIST;
2187
2188#undef CHECK_NODE
2189}
2190
Radek Krejci2a9fc652021-01-22 17:44:34 +01002191/**
2192 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2193 * keep specific order of augments targetting the same node.
2194 *
2195 * @param[in] ctx Compile context
2196 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2197 * the choice itself is expected instead of a specific case node.
2198 * @param[in] node Schema node to connect into the list.
2199 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2200 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2201 */
2202static LY_ERR
2203lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002204{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002205 struct lysc_node **children, *anchor = NULL;
2206 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002207
Radek Krejci2a9fc652021-01-22 17:44:34 +01002208 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002209
Radek Krejci2a9fc652021-01-22 17:44:34 +01002210 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002211 if (node->nodetype == LYS_INPUT) {
2212 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2213 /* input node is part of the action but link it with output */
2214 node->next = &((struct lysc_node_action *)parent)->output.node;
2215 node->prev = node->next;
2216 return LY_SUCCESS;
2217 } else if (node->nodetype == LYS_OUTPUT) {
2218 /* output node is part of the action but link it with input */
2219 node->next = NULL;
2220 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002221 return LY_SUCCESS;
2222 } else if (node->nodetype == LYS_ACTION) {
2223 children = (struct lysc_node **)lysc_node_actions_p(parent);
2224 } else if (node->nodetype == LYS_NOTIF) {
2225 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2226 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002227 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002228 }
2229 assert(children);
2230
2231 if (!(*children)) {
2232 /* first child */
2233 *children = node;
2234 } else if (node->flags & LYS_KEY) {
2235 /* special handling of adding keys */
2236 assert(node->module == parent->module);
2237 anchor = *children;
2238 if (anchor->flags & LYS_KEY) {
2239 while ((anchor->flags & LYS_KEY) && anchor->next) {
2240 anchor = anchor->next;
2241 }
2242 /* insert after the last key */
2243 insert_after = 1;
2244 } /* else insert before anchor (at the beginning) */
2245 } else if ((*children)->prev->module == node->module) {
2246 /* last child is from the same module, keep the order and insert at the end */
2247 anchor = (*children)->prev;
2248 insert_after = 1;
2249 } else if (parent->module == node->module) {
2250 /* adding module child after some augments were connected */
2251 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2252 } else {
2253 /* some augments are already connected and we are connecting new ones,
2254 * keep module name order and insert the node into the children list */
2255 anchor = *children;
2256 do {
2257 anchor = anchor->prev;
2258
2259 /* check that we have not found the last augment node from our module or
2260 * the first augment node from a "smaller" module or
2261 * the first node from a local module */
2262 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2263 (anchor->module == parent->module)) {
2264 /* insert after */
2265 insert_after = 1;
2266 break;
2267 }
2268
2269 /* we have traversed all the nodes, insert before anchor (as the first node) */
2270 } while (anchor->prev->next);
2271 }
2272
2273 /* insert */
2274 if (anchor) {
2275 if (insert_after) {
2276 node->next = anchor->next;
2277 node->prev = anchor;
2278 anchor->next = node;
2279 if (node->next) {
2280 /* middle node */
2281 node->next->prev = node;
2282 } else {
2283 /* last node */
2284 (*children)->prev = node;
2285 }
2286 } else {
2287 node->next = anchor;
2288 node->prev = anchor->prev;
2289 anchor->prev = node;
2290 if (anchor == *children) {
2291 /* first node */
2292 *children = node;
2293 } else {
2294 /* middle node */
2295 node->prev->next = node;
2296 }
2297 }
2298 }
2299
2300 /* check the name uniqueness (even for an only child, it may be in case) */
2301 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2302 return LY_EEXIST;
2303 }
2304 } else {
2305 /* top-level element */
2306 struct lysc_node **list;
2307
Radek Krejci6b88a462021-02-17 12:39:34 +01002308 if (ctx->ext) {
2309 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL);
2310 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002311 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2312 } else if (node->nodetype == LYS_NOTIF) {
2313 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2314 } else {
2315 list = &ctx->cur_mod->compiled->data;
2316 }
2317 if (!(*list)) {
2318 *list = node;
2319 } else {
2320 /* insert at the end of the module's top-level nodes list */
2321 (*list)->prev->next = node;
2322 node->prev = (*list)->prev;
2323 (*list)->prev = node;
2324 }
2325
2326 /* check the name uniqueness on top-level */
2327 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2328 return LY_EEXIST;
2329 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002330 }
2331
Radek Krejci2a9fc652021-01-22 17:44:34 +01002332 return LY_SUCCESS;
2333}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002334
Radek Krejci2a9fc652021-01-22 17:44:34 +01002335/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002336 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002337 *
2338 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002339 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002340 * @return LY_ERR value.
2341 */
2342static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002343lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002344{
2345 /* case never has any explicit config */
2346 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002347
Michal Vasko7c565922021-06-10 14:58:27 +02002348 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Radek Krejci91531e12021-02-08 19:57:54 +01002349 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002350 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002351 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2352 /* config not explicitly set, inherit it from parent */
Radek Krejci91531e12021-02-08 19:57:54 +01002353 if (node->parent) {
2354 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002355 } else {
2356 /* default is config true */
2357 node->flags |= LYS_CONFIG_W;
2358 }
2359 } else {
2360 /* config set explicitly */
2361 node->flags |= LYS_SET_CONFIG;
2362 }
2363
Radek Krejci91531e12021-02-08 19:57:54 +01002364 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002365 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002366 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002367 }
2368
Radek Krejci2a9fc652021-01-22 17:44:34 +01002369 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002370}
2371
Radek Krejci91531e12021-02-08 19:57:54 +01002372/**
2373 * @brief Set various flags of the compiled nodes
2374 *
2375 * @param[in] ctx Compile context.
2376 * @param[in] node Compiled node where the flags will be set.
2377 * @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).
2378 * Zero means no uses, non-zero value with no status bit set mean the default status.
2379 */
2380static LY_ERR
2381lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2382{
2383 /* inherit config flags */
2384 LY_CHECK_RET(lys_compile_config(ctx, node));
2385
2386 /* status - it is not inherited by specification, but it does not make sense to have
2387 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002388 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, node->name, uses_status ? uses_status :
2389 (node->parent ? node->parent->flags : 0), uses_status ? "<uses>" : (node->parent ? node->parent->name : NULL),
2390 !uses_status));
Radek Krejci91531e12021-02-08 19:57:54 +01002391
2392 /* other flags */
Michal Vasko7c565922021-06-10 14:58:27 +02002393 if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002394 node->flags |= LYS_IS_INPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002395 } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002396 node->flags |= LYS_IS_OUTPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002397 } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002398 node->flags |= LYS_IS_NOTIF;
2399 }
2400
2401 return LY_SUCCESS;
2402}
2403
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002404LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002405lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2406 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2407 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002408{
2409 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002410 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002411 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002412 struct lysp_when *pwhen = NULL;
Michal Vaskoe02e7402021-07-23 08:29:28 +02002413 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002414
Radek Krejci2a9fc652021-01-22 17:44:34 +01002415 node->nodetype = pnode->nodetype;
2416 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002417 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002418 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002419 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002420
Radek Krejci2a9fc652021-01-22 17:44:34 +01002421 /* compile any deviations for this node */
2422 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vaskoe02e7402021-07-23 08:29:28 +02002423 if (not_supported && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2424 /* if not supported, keep it just like disabled nodes by if-feature */
2425 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
2426 ctx->compile_opts |= LYS_COMPILE_DISABLED;
2427 }
2428 if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002429 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002430 }
2431
Radek Krejci2a9fc652021-01-22 17:44:34 +01002432 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vaskodfd254d2021-06-24 09:24:59 +02002433 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2434 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2435 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002436
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002437 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002438 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002439 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002440 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002441 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002442 }
2443
Radek Krejci91531e12021-02-08 19:57:54 +01002444 /* config, status and other flags */
2445 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002446 LY_CHECK_GOTO(ret, error);
2447
2448 /* list ordering */
2449 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002450 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002451 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002452 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002453 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002454 node->flags &= ~LYS_ORDBY_MASK;
2455 node->flags |= LYS_ORDBY_SYSTEM;
2456 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2457 /* default ordering is system */
2458 node->flags |= LYS_ORDBY_SYSTEM;
2459 }
2460 }
2461
Radek Krejci2a9fc652021-01-22 17:44:34 +01002462 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2463 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2464
Radek Krejci9a3823e2021-01-27 20:26:46 +01002465 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002466 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002467 ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002468 LY_CHECK_GOTO(ret, cleanup);
2469 }
2470
2471 /* connect any augments */
2472 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2473
2474 /* nodetype-specific part */
2475 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2476
2477 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002478 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002479 if (node->flags & LYS_MAND_TRUE) {
2480 /* inherit LYS_MAND_TRUE in parent containers */
2481 lys_compile_mandatory_parents(parent, 1);
2482 }
2483
2484 if (child_set) {
2485 /* add the new node into set */
2486 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2487 }
2488
2489 goto cleanup;
2490
2491error:
2492 lysc_node_free(ctx->ctx, node, 0);
2493cleanup:
2494 if (ret && dev_pnode) {
2495 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2496 }
Michal Vaskoe02e7402021-07-23 08:29:28 +02002497 ctx->compile_opts = prev_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002498 lysp_dev_node_free(ctx->ctx, dev_pnode);
2499 return ret;
2500}
2501
2502/**
2503 * @brief Compile parsed action's input/output node information.
2504 * @param[in] ctx Compile context
2505 * @param[in] pnode Parsed inout node.
2506 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2507 * is enriched with the inout-specific information.
2508 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2509 */
2510LY_ERR
2511lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2512{
2513 LY_ERR ret = LY_SUCCESS;
2514 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002515 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002516
2517 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2518 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2519
2520 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002521 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002522 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002523
Radek Krejci01180ac2021-01-27 08:48:22 +01002524 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002525 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2526 }
2527
Michal Vasko7c565922021-06-10 14:58:27 +02002528 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002529
2530done:
2531 return ret;
2532}
2533
2534/**
2535 * @brief Compile parsed action node information.
2536 * @param[in] ctx Compile context
2537 * @param[in] pnode Parsed action node.
2538 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2539 * is enriched with the action-specific information.
2540 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2541 */
2542LY_ERR
2543lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2544{
2545 LY_ERR ret;
2546 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2547 struct lysc_node_action *action = (struct lysc_node_action *)node;
2548 struct lysp_node_action_inout *input, implicit_input = {
2549 .nodetype = LYS_INPUT,
2550 .name = "input",
2551 .parent = pnode,
2552 };
2553 struct lysp_node_action_inout *output, implicit_output = {
2554 .nodetype = LYS_OUTPUT,
2555 .name = "output",
2556 .parent = pnode,
2557 };
2558
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002559 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002560 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002561 if (action_p->input.nodetype == LYS_UNKNOWN) {
2562 input = &implicit_input;
2563 } else {
2564 input = &action_p->input;
2565 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002566 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 +01002567 lysc_update_path(ctx, NULL, NULL);
2568 LY_CHECK_GOTO(ret, done);
2569
2570 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002571 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002572 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002573 output = &implicit_output;
2574 } else {
2575 output = &action_p->output;
2576 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002577 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 +01002578 lysc_update_path(ctx, NULL, NULL);
2579 LY_CHECK_GOTO(ret, done);
2580
2581 /* do not check "must" semantics in a grouping */
Michal Vasko6f08e962021-07-23 08:30:47 +02002582 if ((action->input.musts || action->output.musts) && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002583 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2584 LY_CHECK_GOTO(ret, done);
2585 }
2586
2587done:
2588 return ret;
2589}
2590
2591/**
2592 * @brief Compile parsed action node information.
2593 * @param[in] ctx Compile context
2594 * @param[in] pnode Parsed action node.
2595 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2596 * is enriched with the action-specific information.
2597 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2598 */
2599LY_ERR
2600lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2601{
2602 LY_ERR ret = LY_SUCCESS;
2603 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2604 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2605 struct lysp_node *child_p;
2606
2607 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02002608 if (notif_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002609 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002610 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002611 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002612 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002613
Radek Krejci01180ac2021-01-27 08:48:22 +01002614 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002615 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002616 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002617 }
2618
Radek Krejci2a9fc652021-01-22 17:44:34 +01002619done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002620 return ret;
2621}
2622
2623/**
2624 * @brief Compile parsed container node information.
2625 * @param[in] ctx Compile context
2626 * @param[in] pnode Parsed container node.
2627 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2628 * is enriched with the container-specific information.
2629 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2630 */
2631static LY_ERR
2632lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2633{
2634 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2635 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2636 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002637 LY_ERR ret = LY_SUCCESS;
2638
2639 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002640 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002641 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002642 }
2643
2644 /* more cases when the container has meaning but is kept NP for convenience:
2645 * - when condition
2646 * - direct child action/notification
2647 */
2648
2649 LY_LIST_FOR(cont_p->child, child_p) {
2650 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2651 LY_CHECK_GOTO(ret, done);
2652 }
2653
Michal Vasko5347e3a2020-11-03 17:14:57 +01002654 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02002655 if (cont_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002656 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002657 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002658 LY_CHECK_GOTO(ret, done);
2659 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002660
2661 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2662 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2663 LY_CHECK_GOTO(ret, done);
2664 }
2665 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2666 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2667 LY_CHECK_GOTO(ret, done);
2668 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002669
2670done:
2671 return ret;
2672}
2673
Michal Vasko72244882021-01-12 15:21:05 +01002674/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002675 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2676 * @param[in] ctx Compile context.
2677 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2678 * @param[in] type_p Parsed type to compile.
2679 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2680 * @return LY_ERR value.
2681 */
2682static LY_ERR
2683lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2684 struct lysc_node_leaf *leaf)
2685{
2686 struct lysp_qname *dflt;
aPiecekc6526b42021-07-12 15:21:39 +02002687 struct ly_set *leafrefs_set;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002688
Michal Vaskoa99b3572021-02-01 11:54:58 +01002689 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002690 leaf->units ? NULL : &leaf->units, &dflt));
2691
2692 /* store default value, if any */
2693 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2694 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2695 }
2696
aPiecekc6526b42021-07-12 15:21:39 +02002697 leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs;
2698 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002699 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
aPiecekc6526b42021-07-12 15:21:39 +02002700 LY_CHECK_RET(ly_set_add(leafrefs_set, leaf, 0, NULL));
2701 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002702 LY_ARRAY_COUNT_TYPE u;
2703 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2704 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2705 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
aPiecekc6526b42021-07-12 15:21:39 +02002706 LY_CHECK_RET(ly_set_add(leafrefs_set, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002707 }
2708 }
2709 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2710 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002711 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 +02002712 return LY_EVALID;
2713 }
2714 }
2715
2716 return LY_SUCCESS;
2717}
2718
2719/**
2720 * @brief Compile parsed leaf node information.
2721 * @param[in] ctx Compile context
2722 * @param[in] pnode Parsed leaf node.
2723 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2724 * is enriched with the leaf-specific information.
2725 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2726 */
2727static LY_ERR
2728lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2729{
2730 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2731 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002732 LY_ERR ret = LY_SUCCESS;
2733
Michal Vasko5347e3a2020-11-03 17:14:57 +01002734 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02002735 if (leaf_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002736 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002737 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002738 LY_CHECK_GOTO(ret, done);
2739 }
2740 if (leaf_p->units) {
2741 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2742 leaf->flags |= LYS_SET_UNITS;
2743 }
2744
2745 /* compile type */
2746 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2747 LY_CHECK_GOTO(ret, done);
2748
2749 /* store/update default value */
2750 if (leaf_p->dflt.str) {
2751 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2752 leaf->flags |= LYS_SET_DFLT;
2753 }
2754
2755 /* checks */
2756 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002757 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory leaf with a default value.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002758 return LY_EVALID;
2759 }
2760
2761done:
2762 return ret;
2763}
2764
2765/**
2766 * @brief Compile parsed leaf-list node information.
2767 * @param[in] ctx Compile context
2768 * @param[in] pnode Parsed leaf-list node.
2769 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2770 * is enriched with the leaf-list-specific information.
2771 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2772 */
2773static LY_ERR
2774lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2775{
2776 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2777 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002778 LY_ERR ret = LY_SUCCESS;
2779
Michal Vasko5347e3a2020-11-03 17:14:57 +01002780 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02002781 if (llist_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002782 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002783 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002784 LY_CHECK_GOTO(ret, done);
2785 }
2786 if (llist_p->units) {
2787 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2788 llist->flags |= LYS_SET_UNITS;
2789 }
2790
2791 /* compile type */
2792 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2793 LY_CHECK_GOTO(ret, done);
2794
2795 /* store/update default values */
2796 if (llist_p->dflts) {
2797 if (ctx->pmod->version < LYS_VERSION_1_1) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002798 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list default values are allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002799 return LY_EVALID;
2800 }
2801
2802 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2803 llist->flags |= LYS_SET_DFLT;
2804 }
2805
2806 llist->min = llist_p->min;
2807 if (llist->min) {
2808 llist->flags |= LYS_MAND_TRUE;
2809 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002810 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
2811
2812 if (llist->flags & LYS_CONFIG_R) {
2813 /* state leaf-list is always ordered-by user */
2814 llist->flags &= ~LYS_ORDBY_SYSTEM;
2815 llist->flags |= LYS_ORDBY_USER;
2816 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002817
2818 /* checks */
2819 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002820 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 +02002821 return LY_EVALID;
2822 }
2823
2824 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002825 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002826 llist->min, llist->max);
2827 return LY_EVALID;
2828 }
2829
2830done:
2831 return ret;
2832}
2833
2834LY_ERR
2835lysc_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 +02002836 const struct lys_module *cur_mod, LY_VALUE_FORMAT format, void *prefix_data, uint16_t nodetype,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002837 const struct lysc_node **target, uint16_t *result_flag)
2838{
2839 LY_ERR ret = LY_EVALID;
2840 const char *name, *prefix, *id;
2841 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002842 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002843 const char *nodeid_type;
2844 uint32_t getnext_extra_flag = 0;
2845 uint16_t current_nodetype = 0;
2846
2847 assert(nodeid);
2848 assert(target);
2849 assert(result_flag);
2850 *target = NULL;
2851 *result_flag = 0;
2852
2853 id = nodeid;
2854
2855 if (ctx_node) {
2856 /* descendant-schema-nodeid */
2857 nodeid_type = "descendant";
2858
2859 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002860 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002861 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002862 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002863 return LY_EVALID;
2864 }
2865 } else {
2866 /* absolute-schema-nodeid */
2867 nodeid_type = "absolute";
2868
2869 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002870 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002871 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002872 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002873 return LY_EVALID;
2874 }
2875 ++id;
2876 }
2877
2878 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2879 if (prefix) {
2880 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2881 if (!mod) {
2882 /* module must always be found */
2883 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002884 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002885 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002886 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002887 return LY_ENOTFOUND;
2888 }
2889 } else {
2890 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02002891 case LY_VALUE_SCHEMA:
2892 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002893 /* use the current module */
2894 mod = cur_mod;
2895 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02002896 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02002897 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002898 if (!ctx_node) {
2899 LOGINT_RET(ctx->ctx);
2900 }
2901 /* inherit the module of the previous context node */
2902 mod = ctx_node->module;
2903 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02002904 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02002905 case LY_VALUE_XML:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002906 /* not really defined */
2907 LOGINT_RET(ctx->ctx);
2908 }
2909 }
2910
2911 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2912 /* move through input/output manually */
2913 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002914 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2915 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002916 return LY_ENOTFOUND;
2917 }
2918 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002919 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002920 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002921 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002922 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2923 } else {
2924 /* only input or output is valid */
2925 ctx_node = NULL;
2926 }
2927 } else {
2928 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002929 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002930 getnext_extra_flag = 0;
2931 }
2932 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002933 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2934 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002935 return LY_ENOTFOUND;
2936 }
2937 current_nodetype = ctx_node->nodetype;
2938
2939 if (current_nodetype == LYS_NOTIF) {
2940 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2941 } else if (current_nodetype == LYS_INPUT) {
2942 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2943 } else if (current_nodetype == LYS_OUTPUT) {
2944 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2945 }
2946
2947 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2948 break;
2949 }
2950 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002951 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002952 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002953 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002954 return LY_EVALID;
2955 }
2956 ++id;
2957 }
2958
2959 if (ret == LY_SUCCESS) {
2960 *target = ctx_node;
2961 if (nodetype && !(current_nodetype & nodetype)) {
2962 return LY_EDENIED;
2963 }
2964 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002965 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002966 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002967 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002968 }
2969
2970 return ret;
2971}
2972
2973/**
2974 * @brief Compile information about list's uniques.
2975 * @param[in] ctx Compile context.
2976 * @param[in] uniques Sized array list of unique statements.
2977 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2978 * @return LY_ERR value.
2979 */
2980static LY_ERR
2981lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2982{
2983 LY_ERR ret = LY_SUCCESS;
2984 struct lysc_node_leaf **key, ***unique;
2985 struct lysc_node *parent;
2986 const char *keystr, *delim;
2987 size_t len;
2988 LY_ARRAY_COUNT_TYPE v;
2989 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2990 uint16_t flags;
2991
2992 LY_ARRAY_FOR(uniques, v) {
2993 config = -1;
2994 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2995 keystr = uniques[v].str;
2996 while (keystr) {
2997 delim = strpbrk(keystr, " \t\n");
2998 if (delim) {
2999 len = delim - keystr;
3000 while (isspace(*delim)) {
3001 ++delim;
3002 }
3003 } else {
3004 len = strlen(keystr);
3005 }
3006
3007 /* unique node must be present */
3008 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003009 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02003010 LY_VALUE_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003011 if (ret != LY_SUCCESS) {
3012 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003013 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003014 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01003015 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003016 }
3017 return LY_EVALID;
3018 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003019 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003020 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003021 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003022 return LY_EVALID;
3023 }
3024
3025 /* all referenced leafs must be of the same config type */
3026 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3027 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003028 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003029 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3030 return LY_EVALID;
3031 } else if ((*key)->flags & LYS_CONFIG_W) {
3032 config = 1;
3033 } else { /* LYS_CONFIG_R */
3034 config = 0;
3035 }
3036
3037 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3038 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3039 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003040 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003041 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3042 return LY_EVALID;
3043 }
3044 }
3045
3046 /* check status */
3047 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3048 (*key)->flags, (*key)->module, (*key)->name));
3049
3050 /* mark leaf as unique */
3051 (*key)->flags |= LYS_UNIQUE;
3052
3053 /* next unique value in line */
3054 keystr = delim;
3055 }
3056 /* next unique definition */
3057 }
3058
3059 return LY_SUCCESS;
3060}
3061
3062/**
3063 * @brief Compile parsed list node information.
3064 * @param[in] ctx Compile context
3065 * @param[in] pnode Parsed list node.
3066 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3067 * is enriched with the list-specific information.
3068 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3069 */
3070static LY_ERR
3071lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3072{
3073 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3074 struct lysc_node_list *list = (struct lysc_node_list *)node;
3075 struct lysp_node *child_p;
3076 struct lysc_node_leaf *key, *prev_key = NULL;
3077 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003078 const char *keystr, *delim;
3079 LY_ERR ret = LY_SUCCESS;
3080
3081 list->min = list_p->min;
3082 if (list->min) {
3083 list->flags |= LYS_MAND_TRUE;
3084 }
3085 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3086
3087 LY_LIST_FOR(list_p->child, child_p) {
3088 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3089 }
3090
Michal Vasko5347e3a2020-11-03 17:14:57 +01003091 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02003092 if (list_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003093 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003094 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003095 }
3096
3097 /* keys */
3098 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003099 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003100 return LY_EVALID;
3101 }
3102
3103 /* find all the keys (must be direct children) */
3104 keystr = list_p->key;
3105 if (!keystr) {
3106 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003107 list->flags &= ~LYS_ORDBY_SYSTEM;
3108 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003109 }
3110 while (keystr) {
3111 delim = strpbrk(keystr, " \t\n");
3112 if (delim) {
3113 len = delim - keystr;
3114 while (isspace(*delim)) {
3115 ++delim;
3116 }
3117 } else {
3118 len = strlen(keystr);
3119 }
3120
3121 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003122 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3123 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003124 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003125 return LY_EVALID;
3126 }
3127 /* keys must be unique */
3128 if (key->flags & LYS_KEY) {
3129 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003130 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003131 return LY_EVALID;
3132 }
3133
Radek Krejcia6016992021-03-03 10:13:41 +01003134 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003135 /* key must have the same config flag as the list itself */
3136 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003137 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of a configuration list must not be a state leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003138 return LY_EVALID;
3139 }
3140 if (ctx->pmod->version < LYS_VERSION_1_1) {
3141 /* YANG 1.0 denies key to be of empty type */
3142 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003143 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003144 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3145 return LY_EVALID;
3146 }
3147 } else {
3148 /* when and if-feature are illegal on list keys */
3149 if (key->when) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003150 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List's key must not have any \"when\" statement.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003151 return LY_EVALID;
3152 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003153 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003154 }
3155
3156 /* check status */
3157 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3158 key->flags, key->module, key->name));
3159
3160 /* ignore default values of the key */
3161 if (key->dflt) {
3162 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3163 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3164 free(key->dflt);
3165 key->dflt = NULL;
3166 }
3167 /* mark leaf as key */
3168 key->flags |= LYS_KEY;
3169
3170 /* move it to the correct position */
3171 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3172 /* fix links in closest previous siblings of the key */
3173 if (key->next) {
3174 key->next->prev = key->prev;
3175 } else {
3176 /* last child */
3177 list->child->prev = key->prev;
3178 }
3179 if (key->prev->next) {
3180 key->prev->next = key->next;
3181 }
3182 /* fix links in the key */
3183 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003184 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003185 key->next = prev_key->next;
3186 } else {
3187 key->prev = list->child->prev;
3188 key->next = list->child;
3189 }
3190 /* fix links in closes future siblings of the key */
3191 if (prev_key) {
3192 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003193 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003194 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003195 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003196 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003197 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003198 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003199 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003200 }
3201 /* fix links in parent */
3202 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003203 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003204 }
3205 }
3206
3207 /* next key value */
3208 prev_key = key;
3209 keystr = delim;
3210 lysc_update_path(ctx, NULL, NULL);
3211 }
3212
3213 /* uniques */
3214 if (list_p->uniques) {
3215 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3216 }
3217
Radek Krejci2a9fc652021-01-22 17:44:34 +01003218 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3219 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3220 LY_CHECK_GOTO(ret, done);
3221 }
3222 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3223 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3224 LY_CHECK_GOTO(ret, done);
3225 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003226
3227 /* checks */
3228 if (list->min > list->max) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003229 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.", list->min, list->max);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003230 return LY_EVALID;
3231 }
3232
3233done:
3234 return ret;
3235}
3236
3237/**
3238 * @brief Do some checks and set the default choice's case.
3239 *
3240 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3241 *
3242 * @param[in] ctx Compile context.
3243 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3244 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3245 * @return LY_ERR value.
3246 */
3247static LY_ERR
3248lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3249{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003250 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003251 const struct lys_module *mod;
3252 const char *prefix = NULL, *name;
3253 size_t prefix_len = 0;
3254
3255 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3256 name = strchr(dflt->str, ':');
3257 if (name) {
3258 prefix = dflt->str;
3259 prefix_len = name - prefix;
3260 ++name;
3261 } else {
3262 name = dflt->str;
3263 }
3264 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003265 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003266 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003267 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003268 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003269 return LY_EVALID;
3270 }
3271 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003272 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003273 }
3274
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003275 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 +02003276 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003277 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003278 "Default case \"%s\" not found.", dflt->str);
3279 return LY_EVALID;
3280 }
3281
3282 /* no mandatory nodes directly under the default case */
3283 LY_LIST_FOR(ch->dflt->child, iter) {
3284 if (iter->parent != (struct lysc_node *)ch->dflt) {
3285 break;
3286 }
3287 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003288 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003289 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3290 return LY_EVALID;
3291 }
3292 }
3293
3294 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003295 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003296 return LY_EVALID;
3297 }
3298
3299 ch->dflt->flags |= LYS_SET_DFLT;
3300 return LY_SUCCESS;
3301}
3302
3303LY_ERR
3304lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3305 struct ly_set *child_set)
3306{
3307 LY_ERR ret = LY_SUCCESS;
3308 struct lysp_node *child_p_next = child_p->next;
3309 struct lysp_node_case *cs_p;
aPiecekaa320c92021-05-21 07:34:24 +02003310 struct lysc_node_case *compiled_case;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003311
3312 if (child_p->nodetype == LYS_CASE) {
3313 /* standard case under choice */
3314 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3315 } else {
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003316 /* we need the implicit case first, so create a fake parsed (shorthand) case */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003317 cs_p = calloc(1, sizeof *cs_p);
3318 cs_p->nodetype = LYS_CASE;
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003319 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003320 cs_p->child = child_p;
3321
3322 /* make the child the only case child */
3323 child_p->next = NULL;
3324
3325 /* compile it normally */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003326 LY_CHECK_GOTO(ret = lys_compile_node(ctx, (struct lysp_node *)cs_p, node, 0, child_set), revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003327
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003328 if (((struct lysc_node_choice *)node)->cases) {
3329 /* get last case node */
3330 compiled_case = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases->prev;
aPiecek082c7dc2021-05-20 08:55:07 +02003331
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003332 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
3333 /* Compiled case node cannot point to his corresponding parsed node
aPiecek8e351382021-06-25 14:13:55 +02003334 * because it exists temporarily. Therefore, it must be set to NULL.
3335 */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003336 compiled_case->priv = NULL;
3337 }
aPiecekaa320c92021-05-21 07:34:24 +02003338
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003339 /* The status is copied from his child and not from his parent as usual. */
3340 if (compiled_case->child) {
3341 compiled_case->flags &= ~LYS_STATUS_MASK;
3342 compiled_case->flags |= LYS_STATUS_MASK & compiled_case->child->flags;
3343 }
3344 } /* else it was removed by a deviation */
aPiecekaa320c92021-05-21 07:34:24 +02003345
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003346revert_sh_case:
3347 /* free the parsed shorthand case and correct pointers back */
aPiecekaa320c92021-05-21 07:34:24 +02003348 cs_p->child = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003349 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3350 child_p->next = child_p_next;
3351 }
3352
3353 return ret;
3354}
3355
3356/**
3357 * @brief Compile parsed choice node information.
3358 *
3359 * @param[in] ctx Compile context
3360 * @param[in] pnode Parsed choice node.
3361 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3362 * is enriched with the choice-specific information.
3363 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3364 */
3365static LY_ERR
3366lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3367{
3368 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3369 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3370 struct lysp_node *child_p;
3371 LY_ERR ret = LY_SUCCESS;
3372
3373 assert(node->nodetype == LYS_CHOICE);
3374
3375 LY_LIST_FOR(ch_p->child, child_p) {
3376 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3377 }
3378
3379 /* default branch */
3380 if (ch_p->dflt.str) {
3381 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3382 }
3383
3384 return ret;
3385}
3386
3387/**
3388 * @brief Compile parsed anydata or anyxml node information.
3389 * @param[in] ctx Compile context
3390 * @param[in] pnode Parsed anydata or anyxml node.
3391 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3392 * is enriched with the any-specific information.
3393 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3394 */
3395static LY_ERR
3396lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3397{
3398 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3399 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003400 LY_ERR ret = LY_SUCCESS;
3401
Michal Vasko5347e3a2020-11-03 17:14:57 +01003402 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02003403 if (any_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003404 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003405 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003406 LY_CHECK_GOTO(ret, done);
3407 }
3408
Radek Krejci91531e12021-02-08 19:57:54 +01003409 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003410 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003411 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3412 }
3413done:
3414 return ret;
3415}
3416
3417/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003418 * @brief Prepare the case structure in choice node for the new data node.
3419 *
3420 * 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
3421 * created in the choice when the first child was processed.
3422 *
3423 * @param[in] ctx Compile context.
3424 * @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,
3425 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3426 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3427 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3428 * @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,
3429 * it is linked from the case structure only in case it is its first child.
3430 */
3431static LY_ERR
3432lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3433{
3434 struct lysp_node *child_p;
3435 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3436
3437 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3438 /* we have to add an implicit case node into the parent choice */
3439 } else if (pnode->nodetype == LYS_CASE) {
3440 /* explicit parent case */
3441 LY_LIST_FOR(cs_p->child, child_p) {
3442 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3443 }
3444 } else {
3445 LOGINT_RET(ctx->ctx);
3446 }
3447
3448 return LY_SUCCESS;
3449}
3450
3451void
3452lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3453{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003454 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003455
3456 if (add) { /* set flag */
3457 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3458 parent = parent->parent) {
3459 parent->flags |= LYS_MAND_TRUE;
3460 }
3461 } else { /* unset flag */
3462 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003463 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003464 if (iter->flags & LYS_MAND_TRUE) {
3465 /* there is another mandatory node */
3466 return;
3467 }
3468 }
3469 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3470 parent->flags &= ~LYS_MAND_TRUE;
3471 }
3472 }
3473}
3474
3475/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003476 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003477 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003478 * @param[in] name Name of the grouping to find,
3479 * @return NULL when there is no grouping with the specified name
3480 * @return Pointer to the grouping of the specified @p name.
3481 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003482static struct lysp_node_grp *
3483match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003484{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003485 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003486
Radek Krejci2a9fc652021-01-22 17:44:34 +01003487 LY_LIST_FOR(grps, grp) {
3488 if (!strcmp(grp->name, name)) {
3489 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003490 }
3491 }
3492
3493 return NULL;
3494}
3495
3496/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003497 * @brief Find grouping for a uses.
3498 *
3499 * @param[in] ctx Compile context.
3500 * @param[in] uses_p Parsed uses node.
3501 * @param[out] gpr_p Found grouping on success.
3502 * @param[out] grp_pmod Module of @p grp_p on success.
3503 * @return LY_ERR value.
3504 */
3505static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003506lys_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 +02003507 struct lysp_module **grp_pmod)
3508{
3509 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003510 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003511 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003512 const char *id, *name, *prefix, *local_pref;
3513 size_t prefix_len, name_len;
3514 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003515 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003516
3517 *grp_p = NULL;
3518 *grp_pmod = NULL;
3519
3520 /* search for the grouping definition */
3521 id = uses_p->name;
3522 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3523 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3524 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3525 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003526 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003527 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003528 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3529 found = ctx->pmod;
3530 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003531 }
3532 }
3533 } else {
3534 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003535 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003536 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003537 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003538 "Invalid prefix used for grouping reference.", uses_p->name);
3539 return LY_EVALID;
3540 }
3541 pmod = mod->parsed;
3542 }
3543
3544 if (!found) {
3545 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003546 if ((grp = match_grouping(pmod->groupings, name))) {
3547 found = pmod;
3548 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003549 /* ... and all the submodules */
3550 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003551 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3552 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003553 break;
3554 }
3555 }
3556 }
3557 }
3558 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003559 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003560 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3561 return LY_EVALID;
3562 }
3563
Michal Vasko7c565922021-06-10 14:58:27 +02003564 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003565 /* remember that the grouping is instantiated to avoid its standalone validation */
3566 grp->flags |= LYS_USED_GRP;
3567 }
3568
3569 *grp_p = grp;
3570 *grp_pmod = found;
3571 return LY_SUCCESS;
3572}
3573
3574/**
Michal Vaskodfd254d2021-06-24 09:24:59 +02003575 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
3576 */
3577#define LYS_STATUS_USES LYS_CONFIG_MASK
3578
3579/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003580 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3581 * If present, also apply uses's modificators.
3582 *
3583 * @param[in] ctx Compile context
3584 * @param[in] uses_p Parsed uses schema node.
3585 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3586 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3587 * the compile context.
3588 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3589 */
3590static LY_ERR
3591lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3592{
3593 struct lysp_node *pnode;
3594 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003595 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003596 uint32_t i, grp_stack_count;
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003597 uint16_t uses_flags;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003598 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3599 LY_ERR ret = LY_SUCCESS;
3600 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003601 struct ly_set uses_child_set = {0};
3602
3603 /* find the referenced grouping */
3604 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3605
3606 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3607 grp_stack_count = ctx->groupings.count;
3608 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3609 if (grp_stack_count == ctx->groupings.count) {
3610 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003611 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003612 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3613 return LY_EVALID;
3614 }
3615
3616 /* check status */
3617 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3618 LY_CHECK_GOTO(ret, cleanup);
3619
3620 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3621 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3622 LY_CHECK_GOTO(ret, cleanup);
3623
3624 /* switch context's parsed module being processed */
3625 ctx->pmod = grp_mod;
3626
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003627 /* compile special uses status flags */
3628 uses_flags = uses_p->flags;
3629 ret = lys_compile_status(ctx, &uses_flags, "<uses>", parent ? parent->flags : 0, parent ? parent->name : NULL, 0);
3630 LY_CHECK_GOTO(ret, cleanup);
3631 uses_flags |= LYS_STATUS_USES;
3632
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003633 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003634 LY_LIST_FOR(grp->child, pnode) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003635 ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003636 LY_CHECK_GOTO(ret, cleanup);
3637 }
3638
3639 if (child_set) {
3640 /* add these children to our compiled child_set as well since uses is a schema-only node */
3641 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3642 }
3643
3644 if (uses_p->when) {
3645 /* pass uses's when to all the data children */
3646 for (i = 0; i < uses_child_set.count; ++i) {
3647 child = uses_child_set.snodes[i];
3648
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003649 ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003650 LY_CHECK_GOTO(ret, cleanup);
3651 }
3652 }
3653
3654 /* compile actions */
3655 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003656 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003657 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3658 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003659 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003660 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3661 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003662 ret = LY_EVALID;
3663 goto cleanup;
3664 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003665 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003666 ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003667 LY_CHECK_GOTO(ret, cleanup);
3668 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003669
3670 if (uses_p->when) {
3671 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003672 LY_LIST_FOR((struct lysc_node *)*actions, child) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003673 ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003674 LY_CHECK_GOTO(ret, cleanup);
3675 }
3676 }
3677 }
3678
3679 /* compile notifications */
3680 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003681 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003682 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3683 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003684 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003685 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3686 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003687 ret = LY_EVALID;
3688 goto cleanup;
3689 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003690
3691 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003692 ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003693 LY_CHECK_GOTO(ret, cleanup);
3694 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003695
3696 if (uses_p->when) {
3697 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003698 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003699 ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003700 LY_CHECK_GOTO(ret, cleanup);
3701 }
3702 }
3703 }
3704
3705 /* check that all augments were applied */
3706 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003707 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3708 /* augment of some parent uses, irrelevant now */
3709 continue;
3710 }
3711
3712 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003713 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3714 ret = LY_ENOTFOUND;
3715 }
3716 LY_CHECK_GOTO(ret, cleanup);
3717
3718 /* check that all refines were applied */
3719 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003720 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3721 /* refine of some paretn uses, irrelevant now */
3722 continue;
3723 }
3724
3725 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003726 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3727 ret = LY_ENOTFOUND;
3728 }
3729 LY_CHECK_GOTO(ret, cleanup);
3730
3731cleanup:
3732 /* reload previous context's parsed module being processed */
3733 ctx->pmod = mod_old;
3734
3735 /* remove the grouping from the stack for circular groupings dependency check */
3736 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3737 assert(ctx->groupings.count == grp_stack_count);
3738
3739 ly_set_erase(&uses_child_set, NULL);
3740 return ret;
3741}
3742
3743static int
3744lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3745{
3746 struct lysp_node *iter;
3747 int len = 0;
3748
3749 *path = NULL;
3750 for (iter = node; iter && len >= 0; iter = iter->parent) {
3751 char *s = *path;
3752 char *id;
3753
3754 switch (iter->nodetype) {
3755 case LYS_USES:
3756 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3757 break;
3758 case LYS_GROUPING:
3759 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3760 break;
3761 case LYS_AUGMENT:
3762 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3763 break;
3764 default:
3765 id = strdup(iter->name);
3766 break;
3767 }
3768
3769 if (!iter->parent) {
3770 /* print prefix */
3771 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3772 } else {
3773 /* prefix is the same as in parent */
3774 len = asprintf(path, "/%s%s", id, s ? s : "");
3775 }
3776 free(s);
3777 free(id);
3778 }
3779
3780 if (len < 0) {
3781 free(*path);
3782 *path = NULL;
3783 } else if (len == 0) {
3784 *path = strdup("/");
3785 len = 1;
3786 }
3787 return len;
3788}
3789
3790LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003791lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003792{
3793 LY_ERR ret;
3794 char *path;
3795 int len;
3796
3797 struct lysp_node_uses fake_uses = {
3798 .parent = pnode,
3799 .nodetype = LYS_USES,
3800 .flags = 0, .next = NULL,
3801 .name = grp->name,
3802 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3803 .refines = NULL, .augments = NULL
3804 };
3805 struct lysc_node_container fake_container = {
3806 .nodetype = LYS_CONTAINER,
3807 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3808 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003809 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003810 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003811 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003812 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003813 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3814 };
3815
3816 if (grp->parent) {
3817 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3818 }
3819
3820 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3821 if (len < 0) {
3822 LOGMEM(ctx->ctx);
3823 return LY_EMEM;
3824 }
3825 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3826 ctx->path_len = (uint32_t)len;
3827 free(path);
3828
3829 lysc_update_path(ctx, NULL, "{grouping}");
3830 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003831 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003832 lysc_update_path(ctx, NULL, NULL);
3833 lysc_update_path(ctx, NULL, NULL);
3834
3835 ctx->path_len = 1;
3836 ctx->path[1] = '\0';
3837
3838 /* cleanup */
3839 lysc_node_container_free(ctx->ctx, &fake_container);
3840
3841 return ret;
3842}
3843
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003844LY_ERR
3845lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3846 struct ly_set *child_set)
3847{
3848 LY_ERR ret = LY_SUCCESS;
3849 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02003850 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003851
3852 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3853
3854 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003855 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003856 } else {
3857 lysc_update_path(ctx, NULL, "{uses}");
3858 lysc_update_path(ctx, NULL, pnode->name);
3859 }
3860
3861 switch (pnode->nodetype) {
3862 case LYS_CONTAINER:
3863 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3864 node_compile_spec = lys_compile_node_container;
3865 break;
3866 case LYS_LEAF:
3867 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3868 node_compile_spec = lys_compile_node_leaf;
3869 break;
3870 case LYS_LIST:
3871 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3872 node_compile_spec = lys_compile_node_list;
3873 break;
3874 case LYS_LEAFLIST:
3875 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3876 node_compile_spec = lys_compile_node_leaflist;
3877 break;
3878 case LYS_CHOICE:
3879 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3880 node_compile_spec = lys_compile_node_choice;
3881 break;
3882 case LYS_CASE:
3883 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3884 node_compile_spec = lys_compile_node_case;
3885 break;
3886 case LYS_ANYXML:
3887 case LYS_ANYDATA:
3888 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3889 node_compile_spec = lys_compile_node_any;
3890 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003891 case LYS_RPC:
3892 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02003893 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003894 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3895 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02003896 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003897 return LY_EVALID;
3898 }
3899 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3900 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02003901 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003902 break;
3903 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02003904 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003905 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3906 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02003907 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003908 return LY_EVALID;
3909 }
3910 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3911 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02003912 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003913 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003914 case LYS_USES:
3915 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3916 lysc_update_path(ctx, NULL, NULL);
3917 lysc_update_path(ctx, NULL, NULL);
3918 return ret;
3919 default:
3920 LOGINT(ctx->ctx);
3921 return LY_EINT;
3922 }
3923 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3924
Radek Krejci2a9fc652021-01-22 17:44:34 +01003925 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003926
Michal Vasko7c565922021-06-10 14:58:27 +02003927 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003928 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003929 return ret;
3930}