blob: 881759eed8444422f7d730eca216bc44bca03e0d [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/**
Michal Vaskoc130e162021-10-19 11:30:00 +020056 * @brief Add a node with must(s) to unres.
57 *
58 * @param[in] ctx Compile context.
59 * @param[in] node Compiled node with must(s).
60 * @param[in] pnode Parsed ndoe with must(s).
61 * @return LY_ERR value.
62 */
63static LY_ERR
64lysc_unres_must_add(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_node *pnode)
65{
66 struct lysc_unres_must *m = NULL;
67 LY_ARRAY_COUNT_TYPE u;
68 struct lysc_must *musts;
69 struct lysp_restr *pmusts;
70 LY_ERR ret;
71
72 /* do not check must(s) in a grouping */
73 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
74 return LY_SUCCESS;
75 }
76
77 musts = lysc_node_musts(node);
78 pmusts = lysp_node_musts(pnode);
79 assert(LY_ARRAY_COUNT(musts) == LY_ARRAY_COUNT(pmusts));
80
81 if (!musts) {
82 /* no must */
83 return LY_SUCCESS;
84 }
85
86 /* add new unres must */
87 m = calloc(1, sizeof *m);
88 LY_CHECK_ERR_GOTO(!m, ret = LY_EMEM, error);
89 m->node = node;
90
91 /* add must local modules */
92 LY_ARRAY_CREATE_GOTO(ctx->ctx, m->local_mods, LY_ARRAY_COUNT(pmusts), ret, error);
93 LY_ARRAY_FOR(pmusts, u) {
94 m->local_mods[u] = pmusts[u].arg.mod;
95 LY_ARRAY_INCREMENT(m->local_mods);
96 }
97
98 LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->musts, m, 1, NULL), ret = LY_EMEM, error);
99
100 return LY_SUCCESS;
101
102error:
103 if (m) {
104 LY_ARRAY_FREE(m->local_mods);
105 free(m);
106 }
107 LOGMEM(ctx->ctx);
108 return ret;
109}
110
111static LY_ERR
112lysc_unres_leafref_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const struct lysp_module *local_mod)
113{
114 struct lysc_unres_leafref *l = NULL;
115 struct ly_set *leafrefs_set;
116 LY_ARRAY_COUNT_TYPE u;
117 int is_lref = 0;
118
119 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
120 /* do not check leafrefs in groupings */
121 return LY_SUCCESS;
122 }
123
124 /* use special set for disabled leafrefs */
125 leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs;
126
127 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
128 /* leafref */
129 is_lref = 1;
130 } else if (leaf->type->basetype == LY_TYPE_UNION) {
131 /* union with leafrefs */
132 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
133 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
134 is_lref = 1;
135 break;
136 }
137 }
138 }
139
140 if (is_lref) {
141 /* add new unresolved leafref node */
142 l = calloc(1, sizeof *l);
143 LY_CHECK_ERR_RET(!l, LOGMEM(ctx->ctx), LY_EMEM);
144
145 l->node = &leaf->node;
146 l->local_mod = local_mod;
147
148 LY_CHECK_ERR_RET(ly_set_add(leafrefs_set, l, 1, NULL), free(l); LOGMEM(ctx->ctx), LY_EMEM);
149 }
150
151 return LY_SUCCESS;
152}
153
154/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200155 * @brief Add/replace a leaf default value in unres.
156 * Can also be used for a single leaf-list default value.
157 *
158 * @param[in] ctx Compile context.
159 * @param[in] leaf Leaf with the default value.
160 * @param[in] dflt Default value to use.
161 * @return LY_ERR value.
162 */
163static LY_ERR
164lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
165{
166 struct lysc_unres_dflt *r = NULL;
167 uint32_t i;
168
Michal Vasko7c565922021-06-10 14:58:27 +0200169 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100170 return LY_SUCCESS;
171 }
172
Michal Vasko405cc9e2020-12-01 12:01:27 +0100173 for (i = 0; i < ctx->unres->dflts.count; ++i) {
174 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200175 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100176 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200177 lysp_qname_free(ctx->ctx, r->dflt);
178 free(r->dflt);
179 break;
180 }
181 }
182 if (!r) {
183 /* add new unres item */
184 r = calloc(1, sizeof *r);
185 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
186 r->leaf = leaf;
187
Michal Vasko405cc9e2020-12-01 12:01:27 +0100188 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200189 }
190
191 r->dflt = malloc(sizeof *r->dflt);
192 LY_CHECK_GOTO(!r->dflt, error);
193 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
194
195 return LY_SUCCESS;
196
197error:
198 free(r->dflt);
199 LOGMEM(ctx->ctx);
200 return LY_EMEM;
201}
202
203/**
204 * @brief Add/replace a leaf-list default value(s) in unres.
205 *
206 * @param[in] ctx Compile context.
207 * @param[in] llist Leaf-list with the default value.
208 * @param[in] dflts Sized array of the default values.
209 * @return LY_ERR value.
210 */
211static LY_ERR
212lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
213{
214 struct lysc_unres_dflt *r = NULL;
215 uint32_t i;
216
Michal Vasko7c565922021-06-10 14:58:27 +0200217 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100218 return LY_SUCCESS;
219 }
220
Michal Vasko405cc9e2020-12-01 12:01:27 +0100221 for (i = 0; i < ctx->unres->dflts.count; ++i) {
222 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200223 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100224 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200225 lysp_qname_free(ctx->ctx, r->dflt);
226 free(r->dflt);
227 r->dflt = NULL;
228 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
229 r->dflts = NULL;
230 break;
231 }
232 }
233 if (!r) {
234 r = calloc(1, sizeof *r);
235 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
236 r->llist = llist;
237
Michal Vasko405cc9e2020-12-01 12:01:27 +0100238 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200239 }
240
241 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
242
243 return LY_SUCCESS;
244}
245
246/**
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100247 * @brief Add a bits/enumeration type to unres.
248 *
249 * @param[in] ctx Compile context.
250 * @param[in] leaf Leaf of type bits/enumeration whose disabled items to free.
251 * @return LY_ERR value.
252 */
253static LY_ERR
254lysc_unres_bitenum_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf)
255{
256 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
257 /* skip groupings and redundant for disabled nodes */
258 return LY_SUCCESS;
259 }
260
261 LY_CHECK_RET(ly_set_add(&ctx->unres->disabled_bitenums, leaf, 1, NULL));
262
263 return LY_SUCCESS;
264}
265
266/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200267 * @brief Duplicate the compiled pattern structure.
268 *
269 * Instead of duplicating memory, the reference counter in the @p orig is increased.
270 *
271 * @param[in] orig The pattern structure to duplicate.
272 * @return The duplicated structure to use.
273 */
274static struct lysc_pattern *
275lysc_pattern_dup(struct lysc_pattern *orig)
276{
277 ++orig->refcount;
278 return orig;
279}
280
281/**
282 * @brief Duplicate the array of compiled patterns.
283 *
284 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
285 *
286 * @param[in] ctx Libyang context for logging.
287 * @param[in] orig The patterns sized array to duplicate.
288 * @return New sized array as a copy of @p orig.
289 * @return NULL in case of memory allocation error.
290 */
291static struct lysc_pattern **
292lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
293{
294 struct lysc_pattern **dup = NULL;
295 LY_ARRAY_COUNT_TYPE u;
296
297 assert(orig);
298
299 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
300 LY_ARRAY_FOR(orig, u) {
301 dup[u] = lysc_pattern_dup(orig[u]);
302 LY_ARRAY_INCREMENT(dup);
303 }
304 return dup;
305}
306
307/**
308 * @brief Duplicate compiled range structure.
309 *
310 * @param[in] ctx Libyang context for logging.
311 * @param[in] orig The range structure to be duplicated.
312 * @return New compiled range structure as a copy of @p orig.
313 * @return NULL in case of memory allocation error.
314 */
315static struct lysc_range *
316lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
317{
318 struct lysc_range *dup;
319 LY_ERR ret;
320
321 assert(orig);
322
323 dup = calloc(1, sizeof *dup);
324 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
325 if (orig->parts) {
326 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100327 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200328 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
329 }
330 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
331 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
332 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
333
334 return dup;
335cleanup:
336 free(dup);
337 (void) ret; /* set but not used due to the return type */
338 return NULL;
339}
340
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200341/**
Michal Vaskodfd254d2021-06-24 09:24:59 +0200342 * @brief Compile status information of the given statement.
343 *
344 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
345 * has the status correctly set during the compilation.
346 *
347 * @param[in] ctx Compile context
348 * @param[in,out] stmt_flags Compiled flags to update. If the status was set explicitly, it is already set
349 * in the flags value and we just check the compatibility with the parent's status value.
350 * @param[in] stmt_name Statement name, for logging.
351 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
352 * @param[in] parent_name Name of the parent node, for logging.
353 * @param[in] inherit_log Whether to print inherit message.
354 * @return LY_ERR value.
355 */
356static LY_ERR
357lys_compile_status(struct lysc_ctx *ctx, uint16_t *stmt_flags, const char *stmt_name, uint16_t parent_flags,
358 const char *parent_name, ly_bool inherit_log)
359{
360 /* status - it is not inherited by specification, but it does not make sense to have
361 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
362 if (!(*stmt_flags & LYS_STATUS_MASK)) {
363 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
364 if (inherit_log) {
365 LOGVRB("Missing explicit \"%s\" status specified in parent \"%s\", inheriting for \"%s\".",
366 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name, stmt_name);
367 }
368 *stmt_flags |= parent_flags & LYS_STATUS_MASK;
369 } else {
370 *stmt_flags |= LYS_STATUS_CURR;
371 }
372 } else if (parent_flags & LYS_STATUS_MASK) {
373 /* check status compatibility with the parent */
374 if ((parent_flags & LYS_STATUS_MASK) > (*stmt_flags & LYS_STATUS_MASK)) {
375 if (*stmt_flags & LYS_STATUS_CURR) {
376 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
377 "Status \"current\" of \"%s\" is in conflict with the \"%s\" status of parent \"%s\".",
378 stmt_name, (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name);
379 } else { /* LYS_STATUS_DEPRC */
380 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
381 "Status \"deprecated\" of \"%s\" is in conflict with the \"obsolete\" status of parent \"%s\".",
382 stmt_name, parent_name);
383 }
384 return LY_EVALID;
385 }
386 }
387 return LY_SUCCESS;
388}
389
390/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200391 * @brief Compile information from the when statement
392 *
393 * @param[in] ctx Compile context.
394 * @param[in] when_p Parsed when structure.
Michal Vaskodfd254d2021-06-24 09:24:59 +0200395 * @param[in] parent_flags Flags of the parsed node with the when statement.
396 * @param[in] compiled_parent Closest compiled parent of the when statement.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200397 * @param[in] ctx_node Context node for the when statement.
398 * @param[out] when Pointer where to store pointer to the created compiled when structure.
399 * @return LY_ERR value.
400 */
401static LY_ERR
Michal Vaskodfd254d2021-06-24 09:24:59 +0200402lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t parent_flags,
403 const struct lysc_node *compiled_parent, const struct lysc_node *ctx_node, struct lysc_when **when)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200404{
405 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200406 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200407
408 *when = calloc(1, sizeof **when);
409 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
410 (*when)->refcount = 1;
411 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200412 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci8df109d2021-04-23 12:19:08 +0200413 LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200414 (*when)->context = (struct lysc_node *)ctx_node;
415 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
416 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100417 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vaskodfd254d2021-06-24 09:24:59 +0200418 (*when)->flags = (parent_flags & LYS_STATUS_MASK);
419 if (compiled_parent) {
420 LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", compiled_parent->flags, compiled_parent->name, 0));
421 } else {
422 LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", 0, NULL, 0));
423 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200424
425done:
426 return ret;
427}
428
429LY_ERR
Michal Vaskodfd254d2021-06-24 09:24:59 +0200430lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t parent_flags, const struct lysc_node *compiled_parent,
431 const struct lysc_node *ctx_node, struct lysc_node *node, struct lysc_when **when_c)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200432{
433 struct lysc_when **new_when, ***node_when;
434
435 assert(when_p);
436
437 /* get the when array */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100438 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200439
440 /* create new when pointer */
441 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
442 if (!when_c || !(*when_c)) {
443 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +0200444 LY_CHECK_RET(lys_compile_when_(ctx, when_p, parent_flags, compiled_parent, ctx_node, new_when));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200445
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200446 /* remember the compiled when for sharing */
447 if (when_c) {
448 *when_c = *new_when;
449 }
450 } else {
451 /* use the previously compiled when */
452 ++(*when_c)->refcount;
453 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100454 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200455
Michal Vasko6f08e962021-07-23 08:30:47 +0200456 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100457 /* do not check "when" semantics in a grouping, but repeat the check for different node because
458 * of dummy node check */
Michal Vaskoc130e162021-10-19 11:30:00 +0200459 LY_CHECK_RET(ly_set_add(&ctx->unres->whens, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200460 }
461
462 return LY_SUCCESS;
463}
464
465/**
466 * @brief Compile information from the must statement
467 * @param[in] ctx Compile context.
468 * @param[in] must_p The parsed must statement structure.
469 * @param[in,out] must Prepared (empty) compiled must structure to fill.
470 * @return LY_ERR value.
471 */
472static LY_ERR
473lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
474{
475 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200476 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200477
478 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200479 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 +0200480 LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200481 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
482 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
483 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
484 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100485 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200486
487done:
488 return ret;
489}
490
491/**
492 * @brief Validate and normalize numeric value from a range definition.
493 * @param[in] ctx Compile context.
494 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
495 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
496 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
497 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
498 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
499 * @param[in] value String value of the range boundary.
500 * @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.
501 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
502 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
503 */
504static LY_ERR
505range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
506 size_t *len, char **valcopy)
507{
508 size_t fraction = 0, size;
509
510 *len = 0;
511
512 assert(value);
513 /* parse value */
514 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
515 return LY_EVALID;
516 }
517
518 if ((value[*len] == '-') || (value[*len] == '+')) {
519 ++(*len);
520 }
521
522 while (isdigit(value[*len])) {
523 ++(*len);
524 }
525
526 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
527 if (basetype == LY_TYPE_DEC64) {
528 goto decimal;
529 } else {
530 *valcopy = strndup(value, *len);
531 return LY_SUCCESS;
532 }
533 }
534 fraction = *len;
535
536 ++(*len);
537 while (isdigit(value[*len])) {
538 ++(*len);
539 }
540
541 if (basetype == LY_TYPE_DEC64) {
542decimal:
543 assert(frdigits);
544 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100545 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200546 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100547 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200548 return LY_EINVAL;
549 }
550 if (fraction) {
551 size = (*len) + (frdigits - ((*len) - 1 - fraction));
552 } else {
553 size = (*len) + frdigits + 1;
554 }
555 *valcopy = malloc(size * sizeof **valcopy);
556 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
557
558 (*valcopy)[size - 1] = '\0';
559 if (fraction) {
560 memcpy(&(*valcopy)[0], &value[0], fraction);
561 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
562 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
563 } else {
564 memcpy(&(*valcopy)[0], &value[0], *len);
565 memset(&(*valcopy)[*len], '0', frdigits);
566 }
567 }
568 return LY_SUCCESS;
569}
570
571/**
572 * @brief Check that values in range are in ascendant order.
573 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
574 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
575 * max can be also equal.
576 * @param[in] value Current value to check.
577 * @param[in] prev_value The last seen value.
578 * @return LY_SUCCESS or LY_EEXIST for invalid order.
579 */
580static LY_ERR
581range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
582{
583 if (unsigned_value) {
584 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
585 return LY_EEXIST;
586 }
587 } else {
588 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
589 return LY_EEXIST;
590 }
591 }
592 return LY_SUCCESS;
593}
594
595/**
596 * @brief Set min/max value of the range part.
597 * @param[in] ctx Compile context.
598 * @param[in] part Range part structure to fill.
599 * @param[in] max Flag to distinguish if storing min or max value.
600 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
601 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
602 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
603 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
604 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
605 * @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.
606 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
607 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
608 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
609 * frdigits value), LY_EMEM.
610 */
611static LY_ERR
612range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
613 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
614{
615 LY_ERR ret = LY_SUCCESS;
616 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100617 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200618
619 if (value) {
620 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
621 LY_CHECK_GOTO(ret, finalize);
622 }
623 if (!valcopy && base_range) {
624 if (max) {
625 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
626 } else {
627 part->min_64 = base_range->parts[0].min_64;
628 }
629 if (!first) {
630 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
631 }
632 goto finalize;
633 }
634
635 switch (basetype) {
636 case LY_TYPE_INT8: /* range */
637 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100638 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 +0200639 } else if (max) {
640 part->max_64 = INT64_C(127);
641 } else {
642 part->min_64 = INT64_C(-128);
643 }
644 if (!ret && !first) {
645 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
646 }
647 break;
648 case LY_TYPE_INT16: /* range */
649 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100650 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
651 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200652 } else if (max) {
653 part->max_64 = INT64_C(32767);
654 } else {
655 part->min_64 = INT64_C(-32768);
656 }
657 if (!ret && !first) {
658 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
659 }
660 break;
661 case LY_TYPE_INT32: /* range */
662 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100663 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
664 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200665 } else if (max) {
666 part->max_64 = INT64_C(2147483647);
667 } else {
668 part->min_64 = INT64_C(-2147483648);
669 }
670 if (!ret && !first) {
671 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
672 }
673 break;
674 case LY_TYPE_INT64: /* range */
675 case LY_TYPE_DEC64: /* range */
676 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100677 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
678 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200679 } else if (max) {
680 part->max_64 = INT64_C(9223372036854775807);
681 } else {
682 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
683 }
684 if (!ret && !first) {
685 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
686 }
687 break;
688 case LY_TYPE_UINT8: /* range */
689 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100690 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 +0200691 } else if (max) {
692 part->max_u64 = UINT64_C(255);
693 } else {
694 part->min_u64 = UINT64_C(0);
695 }
696 if (!ret && !first) {
697 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
698 }
699 break;
700 case LY_TYPE_UINT16: /* range */
701 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100702 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 +0200703 } else if (max) {
704 part->max_u64 = UINT64_C(65535);
705 } else {
706 part->min_u64 = UINT64_C(0);
707 }
708 if (!ret && !first) {
709 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
710 }
711 break;
712 case LY_TYPE_UINT32: /* range */
713 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100714 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
715 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200716 } else if (max) {
717 part->max_u64 = UINT64_C(4294967295);
718 } else {
719 part->min_u64 = UINT64_C(0);
720 }
721 if (!ret && !first) {
722 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
723 }
724 break;
725 case LY_TYPE_UINT64: /* range */
726 case LY_TYPE_STRING: /* length */
727 case LY_TYPE_BINARY: /* length */
728 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100729 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
730 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200731 } else if (max) {
732 part->max_u64 = UINT64_C(18446744073709551615);
733 } else {
734 part->min_u64 = UINT64_C(0);
735 }
736 if (!ret && !first) {
737 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
738 }
739 break;
740 default:
741 LOGINT(ctx->ctx);
742 ret = LY_EINT;
743 }
744
745finalize:
746 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100747 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200748 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
749 length_restr ? "length" : "range", valcopy ? valcopy : *value);
750 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100751 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200752 "Invalid %s restriction - invalid value \"%s\".",
753 length_restr ? "length" : "range", valcopy ? valcopy : *value);
754 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100755 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200756 "Invalid %s restriction - values are not in ascending order (%s).",
757 length_restr ? "length" : "range",
758 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
759 } else if (!ret && value) {
760 *value = *value + len;
761 }
762 free(valcopy);
763 return ret;
764}
765
766/**
767 * @brief Compile the parsed range restriction.
768 * @param[in] ctx Compile context.
769 * @param[in] range_p Parsed range structure to compile.
770 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
771 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
772 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
773 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
774 * range restriction must be more restrictive than the base_range.
775 * @param[in,out] range Pointer to the created current range structure.
776 * @return LY_ERR value.
777 */
778static LY_ERR
779lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
780 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
781{
aPiecek1c4da362021-04-29 14:26:34 +0200782 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200783 const char *expr;
784 struct lysc_range_part *parts = NULL, *part;
785 ly_bool range_expected = 0, uns;
786 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
787
788 assert(range);
789 assert(range_p);
790
791 expr = range_p->arg.str;
792 while (1) {
793 if (isspace(*expr)) {
794 ++expr;
795 } else if (*expr == '\0') {
796 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100797 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200798 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200799 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200800 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200801 goto cleanup;
802 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100803 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200804 "Invalid %s restriction - unexpected end of the expression (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200805 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200806 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200807 goto cleanup;
808 }
809 parts_done++;
810 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100811 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200812 if (parts) {
813 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100814 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200815 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100816 (int)(expr - range_p->arg.str), range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200817 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200818 goto cleanup;
819 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100820 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200821
822 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200823 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 +0200824 part->max_64 = part->min_64;
825 } else if (*expr == '|') {
826 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100827 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200828 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200829 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200830 goto cleanup;
831 }
832 expr++;
833 parts_done++;
834 /* process next part of the expression */
835 } else if (!strncmp(expr, "..", 2)) {
836 expr += 2;
837 while (isspace(*expr)) {
838 expr++;
839 }
840 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100841 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200842 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
aPiecek1c4da362021-04-29 14:26:34 +0200843 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200844 goto cleanup;
845 }
846 /* continue expecting the upper boundary */
847 range_expected = 1;
848 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
849 /* number */
850 if (range_expected) {
851 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200852 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 +0200853 range_expected = 0;
854 } else {
855 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200856 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 +0200857 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
858 part->max_64 = part->min_64;
859 }
860
861 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100862 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
863 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200864 while (isspace(*expr)) {
865 expr++;
866 }
867 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100868 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200869 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200870 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200871 goto cleanup;
872 }
873 if (range_expected) {
874 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200875 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 +0200876 range_expected = 0;
877 } else {
878 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200879 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 +0200880 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
881 part->min_64 = part->max_64;
882 }
883 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100884 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200885 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200886 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200887 goto cleanup;
888 }
889 }
890
891 /* check with the previous range/length restriction */
892 if (base_range) {
893 switch (basetype) {
894 case LY_TYPE_BINARY:
895 case LY_TYPE_UINT8:
896 case LY_TYPE_UINT16:
897 case LY_TYPE_UINT32:
898 case LY_TYPE_UINT64:
899 case LY_TYPE_STRING:
900 uns = 1;
901 break;
902 case LY_TYPE_DEC64:
903 case LY_TYPE_INT8:
904 case LY_TYPE_INT16:
905 case LY_TYPE_INT32:
906 case LY_TYPE_INT64:
907 uns = 0;
908 break;
909 default:
910 LOGINT(ctx->ctx);
911 ret = LY_EINT;
912 goto cleanup;
913 }
914 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
915 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
916 goto baseerror;
917 }
918 /* current lower bound is not lower than the base */
919 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
920 /* base has single value */
921 if (base_range->parts[v].min_64 == parts[u].min_64) {
922 /* both lower bounds are the same */
923 if (parts[u].min_64 != parts[u].max_64) {
924 /* current continues with a range */
925 goto baseerror;
926 } else {
927 /* equal single values, move both forward */
928 ++v;
929 continue;
930 }
931 } else {
932 /* base is single value lower than current range, so the
933 * value from base range is removed in the current,
934 * move only base and repeat checking */
935 ++v;
936 --u;
937 continue;
938 }
939 } else {
940 /* base is the range */
941 if (parts[u].min_64 == parts[u].max_64) {
942 /* current is a single value */
943 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
944 /* current is behind the base range, so base range is omitted,
945 * move the base and keep the current for further check */
946 ++v;
947 --u;
948 } /* else it is within the base range, so move the current, but keep the base */
949 continue;
950 } else {
951 /* both are ranges - check the higher bound, the lower was already checked */
952 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
953 /* higher bound is higher than the current higher bound */
954 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
955 /* but the current lower bound is also higher, so the base range is omitted,
956 * continue with the same current, but move the base */
957 --u;
958 ++v;
959 continue;
960 }
961 /* current range starts within the base range but end behind it */
962 goto baseerror;
963 } else {
964 /* current range is smaller than the base,
965 * move current, but stay with the base */
966 continue;
967 }
968 }
969 }
970 }
971 if (u != parts_done) {
972baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100973 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200974 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200975 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200976 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200977 goto cleanup;
978 }
979 }
980
981 if (!(*range)) {
982 *range = calloc(1, sizeof **range);
983 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
984 }
985
986 /* we rewrite the following values as the types chain is being processed */
987 if (range_p->eapptag) {
988 lydict_remove(ctx->ctx, (*range)->eapptag);
989 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
990 }
991 if (range_p->emsg) {
992 lydict_remove(ctx->ctx, (*range)->emsg);
993 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
994 }
995 if (range_p->dsc) {
996 lydict_remove(ctx->ctx, (*range)->dsc);
997 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
998 }
999 if (range_p->ref) {
1000 lydict_remove(ctx->ctx, (*range)->ref);
1001 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
1002 }
1003 /* extensions are taken only from the last range by the caller */
1004
1005 (*range)->parts = parts;
1006 parts = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001007cleanup:
1008 LY_ARRAY_FREE(parts);
1009
1010 return ret;
1011}
1012
Michal Vasko215d7fc2022-07-15 14:50:44 +02001013/**
1014 * @brief Transform characters block in an XML Schema pattern into Perl character ranges.
1015 *
1016 * @param[in] ctx libyang context.
1017 * @param[in] pattern Original pattern.
1018 * @param[in,out] regex Pattern to modify.
1019 * @return LY_ERR value.
1020 */
1021static LY_ERR
1022lys_compile_pattern_chblocks_xmlschema2perl(const struct ly_ctx *ctx, const char *pattern, char **regex)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001023{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001024#define URANGE_LEN 19
1025 char *ublock2urange[][2] = {
1026 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1027 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1028 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1029 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1030 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1031 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1032 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1033 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1034 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1035 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1036 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1037 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1038 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1039 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1040 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1041 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1042 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1043 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1044 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1045 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1046 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1047 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1048 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1049 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1050 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1051 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1052 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1053 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1054 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1055 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1056 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1057 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1058 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1059 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1060 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1061 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1062 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1063 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1064 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1065 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1066 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1067 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1068 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1069 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1070 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1071 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1072 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1073 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1074 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1075 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1076 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1077 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1078 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1079 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1080 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1081 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1082 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1083 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1084 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1085 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1086 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1087 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1088 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1089 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1090 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1091 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1092 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1093 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1094 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1095 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1096 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1097 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1098 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1099 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1100 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1101 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1102 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1103 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1104 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1105 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1106 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1107 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1108 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +01001109 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001110 {NULL, NULL}
1111 };
1112
Michal Vasko215d7fc2022-07-15 14:50:44 +02001113 size_t idx, idx2, start, end;
1114 char *perl_regex, *ptr;
1115
1116 perl_regex = *regex;
1117
1118 /* substitute Unicode Character Blocks with exact Character Ranges */
1119 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1120 start = ptr - perl_regex;
1121
1122 ptr = strchr(ptr, '}');
1123 if (!ptr) {
1124 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
1125 return LY_EVALID;
1126 }
1127 end = (ptr - perl_regex) + 1;
1128
1129 /* need more space */
1130 if (end - start < URANGE_LEN) {
1131 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko78419922022-07-15 15:15:20 +02001132 *regex = perl_regex;
Michal Vasko215d7fc2022-07-15 14:50:44 +02001133 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1134 }
1135
1136 /* find our range */
1137 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1138 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1139 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1140 break;
1141 }
1142 }
1143 if (!ublock2urange[idx][0]) {
1144 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
1145 return LY_EVALID;
1146 }
1147
1148 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1149 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1150 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1151 ++idx;
1152 }
1153 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1154 --idx;
1155 }
1156 }
1157 if (idx) {
1158 /* skip brackets */
1159 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1160 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1161 } else {
1162 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1163 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1164 }
1165 }
1166
Michal Vasko215d7fc2022-07-15 14:50:44 +02001167 return LY_SUCCESS;
1168}
1169
1170LY_ERR
1171lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
1172{
1173 size_t idx, size, brack;
1174 char *perl_regex;
1175 int err_code, compile_opts;
1176 const char *orig_ptr;
1177 PCRE2_SIZE err_offset;
1178 pcre2_code *code_local;
aPiecek9e716992022-07-21 16:29:47 +02001179 ly_bool escaped;
Michal Vasko215d7fc2022-07-15 14:50:44 +02001180 LY_ERR r;
1181
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001182 /* adjust the expression to a Perl equivalent
1183 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1184
1185 /* allocate space for the transformed pattern */
1186 size = strlen(pattern) + 1;
Michal Vasko03d430c2022-07-22 09:05:56 +02001187 compile_opts = PCRE2_UTF | PCRE2_UCP | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
Christian Hoppsdafed222021-03-22 13:02:42 -04001188#ifdef PCRE2_ENDANCHORED
1189 compile_opts |= PCRE2_ENDANCHORED;
1190#else
1191 /* add space for trailing $ anchor */
1192 size++;
1193#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001194 perl_regex = malloc(size);
1195 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1196 perl_regex[0] = '\0';
1197
1198 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1199 brack = 0;
1200 idx = 0;
aPiecek9e716992022-07-21 16:29:47 +02001201 escaped = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001202 orig_ptr = pattern;
1203 while (orig_ptr[0]) {
1204 switch (orig_ptr[0]) {
1205 case '$':
1206 case '^':
1207 if (!brack) {
1208 /* make space for the extra character */
1209 ++size;
1210 perl_regex = ly_realloc(perl_regex, size);
1211 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1212
1213 /* print escape slash */
1214 perl_regex[idx] = '\\';
1215 ++idx;
1216 }
1217 break;
aPiecek9e716992022-07-21 16:29:47 +02001218 case '\\':
1219 /* escape character found or backslash is escaped */
1220 escaped = !escaped;
1221 /* copy backslash and continue with the next character */
1222 perl_regex[idx] = orig_ptr[0];
1223 ++idx;
1224 ++orig_ptr;
1225 continue;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001226 case '[':
aPiecek9e716992022-07-21 16:29:47 +02001227 if (!escaped) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001228 ++brack;
1229 }
1230 break;
1231 case ']':
aPiecek9e716992022-07-21 16:29:47 +02001232 if (!brack && !escaped) {
1233 /* If ']' does not terminate a character class expression, then pcre2_compile() implicitly escapes the
1234 * ']' character. But this seems to be against the regular expressions rules declared in
1235 * "XML schema: Datatypes" and therefore an error is returned. So for example if pattern is '\[a]' then
1236 * pcre2 match characters '[a]' literally but in YANG such pattern is not allowed.
1237 */
1238 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, orig_ptr, "character group doesn't begin with '['");
1239 free(perl_regex);
1240 return LY_EVALID;
1241 } else if (!escaped) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001242 --brack;
1243 }
1244 break;
1245 default:
1246 break;
1247 }
1248
1249 /* copy char */
1250 perl_regex[idx] = orig_ptr[0];
1251
1252 ++idx;
1253 ++orig_ptr;
aPiecek9e716992022-07-21 16:29:47 +02001254 escaped = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001255 }
Christian Hoppsdafed222021-03-22 13:02:42 -04001256#ifndef PCRE2_ENDANCHORED
1257 /* anchor match to end of subject */
1258 perl_regex[idx++] = '$';
1259#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001260 perl_regex[idx] = '\0';
1261
Michal Vasko215d7fc2022-07-15 14:50:44 +02001262 /* transform character blocks */
1263 if ((r = lys_compile_pattern_chblocks_xmlschema2perl(ctx, pattern, &perl_regex))) {
1264 free(perl_regex);
1265 return r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001266 }
1267
1268 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001269 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001270 &err_code, &err_offset, NULL);
1271 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001272 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1273 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001274 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001275 free(perl_regex);
1276 return LY_EVALID;
1277 }
1278 free(perl_regex);
1279
1280 if (code) {
1281 *code = code_local;
1282 } else {
1283 free(code_local);
1284 }
1285
1286 return LY_SUCCESS;
1287
1288#undef URANGE_LEN
1289}
1290
Michal Vasko51de7b72022-04-29 09:50:22 +02001291LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001292lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1293 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1294{
1295 struct lysc_pattern **pattern;
1296 LY_ARRAY_COUNT_TYPE u;
1297 LY_ERR ret = LY_SUCCESS;
1298
1299 /* first, copy the patterns from the base type */
1300 if (base_patterns) {
1301 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1302 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1303 }
1304
1305 LY_ARRAY_FOR(patterns_p, u) {
1306 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1307 *pattern = calloc(1, sizeof **pattern);
1308 ++(*pattern)->refcount;
1309
Radek Krejci2efc45b2020-12-22 16:25:44 +01001310 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001311 LY_CHECK_RET(ret);
1312
Radek Krejcif13b87b2020-12-01 22:02:17 +01001313 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001314 (*pattern)->inverted = 1;
1315 }
1316 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1317 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1318 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1319 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1320 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001321 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001322 }
1323done:
1324 return ret;
1325}
1326
1327/**
1328 * @brief map of the possible restrictions combination for the specific built-in type.
1329 */
1330static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1331 0 /* LY_TYPE_UNKNOWN */,
1332 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1333 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1334 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1335 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1336 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1337 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1338 LYS_SET_BIT /* LY_TYPE_BITS */,
1339 0 /* LY_TYPE_BOOL */,
1340 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1341 0 /* LY_TYPE_EMPTY */,
1342 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1343 LYS_SET_BASE /* LY_TYPE_IDENT */,
1344 LYS_SET_REQINST /* LY_TYPE_INST */,
1345 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1346 LYS_SET_TYPE /* LY_TYPE_UNION */,
1347 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1348 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1349 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1350 LYS_SET_RANGE /* LY_TYPE_INT64 */
1351};
1352
1353/**
1354 * @brief stringification of the YANG built-in data types
1355 */
1356const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001357 LY_TYPE_UNKNOWN_STR,
1358 LY_TYPE_BINARY_STR,
1359 LY_TYPE_UINT8_STR,
1360 LY_TYPE_UINT16_STR,
1361 LY_TYPE_UINT32_STR,
1362 LY_TYPE_UINT64_STR,
1363 LY_TYPE_STRING_STR,
1364 LY_TYPE_BITS_STR,
1365 LY_TYPE_BOOL_STR,
1366 LY_TYPE_DEC64_STR,
1367 LY_TYPE_EMPTY_STR,
1368 LY_TYPE_ENUM_STR,
1369 LY_TYPE_IDENT_STR,
1370 LY_TYPE_INST_STR,
1371 LY_TYPE_LEAFREF_STR,
1372 LY_TYPE_UNION_STR,
1373 LY_TYPE_INT8_STR,
1374 LY_TYPE_INT16_STR,
1375 LY_TYPE_INT32_STR,
1376 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001377};
1378
1379/**
1380 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1381 * @param[in] ctx Compile context.
1382 * @param[in] enums_p Array of the parsed enum structures to compile.
1383 * @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.
1384 * @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 +01001385 * @param[out] bitenums Newly created array of the compiled bitenums information for the current type.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001386 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1387 */
1388static LY_ERR
1389lys_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 +01001390 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001391{
1392 LY_ERR ret = LY_SUCCESS;
1393 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001394 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1395 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001396 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001397 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001398
1399 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001400 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001401 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1402 return LY_EVALID;
1403 }
1404
1405 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001406 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001407 if (base_enums) {
1408 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1409 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001410 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001411 break;
1412 }
1413 }
1414 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001415 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001416 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001417 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001418 return LY_EVALID;
1419 }
1420 match = v;
1421 }
1422
1423 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001424 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001425 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001426 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001427 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001428 LY_ARRAY_FOR(*bitenums, v) {
1429 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001430 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001431 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001432 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001433 return LY_EVALID;
1434 }
1435 }
1436 } else if (base_enums) {
1437 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001438 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001439 } else {
1440 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001441 if (u == 0) {
1442 cur_val = 0;
1443 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001444 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001445 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001446 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001447 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001448 } else {
1449 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001450 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001451 }
1452
1453 /* save highest value for auto assing */
1454 if (highest_value < cur_val) {
1455 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001456 }
1457 } else { /* LY_TYPE_BITS */
1458 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001459 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001460 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001461 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001462 LY_ARRAY_FOR(*bitenums, v) {
1463 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001464 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001465 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001466 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001467 return LY_EVALID;
1468 }
1469 }
1470 } else if (base_enums) {
1471 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001472 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001473 } else {
1474 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001475 if (u == 0) {
1476 cur_pos = 0;
1477 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001478 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001479 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001480 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001481 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001482 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001483 } else {
1484 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001485 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001486 }
1487
1488 /* save highest position for auto assing */
1489 if (highest_position < cur_pos) {
1490 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001491 }
1492 }
1493
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001494 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001495 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001496 if (basetype == LY_TYPE_ENUM) {
1497 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001498 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001499 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001500 enums_p[u].name, base_enums[match].value, cur_val);
1501 return LY_EVALID;
1502 }
1503 } else {
1504 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001505 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001506 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001507 enums_p[u].name, base_enums[match].position, cur_pos);
1508 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001509 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001510 }
1511 }
1512
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001513 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001514 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001515 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1516 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1517 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001518 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 +01001519 if (basetype == LY_TYPE_ENUM) {
1520 e->value = cur_val;
1521 } else {
1522 e->position = cur_pos;
1523 }
Radek Krejciab430862021-03-02 20:13:40 +01001524 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001525
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001526 /* evaluate if-ffeatures */
1527 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1528 if (!enabled) {
1529 /* set only flag, later resolved and removed */
1530 e->flags |= LYS_DISABLED;
1531 }
1532
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001533 if (basetype == LY_TYPE_BITS) {
1534 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001535 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001536 if (v != u) {
1537 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001538 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1539 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001540 }
1541 }
1542 }
1543
1544done:
1545 return ret;
1546}
1547
Radek Krejci6a205692020-12-09 13:58:22 +01001548static LY_ERR
1549lys_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 +01001550 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001551{
1552 LY_ERR ret = LY_SUCCESS;
1553 struct lysc_type **utypes = *utypes_p;
1554 struct lysc_type_union *un_aux = NULL;
1555
1556 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1557 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001558 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1559 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001560 LY_CHECK_GOTO(ret, error);
1561 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1562 /* add space for additional types from the union subtype */
1563 un_aux = (struct lysc_type_union *)utypes[u + additional];
1564 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1565 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1566
1567 /* copy subtypes of the subtype union */
1568 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1569 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1570 struct lysc_type_leafref *lref;
1571
1572 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1573 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1574 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1575 lref = (struct lysc_type_leafref *)utypes[u + additional];
1576
1577 lref->basetype = LY_TYPE_LEAFREF;
Michal Vaskoe33134a2022-07-29 14:54:40 +02001578 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, 0, 0, &lref->path);
Radek Krejci6a205692020-12-09 13:58:22 +01001579 LY_CHECK_GOTO(ret, error);
Michal Vasko080064b2021-08-31 15:20:44 +02001580 lref->refcount = 1;
Radek Krejci6a205692020-12-09 13:58:22 +01001581 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001582 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001583 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001584 LY_CHECK_GOTO(ret, error);
1585 /* TODO extensions */
1586
1587 } else {
1588 utypes[u + additional] = un_aux->types[v];
Michal Vasko04338d92021-09-01 07:58:14 +02001589 LY_ATOMIC_INC_BARRIER(un_aux->types[v]->refcount);
Radek Krejci6a205692020-12-09 13:58:22 +01001590 }
1591 ++additional;
1592 LY_ARRAY_INCREMENT(utypes);
1593 }
1594 /* compensate u increment in main loop */
1595 --additional;
1596
1597 /* free the replaced union subtype */
Michal Vaskoc636ea42022-09-16 10:20:31 +02001598 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001599 un_aux = NULL;
1600 } else {
1601 LY_ARRAY_INCREMENT(utypes);
1602 }
1603 }
1604
1605 *utypes_p = utypes;
1606 return LY_SUCCESS;
1607
1608error:
1609 if (un_aux) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001610 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001611 }
1612 *utypes_p = utypes;
1613 return ret;
1614}
1615
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001616/**
1617 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1618 * @param[in] ctx Compile context.
1619 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1620 * @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 +02001621 * @param[in] context_name Name of the context node or referencing typedef for logging.
1622 * @param[in] type_p Parsed type to compile.
1623 * @param[in] basetype Base YANG built-in type of the type to compile.
1624 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1625 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1626 * @param[out] type Newly created type structure with the filled information about the type.
1627 * @return LY_ERR value.
1628 */
1629static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001630lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1631 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 +02001632{
1633 LY_ERR ret = LY_SUCCESS;
1634 struct lysc_type_bin *bin;
1635 struct lysc_type_num *num;
1636 struct lysc_type_str *str;
1637 struct lysc_type_bits *bits;
1638 struct lysc_type_enum *enumeration;
1639 struct lysc_type_dec *dec;
1640 struct lysc_type_identityref *idref;
1641 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001642 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001643
1644 switch (basetype) {
1645 case LY_TYPE_BINARY:
1646 bin = (struct lysc_type_bin *)(*type);
1647
1648 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1649 if (type_p->length) {
1650 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1651 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1652 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001653 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001654 }
1655 }
1656 break;
1657 case LY_TYPE_BITS:
1658 /* RFC 7950 9.7 - bits */
1659 bits = (struct lysc_type_bits *)(*type);
1660 if (type_p->bits) {
1661 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1662 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1663 (struct lysc_type_bitenum_item **)&bits->bits));
1664 }
1665
1666 if (!base && !type_p->flags) {
1667 /* type derived from bits built-in type must contain at least one bit */
1668 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001669 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001670 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001671 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001672 }
1673 return LY_EVALID;
1674 }
1675 break;
1676 case LY_TYPE_DEC64:
1677 dec = (struct lysc_type_dec *)(*type);
1678
1679 /* RFC 7950 9.3.4 - fraction-digits */
1680 if (!base) {
1681 if (!type_p->fraction_digits) {
1682 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001683 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001684 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001685 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001686 }
1687 return LY_EVALID;
1688 }
1689 dec->fraction_digits = type_p->fraction_digits;
1690 } else {
1691 if (type_p->fraction_digits) {
1692 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1693 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001694 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001695 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1696 tpdfname);
1697 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001698 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001699 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1700 }
1701 return LY_EVALID;
1702 }
1703 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1704 }
1705
1706 /* RFC 7950 9.2.4 - range */
1707 if (type_p->range) {
1708 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1709 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1710 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001711 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001712 }
1713 }
1714 break;
1715 case LY_TYPE_STRING:
1716 str = (struct lysc_type_str *)(*type);
1717
1718 /* RFC 7950 9.4.4 - length */
1719 if (type_p->length) {
1720 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1721 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1722 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001723 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001724 }
1725 } else if (base && ((struct lysc_type_str *)base)->length) {
1726 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1727 }
1728
1729 /* RFC 7950 9.4.5 - pattern */
1730 if (type_p->patterns) {
1731 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1732 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1733 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1734 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1735 }
1736 break;
1737 case LY_TYPE_ENUM:
1738 enumeration = (struct lysc_type_enum *)(*type);
1739
1740 /* RFC 7950 9.6 - enum */
1741 if (type_p->enums) {
1742 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1743 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1744 }
1745
1746 if (!base && !type_p->flags) {
1747 /* type derived from enumerations built-in type must contain at least one enum */
1748 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001749 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001750 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001751 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001752 }
1753 return LY_EVALID;
1754 }
1755 break;
1756 case LY_TYPE_INT8:
1757 case LY_TYPE_UINT8:
1758 case LY_TYPE_INT16:
1759 case LY_TYPE_UINT16:
1760 case LY_TYPE_INT32:
1761 case LY_TYPE_UINT32:
1762 case LY_TYPE_INT64:
1763 case LY_TYPE_UINT64:
1764 num = (struct lysc_type_num *)(*type);
1765
1766 /* RFC 6020 9.2.4 - range */
1767 if (type_p->range) {
1768 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1769 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1770 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001771 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001772 }
1773 }
1774 break;
1775 case LY_TYPE_IDENT:
1776 idref = (struct lysc_type_identityref *)(*type);
1777
1778 /* RFC 7950 9.10.2 - base */
1779 if (type_p->bases) {
1780 if (base) {
1781 /* only the directly derived identityrefs can contain base specification */
1782 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001783 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001784 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1785 tpdfname);
1786 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001787 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001788 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1789 }
1790 return LY_EVALID;
1791 }
aPiecekf4a0a192021-08-03 15:14:17 +02001792 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001793 }
1794
1795 if (!base && !type_p->flags) {
1796 /* type derived from identityref built-in type must contain at least one base */
1797 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001798 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001799 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001800 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001801 }
1802 return LY_EVALID;
1803 }
1804 break;
1805 case LY_TYPE_LEAFREF:
1806 lref = (struct lysc_type_leafref *)*type;
1807
1808 /* RFC 7950 9.9.3 - require-instance */
1809 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001810 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001811 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001812 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001813 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1814 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001815 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001816 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1817 }
1818 return LY_EVALID;
1819 }
1820 lref->require_instance = type_p->require_instance;
1821 } else if (base) {
1822 /* inherit */
1823 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1824 } else {
1825 /* default is true */
1826 lref->require_instance = 1;
1827 }
1828 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001829 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001830
Michal Vaskoe33134a2022-07-29 14:54:40 +02001831 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, 0, 0, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001832 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 +02001833 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001834 } else if (base) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02001835 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, 0, 0, &lref->path));
Radek Krejci8df109d2021-04-23 12:19:08 +02001836 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001837 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001838 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001839 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001840 return LY_EVALID;
1841 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001842 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001843 return LY_EVALID;
1844 }
1845 break;
1846 case LY_TYPE_INST:
1847 /* RFC 7950 9.9.3 - require-instance */
1848 if (type_p->flags & LYS_SET_REQINST) {
1849 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1850 } else {
1851 /* default is true */
1852 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1853 }
1854 break;
1855 case LY_TYPE_UNION:
1856 un = (struct lysc_type_union *)(*type);
1857
1858 /* RFC 7950 7.4 - type */
1859 if (type_p->types) {
1860 if (base) {
1861 /* only the directly derived union can contain types specification */
1862 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001863 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001864 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1865 tpdfname);
1866 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001867 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001868 "Invalid type substatement for the type not directly derived from union built-in type.");
1869 }
1870 return LY_EVALID;
1871 }
1872 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001873 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 +02001874 }
1875
1876 if (!base && !type_p->flags) {
1877 /* type derived from union built-in type must contain at least one type */
1878 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001879 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001880 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001881 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001882 }
1883 return LY_EVALID;
1884 }
1885 break;
1886 case LY_TYPE_BOOL:
1887 case LY_TYPE_EMPTY:
1888 case LY_TYPE_UNKNOWN: /* just to complete switch */
1889 break;
1890 }
1891
1892 if (tpdfname) {
1893 switch (basetype) {
1894 case LY_TYPE_BINARY:
1895 type_p->compiled = *type;
1896 *type = calloc(1, sizeof(struct lysc_type_bin));
1897 break;
1898 case LY_TYPE_BITS:
1899 type_p->compiled = *type;
1900 *type = calloc(1, sizeof(struct lysc_type_bits));
1901 break;
1902 case LY_TYPE_DEC64:
1903 type_p->compiled = *type;
1904 *type = calloc(1, sizeof(struct lysc_type_dec));
1905 break;
1906 case LY_TYPE_STRING:
1907 type_p->compiled = *type;
1908 *type = calloc(1, sizeof(struct lysc_type_str));
1909 break;
1910 case LY_TYPE_ENUM:
1911 type_p->compiled = *type;
1912 *type = calloc(1, sizeof(struct lysc_type_enum));
1913 break;
1914 case LY_TYPE_INT8:
1915 case LY_TYPE_UINT8:
1916 case LY_TYPE_INT16:
1917 case LY_TYPE_UINT16:
1918 case LY_TYPE_INT32:
1919 case LY_TYPE_UINT32:
1920 case LY_TYPE_INT64:
1921 case LY_TYPE_UINT64:
1922 type_p->compiled = *type;
1923 *type = calloc(1, sizeof(struct lysc_type_num));
1924 break;
1925 case LY_TYPE_IDENT:
1926 type_p->compiled = *type;
1927 *type = calloc(1, sizeof(struct lysc_type_identityref));
1928 break;
1929 case LY_TYPE_LEAFREF:
1930 type_p->compiled = *type;
1931 *type = calloc(1, sizeof(struct lysc_type_leafref));
1932 break;
1933 case LY_TYPE_INST:
1934 type_p->compiled = *type;
1935 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1936 break;
1937 case LY_TYPE_UNION:
1938 type_p->compiled = *type;
1939 *type = calloc(1, sizeof(struct lysc_type_union));
1940 break;
1941 case LY_TYPE_BOOL:
1942 case LY_TYPE_EMPTY:
1943 case LY_TYPE_UNKNOWN: /* just to complete switch */
1944 break;
1945 }
1946 }
1947 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1948
1949cleanup:
1950 return ret;
1951}
1952
1953LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001954lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1955 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001956{
1957 LY_ERR ret = LY_SUCCESS;
1958 ly_bool dummyloops = 0;
1959 struct type_context {
1960 const struct lysp_tpdf *tpdf;
1961 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001962 } *tctx, *tctx_prev = NULL, *tctx_iter;
1963 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1964 struct lysc_type *base = NULL, *prev_type;
1965 struct ly_set tpdf_chain = {0};
Michal Vasko388a6632021-08-06 11:27:43 +02001966 struct lyplg_type *plugin;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001967
1968 (*type) = NULL;
1969 if (dflt) {
1970 *dflt = NULL;
1971 }
1972
1973 tctx = calloc(1, sizeof *tctx);
1974 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001975 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 +02001976 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001977 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1978 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001979 if (basetype) {
1980 break;
1981 }
1982
1983 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001984 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1985 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001986 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1987
1988 if (units && !*units) {
1989 /* inherit units */
1990 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1991 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1992 }
1993 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1994 /* inherit default */
1995 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1996 }
1997 if (dummyloops && (!units || *units) && dflt && *dflt) {
1998 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1999 break;
2000 }
2001
Michal Vasko080064b2021-08-31 15:20:44 +02002002 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
Radek Krejci720d2612021-03-03 19:44:22 +01002003 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
2004 * and we need now recompile the type again in the updated context. */
Michal Vaskoc636ea42022-09-16 10:20:31 +02002005 lysc_type_free(&ctx->free_ctx, tctx->tpdf->type.compiled);
Radek Krejci720d2612021-03-03 19:44:22 +01002006 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
2007 }
2008
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002009 if (tctx->tpdf->type.compiled) {
2010 /* it is not necessary to continue, the rest of the chain was already compiled,
2011 * but we still may need to inherit default and units values, so start dummy loops */
2012 basetype = tctx->tpdf->type.compiled->basetype;
2013 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2014 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2015
2016 if ((units && !*units) || (dflt && !*dflt)) {
2017 dummyloops = 1;
2018 goto preparenext;
2019 } else {
2020 tctx = NULL;
2021 break;
2022 }
2023 }
2024
2025 /* circular typedef reference detection */
2026 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
2027 /* local part */
2028 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002029 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002030 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002031 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2032 free(tctx);
2033 ret = LY_EVALID;
2034 goto cleanup;
2035 }
2036 }
2037 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
2038 /* global part for unions corner case */
2039 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002040 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002041 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002042 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2043 free(tctx);
2044 ret = LY_EVALID;
2045 goto cleanup;
2046 }
2047 }
2048
2049 /* store information for the following processing */
2050 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2051 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2052
2053preparenext:
2054 /* prepare next loop */
2055 tctx_prev = tctx;
2056 tctx = calloc(1, sizeof *tctx);
2057 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2058 }
2059 free(tctx);
2060
2061 /* allocate type according to the basetype */
2062 switch (basetype) {
2063 case LY_TYPE_BINARY:
2064 *type = calloc(1, sizeof(struct lysc_type_bin));
2065 break;
2066 case LY_TYPE_BITS:
2067 *type = calloc(1, sizeof(struct lysc_type_bits));
2068 break;
2069 case LY_TYPE_BOOL:
2070 case LY_TYPE_EMPTY:
2071 *type = calloc(1, sizeof(struct lysc_type));
2072 break;
2073 case LY_TYPE_DEC64:
2074 *type = calloc(1, sizeof(struct lysc_type_dec));
2075 break;
2076 case LY_TYPE_ENUM:
2077 *type = calloc(1, sizeof(struct lysc_type_enum));
2078 break;
2079 case LY_TYPE_IDENT:
2080 *type = calloc(1, sizeof(struct lysc_type_identityref));
2081 break;
2082 case LY_TYPE_INST:
2083 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2084 break;
2085 case LY_TYPE_LEAFREF:
2086 *type = calloc(1, sizeof(struct lysc_type_leafref));
2087 break;
2088 case LY_TYPE_STRING:
2089 *type = calloc(1, sizeof(struct lysc_type_str));
2090 break;
2091 case LY_TYPE_UNION:
2092 *type = calloc(1, sizeof(struct lysc_type_union));
2093 break;
2094 case LY_TYPE_INT8:
2095 case LY_TYPE_UINT8:
2096 case LY_TYPE_INT16:
2097 case LY_TYPE_UINT16:
2098 case LY_TYPE_INT32:
2099 case LY_TYPE_UINT32:
2100 case LY_TYPE_INT64:
2101 case LY_TYPE_UINT64:
2102 *type = calloc(1, sizeof(struct lysc_type_num));
2103 break;
2104 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002105 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002106 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2107 ret = LY_EVALID;
2108 goto cleanup;
2109 }
2110 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
2111 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002112 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002113 ly_data_type2str[basetype]);
2114 free(*type);
2115 (*type) = NULL;
2116 ret = LY_EVALID;
2117 goto cleanup;
2118 }
2119
2120 /* get restrictions from the referred typedefs */
2121 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2122 tctx = (struct type_context *)tpdf_chain.objs[u];
2123
2124 /* remember the typedef context for circular check */
2125 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
2126 LY_CHECK_GOTO(ret, cleanup);
2127
2128 if (tctx->tpdf->type.compiled) {
Michal Vasko388a6632021-08-06 11:27:43 +02002129 /* already compiled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002130 base = tctx->tpdf->type.compiled;
2131 continue;
Michal Vasko388a6632021-08-06 11:27:43 +02002132 }
2133
Michal Vaskoddd76592022-01-17 13:34:48 +01002134 /* try to find loaded user type plugins */
2135 plugin = lyplg_find(LYPLG_TYPE, tctx->tpdf->type.pmod->mod->name, tctx->tpdf->type.pmod->mod->revision,
2136 tctx->tpdf->name);
Haithem Ben Ghorbal45be15f2022-07-12 18:02:36 +02002137 if (!plugin && base) {
2138 /* use the base type implementation if available */
2139 plugin = base->plugin;
2140 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002141 if (!plugin) {
2142 /* use the internal built-in type implementation */
2143 plugin = lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
2144 }
Michal Vasko388a6632021-08-06 11:27:43 +02002145 assert(plugin);
2146
2147 if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags) &&
2148 (plugin == base->plugin)) {
2149 /* no change, reuse the compiled base */
2150 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002151 LY_ATOMIC_INC_BARRIER(base->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002152 continue;
2153 }
2154
Michal Vasko04338d92021-09-01 07:58:14 +02002155 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002156 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002157 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002158 tctx->tpdf->name, ly_data_type2str[basetype]);
2159 ret = LY_EVALID;
2160 goto cleanup;
2161 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002162 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002163 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2164 tctx->tpdf->name, tctx->tpdf->dflt.str);
2165 ret = LY_EVALID;
2166 goto cleanup;
2167 }
2168
2169 (*type)->basetype = basetype;
Michal Vasko388a6632021-08-06 11:27:43 +02002170 (*type)->plugin = plugin;
2171
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002172 /* collect extensions */
2173 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko388a6632021-08-06 11:27:43 +02002174
2175 /* compile the new typedef */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002176 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01002177 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002178 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
2179 LY_CHECK_GOTO(ret, cleanup);
2180 base = prev_type;
2181 }
2182 /* remove the processed typedef contexts from the stack for circular check */
2183 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
2184
2185 /* process the type definition in leaf */
2186 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
2187 /* get restrictions from the node itself */
2188 (*type)->basetype = basetype;
Radek Krejcibf940f92021-03-24 21:04:13 +01002189 (*type)->plugin = base ? base->plugin : lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
Michal Vasko04338d92021-09-01 07:58:14 +02002190 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vaskoa99b3572021-02-01 11:54:58 +01002191 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002192 LY_CHECK_GOTO(ret, cleanup);
2193 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
2194 /* no specific restriction in leaf's type definition, copy from the base */
2195 free(*type);
2196 (*type) = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002197 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002198 }
2199
Radek Krejciab430862021-03-02 20:13:40 +01002200 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002201
2202cleanup:
2203 ly_set_erase(&tpdf_chain, free);
2204 return ret;
2205}
2206
2207/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002208 * @brief Check uniqness of the node/action/notification name.
2209 *
2210 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2211 * structures, but they share the namespace so we need to check their name collisions.
2212 *
2213 * @param[in] ctx Compile context.
2214 * @param[in] parent Parent of the nodes to check, can be NULL.
2215 * @param[in] name Name of the item to find in the given lists.
2216 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2217 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2218 */
2219static LY_ERR
2220lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2221 const struct lysc_node *exclude)
2222{
2223 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002224 const struct lysc_node_action *actions;
2225 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002226 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002227 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002228
2229#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2230
2231 if (exclude->nodetype == LYS_CASE) {
2232 /* check restricted only to all the cases */
2233 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002234 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002235 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002236 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002237 return LY_EEXIST;
2238 }
2239 }
2240
2241 return LY_SUCCESS;
2242 }
2243
2244 /* no reason for our parent to be choice anymore */
2245 assert(!parent || (parent->nodetype != LYS_CHOICE));
2246
2247 if (parent && (parent->nodetype == LYS_CASE)) {
2248 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002249
2250 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2251 iter = lysc_data_parent(parent);
2252 do {
2253 parent = parent->parent;
2254 if (parent && (parent->nodetype == LYS_CHOICE)) {
2255 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2256 }
2257 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002258 }
2259
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002260 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002261 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2262 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2263 if (exclude->flags & LYS_IS_OUTPUT) {
2264 getnext_flags |= LYS_GETNEXT_OUTPUT;
2265 parent = lysc_node_child(parent)->next;
2266 } else {
2267 parent = lysc_node_child(parent);
2268 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002269 }
2270
2271 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002272 if (!parent && ctx->ext) {
2273 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2274 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2275 goto error;
2276 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002277
Radek Krejci5fa32a32021-02-08 18:12:38 +01002278 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2279 if (iter->nodetype == LYS_CHOICE) {
2280 iter2 = NULL;
2281 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2282 if (CHECK_NODE(iter2, exclude, name)) {
2283 goto error;
2284 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002285 }
2286 }
2287 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002288 } else {
2289 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2290 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2291 goto error;
2292 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002293
Radek Krejci5fa32a32021-02-08 18:12:38 +01002294 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2295 if (iter->nodetype == LYS_CHOICE) {
2296 iter2 = NULL;
2297 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2298 if (CHECK_NODE(iter2, exclude, name)) {
2299 goto error;
2300 }
2301 }
2302 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002303 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002304
Radek Krejci5fa32a32021-02-08 18:12:38 +01002305 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2306 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2307 if (CHECK_NODE(iter, exclude, name)) {
2308 goto error;
2309 }
2310 }
2311
2312 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2313 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2314 if (CHECK_NODE(iter, exclude, name)) {
2315 goto error;
2316 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002317 }
2318 }
Radek Krejci078e4342021-02-12 18:17:26 +01002319 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002320 return LY_SUCCESS;
2321
2322error:
Radek Krejci078e4342021-02-12 18:17:26 +01002323 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002324 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002325 return LY_EEXIST;
2326
2327#undef CHECK_NODE
2328}
2329
Radek Krejci2a9fc652021-01-22 17:44:34 +01002330/**
2331 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2332 * keep specific order of augments targetting the same node.
2333 *
2334 * @param[in] ctx Compile context
2335 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2336 * the choice itself is expected instead of a specific case node.
2337 * @param[in] node Schema node to connect into the list.
2338 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2339 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2340 */
2341static LY_ERR
2342lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002343{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002344 struct lysc_node **children, *anchor = NULL;
2345 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002346
Radek Krejci2a9fc652021-01-22 17:44:34 +01002347 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002348
Radek Krejci2a9fc652021-01-22 17:44:34 +01002349 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002350 if (node->nodetype == LYS_INPUT) {
2351 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2352 /* input node is part of the action but link it with output */
2353 node->next = &((struct lysc_node_action *)parent)->output.node;
2354 node->prev = node->next;
2355 return LY_SUCCESS;
2356 } else if (node->nodetype == LYS_OUTPUT) {
2357 /* output node is part of the action but link it with input */
2358 node->next = NULL;
2359 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002360 return LY_SUCCESS;
2361 } else if (node->nodetype == LYS_ACTION) {
2362 children = (struct lysc_node **)lysc_node_actions_p(parent);
2363 } else if (node->nodetype == LYS_NOTIF) {
2364 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2365 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002366 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002367 }
2368 assert(children);
2369
2370 if (!(*children)) {
2371 /* first child */
2372 *children = node;
2373 } else if (node->flags & LYS_KEY) {
2374 /* special handling of adding keys */
2375 assert(node->module == parent->module);
2376 anchor = *children;
2377 if (anchor->flags & LYS_KEY) {
2378 while ((anchor->flags & LYS_KEY) && anchor->next) {
2379 anchor = anchor->next;
2380 }
2381 /* insert after the last key */
2382 insert_after = 1;
2383 } /* else insert before anchor (at the beginning) */
2384 } else if ((*children)->prev->module == node->module) {
2385 /* last child is from the same module, keep the order and insert at the end */
2386 anchor = (*children)->prev;
2387 insert_after = 1;
2388 } else if (parent->module == node->module) {
2389 /* adding module child after some augments were connected */
2390 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2391 } else {
2392 /* some augments are already connected and we are connecting new ones,
2393 * keep module name order and insert the node into the children list */
2394 anchor = *children;
2395 do {
2396 anchor = anchor->prev;
2397
2398 /* check that we have not found the last augment node from our module or
2399 * the first augment node from a "smaller" module or
2400 * the first node from a local module */
2401 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2402 (anchor->module == parent->module)) {
2403 /* insert after */
2404 insert_after = 1;
2405 break;
2406 }
2407
2408 /* we have traversed all the nodes, insert before anchor (as the first node) */
2409 } while (anchor->prev->next);
2410 }
2411
2412 /* insert */
2413 if (anchor) {
2414 if (insert_after) {
2415 node->next = anchor->next;
2416 node->prev = anchor;
2417 anchor->next = node;
2418 if (node->next) {
2419 /* middle node */
2420 node->next->prev = node;
2421 } else {
2422 /* last node */
2423 (*children)->prev = node;
2424 }
2425 } else {
2426 node->next = anchor;
2427 node->prev = anchor->prev;
2428 anchor->prev = node;
2429 if (anchor == *children) {
2430 /* first node */
2431 *children = node;
2432 } else {
2433 /* middle node */
2434 node->prev->next = node;
2435 }
2436 }
2437 }
2438
2439 /* check the name uniqueness (even for an only child, it may be in case) */
2440 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2441 return LY_EEXIST;
2442 }
2443 } else {
2444 /* top-level element */
2445 struct lysc_node **list;
2446
Radek Krejci6b88a462021-02-17 12:39:34 +01002447 if (ctx->ext) {
Michal Vasko55bce0e2022-02-15 10:46:08 +01002448 /* container matches all data nodes */
2449 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER, (void **)&list, NULL);
Radek Krejci6b88a462021-02-17 12:39:34 +01002450 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002451 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2452 } else if (node->nodetype == LYS_NOTIF) {
2453 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2454 } else {
2455 list = &ctx->cur_mod->compiled->data;
2456 }
2457 if (!(*list)) {
2458 *list = node;
2459 } else {
2460 /* insert at the end of the module's top-level nodes list */
2461 (*list)->prev->next = node;
2462 node->prev = (*list)->prev;
2463 (*list)->prev = node;
2464 }
2465
2466 /* check the name uniqueness on top-level */
2467 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2468 return LY_EEXIST;
2469 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002470 }
2471
Radek Krejci2a9fc652021-01-22 17:44:34 +01002472 return LY_SUCCESS;
2473}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002474
Radek Krejci2a9fc652021-01-22 17:44:34 +01002475/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002476 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002477 *
2478 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002479 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002480 * @return LY_ERR value.
2481 */
2482static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002483lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002484{
2485 /* case never has any explicit config */
2486 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002487
Michal Vasko7c565922021-06-10 14:58:27 +02002488 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Radek Krejci91531e12021-02-08 19:57:54 +01002489 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002490 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002491 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2492 /* config not explicitly set, inherit it from parent */
Michal Vaskoecdc2222022-01-24 08:45:44 +01002493 assert(!node->parent || (node->parent->flags & LYS_CONFIG_MASK) || (node->parent->nodetype & LYS_AUGMENT));
2494 if (node->parent && (node->parent->flags & LYS_CONFIG_MASK)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002495 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002496 } else {
2497 /* default is config true */
2498 node->flags |= LYS_CONFIG_W;
2499 }
2500 } else {
2501 /* config set explicitly */
2502 node->flags |= LYS_SET_CONFIG;
2503 }
2504
Radek Krejci91531e12021-02-08 19:57:54 +01002505 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002506 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002507 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002508 }
2509
Radek Krejci2a9fc652021-01-22 17:44:34 +01002510 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002511}
2512
Radek Krejci91531e12021-02-08 19:57:54 +01002513/**
2514 * @brief Set various flags of the compiled nodes
2515 *
2516 * @param[in] ctx Compile context.
2517 * @param[in] node Compiled node where the flags will be set.
2518 * @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).
2519 * Zero means no uses, non-zero value with no status bit set mean the default status.
2520 */
2521static LY_ERR
2522lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2523{
2524 /* inherit config flags */
2525 LY_CHECK_RET(lys_compile_config(ctx, node));
2526
2527 /* status - it is not inherited by specification, but it does not make sense to have
2528 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002529 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, node->name, uses_status ? uses_status :
2530 (node->parent ? node->parent->flags : 0), uses_status ? "<uses>" : (node->parent ? node->parent->name : NULL),
2531 !uses_status));
Radek Krejci91531e12021-02-08 19:57:54 +01002532
2533 /* other flags */
Michal Vasko7c565922021-06-10 14:58:27 +02002534 if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002535 node->flags |= LYS_IS_INPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002536 } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002537 node->flags |= LYS_IS_OUTPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002538 } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002539 node->flags |= LYS_IS_NOTIF;
2540 }
2541
2542 return LY_SUCCESS;
2543}
2544
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002545LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002546lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2547 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2548 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002549{
2550 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002551 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002552 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002553 struct lysp_when *pwhen = NULL;
Michal Vaskoe02e7402021-07-23 08:29:28 +02002554 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002555
Radek Krejci2a9fc652021-01-22 17:44:34 +01002556 node->nodetype = pnode->nodetype;
2557 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002558 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002559 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002560 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002561
Radek Krejci2a9fc652021-01-22 17:44:34 +01002562 /* compile any deviations for this node */
2563 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vaskoe02e7402021-07-23 08:29:28 +02002564 if (not_supported && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2565 /* if not supported, keep it just like disabled nodes by if-feature */
2566 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
2567 ctx->compile_opts |= LYS_COMPILE_DISABLED;
2568 }
2569 if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002570 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002571 }
2572
Radek Krejci2a9fc652021-01-22 17:44:34 +01002573 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vaskodfd254d2021-06-24 09:24:59 +02002574 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2575 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2576 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002577
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002578 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002579 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002580 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002581 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002582 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002583 }
2584
Radek Krejci91531e12021-02-08 19:57:54 +01002585 /* config, status and other flags */
2586 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002587 LY_CHECK_GOTO(ret, error);
2588
2589 /* list ordering */
2590 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002591 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002592 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002593 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002594 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002595 node->flags &= ~LYS_ORDBY_MASK;
2596 node->flags |= LYS_ORDBY_SYSTEM;
2597 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2598 /* default ordering is system */
2599 node->flags |= LYS_ORDBY_SYSTEM;
2600 }
2601 }
2602
Radek Krejci2a9fc652021-01-22 17:44:34 +01002603 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2604 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2605
Radek Krejci9a3823e2021-01-27 20:26:46 +01002606 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002607 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002608 ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002609 LY_CHECK_GOTO(ret, cleanup);
2610 }
2611
Radek Krejci2a9fc652021-01-22 17:44:34 +01002612 /* nodetype-specific part */
2613 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2614
2615 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002616 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002617 if (node->flags & LYS_MAND_TRUE) {
2618 /* inherit LYS_MAND_TRUE in parent containers */
2619 lys_compile_mandatory_parents(parent, 1);
2620 }
2621
2622 if (child_set) {
2623 /* add the new node into set */
2624 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2625 }
2626
2627 goto cleanup;
2628
2629error:
Michal Vaskoc636ea42022-09-16 10:20:31 +02002630 lysc_node_free(&ctx->free_ctx, node, 0);
2631
Radek Krejci2a9fc652021-01-22 17:44:34 +01002632cleanup:
2633 if (ret && dev_pnode) {
2634 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2635 }
Michal Vaskoe02e7402021-07-23 08:29:28 +02002636 ctx->compile_opts = prev_opts;
Michal Vaskoc636ea42022-09-16 10:20:31 +02002637 lysp_dev_node_free(ctx, dev_pnode);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002638 return ret;
2639}
2640
2641/**
2642 * @brief Compile parsed action's input/output node information.
2643 * @param[in] ctx Compile context
2644 * @param[in] pnode Parsed inout node.
2645 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2646 * is enriched with the inout-specific information.
2647 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2648 */
2649LY_ERR
2650lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2651{
2652 LY_ERR ret = LY_SUCCESS;
2653 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002654 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002655
2656 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2657 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2658
2659 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002660 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002661 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002662
Radek Krejci01180ac2021-01-27 08:48:22 +01002663 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002664 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2665 }
2666
Michal Vasko95f736c2022-06-08 12:03:31 +02002667 /* connect any augments */
2668 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2669
Michal Vasko7c565922021-06-10 14:58:27 +02002670 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002671
2672done:
2673 return ret;
2674}
2675
2676/**
2677 * @brief Compile parsed action node information.
2678 * @param[in] ctx Compile context
2679 * @param[in] pnode Parsed action node.
2680 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2681 * is enriched with the action-specific information.
2682 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2683 */
2684LY_ERR
2685lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2686{
2687 LY_ERR ret;
2688 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2689 struct lysc_node_action *action = (struct lysc_node_action *)node;
2690 struct lysp_node_action_inout *input, implicit_input = {
2691 .nodetype = LYS_INPUT,
2692 .name = "input",
2693 .parent = pnode,
2694 };
2695 struct lysp_node_action_inout *output, implicit_output = {
2696 .nodetype = LYS_OUTPUT,
2697 .name = "output",
2698 .parent = pnode,
2699 };
2700
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002701 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002702 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002703 if (action_p->input.nodetype == LYS_UNKNOWN) {
2704 input = &implicit_input;
2705 } else {
2706 input = &action_p->input;
2707 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002708 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 +01002709 lysc_update_path(ctx, NULL, NULL);
2710 LY_CHECK_GOTO(ret, done);
2711
Michal Vaskoc130e162021-10-19 11:30:00 +02002712 /* add must(s) to unres */
2713 ret = lysc_unres_must_add(ctx, &action->input.node, &input->node);
2714 LY_CHECK_GOTO(ret, done);
2715
Radek Krejci2a9fc652021-01-22 17:44:34 +01002716 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002717 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002718 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002719 output = &implicit_output;
2720 } else {
2721 output = &action_p->output;
2722 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002723 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 +01002724 lysc_update_path(ctx, NULL, NULL);
2725 LY_CHECK_GOTO(ret, done);
2726
Michal Vaskoc130e162021-10-19 11:30:00 +02002727 /* add must(s) to unres */
2728 ret = lysc_unres_must_add(ctx, &action->output.node, &output->node);
2729 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002730
2731done:
2732 return ret;
2733}
2734
2735/**
2736 * @brief Compile parsed action node information.
2737 * @param[in] ctx Compile context
2738 * @param[in] pnode Parsed action node.
2739 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2740 * is enriched with the action-specific information.
2741 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2742 */
2743LY_ERR
2744lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2745{
2746 LY_ERR ret = LY_SUCCESS;
2747 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2748 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2749 struct lysp_node *child_p;
2750
2751 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002752
2753 /* add must(s) to unres */
2754 ret = lysc_unres_must_add(ctx, node, pnode);
2755 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002756
Radek Krejci01180ac2021-01-27 08:48:22 +01002757 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002758 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002759 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002760 }
2761
Michal Vasko95f736c2022-06-08 12:03:31 +02002762 /* connect any augments */
2763 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2764
Radek Krejci2a9fc652021-01-22 17:44:34 +01002765done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002766 return ret;
2767}
2768
2769/**
2770 * @brief Compile parsed container node information.
2771 * @param[in] ctx Compile context
2772 * @param[in] pnode Parsed container node.
2773 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2774 * is enriched with the container-specific information.
2775 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2776 */
2777static LY_ERR
2778lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2779{
2780 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2781 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2782 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002783 LY_ERR ret = LY_SUCCESS;
2784
2785 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002786 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002787 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002788 }
2789
2790 /* more cases when the container has meaning but is kept NP for convenience:
2791 * - when condition
2792 * - direct child action/notification
2793 */
2794
2795 LY_LIST_FOR(cont_p->child, child_p) {
2796 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2797 LY_CHECK_GOTO(ret, done);
2798 }
2799
Michal Vasko5347e3a2020-11-03 17:14:57 +01002800 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002801
2802 /* add must(s) to unres */
2803 ret = lysc_unres_must_add(ctx, node, pnode);
2804 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002805
Michal Vasko95f736c2022-06-08 12:03:31 +02002806 /* connect any augments */
2807 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2808
Radek Krejci2a9fc652021-01-22 17:44:34 +01002809 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2810 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2811 LY_CHECK_GOTO(ret, done);
2812 }
2813 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2814 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2815 LY_CHECK_GOTO(ret, done);
2816 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002817
2818done:
2819 return ret;
2820}
2821
Michal Vasko72244882021-01-12 15:21:05 +01002822/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002823 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2824 * @param[in] ctx Compile context.
2825 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2826 * @param[in] type_p Parsed type to compile.
2827 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2828 * @return LY_ERR value.
2829 */
2830static LY_ERR
2831lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2832 struct lysc_node_leaf *leaf)
2833{
2834 struct lysp_qname *dflt;
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002835 struct lysc_type **t;
2836 LY_ARRAY_COUNT_TYPE u, count;
2837 ly_bool in_unres = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002838
Michal Vaskoa99b3572021-02-01 11:54:58 +01002839 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002840 leaf->units ? NULL : &leaf->units, &dflt));
2841
2842 /* store default value, if any */
2843 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2844 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2845 }
2846
Michal Vaskoc130e162021-10-19 11:30:00 +02002847 /* store leafref(s) to be resolved */
2848 LY_CHECK_RET(lysc_unres_leafref_add(ctx, leaf, type_p->pmod));
2849
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002850 /* type-specific checks */
2851 if (leaf->type->basetype == LY_TYPE_UNION) {
2852 t = ((struct lysc_type_union *)leaf->type)->types;
2853 count = LY_ARRAY_COUNT(t);
2854 } else {
2855 t = &leaf->type;
2856 count = 1;
2857 }
2858 for (u = 0; u < count; ++u) {
2859 if (t[u]->basetype == LY_TYPE_EMPTY) {
2860 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
2861 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2862 return LY_EVALID;
2863 }
2864 } else if (!in_unres && ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM))) {
2865 /* store in unres for all disabled bits/enums to be removed */
2866 LY_CHECK_RET(lysc_unres_bitenum_add(ctx, leaf));
2867 in_unres = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002868 }
2869 }
2870
2871 return LY_SUCCESS;
2872}
2873
2874/**
2875 * @brief Compile parsed leaf node information.
2876 * @param[in] ctx Compile context
2877 * @param[in] pnode Parsed leaf node.
2878 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2879 * is enriched with the leaf-specific information.
2880 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2881 */
2882static LY_ERR
2883lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2884{
2885 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2886 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002887 LY_ERR ret = LY_SUCCESS;
2888
Michal Vasko5347e3a2020-11-03 17:14:57 +01002889 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002890
2891 /* add must(s) to unres */
2892 ret = lysc_unres_must_add(ctx, node, pnode);
2893 LY_CHECK_GOTO(ret, done);
2894
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002895 if (leaf_p->units) {
2896 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2897 leaf->flags |= LYS_SET_UNITS;
2898 }
2899
2900 /* compile type */
2901 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2902 LY_CHECK_GOTO(ret, done);
2903
2904 /* store/update default value */
2905 if (leaf_p->dflt.str) {
2906 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2907 leaf->flags |= LYS_SET_DFLT;
2908 }
2909
2910 /* checks */
2911 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002912 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory leaf with a default value.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002913 return LY_EVALID;
2914 }
2915
2916done:
2917 return ret;
2918}
2919
2920/**
2921 * @brief Compile parsed leaf-list node information.
2922 * @param[in] ctx Compile context
2923 * @param[in] pnode Parsed leaf-list node.
2924 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2925 * is enriched with the leaf-list-specific information.
2926 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2927 */
2928static LY_ERR
2929lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2930{
2931 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2932 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002933 LY_ERR ret = LY_SUCCESS;
2934
Michal Vasko5347e3a2020-11-03 17:14:57 +01002935 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002936
2937 /* add must(s) to unres */
2938 ret = lysc_unres_must_add(ctx, node, pnode);
2939 LY_CHECK_GOTO(ret, done);
2940
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002941 if (llist_p->units) {
2942 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2943 llist->flags |= LYS_SET_UNITS;
2944 }
2945
2946 /* compile type */
2947 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2948 LY_CHECK_GOTO(ret, done);
2949
2950 /* store/update default values */
2951 if (llist_p->dflts) {
2952 if (ctx->pmod->version < LYS_VERSION_1_1) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002953 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list default values are allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002954 return LY_EVALID;
2955 }
2956
2957 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2958 llist->flags |= LYS_SET_DFLT;
2959 }
2960
2961 llist->min = llist_p->min;
2962 if (llist->min) {
2963 llist->flags |= LYS_MAND_TRUE;
2964 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002965 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
2966
2967 if (llist->flags & LYS_CONFIG_R) {
2968 /* state leaf-list is always ordered-by user */
2969 llist->flags &= ~LYS_ORDBY_SYSTEM;
2970 llist->flags |= LYS_ORDBY_USER;
2971 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002972
2973 /* checks */
2974 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002975 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 +02002976 return LY_EVALID;
2977 }
2978
2979 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002980 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002981 llist->min, llist->max);
2982 return LY_EVALID;
2983 }
2984
2985done:
2986 return ret;
2987}
2988
Michal Vaskob5a487d2021-12-14 10:31:04 +01002989/**
2990 * @brief Find the node according to the given descendant/absolute schema nodeid.
2991 * Used in unique, refine and augment statements.
2992 *
2993 * @param[in] ctx Compile context
2994 * @param[in] nodeid Descendant-schema-nodeid (according to the YANG grammar)
2995 * @param[in] nodeid_len Length of the given nodeid, if it is not NULL-terminated string.
2996 * @param[in] ctx_node Context node for a relative nodeid.
Michal Vaskob5a487d2021-12-14 10:31:04 +01002997 * @param[in] format Format of any prefixes.
2998 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
2999 * @param[in] nodetype Optional (can be 0) restriction for target's nodetype. If target exists, but does not match
3000 * the given nodetype, LY_EDENIED is returned (and target is provided), but no error message is printed.
3001 * The value can be even an ORed value to allow multiple nodetypes.
3002 * @param[out] target Found target node if any.
3003 * @param[out] result_flag Output parameter to announce if the schema nodeid goes through the action's input/output or a Notification.
3004 * The LYSC_OPT_RPC_INPUT, LYSC_OPT_RPC_OUTPUT and LYSC_OPT_NOTIFICATION are used as flags.
3005 * @return LY_ERR values - LY_ENOTFOUND, LY_EVALID, LY_EDENIED or LY_SUCCESS.
3006 */
3007static LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003008lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
Michal Vasko56d8da82021-12-16 15:41:30 +01003009 LY_VALUE_FORMAT format, void *prefix_data, uint16_t nodetype, const struct lysc_node **target, uint16_t *result_flag)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003010{
3011 LY_ERR ret = LY_EVALID;
3012 const char *name, *prefix, *id;
3013 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01003014 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003015 const char *nodeid_type;
3016 uint32_t getnext_extra_flag = 0;
3017 uint16_t current_nodetype = 0;
3018
3019 assert(nodeid);
3020 assert(target);
3021 assert(result_flag);
3022 *target = NULL;
3023 *result_flag = 0;
3024
3025 id = nodeid;
3026
3027 if (ctx_node) {
3028 /* descendant-schema-nodeid */
3029 nodeid_type = "descendant";
3030
3031 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003032 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003033 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01003034 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003035 return LY_EVALID;
3036 }
3037 } else {
3038 /* absolute-schema-nodeid */
3039 nodeid_type = "absolute";
3040
3041 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003042 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003043 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003044 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003045 return LY_EVALID;
3046 }
3047 ++id;
3048 }
3049
3050 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
3051 if (prefix) {
3052 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
3053 if (!mod) {
3054 /* module must always be found */
3055 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01003056 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003057 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003058 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003059 return LY_ENOTFOUND;
3060 }
3061 } else {
3062 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003063 case LY_VALUE_SCHEMA:
3064 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003065 /* use the current module */
Michal Vasko56d8da82021-12-16 15:41:30 +01003066 mod = ctx->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003067 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003068 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02003069 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003070 if (!ctx_node) {
3071 LOGINT_RET(ctx->ctx);
3072 }
3073 /* inherit the module of the previous context node */
3074 mod = ctx_node->module;
3075 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02003076 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02003077 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01003078 case LY_VALUE_STR_NS:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003079 /* not really defined */
3080 LOGINT_RET(ctx->ctx);
3081 }
3082 }
3083
3084 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
3085 /* move through input/output manually */
3086 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01003087 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3088 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003089 return LY_ENOTFOUND;
3090 }
3091 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003092 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003093 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003094 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003095 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
3096 } else {
3097 /* only input or output is valid */
3098 ctx_node = NULL;
3099 }
3100 } else {
3101 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003102 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003103 getnext_extra_flag = 0;
3104 }
3105 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01003106 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3107 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003108 return LY_ENOTFOUND;
3109 }
3110 current_nodetype = ctx_node->nodetype;
3111
3112 if (current_nodetype == LYS_NOTIF) {
3113 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
3114 } else if (current_nodetype == LYS_INPUT) {
3115 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
3116 } else if (current_nodetype == LYS_OUTPUT) {
3117 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
3118 }
3119
3120 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
3121 break;
3122 }
3123 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003124 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003125 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01003126 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003127 return LY_EVALID;
3128 }
3129 ++id;
3130 }
3131
3132 if (ret == LY_SUCCESS) {
3133 *target = ctx_node;
3134 if (nodetype && !(current_nodetype & nodetype)) {
3135 return LY_EDENIED;
3136 }
3137 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003138 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003139 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01003140 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003141 }
3142
3143 return ret;
3144}
3145
3146/**
3147 * @brief Compile information about list's uniques.
3148 * @param[in] ctx Compile context.
3149 * @param[in] uniques Sized array list of unique statements.
3150 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3151 * @return LY_ERR value.
3152 */
3153static LY_ERR
3154lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
3155{
3156 LY_ERR ret = LY_SUCCESS;
3157 struct lysc_node_leaf **key, ***unique;
3158 struct lysc_node *parent;
3159 const char *keystr, *delim;
3160 size_t len;
3161 LY_ARRAY_COUNT_TYPE v;
3162 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
3163 uint16_t flags;
3164
3165 LY_ARRAY_FOR(uniques, v) {
3166 config = -1;
3167 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3168 keystr = uniques[v].str;
3169 while (keystr) {
3170 delim = strpbrk(keystr, " \t\n");
3171 if (delim) {
3172 len = delim - keystr;
3173 while (isspace(*delim)) {
3174 ++delim;
3175 }
3176 } else {
3177 len = strlen(keystr);
3178 }
3179
3180 /* unique node must be present */
3181 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko56d8da82021-12-16 15:41:30 +01003182 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, LY_VALUE_SCHEMA, (void *)uniques[v].mod,
3183 LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003184 if (ret != LY_SUCCESS) {
3185 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003186 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003187 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01003188 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003189 }
3190 return LY_EVALID;
3191 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003192 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003193 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003194 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003195 return LY_EVALID;
3196 }
3197
3198 /* all referenced leafs must be of the same config type */
3199 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3200 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003201 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003202 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3203 return LY_EVALID;
3204 } else if ((*key)->flags & LYS_CONFIG_W) {
3205 config = 1;
3206 } else { /* LYS_CONFIG_R */
3207 config = 0;
3208 }
3209
3210 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3211 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3212 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003213 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003214 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3215 return LY_EVALID;
3216 }
3217 }
3218
3219 /* check status */
Michal Vaskoc130e162021-10-19 11:30:00 +02003220 LY_CHECK_RET(lysc_check_status(ctx, list->flags, uniques[v].mod->mod, list->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003221 (*key)->flags, (*key)->module, (*key)->name));
3222
3223 /* mark leaf as unique */
3224 (*key)->flags |= LYS_UNIQUE;
3225
3226 /* next unique value in line */
3227 keystr = delim;
3228 }
3229 /* next unique definition */
3230 }
3231
3232 return LY_SUCCESS;
3233}
3234
3235/**
3236 * @brief Compile parsed list node information.
3237 * @param[in] ctx Compile context
3238 * @param[in] pnode Parsed list node.
3239 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3240 * is enriched with the list-specific information.
3241 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3242 */
3243static LY_ERR
3244lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3245{
3246 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3247 struct lysc_node_list *list = (struct lysc_node_list *)node;
3248 struct lysp_node *child_p;
Michal Vasko2d6507a2022-03-16 09:40:52 +01003249 struct lysc_node *parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003250 struct lysc_node_leaf *key, *prev_key = NULL;
3251 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003252 const char *keystr, *delim;
3253 LY_ERR ret = LY_SUCCESS;
3254
3255 list->min = list_p->min;
3256 if (list->min) {
3257 list->flags |= LYS_MAND_TRUE;
3258 }
3259 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3260
3261 LY_LIST_FOR(list_p->child, child_p) {
3262 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3263 }
3264
Michal Vasko5347e3a2020-11-03 17:14:57 +01003265 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003266
3267 /* add must(s) to unres */
3268 ret = lysc_unres_must_add(ctx, node, pnode);
3269 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003270
3271 /* keys */
Michal Vasko2d6507a2022-03-16 09:40:52 +01003272 if (list->flags & LYS_CONFIG_W) {
3273 parent = node;
3274 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
3275 /* compiling individual grouping, we can check this only if there is an explicit config set */
3276 while (parent) {
3277 if (parent->flags & LYS_SET_CONFIG) {
3278 break;
3279 }
3280 parent = parent->parent;
3281 }
3282 }
3283
3284 if (parent && (!list_p->key || !list_p->key[0])) {
3285 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3286 return LY_EVALID;
3287 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003288 }
3289
3290 /* find all the keys (must be direct children) */
3291 keystr = list_p->key;
3292 if (!keystr) {
3293 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003294 list->flags &= ~LYS_ORDBY_SYSTEM;
3295 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003296 }
3297 while (keystr) {
3298 delim = strpbrk(keystr, " \t\n");
3299 if (delim) {
3300 len = delim - keystr;
3301 while (isspace(*delim)) {
3302 ++delim;
3303 }
3304 } else {
3305 len = strlen(keystr);
3306 }
3307
3308 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003309 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3310 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003311 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003312 return LY_EVALID;
3313 }
3314 /* keys must be unique */
3315 if (key->flags & LYS_KEY) {
3316 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003317 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003318 return LY_EVALID;
3319 }
3320
Radek Krejcia6016992021-03-03 10:13:41 +01003321 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003322 /* key must have the same config flag as the list itself */
3323 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003324 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of a configuration list must not be a state leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003325 return LY_EVALID;
3326 }
3327 if (ctx->pmod->version < LYS_VERSION_1_1) {
3328 /* YANG 1.0 denies key to be of empty type */
3329 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003330 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003331 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3332 return LY_EVALID;
3333 }
3334 } else {
3335 /* when and if-feature are illegal on list keys */
3336 if (key->when) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003337 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List's key must not have any \"when\" statement.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003338 return LY_EVALID;
3339 }
Michal Vasko93b4ab92022-05-13 12:29:59 +02003340 /* unable to check if-features but compilation would fail if disabled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003341 }
3342
3343 /* check status */
Michal Vaskoc636ea42022-09-16 10:20:31 +02003344 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name, key->flags, key->module, key->name));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003345
3346 /* ignore default values of the key */
3347 if (key->dflt) {
3348 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003349 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)key->dflt->realtype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003350 free(key->dflt);
3351 key->dflt = NULL;
3352 }
3353 /* mark leaf as key */
3354 key->flags |= LYS_KEY;
3355
3356 /* move it to the correct position */
3357 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3358 /* fix links in closest previous siblings of the key */
3359 if (key->next) {
3360 key->next->prev = key->prev;
3361 } else {
3362 /* last child */
3363 list->child->prev = key->prev;
3364 }
3365 if (key->prev->next) {
3366 key->prev->next = key->next;
3367 }
3368 /* fix links in the key */
3369 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003370 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003371 key->next = prev_key->next;
3372 } else {
3373 key->prev = list->child->prev;
3374 key->next = list->child;
3375 }
3376 /* fix links in closes future siblings of the key */
3377 if (prev_key) {
3378 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003379 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003380 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003381 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003382 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003383 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003384 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003385 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003386 }
3387 /* fix links in parent */
3388 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003389 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003390 }
3391 }
3392
3393 /* next key value */
3394 prev_key = key;
3395 keystr = delim;
3396 lysc_update_path(ctx, NULL, NULL);
3397 }
3398
Michal Vasko95f736c2022-06-08 12:03:31 +02003399 /* connect any augments */
3400 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3401
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003402 /* uniques */
3403 if (list_p->uniques) {
3404 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3405 }
3406
Radek Krejci2a9fc652021-01-22 17:44:34 +01003407 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3408 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3409 LY_CHECK_GOTO(ret, done);
3410 }
3411 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3412 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3413 LY_CHECK_GOTO(ret, done);
3414 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003415
3416 /* checks */
3417 if (list->min > list->max) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003418 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 +02003419 return LY_EVALID;
3420 }
3421
3422done:
3423 return ret;
3424}
3425
3426/**
3427 * @brief Do some checks and set the default choice's case.
3428 *
3429 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3430 *
3431 * @param[in] ctx Compile context.
3432 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3433 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3434 * @return LY_ERR value.
3435 */
3436static LY_ERR
3437lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3438{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003439 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003440 const struct lys_module *mod;
3441 const char *prefix = NULL, *name;
3442 size_t prefix_len = 0;
3443
3444 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3445 name = strchr(dflt->str, ':');
3446 if (name) {
3447 prefix = dflt->str;
3448 prefix_len = name - prefix;
3449 ++name;
3450 } else {
3451 name = dflt->str;
3452 }
3453 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003454 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003455 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003456 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003457 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003458 return LY_EVALID;
3459 }
3460 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003461 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003462 }
3463
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003464 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 +02003465 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003466 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003467 "Default case \"%s\" not found.", dflt->str);
3468 return LY_EVALID;
3469 }
3470
3471 /* no mandatory nodes directly under the default case */
3472 LY_LIST_FOR(ch->dflt->child, iter) {
3473 if (iter->parent != (struct lysc_node *)ch->dflt) {
3474 break;
3475 }
3476 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003477 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003478 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3479 return LY_EVALID;
3480 }
3481 }
3482
3483 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003484 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003485 return LY_EVALID;
3486 }
3487
3488 ch->dflt->flags |= LYS_SET_DFLT;
3489 return LY_SUCCESS;
3490}
3491
3492LY_ERR
3493lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3494 struct ly_set *child_set)
3495{
3496 LY_ERR ret = LY_SUCCESS;
3497 struct lysp_node *child_p_next = child_p->next;
3498 struct lysp_node_case *cs_p;
Michal Vasko4eb49d52022-02-17 15:05:00 +01003499 struct lysc_node_case *cs_c;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003500
3501 if (child_p->nodetype == LYS_CASE) {
3502 /* standard case under choice */
3503 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3504 } else {
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003505 /* we need the implicit case first, so create a fake parsed (shorthand) case */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003506 cs_p = calloc(1, sizeof *cs_p);
3507 cs_p->nodetype = LYS_CASE;
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003508 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003509 cs_p->child = child_p;
3510
3511 /* make the child the only case child */
3512 child_p->next = NULL;
3513
3514 /* compile it normally */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003515 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 +02003516
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003517 if (((struct lysc_node_choice *)node)->cases) {
Michal Vasko4eb49d52022-02-17 15:05:00 +01003518 /* find our case node */
3519 cs_c = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases;
3520 while (cs_c->name != cs_p->name) {
3521 cs_c = (struct lysc_node_case *)cs_c->next;
3522 assert(cs_c);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003523 }
aPiecekaa320c92021-05-21 07:34:24 +02003524
Michal Vasko4eb49d52022-02-17 15:05:00 +01003525 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
3526 /* compiled case node cannot point to his corresponding parsed node
3527 * because it exists temporarily so it must be set to NULL
3528 */
3529 assert(cs_c->priv == cs_p);
3530 cs_c->priv = NULL;
3531 }
3532
3533 /* status is copied from his child and not from his parent as usual. */
3534 if (cs_c->child) {
3535 cs_c->flags &= ~LYS_STATUS_MASK;
3536 cs_c->flags |= (LYS_STATUS_MASK & cs_c->child->flags);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003537 }
3538 } /* else it was removed by a deviation */
aPiecekaa320c92021-05-21 07:34:24 +02003539
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003540revert_sh_case:
3541 /* free the parsed shorthand case and correct pointers back */
aPiecekaa320c92021-05-21 07:34:24 +02003542 cs_p->child = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003543 lysp_node_free(&ctx->free_ctx, (struct lysp_node *)cs_p);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003544 child_p->next = child_p_next;
3545 }
3546
3547 return ret;
3548}
3549
3550/**
3551 * @brief Compile parsed choice node information.
3552 *
3553 * @param[in] ctx Compile context
3554 * @param[in] pnode Parsed choice node.
3555 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3556 * is enriched with the choice-specific information.
3557 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3558 */
3559static LY_ERR
3560lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3561{
3562 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3563 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3564 struct lysp_node *child_p;
3565 LY_ERR ret = LY_SUCCESS;
3566
3567 assert(node->nodetype == LYS_CHOICE);
3568
3569 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003570 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, child_p, node, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003571 }
3572
Michal Vasko95f736c2022-06-08 12:03:31 +02003573 /* connect any augments */
3574 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3575
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003576 /* default branch */
3577 if (ch_p->dflt.str) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003578 LY_CHECK_GOTO(ret = lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003579 }
3580
Michal Vasko95f736c2022-06-08 12:03:31 +02003581done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003582 return ret;
3583}
3584
3585/**
3586 * @brief Compile parsed anydata or anyxml node information.
3587 * @param[in] ctx Compile context
3588 * @param[in] pnode Parsed anydata or anyxml node.
3589 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3590 * is enriched with the any-specific information.
3591 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3592 */
3593static LY_ERR
3594lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3595{
3596 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3597 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003598 LY_ERR ret = LY_SUCCESS;
3599
Michal Vasko5347e3a2020-11-03 17:14:57 +01003600 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003601
3602 /* add must(s) to unres */
3603 ret = lysc_unres_must_add(ctx, node, pnode);
3604 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003605
Radek Krejci91531e12021-02-08 19:57:54 +01003606 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003607 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003608 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3609 }
3610done:
3611 return ret;
3612}
3613
3614/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003615 * @brief Prepare the case structure in choice node for the new data node.
3616 *
3617 * 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
3618 * created in the choice when the first child was processed.
3619 *
3620 * @param[in] ctx Compile context.
3621 * @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,
3622 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3623 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3624 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3625 * @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,
3626 * it is linked from the case structure only in case it is its first child.
3627 */
3628static LY_ERR
3629lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3630{
Michal Vasko95f736c2022-06-08 12:03:31 +02003631 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003632 struct lysp_node *child_p;
3633 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3634
3635 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3636 /* we have to add an implicit case node into the parent choice */
3637 } else if (pnode->nodetype == LYS_CASE) {
3638 /* explicit parent case */
3639 LY_LIST_FOR(cs_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003640 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003641 }
3642 } else {
3643 LOGINT_RET(ctx->ctx);
3644 }
3645
Michal Vasko95f736c2022-06-08 12:03:31 +02003646 /* connect any augments */
3647 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3648
3649done:
3650 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003651}
3652
3653void
3654lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3655{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003656 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003657
3658 if (add) { /* set flag */
3659 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3660 parent = parent->parent) {
3661 parent->flags |= LYS_MAND_TRUE;
3662 }
3663 } else { /* unset flag */
3664 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003665 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003666 if (iter->flags & LYS_MAND_TRUE) {
3667 /* there is another mandatory node */
3668 return;
3669 }
3670 }
3671 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3672 parent->flags &= ~LYS_MAND_TRUE;
3673 }
3674 }
3675}
3676
3677/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003678 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003679 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003680 * @param[in] name Name of the grouping to find,
3681 * @return NULL when there is no grouping with the specified name
3682 * @return Pointer to the grouping of the specified @p name.
3683 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003684static struct lysp_node_grp *
3685match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003686{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003687 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003688
Radek Krejci2a9fc652021-01-22 17:44:34 +01003689 LY_LIST_FOR(grps, grp) {
3690 if (!strcmp(grp->name, name)) {
3691 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003692 }
3693 }
3694
3695 return NULL;
3696}
3697
3698/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003699 * @brief Find grouping for a uses.
3700 *
3701 * @param[in] ctx Compile context.
3702 * @param[in] uses_p Parsed uses node.
3703 * @param[out] gpr_p Found grouping on success.
3704 * @param[out] grp_pmod Module of @p grp_p on success.
3705 * @return LY_ERR value.
3706 */
3707static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003708lys_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 +02003709 struct lysp_module **grp_pmod)
3710{
3711 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003712 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003713 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003714 const char *id, *name, *prefix, *local_pref;
3715 size_t prefix_len, name_len;
3716 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003717 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003718
3719 *grp_p = NULL;
3720 *grp_pmod = NULL;
3721
3722 /* search for the grouping definition */
3723 id = uses_p->name;
3724 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3725 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3726 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3727 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003728 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003729 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003730 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3731 found = ctx->pmod;
3732 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003733 }
3734 }
3735 } else {
3736 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003737 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003738 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003739 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003740 "Invalid prefix used for grouping reference.", uses_p->name);
3741 return LY_EVALID;
3742 }
3743 pmod = mod->parsed;
3744 }
3745
3746 if (!found) {
3747 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003748 if ((grp = match_grouping(pmod->groupings, name))) {
3749 found = pmod;
3750 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003751 /* ... and all the submodules */
3752 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003753 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3754 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003755 break;
3756 }
3757 }
3758 }
3759 }
3760 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003761 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003762 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3763 return LY_EVALID;
3764 }
3765
Michal Vasko7c565922021-06-10 14:58:27 +02003766 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003767 /* remember that the grouping is instantiated to avoid its standalone validation */
3768 grp->flags |= LYS_USED_GRP;
3769 }
3770
3771 *grp_p = grp;
3772 *grp_pmod = found;
3773 return LY_SUCCESS;
3774}
3775
3776/**
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003777 * @brief Compile uses grouping children.
3778 *
3779 * @param[in] ctx Compile context.
3780 * @param[in] uses_p Parsed uses.
3781 * @param[in] uses_flags Special uses flags to use.
3782 * @param[in] child First grouping child to compile.
3783 * @param[in] grp_mod Grouping parsed module.
3784 * @param[in] parent Uses compiled parent, may be NULL if top-level.
3785 * @param[in,out] child_set Set of all compiled child nodes.
3786 * @param[in] child_unres_disabled Whether the children are to be put into unres disabled set or not.
3787 * @return LY_SUCCESS on success.
3788 * @return LY_EVALID on failure.
3789 */
3790static LY_ERR
3791lys_compile_uses_children(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, uint16_t uses_flags, struct lysp_node *child,
3792 struct lysp_module *grp_mod, struct lysc_node *parent, struct ly_set *child_set, ly_bool child_unres_disabled)
3793{
3794 LY_ERR rc = LY_SUCCESS;
3795 struct lysp_module *mod_old = ctx->pmod;
3796 uint32_t child_i, opt_prev = ctx->compile_opts;
3797 ly_bool enabled;
3798 struct lysp_node *pnode;
3799 struct lysc_node *node;
3800 struct lysc_when *when_shared = NULL;
3801
3802 assert(child_set);
3803
Michal Vasko5940c302022-07-14 13:54:38 +02003804 child_i = child_set->count;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003805 LY_LIST_FOR(child, pnode) {
3806 /* compile the nodes with their parsed (grouping) module */
3807 ctx->pmod = grp_mod;
3808 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, parent, uses_flags, child_set), cleanup);
3809
3810 /* eval if-features again for the rest of this node processing */
3811 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
3812 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3813 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3814 }
3815
3816 /* restore the parsed module */
3817 ctx->pmod = mod_old;
3818
3819 /* since the uses node is not present in the compiled tree, we need to pass some of its
3820 * statements to all its children */
3821 while (child_i < child_set->count) {
3822 node = child_set->snodes[child_i];
3823
3824 if (uses_p->when) {
3825 /* pass uses when to all the children */
3826 rc = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), node, &when_shared);
3827 LY_CHECK_GOTO(rc, cleanup);
3828 }
3829
3830 if (child_unres_disabled) {
3831 /* child is disabled by the uses if-features */
3832 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
3833 }
3834
3835 /* child processed */
3836 ++child_i;
3837 }
3838
3839 /* next iter */
3840 ctx->compile_opts = opt_prev;
3841 }
3842
3843cleanup:
3844 ctx->compile_opts = opt_prev;
3845 return rc;
3846}
3847
3848/**
Michal Vaskodfd254d2021-06-24 09:24:59 +02003849 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
3850 */
3851#define LYS_STATUS_USES LYS_CONFIG_MASK
3852
3853/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003854 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3855 * If present, also apply uses's modificators.
3856 *
3857 * @param[in] ctx Compile context
3858 * @param[in] uses_p Parsed uses schema node.
3859 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3860 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3861 * the compile context.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003862 * @param[in] child_set Optional set of all the compiled children.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003863 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3864 */
3865static LY_ERR
3866lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3867{
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003868 LY_ERR rc = LY_SUCCESS;
3869 ly_bool enabled, child_unres_disabled = 0;
3870 uint32_t i, grp_stack_count, opt_prev = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003871 struct lysp_node_grp *grp = NULL;
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003872 uint16_t uses_flags;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003873 struct lysp_module *grp_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003874 struct ly_set uses_child_set = {0};
3875
3876 /* find the referenced grouping */
3877 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3878
3879 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3880 grp_stack_count = ctx->groupings.count;
3881 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3882 if (grp_stack_count == ctx->groupings.count) {
3883 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003884 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003885 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3886 return LY_EVALID;
3887 }
3888
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003889 /* nodetype checks */
3890 if (grp->actions && (parent && !lysc_node_actions_p(parent))) {
3891 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3892 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3893 parent->name, lys_nodetype2str(parent->nodetype));
3894 rc = LY_EVALID;
3895 goto cleanup;
3896 }
3897 if (grp->notifs && (parent && !lysc_node_notifs_p(parent))) {
3898 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3899 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3900 parent->name, lys_nodetype2str(parent->nodetype));
3901 rc = LY_EVALID;
3902 goto cleanup;
3903 }
3904
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003905 /* check status */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003906 rc = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3907 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003908
3909 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003910 rc = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3911 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003912
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003913 /* compile special uses status flags */
3914 uses_flags = uses_p->flags;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003915 rc = lys_compile_status(ctx, &uses_flags, "<uses>", parent ? parent->flags : 0, parent ? parent->name : NULL, 0);
3916 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003917 uses_flags |= LYS_STATUS_USES;
3918
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003919 /* uses if-features */
3920 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, uses_p->iffeatures, &enabled), cleanup);
3921 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3922 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3923 child_unres_disabled = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003924 }
3925
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003926 /* uses grouping children */
3927 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, grp->child, grp_mod, parent,
3928 child_set ? child_set : &uses_child_set, child_unres_disabled);
3929 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003930
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003931 /* uses grouping RPCs/actions */
3932 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->actions, grp_mod, parent,
3933 child_set ? child_set : &uses_child_set, child_unres_disabled);
3934 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003935
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003936 /* uses grouping notifications */
3937 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->notifs, grp_mod, parent,
3938 child_set ? child_set : &uses_child_set, child_unres_disabled);
3939 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003940
3941 /* check that all augments were applied */
3942 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003943 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3944 /* augment of some parent uses, irrelevant now */
3945 continue;
3946 }
3947
3948 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003949 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003950 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003951 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003952 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003953
3954 /* check that all refines were applied */
3955 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003956 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003957 /* refine of some parent uses, irrelevant now */
Michal Vaskod8655722021-01-12 15:20:36 +01003958 continue;
3959 }
3960
3961 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003962 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003963 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003964 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003965 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003966
3967cleanup:
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003968 /* restore previous context */
3969 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003970
3971 /* remove the grouping from the stack for circular groupings dependency check */
3972 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3973 assert(ctx->groupings.count == grp_stack_count);
3974
3975 ly_set_erase(&uses_child_set, NULL);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003976 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003977}
3978
3979static int
3980lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3981{
3982 struct lysp_node *iter;
3983 int len = 0;
3984
3985 *path = NULL;
3986 for (iter = node; iter && len >= 0; iter = iter->parent) {
3987 char *s = *path;
3988 char *id;
3989
3990 switch (iter->nodetype) {
3991 case LYS_USES:
3992 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3993 break;
3994 case LYS_GROUPING:
3995 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3996 break;
3997 case LYS_AUGMENT:
3998 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3999 break;
4000 default:
4001 id = strdup(iter->name);
4002 break;
4003 }
4004
4005 if (!iter->parent) {
4006 /* print prefix */
4007 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
4008 } else {
4009 /* prefix is the same as in parent */
4010 len = asprintf(path, "/%s%s", id, s ? s : "");
4011 }
4012 free(s);
4013 free(id);
4014 }
4015
4016 if (len < 0) {
4017 free(*path);
4018 *path = NULL;
4019 } else if (len == 0) {
4020 *path = strdup("/");
4021 len = 1;
4022 }
4023 return len;
4024}
4025
4026LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01004027lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004028{
4029 LY_ERR ret;
4030 char *path;
4031 int len;
Michal Vaskoecdc2222022-01-24 08:45:44 +01004032 uint16_t cont_flags;
4033
4034 cont_flags = pnode ? pnode->flags & LYS_FLAGS_COMPILED_MASK : 0;
4035 if (!(cont_flags & LYS_CONFIG_MASK)) {
4036 /* default config */
4037 cont_flags |= LYS_CONFIG_W;
4038 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004039
Michal Vasko8254d852022-04-25 10:05:59 +02004040 /* use grouping status to avoid errors */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004041 struct lysp_node_uses fake_uses = {
4042 .parent = pnode,
4043 .nodetype = LYS_USES,
Michal Vasko8254d852022-04-25 10:05:59 +02004044 .flags = grp->flags & LYS_STATUS_MASK, .next = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004045 .name = grp->name,
4046 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4047 .refines = NULL, .augments = NULL
4048 };
4049 struct lysc_node_container fake_container = {
4050 .nodetype = LYS_CONTAINER,
Michal Vaskoecdc2222022-01-24 08:45:44 +01004051 .flags = cont_flags,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004052 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02004053 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01004054 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004055 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004056 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004057 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4058 };
4059
4060 if (grp->parent) {
4061 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4062 }
4063
4064 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
4065 if (len < 0) {
4066 LOGMEM(ctx->ctx);
4067 return LY_EMEM;
4068 }
4069 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
4070 ctx->path_len = (uint32_t)len;
4071 free(path);
4072
4073 lysc_update_path(ctx, NULL, "{grouping}");
4074 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01004075 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004076 lysc_update_path(ctx, NULL, NULL);
4077 lysc_update_path(ctx, NULL, NULL);
4078
4079 ctx->path_len = 1;
4080 ctx->path[1] = '\0';
4081
4082 /* cleanup */
Michal Vaskoc636ea42022-09-16 10:20:31 +02004083 lysc_node_container_free(&ctx->free_ctx, &fake_container);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004084
4085 return ret;
4086}
4087
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004088LY_ERR
4089lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
4090 struct ly_set *child_set)
4091{
4092 LY_ERR ret = LY_SUCCESS;
4093 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02004094 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004095
4096 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
4097
4098 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01004099 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004100 } else {
4101 lysc_update_path(ctx, NULL, "{uses}");
4102 lysc_update_path(ctx, NULL, pnode->name);
4103 }
4104
4105 switch (pnode->nodetype) {
4106 case LYS_CONTAINER:
4107 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
4108 node_compile_spec = lys_compile_node_container;
4109 break;
4110 case LYS_LEAF:
4111 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
4112 node_compile_spec = lys_compile_node_leaf;
4113 break;
4114 case LYS_LIST:
4115 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
4116 node_compile_spec = lys_compile_node_list;
4117 break;
4118 case LYS_LEAFLIST:
4119 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
4120 node_compile_spec = lys_compile_node_leaflist;
4121 break;
4122 case LYS_CHOICE:
4123 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
4124 node_compile_spec = lys_compile_node_choice;
4125 break;
4126 case LYS_CASE:
4127 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
4128 node_compile_spec = lys_compile_node_case;
4129 break;
4130 case LYS_ANYXML:
4131 case LYS_ANYDATA:
4132 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
4133 node_compile_spec = lys_compile_node_any;
4134 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004135 case LYS_RPC:
4136 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02004137 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004138 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4139 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004140 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004141 return LY_EVALID;
4142 }
4143 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
4144 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02004145 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004146 break;
4147 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02004148 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004149 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4150 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004151 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004152 return LY_EVALID;
4153 }
4154 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
4155 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02004156 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004157 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004158 case LYS_USES:
4159 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
4160 lysc_update_path(ctx, NULL, NULL);
4161 lysc_update_path(ctx, NULL, NULL);
4162 return ret;
4163 default:
4164 LOGINT(ctx->ctx);
4165 return LY_EINT;
4166 }
4167 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4168
Radek Krejci2a9fc652021-01-22 17:44:34 +01004169 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004170
Michal Vasko7c565922021-06-10 14:58:27 +02004171 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004172 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004173 return ret;
4174}