blob: 0b64dcb9e1e795496364cd85b1aeb45fc10f1ee5 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_node.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
Michal Vasko0b50f6b2022-10-05 15:07:55 +02004 * @author Michal Vasko <mvasko@cesnet.cz>
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02005 * @brief Schema compilation of common nodes.
6 *
Michal Vasko0b50f6b2022-10-05 15:07:55 +02007 * Copyright (c) 2015 - 2022 CESNET, z.s.p.o.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02008 *
9 * This source code is licensed under BSD 3-Clause License (the "License").
10 * You may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * https://opensource.org/licenses/BSD-3-Clause
14 */
15
Christian Hopps32874e12021-05-01 09:43:54 -040016#define _GNU_SOURCE /* asprintf, strdup */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020017
18#include "schema_compile_node.h"
19
20#include <assert.h>
21#include <ctype.h>
22#include <stddef.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "common.h"
29#include "compat.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020030#include "dict.h"
31#include "log.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020032#include "plugins.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 Vasko4ed3bd52022-12-02 10:28:04 +010056 * @brief Add a node with a when to unres.
57 *
58 * @param[in] ctx Compile context.
59 * @param[in] when Specific compiled when to check.
60 * @param[in] node Compiled node with when(s).
61 * @return LY_ERR value.
62 */
63static LY_ERR
64lysc_unres_when_add(struct lysc_ctx *ctx, struct lysc_when *when, struct lysc_node *node)
65{
66 LY_ERR rc = LY_SUCCESS;
67 struct lysc_unres_when *w = NULL;
68
69 /* do not check must(s) in a grouping */
70 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
71 goto cleanup;
72 }
73
74 /* add new unres when */
75 w = calloc(1, sizeof *w);
76 LY_CHECK_ERR_GOTO(!w, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
77
78 w->node = node;
79 w->when = when;
80
81 /* add into the unres set */
82 LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->whens, w, 1, NULL), LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
83 w = NULL;
84
85cleanup:
86 free(w);
87 return rc;
88}
89
90/**
Michal Vaskoc130e162021-10-19 11:30:00 +020091 * @brief Add a node with must(s) to unres.
92 *
93 * @param[in] ctx Compile context.
94 * @param[in] node Compiled node with must(s).
95 * @param[in] pnode Parsed ndoe with must(s).
96 * @return LY_ERR value.
97 */
98static LY_ERR
99lysc_unres_must_add(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_node *pnode)
100{
101 struct lysc_unres_must *m = NULL;
102 LY_ARRAY_COUNT_TYPE u;
103 struct lysc_must *musts;
104 struct lysp_restr *pmusts;
105 LY_ERR ret;
106
107 /* do not check must(s) in a grouping */
108 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
109 return LY_SUCCESS;
110 }
111
112 musts = lysc_node_musts(node);
113 pmusts = lysp_node_musts(pnode);
114 assert(LY_ARRAY_COUNT(musts) == LY_ARRAY_COUNT(pmusts));
115
116 if (!musts) {
117 /* no must */
118 return LY_SUCCESS;
119 }
120
121 /* add new unres must */
122 m = calloc(1, sizeof *m);
123 LY_CHECK_ERR_GOTO(!m, ret = LY_EMEM, error);
124 m->node = node;
125
126 /* add must local modules */
127 LY_ARRAY_CREATE_GOTO(ctx->ctx, m->local_mods, LY_ARRAY_COUNT(pmusts), ret, error);
128 LY_ARRAY_FOR(pmusts, u) {
129 m->local_mods[u] = pmusts[u].arg.mod;
130 LY_ARRAY_INCREMENT(m->local_mods);
131 }
132
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200133 /* store ext */
134 m->ext = ctx->ext;
135
Michal Vaskoc130e162021-10-19 11:30:00 +0200136 LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->musts, m, 1, NULL), ret = LY_EMEM, error);
137
138 return LY_SUCCESS;
139
140error:
141 if (m) {
142 LY_ARRAY_FREE(m->local_mods);
143 free(m);
144 }
145 LOGMEM(ctx->ctx);
146 return ret;
147}
148
149static LY_ERR
150lysc_unres_leafref_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const struct lysp_module *local_mod)
151{
152 struct lysc_unres_leafref *l = NULL;
153 struct ly_set *leafrefs_set;
154 LY_ARRAY_COUNT_TYPE u;
155 int is_lref = 0;
156
157 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
158 /* do not check leafrefs in groupings */
159 return LY_SUCCESS;
160 }
161
162 /* use special set for disabled leafrefs */
163 leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs;
164
165 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
166 /* leafref */
167 is_lref = 1;
168 } else if (leaf->type->basetype == LY_TYPE_UNION) {
169 /* union with leafrefs */
170 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
171 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
172 is_lref = 1;
173 break;
174 }
175 }
176 }
177
178 if (is_lref) {
179 /* add new unresolved leafref node */
180 l = calloc(1, sizeof *l);
181 LY_CHECK_ERR_RET(!l, LOGMEM(ctx->ctx), LY_EMEM);
182
183 l->node = &leaf->node;
184 l->local_mod = local_mod;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200185 l->ext = ctx->ext;
Michal Vaskoc130e162021-10-19 11:30:00 +0200186
187 LY_CHECK_ERR_RET(ly_set_add(leafrefs_set, l, 1, NULL), free(l); LOGMEM(ctx->ctx), LY_EMEM);
188 }
189
190 return LY_SUCCESS;
191}
192
193/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200194 * @brief Add/replace a leaf default value in unres.
195 * Can also be used for a single leaf-list default value.
196 *
197 * @param[in] ctx Compile context.
198 * @param[in] leaf Leaf with the default value.
199 * @param[in] dflt Default value to use.
200 * @return LY_ERR value.
201 */
202static LY_ERR
203lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
204{
205 struct lysc_unres_dflt *r = NULL;
206 uint32_t i;
207
Michal Vasko7c565922021-06-10 14:58:27 +0200208 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100209 return LY_SUCCESS;
210 }
211
Michal Vasko405cc9e2020-12-01 12:01:27 +0100212 for (i = 0; i < ctx->unres->dflts.count; ++i) {
213 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200214 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100215 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200216 lysp_qname_free(ctx->ctx, r->dflt);
217 free(r->dflt);
218 break;
219 }
220 }
221 if (!r) {
222 /* add new unres item */
223 r = calloc(1, sizeof *r);
224 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
225 r->leaf = leaf;
226
Michal Vasko405cc9e2020-12-01 12:01:27 +0100227 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200228 }
229
230 r->dflt = malloc(sizeof *r->dflt);
231 LY_CHECK_GOTO(!r->dflt, error);
Michal Vaskoc621d862022-11-08 14:23:45 +0100232 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, dflt, r->dflt), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200233
234 return LY_SUCCESS;
235
236error:
237 free(r->dflt);
238 LOGMEM(ctx->ctx);
239 return LY_EMEM;
240}
241
242/**
243 * @brief Add/replace a leaf-list default value(s) in unres.
244 *
245 * @param[in] ctx Compile context.
246 * @param[in] llist Leaf-list with the default value.
247 * @param[in] dflts Sized array of the default values.
248 * @return LY_ERR value.
249 */
250static LY_ERR
251lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
252{
253 struct lysc_unres_dflt *r = NULL;
254 uint32_t i;
255
Michal Vasko7c565922021-06-10 14:58:27 +0200256 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100257 return LY_SUCCESS;
258 }
259
Michal Vasko405cc9e2020-12-01 12:01:27 +0100260 for (i = 0; i < ctx->unres->dflts.count; ++i) {
261 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200262 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100263 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200264 lysp_qname_free(ctx->ctx, r->dflt);
265 free(r->dflt);
266 r->dflt = NULL;
267 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
268 r->dflts = NULL;
269 break;
270 }
271 }
272 if (!r) {
273 r = calloc(1, sizeof *r);
274 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
275 r->llist = llist;
276
Michal Vasko405cc9e2020-12-01 12:01:27 +0100277 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200278 }
279
280 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
281
282 return LY_SUCCESS;
283}
284
285/**
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100286 * @brief Add a bits/enumeration type to unres.
287 *
288 * @param[in] ctx Compile context.
289 * @param[in] leaf Leaf of type bits/enumeration whose disabled items to free.
290 * @return LY_ERR value.
291 */
292static LY_ERR
293lysc_unres_bitenum_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf)
294{
295 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
296 /* skip groupings and redundant for disabled nodes */
297 return LY_SUCCESS;
298 }
299
300 LY_CHECK_RET(ly_set_add(&ctx->unres->disabled_bitenums, leaf, 1, NULL));
301
302 return LY_SUCCESS;
303}
304
305/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200306 * @brief Duplicate the compiled pattern structure.
307 *
308 * Instead of duplicating memory, the reference counter in the @p orig is increased.
309 *
310 * @param[in] orig The pattern structure to duplicate.
311 * @return The duplicated structure to use.
312 */
313static struct lysc_pattern *
314lysc_pattern_dup(struct lysc_pattern *orig)
315{
316 ++orig->refcount;
317 return orig;
318}
319
320/**
321 * @brief Duplicate the array of compiled patterns.
322 *
323 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
324 *
325 * @param[in] ctx Libyang context for logging.
326 * @param[in] orig The patterns sized array to duplicate.
327 * @return New sized array as a copy of @p orig.
328 * @return NULL in case of memory allocation error.
329 */
330static struct lysc_pattern **
331lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
332{
333 struct lysc_pattern **dup = NULL;
334 LY_ARRAY_COUNT_TYPE u;
335
336 assert(orig);
337
338 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
339 LY_ARRAY_FOR(orig, u) {
340 dup[u] = lysc_pattern_dup(orig[u]);
341 LY_ARRAY_INCREMENT(dup);
342 }
343 return dup;
344}
345
346/**
347 * @brief Duplicate compiled range structure.
348 *
349 * @param[in] ctx Libyang context for logging.
350 * @param[in] orig The range structure to be duplicated.
351 * @return New compiled range structure as a copy of @p orig.
352 * @return NULL in case of memory allocation error.
353 */
354static struct lysc_range *
355lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
356{
357 struct lysc_range *dup;
358 LY_ERR ret;
359
360 assert(orig);
361
362 dup = calloc(1, sizeof *dup);
363 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
364 if (orig->parts) {
365 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100366 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200367 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
368 }
369 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
370 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
371 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
372
373 return dup;
374cleanup:
375 free(dup);
376 (void) ret; /* set but not used due to the return type */
377 return NULL;
378}
379
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200380/**
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200381 * @brief Print status into a string.
382 *
383 * @param[in] flags Flags with the status to print.
384 * @return String status.
385 */
386static const char *
387lys_status2str(uint16_t flags)
388{
389 flags &= LYS_STATUS_MASK;
390
391 switch (flags) {
392 case 0:
393 case LYS_STATUS_CURR:
394 return "current";
395 case LYS_STATUS_DEPRC:
396 return "deprecated";
397 case LYS_STATUS_OBSLT:
398 return "obsolete";
399 default:
400 LOGINT(NULL);
401 return NULL;
402 }
403}
404
405/**
Michal Vaskodfd254d2021-06-24 09:24:59 +0200406 * @brief Compile status information of the given statement.
407 *
408 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
409 * has the status correctly set during the compilation.
410 *
411 * @param[in] ctx Compile context
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200412 * @param[in] parsed_flags Parsed statement flags.
413 * @param[in] inherited_flags Parsed inherited flags from a schema-only statement (augment, uses, ext instance, ...).
414 * @param[in] parent_flags Compiled parent node flags.
Michal Vaskodfd254d2021-06-24 09:24:59 +0200415 * @param[in] parent_name Name of the parent node, for logging.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200416 * @param[in] stmt_name Statement name, for logging.
417 * @param[in,out] stmt_flags Statement flags with the correct status set.
Michal Vaskodfd254d2021-06-24 09:24:59 +0200418 * @return LY_ERR value.
419 */
420static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200421lys_compile_status(struct lysc_ctx *ctx, uint16_t parsed_flags, uint16_t inherited_flags, uint16_t parent_flags,
422 const char *parent_name, const char *stmt_name, uint16_t *stmt_flags)
Michal Vaskodfd254d2021-06-24 09:24:59 +0200423{
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200424 /* normalize to status-only */
425 parsed_flags &= LYS_STATUS_MASK;
426 inherited_flags &= LYS_STATUS_MASK;
427 parent_flags &= LYS_STATUS_MASK;
428
429 /* check for conflicts */
430 if (parent_flags && parsed_flags && (parent_flags > parsed_flags)) {
431 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Status \"%s\" of \"%s\" is in conflict with \"%s\" status of parent \"%s\".",
432 lys_status2str(parsed_flags), stmt_name, lys_status2str(parent_flags), parent_name);
433 return LY_EVALID;
434 } else if (inherited_flags && parsed_flags && (inherited_flags > parsed_flags)) {
435 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Inherited schema-only status \"%s\" is in conflict with \"%s\" status of \"%s\".",
436 lys_status2str(inherited_flags), lys_status2str(parsed_flags), stmt_name);
437 return LY_EVALID;
438 } else if (parent_flags && inherited_flags && (parent_flags > inherited_flags)) {
439 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Status \"%s\" of parent \"%s\" is in conflict with inherited schema-only status \"%s\".",
440 lys_status2str(parent_flags), parent_name, lys_status2str(inherited_flags));
441 return LY_EVALID;
Michal Vaskodfd254d2021-06-24 09:24:59 +0200442 }
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200443
444 /* clear */
445 (*stmt_flags) &= ~LYS_STATUS_MASK;
446
447 if (parsed_flags) {
448 /* explicit status */
449 (*stmt_flags) |= parsed_flags;
450 } else if (inherited_flags) {
451 /* inherited status from a schema-only statement */
452 (*stmt_flags) |= inherited_flags;
453 } else if (parent_flags) {
454 /* inherited status from a parent node */
455 (*stmt_flags) |= parent_flags;
456 } else {
457 /* default status */
458 (*stmt_flags) |= LYS_STATUS_CURR;
459 }
460
Michal Vaskodfd254d2021-06-24 09:24:59 +0200461 return LY_SUCCESS;
462}
463
464/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200465 * @brief Compile information from the when statement
466 *
467 * @param[in] ctx Compile context.
468 * @param[in] when_p Parsed when structure.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200469 * @param[in] inherited_flags Inherited flags from a schema-only statement.
470 * @param[in] parent Parent node, if any.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200471 * @param[in] ctx_node Context node for the when statement.
472 * @param[out] when Pointer where to store pointer to the created compiled when structure.
473 * @return LY_ERR value.
474 */
475static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200476lys_compile_when_(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t inherited_flags,
477 const struct lysc_node *parent, const struct lysc_node *ctx_node, struct lysc_when **when)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200478{
479 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200480 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200481
482 *when = calloc(1, sizeof **when);
483 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
484 (*when)->refcount = 1;
485 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200486 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci8df109d2021-04-23 12:19:08 +0200487 LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200488 (*when)->context = (struct lysc_node *)ctx_node;
489 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
490 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100491 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200492 LY_CHECK_RET(lys_compile_status(ctx, 0, inherited_flags, parent ? parent->flags : 0, parent ? parent->name : NULL,
493 "when", &(*when)->flags));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200494
495done:
496 return ret;
497}
498
499LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200500lys_compile_when(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t inherited_flags, const struct lysc_node *parent,
501 const struct lysc_node *ctx_node, struct lysc_node *node, struct lysc_when **when_c)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200502{
Michal Vasko193dacd2022-10-13 08:43:05 +0200503 LY_ERR rc = LY_SUCCESS;
504 struct lysc_when **new_when, ***node_when, *ptr;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200505
Michal Vasko193dacd2022-10-13 08:43:05 +0200506 assert(when_p && (node || when_c));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200507
Michal Vasko193dacd2022-10-13 08:43:05 +0200508 if (node) {
509 /* get the when array */
510 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200511
Michal Vasko193dacd2022-10-13 08:43:05 +0200512 /* create new when pointer */
513 LY_ARRAY_NEW_GOTO(ctx->ctx, *node_when, new_when, rc, cleanup);
514 } else {
515 /* individual when */
516 new_when = &ptr;
517 *new_when = calloc(1, sizeof **new_when);
518 LY_CHECK_ERR_GOTO(!*new_when, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
519 }
520
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200521 if (!when_c || !(*when_c)) {
522 /* compile when */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +0200523 LY_CHECK_GOTO(rc = lys_compile_when_(ctx, when_p, inherited_flags, parent, ctx_node, new_when), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200524
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200525 /* remember the compiled when for sharing */
526 if (when_c) {
527 *when_c = *new_when;
528 }
529 } else {
530 /* use the previously compiled when */
531 ++(*when_c)->refcount;
532 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100533 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200534
Michal Vasko4ed3bd52022-12-02 10:28:04 +0100535 /* add when to unres */
536 LY_CHECK_GOTO(rc = lysc_unres_when_add(ctx, *new_when, node), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200537
Michal Vasko193dacd2022-10-13 08:43:05 +0200538cleanup:
539 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200540}
541
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200542LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200543lys_compile_must(struct lysc_ctx *ctx, const struct lysp_restr *must_p, struct lysc_must *must)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200544{
545 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200546 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200547
548 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200549 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 +0200550 LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200551 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
552 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
553 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
554 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100555 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200556
557done:
558 return ret;
559}
560
561/**
562 * @brief Validate and normalize numeric value from a range definition.
563 * @param[in] ctx Compile context.
564 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
565 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
566 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
567 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
568 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
569 * @param[in] value String value of the range boundary.
570 * @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.
571 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
572 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
573 */
574static LY_ERR
575range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
576 size_t *len, char **valcopy)
577{
578 size_t fraction = 0, size;
579
580 *len = 0;
581
582 assert(value);
583 /* parse value */
584 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
585 return LY_EVALID;
586 }
587
588 if ((value[*len] == '-') || (value[*len] == '+')) {
589 ++(*len);
590 }
591
592 while (isdigit(value[*len])) {
593 ++(*len);
594 }
595
596 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
597 if (basetype == LY_TYPE_DEC64) {
598 goto decimal;
599 } else {
600 *valcopy = strndup(value, *len);
601 return LY_SUCCESS;
602 }
603 }
604 fraction = *len;
605
606 ++(*len);
607 while (isdigit(value[*len])) {
608 ++(*len);
609 }
610
611 if (basetype == LY_TYPE_DEC64) {
612decimal:
613 assert(frdigits);
614 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100615 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200616 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100617 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200618 return LY_EINVAL;
619 }
620 if (fraction) {
621 size = (*len) + (frdigits - ((*len) - 1 - fraction));
622 } else {
623 size = (*len) + frdigits + 1;
624 }
625 *valcopy = malloc(size * sizeof **valcopy);
626 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
627
628 (*valcopy)[size - 1] = '\0';
629 if (fraction) {
630 memcpy(&(*valcopy)[0], &value[0], fraction);
631 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
632 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
633 } else {
634 memcpy(&(*valcopy)[0], &value[0], *len);
635 memset(&(*valcopy)[*len], '0', frdigits);
636 }
637 }
638 return LY_SUCCESS;
639}
640
641/**
642 * @brief Check that values in range are in ascendant order.
643 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
644 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
645 * max can be also equal.
646 * @param[in] value Current value to check.
647 * @param[in] prev_value The last seen value.
648 * @return LY_SUCCESS or LY_EEXIST for invalid order.
649 */
650static LY_ERR
651range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
652{
653 if (unsigned_value) {
654 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
655 return LY_EEXIST;
656 }
657 } else {
658 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
659 return LY_EEXIST;
660 }
661 }
662 return LY_SUCCESS;
663}
664
665/**
666 * @brief Set min/max value of the range part.
667 * @param[in] ctx Compile context.
668 * @param[in] part Range part structure to fill.
669 * @param[in] max Flag to distinguish if storing min or max value.
670 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
671 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
672 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
673 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
674 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
675 * @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.
676 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
677 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
678 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
679 * frdigits value), LY_EMEM.
680 */
681static LY_ERR
682range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
683 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
684{
685 LY_ERR ret = LY_SUCCESS;
686 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100687 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200688
689 if (value) {
690 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
691 LY_CHECK_GOTO(ret, finalize);
692 }
693 if (!valcopy && base_range) {
694 if (max) {
695 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
696 } else {
697 part->min_64 = base_range->parts[0].min_64;
698 }
699 if (!first) {
700 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
701 }
702 goto finalize;
703 }
704
705 switch (basetype) {
706 case LY_TYPE_INT8: /* range */
707 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100708 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 +0200709 } else if (max) {
710 part->max_64 = INT64_C(127);
711 } else {
712 part->min_64 = INT64_C(-128);
713 }
714 if (!ret && !first) {
715 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
716 }
717 break;
718 case LY_TYPE_INT16: /* range */
719 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100720 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
721 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200722 } else if (max) {
723 part->max_64 = INT64_C(32767);
724 } else {
725 part->min_64 = INT64_C(-32768);
726 }
727 if (!ret && !first) {
728 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
729 }
730 break;
731 case LY_TYPE_INT32: /* range */
732 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100733 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
734 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200735 } else if (max) {
736 part->max_64 = INT64_C(2147483647);
737 } else {
738 part->min_64 = INT64_C(-2147483648);
739 }
740 if (!ret && !first) {
741 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
742 }
743 break;
744 case LY_TYPE_INT64: /* range */
745 case LY_TYPE_DEC64: /* range */
746 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100747 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
748 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200749 } else if (max) {
750 part->max_64 = INT64_C(9223372036854775807);
751 } else {
752 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
753 }
754 if (!ret && !first) {
755 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
756 }
757 break;
758 case LY_TYPE_UINT8: /* range */
759 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100760 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 +0200761 } else if (max) {
762 part->max_u64 = UINT64_C(255);
763 } else {
764 part->min_u64 = UINT64_C(0);
765 }
766 if (!ret && !first) {
767 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
768 }
769 break;
770 case LY_TYPE_UINT16: /* range */
771 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100772 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 +0200773 } else if (max) {
774 part->max_u64 = UINT64_C(65535);
775 } else {
776 part->min_u64 = UINT64_C(0);
777 }
778 if (!ret && !first) {
779 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
780 }
781 break;
782 case LY_TYPE_UINT32: /* range */
783 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100784 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
785 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200786 } else if (max) {
787 part->max_u64 = UINT64_C(4294967295);
788 } else {
789 part->min_u64 = UINT64_C(0);
790 }
791 if (!ret && !first) {
792 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
793 }
794 break;
795 case LY_TYPE_UINT64: /* range */
796 case LY_TYPE_STRING: /* length */
797 case LY_TYPE_BINARY: /* length */
798 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100799 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
800 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200801 } else if (max) {
802 part->max_u64 = UINT64_C(18446744073709551615);
803 } else {
804 part->min_u64 = UINT64_C(0);
805 }
806 if (!ret && !first) {
807 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
808 }
809 break;
810 default:
811 LOGINT(ctx->ctx);
812 ret = LY_EINT;
813 }
814
815finalize:
816 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100817 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200818 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
819 length_restr ? "length" : "range", valcopy ? valcopy : *value);
820 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100821 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200822 "Invalid %s restriction - invalid value \"%s\".",
823 length_restr ? "length" : "range", valcopy ? valcopy : *value);
824 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100825 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200826 "Invalid %s restriction - values are not in ascending order (%s).",
827 length_restr ? "length" : "range",
828 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
829 } else if (!ret && value) {
830 *value = *value + len;
831 }
832 free(valcopy);
833 return ret;
834}
835
Michal Vasko193dacd2022-10-13 08:43:05 +0200836LY_ERR
837lys_compile_type_range(struct lysc_ctx *ctx, const struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200838 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
839{
aPiecek1c4da362021-04-29 14:26:34 +0200840 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200841 const char *expr;
842 struct lysc_range_part *parts = NULL, *part;
843 ly_bool range_expected = 0, uns;
844 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
845
846 assert(range);
847 assert(range_p);
848
849 expr = range_p->arg.str;
850 while (1) {
851 if (isspace(*expr)) {
852 ++expr;
853 } else if (*expr == '\0') {
854 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100855 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200856 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200857 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200858 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200859 goto cleanup;
860 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100861 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200862 "Invalid %s restriction - unexpected end of the expression (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200863 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200864 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200865 goto cleanup;
866 }
867 parts_done++;
868 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100869 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200870 if (parts) {
871 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100872 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200873 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100874 (int)(expr - range_p->arg.str), range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200875 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200876 goto cleanup;
877 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100878 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200879
880 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200881 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 +0200882 part->max_64 = part->min_64;
883 } else if (*expr == '|') {
884 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100885 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200886 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200887 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200888 goto cleanup;
889 }
890 expr++;
891 parts_done++;
892 /* process next part of the expression */
893 } else if (!strncmp(expr, "..", 2)) {
894 expr += 2;
895 while (isspace(*expr)) {
896 expr++;
897 }
898 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100899 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200900 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
aPiecek1c4da362021-04-29 14:26:34 +0200901 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200902 goto cleanup;
903 }
904 /* continue expecting the upper boundary */
905 range_expected = 1;
906 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
907 /* number */
908 if (range_expected) {
909 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200910 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 +0200911 range_expected = 0;
912 } else {
913 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200914 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 +0200915 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
916 part->max_64 = part->min_64;
917 }
918
919 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100920 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
921 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200922 while (isspace(*expr)) {
923 expr++;
924 }
925 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100926 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200927 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200928 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200929 goto cleanup;
930 }
931 if (range_expected) {
932 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200933 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 +0200934 range_expected = 0;
935 } else {
936 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200937 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 +0200938 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
939 part->min_64 = part->max_64;
940 }
941 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100942 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200943 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200944 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200945 goto cleanup;
946 }
947 }
948
949 /* check with the previous range/length restriction */
950 if (base_range) {
951 switch (basetype) {
952 case LY_TYPE_BINARY:
953 case LY_TYPE_UINT8:
954 case LY_TYPE_UINT16:
955 case LY_TYPE_UINT32:
956 case LY_TYPE_UINT64:
957 case LY_TYPE_STRING:
958 uns = 1;
959 break;
960 case LY_TYPE_DEC64:
961 case LY_TYPE_INT8:
962 case LY_TYPE_INT16:
963 case LY_TYPE_INT32:
964 case LY_TYPE_INT64:
965 uns = 0;
966 break;
967 default:
968 LOGINT(ctx->ctx);
969 ret = LY_EINT;
970 goto cleanup;
971 }
972 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
973 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
974 goto baseerror;
975 }
976 /* current lower bound is not lower than the base */
977 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
978 /* base has single value */
979 if (base_range->parts[v].min_64 == parts[u].min_64) {
980 /* both lower bounds are the same */
981 if (parts[u].min_64 != parts[u].max_64) {
982 /* current continues with a range */
983 goto baseerror;
984 } else {
985 /* equal single values, move both forward */
986 ++v;
987 continue;
988 }
989 } else {
990 /* base is single value lower than current range, so the
991 * value from base range is removed in the current,
992 * move only base and repeat checking */
993 ++v;
994 --u;
995 continue;
996 }
997 } else {
998 /* base is the range */
999 if (parts[u].min_64 == parts[u].max_64) {
1000 /* current is a single value */
1001 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
1002 /* current is behind the base range, so base range is omitted,
1003 * move the base and keep the current for further check */
1004 ++v;
1005 --u;
1006 } /* else it is within the base range, so move the current, but keep the base */
1007 continue;
1008 } else {
1009 /* both are ranges - check the higher bound, the lower was already checked */
1010 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
1011 /* higher bound is higher than the current higher bound */
1012 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
1013 /* but the current lower bound is also higher, so the base range is omitted,
1014 * continue with the same current, but move the base */
1015 --u;
1016 ++v;
1017 continue;
1018 }
1019 /* current range starts within the base range but end behind it */
1020 goto baseerror;
1021 } else {
1022 /* current range is smaller than the base,
1023 * move current, but stay with the base */
1024 continue;
1025 }
1026 }
1027 }
1028 }
1029 if (u != parts_done) {
1030baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001031 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001032 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
Michal Vasko20c0b9f2021-08-24 08:16:27 +02001033 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +02001034 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001035 goto cleanup;
1036 }
1037 }
1038
1039 if (!(*range)) {
1040 *range = calloc(1, sizeof **range);
1041 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1042 }
1043
1044 /* we rewrite the following values as the types chain is being processed */
1045 if (range_p->eapptag) {
1046 lydict_remove(ctx->ctx, (*range)->eapptag);
1047 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
1048 }
1049 if (range_p->emsg) {
1050 lydict_remove(ctx->ctx, (*range)->emsg);
1051 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
1052 }
1053 if (range_p->dsc) {
1054 lydict_remove(ctx->ctx, (*range)->dsc);
1055 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
1056 }
1057 if (range_p->ref) {
1058 lydict_remove(ctx->ctx, (*range)->ref);
1059 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
1060 }
1061 /* extensions are taken only from the last range by the caller */
1062
1063 (*range)->parts = parts;
1064 parts = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001065cleanup:
1066 LY_ARRAY_FREE(parts);
1067
1068 return ret;
1069}
1070
Michal Vasko215d7fc2022-07-15 14:50:44 +02001071/**
1072 * @brief Transform characters block in an XML Schema pattern into Perl character ranges.
1073 *
1074 * @param[in] ctx libyang context.
1075 * @param[in] pattern Original pattern.
1076 * @param[in,out] regex Pattern to modify.
1077 * @return LY_ERR value.
1078 */
1079static LY_ERR
1080lys_compile_pattern_chblocks_xmlschema2perl(const struct ly_ctx *ctx, const char *pattern, char **regex)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001081{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001082#define URANGE_LEN 19
1083 char *ublock2urange[][2] = {
1084 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1085 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1086 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1087 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1088 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1089 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1090 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1091 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1092 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1093 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1094 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1095 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1096 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1097 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1098 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1099 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1100 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1101 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1102 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1103 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1104 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1105 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1106 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1107 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1108 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1109 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1110 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1111 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1112 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1113 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1114 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1115 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1116 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1117 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1118 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1119 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1120 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1121 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1122 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1123 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1124 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1125 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1126 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1127 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1128 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1129 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1130 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1131 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1132 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1133 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1134 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1135 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1136 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1137 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1138 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1139 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1140 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1141 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1142 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1143 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1144 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1145 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1146 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1147 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1148 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1149 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1150 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1151 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1152 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1153 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1154 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1155 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1156 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1157 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1158 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1159 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1160 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1161 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1162 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1163 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1164 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1165 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1166 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +01001167 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001168 {NULL, NULL}
1169 };
1170
Michal Vasko215d7fc2022-07-15 14:50:44 +02001171 size_t idx, idx2, start, end;
1172 char *perl_regex, *ptr;
1173
1174 perl_regex = *regex;
1175
1176 /* substitute Unicode Character Blocks with exact Character Ranges */
1177 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1178 start = ptr - perl_regex;
1179
1180 ptr = strchr(ptr, '}');
1181 if (!ptr) {
1182 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
1183 return LY_EVALID;
1184 }
1185 end = (ptr - perl_regex) + 1;
1186
1187 /* need more space */
1188 if (end - start < URANGE_LEN) {
1189 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko78419922022-07-15 15:15:20 +02001190 *regex = perl_regex;
Michal Vasko215d7fc2022-07-15 14:50:44 +02001191 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1192 }
1193
1194 /* find our range */
1195 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1196 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1197 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1198 break;
1199 }
1200 }
1201 if (!ublock2urange[idx][0]) {
1202 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
1203 return LY_EVALID;
1204 }
1205
1206 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1207 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1208 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1209 ++idx;
1210 }
1211 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1212 --idx;
1213 }
1214 }
1215 if (idx) {
1216 /* skip brackets */
1217 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1218 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1219 } else {
1220 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1221 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1222 }
1223 }
1224
Michal Vasko215d7fc2022-07-15 14:50:44 +02001225 return LY_SUCCESS;
1226}
1227
1228LY_ERR
1229lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
1230{
1231 size_t idx, size, brack;
1232 char *perl_regex;
1233 int err_code, compile_opts;
1234 const char *orig_ptr;
1235 PCRE2_SIZE err_offset;
1236 pcre2_code *code_local;
aPiecek9e716992022-07-21 16:29:47 +02001237 ly_bool escaped;
Michal Vasko215d7fc2022-07-15 14:50:44 +02001238 LY_ERR r;
1239
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001240 /* adjust the expression to a Perl equivalent
1241 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1242
1243 /* allocate space for the transformed pattern */
1244 size = strlen(pattern) + 1;
Michal Vasko03d430c2022-07-22 09:05:56 +02001245 compile_opts = PCRE2_UTF | PCRE2_UCP | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
Christian Hoppsdafed222021-03-22 13:02:42 -04001246#ifdef PCRE2_ENDANCHORED
1247 compile_opts |= PCRE2_ENDANCHORED;
1248#else
1249 /* add space for trailing $ anchor */
1250 size++;
1251#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001252 perl_regex = malloc(size);
1253 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1254 perl_regex[0] = '\0';
1255
1256 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1257 brack = 0;
1258 idx = 0;
aPiecek9e716992022-07-21 16:29:47 +02001259 escaped = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001260 orig_ptr = pattern;
1261 while (orig_ptr[0]) {
1262 switch (orig_ptr[0]) {
1263 case '$':
1264 case '^':
1265 if (!brack) {
1266 /* make space for the extra character */
1267 ++size;
1268 perl_regex = ly_realloc(perl_regex, size);
1269 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1270
1271 /* print escape slash */
1272 perl_regex[idx] = '\\';
1273 ++idx;
1274 }
1275 break;
aPiecek9e716992022-07-21 16:29:47 +02001276 case '\\':
1277 /* escape character found or backslash is escaped */
1278 escaped = !escaped;
1279 /* copy backslash and continue with the next character */
1280 perl_regex[idx] = orig_ptr[0];
1281 ++idx;
1282 ++orig_ptr;
1283 continue;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001284 case '[':
aPiecek9e716992022-07-21 16:29:47 +02001285 if (!escaped) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001286 ++brack;
1287 }
1288 break;
1289 case ']':
aPiecek9e716992022-07-21 16:29:47 +02001290 if (!brack && !escaped) {
1291 /* If ']' does not terminate a character class expression, then pcre2_compile() implicitly escapes the
1292 * ']' character. But this seems to be against the regular expressions rules declared in
1293 * "XML schema: Datatypes" and therefore an error is returned. So for example if pattern is '\[a]' then
1294 * pcre2 match characters '[a]' literally but in YANG such pattern is not allowed.
1295 */
1296 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, orig_ptr, "character group doesn't begin with '['");
1297 free(perl_regex);
1298 return LY_EVALID;
1299 } else if (!escaped) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001300 --brack;
1301 }
1302 break;
1303 default:
1304 break;
1305 }
1306
1307 /* copy char */
1308 perl_regex[idx] = orig_ptr[0];
1309
1310 ++idx;
1311 ++orig_ptr;
aPiecek9e716992022-07-21 16:29:47 +02001312 escaped = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001313 }
Christian Hoppsdafed222021-03-22 13:02:42 -04001314#ifndef PCRE2_ENDANCHORED
1315 /* anchor match to end of subject */
1316 perl_regex[idx++] = '$';
1317#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001318 perl_regex[idx] = '\0';
1319
Michal Vasko215d7fc2022-07-15 14:50:44 +02001320 /* transform character blocks */
1321 if ((r = lys_compile_pattern_chblocks_xmlschema2perl(ctx, pattern, &perl_regex))) {
1322 free(perl_regex);
1323 return r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001324 }
1325
1326 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001327 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001328 &err_code, &err_offset, NULL);
1329 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001330 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
Michal Vasko2bf4af42023-01-04 12:08:38 +01001331
Radek Krejcif13b87b2020-12-01 22:02:17 +01001332 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001333 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001334 free(perl_regex);
1335 return LY_EVALID;
1336 }
1337 free(perl_regex);
1338
1339 if (code) {
1340 *code = code_local;
1341 } else {
1342 free(code_local);
1343 }
1344
1345 return LY_SUCCESS;
1346
1347#undef URANGE_LEN
1348}
1349
Michal Vasko51de7b72022-04-29 09:50:22 +02001350LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001351lys_compile_type_patterns(struct lysc_ctx *ctx, const struct lysp_restr *patterns_p, struct lysc_pattern **base_patterns,
1352 struct lysc_pattern ***patterns)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001353{
1354 struct lysc_pattern **pattern;
1355 LY_ARRAY_COUNT_TYPE u;
1356 LY_ERR ret = LY_SUCCESS;
1357
1358 /* first, copy the patterns from the base type */
1359 if (base_patterns) {
1360 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1361 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1362 }
1363
1364 LY_ARRAY_FOR(patterns_p, u) {
1365 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1366 *pattern = calloc(1, sizeof **pattern);
1367 ++(*pattern)->refcount;
1368
Radek Krejci2efc45b2020-12-22 16:25:44 +01001369 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001370 LY_CHECK_RET(ret);
1371
Radek Krejcif13b87b2020-12-01 22:02:17 +01001372 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001373 (*pattern)->inverted = 1;
1374 }
1375 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1376 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1377 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1378 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1379 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001380 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001381 }
1382done:
1383 return ret;
1384}
1385
1386/**
1387 * @brief map of the possible restrictions combination for the specific built-in type.
1388 */
1389static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1390 0 /* LY_TYPE_UNKNOWN */,
1391 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1392 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1393 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1394 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1395 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1396 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1397 LYS_SET_BIT /* LY_TYPE_BITS */,
1398 0 /* LY_TYPE_BOOL */,
1399 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1400 0 /* LY_TYPE_EMPTY */,
1401 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1402 LYS_SET_BASE /* LY_TYPE_IDENT */,
1403 LYS_SET_REQINST /* LY_TYPE_INST */,
1404 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1405 LYS_SET_TYPE /* LY_TYPE_UNION */,
1406 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1407 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1408 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1409 LYS_SET_RANGE /* LY_TYPE_INT64 */
1410};
1411
1412/**
1413 * @brief stringification of the YANG built-in data types
1414 */
1415const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001416 LY_TYPE_UNKNOWN_STR,
1417 LY_TYPE_BINARY_STR,
1418 LY_TYPE_UINT8_STR,
1419 LY_TYPE_UINT16_STR,
1420 LY_TYPE_UINT32_STR,
1421 LY_TYPE_UINT64_STR,
1422 LY_TYPE_STRING_STR,
1423 LY_TYPE_BITS_STR,
1424 LY_TYPE_BOOL_STR,
1425 LY_TYPE_DEC64_STR,
1426 LY_TYPE_EMPTY_STR,
1427 LY_TYPE_ENUM_STR,
1428 LY_TYPE_IDENT_STR,
1429 LY_TYPE_INST_STR,
1430 LY_TYPE_LEAFREF_STR,
1431 LY_TYPE_UNION_STR,
1432 LY_TYPE_INT8_STR,
1433 LY_TYPE_INT16_STR,
1434 LY_TYPE_INT32_STR,
1435 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001436};
1437
Michal Vasko193dacd2022-10-13 08:43:05 +02001438LY_ERR
1439lys_compile_type_enums(struct lysc_ctx *ctx, const struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001440 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001441{
1442 LY_ERR ret = LY_SUCCESS;
1443 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001444 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1445 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001446 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001447 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001448
1449 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001450 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001451 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1452 return LY_EVALID;
1453 }
1454
1455 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001456 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001457 if (base_enums) {
1458 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1459 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001460 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001461 break;
1462 }
1463 }
1464 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001465 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001466 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001467 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001468 return LY_EVALID;
1469 }
1470 match = v;
1471 }
1472
1473 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001474 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001475 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001476 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001477 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001478 LY_ARRAY_FOR(*bitenums, v) {
1479 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001480 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001481 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001482 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001483 return LY_EVALID;
1484 }
1485 }
1486 } else if (base_enums) {
1487 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001488 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001489 } else {
1490 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001491 if (u == 0) {
1492 cur_val = 0;
1493 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001494 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001495 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001496 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001497 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001498 } else {
1499 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001500 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001501 }
1502
1503 /* save highest value for auto assing */
1504 if (highest_value < cur_val) {
1505 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001506 }
1507 } else { /* LY_TYPE_BITS */
1508 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001509 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001510 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001511 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001512 LY_ARRAY_FOR(*bitenums, v) {
1513 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001514 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001515 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001516 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001517 return LY_EVALID;
1518 }
1519 }
1520 } else if (base_enums) {
1521 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001522 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001523 } else {
1524 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001525 if (u == 0) {
1526 cur_pos = 0;
1527 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001528 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001529 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001530 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001531 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001532 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001533 } else {
1534 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001535 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001536 }
1537
1538 /* save highest position for auto assing */
1539 if (highest_position < cur_pos) {
1540 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001541 }
1542 }
1543
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001544 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001545 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001546 if (basetype == LY_TYPE_ENUM) {
1547 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001548 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001549 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001550 enums_p[u].name, base_enums[match].value, cur_val);
1551 return LY_EVALID;
1552 }
1553 } else {
1554 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001555 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001556 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001557 enums_p[u].name, base_enums[match].position, cur_pos);
1558 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001559 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001560 }
1561 }
1562
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001563 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001564 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001565 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1566 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1567 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001568 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 +01001569 if (basetype == LY_TYPE_ENUM) {
1570 e->value = cur_val;
1571 } else {
1572 e->position = cur_pos;
1573 }
Radek Krejciab430862021-03-02 20:13:40 +01001574 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001575
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001576 /* evaluate if-ffeatures */
1577 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1578 if (!enabled) {
1579 /* set only flag, later resolved and removed */
1580 e->flags |= LYS_DISABLED;
1581 }
1582
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001583 if (basetype == LY_TYPE_BITS) {
1584 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001585 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001586 if (v != u) {
1587 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001588 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1589 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001590 }
1591 }
1592 }
1593
1594done:
1595 return ret;
1596}
1597
Radek Krejci6a205692020-12-09 13:58:22 +01001598static LY_ERR
1599lys_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 +01001600 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001601{
1602 LY_ERR ret = LY_SUCCESS;
1603 struct lysc_type **utypes = *utypes_p;
1604 struct lysc_type_union *un_aux = NULL;
1605
1606 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1607 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001608 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1609 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001610 LY_CHECK_GOTO(ret, error);
1611 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1612 /* add space for additional types from the union subtype */
1613 un_aux = (struct lysc_type_union *)utypes[u + additional];
1614 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1615 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1616
1617 /* copy subtypes of the subtype union */
1618 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1619 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1620 struct lysc_type_leafref *lref;
1621
1622 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1623 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1624 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1625 lref = (struct lysc_type_leafref *)utypes[u + additional];
1626
1627 lref->basetype = LY_TYPE_LEAFREF;
Michal Vaskoe33134a2022-07-29 14:54:40 +02001628 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 +01001629 LY_CHECK_GOTO(ret, error);
Michal Vasko080064b2021-08-31 15:20:44 +02001630 lref->refcount = 1;
Radek Krejci6a205692020-12-09 13:58:22 +01001631 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001632 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001633 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001634 LY_CHECK_GOTO(ret, error);
1635 /* TODO extensions */
1636
1637 } else {
1638 utypes[u + additional] = un_aux->types[v];
Michal Vasko04338d92021-09-01 07:58:14 +02001639 LY_ATOMIC_INC_BARRIER(un_aux->types[v]->refcount);
Radek Krejci6a205692020-12-09 13:58:22 +01001640 }
1641 ++additional;
1642 LY_ARRAY_INCREMENT(utypes);
1643 }
1644 /* compensate u increment in main loop */
1645 --additional;
1646
1647 /* free the replaced union subtype */
Michal Vaskoc636ea42022-09-16 10:20:31 +02001648 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001649 un_aux = NULL;
1650 } else {
1651 LY_ARRAY_INCREMENT(utypes);
1652 }
1653 }
1654
1655 *utypes_p = utypes;
1656 return LY_SUCCESS;
1657
1658error:
1659 if (un_aux) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001660 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001661 }
1662 *utypes_p = utypes;
1663 return ret;
1664}
1665
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001666/**
1667 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1668 * @param[in] ctx Compile context.
1669 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1670 * @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 +02001671 * @param[in] context_name Name of the context node or referencing typedef for logging.
1672 * @param[in] type_p Parsed type to compile.
1673 * @param[in] basetype Base YANG built-in type of the type to compile.
1674 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1675 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1676 * @param[out] type Newly created type structure with the filled information about the type.
1677 * @return LY_ERR value.
1678 */
1679static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001680lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
Michal Vasko193dacd2022-10-13 08:43:05 +02001681 struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, const struct lysc_type *base,
1682 struct lysc_type **type)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001683{
1684 LY_ERR ret = LY_SUCCESS;
1685 struct lysc_type_bin *bin;
1686 struct lysc_type_num *num;
1687 struct lysc_type_str *str;
1688 struct lysc_type_bits *bits;
1689 struct lysc_type_enum *enumeration;
1690 struct lysc_type_dec *dec;
1691 struct lysc_type_identityref *idref;
1692 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001693 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001694
1695 switch (basetype) {
1696 case LY_TYPE_BINARY:
1697 bin = (struct lysc_type_bin *)(*type);
1698
1699 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1700 if (type_p->length) {
1701 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1702 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1703 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001704 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001705 }
1706 }
1707 break;
1708 case LY_TYPE_BITS:
1709 /* RFC 7950 9.7 - bits */
1710 bits = (struct lysc_type_bits *)(*type);
1711 if (type_p->bits) {
1712 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1713 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1714 (struct lysc_type_bitenum_item **)&bits->bits));
1715 }
1716
1717 if (!base && !type_p->flags) {
1718 /* type derived from bits built-in type must contain at least one bit */
1719 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001720 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001721 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001722 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001723 }
1724 return LY_EVALID;
1725 }
1726 break;
1727 case LY_TYPE_DEC64:
1728 dec = (struct lysc_type_dec *)(*type);
1729
1730 /* RFC 7950 9.3.4 - fraction-digits */
1731 if (!base) {
1732 if (!type_p->fraction_digits) {
1733 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001734 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001735 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001736 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001737 }
1738 return LY_EVALID;
1739 }
1740 dec->fraction_digits = type_p->fraction_digits;
1741 } else {
1742 if (type_p->fraction_digits) {
1743 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1744 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001745 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001746 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1747 tpdfname);
1748 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001749 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001750 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1751 }
1752 return LY_EVALID;
1753 }
1754 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1755 }
1756
1757 /* RFC 7950 9.2.4 - range */
1758 if (type_p->range) {
1759 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1760 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1761 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001762 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001763 }
1764 }
1765 break;
1766 case LY_TYPE_STRING:
1767 str = (struct lysc_type_str *)(*type);
1768
1769 /* RFC 7950 9.4.4 - length */
1770 if (type_p->length) {
1771 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1772 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1773 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001774 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001775 }
1776 } else if (base && ((struct lysc_type_str *)base)->length) {
1777 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1778 }
1779
1780 /* RFC 7950 9.4.5 - pattern */
1781 if (type_p->patterns) {
1782 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1783 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1784 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1785 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1786 }
1787 break;
1788 case LY_TYPE_ENUM:
1789 enumeration = (struct lysc_type_enum *)(*type);
1790
1791 /* RFC 7950 9.6 - enum */
1792 if (type_p->enums) {
1793 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1794 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1795 }
1796
1797 if (!base && !type_p->flags) {
1798 /* type derived from enumerations built-in type must contain at least one enum */
1799 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001800 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001801 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001802 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001803 }
1804 return LY_EVALID;
1805 }
1806 break;
1807 case LY_TYPE_INT8:
1808 case LY_TYPE_UINT8:
1809 case LY_TYPE_INT16:
1810 case LY_TYPE_UINT16:
1811 case LY_TYPE_INT32:
1812 case LY_TYPE_UINT32:
1813 case LY_TYPE_INT64:
1814 case LY_TYPE_UINT64:
1815 num = (struct lysc_type_num *)(*type);
1816
1817 /* RFC 6020 9.2.4 - range */
1818 if (type_p->range) {
1819 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1820 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1821 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001822 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001823 }
1824 }
1825 break;
1826 case LY_TYPE_IDENT:
1827 idref = (struct lysc_type_identityref *)(*type);
1828
1829 /* RFC 7950 9.10.2 - base */
1830 if (type_p->bases) {
1831 if (base) {
1832 /* only the directly derived identityrefs can contain base specification */
1833 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001834 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001835 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1836 tpdfname);
1837 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001838 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001839 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1840 }
1841 return LY_EVALID;
1842 }
aPiecekf4a0a192021-08-03 15:14:17 +02001843 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001844 }
1845
1846 if (!base && !type_p->flags) {
1847 /* type derived from identityref built-in type must contain at least one base */
1848 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001849 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001850 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001851 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001852 }
1853 return LY_EVALID;
1854 }
1855 break;
1856 case LY_TYPE_LEAFREF:
1857 lref = (struct lysc_type_leafref *)*type;
1858
1859 /* RFC 7950 9.9.3 - require-instance */
1860 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001861 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001862 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001863 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001864 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1865 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001866 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001867 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1868 }
1869 return LY_EVALID;
1870 }
1871 lref->require_instance = type_p->require_instance;
1872 } else if (base) {
1873 /* inherit */
1874 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1875 } else {
1876 /* default is true */
1877 lref->require_instance = 1;
1878 }
1879 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001880 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001881
Michal Vaskoe33134a2022-07-29 14:54:40 +02001882 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, 0, 0, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001883 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 +02001884 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001885 } else if (base) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02001886 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 +02001887 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001888 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001889 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001890 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001891 return LY_EVALID;
1892 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001893 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001894 return LY_EVALID;
1895 }
1896 break;
1897 case LY_TYPE_INST:
1898 /* RFC 7950 9.9.3 - require-instance */
1899 if (type_p->flags & LYS_SET_REQINST) {
1900 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1901 } else {
1902 /* default is true */
1903 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1904 }
1905 break;
1906 case LY_TYPE_UNION:
1907 un = (struct lysc_type_union *)(*type);
1908
1909 /* RFC 7950 7.4 - type */
1910 if (type_p->types) {
1911 if (base) {
1912 /* only the directly derived union can contain types specification */
1913 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001914 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001915 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1916 tpdfname);
1917 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001918 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001919 "Invalid type substatement for the type not directly derived from union built-in type.");
1920 }
1921 return LY_EVALID;
1922 }
1923 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001924 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 +02001925 }
1926
1927 if (!base && !type_p->flags) {
1928 /* type derived from union built-in type must contain at least one type */
1929 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001930 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001931 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001932 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001933 }
1934 return LY_EVALID;
1935 }
1936 break;
1937 case LY_TYPE_BOOL:
1938 case LY_TYPE_EMPTY:
1939 case LY_TYPE_UNKNOWN: /* just to complete switch */
1940 break;
1941 }
1942
1943 if (tpdfname) {
1944 switch (basetype) {
1945 case LY_TYPE_BINARY:
1946 type_p->compiled = *type;
1947 *type = calloc(1, sizeof(struct lysc_type_bin));
1948 break;
1949 case LY_TYPE_BITS:
1950 type_p->compiled = *type;
1951 *type = calloc(1, sizeof(struct lysc_type_bits));
1952 break;
1953 case LY_TYPE_DEC64:
1954 type_p->compiled = *type;
1955 *type = calloc(1, sizeof(struct lysc_type_dec));
1956 break;
1957 case LY_TYPE_STRING:
1958 type_p->compiled = *type;
1959 *type = calloc(1, sizeof(struct lysc_type_str));
1960 break;
1961 case LY_TYPE_ENUM:
1962 type_p->compiled = *type;
1963 *type = calloc(1, sizeof(struct lysc_type_enum));
1964 break;
1965 case LY_TYPE_INT8:
1966 case LY_TYPE_UINT8:
1967 case LY_TYPE_INT16:
1968 case LY_TYPE_UINT16:
1969 case LY_TYPE_INT32:
1970 case LY_TYPE_UINT32:
1971 case LY_TYPE_INT64:
1972 case LY_TYPE_UINT64:
1973 type_p->compiled = *type;
1974 *type = calloc(1, sizeof(struct lysc_type_num));
1975 break;
1976 case LY_TYPE_IDENT:
1977 type_p->compiled = *type;
1978 *type = calloc(1, sizeof(struct lysc_type_identityref));
1979 break;
1980 case LY_TYPE_LEAFREF:
1981 type_p->compiled = *type;
1982 *type = calloc(1, sizeof(struct lysc_type_leafref));
1983 break;
1984 case LY_TYPE_INST:
1985 type_p->compiled = *type;
1986 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1987 break;
1988 case LY_TYPE_UNION:
1989 type_p->compiled = *type;
1990 *type = calloc(1, sizeof(struct lysc_type_union));
1991 break;
1992 case LY_TYPE_BOOL:
1993 case LY_TYPE_EMPTY:
1994 case LY_TYPE_UNKNOWN: /* just to complete switch */
1995 break;
1996 }
1997 }
1998 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1999
2000cleanup:
2001 return ret;
2002}
2003
2004LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01002005lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
Michal Vasko193dacd2022-10-13 08:43:05 +02002006 const struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002007{
2008 LY_ERR ret = LY_SUCCESS;
2009 ly_bool dummyloops = 0;
2010 struct type_context {
2011 const struct lysp_tpdf *tpdf;
2012 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002013 } *tctx, *tctx_prev = NULL, *tctx_iter;
2014 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
2015 struct lysc_type *base = NULL, *prev_type;
2016 struct ly_set tpdf_chain = {0};
Michal Vasko388a6632021-08-06 11:27:43 +02002017 struct lyplg_type *plugin;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002018
2019 (*type) = NULL;
2020 if (dflt) {
2021 *dflt = NULL;
2022 }
2023
2024 tctx = calloc(1, sizeof *tctx);
2025 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002026 for (ret = lysp_type_find(type_p->name, context_pnode, type_p->pmod, ctx->ext, &basetype, &tctx->tpdf, &tctx->node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002027 ret == LY_SUCCESS;
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002028 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod, ctx->ext,
Michal Vaskoa99b3572021-02-01 11:54:58 +01002029 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002030 if (basetype) {
2031 break;
2032 }
2033
2034 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01002035 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
2036 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002037 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2038
2039 if (units && !*units) {
2040 /* inherit units */
2041 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
2042 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2043 }
2044 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
2045 /* inherit default */
2046 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
2047 }
2048 if (dummyloops && (!units || *units) && dflt && *dflt) {
2049 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2050 break;
2051 }
2052
Michal Vasko080064b2021-08-31 15:20:44 +02002053 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
Radek Krejci720d2612021-03-03 19:44:22 +01002054 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
2055 * and we need now recompile the type again in the updated context. */
Michal Vaskoc636ea42022-09-16 10:20:31 +02002056 lysc_type_free(&ctx->free_ctx, tctx->tpdf->type.compiled);
Radek Krejci720d2612021-03-03 19:44:22 +01002057 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
2058 }
2059
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002060 if (tctx->tpdf->type.compiled) {
2061 /* it is not necessary to continue, the rest of the chain was already compiled,
2062 * but we still may need to inherit default and units values, so start dummy loops */
2063 basetype = tctx->tpdf->type.compiled->basetype;
2064 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2065 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2066
2067 if ((units && !*units) || (dflt && !*dflt)) {
2068 dummyloops = 1;
2069 goto preparenext;
2070 } else {
2071 tctx = NULL;
2072 break;
2073 }
2074 }
2075
2076 /* circular typedef reference detection */
2077 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
2078 /* local part */
2079 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002080 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002081 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002082 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2083 free(tctx);
2084 ret = LY_EVALID;
2085 goto cleanup;
2086 }
2087 }
2088 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
2089 /* global part for unions corner case */
2090 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002091 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002092 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002093 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2094 free(tctx);
2095 ret = LY_EVALID;
2096 goto cleanup;
2097 }
2098 }
2099
2100 /* store information for the following processing */
2101 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2102 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2103
2104preparenext:
2105 /* prepare next loop */
2106 tctx_prev = tctx;
2107 tctx = calloc(1, sizeof *tctx);
2108 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2109 }
2110 free(tctx);
2111
2112 /* allocate type according to the basetype */
2113 switch (basetype) {
2114 case LY_TYPE_BINARY:
2115 *type = calloc(1, sizeof(struct lysc_type_bin));
2116 break;
2117 case LY_TYPE_BITS:
2118 *type = calloc(1, sizeof(struct lysc_type_bits));
2119 break;
2120 case LY_TYPE_BOOL:
2121 case LY_TYPE_EMPTY:
2122 *type = calloc(1, sizeof(struct lysc_type));
2123 break;
2124 case LY_TYPE_DEC64:
2125 *type = calloc(1, sizeof(struct lysc_type_dec));
2126 break;
2127 case LY_TYPE_ENUM:
2128 *type = calloc(1, sizeof(struct lysc_type_enum));
2129 break;
2130 case LY_TYPE_IDENT:
2131 *type = calloc(1, sizeof(struct lysc_type_identityref));
2132 break;
2133 case LY_TYPE_INST:
2134 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2135 break;
2136 case LY_TYPE_LEAFREF:
2137 *type = calloc(1, sizeof(struct lysc_type_leafref));
2138 break;
2139 case LY_TYPE_STRING:
2140 *type = calloc(1, sizeof(struct lysc_type_str));
2141 break;
2142 case LY_TYPE_UNION:
2143 *type = calloc(1, sizeof(struct lysc_type_union));
2144 break;
2145 case LY_TYPE_INT8:
2146 case LY_TYPE_UINT8:
2147 case LY_TYPE_INT16:
2148 case LY_TYPE_UINT16:
2149 case LY_TYPE_INT32:
2150 case LY_TYPE_UINT32:
2151 case LY_TYPE_INT64:
2152 case LY_TYPE_UINT64:
2153 *type = calloc(1, sizeof(struct lysc_type_num));
2154 break;
2155 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002156 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002157 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2158 ret = LY_EVALID;
2159 goto cleanup;
2160 }
2161 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
2162 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002163 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002164 ly_data_type2str[basetype]);
2165 free(*type);
2166 (*type) = NULL;
2167 ret = LY_EVALID;
2168 goto cleanup;
2169 }
2170
2171 /* get restrictions from the referred typedefs */
2172 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2173 tctx = (struct type_context *)tpdf_chain.objs[u];
2174
2175 /* remember the typedef context for circular check */
2176 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
2177 LY_CHECK_GOTO(ret, cleanup);
2178
2179 if (tctx->tpdf->type.compiled) {
Michal Vasko388a6632021-08-06 11:27:43 +02002180 /* already compiled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002181 base = tctx->tpdf->type.compiled;
2182 continue;
Michal Vasko388a6632021-08-06 11:27:43 +02002183 }
2184
Michal Vaskoddd76592022-01-17 13:34:48 +01002185 /* try to find loaded user type plugins */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002186 plugin = lyplg_type_plugin_find(tctx->tpdf->type.pmod->mod->name, tctx->tpdf->type.pmod->mod->revision,
Michal Vaskoddd76592022-01-17 13:34:48 +01002187 tctx->tpdf->name);
Haithem Ben Ghorbal45be15f2022-07-12 18:02:36 +02002188 if (!plugin && base) {
2189 /* use the base type implementation if available */
2190 plugin = base->plugin;
2191 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002192 if (!plugin) {
2193 /* use the internal built-in type implementation */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002194 plugin = lyplg_type_plugin_find("", NULL, ly_data_type2str[basetype]);
Michal Vaskoddd76592022-01-17 13:34:48 +01002195 }
Michal Vasko388a6632021-08-06 11:27:43 +02002196 assert(plugin);
2197
2198 if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags) &&
2199 (plugin == base->plugin)) {
2200 /* no change, reuse the compiled base */
2201 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002202 LY_ATOMIC_INC_BARRIER(base->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002203 continue;
2204 }
2205
Michal Vasko04338d92021-09-01 07:58:14 +02002206 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002207 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002208 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002209 tctx->tpdf->name, ly_data_type2str[basetype]);
2210 ret = LY_EVALID;
2211 goto cleanup;
2212 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002213 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002214 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2215 tctx->tpdf->name, tctx->tpdf->dflt.str);
2216 ret = LY_EVALID;
2217 goto cleanup;
2218 }
2219
2220 (*type)->basetype = basetype;
Michal Vasko388a6632021-08-06 11:27:43 +02002221 (*type)->plugin = plugin;
2222
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002223 /* collect extensions */
2224 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko388a6632021-08-06 11:27:43 +02002225
2226 /* compile the new typedef */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002227 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01002228 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002229 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
2230 LY_CHECK_GOTO(ret, cleanup);
2231 base = prev_type;
2232 }
2233 /* remove the processed typedef contexts from the stack for circular check */
2234 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
2235
2236 /* process the type definition in leaf */
2237 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
2238 /* get restrictions from the node itself */
2239 (*type)->basetype = basetype;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002240 (*type)->plugin = base ? base->plugin : lyplg_type_plugin_find("", NULL, ly_data_type2str[basetype]);
Michal Vasko04338d92021-09-01 07:58:14 +02002241 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko193dacd2022-10-13 08:43:05 +02002242 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, (struct lysp_type *)type_p, basetype,
2243 NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002244 LY_CHECK_GOTO(ret, cleanup);
2245 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
2246 /* no specific restriction in leaf's type definition, copy from the base */
2247 free(*type);
2248 (*type) = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002249 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002250 }
2251
Radek Krejciab430862021-03-02 20:13:40 +01002252 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002253
2254cleanup:
2255 ly_set_erase(&tpdf_chain, free);
2256 return ret;
2257}
2258
2259/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002260 * @brief Check uniqness of the node/action/notification name.
2261 *
2262 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2263 * structures, but they share the namespace so we need to check their name collisions.
2264 *
2265 * @param[in] ctx Compile context.
2266 * @param[in] parent Parent of the nodes to check, can be NULL.
2267 * @param[in] name Name of the item to find in the given lists.
2268 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2269 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2270 */
2271static LY_ERR
2272lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2273 const struct lysc_node *exclude)
2274{
2275 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002276 const struct lysc_node_action *actions;
2277 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002278 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002279 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002280
2281#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2282
2283 if (exclude->nodetype == LYS_CASE) {
2284 /* check restricted only to all the cases */
2285 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002286 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002287 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002288 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002289 return LY_EEXIST;
2290 }
2291 }
2292
2293 return LY_SUCCESS;
2294 }
2295
2296 /* no reason for our parent to be choice anymore */
2297 assert(!parent || (parent->nodetype != LYS_CHOICE));
2298
2299 if (parent && (parent->nodetype == LYS_CASE)) {
2300 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002301
2302 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2303 iter = lysc_data_parent(parent);
2304 do {
2305 parent = parent->parent;
2306 if (parent && (parent->nodetype == LYS_CHOICE)) {
2307 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2308 }
2309 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002310 }
2311
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002312 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002313 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2314 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2315 if (exclude->flags & LYS_IS_OUTPUT) {
2316 getnext_flags |= LYS_GETNEXT_OUTPUT;
2317 parent = lysc_node_child(parent)->next;
2318 } else {
2319 parent = lysc_node_child(parent);
2320 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002321 }
2322
2323 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002324 if (!parent && ctx->ext) {
2325 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2326 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2327 goto error;
2328 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002329
Radek Krejci5fa32a32021-02-08 18:12:38 +01002330 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2331 if (iter->nodetype == LYS_CHOICE) {
2332 iter2 = NULL;
2333 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2334 if (CHECK_NODE(iter2, exclude, name)) {
2335 goto error;
2336 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002337 }
2338 }
2339 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002340 } else {
2341 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2342 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2343 goto error;
2344 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002345
Radek Krejci5fa32a32021-02-08 18:12:38 +01002346 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2347 if (iter->nodetype == LYS_CHOICE) {
2348 iter2 = NULL;
2349 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2350 if (CHECK_NODE(iter2, exclude, name)) {
2351 goto error;
2352 }
2353 }
2354 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002355 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002356
Radek Krejci5fa32a32021-02-08 18:12:38 +01002357 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2358 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2359 if (CHECK_NODE(iter, exclude, name)) {
2360 goto error;
2361 }
2362 }
2363
2364 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2365 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2366 if (CHECK_NODE(iter, exclude, name)) {
2367 goto error;
2368 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002369 }
2370 }
Radek Krejci078e4342021-02-12 18:17:26 +01002371 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002372 return LY_SUCCESS;
2373
2374error:
Radek Krejci078e4342021-02-12 18:17:26 +01002375 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002376 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002377 return LY_EEXIST;
2378
2379#undef CHECK_NODE
2380}
2381
Michal Vasko193dacd2022-10-13 08:43:05 +02002382LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002383lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002384{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002385 struct lysc_node **children, *anchor = NULL;
2386 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002387
Radek Krejci2a9fc652021-01-22 17:44:34 +01002388 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002389
Radek Krejci2a9fc652021-01-22 17:44:34 +01002390 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002391 if (node->nodetype == LYS_INPUT) {
2392 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2393 /* input node is part of the action but link it with output */
2394 node->next = &((struct lysc_node_action *)parent)->output.node;
2395 node->prev = node->next;
2396 return LY_SUCCESS;
2397 } else if (node->nodetype == LYS_OUTPUT) {
2398 /* output node is part of the action but link it with input */
2399 node->next = NULL;
2400 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002401 return LY_SUCCESS;
2402 } else if (node->nodetype == LYS_ACTION) {
2403 children = (struct lysc_node **)lysc_node_actions_p(parent);
2404 } else if (node->nodetype == LYS_NOTIF) {
2405 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2406 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002407 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002408 }
2409 assert(children);
2410
2411 if (!(*children)) {
2412 /* first child */
2413 *children = node;
2414 } else if (node->flags & LYS_KEY) {
2415 /* special handling of adding keys */
2416 assert(node->module == parent->module);
2417 anchor = *children;
2418 if (anchor->flags & LYS_KEY) {
2419 while ((anchor->flags & LYS_KEY) && anchor->next) {
2420 anchor = anchor->next;
2421 }
2422 /* insert after the last key */
2423 insert_after = 1;
2424 } /* else insert before anchor (at the beginning) */
2425 } else if ((*children)->prev->module == node->module) {
2426 /* last child is from the same module, keep the order and insert at the end */
2427 anchor = (*children)->prev;
2428 insert_after = 1;
2429 } else if (parent->module == node->module) {
2430 /* adding module child after some augments were connected */
2431 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2432 } else {
2433 /* some augments are already connected and we are connecting new ones,
2434 * keep module name order and insert the node into the children list */
2435 anchor = *children;
2436 do {
2437 anchor = anchor->prev;
2438
2439 /* check that we have not found the last augment node from our module or
2440 * the first augment node from a "smaller" module or
2441 * the first node from a local module */
2442 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2443 (anchor->module == parent->module)) {
2444 /* insert after */
2445 insert_after = 1;
2446 break;
2447 }
2448
2449 /* we have traversed all the nodes, insert before anchor (as the first node) */
2450 } while (anchor->prev->next);
2451 }
2452
2453 /* insert */
2454 if (anchor) {
2455 if (insert_after) {
2456 node->next = anchor->next;
2457 node->prev = anchor;
2458 anchor->next = node;
2459 if (node->next) {
2460 /* middle node */
2461 node->next->prev = node;
2462 } else {
2463 /* last node */
2464 (*children)->prev = node;
2465 }
2466 } else {
2467 node->next = anchor;
2468 node->prev = anchor->prev;
2469 anchor->prev = node;
2470 if (anchor == *children) {
2471 /* first node */
2472 *children = node;
2473 } else {
2474 /* middle node */
2475 node->prev->next = node;
2476 }
2477 }
2478 }
2479
2480 /* check the name uniqueness (even for an only child, it may be in case) */
2481 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2482 return LY_EEXIST;
2483 }
2484 } else {
2485 /* top-level element */
2486 struct lysc_node **list;
2487
Radek Krejci6b88a462021-02-17 12:39:34 +01002488 if (ctx->ext) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002489 lyplg_ext_get_storage_p(ctx->ext, LY_STMT_DATA_NODE_MASK, (const void ***)&list);
Radek Krejci6b88a462021-02-17 12:39:34 +01002490 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002491 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2492 } else if (node->nodetype == LYS_NOTIF) {
2493 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2494 } else {
2495 list = &ctx->cur_mod->compiled->data;
2496 }
2497 if (!(*list)) {
2498 *list = node;
2499 } else {
2500 /* insert at the end of the module's top-level nodes list */
2501 (*list)->prev->next = node;
2502 node->prev = (*list)->prev;
2503 (*list)->prev = node;
2504 }
2505
2506 /* check the name uniqueness on top-level */
2507 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2508 return LY_EEXIST;
2509 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002510 }
2511
Radek Krejci2a9fc652021-01-22 17:44:34 +01002512 return LY_SUCCESS;
2513}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002514
Radek Krejci2a9fc652021-01-22 17:44:34 +01002515/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002516 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002517 *
2518 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002519 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002520 * @return LY_ERR value.
2521 */
2522static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002523lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002524{
2525 /* case never has any explicit config */
2526 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002527
Michal Vasko7c565922021-06-10 14:58:27 +02002528 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Radek Krejci91531e12021-02-08 19:57:54 +01002529 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002530 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002531 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2532 /* config not explicitly set, inherit it from parent */
Michal Vaskoecdc2222022-01-24 08:45:44 +01002533 assert(!node->parent || (node->parent->flags & LYS_CONFIG_MASK) || (node->parent->nodetype & LYS_AUGMENT));
2534 if (node->parent && (node->parent->flags & LYS_CONFIG_MASK)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002535 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002536 } else {
2537 /* default is config true */
2538 node->flags |= LYS_CONFIG_W;
2539 }
2540 } else {
2541 /* config set explicitly */
2542 node->flags |= LYS_SET_CONFIG;
2543 }
2544
Radek Krejci91531e12021-02-08 19:57:54 +01002545 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002546 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002547 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002548 }
2549
Radek Krejci2a9fc652021-01-22 17:44:34 +01002550 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002551}
2552
Radek Krejci91531e12021-02-08 19:57:54 +01002553/**
2554 * @brief Set various flags of the compiled nodes
2555 *
2556 * @param[in] ctx Compile context.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002557 * @param[in] parsed_flags Parsed node flags.
2558 * @param[in] inherited_flags Inherited flags from a schema-only statement.
2559 * @param[in,out] node Compiled node where the flags will be set.
Radek Krejci91531e12021-02-08 19:57:54 +01002560 */
2561static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002562lys_compile_node_flags(struct lysc_ctx *ctx, uint16_t parsed_flags, uint16_t inherited_flags, struct lysc_node *node)
Radek Krejci91531e12021-02-08 19:57:54 +01002563{
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002564 uint16_t parent_flags;
2565 const char *parent_name;
2566
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002567 /* copy flags except for status */
2568 node->flags = (parsed_flags & LYS_FLAGS_COMPILED_MASK) & ~LYS_STATUS_MASK;
2569
Radek Krejci91531e12021-02-08 19:57:54 +01002570 /* inherit config flags */
2571 LY_CHECK_RET(lys_compile_config(ctx, node));
2572
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002573 /* compile status */
2574 parent_flags = node->parent ? node->parent->flags : 0;
2575 parent_name = node->parent ? node->parent->name : NULL;
2576 LY_CHECK_RET(lys_compile_status(ctx, parsed_flags, inherited_flags, parent_flags, parent_name, node->name, &node->flags));
Radek Krejci91531e12021-02-08 19:57:54 +01002577
2578 /* other flags */
Michal Vasko7c565922021-06-10 14:58:27 +02002579 if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002580 node->flags |= LYS_IS_INPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002581 } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002582 node->flags |= LYS_IS_OUTPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002583 } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002584 node->flags |= LYS_IS_NOTIF;
2585 }
2586
2587 return LY_SUCCESS;
2588}
2589
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002590static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002591lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t inherited_flags,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002592 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2593 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002594{
2595 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002596 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002597 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002598 struct lysp_when *pwhen = NULL;
Michal Vaskoe02e7402021-07-23 08:29:28 +02002599 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002600
Radek Krejci2a9fc652021-01-22 17:44:34 +01002601 node->nodetype = pnode->nodetype;
2602 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002603 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002604 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002605 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002606
Radek Krejci2a9fc652021-01-22 17:44:34 +01002607 /* compile any deviations for this node */
2608 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vaskoe02e7402021-07-23 08:29:28 +02002609 if (not_supported && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2610 /* if not supported, keep it just like disabled nodes by if-feature */
2611 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
2612 ctx->compile_opts |= LYS_COMPILE_DISABLED;
2613 }
2614 if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002615 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002616 }
2617
Michal Vaskodfd254d2021-06-24 09:24:59 +02002618 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2619 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2620 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002621
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002622 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002623 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002624 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002625 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002626 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002627 }
2628
Radek Krejci91531e12021-02-08 19:57:54 +01002629 /* config, status and other flags */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002630 LY_CHECK_GOTO(ret = lys_compile_node_flags(ctx, pnode->flags, inherited_flags, node), error);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002631
2632 /* list ordering */
2633 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002634 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002635 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002636 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002637 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002638 node->flags &= ~LYS_ORDBY_MASK;
2639 node->flags |= LYS_ORDBY_SYSTEM;
2640 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2641 /* default ordering is system */
2642 node->flags |= LYS_ORDBY_SYSTEM;
2643 }
2644 }
2645
Radek Krejci2a9fc652021-01-22 17:44:34 +01002646 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2647 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2648
Radek Krejci9a3823e2021-01-27 20:26:46 +01002649 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002650 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002651 ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002652 LY_CHECK_GOTO(ret, cleanup);
2653 }
2654
Radek Krejci2a9fc652021-01-22 17:44:34 +01002655 /* nodetype-specific part */
2656 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2657
2658 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002659 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002660 if (node->flags & LYS_MAND_TRUE) {
2661 /* inherit LYS_MAND_TRUE in parent containers */
2662 lys_compile_mandatory_parents(parent, 1);
2663 }
2664
2665 if (child_set) {
2666 /* add the new node into set */
2667 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2668 }
2669
2670 goto cleanup;
2671
2672error:
Michal Vaskoc636ea42022-09-16 10:20:31 +02002673 lysc_node_free(&ctx->free_ctx, node, 0);
2674
Radek Krejci2a9fc652021-01-22 17:44:34 +01002675cleanup:
2676 if (ret && dev_pnode) {
2677 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2678 }
Michal Vaskoe02e7402021-07-23 08:29:28 +02002679 ctx->compile_opts = prev_opts;
Michal Vaskoc636ea42022-09-16 10:20:31 +02002680 lysp_dev_node_free(ctx, dev_pnode);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002681 return ret;
2682}
2683
Radek Krejci2a9fc652021-01-22 17:44:34 +01002684LY_ERR
2685lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2686{
2687 LY_ERR ret = LY_SUCCESS;
2688 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002689 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002690
2691 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2692 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2693
2694 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002695 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002696 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002697
Radek Krejci01180ac2021-01-27 08:48:22 +01002698 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002699 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2700 }
2701
Michal Vasko95f736c2022-06-08 12:03:31 +02002702 /* connect any augments */
2703 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2704
Michal Vasko7c565922021-06-10 14:58:27 +02002705 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002706
2707done:
2708 return ret;
2709}
2710
2711/**
2712 * @brief Compile parsed action node information.
Michal Vasko193dacd2022-10-13 08:43:05 +02002713 *
Radek Krejci2a9fc652021-01-22 17:44:34 +01002714 * @param[in] ctx Compile context
2715 * @param[in] pnode Parsed action node.
2716 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2717 * is enriched with the action-specific information.
2718 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2719 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002720static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002721lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2722{
2723 LY_ERR ret;
2724 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2725 struct lysc_node_action *action = (struct lysc_node_action *)node;
2726 struct lysp_node_action_inout *input, implicit_input = {
2727 .nodetype = LYS_INPUT,
2728 .name = "input",
2729 .parent = pnode,
2730 };
2731 struct lysp_node_action_inout *output, implicit_output = {
2732 .nodetype = LYS_OUTPUT,
2733 .name = "output",
2734 .parent = pnode,
2735 };
2736
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002737 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002738 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002739 if (action_p->input.nodetype == LYS_UNKNOWN) {
2740 input = &implicit_input;
2741 } else {
2742 input = &action_p->input;
2743 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002744 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 +01002745 lysc_update_path(ctx, NULL, NULL);
2746 LY_CHECK_GOTO(ret, done);
2747
Michal Vaskoc130e162021-10-19 11:30:00 +02002748 /* add must(s) to unres */
2749 ret = lysc_unres_must_add(ctx, &action->input.node, &input->node);
2750 LY_CHECK_GOTO(ret, done);
2751
Radek Krejci2a9fc652021-01-22 17:44:34 +01002752 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002753 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002754 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002755 output = &implicit_output;
2756 } else {
2757 output = &action_p->output;
2758 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002759 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 +01002760 lysc_update_path(ctx, NULL, NULL);
2761 LY_CHECK_GOTO(ret, done);
2762
Michal Vaskoc130e162021-10-19 11:30:00 +02002763 /* add must(s) to unres */
2764 ret = lysc_unres_must_add(ctx, &action->output.node, &output->node);
2765 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002766
2767done:
2768 return ret;
2769}
2770
2771/**
2772 * @brief Compile parsed action node information.
2773 * @param[in] ctx Compile context
2774 * @param[in] pnode Parsed action node.
2775 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2776 * is enriched with the action-specific information.
2777 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2778 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002779static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002780lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2781{
2782 LY_ERR ret = LY_SUCCESS;
2783 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2784 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2785 struct lysp_node *child_p;
2786
2787 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002788
2789 /* add must(s) to unres */
2790 ret = lysc_unres_must_add(ctx, node, pnode);
2791 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002792
Radek Krejci01180ac2021-01-27 08:48:22 +01002793 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002794 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002795 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002796 }
2797
Michal Vasko95f736c2022-06-08 12:03:31 +02002798 /* connect any augments */
2799 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2800
Radek Krejci2a9fc652021-01-22 17:44:34 +01002801done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002802 return ret;
2803}
2804
2805/**
2806 * @brief Compile parsed container node information.
2807 * @param[in] ctx Compile context
2808 * @param[in] pnode Parsed container node.
2809 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2810 * is enriched with the container-specific information.
2811 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2812 */
2813static LY_ERR
2814lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2815{
2816 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2817 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2818 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002819 LY_ERR ret = LY_SUCCESS;
2820
2821 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002822 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002823 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002824 }
2825
2826 /* more cases when the container has meaning but is kept NP for convenience:
2827 * - when condition
2828 * - direct child action/notification
2829 */
2830
2831 LY_LIST_FOR(cont_p->child, child_p) {
2832 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2833 LY_CHECK_GOTO(ret, done);
2834 }
2835
Michal Vasko5347e3a2020-11-03 17:14:57 +01002836 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002837
2838 /* add must(s) to unres */
2839 ret = lysc_unres_must_add(ctx, node, pnode);
2840 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002841
Michal Vasko95f736c2022-06-08 12:03:31 +02002842 /* connect any augments */
2843 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2844
Radek Krejci2a9fc652021-01-22 17:44:34 +01002845 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2846 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2847 LY_CHECK_GOTO(ret, done);
2848 }
2849 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2850 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2851 LY_CHECK_GOTO(ret, done);
2852 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002853
2854done:
2855 return ret;
2856}
2857
Michal Vasko72244882021-01-12 15:21:05 +01002858/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002859 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2860 * @param[in] ctx Compile context.
2861 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2862 * @param[in] type_p Parsed type to compile.
2863 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2864 * @return LY_ERR value.
2865 */
2866static LY_ERR
2867lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2868 struct lysc_node_leaf *leaf)
2869{
2870 struct lysp_qname *dflt;
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002871 struct lysc_type **t;
2872 LY_ARRAY_COUNT_TYPE u, count;
2873 ly_bool in_unres = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002874
Michal Vaskoa99b3572021-02-01 11:54:58 +01002875 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002876 leaf->units ? NULL : &leaf->units, &dflt));
2877
2878 /* store default value, if any */
2879 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2880 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2881 }
2882
Michal Vaskoc130e162021-10-19 11:30:00 +02002883 /* store leafref(s) to be resolved */
2884 LY_CHECK_RET(lysc_unres_leafref_add(ctx, leaf, type_p->pmod));
2885
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002886 /* type-specific checks */
2887 if (leaf->type->basetype == LY_TYPE_UNION) {
2888 t = ((struct lysc_type_union *)leaf->type)->types;
2889 count = LY_ARRAY_COUNT(t);
2890 } else {
2891 t = &leaf->type;
2892 count = 1;
2893 }
2894 for (u = 0; u < count; ++u) {
2895 if (t[u]->basetype == LY_TYPE_EMPTY) {
2896 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
2897 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2898 return LY_EVALID;
2899 }
2900 } else if (!in_unres && ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM))) {
2901 /* store in unres for all disabled bits/enums to be removed */
2902 LY_CHECK_RET(lysc_unres_bitenum_add(ctx, leaf));
2903 in_unres = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002904 }
2905 }
2906
2907 return LY_SUCCESS;
2908}
2909
2910/**
2911 * @brief Compile parsed leaf node information.
2912 * @param[in] ctx Compile context
2913 * @param[in] pnode Parsed leaf node.
2914 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2915 * is enriched with the leaf-specific information.
2916 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2917 */
2918static LY_ERR
2919lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2920{
2921 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2922 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002923 LY_ERR ret = LY_SUCCESS;
2924
Michal Vasko5347e3a2020-11-03 17:14:57 +01002925 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002926
2927 /* add must(s) to unres */
2928 ret = lysc_unres_must_add(ctx, node, pnode);
2929 LY_CHECK_GOTO(ret, done);
2930
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002931 if (leaf_p->units) {
2932 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2933 leaf->flags |= LYS_SET_UNITS;
2934 }
2935
2936 /* compile type */
2937 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2938 LY_CHECK_GOTO(ret, done);
2939
2940 /* store/update default value */
2941 if (leaf_p->dflt.str) {
2942 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2943 leaf->flags |= LYS_SET_DFLT;
2944 }
2945
2946 /* checks */
2947 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002948 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory leaf with a default value.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002949 return LY_EVALID;
2950 }
2951
2952done:
2953 return ret;
2954}
2955
2956/**
2957 * @brief Compile parsed leaf-list node information.
2958 * @param[in] ctx Compile context
2959 * @param[in] pnode Parsed leaf-list node.
2960 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2961 * is enriched with the leaf-list-specific information.
2962 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2963 */
2964static LY_ERR
2965lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2966{
2967 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2968 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002969 LY_ERR ret = LY_SUCCESS;
2970
Michal Vasko5347e3a2020-11-03 17:14:57 +01002971 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002972
2973 /* add must(s) to unres */
2974 ret = lysc_unres_must_add(ctx, node, pnode);
2975 LY_CHECK_GOTO(ret, done);
2976
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002977 if (llist_p->units) {
2978 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2979 llist->flags |= LYS_SET_UNITS;
2980 }
2981
2982 /* compile type */
2983 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2984 LY_CHECK_GOTO(ret, done);
2985
2986 /* store/update default values */
2987 if (llist_p->dflts) {
2988 if (ctx->pmod->version < LYS_VERSION_1_1) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002989 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list default values are allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002990 return LY_EVALID;
2991 }
2992
2993 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2994 llist->flags |= LYS_SET_DFLT;
2995 }
2996
2997 llist->min = llist_p->min;
2998 if (llist->min) {
2999 llist->flags |= LYS_MAND_TRUE;
3000 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02003001 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
3002
3003 if (llist->flags & LYS_CONFIG_R) {
3004 /* state leaf-list is always ordered-by user */
3005 llist->flags &= ~LYS_ORDBY_SYSTEM;
3006 llist->flags |= LYS_ORDBY_USER;
3007 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003008
3009 /* checks */
3010 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003011 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 +02003012 return LY_EVALID;
3013 }
3014
3015 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003016 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003017 llist->min, llist->max);
3018 return LY_EVALID;
3019 }
3020
3021done:
3022 return ret;
3023}
3024
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003025/**
3026 * @brief Find the node according to the given descendant/absolute schema nodeid.
3027 * Used in unique, refine and augment statements.
3028 *
3029 * @param[in] ctx Compile context
3030 * @param[in] nodeid Descendant-schema-nodeid (according to the YANG grammar)
3031 * @param[in] nodeid_len Length of the given nodeid, if it is not NULL-terminated string.
3032 * @param[in] ctx_node Context node for a relative nodeid.
3033 * @param[in] format Format of any prefixes.
3034 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
3035 * @param[in] nodetype Optional (can be 0) restriction for target's nodetype. If target exists, but does not match
3036 * the given nodetype, LY_EDENIED is returned (and target is provided), but no error message is printed.
3037 * The value can be even an ORed value to allow multiple nodetypes.
3038 * @param[out] target Found target node if any.
3039 * @param[out] result_flag Output parameter to announce if the schema nodeid goes through the action's input/output or a Notification.
3040 * The LYSC_OPT_RPC_INPUT, LYSC_OPT_RPC_OUTPUT and LYSC_OPT_NOTIFICATION are used as flags.
3041 * @return LY_ERR values - LY_ENOTFOUND, LY_EVALID, LY_EDENIED or LY_SUCCESS.
3042 */
3043static LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003044lysc_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 +01003045 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 +02003046{
3047 LY_ERR ret = LY_EVALID;
3048 const char *name, *prefix, *id;
3049 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01003050 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003051 const char *nodeid_type;
3052 uint32_t getnext_extra_flag = 0;
3053 uint16_t current_nodetype = 0;
3054
3055 assert(nodeid);
3056 assert(target);
3057 assert(result_flag);
3058 *target = NULL;
3059 *result_flag = 0;
3060
3061 id = nodeid;
3062
3063 if (ctx_node) {
3064 /* descendant-schema-nodeid */
3065 nodeid_type = "descendant";
3066
3067 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003068 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003069 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01003070 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003071 return LY_EVALID;
3072 }
3073 } else {
3074 /* absolute-schema-nodeid */
3075 nodeid_type = "absolute";
3076
3077 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003078 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003079 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003080 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003081 return LY_EVALID;
3082 }
3083 ++id;
3084 }
3085
3086 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
3087 if (prefix) {
3088 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
3089 if (!mod) {
3090 /* module must always be found */
3091 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01003092 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003093 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003094 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003095 return LY_ENOTFOUND;
3096 }
3097 } else {
3098 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003099 case LY_VALUE_SCHEMA:
3100 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003101 /* use the current module */
Michal Vasko56d8da82021-12-16 15:41:30 +01003102 mod = ctx->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003103 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003104 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02003105 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003106 if (!ctx_node) {
3107 LOGINT_RET(ctx->ctx);
3108 }
3109 /* inherit the module of the previous context node */
3110 mod = ctx_node->module;
3111 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02003112 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02003113 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01003114 case LY_VALUE_STR_NS:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003115 /* not really defined */
3116 LOGINT_RET(ctx->ctx);
3117 }
3118 }
3119
3120 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
3121 /* move through input/output manually */
3122 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01003123 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3124 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003125 return LY_ENOTFOUND;
3126 }
3127 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003128 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003129 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003130 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003131 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
3132 } else {
3133 /* only input or output is valid */
3134 ctx_node = NULL;
3135 }
Michal Vasko0b50f6b2022-10-05 15:07:55 +02003136 } else if (ctx->ext && !ctx_node) {
3137 /* top-level extension nodes */
3138 ctx_node = lysc_ext_find_node(ctx->ext, mod, name, name_len, 0, LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003139 } else {
3140 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003141 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003142 getnext_extra_flag = 0;
3143 }
3144 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01003145 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3146 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003147 return LY_ENOTFOUND;
3148 }
3149 current_nodetype = ctx_node->nodetype;
3150
3151 if (current_nodetype == LYS_NOTIF) {
3152 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
3153 } else if (current_nodetype == LYS_INPUT) {
3154 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
3155 } else if (current_nodetype == LYS_OUTPUT) {
3156 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
3157 }
3158
3159 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
3160 break;
3161 }
3162 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003163 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003164 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01003165 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003166 return LY_EVALID;
3167 }
3168 ++id;
3169 }
3170
3171 if (ret == LY_SUCCESS) {
3172 *target = ctx_node;
3173 if (nodetype && !(current_nodetype & nodetype)) {
3174 return LY_EDENIED;
3175 }
3176 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003177 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003178 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01003179 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003180 }
3181
3182 return ret;
3183}
3184
3185/**
3186 * @brief Compile information about list's uniques.
3187 * @param[in] ctx Compile context.
3188 * @param[in] uniques Sized array list of unique statements.
3189 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3190 * @return LY_ERR value.
3191 */
3192static LY_ERR
3193lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
3194{
3195 LY_ERR ret = LY_SUCCESS;
3196 struct lysc_node_leaf **key, ***unique;
3197 struct lysc_node *parent;
3198 const char *keystr, *delim;
3199 size_t len;
3200 LY_ARRAY_COUNT_TYPE v;
3201 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
3202 uint16_t flags;
3203
3204 LY_ARRAY_FOR(uniques, v) {
3205 config = -1;
3206 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3207 keystr = uniques[v].str;
3208 while (keystr) {
3209 delim = strpbrk(keystr, " \t\n");
3210 if (delim) {
3211 len = delim - keystr;
3212 while (isspace(*delim)) {
3213 ++delim;
3214 }
3215 } else {
3216 len = strlen(keystr);
3217 }
3218
3219 /* unique node must be present */
3220 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko56d8da82021-12-16 15:41:30 +01003221 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, LY_VALUE_SCHEMA, (void *)uniques[v].mod,
3222 LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003223 if (ret != LY_SUCCESS) {
3224 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003225 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003226 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01003227 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003228 }
3229 return LY_EVALID;
3230 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003231 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003232 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003233 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003234 return LY_EVALID;
3235 }
3236
3237 /* all referenced leafs must be of the same config type */
3238 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3239 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003240 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003241 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3242 return LY_EVALID;
3243 } else if ((*key)->flags & LYS_CONFIG_W) {
3244 config = 1;
3245 } else { /* LYS_CONFIG_R */
3246 config = 0;
3247 }
3248
3249 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3250 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3251 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003252 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003253 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3254 return LY_EVALID;
3255 }
3256 }
3257
3258 /* check status */
Michal Vaskoc130e162021-10-19 11:30:00 +02003259 LY_CHECK_RET(lysc_check_status(ctx, list->flags, uniques[v].mod->mod, list->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003260 (*key)->flags, (*key)->module, (*key)->name));
3261
3262 /* mark leaf as unique */
3263 (*key)->flags |= LYS_UNIQUE;
3264
3265 /* next unique value in line */
3266 keystr = delim;
3267 }
3268 /* next unique definition */
3269 }
3270
3271 return LY_SUCCESS;
3272}
3273
3274/**
3275 * @brief Compile parsed list node information.
3276 * @param[in] ctx Compile context
3277 * @param[in] pnode Parsed list node.
3278 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3279 * is enriched with the list-specific information.
3280 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3281 */
3282static LY_ERR
3283lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3284{
3285 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3286 struct lysc_node_list *list = (struct lysc_node_list *)node;
3287 struct lysp_node *child_p;
Michal Vasko2d6507a2022-03-16 09:40:52 +01003288 struct lysc_node *parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003289 struct lysc_node_leaf *key, *prev_key = NULL;
3290 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003291 const char *keystr, *delim;
3292 LY_ERR ret = LY_SUCCESS;
3293
3294 list->min = list_p->min;
3295 if (list->min) {
3296 list->flags |= LYS_MAND_TRUE;
3297 }
3298 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3299
3300 LY_LIST_FOR(list_p->child, child_p) {
3301 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3302 }
3303
Michal Vasko5347e3a2020-11-03 17:14:57 +01003304 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003305
3306 /* add must(s) to unres */
3307 ret = lysc_unres_must_add(ctx, node, pnode);
3308 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003309
3310 /* keys */
Michal Vasko2d6507a2022-03-16 09:40:52 +01003311 if (list->flags & LYS_CONFIG_W) {
3312 parent = node;
3313 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
3314 /* compiling individual grouping, we can check this only if there is an explicit config set */
3315 while (parent) {
3316 if (parent->flags & LYS_SET_CONFIG) {
3317 break;
3318 }
3319 parent = parent->parent;
3320 }
3321 }
3322
3323 if (parent && (!list_p->key || !list_p->key[0])) {
3324 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3325 return LY_EVALID;
3326 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003327 }
3328
3329 /* find all the keys (must be direct children) */
3330 keystr = list_p->key;
3331 if (!keystr) {
3332 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003333 list->flags &= ~LYS_ORDBY_SYSTEM;
3334 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003335 }
3336 while (keystr) {
3337 delim = strpbrk(keystr, " \t\n");
3338 if (delim) {
3339 len = delim - keystr;
3340 while (isspace(*delim)) {
3341 ++delim;
3342 }
3343 } else {
3344 len = strlen(keystr);
3345 }
3346
3347 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003348 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3349 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003350 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003351 return LY_EVALID;
3352 }
3353 /* keys must be unique */
3354 if (key->flags & LYS_KEY) {
3355 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003356 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003357 return LY_EVALID;
3358 }
3359
Radek Krejcia6016992021-03-03 10:13:41 +01003360 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003361 /* key must have the same config flag as the list itself */
3362 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003363 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of a configuration list must not be a state leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003364 return LY_EVALID;
3365 }
3366 if (ctx->pmod->version < LYS_VERSION_1_1) {
3367 /* YANG 1.0 denies key to be of empty type */
3368 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003369 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003370 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3371 return LY_EVALID;
3372 }
3373 } else {
3374 /* when and if-feature are illegal on list keys */
3375 if (key->when) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003376 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List's key must not have any \"when\" statement.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003377 return LY_EVALID;
3378 }
Michal Vasko93b4ab92022-05-13 12:29:59 +02003379 /* unable to check if-features but compilation would fail if disabled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003380 }
3381
3382 /* check status */
Michal Vaskoc636ea42022-09-16 10:20:31 +02003383 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 +02003384
3385 /* ignore default values of the key */
3386 if (key->dflt) {
3387 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003388 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)key->dflt->realtype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003389 free(key->dflt);
3390 key->dflt = NULL;
3391 }
3392 /* mark leaf as key */
3393 key->flags |= LYS_KEY;
3394
3395 /* move it to the correct position */
3396 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3397 /* fix links in closest previous siblings of the key */
3398 if (key->next) {
3399 key->next->prev = key->prev;
3400 } else {
3401 /* last child */
3402 list->child->prev = key->prev;
3403 }
3404 if (key->prev->next) {
3405 key->prev->next = key->next;
3406 }
3407 /* fix links in the key */
3408 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003409 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003410 key->next = prev_key->next;
3411 } else {
3412 key->prev = list->child->prev;
3413 key->next = list->child;
3414 }
3415 /* fix links in closes future siblings of the key */
3416 if (prev_key) {
3417 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003418 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003419 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003420 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003421 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003422 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003423 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003424 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003425 }
3426 /* fix links in parent */
3427 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003428 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003429 }
3430 }
3431
3432 /* next key value */
3433 prev_key = key;
3434 keystr = delim;
3435 lysc_update_path(ctx, NULL, NULL);
3436 }
3437
Michal Vasko95f736c2022-06-08 12:03:31 +02003438 /* connect any augments */
3439 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3440
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003441 /* uniques */
3442 if (list_p->uniques) {
3443 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3444 }
3445
Radek Krejci2a9fc652021-01-22 17:44:34 +01003446 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3447 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3448 LY_CHECK_GOTO(ret, done);
3449 }
3450 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3451 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3452 LY_CHECK_GOTO(ret, done);
3453 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003454
3455 /* checks */
3456 if (list->min > list->max) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003457 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 +02003458 return LY_EVALID;
3459 }
3460
3461done:
3462 return ret;
3463}
3464
3465/**
3466 * @brief Do some checks and set the default choice's case.
3467 *
3468 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3469 *
3470 * @param[in] ctx Compile context.
3471 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3472 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3473 * @return LY_ERR value.
3474 */
3475static LY_ERR
3476lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3477{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003478 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003479 const struct lys_module *mod;
3480 const char *prefix = NULL, *name;
3481 size_t prefix_len = 0;
3482
3483 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3484 name = strchr(dflt->str, ':');
3485 if (name) {
3486 prefix = dflt->str;
3487 prefix_len = name - prefix;
3488 ++name;
3489 } else {
3490 name = dflt->str;
3491 }
3492 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003493 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003494 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003495 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003496 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003497 return LY_EVALID;
3498 }
3499 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003500 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003501 }
3502
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003503 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 +02003504 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003505 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003506 "Default case \"%s\" not found.", dflt->str);
3507 return LY_EVALID;
3508 }
3509
3510 /* no mandatory nodes directly under the default case */
3511 LY_LIST_FOR(ch->dflt->child, iter) {
3512 if (iter->parent != (struct lysc_node *)ch->dflt) {
3513 break;
3514 }
3515 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003516 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003517 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3518 return LY_EVALID;
3519 }
3520 }
3521
3522 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003523 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003524 return LY_EVALID;
3525 }
3526
3527 ch->dflt->flags |= LYS_SET_DFLT;
3528 return LY_SUCCESS;
3529}
3530
3531LY_ERR
3532lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3533 struct ly_set *child_set)
3534{
3535 LY_ERR ret = LY_SUCCESS;
3536 struct lysp_node *child_p_next = child_p->next;
3537 struct lysp_node_case *cs_p;
Michal Vasko4eb49d52022-02-17 15:05:00 +01003538 struct lysc_node_case *cs_c;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003539
3540 if (child_p->nodetype == LYS_CASE) {
3541 /* standard case under choice */
3542 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3543 } else {
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003544 /* we need the implicit case first, so create a fake parsed (shorthand) case */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003545 cs_p = calloc(1, sizeof *cs_p);
Michal Vasko786a8832022-12-05 10:41:27 +01003546 LY_CHECK_ERR_RET(!cs_p, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003547 cs_p->nodetype = LYS_CASE;
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003548 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003549 cs_p->child = child_p;
3550
3551 /* make the child the only case child */
3552 child_p->next = NULL;
3553
3554 /* compile it normally */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003555 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 +02003556
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003557 if (((struct lysc_node_choice *)node)->cases) {
Michal Vasko4eb49d52022-02-17 15:05:00 +01003558 /* find our case node */
3559 cs_c = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases;
3560 while (cs_c->name != cs_p->name) {
3561 cs_c = (struct lysc_node_case *)cs_c->next;
3562 assert(cs_c);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003563 }
aPiecekaa320c92021-05-21 07:34:24 +02003564
Michal Vasko4eb49d52022-02-17 15:05:00 +01003565 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
3566 /* compiled case node cannot point to his corresponding parsed node
3567 * because it exists temporarily so it must be set to NULL
3568 */
3569 assert(cs_c->priv == cs_p);
3570 cs_c->priv = NULL;
3571 }
3572
3573 /* status is copied from his child and not from his parent as usual. */
3574 if (cs_c->child) {
3575 cs_c->flags &= ~LYS_STATUS_MASK;
3576 cs_c->flags |= (LYS_STATUS_MASK & cs_c->child->flags);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003577 }
3578 } /* else it was removed by a deviation */
aPiecekaa320c92021-05-21 07:34:24 +02003579
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003580revert_sh_case:
3581 /* free the parsed shorthand case and correct pointers back */
aPiecekaa320c92021-05-21 07:34:24 +02003582 cs_p->child = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003583 lysp_node_free(&ctx->free_ctx, (struct lysp_node *)cs_p);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003584 child_p->next = child_p_next;
3585 }
3586
3587 return ret;
3588}
3589
3590/**
3591 * @brief Compile parsed choice node information.
3592 *
3593 * @param[in] ctx Compile context
3594 * @param[in] pnode Parsed choice node.
3595 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3596 * is enriched with the choice-specific information.
3597 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3598 */
3599static LY_ERR
3600lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3601{
3602 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3603 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3604 struct lysp_node *child_p;
3605 LY_ERR ret = LY_SUCCESS;
3606
3607 assert(node->nodetype == LYS_CHOICE);
3608
3609 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003610 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, child_p, node, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003611 }
3612
Michal Vasko95f736c2022-06-08 12:03:31 +02003613 /* connect any augments */
3614 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3615
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003616 /* default branch */
3617 if (ch_p->dflt.str) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003618 LY_CHECK_GOTO(ret = lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003619 }
3620
Michal Vasko95f736c2022-06-08 12:03:31 +02003621done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003622 return ret;
3623}
3624
3625/**
3626 * @brief Compile parsed anydata or anyxml node information.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003627 *
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003628 * @param[in] ctx Compile context
3629 * @param[in] pnode Parsed anydata or anyxml node.
3630 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3631 * is enriched with the any-specific information.
3632 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3633 */
3634static LY_ERR
3635lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3636{
3637 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3638 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003639 LY_ERR ret = LY_SUCCESS;
3640
Michal Vasko5347e3a2020-11-03 17:14:57 +01003641 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003642
3643 /* add must(s) to unres */
3644 ret = lysc_unres_must_add(ctx, node, pnode);
3645 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003646
Radek Krejci91531e12021-02-08 19:57:54 +01003647 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003648 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko193dacd2022-10-13 08:43:05 +02003649 lyplg_ext_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003650 }
3651done:
3652 return ret;
3653}
3654
3655/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003656 * @brief Prepare the case structure in choice node for the new data node.
3657 *
3658 * 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
3659 * created in the choice when the first child was processed.
3660 *
3661 * @param[in] ctx Compile context.
3662 * @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,
3663 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3664 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3665 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3666 * @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,
3667 * it is linked from the case structure only in case it is its first child.
3668 */
3669static LY_ERR
3670lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3671{
Michal Vasko95f736c2022-06-08 12:03:31 +02003672 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003673 struct lysp_node *child_p;
3674 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3675
3676 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3677 /* we have to add an implicit case node into the parent choice */
3678 } else if (pnode->nodetype == LYS_CASE) {
3679 /* explicit parent case */
3680 LY_LIST_FOR(cs_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003681 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003682 }
3683 } else {
3684 LOGINT_RET(ctx->ctx);
3685 }
3686
Michal Vasko95f736c2022-06-08 12:03:31 +02003687 /* connect any augments */
3688 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3689
3690done:
3691 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003692}
3693
3694void
3695lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3696{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003697 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003698
3699 if (add) { /* set flag */
3700 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3701 parent = parent->parent) {
3702 parent->flags |= LYS_MAND_TRUE;
3703 }
3704 } else { /* unset flag */
3705 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003706 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003707 if (iter->flags & LYS_MAND_TRUE) {
3708 /* there is another mandatory node */
3709 return;
3710 }
3711 }
3712 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3713 parent->flags &= ~LYS_MAND_TRUE;
3714 }
3715 }
3716}
3717
3718/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003719 * @brief Get the grouping with the specified name from given groupings sized array.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003720 *
3721 * @param[in] node Linked list of nodes with groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003722 * @param[in] name Name of the grouping to find,
3723 * @return NULL when there is no grouping with the specified name
3724 * @return Pointer to the grouping of the specified @p name.
3725 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003726static struct lysp_node_grp *
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003727match_grouping(const struct lysp_node_grp *node, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003728{
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003729 LY_LIST_FOR(node, node) {
3730 if ((node->nodetype == LYS_GROUPING) && !strcmp(node->name, name)) {
3731 return (struct lysp_node_grp *)node;
Radek Krejciabd33a42021-01-20 17:18:59 +01003732 }
3733 }
3734
3735 return NULL;
3736}
3737
3738/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003739 * @brief Find grouping for a uses.
3740 *
3741 * @param[in] ctx Compile context.
3742 * @param[in] uses_p Parsed uses node.
3743 * @param[out] gpr_p Found grouping on success.
3744 * @param[out] grp_pmod Module of @p grp_p on success.
3745 * @return LY_ERR value.
3746 */
3747static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003748lys_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 +02003749 struct lysp_module **grp_pmod)
3750{
3751 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003752 struct lysp_node_grp *grp;
Michal Vasko193dacd2022-10-13 08:43:05 +02003753 const struct lysp_node_grp *ext_grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003754 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003755 const char *id, *name, *prefix, *local_pref;
3756 size_t prefix_len, name_len;
3757 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003758 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003759
3760 *grp_p = NULL;
3761 *grp_pmod = NULL;
3762
3763 /* search for the grouping definition */
3764 id = uses_p->name;
3765 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3766 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3767 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3768 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003769 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003770 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003771 if ((grp = match_grouping(lysp_node_groupings(pnode), name))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003772 found = ctx->pmod;
3773 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003774 }
3775 }
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003776
3777 /* if in an extension, search possible groupings in it */
Michal Vaskob62e1e32022-11-08 10:50:41 +01003778 if (!found && ctx->ext) {
Michal Vaskofbd037c2022-11-08 10:34:20 +01003779 lyplg_ext_parsed_get_storage(ctx->ext, LY_STMT_GROUPING, sizeof ext_grp, (const void **)&ext_grp);
Michal Vasko193dacd2022-10-13 08:43:05 +02003780 if ((grp = match_grouping(ext_grp, name))) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003781 found = ctx->pmod;
3782 }
3783 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003784 } else {
3785 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003786 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003787 if (!mod) {
Michal Vasko9925a112022-12-14 12:15:50 +01003788 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid prefix used for grouping \"%s\" reference.", uses_p->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003789 return LY_EVALID;
3790 }
3791 pmod = mod->parsed;
3792 }
3793
3794 if (!found) {
3795 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003796 if ((grp = match_grouping(pmod->groupings, name))) {
3797 found = pmod;
3798 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003799 /* ... and all the submodules */
3800 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003801 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3802 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003803 break;
3804 }
3805 }
3806 }
3807 }
3808 if (!found) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003809 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003810 return LY_EVALID;
3811 }
3812
Michal Vasko7c565922021-06-10 14:58:27 +02003813 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003814 /* remember that the grouping is instantiated to avoid its standalone validation */
3815 grp->flags |= LYS_USED_GRP;
3816 }
3817
3818 *grp_p = grp;
3819 *grp_pmod = found;
3820 return LY_SUCCESS;
3821}
3822
3823/**
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003824 * @brief Compile uses grouping children.
3825 *
3826 * @param[in] ctx Compile context.
3827 * @param[in] uses_p Parsed uses.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003828 * @param[in] inherited_flags Inherited flags from the uses.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003829 * @param[in] child First grouping child to compile.
3830 * @param[in] grp_mod Grouping parsed module.
3831 * @param[in] parent Uses compiled parent, may be NULL if top-level.
3832 * @param[in,out] child_set Set of all compiled child nodes.
3833 * @param[in] child_unres_disabled Whether the children are to be put into unres disabled set or not.
3834 * @return LY_SUCCESS on success.
3835 * @return LY_EVALID on failure.
3836 */
3837static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003838lys_compile_uses_children(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, uint16_t inherited_flags,
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003839 struct lysp_node *child, struct lysp_module *grp_mod, struct lysc_node *parent, struct ly_set *child_set,
3840 ly_bool child_unres_disabled)
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003841{
3842 LY_ERR rc = LY_SUCCESS;
3843 struct lysp_module *mod_old = ctx->pmod;
3844 uint32_t child_i, opt_prev = ctx->compile_opts;
3845 ly_bool enabled;
3846 struct lysp_node *pnode;
3847 struct lysc_node *node;
3848 struct lysc_when *when_shared = NULL;
3849
3850 assert(child_set);
3851
Michal Vasko5940c302022-07-14 13:54:38 +02003852 child_i = child_set->count;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003853 LY_LIST_FOR(child, pnode) {
3854 /* compile the nodes with their parsed (grouping) module */
3855 ctx->pmod = grp_mod;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003856 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, parent, inherited_flags, child_set), cleanup);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003857
3858 /* eval if-features again for the rest of this node processing */
3859 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
3860 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3861 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3862 }
3863
3864 /* restore the parsed module */
3865 ctx->pmod = mod_old;
3866
3867 /* since the uses node is not present in the compiled tree, we need to pass some of its
3868 * statements to all its children */
3869 while (child_i < child_set->count) {
3870 node = child_set->snodes[child_i];
3871
3872 if (uses_p->when) {
3873 /* pass uses when to all the children */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003874 rc = lys_compile_when(ctx, uses_p->when, inherited_flags, parent, lysc_data_node(parent), node, &when_shared);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003875 LY_CHECK_GOTO(rc, cleanup);
3876 }
3877
3878 if (child_unres_disabled) {
3879 /* child is disabled by the uses if-features */
3880 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
3881 }
3882
3883 /* child processed */
3884 ++child_i;
3885 }
3886
3887 /* next iter */
3888 ctx->compile_opts = opt_prev;
3889 }
3890
3891cleanup:
3892 ctx->compile_opts = opt_prev;
3893 return rc;
3894}
3895
3896/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003897 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3898 * If present, also apply uses's modificators.
3899 *
3900 * @param[in] ctx Compile context
3901 * @param[in] uses_p Parsed uses schema node.
3902 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3903 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3904 * the compile context.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003905 * @param[in] inherited_flags Inherited flags from a schema-only statement.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003906 * @param[in] child_set Optional set of all the compiled children.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003907 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3908 */
3909static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003910lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, uint16_t inherited_flags,
3911 struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003912{
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003913 LY_ERR rc = LY_SUCCESS;
3914 ly_bool enabled, child_unres_disabled = 0;
3915 uint32_t i, grp_stack_count, opt_prev = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003916 struct lysp_node_grp *grp = NULL;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003917 uint16_t uses_flags = 0;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003918 struct lysp_module *grp_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003919 struct ly_set uses_child_set = {0};
3920
3921 /* find the referenced grouping */
3922 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3923
3924 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3925 grp_stack_count = ctx->groupings.count;
3926 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3927 if (grp_stack_count == ctx->groupings.count) {
3928 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003929 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003930 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3931 return LY_EVALID;
3932 }
3933
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003934 /* nodetype checks */
3935 if (grp->actions && (parent && !lysc_node_actions_p(parent))) {
3936 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3937 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3938 parent->name, lys_nodetype2str(parent->nodetype));
3939 rc = LY_EVALID;
3940 goto cleanup;
3941 }
3942 if (grp->notifs && (parent && !lysc_node_notifs_p(parent))) {
3943 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3944 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3945 parent->name, lys_nodetype2str(parent->nodetype));
3946 rc = LY_EVALID;
3947 goto cleanup;
3948 }
3949
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003950 /* check status */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003951 rc = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3952 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003953
3954 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003955 rc = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3956 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003957
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003958 /* compile special uses status flags */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003959 rc = lys_compile_status(ctx, uses_p->flags, inherited_flags, parent ? parent->flags : 0,
3960 parent ? parent->name : NULL, "<uses>", &uses_flags);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003961 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003962
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003963 /* uses if-features */
3964 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, uses_p->iffeatures, &enabled), cleanup);
3965 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3966 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3967 child_unres_disabled = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003968 }
3969
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003970 /* uses grouping children */
3971 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, grp->child, grp_mod, parent,
3972 child_set ? child_set : &uses_child_set, child_unres_disabled);
3973 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003974
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003975 /* uses grouping RPCs/actions */
3976 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->actions, grp_mod, parent,
3977 child_set ? child_set : &uses_child_set, child_unres_disabled);
3978 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003979
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003980 /* uses grouping notifications */
3981 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->notifs, grp_mod, parent,
3982 child_set ? child_set : &uses_child_set, child_unres_disabled);
3983 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003984
3985 /* check that all augments were applied */
3986 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003987 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3988 /* augment of some parent uses, irrelevant now */
3989 continue;
3990 }
3991
3992 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003993 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003994 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003995 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003996 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003997
3998 /* check that all refines were applied */
3999 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01004000 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004001 /* refine of some parent uses, irrelevant now */
Michal Vaskod8655722021-01-12 15:20:36 +01004002 continue;
4003 }
4004
4005 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004006 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004007 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004008 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004009 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004010
Michal Vasko193dacd2022-10-13 08:43:05 +02004011 /* compile uses and grouping extensions into the parent */
4012 COMPILE_EXTS_GOTO(ctx, uses_p->exts, parent->exts, parent, rc, cleanup);
4013 COMPILE_EXTS_GOTO(ctx, grp->exts, parent->exts, parent, rc, cleanup);
4014
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004015cleanup:
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004016 /* restore previous context */
4017 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004018
4019 /* remove the grouping from the stack for circular groupings dependency check */
4020 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4021 assert(ctx->groupings.count == grp_stack_count);
4022
4023 ly_set_erase(&uses_child_set, NULL);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004024 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004025}
4026
4027static int
4028lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
4029{
4030 struct lysp_node *iter;
4031 int len = 0;
4032
4033 *path = NULL;
4034 for (iter = node; iter && len >= 0; iter = iter->parent) {
4035 char *s = *path;
4036 char *id;
4037
4038 switch (iter->nodetype) {
4039 case LYS_USES:
4040 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
4041 break;
4042 case LYS_GROUPING:
4043 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
4044 break;
4045 case LYS_AUGMENT:
4046 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
4047 break;
4048 default:
4049 id = strdup(iter->name);
4050 break;
4051 }
4052
4053 if (!iter->parent) {
4054 /* print prefix */
4055 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
4056 } else {
4057 /* prefix is the same as in parent */
4058 len = asprintf(path, "/%s%s", id, s ? s : "");
4059 }
4060 free(s);
4061 free(id);
4062 }
4063
4064 if (len < 0) {
4065 free(*path);
4066 *path = NULL;
4067 } else if (len == 0) {
4068 *path = strdup("/");
4069 len = 1;
4070 }
4071 return len;
4072}
4073
4074LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01004075lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004076{
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004077 LY_ERR rc = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004078 char *path;
4079 int len;
4080
Michal Vasko8254d852022-04-25 10:05:59 +02004081 /* use grouping status to avoid errors */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004082 struct lysp_node_uses fake_uses = {
4083 .parent = pnode,
4084 .nodetype = LYS_USES,
Michal Vasko8254d852022-04-25 10:05:59 +02004085 .flags = grp->flags & LYS_STATUS_MASK, .next = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004086 .name = grp->name,
4087 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4088 .refines = NULL, .augments = NULL
4089 };
4090 struct lysc_node_container fake_container = {
4091 .nodetype = LYS_CONTAINER,
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004092 .flags = 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004093 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02004094 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01004095 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004096 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004097 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004098 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4099 };
4100
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004101 /* compile fake container flags */
4102 LY_CHECK_GOTO(rc = lys_compile_node_flags(ctx, pnode ? pnode->flags : 0, 0, &fake_container.node), cleanup);
4103
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004104 if (grp->parent) {
4105 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4106 }
4107
4108 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
4109 if (len < 0) {
4110 LOGMEM(ctx->ctx);
4111 return LY_EMEM;
4112 }
4113 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
4114 ctx->path_len = (uint32_t)len;
4115 free(path);
4116
4117 lysc_update_path(ctx, NULL, "{grouping}");
4118 lysc_update_path(ctx, NULL, grp->name);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004119 rc = lys_compile_uses(ctx, &fake_uses, &fake_container.node, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004120 lysc_update_path(ctx, NULL, NULL);
4121 lysc_update_path(ctx, NULL, NULL);
4122
4123 ctx->path_len = 1;
4124 ctx->path[1] = '\0';
4125
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004126cleanup:
Michal Vaskoc636ea42022-09-16 10:20:31 +02004127 lysc_node_container_free(&ctx->free_ctx, &fake_container);
Michal Vaskob7402382023-01-04 12:05:06 +01004128 FREE_ARRAY(&ctx->free_ctx, fake_container.exts, lysc_ext_instance_free);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004129 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004130}
4131
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004132LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004133lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t inherited_flags,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004134 struct ly_set *child_set)
4135{
4136 LY_ERR ret = LY_SUCCESS;
4137 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02004138 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004139
4140 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
4141
4142 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01004143 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004144 } else {
4145 lysc_update_path(ctx, NULL, "{uses}");
4146 lysc_update_path(ctx, NULL, pnode->name);
4147 }
4148
4149 switch (pnode->nodetype) {
4150 case LYS_CONTAINER:
4151 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
4152 node_compile_spec = lys_compile_node_container;
4153 break;
4154 case LYS_LEAF:
4155 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
4156 node_compile_spec = lys_compile_node_leaf;
4157 break;
4158 case LYS_LIST:
4159 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
4160 node_compile_spec = lys_compile_node_list;
4161 break;
4162 case LYS_LEAFLIST:
4163 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
4164 node_compile_spec = lys_compile_node_leaflist;
4165 break;
4166 case LYS_CHOICE:
4167 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
4168 node_compile_spec = lys_compile_node_choice;
4169 break;
4170 case LYS_CASE:
4171 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
4172 node_compile_spec = lys_compile_node_case;
4173 break;
4174 case LYS_ANYXML:
4175 case LYS_ANYDATA:
4176 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
4177 node_compile_spec = lys_compile_node_any;
4178 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004179 case LYS_RPC:
4180 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02004181 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004182 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4183 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004184 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004185 return LY_EVALID;
4186 }
4187 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
4188 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02004189 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004190 break;
4191 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02004192 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004193 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4194 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004195 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004196 return LY_EVALID;
4197 }
4198 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
4199 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02004200 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004201 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004202 case LYS_USES:
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004203 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, inherited_flags, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004204 lysc_update_path(ctx, NULL, NULL);
4205 lysc_update_path(ctx, NULL, NULL);
4206 return ret;
4207 default:
4208 LOGINT(ctx->ctx);
4209 return LY_EINT;
4210 }
4211 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4212
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004213 ret = lys_compile_node_(ctx, pnode, parent, inherited_flags, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004214
Michal Vasko7c565922021-06-10 14:58:27 +02004215 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004216 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004217 return ret;
4218}