blob: 6529a0e2b2854d77532ef1f637fb457c1f4654c4 [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/**
56 * @brief Add/replace a leaf default value in unres.
57 * Can also be used for a single leaf-list default value.
58 *
59 * @param[in] ctx Compile context.
60 * @param[in] leaf Leaf with the default value.
61 * @param[in] dflt Default value to use.
62 * @return LY_ERR value.
63 */
64static LY_ERR
65lysc_unres_leaf_dflt_add(struct lysc_ctx *ctx, struct lysc_node_leaf *leaf, struct lysp_qname *dflt)
66{
67 struct lysc_unres_dflt *r = NULL;
68 uint32_t i;
69
Michal Vasko7c565922021-06-10 14:58:27 +020070 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +010071 return LY_SUCCESS;
72 }
73
Michal Vasko405cc9e2020-12-01 12:01:27 +010074 for (i = 0; i < ctx->unres->dflts.count; ++i) {
75 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->leaf == leaf) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020076 /* just replace the default */
Michal Vasko405cc9e2020-12-01 12:01:27 +010077 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020078 lysp_qname_free(ctx->ctx, r->dflt);
79 free(r->dflt);
80 break;
81 }
82 }
83 if (!r) {
84 /* add new unres item */
85 r = calloc(1, sizeof *r);
86 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
87 r->leaf = leaf;
88
Michal Vasko405cc9e2020-12-01 12:01:27 +010089 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +020090 }
91
92 r->dflt = malloc(sizeof *r->dflt);
93 LY_CHECK_GOTO(!r->dflt, error);
94 LY_CHECK_GOTO(lysp_qname_dup(ctx->ctx, r->dflt, dflt), error);
95
96 return LY_SUCCESS;
97
98error:
99 free(r->dflt);
100 LOGMEM(ctx->ctx);
101 return LY_EMEM;
102}
103
104/**
105 * @brief Add/replace a leaf-list default value(s) in unres.
106 *
107 * @param[in] ctx Compile context.
108 * @param[in] llist Leaf-list with the default value.
109 * @param[in] dflts Sized array of the default values.
110 * @return LY_ERR value.
111 */
112static LY_ERR
113lysc_unres_llist_dflts_add(struct lysc_ctx *ctx, struct lysc_node_leaflist *llist, struct lysp_qname *dflts)
114{
115 struct lysc_unres_dflt *r = NULL;
116 uint32_t i;
117
Michal Vasko7c565922021-06-10 14:58:27 +0200118 if (ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING)) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +0100119 return LY_SUCCESS;
120 }
121
Michal Vasko405cc9e2020-12-01 12:01:27 +0100122 for (i = 0; i < ctx->unres->dflts.count; ++i) {
123 if (((struct lysc_unres_dflt *)ctx->unres->dflts.objs[i])->llist == llist) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200124 /* just replace the defaults */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100125 r = ctx->unres->dflts.objs[i];
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200126 lysp_qname_free(ctx->ctx, r->dflt);
127 free(r->dflt);
128 r->dflt = NULL;
129 FREE_ARRAY(ctx->ctx, r->dflts, lysp_qname_free);
130 r->dflts = NULL;
131 break;
132 }
133 }
134 if (!r) {
135 r = calloc(1, sizeof *r);
136 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
137 r->llist = llist;
138
Michal Vasko405cc9e2020-12-01 12:01:27 +0100139 LY_CHECK_RET(ly_set_add(&ctx->unres->dflts, r, 1, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200140 }
141
142 DUP_ARRAY(ctx->ctx, dflts, r->dflts, lysp_qname_dup);
143
144 return LY_SUCCESS;
145}
146
147/**
148 * @brief Duplicate the compiled pattern structure.
149 *
150 * Instead of duplicating memory, the reference counter in the @p orig is increased.
151 *
152 * @param[in] orig The pattern structure to duplicate.
153 * @return The duplicated structure to use.
154 */
155static struct lysc_pattern *
156lysc_pattern_dup(struct lysc_pattern *orig)
157{
158 ++orig->refcount;
159 return orig;
160}
161
162/**
163 * @brief Duplicate the array of compiled patterns.
164 *
165 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
166 *
167 * @param[in] ctx Libyang context for logging.
168 * @param[in] orig The patterns sized array to duplicate.
169 * @return New sized array as a copy of @p orig.
170 * @return NULL in case of memory allocation error.
171 */
172static struct lysc_pattern **
173lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
174{
175 struct lysc_pattern **dup = NULL;
176 LY_ARRAY_COUNT_TYPE u;
177
178 assert(orig);
179
180 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_COUNT(orig), NULL);
181 LY_ARRAY_FOR(orig, u) {
182 dup[u] = lysc_pattern_dup(orig[u]);
183 LY_ARRAY_INCREMENT(dup);
184 }
185 return dup;
186}
187
188/**
189 * @brief Duplicate compiled range structure.
190 *
191 * @param[in] ctx Libyang context for logging.
192 * @param[in] orig The range structure to be duplicated.
193 * @return New compiled range structure as a copy of @p orig.
194 * @return NULL in case of memory allocation error.
195 */
196static struct lysc_range *
197lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
198{
199 struct lysc_range *dup;
200 LY_ERR ret;
201
202 assert(orig);
203
204 dup = calloc(1, sizeof *dup);
205 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
206 if (orig->parts) {
207 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
Michal Vasko79135ae2020-12-16 10:08:35 +0100208 (*((LY_ARRAY_COUNT_TYPE *)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200209 memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
210 }
211 DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
212 DUP_STRING_GOTO(ctx, orig->emsg, dup->emsg, ret, cleanup);
213 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
214
215 return dup;
216cleanup:
217 free(dup);
218 (void) ret; /* set but not used due to the return type */
219 return NULL;
220}
221
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200222/**
Michal Vaskodfd254d2021-06-24 09:24:59 +0200223 * @brief Compile status information of the given statement.
224 *
225 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
226 * has the status correctly set during the compilation.
227 *
228 * @param[in] ctx Compile context
229 * @param[in,out] stmt_flags Compiled flags to update. If the status was set explicitly, it is already set
230 * in the flags value and we just check the compatibility with the parent's status value.
231 * @param[in] stmt_name Statement name, for logging.
232 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
233 * @param[in] parent_name Name of the parent node, for logging.
234 * @param[in] inherit_log Whether to print inherit message.
235 * @return LY_ERR value.
236 */
237static LY_ERR
238lys_compile_status(struct lysc_ctx *ctx, uint16_t *stmt_flags, const char *stmt_name, uint16_t parent_flags,
239 const char *parent_name, ly_bool inherit_log)
240{
241 /* status - it is not inherited by specification, but it does not make sense to have
242 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
243 if (!(*stmt_flags & LYS_STATUS_MASK)) {
244 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
245 if (inherit_log) {
246 LOGVRB("Missing explicit \"%s\" status specified in parent \"%s\", inheriting for \"%s\".",
247 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name, stmt_name);
248 }
249 *stmt_flags |= parent_flags & LYS_STATUS_MASK;
250 } else {
251 *stmt_flags |= LYS_STATUS_CURR;
252 }
253 } else if (parent_flags & LYS_STATUS_MASK) {
254 /* check status compatibility with the parent */
255 if ((parent_flags & LYS_STATUS_MASK) > (*stmt_flags & LYS_STATUS_MASK)) {
256 if (*stmt_flags & LYS_STATUS_CURR) {
257 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
258 "Status \"current\" of \"%s\" is in conflict with the \"%s\" status of parent \"%s\".",
259 stmt_name, (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete", parent_name);
260 } else { /* LYS_STATUS_DEPRC */
261 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
262 "Status \"deprecated\" of \"%s\" is in conflict with the \"obsolete\" status of parent \"%s\".",
263 stmt_name, parent_name);
264 }
265 return LY_EVALID;
266 }
267 }
268 return LY_SUCCESS;
269}
270
271/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200272 * @brief Compile information from the when statement
273 *
274 * @param[in] ctx Compile context.
275 * @param[in] when_p Parsed when structure.
Michal Vaskodfd254d2021-06-24 09:24:59 +0200276 * @param[in] parent_flags Flags of the parsed node with the when statement.
277 * @param[in] compiled_parent Closest compiled parent of the when statement.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200278 * @param[in] ctx_node Context node for the when statement.
279 * @param[out] when Pointer where to store pointer to the created compiled when structure.
280 * @return LY_ERR value.
281 */
282static LY_ERR
Michal Vaskodfd254d2021-06-24 09:24:59 +0200283lys_compile_when_(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t parent_flags,
284 const struct lysc_node *compiled_parent, const struct lysc_node *ctx_node, struct lysc_when **when)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200285{
286 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200287 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200288
289 *when = calloc(1, sizeof **when);
290 LY_CHECK_ERR_RET(!(*when), LOGMEM(ctx->ctx), LY_EMEM);
291 (*when)->refcount = 1;
292 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, when_p->cond, 0, 1, &(*when)->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200293 LY_CHECK_RET(lyplg_type_prefix_data_new(ctx->ctx, when_p->cond, strlen(when_p->cond),
Radek Krejci8df109d2021-04-23 12:19:08 +0200294 LY_VALUE_SCHEMA, ctx->pmod, &format, (void **)&(*when)->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200295 (*when)->context = (struct lysc_node *)ctx_node;
296 DUP_STRING_GOTO(ctx->ctx, when_p->dsc, (*when)->dsc, ret, done);
297 DUP_STRING_GOTO(ctx->ctx, when_p->ref, (*when)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100298 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), ret, done);
Michal Vaskodfd254d2021-06-24 09:24:59 +0200299 (*when)->flags = (parent_flags & LYS_STATUS_MASK);
300 if (compiled_parent) {
301 LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", compiled_parent->flags, compiled_parent->name, 0));
302 } else {
303 LY_CHECK_RET(lys_compile_status(ctx, &(*when)->flags, "when", 0, NULL, 0));
304 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200305
306done:
307 return ret;
308}
309
310LY_ERR
Michal Vaskodfd254d2021-06-24 09:24:59 +0200311lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t parent_flags, const struct lysc_node *compiled_parent,
312 const struct lysc_node *ctx_node, struct lysc_node *node, struct lysc_when **when_c)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200313{
314 struct lysc_when **new_when, ***node_when;
315
316 assert(when_p);
317
318 /* get the when array */
Radek Krejci9a3823e2021-01-27 20:26:46 +0100319 node_when = lysc_node_when_p(node);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200320
321 /* create new when pointer */
322 LY_ARRAY_NEW_RET(ctx->ctx, *node_when, new_when, LY_EMEM);
323 if (!when_c || !(*when_c)) {
324 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +0200325 LY_CHECK_RET(lys_compile_when_(ctx, when_p, parent_flags, compiled_parent, ctx_node, new_when));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200326
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200327 /* remember the compiled when for sharing */
328 if (when_c) {
329 *when_c = *new_when;
330 }
331 } else {
332 /* use the previously compiled when */
333 ++(*when_c)->refcount;
334 *new_when = *when_c;
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100335 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200336
Michal Vasko6f08e962021-07-23 08:30:47 +0200337 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vaskof01fd0b2020-11-23 16:53:07 +0100338 /* do not check "when" semantics in a grouping, but repeat the check for different node because
339 * of dummy node check */
Michal Vasko405cc9e2020-12-01 12:01:27 +0100340 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, node, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200341 }
342
343 return LY_SUCCESS;
344}
345
346/**
347 * @brief Compile information from the must statement
348 * @param[in] ctx Compile context.
349 * @param[in] must_p The parsed must statement structure.
350 * @param[in,out] must Prepared (empty) compiled must structure to fill.
351 * @return LY_ERR value.
352 */
353static LY_ERR
354lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
355{
356 LY_ERR ret = LY_SUCCESS;
Radek Krejci8df109d2021-04-23 12:19:08 +0200357 LY_VALUE_FORMAT format;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200358
359 LY_CHECK_RET(lyxp_expr_parse(ctx->ctx, must_p->arg.str, 0, 1, &must->cond));
Radek Krejci0b013302021-03-29 15:22:32 +0200360 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 +0200361 LY_VALUE_SCHEMA, must_p->arg.mod, &format, (void **)&must->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200362 DUP_STRING_GOTO(ctx->ctx, must_p->eapptag, must->eapptag, ret, done);
363 DUP_STRING_GOTO(ctx->ctx, must_p->emsg, must->emsg, ret, done);
364 DUP_STRING_GOTO(ctx->ctx, must_p->dsc, must->dsc, ret, done);
365 DUP_STRING_GOTO(ctx->ctx, must_p->ref, must->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +0100366 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200367
368done:
369 return ret;
370}
371
372/**
373 * @brief Validate and normalize numeric value from a range definition.
374 * @param[in] ctx Compile context.
375 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
376 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
377 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
378 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
379 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
380 * @param[in] value String value of the range boundary.
381 * @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.
382 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
383 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
384 */
385static LY_ERR
386range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value,
387 size_t *len, char **valcopy)
388{
389 size_t fraction = 0, size;
390
391 *len = 0;
392
393 assert(value);
394 /* parse value */
395 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
396 return LY_EVALID;
397 }
398
399 if ((value[*len] == '-') || (value[*len] == '+')) {
400 ++(*len);
401 }
402
403 while (isdigit(value[*len])) {
404 ++(*len);
405 }
406
407 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
408 if (basetype == LY_TYPE_DEC64) {
409 goto decimal;
410 } else {
411 *valcopy = strndup(value, *len);
412 return LY_SUCCESS;
413 }
414 }
415 fraction = *len;
416
417 ++(*len);
418 while (isdigit(value[*len])) {
419 ++(*len);
420 }
421
422 if (basetype == LY_TYPE_DEC64) {
423decimal:
424 assert(frdigits);
425 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100426 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200427 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
Radek Krejci422afb12021-03-04 16:38:16 +0100428 (int)(*len), value, frdigits);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200429 return LY_EINVAL;
430 }
431 if (fraction) {
432 size = (*len) + (frdigits - ((*len) - 1 - fraction));
433 } else {
434 size = (*len) + frdigits + 1;
435 }
436 *valcopy = malloc(size * sizeof **valcopy);
437 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
438
439 (*valcopy)[size - 1] = '\0';
440 if (fraction) {
441 memcpy(&(*valcopy)[0], &value[0], fraction);
442 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
443 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
444 } else {
445 memcpy(&(*valcopy)[0], &value[0], *len);
446 memset(&(*valcopy)[*len], '0', frdigits);
447 }
448 }
449 return LY_SUCCESS;
450}
451
452/**
453 * @brief Check that values in range are in ascendant order.
454 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
455 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
456 * max can be also equal.
457 * @param[in] value Current value to check.
458 * @param[in] prev_value The last seen value.
459 * @return LY_SUCCESS or LY_EEXIST for invalid order.
460 */
461static LY_ERR
462range_part_check_ascendancy(ly_bool unsigned_value, ly_bool max, int64_t value, int64_t prev_value)
463{
464 if (unsigned_value) {
465 if ((max && ((uint64_t)prev_value > (uint64_t)value)) || (!max && ((uint64_t)prev_value >= (uint64_t)value))) {
466 return LY_EEXIST;
467 }
468 } else {
469 if ((max && (prev_value > value)) || (!max && (prev_value >= value))) {
470 return LY_EEXIST;
471 }
472 }
473 return LY_SUCCESS;
474}
475
476/**
477 * @brief Set min/max value of the range part.
478 * @param[in] ctx Compile context.
479 * @param[in] part Range part structure to fill.
480 * @param[in] max Flag to distinguish if storing min or max value.
481 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
482 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
483 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
484 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
485 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
486 * @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.
487 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
488 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
489 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
490 * frdigits value), LY_EMEM.
491 */
492static LY_ERR
493range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, ly_bool max, int64_t prev, LY_DATA_TYPE basetype,
494 ly_bool first, ly_bool length_restr, uint8_t frdigits, struct lysc_range *base_range, const char **value)
495{
496 LY_ERR ret = LY_SUCCESS;
497 char *valcopy = NULL;
Radek Krejci2b18bf12020-11-06 11:20:20 +0100498 size_t len = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200499
500 if (value) {
501 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
502 LY_CHECK_GOTO(ret, finalize);
503 }
504 if (!valcopy && base_range) {
505 if (max) {
506 part->max_64 = base_range->parts[LY_ARRAY_COUNT(base_range->parts) - 1].max_64;
507 } else {
508 part->min_64 = base_range->parts[0].min_64;
509 }
510 if (!first) {
511 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
512 }
513 goto finalize;
514 }
515
516 switch (basetype) {
517 case LY_TYPE_INT8: /* range */
518 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100519 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 +0200520 } else if (max) {
521 part->max_64 = INT64_C(127);
522 } else {
523 part->min_64 = INT64_C(-128);
524 }
525 if (!ret && !first) {
526 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
527 }
528 break;
529 case LY_TYPE_INT16: /* range */
530 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100531 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), LY_BASE_DEC,
532 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200533 } else if (max) {
534 part->max_64 = INT64_C(32767);
535 } else {
536 part->min_64 = INT64_C(-32768);
537 }
538 if (!ret && !first) {
539 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
540 }
541 break;
542 case LY_TYPE_INT32: /* range */
543 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100544 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), LY_BASE_DEC,
545 max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200546 } else if (max) {
547 part->max_64 = INT64_C(2147483647);
548 } else {
549 part->min_64 = INT64_C(-2147483648);
550 }
551 if (!ret && !first) {
552 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
553 }
554 break;
555 case LY_TYPE_INT64: /* range */
556 case LY_TYPE_DEC64: /* range */
557 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100558 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807),
559 LY_BASE_DEC, max ? &part->max_64 : &part->min_64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200560 } else if (max) {
561 part->max_64 = INT64_C(9223372036854775807);
562 } else {
563 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
564 }
565 if (!ret && !first) {
566 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
567 }
568 break;
569 case LY_TYPE_UINT8: /* range */
570 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100571 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 +0200572 } else if (max) {
573 part->max_u64 = UINT64_C(255);
574 } else {
575 part->min_u64 = UINT64_C(0);
576 }
577 if (!ret && !first) {
578 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
579 }
580 break;
581 case LY_TYPE_UINT16: /* range */
582 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100583 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 +0200584 } else if (max) {
585 part->max_u64 = UINT64_C(65535);
586 } else {
587 part->min_u64 = UINT64_C(0);
588 }
589 if (!ret && !first) {
590 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
591 }
592 break;
593 case LY_TYPE_UINT32: /* range */
594 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100595 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), LY_BASE_DEC,
596 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200597 } else if (max) {
598 part->max_u64 = UINT64_C(4294967295);
599 } else {
600 part->min_u64 = UINT64_C(0);
601 }
602 if (!ret && !first) {
603 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
604 }
605 break;
606 case LY_TYPE_UINT64: /* range */
607 case LY_TYPE_STRING: /* length */
608 case LY_TYPE_BINARY: /* length */
609 if (valcopy) {
Radek Krejcif13b87b2020-12-01 22:02:17 +0100610 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), LY_BASE_DEC,
611 max ? &part->max_u64 : &part->min_u64);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200612 } else if (max) {
613 part->max_u64 = UINT64_C(18446744073709551615);
614 } else {
615 part->min_u64 = UINT64_C(0);
616 }
617 if (!ret && !first) {
618 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
619 }
620 break;
621 default:
622 LOGINT(ctx->ctx);
623 ret = LY_EINT;
624 }
625
626finalize:
627 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100628 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200629 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
630 length_restr ? "length" : "range", valcopy ? valcopy : *value);
631 } else if (ret == LY_EVALID) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100632 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200633 "Invalid %s restriction - invalid value \"%s\".",
634 length_restr ? "length" : "range", valcopy ? valcopy : *value);
635 } else if (ret == LY_EEXIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100636 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200637 "Invalid %s restriction - values are not in ascending order (%s).",
638 length_restr ? "length" : "range",
639 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
640 } else if (!ret && value) {
641 *value = *value + len;
642 }
643 free(valcopy);
644 return ret;
645}
646
647/**
648 * @brief Compile the parsed range restriction.
649 * @param[in] ctx Compile context.
650 * @param[in] range_p Parsed range structure to compile.
651 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
652 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
653 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
654 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
655 * range restriction must be more restrictive than the base_range.
656 * @param[in,out] range Pointer to the created current range structure.
657 * @return LY_ERR value.
658 */
659static LY_ERR
660lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, ly_bool length_restr,
661 uint8_t frdigits, struct lysc_range *base_range, struct lysc_range **range)
662{
aPiecek1c4da362021-04-29 14:26:34 +0200663 LY_ERR ret = LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200664 const char *expr;
665 struct lysc_range_part *parts = NULL, *part;
666 ly_bool range_expected = 0, uns;
667 LY_ARRAY_COUNT_TYPE parts_done = 0, u, v;
668
669 assert(range);
670 assert(range_p);
671
672 expr = range_p->arg.str;
673 while (1) {
674 if (isspace(*expr)) {
675 ++expr;
676 } else if (*expr == '\0') {
677 if (range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100678 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200679 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
680 length_restr ? "length" : "range", range_p->arg);
aPiecek1c4da362021-04-29 14:26:34 +0200681 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200682 goto cleanup;
683 } else if (!parts || (parts_done == LY_ARRAY_COUNT(parts))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100684 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200685 "Invalid %s restriction - unexpected end of the expression (%s).",
686 length_restr ? "length" : "range", range_p->arg);
aPiecek1c4da362021-04-29 14:26:34 +0200687 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200688 goto cleanup;
689 }
690 parts_done++;
691 break;
Radek Krejcif13b87b2020-12-01 22:02:17 +0100692 } else if (!strncmp(expr, "min", ly_strlen_const("min"))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200693 if (parts) {
694 /* min cannot be used elsewhere than in the first part */
Radek Krejci2efc45b2020-12-22 16:25:44 +0100695 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200696 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
Radek Krejci52409202021-03-15 09:30:43 +0100697 (int)(expr - range_p->arg.str), range_p->arg.str);
aPiecek1c4da362021-04-29 14:26:34 +0200698 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200699 goto cleanup;
700 }
Radek Krejcif13b87b2020-12-01 22:02:17 +0100701 expr += ly_strlen_const("min");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200702
703 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200704 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 +0200705 part->max_64 = part->min_64;
706 } else if (*expr == '|') {
707 if (!parts || range_expected) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100708 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200709 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200710 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200711 goto cleanup;
712 }
713 expr++;
714 parts_done++;
715 /* process next part of the expression */
716 } else if (!strncmp(expr, "..", 2)) {
717 expr += 2;
718 while (isspace(*expr)) {
719 expr++;
720 }
721 if (!parts || (LY_ARRAY_COUNT(parts) == parts_done)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100722 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200723 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
aPiecek1c4da362021-04-29 14:26:34 +0200724 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200725 goto cleanup;
726 }
727 /* continue expecting the upper boundary */
728 range_expected = 1;
729 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
730 /* number */
731 if (range_expected) {
732 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200733 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 +0200734 range_expected = 0;
735 } else {
736 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200737 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 +0200738 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
739 part->max_64 = part->min_64;
740 }
741
742 /* continue with possible another expression part */
Radek Krejcif13b87b2020-12-01 22:02:17 +0100743 } else if (!strncmp(expr, "max", ly_strlen_const("max"))) {
744 expr += ly_strlen_const("max");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200745 while (isspace(*expr)) {
746 expr++;
747 }
748 if (*expr != '\0') {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100749 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200750 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200751 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200752 goto cleanup;
753 }
754 if (range_expected) {
755 part = &parts[LY_ARRAY_COUNT(parts) - 1];
aPiecek1c4da362021-04-29 14:26:34 +0200756 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 +0200757 range_expected = 0;
758 } else {
759 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
aPiecek1c4da362021-04-29 14:26:34 +0200760 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 +0200761 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
762 part->min_64 = part->max_64;
763 }
764 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +0100765 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200766 length_restr ? "length" : "range", expr);
aPiecek1c4da362021-04-29 14:26:34 +0200767 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200768 goto cleanup;
769 }
770 }
771
772 /* check with the previous range/length restriction */
773 if (base_range) {
774 switch (basetype) {
775 case LY_TYPE_BINARY:
776 case LY_TYPE_UINT8:
777 case LY_TYPE_UINT16:
778 case LY_TYPE_UINT32:
779 case LY_TYPE_UINT64:
780 case LY_TYPE_STRING:
781 uns = 1;
782 break;
783 case LY_TYPE_DEC64:
784 case LY_TYPE_INT8:
785 case LY_TYPE_INT16:
786 case LY_TYPE_INT32:
787 case LY_TYPE_INT64:
788 uns = 0;
789 break;
790 default:
791 LOGINT(ctx->ctx);
792 ret = LY_EINT;
793 goto cleanup;
794 }
795 for (u = v = 0; u < parts_done && v < LY_ARRAY_COUNT(base_range->parts); ++u) {
796 if ((uns && (parts[u].min_u64 < base_range->parts[v].min_u64)) || (!uns && (parts[u].min_64 < base_range->parts[v].min_64))) {
797 goto baseerror;
798 }
799 /* current lower bound is not lower than the base */
800 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
801 /* base has single value */
802 if (base_range->parts[v].min_64 == parts[u].min_64) {
803 /* both lower bounds are the same */
804 if (parts[u].min_64 != parts[u].max_64) {
805 /* current continues with a range */
806 goto baseerror;
807 } else {
808 /* equal single values, move both forward */
809 ++v;
810 continue;
811 }
812 } else {
813 /* base is single value lower than current range, so the
814 * value from base range is removed in the current,
815 * move only base and repeat checking */
816 ++v;
817 --u;
818 continue;
819 }
820 } else {
821 /* base is the range */
822 if (parts[u].min_64 == parts[u].max_64) {
823 /* current is a single value */
824 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
825 /* current is behind the base range, so base range is omitted,
826 * move the base and keep the current for further check */
827 ++v;
828 --u;
829 } /* else it is within the base range, so move the current, but keep the base */
830 continue;
831 } else {
832 /* both are ranges - check the higher bound, the lower was already checked */
833 if ((uns && (parts[u].max_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].max_64 > base_range->parts[v].max_64))) {
834 /* higher bound is higher than the current higher bound */
835 if ((uns && (parts[u].min_u64 > base_range->parts[v].max_u64)) || (!uns && (parts[u].min_64 > base_range->parts[v].max_64))) {
836 /* but the current lower bound is also higher, so the base range is omitted,
837 * continue with the same current, but move the base */
838 --u;
839 ++v;
840 continue;
841 }
842 /* current range starts within the base range but end behind it */
843 goto baseerror;
844 } else {
845 /* current range is smaller than the base,
846 * move current, but stay with the base */
847 continue;
848 }
849 }
850 }
851 }
852 if (u != parts_done) {
853baseerror:
Radek Krejci2efc45b2020-12-22 16:25:44 +0100854 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200855 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
856 length_restr ? "length" : "range", range_p->arg);
aPiecek1c4da362021-04-29 14:26:34 +0200857 ret = LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200858 goto cleanup;
859 }
860 }
861
862 if (!(*range)) {
863 *range = calloc(1, sizeof **range);
864 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
865 }
866
867 /* we rewrite the following values as the types chain is being processed */
868 if (range_p->eapptag) {
869 lydict_remove(ctx->ctx, (*range)->eapptag);
870 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->eapptag, 0, &(*range)->eapptag), cleanup);
871 }
872 if (range_p->emsg) {
873 lydict_remove(ctx->ctx, (*range)->emsg);
874 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->emsg, 0, &(*range)->emsg), cleanup);
875 }
876 if (range_p->dsc) {
877 lydict_remove(ctx->ctx, (*range)->dsc);
878 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->dsc, 0, &(*range)->dsc), cleanup);
879 }
880 if (range_p->ref) {
881 lydict_remove(ctx->ctx, (*range)->ref);
882 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, range_p->ref, 0, &(*range)->ref), cleanup);
883 }
884 /* extensions are taken only from the last range by the caller */
885
886 (*range)->parts = parts;
887 parts = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200888cleanup:
889 LY_ARRAY_FREE(parts);
890
891 return ret;
892}
893
894LY_ERR
Radek Krejci2efc45b2020-12-22 16:25:44 +0100895lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *pattern, pcre2_code **code)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200896{
897 size_t idx, idx2, start, end, size, brack;
898 char *perl_regex, *ptr;
Christian Hoppsdafed222021-03-22 13:02:42 -0400899 int err_code, compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200900 const char *orig_ptr;
901 PCRE2_SIZE err_offset;
902 pcre2_code *code_local;
903
904#define URANGE_LEN 19
905 char *ublock2urange[][2] = {
906 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
907 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
908 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
909 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
910 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
911 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
912 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
913 {"Greek", "[\\x{0370}-\\x{03FF}]"},
914 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
915 {"Armenian", "[\\x{0530}-\\x{058F}]"},
916 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
917 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
918 {"Syriac", "[\\x{0700}-\\x{074F}]"},
919 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
920 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
921 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
922 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
923 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
924 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
925 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
926 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
927 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
928 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
929 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
930 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
931 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
932 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
933 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
934 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
935 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
936 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
937 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
938 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
939 {"Ogham", "[\\x{1680}-\\x{169F}]"},
940 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
941 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
942 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
943 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
944 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
945 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
946 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
947 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
948 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
949 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
950 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
951 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
952 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
953 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
954 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
955 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
956 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
957 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
958 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
959 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
960 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
961 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
962 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
963 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
964 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
965 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
966 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
967 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
968 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
969 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
970 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
971 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
972 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
973 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
974 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
975 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
976 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
977 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
978 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
979 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
980 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
981 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
982 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
983 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
984 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
985 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
986 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
987 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
988 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
Michal Vasko2b71ab52020-12-01 16:44:11 +0100989 {"Specials", "[\\x{FEFF}|\\x{FFF0}-\\x{FFFD}]"},
Michal Vasko1a7a7bd2020-10-16 14:39:15 +0200990 {NULL, NULL}
991 };
992
993 /* adjust the expression to a Perl equivalent
994 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
995
996 /* allocate space for the transformed pattern */
997 size = strlen(pattern) + 1;
Christian Hoppsdafed222021-03-22 13:02:42 -0400998 compile_opts = PCRE2_UTF | PCRE2_ANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE;
999#ifdef PCRE2_ENDANCHORED
1000 compile_opts |= PCRE2_ENDANCHORED;
1001#else
1002 /* add space for trailing $ anchor */
1003 size++;
1004#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001005 perl_regex = malloc(size);
1006 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1007 perl_regex[0] = '\0';
1008
1009 /* we need to replace all "$" and "^" (that are not in "[]") with "\$" and "\^" */
1010 brack = 0;
1011 idx = 0;
1012 orig_ptr = pattern;
1013 while (orig_ptr[0]) {
1014 switch (orig_ptr[0]) {
1015 case '$':
1016 case '^':
1017 if (!brack) {
1018 /* make space for the extra character */
1019 ++size;
1020 perl_regex = ly_realloc(perl_regex, size);
1021 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
1022
1023 /* print escape slash */
1024 perl_regex[idx] = '\\';
1025 ++idx;
1026 }
1027 break;
1028 case '[':
1029 /* must not be escaped */
1030 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1031 ++brack;
1032 }
1033 break;
1034 case ']':
1035 if ((orig_ptr == pattern) || (orig_ptr[-1] != '\\')) {
1036 /* pattern was checked and compiled already */
1037 assert(brack);
1038 --brack;
1039 }
1040 break;
1041 default:
1042 break;
1043 }
1044
1045 /* copy char */
1046 perl_regex[idx] = orig_ptr[0];
1047
1048 ++idx;
1049 ++orig_ptr;
1050 }
Christian Hoppsdafed222021-03-22 13:02:42 -04001051#ifndef PCRE2_ENDANCHORED
1052 /* anchor match to end of subject */
1053 perl_regex[idx++] = '$';
1054#endif
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001055 perl_regex[idx] = '\0';
1056
1057 /* substitute Unicode Character Blocks with exact Character Ranges */
1058 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1059 start = ptr - perl_regex;
1060
1061 ptr = strchr(ptr, '}');
1062 if (!ptr) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001063 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 2, "unterminated character property");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001064 free(perl_regex);
1065 return LY_EVALID;
1066 }
1067 end = (ptr - perl_regex) + 1;
1068
1069 /* need more space */
1070 if (end - start < URANGE_LEN) {
1071 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1072 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
1073 }
1074
1075 /* find our range */
1076 for (idx = 0; ublock2urange[idx][0]; ++idx) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001077 if (!strncmp(perl_regex + start + ly_strlen_const("\\p{Is"),
1078 ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001079 break;
1080 }
1081 }
1082 if (!ublock2urange[idx][0]) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001083 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + start + 5, "unknown block name");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001084 free(perl_regex);
1085 return LY_EVALID;
1086 }
1087
1088 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1089 for (idx2 = 0, idx = 0; idx2 < start; ++idx2) {
1090 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1091 ++idx;
1092 }
1093 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1094 --idx;
1095 }
1096 }
1097 if (idx) {
1098 /* skip brackets */
1099 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1100 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1101 } else {
1102 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1103 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1104 }
1105 }
1106
1107 /* must return 0, already checked during parsing */
Christian Hoppsdafed222021-03-22 13:02:42 -04001108 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED, compile_opts,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001109 &err_code, &err_offset, NULL);
1110 if (!code_local) {
Radek Krejcif13b87b2020-12-01 22:02:17 +01001111 PCRE2_UCHAR err_msg[LY_PCRE2_MSG_LIMIT] = {0};
1112 pcre2_get_error_message(err_code, err_msg, LY_PCRE2_MSG_LIMIT);
Radek Krejci2efc45b2020-12-22 16:25:44 +01001113 LOGVAL(ctx, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001114 free(perl_regex);
1115 return LY_EVALID;
1116 }
1117 free(perl_regex);
1118
1119 if (code) {
1120 *code = code_local;
1121 } else {
1122 free(code_local);
1123 }
1124
1125 return LY_SUCCESS;
1126
1127#undef URANGE_LEN
1128}
1129
1130/**
1131 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1132 * @param[in] ctx Compile context.
1133 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
1134 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1135 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1136 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1137 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1138 */
1139static LY_ERR
1140lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
1141 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1142{
1143 struct lysc_pattern **pattern;
1144 LY_ARRAY_COUNT_TYPE u;
1145 LY_ERR ret = LY_SUCCESS;
1146
1147 /* first, copy the patterns from the base type */
1148 if (base_patterns) {
1149 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1150 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1151 }
1152
1153 LY_ARRAY_FOR(patterns_p, u) {
1154 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1155 *pattern = calloc(1, sizeof **pattern);
1156 ++(*pattern)->refcount;
1157
Radek Krejci2efc45b2020-12-22 16:25:44 +01001158 ret = lys_compile_type_pattern_check(ctx->ctx, &patterns_p[u].arg.str[1], &(*pattern)->code);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001159 LY_CHECK_RET(ret);
1160
Radek Krejcif13b87b2020-12-01 22:02:17 +01001161 if (patterns_p[u].arg.str[0] == LYSP_RESTR_PATTERN_NACK) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001162 (*pattern)->inverted = 1;
1163 }
1164 DUP_STRING_GOTO(ctx->ctx, &patterns_p[u].arg.str[1], (*pattern)->expr, ret, done);
1165 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag, ret, done);
1166 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg, ret, done);
1167 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc, ret, done);
1168 DUP_STRING_GOTO(ctx->ctx, patterns_p[u].ref, (*pattern)->ref, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01001169 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001170 }
1171done:
1172 return ret;
1173}
1174
1175/**
1176 * @brief map of the possible restrictions combination for the specific built-in type.
1177 */
1178static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1179 0 /* LY_TYPE_UNKNOWN */,
1180 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
1181 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1182 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1183 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1184 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1185 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
1186 LYS_SET_BIT /* LY_TYPE_BITS */,
1187 0 /* LY_TYPE_BOOL */,
1188 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1189 0 /* LY_TYPE_EMPTY */,
1190 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1191 LYS_SET_BASE /* LY_TYPE_IDENT */,
1192 LYS_SET_REQINST /* LY_TYPE_INST */,
1193 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
1194 LYS_SET_TYPE /* LY_TYPE_UNION */,
1195 LYS_SET_RANGE /* LY_TYPE_INT8 */,
1196 LYS_SET_RANGE /* LY_TYPE_INT16 */,
1197 LYS_SET_RANGE /* LY_TYPE_INT32 */,
1198 LYS_SET_RANGE /* LY_TYPE_INT64 */
1199};
1200
1201/**
1202 * @brief stringification of the YANG built-in data types
1203 */
1204const char *ly_data_type2str[LY_DATA_TYPE_COUNT] = {
Radek Krejci04699f02021-03-22 21:50:29 +01001205 LY_TYPE_UNKNOWN_STR,
1206 LY_TYPE_BINARY_STR,
1207 LY_TYPE_UINT8_STR,
1208 LY_TYPE_UINT16_STR,
1209 LY_TYPE_UINT32_STR,
1210 LY_TYPE_UINT64_STR,
1211 LY_TYPE_STRING_STR,
1212 LY_TYPE_BITS_STR,
1213 LY_TYPE_BOOL_STR,
1214 LY_TYPE_DEC64_STR,
1215 LY_TYPE_EMPTY_STR,
1216 LY_TYPE_ENUM_STR,
1217 LY_TYPE_IDENT_STR,
1218 LY_TYPE_INST_STR,
1219 LY_TYPE_LEAFREF_STR,
1220 LY_TYPE_UNION_STR,
1221 LY_TYPE_INT8_STR,
1222 LY_TYPE_INT16_STR,
1223 LY_TYPE_INT32_STR,
1224 LY_TYPE_INT64_STR
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001225};
1226
1227/**
1228 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1229 * @param[in] ctx Compile context.
1230 * @param[in] enums_p Array of the parsed enum structures to compile.
1231 * @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.
1232 * @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 +01001233 * @param[out] bitenums Newly created array of the compiled bitenums information for the current type.
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001234 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1235 */
1236static LY_ERR
1237lys_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 +01001238 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **bitenums)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001239{
1240 LY_ERR ret = LY_SUCCESS;
1241 LY_ARRAY_COUNT_TYPE u, v, match = 0;
Radek Krejci430a5582020-12-01 13:35:18 +01001242 int32_t highest_value = INT32_MIN, cur_val = INT32_MIN;
1243 uint32_t highest_position = 0, cur_pos = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001244 struct lysc_type_bitenum_item *e, storage;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001245 ly_bool enabled;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001246
1247 if (base_enums && (ctx->pmod->version < LYS_VERSION_1_1)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001248 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001249 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1250 return LY_EVALID;
1251 }
1252
1253 LY_ARRAY_FOR(enums_p, u) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001254 /* perform all checks */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001255 if (base_enums) {
1256 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1257 LY_ARRAY_FOR(base_enums, v) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001258 if (!strcmp(enums_p[u].name, base_enums[v].name)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001259 break;
1260 }
1261 }
1262 if (v == LY_ARRAY_COUNT(base_enums)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001263 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001264 "Invalid %s - derived type adds new item \"%s\".",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001265 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001266 return LY_EVALID;
1267 }
1268 match = v;
1269 }
1270
1271 if (basetype == LY_TYPE_ENUM) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001272 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001273 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001274 cur_val = (int32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001275 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001276 LY_ARRAY_FOR(*bitenums, v) {
1277 if (cur_val == (*bitenums)[v].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001278 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001279 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001280 cur_val, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001281 return LY_EVALID;
1282 }
1283 }
1284 } else if (base_enums) {
1285 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001286 cur_val = base_enums[match].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001287 } else {
1288 /* assign value automatically */
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001289 if (u == 0) {
1290 cur_val = 0;
1291 } else if (highest_value == INT32_MAX) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001292 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001293 "Invalid enumeration - it is not possible to auto-assign enum value for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001294 "\"%s\" since the highest value is already 2147483647.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001295 return LY_EVALID;
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001296 } else {
1297 cur_val = highest_value + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001298 }
Radek IÅ¡a038b60d2020-11-26 11:37:15 +01001299 }
1300
1301 /* save highest value for auto assing */
1302 if (highest_value < cur_val) {
1303 highest_value = cur_val;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001304 }
1305 } else { /* LY_TYPE_BITS */
1306 if (enums_p[u].flags & LYS_SET_VALUE) {
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001307 /* value assigned by model */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001308 cur_pos = (uint32_t)enums_p[u].value;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001309 /* check collision with other values */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001310 LY_ARRAY_FOR(*bitenums, v) {
1311 if (cur_pos == (*bitenums)[v].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001312 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001313 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001314 cur_pos, enums_p[u].name, (*bitenums)[v].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001315 return LY_EVALID;
1316 }
1317 }
1318 } else if (base_enums) {
1319 /* inherit the assigned value */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001320 cur_pos = base_enums[match].position;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001321 } else {
1322 /* assign value automatically */
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001323 if (u == 0) {
1324 cur_pos = 0;
1325 } else if (highest_position == UINT32_MAX) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001326 /* counter overflow */
Radek Krejci2efc45b2020-12-22 16:25:44 +01001327 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001328 "Invalid bits - it is not possible to auto-assign bit position for "
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001329 "\"%s\" since the highest value is already 4294967295.", enums_p[u].name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001330 return LY_EVALID;
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001331 } else {
1332 cur_pos = highest_position + 1;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001333 }
Radek IÅ¡aca013ee2020-11-26 17:51:26 +01001334 }
1335
1336 /* save highest position for auto assing */
1337 if (highest_position < cur_pos) {
1338 highest_position = cur_pos;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001339 }
1340 }
1341
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001342 /* the assigned values must not change from the derived type */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001343 if (base_enums) {
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001344 if (basetype == LY_TYPE_ENUM) {
1345 if (cur_val != base_enums[match].value) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001346 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001347 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001348 enums_p[u].name, base_enums[match].value, cur_val);
1349 return LY_EVALID;
1350 }
1351 } else {
1352 if (cur_pos != base_enums[match].position) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001353 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001354 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001355 enums_p[u].name, base_enums[match].position, cur_pos);
1356 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001357 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001358 }
1359 }
1360
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001361 /* evaluate if-ffeatures */
1362 LY_CHECK_RET(lys_eval_iffeatures(ctx->ctx, enums_p[u].iffeatures, &enabled));
1363 if (!enabled) {
1364 continue;
1365 }
1366
1367 /* add new enum/bit */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001368 LY_ARRAY_NEW_RET(ctx->ctx, *bitenums, e, LY_EMEM);
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001369 DUP_STRING_GOTO(ctx->ctx, enums_p[u].name, e->name, ret, done);
1370 DUP_STRING_GOTO(ctx->ctx, enums_p[u].dsc, e->dsc, ret, done);
1371 DUP_STRING_GOTO(ctx->ctx, enums_p[u].ref, e->ref, ret, done);
Michal Vaskod1e53b92021-01-28 13:11:06 +01001372 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 +01001373 if (basetype == LY_TYPE_ENUM) {
1374 e->value = cur_val;
1375 } else {
1376 e->position = cur_pos;
1377 }
Radek Krejciab430862021-03-02 20:13:40 +01001378 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001379
1380 if (basetype == LY_TYPE_BITS) {
1381 /* keep bits ordered by position */
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001382 for (v = u; v && (*bitenums)[v - 1].position > e->position; --v) {}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001383 if (v != u) {
1384 memcpy(&storage, e, sizeof *e);
Radek IÅ¡a0fe25a92021-03-18 08:45:18 +01001385 memmove(&(*bitenums)[v + 1], &(*bitenums)[v], (u - v) * sizeof **bitenums);
1386 memcpy(&(*bitenums)[v], &storage, sizeof storage);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001387 }
1388 }
1389 }
1390
1391done:
1392 return ret;
1393}
1394
Radek Krejci6a205692020-12-09 13:58:22 +01001395static LY_ERR
1396lys_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 +01001397 const char *context_name, struct lysc_type ***utypes_p)
Radek Krejci6a205692020-12-09 13:58:22 +01001398{
1399 LY_ERR ret = LY_SUCCESS;
1400 struct lysc_type **utypes = *utypes_p;
1401 struct lysc_type_union *un_aux = NULL;
1402
1403 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes, LY_ARRAY_COUNT(ptypes), ret, error);
1404 for (LY_ARRAY_COUNT_TYPE u = 0, additional = 0; u < LY_ARRAY_COUNT(ptypes); ++u) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001405 ret = lys_compile_type(ctx, context_pnode, context_flags, context_name, &ptypes[u], &utypes[u + additional],
1406 NULL, NULL);
Radek Krejci6a205692020-12-09 13:58:22 +01001407 LY_CHECK_GOTO(ret, error);
1408 if (utypes[u + additional]->basetype == LY_TYPE_UNION) {
1409 /* add space for additional types from the union subtype */
1410 un_aux = (struct lysc_type_union *)utypes[u + additional];
1411 LY_ARRAY_CREATE_GOTO(ctx->ctx, utypes,
1412 LY_ARRAY_COUNT(ptypes) + additional + LY_ARRAY_COUNT(un_aux->types) - LY_ARRAY_COUNT(utypes), ret, error);
1413
1414 /* copy subtypes of the subtype union */
1415 for (LY_ARRAY_COUNT_TYPE v = 0; v < LY_ARRAY_COUNT(un_aux->types); ++v) {
1416 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
1417 struct lysc_type_leafref *lref;
1418
1419 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
1420 utypes[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
1421 LY_CHECK_ERR_GOTO(!utypes[u + additional], LOGMEM(ctx->ctx); ret = LY_EMEM, error);
1422 lref = (struct lysc_type_leafref *)utypes[u + additional];
1423
1424 lref->basetype = LY_TYPE_LEAFREF;
1425 ret = lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path);
1426 LY_CHECK_GOTO(ret, error);
1427 lref->refcount = 1;
Radek Krejci6a205692020-12-09 13:58:22 +01001428 lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
Radek Krejci8df109d2021-04-23 12:19:08 +02001429 ret = lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001430 ((struct lysc_type_leafref *)un_aux->types[v])->prefixes, (void **)&lref->prefixes);
Radek Krejci6a205692020-12-09 13:58:22 +01001431 LY_CHECK_GOTO(ret, error);
1432 /* TODO extensions */
1433
1434 } else {
1435 utypes[u + additional] = un_aux->types[v];
1436 ++un_aux->types[v]->refcount;
1437 }
1438 ++additional;
1439 LY_ARRAY_INCREMENT(utypes);
1440 }
1441 /* compensate u increment in main loop */
1442 --additional;
1443
1444 /* free the replaced union subtype */
1445 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1446 un_aux = NULL;
1447 } else {
1448 LY_ARRAY_INCREMENT(utypes);
1449 }
1450 }
1451
1452 *utypes_p = utypes;
1453 return LY_SUCCESS;
1454
1455error:
1456 if (un_aux) {
1457 lysc_type_free(ctx->ctx, (struct lysc_type *)un_aux);
1458 }
1459 *utypes_p = utypes;
1460 return ret;
1461}
1462
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001463/**
1464 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
1465 * @param[in] ctx Compile context.
1466 * @param[in] context_pnode Schema node where the type/typedef is placed to correctly find the base types.
1467 * @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 +02001468 * @param[in] context_name Name of the context node or referencing typedef for logging.
1469 * @param[in] type_p Parsed type to compile.
1470 * @param[in] basetype Base YANG built-in type of the type to compile.
1471 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
1472 * @param[in] base The latest base (compiled) type from which the current type is being derived.
1473 * @param[out] type Newly created type structure with the filled information about the type.
1474 * @return LY_ERR value.
1475 */
1476static LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001477lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1478 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 +02001479{
1480 LY_ERR ret = LY_SUCCESS;
1481 struct lysc_type_bin *bin;
1482 struct lysc_type_num *num;
1483 struct lysc_type_str *str;
1484 struct lysc_type_bits *bits;
1485 struct lysc_type_enum *enumeration;
1486 struct lysc_type_dec *dec;
1487 struct lysc_type_identityref *idref;
1488 struct lysc_type_leafref *lref;
Radek Krejci6a205692020-12-09 13:58:22 +01001489 struct lysc_type_union *un;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001490
1491 switch (basetype) {
1492 case LY_TYPE_BINARY:
1493 bin = (struct lysc_type_bin *)(*type);
1494
1495 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
1496 if (type_p->length) {
1497 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1498 base ? ((struct lysc_type_bin *)base)->length : NULL, &bin->length));
1499 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001500 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001501 }
1502 }
1503 break;
1504 case LY_TYPE_BITS:
1505 /* RFC 7950 9.7 - bits */
1506 bits = (struct lysc_type_bits *)(*type);
1507 if (type_p->bits) {
1508 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
1509 base ? (struct lysc_type_bitenum_item *)((struct lysc_type_bits *)base)->bits : NULL,
1510 (struct lysc_type_bitenum_item **)&bits->bits));
1511 }
1512
1513 if (!base && !type_p->flags) {
1514 /* type derived from bits built-in type must contain at least one bit */
1515 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001516 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001517 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001518 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001519 }
1520 return LY_EVALID;
1521 }
1522 break;
1523 case LY_TYPE_DEC64:
1524 dec = (struct lysc_type_dec *)(*type);
1525
1526 /* RFC 7950 9.3.4 - fraction-digits */
1527 if (!base) {
1528 if (!type_p->fraction_digits) {
1529 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001530 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001531 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001532 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001533 }
1534 return LY_EVALID;
1535 }
1536 dec->fraction_digits = type_p->fraction_digits;
1537 } else {
1538 if (type_p->fraction_digits) {
1539 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
1540 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001541 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001542 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
1543 tpdfname);
1544 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001545 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001546 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
1547 }
1548 return LY_EVALID;
1549 }
1550 dec->fraction_digits = ((struct lysc_type_dec *)base)->fraction_digits;
1551 }
1552
1553 /* RFC 7950 9.2.4 - range */
1554 if (type_p->range) {
1555 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
1556 base ? ((struct lysc_type_dec *)base)->range : NULL, &dec->range));
1557 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001558 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001559 }
1560 }
1561 break;
1562 case LY_TYPE_STRING:
1563 str = (struct lysc_type_str *)(*type);
1564
1565 /* RFC 7950 9.4.4 - length */
1566 if (type_p->length) {
1567 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
1568 base ? ((struct lysc_type_str *)base)->length : NULL, &str->length));
1569 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001570 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001571 }
1572 } else if (base && ((struct lysc_type_str *)base)->length) {
1573 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str *)base)->length);
1574 }
1575
1576 /* RFC 7950 9.4.5 - pattern */
1577 if (type_p->patterns) {
1578 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
1579 base ? ((struct lysc_type_str *)base)->patterns : NULL, &str->patterns));
1580 } else if (base && ((struct lysc_type_str *)base)->patterns) {
1581 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str *)base)->patterns);
1582 }
1583 break;
1584 case LY_TYPE_ENUM:
1585 enumeration = (struct lysc_type_enum *)(*type);
1586
1587 /* RFC 7950 9.6 - enum */
1588 if (type_p->enums) {
1589 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
1590 base ? ((struct lysc_type_enum *)base)->enums : NULL, &enumeration->enums));
1591 }
1592
1593 if (!base && !type_p->flags) {
1594 /* type derived from enumerations built-in type must contain at least one enum */
1595 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001596 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001597 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001598 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001599 }
1600 return LY_EVALID;
1601 }
1602 break;
1603 case LY_TYPE_INT8:
1604 case LY_TYPE_UINT8:
1605 case LY_TYPE_INT16:
1606 case LY_TYPE_UINT16:
1607 case LY_TYPE_INT32:
1608 case LY_TYPE_UINT32:
1609 case LY_TYPE_INT64:
1610 case LY_TYPE_UINT64:
1611 num = (struct lysc_type_num *)(*type);
1612
1613 /* RFC 6020 9.2.4 - range */
1614 if (type_p->range) {
1615 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
1616 base ? ((struct lysc_type_num *)base)->range : NULL, &num->range));
1617 if (!tpdfname) {
Radek Krejciab430862021-03-02 20:13:40 +01001618 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001619 }
1620 }
1621 break;
1622 case LY_TYPE_IDENT:
1623 idref = (struct lysc_type_identityref *)(*type);
1624
1625 /* RFC 7950 9.10.2 - base */
1626 if (type_p->bases) {
1627 if (base) {
1628 /* only the directly derived identityrefs can contain base specification */
1629 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001630 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001631 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
1632 tpdfname);
1633 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001634 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001635 "Invalid base substatement for the type not directly derived from identityref built-in type.");
1636 }
1637 return LY_EVALID;
1638 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01001639 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->pmod, type_p->bases, NULL, &idref->bases, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001640 }
1641
1642 if (!base && !type_p->flags) {
1643 /* type derived from identityref built-in type must contain at least one base */
1644 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001645 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001646 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001647 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001648 }
1649 return LY_EVALID;
1650 }
1651 break;
1652 case LY_TYPE_LEAFREF:
1653 lref = (struct lysc_type_leafref *)*type;
1654
1655 /* RFC 7950 9.9.3 - require-instance */
1656 if (type_p->flags & LYS_SET_REQINST) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01001657 if (type_p->pmod->version < LYS_VERSION_1_1) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001658 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001659 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001660 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
1661 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001662 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001663 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
1664 }
1665 return LY_EVALID;
1666 }
1667 lref->require_instance = type_p->require_instance;
1668 } else if (base) {
1669 /* inherit */
1670 lref->require_instance = ((struct lysc_type_leafref *)base)->require_instance;
1671 } else {
1672 /* default is true */
1673 lref->require_instance = 1;
1674 }
1675 if (type_p->path) {
Radek Krejci8df109d2021-04-23 12:19:08 +02001676 LY_VALUE_FORMAT format;
Radek Krejci2926c1b2021-03-16 14:45:45 +01001677
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001678 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, type_p->path, &lref->path));
Radek Krejci0b013302021-03-29 15:22:32 +02001679 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 +02001680 LY_VALUE_SCHEMA, type_p->pmod, &format, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001681 } else if (base) {
1682 LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)base)->path, &lref->path));
Radek Krejci8df109d2021-04-23 12:19:08 +02001683 LY_CHECK_RET(lyplg_type_prefix_data_dup(ctx->ctx, LY_VALUE_SCHEMA_RESOLVED,
Radek Krejci2926c1b2021-03-16 14:45:45 +01001684 ((struct lysc_type_leafref *)base)->prefixes, (void **)&lref->prefixes));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001685 } else if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001686 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001687 return LY_EVALID;
1688 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001689 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001690 return LY_EVALID;
1691 }
1692 break;
1693 case LY_TYPE_INST:
1694 /* RFC 7950 9.9.3 - require-instance */
1695 if (type_p->flags & LYS_SET_REQINST) {
1696 ((struct lysc_type_instanceid *)(*type))->require_instance = type_p->require_instance;
1697 } else {
1698 /* default is true */
1699 ((struct lysc_type_instanceid *)(*type))->require_instance = 1;
1700 }
1701 break;
1702 case LY_TYPE_UNION:
1703 un = (struct lysc_type_union *)(*type);
1704
1705 /* RFC 7950 7.4 - type */
1706 if (type_p->types) {
1707 if (base) {
1708 /* only the directly derived union can contain types specification */
1709 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001710 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001711 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
1712 tpdfname);
1713 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001714 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001715 "Invalid type substatement for the type not directly derived from union built-in type.");
1716 }
1717 return LY_EVALID;
1718 }
1719 /* compile the type */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001720 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 +02001721 }
1722
1723 if (!base && !type_p->flags) {
1724 /* type derived from union built-in type must contain at least one type */
1725 if (tpdfname) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001726 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001727 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001728 LOGVAL(ctx->ctx, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001729 }
1730 return LY_EVALID;
1731 }
1732 break;
1733 case LY_TYPE_BOOL:
1734 case LY_TYPE_EMPTY:
1735 case LY_TYPE_UNKNOWN: /* just to complete switch */
1736 break;
1737 }
1738
1739 if (tpdfname) {
1740 switch (basetype) {
1741 case LY_TYPE_BINARY:
1742 type_p->compiled = *type;
1743 *type = calloc(1, sizeof(struct lysc_type_bin));
1744 break;
1745 case LY_TYPE_BITS:
1746 type_p->compiled = *type;
1747 *type = calloc(1, sizeof(struct lysc_type_bits));
1748 break;
1749 case LY_TYPE_DEC64:
1750 type_p->compiled = *type;
1751 *type = calloc(1, sizeof(struct lysc_type_dec));
1752 break;
1753 case LY_TYPE_STRING:
1754 type_p->compiled = *type;
1755 *type = calloc(1, sizeof(struct lysc_type_str));
1756 break;
1757 case LY_TYPE_ENUM:
1758 type_p->compiled = *type;
1759 *type = calloc(1, sizeof(struct lysc_type_enum));
1760 break;
1761 case LY_TYPE_INT8:
1762 case LY_TYPE_UINT8:
1763 case LY_TYPE_INT16:
1764 case LY_TYPE_UINT16:
1765 case LY_TYPE_INT32:
1766 case LY_TYPE_UINT32:
1767 case LY_TYPE_INT64:
1768 case LY_TYPE_UINT64:
1769 type_p->compiled = *type;
1770 *type = calloc(1, sizeof(struct lysc_type_num));
1771 break;
1772 case LY_TYPE_IDENT:
1773 type_p->compiled = *type;
1774 *type = calloc(1, sizeof(struct lysc_type_identityref));
1775 break;
1776 case LY_TYPE_LEAFREF:
1777 type_p->compiled = *type;
1778 *type = calloc(1, sizeof(struct lysc_type_leafref));
1779 break;
1780 case LY_TYPE_INST:
1781 type_p->compiled = *type;
1782 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1783 break;
1784 case LY_TYPE_UNION:
1785 type_p->compiled = *type;
1786 *type = calloc(1, sizeof(struct lysc_type_union));
1787 break;
1788 case LY_TYPE_BOOL:
1789 case LY_TYPE_EMPTY:
1790 case LY_TYPE_UNKNOWN: /* just to complete switch */
1791 break;
1792 }
1793 }
1794 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
1795
1796cleanup:
1797 return ret;
1798}
1799
Radek Krejcibf940f92021-03-24 21:04:13 +01001800/**
1801 * @brief Find the correct plugin implementing the described type
1802 *
1803 * @param[in] mod Module where the type is defined
1804 * @param[in] name Name of the type (typedef)
1805 * @param[in] basetype Type's basetype (when the built-in base plugin is supposed to be used)
1806 * @return Pointer to the plugin implementing the described data type.
1807 */
1808static struct lyplg_type *
1809lys_compile_type_get_plugin(struct lys_module *mod, const char *name, LY_DATA_TYPE basetype)
1810{
1811 struct lyplg_type *p;
1812
1813 /* try to find loaded user type plugins */
1814 p = lyplg_find(LYPLG_TYPE, mod->name, mod->revision, name);
1815 if (!p) {
1816 /* use the internal built-in type implementation */
1817 p = lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
1818 }
1819
1820 return p;
1821}
1822
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001823LY_ERR
Michal Vaskoa99b3572021-02-01 11:54:58 +01001824lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_pnode, uint16_t context_flags, const char *context_name,
1825 struct lysp_type *type_p, struct lysc_type **type, const char **units, struct lysp_qname **dflt)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001826{
1827 LY_ERR ret = LY_SUCCESS;
1828 ly_bool dummyloops = 0;
1829 struct type_context {
1830 const struct lysp_tpdf *tpdf;
1831 struct lysp_node *node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001832 } *tctx, *tctx_prev = NULL, *tctx_iter;
1833 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
1834 struct lysc_type *base = NULL, *prev_type;
1835 struct ly_set tpdf_chain = {0};
1836
1837 (*type) = NULL;
1838 if (dflt) {
1839 *dflt = NULL;
1840 }
1841
1842 tctx = calloc(1, sizeof *tctx);
1843 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Michal Vaskoa99b3572021-02-01 11:54:58 +01001844 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 +02001845 ret == LY_SUCCESS;
Michal Vaskoa99b3572021-02-01 11:54:58 +01001846 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->tpdf->type.pmod,
1847 &basetype, &tctx->tpdf, &tctx->node)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001848 if (basetype) {
1849 break;
1850 }
1851
1852 /* check status */
Michal Vaskoa99b3572021-02-01 11:54:58 +01001853 ret = lysc_check_status(ctx, context_flags, (void *)type_p->pmod, context_name, tctx->tpdf->flags,
1854 (void *)tctx->tpdf->type.pmod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001855 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1856
1857 if (units && !*units) {
1858 /* inherit units */
1859 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units, ret);
1860 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1861 }
1862 if (dflt && !*dflt && tctx->tpdf->dflt.str) {
1863 /* inherit default */
1864 *dflt = (struct lysp_qname *)&tctx->tpdf->dflt;
1865 }
1866 if (dummyloops && (!units || *units) && dflt && *dflt) {
1867 basetype = ((struct type_context *)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
1868 break;
1869 }
1870
Radek Krejci720d2612021-03-03 19:44:22 +01001871 if (tctx->tpdf->type.compiled && (tctx->tpdf->type.compiled->refcount == 1)) {
1872 /* context recompilation - everything was freed previously (the only reference is from the parsed type itself)
1873 * and we need now recompile the type again in the updated context. */
1874 lysc_type_free(ctx->ctx, tctx->tpdf->type.compiled);
1875 ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = NULL;
1876 }
1877
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001878 if (tctx->tpdf->type.compiled) {
1879 /* it is not necessary to continue, the rest of the chain was already compiled,
1880 * but we still may need to inherit default and units values, so start dummy loops */
1881 basetype = tctx->tpdf->type.compiled->basetype;
1882 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1883 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1884
1885 if ((units && !*units) || (dflt && !*dflt)) {
1886 dummyloops = 1;
1887 goto preparenext;
1888 } else {
1889 tctx = NULL;
1890 break;
1891 }
1892 }
1893
1894 /* circular typedef reference detection */
1895 for (uint32_t u = 0; u < tpdf_chain.count; u++) {
1896 /* local part */
1897 tctx_iter = (struct type_context *)tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001898 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001899 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001900 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1901 free(tctx);
1902 ret = LY_EVALID;
1903 goto cleanup;
1904 }
1905 }
1906 for (uint32_t u = 0; u < ctx->tpdf_chain.count; u++) {
1907 /* global part for unions corner case */
1908 tctx_iter = (struct type_context *)ctx->tpdf_chain.objs[u];
Michal Vaskoa99b3572021-02-01 11:54:58 +01001909 if (tctx_iter->tpdf == tctx->tpdf) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001910 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001911 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
1912 free(tctx);
1913 ret = LY_EVALID;
1914 goto cleanup;
1915 }
1916 }
1917
1918 /* store information for the following processing */
1919 ret = ly_set_add(&tpdf_chain, tctx, 1, NULL);
1920 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
1921
1922preparenext:
1923 /* prepare next loop */
1924 tctx_prev = tctx;
1925 tctx = calloc(1, sizeof *tctx);
1926 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
1927 }
1928 free(tctx);
1929
1930 /* allocate type according to the basetype */
1931 switch (basetype) {
1932 case LY_TYPE_BINARY:
1933 *type = calloc(1, sizeof(struct lysc_type_bin));
1934 break;
1935 case LY_TYPE_BITS:
1936 *type = calloc(1, sizeof(struct lysc_type_bits));
1937 break;
1938 case LY_TYPE_BOOL:
1939 case LY_TYPE_EMPTY:
1940 *type = calloc(1, sizeof(struct lysc_type));
1941 break;
1942 case LY_TYPE_DEC64:
1943 *type = calloc(1, sizeof(struct lysc_type_dec));
1944 break;
1945 case LY_TYPE_ENUM:
1946 *type = calloc(1, sizeof(struct lysc_type_enum));
1947 break;
1948 case LY_TYPE_IDENT:
1949 *type = calloc(1, sizeof(struct lysc_type_identityref));
1950 break;
1951 case LY_TYPE_INST:
1952 *type = calloc(1, sizeof(struct lysc_type_instanceid));
1953 break;
1954 case LY_TYPE_LEAFREF:
1955 *type = calloc(1, sizeof(struct lysc_type_leafref));
1956 break;
1957 case LY_TYPE_STRING:
1958 *type = calloc(1, sizeof(struct lysc_type_str));
1959 break;
1960 case LY_TYPE_UNION:
1961 *type = calloc(1, sizeof(struct lysc_type_union));
1962 break;
1963 case LY_TYPE_INT8:
1964 case LY_TYPE_UINT8:
1965 case LY_TYPE_INT16:
1966 case LY_TYPE_UINT16:
1967 case LY_TYPE_INT32:
1968 case LY_TYPE_UINT32:
1969 case LY_TYPE_INT64:
1970 case LY_TYPE_UINT64:
1971 *type = calloc(1, sizeof(struct lysc_type_num));
1972 break;
1973 case LY_TYPE_UNKNOWN:
Radek Krejci2efc45b2020-12-22 16:25:44 +01001974 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001975 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
1976 ret = LY_EVALID;
1977 goto cleanup;
1978 }
1979 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
1980 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01001981 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02001982 ly_data_type2str[basetype]);
1983 free(*type);
1984 (*type) = NULL;
1985 ret = LY_EVALID;
1986 goto cleanup;
1987 }
1988
1989 /* get restrictions from the referred typedefs */
1990 for (uint32_t u = tpdf_chain.count - 1; u + 1 > 0; --u) {
1991 tctx = (struct type_context *)tpdf_chain.objs[u];
1992
1993 /* remember the typedef context for circular check */
1994 ret = ly_set_add(&ctx->tpdf_chain, tctx, 1, NULL);
1995 LY_CHECK_GOTO(ret, cleanup);
1996
1997 if (tctx->tpdf->type.compiled) {
1998 base = tctx->tpdf->type.compiled;
1999 continue;
2000 } else if ((basetype != LY_TYPE_LEAFREF) && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
2001 /* no change, just use the type information from the base */
2002 base = ((struct lysp_tpdf *)tctx->tpdf)->type.compiled = ((struct type_context *)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
2003 ++base->refcount;
2004 continue;
2005 }
2006
2007 ++(*type)->refcount;
2008 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002009 LOGVAL(ctx->ctx, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002010 tctx->tpdf->name, ly_data_type2str[basetype]);
2011 ret = LY_EVALID;
2012 goto cleanup;
2013 } else if ((basetype == LY_TYPE_EMPTY) && tctx->tpdf->dflt.str) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002014 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002015 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
2016 tctx->tpdf->name, tctx->tpdf->dflt.str);
2017 ret = LY_EVALID;
2018 goto cleanup;
2019 }
2020
2021 (*type)->basetype = basetype;
Radek Krejci4f2e3e52021-03-30 14:20:28 +02002022 /* collect extensions */
2023 COMPILE_EXTS_GOTO(ctx, tctx->tpdf->type.exts, (*type)->exts, (*type), ret, cleanup);
2024 /* get plugin for the type */
Radek Krejcibf940f92021-03-24 21:04:13 +01002025 (*type)->plugin = lys_compile_type_get_plugin(tctx->tpdf->type.pmod->mod, tctx->tpdf->name, basetype);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002026 prev_type = *type;
Michal Vaskoa99b3572021-02-01 11:54:58 +01002027 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->tpdf->name,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002028 &((struct lysp_tpdf *)tctx->tpdf)->type, basetype, tctx->tpdf->name, base, type);
2029 LY_CHECK_GOTO(ret, cleanup);
2030 base = prev_type;
2031 }
2032 /* remove the processed typedef contexts from the stack for circular check */
2033 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
2034
2035 /* process the type definition in leaf */
2036 if (type_p->flags || !base || (basetype == LY_TYPE_LEAFREF)) {
2037 /* get restrictions from the node itself */
2038 (*type)->basetype = basetype;
Radek Krejcibf940f92021-03-24 21:04:13 +01002039 (*type)->plugin = base ? base->plugin : lyplg_find(LYPLG_TYPE, "", NULL, ly_data_type2str[basetype]);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002040 ++(*type)->refcount;
Michal Vaskoa99b3572021-02-01 11:54:58 +01002041 ret = lys_compile_type_(ctx, context_pnode, context_flags, context_name, type_p, basetype, NULL, base, type);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002042 LY_CHECK_GOTO(ret, cleanup);
2043 } else if ((basetype != LY_TYPE_BOOL) && (basetype != LY_TYPE_EMPTY)) {
2044 /* no specific restriction in leaf's type definition, copy from the base */
2045 free(*type);
2046 (*type) = base;
2047 ++(*type)->refcount;
2048 }
2049
Radek Krejciab430862021-03-02 20:13:40 +01002050 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), ret, cleanup);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002051
2052cleanup:
2053 ly_set_erase(&tpdf_chain, free);
2054 return ret;
2055}
2056
2057/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002058 * @brief Check uniqness of the node/action/notification name.
2059 *
2060 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
2061 * structures, but they share the namespace so we need to check their name collisions.
2062 *
2063 * @param[in] ctx Compile context.
2064 * @param[in] parent Parent of the nodes to check, can be NULL.
2065 * @param[in] name Name of the item to find in the given lists.
2066 * @param[in] exclude Node that was just added that should be excluded from the name checking.
2067 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
2068 */
2069static LY_ERR
2070lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *parent, const char *name,
2071 const struct lysc_node *exclude)
2072{
2073 const struct lysc_node *iter, *iter2;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002074 const struct lysc_node_action *actions;
2075 const struct lysc_node_notif *notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002076 uint32_t getnext_flags;
Radek Krejci078e4342021-02-12 18:17:26 +01002077 struct ly_set parent_choices = {0};
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002078
2079#define CHECK_NODE(iter, exclude, name) (iter != (void *)exclude && (iter)->module == exclude->module && !strcmp(name, (iter)->name))
2080
2081 if (exclude->nodetype == LYS_CASE) {
2082 /* check restricted only to all the cases */
2083 assert(parent->nodetype == LYS_CHOICE);
Michal Vasko544e58a2021-01-28 14:33:41 +01002084 LY_LIST_FOR(lysc_node_child(parent), iter) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002085 if (CHECK_NODE(iter, exclude, name)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002086 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "case");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002087 return LY_EEXIST;
2088 }
2089 }
2090
2091 return LY_SUCCESS;
2092 }
2093
2094 /* no reason for our parent to be choice anymore */
2095 assert(!parent || (parent->nodetype != LYS_CHOICE));
2096
2097 if (parent && (parent->nodetype == LYS_CASE)) {
2098 /* move to the first data definition parent */
Radek Krejci078e4342021-02-12 18:17:26 +01002099
2100 /* but remember the choice nodes on the parents path to avoid believe they collide with our node */
2101 iter = lysc_data_parent(parent);
2102 do {
2103 parent = parent->parent;
2104 if (parent && (parent->nodetype == LYS_CHOICE)) {
2105 ly_set_add(&parent_choices, (void *)parent, 1, NULL);
2106 }
2107 } while (parent != iter);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002108 }
2109
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002110 getnext_flags = LYS_GETNEXT_WITHCHOICE;
Michal Vaskob322b7d2021-01-29 17:22:24 +01002111 if (parent && (parent->nodetype & (LYS_RPC | LYS_ACTION))) {
2112 /* move to the inout to avoid traversing a not-filled-yet (the other) node */
2113 if (exclude->flags & LYS_IS_OUTPUT) {
2114 getnext_flags |= LYS_GETNEXT_OUTPUT;
2115 parent = lysc_node_child(parent)->next;
2116 } else {
2117 parent = lysc_node_child(parent);
2118 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002119 }
2120
2121 iter = NULL;
Radek Krejci5fa32a32021-02-08 18:12:38 +01002122 if (!parent && ctx->ext) {
2123 while ((iter = lys_getnext_ext(iter, parent, ctx->ext, getnext_flags))) {
2124 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2125 goto error;
2126 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002127
Radek Krejci5fa32a32021-02-08 18:12:38 +01002128 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2129 if (iter->nodetype == LYS_CHOICE) {
2130 iter2 = NULL;
2131 while ((iter2 = lys_getnext_ext(iter2, iter, NULL, 0))) {
2132 if (CHECK_NODE(iter2, exclude, name)) {
2133 goto error;
2134 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002135 }
2136 }
2137 }
Radek Krejci5fa32a32021-02-08 18:12:38 +01002138 } else {
2139 while ((iter = lys_getnext(iter, parent, ctx->cur_mod->compiled, getnext_flags))) {
2140 if (!ly_set_contains(&parent_choices, (void *)iter, NULL) && CHECK_NODE(iter, exclude, name)) {
2141 goto error;
2142 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002143
Radek Krejci5fa32a32021-02-08 18:12:38 +01002144 /* we must compare with both the choice and all its nested data-definiition nodes (but not recursively) */
2145 if (iter->nodetype == LYS_CHOICE) {
2146 iter2 = NULL;
2147 while ((iter2 = lys_getnext(iter2, iter, NULL, 0))) {
2148 if (CHECK_NODE(iter2, exclude, name)) {
2149 goto error;
2150 }
2151 }
2152 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002153 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002154
Radek Krejci5fa32a32021-02-08 18:12:38 +01002155 actions = parent ? lysc_node_actions(parent) : ctx->cur_mod->compiled->rpcs;
2156 LY_LIST_FOR((struct lysc_node *)actions, iter) {
2157 if (CHECK_NODE(iter, exclude, name)) {
2158 goto error;
2159 }
2160 }
2161
2162 notifs = parent ? lysc_node_notifs(parent) : ctx->cur_mod->compiled->notifs;
2163 LY_LIST_FOR((struct lysc_node *)notifs, iter) {
2164 if (CHECK_NODE(iter, exclude, name)) {
2165 goto error;
2166 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002167 }
2168 }
Radek Krejci078e4342021-02-12 18:17:26 +01002169 ly_set_erase(&parent_choices, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002170 return LY_SUCCESS;
2171
2172error:
Radek Krejci078e4342021-02-12 18:17:26 +01002173 ly_set_erase(&parent_choices, NULL);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002174 LOGVAL(ctx->ctx, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002175 return LY_EEXIST;
2176
2177#undef CHECK_NODE
2178}
2179
Radek Krejci2a9fc652021-01-22 17:44:34 +01002180/**
2181 * @brief Connect the node into the siblings list and check its name uniqueness. Also,
2182 * keep specific order of augments targetting the same node.
2183 *
2184 * @param[in] ctx Compile context
2185 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
2186 * the choice itself is expected instead of a specific case node.
2187 * @param[in] node Schema node to connect into the list.
2188 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
2189 * In case of LY_EEXIST, the node is actually kept in the tree, so do not free it directly.
2190 */
2191static LY_ERR
2192lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002193{
Radek Krejci2a9fc652021-01-22 17:44:34 +01002194 struct lysc_node **children, *anchor = NULL;
2195 int insert_after = 0;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002196
Radek Krejci2a9fc652021-01-22 17:44:34 +01002197 node->parent = parent;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002198
Radek Krejci2a9fc652021-01-22 17:44:34 +01002199 if (parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01002200 if (node->nodetype == LYS_INPUT) {
2201 assert(parent->nodetype & (LYS_ACTION | LYS_RPC));
2202 /* input node is part of the action but link it with output */
2203 node->next = &((struct lysc_node_action *)parent)->output.node;
2204 node->prev = node->next;
2205 return LY_SUCCESS;
2206 } else if (node->nodetype == LYS_OUTPUT) {
2207 /* output node is part of the action but link it with input */
2208 node->next = NULL;
2209 node->prev = &((struct lysc_node_action *)parent)->input.node;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002210 return LY_SUCCESS;
2211 } else if (node->nodetype == LYS_ACTION) {
2212 children = (struct lysc_node **)lysc_node_actions_p(parent);
2213 } else if (node->nodetype == LYS_NOTIF) {
2214 children = (struct lysc_node **)lysc_node_notifs_p(parent);
2215 } else {
Michal Vasko544e58a2021-01-28 14:33:41 +01002216 children = lysc_node_child_p(parent);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002217 }
2218 assert(children);
2219
2220 if (!(*children)) {
2221 /* first child */
2222 *children = node;
2223 } else if (node->flags & LYS_KEY) {
2224 /* special handling of adding keys */
2225 assert(node->module == parent->module);
2226 anchor = *children;
2227 if (anchor->flags & LYS_KEY) {
2228 while ((anchor->flags & LYS_KEY) && anchor->next) {
2229 anchor = anchor->next;
2230 }
2231 /* insert after the last key */
2232 insert_after = 1;
2233 } /* else insert before anchor (at the beginning) */
2234 } else if ((*children)->prev->module == node->module) {
2235 /* last child is from the same module, keep the order and insert at the end */
2236 anchor = (*children)->prev;
2237 insert_after = 1;
2238 } else if (parent->module == node->module) {
2239 /* adding module child after some augments were connected */
2240 for (anchor = *children; anchor->module == node->module; anchor = anchor->next) {}
2241 } else {
2242 /* some augments are already connected and we are connecting new ones,
2243 * keep module name order and insert the node into the children list */
2244 anchor = *children;
2245 do {
2246 anchor = anchor->prev;
2247
2248 /* check that we have not found the last augment node from our module or
2249 * the first augment node from a "smaller" module or
2250 * the first node from a local module */
2251 if ((anchor->module == node->module) || (strcmp(anchor->module->name, node->module->name) < 0) ||
2252 (anchor->module == parent->module)) {
2253 /* insert after */
2254 insert_after = 1;
2255 break;
2256 }
2257
2258 /* we have traversed all the nodes, insert before anchor (as the first node) */
2259 } while (anchor->prev->next);
2260 }
2261
2262 /* insert */
2263 if (anchor) {
2264 if (insert_after) {
2265 node->next = anchor->next;
2266 node->prev = anchor;
2267 anchor->next = node;
2268 if (node->next) {
2269 /* middle node */
2270 node->next->prev = node;
2271 } else {
2272 /* last node */
2273 (*children)->prev = node;
2274 }
2275 } else {
2276 node->next = anchor;
2277 node->prev = anchor->prev;
2278 anchor->prev = node;
2279 if (anchor == *children) {
2280 /* first node */
2281 *children = node;
2282 } else {
2283 /* middle node */
2284 node->prev->next = node;
2285 }
2286 }
2287 }
2288
2289 /* check the name uniqueness (even for an only child, it may be in case) */
2290 if (lys_compile_node_uniqness(ctx, parent, node->name, node)) {
2291 return LY_EEXIST;
2292 }
2293 } else {
2294 /* top-level element */
2295 struct lysc_node **list;
2296
Radek Krejci6b88a462021-02-17 12:39:34 +01002297 if (ctx->ext) {
2298 lysc_ext_substmt(ctx->ext, LY_STMT_CONTAINER /* matches all data nodes */, (void **)&list, NULL);
2299 } else if (node->nodetype == LYS_RPC) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002300 list = (struct lysc_node **)&ctx->cur_mod->compiled->rpcs;
2301 } else if (node->nodetype == LYS_NOTIF) {
2302 list = (struct lysc_node **)&ctx->cur_mod->compiled->notifs;
2303 } else {
2304 list = &ctx->cur_mod->compiled->data;
2305 }
2306 if (!(*list)) {
2307 *list = node;
2308 } else {
2309 /* insert at the end of the module's top-level nodes list */
2310 (*list)->prev->next = node;
2311 node->prev = (*list)->prev;
2312 (*list)->prev = node;
2313 }
2314
2315 /* check the name uniqueness on top-level */
2316 if (lys_compile_node_uniqness(ctx, NULL, node->name, node)) {
2317 return LY_EEXIST;
2318 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002319 }
2320
Radek Krejci2a9fc652021-01-22 17:44:34 +01002321 return LY_SUCCESS;
2322}
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002323
Radek Krejci2a9fc652021-01-22 17:44:34 +01002324/**
Michal Vaskod1e53b92021-01-28 13:11:06 +01002325 * @brief Set config and operation flags for a node.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002326 *
2327 * @param[in] ctx Compile context.
Michal Vaskod1e53b92021-01-28 13:11:06 +01002328 * @param[in] node Compiled node flags to set.
Radek Krejci2a9fc652021-01-22 17:44:34 +01002329 * @return LY_ERR value.
2330 */
2331static LY_ERR
Radek Krejci91531e12021-02-08 19:57:54 +01002332lys_compile_config(struct lysc_ctx *ctx, struct lysc_node *node)
Radek Krejci2a9fc652021-01-22 17:44:34 +01002333{
2334 /* case never has any explicit config */
2335 assert((node->nodetype != LYS_CASE) || !(node->flags & LYS_CONFIG_MASK));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002336
Michal Vasko7c565922021-06-10 14:58:27 +02002337 if (ctx->compile_opts & LYS_COMPILE_NO_CONFIG) {
Radek Krejci91531e12021-02-08 19:57:54 +01002338 /* ignore config statements inside Notification/RPC/action/... data */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002339 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002340 } else if (!(node->flags & LYS_CONFIG_MASK)) {
2341 /* config not explicitly set, inherit it from parent */
Radek Krejci91531e12021-02-08 19:57:54 +01002342 if (node->parent) {
2343 node->flags |= node->parent->flags & LYS_CONFIG_MASK;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002344 } else {
2345 /* default is config true */
2346 node->flags |= LYS_CONFIG_W;
2347 }
2348 } else {
2349 /* config set explicitly */
2350 node->flags |= LYS_SET_CONFIG;
2351 }
2352
Radek Krejci91531e12021-02-08 19:57:54 +01002353 if (node->parent && (node->parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002354 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Configuration node cannot be child of any state data node.");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002355 return LY_EVALID;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002356 }
2357
Radek Krejci2a9fc652021-01-22 17:44:34 +01002358 return LY_SUCCESS;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002359}
2360
Radek Krejci91531e12021-02-08 19:57:54 +01002361/**
2362 * @brief Set various flags of the compiled nodes
2363 *
2364 * @param[in] ctx Compile context.
2365 * @param[in] node Compiled node where the flags will be set.
2366 * @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).
2367 * Zero means no uses, non-zero value with no status bit set mean the default status.
2368 */
2369static LY_ERR
2370lys_compile_node_flags(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t uses_status)
2371{
2372 /* inherit config flags */
2373 LY_CHECK_RET(lys_compile_config(ctx, node));
2374
2375 /* status - it is not inherited by specification, but it does not make sense to have
2376 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002377 LY_CHECK_RET(lys_compile_status(ctx, &node->flags, node->name, uses_status ? uses_status :
2378 (node->parent ? node->parent->flags : 0), uses_status ? "<uses>" : (node->parent ? node->parent->name : NULL),
2379 !uses_status));
Radek Krejci91531e12021-02-08 19:57:54 +01002380
2381 /* other flags */
Michal Vasko7c565922021-06-10 14:58:27 +02002382 if ((ctx->compile_opts & LYS_IS_INPUT) && (node->nodetype != LYS_INPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002383 node->flags |= LYS_IS_INPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002384 } else if ((ctx->compile_opts & LYS_IS_OUTPUT) && (node->nodetype != LYS_OUTPUT)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002385 node->flags |= LYS_IS_OUTPUT;
Michal Vasko7c565922021-06-10 14:58:27 +02002386 } else if ((ctx->compile_opts & LYS_IS_NOTIF) && (node->nodetype != LYS_NOTIF)) {
Radek Krejci91531e12021-02-08 19:57:54 +01002387 node->flags |= LYS_IS_NOTIF;
2388 }
2389
2390 return LY_SUCCESS;
2391}
2392
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002393LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01002394lys_compile_node_(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
2395 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *),
2396 struct lysc_node *node, struct ly_set *child_set)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002397{
2398 LY_ERR ret = LY_SUCCESS;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002399 ly_bool not_supported, enabled;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002400 struct lysp_node *dev_pnode = NULL;
Radek Krejci9a3823e2021-01-27 20:26:46 +01002401 struct lysp_when *pwhen = NULL;
Michal Vaskoe02e7402021-07-23 08:29:28 +02002402 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002403
Radek Krejci2a9fc652021-01-22 17:44:34 +01002404 node->nodetype = pnode->nodetype;
2405 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002406 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002407 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002408 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002409
Radek Krejci2a9fc652021-01-22 17:44:34 +01002410 /* compile any deviations for this node */
2411 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vaskoe02e7402021-07-23 08:29:28 +02002412 if (not_supported && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
2413 /* if not supported, keep it just like disabled nodes by if-feature */
2414 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
2415 ctx->compile_opts |= LYS_COMPILE_DISABLED;
2416 }
2417 if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002418 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002419 }
2420
Radek Krejci2a9fc652021-01-22 17:44:34 +01002421 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vaskodfd254d2021-06-24 09:24:59 +02002422 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2423 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2424 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002425
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002426 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002427 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002428 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002429 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002430 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002431 }
2432
Radek Krejci91531e12021-02-08 19:57:54 +01002433 /* config, status and other flags */
2434 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002435 LY_CHECK_GOTO(ret, error);
2436
2437 /* list ordering */
2438 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002439 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002440 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002441 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002442 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002443 node->flags &= ~LYS_ORDBY_MASK;
2444 node->flags |= LYS_ORDBY_SYSTEM;
2445 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2446 /* default ordering is system */
2447 node->flags |= LYS_ORDBY_SYSTEM;
2448 }
2449 }
2450
Radek Krejci2a9fc652021-01-22 17:44:34 +01002451 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2452 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2453
Radek Krejci9a3823e2021-01-27 20:26:46 +01002454 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002455 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002456 ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002457 LY_CHECK_GOTO(ret, cleanup);
2458 }
2459
2460 /* connect any augments */
2461 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2462
2463 /* nodetype-specific part */
2464 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2465
2466 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002467 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002468 if (node->flags & LYS_MAND_TRUE) {
2469 /* inherit LYS_MAND_TRUE in parent containers */
2470 lys_compile_mandatory_parents(parent, 1);
2471 }
2472
2473 if (child_set) {
2474 /* add the new node into set */
2475 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2476 }
2477
2478 goto cleanup;
2479
2480error:
2481 lysc_node_free(ctx->ctx, node, 0);
2482cleanup:
2483 if (ret && dev_pnode) {
2484 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2485 }
Michal Vaskoe02e7402021-07-23 08:29:28 +02002486 ctx->compile_opts = prev_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002487 lysp_dev_node_free(ctx->ctx, dev_pnode);
2488 return ret;
2489}
2490
2491/**
2492 * @brief Compile parsed action's input/output node information.
2493 * @param[in] ctx Compile context
2494 * @param[in] pnode Parsed inout node.
2495 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2496 * is enriched with the inout-specific information.
2497 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2498 */
2499LY_ERR
2500lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2501{
2502 LY_ERR ret = LY_SUCCESS;
2503 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002504 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002505
2506 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2507 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2508
2509 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002510 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002511 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002512
Radek Krejci01180ac2021-01-27 08:48:22 +01002513 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002514 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2515 }
2516
Michal Vasko7c565922021-06-10 14:58:27 +02002517 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002518
2519done:
2520 return ret;
2521}
2522
2523/**
2524 * @brief Compile parsed action node information.
2525 * @param[in] ctx Compile context
2526 * @param[in] pnode Parsed action node.
2527 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2528 * is enriched with the action-specific information.
2529 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2530 */
2531LY_ERR
2532lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2533{
2534 LY_ERR ret;
2535 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2536 struct lysc_node_action *action = (struct lysc_node_action *)node;
2537 struct lysp_node_action_inout *input, implicit_input = {
2538 .nodetype = LYS_INPUT,
2539 .name = "input",
2540 .parent = pnode,
2541 };
2542 struct lysp_node_action_inout *output, implicit_output = {
2543 .nodetype = LYS_OUTPUT,
2544 .name = "output",
2545 .parent = pnode,
2546 };
2547
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002548 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002549 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002550 if (action_p->input.nodetype == LYS_UNKNOWN) {
2551 input = &implicit_input;
2552 } else {
2553 input = &action_p->input;
2554 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002555 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 +01002556 lysc_update_path(ctx, NULL, NULL);
2557 LY_CHECK_GOTO(ret, done);
2558
2559 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002560 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002561 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002562 output = &implicit_output;
2563 } else {
2564 output = &action_p->output;
2565 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002566 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 +01002567 lysc_update_path(ctx, NULL, NULL);
2568 LY_CHECK_GOTO(ret, done);
2569
2570 /* do not check "must" semantics in a grouping */
Michal Vasko6f08e962021-07-23 08:30:47 +02002571 if ((action->input.musts || action->output.musts) && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002572 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2573 LY_CHECK_GOTO(ret, done);
2574 }
2575
2576done:
2577 return ret;
2578}
2579
2580/**
2581 * @brief Compile parsed action node information.
2582 * @param[in] ctx Compile context
2583 * @param[in] pnode Parsed action node.
2584 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2585 * is enriched with the action-specific information.
2586 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2587 */
2588LY_ERR
2589lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2590{
2591 LY_ERR ret = LY_SUCCESS;
2592 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2593 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2594 struct lysp_node *child_p;
2595
2596 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02002597 if (notif_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002598 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002599 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002600 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002601 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002602
Radek Krejci01180ac2021-01-27 08:48:22 +01002603 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002604 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002605 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002606 }
2607
Radek Krejci2a9fc652021-01-22 17:44:34 +01002608done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002609 return ret;
2610}
2611
2612/**
2613 * @brief Compile parsed container node information.
2614 * @param[in] ctx Compile context
2615 * @param[in] pnode Parsed container node.
2616 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2617 * is enriched with the container-specific information.
2618 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2619 */
2620static LY_ERR
2621lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2622{
2623 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2624 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2625 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002626 LY_ERR ret = LY_SUCCESS;
2627
2628 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002629 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002630 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002631 }
2632
2633 /* more cases when the container has meaning but is kept NP for convenience:
2634 * - when condition
2635 * - direct child action/notification
2636 */
2637
2638 LY_LIST_FOR(cont_p->child, child_p) {
2639 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2640 LY_CHECK_GOTO(ret, done);
2641 }
2642
Michal Vasko5347e3a2020-11-03 17:14:57 +01002643 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02002644 if (cont_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002645 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002646 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002647 LY_CHECK_GOTO(ret, done);
2648 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002649
2650 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2651 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2652 LY_CHECK_GOTO(ret, done);
2653 }
2654 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2655 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2656 LY_CHECK_GOTO(ret, done);
2657 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002658
2659done:
2660 return ret;
2661}
2662
Michal Vasko72244882021-01-12 15:21:05 +01002663/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002664 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2665 * @param[in] ctx Compile context.
2666 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2667 * @param[in] type_p Parsed type to compile.
2668 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2669 * @return LY_ERR value.
2670 */
2671static LY_ERR
2672lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2673 struct lysc_node_leaf *leaf)
2674{
2675 struct lysp_qname *dflt;
aPiecekc6526b42021-07-12 15:21:39 +02002676 struct ly_set *leafrefs_set;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002677
Michal Vaskoa99b3572021-02-01 11:54:58 +01002678 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002679 leaf->units ? NULL : &leaf->units, &dflt));
2680
2681 /* store default value, if any */
2682 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2683 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2684 }
2685
aPiecekc6526b42021-07-12 15:21:39 +02002686 leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs;
2687 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002688 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
aPiecekc6526b42021-07-12 15:21:39 +02002689 LY_CHECK_RET(ly_set_add(leafrefs_set, leaf, 0, NULL));
2690 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002691 LY_ARRAY_COUNT_TYPE u;
2692 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2693 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2694 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
aPiecekc6526b42021-07-12 15:21:39 +02002695 LY_CHECK_RET(ly_set_add(leafrefs_set, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002696 }
2697 }
2698 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2699 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002700 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002701 return LY_EVALID;
2702 }
2703 }
2704
2705 return LY_SUCCESS;
2706}
2707
2708/**
2709 * @brief Compile parsed leaf node information.
2710 * @param[in] ctx Compile context
2711 * @param[in] pnode Parsed leaf node.
2712 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2713 * is enriched with the leaf-specific information.
2714 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2715 */
2716static LY_ERR
2717lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2718{
2719 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2720 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002721 LY_ERR ret = LY_SUCCESS;
2722
Michal Vasko5347e3a2020-11-03 17:14:57 +01002723 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02002724 if (leaf_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002725 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002726 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002727 LY_CHECK_GOTO(ret, done);
2728 }
2729 if (leaf_p->units) {
2730 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2731 leaf->flags |= LYS_SET_UNITS;
2732 }
2733
2734 /* compile type */
2735 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2736 LY_CHECK_GOTO(ret, done);
2737
2738 /* store/update default value */
2739 if (leaf_p->dflt.str) {
2740 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2741 leaf->flags |= LYS_SET_DFLT;
2742 }
2743
2744 /* checks */
2745 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002746 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002747 "Invalid mandatory leaf with a default value.");
2748 return LY_EVALID;
2749 }
2750
2751done:
2752 return ret;
2753}
2754
2755/**
2756 * @brief Compile parsed leaf-list node information.
2757 * @param[in] ctx Compile context
2758 * @param[in] pnode Parsed leaf-list node.
2759 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2760 * is enriched with the leaf-list-specific information.
2761 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2762 */
2763static LY_ERR
2764lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2765{
2766 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2767 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002768 LY_ERR ret = LY_SUCCESS;
2769
Michal Vasko5347e3a2020-11-03 17:14:57 +01002770 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02002771 if (llist_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002772 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002773 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002774 LY_CHECK_GOTO(ret, done);
2775 }
2776 if (llist_p->units) {
2777 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2778 llist->flags |= LYS_SET_UNITS;
2779 }
2780
2781 /* compile type */
2782 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2783 LY_CHECK_GOTO(ret, done);
2784
2785 /* store/update default values */
2786 if (llist_p->dflts) {
2787 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002788 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002789 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2790 return LY_EVALID;
2791 }
2792
2793 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2794 llist->flags |= LYS_SET_DFLT;
2795 }
2796
2797 llist->min = llist_p->min;
2798 if (llist->min) {
2799 llist->flags |= LYS_MAND_TRUE;
2800 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002801 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
2802
2803 if (llist->flags & LYS_CONFIG_R) {
2804 /* state leaf-list is always ordered-by user */
2805 llist->flags &= ~LYS_ORDBY_SYSTEM;
2806 llist->flags |= LYS_ORDBY_USER;
2807 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002808
2809 /* checks */
2810 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002811 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 +02002812 return LY_EVALID;
2813 }
2814
2815 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002816 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002817 llist->min, llist->max);
2818 return LY_EVALID;
2819 }
2820
2821done:
2822 return ret;
2823}
2824
2825LY_ERR
2826lysc_resolve_schema_nodeid(struct lysc_ctx *ctx, const char *nodeid, size_t nodeid_len, const struct lysc_node *ctx_node,
Radek Krejci8df109d2021-04-23 12:19:08 +02002827 const struct lys_module *cur_mod, LY_VALUE_FORMAT format, void *prefix_data, uint16_t nodetype,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002828 const struct lysc_node **target, uint16_t *result_flag)
2829{
2830 LY_ERR ret = LY_EVALID;
2831 const char *name, *prefix, *id;
2832 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002833 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002834 const char *nodeid_type;
2835 uint32_t getnext_extra_flag = 0;
2836 uint16_t current_nodetype = 0;
2837
2838 assert(nodeid);
2839 assert(target);
2840 assert(result_flag);
2841 *target = NULL;
2842 *result_flag = 0;
2843
2844 id = nodeid;
2845
2846 if (ctx_node) {
2847 /* descendant-schema-nodeid */
2848 nodeid_type = "descendant";
2849
2850 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002851 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002852 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002853 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002854 return LY_EVALID;
2855 }
2856 } else {
2857 /* absolute-schema-nodeid */
2858 nodeid_type = "absolute";
2859
2860 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002861 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002862 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002863 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002864 return LY_EVALID;
2865 }
2866 ++id;
2867 }
2868
2869 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2870 if (prefix) {
2871 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2872 if (!mod) {
2873 /* module must always be found */
2874 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002875 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002876 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002877 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002878 return LY_ENOTFOUND;
2879 }
2880 } else {
2881 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02002882 case LY_VALUE_SCHEMA:
2883 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002884 /* use the current module */
2885 mod = cur_mod;
2886 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02002887 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02002888 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002889 if (!ctx_node) {
2890 LOGINT_RET(ctx->ctx);
2891 }
2892 /* inherit the module of the previous context node */
2893 mod = ctx_node->module;
2894 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02002895 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02002896 case LY_VALUE_XML:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002897 /* not really defined */
2898 LOGINT_RET(ctx->ctx);
2899 }
2900 }
2901
2902 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2903 /* move through input/output manually */
2904 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002905 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2906 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002907 return LY_ENOTFOUND;
2908 }
2909 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002910 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002911 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002912 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002913 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2914 } else {
2915 /* only input or output is valid */
2916 ctx_node = NULL;
2917 }
2918 } else {
2919 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002920 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002921 getnext_extra_flag = 0;
2922 }
2923 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002924 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2925 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002926 return LY_ENOTFOUND;
2927 }
2928 current_nodetype = ctx_node->nodetype;
2929
2930 if (current_nodetype == LYS_NOTIF) {
2931 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2932 } else if (current_nodetype == LYS_INPUT) {
2933 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2934 } else if (current_nodetype == LYS_OUTPUT) {
2935 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2936 }
2937
2938 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2939 break;
2940 }
2941 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002942 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002943 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002944 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002945 return LY_EVALID;
2946 }
2947 ++id;
2948 }
2949
2950 if (ret == LY_SUCCESS) {
2951 *target = ctx_node;
2952 if (nodetype && !(current_nodetype & nodetype)) {
2953 return LY_EDENIED;
2954 }
2955 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002956 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002957 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002958 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002959 }
2960
2961 return ret;
2962}
2963
2964/**
2965 * @brief Compile information about list's uniques.
2966 * @param[in] ctx Compile context.
2967 * @param[in] uniques Sized array list of unique statements.
2968 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2969 * @return LY_ERR value.
2970 */
2971static LY_ERR
2972lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2973{
2974 LY_ERR ret = LY_SUCCESS;
2975 struct lysc_node_leaf **key, ***unique;
2976 struct lysc_node *parent;
2977 const char *keystr, *delim;
2978 size_t len;
2979 LY_ARRAY_COUNT_TYPE v;
2980 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2981 uint16_t flags;
2982
2983 LY_ARRAY_FOR(uniques, v) {
2984 config = -1;
2985 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2986 keystr = uniques[v].str;
2987 while (keystr) {
2988 delim = strpbrk(keystr, " \t\n");
2989 if (delim) {
2990 len = delim - keystr;
2991 while (isspace(*delim)) {
2992 ++delim;
2993 }
2994 } else {
2995 len = strlen(keystr);
2996 }
2997
2998 /* unique node must be present */
2999 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003000 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02003001 LY_VALUE_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003002 if (ret != LY_SUCCESS) {
3003 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003004 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003005 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01003006 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003007 }
3008 return LY_EVALID;
3009 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003010 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003011 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003012 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003013 return LY_EVALID;
3014 }
3015
3016 /* all referenced leafs must be of the same config type */
3017 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3018 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003019 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003020 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3021 return LY_EVALID;
3022 } else if ((*key)->flags & LYS_CONFIG_W) {
3023 config = 1;
3024 } else { /* LYS_CONFIG_R */
3025 config = 0;
3026 }
3027
3028 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3029 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3030 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003031 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003032 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3033 return LY_EVALID;
3034 }
3035 }
3036
3037 /* check status */
3038 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3039 (*key)->flags, (*key)->module, (*key)->name));
3040
3041 /* mark leaf as unique */
3042 (*key)->flags |= LYS_UNIQUE;
3043
3044 /* next unique value in line */
3045 keystr = delim;
3046 }
3047 /* next unique definition */
3048 }
3049
3050 return LY_SUCCESS;
3051}
3052
3053/**
3054 * @brief Compile parsed list node information.
3055 * @param[in] ctx Compile context
3056 * @param[in] pnode Parsed list node.
3057 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3058 * is enriched with the list-specific information.
3059 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3060 */
3061static LY_ERR
3062lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3063{
3064 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3065 struct lysc_node_list *list = (struct lysc_node_list *)node;
3066 struct lysp_node *child_p;
3067 struct lysc_node_leaf *key, *prev_key = NULL;
3068 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003069 const char *keystr, *delim;
3070 LY_ERR ret = LY_SUCCESS;
3071
3072 list->min = list_p->min;
3073 if (list->min) {
3074 list->flags |= LYS_MAND_TRUE;
3075 }
3076 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3077
3078 LY_LIST_FOR(list_p->child, child_p) {
3079 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3080 }
3081
Michal Vasko5347e3a2020-11-03 17:14:57 +01003082 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02003083 if (list_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003084 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003085 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003086 }
3087
3088 /* keys */
3089 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003090 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003091 return LY_EVALID;
3092 }
3093
3094 /* find all the keys (must be direct children) */
3095 keystr = list_p->key;
3096 if (!keystr) {
3097 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003098 list->flags &= ~LYS_ORDBY_SYSTEM;
3099 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003100 }
3101 while (keystr) {
3102 delim = strpbrk(keystr, " \t\n");
3103 if (delim) {
3104 len = delim - keystr;
3105 while (isspace(*delim)) {
3106 ++delim;
3107 }
3108 } else {
3109 len = strlen(keystr);
3110 }
3111
3112 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003113 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3114 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003115 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003116 return LY_EVALID;
3117 }
3118 /* keys must be unique */
3119 if (key->flags & LYS_KEY) {
3120 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003121 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003122 return LY_EVALID;
3123 }
3124
Radek Krejcia6016992021-03-03 10:13:41 +01003125 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003126 /* key must have the same config flag as the list itself */
3127 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003128 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003129 return LY_EVALID;
3130 }
3131 if (ctx->pmod->version < LYS_VERSION_1_1) {
3132 /* YANG 1.0 denies key to be of empty type */
3133 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003134 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003135 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3136 return LY_EVALID;
3137 }
3138 } else {
3139 /* when and if-feature are illegal on list keys */
3140 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003141 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003142 "List's key must not have any \"when\" statement.");
3143 return LY_EVALID;
3144 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003145 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003146 }
3147
3148 /* check status */
3149 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3150 key->flags, key->module, key->name));
3151
3152 /* ignore default values of the key */
3153 if (key->dflt) {
3154 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3155 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3156 free(key->dflt);
3157 key->dflt = NULL;
3158 }
3159 /* mark leaf as key */
3160 key->flags |= LYS_KEY;
3161
3162 /* move it to the correct position */
3163 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3164 /* fix links in closest previous siblings of the key */
3165 if (key->next) {
3166 key->next->prev = key->prev;
3167 } else {
3168 /* last child */
3169 list->child->prev = key->prev;
3170 }
3171 if (key->prev->next) {
3172 key->prev->next = key->next;
3173 }
3174 /* fix links in the key */
3175 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003176 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003177 key->next = prev_key->next;
3178 } else {
3179 key->prev = list->child->prev;
3180 key->next = list->child;
3181 }
3182 /* fix links in closes future siblings of the key */
3183 if (prev_key) {
3184 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003185 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003186 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003187 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003188 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003189 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003190 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003191 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003192 }
3193 /* fix links in parent */
3194 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003195 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003196 }
3197 }
3198
3199 /* next key value */
3200 prev_key = key;
3201 keystr = delim;
3202 lysc_update_path(ctx, NULL, NULL);
3203 }
3204
3205 /* uniques */
3206 if (list_p->uniques) {
3207 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3208 }
3209
Radek Krejci2a9fc652021-01-22 17:44:34 +01003210 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3211 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3212 LY_CHECK_GOTO(ret, done);
3213 }
3214 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3215 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3216 LY_CHECK_GOTO(ret, done);
3217 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003218
3219 /* checks */
3220 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003221 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003222 list->min, list->max);
3223 return LY_EVALID;
3224 }
3225
3226done:
3227 return ret;
3228}
3229
3230/**
3231 * @brief Do some checks and set the default choice's case.
3232 *
3233 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3234 *
3235 * @param[in] ctx Compile context.
3236 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3237 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3238 * @return LY_ERR value.
3239 */
3240static LY_ERR
3241lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3242{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003243 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003244 const struct lys_module *mod;
3245 const char *prefix = NULL, *name;
3246 size_t prefix_len = 0;
3247
3248 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3249 name = strchr(dflt->str, ':');
3250 if (name) {
3251 prefix = dflt->str;
3252 prefix_len = name - prefix;
3253 ++name;
3254 } else {
3255 name = dflt->str;
3256 }
3257 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003258 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003259 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003260 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003261 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003262 return LY_EVALID;
3263 }
3264 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003265 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003266 }
3267
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003268 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 +02003269 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003270 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003271 "Default case \"%s\" not found.", dflt->str);
3272 return LY_EVALID;
3273 }
3274
3275 /* no mandatory nodes directly under the default case */
3276 LY_LIST_FOR(ch->dflt->child, iter) {
3277 if (iter->parent != (struct lysc_node *)ch->dflt) {
3278 break;
3279 }
3280 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003281 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003282 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3283 return LY_EVALID;
3284 }
3285 }
3286
3287 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003288 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003289 return LY_EVALID;
3290 }
3291
3292 ch->dflt->flags |= LYS_SET_DFLT;
3293 return LY_SUCCESS;
3294}
3295
3296LY_ERR
3297lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3298 struct ly_set *child_set)
3299{
3300 LY_ERR ret = LY_SUCCESS;
3301 struct lysp_node *child_p_next = child_p->next;
3302 struct lysp_node_case *cs_p;
aPiecekaa320c92021-05-21 07:34:24 +02003303 struct lysc_node_case *compiled_case;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003304
3305 if (child_p->nodetype == LYS_CASE) {
3306 /* standard case under choice */
3307 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3308 } else {
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003309 /* we need the implicit case first, so create a fake parsed (shorthand) case */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003310 cs_p = calloc(1, sizeof *cs_p);
3311 cs_p->nodetype = LYS_CASE;
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003312 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003313 cs_p->child = child_p;
3314
3315 /* make the child the only case child */
3316 child_p->next = NULL;
3317
3318 /* compile it normally */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003319 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 +02003320
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003321 if (((struct lysc_node_choice *)node)->cases) {
3322 /* get last case node */
3323 compiled_case = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases->prev;
aPiecek082c7dc2021-05-20 08:55:07 +02003324
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003325 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
3326 /* Compiled case node cannot point to his corresponding parsed node
aPiecek8e351382021-06-25 14:13:55 +02003327 * because it exists temporarily. Therefore, it must be set to NULL.
3328 */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003329 compiled_case->priv = NULL;
3330 }
aPiecekaa320c92021-05-21 07:34:24 +02003331
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003332 /* The status is copied from his child and not from his parent as usual. */
3333 if (compiled_case->child) {
3334 compiled_case->flags &= ~LYS_STATUS_MASK;
3335 compiled_case->flags |= LYS_STATUS_MASK & compiled_case->child->flags;
3336 }
3337 } /* else it was removed by a deviation */
aPiecekaa320c92021-05-21 07:34:24 +02003338
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003339revert_sh_case:
3340 /* free the parsed shorthand case and correct pointers back */
aPiecekaa320c92021-05-21 07:34:24 +02003341 cs_p->child = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003342 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3343 child_p->next = child_p_next;
3344 }
3345
3346 return ret;
3347}
3348
3349/**
3350 * @brief Compile parsed choice node information.
3351 *
3352 * @param[in] ctx Compile context
3353 * @param[in] pnode Parsed choice node.
3354 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3355 * is enriched with the choice-specific information.
3356 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3357 */
3358static LY_ERR
3359lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3360{
3361 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3362 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3363 struct lysp_node *child_p;
3364 LY_ERR ret = LY_SUCCESS;
3365
3366 assert(node->nodetype == LYS_CHOICE);
3367
3368 LY_LIST_FOR(ch_p->child, child_p) {
3369 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3370 }
3371
3372 /* default branch */
3373 if (ch_p->dflt.str) {
3374 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3375 }
3376
3377 return ret;
3378}
3379
3380/**
3381 * @brief Compile parsed anydata or anyxml node information.
3382 * @param[in] ctx Compile context
3383 * @param[in] pnode Parsed anydata or anyxml node.
3384 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3385 * is enriched with the any-specific information.
3386 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3387 */
3388static LY_ERR
3389lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3390{
3391 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3392 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003393 LY_ERR ret = LY_SUCCESS;
3394
Michal Vasko5347e3a2020-11-03 17:14:57 +01003395 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko6f08e962021-07-23 08:30:47 +02003396 if (any_p->musts && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003397 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003398 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003399 LY_CHECK_GOTO(ret, done);
3400 }
3401
Radek Krejci91531e12021-02-08 19:57:54 +01003402 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003403 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003404 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3405 }
3406done:
3407 return ret;
3408}
3409
3410/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003411 * @brief Prepare the case structure in choice node for the new data node.
3412 *
3413 * 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
3414 * created in the choice when the first child was processed.
3415 *
3416 * @param[in] ctx Compile context.
3417 * @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,
3418 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3419 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3420 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3421 * @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,
3422 * it is linked from the case structure only in case it is its first child.
3423 */
3424static LY_ERR
3425lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3426{
3427 struct lysp_node *child_p;
3428 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3429
3430 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3431 /* we have to add an implicit case node into the parent choice */
3432 } else if (pnode->nodetype == LYS_CASE) {
3433 /* explicit parent case */
3434 LY_LIST_FOR(cs_p->child, child_p) {
3435 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3436 }
3437 } else {
3438 LOGINT_RET(ctx->ctx);
3439 }
3440
3441 return LY_SUCCESS;
3442}
3443
3444void
3445lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3446{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003447 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003448
3449 if (add) { /* set flag */
3450 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3451 parent = parent->parent) {
3452 parent->flags |= LYS_MAND_TRUE;
3453 }
3454 } else { /* unset flag */
3455 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003456 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003457 if (iter->flags & LYS_MAND_TRUE) {
3458 /* there is another mandatory node */
3459 return;
3460 }
3461 }
3462 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3463 parent->flags &= ~LYS_MAND_TRUE;
3464 }
3465 }
3466}
3467
3468/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003469 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003470 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003471 * @param[in] name Name of the grouping to find,
3472 * @return NULL when there is no grouping with the specified name
3473 * @return Pointer to the grouping of the specified @p name.
3474 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003475static struct lysp_node_grp *
3476match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003477{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003478 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003479
Radek Krejci2a9fc652021-01-22 17:44:34 +01003480 LY_LIST_FOR(grps, grp) {
3481 if (!strcmp(grp->name, name)) {
3482 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003483 }
3484 }
3485
3486 return NULL;
3487}
3488
3489/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003490 * @brief Find grouping for a uses.
3491 *
3492 * @param[in] ctx Compile context.
3493 * @param[in] uses_p Parsed uses node.
3494 * @param[out] gpr_p Found grouping on success.
3495 * @param[out] grp_pmod Module of @p grp_p on success.
3496 * @return LY_ERR value.
3497 */
3498static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003499lys_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 +02003500 struct lysp_module **grp_pmod)
3501{
3502 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003503 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003504 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003505 const char *id, *name, *prefix, *local_pref;
3506 size_t prefix_len, name_len;
3507 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003508 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003509
3510 *grp_p = NULL;
3511 *grp_pmod = NULL;
3512
3513 /* search for the grouping definition */
3514 id = uses_p->name;
3515 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3516 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3517 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3518 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003519 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003520 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003521 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3522 found = ctx->pmod;
3523 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003524 }
3525 }
3526 } else {
3527 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003528 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003529 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003530 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003531 "Invalid prefix used for grouping reference.", uses_p->name);
3532 return LY_EVALID;
3533 }
3534 pmod = mod->parsed;
3535 }
3536
3537 if (!found) {
3538 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003539 if ((grp = match_grouping(pmod->groupings, name))) {
3540 found = pmod;
3541 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003542 /* ... and all the submodules */
3543 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003544 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3545 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003546 break;
3547 }
3548 }
3549 }
3550 }
3551 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003552 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003553 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3554 return LY_EVALID;
3555 }
3556
Michal Vasko7c565922021-06-10 14:58:27 +02003557 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003558 /* remember that the grouping is instantiated to avoid its standalone validation */
3559 grp->flags |= LYS_USED_GRP;
3560 }
3561
3562 *grp_p = grp;
3563 *grp_pmod = found;
3564 return LY_SUCCESS;
3565}
3566
3567/**
Michal Vaskodfd254d2021-06-24 09:24:59 +02003568 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
3569 */
3570#define LYS_STATUS_USES LYS_CONFIG_MASK
3571
3572/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003573 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3574 * If present, also apply uses's modificators.
3575 *
3576 * @param[in] ctx Compile context
3577 * @param[in] uses_p Parsed uses schema node.
3578 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3579 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3580 * the compile context.
3581 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3582 */
3583static LY_ERR
3584lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3585{
3586 struct lysp_node *pnode;
3587 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003588 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003589 uint32_t i, grp_stack_count;
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003590 uint16_t uses_flags;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003591 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3592 LY_ERR ret = LY_SUCCESS;
3593 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003594 struct ly_set uses_child_set = {0};
3595
3596 /* find the referenced grouping */
3597 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3598
3599 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3600 grp_stack_count = ctx->groupings.count;
3601 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3602 if (grp_stack_count == ctx->groupings.count) {
3603 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003604 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003605 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3606 return LY_EVALID;
3607 }
3608
3609 /* check status */
3610 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3611 LY_CHECK_GOTO(ret, cleanup);
3612
3613 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3614 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3615 LY_CHECK_GOTO(ret, cleanup);
3616
3617 /* switch context's parsed module being processed */
3618 ctx->pmod = grp_mod;
3619
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003620 /* compile special uses status flags */
3621 uses_flags = uses_p->flags;
3622 ret = lys_compile_status(ctx, &uses_flags, "<uses>", parent ? parent->flags : 0, parent ? parent->name : NULL, 0);
3623 LY_CHECK_GOTO(ret, cleanup);
3624 uses_flags |= LYS_STATUS_USES;
3625
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003626 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003627 LY_LIST_FOR(grp->child, pnode) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003628 ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003629 LY_CHECK_GOTO(ret, cleanup);
3630 }
3631
3632 if (child_set) {
3633 /* add these children to our compiled child_set as well since uses is a schema-only node */
3634 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3635 }
3636
3637 if (uses_p->when) {
3638 /* pass uses's when to all the data children */
3639 for (i = 0; i < uses_child_set.count; ++i) {
3640 child = uses_child_set.snodes[i];
3641
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003642 ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003643 LY_CHECK_GOTO(ret, cleanup);
3644 }
3645 }
3646
3647 /* compile actions */
3648 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003649 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003650 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3651 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003652 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003653 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3654 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003655 ret = LY_EVALID;
3656 goto cleanup;
3657 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003658 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003659 ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003660 LY_CHECK_GOTO(ret, cleanup);
3661 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003662
3663 if (uses_p->when) {
3664 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003665 LY_LIST_FOR((struct lysc_node *)*actions, child) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003666 ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003667 LY_CHECK_GOTO(ret, cleanup);
3668 }
3669 }
3670 }
3671
3672 /* compile notifications */
3673 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003674 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003675 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3676 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003677 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003678 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3679 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003680 ret = LY_EVALID;
3681 goto cleanup;
3682 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003683
3684 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003685 ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003686 LY_CHECK_GOTO(ret, cleanup);
3687 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003688
3689 if (uses_p->when) {
3690 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003691 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003692 ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003693 LY_CHECK_GOTO(ret, cleanup);
3694 }
3695 }
3696 }
3697
3698 /* check that all augments were applied */
3699 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003700 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3701 /* augment of some parent uses, irrelevant now */
3702 continue;
3703 }
3704
3705 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003706 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3707 ret = LY_ENOTFOUND;
3708 }
3709 LY_CHECK_GOTO(ret, cleanup);
3710
3711 /* check that all refines were applied */
3712 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003713 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3714 /* refine of some paretn uses, irrelevant now */
3715 continue;
3716 }
3717
3718 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003719 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3720 ret = LY_ENOTFOUND;
3721 }
3722 LY_CHECK_GOTO(ret, cleanup);
3723
3724cleanup:
3725 /* reload previous context's parsed module being processed */
3726 ctx->pmod = mod_old;
3727
3728 /* remove the grouping from the stack for circular groupings dependency check */
3729 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3730 assert(ctx->groupings.count == grp_stack_count);
3731
3732 ly_set_erase(&uses_child_set, NULL);
3733 return ret;
3734}
3735
3736static int
3737lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3738{
3739 struct lysp_node *iter;
3740 int len = 0;
3741
3742 *path = NULL;
3743 for (iter = node; iter && len >= 0; iter = iter->parent) {
3744 char *s = *path;
3745 char *id;
3746
3747 switch (iter->nodetype) {
3748 case LYS_USES:
3749 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3750 break;
3751 case LYS_GROUPING:
3752 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3753 break;
3754 case LYS_AUGMENT:
3755 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3756 break;
3757 default:
3758 id = strdup(iter->name);
3759 break;
3760 }
3761
3762 if (!iter->parent) {
3763 /* print prefix */
3764 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3765 } else {
3766 /* prefix is the same as in parent */
3767 len = asprintf(path, "/%s%s", id, s ? s : "");
3768 }
3769 free(s);
3770 free(id);
3771 }
3772
3773 if (len < 0) {
3774 free(*path);
3775 *path = NULL;
3776 } else if (len == 0) {
3777 *path = strdup("/");
3778 len = 1;
3779 }
3780 return len;
3781}
3782
3783LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003784lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003785{
3786 LY_ERR ret;
3787 char *path;
3788 int len;
3789
3790 struct lysp_node_uses fake_uses = {
3791 .parent = pnode,
3792 .nodetype = LYS_USES,
3793 .flags = 0, .next = NULL,
3794 .name = grp->name,
3795 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3796 .refines = NULL, .augments = NULL
3797 };
3798 struct lysc_node_container fake_container = {
3799 .nodetype = LYS_CONTAINER,
3800 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3801 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003802 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003803 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003804 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003805 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003806 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3807 };
3808
3809 if (grp->parent) {
3810 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3811 }
3812
3813 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3814 if (len < 0) {
3815 LOGMEM(ctx->ctx);
3816 return LY_EMEM;
3817 }
3818 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3819 ctx->path_len = (uint32_t)len;
3820 free(path);
3821
3822 lysc_update_path(ctx, NULL, "{grouping}");
3823 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003824 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003825 lysc_update_path(ctx, NULL, NULL);
3826 lysc_update_path(ctx, NULL, NULL);
3827
3828 ctx->path_len = 1;
3829 ctx->path[1] = '\0';
3830
3831 /* cleanup */
3832 lysc_node_container_free(ctx->ctx, &fake_container);
3833
3834 return ret;
3835}
3836
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003837LY_ERR
3838lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3839 struct ly_set *child_set)
3840{
3841 LY_ERR ret = LY_SUCCESS;
3842 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02003843 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003844
3845 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3846
3847 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003848 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003849 } else {
3850 lysc_update_path(ctx, NULL, "{uses}");
3851 lysc_update_path(ctx, NULL, pnode->name);
3852 }
3853
3854 switch (pnode->nodetype) {
3855 case LYS_CONTAINER:
3856 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3857 node_compile_spec = lys_compile_node_container;
3858 break;
3859 case LYS_LEAF:
3860 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3861 node_compile_spec = lys_compile_node_leaf;
3862 break;
3863 case LYS_LIST:
3864 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3865 node_compile_spec = lys_compile_node_list;
3866 break;
3867 case LYS_LEAFLIST:
3868 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3869 node_compile_spec = lys_compile_node_leaflist;
3870 break;
3871 case LYS_CHOICE:
3872 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3873 node_compile_spec = lys_compile_node_choice;
3874 break;
3875 case LYS_CASE:
3876 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3877 node_compile_spec = lys_compile_node_case;
3878 break;
3879 case LYS_ANYXML:
3880 case LYS_ANYDATA:
3881 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3882 node_compile_spec = lys_compile_node_any;
3883 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003884 case LYS_RPC:
3885 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02003886 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003887 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3888 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02003889 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003890 return LY_EVALID;
3891 }
3892 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3893 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02003894 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003895 break;
3896 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02003897 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003898 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3899 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02003900 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003901 return LY_EVALID;
3902 }
3903 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3904 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02003905 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003906 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003907 case LYS_USES:
3908 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3909 lysc_update_path(ctx, NULL, NULL);
3910 lysc_update_path(ctx, NULL, NULL);
3911 return ret;
3912 default:
3913 LOGINT(ctx->ctx);
3914 return LY_EINT;
3915 }
3916 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3917
Radek Krejci2a9fc652021-01-22 17:44:34 +01003918 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003919
Michal Vasko7c565922021-06-10 14:58:27 +02003920 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003921 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003922 return ret;
3923}