blob: a1708b091fd90cdc64f2d7b0163fc2030db6284f [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 Vaskoc130e162021-10-19 11:30:00 +020056 * @brief Add a node with must(s) to unres.
57 *
58 * @param[in] ctx Compile context.
59 * @param[in] node Compiled node with must(s).
60 * @param[in] pnode Parsed ndoe with must(s).
61 * @return LY_ERR value.
62 */
63static LY_ERR
64lysc_unres_must_add(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_node *pnode)
65{
66 struct lysc_unres_must *m = NULL;
67 LY_ARRAY_COUNT_TYPE u;
68 struct lysc_must *musts;
69 struct lysp_restr *pmusts;
70 LY_ERR ret;
71
72 /* do not check must(s) in a grouping */
73 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
74 return LY_SUCCESS;
75 }
76
77 musts = lysc_node_musts(node);
78 pmusts = lysp_node_musts(pnode);
79 assert(LY_ARRAY_COUNT(musts) == LY_ARRAY_COUNT(pmusts));
80
81 if (!musts) {
82 /* no must */
83 return LY_SUCCESS;
84 }
85
86 /* add new unres must */
87 m = calloc(1, sizeof *m);
88 LY_CHECK_ERR_GOTO(!m, ret = LY_EMEM, error);
89 m->node = node;
90
91 /* add must local modules */
92 LY_ARRAY_CREATE_GOTO(ctx->ctx, m->local_mods, LY_ARRAY_COUNT(pmusts), ret, error);
93 LY_ARRAY_FOR(pmusts, u) {
94 m->local_mods[u] = pmusts[u].arg.mod;
95 LY_ARRAY_INCREMENT(m->local_mods);
96 }
97
Michal Vaskoedb0fa52022-10-04 10:36:00 +020098 /* store ext */
99 m->ext = ctx->ext;
100
Michal Vaskoc130e162021-10-19 11:30:00 +0200101 LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->musts, m, 1, NULL), ret = LY_EMEM, error);
102
103 return LY_SUCCESS;
104
105error:
106 if (m) {
107 LY_ARRAY_FREE(m->local_mods);
108 free(m);
109 }
110 LOGMEM(ctx->ctx);
111 return ret;
112}
113
114static LY_ERR
115lysc_unres_leafref_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const struct lysp_module *local_mod)
116{
117 struct lysc_unres_leafref *l = NULL;
118 struct ly_set *leafrefs_set;
119 LY_ARRAY_COUNT_TYPE u;
120 int is_lref = 0;
121
122 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
123 /* do not check leafrefs in groupings */
124 return LY_SUCCESS;
125 }
126
127 /* use special set for disabled leafrefs */
128 leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs;
129
130 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
131 /* leafref */
132 is_lref = 1;
133 } else if (leaf->type->basetype == LY_TYPE_UNION) {
134 /* union with leafrefs */
135 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
136 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
137 is_lref = 1;
138 break;
139 }
140 }
141 }
142
143 if (is_lref) {
144 /* add new unresolved leafref node */
145 l = calloc(1, sizeof *l);
146 LY_CHECK_ERR_RET(!l, LOGMEM(ctx->ctx), LY_EMEM);
147
148 l->node = &leaf->node;
149 l->local_mod = local_mod;
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200150 l->ext = ctx->ext;
Michal Vaskoc130e162021-10-19 11:30:00 +0200151
152 LY_CHECK_ERR_RET(ly_set_add(leafrefs_set, l, 1, NULL), free(l); LOGMEM(ctx->ctx), LY_EMEM);
153 }
154
155 return LY_SUCCESS;
156}
157
158/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200159 * @brief Add/replace a leaf default value in unres.
160 * Can also be used for a single leaf-list default value.
161 *
162 * @param[in] ctx Compile context.
163 * @param[in] leaf Leaf with the default value.
164 * @param[in] dflt Default value to use.
165 * @return LY_ERR value.
166 */
167static LY_ERR
168lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
169{
170 struct lysc_unres_dflt *r = NULL;
171 uint32_t i;
172
Michal Vasko7c565922021-06-10 14:58:27 +0200173 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100174 return LY_SUCCESS;
175 }
176
Michal Vasko405cc9e2020-12-01 12:01:27 +0100177 for (i = 0; i < ctx->unres->dflts.count; ++i) {
178 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200179 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100180 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200181 lysp_qname_free(ctx->ctx, r->dflt);
182 free(r->dflt);
183 break;
184 }
185 }
186 if (!r) {
187 /* add new unres item */
188 r = calloc(1, sizeof *r);
189 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
190 r->leaf = leaf;
191
Michal Vasko405cc9e2020-12-01 12:01:27 +0100192 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200193 }
194
195 r->dflt = malloc(sizeof *r->dflt);
196 LY_CHECK_GOTO(!r->dflt, error);
197 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
198
199 return LY_SUCCESS;
200
201error:
202 free(r->dflt);
203 LOGMEM(ctx->ctx);
204 return LY_EMEM;
205}
206
207/**
208 * @brief Add/replace a leaf-list default value(s) in unres.
209 *
210 * @param[in] ctx Compile context.
211 * @param[in] llist Leaf-list with the default value.
212 * @param[in] dflts Sized array of the default values.
213 * @return LY_ERR value.
214 */
215static LY_ERR
216lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
217{
218 struct lysc_unres_dflt *r = NULL;
219 uint32_t i;
220
Michal Vasko7c565922021-06-10 14:58:27 +0200221 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100222 return LY_SUCCESS;
223 }
224
Michal Vasko405cc9e2020-12-01 12:01:27 +0100225 for (i = 0; i < ctx->unres->dflts.count; ++i) {
226 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200227 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100228 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200229 lysp_qname_free(ctx->ctx, r->dflt);
230 free(r->dflt);
231 r->dflt = NULL;
232 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
233 r->dflts = NULL;
234 break;
235 }
236 }
237 if (!r) {
238 r = calloc(1, sizeof *r);
239 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
240 r->llist = llist;
241
Michal Vasko405cc9e2020-12-01 12:01:27 +0100242 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200243 }
244
245 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
246
247 return LY_SUCCESS;
248}
249
250/**
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100251 * @brief Add a bits/enumeration type to unres.
252 *
253 * @param[in] ctx Compile context.
254 * @param[in] leaf Leaf of type bits/enumeration whose disabled items to free.
255 * @return LY_ERR value.
256 */
257static LY_ERR
258lysc_unres_bitenum_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf)
259{
260 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
261 /* skip groupings and redundant for disabled nodes */
262 return LY_SUCCESS;
263 }
264
265 LY_CHECK_RET(ly_set_add(&ctx->unres->disabled_bitenums, leaf, 1, NULL));
266
267 return LY_SUCCESS;
268}
269
270/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200271 * @brief Duplicate the compiled pattern structure.
272 *
273 * Instead of duplicating memory, the reference counter in the @p orig is increased.
274 *
275 * @param[in] orig The pattern structure to duplicate.
276 * @return The duplicated structure to use.
277 */
278static struct lysc_pattern *
279lysc_pattern_dup(struct lysc_pattern *orig)
280{
281 ++orig->refcount;
282 return orig;
283}
284
285/**
286 * @brief Duplicate the array of compiled patterns.
287 *
288 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
289 *
290 * @param[in] ctx Libyang context for logging.
291 * @param[in] orig The patterns sized array to duplicate.
292 * @return New sized array as a copy of @p orig.
293 * @return NULL in case of memory allocation error.
294 */
295static struct lysc_pattern **
296lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
297{
298 struct lysc_pattern **dup = NULL;
299 LY_ARRAY_COUNT_TYPE u;
300
301 assert(orig);
302
303 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
304 LY_ARRAY_FOR(orig, u) {
305 dup[u] = lysc_pattern_dup(orig[u]);
306 LY_ARRAY_INCREMENT(dup);
307 }
308 return dup;
309}
310
311/**
312 * @brief Duplicate compiled range structure.
313 *
314 * @param[in] ctx Libyang context for logging.
315 * @param[in] orig The range structure to be duplicated.
316 * @return New compiled range structure as a copy of @p orig.
317 * @return NULL in case of memory allocation error.
318 */
319static struct lysc_range *
320lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
321{
322 struct lysc_range *dup;
323 LY_ERR ret;
324
325 assert(orig);
326
327 dup = calloc(1, sizeof *dup);
328 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
329 if (orig->parts) {
330 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100331 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200332 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
333 }
334 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
335 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
336 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
337
338 return dup;
339cleanup:
340 free(dup);
341 (void) ret; /* set but not used due to the return type */
342 return NULL;
343}
344
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200345/**
Michal Vaskodfd254d2021-06-24 09:24:59 +0200346 * @brief Compile status information of the given statement.
347 *
348 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
349 * has the status correctly set during the compilation.
350 *
351 * @param[in] ctx Compile context
352 * @param[in,out] stmt_flags Compiled flags to update. If the status was set explicitly, it is already set
353 * in the flags value and we just check the compatibility with the parent's status value.
354 * @param[in] stmt_name Statement name, for logging.
355 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
356 * @param[in] parent_name Name of the parent node, for logging.
357 * @param[in] inherit_log Whether to print inherit message.
358 * @return LY_ERR value.
359 */
360static LY_ERR
361lys_compile_status(struct lysc_ctx *ctx, uint16_t *stmt_flags, const char *stmt_name, uint16_t parent_flags,
362 const char *parent_name, ly_bool inherit_log)
363{
364 /* status - it is not inherited by specification, but it does not make sense to have
365 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
366 if (!(*stmt_flags & LYS_STATUS_MASK)) {
367 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
368 if (inherit_log) {
369 LOGVRB("Missing explicit \"%s\" status specified in parent \"%s\", inheriting for \"%s\".",
370 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name, stmt_name);
371 }
372 *stmt_flags |= parent_flags & LYS_STATUS_MASK;
373 } else {
374 *stmt_flags |= LYS_STATUS_CURR;
375 }
376 } else if (parent_flags & LYS_STATUS_MASK) {
377 /* check status compatibility with the parent */
378 if ((parent_flags & LYS_STATUS_MASK) > (*stmt_flags & LYS_STATUS_MASK)) {
379 if (*stmt_flags & LYS_STATUS_CURR) {
380 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
381 "Status \"current\" of \"%s\" is in conflict with the \"%s\" status of parent \"%s\".",
382 stmt_name, (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name);
383 } else { /* LYS_STATUS_DEPRC */
384 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
385 "Status \"deprecated\" of \"%s\" is in conflict with the \"obsolete\" status of parent \"%s\".",
386 stmt_name, parent_name);
387 }
388 return LY_EVALID;
389 }
390 }
391 return LY_SUCCESS;
392}
393
394/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200395 * @brief Compile information from the when statement
396 *
397 * @param[in] ctx Compile context.
398 * @param[in] when_p Parsed when structure.
Michal Vaskodfd254d2021-06-24 09:24:59 +0200399 * @param[in] parent_flags Flags of the parsed node with the when statement.
400 * @param[in] compiled_parent Closest compiled parent of the when statement.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200401 * @param[in] ctx_node Context node for the when statement.
402 * @param[out] when Pointer where to store pointer to the created compiled when structure.
403 * @return LY_ERR value.
404 */
405static LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200406lys_compile_when_(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t parent_flags,
Michal Vaskodfd254d2021-06-24 09:24:59 +0200407 const struct lysc_node *compiled_parent, const struct lysc_node *ctx_node, struct lysc_when **when)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200408{
409 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200410 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200411
412 *when = calloc(1, sizeof **when);
413 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
414 (*when)->refcount = 1;
415 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200416 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci8df109d2021-04-23 12:19:08 +0200417 LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200418 (*when)->context = (struct lysc_node *)ctx_node;
419 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
420 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100421 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vaskodfd254d2021-06-24 09:24:59 +0200422 (*when)->flags = (parent_flags & LYS_STATUS_MASK);
423 if (compiled_parent) {
424 LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", compiled_parent->flags, compiled_parent->name, 0));
425 } else {
426 LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", 0, NULL, 0));
427 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200428
429done:
430 return ret;
431}
432
433LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200434lys_compile_when(struct lysc_ctx *ctx, const struct lysp_when *when_p, uint16_t parent_flags,
435 const struct lysc_node *compiled_parent, const struct lysc_node *ctx_node, struct lysc_node *node,
436 struct lysc_when **when_c)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200437{
Michal Vasko193dacd2022-10-13 08:43:05 +0200438 LY_ERR rc = LY_SUCCESS;
439 struct lysc_when **new_when, ***node_when, *ptr;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200440
Michal Vasko193dacd2022-10-13 08:43:05 +0200441 assert(when_p && (node || when_c));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200442
Michal Vasko193dacd2022-10-13 08:43:05 +0200443 if (node) {
444 /* get the when array */
445 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200446
Michal Vasko193dacd2022-10-13 08:43:05 +0200447 /* create new when pointer */
448 LY_ARRAY_NEW_GOTO(ctx->ctx, *node_when, new_when, rc, cleanup);
449 } else {
450 /* individual when */
451 new_when = &ptr;
452 *new_when = calloc(1, sizeof **new_when);
453 LY_CHECK_ERR_GOTO(!*new_when, LOGMEM(ctx->ctx); rc = LY_EMEM, cleanup);
454 }
455
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200456 if (!when_c || !(*when_c)) {
457 /* compile when */
Michal Vasko193dacd2022-10-13 08:43:05 +0200458 LY_CHECK_GOTO(rc = lys_compile_when_(ctx, when_p, parent_flags, compiled_parent, ctx_node, new_when), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200459
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200460 /* remember the compiled when for sharing */
461 if (when_c) {
462 *when_c = *new_when;
463 }
464 } else {
465 /* use the previously compiled when */
466 ++(*when_c)->refcount;
467 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100468 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200469
Michal Vasko6f08e962021-07-23 08:30:47 +0200470 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100471 /* do not check "when" semantics in a grouping, but repeat the check for different node because
472 * of dummy node check */
Michal Vasko193dacd2022-10-13 08:43:05 +0200473 LY_CHECK_GOTO(rc = ly_set_add(&ctx->unres->whens, node, 0, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200474 }
475
Michal Vasko193dacd2022-10-13 08:43:05 +0200476cleanup:
477 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200478}
479
Michal Vaskoedb0fa52022-10-04 10:36:00 +0200480LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +0200481lys_compile_must(struct lysc_ctx *ctx, const struct lysp_restr *must_p, struct lysc_must *must)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200482{
483 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200484 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200485
486 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200487 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 +0200488 LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200489 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
490 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
491 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
492 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100493 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200494
495done:
496 return ret;
497}
498
499/**
500 * @brief Validate and normalize numeric value from a range definition.
501 * @param[in] ctx Compile context.
502 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
503 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
504 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
505 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
506 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
507 * @param[in] value String value of the range boundary.
508 * @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.
509 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
510 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
511 */
512static LY_ERR
513range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
514 size_t *len, char **valcopy)
515{
516 size_t fraction = 0, size;
517
518 *len = 0;
519
520 assert(value);
521 /* parse value */
522 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
523 return LY_EVALID;
524 }
525
526 if ((value[*len] == '-') || (value[*len] == '+')) {
527 ++(*len);
528 }
529
530 while (isdigit(value[*len])) {
531 ++(*len);
532 }
533
534 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
535 if (basetype == LY_TYPE_DEC64) {
536 goto decimal;
537 } else {
538 *valcopy = strndup(value, *len);
539 return LY_SUCCESS;
540 }
541 }
542 fraction = *len;
543
544 ++(*len);
545 while (isdigit(value[*len])) {
546 ++(*len);
547 }
548
549 if (basetype == LY_TYPE_DEC64) {
550decimal:
551 assert(frdigits);
552 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100553 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200554 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100555 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200556 return LY_EINVAL;
557 }
558 if (fraction) {
559 size = (*len) + (frdigits - ((*len) - 1 - fraction));
560 } else {
561 size = (*len) + frdigits + 1;
562 }
563 *valcopy = malloc(size * sizeof **valcopy);
564 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
565
566 (*valcopy)[size - 1] = '\0';
567 if (fraction) {
568 memcpy(&(*valcopy)[0], &value[0], fraction);
569 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
570 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
571 } else {
572 memcpy(&(*valcopy)[0], &value[0], *len);
573 memset(&(*valcopy)[*len], '0', frdigits);
574 }
575 }
576 return LY_SUCCESS;
577}
578
579/**
580 * @brief Check that values in range are in ascendant order.
581 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
582 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
583 * max can be also equal.
584 * @param[in] value Current value to check.
585 * @param[in] prev_value The last seen value.
586 * @return LY_SUCCESS or LY_EEXIST for invalid order.
587 */
588static LY_ERR
589range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
590{
591 if (unsigned_value) {
592 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
593 return LY_EEXIST;
594 }
595 } else {
596 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
597 return LY_EEXIST;
598 }
599 }
600 return LY_SUCCESS;
601}
602
603/**
604 * @brief Set min/max value of the range part.
605 * @param[in] ctx Compile context.
606 * @param[in] part Range part structure to fill.
607 * @param[in] max Flag to distinguish if storing min or max value.
608 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
609 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
610 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
611 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
612 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
613 * @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.
614 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
615 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
616 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
617 * frdigits value), LY_EMEM.
618 */
619static LY_ERR
620range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
621 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
622{
623 LY_ERR ret = LY_SUCCESS;
624 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100625 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200626
627 if (value) {
628 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
629 LY_CHECK_GOTO(ret, finalize);
630 }
631 if (!valcopy && base_range) {
632 if (max) {
633 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
634 } else {
635 part->min_64 = base_range->parts[0].min_64;
636 }
637 if (!first) {
638 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
639 }
640 goto finalize;
641 }
642
643 switch (basetype) {
644 case LY_TYPE_INT8: /* range */
645 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100646 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 +0200647 } else if (max) {
648 part->max_64 = INT64_C(127);
649 } else {
650 part->min_64 = INT64_C(-128);
651 }
652 if (!ret && !first) {
653 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
654 }
655 break;
656 case LY_TYPE_INT16: /* range */
657 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100658 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
659 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200660 } else if (max) {
661 part->max_64 = INT64_C(32767);
662 } else {
663 part->min_64 = INT64_C(-32768);
664 }
665 if (!ret && !first) {
666 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
667 }
668 break;
669 case LY_TYPE_INT32: /* range */
670 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100671 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
672 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200673 } else if (max) {
674 part->max_64 = INT64_C(2147483647);
675 } else {
676 part->min_64 = INT64_C(-2147483648);
677 }
678 if (!ret && !first) {
679 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
680 }
681 break;
682 case LY_TYPE_INT64: /* range */
683 case LY_TYPE_DEC64: /* range */
684 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100685 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
686 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200687 } else if (max) {
688 part->max_64 = INT64_C(9223372036854775807);
689 } else {
690 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
691 }
692 if (!ret && !first) {
693 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
694 }
695 break;
696 case LY_TYPE_UINT8: /* range */
697 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100698 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 +0200699 } else if (max) {
700 part->max_u64 = UINT64_C(255);
701 } else {
702 part->min_u64 = UINT64_C(0);
703 }
704 if (!ret && !first) {
705 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
706 }
707 break;
708 case LY_TYPE_UINT16: /* range */
709 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100710 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 +0200711 } else if (max) {
712 part->max_u64 = UINT64_C(65535);
713 } else {
714 part->min_u64 = UINT64_C(0);
715 }
716 if (!ret && !first) {
717 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
718 }
719 break;
720 case LY_TYPE_UINT32: /* range */
721 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100722 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
723 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200724 } else if (max) {
725 part->max_u64 = UINT64_C(4294967295);
726 } else {
727 part->min_u64 = UINT64_C(0);
728 }
729 if (!ret && !first) {
730 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
731 }
732 break;
733 case LY_TYPE_UINT64: /* range */
734 case LY_TYPE_STRING: /* length */
735 case LY_TYPE_BINARY: /* length */
736 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100737 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
738 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200739 } else if (max) {
740 part->max_u64 = UINT64_C(18446744073709551615);
741 } else {
742 part->min_u64 = UINT64_C(0);
743 }
744 if (!ret && !first) {
745 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
746 }
747 break;
748 default:
749 LOGINT(ctx->ctx);
750 ret = LY_EINT;
751 }
752
753finalize:
754 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100755 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200756 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
757 length_restr ? "length" : "range", valcopy ? valcopy : *value);
758 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100759 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200760 "Invalid %s restriction - invalid value \"%s\".",
761 length_restr ? "length" : "range", valcopy ? valcopy : *value);
762 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100763 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200764 "Invalid %s restriction - values are not in ascending order (%s).",
765 length_restr ? "length" : "range",
766 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
767 } else if (!ret && value) {
768 *value = *value + len;
769 }
770 free(valcopy);
771 return ret;
772}
773
Michal Vasko193dacd2022-10-13 08:43:05 +0200774LY_ERR
775lys_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 +0200776 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
777{
aPiecek1c4da362021-04-29 14:26:34 +0200778 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200779 const char *expr;
780 struct lysc_range_part *parts = NULL, *part;
781 ly_bool range_expected = 0, uns;
782 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
783
784 assert(range);
785 assert(range_p);
786
787 expr = range_p->arg.str;
788 while (1) {
789 if (isspace(*expr)) {
790 ++expr;
791 } else if (*expr == '\0') {
792 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100793 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200794 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200795 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200796 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200797 goto cleanup;
798 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100799 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200800 "Invalid %s restriction - unexpected end of the expression (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200801 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200802 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200803 goto cleanup;
804 }
805 parts_done++;
806 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100807 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200808 if (parts) {
809 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100810 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200811 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100812 (int)(expr - range_p->arg.str), range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200813 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200814 goto cleanup;
815 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100816 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200817
818 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200819 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 +0200820 part->max_64 = part->min_64;
821 } else if (*expr == '|') {
822 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100823 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200824 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200825 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200826 goto cleanup;
827 }
828 expr++;
829 parts_done++;
830 /* process next part of the expression */
831 } else if (!strncmp(expr, "..", 2)) {
832 expr += 2;
833 while (isspace(*expr)) {
834 expr++;
835 }
836 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100837 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200838 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
aPiecek1c4da362021-04-29 14:26:34 +0200839 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200840 goto cleanup;
841 }
842 /* continue expecting the upper boundary */
843 range_expected = 1;
844 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
845 /* number */
846 if (range_expected) {
847 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200848 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 +0200849 range_expected = 0;
850 } else {
851 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200852 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 +0200853 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
854 part->max_64 = part->min_64;
855 }
856
857 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100858 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
859 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200860 while (isspace(*expr)) {
861 expr++;
862 }
863 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100864 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200865 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200866 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200867 goto cleanup;
868 }
869 if (range_expected) {
870 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200871 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 +0200872 range_expected = 0;
873 } else {
874 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200875 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 +0200876 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
877 part->min_64 = part->max_64;
878 }
879 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100880 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200881 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200882 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200883 goto cleanup;
884 }
885 }
886
887 /* check with the previous range/length restriction */
888 if (base_range) {
889 switch (basetype) {
890 case LY_TYPE_BINARY:
891 case LY_TYPE_UINT8:
892 case LY_TYPE_UINT16:
893 case LY_TYPE_UINT32:
894 case LY_TYPE_UINT64:
895 case LY_TYPE_STRING:
896 uns = 1;
897 break;
898 case LY_TYPE_DEC64:
899 case LY_TYPE_INT8:
900 case LY_TYPE_INT16:
901 case LY_TYPE_INT32:
902 case LY_TYPE_INT64:
903 uns = 0;
904 break;
905 default:
906 LOGINT(ctx->ctx);
907 ret = LY_EINT;
908 goto cleanup;
909 }
910 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
911 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
912 goto baseerror;
913 }
914 /* current lower bound is not lower than the base */
915 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
916 /* base has single value */
917 if (base_range->parts[v].min_64 == parts[u].min_64) {
918 /* both lower bounds are the same */
919 if (parts[u].min_64 != parts[u].max_64) {
920 /* current continues with a range */
921 goto baseerror;
922 } else {
923 /* equal single values, move both forward */
924 ++v;
925 continue;
926 }
927 } else {
928 /* base is single value lower than current range, so the
929 * value from base range is removed in the current,
930 * move only base and repeat checking */
931 ++v;
932 --u;
933 continue;
934 }
935 } else {
936 /* base is the range */
937 if (parts[u].min_64 == parts[u].max_64) {
938 /* current is a single value */
939 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
940 /* current is behind the base range, so base range is omitted,
941 * move the base and keep the current for further check */
942 ++v;
943 --u;
944 } /* else it is within the base range, so move the current, but keep the base */
945 continue;
946 } else {
947 /* both are ranges - check the higher bound, the lower was already checked */
948 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
949 /* higher bound is higher than the current higher bound */
950 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
951 /* but the current lower bound is also higher, so the base range is omitted,
952 * continue with the same current, but move the base */
953 --u;
954 ++v;
955 continue;
956 }
957 /* current range starts within the base range but end behind it */
958 goto baseerror;
959 } else {
960 /* current range is smaller than the base,
961 * move current, but stay with the base */
962 continue;
963 }
964 }
965 }
966 }
967 if (u != parts_done) {
968baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100969 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200970 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200971 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200972 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200973 goto cleanup;
974 }
975 }
976
977 if (!(*range)) {
978 *range = calloc(1, sizeof **range);
979 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
980 }
981
982 /* we rewrite the following values as the types chain is being processed */
983 if (range_p->eapptag) {
984 lydict_remove(ctx->ctx, (*range)->eapptag);
985 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
986 }
987 if (range_p->emsg) {
988 lydict_remove(ctx->ctx, (*range)->emsg);
989 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
990 }
991 if (range_p->dsc) {
992 lydict_remove(ctx->ctx, (*range)->dsc);
993 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
994 }
995 if (range_p->ref) {
996 lydict_remove(ctx->ctx, (*range)->ref);
997 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
998 }
999 /* extensions are taken only from the last range by the caller */
1000
1001 (*range)->parts = parts;
1002 parts = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001003cleanup:
1004 LY_ARRAY_FREE(parts);
1005
1006 return ret;
1007}
1008
Michal Vasko215d7fc2022-07-15 14:50:44 +02001009/**
1010 * @brief Transform characters block in an XML Schema pattern into Perl character ranges.
1011 *
1012 * @param[in] ctx libyang context.
1013 * @param[in] pattern Original pattern.
1014 * @param[in,out] regex Pattern to modify.
1015 * @return LY_ERR value.
1016 */
1017static LY_ERR
1018lys_compile_pattern_chblocks_xmlschema2perl(const struct ly_ctx *ctx, const char *pattern, char **regex)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001019{
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001020#define URANGE_LEN 19
1021 char *ublock2urange[][2] = {
1022 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1023 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1024 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1025 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1026 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1027 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1028 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1029 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1030 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1031 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1032 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1033 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1034 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1035 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1036 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1037 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1038 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1039 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1040 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1041 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1042 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1043 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1044 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1045 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1046 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1047 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1048 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1049 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1050 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1051 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1052 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1053 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1054 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1055 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1056 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1057 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1058 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1059 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1060 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1061 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1062 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1063 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1064 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1065 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1066 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1067 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1068 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1069 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1070 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1071 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1072 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1073 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1074 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1075 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1076 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1077 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1078 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1079 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1080 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1081 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1082 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1083 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1084 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1085 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1086 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1087 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1088 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1089 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1090 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1091 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1092 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1093 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1094 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1095 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1096 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1097 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1098 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1099 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1100 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1101 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1102 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1103 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1104 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +01001105 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001106 {NULL, NULL}
1107 };
1108
Michal Vasko215d7fc2022-07-15 14:50:44 +02001109 size_t idx, idx2, start, end;
1110 char *perl_regex, *ptr;
1111
1112 perl_regex = *regex;
1113
1114 /* substitute Unicode Character Blocks with exact Character Ranges */
1115 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1116 start = ptr - perl_regex;
1117
1118 ptr = strchr(ptr, '}');
1119 if (!ptr) {
1120 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
1121 return LY_EVALID;
1122 }
1123 end = (ptr - perl_regex) + 1;
1124
1125 /* need more space */
1126 if (end - start < URANGE_LEN) {
1127 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko78419922022-07-15 15:15:20 +02001128 *regex = perl_regex;
Michal Vasko215d7fc2022-07-15 14:50:44 +02001129 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1130 }
1131
1132 /* find our range */
1133 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1134 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1135 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1136 break;
1137 }
1138 }
1139 if (!ublock2urange[idx][0]) {
1140 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
1141 return LY_EVALID;
1142 }
1143
1144 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1145 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1146 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1147 ++idx;
1148 }
1149 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1150 --idx;
1151 }
1152 }
1153 if (idx) {
1154 /* skip brackets */
1155 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1156 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1157 } else {
1158 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1159 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1160 }
1161 }
1162
Michal Vasko215d7fc2022-07-15 14:50:44 +02001163 return LY_SUCCESS;
1164}
1165
1166LY_ERR
1167lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
1168{
1169 size_t idx, size, brack;
1170 char *perl_regex;
1171 int err_code, compile_opts;
1172 const char *orig_ptr;
1173 PCRE2_SIZE err_offset;
1174 pcre2_code *code_local;
aPiecek9e716992022-07-21 16:29:47 +02001175 ly_bool escaped;
Michal Vasko215d7fc2022-07-15 14:50:44 +02001176 LY_ERR r;
1177
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001178 /* adjust the expression to a Perl equivalent
1179 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1180
1181 /* allocate space for the transformed pattern */
1182 size = strlen(pattern) + 1;
Michal Vasko03d430c2022-07-22 09:05:56 +02001183 compile_opts = PCRE2_UTF | PCRE2_UCP | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
Christian Hoppsdafed222021-03-22 13:02:42 -04001184#ifdef PCRE2_ENDANCHORED
1185 compile_opts |= PCRE2_ENDANCHORED;
1186#else
1187 /* add space for trailing $ anchor */
1188 size++;
1189#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001190 perl_regex = malloc(size);
1191 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1192 perl_regex[0] = '\0';
1193
1194 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1195 brack = 0;
1196 idx = 0;
aPiecek9e716992022-07-21 16:29:47 +02001197 escaped = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001198 orig_ptr = pattern;
1199 while (orig_ptr[0]) {
1200 switch (orig_ptr[0]) {
1201 case '$':
1202 case '^':
1203 if (!brack) {
1204 /* make space for the extra character */
1205 ++size;
1206 perl_regex = ly_realloc(perl_regex, size);
1207 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1208
1209 /* print escape slash */
1210 perl_regex[idx] = '\\';
1211 ++idx;
1212 }
1213 break;
aPiecek9e716992022-07-21 16:29:47 +02001214 case '\\':
1215 /* escape character found or backslash is escaped */
1216 escaped = !escaped;
1217 /* copy backslash and continue with the next character */
1218 perl_regex[idx] = orig_ptr[0];
1219 ++idx;
1220 ++orig_ptr;
1221 continue;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001222 case '[':
aPiecek9e716992022-07-21 16:29:47 +02001223 if (!escaped) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001224 ++brack;
1225 }
1226 break;
1227 case ']':
aPiecek9e716992022-07-21 16:29:47 +02001228 if (!brack && !escaped) {
1229 /* If ']' does not terminate a character class expression, then pcre2_compile() implicitly escapes the
1230 * ']' character. But this seems to be against the regular expressions rules declared in
1231 * "XML schema: Datatypes" and therefore an error is returned. So for example if pattern is '\[a]' then
1232 * pcre2 match characters '[a]' literally but in YANG such pattern is not allowed.
1233 */
1234 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, orig_ptr, "character group doesn't begin with '['");
1235 free(perl_regex);
1236 return LY_EVALID;
1237 } else if (!escaped) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001238 --brack;
1239 }
1240 break;
1241 default:
1242 break;
1243 }
1244
1245 /* copy char */
1246 perl_regex[idx] = orig_ptr[0];
1247
1248 ++idx;
1249 ++orig_ptr;
aPiecek9e716992022-07-21 16:29:47 +02001250 escaped = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001251 }
Christian Hoppsdafed222021-03-22 13:02:42 -04001252#ifndef PCRE2_ENDANCHORED
1253 /* anchor match to end of subject */
1254 perl_regex[idx++] = '$';
1255#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001256 perl_regex[idx] = '\0';
1257
Michal Vasko215d7fc2022-07-15 14:50:44 +02001258 /* transform character blocks */
1259 if ((r = lys_compile_pattern_chblocks_xmlschema2perl(ctx, pattern, &perl_regex))) {
1260 free(perl_regex);
1261 return r;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001262 }
1263
1264 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001265 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001266 &err_code, &err_offset, NULL);
1267 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001268 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1269 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001270 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001271 free(perl_regex);
1272 return LY_EVALID;
1273 }
1274 free(perl_regex);
1275
1276 if (code) {
1277 *code = code_local;
1278 } else {
1279 free(code_local);
1280 }
1281
1282 return LY_SUCCESS;
1283
1284#undef URANGE_LEN
1285}
1286
Michal Vasko51de7b72022-04-29 09:50:22 +02001287LY_ERR
Michal Vasko193dacd2022-10-13 08:43:05 +02001288lys_compile_type_patterns(struct lysc_ctx *ctx, const struct lysp_restr *patterns_p, struct lysc_pattern **base_patterns,
1289 struct lysc_pattern ***patterns)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001290{
1291 struct lysc_pattern **pattern;
1292 LY_ARRAY_COUNT_TYPE u;
1293 LY_ERR ret = LY_SUCCESS;
1294
1295 /* first, copy the patterns from the base type */
1296 if (base_patterns) {
1297 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1298 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1299 }
1300
1301 LY_ARRAY_FOR(patterns_p, u) {
1302 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1303 *pattern = calloc(1, sizeof **pattern);
1304 ++(*pattern)->refcount;
1305
Radek Krejci2efc45b2020-12-22 16:25:44 +01001306 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001307 LY_CHECK_RET(ret);
1308
Radek Krejcif13b87b2020-12-01 22:02:17 +01001309 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001310 (*pattern)->inverted = 1;
1311 }
1312 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1313 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1314 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1315 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1316 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001317 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001318 }
1319done:
1320 return ret;
1321}
1322
1323/**
1324 * @brief map of the possible restrictions combination for the specific built-in type.
1325 */
1326static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1327 0 /* LY_TYPE_UNKNOWN */,
1328 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1329 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1330 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1331 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1332 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1333 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1334 LYS_SET_BIT /* LY_TYPE_BITS */,
1335 0 /* LY_TYPE_BOOL */,
1336 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1337 0 /* LY_TYPE_EMPTY */,
1338 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1339 LYS_SET_BASE /* LY_TYPE_IDENT */,
1340 LYS_SET_REQINST /* LY_TYPE_INST */,
1341 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1342 LYS_SET_TYPE /* LY_TYPE_UNION */,
1343 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1344 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1345 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1346 LYS_SET_RANGE /* LY_TYPE_INT64 */
1347};
1348
1349/**
1350 * @brief stringification of the YANG built-in data types
1351 */
1352const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001353 LY_TYPE_UNKNOWN_STR,
1354 LY_TYPE_BINARY_STR,
1355 LY_TYPE_UINT8_STR,
1356 LY_TYPE_UINT16_STR,
1357 LY_TYPE_UINT32_STR,
1358 LY_TYPE_UINT64_STR,
1359 LY_TYPE_STRING_STR,
1360 LY_TYPE_BITS_STR,
1361 LY_TYPE_BOOL_STR,
1362 LY_TYPE_DEC64_STR,
1363 LY_TYPE_EMPTY_STR,
1364 LY_TYPE_ENUM_STR,
1365 LY_TYPE_IDENT_STR,
1366 LY_TYPE_INST_STR,
1367 LY_TYPE_LEAFREF_STR,
1368 LY_TYPE_UNION_STR,
1369 LY_TYPE_INT8_STR,
1370 LY_TYPE_INT16_STR,
1371 LY_TYPE_INT32_STR,
1372 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001373};
1374
Michal Vasko193dacd2022-10-13 08:43:05 +02001375LY_ERR
1376lys_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 +01001377 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001378{
1379 LY_ERR ret = LY_SUCCESS;
1380 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001381 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1382 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001383 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001384 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001385
1386 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001387 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001388 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1389 return LY_EVALID;
1390 }
1391
1392 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001393 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001394 if (base_enums) {
1395 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1396 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001397 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001398 break;
1399 }
1400 }
1401 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001402 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001403 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001404 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001405 return LY_EVALID;
1406 }
1407 match = v;
1408 }
1409
1410 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001411 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001412 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001413 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001414 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001415 LY_ARRAY_FOR(*bitenums, v) {
1416 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001417 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001418 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001419 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001420 return LY_EVALID;
1421 }
1422 }
1423 } else if (base_enums) {
1424 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001425 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001426 } else {
1427 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001428 if (u == 0) {
1429 cur_val = 0;
1430 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001431 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001432 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001433 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001434 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001435 } else {
1436 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001437 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001438 }
1439
1440 /* save highest value for auto assing */
1441 if (highest_value < cur_val) {
1442 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001443 }
1444 } else { /* LY_TYPE_BITS */
1445 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001446 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001447 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001448 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001449 LY_ARRAY_FOR(*bitenums, v) {
1450 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001451 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001452 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001453 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001454 return LY_EVALID;
1455 }
1456 }
1457 } else if (base_enums) {
1458 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001459 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001460 } else {
1461 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001462 if (u == 0) {
1463 cur_pos = 0;
1464 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001465 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001466 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001467 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001468 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001469 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001470 } else {
1471 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001472 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001473 }
1474
1475 /* save highest position for auto assing */
1476 if (highest_position < cur_pos) {
1477 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001478 }
1479 }
1480
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001481 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001482 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001483 if (basetype == LY_TYPE_ENUM) {
1484 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001485 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001486 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001487 enums_p[u].name, base_enums[match].value, cur_val);
1488 return LY_EVALID;
1489 }
1490 } else {
1491 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001492 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001493 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001494 enums_p[u].name, base_enums[match].position, cur_pos);
1495 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001496 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001497 }
1498 }
1499
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001500 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001501 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001502 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1503 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1504 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001505 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 +01001506 if (basetype == LY_TYPE_ENUM) {
1507 e->value = cur_val;
1508 } else {
1509 e->position = cur_pos;
1510 }
Radek Krejciab430862021-03-02 20:13:40 +01001511 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001512
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001513 /* evaluate if-ffeatures */
1514 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1515 if (!enabled) {
1516 /* set only flag, later resolved and removed */
1517 e->flags |= LYS_DISABLED;
1518 }
1519
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001520 if (basetype == LY_TYPE_BITS) {
1521 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001522 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001523 if (v != u) {
1524 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001525 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1526 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001527 }
1528 }
1529 }
1530
1531done:
1532 return ret;
1533}
1534
Radek Krejci6a205692020-12-09 13:58:22 +01001535static LY_ERR
1536lys_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 +01001537 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001538{
1539 LY_ERR ret = LY_SUCCESS;
1540 struct lysc_type **utypes = *utypes_p;
1541 struct lysc_type_union *un_aux = NULL;
1542
1543 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1544 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001545 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1546 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001547 LY_CHECK_GOTO(ret, error);
1548 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1549 /* add space for additional types from the union subtype */
1550 un_aux = (struct lysc_type_union *)utypes[u + additional];
1551 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1552 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1553
1554 /* copy subtypes of the subtype union */
1555 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1556 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1557 struct lysc_type_leafref *lref;
1558
1559 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1560 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1561 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1562 lref = (struct lysc_type_leafref *)utypes[u + additional];
1563
1564 lref->basetype = LY_TYPE_LEAFREF;
Michal Vaskoe33134a2022-07-29 14:54:40 +02001565 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 +01001566 LY_CHECK_GOTO(ret, error);
Michal Vasko080064b2021-08-31 15:20:44 +02001567 lref->refcount = 1;
Radek Krejci6a205692020-12-09 13:58:22 +01001568 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001569 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001570 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001571 LY_CHECK_GOTO(ret, error);
1572 /* TODO extensions */
1573
1574 } else {
1575 utypes[u + additional] = un_aux->types[v];
Michal Vasko04338d92021-09-01 07:58:14 +02001576 LY_ATOMIC_INC_BARRIER(un_aux->types[v]->refcount);
Radek Krejci6a205692020-12-09 13:58:22 +01001577 }
1578 ++additional;
1579 LY_ARRAY_INCREMENT(utypes);
1580 }
1581 /* compensate u increment in main loop */
1582 --additional;
1583
1584 /* free the replaced union subtype */
Michal Vaskoc636ea42022-09-16 10:20:31 +02001585 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001586 un_aux = NULL;
1587 } else {
1588 LY_ARRAY_INCREMENT(utypes);
1589 }
1590 }
1591
1592 *utypes_p = utypes;
1593 return LY_SUCCESS;
1594
1595error:
1596 if (un_aux) {
Michal Vaskoc636ea42022-09-16 10:20:31 +02001597 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)un_aux);
Radek Krejci6a205692020-12-09 13:58:22 +01001598 }
1599 *utypes_p = utypes;
1600 return ret;
1601}
1602
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001603/**
1604 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1605 * @param[in] ctx Compile context.
1606 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1607 * @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 +02001608 * @param[in] context_name Name of the context node or referencing typedef for logging.
1609 * @param[in] type_p Parsed type to compile.
1610 * @param[in] basetype Base YANG built-in type of the type to compile.
1611 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1612 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1613 * @param[out] type Newly created type structure with the filled information about the type.
1614 * @return LY_ERR value.
1615 */
1616static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001617lys_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 +02001618 struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, const struct lysc_type *base,
1619 struct lysc_type **type)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001620{
1621 LY_ERR ret = LY_SUCCESS;
1622 struct lysc_type_bin *bin;
1623 struct lysc_type_num *num;
1624 struct lysc_type_str *str;
1625 struct lysc_type_bits *bits;
1626 struct lysc_type_enum *enumeration;
1627 struct lysc_type_dec *dec;
1628 struct lysc_type_identityref *idref;
1629 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001630 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001631
1632 switch (basetype) {
1633 case LY_TYPE_BINARY:
1634 bin = (struct lysc_type_bin *)(*type);
1635
1636 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1637 if (type_p->length) {
1638 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1639 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1640 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001641 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001642 }
1643 }
1644 break;
1645 case LY_TYPE_BITS:
1646 /* RFC 7950 9.7 - bits */
1647 bits = (struct lysc_type_bits *)(*type);
1648 if (type_p->bits) {
1649 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1650 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1651 (struct lysc_type_bitenum_item **)&bits->bits));
1652 }
1653
1654 if (!base && !type_p->flags) {
1655 /* type derived from bits built-in type must contain at least one bit */
1656 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001657 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001658 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001659 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001660 }
1661 return LY_EVALID;
1662 }
1663 break;
1664 case LY_TYPE_DEC64:
1665 dec = (struct lysc_type_dec *)(*type);
1666
1667 /* RFC 7950 9.3.4 - fraction-digits */
1668 if (!base) {
1669 if (!type_p->fraction_digits) {
1670 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001671 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001672 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001673 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001674 }
1675 return LY_EVALID;
1676 }
1677 dec->fraction_digits = type_p->fraction_digits;
1678 } else {
1679 if (type_p->fraction_digits) {
1680 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1681 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001682 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001683 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1684 tpdfname);
1685 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001686 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001687 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1688 }
1689 return LY_EVALID;
1690 }
1691 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1692 }
1693
1694 /* RFC 7950 9.2.4 - range */
1695 if (type_p->range) {
1696 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1697 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1698 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001699 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001700 }
1701 }
1702 break;
1703 case LY_TYPE_STRING:
1704 str = (struct lysc_type_str *)(*type);
1705
1706 /* RFC 7950 9.4.4 - length */
1707 if (type_p->length) {
1708 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1709 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1710 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001711 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001712 }
1713 } else if (base && ((struct lysc_type_str *)base)->length) {
1714 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1715 }
1716
1717 /* RFC 7950 9.4.5 - pattern */
1718 if (type_p->patterns) {
1719 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1720 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1721 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1722 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1723 }
1724 break;
1725 case LY_TYPE_ENUM:
1726 enumeration = (struct lysc_type_enum *)(*type);
1727
1728 /* RFC 7950 9.6 - enum */
1729 if (type_p->enums) {
1730 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1731 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1732 }
1733
1734 if (!base && !type_p->flags) {
1735 /* type derived from enumerations built-in type must contain at least one enum */
1736 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001737 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001738 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001739 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001740 }
1741 return LY_EVALID;
1742 }
1743 break;
1744 case LY_TYPE_INT8:
1745 case LY_TYPE_UINT8:
1746 case LY_TYPE_INT16:
1747 case LY_TYPE_UINT16:
1748 case LY_TYPE_INT32:
1749 case LY_TYPE_UINT32:
1750 case LY_TYPE_INT64:
1751 case LY_TYPE_UINT64:
1752 num = (struct lysc_type_num *)(*type);
1753
1754 /* RFC 6020 9.2.4 - range */
1755 if (type_p->range) {
1756 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1757 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1758 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001759 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001760 }
1761 }
1762 break;
1763 case LY_TYPE_IDENT:
1764 idref = (struct lysc_type_identityref *)(*type);
1765
1766 /* RFC 7950 9.10.2 - base */
1767 if (type_p->bases) {
1768 if (base) {
1769 /* only the directly derived identityrefs can contain base specification */
1770 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001771 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001772 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1773 tpdfname);
1774 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001775 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001776 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1777 }
1778 return LY_EVALID;
1779 }
aPiecekf4a0a192021-08-03 15:14:17 +02001780 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001781 }
1782
1783 if (!base && !type_p->flags) {
1784 /* type derived from identityref built-in type must contain at least one base */
1785 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001786 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001787 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001788 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001789 }
1790 return LY_EVALID;
1791 }
1792 break;
1793 case LY_TYPE_LEAFREF:
1794 lref = (struct lysc_type_leafref *)*type;
1795
1796 /* RFC 7950 9.9.3 - require-instance */
1797 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001798 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001799 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001800 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001801 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1802 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001803 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001804 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1805 }
1806 return LY_EVALID;
1807 }
1808 lref->require_instance = type_p->require_instance;
1809 } else if (base) {
1810 /* inherit */
1811 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1812 } else {
1813 /* default is true */
1814 lref->require_instance = 1;
1815 }
1816 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001817 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001818
Michal Vaskoe33134a2022-07-29 14:54:40 +02001819 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, 0, 0, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001820 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 +02001821 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001822 } else if (base) {
Michal Vaskoe33134a2022-07-29 14:54:40 +02001823 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 +02001824 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001825 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001826 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001827 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001828 return LY_EVALID;
1829 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001830 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001831 return LY_EVALID;
1832 }
1833 break;
1834 case LY_TYPE_INST:
1835 /* RFC 7950 9.9.3 - require-instance */
1836 if (type_p->flags & LYS_SET_REQINST) {
1837 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1838 } else {
1839 /* default is true */
1840 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1841 }
1842 break;
1843 case LY_TYPE_UNION:
1844 un = (struct lysc_type_union *)(*type);
1845
1846 /* RFC 7950 7.4 - type */
1847 if (type_p->types) {
1848 if (base) {
1849 /* only the directly derived union can contain types specification */
1850 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001851 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001852 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1853 tpdfname);
1854 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001855 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001856 "Invalid type substatement for the type not directly derived from union built-in type.");
1857 }
1858 return LY_EVALID;
1859 }
1860 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001861 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 +02001862 }
1863
1864 if (!base && !type_p->flags) {
1865 /* type derived from union built-in type must contain at least one type */
1866 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001867 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001868 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001869 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001870 }
1871 return LY_EVALID;
1872 }
1873 break;
1874 case LY_TYPE_BOOL:
1875 case LY_TYPE_EMPTY:
1876 case LY_TYPE_UNKNOWN: /* just to complete switch */
1877 break;
1878 }
1879
1880 if (tpdfname) {
1881 switch (basetype) {
1882 case LY_TYPE_BINARY:
1883 type_p->compiled = *type;
1884 *type = calloc(1, sizeof(struct lysc_type_bin));
1885 break;
1886 case LY_TYPE_BITS:
1887 type_p->compiled = *type;
1888 *type = calloc(1, sizeof(struct lysc_type_bits));
1889 break;
1890 case LY_TYPE_DEC64:
1891 type_p->compiled = *type;
1892 *type = calloc(1, sizeof(struct lysc_type_dec));
1893 break;
1894 case LY_TYPE_STRING:
1895 type_p->compiled = *type;
1896 *type = calloc(1, sizeof(struct lysc_type_str));
1897 break;
1898 case LY_TYPE_ENUM:
1899 type_p->compiled = *type;
1900 *type = calloc(1, sizeof(struct lysc_type_enum));
1901 break;
1902 case LY_TYPE_INT8:
1903 case LY_TYPE_UINT8:
1904 case LY_TYPE_INT16:
1905 case LY_TYPE_UINT16:
1906 case LY_TYPE_INT32:
1907 case LY_TYPE_UINT32:
1908 case LY_TYPE_INT64:
1909 case LY_TYPE_UINT64:
1910 type_p->compiled = *type;
1911 *type = calloc(1, sizeof(struct lysc_type_num));
1912 break;
1913 case LY_TYPE_IDENT:
1914 type_p->compiled = *type;
1915 *type = calloc(1, sizeof(struct lysc_type_identityref));
1916 break;
1917 case LY_TYPE_LEAFREF:
1918 type_p->compiled = *type;
1919 *type = calloc(1, sizeof(struct lysc_type_leafref));
1920 break;
1921 case LY_TYPE_INST:
1922 type_p->compiled = *type;
1923 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1924 break;
1925 case LY_TYPE_UNION:
1926 type_p->compiled = *type;
1927 *type = calloc(1, sizeof(struct lysc_type_union));
1928 break;
1929 case LY_TYPE_BOOL:
1930 case LY_TYPE_EMPTY:
1931 case LY_TYPE_UNKNOWN: /* just to complete switch */
1932 break;
1933 }
1934 }
1935 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1936
1937cleanup:
1938 return ret;
1939}
1940
1941LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001942lys_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 +02001943 const struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001944{
1945 LY_ERR ret = LY_SUCCESS;
1946 ly_bool dummyloops = 0;
1947 struct type_context {
1948 const struct lysp_tpdf *tpdf;
1949 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001950 } *tctx, *tctx_prev = NULL, *tctx_iter;
1951 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1952 struct lysc_type *base = NULL, *prev_type;
1953 struct ly_set tpdf_chain = {0};
Michal Vasko388a6632021-08-06 11:27:43 +02001954 struct lyplg_type *plugin;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001955
1956 (*type) = NULL;
1957 if (dflt) {
1958 *dflt = NULL;
1959 }
1960
1961 tctx = calloc(1, sizeof *tctx);
1962 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001963 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 +02001964 ret == LY_SUCCESS;
Michal Vaskoedb0fa52022-10-04 10:36:00 +02001965 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 +01001966 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001967 if (basetype) {
1968 break;
1969 }
1970
1971 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001972 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1973 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001974 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1975
1976 if (units && !*units) {
1977 /* inherit units */
1978 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1979 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1980 }
1981 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1982 /* inherit default */
1983 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1984 }
1985 if (dummyloops && (!units || *units) && dflt && *dflt) {
1986 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1987 break;
1988 }
1989
Michal Vasko080064b2021-08-31 15:20:44 +02001990 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
Radek Krejci720d2612021-03-03 19:44:22 +01001991 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1992 * and we need now recompile the type again in the updated context. */
Michal Vaskoc636ea42022-09-16 10:20:31 +02001993 lysc_type_free(&ctx->free_ctx, tctx->tpdf->type.compiled);
Radek Krejci720d2612021-03-03 19:44:22 +01001994 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1995 }
1996
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001997 if (tctx->tpdf->type.compiled) {
1998 /* it is not necessary to continue, the rest of the chain was already compiled,
1999 * but we still may need to inherit default and units values, so start dummy loops */
2000 basetype = tctx->tpdf->type.compiled->basetype;
2001 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2002 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2003
2004 if ((units && !*units) || (dflt && !*dflt)) {
2005 dummyloops = 1;
2006 goto preparenext;
2007 } else {
2008 tctx = NULL;
2009 break;
2010 }
2011 }
2012
2013 /* circular typedef reference detection */
2014 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
2015 /* local part */
2016 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002017 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002018 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002019 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2020 free(tctx);
2021 ret = LY_EVALID;
2022 goto cleanup;
2023 }
2024 }
2025 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
2026 /* global part for unions corner case */
2027 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01002028 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002029 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002030 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2031 free(tctx);
2032 ret = LY_EVALID;
2033 goto cleanup;
2034 }
2035 }
2036
2037 /* store information for the following processing */
2038 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2039 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2040
2041preparenext:
2042 /* prepare next loop */
2043 tctx_prev = tctx;
2044 tctx = calloc(1, sizeof *tctx);
2045 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2046 }
2047 free(tctx);
2048
2049 /* allocate type according to the basetype */
2050 switch (basetype) {
2051 case LY_TYPE_BINARY:
2052 *type = calloc(1, sizeof(struct lysc_type_bin));
2053 break;
2054 case LY_TYPE_BITS:
2055 *type = calloc(1, sizeof(struct lysc_type_bits));
2056 break;
2057 case LY_TYPE_BOOL:
2058 case LY_TYPE_EMPTY:
2059 *type = calloc(1, sizeof(struct lysc_type));
2060 break;
2061 case LY_TYPE_DEC64:
2062 *type = calloc(1, sizeof(struct lysc_type_dec));
2063 break;
2064 case LY_TYPE_ENUM:
2065 *type = calloc(1, sizeof(struct lysc_type_enum));
2066 break;
2067 case LY_TYPE_IDENT:
2068 *type = calloc(1, sizeof(struct lysc_type_identityref));
2069 break;
2070 case LY_TYPE_INST:
2071 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2072 break;
2073 case LY_TYPE_LEAFREF:
2074 *type = calloc(1, sizeof(struct lysc_type_leafref));
2075 break;
2076 case LY_TYPE_STRING:
2077 *type = calloc(1, sizeof(struct lysc_type_str));
2078 break;
2079 case LY_TYPE_UNION:
2080 *type = calloc(1, sizeof(struct lysc_type_union));
2081 break;
2082 case LY_TYPE_INT8:
2083 case LY_TYPE_UINT8:
2084 case LY_TYPE_INT16:
2085 case LY_TYPE_UINT16:
2086 case LY_TYPE_INT32:
2087 case LY_TYPE_UINT32:
2088 case LY_TYPE_INT64:
2089 case LY_TYPE_UINT64:
2090 *type = calloc(1, sizeof(struct lysc_type_num));
2091 break;
2092 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002093 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002094 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2095 ret = LY_EVALID;
2096 goto cleanup;
2097 }
2098 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
2099 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002100 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002101 ly_data_type2str[basetype]);
2102 free(*type);
2103 (*type) = NULL;
2104 ret = LY_EVALID;
2105 goto cleanup;
2106 }
2107
2108 /* get restrictions from the referred typedefs */
2109 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2110 tctx = (struct type_context *)tpdf_chain.objs[u];
2111
2112 /* remember the typedef context for circular check */
2113 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
2114 LY_CHECK_GOTO(ret, cleanup);
2115
2116 if (tctx->tpdf->type.compiled) {
Michal Vasko388a6632021-08-06 11:27:43 +02002117 /* already compiled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002118 base = tctx->tpdf->type.compiled;
2119 continue;
Michal Vasko388a6632021-08-06 11:27:43 +02002120 }
2121
Michal Vaskoddd76592022-01-17 13:34:48 +01002122 /* try to find loaded user type plugins */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002123 plugin = lyplg_type_plugin_find(tctx->tpdf->type.pmod->mod->name, tctx->tpdf->type.pmod->mod->revision,
Michal Vaskoddd76592022-01-17 13:34:48 +01002124 tctx->tpdf->name);
Haithem Ben Ghorbal45be15f2022-07-12 18:02:36 +02002125 if (!plugin && base) {
2126 /* use the base type implementation if available */
2127 plugin = base->plugin;
2128 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002129 if (!plugin) {
2130 /* use the internal built-in type implementation */
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002131 plugin = lyplg_type_plugin_find("", NULL, ly_data_type2str[basetype]);
Michal Vaskoddd76592022-01-17 13:34:48 +01002132 }
Michal Vasko388a6632021-08-06 11:27:43 +02002133 assert(plugin);
2134
2135 if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags) &&
2136 (plugin == base->plugin)) {
2137 /* no change, reuse the compiled base */
2138 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002139 LY_ATOMIC_INC_BARRIER(base->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002140 continue;
2141 }
2142
Michal Vasko04338d92021-09-01 07:58:14 +02002143 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002144 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002145 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002146 tctx->tpdf->name, ly_data_type2str[basetype]);
2147 ret = LY_EVALID;
2148 goto cleanup;
2149 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002150 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002151 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2152 tctx->tpdf->name, tctx->tpdf->dflt.str);
2153 ret = LY_EVALID;
2154 goto cleanup;
2155 }
2156
2157 (*type)->basetype = basetype;
Michal Vasko388a6632021-08-06 11:27:43 +02002158 (*type)->plugin = plugin;
2159
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002160 /* collect extensions */
2161 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko388a6632021-08-06 11:27:43 +02002162
2163 /* compile the new typedef */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002164 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01002165 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002166 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
2167 LY_CHECK_GOTO(ret, cleanup);
2168 base = prev_type;
2169 }
2170 /* remove the processed typedef contexts from the stack for circular check */
2171 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
2172
2173 /* process the type definition in leaf */
2174 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
2175 /* get restrictions from the node itself */
2176 (*type)->basetype = basetype;
Michal Vaskoc0c64ae2022-10-06 10:15:23 +02002177 (*type)->plugin = base ? base->plugin : lyplg_type_plugin_find("", NULL, ly_data_type2str[basetype]);
Michal Vasko04338d92021-09-01 07:58:14 +02002178 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko193dacd2022-10-13 08:43:05 +02002179 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, (struct lysp_type *)type_p, basetype,
2180 NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002181 LY_CHECK_GOTO(ret, cleanup);
2182 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
2183 /* no specific restriction in leaf's type definition, copy from the base */
2184 free(*type);
2185 (*type) = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002186 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002187 }
2188
Radek Krejciab430862021-03-02 20:13:40 +01002189 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002190
2191cleanup:
2192 ly_set_erase(&tpdf_chain, free);
2193 return ret;
2194}
2195
2196/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002197 * @brief Check uniqness of the node/action/notification name.
2198 *
2199 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2200 * structures, but they share the namespace so we need to check their name collisions.
2201 *
2202 * @param[in] ctx Compile context.
2203 * @param[in] parent Parent of the nodes to check, can be NULL.
2204 * @param[in] name Name of the item to find in the given lists.
2205 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2206 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2207 */
2208static LY_ERR
2209lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2210 const struct lysc_node *exclude)
2211{
2212 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002213 const struct lysc_node_action *actions;
2214 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002215 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002216 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002217
2218#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2219
2220 if (exclude->nodetype == LYS_CASE) {
2221 /* check restricted only to all the cases */
2222 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002223 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002224 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002225 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002226 return LY_EEXIST;
2227 }
2228 }
2229
2230 return LY_SUCCESS;
2231 }
2232
2233 /* no reason for our parent to be choice anymore */
2234 assert(!parent || (parent->nodetype != LYS_CHOICE));
2235
2236 if (parent && (parent->nodetype == LYS_CASE)) {
2237 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002238
2239 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2240 iter = lysc_data_parent(parent);
2241 do {
2242 parent = parent->parent;
2243 if (parent && (parent->nodetype == LYS_CHOICE)) {
2244 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2245 }
2246 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002247 }
2248
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002249 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002250 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2251 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2252 if (exclude->flags & LYS_IS_OUTPUT) {
2253 getnext_flags |= LYS_GETNEXT_OUTPUT;
2254 parent = lysc_node_child(parent)->next;
2255 } else {
2256 parent = lysc_node_child(parent);
2257 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002258 }
2259
2260 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002261 if (!parent && ctx->ext) {
2262 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2263 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2264 goto error;
2265 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002266
Radek Krejci5fa32a32021-02-08 18:12:38 +01002267 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2268 if (iter->nodetype == LYS_CHOICE) {
2269 iter2 = NULL;
2270 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2271 if (CHECK_NODE(iter2, exclude, name)) {
2272 goto error;
2273 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002274 }
2275 }
2276 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002277 } else {
2278 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2279 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2280 goto error;
2281 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002282
Radek Krejci5fa32a32021-02-08 18:12:38 +01002283 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2284 if (iter->nodetype == LYS_CHOICE) {
2285 iter2 = NULL;
2286 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2287 if (CHECK_NODE(iter2, exclude, name)) {
2288 goto error;
2289 }
2290 }
2291 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002292 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002293
Radek Krejci5fa32a32021-02-08 18:12:38 +01002294 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2295 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2296 if (CHECK_NODE(iter, exclude, name)) {
2297 goto error;
2298 }
2299 }
2300
2301 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2302 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2303 if (CHECK_NODE(iter, exclude, name)) {
2304 goto error;
2305 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002306 }
2307 }
Radek Krejci078e4342021-02-12 18:17:26 +01002308 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002309 return LY_SUCCESS;
2310
2311error:
Radek Krejci078e4342021-02-12 18:17:26 +01002312 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002313 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002314 return LY_EEXIST;
2315
2316#undef CHECK_NODE
2317}
2318
Michal Vasko193dacd2022-10-13 08:43:05 +02002319LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002320lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002321{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002322 struct lysc_node **children, *anchor = NULL;
2323 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002324
Radek Krejci2a9fc652021-01-22 17:44:34 +01002325 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002326
Radek Krejci2a9fc652021-01-22 17:44:34 +01002327 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002328 if (node->nodetype == LYS_INPUT) {
2329 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2330 /* input node is part of the action but link it with output */
2331 node->next = &((struct lysc_node_action *)parent)->output.node;
2332 node->prev = node->next;
2333 return LY_SUCCESS;
2334 } else if (node->nodetype == LYS_OUTPUT) {
2335 /* output node is part of the action but link it with input */
2336 node->next = NULL;
2337 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002338 return LY_SUCCESS;
2339 } else if (node->nodetype == LYS_ACTION) {
2340 children = (struct lysc_node **)lysc_node_actions_p(parent);
2341 } else if (node->nodetype == LYS_NOTIF) {
2342 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2343 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002344 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002345 }
2346 assert(children);
2347
2348 if (!(*children)) {
2349 /* first child */
2350 *children = node;
2351 } else if (node->flags & LYS_KEY) {
2352 /* special handling of adding keys */
2353 assert(node->module == parent->module);
2354 anchor = *children;
2355 if (anchor->flags & LYS_KEY) {
2356 while ((anchor->flags & LYS_KEY) && anchor->next) {
2357 anchor = anchor->next;
2358 }
2359 /* insert after the last key */
2360 insert_after = 1;
2361 } /* else insert before anchor (at the beginning) */
2362 } else if ((*children)->prev->module == node->module) {
2363 /* last child is from the same module, keep the order and insert at the end */
2364 anchor = (*children)->prev;
2365 insert_after = 1;
2366 } else if (parent->module == node->module) {
2367 /* adding module child after some augments were connected */
2368 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2369 } else {
2370 /* some augments are already connected and we are connecting new ones,
2371 * keep module name order and insert the node into the children list */
2372 anchor = *children;
2373 do {
2374 anchor = anchor->prev;
2375
2376 /* check that we have not found the last augment node from our module or
2377 * the first augment node from a "smaller" module or
2378 * the first node from a local module */
2379 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2380 (anchor->module == parent->module)) {
2381 /* insert after */
2382 insert_after = 1;
2383 break;
2384 }
2385
2386 /* we have traversed all the nodes, insert before anchor (as the first node) */
2387 } while (anchor->prev->next);
2388 }
2389
2390 /* insert */
2391 if (anchor) {
2392 if (insert_after) {
2393 node->next = anchor->next;
2394 node->prev = anchor;
2395 anchor->next = node;
2396 if (node->next) {
2397 /* middle node */
2398 node->next->prev = node;
2399 } else {
2400 /* last node */
2401 (*children)->prev = node;
2402 }
2403 } else {
2404 node->next = anchor;
2405 node->prev = anchor->prev;
2406 anchor->prev = node;
2407 if (anchor == *children) {
2408 /* first node */
2409 *children = node;
2410 } else {
2411 /* middle node */
2412 node->prev->next = node;
2413 }
2414 }
2415 }
2416
2417 /* check the name uniqueness (even for an only child, it may be in case) */
2418 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2419 return LY_EEXIST;
2420 }
2421 } else {
2422 /* top-level element */
2423 struct lysc_node **list;
2424
Radek Krejci6b88a462021-02-17 12:39:34 +01002425 if (ctx->ext) {
Michal Vasko193dacd2022-10-13 08:43:05 +02002426 lyplg_ext_get_storage_p(ctx->ext, LY_STMT_DATA_NODE_MASK, (const void ***)&list);
Radek Krejci6b88a462021-02-17 12:39:34 +01002427 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002428 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2429 } else if (node->nodetype == LYS_NOTIF) {
2430 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2431 } else {
2432 list = &ctx->cur_mod->compiled->data;
2433 }
2434 if (!(*list)) {
2435 *list = node;
2436 } else {
2437 /* insert at the end of the module's top-level nodes list */
2438 (*list)->prev->next = node;
2439 node->prev = (*list)->prev;
2440 (*list)->prev = node;
2441 }
2442
2443 /* check the name uniqueness on top-level */
2444 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2445 return LY_EEXIST;
2446 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002447 }
2448
Radek Krejci2a9fc652021-01-22 17:44:34 +01002449 return LY_SUCCESS;
2450}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002451
Radek Krejci2a9fc652021-01-22 17:44:34 +01002452/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002453 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002454 *
2455 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002456 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002457 * @return LY_ERR value.
2458 */
2459static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002460lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002461{
2462 /* case never has any explicit config */
2463 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002464
Michal Vasko7c565922021-06-10 14:58:27 +02002465 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Radek Krejci91531e12021-02-08 19:57:54 +01002466 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002467 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002468 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2469 /* config not explicitly set, inherit it from parent */
Michal Vaskoecdc2222022-01-24 08:45:44 +01002470 assert(!node->parent || (node->parent->flags & LYS_CONFIG_MASK) || (node->parent->nodetype & LYS_AUGMENT));
2471 if (node->parent && (node->parent->flags & LYS_CONFIG_MASK)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002472 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002473 } else {
2474 /* default is config true */
2475 node->flags |= LYS_CONFIG_W;
2476 }
2477 } else {
2478 /* config set explicitly */
2479 node->flags |= LYS_SET_CONFIG;
2480 }
2481
Radek Krejci91531e12021-02-08 19:57:54 +01002482 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002483 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002484 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002485 }
2486
Radek Krejci2a9fc652021-01-22 17:44:34 +01002487 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002488}
2489
Radek Krejci91531e12021-02-08 19:57:54 +01002490/**
2491 * @brief Set various flags of the compiled nodes
2492 *
2493 * @param[in] ctx Compile context.
2494 * @param[in] node Compiled node where the flags will be set.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002495 * @param[in] inherited_status Explicitly inherited status (from uses/extension instance), if any.
Radek Krejci91531e12021-02-08 19:57:54 +01002496 */
2497static LY_ERR
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002498lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, const uint16_t *inherited_status)
Radek Krejci91531e12021-02-08 19:57:54 +01002499{
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002500 uint16_t parent_flags;
2501 const char *parent_name;
2502
Radek Krejci91531e12021-02-08 19:57:54 +01002503 /* inherit config flags */
2504 LY_CHECK_RET(lys_compile_config(ctx, node));
2505
2506 /* status - it is not inherited by specification, but it does not make sense to have
2507 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002508 parent_flags = inherited_status ? *inherited_status : (node->parent ? node->parent->flags : 0);
2509 parent_name = inherited_status ? "<schema-only-node>" : (node->parent ? node->parent->name : NULL);
2510 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, node->name, parent_flags, parent_name, inherited_status ? 1 : 0));
Radek Krejci91531e12021-02-08 19:57:54 +01002511
2512 /* other flags */
Michal Vasko7c565922021-06-10 14:58:27 +02002513 if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002514 node->flags |= LYS_IS_INPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002515 } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002516 node->flags |= LYS_IS_OUTPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002517 } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002518 node->flags |= LYS_IS_NOTIF;
2519 }
2520
2521 return LY_SUCCESS;
2522}
2523
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002524static LY_ERR
2525lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, const uint16_t *inherited_status,
Radek Krejci2a9fc652021-01-22 17:44:34 +01002526 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2527 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002528{
2529 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002530 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002531 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002532 struct lysp_when *pwhen = NULL;
Michal Vaskoe02e7402021-07-23 08:29:28 +02002533 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002534
Radek Krejci2a9fc652021-01-22 17:44:34 +01002535 node->nodetype = pnode->nodetype;
2536 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002537 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002538 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002539 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002540
Radek Krejci2a9fc652021-01-22 17:44:34 +01002541 /* compile any deviations for this node */
2542 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vaskoe02e7402021-07-23 08:29:28 +02002543 if (not_supported && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2544 /* if not supported, keep it just like disabled nodes by if-feature */
2545 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
2546 ctx->compile_opts |= LYS_COMPILE_DISABLED;
2547 }
2548 if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002549 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002550 }
2551
Radek Krejci2a9fc652021-01-22 17:44:34 +01002552 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vaskodfd254d2021-06-24 09:24:59 +02002553 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2554 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2555 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002556
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002557 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002558 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002559 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002560 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002561 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002562 }
2563
Radek Krejci91531e12021-02-08 19:57:54 +01002564 /* config, status and other flags */
Michal Vaskoedb0fa52022-10-04 10:36:00 +02002565 ret = lys_compile_node_flags(ctx, node, inherited_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002566 LY_CHECK_GOTO(ret, error);
2567
2568 /* list ordering */
2569 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002570 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002571 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002572 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002573 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002574 node->flags &= ~LYS_ORDBY_MASK;
2575 node->flags |= LYS_ORDBY_SYSTEM;
2576 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2577 /* default ordering is system */
2578 node->flags |= LYS_ORDBY_SYSTEM;
2579 }
2580 }
2581
Radek Krejci2a9fc652021-01-22 17:44:34 +01002582 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2583 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2584
Radek Krejci9a3823e2021-01-27 20:26:46 +01002585 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002586 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002587 ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002588 LY_CHECK_GOTO(ret, cleanup);
2589 }
2590
Radek Krejci2a9fc652021-01-22 17:44:34 +01002591 /* nodetype-specific part */
2592 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2593
2594 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002595 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002596 if (node->flags & LYS_MAND_TRUE) {
2597 /* inherit LYS_MAND_TRUE in parent containers */
2598 lys_compile_mandatory_parents(parent, 1);
2599 }
2600
2601 if (child_set) {
2602 /* add the new node into set */
2603 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2604 }
2605
2606 goto cleanup;
2607
2608error:
Michal Vaskoc636ea42022-09-16 10:20:31 +02002609 lysc_node_free(&ctx->free_ctx, node, 0);
2610
Radek Krejci2a9fc652021-01-22 17:44:34 +01002611cleanup:
2612 if (ret && dev_pnode) {
2613 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2614 }
Michal Vaskoe02e7402021-07-23 08:29:28 +02002615 ctx->compile_opts = prev_opts;
Michal Vaskoc636ea42022-09-16 10:20:31 +02002616 lysp_dev_node_free(ctx, dev_pnode);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002617 return ret;
2618}
2619
Radek Krejci2a9fc652021-01-22 17:44:34 +01002620LY_ERR
2621lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2622{
2623 LY_ERR ret = LY_SUCCESS;
2624 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002625 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002626
2627 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2628 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2629
2630 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002631 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002632 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002633
Radek Krejci01180ac2021-01-27 08:48:22 +01002634 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002635 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2636 }
2637
Michal Vasko95f736c2022-06-08 12:03:31 +02002638 /* connect any augments */
2639 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2640
Michal Vasko7c565922021-06-10 14:58:27 +02002641 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002642
2643done:
2644 return ret;
2645}
2646
2647/**
2648 * @brief Compile parsed action node information.
Michal Vasko193dacd2022-10-13 08:43:05 +02002649 *
Radek Krejci2a9fc652021-01-22 17:44:34 +01002650 * @param[in] ctx Compile context
2651 * @param[in] pnode Parsed action node.
2652 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2653 * is enriched with the action-specific information.
2654 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2655 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002656static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002657lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2658{
2659 LY_ERR ret;
2660 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2661 struct lysc_node_action *action = (struct lysc_node_action *)node;
2662 struct lysp_node_action_inout *input, implicit_input = {
2663 .nodetype = LYS_INPUT,
2664 .name = "input",
2665 .parent = pnode,
2666 };
2667 struct lysp_node_action_inout *output, implicit_output = {
2668 .nodetype = LYS_OUTPUT,
2669 .name = "output",
2670 .parent = pnode,
2671 };
2672
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002673 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002674 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002675 if (action_p->input.nodetype == LYS_UNKNOWN) {
2676 input = &implicit_input;
2677 } else {
2678 input = &action_p->input;
2679 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002680 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 +01002681 lysc_update_path(ctx, NULL, NULL);
2682 LY_CHECK_GOTO(ret, done);
2683
Michal Vaskoc130e162021-10-19 11:30:00 +02002684 /* add must(s) to unres */
2685 ret = lysc_unres_must_add(ctx, &action->input.node, &input->node);
2686 LY_CHECK_GOTO(ret, done);
2687
Radek Krejci2a9fc652021-01-22 17:44:34 +01002688 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002689 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002690 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002691 output = &implicit_output;
2692 } else {
2693 output = &action_p->output;
2694 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002695 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 +01002696 lysc_update_path(ctx, NULL, NULL);
2697 LY_CHECK_GOTO(ret, done);
2698
Michal Vaskoc130e162021-10-19 11:30:00 +02002699 /* add must(s) to unres */
2700 ret = lysc_unres_must_add(ctx, &action->output.node, &output->node);
2701 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002702
2703done:
2704 return ret;
2705}
2706
2707/**
2708 * @brief Compile parsed action node information.
2709 * @param[in] ctx Compile context
2710 * @param[in] pnode Parsed action node.
2711 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2712 * is enriched with the action-specific information.
2713 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2714 */
Michal Vasko193dacd2022-10-13 08:43:05 +02002715static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002716lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2717{
2718 LY_ERR ret = LY_SUCCESS;
2719 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2720 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2721 struct lysp_node *child_p;
2722
2723 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002724
2725 /* add must(s) to unres */
2726 ret = lysc_unres_must_add(ctx, node, pnode);
2727 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002728
Radek Krejci01180ac2021-01-27 08:48:22 +01002729 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002730 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002731 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002732 }
2733
Michal Vasko95f736c2022-06-08 12:03:31 +02002734 /* connect any augments */
2735 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2736
Radek Krejci2a9fc652021-01-22 17:44:34 +01002737done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002738 return ret;
2739}
2740
2741/**
2742 * @brief Compile parsed container node information.
2743 * @param[in] ctx Compile context
2744 * @param[in] pnode Parsed container node.
2745 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2746 * is enriched with the container-specific information.
2747 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2748 */
2749static LY_ERR
2750lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2751{
2752 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2753 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2754 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002755 LY_ERR ret = LY_SUCCESS;
2756
2757 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002758 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002759 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002760 }
2761
2762 /* more cases when the container has meaning but is kept NP for convenience:
2763 * - when condition
2764 * - direct child action/notification
2765 */
2766
2767 LY_LIST_FOR(cont_p->child, child_p) {
2768 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2769 LY_CHECK_GOTO(ret, done);
2770 }
2771
Michal Vasko5347e3a2020-11-03 17:14:57 +01002772 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002773
2774 /* add must(s) to unres */
2775 ret = lysc_unres_must_add(ctx, node, pnode);
2776 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002777
Michal Vasko95f736c2022-06-08 12:03:31 +02002778 /* connect any augments */
2779 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2780
Radek Krejci2a9fc652021-01-22 17:44:34 +01002781 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2782 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2783 LY_CHECK_GOTO(ret, done);
2784 }
2785 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2786 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2787 LY_CHECK_GOTO(ret, done);
2788 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002789
2790done:
2791 return ret;
2792}
2793
Michal Vasko72244882021-01-12 15:21:05 +01002794/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002795 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2796 * @param[in] ctx Compile context.
2797 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2798 * @param[in] type_p Parsed type to compile.
2799 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2800 * @return LY_ERR value.
2801 */
2802static LY_ERR
2803lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2804 struct lysc_node_leaf *leaf)
2805{
2806 struct lysp_qname *dflt;
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002807 struct lysc_type **t;
2808 LY_ARRAY_COUNT_TYPE u, count;
2809 ly_bool in_unres = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002810
Michal Vaskoa99b3572021-02-01 11:54:58 +01002811 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002812 leaf->units ? NULL : &leaf->units, &dflt));
2813
2814 /* store default value, if any */
2815 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2816 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2817 }
2818
Michal Vaskoc130e162021-10-19 11:30:00 +02002819 /* store leafref(s) to be resolved */
2820 LY_CHECK_RET(lysc_unres_leafref_add(ctx, leaf, type_p->pmod));
2821
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002822 /* type-specific checks */
2823 if (leaf->type->basetype == LY_TYPE_UNION) {
2824 t = ((struct lysc_type_union *)leaf->type)->types;
2825 count = LY_ARRAY_COUNT(t);
2826 } else {
2827 t = &leaf->type;
2828 count = 1;
2829 }
2830 for (u = 0; u < count; ++u) {
2831 if (t[u]->basetype == LY_TYPE_EMPTY) {
2832 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
2833 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2834 return LY_EVALID;
2835 }
2836 } else if (!in_unres && ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM))) {
2837 /* store in unres for all disabled bits/enums to be removed */
2838 LY_CHECK_RET(lysc_unres_bitenum_add(ctx, leaf));
2839 in_unres = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002840 }
2841 }
2842
2843 return LY_SUCCESS;
2844}
2845
2846/**
2847 * @brief Compile parsed leaf node information.
2848 * @param[in] ctx Compile context
2849 * @param[in] pnode Parsed leaf node.
2850 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2851 * is enriched with the leaf-specific information.
2852 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2853 */
2854static LY_ERR
2855lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2856{
2857 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2858 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002859 LY_ERR ret = LY_SUCCESS;
2860
Michal Vasko5347e3a2020-11-03 17:14:57 +01002861 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002862
2863 /* add must(s) to unres */
2864 ret = lysc_unres_must_add(ctx, node, pnode);
2865 LY_CHECK_GOTO(ret, done);
2866
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002867 if (leaf_p->units) {
2868 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2869 leaf->flags |= LYS_SET_UNITS;
2870 }
2871
2872 /* compile type */
2873 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2874 LY_CHECK_GOTO(ret, done);
2875
2876 /* store/update default value */
2877 if (leaf_p->dflt.str) {
2878 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2879 leaf->flags |= LYS_SET_DFLT;
2880 }
2881
2882 /* checks */
2883 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002884 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory leaf with a default value.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002885 return LY_EVALID;
2886 }
2887
2888done:
2889 return ret;
2890}
2891
2892/**
2893 * @brief Compile parsed leaf-list node information.
2894 * @param[in] ctx Compile context
2895 * @param[in] pnode Parsed leaf-list node.
2896 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2897 * is enriched with the leaf-list-specific information.
2898 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2899 */
2900static LY_ERR
2901lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2902{
2903 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2904 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002905 LY_ERR ret = LY_SUCCESS;
2906
Michal Vasko5347e3a2020-11-03 17:14:57 +01002907 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002908
2909 /* add must(s) to unres */
2910 ret = lysc_unres_must_add(ctx, node, pnode);
2911 LY_CHECK_GOTO(ret, done);
2912
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002913 if (llist_p->units) {
2914 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2915 llist->flags |= LYS_SET_UNITS;
2916 }
2917
2918 /* compile type */
2919 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2920 LY_CHECK_GOTO(ret, done);
2921
2922 /* store/update default values */
2923 if (llist_p->dflts) {
2924 if (ctx->pmod->version < LYS_VERSION_1_1) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002925 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list default values are allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002926 return LY_EVALID;
2927 }
2928
2929 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2930 llist->flags |= LYS_SET_DFLT;
2931 }
2932
2933 llist->min = llist_p->min;
2934 if (llist->min) {
2935 llist->flags |= LYS_MAND_TRUE;
2936 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002937 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
2938
2939 if (llist->flags & LYS_CONFIG_R) {
2940 /* state leaf-list is always ordered-by user */
2941 llist->flags &= ~LYS_ORDBY_SYSTEM;
2942 llist->flags |= LYS_ORDBY_USER;
2943 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002944
2945 /* checks */
2946 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002947 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 +02002948 return LY_EVALID;
2949 }
2950
2951 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002952 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002953 llist->min, llist->max);
2954 return LY_EVALID;
2955 }
2956
2957done:
2958 return ret;
2959}
2960
Michal Vasko0b50f6b2022-10-05 15:07:55 +02002961LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002962lysc_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 +01002963 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 +02002964{
2965 LY_ERR ret = LY_EVALID;
2966 const char *name, *prefix, *id;
2967 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002968 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002969 const char *nodeid_type;
2970 uint32_t getnext_extra_flag = 0;
2971 uint16_t current_nodetype = 0;
2972
2973 assert(nodeid);
2974 assert(target);
2975 assert(result_flag);
2976 *target = NULL;
2977 *result_flag = 0;
2978
2979 id = nodeid;
2980
2981 if (ctx_node) {
2982 /* descendant-schema-nodeid */
2983 nodeid_type = "descendant";
2984
2985 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002986 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002987 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002988 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002989 return LY_EVALID;
2990 }
2991 } else {
2992 /* absolute-schema-nodeid */
2993 nodeid_type = "absolute";
2994
2995 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002996 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002997 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002998 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002999 return LY_EVALID;
3000 }
3001 ++id;
3002 }
3003
3004 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
3005 if (prefix) {
3006 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
3007 if (!mod) {
3008 /* module must always be found */
3009 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01003010 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003011 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003012 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003013 return LY_ENOTFOUND;
3014 }
3015 } else {
3016 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003017 case LY_VALUE_SCHEMA:
3018 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003019 /* use the current module */
Michal Vasko56d8da82021-12-16 15:41:30 +01003020 mod = ctx->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003021 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003022 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02003023 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003024 if (!ctx_node) {
3025 LOGINT_RET(ctx->ctx);
3026 }
3027 /* inherit the module of the previous context node */
3028 mod = ctx_node->module;
3029 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02003030 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02003031 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01003032 case LY_VALUE_STR_NS:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003033 /* not really defined */
3034 LOGINT_RET(ctx->ctx);
3035 }
3036 }
3037
3038 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
3039 /* move through input/output manually */
3040 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01003041 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3042 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003043 return LY_ENOTFOUND;
3044 }
3045 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003046 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003047 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003048 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003049 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
3050 } else {
3051 /* only input or output is valid */
3052 ctx_node = NULL;
3053 }
Michal Vasko0b50f6b2022-10-05 15:07:55 +02003054 } else if (ctx->ext && !ctx_node) {
3055 /* top-level extension nodes */
3056 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 +02003057 } else {
3058 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003059 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003060 getnext_extra_flag = 0;
3061 }
3062 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01003063 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3064 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003065 return LY_ENOTFOUND;
3066 }
3067 current_nodetype = ctx_node->nodetype;
3068
3069 if (current_nodetype == LYS_NOTIF) {
3070 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
3071 } else if (current_nodetype == LYS_INPUT) {
3072 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
3073 } else if (current_nodetype == LYS_OUTPUT) {
3074 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
3075 }
3076
3077 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
3078 break;
3079 }
3080 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003081 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003082 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01003083 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003084 return LY_EVALID;
3085 }
3086 ++id;
3087 }
3088
3089 if (ret == LY_SUCCESS) {
3090 *target = ctx_node;
3091 if (nodetype && !(current_nodetype & nodetype)) {
3092 return LY_EDENIED;
3093 }
3094 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003095 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003096 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01003097 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003098 }
3099
3100 return ret;
3101}
3102
3103/**
3104 * @brief Compile information about list's uniques.
3105 * @param[in] ctx Compile context.
3106 * @param[in] uniques Sized array list of unique statements.
3107 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3108 * @return LY_ERR value.
3109 */
3110static LY_ERR
3111lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
3112{
3113 LY_ERR ret = LY_SUCCESS;
3114 struct lysc_node_leaf **key, ***unique;
3115 struct lysc_node *parent;
3116 const char *keystr, *delim;
3117 size_t len;
3118 LY_ARRAY_COUNT_TYPE v;
3119 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
3120 uint16_t flags;
3121
3122 LY_ARRAY_FOR(uniques, v) {
3123 config = -1;
3124 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3125 keystr = uniques[v].str;
3126 while (keystr) {
3127 delim = strpbrk(keystr, " \t\n");
3128 if (delim) {
3129 len = delim - keystr;
3130 while (isspace(*delim)) {
3131 ++delim;
3132 }
3133 } else {
3134 len = strlen(keystr);
3135 }
3136
3137 /* unique node must be present */
3138 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko56d8da82021-12-16 15:41:30 +01003139 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, LY_VALUE_SCHEMA, (void *)uniques[v].mod,
3140 LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003141 if (ret != LY_SUCCESS) {
3142 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003143 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003144 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01003145 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003146 }
3147 return LY_EVALID;
3148 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003149 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003150 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003151 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003152 return LY_EVALID;
3153 }
3154
3155 /* all referenced leafs must be of the same config type */
3156 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3157 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003158 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003159 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3160 return LY_EVALID;
3161 } else if ((*key)->flags & LYS_CONFIG_W) {
3162 config = 1;
3163 } else { /* LYS_CONFIG_R */
3164 config = 0;
3165 }
3166
3167 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3168 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3169 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003170 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003171 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3172 return LY_EVALID;
3173 }
3174 }
3175
3176 /* check status */
Michal Vaskoc130e162021-10-19 11:30:00 +02003177 LY_CHECK_RET(lysc_check_status(ctx, list->flags, uniques[v].mod->mod, list->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003178 (*key)->flags, (*key)->module, (*key)->name));
3179
3180 /* mark leaf as unique */
3181 (*key)->flags |= LYS_UNIQUE;
3182
3183 /* next unique value in line */
3184 keystr = delim;
3185 }
3186 /* next unique definition */
3187 }
3188
3189 return LY_SUCCESS;
3190}
3191
3192/**
3193 * @brief Compile parsed list node information.
3194 * @param[in] ctx Compile context
3195 * @param[in] pnode Parsed list node.
3196 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3197 * is enriched with the list-specific information.
3198 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3199 */
3200static LY_ERR
3201lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3202{
3203 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3204 struct lysc_node_list *list = (struct lysc_node_list *)node;
3205 struct lysp_node *child_p;
Michal Vasko2d6507a2022-03-16 09:40:52 +01003206 struct lysc_node *parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003207 struct lysc_node_leaf *key, *prev_key = NULL;
3208 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003209 const char *keystr, *delim;
3210 LY_ERR ret = LY_SUCCESS;
3211
3212 list->min = list_p->min;
3213 if (list->min) {
3214 list->flags |= LYS_MAND_TRUE;
3215 }
3216 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3217
3218 LY_LIST_FOR(list_p->child, child_p) {
3219 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3220 }
3221
Michal Vasko5347e3a2020-11-03 17:14:57 +01003222 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003223
3224 /* add must(s) to unres */
3225 ret = lysc_unres_must_add(ctx, node, pnode);
3226 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003227
3228 /* keys */
Michal Vasko2d6507a2022-03-16 09:40:52 +01003229 if (list->flags & LYS_CONFIG_W) {
3230 parent = node;
3231 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
3232 /* compiling individual grouping, we can check this only if there is an explicit config set */
3233 while (parent) {
3234 if (parent->flags & LYS_SET_CONFIG) {
3235 break;
3236 }
3237 parent = parent->parent;
3238 }
3239 }
3240
3241 if (parent && (!list_p->key || !list_p->key[0])) {
3242 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3243 return LY_EVALID;
3244 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003245 }
3246
3247 /* find all the keys (must be direct children) */
3248 keystr = list_p->key;
3249 if (!keystr) {
3250 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003251 list->flags &= ~LYS_ORDBY_SYSTEM;
3252 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003253 }
3254 while (keystr) {
3255 delim = strpbrk(keystr, " \t\n");
3256 if (delim) {
3257 len = delim - keystr;
3258 while (isspace(*delim)) {
3259 ++delim;
3260 }
3261 } else {
3262 len = strlen(keystr);
3263 }
3264
3265 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003266 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3267 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003268 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003269 return LY_EVALID;
3270 }
3271 /* keys must be unique */
3272 if (key->flags & LYS_KEY) {
3273 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003274 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003275 return LY_EVALID;
3276 }
3277
Radek Krejcia6016992021-03-03 10:13:41 +01003278 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003279 /* key must have the same config flag as the list itself */
3280 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003281 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of a configuration list must not be a state leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003282 return LY_EVALID;
3283 }
3284 if (ctx->pmod->version < LYS_VERSION_1_1) {
3285 /* YANG 1.0 denies key to be of empty type */
3286 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003287 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003288 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3289 return LY_EVALID;
3290 }
3291 } else {
3292 /* when and if-feature are illegal on list keys */
3293 if (key->when) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003294 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List's key must not have any \"when\" statement.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003295 return LY_EVALID;
3296 }
Michal Vasko93b4ab92022-05-13 12:29:59 +02003297 /* unable to check if-features but compilation would fail if disabled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003298 }
3299
3300 /* check status */
Michal Vaskoc636ea42022-09-16 10:20:31 +02003301 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 +02003302
3303 /* ignore default values of the key */
3304 if (key->dflt) {
3305 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
Michal Vaskoc636ea42022-09-16 10:20:31 +02003306 lysc_type_free(&ctx->free_ctx, (struct lysc_type *)key->dflt->realtype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003307 free(key->dflt);
3308 key->dflt = NULL;
3309 }
3310 /* mark leaf as key */
3311 key->flags |= LYS_KEY;
3312
3313 /* move it to the correct position */
3314 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3315 /* fix links in closest previous siblings of the key */
3316 if (key->next) {
3317 key->next->prev = key->prev;
3318 } else {
3319 /* last child */
3320 list->child->prev = key->prev;
3321 }
3322 if (key->prev->next) {
3323 key->prev->next = key->next;
3324 }
3325 /* fix links in the key */
3326 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003327 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003328 key->next = prev_key->next;
3329 } else {
3330 key->prev = list->child->prev;
3331 key->next = list->child;
3332 }
3333 /* fix links in closes future siblings of the key */
3334 if (prev_key) {
3335 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003336 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003337 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003338 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003339 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003340 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003341 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003342 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003343 }
3344 /* fix links in parent */
3345 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003346 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003347 }
3348 }
3349
3350 /* next key value */
3351 prev_key = key;
3352 keystr = delim;
3353 lysc_update_path(ctx, NULL, NULL);
3354 }
3355
Michal Vasko95f736c2022-06-08 12:03:31 +02003356 /* connect any augments */
3357 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3358
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003359 /* uniques */
3360 if (list_p->uniques) {
3361 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3362 }
3363
Radek Krejci2a9fc652021-01-22 17:44:34 +01003364 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3365 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3366 LY_CHECK_GOTO(ret, done);
3367 }
3368 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3369 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3370 LY_CHECK_GOTO(ret, done);
3371 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003372
3373 /* checks */
3374 if (list->min > list->max) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003375 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 +02003376 return LY_EVALID;
3377 }
3378
3379done:
3380 return ret;
3381}
3382
3383/**
3384 * @brief Do some checks and set the default choice's case.
3385 *
3386 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3387 *
3388 * @param[in] ctx Compile context.
3389 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3390 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3391 * @return LY_ERR value.
3392 */
3393static LY_ERR
3394lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3395{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003396 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003397 const struct lys_module *mod;
3398 const char *prefix = NULL, *name;
3399 size_t prefix_len = 0;
3400
3401 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3402 name = strchr(dflt->str, ':');
3403 if (name) {
3404 prefix = dflt->str;
3405 prefix_len = name - prefix;
3406 ++name;
3407 } else {
3408 name = dflt->str;
3409 }
3410 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003411 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003412 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003413 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003414 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003415 return LY_EVALID;
3416 }
3417 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003418 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003419 }
3420
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003421 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 +02003422 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003423 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003424 "Default case \"%s\" not found.", dflt->str);
3425 return LY_EVALID;
3426 }
3427
3428 /* no mandatory nodes directly under the default case */
3429 LY_LIST_FOR(ch->dflt->child, iter) {
3430 if (iter->parent != (struct lysc_node *)ch->dflt) {
3431 break;
3432 }
3433 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003434 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003435 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3436 return LY_EVALID;
3437 }
3438 }
3439
3440 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003441 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003442 return LY_EVALID;
3443 }
3444
3445 ch->dflt->flags |= LYS_SET_DFLT;
3446 return LY_SUCCESS;
3447}
3448
3449LY_ERR
3450lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3451 struct ly_set *child_set)
3452{
3453 LY_ERR ret = LY_SUCCESS;
3454 struct lysp_node *child_p_next = child_p->next;
3455 struct lysp_node_case *cs_p;
Michal Vasko4eb49d52022-02-17 15:05:00 +01003456 struct lysc_node_case *cs_c;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003457
3458 if (child_p->nodetype == LYS_CASE) {
3459 /* standard case under choice */
3460 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3461 } else {
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003462 /* we need the implicit case first, so create a fake parsed (shorthand) case */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003463 cs_p = calloc(1, sizeof *cs_p);
3464 cs_p->nodetype = LYS_CASE;
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003465 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003466 cs_p->child = child_p;
3467
3468 /* make the child the only case child */
3469 child_p->next = NULL;
3470
3471 /* compile it normally */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003472 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 +02003473
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003474 if (((struct lysc_node_choice *)node)->cases) {
Michal Vasko4eb49d52022-02-17 15:05:00 +01003475 /* find our case node */
3476 cs_c = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases;
3477 while (cs_c->name != cs_p->name) {
3478 cs_c = (struct lysc_node_case *)cs_c->next;
3479 assert(cs_c);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003480 }
aPiecekaa320c92021-05-21 07:34:24 +02003481
Michal Vasko4eb49d52022-02-17 15:05:00 +01003482 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
3483 /* compiled case node cannot point to his corresponding parsed node
3484 * because it exists temporarily so it must be set to NULL
3485 */
3486 assert(cs_c->priv == cs_p);
3487 cs_c->priv = NULL;
3488 }
3489
3490 /* status is copied from his child and not from his parent as usual. */
3491 if (cs_c->child) {
3492 cs_c->flags &= ~LYS_STATUS_MASK;
3493 cs_c->flags |= (LYS_STATUS_MASK & cs_c->child->flags);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003494 }
3495 } /* else it was removed by a deviation */
aPiecekaa320c92021-05-21 07:34:24 +02003496
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003497revert_sh_case:
3498 /* free the parsed shorthand case and correct pointers back */
aPiecekaa320c92021-05-21 07:34:24 +02003499 cs_p->child = NULL;
Michal Vaskoc636ea42022-09-16 10:20:31 +02003500 lysp_node_free(&ctx->free_ctx, (struct lysp_node *)cs_p);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003501 child_p->next = child_p_next;
3502 }
3503
3504 return ret;
3505}
3506
3507/**
3508 * @brief Compile parsed choice node information.
3509 *
3510 * @param[in] ctx Compile context
3511 * @param[in] pnode Parsed choice node.
3512 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3513 * is enriched with the choice-specific information.
3514 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3515 */
3516static LY_ERR
3517lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3518{
3519 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3520 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3521 struct lysp_node *child_p;
3522 LY_ERR ret = LY_SUCCESS;
3523
3524 assert(node->nodetype == LYS_CHOICE);
3525
3526 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003527 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, child_p, node, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003528 }
3529
Michal Vasko95f736c2022-06-08 12:03:31 +02003530 /* connect any augments */
3531 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3532
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003533 /* default branch */
3534 if (ch_p->dflt.str) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003535 LY_CHECK_GOTO(ret = lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003536 }
3537
Michal Vasko95f736c2022-06-08 12:03:31 +02003538done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003539 return ret;
3540}
3541
3542/**
3543 * @brief Compile parsed anydata or anyxml node information.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003544 *
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003545 * @param[in] ctx Compile context
3546 * @param[in] pnode Parsed anydata or anyxml node.
3547 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3548 * is enriched with the any-specific information.
3549 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3550 */
3551static LY_ERR
3552lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3553{
3554 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3555 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003556 LY_ERR ret = LY_SUCCESS;
3557
Michal Vasko5347e3a2020-11-03 17:14:57 +01003558 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02003559
3560 /* add must(s) to unres */
3561 ret = lysc_unres_must_add(ctx, node, pnode);
3562 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003563
Radek Krejci91531e12021-02-08 19:57:54 +01003564 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003565 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko193dacd2022-10-13 08:43:05 +02003566 lyplg_ext_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003567 }
3568done:
3569 return ret;
3570}
3571
3572/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003573 * @brief Prepare the case structure in choice node for the new data node.
3574 *
3575 * 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
3576 * created in the choice when the first child was processed.
3577 *
3578 * @param[in] ctx Compile context.
3579 * @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,
3580 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3581 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3582 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3583 * @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,
3584 * it is linked from the case structure only in case it is its first child.
3585 */
3586static LY_ERR
3587lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3588{
Michal Vasko95f736c2022-06-08 12:03:31 +02003589 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003590 struct lysp_node *child_p;
3591 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3592
3593 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3594 /* we have to add an implicit case node into the parent choice */
3595 } else if (pnode->nodetype == LYS_CASE) {
3596 /* explicit parent case */
3597 LY_LIST_FOR(cs_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003598 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003599 }
3600 } else {
3601 LOGINT_RET(ctx->ctx);
3602 }
3603
Michal Vasko95f736c2022-06-08 12:03:31 +02003604 /* connect any augments */
3605 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3606
3607done:
3608 return ret;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003609}
3610
3611void
3612lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3613{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003614 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003615
3616 if (add) { /* set flag */
3617 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3618 parent = parent->parent) {
3619 parent->flags |= LYS_MAND_TRUE;
3620 }
3621 } else { /* unset flag */
3622 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003623 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003624 if (iter->flags & LYS_MAND_TRUE) {
3625 /* there is another mandatory node */
3626 return;
3627 }
3628 }
3629 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3630 parent->flags &= ~LYS_MAND_TRUE;
3631 }
3632 }
3633}
3634
3635/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003636 * @brief Get the grouping with the specified name from given groupings sized array.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003637 *
3638 * @param[in] node Linked list of nodes with groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003639 * @param[in] name Name of the grouping to find,
3640 * @return NULL when there is no grouping with the specified name
3641 * @return Pointer to the grouping of the specified @p name.
3642 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003643static struct lysp_node_grp *
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003644match_grouping(const struct lysp_node_grp *node, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003645{
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003646 LY_LIST_FOR(node, node) {
3647 if ((node->nodetype == LYS_GROUPING) && !strcmp(node->name, name)) {
3648 return (struct lysp_node_grp *)node;
Radek Krejciabd33a42021-01-20 17:18:59 +01003649 }
3650 }
3651
3652 return NULL;
3653}
3654
3655/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003656 * @brief Find grouping for a uses.
3657 *
3658 * @param[in] ctx Compile context.
3659 * @param[in] uses_p Parsed uses node.
3660 * @param[out] gpr_p Found grouping on success.
3661 * @param[out] grp_pmod Module of @p grp_p on success.
3662 * @return LY_ERR value.
3663 */
3664static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003665lys_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 +02003666 struct lysp_module **grp_pmod)
3667{
3668 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003669 struct lysp_node_grp *grp;
Michal Vasko193dacd2022-10-13 08:43:05 +02003670 const struct lysp_node_grp *ext_grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003671 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003672 const char *id, *name, *prefix, *local_pref;
3673 size_t prefix_len, name_len;
3674 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003675 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003676
3677 *grp_p = NULL;
3678 *grp_pmod = NULL;
3679
3680 /* search for the grouping definition */
3681 id = uses_p->name;
3682 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3683 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3684 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3685 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003686 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003687 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003688 if ((grp = match_grouping(lysp_node_groupings(pnode), name))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003689 found = ctx->pmod;
3690 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003691 }
3692 }
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003693
3694 /* if in an extension, search possible groupings in it */
3695 if (ctx->ext) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003696 lyplg_ext_parsed_get_storage(ctx->ext, LY_STMT_GROUPING, (const void **)&ext_grp);
3697 if ((grp = match_grouping(ext_grp, name))) {
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003698 found = ctx->pmod;
3699 }
3700 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003701 } else {
3702 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003703 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003704 if (!mod) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003705 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid prefix used for grouping reference.", uses_p->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003706 return LY_EVALID;
3707 }
3708 pmod = mod->parsed;
3709 }
3710
3711 if (!found) {
3712 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003713 if ((grp = match_grouping(pmod->groupings, name))) {
3714 found = pmod;
3715 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003716 /* ... and all the submodules */
3717 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003718 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3719 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003720 break;
3721 }
3722 }
3723 }
3724 }
3725 if (!found) {
Michal Vasko193dacd2022-10-13 08:43:05 +02003726 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003727 return LY_EVALID;
3728 }
3729
Michal Vasko7c565922021-06-10 14:58:27 +02003730 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003731 /* remember that the grouping is instantiated to avoid its standalone validation */
3732 grp->flags |= LYS_USED_GRP;
3733 }
3734
3735 *grp_p = grp;
3736 *grp_pmod = found;
3737 return LY_SUCCESS;
3738}
3739
3740/**
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003741 * @brief Compile uses grouping children.
3742 *
3743 * @param[in] ctx Compile context.
3744 * @param[in] uses_p Parsed uses.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003745 * @param[in] parent_status Parent status flags to inherit.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003746 * @param[in] child First grouping child to compile.
3747 * @param[in] grp_mod Grouping parsed module.
3748 * @param[in] parent Uses compiled parent, may be NULL if top-level.
3749 * @param[in,out] child_set Set of all compiled child nodes.
3750 * @param[in] child_unres_disabled Whether the children are to be put into unres disabled set or not.
3751 * @return LY_SUCCESS on success.
3752 * @return LY_EVALID on failure.
3753 */
3754static LY_ERR
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003755lys_compile_uses_children(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, uint16_t parent_status,
3756 struct lysp_node *child, struct lysp_module *grp_mod, struct lysc_node *parent, struct ly_set *child_set,
3757 ly_bool child_unres_disabled)
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003758{
3759 LY_ERR rc = LY_SUCCESS;
3760 struct lysp_module *mod_old = ctx->pmod;
3761 uint32_t child_i, opt_prev = ctx->compile_opts;
3762 ly_bool enabled;
3763 struct lysp_node *pnode;
3764 struct lysc_node *node;
3765 struct lysc_when *when_shared = NULL;
3766
3767 assert(child_set);
3768
Michal Vasko5940c302022-07-14 13:54:38 +02003769 child_i = child_set->count;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003770 LY_LIST_FOR(child, pnode) {
3771 /* compile the nodes with their parsed (grouping) module */
3772 ctx->pmod = grp_mod;
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003773 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, parent, &parent_status, child_set), cleanup);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003774
3775 /* eval if-features again for the rest of this node processing */
3776 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
3777 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3778 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3779 }
3780
3781 /* restore the parsed module */
3782 ctx->pmod = mod_old;
3783
3784 /* since the uses node is not present in the compiled tree, we need to pass some of its
3785 * statements to all its children */
3786 while (child_i < child_set->count) {
3787 node = child_set->snodes[child_i];
3788
3789 if (uses_p->when) {
3790 /* pass uses when to all the children */
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003791 rc = lys_compile_when(ctx, uses_p->when, parent_status, parent, lysc_data_node(parent), node, &when_shared);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003792 LY_CHECK_GOTO(rc, cleanup);
3793 }
3794
3795 if (child_unres_disabled) {
3796 /* child is disabled by the uses if-features */
3797 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
3798 }
3799
3800 /* child processed */
3801 ++child_i;
3802 }
3803
3804 /* next iter */
3805 ctx->compile_opts = opt_prev;
3806 }
3807
3808cleanup:
3809 ctx->compile_opts = opt_prev;
3810 return rc;
3811}
3812
3813/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003814 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3815 * If present, also apply uses's modificators.
3816 *
3817 * @param[in] ctx Compile context
3818 * @param[in] uses_p Parsed uses schema node.
3819 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3820 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3821 * the compile context.
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003822 * @param[in] inherited_status Explicitly inherited status (from uses/extension instance), if any.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003823 * @param[in] child_set Optional set of all the compiled children.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003824 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3825 */
3826static LY_ERR
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003827lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent,
3828 const uint16_t *inherited_status, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003829{
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003830 LY_ERR rc = LY_SUCCESS;
3831 ly_bool enabled, child_unres_disabled = 0;
3832 uint32_t i, grp_stack_count, opt_prev = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003833 struct lysp_node_grp *grp = NULL;
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003834 uint16_t uses_flags, parent_flags;
3835 const char *parent_name;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003836 struct lysp_module *grp_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003837 struct ly_set uses_child_set = {0};
3838
3839 /* find the referenced grouping */
3840 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3841
3842 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3843 grp_stack_count = ctx->groupings.count;
3844 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3845 if (grp_stack_count == ctx->groupings.count) {
3846 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003847 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003848 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3849 return LY_EVALID;
3850 }
3851
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003852 /* nodetype checks */
3853 if (grp->actions && (parent && !lysc_node_actions_p(parent))) {
3854 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3855 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3856 parent->name, lys_nodetype2str(parent->nodetype));
3857 rc = LY_EVALID;
3858 goto cleanup;
3859 }
3860 if (grp->notifs && (parent && !lysc_node_notifs_p(parent))) {
3861 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3862 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3863 parent->name, lys_nodetype2str(parent->nodetype));
3864 rc = LY_EVALID;
3865 goto cleanup;
3866 }
3867
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003868 /* check status */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003869 rc = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3870 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003871
3872 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003873 rc = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3874 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003875
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003876 /* compile special uses status flags */
3877 uses_flags = uses_p->flags;
Michal Vaskoedb0fa52022-10-04 10:36:00 +02003878 parent_flags = inherited_status ? *inherited_status : (parent ? parent->flags : 0);
3879 parent_name = inherited_status ? "<schema-only-node>" : (parent ? parent->name : NULL);
3880 rc = lys_compile_status(ctx, &uses_flags, "<uses>", parent_flags, parent_name, inherited_status ? 1 : 0);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003881 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003882
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003883 /* uses if-features */
3884 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, uses_p->iffeatures, &enabled), cleanup);
3885 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3886 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3887 child_unres_disabled = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003888 }
3889
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003890 /* uses grouping children */
3891 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, grp->child, grp_mod, parent,
3892 child_set ? child_set : &uses_child_set, child_unres_disabled);
3893 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003894
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003895 /* uses grouping RPCs/actions */
3896 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->actions, grp_mod, parent,
3897 child_set ? child_set : &uses_child_set, child_unres_disabled);
3898 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003899
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003900 /* uses grouping notifications */
3901 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->notifs, grp_mod, parent,
3902 child_set ? child_set : &uses_child_set, child_unres_disabled);
3903 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003904
3905 /* check that all augments were applied */
3906 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003907 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3908 /* augment of some parent uses, irrelevant now */
3909 continue;
3910 }
3911
3912 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003913 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003914 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003915 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003916 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003917
3918 /* check that all refines were applied */
3919 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003920 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003921 /* refine of some parent uses, irrelevant now */
Michal Vaskod8655722021-01-12 15:20:36 +01003922 continue;
3923 }
3924
3925 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003926 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003927 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003928 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003929 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003930
Michal Vasko193dacd2022-10-13 08:43:05 +02003931 /* compile uses and grouping extensions into the parent */
3932 COMPILE_EXTS_GOTO(ctx, uses_p->exts, parent->exts, parent, rc, cleanup);
3933 COMPILE_EXTS_GOTO(ctx, grp->exts, parent->exts, parent, rc, cleanup);
3934
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003935cleanup:
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003936 /* restore previous context */
3937 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003938
3939 /* remove the grouping from the stack for circular groupings dependency check */
3940 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3941 assert(ctx->groupings.count == grp_stack_count);
3942
3943 ly_set_erase(&uses_child_set, NULL);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003944 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003945}
3946
3947static int
3948lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3949{
3950 struct lysp_node *iter;
3951 int len = 0;
3952
3953 *path = NULL;
3954 for (iter = node; iter && len >= 0; iter = iter->parent) {
3955 char *s = *path;
3956 char *id;
3957
3958 switch (iter->nodetype) {
3959 case LYS_USES:
3960 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3961 break;
3962 case LYS_GROUPING:
3963 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3964 break;
3965 case LYS_AUGMENT:
3966 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3967 break;
3968 default:
3969 id = strdup(iter->name);
3970 break;
3971 }
3972
3973 if (!iter->parent) {
3974 /* print prefix */
3975 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3976 } else {
3977 /* prefix is the same as in parent */
3978 len = asprintf(path, "/%s%s", id, s ? s : "");
3979 }
3980 free(s);
3981 free(id);
3982 }
3983
3984 if (len < 0) {
3985 free(*path);
3986 *path = NULL;
3987 } else if (len == 0) {
3988 *path = strdup("/");
3989 len = 1;
3990 }
3991 return len;
3992}
3993
3994LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003995lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003996{
3997 LY_ERR ret;
3998 char *path;
3999 int len;
Michal Vaskoecdc2222022-01-24 08:45:44 +01004000 uint16_t cont_flags;
4001
4002 cont_flags = pnode ? pnode->flags & LYS_FLAGS_COMPILED_MASK : 0;
4003 if (!(cont_flags & LYS_CONFIG_MASK)) {
4004 /* default config */
4005 cont_flags |= LYS_CONFIG_W;
4006 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004007
Michal Vasko8254d852022-04-25 10:05:59 +02004008 /* use grouping status to avoid errors */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004009 struct lysp_node_uses fake_uses = {
4010 .parent = pnode,
4011 .nodetype = LYS_USES,
Michal Vasko8254d852022-04-25 10:05:59 +02004012 .flags = grp->flags & LYS_STATUS_MASK, .next = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004013 .name = grp->name,
4014 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4015 .refines = NULL, .augments = NULL
4016 };
4017 struct lysc_node_container fake_container = {
4018 .nodetype = LYS_CONTAINER,
Michal Vaskoecdc2222022-01-24 08:45:44 +01004019 .flags = cont_flags,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004020 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02004021 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01004022 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004023 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004024 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004025 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4026 };
4027
4028 if (grp->parent) {
4029 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4030 }
4031
4032 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
4033 if (len < 0) {
4034 LOGMEM(ctx->ctx);
4035 return LY_EMEM;
4036 }
4037 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
4038 ctx->path_len = (uint32_t)len;
4039 free(path);
4040
4041 lysc_update_path(ctx, NULL, "{grouping}");
4042 lysc_update_path(ctx, NULL, grp->name);
Michal Vaskoedb0fa52022-10-04 10:36:00 +02004043 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004044 lysc_update_path(ctx, NULL, NULL);
4045 lysc_update_path(ctx, NULL, NULL);
4046
4047 ctx->path_len = 1;
4048 ctx->path[1] = '\0';
4049
4050 /* cleanup */
Michal Vaskoc636ea42022-09-16 10:20:31 +02004051 lysc_node_container_free(&ctx->free_ctx, &fake_container);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004052
4053 return ret;
4054}
4055
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004056LY_ERR
Michal Vaskoedb0fa52022-10-04 10:36:00 +02004057lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, const uint16_t *inherited_status,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004058 struct ly_set *child_set)
4059{
4060 LY_ERR ret = LY_SUCCESS;
4061 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02004062 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004063
4064 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
4065
4066 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01004067 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004068 } else {
4069 lysc_update_path(ctx, NULL, "{uses}");
4070 lysc_update_path(ctx, NULL, pnode->name);
4071 }
4072
4073 switch (pnode->nodetype) {
4074 case LYS_CONTAINER:
4075 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
4076 node_compile_spec = lys_compile_node_container;
4077 break;
4078 case LYS_LEAF:
4079 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
4080 node_compile_spec = lys_compile_node_leaf;
4081 break;
4082 case LYS_LIST:
4083 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
4084 node_compile_spec = lys_compile_node_list;
4085 break;
4086 case LYS_LEAFLIST:
4087 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
4088 node_compile_spec = lys_compile_node_leaflist;
4089 break;
4090 case LYS_CHOICE:
4091 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
4092 node_compile_spec = lys_compile_node_choice;
4093 break;
4094 case LYS_CASE:
4095 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
4096 node_compile_spec = lys_compile_node_case;
4097 break;
4098 case LYS_ANYXML:
4099 case LYS_ANYDATA:
4100 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
4101 node_compile_spec = lys_compile_node_any;
4102 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004103 case LYS_RPC:
4104 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02004105 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004106 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4107 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004108 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004109 return LY_EVALID;
4110 }
4111 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
4112 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02004113 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004114 break;
4115 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02004116 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004117 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4118 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004119 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004120 return LY_EVALID;
4121 }
4122 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
4123 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02004124 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004125 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004126 case LYS_USES:
Michal Vaskoedb0fa52022-10-04 10:36:00 +02004127 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, inherited_status, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004128 lysc_update_path(ctx, NULL, NULL);
4129 lysc_update_path(ctx, NULL, NULL);
4130 return ret;
4131 default:
4132 LOGINT(ctx->ctx);
4133 return LY_EINT;
4134 }
4135 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4136
Michal Vaskoedb0fa52022-10-04 10:36:00 +02004137 ret = lys_compile_node_(ctx, pnode, parent, inherited_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004138
Michal Vasko7c565922021-06-10 14:58:27 +02004139 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004140 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004141 return ret;
4142}