blob: bdbb7091e257ab4ee94bcaf6600b8fbeb1f7ecce [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};
1331 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001332 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001333 free(perl_regex);
1334 return LY_EVALID;
1335 }
1336 free(perl_regex);
1337
1338 if (code) {
1339 *code = code_local;
1340 } else {
1341 free(code_local);
1342 }
1343
1344 return LY_SUCCESS;
1345
1346#undef URANGE_LEN
1347}
1348
Michal Vasko51de7b72022-04-29 09:50:22 +02001349LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001350lys_compile_type_patterns(struct lysc_ctx *ctx, const struct lysp_restr *patterns_p, struct lysc_pattern **base_patterns,
1351 struct lysc_pattern ***patterns)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001352{
1353 struct lysc_pattern **pattern;
1354 LY_ARRAY_COUNT_TYPE u;
1355 LY_ERR ret = LY_SUCCESS;
1356
1357 /* first, copy the patterns from the base type */
1358 if (base_patterns) {
1359 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1360 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1361 }
1362
1363 LY_ARRAY_FOR(patterns_p, u) {
1364 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1365 *pattern = calloc(1, sizeof **pattern);
1366 ++(*pattern)->refcount;
1367
Radek Krejci2efc45b2020-12-22 16:25:44 +01001368 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001369 LY_CHECK_RET(ret);
1370
Radek Krejcif13b87b2020-12-01 22:02:17 +01001371 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001372 (*pattern)->inverted = 1;
1373 }
1374 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1375 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1376 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1377 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1378 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001379 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001380 }
1381done:
1382 return ret;
1383}
1384
1385/**
1386 * @brief map of the possible restrictions combination for the specific built-in type.
1387 */
1388static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1389 0 /* LY_TYPE_UNKNOWN */,
1390 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1391 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1392 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1393 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1394 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1395 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1396 LYS_SET_BIT /* LY_TYPE_BITS */,
1397 0 /* LY_TYPE_BOOL */,
1398 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1399 0 /* LY_TYPE_EMPTY */,
1400 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1401 LYS_SET_BASE /* LY_TYPE_IDENT */,
1402 LYS_SET_REQINST /* LY_TYPE_INST */,
1403 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1404 LYS_SET_TYPE /* LY_TYPE_UNION */,
1405 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1406 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1407 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1408 LYS_SET_RANGE /* LY_TYPE_INT64 */
1409};
1410
1411/**
1412 * @brief stringification of the YANG built-in data types
1413 */
1414const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001415 LY_TYPE_UNKNOWN_STR,
1416 LY_TYPE_BINARY_STR,
1417 LY_TYPE_UINT8_STR,
1418 LY_TYPE_UINT16_STR,
1419 LY_TYPE_UINT32_STR,
1420 LY_TYPE_UINT64_STR,
1421 LY_TYPE_STRING_STR,
1422 LY_TYPE_BITS_STR,
1423 LY_TYPE_BOOL_STR,
1424 LY_TYPE_DEC64_STR,
1425 LY_TYPE_EMPTY_STR,
1426 LY_TYPE_ENUM_STR,
1427 LY_TYPE_IDENT_STR,
1428 LY_TYPE_INST_STR,
1429 LY_TYPE_LEAFREF_STR,
1430 LY_TYPE_UNION_STR,
1431 LY_TYPE_INT8_STR,
1432 LY_TYPE_INT16_STR,
1433 LY_TYPE_INT32_STR,
1434 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001435};
1436
Michal Vasko193dacd2022-10-13 08:43:05 +02001437LY_ERR
1438lys_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 +01001439 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001440{
1441 LY_ERR ret = LY_SUCCESS;
1442 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001443 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1444 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001445 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001446 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001447
1448 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001449 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001450 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1451 return LY_EVALID;
1452 }
1453
1454 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001455 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001456 if (base_enums) {
1457 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1458 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001459 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001460 break;
1461 }
1462 }
1463 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001464 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001465 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001466 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001467 return LY_EVALID;
1468 }
1469 match = v;
1470 }
1471
1472 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001473 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001474 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001475 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001476 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001477 LY_ARRAY_FOR(*bitenums, v) {
1478 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001479 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001480 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001481 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001482 return LY_EVALID;
1483 }
1484 }
1485 } else if (base_enums) {
1486 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001487 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001488 } else {
1489 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001490 if (u == 0) {
1491 cur_val = 0;
1492 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001493 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001494 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001495 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001496 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001497 } else {
1498 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001499 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001500 }
1501
1502 /* save highest value for auto assing */
1503 if (highest_value < cur_val) {
1504 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001505 }
1506 } else { /* LY_TYPE_BITS */
1507 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001508 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001509 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001510 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001511 LY_ARRAY_FOR(*bitenums, v) {
1512 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001513 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001514 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001515 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001516 return LY_EVALID;
1517 }
1518 }
1519 } else if (base_enums) {
1520 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001521 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001522 } else {
1523 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001524 if (u == 0) {
1525 cur_pos = 0;
1526 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001527 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001528 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001529 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001530 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001531 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001532 } else {
1533 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001534 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001535 }
1536
1537 /* save highest position for auto assing */
1538 if (highest_position < cur_pos) {
1539 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001540 }
1541 }
1542
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001543 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001544 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001545 if (basetype == LY_TYPE_ENUM) {
1546 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001547 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001548 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001549 enums_p[u].name, base_enums[match].value, cur_val);
1550 return LY_EVALID;
1551 }
1552 } else {
1553 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001554 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001555 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001556 enums_p[u].name, base_enums[match].position, cur_pos);
1557 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001558 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001559 }
1560 }
1561
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001562 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001563 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001564 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1565 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1566 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001567 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 +01001568 if (basetype == LY_TYPE_ENUM) {
1569 e->value = cur_val;
1570 } else {
1571 e->position = cur_pos;
1572 }
Radek Krejciab430862021-03-02 20:13:40 +01001573 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001574
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001575 /* evaluate if-ffeatures */
1576 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1577 if (!enabled) {
1578 /* set only flag, later resolved and removed */
1579 e->flags |= LYS_DISABLED;
1580 }
1581
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001582 if (basetype == LY_TYPE_BITS) {
1583 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001584 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001585 if (v != u) {
1586 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001587 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1588 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001589 }
1590 }
1591 }
1592
1593done:
1594 return ret;
1595}
1596
Radek Krejci6a205692020-12-09 13:58:22 +01001597static LY_ERR
1598lys_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 +01001599 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001600{
1601 LY_ERR ret = LY_SUCCESS;
1602 struct lysc_type **utypes = *utypes_p;
1603 struct lysc_type_union *un_aux = NULL;
1604
1605 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1606 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001607 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1608 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001609 LY_CHECK_GOTO(ret, error);
1610 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1611 /* add space for additional types from the union subtype */
1612 un_aux = (struct lysc_type_union *)utypes[u + additional];
1613 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1614 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1615
1616 /* copy subtypes of the subtype union */
1617 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1618 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1619 struct lysc_type_leafref *lref;
1620
1621 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1622 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1623 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1624 lref = (struct lysc_type_leafref *)utypes[u + additional];
1625
1626 lref->basetype = LY_TYPE_LEAFREF;
Michal Vaskoe33134a2022-07-29 14:54:40 +02001627 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 +01001628 LY_CHECK_GOTO(ret, error);
Michal Vasko080064b2021-08-31 15:20:44 +02001629 lref->refcount = 1;
Radek Krejci6a205692020-12-09 13:58:22 +01001630 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001631 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001632 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001633 LY_CHECK_GOTO(ret, error);
1634 /* TODO extensions */
1635
1636 } else {
1637 utypes[u + additional] = un_aux->types[v];
Michal Vasko04338d92021-09-01 07:58:14 +02001638 LY_ATOMIC_INC_BARRIER(un_aux->types[v]->refcount);
Radek Krejci6a205692020-12-09 13:58:22 +01001639 }
1640 ++additional;
1641 LY_ARRAY_INCREMENT(utypes);
1642 }
1643 /* compensate u increment in main loop */
1644 --additional;
1645
1646 /* free the replaced union subtype */
Michal Vaskoc636ea42022-09-16 10:20:31 +02001647 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001648 un_aux = NULL;
1649 } else {
1650 LY_ARRAY_INCREMENT(utypes);
1651 }
1652 }
1653
1654 *utypes_p = utypes;
1655 return LY_SUCCESS;
1656
1657error:
1658 if (un_aux) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001659 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001660 }
1661 *utypes_p = utypes;
1662 return ret;
1663}
1664
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001665/**
1666 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1667 * @param[in] ctx Compile context.
1668 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1669 * @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 +02001670 * @param[in] context_name Name of the context node or referencing typedef for logging.
1671 * @param[in] type_p Parsed type to compile.
1672 * @param[in] basetype Base YANG built-in type of the type to compile.
1673 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1674 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1675 * @param[out] type Newly created type structure with the filled information about the type.
1676 * @return LY_ERR value.
1677 */
1678static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001679lys_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 +02001680 struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, const struct lysc_type *base,
1681 struct lysc_type **type)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001682{
1683 LY_ERR ret = LY_SUCCESS;
1684 struct lysc_type_bin *bin;
1685 struct lysc_type_num *num;
1686 struct lysc_type_str *str;
1687 struct lysc_type_bits *bits;
1688 struct lysc_type_enum *enumeration;
1689 struct lysc_type_dec *dec;
1690 struct lysc_type_identityref *idref;
1691 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001692 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001693
1694 switch (basetype) {
1695 case LY_TYPE_BINARY:
1696 bin = (struct lysc_type_bin *)(*type);
1697
1698 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1699 if (type_p->length) {
1700 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1701 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1702 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001703 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001704 }
1705 }
1706 break;
1707 case LY_TYPE_BITS:
1708 /* RFC 7950 9.7 - bits */
1709 bits = (struct lysc_type_bits *)(*type);
1710 if (type_p->bits) {
1711 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1712 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1713 (struct lysc_type_bitenum_item **)&bits->bits));
1714 }
1715
1716 if (!base && !type_p->flags) {
1717 /* type derived from bits built-in type must contain at least one bit */
1718 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001719 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001720 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001721 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001722 }
1723 return LY_EVALID;
1724 }
1725 break;
1726 case LY_TYPE_DEC64:
1727 dec = (struct lysc_type_dec *)(*type);
1728
1729 /* RFC 7950 9.3.4 - fraction-digits */
1730 if (!base) {
1731 if (!type_p->fraction_digits) {
1732 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001733 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001734 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001735 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001736 }
1737 return LY_EVALID;
1738 }
1739 dec->fraction_digits = type_p->fraction_digits;
1740 } else {
1741 if (type_p->fraction_digits) {
1742 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1743 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001744 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001745 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1746 tpdfname);
1747 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001748 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001749 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1750 }
1751 return LY_EVALID;
1752 }
1753 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1754 }
1755
1756 /* RFC 7950 9.2.4 - range */
1757 if (type_p->range) {
1758 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1759 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1760 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001761 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001762 }
1763 }
1764 break;
1765 case LY_TYPE_STRING:
1766 str = (struct lysc_type_str *)(*type);
1767
1768 /* RFC 7950 9.4.4 - length */
1769 if (type_p->length) {
1770 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1771 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1772 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001773 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001774 }
1775 } else if (base && ((struct lysc_type_str *)base)->length) {
1776 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1777 }
1778
1779 /* RFC 7950 9.4.5 - pattern */
1780 if (type_p->patterns) {
1781 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1782 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1783 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1784 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1785 }
1786 break;
1787 case LY_TYPE_ENUM:
1788 enumeration = (struct lysc_type_enum *)(*type);
1789
1790 /* RFC 7950 9.6 - enum */
1791 if (type_p->enums) {
1792 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1793 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1794 }
1795
1796 if (!base && !type_p->flags) {
1797 /* type derived from enumerations built-in type must contain at least one enum */
1798 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001799 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001800 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001801 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001802 }
1803 return LY_EVALID;
1804 }
1805 break;
1806 case LY_TYPE_INT8:
1807 case LY_TYPE_UINT8:
1808 case LY_TYPE_INT16:
1809 case LY_TYPE_UINT16:
1810 case LY_TYPE_INT32:
1811 case LY_TYPE_UINT32:
1812 case LY_TYPE_INT64:
1813 case LY_TYPE_UINT64:
1814 num = (struct lysc_type_num *)(*type);
1815
1816 /* RFC 6020 9.2.4 - range */
1817 if (type_p->range) {
1818 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1819 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1820 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001821 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001822 }
1823 }
1824 break;
1825 case LY_TYPE_IDENT:
1826 idref = (struct lysc_type_identityref *)(*type);
1827
1828 /* RFC 7950 9.10.2 - base */
1829 if (type_p->bases) {
1830 if (base) {
1831 /* only the directly derived identityrefs can contain base specification */
1832 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001833 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001834 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1835 tpdfname);
1836 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001837 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001838 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1839 }
1840 return LY_EVALID;
1841 }
aPiecekf4a0a192021-08-03 15:14:17 +02001842 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001843 }
1844
1845 if (!base && !type_p->flags) {
1846 /* type derived from identityref built-in type must contain at least one base */
1847 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001848 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001849 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001850 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001851 }
1852 return LY_EVALID;
1853 }
1854 break;
1855 case LY_TYPE_LEAFREF:
1856 lref = (struct lysc_type_leafref *)*type;
1857
1858 /* RFC 7950 9.9.3 - require-instance */
1859 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001860 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001861 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001862 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001863 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1864 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001865 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001866 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1867 }
1868 return LY_EVALID;
1869 }
1870 lref->require_instance = type_p->require_instance;
1871 } else if (base) {
1872 /* inherit */
1873 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1874 } else {
1875 /* default is true */
1876 lref->require_instance = 1;
1877 }
1878 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001879 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001880
Michal Vaskoe33134a2022-07-29 14:54:40 +02001881 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, 0, 0, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001882 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 +02001883 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001884 } else if (base) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02001885 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 +02001886 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001887 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001888 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001889 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001890 return LY_EVALID;
1891 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001892 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001893 return LY_EVALID;
1894 }
1895 break;
1896 case LY_TYPE_INST:
1897 /* RFC 7950 9.9.3 - require-instance */
1898 if (type_p->flags & LYS_SET_REQINST) {
1899 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1900 } else {
1901 /* default is true */
1902 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1903 }
1904 break;
1905 case LY_TYPE_UNION:
1906 un = (struct lysc_type_union *)(*type);
1907
1908 /* RFC 7950 7.4 - type */
1909 if (type_p->types) {
1910 if (base) {
1911 /* only the directly derived union can contain types specification */
1912 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001913 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001914 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1915 tpdfname);
1916 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001917 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001918 "Invalid type substatement for the type not directly derived from union built-in type.");
1919 }
1920 return LY_EVALID;
1921 }
1922 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001923 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 +02001924 }
1925
1926 if (!base && !type_p->flags) {
1927 /* type derived from union built-in type must contain at least one type */
1928 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001929 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001930 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001931 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001932 }
1933 return LY_EVALID;
1934 }
1935 break;
1936 case LY_TYPE_BOOL:
1937 case LY_TYPE_EMPTY:
1938 case LY_TYPE_UNKNOWN: /* just to complete switch */
1939 break;
1940 }
1941
1942 if (tpdfname) {
1943 switch (basetype) {
1944 case LY_TYPE_BINARY:
1945 type_p->compiled = *type;
1946 *type = calloc(1, sizeof(struct lysc_type_bin));
1947 break;
1948 case LY_TYPE_BITS:
1949 type_p->compiled = *type;
1950 *type = calloc(1, sizeof(struct lysc_type_bits));
1951 break;
1952 case LY_TYPE_DEC64:
1953 type_p->compiled = *type;
1954 *type = calloc(1, sizeof(struct lysc_type_dec));
1955 break;
1956 case LY_TYPE_STRING:
1957 type_p->compiled = *type;
1958 *type = calloc(1, sizeof(struct lysc_type_str));
1959 break;
1960 case LY_TYPE_ENUM:
1961 type_p->compiled = *type;
1962 *type = calloc(1, sizeof(struct lysc_type_enum));
1963 break;
1964 case LY_TYPE_INT8:
1965 case LY_TYPE_UINT8:
1966 case LY_TYPE_INT16:
1967 case LY_TYPE_UINT16:
1968 case LY_TYPE_INT32:
1969 case LY_TYPE_UINT32:
1970 case LY_TYPE_INT64:
1971 case LY_TYPE_UINT64:
1972 type_p->compiled = *type;
1973 *type = calloc(1, sizeof(struct lysc_type_num));
1974 break;
1975 case LY_TYPE_IDENT:
1976 type_p->compiled = *type;
1977 *type = calloc(1, sizeof(struct lysc_type_identityref));
1978 break;
1979 case LY_TYPE_LEAFREF:
1980 type_p->compiled = *type;
1981 *type = calloc(1, sizeof(struct lysc_type_leafref));
1982 break;
1983 case LY_TYPE_INST:
1984 type_p->compiled = *type;
1985 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1986 break;
1987 case LY_TYPE_UNION:
1988 type_p->compiled = *type;
1989 *type = calloc(1, sizeof(struct lysc_type_union));
1990 break;
1991 case LY_TYPE_BOOL:
1992 case LY_TYPE_EMPTY:
1993 case LY_TYPE_UNKNOWN: /* just to complete switch */
1994 break;
1995 }
1996 }
1997 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1998
1999cleanup:
2000 return ret;
2001}
2002
2003LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01002004lys_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 +02002005 const struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002006{
2007 LY_ERR ret = LY_SUCCESS;
2008 ly_bool dummyloops = 0;
2009 struct type_context {
2010 const struct lysp_tpdf *tpdf;
2011 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002012 } *tctx, *tctx_prev = NULL, *tctx_iter;
2013 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
2014 struct lysc_type *base = NULL, *prev_type;
2015 struct ly_set tpdf_chain = {0};
Michal Vasko388a6632021-08-06 11:27:43 +02002016 struct lyplg_type *plugin;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002017
2018 (*type) = NULL;
2019 if (dflt) {
2020 *dflt = NULL;
2021 }
2022
2023 tctx = calloc(1, sizeof *tctx);
2024 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002025 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 +02002026 ret == LY_SUCCESS;
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002027 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 +01002028 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002029 if (basetype) {
2030 break;
2031 }
2032
2033 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01002034 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
2035 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002036 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2037
2038 if (units && !*units) {
2039 /* inherit units */
2040 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
2041 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2042 }
2043 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
2044 /* inherit default */
2045 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
2046 }
2047 if (dummyloops && (!units || *units) && dflt && *dflt) {
2048 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
2049 break;
2050 }
2051
Michal Vasko080064b2021-08-31 15:20:44 +02002052 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
Radek Krejci720d2612021-03-03 19:44:22 +01002053 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
2054 * and we need now recompile the type again in the updated context. */
Michal Vaskoc636ea42022-09-16 10:20:31 +02002055 lysc_type_free(&ctx->free_ctx, tctx->tpdf->type.compiled);
Radek Krejci720d2612021-03-03 19:44:22 +01002056 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
2057 }
2058
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002059 if (tctx->tpdf->type.compiled) {
2060 /* it is not necessary to continue, the rest of the chain was already compiled,
2061 * but we still may need to inherit default and units values, so start dummy loops */
2062 basetype = tctx->tpdf->type.compiled->basetype;
2063 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2064 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2065
2066 if ((units && !*units) || (dflt && !*dflt)) {
2067 dummyloops = 1;
2068 goto preparenext;
2069 } else {
2070 tctx = NULL;
2071 break;
2072 }
2073 }
2074
2075 /* circular typedef reference detection */
2076 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
2077 /* local part */
2078 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002079 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002080 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002081 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2082 free(tctx);
2083 ret = LY_EVALID;
2084 goto cleanup;
2085 }
2086 }
2087 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
2088 /* global part for unions corner case */
2089 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002090 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002091 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002092 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2093 free(tctx);
2094 ret = LY_EVALID;
2095 goto cleanup;
2096 }
2097 }
2098
2099 /* store information for the following processing */
2100 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2101 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2102
2103preparenext:
2104 /* prepare next loop */
2105 tctx_prev = tctx;
2106 tctx = calloc(1, sizeof *tctx);
2107 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2108 }
2109 free(tctx);
2110
2111 /* allocate type according to the basetype */
2112 switch (basetype) {
2113 case LY_TYPE_BINARY:
2114 *type = calloc(1, sizeof(struct lysc_type_bin));
2115 break;
2116 case LY_TYPE_BITS:
2117 *type = calloc(1, sizeof(struct lysc_type_bits));
2118 break;
2119 case LY_TYPE_BOOL:
2120 case LY_TYPE_EMPTY:
2121 *type = calloc(1, sizeof(struct lysc_type));
2122 break;
2123 case LY_TYPE_DEC64:
2124 *type = calloc(1, sizeof(struct lysc_type_dec));
2125 break;
2126 case LY_TYPE_ENUM:
2127 *type = calloc(1, sizeof(struct lysc_type_enum));
2128 break;
2129 case LY_TYPE_IDENT:
2130 *type = calloc(1, sizeof(struct lysc_type_identityref));
2131 break;
2132 case LY_TYPE_INST:
2133 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2134 break;
2135 case LY_TYPE_LEAFREF:
2136 *type = calloc(1, sizeof(struct lysc_type_leafref));
2137 break;
2138 case LY_TYPE_STRING:
2139 *type = calloc(1, sizeof(struct lysc_type_str));
2140 break;
2141 case LY_TYPE_UNION:
2142 *type = calloc(1, sizeof(struct lysc_type_union));
2143 break;
2144 case LY_TYPE_INT8:
2145 case LY_TYPE_UINT8:
2146 case LY_TYPE_INT16:
2147 case LY_TYPE_UINT16:
2148 case LY_TYPE_INT32:
2149 case LY_TYPE_UINT32:
2150 case LY_TYPE_INT64:
2151 case LY_TYPE_UINT64:
2152 *type = calloc(1, sizeof(struct lysc_type_num));
2153 break;
2154 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002155 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002156 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2157 ret = LY_EVALID;
2158 goto cleanup;
2159 }
2160 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
2161 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002162 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002163 ly_data_type2str[basetype]);
2164 free(*type);
2165 (*type) = NULL;
2166 ret = LY_EVALID;
2167 goto cleanup;
2168 }
2169
2170 /* get restrictions from the referred typedefs */
2171 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2172 tctx = (struct type_context *)tpdf_chain.objs[u];
2173
2174 /* remember the typedef context for circular check */
2175 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
2176 LY_CHECK_GOTO(ret, cleanup);
2177
2178 if (tctx->tpdf->type.compiled) {
Michal Vasko388a6632021-08-06 11:27:43 +02002179 /* already compiled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002180 base = tctx->tpdf->type.compiled;
2181 continue;
Michal Vasko388a6632021-08-06 11:27:43 +02002182 }
2183
Michal Vaskoddd76592022-01-17 13:34:48 +01002184 /* try to find loaded user type plugins */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002185 plugin = lyplg_type_plugin_find(tctx->tpdf->type.pmod->mod->name, tctx->tpdf->type.pmod->mod->revision,
Michal Vaskoddd76592022-01-17 13:34:48 +01002186 tctx->tpdf->name);
Haithem Ben Ghorbal45be15f2022-07-12 18:02:36 +02002187 if (!plugin && base) {
2188 /* use the base type implementation if available */
2189 plugin = base->plugin;
2190 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002191 if (!plugin) {
2192 /* use the internal built-in type implementation */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002193 plugin = lyplg_type_plugin_find("", NULL, ly_data_type2str[basetype]);
Michal Vaskoddd76592022-01-17 13:34:48 +01002194 }
Michal Vasko388a6632021-08-06 11:27:43 +02002195 assert(plugin);
2196
2197 if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags) &&
2198 (plugin == base->plugin)) {
2199 /* no change, reuse the compiled base */
2200 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002201 LY_ATOMIC_INC_BARRIER(base->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002202 continue;
2203 }
2204
Michal Vasko04338d92021-09-01 07:58:14 +02002205 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002206 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002207 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002208 tctx->tpdf->name, ly_data_type2str[basetype]);
2209 ret = LY_EVALID;
2210 goto cleanup;
2211 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002212 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002213 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2214 tctx->tpdf->name, tctx->tpdf->dflt.str);
2215 ret = LY_EVALID;
2216 goto cleanup;
2217 }
2218
2219 (*type)->basetype = basetype;
Michal Vasko388a6632021-08-06 11:27:43 +02002220 (*type)->plugin = plugin;
2221
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002222 /* collect extensions */
2223 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko388a6632021-08-06 11:27:43 +02002224
2225 /* compile the new typedef */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002226 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01002227 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002228 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
2229 LY_CHECK_GOTO(ret, cleanup);
2230 base = prev_type;
2231 }
2232 /* remove the processed typedef contexts from the stack for circular check */
2233 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
2234
2235 /* process the type definition in leaf */
2236 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
2237 /* get restrictions from the node itself */
2238 (*type)->basetype = basetype;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002239 (*type)->plugin = base ? base->plugin : lyplg_type_plugin_find("", NULL, ly_data_type2str[basetype]);
Michal Vasko04338d92021-09-01 07:58:14 +02002240 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko193dacd2022-10-13 08:43:05 +02002241 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, (struct lysp_type *)type_p, basetype,
2242 NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002243 LY_CHECK_GOTO(ret, cleanup);
2244 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
2245 /* no specific restriction in leaf's type definition, copy from the base */
2246 free(*type);
2247 (*type) = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002248 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002249 }
2250
Radek Krejciab430862021-03-02 20:13:40 +01002251 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002252
2253cleanup:
2254 ly_set_erase(&tpdf_chain, free);
2255 return ret;
2256}
2257
2258/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002259 * @brief Check uniqness of the node/action/notification name.
2260 *
2261 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2262 * structures, but they share the namespace so we need to check their name collisions.
2263 *
2264 * @param[in] ctx Compile context.
2265 * @param[in] parent Parent of the nodes to check, can be NULL.
2266 * @param[in] name Name of the item to find in the given lists.
2267 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2268 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2269 */
2270static LY_ERR
2271lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2272 const struct lysc_node *exclude)
2273{
2274 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002275 const struct lysc_node_action *actions;
2276 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002277 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002278 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002279
2280#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2281
2282 if (exclude->nodetype == LYS_CASE) {
2283 /* check restricted only to all the cases */
2284 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002285 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002286 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002287 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002288 return LY_EEXIST;
2289 }
2290 }
2291
2292 return LY_SUCCESS;
2293 }
2294
2295 /* no reason for our parent to be choice anymore */
2296 assert(!parent || (parent->nodetype != LYS_CHOICE));
2297
2298 if (parent && (parent->nodetype == LYS_CASE)) {
2299 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002300
2301 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2302 iter = lysc_data_parent(parent);
2303 do {
2304 parent = parent->parent;
2305 if (parent && (parent->nodetype == LYS_CHOICE)) {
2306 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2307 }
2308 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002309 }
2310
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002311 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002312 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2313 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2314 if (exclude->flags & LYS_IS_OUTPUT) {
2315 getnext_flags |= LYS_GETNEXT_OUTPUT;
2316 parent = lysc_node_child(parent)->next;
2317 } else {
2318 parent = lysc_node_child(parent);
2319 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002320 }
2321
2322 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002323 if (!parent && ctx->ext) {
2324 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2325 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2326 goto error;
2327 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002328
Radek Krejci5fa32a32021-02-08 18:12:38 +01002329 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2330 if (iter->nodetype == LYS_CHOICE) {
2331 iter2 = NULL;
2332 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2333 if (CHECK_NODE(iter2, exclude, name)) {
2334 goto error;
2335 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002336 }
2337 }
2338 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002339 } else {
2340 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2341 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2342 goto error;
2343 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002344
Radek Krejci5fa32a32021-02-08 18:12:38 +01002345 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2346 if (iter->nodetype == LYS_CHOICE) {
2347 iter2 = NULL;
2348 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2349 if (CHECK_NODE(iter2, exclude, name)) {
2350 goto error;
2351 }
2352 }
2353 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002354 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002355
Radek Krejci5fa32a32021-02-08 18:12:38 +01002356 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2357 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2358 if (CHECK_NODE(iter, exclude, name)) {
2359 goto error;
2360 }
2361 }
2362
2363 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2364 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2365 if (CHECK_NODE(iter, exclude, name)) {
2366 goto error;
2367 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002368 }
2369 }
Radek Krejci078e4342021-02-12 18:17:26 +01002370 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002371 return LY_SUCCESS;
2372
2373error:
Radek Krejci078e4342021-02-12 18:17:26 +01002374 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002375 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002376 return LY_EEXIST;
2377
2378#undef CHECK_NODE
2379}
2380
Michal Vasko193dacd2022-10-13 08:43:05 +02002381LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002382lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002383{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002384 struct lysc_node **children, *anchor = NULL;
2385 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002386
Radek Krejci2a9fc652021-01-22 17:44:34 +01002387 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002388
Radek Krejci2a9fc652021-01-22 17:44:34 +01002389 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002390 if (node->nodetype == LYS_INPUT) {
2391 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2392 /* input node is part of the action but link it with output */
2393 node->next = &((struct lysc_node_action *)parent)->output.node;
2394 node->prev = node->next;
2395 return LY_SUCCESS;
2396 } else if (node->nodetype == LYS_OUTPUT) {
2397 /* output node is part of the action but link it with input */
2398 node->next = NULL;
2399 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002400 return LY_SUCCESS;
2401 } else if (node->nodetype == LYS_ACTION) {
2402 children = (struct lysc_node **)lysc_node_actions_p(parent);
2403 } else if (node->nodetype == LYS_NOTIF) {
2404 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2405 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002406 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002407 }
2408 assert(children);
2409
2410 if (!(*children)) {
2411 /* first child */
2412 *children = node;
2413 } else if (node->flags & LYS_KEY) {
2414 /* special handling of adding keys */
2415 assert(node->module == parent->module);
2416 anchor = *children;
2417 if (anchor->flags & LYS_KEY) {
2418 while ((anchor->flags & LYS_KEY) && anchor->next) {
2419 anchor = anchor->next;
2420 }
2421 /* insert after the last key */
2422 insert_after = 1;
2423 } /* else insert before anchor (at the beginning) */
2424 } else if ((*children)->prev->module == node->module) {
2425 /* last child is from the same module, keep the order and insert at the end */
2426 anchor = (*children)->prev;
2427 insert_after = 1;
2428 } else if (parent->module == node->module) {
2429 /* adding module child after some augments were connected */
2430 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2431 } else {
2432 /* some augments are already connected and we are connecting new ones,
2433 * keep module name order and insert the node into the children list */
2434 anchor = *children;
2435 do {
2436 anchor = anchor->prev;
2437
2438 /* check that we have not found the last augment node from our module or
2439 * the first augment node from a "smaller" module or
2440 * the first node from a local module */
2441 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2442 (anchor->module == parent->module)) {
2443 /* insert after */
2444 insert_after = 1;
2445 break;
2446 }
2447
2448 /* we have traversed all the nodes, insert before anchor (as the first node) */
2449 } while (anchor->prev->next);
2450 }
2451
2452 /* insert */
2453 if (anchor) {
2454 if (insert_after) {
2455 node->next = anchor->next;
2456 node->prev = anchor;
2457 anchor->next = node;
2458 if (node->next) {
2459 /* middle node */
2460 node->next->prev = node;
2461 } else {
2462 /* last node */
2463 (*children)->prev = node;
2464 }
2465 } else {
2466 node->next = anchor;
2467 node->prev = anchor->prev;
2468 anchor->prev = node;
2469 if (anchor == *children) {
2470 /* first node */
2471 *children = node;
2472 } else {
2473 /* middle node */
2474 node->prev->next = node;
2475 }
2476 }
2477 }
2478
2479 /* check the name uniqueness (even for an only child, it may be in case) */
2480 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2481 return LY_EEXIST;
2482 }
2483 } else {
2484 /* top-level element */
2485 struct lysc_node **list;
2486
Radek Krejci6b88a462021-02-17 12:39:34 +01002487 if (ctx->ext) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002488 lyplg_ext_get_storage_p(ctx->ext, LY_STMT_DATA_NODE_MASK, (const void ***)&list);
Radek Krejci6b88a462021-02-17 12:39:34 +01002489 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002490 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2491 } else if (node->nodetype == LYS_NOTIF) {
2492 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2493 } else {
2494 list = &ctx->cur_mod->compiled->data;
2495 }
2496 if (!(*list)) {
2497 *list = node;
2498 } else {
2499 /* insert at the end of the module's top-level nodes list */
2500 (*list)->prev->next = node;
2501 node->prev = (*list)->prev;
2502 (*list)->prev = node;
2503 }
2504
2505 /* check the name uniqueness on top-level */
2506 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2507 return LY_EEXIST;
2508 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002509 }
2510
Radek Krejci2a9fc652021-01-22 17:44:34 +01002511 return LY_SUCCESS;
2512}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002513
Radek Krejci2a9fc652021-01-22 17:44:34 +01002514/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002515 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002516 *
2517 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002518 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002519 * @return LY_ERR value.
2520 */
2521static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002522lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002523{
2524 /* case never has any explicit config */
2525 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002526
Michal Vasko7c565922021-06-10 14:58:27 +02002527 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Radek Krejci91531e12021-02-08 19:57:54 +01002528 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002529 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002530 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2531 /* config not explicitly set, inherit it from parent */
Michal Vaskoecdc2222022-01-24 08:45:44 +01002532 assert(!node->parent || (node->parent->flags & LYS_CONFIG_MASK) || (node->parent->nodetype & LYS_AUGMENT));
2533 if (node->parent && (node->parent->flags & LYS_CONFIG_MASK)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002534 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002535 } else {
2536 /* default is config true */
2537 node->flags |= LYS_CONFIG_W;
2538 }
2539 } else {
2540 /* config set explicitly */
2541 node->flags |= LYS_SET_CONFIG;
2542 }
2543
Radek Krejci91531e12021-02-08 19:57:54 +01002544 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002545 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002546 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002547 }
2548
Radek Krejci2a9fc652021-01-22 17:44:34 +01002549 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002550}
2551
Radek Krejci91531e12021-02-08 19:57:54 +01002552/**
2553 * @brief Set various flags of the compiled nodes
2554 *
2555 * @param[in] ctx Compile context.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002556 * @param[in] parsed_flags Parsed node flags.
2557 * @param[in] inherited_flags Inherited flags from a schema-only statement.
2558 * @param[in,out] node Compiled node where the flags will be set.
Radek Krejci91531e12021-02-08 19:57:54 +01002559 */
2560static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002561lys_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 +01002562{
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002563 uint16_t parent_flags;
2564 const char *parent_name;
2565
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002566 /* copy flags except for status */
2567 node->flags = (parsed_flags & LYS_FLAGS_COMPILED_MASK) & ~LYS_STATUS_MASK;
2568
Radek Krejci91531e12021-02-08 19:57:54 +01002569 /* inherit config flags */
2570 LY_CHECK_RET(lys_compile_config(ctx, node));
2571
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002572 /* compile status */
2573 parent_flags = node->parent ? node->parent->flags : 0;
2574 parent_name = node->parent ? node->parent->name : NULL;
2575 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 +01002576
2577 /* other flags */
Michal Vasko7c565922021-06-10 14:58:27 +02002578 if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002579 node->flags |= LYS_IS_INPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002580 } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002581 node->flags |= LYS_IS_OUTPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002582 } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002583 node->flags |= LYS_IS_NOTIF;
2584 }
2585
2586 return LY_SUCCESS;
2587}
2588
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002589static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002590lys_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 +01002591 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2592 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002593{
2594 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002595 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002596 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002597 struct lysp_when *pwhen = NULL;
Michal Vaskoe02e7402021-07-23 08:29:28 +02002598 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002599
Radek Krejci2a9fc652021-01-22 17:44:34 +01002600 node->nodetype = pnode->nodetype;
2601 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002602 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002603 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002604 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002605
Radek Krejci2a9fc652021-01-22 17:44:34 +01002606 /* compile any deviations for this node */
2607 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vaskoe02e7402021-07-23 08:29:28 +02002608 if (not_supported && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2609 /* if not supported, keep it just like disabled nodes by if-feature */
2610 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
2611 ctx->compile_opts |= LYS_COMPILE_DISABLED;
2612 }
2613 if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002614 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002615 }
2616
Michal Vaskodfd254d2021-06-24 09:24:59 +02002617 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2618 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2619 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002620
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002621 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002622 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002623 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002624 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002625 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002626 }
2627
Radek Krejci91531e12021-02-08 19:57:54 +01002628 /* config, status and other flags */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02002629 LY_CHECK_GOTO(ret = lys_compile_node_flags(ctx, pnode->flags, inherited_flags, node), error);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002630
2631 /* list ordering */
2632 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002633 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002634 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002635 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002636 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002637 node->flags &= ~LYS_ORDBY_MASK;
2638 node->flags |= LYS_ORDBY_SYSTEM;
2639 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2640 /* default ordering is system */
2641 node->flags |= LYS_ORDBY_SYSTEM;
2642 }
2643 }
2644
Radek Krejci2a9fc652021-01-22 17:44:34 +01002645 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2646 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2647
Radek Krejci9a3823e2021-01-27 20:26:46 +01002648 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002649 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002650 ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002651 LY_CHECK_GOTO(ret, cleanup);
2652 }
2653
Radek Krejci2a9fc652021-01-22 17:44:34 +01002654 /* nodetype-specific part */
2655 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2656
2657 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002658 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002659 if (node->flags & LYS_MAND_TRUE) {
2660 /* inherit LYS_MAND_TRUE in parent containers */
2661 lys_compile_mandatory_parents(parent, 1);
2662 }
2663
2664 if (child_set) {
2665 /* add the new node into set */
2666 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2667 }
2668
2669 goto cleanup;
2670
2671error:
Michal Vaskoc636ea42022-09-16 10:20:31 +02002672 lysc_node_free(&ctx->free_ctx, node, 0);
2673
Radek Krejci2a9fc652021-01-22 17:44:34 +01002674cleanup:
2675 if (ret && dev_pnode) {
2676 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2677 }
Michal Vaskoe02e7402021-07-23 08:29:28 +02002678 ctx->compile_opts = prev_opts;
Michal Vaskoc636ea42022-09-16 10:20:31 +02002679 lysp_dev_node_free(ctx, dev_pnode);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002680 return ret;
2681}
2682
Radek Krejci2a9fc652021-01-22 17:44:34 +01002683LY_ERR
2684lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2685{
2686 LY_ERR ret = LY_SUCCESS;
2687 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002688 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002689
2690 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2691 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2692
2693 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002694 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002695 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002696
Radek Krejci01180ac2021-01-27 08:48:22 +01002697 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002698 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2699 }
2700
Michal Vasko95f736c2022-06-08 12:03:31 +02002701 /* connect any augments */
2702 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2703
Michal Vasko7c565922021-06-10 14:58:27 +02002704 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002705
2706done:
2707 return ret;
2708}
2709
2710/**
2711 * @brief Compile parsed action node information.
Michal Vasko193dacd2022-10-13 08:43:05 +02002712 *
Radek Krejci2a9fc652021-01-22 17:44:34 +01002713 * @param[in] ctx Compile context
2714 * @param[in] pnode Parsed action node.
2715 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2716 * is enriched with the action-specific information.
2717 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2718 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002719static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002720lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2721{
2722 LY_ERR ret;
2723 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2724 struct lysc_node_action *action = (struct lysc_node_action *)node;
2725 struct lysp_node_action_inout *input, implicit_input = {
2726 .nodetype = LYS_INPUT,
2727 .name = "input",
2728 .parent = pnode,
2729 };
2730 struct lysp_node_action_inout *output, implicit_output = {
2731 .nodetype = LYS_OUTPUT,
2732 .name = "output",
2733 .parent = pnode,
2734 };
2735
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002736 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002737 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002738 if (action_p->input.nodetype == LYS_UNKNOWN) {
2739 input = &implicit_input;
2740 } else {
2741 input = &action_p->input;
2742 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002743 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 +01002744 lysc_update_path(ctx, NULL, NULL);
2745 LY_CHECK_GOTO(ret, done);
2746
Michal Vaskoc130e162021-10-19 11:30:00 +02002747 /* add must(s) to unres */
2748 ret = lysc_unres_must_add(ctx, &action->input.node, &input->node);
2749 LY_CHECK_GOTO(ret, done);
2750
Radek Krejci2a9fc652021-01-22 17:44:34 +01002751 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002752 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002753 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002754 output = &implicit_output;
2755 } else {
2756 output = &action_p->output;
2757 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002758 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 +01002759 lysc_update_path(ctx, NULL, NULL);
2760 LY_CHECK_GOTO(ret, done);
2761
Michal Vaskoc130e162021-10-19 11:30:00 +02002762 /* add must(s) to unres */
2763 ret = lysc_unres_must_add(ctx, &action->output.node, &output->node);
2764 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002765
2766done:
2767 return ret;
2768}
2769
2770/**
2771 * @brief Compile parsed action node information.
2772 * @param[in] ctx Compile context
2773 * @param[in] pnode Parsed action node.
2774 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2775 * is enriched with the action-specific information.
2776 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2777 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002778static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002779lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2780{
2781 LY_ERR ret = LY_SUCCESS;
2782 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2783 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2784 struct lysp_node *child_p;
2785
2786 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002787
2788 /* add must(s) to unres */
2789 ret = lysc_unres_must_add(ctx, node, pnode);
2790 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002791
Radek Krejci01180ac2021-01-27 08:48:22 +01002792 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002793 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002794 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002795 }
2796
Michal Vasko95f736c2022-06-08 12:03:31 +02002797 /* connect any augments */
2798 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2799
Radek Krejci2a9fc652021-01-22 17:44:34 +01002800done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002801 return ret;
2802}
2803
2804/**
2805 * @brief Compile parsed container node information.
2806 * @param[in] ctx Compile context
2807 * @param[in] pnode Parsed container node.
2808 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2809 * is enriched with the container-specific information.
2810 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2811 */
2812static LY_ERR
2813lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2814{
2815 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2816 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2817 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002818 LY_ERR ret = LY_SUCCESS;
2819
2820 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002821 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002822 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002823 }
2824
2825 /* more cases when the container has meaning but is kept NP for convenience:
2826 * - when condition
2827 * - direct child action/notification
2828 */
2829
2830 LY_LIST_FOR(cont_p->child, child_p) {
2831 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2832 LY_CHECK_GOTO(ret, done);
2833 }
2834
Michal Vasko5347e3a2020-11-03 17:14:57 +01002835 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002836
2837 /* add must(s) to unres */
2838 ret = lysc_unres_must_add(ctx, node, pnode);
2839 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002840
Michal Vasko95f736c2022-06-08 12:03:31 +02002841 /* connect any augments */
2842 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2843
Radek Krejci2a9fc652021-01-22 17:44:34 +01002844 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2845 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2846 LY_CHECK_GOTO(ret, done);
2847 }
2848 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2849 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2850 LY_CHECK_GOTO(ret, done);
2851 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002852
2853done:
2854 return ret;
2855}
2856
Michal Vasko72244882021-01-12 15:21:05 +01002857/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002858 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2859 * @param[in] ctx Compile context.
2860 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2861 * @param[in] type_p Parsed type to compile.
2862 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2863 * @return LY_ERR value.
2864 */
2865static LY_ERR
2866lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2867 struct lysc_node_leaf *leaf)
2868{
2869 struct lysp_qname *dflt;
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002870 struct lysc_type **t;
2871 LY_ARRAY_COUNT_TYPE u, count;
2872 ly_bool in_unres = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002873
Michal Vaskoa99b3572021-02-01 11:54:58 +01002874 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002875 leaf->units ? NULL : &leaf->units, &dflt));
2876
2877 /* store default value, if any */
2878 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2879 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2880 }
2881
Michal Vaskoc130e162021-10-19 11:30:00 +02002882 /* store leafref(s) to be resolved */
2883 LY_CHECK_RET(lysc_unres_leafref_add(ctx, leaf, type_p->pmod));
2884
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002885 /* type-specific checks */
2886 if (leaf->type->basetype == LY_TYPE_UNION) {
2887 t = ((struct lysc_type_union *)leaf->type)->types;
2888 count = LY_ARRAY_COUNT(t);
2889 } else {
2890 t = &leaf->type;
2891 count = 1;
2892 }
2893 for (u = 0; u < count; ++u) {
2894 if (t[u]->basetype == LY_TYPE_EMPTY) {
2895 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
2896 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2897 return LY_EVALID;
2898 }
2899 } else if (!in_unres && ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM))) {
2900 /* store in unres for all disabled bits/enums to be removed */
2901 LY_CHECK_RET(lysc_unres_bitenum_add(ctx, leaf));
2902 in_unres = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002903 }
2904 }
2905
2906 return LY_SUCCESS;
2907}
2908
2909/**
2910 * @brief Compile parsed leaf node information.
2911 * @param[in] ctx Compile context
2912 * @param[in] pnode Parsed leaf node.
2913 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2914 * is enriched with the leaf-specific information.
2915 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2916 */
2917static LY_ERR
2918lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2919{
2920 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2921 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002922 LY_ERR ret = LY_SUCCESS;
2923
Michal Vasko5347e3a2020-11-03 17:14:57 +01002924 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002925
2926 /* add must(s) to unres */
2927 ret = lysc_unres_must_add(ctx, node, pnode);
2928 LY_CHECK_GOTO(ret, done);
2929
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002930 if (leaf_p->units) {
2931 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2932 leaf->flags |= LYS_SET_UNITS;
2933 }
2934
2935 /* compile type */
2936 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2937 LY_CHECK_GOTO(ret, done);
2938
2939 /* store/update default value */
2940 if (leaf_p->dflt.str) {
2941 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2942 leaf->flags |= LYS_SET_DFLT;
2943 }
2944
2945 /* checks */
2946 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002947 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory leaf with a default value.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002948 return LY_EVALID;
2949 }
2950
2951done:
2952 return ret;
2953}
2954
2955/**
2956 * @brief Compile parsed leaf-list node information.
2957 * @param[in] ctx Compile context
2958 * @param[in] pnode Parsed leaf-list node.
2959 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2960 * is enriched with the leaf-list-specific information.
2961 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2962 */
2963static LY_ERR
2964lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2965{
2966 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2967 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002968 LY_ERR ret = LY_SUCCESS;
2969
Michal Vasko5347e3a2020-11-03 17:14:57 +01002970 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002971
2972 /* add must(s) to unres */
2973 ret = lysc_unres_must_add(ctx, node, pnode);
2974 LY_CHECK_GOTO(ret, done);
2975
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002976 if (llist_p->units) {
2977 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2978 llist->flags |= LYS_SET_UNITS;
2979 }
2980
2981 /* compile type */
2982 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2983 LY_CHECK_GOTO(ret, done);
2984
2985 /* store/update default values */
2986 if (llist_p->dflts) {
2987 if (ctx->pmod->version < LYS_VERSION_1_1) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002988 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list default values are allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002989 return LY_EVALID;
2990 }
2991
2992 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2993 llist->flags |= LYS_SET_DFLT;
2994 }
2995
2996 llist->min = llist_p->min;
2997 if (llist->min) {
2998 llist->flags |= LYS_MAND_TRUE;
2999 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02003000 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
3001
3002 if (llist->flags & LYS_CONFIG_R) {
3003 /* state leaf-list is always ordered-by user */
3004 llist->flags &= ~LYS_ORDBY_SYSTEM;
3005 llist->flags |= LYS_ORDBY_USER;
3006 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003007
3008 /* checks */
3009 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003010 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 +02003011 return LY_EVALID;
3012 }
3013
3014 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003015 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003016 llist->min, llist->max);
3017 return LY_EVALID;
3018 }
3019
3020done:
3021 return ret;
3022}
3023
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003024/**
3025 * @brief Find the node according to the given descendant/absolute schema nodeid.
3026 * Used in unique, refine and augment statements.
3027 *
3028 * @param[in] ctx Compile context
3029 * @param[in] nodeid Descendant-schema-nodeid (according to the YANG grammar)
3030 * @param[in] nodeid_len Length of the given nodeid, if it is not NULL-terminated string.
3031 * @param[in] ctx_node Context node for a relative nodeid.
3032 * @param[in] format Format of any prefixes.
3033 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
3034 * @param[in] nodetype Optional (can be 0) restriction for target's nodetype. If target exists, but does not match
3035 * the given nodetype, LY_EDENIED is returned (and target is provided), but no error message is printed.
3036 * The value can be even an ORed value to allow multiple nodetypes.
3037 * @param[out] target Found target node if any.
3038 * @param[out] result_flag Output parameter to announce if the schema nodeid goes through the action's input/output or a Notification.
3039 * The LYSC_OPT_RPC_INPUT, LYSC_OPT_RPC_OUTPUT and LYSC_OPT_NOTIFICATION are used as flags.
3040 * @return LY_ERR values - LY_ENOTFOUND, LY_EVALID, LY_EDENIED or LY_SUCCESS.
3041 */
3042static LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003043lysc_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 +01003044 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 +02003045{
3046 LY_ERR ret = LY_EVALID;
3047 const char *name, *prefix, *id;
3048 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01003049 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003050 const char *nodeid_type;
3051 uint32_t getnext_extra_flag = 0;
3052 uint16_t current_nodetype = 0;
3053
3054 assert(nodeid);
3055 assert(target);
3056 assert(result_flag);
3057 *target = NULL;
3058 *result_flag = 0;
3059
3060 id = nodeid;
3061
3062 if (ctx_node) {
3063 /* descendant-schema-nodeid */
3064 nodeid_type = "descendant";
3065
3066 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003067 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003068 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01003069 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003070 return LY_EVALID;
3071 }
3072 } else {
3073 /* absolute-schema-nodeid */
3074 nodeid_type = "absolute";
3075
3076 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003077 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003078 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003079 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003080 return LY_EVALID;
3081 }
3082 ++id;
3083 }
3084
3085 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
3086 if (prefix) {
3087 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
3088 if (!mod) {
3089 /* module must always be found */
3090 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01003091 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003092 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003093 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003094 return LY_ENOTFOUND;
3095 }
3096 } else {
3097 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003098 case LY_VALUE_SCHEMA:
3099 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003100 /* use the current module */
Michal Vasko56d8da82021-12-16 15:41:30 +01003101 mod = ctx->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003102 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003103 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02003104 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003105 if (!ctx_node) {
3106 LOGINT_RET(ctx->ctx);
3107 }
3108 /* inherit the module of the previous context node */
3109 mod = ctx_node->module;
3110 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02003111 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02003112 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01003113 case LY_VALUE_STR_NS:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003114 /* not really defined */
3115 LOGINT_RET(ctx->ctx);
3116 }
3117 }
3118
3119 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
3120 /* move through input/output manually */
3121 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01003122 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3123 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003124 return LY_ENOTFOUND;
3125 }
3126 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003127 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003128 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003129 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003130 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
3131 } else {
3132 /* only input or output is valid */
3133 ctx_node = NULL;
3134 }
Michal Vasko0b50f6b2022-10-05 15:07:55 +02003135 } else if (ctx->ext && !ctx_node) {
3136 /* top-level extension nodes */
3137 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 +02003138 } else {
3139 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003140 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003141 getnext_extra_flag = 0;
3142 }
3143 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01003144 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3145 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003146 return LY_ENOTFOUND;
3147 }
3148 current_nodetype = ctx_node->nodetype;
3149
3150 if (current_nodetype == LYS_NOTIF) {
3151 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
3152 } else if (current_nodetype == LYS_INPUT) {
3153 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
3154 } else if (current_nodetype == LYS_OUTPUT) {
3155 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
3156 }
3157
3158 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
3159 break;
3160 }
3161 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003162 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003163 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01003164 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003165 return LY_EVALID;
3166 }
3167 ++id;
3168 }
3169
3170 if (ret == LY_SUCCESS) {
3171 *target = ctx_node;
3172 if (nodetype && !(current_nodetype & nodetype)) {
3173 return LY_EDENIED;
3174 }
3175 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003176 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003177 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01003178 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003179 }
3180
3181 return ret;
3182}
3183
3184/**
3185 * @brief Compile information about list's uniques.
3186 * @param[in] ctx Compile context.
3187 * @param[in] uniques Sized array list of unique statements.
3188 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3189 * @return LY_ERR value.
3190 */
3191static LY_ERR
3192lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
3193{
3194 LY_ERR ret = LY_SUCCESS;
3195 struct lysc_node_leaf **key, ***unique;
3196 struct lysc_node *parent;
3197 const char *keystr, *delim;
3198 size_t len;
3199 LY_ARRAY_COUNT_TYPE v;
3200 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
3201 uint16_t flags;
3202
3203 LY_ARRAY_FOR(uniques, v) {
3204 config = -1;
3205 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3206 keystr = uniques[v].str;
3207 while (keystr) {
3208 delim = strpbrk(keystr, " \t\n");
3209 if (delim) {
3210 len = delim - keystr;
3211 while (isspace(*delim)) {
3212 ++delim;
3213 }
3214 } else {
3215 len = strlen(keystr);
3216 }
3217
3218 /* unique node must be present */
3219 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko56d8da82021-12-16 15:41:30 +01003220 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, LY_VALUE_SCHEMA, (void *)uniques[v].mod,
3221 LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003222 if (ret != LY_SUCCESS) {
3223 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003224 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003225 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01003226 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003227 }
3228 return LY_EVALID;
3229 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003230 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003231 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003232 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003233 return LY_EVALID;
3234 }
3235
3236 /* all referenced leafs must be of the same config type */
3237 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3238 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003239 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003240 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3241 return LY_EVALID;
3242 } else if ((*key)->flags & LYS_CONFIG_W) {
3243 config = 1;
3244 } else { /* LYS_CONFIG_R */
3245 config = 0;
3246 }
3247
3248 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3249 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3250 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003251 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003252 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3253 return LY_EVALID;
3254 }
3255 }
3256
3257 /* check status */
Michal Vaskoc130e162021-10-19 11:30:00 +02003258 LY_CHECK_RET(lysc_check_status(ctx, list->flags, uniques[v].mod->mod, list->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003259 (*key)->flags, (*key)->module, (*key)->name));
3260
3261 /* mark leaf as unique */
3262 (*key)->flags |= LYS_UNIQUE;
3263
3264 /* next unique value in line */
3265 keystr = delim;
3266 }
3267 /* next unique definition */
3268 }
3269
3270 return LY_SUCCESS;
3271}
3272
3273/**
3274 * @brief Compile parsed list node information.
3275 * @param[in] ctx Compile context
3276 * @param[in] pnode Parsed list node.
3277 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3278 * is enriched with the list-specific information.
3279 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3280 */
3281static LY_ERR
3282lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3283{
3284 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3285 struct lysc_node_list *list = (struct lysc_node_list *)node;
3286 struct lysp_node *child_p;
Michal Vasko2d6507a2022-03-16 09:40:52 +01003287 struct lysc_node *parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003288 struct lysc_node_leaf *key, *prev_key = NULL;
3289 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003290 const char *keystr, *delim;
3291 LY_ERR ret = LY_SUCCESS;
3292
3293 list->min = list_p->min;
3294 if (list->min) {
3295 list->flags |= LYS_MAND_TRUE;
3296 }
3297 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3298
3299 LY_LIST_FOR(list_p->child, child_p) {
3300 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3301 }
3302
Michal Vasko5347e3a2020-11-03 17:14:57 +01003303 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003304
3305 /* add must(s) to unres */
3306 ret = lysc_unres_must_add(ctx, node, pnode);
3307 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003308
3309 /* keys */
Michal Vasko2d6507a2022-03-16 09:40:52 +01003310 if (list->flags & LYS_CONFIG_W) {
3311 parent = node;
3312 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
3313 /* compiling individual grouping, we can check this only if there is an explicit config set */
3314 while (parent) {
3315 if (parent->flags & LYS_SET_CONFIG) {
3316 break;
3317 }
3318 parent = parent->parent;
3319 }
3320 }
3321
3322 if (parent && (!list_p->key || !list_p->key[0])) {
3323 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3324 return LY_EVALID;
3325 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003326 }
3327
3328 /* find all the keys (must be direct children) */
3329 keystr = list_p->key;
3330 if (!keystr) {
3331 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003332 list->flags &= ~LYS_ORDBY_SYSTEM;
3333 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003334 }
3335 while (keystr) {
3336 delim = strpbrk(keystr, " \t\n");
3337 if (delim) {
3338 len = delim - keystr;
3339 while (isspace(*delim)) {
3340 ++delim;
3341 }
3342 } else {
3343 len = strlen(keystr);
3344 }
3345
3346 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003347 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3348 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003349 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003350 return LY_EVALID;
3351 }
3352 /* keys must be unique */
3353 if (key->flags & LYS_KEY) {
3354 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003355 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003356 return LY_EVALID;
3357 }
3358
Radek Krejcia6016992021-03-03 10:13:41 +01003359 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003360 /* key must have the same config flag as the list itself */
3361 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003362 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of a configuration list must not be a state leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003363 return LY_EVALID;
3364 }
3365 if (ctx->pmod->version < LYS_VERSION_1_1) {
3366 /* YANG 1.0 denies key to be of empty type */
3367 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003368 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003369 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3370 return LY_EVALID;
3371 }
3372 } else {
3373 /* when and if-feature are illegal on list keys */
3374 if (key->when) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003375 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List's key must not have any \"when\" statement.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003376 return LY_EVALID;
3377 }
Michal Vasko93b4ab92022-05-13 12:29:59 +02003378 /* unable to check if-features but compilation would fail if disabled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003379 }
3380
3381 /* check status */
Michal Vaskoc636ea42022-09-16 10:20:31 +02003382 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 +02003383
3384 /* ignore default values of the key */
3385 if (key->dflt) {
3386 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003387 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)key->dflt->realtype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003388 free(key->dflt);
3389 key->dflt = NULL;
3390 }
3391 /* mark leaf as key */
3392 key->flags |= LYS_KEY;
3393
3394 /* move it to the correct position */
3395 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3396 /* fix links in closest previous siblings of the key */
3397 if (key->next) {
3398 key->next->prev = key->prev;
3399 } else {
3400 /* last child */
3401 list->child->prev = key->prev;
3402 }
3403 if (key->prev->next) {
3404 key->prev->next = key->next;
3405 }
3406 /* fix links in the key */
3407 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003408 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003409 key->next = prev_key->next;
3410 } else {
3411 key->prev = list->child->prev;
3412 key->next = list->child;
3413 }
3414 /* fix links in closes future siblings of the key */
3415 if (prev_key) {
3416 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003417 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003418 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003419 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003420 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003421 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003422 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003423 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003424 }
3425 /* fix links in parent */
3426 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003427 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003428 }
3429 }
3430
3431 /* next key value */
3432 prev_key = key;
3433 keystr = delim;
3434 lysc_update_path(ctx, NULL, NULL);
3435 }
3436
Michal Vasko95f736c2022-06-08 12:03:31 +02003437 /* connect any augments */
3438 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3439
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003440 /* uniques */
3441 if (list_p->uniques) {
3442 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3443 }
3444
Radek Krejci2a9fc652021-01-22 17:44:34 +01003445 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3446 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3447 LY_CHECK_GOTO(ret, done);
3448 }
3449 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3450 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3451 LY_CHECK_GOTO(ret, done);
3452 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003453
3454 /* checks */
3455 if (list->min > list->max) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003456 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 +02003457 return LY_EVALID;
3458 }
3459
3460done:
3461 return ret;
3462}
3463
3464/**
3465 * @brief Do some checks and set the default choice's case.
3466 *
3467 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3468 *
3469 * @param[in] ctx Compile context.
3470 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3471 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3472 * @return LY_ERR value.
3473 */
3474static LY_ERR
3475lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3476{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003477 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003478 const struct lys_module *mod;
3479 const char *prefix = NULL, *name;
3480 size_t prefix_len = 0;
3481
3482 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3483 name = strchr(dflt->str, ':');
3484 if (name) {
3485 prefix = dflt->str;
3486 prefix_len = name - prefix;
3487 ++name;
3488 } else {
3489 name = dflt->str;
3490 }
3491 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003492 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003493 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003494 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003495 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003496 return LY_EVALID;
3497 }
3498 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003499 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003500 }
3501
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003502 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 +02003503 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003504 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003505 "Default case \"%s\" not found.", dflt->str);
3506 return LY_EVALID;
3507 }
3508
3509 /* no mandatory nodes directly under the default case */
3510 LY_LIST_FOR(ch->dflt->child, iter) {
3511 if (iter->parent != (struct lysc_node *)ch->dflt) {
3512 break;
3513 }
3514 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003515 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003516 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3517 return LY_EVALID;
3518 }
3519 }
3520
3521 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003522 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003523 return LY_EVALID;
3524 }
3525
3526 ch->dflt->flags |= LYS_SET_DFLT;
3527 return LY_SUCCESS;
3528}
3529
3530LY_ERR
3531lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3532 struct ly_set *child_set)
3533{
3534 LY_ERR ret = LY_SUCCESS;
3535 struct lysp_node *child_p_next = child_p->next;
3536 struct lysp_node_case *cs_p;
Michal Vasko4eb49d52022-02-17 15:05:00 +01003537 struct lysc_node_case *cs_c;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003538
3539 if (child_p->nodetype == LYS_CASE) {
3540 /* standard case under choice */
3541 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3542 } else {
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003543 /* we need the implicit case first, so create a fake parsed (shorthand) case */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003544 cs_p = calloc(1, sizeof *cs_p);
Michal Vasko786a8832022-12-05 10:41:27 +01003545 LY_CHECK_ERR_RET(!cs_p, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003546 cs_p->nodetype = LYS_CASE;
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003547 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003548 cs_p->child = child_p;
3549
3550 /* make the child the only case child */
3551 child_p->next = NULL;
3552
3553 /* compile it normally */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003554 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 +02003555
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003556 if (((struct lysc_node_choice *)node)->cases) {
Michal Vasko4eb49d52022-02-17 15:05:00 +01003557 /* find our case node */
3558 cs_c = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases;
3559 while (cs_c->name != cs_p->name) {
3560 cs_c = (struct lysc_node_case *)cs_c->next;
3561 assert(cs_c);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003562 }
aPiecekaa320c92021-05-21 07:34:24 +02003563
Michal Vasko4eb49d52022-02-17 15:05:00 +01003564 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
3565 /* compiled case node cannot point to his corresponding parsed node
3566 * because it exists temporarily so it must be set to NULL
3567 */
3568 assert(cs_c->priv == cs_p);
3569 cs_c->priv = NULL;
3570 }
3571
3572 /* status is copied from his child and not from his parent as usual. */
3573 if (cs_c->child) {
3574 cs_c->flags &= ~LYS_STATUS_MASK;
3575 cs_c->flags |= (LYS_STATUS_MASK & cs_c->child->flags);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003576 }
3577 } /* else it was removed by a deviation */
aPiecekaa320c92021-05-21 07:34:24 +02003578
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003579revert_sh_case:
3580 /* free the parsed shorthand case and correct pointers back */
aPiecekaa320c92021-05-21 07:34:24 +02003581 cs_p->child = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003582 lysp_node_free(&ctx->free_ctx, (struct lysp_node *)cs_p);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003583 child_p->next = child_p_next;
3584 }
3585
3586 return ret;
3587}
3588
3589/**
3590 * @brief Compile parsed choice node information.
3591 *
3592 * @param[in] ctx Compile context
3593 * @param[in] pnode Parsed choice node.
3594 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3595 * is enriched with the choice-specific information.
3596 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3597 */
3598static LY_ERR
3599lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3600{
3601 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3602 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3603 struct lysp_node *child_p;
3604 LY_ERR ret = LY_SUCCESS;
3605
3606 assert(node->nodetype == LYS_CHOICE);
3607
3608 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003609 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, child_p, node, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003610 }
3611
Michal Vasko95f736c2022-06-08 12:03:31 +02003612 /* connect any augments */
3613 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3614
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003615 /* default branch */
3616 if (ch_p->dflt.str) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003617 LY_CHECK_GOTO(ret = lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003618 }
3619
Michal Vasko95f736c2022-06-08 12:03:31 +02003620done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003621 return ret;
3622}
3623
3624/**
3625 * @brief Compile parsed anydata or anyxml node information.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003626 *
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003627 * @param[in] ctx Compile context
3628 * @param[in] pnode Parsed anydata or anyxml node.
3629 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3630 * is enriched with the any-specific information.
3631 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3632 */
3633static LY_ERR
3634lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3635{
3636 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3637 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003638 LY_ERR ret = LY_SUCCESS;
3639
Michal Vasko5347e3a2020-11-03 17:14:57 +01003640 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003641
3642 /* add must(s) to unres */
3643 ret = lysc_unres_must_add(ctx, node, pnode);
3644 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003645
Radek Krejci91531e12021-02-08 19:57:54 +01003646 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003647 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko193dacd2022-10-13 08:43:05 +02003648 lyplg_ext_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003649 }
3650done:
3651 return ret;
3652}
3653
3654/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003655 * @brief Prepare the case structure in choice node for the new data node.
3656 *
3657 * 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
3658 * created in the choice when the first child was processed.
3659 *
3660 * @param[in] ctx Compile context.
3661 * @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,
3662 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3663 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3664 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3665 * @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,
3666 * it is linked from the case structure only in case it is its first child.
3667 */
3668static LY_ERR
3669lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3670{
Michal Vasko95f736c2022-06-08 12:03:31 +02003671 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003672 struct lysp_node *child_p;
3673 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3674
3675 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3676 /* we have to add an implicit case node into the parent choice */
3677 } else if (pnode->nodetype == LYS_CASE) {
3678 /* explicit parent case */
3679 LY_LIST_FOR(cs_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003680 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003681 }
3682 } else {
3683 LOGINT_RET(ctx->ctx);
3684 }
3685
Michal Vasko95f736c2022-06-08 12:03:31 +02003686 /* connect any augments */
3687 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3688
3689done:
3690 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003691}
3692
3693void
3694lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3695{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003696 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003697
3698 if (add) { /* set flag */
3699 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3700 parent = parent->parent) {
3701 parent->flags |= LYS_MAND_TRUE;
3702 }
3703 } else { /* unset flag */
3704 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003705 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003706 if (iter->flags & LYS_MAND_TRUE) {
3707 /* there is another mandatory node */
3708 return;
3709 }
3710 }
3711 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3712 parent->flags &= ~LYS_MAND_TRUE;
3713 }
3714 }
3715}
3716
3717/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003718 * @brief Get the grouping with the specified name from given groupings sized array.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003719 *
3720 * @param[in] node Linked list of nodes with groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003721 * @param[in] name Name of the grouping to find,
3722 * @return NULL when there is no grouping with the specified name
3723 * @return Pointer to the grouping of the specified @p name.
3724 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003725static struct lysp_node_grp *
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003726match_grouping(const struct lysp_node_grp *node, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003727{
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003728 LY_LIST_FOR(node, node) {
3729 if ((node->nodetype == LYS_GROUPING) && !strcmp(node->name, name)) {
3730 return (struct lysp_node_grp *)node;
Radek Krejciabd33a42021-01-20 17:18:59 +01003731 }
3732 }
3733
3734 return NULL;
3735}
3736
3737/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003738 * @brief Find grouping for a uses.
3739 *
3740 * @param[in] ctx Compile context.
3741 * @param[in] uses_p Parsed uses node.
3742 * @param[out] gpr_p Found grouping on success.
3743 * @param[out] grp_pmod Module of @p grp_p on success.
3744 * @return LY_ERR value.
3745 */
3746static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003747lys_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 +02003748 struct lysp_module **grp_pmod)
3749{
3750 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003751 struct lysp_node_grp *grp;
Michal Vasko193dacd2022-10-13 08:43:05 +02003752 const struct lysp_node_grp *ext_grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003753 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003754 const char *id, *name, *prefix, *local_pref;
3755 size_t prefix_len, name_len;
3756 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003757 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003758
3759 *grp_p = NULL;
3760 *grp_pmod = NULL;
3761
3762 /* search for the grouping definition */
3763 id = uses_p->name;
3764 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3765 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3766 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3767 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003768 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003769 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003770 if ((grp = match_grouping(lysp_node_groupings(pnode), name))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003771 found = ctx->pmod;
3772 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003773 }
3774 }
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003775
3776 /* if in an extension, search possible groupings in it */
Michal Vaskob62e1e32022-11-08 10:50:41 +01003777 if (!found && ctx->ext) {
Michal Vaskofbd037c2022-11-08 10:34:20 +01003778 lyplg_ext_parsed_get_storage(ctx->ext, LY_STMT_GROUPING, sizeof ext_grp, (const void **)&ext_grp);
Michal Vasko193dacd2022-10-13 08:43:05 +02003779 if ((grp = match_grouping(ext_grp, name))) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003780 found = ctx->pmod;
3781 }
3782 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003783 } else {
3784 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003785 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003786 if (!mod) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003787 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid prefix used for grouping reference.", uses_p->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003788 return LY_EVALID;
3789 }
3790 pmod = mod->parsed;
3791 }
3792
3793 if (!found) {
3794 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003795 if ((grp = match_grouping(pmod->groupings, name))) {
3796 found = pmod;
3797 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003798 /* ... and all the submodules */
3799 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003800 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3801 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003802 break;
3803 }
3804 }
3805 }
3806 }
3807 if (!found) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003808 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003809 return LY_EVALID;
3810 }
3811
Michal Vasko7c565922021-06-10 14:58:27 +02003812 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003813 /* remember that the grouping is instantiated to avoid its standalone validation */
3814 grp->flags |= LYS_USED_GRP;
3815 }
3816
3817 *grp_p = grp;
3818 *grp_pmod = found;
3819 return LY_SUCCESS;
3820}
3821
3822/**
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003823 * @brief Compile uses grouping children.
3824 *
3825 * @param[in] ctx Compile context.
3826 * @param[in] uses_p Parsed uses.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003827 * @param[in] inherited_flags Inherited flags from the uses.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003828 * @param[in] child First grouping child to compile.
3829 * @param[in] grp_mod Grouping parsed module.
3830 * @param[in] parent Uses compiled parent, may be NULL if top-level.
3831 * @param[in,out] child_set Set of all compiled child nodes.
3832 * @param[in] child_unres_disabled Whether the children are to be put into unres disabled set or not.
3833 * @return LY_SUCCESS on success.
3834 * @return LY_EVALID on failure.
3835 */
3836static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003837lys_compile_uses_children(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, uint16_t inherited_flags,
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003838 struct lysp_node *child, struct lysp_module *grp_mod, struct lysc_node *parent, struct ly_set *child_set,
3839 ly_bool child_unres_disabled)
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003840{
3841 LY_ERR rc = LY_SUCCESS;
3842 struct lysp_module *mod_old = ctx->pmod;
3843 uint32_t child_i, opt_prev = ctx->compile_opts;
3844 ly_bool enabled;
3845 struct lysp_node *pnode;
3846 struct lysc_node *node;
3847 struct lysc_when *when_shared = NULL;
3848
3849 assert(child_set);
3850
Michal Vasko5940c302022-07-14 13:54:38 +02003851 child_i = child_set->count;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003852 LY_LIST_FOR(child, pnode) {
3853 /* compile the nodes with their parsed (grouping) module */
3854 ctx->pmod = grp_mod;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003855 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, parent, inherited_flags, child_set), cleanup);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003856
3857 /* eval if-features again for the rest of this node processing */
3858 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
3859 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3860 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3861 }
3862
3863 /* restore the parsed module */
3864 ctx->pmod = mod_old;
3865
3866 /* since the uses node is not present in the compiled tree, we need to pass some of its
3867 * statements to all its children */
3868 while (child_i < child_set->count) {
3869 node = child_set->snodes[child_i];
3870
3871 if (uses_p->when) {
3872 /* pass uses when to all the children */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003873 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 +02003874 LY_CHECK_GOTO(rc, cleanup);
3875 }
3876
3877 if (child_unres_disabled) {
3878 /* child is disabled by the uses if-features */
3879 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
3880 }
3881
3882 /* child processed */
3883 ++child_i;
3884 }
3885
3886 /* next iter */
3887 ctx->compile_opts = opt_prev;
3888 }
3889
3890cleanup:
3891 ctx->compile_opts = opt_prev;
3892 return rc;
3893}
3894
3895/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003896 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3897 * If present, also apply uses's modificators.
3898 *
3899 * @param[in] ctx Compile context
3900 * @param[in] uses_p Parsed uses schema node.
3901 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3902 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3903 * the compile context.
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003904 * @param[in] inherited_flags Inherited flags from a schema-only statement.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003905 * @param[in] child_set Optional set of all the compiled children.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003906 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3907 */
3908static LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003909lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, uint16_t inherited_flags,
3910 struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003911{
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003912 LY_ERR rc = LY_SUCCESS;
3913 ly_bool enabled, child_unres_disabled = 0;
3914 uint32_t i, grp_stack_count, opt_prev = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003915 struct lysp_node_grp *grp = NULL;
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003916 uint16_t uses_flags = 0;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003917 struct lysp_module *grp_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003918 struct ly_set uses_child_set = {0};
3919
3920 /* find the referenced grouping */
3921 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3922
3923 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3924 grp_stack_count = ctx->groupings.count;
3925 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3926 if (grp_stack_count == ctx->groupings.count) {
3927 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003928 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003929 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3930 return LY_EVALID;
3931 }
3932
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003933 /* nodetype checks */
3934 if (grp->actions && (parent && !lysc_node_actions_p(parent))) {
3935 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3936 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3937 parent->name, lys_nodetype2str(parent->nodetype));
3938 rc = LY_EVALID;
3939 goto cleanup;
3940 }
3941 if (grp->notifs && (parent && !lysc_node_notifs_p(parent))) {
3942 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3943 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3944 parent->name, lys_nodetype2str(parent->nodetype));
3945 rc = LY_EVALID;
3946 goto cleanup;
3947 }
3948
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003949 /* check status */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003950 rc = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3951 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003952
3953 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003954 rc = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3955 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003956
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003957 /* compile special uses status flags */
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02003958 rc = lys_compile_status(ctx, uses_p->flags, inherited_flags, parent ? parent->flags : 0,
3959 parent ? parent->name : NULL, "<uses>", &uses_flags);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003960 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003961
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003962 /* uses if-features */
3963 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, uses_p->iffeatures, &enabled), cleanup);
3964 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3965 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3966 child_unres_disabled = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003967 }
3968
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003969 /* uses grouping children */
3970 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, grp->child, grp_mod, parent,
3971 child_set ? child_set : &uses_child_set, child_unres_disabled);
3972 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003973
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003974 /* uses grouping RPCs/actions */
3975 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->actions, grp_mod, parent,
3976 child_set ? child_set : &uses_child_set, child_unres_disabled);
3977 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003978
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003979 /* uses grouping notifications */
3980 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->notifs, grp_mod, parent,
3981 child_set ? child_set : &uses_child_set, child_unres_disabled);
3982 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003983
3984 /* check that all augments were applied */
3985 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003986 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3987 /* augment of some parent uses, irrelevant now */
3988 continue;
3989 }
3990
3991 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003992 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003993 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003994 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003995 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003996
3997 /* check that all refines were applied */
3998 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003999 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004000 /* refine of some parent uses, irrelevant now */
Michal Vaskod8655722021-01-12 15:20:36 +01004001 continue;
4002 }
4003
4004 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004005 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004006 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004007 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004008 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004009
Michal Vasko193dacd2022-10-13 08:43:05 +02004010 /* compile uses and grouping extensions into the parent */
4011 COMPILE_EXTS_GOTO(ctx, uses_p->exts, parent->exts, parent, rc, cleanup);
4012 COMPILE_EXTS_GOTO(ctx, grp->exts, parent->exts, parent, rc, cleanup);
4013
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004014cleanup:
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004015 /* restore previous context */
4016 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004017
4018 /* remove the grouping from the stack for circular groupings dependency check */
4019 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
4020 assert(ctx->groupings.count == grp_stack_count);
4021
4022 ly_set_erase(&uses_child_set, NULL);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02004023 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004024}
4025
4026static int
4027lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
4028{
4029 struct lysp_node *iter;
4030 int len = 0;
4031
4032 *path = NULL;
4033 for (iter = node; iter && len >= 0; iter = iter->parent) {
4034 char *s = *path;
4035 char *id;
4036
4037 switch (iter->nodetype) {
4038 case LYS_USES:
4039 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
4040 break;
4041 case LYS_GROUPING:
4042 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
4043 break;
4044 case LYS_AUGMENT:
4045 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
4046 break;
4047 default:
4048 id = strdup(iter->name);
4049 break;
4050 }
4051
4052 if (!iter->parent) {
4053 /* print prefix */
4054 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
4055 } else {
4056 /* prefix is the same as in parent */
4057 len = asprintf(path, "/%s%s", id, s ? s : "");
4058 }
4059 free(s);
4060 free(id);
4061 }
4062
4063 if (len < 0) {
4064 free(*path);
4065 *path = NULL;
4066 } else if (len == 0) {
4067 *path = strdup("/");
4068 len = 1;
4069 }
4070 return len;
4071}
4072
4073LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01004074lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004075{
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004076 LY_ERR rc = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004077 char *path;
4078 int len;
4079
Michal Vasko8254d852022-04-25 10:05:59 +02004080 /* use grouping status to avoid errors */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004081 struct lysp_node_uses fake_uses = {
4082 .parent = pnode,
4083 .nodetype = LYS_USES,
Michal Vasko8254d852022-04-25 10:05:59 +02004084 .flags = grp->flags & LYS_STATUS_MASK, .next = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004085 .name = grp->name,
4086 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4087 .refines = NULL, .augments = NULL
4088 };
4089 struct lysc_node_container fake_container = {
4090 .nodetype = LYS_CONTAINER,
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004091 .flags = 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004092 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02004093 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01004094 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004095 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004096 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004097 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4098 };
4099
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004100 /* compile fake container flags */
4101 LY_CHECK_GOTO(rc = lys_compile_node_flags(ctx, pnode ? pnode->flags : 0, 0, &fake_container.node), cleanup);
4102
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004103 if (grp->parent) {
4104 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4105 }
4106
4107 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
4108 if (len < 0) {
4109 LOGMEM(ctx->ctx);
4110 return LY_EMEM;
4111 }
4112 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
4113 ctx->path_len = (uint32_t)len;
4114 free(path);
4115
4116 lysc_update_path(ctx, NULL, "{grouping}");
4117 lysc_update_path(ctx, NULL, grp->name);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004118 rc = lys_compile_uses(ctx, &fake_uses, &fake_container.node, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004119 lysc_update_path(ctx, NULL, NULL);
4120 lysc_update_path(ctx, NULL, NULL);
4121
4122 ctx->path_len = 1;
4123 ctx->path[1] = '\0';
4124
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004125cleanup:
Michal Vaskoc636ea42022-09-16 10:20:31 +02004126 lysc_node_container_free(&ctx->free_ctx, &fake_container);
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004127 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004128}
4129
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004130LY_ERR
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004131lys_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 +02004132 struct ly_set *child_set)
4133{
4134 LY_ERR ret = LY_SUCCESS;
4135 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02004136 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004137
4138 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
4139
4140 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01004141 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004142 } else {
4143 lysc_update_path(ctx, NULL, "{uses}");
4144 lysc_update_path(ctx, NULL, pnode->name);
4145 }
4146
4147 switch (pnode->nodetype) {
4148 case LYS_CONTAINER:
4149 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
4150 node_compile_spec = lys_compile_node_container;
4151 break;
4152 case LYS_LEAF:
4153 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
4154 node_compile_spec = lys_compile_node_leaf;
4155 break;
4156 case LYS_LIST:
4157 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
4158 node_compile_spec = lys_compile_node_list;
4159 break;
4160 case LYS_LEAFLIST:
4161 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
4162 node_compile_spec = lys_compile_node_leaflist;
4163 break;
4164 case LYS_CHOICE:
4165 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
4166 node_compile_spec = lys_compile_node_choice;
4167 break;
4168 case LYS_CASE:
4169 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
4170 node_compile_spec = lys_compile_node_case;
4171 break;
4172 case LYS_ANYXML:
4173 case LYS_ANYDATA:
4174 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
4175 node_compile_spec = lys_compile_node_any;
4176 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004177 case LYS_RPC:
4178 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02004179 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004180 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4181 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004182 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004183 return LY_EVALID;
4184 }
4185 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
4186 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02004187 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004188 break;
4189 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02004190 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004191 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4192 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004193 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004194 return LY_EVALID;
4195 }
4196 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
4197 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02004198 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004199 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004200 case LYS_USES:
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004201 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, inherited_flags, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004202 lysc_update_path(ctx, NULL, NULL);
4203 lysc_update_path(ctx, NULL, NULL);
4204 return ret;
4205 default:
4206 LOGINT(ctx->ctx);
4207 return LY_EINT;
4208 }
4209 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4210
Michal Vaskoa0ba01e2022-10-19 13:26:57 +02004211 ret = lys_compile_node_(ctx, pnode, parent, inherited_flags, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004212
Michal Vasko7c565922021-06-10 14:58:27 +02004213 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004214 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004215 return ret;
4216}