blob: 65072c51e5091b066a3c1164cfc227ec7930be34 [file] [log] [blame]
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001/**
2 * @file schema_compile_node.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema compilation of common nodes.
5 *
6 * Copyright (c) 2015 - 2020 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
Christian Hopps32874e12021-05-01 09:43:54 -040015#define _GNU_SOURCE /* asprintf, strdup */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020016
17#include "schema_compile_node.h"
18
19#include <assert.h>
20#include <ctype.h>
21#include <stddef.h>
22#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include "common.h"
28#include "compat.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020029#include "dict.h"
30#include "log.h"
Radek Krejci0aa1f702021-04-01 16:16:19 +020031#include "plugins.h"
Radek Krejci77114102021-03-10 15:21:57 +010032#include "plugins_exts_compile.h"
Radek Krejci04699f02021-03-22 21:50:29 +010033#include "plugins_internal.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020034#include "plugins_types.h"
35#include "schema_compile.h"
36#include "schema_compile_amend.h"
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010037#include "schema_features.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020038#include "set.h"
39#include "tree.h"
40#include "tree_data.h"
Radek Krejci859a15a2021-03-05 20:56:59 +010041#include "tree_edit.h"
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020042#include "tree_schema.h"
43#include "tree_schema_internal.h"
44#include "xpath.h"
45
46static struct lysc_ext_instance *
47lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
48{
49 /* TODO - extensions, increase refcount */
50 (void) ctx;
51 (void) orig;
52 return NULL;
53}
54
55/**
Michal Vaskoc130e162021-10-19 11:30:00 +020056 * @brief Add a node with must(s) to unres.
57 *
58 * @param[in] ctx Compile context.
59 * @param[in] node Compiled node with must(s).
60 * @param[in] pnode Parsed ndoe with must(s).
61 * @return LY_ERR value.
62 */
63static LY_ERR
64lysc_unres_must_add(struct lysc_ctx *ctx, struct lysc_node *node, struct lysp_node *pnode)
65{
66 struct lysc_unres_must *m = NULL;
67 LY_ARRAY_COUNT_TYPE u;
68 struct lysc_must *musts;
69 struct lysp_restr *pmusts;
70 LY_ERR ret;
71
72 /* do not check must(s) in a grouping */
73 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
74 return LY_SUCCESS;
75 }
76
77 musts = lysc_node_musts(node);
78 pmusts = lysp_node_musts(pnode);
79 assert(LY_ARRAY_COUNT(musts) == LY_ARRAY_COUNT(pmusts));
80
81 if (!musts) {
82 /* no must */
83 return LY_SUCCESS;
84 }
85
86 /* add new unres must */
87 m = calloc(1, sizeof *m);
88 LY_CHECK_ERR_GOTO(!m, ret = LY_EMEM, error);
89 m->node = node;
90
91 /* add must local modules */
92 LY_ARRAY_CREATE_GOTO(ctx->ctx, m->local_mods, LY_ARRAY_COUNT(pmusts), ret, error);
93 LY_ARRAY_FOR(pmusts, u) {
94 m->local_mods[u] = pmusts[u].arg.mod;
95 LY_ARRAY_INCREMENT(m->local_mods);
96 }
97
98 LY_CHECK_ERR_GOTO(ly_set_add(&ctx->unres->musts, m, 1, NULL), ret = LY_EMEM, error);
99
100 return LY_SUCCESS;
101
102error:
103 if (m) {
104 LY_ARRAY_FREE(m->local_mods);
105 free(m);
106 }
107 LOGMEM(ctx->ctx);
108 return ret;
109}
110
111static LY_ERR
112lysc_unres_leafref_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, const struct lysp_module *local_mod)
113{
114 struct lysc_unres_leafref *l = NULL;
115 struct ly_set *leafrefs_set;
116 LY_ARRAY_COUNT_TYPE u;
117 int is_lref = 0;
118
119 if (ctx->compile_opts & LYS_COMPILE_GROUPING) {
120 /* do not check leafrefs in groupings */
121 return LY_SUCCESS;
122 }
123
124 /* use special set for disabled leafrefs */
125 leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs;
126
127 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
128 /* leafref */
129 is_lref = 1;
130 } else if (leaf->type->basetype == LY_TYPE_UNION) {
131 /* union with leafrefs */
132 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
133 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
134 is_lref = 1;
135 break;
136 }
137 }
138 }
139
140 if (is_lref) {
141 /* add new unresolved leafref node */
142 l = calloc(1, sizeof *l);
143 LY_CHECK_ERR_RET(!l, LOGMEM(ctx->ctx), LY_EMEM);
144
145 l->node = &leaf->node;
146 l->local_mod = local_mod;
147
148 LY_CHECK_ERR_RET(ly_set_add(leafrefs_set, l, 1, NULL), free(l); LOGMEM(ctx->ctx), LY_EMEM);
149 }
150
151 return LY_SUCCESS;
152}
153
154/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200155 * @brief Add/replace a leaf default value in unres.
156 * Can also be used for a single leaf-list default value.
157 *
158 * @param[in] ctx Compile context.
159 * @param[in] leaf Leaf with the default value.
160 * @param[in] dflt Default value to use.
161 * @return LY_ERR value.
162 */
163static LY_ERR
164lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
165{
166 struct lysc_unres_dflt *r = NULL;
167 uint32_t i;
168
Michal Vasko7c565922021-06-10 14:58:27 +0200169 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100170 return LY_SUCCESS;
171 }
172
Michal Vasko405cc9e2020-12-01 12:01:27 +0100173 for (i = 0; i < ctx->unres->dflts.count; ++i) {
174 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200175 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100176 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200177 lysp_qname_free(ctx->ctx, r->dflt);
178 free(r->dflt);
179 break;
180 }
181 }
182 if (!r) {
183 /* add new unres item */
184 r = calloc(1, sizeof *r);
185 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
186 r->leaf = leaf;
187
Michal Vasko405cc9e2020-12-01 12:01:27 +0100188 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200189 }
190
191 r->dflt = malloc(sizeof *r->dflt);
192 LY_CHECK_GOTO(!r->dflt, error);
193 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
194
195 return LY_SUCCESS;
196
197error:
198 free(r->dflt);
199 LOGMEM(ctx->ctx);
200 return LY_EMEM;
201}
202
203/**
204 * @brief Add/replace a leaf-list default value(s) in unres.
205 *
206 * @param[in] ctx Compile context.
207 * @param[in] llist Leaf-list with the default value.
208 * @param[in] dflts Sized array of the default values.
209 * @return LY_ERR value.
210 */
211static LY_ERR
212lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
213{
214 struct lysc_unres_dflt *r = NULL;
215 uint32_t i;
216
Michal Vasko7c565922021-06-10 14:58:27 +0200217 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100218 return LY_SUCCESS;
219 }
220
Michal Vasko405cc9e2020-12-01 12:01:27 +0100221 for (i = 0; i < ctx->unres->dflts.count; ++i) {
222 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200223 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100224 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200225 lysp_qname_free(ctx->ctx, r->dflt);
226 free(r->dflt);
227 r->dflt = NULL;
228 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
229 r->dflts = NULL;
230 break;
231 }
232 }
233 if (!r) {
234 r = calloc(1, sizeof *r);
235 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
236 r->llist = llist;
237
Michal Vasko405cc9e2020-12-01 12:01:27 +0100238 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200239 }
240
241 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
242
243 return LY_SUCCESS;
244}
245
246/**
Michal Vaskof4fa90d2021-11-11 15:05:19 +0100247 * @brief Add a bits/enumeration type to unres.
248 *
249 * @param[in] ctx Compile context.
250 * @param[in] leaf Leaf of type bits/enumeration whose disabled items to free.
251 * @return LY_ERR value.
252 */
253static LY_ERR
254lysc_unres_bitenum_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf)
255{
256 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
257 /* skip groupings and redundant for disabled nodes */
258 return LY_SUCCESS;
259 }
260
261 LY_CHECK_RET(ly_set_add(&ctx->unres->disabled_bitenums, leaf, 1, NULL));
262
263 return LY_SUCCESS;
264}
265
266/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200267 * @brief Duplicate the compiled pattern structure.
268 *
269 * Instead of duplicating memory, the reference counter in the @p orig is increased.
270 *
271 * @param[in] orig The pattern structure to duplicate.
272 * @return The duplicated structure to use.
273 */
274static struct lysc_pattern *
275lysc_pattern_dup(struct lysc_pattern *orig)
276{
277 ++orig->refcount;
278 return orig;
279}
280
281/**
282 * @brief Duplicate the array of compiled patterns.
283 *
284 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
285 *
286 * @param[in] ctx Libyang context for logging.
287 * @param[in] orig The patterns sized array to duplicate.
288 * @return New sized array as a copy of @p orig.
289 * @return NULL in case of memory allocation error.
290 */
291static struct lysc_pattern **
292lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
293{
294 struct lysc_pattern **dup = NULL;
295 LY_ARRAY_COUNT_TYPE u;
296
297 assert(orig);
298
299 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
300 LY_ARRAY_FOR(orig, u) {
301 dup[u] = lysc_pattern_dup(orig[u]);
302 LY_ARRAY_INCREMENT(dup);
303 }
304 return dup;
305}
306
307/**
308 * @brief Duplicate compiled range structure.
309 *
310 * @param[in] ctx Libyang context for logging.
311 * @param[in] orig The range structure to be duplicated.
312 * @return New compiled range structure as a copy of @p orig.
313 * @return NULL in case of memory allocation error.
314 */
315static struct lysc_range *
316lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
317{
318 struct lysc_range *dup;
319 LY_ERR ret;
320
321 assert(orig);
322
323 dup = calloc(1, sizeof *dup);
324 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
325 if (orig->parts) {
326 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100327 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200328 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
329 }
330 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
331 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
332 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
333
334 return dup;
335cleanup:
336 free(dup);
337 (void) ret; /* set but not used due to the return type */
338 return NULL;
339}
340
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200341/**
Michal Vaskodfd254d2021-06-24 09:24:59 +0200342 * @brief Compile status information of the given statement.
343 *
344 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
345 * has the status correctly set during the compilation.
346 *
347 * @param[in] ctx Compile context
348 * @param[in,out] stmt_flags Compiled flags to update. If the status was set explicitly, it is already set
349 * in the flags value and we just check the compatibility with the parent's status value.
350 * @param[in] stmt_name Statement name, for logging.
351 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
352 * @param[in] parent_name Name of the parent node, for logging.
353 * @param[in] inherit_log Whether to print inherit message.
354 * @return LY_ERR value.
355 */
356static LY_ERR
357lys_compile_status(struct lysc_ctx *ctx, uint16_t *stmt_flags, const char *stmt_name, uint16_t parent_flags,
358 const char *parent_name, ly_bool inherit_log)
359{
360 /* status - it is not inherited by specification, but it does not make sense to have
361 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
362 if (!(*stmt_flags & LYS_STATUS_MASK)) {
363 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
364 if (inherit_log) {
365 LOGVRB("Missing explicit \"%s\" status specified in parent \"%s\", inheriting for \"%s\".",
366 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name, stmt_name);
367 }
368 *stmt_flags |= parent_flags & LYS_STATUS_MASK;
369 } else {
370 *stmt_flags |= LYS_STATUS_CURR;
371 }
372 } else if (parent_flags & LYS_STATUS_MASK) {
373 /* check status compatibility with the parent */
374 if ((parent_flags & LYS_STATUS_MASK) > (*stmt_flags & LYS_STATUS_MASK)) {
375 if (*stmt_flags & LYS_STATUS_CURR) {
376 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
377 "Status \"current\" of \"%s\" is in conflict with the \"%s\" status of parent \"%s\".",
378 stmt_name, (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name);
379 } else { /* LYS_STATUS_DEPRC */
380 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
381 "Status \"deprecated\" of \"%s\" is in conflict with the \"obsolete\" status of parent \"%s\".",
382 stmt_name, parent_name);
383 }
384 return LY_EVALID;
385 }
386 }
387 return LY_SUCCESS;
388}
389
390/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200391 * @brief Compile information from the when statement
392 *
393 * @param[in] ctx Compile context.
394 * @param[in] when_p Parsed when structure.
Michal Vaskodfd254d2021-06-24 09:24:59 +0200395 * @param[in] parent_flags Flags of the parsed node with the when statement.
396 * @param[in] compiled_parent Closest compiled parent of the when statement.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200397 * @param[in] ctx_node Context node for the when statement.
398 * @param[out] when Pointer where to store pointer to the created compiled when structure.
399 * @return LY_ERR value.
400 */
401static LY_ERR
Michal Vaskodfd254d2021-06-24 09:24:59 +0200402lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t parent_flags,
403 const struct lysc_node *compiled_parent, const struct lysc_node *ctx_node, struct lysc_when **when)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200404{
405 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200406 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200407
408 *when = calloc(1, sizeof **when);
409 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
410 (*when)->refcount = 1;
411 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200412 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci8df109d2021-04-23 12:19:08 +0200413 LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200414 (*when)->context = (struct lysc_node *)ctx_node;
415 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
416 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100417 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vaskodfd254d2021-06-24 09:24:59 +0200418 (*when)->flags = (parent_flags & LYS_STATUS_MASK);
419 if (compiled_parent) {
420 LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", compiled_parent->flags, compiled_parent->name, 0));
421 } else {
422 LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", 0, NULL, 0));
423 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200424
425done:
426 return ret;
427}
428
429LY_ERR
Michal Vaskodfd254d2021-06-24 09:24:59 +0200430lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t parent_flags, const struct lysc_node *compiled_parent,
431 const struct lysc_node *ctx_node, struct lysc_node *node, struct lysc_when **when_c)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200432{
433 struct lysc_when **new_when, ***node_when;
434
435 assert(when_p);
436
437 /* get the when array */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100438 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200439
440 /* create new when pointer */
441 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
442 if (!when_c || !(*when_c)) {
443 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +0200444 LY_CHECK_RET(lys_compile_when_(ctx, when_p, parent_flags, compiled_parent, ctx_node, new_when));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200445
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200446 /* remember the compiled when for sharing */
447 if (when_c) {
448 *when_c = *new_when;
449 }
450 } else {
451 /* use the previously compiled when */
452 ++(*when_c)->refcount;
453 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100454 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200455
Michal Vasko6f08e962021-07-23 08:30:47 +0200456 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100457 /* do not check "when" semantics in a grouping, but repeat the check for different node because
458 * of dummy node check */
Michal Vaskoc130e162021-10-19 11:30:00 +0200459 LY_CHECK_RET(ly_set_add(&ctx->unres->whens, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200460 }
461
462 return LY_SUCCESS;
463}
464
465/**
466 * @brief Compile information from the must statement
467 * @param[in] ctx Compile context.
468 * @param[in] must_p The parsed must statement structure.
469 * @param[in,out] must Prepared (empty) compiled must structure to fill.
470 * @return LY_ERR value.
471 */
472static LY_ERR
473lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
474{
475 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200476 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200477
478 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200479 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, must_p->arg.str, strlen(must_p->arg.str),
Radek Krejci8df109d2021-04-23 12:19:08 +0200480 LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200481 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
482 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
483 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
484 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100485 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200486
487done:
488 return ret;
489}
490
491/**
492 * @brief Validate and normalize numeric value from a range definition.
493 * @param[in] ctx Compile context.
494 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
495 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
496 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
497 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
498 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
499 * @param[in] value String value of the range boundary.
500 * @param[out] len Number of the processed bytes from the value. Processing stops on the first character which is not part of the number boundary.
501 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
502 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
503 */
504static LY_ERR
505range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
506 size_t *len, char **valcopy)
507{
508 size_t fraction = 0, size;
509
510 *len = 0;
511
512 assert(value);
513 /* parse value */
514 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
515 return LY_EVALID;
516 }
517
518 if ((value[*len] == '-') || (value[*len] == '+')) {
519 ++(*len);
520 }
521
522 while (isdigit(value[*len])) {
523 ++(*len);
524 }
525
526 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
527 if (basetype == LY_TYPE_DEC64) {
528 goto decimal;
529 } else {
530 *valcopy = strndup(value, *len);
531 return LY_SUCCESS;
532 }
533 }
534 fraction = *len;
535
536 ++(*len);
537 while (isdigit(value[*len])) {
538 ++(*len);
539 }
540
541 if (basetype == LY_TYPE_DEC64) {
542decimal:
543 assert(frdigits);
544 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100545 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200546 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100547 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200548 return LY_EINVAL;
549 }
550 if (fraction) {
551 size = (*len) + (frdigits - ((*len) - 1 - fraction));
552 } else {
553 size = (*len) + frdigits + 1;
554 }
555 *valcopy = malloc(size * sizeof **valcopy);
556 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
557
558 (*valcopy)[size - 1] = '\0';
559 if (fraction) {
560 memcpy(&(*valcopy)[0], &value[0], fraction);
561 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
562 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
563 } else {
564 memcpy(&(*valcopy)[0], &value[0], *len);
565 memset(&(*valcopy)[*len], '0', frdigits);
566 }
567 }
568 return LY_SUCCESS;
569}
570
571/**
572 * @brief Check that values in range are in ascendant order.
573 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
574 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
575 * max can be also equal.
576 * @param[in] value Current value to check.
577 * @param[in] prev_value The last seen value.
578 * @return LY_SUCCESS or LY_EEXIST for invalid order.
579 */
580static LY_ERR
581range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
582{
583 if (unsigned_value) {
584 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
585 return LY_EEXIST;
586 }
587 } else {
588 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
589 return LY_EEXIST;
590 }
591 }
592 return LY_SUCCESS;
593}
594
595/**
596 * @brief Set min/max value of the range part.
597 * @param[in] ctx Compile context.
598 * @param[in] part Range part structure to fill.
599 * @param[in] max Flag to distinguish if storing min or max value.
600 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
601 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
602 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
603 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
604 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
605 * @param[in] base_range Range from the type from which the current type is derived (if not built-in) to get type's min and max values.
606 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
607 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
608 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
609 * frdigits value), LY_EMEM.
610 */
611static LY_ERR
612range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
613 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
614{
615 LY_ERR ret = LY_SUCCESS;
616 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100617 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200618
619 if (value) {
620 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
621 LY_CHECK_GOTO(ret, finalize);
622 }
623 if (!valcopy && base_range) {
624 if (max) {
625 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
626 } else {
627 part->min_64 = base_range->parts[0].min_64;
628 }
629 if (!first) {
630 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
631 }
632 goto finalize;
633 }
634
635 switch (basetype) {
636 case LY_TYPE_INT8: /* range */
637 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100638 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200639 } else if (max) {
640 part->max_64 = INT64_C(127);
641 } else {
642 part->min_64 = INT64_C(-128);
643 }
644 if (!ret && !first) {
645 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
646 }
647 break;
648 case LY_TYPE_INT16: /* range */
649 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100650 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
651 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200652 } else if (max) {
653 part->max_64 = INT64_C(32767);
654 } else {
655 part->min_64 = INT64_C(-32768);
656 }
657 if (!ret && !first) {
658 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
659 }
660 break;
661 case LY_TYPE_INT32: /* range */
662 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100663 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
664 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200665 } else if (max) {
666 part->max_64 = INT64_C(2147483647);
667 } else {
668 part->min_64 = INT64_C(-2147483648);
669 }
670 if (!ret && !first) {
671 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
672 }
673 break;
674 case LY_TYPE_INT64: /* range */
675 case LY_TYPE_DEC64: /* range */
676 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100677 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
678 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200679 } else if (max) {
680 part->max_64 = INT64_C(9223372036854775807);
681 } else {
682 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
683 }
684 if (!ret && !first) {
685 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
686 }
687 break;
688 case LY_TYPE_UINT8: /* range */
689 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100690 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200691 } else if (max) {
692 part->max_u64 = UINT64_C(255);
693 } else {
694 part->min_u64 = UINT64_C(0);
695 }
696 if (!ret && !first) {
697 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
698 }
699 break;
700 case LY_TYPE_UINT16: /* range */
701 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100702 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), LY_BASE_DEC, max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200703 } else if (max) {
704 part->max_u64 = UINT64_C(65535);
705 } else {
706 part->min_u64 = UINT64_C(0);
707 }
708 if (!ret && !first) {
709 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
710 }
711 break;
712 case LY_TYPE_UINT32: /* range */
713 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100714 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
715 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200716 } else if (max) {
717 part->max_u64 = UINT64_C(4294967295);
718 } else {
719 part->min_u64 = UINT64_C(0);
720 }
721 if (!ret && !first) {
722 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
723 }
724 break;
725 case LY_TYPE_UINT64: /* range */
726 case LY_TYPE_STRING: /* length */
727 case LY_TYPE_BINARY: /* length */
728 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100729 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
730 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200731 } else if (max) {
732 part->max_u64 = UINT64_C(18446744073709551615);
733 } else {
734 part->min_u64 = UINT64_C(0);
735 }
736 if (!ret && !first) {
737 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
738 }
739 break;
740 default:
741 LOGINT(ctx->ctx);
742 ret = LY_EINT;
743 }
744
745finalize:
746 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100747 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200748 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
749 length_restr ? "length" : "range", valcopy ? valcopy : *value);
750 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100751 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200752 "Invalid %s restriction - invalid value \"%s\".",
753 length_restr ? "length" : "range", valcopy ? valcopy : *value);
754 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100755 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200756 "Invalid %s restriction - values are not in ascending order (%s).",
757 length_restr ? "length" : "range",
758 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
759 } else if (!ret && value) {
760 *value = *value + len;
761 }
762 free(valcopy);
763 return ret;
764}
765
766/**
767 * @brief Compile the parsed range restriction.
768 * @param[in] ctx Compile context.
769 * @param[in] range_p Parsed range structure to compile.
770 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
771 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
772 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
773 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
774 * range restriction must be more restrictive than the base_range.
775 * @param[in,out] range Pointer to the created current range structure.
776 * @return LY_ERR value.
777 */
778static LY_ERR
779lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
780 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
781{
aPiecek1c4da362021-04-29 14:26:34 +0200782 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200783 const char *expr;
784 struct lysc_range_part *parts = NULL, *part;
785 ly_bool range_expected = 0, uns;
786 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
787
788 assert(range);
789 assert(range_p);
790
791 expr = range_p->arg.str;
792 while (1) {
793 if (isspace(*expr)) {
794 ++expr;
795 } else if (*expr == '\0') {
796 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100797 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200798 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200799 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200800 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200801 goto cleanup;
802 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100803 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200804 "Invalid %s restriction - unexpected end of the expression (%s).",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200805 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200806 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200807 goto cleanup;
808 }
809 parts_done++;
810 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100811 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200812 if (parts) {
813 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100814 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200815 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100816 (int)(expr - range_p->arg.str), range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200817 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200818 goto cleanup;
819 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100820 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200821
822 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200823 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200824 part->max_64 = part->min_64;
825 } else if (*expr == '|') {
826 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100827 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200828 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200829 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200830 goto cleanup;
831 }
832 expr++;
833 parts_done++;
834 /* process next part of the expression */
835 } else if (!strncmp(expr, "..", 2)) {
836 expr += 2;
837 while (isspace(*expr)) {
838 expr++;
839 }
840 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100841 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200842 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
aPiecek1c4da362021-04-29 14:26:34 +0200843 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200844 goto cleanup;
845 }
846 /* continue expecting the upper boundary */
847 range_expected = 1;
848 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
849 /* number */
850 if (range_expected) {
851 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200852 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200853 range_expected = 0;
854 } else {
855 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200856 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200857 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
858 part->max_64 = part->min_64;
859 }
860
861 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100862 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
863 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200864 while (isspace(*expr)) {
865 expr++;
866 }
867 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100868 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200869 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200870 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200871 goto cleanup;
872 }
873 if (range_expected) {
874 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200875 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200876 range_expected = 0;
877 } else {
878 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200879 LY_CHECK_GOTO(ret = range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_COUNT(parts) - 2].max_64 : 0,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200880 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
881 part->min_64 = part->max_64;
882 }
883 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100884 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200885 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200886 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200887 goto cleanup;
888 }
889 }
890
891 /* check with the previous range/length restriction */
892 if (base_range) {
893 switch (basetype) {
894 case LY_TYPE_BINARY:
895 case LY_TYPE_UINT8:
896 case LY_TYPE_UINT16:
897 case LY_TYPE_UINT32:
898 case LY_TYPE_UINT64:
899 case LY_TYPE_STRING:
900 uns = 1;
901 break;
902 case LY_TYPE_DEC64:
903 case LY_TYPE_INT8:
904 case LY_TYPE_INT16:
905 case LY_TYPE_INT32:
906 case LY_TYPE_INT64:
907 uns = 0;
908 break;
909 default:
910 LOGINT(ctx->ctx);
911 ret = LY_EINT;
912 goto cleanup;
913 }
914 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
915 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
916 goto baseerror;
917 }
918 /* current lower bound is not lower than the base */
919 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
920 /* base has single value */
921 if (base_range->parts[v].min_64 == parts[u].min_64) {
922 /* both lower bounds are the same */
923 if (parts[u].min_64 != parts[u].max_64) {
924 /* current continues with a range */
925 goto baseerror;
926 } else {
927 /* equal single values, move both forward */
928 ++v;
929 continue;
930 }
931 } else {
932 /* base is single value lower than current range, so the
933 * value from base range is removed in the current,
934 * move only base and repeat checking */
935 ++v;
936 --u;
937 continue;
938 }
939 } else {
940 /* base is the range */
941 if (parts[u].min_64 == parts[u].max_64) {
942 /* current is a single value */
943 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
944 /* current is behind the base range, so base range is omitted,
945 * move the base and keep the current for further check */
946 ++v;
947 --u;
948 } /* else it is within the base range, so move the current, but keep the base */
949 continue;
950 } else {
951 /* both are ranges - check the higher bound, the lower was already checked */
952 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
953 /* higher bound is higher than the current higher bound */
954 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
955 /* but the current lower bound is also higher, so the base range is omitted,
956 * continue with the same current, but move the base */
957 --u;
958 ++v;
959 continue;
960 }
961 /* current range starts within the base range but end behind it */
962 goto baseerror;
963 } else {
964 /* current range is smaller than the base,
965 * move current, but stay with the base */
966 continue;
967 }
968 }
969 }
970 }
971 if (u != parts_done) {
972baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100973 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200974 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
Michal Vasko20c0b9f2021-08-24 08:16:27 +0200975 length_restr ? "length" : "range", range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200976 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200977 goto cleanup;
978 }
979 }
980
981 if (!(*range)) {
982 *range = calloc(1, sizeof **range);
983 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
984 }
985
986 /* we rewrite the following values as the types chain is being processed */
987 if (range_p->eapptag) {
988 lydict_remove(ctx->ctx, (*range)->eapptag);
989 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
990 }
991 if (range_p->emsg) {
992 lydict_remove(ctx->ctx, (*range)->emsg);
993 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
994 }
995 if (range_p->dsc) {
996 lydict_remove(ctx->ctx, (*range)->dsc);
997 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
998 }
999 if (range_p->ref) {
1000 lydict_remove(ctx->ctx, (*range)->ref);
1001 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
1002 }
1003 /* extensions are taken only from the last range by the caller */
1004
1005 (*range)->parts = parts;
1006 parts = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001007cleanup:
1008 LY_ARRAY_FREE(parts);
1009
1010 return ret;
1011}
1012
1013LY_ERR
Radek Krejci2efc45b2020-12-22 16:25:44 +01001014lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001015{
1016 size_t idx, idx2, start, end, size, brack;
1017 char *perl_regex, *ptr;
Christian Hoppsdafed222021-03-22 13:02:42 -04001018 int err_code, compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001019 const char *orig_ptr;
1020 PCRE2_SIZE err_offset;
1021 pcre2_code *code_local;
1022
1023#define URANGE_LEN 19
1024 char *ublock2urange[][2] = {
1025 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1026 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1027 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1028 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1029 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1030 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1031 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1032 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1033 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1034 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1035 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1036 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1037 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1038 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1039 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1040 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1041 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1042 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1043 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1044 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1045 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1046 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1047 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1048 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1049 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1050 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1051 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1052 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1053 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1054 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1055 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1056 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1057 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1058 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1059 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1060 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1061 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1062 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1063 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1064 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1065 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1066 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1067 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1068 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1069 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1070 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1071 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1072 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1073 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1074 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1075 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1076 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1077 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1078 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1079 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1080 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1081 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1082 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1083 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1084 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1085 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1086 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1087 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1088 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1089 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1090 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1091 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1092 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1093 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1094 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1095 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1096 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1097 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1098 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1099 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1100 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1101 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1102 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1103 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1104 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1105 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1106 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1107 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +01001108 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001109 {NULL, NULL}
1110 };
1111
1112 /* adjust the expression to a Perl equivalent
1113 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1114
1115 /* allocate space for the transformed pattern */
1116 size = strlen(pattern) + 1;
Christian Hoppsdafed222021-03-22 13:02:42 -04001117 compile_opts = PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
1118#ifdef PCRE2_ENDANCHORED
1119 compile_opts |= PCRE2_ENDANCHORED;
1120#else
1121 /* add space for trailing $ anchor */
1122 size++;
1123#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001124 perl_regex = malloc(size);
1125 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1126 perl_regex[0] = '\0';
1127
1128 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1129 brack = 0;
1130 idx = 0;
1131 orig_ptr = pattern;
1132 while (orig_ptr[0]) {
1133 switch (orig_ptr[0]) {
1134 case '$':
1135 case '^':
1136 if (!brack) {
1137 /* make space for the extra character */
1138 ++size;
1139 perl_regex = ly_realloc(perl_regex, size);
1140 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1141
1142 /* print escape slash */
1143 perl_regex[idx] = '\\';
1144 ++idx;
1145 }
1146 break;
1147 case '[':
1148 /* must not be escaped */
1149 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1150 ++brack;
1151 }
1152 break;
1153 case ']':
1154 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1155 /* pattern was checked and compiled already */
1156 assert(brack);
1157 --brack;
1158 }
1159 break;
1160 default:
1161 break;
1162 }
1163
1164 /* copy char */
1165 perl_regex[idx] = orig_ptr[0];
1166
1167 ++idx;
1168 ++orig_ptr;
1169 }
Christian Hoppsdafed222021-03-22 13:02:42 -04001170#ifndef PCRE2_ENDANCHORED
1171 /* anchor match to end of subject */
1172 perl_regex[idx++] = '$';
1173#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001174 perl_regex[idx] = '\0';
1175
1176 /* substitute Unicode Character Blocks with exact Character Ranges */
1177 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1178 start = ptr - perl_regex;
1179
1180 ptr = strchr(ptr, '}');
1181 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001182 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001183 free(perl_regex);
1184 return LY_EVALID;
1185 }
1186 end = (ptr - perl_regex) + 1;
1187
1188 /* need more space */
1189 if (end - start < URANGE_LEN) {
1190 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1191 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1192 }
1193
1194 /* find our range */
1195 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001196 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1197 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001198 break;
1199 }
1200 }
1201 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001202 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001203 free(perl_regex);
1204 return LY_EVALID;
1205 }
1206
1207 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1208 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1209 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1210 ++idx;
1211 }
1212 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1213 --idx;
1214 }
1215 }
1216 if (idx) {
1217 /* skip brackets */
1218 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1219 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1220 } else {
1221 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1222 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1223 }
1224 }
1225
1226 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001227 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001228 &err_code, &err_offset, NULL);
1229 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001230 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1231 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001232 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001233 free(perl_regex);
1234 return LY_EVALID;
1235 }
1236 free(perl_regex);
1237
1238 if (code) {
1239 *code = code_local;
1240 } else {
1241 free(code_local);
1242 }
1243
1244 return LY_SUCCESS;
1245
1246#undef URANGE_LEN
1247}
1248
Michal Vasko51de7b72022-04-29 09:50:22 +02001249LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001250lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1251 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1252{
1253 struct lysc_pattern **pattern;
1254 LY_ARRAY_COUNT_TYPE u;
1255 LY_ERR ret = LY_SUCCESS;
1256
1257 /* first, copy the patterns from the base type */
1258 if (base_patterns) {
1259 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1260 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1261 }
1262
1263 LY_ARRAY_FOR(patterns_p, u) {
1264 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1265 *pattern = calloc(1, sizeof **pattern);
1266 ++(*pattern)->refcount;
1267
Radek Krejci2efc45b2020-12-22 16:25:44 +01001268 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001269 LY_CHECK_RET(ret);
1270
Radek Krejcif13b87b2020-12-01 22:02:17 +01001271 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001272 (*pattern)->inverted = 1;
1273 }
1274 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1275 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1276 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1277 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1278 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001279 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001280 }
1281done:
1282 return ret;
1283}
1284
1285/**
1286 * @brief map of the possible restrictions combination for the specific built-in type.
1287 */
1288static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1289 0 /* LY_TYPE_UNKNOWN */,
1290 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1291 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1292 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1293 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1294 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1295 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1296 LYS_SET_BIT /* LY_TYPE_BITS */,
1297 0 /* LY_TYPE_BOOL */,
1298 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1299 0 /* LY_TYPE_EMPTY */,
1300 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1301 LYS_SET_BASE /* LY_TYPE_IDENT */,
1302 LYS_SET_REQINST /* LY_TYPE_INST */,
1303 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1304 LYS_SET_TYPE /* LY_TYPE_UNION */,
1305 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1306 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1307 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1308 LYS_SET_RANGE /* LY_TYPE_INT64 */
1309};
1310
1311/**
1312 * @brief stringification of the YANG built-in data types
1313 */
1314const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001315 LY_TYPE_UNKNOWN_STR,
1316 LY_TYPE_BINARY_STR,
1317 LY_TYPE_UINT8_STR,
1318 LY_TYPE_UINT16_STR,
1319 LY_TYPE_UINT32_STR,
1320 LY_TYPE_UINT64_STR,
1321 LY_TYPE_STRING_STR,
1322 LY_TYPE_BITS_STR,
1323 LY_TYPE_BOOL_STR,
1324 LY_TYPE_DEC64_STR,
1325 LY_TYPE_EMPTY_STR,
1326 LY_TYPE_ENUM_STR,
1327 LY_TYPE_IDENT_STR,
1328 LY_TYPE_INST_STR,
1329 LY_TYPE_LEAFREF_STR,
1330 LY_TYPE_UNION_STR,
1331 LY_TYPE_INT8_STR,
1332 LY_TYPE_INT16_STR,
1333 LY_TYPE_INT32_STR,
1334 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001335};
1336
1337/**
1338 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1339 * @param[in] ctx Compile context.
1340 * @param[in] enums_p Array of the parsed enum structures to compile.
1341 * @param[in] basetype Base YANG built-in type from which the current type is derived. Only LY_TYPE_ENUM and LY_TYPE_BITS are expected.
1342 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001343 * @param[out] bitenums Newly created array of the compiled bitenums information for the current type.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001344 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1345 */
1346static LY_ERR
1347lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001348 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001349{
1350 LY_ERR ret = LY_SUCCESS;
1351 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001352 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1353 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001354 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001355 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001356
1357 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001358 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001359 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1360 return LY_EVALID;
1361 }
1362
1363 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001364 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001365 if (base_enums) {
1366 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1367 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001368 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001369 break;
1370 }
1371 }
1372 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001373 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001374 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001375 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001376 return LY_EVALID;
1377 }
1378 match = v;
1379 }
1380
1381 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001382 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001383 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001384 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001385 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001386 LY_ARRAY_FOR(*bitenums, v) {
1387 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001388 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001389 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001390 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001391 return LY_EVALID;
1392 }
1393 }
1394 } else if (base_enums) {
1395 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001396 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001397 } else {
1398 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001399 if (u == 0) {
1400 cur_val = 0;
1401 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001402 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001403 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001404 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001405 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001406 } else {
1407 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001408 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001409 }
1410
1411 /* save highest value for auto assing */
1412 if (highest_value < cur_val) {
1413 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001414 }
1415 } else { /* LY_TYPE_BITS */
1416 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001417 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001418 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001419 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001420 LY_ARRAY_FOR(*bitenums, v) {
1421 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001422 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001423 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001424 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001425 return LY_EVALID;
1426 }
1427 }
1428 } else if (base_enums) {
1429 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001430 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001431 } else {
1432 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001433 if (u == 0) {
1434 cur_pos = 0;
1435 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001436 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001437 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001438 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001439 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001440 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001441 } else {
1442 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001443 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001444 }
1445
1446 /* save highest position for auto assing */
1447 if (highest_position < cur_pos) {
1448 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001449 }
1450 }
1451
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001452 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001453 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001454 if (basetype == LY_TYPE_ENUM) {
1455 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001456 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001457 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001458 enums_p[u].name, base_enums[match].value, cur_val);
1459 return LY_EVALID;
1460 }
1461 } else {
1462 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001463 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001464 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001465 enums_p[u].name, base_enums[match].position, cur_pos);
1466 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001467 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001468 }
1469 }
1470
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001471 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001472 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001473 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1474 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1475 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001476 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 +01001477 if (basetype == LY_TYPE_ENUM) {
1478 e->value = cur_val;
1479 } else {
1480 e->position = cur_pos;
1481 }
Radek Krejciab430862021-03-02 20:13:40 +01001482 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001483
Michal Vaskof4fa90d2021-11-11 15:05:19 +01001484 /* evaluate if-ffeatures */
1485 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1486 if (!enabled) {
1487 /* set only flag, later resolved and removed */
1488 e->flags |= LYS_DISABLED;
1489 }
1490
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001491 if (basetype == LY_TYPE_BITS) {
1492 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001493 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001494 if (v != u) {
1495 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001496 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1497 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001498 }
1499 }
1500 }
1501
1502done:
1503 return ret;
1504}
1505
Radek Krejci6a205692020-12-09 13:58:22 +01001506static LY_ERR
1507lys_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 +01001508 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001509{
1510 LY_ERR ret = LY_SUCCESS;
1511 struct lysc_type **utypes = *utypes_p;
1512 struct lysc_type_union *un_aux = NULL;
1513
1514 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1515 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001516 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1517 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001518 LY_CHECK_GOTO(ret, error);
1519 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1520 /* add space for additional types from the union subtype */
1521 un_aux = (struct lysc_type_union *)utypes[u + additional];
1522 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1523 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1524
1525 /* copy subtypes of the subtype union */
1526 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1527 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1528 struct lysc_type_leafref *lref;
1529
1530 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1531 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1532 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1533 lref = (struct lysc_type_leafref *)utypes[u + additional];
1534
1535 lref->basetype = LY_TYPE_LEAFREF;
1536 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1537 LY_CHECK_GOTO(ret, error);
Michal Vasko080064b2021-08-31 15:20:44 +02001538 lref->refcount = 1;
Radek Krejci6a205692020-12-09 13:58:22 +01001539 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001540 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001541 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001542 LY_CHECK_GOTO(ret, error);
1543 /* TODO extensions */
1544
1545 } else {
1546 utypes[u + additional] = un_aux->types[v];
Michal Vasko04338d92021-09-01 07:58:14 +02001547 LY_ATOMIC_INC_BARRIER(un_aux->types[v]->refcount);
Radek Krejci6a205692020-12-09 13:58:22 +01001548 }
1549 ++additional;
1550 LY_ARRAY_INCREMENT(utypes);
1551 }
1552 /* compensate u increment in main loop */
1553 --additional;
1554
1555 /* free the replaced union subtype */
1556 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1557 un_aux = NULL;
1558 } else {
1559 LY_ARRAY_INCREMENT(utypes);
1560 }
1561 }
1562
1563 *utypes_p = utypes;
1564 return LY_SUCCESS;
1565
1566error:
1567 if (un_aux) {
1568 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1569 }
1570 *utypes_p = utypes;
1571 return ret;
1572}
1573
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001574/**
1575 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1576 * @param[in] ctx Compile context.
1577 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1578 * @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 +02001579 * @param[in] context_name Name of the context node or referencing typedef for logging.
1580 * @param[in] type_p Parsed type to compile.
1581 * @param[in] basetype Base YANG built-in type of the type to compile.
1582 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1583 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1584 * @param[out] type Newly created type structure with the filled information about the type.
1585 * @return LY_ERR value.
1586 */
1587static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001588lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1589 struct lysp_type *type_p, LY_DATA_TYPE basetype, const char *tpdfname, struct lysc_type *base, struct lysc_type **type)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001590{
1591 LY_ERR ret = LY_SUCCESS;
1592 struct lysc_type_bin *bin;
1593 struct lysc_type_num *num;
1594 struct lysc_type_str *str;
1595 struct lysc_type_bits *bits;
1596 struct lysc_type_enum *enumeration;
1597 struct lysc_type_dec *dec;
1598 struct lysc_type_identityref *idref;
1599 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001600 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001601
1602 switch (basetype) {
1603 case LY_TYPE_BINARY:
1604 bin = (struct lysc_type_bin *)(*type);
1605
1606 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1607 if (type_p->length) {
1608 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1609 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1610 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001611 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001612 }
1613 }
1614 break;
1615 case LY_TYPE_BITS:
1616 /* RFC 7950 9.7 - bits */
1617 bits = (struct lysc_type_bits *)(*type);
1618 if (type_p->bits) {
1619 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1620 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1621 (struct lysc_type_bitenum_item **)&bits->bits));
1622 }
1623
1624 if (!base && !type_p->flags) {
1625 /* type derived from bits built-in type must contain at least one bit */
1626 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001627 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001628 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001629 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001630 }
1631 return LY_EVALID;
1632 }
1633 break;
1634 case LY_TYPE_DEC64:
1635 dec = (struct lysc_type_dec *)(*type);
1636
1637 /* RFC 7950 9.3.4 - fraction-digits */
1638 if (!base) {
1639 if (!type_p->fraction_digits) {
1640 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001641 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001642 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001643 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001644 }
1645 return LY_EVALID;
1646 }
1647 dec->fraction_digits = type_p->fraction_digits;
1648 } else {
1649 if (type_p->fraction_digits) {
1650 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1651 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001652 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001653 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1654 tpdfname);
1655 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001656 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001657 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1658 }
1659 return LY_EVALID;
1660 }
1661 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1662 }
1663
1664 /* RFC 7950 9.2.4 - range */
1665 if (type_p->range) {
1666 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1667 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1668 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001669 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001670 }
1671 }
1672 break;
1673 case LY_TYPE_STRING:
1674 str = (struct lysc_type_str *)(*type);
1675
1676 /* RFC 7950 9.4.4 - length */
1677 if (type_p->length) {
1678 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1679 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1680 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001681 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001682 }
1683 } else if (base && ((struct lysc_type_str *)base)->length) {
1684 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1685 }
1686
1687 /* RFC 7950 9.4.5 - pattern */
1688 if (type_p->patterns) {
1689 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1690 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1691 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1692 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1693 }
1694 break;
1695 case LY_TYPE_ENUM:
1696 enumeration = (struct lysc_type_enum *)(*type);
1697
1698 /* RFC 7950 9.6 - enum */
1699 if (type_p->enums) {
1700 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1701 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1702 }
1703
1704 if (!base && !type_p->flags) {
1705 /* type derived from enumerations built-in type must contain at least one enum */
1706 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001707 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001708 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001709 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001710 }
1711 return LY_EVALID;
1712 }
1713 break;
1714 case LY_TYPE_INT8:
1715 case LY_TYPE_UINT8:
1716 case LY_TYPE_INT16:
1717 case LY_TYPE_UINT16:
1718 case LY_TYPE_INT32:
1719 case LY_TYPE_UINT32:
1720 case LY_TYPE_INT64:
1721 case LY_TYPE_UINT64:
1722 num = (struct lysc_type_num *)(*type);
1723
1724 /* RFC 6020 9.2.4 - range */
1725 if (type_p->range) {
1726 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1727 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1728 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001729 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001730 }
1731 }
1732 break;
1733 case LY_TYPE_IDENT:
1734 idref = (struct lysc_type_identityref *)(*type);
1735
1736 /* RFC 7950 9.10.2 - base */
1737 if (type_p->bases) {
1738 if (base) {
1739 /* only the directly derived identityrefs can contain base specification */
1740 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001741 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001742 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1743 tpdfname);
1744 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001745 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001746 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1747 }
1748 return LY_EVALID;
1749 }
aPiecekf4a0a192021-08-03 15:14:17 +02001750 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001751 }
1752
1753 if (!base && !type_p->flags) {
1754 /* type derived from identityref built-in type must contain at least one base */
1755 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001756 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001757 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001758 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001759 }
1760 return LY_EVALID;
1761 }
1762 break;
1763 case LY_TYPE_LEAFREF:
1764 lref = (struct lysc_type_leafref *)*type;
1765
1766 /* RFC 7950 9.9.3 - require-instance */
1767 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001768 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001769 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001770 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001771 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1772 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001773 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001774 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1775 }
1776 return LY_EVALID;
1777 }
1778 lref->require_instance = type_p->require_instance;
1779 } else if (base) {
1780 /* inherit */
1781 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1782 } else {
1783 /* default is true */
1784 lref->require_instance = 1;
1785 }
1786 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001787 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001788
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001789 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001790 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 +02001791 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001792 } else if (base) {
1793 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Radek Krejci8df109d2021-04-23 12:19:08 +02001794 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001795 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001796 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001797 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001798 return LY_EVALID;
1799 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001800 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001801 return LY_EVALID;
1802 }
1803 break;
1804 case LY_TYPE_INST:
1805 /* RFC 7950 9.9.3 - require-instance */
1806 if (type_p->flags & LYS_SET_REQINST) {
1807 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1808 } else {
1809 /* default is true */
1810 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1811 }
1812 break;
1813 case LY_TYPE_UNION:
1814 un = (struct lysc_type_union *)(*type);
1815
1816 /* RFC 7950 7.4 - type */
1817 if (type_p->types) {
1818 if (base) {
1819 /* only the directly derived union can contain types specification */
1820 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001821 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001822 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1823 tpdfname);
1824 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001825 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001826 "Invalid type substatement for the type not directly derived from union built-in type.");
1827 }
1828 return LY_EVALID;
1829 }
1830 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001831 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 +02001832 }
1833
1834 if (!base && !type_p->flags) {
1835 /* type derived from union built-in type must contain at least one type */
1836 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001837 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001838 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001839 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001840 }
1841 return LY_EVALID;
1842 }
1843 break;
1844 case LY_TYPE_BOOL:
1845 case LY_TYPE_EMPTY:
1846 case LY_TYPE_UNKNOWN: /* just to complete switch */
1847 break;
1848 }
1849
1850 if (tpdfname) {
1851 switch (basetype) {
1852 case LY_TYPE_BINARY:
1853 type_p->compiled = *type;
1854 *type = calloc(1, sizeof(struct lysc_type_bin));
1855 break;
1856 case LY_TYPE_BITS:
1857 type_p->compiled = *type;
1858 *type = calloc(1, sizeof(struct lysc_type_bits));
1859 break;
1860 case LY_TYPE_DEC64:
1861 type_p->compiled = *type;
1862 *type = calloc(1, sizeof(struct lysc_type_dec));
1863 break;
1864 case LY_TYPE_STRING:
1865 type_p->compiled = *type;
1866 *type = calloc(1, sizeof(struct lysc_type_str));
1867 break;
1868 case LY_TYPE_ENUM:
1869 type_p->compiled = *type;
1870 *type = calloc(1, sizeof(struct lysc_type_enum));
1871 break;
1872 case LY_TYPE_INT8:
1873 case LY_TYPE_UINT8:
1874 case LY_TYPE_INT16:
1875 case LY_TYPE_UINT16:
1876 case LY_TYPE_INT32:
1877 case LY_TYPE_UINT32:
1878 case LY_TYPE_INT64:
1879 case LY_TYPE_UINT64:
1880 type_p->compiled = *type;
1881 *type = calloc(1, sizeof(struct lysc_type_num));
1882 break;
1883 case LY_TYPE_IDENT:
1884 type_p->compiled = *type;
1885 *type = calloc(1, sizeof(struct lysc_type_identityref));
1886 break;
1887 case LY_TYPE_LEAFREF:
1888 type_p->compiled = *type;
1889 *type = calloc(1, sizeof(struct lysc_type_leafref));
1890 break;
1891 case LY_TYPE_INST:
1892 type_p->compiled = *type;
1893 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1894 break;
1895 case LY_TYPE_UNION:
1896 type_p->compiled = *type;
1897 *type = calloc(1, sizeof(struct lysc_type_union));
1898 break;
1899 case LY_TYPE_BOOL:
1900 case LY_TYPE_EMPTY:
1901 case LY_TYPE_UNKNOWN: /* just to complete switch */
1902 break;
1903 }
1904 }
1905 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1906
1907cleanup:
1908 return ret;
1909}
1910
1911LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001912lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1913 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001914{
1915 LY_ERR ret = LY_SUCCESS;
1916 ly_bool dummyloops = 0;
1917 struct type_context {
1918 const struct lysp_tpdf *tpdf;
1919 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001920 } *tctx, *tctx_prev = NULL, *tctx_iter;
1921 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1922 struct lysc_type *base = NULL, *prev_type;
1923 struct ly_set tpdf_chain = {0};
Michal Vasko388a6632021-08-06 11:27:43 +02001924 struct lyplg_type *plugin;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001925
1926 (*type) = NULL;
1927 if (dflt) {
1928 *dflt = NULL;
1929 }
1930
1931 tctx = calloc(1, sizeof *tctx);
1932 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001933 for (ret = lysp_type_find(type_p->name, context_pnode, type_p->pmod, &basetype, &tctx->tpdf, &tctx->node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001934 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001935 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1936 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001937 if (basetype) {
1938 break;
1939 }
1940
1941 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001942 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1943 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001944 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1945
1946 if (units && !*units) {
1947 /* inherit units */
1948 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1949 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1950 }
1951 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1952 /* inherit default */
1953 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1954 }
1955 if (dummyloops && (!units || *units) && dflt && *dflt) {
1956 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1957 break;
1958 }
1959
Michal Vasko080064b2021-08-31 15:20:44 +02001960 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
Radek Krejci720d2612021-03-03 19:44:22 +01001961 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1962 * and we need now recompile the type again in the updated context. */
1963 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1964 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1965 }
1966
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001967 if (tctx->tpdf->type.compiled) {
1968 /* it is not necessary to continue, the rest of the chain was already compiled,
1969 * but we still may need to inherit default and units values, so start dummy loops */
1970 basetype = tctx->tpdf->type.compiled->basetype;
1971 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1972 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1973
1974 if ((units && !*units) || (dflt && !*dflt)) {
1975 dummyloops = 1;
1976 goto preparenext;
1977 } else {
1978 tctx = NULL;
1979 break;
1980 }
1981 }
1982
1983 /* circular typedef reference detection */
1984 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1985 /* local part */
1986 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001987 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001988 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001989 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1990 free(tctx);
1991 ret = LY_EVALID;
1992 goto cleanup;
1993 }
1994 }
1995 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1996 /* global part for unions corner case */
1997 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001998 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001999 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002000 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
2001 free(tctx);
2002 ret = LY_EVALID;
2003 goto cleanup;
2004 }
2005 }
2006
2007 /* store information for the following processing */
2008 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
2009 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
2010
2011preparenext:
2012 /* prepare next loop */
2013 tctx_prev = tctx;
2014 tctx = calloc(1, sizeof *tctx);
2015 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
2016 }
2017 free(tctx);
2018
2019 /* allocate type according to the basetype */
2020 switch (basetype) {
2021 case LY_TYPE_BINARY:
2022 *type = calloc(1, sizeof(struct lysc_type_bin));
2023 break;
2024 case LY_TYPE_BITS:
2025 *type = calloc(1, sizeof(struct lysc_type_bits));
2026 break;
2027 case LY_TYPE_BOOL:
2028 case LY_TYPE_EMPTY:
2029 *type = calloc(1, sizeof(struct lysc_type));
2030 break;
2031 case LY_TYPE_DEC64:
2032 *type = calloc(1, sizeof(struct lysc_type_dec));
2033 break;
2034 case LY_TYPE_ENUM:
2035 *type = calloc(1, sizeof(struct lysc_type_enum));
2036 break;
2037 case LY_TYPE_IDENT:
2038 *type = calloc(1, sizeof(struct lysc_type_identityref));
2039 break;
2040 case LY_TYPE_INST:
2041 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2042 break;
2043 case LY_TYPE_LEAFREF:
2044 *type = calloc(1, sizeof(struct lysc_type_leafref));
2045 break;
2046 case LY_TYPE_STRING:
2047 *type = calloc(1, sizeof(struct lysc_type_str));
2048 break;
2049 case LY_TYPE_UNION:
2050 *type = calloc(1, sizeof(struct lysc_type_union));
2051 break;
2052 case LY_TYPE_INT8:
2053 case LY_TYPE_UINT8:
2054 case LY_TYPE_INT16:
2055 case LY_TYPE_UINT16:
2056 case LY_TYPE_INT32:
2057 case LY_TYPE_UINT32:
2058 case LY_TYPE_INT64:
2059 case LY_TYPE_UINT64:
2060 *type = calloc(1, sizeof(struct lysc_type_num));
2061 break;
2062 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01002063 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002064 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
2065 ret = LY_EVALID;
2066 goto cleanup;
2067 }
2068 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
2069 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002070 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002071 ly_data_type2str[basetype]);
2072 free(*type);
2073 (*type) = NULL;
2074 ret = LY_EVALID;
2075 goto cleanup;
2076 }
2077
2078 /* get restrictions from the referred typedefs */
2079 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
2080 tctx = (struct type_context *)tpdf_chain.objs[u];
2081
2082 /* remember the typedef context for circular check */
2083 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
2084 LY_CHECK_GOTO(ret, cleanup);
2085
2086 if (tctx->tpdf->type.compiled) {
Michal Vasko388a6632021-08-06 11:27:43 +02002087 /* already compiled */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002088 base = tctx->tpdf->type.compiled;
2089 continue;
Michal Vasko388a6632021-08-06 11:27:43 +02002090 }
2091
Michal Vaskoddd76592022-01-17 13:34:48 +01002092 /* try to find loaded user type plugins */
2093 plugin = lyplg_find(LYPLG_TYPE, tctx->tpdf->type.pmod->mod->name, tctx->tpdf->type.pmod->mod->revision,
2094 tctx->tpdf->name);
Haithem Ben Ghorbal45be15f2022-07-12 18:02:36 +02002095 if (!plugin && base) {
2096 /* use the base type implementation if available */
2097 plugin = base->plugin;
2098 }
Michal Vaskoddd76592022-01-17 13:34:48 +01002099 if (!plugin) {
2100 /* use the internal built-in type implementation */
2101 plugin = lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
2102 }
Michal Vasko388a6632021-08-06 11:27:43 +02002103 assert(plugin);
2104
2105 if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags) &&
2106 (plugin == base->plugin)) {
2107 /* no change, reuse the compiled base */
2108 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002109 LY_ATOMIC_INC_BARRIER(base->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002110 continue;
2111 }
2112
Michal Vasko04338d92021-09-01 07:58:14 +02002113 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002114 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002115 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002116 tctx->tpdf->name, ly_data_type2str[basetype]);
2117 ret = LY_EVALID;
2118 goto cleanup;
2119 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002120 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002121 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2122 tctx->tpdf->name, tctx->tpdf->dflt.str);
2123 ret = LY_EVALID;
2124 goto cleanup;
2125 }
2126
2127 (*type)->basetype = basetype;
Michal Vasko388a6632021-08-06 11:27:43 +02002128 (*type)->plugin = plugin;
2129
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002130 /* collect extensions */
2131 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko388a6632021-08-06 11:27:43 +02002132
2133 /* compile the new typedef */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002134 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01002135 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002136 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
2137 LY_CHECK_GOTO(ret, cleanup);
2138 base = prev_type;
2139 }
2140 /* remove the processed typedef contexts from the stack for circular check */
2141 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
2142
2143 /* process the type definition in leaf */
2144 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
2145 /* get restrictions from the node itself */
2146 (*type)->basetype = basetype;
Radek Krejcibf940f92021-03-24 21:04:13 +01002147 (*type)->plugin = base ? base->plugin : lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
Michal Vasko04338d92021-09-01 07:58:14 +02002148 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vaskoa99b3572021-02-01 11:54:58 +01002149 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002150 LY_CHECK_GOTO(ret, cleanup);
2151 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
2152 /* no specific restriction in leaf's type definition, copy from the base */
2153 free(*type);
2154 (*type) = base;
Michal Vasko04338d92021-09-01 07:58:14 +02002155 LY_ATOMIC_INC_BARRIER((*type)->refcount);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002156 }
2157
Radek Krejciab430862021-03-02 20:13:40 +01002158 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002159
2160cleanup:
2161 ly_set_erase(&tpdf_chain, free);
2162 return ret;
2163}
2164
2165/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002166 * @brief Check uniqness of the node/action/notification name.
2167 *
2168 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2169 * structures, but they share the namespace so we need to check their name collisions.
2170 *
2171 * @param[in] ctx Compile context.
2172 * @param[in] parent Parent of the nodes to check, can be NULL.
2173 * @param[in] name Name of the item to find in the given lists.
2174 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2175 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2176 */
2177static LY_ERR
2178lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2179 const struct lysc_node *exclude)
2180{
2181 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002182 const struct lysc_node_action *actions;
2183 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002184 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002185 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002186
2187#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2188
2189 if (exclude->nodetype == LYS_CASE) {
2190 /* check restricted only to all the cases */
2191 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002192 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002193 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002194 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002195 return LY_EEXIST;
2196 }
2197 }
2198
2199 return LY_SUCCESS;
2200 }
2201
2202 /* no reason for our parent to be choice anymore */
2203 assert(!parent || (parent->nodetype != LYS_CHOICE));
2204
2205 if (parent && (parent->nodetype == LYS_CASE)) {
2206 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002207
2208 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2209 iter = lysc_data_parent(parent);
2210 do {
2211 parent = parent->parent;
2212 if (parent && (parent->nodetype == LYS_CHOICE)) {
2213 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2214 }
2215 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002216 }
2217
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002218 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002219 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2220 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2221 if (exclude->flags & LYS_IS_OUTPUT) {
2222 getnext_flags |= LYS_GETNEXT_OUTPUT;
2223 parent = lysc_node_child(parent)->next;
2224 } else {
2225 parent = lysc_node_child(parent);
2226 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002227 }
2228
2229 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002230 if (!parent && ctx->ext) {
2231 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2232 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2233 goto error;
2234 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002235
Radek Krejci5fa32a32021-02-08 18:12:38 +01002236 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2237 if (iter->nodetype == LYS_CHOICE) {
2238 iter2 = NULL;
2239 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2240 if (CHECK_NODE(iter2, exclude, name)) {
2241 goto error;
2242 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002243 }
2244 }
2245 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002246 } else {
2247 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2248 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2249 goto error;
2250 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002251
Radek Krejci5fa32a32021-02-08 18:12:38 +01002252 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2253 if (iter->nodetype == LYS_CHOICE) {
2254 iter2 = NULL;
2255 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2256 if (CHECK_NODE(iter2, exclude, name)) {
2257 goto error;
2258 }
2259 }
2260 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002261 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002262
Radek Krejci5fa32a32021-02-08 18:12:38 +01002263 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2264 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2265 if (CHECK_NODE(iter, exclude, name)) {
2266 goto error;
2267 }
2268 }
2269
2270 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2271 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2272 if (CHECK_NODE(iter, exclude, name)) {
2273 goto error;
2274 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002275 }
2276 }
Radek Krejci078e4342021-02-12 18:17:26 +01002277 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002278 return LY_SUCCESS;
2279
2280error:
Radek Krejci078e4342021-02-12 18:17:26 +01002281 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002282 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002283 return LY_EEXIST;
2284
2285#undef CHECK_NODE
2286}
2287
Radek Krejci2a9fc652021-01-22 17:44:34 +01002288/**
2289 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2290 * keep specific order of augments targetting the same node.
2291 *
2292 * @param[in] ctx Compile context
2293 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2294 * the choice itself is expected instead of a specific case node.
2295 * @param[in] node Schema node to connect into the list.
2296 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2297 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2298 */
2299static LY_ERR
2300lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002301{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002302 struct lysc_node **children, *anchor = NULL;
2303 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002304
Radek Krejci2a9fc652021-01-22 17:44:34 +01002305 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002306
Radek Krejci2a9fc652021-01-22 17:44:34 +01002307 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002308 if (node->nodetype == LYS_INPUT) {
2309 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2310 /* input node is part of the action but link it with output */
2311 node->next = &((struct lysc_node_action *)parent)->output.node;
2312 node->prev = node->next;
2313 return LY_SUCCESS;
2314 } else if (node->nodetype == LYS_OUTPUT) {
2315 /* output node is part of the action but link it with input */
2316 node->next = NULL;
2317 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002318 return LY_SUCCESS;
2319 } else if (node->nodetype == LYS_ACTION) {
2320 children = (struct lysc_node **)lysc_node_actions_p(parent);
2321 } else if (node->nodetype == LYS_NOTIF) {
2322 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2323 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002324 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002325 }
2326 assert(children);
2327
2328 if (!(*children)) {
2329 /* first child */
2330 *children = node;
2331 } else if (node->flags & LYS_KEY) {
2332 /* special handling of adding keys */
2333 assert(node->module == parent->module);
2334 anchor = *children;
2335 if (anchor->flags & LYS_KEY) {
2336 while ((anchor->flags & LYS_KEY) && anchor->next) {
2337 anchor = anchor->next;
2338 }
2339 /* insert after the last key */
2340 insert_after = 1;
2341 } /* else insert before anchor (at the beginning) */
2342 } else if ((*children)->prev->module == node->module) {
2343 /* last child is from the same module, keep the order and insert at the end */
2344 anchor = (*children)->prev;
2345 insert_after = 1;
2346 } else if (parent->module == node->module) {
2347 /* adding module child after some augments were connected */
2348 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2349 } else {
2350 /* some augments are already connected and we are connecting new ones,
2351 * keep module name order and insert the node into the children list */
2352 anchor = *children;
2353 do {
2354 anchor = anchor->prev;
2355
2356 /* check that we have not found the last augment node from our module or
2357 * the first augment node from a "smaller" module or
2358 * the first node from a local module */
2359 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2360 (anchor->module == parent->module)) {
2361 /* insert after */
2362 insert_after = 1;
2363 break;
2364 }
2365
2366 /* we have traversed all the nodes, insert before anchor (as the first node) */
2367 } while (anchor->prev->next);
2368 }
2369
2370 /* insert */
2371 if (anchor) {
2372 if (insert_after) {
2373 node->next = anchor->next;
2374 node->prev = anchor;
2375 anchor->next = node;
2376 if (node->next) {
2377 /* middle node */
2378 node->next->prev = node;
2379 } else {
2380 /* last node */
2381 (*children)->prev = node;
2382 }
2383 } else {
2384 node->next = anchor;
2385 node->prev = anchor->prev;
2386 anchor->prev = node;
2387 if (anchor == *children) {
2388 /* first node */
2389 *children = node;
2390 } else {
2391 /* middle node */
2392 node->prev->next = node;
2393 }
2394 }
2395 }
2396
2397 /* check the name uniqueness (even for an only child, it may be in case) */
2398 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2399 return LY_EEXIST;
2400 }
2401 } else {
2402 /* top-level element */
2403 struct lysc_node **list;
2404
Radek Krejci6b88a462021-02-17 12:39:34 +01002405 if (ctx->ext) {
Michal Vasko55bce0e2022-02-15 10:46:08 +01002406 /* container matches all data nodes */
2407 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER, (void **)&list, NULL);
Radek Krejci6b88a462021-02-17 12:39:34 +01002408 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002409 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2410 } else if (node->nodetype == LYS_NOTIF) {
2411 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2412 } else {
2413 list = &ctx->cur_mod->compiled->data;
2414 }
2415 if (!(*list)) {
2416 *list = node;
2417 } else {
2418 /* insert at the end of the module's top-level nodes list */
2419 (*list)->prev->next = node;
2420 node->prev = (*list)->prev;
2421 (*list)->prev = node;
2422 }
2423
2424 /* check the name uniqueness on top-level */
2425 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2426 return LY_EEXIST;
2427 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002428 }
2429
Radek Krejci2a9fc652021-01-22 17:44:34 +01002430 return LY_SUCCESS;
2431}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002432
Radek Krejci2a9fc652021-01-22 17:44:34 +01002433/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002434 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002435 *
2436 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002437 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002438 * @return LY_ERR value.
2439 */
2440static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002441lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002442{
2443 /* case never has any explicit config */
2444 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002445
Michal Vasko7c565922021-06-10 14:58:27 +02002446 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Radek Krejci91531e12021-02-08 19:57:54 +01002447 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002448 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002449 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2450 /* config not explicitly set, inherit it from parent */
Michal Vaskoecdc2222022-01-24 08:45:44 +01002451 assert(!node->parent || (node->parent->flags & LYS_CONFIG_MASK) || (node->parent->nodetype & LYS_AUGMENT));
2452 if (node->parent && (node->parent->flags & LYS_CONFIG_MASK)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002453 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002454 } else {
2455 /* default is config true */
2456 node->flags |= LYS_CONFIG_W;
2457 }
2458 } else {
2459 /* config set explicitly */
2460 node->flags |= LYS_SET_CONFIG;
2461 }
2462
Radek Krejci91531e12021-02-08 19:57:54 +01002463 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002464 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002465 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002466 }
2467
Radek Krejci2a9fc652021-01-22 17:44:34 +01002468 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002469}
2470
Radek Krejci91531e12021-02-08 19:57:54 +01002471/**
2472 * @brief Set various flags of the compiled nodes
2473 *
2474 * @param[in] ctx Compile context.
2475 * @param[in] node Compiled node where the flags will be set.
2476 * @param[in] uses_status If the node is being placed instead of uses, here we have the uses's status value (as node's flags).
2477 * Zero means no uses, non-zero value with no status bit set mean the default status.
2478 */
2479static LY_ERR
2480lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2481{
2482 /* inherit config flags */
2483 LY_CHECK_RET(lys_compile_config(ctx, node));
2484
2485 /* status - it is not inherited by specification, but it does not make sense to have
2486 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002487 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, node->name, uses_status ? uses_status :
2488 (node->parent ? node->parent->flags : 0), uses_status ? "<uses>" : (node->parent ? node->parent->name : NULL),
2489 !uses_status));
Radek Krejci91531e12021-02-08 19:57:54 +01002490
2491 /* other flags */
Michal Vasko7c565922021-06-10 14:58:27 +02002492 if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002493 node->flags |= LYS_IS_INPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002494 } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002495 node->flags |= LYS_IS_OUTPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002496 } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002497 node->flags |= LYS_IS_NOTIF;
2498 }
2499
2500 return LY_SUCCESS;
2501}
2502
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002503LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002504lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2505 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2506 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002507{
2508 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002509 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002510 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002511 struct lysp_when *pwhen = NULL;
Michal Vaskoe02e7402021-07-23 08:29:28 +02002512 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002513
Radek Krejci2a9fc652021-01-22 17:44:34 +01002514 node->nodetype = pnode->nodetype;
2515 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002516 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002517 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002518 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002519
Radek Krejci2a9fc652021-01-22 17:44:34 +01002520 /* compile any deviations for this node */
2521 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vaskoe02e7402021-07-23 08:29:28 +02002522 if (not_supported && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2523 /* if not supported, keep it just like disabled nodes by if-feature */
2524 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
2525 ctx->compile_opts |= LYS_COMPILE_DISABLED;
2526 }
2527 if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002528 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002529 }
2530
Radek Krejci2a9fc652021-01-22 17:44:34 +01002531 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vaskodfd254d2021-06-24 09:24:59 +02002532 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2533 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2534 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002535
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002536 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002537 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002538 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002539 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002540 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002541 }
2542
Radek Krejci91531e12021-02-08 19:57:54 +01002543 /* config, status and other flags */
2544 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002545 LY_CHECK_GOTO(ret, error);
2546
2547 /* list ordering */
2548 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002549 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002550 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002551 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002552 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002553 node->flags &= ~LYS_ORDBY_MASK;
2554 node->flags |= LYS_ORDBY_SYSTEM;
2555 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2556 /* default ordering is system */
2557 node->flags |= LYS_ORDBY_SYSTEM;
2558 }
2559 }
2560
Radek Krejci2a9fc652021-01-22 17:44:34 +01002561 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2562 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2563
Radek Krejci9a3823e2021-01-27 20:26:46 +01002564 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002565 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002566 ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002567 LY_CHECK_GOTO(ret, cleanup);
2568 }
2569
Radek Krejci2a9fc652021-01-22 17:44:34 +01002570 /* nodetype-specific part */
2571 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2572
2573 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002574 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002575 if (node->flags & LYS_MAND_TRUE) {
2576 /* inherit LYS_MAND_TRUE in parent containers */
2577 lys_compile_mandatory_parents(parent, 1);
2578 }
2579
2580 if (child_set) {
2581 /* add the new node into set */
2582 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2583 }
2584
2585 goto cleanup;
2586
2587error:
2588 lysc_node_free(ctx->ctx, node, 0);
2589cleanup:
2590 if (ret && dev_pnode) {
2591 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2592 }
Michal Vaskoe02e7402021-07-23 08:29:28 +02002593 ctx->compile_opts = prev_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002594 lysp_dev_node_free(ctx->ctx, dev_pnode);
2595 return ret;
2596}
2597
2598/**
2599 * @brief Compile parsed action's input/output node information.
2600 * @param[in] ctx Compile context
2601 * @param[in] pnode Parsed inout node.
2602 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2603 * is enriched with the inout-specific information.
2604 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2605 */
2606LY_ERR
2607lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2608{
2609 LY_ERR ret = LY_SUCCESS;
2610 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002611 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002612
2613 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2614 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2615
2616 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002617 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002618 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002619
Radek Krejci01180ac2021-01-27 08:48:22 +01002620 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002621 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2622 }
2623
Michal Vasko95f736c2022-06-08 12:03:31 +02002624 /* connect any augments */
2625 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2626
Michal Vasko7c565922021-06-10 14:58:27 +02002627 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002628
2629done:
2630 return ret;
2631}
2632
2633/**
2634 * @brief Compile parsed action node information.
2635 * @param[in] ctx Compile context
2636 * @param[in] pnode Parsed action node.
2637 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2638 * is enriched with the action-specific information.
2639 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2640 */
2641LY_ERR
2642lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2643{
2644 LY_ERR ret;
2645 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2646 struct lysc_node_action *action = (struct lysc_node_action *)node;
2647 struct lysp_node_action_inout *input, implicit_input = {
2648 .nodetype = LYS_INPUT,
2649 .name = "input",
2650 .parent = pnode,
2651 };
2652 struct lysp_node_action_inout *output, implicit_output = {
2653 .nodetype = LYS_OUTPUT,
2654 .name = "output",
2655 .parent = pnode,
2656 };
2657
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002658 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002659 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002660 if (action_p->input.nodetype == LYS_UNKNOWN) {
2661 input = &implicit_input;
2662 } else {
2663 input = &action_p->input;
2664 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002665 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 +01002666 lysc_update_path(ctx, NULL, NULL);
2667 LY_CHECK_GOTO(ret, done);
2668
Michal Vaskoc130e162021-10-19 11:30:00 +02002669 /* add must(s) to unres */
2670 ret = lysc_unres_must_add(ctx, &action->input.node, &input->node);
2671 LY_CHECK_GOTO(ret, done);
2672
Radek Krejci2a9fc652021-01-22 17:44:34 +01002673 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002674 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002675 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002676 output = &implicit_output;
2677 } else {
2678 output = &action_p->output;
2679 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002680 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 +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->output.node, &output->node);
2686 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002687
2688done:
2689 return ret;
2690}
2691
2692/**
2693 * @brief Compile parsed action node information.
2694 * @param[in] ctx Compile context
2695 * @param[in] pnode Parsed action node.
2696 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2697 * is enriched with the action-specific information.
2698 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2699 */
2700LY_ERR
2701lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2702{
2703 LY_ERR ret = LY_SUCCESS;
2704 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2705 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2706 struct lysp_node *child_p;
2707
2708 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002709
2710 /* add must(s) to unres */
2711 ret = lysc_unres_must_add(ctx, node, pnode);
2712 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002713
Radek Krejci01180ac2021-01-27 08:48:22 +01002714 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002715 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002716 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002717 }
2718
Michal Vasko95f736c2022-06-08 12:03:31 +02002719 /* connect any augments */
2720 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2721
Radek Krejci2a9fc652021-01-22 17:44:34 +01002722done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002723 return ret;
2724}
2725
2726/**
2727 * @brief Compile parsed container node information.
2728 * @param[in] ctx Compile context
2729 * @param[in] pnode Parsed container node.
2730 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2731 * is enriched with the container-specific information.
2732 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2733 */
2734static LY_ERR
2735lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2736{
2737 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2738 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2739 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002740 LY_ERR ret = LY_SUCCESS;
2741
2742 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002743 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002744 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002745 }
2746
2747 /* more cases when the container has meaning but is kept NP for convenience:
2748 * - when condition
2749 * - direct child action/notification
2750 */
2751
2752 LY_LIST_FOR(cont_p->child, child_p) {
2753 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2754 LY_CHECK_GOTO(ret, done);
2755 }
2756
Michal Vasko5347e3a2020-11-03 17:14:57 +01002757 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002758
2759 /* add must(s) to unres */
2760 ret = lysc_unres_must_add(ctx, node, pnode);
2761 LY_CHECK_GOTO(ret, done);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002762
Michal Vasko95f736c2022-06-08 12:03:31 +02002763 /* connect any augments */
2764 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
2765
Radek Krejci2a9fc652021-01-22 17:44:34 +01002766 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2767 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2768 LY_CHECK_GOTO(ret, done);
2769 }
2770 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2771 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2772 LY_CHECK_GOTO(ret, done);
2773 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002774
2775done:
2776 return ret;
2777}
2778
Michal Vasko72244882021-01-12 15:21:05 +01002779/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002780 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2781 * @param[in] ctx Compile context.
2782 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2783 * @param[in] type_p Parsed type to compile.
2784 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2785 * @return LY_ERR value.
2786 */
2787static LY_ERR
2788lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2789 struct lysc_node_leaf *leaf)
2790{
2791 struct lysp_qname *dflt;
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002792 struct lysc_type **t;
2793 LY_ARRAY_COUNT_TYPE u, count;
2794 ly_bool in_unres = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002795
Michal Vaskoa99b3572021-02-01 11:54:58 +01002796 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002797 leaf->units ? NULL : &leaf->units, &dflt));
2798
2799 /* store default value, if any */
2800 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2801 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2802 }
2803
Michal Vaskoc130e162021-10-19 11:30:00 +02002804 /* store leafref(s) to be resolved */
2805 LY_CHECK_RET(lysc_unres_leafref_add(ctx, leaf, type_p->pmod));
2806
Michal Vaskof4fa90d2021-11-11 15:05:19 +01002807 /* type-specific checks */
2808 if (leaf->type->basetype == LY_TYPE_UNION) {
2809 t = ((struct lysc_type_union *)leaf->type)->types;
2810 count = LY_ARRAY_COUNT(t);
2811 } else {
2812 t = &leaf->type;
2813 count = 1;
2814 }
2815 for (u = 0; u < count; ++u) {
2816 if (t[u]->basetype == LY_TYPE_EMPTY) {
2817 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
2818 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
2819 return LY_EVALID;
2820 }
2821 } else if (!in_unres && ((t[u]->basetype == LY_TYPE_BITS) || (t[u]->basetype == LY_TYPE_ENUM))) {
2822 /* store in unres for all disabled bits/enums to be removed */
2823 LY_CHECK_RET(lysc_unres_bitenum_add(ctx, leaf));
2824 in_unres = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002825 }
2826 }
2827
2828 return LY_SUCCESS;
2829}
2830
2831/**
2832 * @brief Compile parsed leaf node information.
2833 * @param[in] ctx Compile context
2834 * @param[in] pnode Parsed leaf node.
2835 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2836 * is enriched with the leaf-specific information.
2837 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2838 */
2839static LY_ERR
2840lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2841{
2842 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2843 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002844 LY_ERR ret = LY_SUCCESS;
2845
Michal Vasko5347e3a2020-11-03 17:14:57 +01002846 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002847
2848 /* add must(s) to unres */
2849 ret = lysc_unres_must_add(ctx, node, pnode);
2850 LY_CHECK_GOTO(ret, done);
2851
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002852 if (leaf_p->units) {
2853 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2854 leaf->flags |= LYS_SET_UNITS;
2855 }
2856
2857 /* compile type */
2858 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2859 LY_CHECK_GOTO(ret, done);
2860
2861 /* store/update default value */
2862 if (leaf_p->dflt.str) {
2863 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2864 leaf->flags |= LYS_SET_DFLT;
2865 }
2866
2867 /* checks */
2868 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002869 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory leaf with a default value.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002870 return LY_EVALID;
2871 }
2872
2873done:
2874 return ret;
2875}
2876
2877/**
2878 * @brief Compile parsed leaf-list node information.
2879 * @param[in] ctx Compile context
2880 * @param[in] pnode Parsed leaf-list node.
2881 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2882 * is enriched with the leaf-list-specific information.
2883 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2884 */
2885static LY_ERR
2886lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2887{
2888 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2889 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002890 LY_ERR ret = LY_SUCCESS;
2891
Michal Vasko5347e3a2020-11-03 17:14:57 +01002892 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vaskoc130e162021-10-19 11:30:00 +02002893
2894 /* add must(s) to unres */
2895 ret = lysc_unres_must_add(ctx, node, pnode);
2896 LY_CHECK_GOTO(ret, done);
2897
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002898 if (llist_p->units) {
2899 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2900 llist->flags |= LYS_SET_UNITS;
2901 }
2902
2903 /* compile type */
2904 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2905 LY_CHECK_GOTO(ret, done);
2906
2907 /* store/update default values */
2908 if (llist_p->dflts) {
2909 if (ctx->pmod->version < LYS_VERSION_1_1) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02002910 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list default values are allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002911 return LY_EVALID;
2912 }
2913
2914 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2915 llist->flags |= LYS_SET_DFLT;
2916 }
2917
2918 llist->min = llist_p->min;
2919 if (llist->min) {
2920 llist->flags |= LYS_MAND_TRUE;
2921 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002922 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
2923
2924 if (llist->flags & LYS_CONFIG_R) {
2925 /* state leaf-list is always ordered-by user */
2926 llist->flags &= ~LYS_ORDBY_SYSTEM;
2927 llist->flags |= LYS_ORDBY_USER;
2928 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002929
2930 /* checks */
2931 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002932 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 +02002933 return LY_EVALID;
2934 }
2935
2936 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002937 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002938 llist->min, llist->max);
2939 return LY_EVALID;
2940 }
2941
2942done:
2943 return ret;
2944}
2945
Michal Vaskob5a487d2021-12-14 10:31:04 +01002946/**
2947 * @brief Find the node according to the given descendant/absolute schema nodeid.
2948 * Used in unique, refine and augment statements.
2949 *
2950 * @param[in] ctx Compile context
2951 * @param[in] nodeid Descendant-schema-nodeid (according to the YANG grammar)
2952 * @param[in] nodeid_len Length of the given nodeid, if it is not NULL-terminated string.
2953 * @param[in] ctx_node Context node for a relative nodeid.
Michal Vaskob5a487d2021-12-14 10:31:04 +01002954 * @param[in] format Format of any prefixes.
2955 * @param[in] prefix_data Format-specific prefix data (see ::ly_resolve_prefix).
2956 * @param[in] nodetype Optional (can be 0) restriction for target's nodetype. If target exists, but does not match
2957 * the given nodetype, LY_EDENIED is returned (and target is provided), but no error message is printed.
2958 * The value can be even an ORed value to allow multiple nodetypes.
2959 * @param[out] target Found target node if any.
2960 * @param[out] result_flag Output parameter to announce if the schema nodeid goes through the action's input/output or a Notification.
2961 * The LYSC_OPT_RPC_INPUT, LYSC_OPT_RPC_OUTPUT and LYSC_OPT_NOTIFICATION are used as flags.
2962 * @return LY_ERR values - LY_ENOTFOUND, LY_EVALID, LY_EDENIED or LY_SUCCESS.
2963 */
2964static LY_ERR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002965lysc_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 +01002966 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 +02002967{
2968 LY_ERR ret = LY_EVALID;
2969 const char *name, *prefix, *id;
2970 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002971 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002972 const char *nodeid_type;
2973 uint32_t getnext_extra_flag = 0;
2974 uint16_t current_nodetype = 0;
2975
2976 assert(nodeid);
2977 assert(target);
2978 assert(result_flag);
2979 *target = NULL;
2980 *result_flag = 0;
2981
2982 id = nodeid;
2983
2984 if (ctx_node) {
2985 /* descendant-schema-nodeid */
2986 nodeid_type = "descendant";
2987
2988 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002989 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002990 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002991 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002992 return LY_EVALID;
2993 }
2994 } else {
2995 /* absolute-schema-nodeid */
2996 nodeid_type = "absolute";
2997
2998 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002999 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003000 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003001 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003002 return LY_EVALID;
3003 }
3004 ++id;
3005 }
3006
3007 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
3008 if (prefix) {
3009 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
3010 if (!mod) {
3011 /* module must always be found */
3012 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01003013 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003014 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01003015 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003016 return LY_ENOTFOUND;
3017 }
3018 } else {
3019 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003020 case LY_VALUE_SCHEMA:
3021 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003022 /* use the current module */
Michal Vasko56d8da82021-12-16 15:41:30 +01003023 mod = ctx->cur_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003024 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02003025 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02003026 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003027 if (!ctx_node) {
3028 LOGINT_RET(ctx->ctx);
3029 }
3030 /* inherit the module of the previous context node */
3031 mod = ctx_node->module;
3032 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02003033 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02003034 case LY_VALUE_XML:
Michal Vaskoddd76592022-01-17 13:34:48 +01003035 case LY_VALUE_STR_NS:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003036 /* not really defined */
3037 LOGINT_RET(ctx->ctx);
3038 }
3039 }
3040
3041 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
3042 /* move through input/output manually */
3043 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01003044 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
3045 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003046 return LY_ENOTFOUND;
3047 }
3048 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003049 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003050 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003051 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003052 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
3053 } else {
3054 /* only input or output is valid */
3055 ctx_node = NULL;
3056 }
3057 } 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 */
3301 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3302 key->flags, key->module, key->name));
3303
3304 /* ignore default values of the key */
3305 if (key->dflt) {
3306 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3307 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3308 free(key->dflt);
3309 key->dflt = NULL;
3310 }
3311 /* mark leaf as key */
3312 key->flags |= LYS_KEY;
3313
3314 /* move it to the correct position */
3315 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3316 /* fix links in closest previous siblings of the key */
3317 if (key->next) {
3318 key->next->prev = key->prev;
3319 } else {
3320 /* last child */
3321 list->child->prev = key->prev;
3322 }
3323 if (key->prev->next) {
3324 key->prev->next = key->next;
3325 }
3326 /* fix links in the key */
3327 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003328 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003329 key->next = prev_key->next;
3330 } else {
3331 key->prev = list->child->prev;
3332 key->next = list->child;
3333 }
3334 /* fix links in closes future siblings of the key */
3335 if (prev_key) {
3336 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003337 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003338 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003339 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003340 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003341 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003342 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003343 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003344 }
3345 /* fix links in parent */
3346 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003347 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003348 }
3349 }
3350
3351 /* next key value */
3352 prev_key = key;
3353 keystr = delim;
3354 lysc_update_path(ctx, NULL, NULL);
3355 }
3356
Michal Vasko95f736c2022-06-08 12:03:31 +02003357 /* connect any augments */
3358 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3359
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003360 /* uniques */
3361 if (list_p->uniques) {
3362 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3363 }
3364
Radek Krejci2a9fc652021-01-22 17:44:34 +01003365 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3366 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3367 LY_CHECK_GOTO(ret, done);
3368 }
3369 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3370 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3371 LY_CHECK_GOTO(ret, done);
3372 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003373
3374 /* checks */
3375 if (list->min > list->max) {
Michal Vasko6b8c5102021-10-19 11:29:32 +02003376 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 +02003377 return LY_EVALID;
3378 }
3379
3380done:
3381 return ret;
3382}
3383
3384/**
3385 * @brief Do some checks and set the default choice's case.
3386 *
3387 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3388 *
3389 * @param[in] ctx Compile context.
3390 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3391 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3392 * @return LY_ERR value.
3393 */
3394static LY_ERR
3395lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3396{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003397 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003398 const struct lys_module *mod;
3399 const char *prefix = NULL, *name;
3400 size_t prefix_len = 0;
3401
3402 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3403 name = strchr(dflt->str, ':');
3404 if (name) {
3405 prefix = dflt->str;
3406 prefix_len = name - prefix;
3407 ++name;
3408 } else {
3409 name = dflt->str;
3410 }
3411 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003412 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003413 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003414 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003415 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003416 return LY_EVALID;
3417 }
3418 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003419 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003420 }
3421
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003422 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 +02003423 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003424 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003425 "Default case \"%s\" not found.", dflt->str);
3426 return LY_EVALID;
3427 }
3428
3429 /* no mandatory nodes directly under the default case */
3430 LY_LIST_FOR(ch->dflt->child, iter) {
3431 if (iter->parent != (struct lysc_node *)ch->dflt) {
3432 break;
3433 }
3434 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003435 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003436 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3437 return LY_EVALID;
3438 }
3439 }
3440
3441 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003442 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003443 return LY_EVALID;
3444 }
3445
3446 ch->dflt->flags |= LYS_SET_DFLT;
3447 return LY_SUCCESS;
3448}
3449
3450LY_ERR
3451lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3452 struct ly_set *child_set)
3453{
3454 LY_ERR ret = LY_SUCCESS;
3455 struct lysp_node *child_p_next = child_p->next;
3456 struct lysp_node_case *cs_p;
Michal Vasko4eb49d52022-02-17 15:05:00 +01003457 struct lysc_node_case *cs_c;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003458
3459 if (child_p->nodetype == LYS_CASE) {
3460 /* standard case under choice */
3461 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3462 } else {
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003463 /* we need the implicit case first, so create a fake parsed (shorthand) case */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003464 cs_p = calloc(1, sizeof *cs_p);
3465 cs_p->nodetype = LYS_CASE;
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003466 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003467 cs_p->child = child_p;
3468
3469 /* make the child the only case child */
3470 child_p->next = NULL;
3471
3472 /* compile it normally */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003473 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 +02003474
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003475 if (((struct lysc_node_choice *)node)->cases) {
Michal Vasko4eb49d52022-02-17 15:05:00 +01003476 /* find our case node */
3477 cs_c = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases;
3478 while (cs_c->name != cs_p->name) {
3479 cs_c = (struct lysc_node_case *)cs_c->next;
3480 assert(cs_c);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003481 }
aPiecekaa320c92021-05-21 07:34:24 +02003482
Michal Vasko4eb49d52022-02-17 15:05:00 +01003483 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
3484 /* compiled case node cannot point to his corresponding parsed node
3485 * because it exists temporarily so it must be set to NULL
3486 */
3487 assert(cs_c->priv == cs_p);
3488 cs_c->priv = NULL;
3489 }
3490
3491 /* status is copied from his child and not from his parent as usual. */
3492 if (cs_c->child) {
3493 cs_c->flags &= ~LYS_STATUS_MASK;
3494 cs_c->flags |= (LYS_STATUS_MASK & cs_c->child->flags);
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003495 }
3496 } /* else it was removed by a deviation */
aPiecekaa320c92021-05-21 07:34:24 +02003497
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003498revert_sh_case:
3499 /* free the parsed shorthand case and correct pointers back */
aPiecekaa320c92021-05-21 07:34:24 +02003500 cs_p->child = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003501 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3502 child_p->next = child_p_next;
3503 }
3504
3505 return ret;
3506}
3507
3508/**
3509 * @brief Compile parsed choice node information.
3510 *
3511 * @param[in] ctx Compile context
3512 * @param[in] pnode Parsed choice node.
3513 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3514 * is enriched with the choice-specific information.
3515 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3516 */
3517static LY_ERR
3518lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3519{
3520 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3521 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3522 struct lysp_node *child_p;
3523 LY_ERR ret = LY_SUCCESS;
3524
3525 assert(node->nodetype == LYS_CHOICE);
3526
3527 LY_LIST_FOR(ch_p->child, child_p) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003528 LY_CHECK_GOTO(ret = lys_compile_node_choice_child(ctx, child_p, node, NULL), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003529 }
3530
Michal Vasko95f736c2022-06-08 12:03:31 +02003531 /* connect any augments */
3532 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), done);
3533
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003534 /* default branch */
3535 if (ch_p->dflt.str) {
Michal Vasko95f736c2022-06-08 12:03:31 +02003536 LY_CHECK_GOTO(ret = lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch), done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003537 }
3538
Michal Vasko95f736c2022-06-08 12:03:31 +02003539done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003540 return ret;
3541}
3542
3543/**
3544 * @brief Compile parsed anydata or anyxml node information.
3545 * @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 Vasko1a7a7bd2020-10-16 14:39:15 +02003566 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3567 }
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.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003637 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003638 * @param[in] name Name of the grouping to find,
3639 * @return NULL when there is no grouping with the specified name
3640 * @return Pointer to the grouping of the specified @p name.
3641 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003642static struct lysp_node_grp *
3643match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003644{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003645 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003646
Radek Krejci2a9fc652021-01-22 17:44:34 +01003647 LY_LIST_FOR(grps, grp) {
3648 if (!strcmp(grp->name, name)) {
3649 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003650 }
3651 }
3652
3653 return NULL;
3654}
3655
3656/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003657 * @brief Find grouping for a uses.
3658 *
3659 * @param[in] ctx Compile context.
3660 * @param[in] uses_p Parsed uses node.
3661 * @param[out] gpr_p Found grouping on success.
3662 * @param[out] grp_pmod Module of @p grp_p on success.
3663 * @return LY_ERR value.
3664 */
3665static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003666lys_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 +02003667 struct lysp_module **grp_pmod)
3668{
3669 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003670 struct lysp_node_grp *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) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003688 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3689 found = ctx->pmod;
3690 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003691 }
3692 }
3693 } else {
3694 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003695 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003696 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003697 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003698 "Invalid prefix used for grouping reference.", uses_p->name);
3699 return LY_EVALID;
3700 }
3701 pmod = mod->parsed;
3702 }
3703
3704 if (!found) {
3705 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003706 if ((grp = match_grouping(pmod->groupings, name))) {
3707 found = pmod;
3708 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003709 /* ... and all the submodules */
3710 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003711 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3712 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003713 break;
3714 }
3715 }
3716 }
3717 }
3718 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003719 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003720 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3721 return LY_EVALID;
3722 }
3723
Michal Vasko7c565922021-06-10 14:58:27 +02003724 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003725 /* remember that the grouping is instantiated to avoid its standalone validation */
3726 grp->flags |= LYS_USED_GRP;
3727 }
3728
3729 *grp_p = grp;
3730 *grp_pmod = found;
3731 return LY_SUCCESS;
3732}
3733
3734/**
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003735 * @brief Compile uses grouping children.
3736 *
3737 * @param[in] ctx Compile context.
3738 * @param[in] uses_p Parsed uses.
3739 * @param[in] uses_flags Special uses flags to use.
3740 * @param[in] child First grouping child to compile.
3741 * @param[in] grp_mod Grouping parsed module.
3742 * @param[in] parent Uses compiled parent, may be NULL if top-level.
3743 * @param[in,out] child_set Set of all compiled child nodes.
3744 * @param[in] child_unres_disabled Whether the children are to be put into unres disabled set or not.
3745 * @return LY_SUCCESS on success.
3746 * @return LY_EVALID on failure.
3747 */
3748static LY_ERR
3749lys_compile_uses_children(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, uint16_t uses_flags, struct lysp_node *child,
3750 struct lysp_module *grp_mod, struct lysc_node *parent, struct ly_set *child_set, ly_bool child_unres_disabled)
3751{
3752 LY_ERR rc = LY_SUCCESS;
3753 struct lysp_module *mod_old = ctx->pmod;
3754 uint32_t child_i, opt_prev = ctx->compile_opts;
3755 ly_bool enabled;
3756 struct lysp_node *pnode;
3757 struct lysc_node *node;
3758 struct lysc_when *when_shared = NULL;
3759
3760 assert(child_set);
3761
Michal Vasko5940c302022-07-14 13:54:38 +02003762 child_i = child_set->count;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003763 LY_LIST_FOR(child, pnode) {
3764 /* compile the nodes with their parsed (grouping) module */
3765 ctx->pmod = grp_mod;
3766 LY_CHECK_GOTO(rc = lys_compile_node(ctx, pnode, parent, uses_flags, child_set), cleanup);
3767
3768 /* eval if-features again for the rest of this node processing */
3769 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), cleanup);
3770 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3771 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3772 }
3773
3774 /* restore the parsed module */
3775 ctx->pmod = mod_old;
3776
3777 /* since the uses node is not present in the compiled tree, we need to pass some of its
3778 * statements to all its children */
3779 while (child_i < child_set->count) {
3780 node = child_set->snodes[child_i];
3781
3782 if (uses_p->when) {
3783 /* pass uses when to all the children */
3784 rc = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), node, &when_shared);
3785 LY_CHECK_GOTO(rc, cleanup);
3786 }
3787
3788 if (child_unres_disabled) {
3789 /* child is disabled by the uses if-features */
3790 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
3791 }
3792
3793 /* child processed */
3794 ++child_i;
3795 }
3796
3797 /* next iter */
3798 ctx->compile_opts = opt_prev;
3799 }
3800
3801cleanup:
3802 ctx->compile_opts = opt_prev;
3803 return rc;
3804}
3805
3806/**
Michal Vaskodfd254d2021-06-24 09:24:59 +02003807 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
3808 */
3809#define LYS_STATUS_USES LYS_CONFIG_MASK
3810
3811/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003812 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3813 * If present, also apply uses's modificators.
3814 *
3815 * @param[in] ctx Compile context
3816 * @param[in] uses_p Parsed uses schema node.
3817 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3818 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3819 * the compile context.
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003820 * @param[in] child_set Optional set of all the compiled children.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003821 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3822 */
3823static LY_ERR
3824lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3825{
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003826 LY_ERR rc = LY_SUCCESS;
3827 ly_bool enabled, child_unres_disabled = 0;
3828 uint32_t i, grp_stack_count, opt_prev = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003829 struct lysp_node_grp *grp = NULL;
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003830 uint16_t uses_flags;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003831 struct lysp_module *grp_mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003832 struct ly_set uses_child_set = {0};
3833
3834 /* find the referenced grouping */
3835 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3836
3837 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3838 grp_stack_count = ctx->groupings.count;
3839 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3840 if (grp_stack_count == ctx->groupings.count) {
3841 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003842 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003843 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3844 return LY_EVALID;
3845 }
3846
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003847 /* nodetype checks */
3848 if (grp->actions && (parent && !lysc_node_actions_p(parent))) {
3849 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3850 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3851 parent->name, lys_nodetype2str(parent->nodetype));
3852 rc = LY_EVALID;
3853 goto cleanup;
3854 }
3855 if (grp->notifs && (parent && !lysc_node_notifs_p(parent))) {
3856 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
3857 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3858 parent->name, lys_nodetype2str(parent->nodetype));
3859 rc = LY_EVALID;
3860 goto cleanup;
3861 }
3862
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003863 /* check status */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003864 rc = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3865 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003866
3867 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003868 rc = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3869 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003870
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003871 /* compile special uses status flags */
3872 uses_flags = uses_p->flags;
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003873 rc = lys_compile_status(ctx, &uses_flags, "<uses>", parent ? parent->flags : 0, parent ? parent->name : NULL, 0);
3874 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003875 uses_flags |= LYS_STATUS_USES;
3876
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003877 /* uses if-features */
3878 LY_CHECK_GOTO(rc = lys_eval_iffeatures(ctx->ctx, uses_p->iffeatures, &enabled), cleanup);
3879 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
3880 ctx->compile_opts |= LYS_COMPILE_DISABLED;
3881 child_unres_disabled = 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003882 }
3883
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003884 /* uses grouping children */
3885 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, grp->child, grp_mod, parent,
3886 child_set ? child_set : &uses_child_set, child_unres_disabled);
3887 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003888
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003889 /* uses grouping RPCs/actions */
3890 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->actions, grp_mod, parent,
3891 child_set ? child_set : &uses_child_set, child_unres_disabled);
3892 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003893
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003894 /* uses grouping notifications */
3895 rc = lys_compile_uses_children(ctx, uses_p, uses_flags, (struct lysp_node *)grp->notifs, grp_mod, parent,
3896 child_set ? child_set : &uses_child_set, child_unres_disabled);
3897 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003898
3899 /* check that all augments were applied */
3900 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003901 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3902 /* augment of some parent uses, irrelevant now */
3903 continue;
3904 }
3905
3906 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003907 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003908 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003909 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003910 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003911
3912 /* check that all refines were applied */
3913 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003914 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003915 /* refine of some parent uses, irrelevant now */
Michal Vaskod8655722021-01-12 15:20:36 +01003916 continue;
3917 }
3918
3919 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003920 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003921 rc = LY_ENOTFOUND;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003922 }
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003923 LY_CHECK_GOTO(rc, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003924
3925cleanup:
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003926 /* restore previous context */
3927 ctx->compile_opts = opt_prev;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003928
3929 /* remove the grouping from the stack for circular groupings dependency check */
3930 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3931 assert(ctx->groupings.count == grp_stack_count);
3932
3933 ly_set_erase(&uses_child_set, NULL);
Michal Vaskobf8e65d2022-05-30 09:27:49 +02003934 return rc;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003935}
3936
3937static int
3938lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3939{
3940 struct lysp_node *iter;
3941 int len = 0;
3942
3943 *path = NULL;
3944 for (iter = node; iter && len >= 0; iter = iter->parent) {
3945 char *s = *path;
3946 char *id;
3947
3948 switch (iter->nodetype) {
3949 case LYS_USES:
3950 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3951 break;
3952 case LYS_GROUPING:
3953 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3954 break;
3955 case LYS_AUGMENT:
3956 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3957 break;
3958 default:
3959 id = strdup(iter->name);
3960 break;
3961 }
3962
3963 if (!iter->parent) {
3964 /* print prefix */
3965 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3966 } else {
3967 /* prefix is the same as in parent */
3968 len = asprintf(path, "/%s%s", id, s ? s : "");
3969 }
3970 free(s);
3971 free(id);
3972 }
3973
3974 if (len < 0) {
3975 free(*path);
3976 *path = NULL;
3977 } else if (len == 0) {
3978 *path = strdup("/");
3979 len = 1;
3980 }
3981 return len;
3982}
3983
3984LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003985lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003986{
3987 LY_ERR ret;
3988 char *path;
3989 int len;
Michal Vaskoecdc2222022-01-24 08:45:44 +01003990 uint16_t cont_flags;
3991
3992 cont_flags = pnode ? pnode->flags & LYS_FLAGS_COMPILED_MASK : 0;
3993 if (!(cont_flags & LYS_CONFIG_MASK)) {
3994 /* default config */
3995 cont_flags |= LYS_CONFIG_W;
3996 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003997
Michal Vasko8254d852022-04-25 10:05:59 +02003998 /* use grouping status to avoid errors */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003999 struct lysp_node_uses fake_uses = {
4000 .parent = pnode,
4001 .nodetype = LYS_USES,
Michal Vasko8254d852022-04-25 10:05:59 +02004002 .flags = grp->flags & LYS_STATUS_MASK, .next = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004003 .name = grp->name,
4004 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
4005 .refines = NULL, .augments = NULL
4006 };
4007 struct lysc_node_container fake_container = {
4008 .nodetype = LYS_CONTAINER,
Michal Vaskoecdc2222022-01-24 08:45:44 +01004009 .flags = cont_flags,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004010 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02004011 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01004012 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004013 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004014 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004015 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
4016 };
4017
4018 if (grp->parent) {
4019 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
4020 }
4021
4022 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
4023 if (len < 0) {
4024 LOGMEM(ctx->ctx);
4025 return LY_EMEM;
4026 }
4027 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
4028 ctx->path_len = (uint32_t)len;
4029 free(path);
4030
4031 lysc_update_path(ctx, NULL, "{grouping}");
4032 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01004033 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004034 lysc_update_path(ctx, NULL, NULL);
4035 lysc_update_path(ctx, NULL, NULL);
4036
4037 ctx->path_len = 1;
4038 ctx->path[1] = '\0';
4039
4040 /* cleanup */
4041 lysc_node_container_free(ctx->ctx, &fake_container);
4042
4043 return ret;
4044}
4045
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004046LY_ERR
4047lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
4048 struct ly_set *child_set)
4049{
4050 LY_ERR ret = LY_SUCCESS;
4051 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02004052 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004053
4054 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
4055
4056 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01004057 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004058 } else {
4059 lysc_update_path(ctx, NULL, "{uses}");
4060 lysc_update_path(ctx, NULL, pnode->name);
4061 }
4062
4063 switch (pnode->nodetype) {
4064 case LYS_CONTAINER:
4065 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
4066 node_compile_spec = lys_compile_node_container;
4067 break;
4068 case LYS_LEAF:
4069 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
4070 node_compile_spec = lys_compile_node_leaf;
4071 break;
4072 case LYS_LIST:
4073 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
4074 node_compile_spec = lys_compile_node_list;
4075 break;
4076 case LYS_LEAFLIST:
4077 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
4078 node_compile_spec = lys_compile_node_leaflist;
4079 break;
4080 case LYS_CHOICE:
4081 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
4082 node_compile_spec = lys_compile_node_choice;
4083 break;
4084 case LYS_CASE:
4085 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
4086 node_compile_spec = lys_compile_node_case;
4087 break;
4088 case LYS_ANYXML:
4089 case LYS_ANYDATA:
4090 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
4091 node_compile_spec = lys_compile_node_any;
4092 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004093 case LYS_RPC:
4094 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02004095 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004096 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4097 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004098 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004099 return LY_EVALID;
4100 }
4101 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
4102 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02004103 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004104 break;
4105 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02004106 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01004107 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
4108 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02004109 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01004110 return LY_EVALID;
4111 }
4112 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
4113 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02004114 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01004115 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004116 case LYS_USES:
4117 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
4118 lysc_update_path(ctx, NULL, NULL);
4119 lysc_update_path(ctx, NULL, NULL);
4120 return ret;
4121 default:
4122 LOGINT(ctx->ctx);
4123 return LY_EINT;
4124 }
4125 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
4126
Radek Krejci2a9fc652021-01-22 17:44:34 +01004127 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004128
Michal Vasko7c565922021-06-10 14:58:27 +02004129 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01004130 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02004131 return ret;
4132}