blob: 59b2769fe635b2d53b925bb07d33d4e15808956c [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 Vasko7c565922021-06-10 14:58:27 +0200337 if (!(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
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 Vasko1a7a7bd2020-10-16 14:39:15 +02002402
Radek Krejci2a9fc652021-01-22 17:44:34 +01002403 node->nodetype = pnode->nodetype;
2404 node->module = ctx->cur_mod;
Radek Krejci91531e12021-02-08 19:57:54 +01002405 node->parent = parent;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002406 node->prev = node;
aPiecek9922ea92021-04-12 07:59:20 +02002407 node->priv = ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED ? pnode : NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002408
Radek Krejci2a9fc652021-01-22 17:44:34 +01002409 /* compile any deviations for this node */
2410 LY_CHECK_GOTO(ret = lys_compile_node_deviations_refines(ctx, pnode, parent, &dev_pnode, &not_supported), error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002411 if (not_supported) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002412 goto error;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002413 } else if (dev_pnode) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002414 pnode = dev_pnode;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002415 }
2416
Radek Krejci2a9fc652021-01-22 17:44:34 +01002417 node->flags = pnode->flags & LYS_FLAGS_COMPILED_MASK;
Michal Vaskodfd254d2021-06-24 09:24:59 +02002418 DUP_STRING_GOTO(ctx->ctx, pnode->name, node->name, ret, error);
2419 DUP_STRING_GOTO(ctx->ctx, pnode->dsc, node->dsc, ret, error);
2420 DUP_STRING_GOTO(ctx->ctx, pnode->ref, node->ref, ret, error);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002421
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002422 /* if-features */
Radek Krejci2a9fc652021-01-22 17:44:34 +01002423 LY_CHECK_GOTO(ret = lys_eval_iffeatures(ctx->ctx, pnode->iffeatures, &enabled), error);
Michal Vasko7c565922021-06-10 14:58:27 +02002424 if (!enabled && !(ctx->compile_opts & (LYS_COMPILE_NO_DISABLED | LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Michal Vasko30ab8e72021-04-19 12:47:52 +02002425 ly_set_add(&ctx->unres->disabled, node, 1, NULL);
Michal Vasko7c565922021-06-10 14:58:27 +02002426 ctx->compile_opts |= LYS_COMPILE_DISABLED;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002427 }
2428
Radek Krejci91531e12021-02-08 19:57:54 +01002429 /* config, status and other flags */
2430 ret = lys_compile_node_flags(ctx, node, uses_status);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002431 LY_CHECK_GOTO(ret, error);
2432
2433 /* list ordering */
2434 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
Michal Vaskod1e53b92021-01-28 13:11:06 +01002435 if ((node->flags & (LYS_CONFIG_R | LYS_IS_OUTPUT | LYS_IS_NOTIF)) && (node->flags & LYS_ORDBY_MASK)) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02002436 LOGVRB("The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejci91531e12021-02-08 19:57:54 +01002437 (node->flags & LYS_IS_OUTPUT) ? "RPC/action output parameters" :
Michal Vasko7c565922021-06-10 14:58:27 +02002438 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification content" : "state data", ctx->path);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002439 node->flags &= ~LYS_ORDBY_MASK;
2440 node->flags |= LYS_ORDBY_SYSTEM;
2441 } else if (!(node->flags & LYS_ORDBY_MASK)) {
2442 /* default ordering is system */
2443 node->flags |= LYS_ORDBY_SYSTEM;
2444 }
2445 }
2446
Radek Krejci2a9fc652021-01-22 17:44:34 +01002447 /* insert into parent's children/compiled module (we can no longer free the node separately on error) */
2448 LY_CHECK_GOTO(ret = lys_compile_node_connect(ctx, parent, node), cleanup);
2449
Radek Krejci9a3823e2021-01-27 20:26:46 +01002450 if ((pwhen = lysp_node_when(pnode))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002451 /* compile when */
Michal Vaskodfd254d2021-06-24 09:24:59 +02002452 ret = lys_compile_when(ctx, pwhen, pnode->flags, node, lysc_data_node(node), node, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002453 LY_CHECK_GOTO(ret, cleanup);
2454 }
2455
2456 /* connect any augments */
2457 LY_CHECK_GOTO(ret = lys_compile_node_augments(ctx, node), cleanup);
2458
2459 /* nodetype-specific part */
2460 LY_CHECK_GOTO(ret = node_compile_spec(ctx, pnode, node), cleanup);
2461
2462 /* final compilation tasks that require the node to be connected */
Radek Krejciab430862021-03-02 20:13:40 +01002463 COMPILE_EXTS_GOTO(ctx, pnode->exts, node->exts, node, ret, cleanup);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002464 if (node->flags & LYS_MAND_TRUE) {
2465 /* inherit LYS_MAND_TRUE in parent containers */
2466 lys_compile_mandatory_parents(parent, 1);
2467 }
2468
2469 if (child_set) {
2470 /* add the new node into set */
2471 LY_CHECK_GOTO(ret = ly_set_add(child_set, node, 1, NULL), cleanup);
2472 }
2473
2474 goto cleanup;
2475
2476error:
2477 lysc_node_free(ctx->ctx, node, 0);
2478cleanup:
2479 if (ret && dev_pnode) {
2480 LOGVAL(ctx->ctx, LYVE_OTHER, "Compilation of a deviated and/or refined node failed.");
2481 }
2482 lysp_dev_node_free(ctx->ctx, dev_pnode);
2483 return ret;
2484}
2485
2486/**
2487 * @brief Compile parsed action's input/output node information.
2488 * @param[in] ctx Compile context
2489 * @param[in] pnode Parsed inout node.
2490 * @param[in,out] node Pre-prepared structure from lys_compile_node_() with filled generic node information
2491 * is enriched with the inout-specific information.
2492 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2493 */
2494LY_ERR
2495lys_compile_node_action_inout(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2496{
2497 LY_ERR ret = LY_SUCCESS;
2498 struct lysp_node *child_p;
Michal Vasko7c565922021-06-10 14:58:27 +02002499 uint32_t prev_options = ctx->compile_opts;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002500
2501 struct lysp_node_action_inout *inout_p = (struct lysp_node_action_inout *)pnode;
2502 struct lysc_node_action_inout *inout = (struct lysc_node_action_inout *)node;
2503
2504 COMPILE_ARRAY_GOTO(ctx, inout_p->musts, inout->musts, lys_compile_must, ret, done);
Radek Krejciab430862021-03-02 20:13:40 +01002505 COMPILE_EXTS_GOTO(ctx, inout_p->exts, inout->exts, inout, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002506 ctx->compile_opts |= (inout_p->nodetype == LYS_INPUT) ? LYS_COMPILE_RPC_INPUT : LYS_COMPILE_RPC_OUTPUT;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002507
Radek Krejci01180ac2021-01-27 08:48:22 +01002508 LY_LIST_FOR(inout_p->child, child_p) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002509 LY_CHECK_GOTO(ret = lys_compile_node(ctx, child_p, node, 0, NULL), done);
2510 }
2511
Michal Vasko7c565922021-06-10 14:58:27 +02002512 ctx->compile_opts = prev_options;
Radek Krejci2a9fc652021-01-22 17:44:34 +01002513
2514done:
2515 return ret;
2516}
2517
2518/**
2519 * @brief Compile parsed action node information.
2520 * @param[in] ctx Compile context
2521 * @param[in] pnode Parsed action node.
2522 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2523 * is enriched with the action-specific information.
2524 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2525 */
2526LY_ERR
2527lys_compile_node_action(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2528{
2529 LY_ERR ret;
2530 struct lysp_node_action *action_p = (struct lysp_node_action *)pnode;
2531 struct lysc_node_action *action = (struct lysc_node_action *)node;
2532 struct lysp_node_action_inout *input, implicit_input = {
2533 .nodetype = LYS_INPUT,
2534 .name = "input",
2535 .parent = pnode,
2536 };
2537 struct lysp_node_action_inout *output, implicit_output = {
2538 .nodetype = LYS_OUTPUT,
2539 .name = "output",
2540 .parent = pnode,
2541 };
2542
Michal Vaskoe17ff5f2021-01-29 17:23:03 +01002543 /* input */
Radek Krejcia6016992021-03-03 10:13:41 +01002544 lysc_update_path(ctx, action->module, "input");
Radek Krejci2a9fc652021-01-22 17:44:34 +01002545 if (action_p->input.nodetype == LYS_UNKNOWN) {
2546 input = &implicit_input;
2547 } else {
2548 input = &action_p->input;
2549 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002550 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 +01002551 lysc_update_path(ctx, NULL, NULL);
2552 LY_CHECK_GOTO(ret, done);
2553
2554 /* output */
Radek Krejcia6016992021-03-03 10:13:41 +01002555 lysc_update_path(ctx, action->module, "output");
Michal Vasko9149efd2021-01-29 11:16:59 +01002556 if (action_p->output.nodetype == LYS_UNKNOWN) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002557 output = &implicit_output;
2558 } else {
2559 output = &action_p->output;
2560 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002561 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 +01002562 lysc_update_path(ctx, NULL, NULL);
2563 LY_CHECK_GOTO(ret, done);
2564
2565 /* do not check "must" semantics in a grouping */
Michal Vasko7c565922021-06-10 14:58:27 +02002566 if ((action->input.musts || action->output.musts) && !(ctx->compile_opts & (LYS_COMPILE_DISABLED | LYS_COMPILE_GROUPING))) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01002567 ret = ly_set_add(&ctx->unres->xpath, action, 0, NULL);
2568 LY_CHECK_GOTO(ret, done);
2569 }
2570
2571done:
2572 return ret;
2573}
2574
2575/**
2576 * @brief Compile parsed action node information.
2577 * @param[in] ctx Compile context
2578 * @param[in] pnode Parsed action node.
2579 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2580 * is enriched with the action-specific information.
2581 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2582 */
2583LY_ERR
2584lys_compile_node_notif(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2585{
2586 LY_ERR ret = LY_SUCCESS;
2587 struct lysp_node_notif *notif_p = (struct lysp_node_notif *)pnode;
2588 struct lysc_node_notif *notif = (struct lysc_node_notif *)node;
2589 struct lysp_node *child_p;
2590
2591 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002592 if (notif_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002593 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002594 ret = ly_set_add(&ctx->unres->xpath, notif, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002595 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002596 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002597
Radek Krejci01180ac2021-01-27 08:48:22 +01002598 LY_LIST_FOR(notif_p->child, child_p) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002599 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
Radek Krejci2a9fc652021-01-22 17:44:34 +01002600 LY_CHECK_GOTO(ret, done);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002601 }
2602
Radek Krejci2a9fc652021-01-22 17:44:34 +01002603done:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002604 return ret;
2605}
2606
2607/**
2608 * @brief Compile parsed container node information.
2609 * @param[in] ctx Compile context
2610 * @param[in] pnode Parsed container node.
2611 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2612 * is enriched with the container-specific information.
2613 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2614 */
2615static LY_ERR
2616lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2617{
2618 struct lysp_node_container *cont_p = (struct lysp_node_container *)pnode;
2619 struct lysc_node_container *cont = (struct lysc_node_container *)node;
2620 struct lysp_node *child_p;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002621 LY_ERR ret = LY_SUCCESS;
2622
2623 if (cont_p->presence) {
Michal Vaskoe16c7b72021-02-26 10:39:06 +01002624 /* presence container */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002625 cont->flags |= LYS_PRESENCE;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002626 }
2627
2628 /* more cases when the container has meaning but is kept NP for convenience:
2629 * - when condition
2630 * - direct child action/notification
2631 */
2632
2633 LY_LIST_FOR(cont_p->child, child_p) {
2634 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2635 LY_CHECK_GOTO(ret, done);
2636 }
2637
Michal Vasko5347e3a2020-11-03 17:14:57 +01002638 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002639 if (cont_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002640 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002641 ret = ly_set_add(&ctx->unres->xpath, cont, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002642 LY_CHECK_GOTO(ret, done);
2643 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01002644
2645 LY_LIST_FOR((struct lysp_node *)cont_p->actions, child_p) {
2646 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2647 LY_CHECK_GOTO(ret, done);
2648 }
2649 LY_LIST_FOR((struct lysp_node *)cont_p->notifs, child_p) {
2650 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
2651 LY_CHECK_GOTO(ret, done);
2652 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002653
2654done:
2655 return ret;
2656}
2657
Michal Vasko72244882021-01-12 15:21:05 +01002658/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002659 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
2660 * @param[in] ctx Compile context.
2661 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
2662 * @param[in] type_p Parsed type to compile.
2663 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
2664 * @return LY_ERR value.
2665 */
2666static LY_ERR
2667lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p,
2668 struct lysc_node_leaf *leaf)
2669{
2670 struct lysp_qname *dflt;
aPiecekc6526b42021-07-12 15:21:39 +02002671 struct ly_set *leafrefs_set;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002672
Michal Vaskoa99b3572021-02-01 11:54:58 +01002673 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, leaf->name, type_p, &leaf->type,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002674 leaf->units ? NULL : &leaf->units, &dflt));
2675
2676 /* store default value, if any */
2677 if (dflt && !(leaf->flags & LYS_SET_DFLT)) {
2678 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, dflt));
2679 }
2680
aPiecekc6526b42021-07-12 15:21:39 +02002681 leafrefs_set = ctx->compile_opts & LYS_COMPILE_DISABLED ? &ctx->unres->disabled_leafrefs : &ctx->unres->leafrefs;
2682 if ((leaf->type->basetype == LY_TYPE_LEAFREF) && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002683 /* 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 +02002684 LY_CHECK_RET(ly_set_add(leafrefs_set, leaf, 0, NULL));
2685 } else if ((leaf->type->basetype == LY_TYPE_UNION) && !(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002686 LY_ARRAY_COUNT_TYPE u;
2687 LY_ARRAY_FOR(((struct lysc_type_union *)leaf->type)->types, u) {
2688 if (((struct lysc_type_union *)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
2689 /* 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 +02002690 LY_CHECK_RET(ly_set_add(leafrefs_set, leaf, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002691 }
2692 }
2693 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
2694 if ((leaf->nodetype == LYS_LEAFLIST) && (ctx->pmod->version < LYS_VERSION_1_1)) {
Michal Vaskoa99b3572021-02-01 11:54:58 +01002695 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 +02002696 return LY_EVALID;
2697 }
2698 }
2699
2700 return LY_SUCCESS;
2701}
2702
2703/**
2704 * @brief Compile parsed leaf node information.
2705 * @param[in] ctx Compile context
2706 * @param[in] pnode Parsed leaf node.
2707 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2708 * is enriched with the leaf-specific information.
2709 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2710 */
2711static LY_ERR
2712lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2713{
2714 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf *)pnode;
2715 struct lysc_node_leaf *leaf = (struct lysc_node_leaf *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002716 LY_ERR ret = LY_SUCCESS;
2717
Michal Vasko5347e3a2020-11-03 17:14:57 +01002718 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002719 if (leaf_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002720 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002721 ret = ly_set_add(&ctx->unres->xpath, leaf, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002722 LY_CHECK_GOTO(ret, done);
2723 }
2724 if (leaf_p->units) {
2725 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, leaf_p->units, 0, &leaf->units), done);
2726 leaf->flags |= LYS_SET_UNITS;
2727 }
2728
2729 /* compile type */
2730 ret = lys_compile_node_type(ctx, pnode, &leaf_p->type, leaf);
2731 LY_CHECK_GOTO(ret, done);
2732
2733 /* store/update default value */
2734 if (leaf_p->dflt.str) {
2735 LY_CHECK_RET(lysc_unres_leaf_dflt_add(ctx, leaf, &leaf_p->dflt));
2736 leaf->flags |= LYS_SET_DFLT;
2737 }
2738
2739 /* checks */
2740 if ((leaf->flags & LYS_SET_DFLT) && (leaf->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002741 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002742 "Invalid mandatory leaf with a default value.");
2743 return LY_EVALID;
2744 }
2745
2746done:
2747 return ret;
2748}
2749
2750/**
2751 * @brief Compile parsed leaf-list node information.
2752 * @param[in] ctx Compile context
2753 * @param[in] pnode Parsed leaf-list node.
2754 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
2755 * is enriched with the leaf-list-specific information.
2756 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2757 */
2758static LY_ERR
2759lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
2760{
2761 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist *)pnode;
2762 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002763 LY_ERR ret = LY_SUCCESS;
2764
Michal Vasko5347e3a2020-11-03 17:14:57 +01002765 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02002766 if (llist_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002767 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01002768 ret = ly_set_add(&ctx->unres->xpath, llist, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002769 LY_CHECK_GOTO(ret, done);
2770 }
2771 if (llist_p->units) {
2772 LY_CHECK_GOTO(ret = lydict_insert(ctx->ctx, llist_p->units, 0, &llist->units), done);
2773 llist->flags |= LYS_SET_UNITS;
2774 }
2775
2776 /* compile type */
2777 ret = lys_compile_node_type(ctx, pnode, &llist_p->type, (struct lysc_node_leaf *)llist);
2778 LY_CHECK_GOTO(ret, done);
2779
2780 /* store/update default values */
2781 if (llist_p->dflts) {
2782 if (ctx->pmod->version < LYS_VERSION_1_1) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002783 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002784 "Leaf-list default values are allowed only in YANG 1.1 modules.");
2785 return LY_EVALID;
2786 }
2787
2788 LY_CHECK_GOTO(lysc_unres_llist_dflts_add(ctx, llist, llist_p->dflts), done);
2789 llist->flags |= LYS_SET_DFLT;
2790 }
2791
2792 llist->min = llist_p->min;
2793 if (llist->min) {
2794 llist->flags |= LYS_MAND_TRUE;
2795 }
Michal Vaskoe78faec2021-04-08 17:24:43 +02002796 llist->max = llist_p->max ? llist_p->max : UINT32_MAX;
2797
2798 if (llist->flags & LYS_CONFIG_R) {
2799 /* state leaf-list is always ordered-by user */
2800 llist->flags &= ~LYS_ORDBY_SYSTEM;
2801 llist->flags |= LYS_ORDBY_USER;
2802 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002803
2804 /* checks */
2805 if ((llist->flags & LYS_SET_DFLT) && (llist->flags & LYS_MAND_TRUE)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002806 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 +02002807 return LY_EVALID;
2808 }
2809
2810 if (llist->min > llist->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002811 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Leaf-list min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002812 llist->min, llist->max);
2813 return LY_EVALID;
2814 }
2815
2816done:
2817 return ret;
2818}
2819
2820LY_ERR
2821lysc_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 +02002822 const struct lys_module *cur_mod, LY_VALUE_FORMAT format, void *prefix_data, uint16_t nodetype,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002823 const struct lysc_node **target, uint16_t *result_flag)
2824{
2825 LY_ERR ret = LY_EVALID;
2826 const char *name, *prefix, *id;
2827 size_t name_len, prefix_len;
Radek Krejci2b18bf12020-11-06 11:20:20 +01002828 const struct lys_module *mod = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002829 const char *nodeid_type;
2830 uint32_t getnext_extra_flag = 0;
2831 uint16_t current_nodetype = 0;
2832
2833 assert(nodeid);
2834 assert(target);
2835 assert(result_flag);
2836 *target = NULL;
2837 *result_flag = 0;
2838
2839 id = nodeid;
2840
2841 if (ctx_node) {
2842 /* descendant-schema-nodeid */
2843 nodeid_type = "descendant";
2844
2845 if (*id == '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002846 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002847 "Invalid descendant-schema-nodeid value \"%.*s\" - absolute-schema-nodeid used.",
Radek Krejci422afb12021-03-04 16:38:16 +01002848 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002849 return LY_EVALID;
2850 }
2851 } else {
2852 /* absolute-schema-nodeid */
2853 nodeid_type = "absolute";
2854
2855 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002856 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002857 "Invalid absolute-schema-nodeid value \"%.*s\" - missing starting \"/\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002858 (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002859 return LY_EVALID;
2860 }
2861 ++id;
2862 }
2863
2864 while (*id && (ret = ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len)) == LY_SUCCESS) {
2865 if (prefix) {
2866 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, format, prefix_data);
2867 if (!mod) {
2868 /* module must always be found */
2869 assert(prefix);
Radek Krejci2efc45b2020-12-22 16:25:44 +01002870 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002871 "Invalid %s-schema-nodeid value \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci422afb12021-03-04 16:38:16 +01002872 nodeid_type, (int)(id - nodeid), nodeid, (int)prefix_len, prefix, LYSP_MODULE_NAME(ctx->pmod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002873 return LY_ENOTFOUND;
2874 }
2875 } else {
2876 switch (format) {
Radek Krejci8df109d2021-04-23 12:19:08 +02002877 case LY_VALUE_SCHEMA:
2878 case LY_VALUE_SCHEMA_RESOLVED:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002879 /* use the current module */
2880 mod = cur_mod;
2881 break;
Radek Krejci8df109d2021-04-23 12:19:08 +02002882 case LY_VALUE_JSON:
Radek Krejcif9943642021-04-26 10:18:21 +02002883 case LY_VALUE_LYB:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002884 if (!ctx_node) {
2885 LOGINT_RET(ctx->ctx);
2886 }
2887 /* inherit the module of the previous context node */
2888 mod = ctx_node->module;
2889 break;
Radek Krejci224d4b42021-04-23 13:54:59 +02002890 case LY_VALUE_CANON:
Radek Krejci8df109d2021-04-23 12:19:08 +02002891 case LY_VALUE_XML:
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002892 /* not really defined */
2893 LOGINT_RET(ctx->ctx);
2894 }
2895 }
2896
2897 if (ctx_node && (ctx_node->nodetype & (LYS_RPC | LYS_ACTION))) {
2898 /* move through input/output manually */
2899 if (mod != ctx_node->module) {
Radek Krejci422afb12021-03-04 16:38:16 +01002900 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2901 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002902 return LY_ENOTFOUND;
2903 }
2904 if (!ly_strncmp("input", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002905 ctx_node = &((struct lysc_node_action *)ctx_node)->input.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002906 } else if (!ly_strncmp("output", name, name_len)) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002907 ctx_node = &((struct lysc_node_action *)ctx_node)->output.node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002908 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
2909 } else {
2910 /* only input or output is valid */
2911 ctx_node = NULL;
2912 }
2913 } else {
2914 ctx_node = lys_find_child(ctx_node, mod, name, name_len, 0,
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01002915 getnext_extra_flag | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002916 getnext_extra_flag = 0;
2917 }
2918 if (!ctx_node) {
Radek Krejci422afb12021-03-04 16:38:16 +01002919 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid %s-schema-nodeid value \"%.*s\" - target node not found.",
2920 nodeid_type, (int)(id - nodeid), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002921 return LY_ENOTFOUND;
2922 }
2923 current_nodetype = ctx_node->nodetype;
2924
2925 if (current_nodetype == LYS_NOTIF) {
2926 (*result_flag) |= LYS_COMPILE_NOTIFICATION;
2927 } else if (current_nodetype == LYS_INPUT) {
2928 (*result_flag) |= LYS_COMPILE_RPC_INPUT;
2929 } else if (current_nodetype == LYS_OUTPUT) {
2930 (*result_flag) |= LYS_COMPILE_RPC_OUTPUT;
2931 }
2932
2933 if (!*id || (nodeid_len && ((size_t)(id - nodeid) >= nodeid_len))) {
2934 break;
2935 }
2936 if (*id != '/') {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002937 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002938 "Invalid %s-schema-nodeid value \"%.*s\" - missing \"/\" as node-identifier separator.",
Radek Krejci422afb12021-03-04 16:38:16 +01002939 nodeid_type, (int)(id - nodeid + 1), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002940 return LY_EVALID;
2941 }
2942 ++id;
2943 }
2944
2945 if (ret == LY_SUCCESS) {
2946 *target = ctx_node;
2947 if (nodetype && !(current_nodetype & nodetype)) {
2948 return LY_EDENIED;
2949 }
2950 } else {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002951 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002952 "Invalid %s-schema-nodeid value \"%.*s\" - unexpected end of expression.",
Radek Krejci422afb12021-03-04 16:38:16 +01002953 nodeid_type, (int)(nodeid_len ? nodeid_len : strlen(nodeid)), nodeid);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002954 }
2955
2956 return ret;
2957}
2958
2959/**
2960 * @brief Compile information about list's uniques.
2961 * @param[in] ctx Compile context.
2962 * @param[in] uniques Sized array list of unique statements.
2963 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
2964 * @return LY_ERR value.
2965 */
2966static LY_ERR
2967lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lysp_qname *uniques, struct lysc_node_list *list)
2968{
2969 LY_ERR ret = LY_SUCCESS;
2970 struct lysc_node_leaf **key, ***unique;
2971 struct lysc_node *parent;
2972 const char *keystr, *delim;
2973 size_t len;
2974 LY_ARRAY_COUNT_TYPE v;
2975 int8_t config; /* -1 - not yet seen; 0 - LYS_CONFIG_R; 1 - LYS_CONFIG_W */
2976 uint16_t flags;
2977
2978 LY_ARRAY_FOR(uniques, v) {
2979 config = -1;
2980 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
2981 keystr = uniques[v].str;
2982 while (keystr) {
2983 delim = strpbrk(keystr, " \t\n");
2984 if (delim) {
2985 len = delim - keystr;
2986 while (isspace(*delim)) {
2987 ++delim;
2988 }
2989 } else {
2990 len = strlen(keystr);
2991 }
2992
2993 /* unique node must be present */
2994 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01002995 ret = lysc_resolve_schema_nodeid(ctx, keystr, len, &list->node, uniques[v].mod->mod,
Radek Krejci8df109d2021-04-23 12:19:08 +02002996 LY_VALUE_SCHEMA, (void *)uniques[v].mod, LYS_LEAF, (const struct lysc_node **)key, &flags);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02002997 if (ret != LY_SUCCESS) {
2998 if (ret == LY_EDENIED) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01002999 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003000 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci422afb12021-03-04 16:38:16 +01003001 (int)len, keystr, lys_nodetype2str((*key)->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003002 }
3003 return LY_EVALID;
3004 } else if (flags) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003005 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003006 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Radek Krejci422afb12021-03-04 16:38:16 +01003007 (int)len, keystr, flags & LYS_IS_NOTIF ? "notification" : "RPC/action");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003008 return LY_EVALID;
3009 }
3010
3011 /* all referenced leafs must be of the same config type */
3012 if ((config != -1) && ((((*key)->flags & LYS_CONFIG_W) && (config == 0)) ||
3013 (((*key)->flags & LYS_CONFIG_R) && (config == 1)))) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003014 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003015 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v].str);
3016 return LY_EVALID;
3017 } else if ((*key)->flags & LYS_CONFIG_W) {
3018 config = 1;
3019 } else { /* LYS_CONFIG_R */
3020 config = 0;
3021 }
3022
3023 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
3024 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
3025 if (parent->nodetype == LYS_LIST) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003026 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003027 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v].str, parent->name);
3028 return LY_EVALID;
3029 }
3030 }
3031
3032 /* check status */
3033 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3034 (*key)->flags, (*key)->module, (*key)->name));
3035
3036 /* mark leaf as unique */
3037 (*key)->flags |= LYS_UNIQUE;
3038
3039 /* next unique value in line */
3040 keystr = delim;
3041 }
3042 /* next unique definition */
3043 }
3044
3045 return LY_SUCCESS;
3046}
3047
3048/**
3049 * @brief Compile parsed list node information.
3050 * @param[in] ctx Compile context
3051 * @param[in] pnode Parsed list node.
3052 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3053 * is enriched with the list-specific information.
3054 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3055 */
3056static LY_ERR
3057lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3058{
3059 struct lysp_node_list *list_p = (struct lysp_node_list *)pnode;
3060 struct lysc_node_list *list = (struct lysc_node_list *)node;
3061 struct lysp_node *child_p;
3062 struct lysc_node_leaf *key, *prev_key = NULL;
3063 size_t len;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003064 const char *keystr, *delim;
3065 LY_ERR ret = LY_SUCCESS;
3066
3067 list->min = list_p->min;
3068 if (list->min) {
3069 list->flags |= LYS_MAND_TRUE;
3070 }
3071 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3072
3073 LY_LIST_FOR(list_p->child, child_p) {
3074 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3075 }
3076
Michal Vasko5347e3a2020-11-03 17:14:57 +01003077 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02003078 if (list_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003079 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003080 LY_CHECK_RET(ly_set_add(&ctx->unres->xpath, list, 0, NULL));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003081 }
3082
3083 /* keys */
3084 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003085 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003086 return LY_EVALID;
3087 }
3088
3089 /* find all the keys (must be direct children) */
3090 keystr = list_p->key;
3091 if (!keystr) {
3092 /* keyless list */
Michal Vaskoe78faec2021-04-08 17:24:43 +02003093 list->flags &= ~LYS_ORDBY_SYSTEM;
3094 list->flags |= LYS_KEYLESS | LYS_ORDBY_USER;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003095 }
3096 while (keystr) {
3097 delim = strpbrk(keystr, " \t\n");
3098 if (delim) {
3099 len = delim - keystr;
3100 while (isspace(*delim)) {
3101 ++delim;
3102 }
3103 } else {
3104 len = strlen(keystr);
3105 }
3106
3107 /* key node must be present */
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003108 key = (struct lysc_node_leaf *)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE);
3109 if (!key) {
Radek Krejci422afb12021-03-04 16:38:16 +01003110 LOGVAL(ctx->ctx, LYVE_REFERENCE, "The list's key \"%.*s\" not found.", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003111 return LY_EVALID;
3112 }
3113 /* keys must be unique */
3114 if (key->flags & LYS_KEY) {
3115 /* the node was already marked as a key */
Radek Krejci422afb12021-03-04 16:38:16 +01003116 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Duplicated key identifier \"%.*s\".", (int)len, keystr);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003117 return LY_EVALID;
3118 }
3119
Radek Krejcia6016992021-03-03 10:13:41 +01003120 lysc_update_path(ctx, list->module, key->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003121 /* key must have the same config flag as the list itself */
3122 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003123 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003124 return LY_EVALID;
3125 }
3126 if (ctx->pmod->version < LYS_VERSION_1_1) {
3127 /* YANG 1.0 denies key to be of empty type */
3128 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003129 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003130 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
3131 return LY_EVALID;
3132 }
3133 } else {
3134 /* when and if-feature are illegal on list keys */
3135 if (key->when) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003136 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003137 "List's key must not have any \"when\" statement.");
3138 return LY_EVALID;
3139 }
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003140 /* TODO check key, it cannot have any if-features */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003141 }
3142
3143 /* check status */
3144 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3145 key->flags, key->module, key->name));
3146
3147 /* ignore default values of the key */
3148 if (key->dflt) {
3149 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3150 lysc_type_free(ctx->ctx, (struct lysc_type *)key->dflt->realtype);
3151 free(key->dflt);
3152 key->dflt = NULL;
3153 }
3154 /* mark leaf as key */
3155 key->flags |= LYS_KEY;
3156
3157 /* move it to the correct position */
3158 if ((prev_key && ((struct lysc_node *)prev_key != key->prev)) || (!prev_key && key->prev->next)) {
3159 /* fix links in closest previous siblings of the key */
3160 if (key->next) {
3161 key->next->prev = key->prev;
3162 } else {
3163 /* last child */
3164 list->child->prev = key->prev;
3165 }
3166 if (key->prev->next) {
3167 key->prev->next = key->next;
3168 }
3169 /* fix links in the key */
3170 if (prev_key) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003171 key->prev = &prev_key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003172 key->next = prev_key->next;
3173 } else {
3174 key->prev = list->child->prev;
3175 key->next = list->child;
3176 }
3177 /* fix links in closes future siblings of the key */
3178 if (prev_key) {
3179 if (prev_key->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003180 prev_key->next->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003181 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003182 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003183 }
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003184 prev_key->next = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003185 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003186 list->child->prev = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003187 }
3188 /* fix links in parent */
3189 if (!key->prev->next) {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003190 list->child = &key->node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003191 }
3192 }
3193
3194 /* next key value */
3195 prev_key = key;
3196 keystr = delim;
3197 lysc_update_path(ctx, NULL, NULL);
3198 }
3199
3200 /* uniques */
3201 if (list_p->uniques) {
3202 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list_p->uniques, list));
3203 }
3204
Radek Krejci2a9fc652021-01-22 17:44:34 +01003205 LY_LIST_FOR((struct lysp_node *)list_p->actions, child_p) {
3206 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3207 LY_CHECK_GOTO(ret, done);
3208 }
3209 LY_LIST_FOR((struct lysp_node *)list_p->notifs, child_p) {
3210 ret = lys_compile_node(ctx, child_p, node, 0, NULL);
3211 LY_CHECK_GOTO(ret, done);
3212 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003213
3214 /* checks */
3215 if (list->min > list->max) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003216 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "List min-elements %u is bigger than max-elements %u.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003217 list->min, list->max);
3218 return LY_EVALID;
3219 }
3220
3221done:
3222 return ret;
3223}
3224
3225/**
3226 * @brief Do some checks and set the default choice's case.
3227 *
3228 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3229 *
3230 * @param[in] ctx Compile context.
3231 * @param[in] dflt Name of the default branch. Can even contain a prefix.
3232 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3233 * @return LY_ERR value.
3234 */
3235static LY_ERR
3236lys_compile_node_choice_dflt(struct lysc_ctx *ctx, struct lysp_qname *dflt, struct lysc_node_choice *ch)
3237{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003238 struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003239 const struct lys_module *mod;
3240 const char *prefix = NULL, *name;
3241 size_t prefix_len = 0;
3242
3243 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3244 name = strchr(dflt->str, ':');
3245 if (name) {
3246 prefix = dflt->str;
3247 prefix_len = name - prefix;
3248 ++name;
3249 } else {
3250 name = dflt->str;
3251 }
3252 if (prefix) {
Radek Krejci8df109d2021-04-23 12:19:08 +02003253 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, (void *)dflt->mod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003254 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003255 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Default case prefix \"%.*s\" not found "
Radek Krejci422afb12021-03-04 16:38:16 +01003256 "in imports of \"%s\".", (int)prefix_len, prefix, LYSP_MODULE_NAME(dflt->mod));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003257 return LY_EVALID;
3258 }
3259 } else {
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003260 mod = ch->module;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003261 }
3262
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003263 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 +02003264 if (!ch->dflt) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003265 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003266 "Default case \"%s\" not found.", dflt->str);
3267 return LY_EVALID;
3268 }
3269
3270 /* no mandatory nodes directly under the default case */
3271 LY_LIST_FOR(ch->dflt->child, iter) {
3272 if (iter->parent != (struct lysc_node *)ch->dflt) {
3273 break;
3274 }
3275 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003276 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003277 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt->str);
3278 return LY_EVALID;
3279 }
3280 }
3281
3282 if (ch->flags & LYS_MAND_TRUE) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003283 LOGVAL(ctx->ctx, LYVE_SEMANTICS, "Invalid mandatory choice with a default case.");
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003284 return LY_EVALID;
3285 }
3286
3287 ch->dflt->flags |= LYS_SET_DFLT;
3288 return LY_SUCCESS;
3289}
3290
3291LY_ERR
3292lys_compile_node_choice_child(struct lysc_ctx *ctx, struct lysp_node *child_p, struct lysc_node *node,
3293 struct ly_set *child_set)
3294{
3295 LY_ERR ret = LY_SUCCESS;
3296 struct lysp_node *child_p_next = child_p->next;
3297 struct lysp_node_case *cs_p;
aPiecekaa320c92021-05-21 07:34:24 +02003298 struct lysc_node_case *compiled_case;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003299
3300 if (child_p->nodetype == LYS_CASE) {
3301 /* standard case under choice */
3302 ret = lys_compile_node(ctx, child_p, node, 0, child_set);
3303 } else {
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003304 /* we need the implicit case first, so create a fake parsed (shorthand) case */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003305 cs_p = calloc(1, sizeof *cs_p);
3306 cs_p->nodetype = LYS_CASE;
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003307 DUP_STRING_GOTO(ctx->ctx, child_p->name, cs_p->name, ret, revert_sh_case);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003308 cs_p->child = child_p;
3309
3310 /* make the child the only case child */
3311 child_p->next = NULL;
3312
3313 /* compile it normally */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003314 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 +02003315
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003316 if (((struct lysc_node_choice *)node)->cases) {
3317 /* get last case node */
3318 compiled_case = (struct lysc_node_case *)((struct lysc_node_choice *)node)->cases->prev;
aPiecek082c7dc2021-05-20 08:55:07 +02003319
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003320 if (ctx->ctx->flags & LY_CTX_SET_PRIV_PARSED) {
3321 /* Compiled case node cannot point to his corresponding parsed node
aPiecek8e351382021-06-25 14:13:55 +02003322 * because it exists temporarily. Therefore, it must be set to NULL.
3323 */
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003324 compiled_case->priv = NULL;
3325 }
aPiecekaa320c92021-05-21 07:34:24 +02003326
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003327 /* The status is copied from his child and not from his parent as usual. */
3328 if (compiled_case->child) {
3329 compiled_case->flags &= ~LYS_STATUS_MASK;
3330 compiled_case->flags |= LYS_STATUS_MASK & compiled_case->child->flags;
3331 }
3332 } /* else it was removed by a deviation */
aPiecekaa320c92021-05-21 07:34:24 +02003333
Michal Vaskoa6cf80a2021-06-25 07:42:19 +02003334revert_sh_case:
3335 /* free the parsed shorthand case and correct pointers back */
aPiecekaa320c92021-05-21 07:34:24 +02003336 cs_p->child = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003337 lysp_node_free(ctx->ctx, (struct lysp_node *)cs_p);
3338 child_p->next = child_p_next;
3339 }
3340
3341 return ret;
3342}
3343
3344/**
3345 * @brief Compile parsed choice node information.
3346 *
3347 * @param[in] ctx Compile context
3348 * @param[in] pnode Parsed choice node.
3349 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3350 * is enriched with the choice-specific information.
3351 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3352 */
3353static LY_ERR
3354lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3355{
3356 struct lysp_node_choice *ch_p = (struct lysp_node_choice *)pnode;
3357 struct lysc_node_choice *ch = (struct lysc_node_choice *)node;
3358 struct lysp_node *child_p;
3359 LY_ERR ret = LY_SUCCESS;
3360
3361 assert(node->nodetype == LYS_CHOICE);
3362
3363 LY_LIST_FOR(ch_p->child, child_p) {
3364 LY_CHECK_RET(lys_compile_node_choice_child(ctx, child_p, node, NULL));
3365 }
3366
3367 /* default branch */
3368 if (ch_p->dflt.str) {
3369 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, &ch_p->dflt, ch));
3370 }
3371
3372 return ret;
3373}
3374
3375/**
3376 * @brief Compile parsed anydata or anyxml node information.
3377 * @param[in] ctx Compile context
3378 * @param[in] pnode Parsed anydata or anyxml node.
3379 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3380 * is enriched with the any-specific information.
3381 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3382 */
3383static LY_ERR
3384lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3385{
3386 struct lysp_node_anydata *any_p = (struct lysp_node_anydata *)pnode;
3387 struct lysc_node_anydata *any = (struct lysc_node_anydata *)node;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003388 LY_ERR ret = LY_SUCCESS;
3389
Michal Vasko5347e3a2020-11-03 17:14:57 +01003390 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, lys_compile_must, ret, done);
Michal Vasko7c565922021-06-10 14:58:27 +02003391 if (any_p->musts && !(ctx->compile_opts & (LYS_COMPILE_GROUPING | LYS_COMPILE_DISABLED))) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003392 /* do not check "must" semantics in a grouping */
Michal Vasko405cc9e2020-12-01 12:01:27 +01003393 ret = ly_set_add(&ctx->unres->xpath, any, 0, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003394 LY_CHECK_GOTO(ret, done);
3395 }
3396
Radek Krejci91531e12021-02-08 19:57:54 +01003397 if (any->flags & LYS_CONFIG_W) {
Michal Vasko5676f4e2021-04-06 17:14:45 +02003398 LOGVRB("Use of %s to define configuration data is not recommended. %s",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003399 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML), ctx->path);
3400 }
3401done:
3402 return ret;
3403}
3404
3405/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003406 * @brief Prepare the case structure in choice node for the new data node.
3407 *
3408 * 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
3409 * created in the choice when the first child was processed.
3410 *
3411 * @param[in] ctx Compile context.
3412 * @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,
3413 * it is the LYS_CHOICE, LYS_AUGMENT or LYS_GROUPING node.
3414 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
3415 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
3416 * @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,
3417 * it is linked from the case structure only in case it is its first child.
3418 */
3419static LY_ERR
3420lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *node)
3421{
3422 struct lysp_node *child_p;
3423 struct lysp_node_case *cs_p = (struct lysp_node_case *)pnode;
3424
3425 if (pnode->nodetype & (LYS_CHOICE | LYS_AUGMENT | LYS_GROUPING)) {
3426 /* we have to add an implicit case node into the parent choice */
3427 } else if (pnode->nodetype == LYS_CASE) {
3428 /* explicit parent case */
3429 LY_LIST_FOR(cs_p->child, child_p) {
3430 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0, NULL));
3431 }
3432 } else {
3433 LOGINT_RET(ctx->ctx);
3434 }
3435
3436 return LY_SUCCESS;
3437}
3438
3439void
3440lys_compile_mandatory_parents(struct lysc_node *parent, ly_bool add)
3441{
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003442 const struct lysc_node *iter;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003443
3444 if (add) { /* set flag */
3445 for ( ; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
3446 parent = parent->parent) {
3447 parent->flags |= LYS_MAND_TRUE;
3448 }
3449 } else { /* unset flag */
3450 for ( ; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Michal Vasko544e58a2021-01-28 14:33:41 +01003451 for (iter = lysc_node_child(parent); iter; iter = iter->next) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003452 if (iter->flags & LYS_MAND_TRUE) {
3453 /* there is another mandatory node */
3454 return;
3455 }
3456 }
3457 /* unset mandatory flag - there is no mandatory children in the non-presence container */
3458 parent->flags &= ~LYS_MAND_TRUE;
3459 }
3460 }
3461}
3462
3463/**
Radek Krejciabd33a42021-01-20 17:18:59 +01003464 * @brief Get the grouping with the specified name from given groupings sized array.
Radek Krejci2a9fc652021-01-22 17:44:34 +01003465 * @param[in] grps Linked list of groupings.
Radek Krejciabd33a42021-01-20 17:18:59 +01003466 * @param[in] name Name of the grouping to find,
3467 * @return NULL when there is no grouping with the specified name
3468 * @return Pointer to the grouping of the specified @p name.
3469 */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003470static struct lysp_node_grp *
3471match_grouping(struct lysp_node_grp *grps, const char *name)
Radek Krejciabd33a42021-01-20 17:18:59 +01003472{
Radek Krejci2a9fc652021-01-22 17:44:34 +01003473 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003474
Radek Krejci2a9fc652021-01-22 17:44:34 +01003475 LY_LIST_FOR(grps, grp) {
3476 if (!strcmp(grp->name, name)) {
3477 return grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003478 }
3479 }
3480
3481 return NULL;
3482}
3483
3484/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003485 * @brief Find grouping for a uses.
3486 *
3487 * @param[in] ctx Compile context.
3488 * @param[in] uses_p Parsed uses node.
3489 * @param[out] gpr_p Found grouping on success.
3490 * @param[out] grp_pmod Module of @p grp_p on success.
3491 * @return LY_ERR value.
3492 */
3493static LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003494lys_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 +02003495 struct lysp_module **grp_pmod)
3496{
3497 struct lysp_node *pnode;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003498 struct lysp_node_grp *grp;
Radek Krejciabd33a42021-01-20 17:18:59 +01003499 LY_ARRAY_COUNT_TYPE u;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003500 const char *id, *name, *prefix, *local_pref;
3501 size_t prefix_len, name_len;
3502 struct lysp_module *pmod, *found = NULL;
Michal Vaskob2d55bf2020-11-02 15:42:43 +01003503 const struct lys_module *mod;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003504
3505 *grp_p = NULL;
3506 *grp_pmod = NULL;
3507
3508 /* search for the grouping definition */
3509 id = uses_p->name;
3510 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
3511 local_pref = ctx->pmod->is_submod ? ((struct lysp_submodule *)ctx->pmod)->prefix : ctx->pmod->mod->prefix;
3512 if (!prefix || !ly_strncmp(local_pref, prefix, prefix_len)) {
3513 /* current module, search local groupings first */
Radek Krejciabd33a42021-01-20 17:18:59 +01003514 pmod = ctx->pmod->mod->parsed; /* make sure that we will start in main_module, not submodule */
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003515 for (pnode = uses_p->parent; !found && pnode; pnode = pnode->parent) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003516 if ((grp = match_grouping((struct lysp_node_grp *)lysp_node_groupings(pnode), name))) {
3517 found = ctx->pmod;
3518 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003519 }
3520 }
3521 } else {
3522 /* foreign module, find it first */
Radek Krejci8df109d2021-04-23 12:19:08 +02003523 mod = ly_resolve_prefix(ctx->ctx, prefix, prefix_len, LY_VALUE_SCHEMA, ctx->pmod);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003524 if (!mod) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003525 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003526 "Invalid prefix used for grouping reference.", uses_p->name);
3527 return LY_EVALID;
3528 }
3529 pmod = mod->parsed;
3530 }
3531
3532 if (!found) {
3533 /* search in top-level groupings of the main module ... */
Radek Krejciabd33a42021-01-20 17:18:59 +01003534 if ((grp = match_grouping(pmod->groupings, name))) {
3535 found = pmod;
3536 } else {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003537 /* ... and all the submodules */
3538 LY_ARRAY_FOR(pmod->includes, u) {
Radek Krejciabd33a42021-01-20 17:18:59 +01003539 if ((grp = match_grouping(pmod->includes[u].submodule->groupings, name))) {
3540 found = (struct lysp_module *)pmod->includes[u].submodule;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003541 break;
3542 }
3543 }
3544 }
3545 }
3546 if (!found) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003547 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003548 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
3549 return LY_EVALID;
3550 }
3551
Michal Vasko7c565922021-06-10 14:58:27 +02003552 if (!(ctx->compile_opts & LYS_COMPILE_GROUPING)) {
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003553 /* remember that the grouping is instantiated to avoid its standalone validation */
3554 grp->flags |= LYS_USED_GRP;
3555 }
3556
3557 *grp_p = grp;
3558 *grp_pmod = found;
3559 return LY_SUCCESS;
3560}
3561
3562/**
Michal Vaskodfd254d2021-06-24 09:24:59 +02003563 * @brief Special bits combination marking the uses_status value and propagated by ::lys_compile_uses() function.
3564 */
3565#define LYS_STATUS_USES LYS_CONFIG_MASK
3566
3567/**
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003568 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
3569 * If present, also apply uses's modificators.
3570 *
3571 * @param[in] ctx Compile context
3572 * @param[in] uses_p Parsed uses schema node.
3573 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
3574 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
3575 * the compile context.
3576 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3577 */
3578static LY_ERR
3579lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent, struct ly_set *child_set)
3580{
3581 struct lysp_node *pnode;
3582 struct lysc_node *child;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003583 struct lysp_node_grp *grp = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003584 uint32_t i, grp_stack_count;
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003585 uint16_t uses_flags;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003586 struct lysp_module *grp_mod, *mod_old = ctx->pmod;
3587 LY_ERR ret = LY_SUCCESS;
3588 struct lysc_when *when_shared = NULL;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003589 struct ly_set uses_child_set = {0};
3590
3591 /* find the referenced grouping */
3592 LY_CHECK_RET(lys_compile_uses_find_grouping(ctx, uses_p, &grp, &grp_mod));
3593
3594 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
3595 grp_stack_count = ctx->groupings.count;
3596 LY_CHECK_RET(ly_set_add(&ctx->groupings, (void *)grp, 0, NULL));
3597 if (grp_stack_count == ctx->groupings.count) {
3598 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
Radek Krejci2efc45b2020-12-22 16:25:44 +01003599 LOGVAL(ctx->ctx, LYVE_REFERENCE,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003600 "Grouping \"%s\" references itself through a uses statement.", grp->name);
3601 return LY_EVALID;
3602 }
3603
3604 /* check status */
3605 ret = lysc_check_status(ctx, uses_p->flags, ctx->pmod, uses_p->name, grp->flags, grp_mod, grp->name);
3606 LY_CHECK_GOTO(ret, cleanup);
3607
3608 /* compile any augments and refines so they can be applied during the grouping nodes compilation */
3609 ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
3610 LY_CHECK_GOTO(ret, cleanup);
3611
3612 /* switch context's parsed module being processed */
3613 ctx->pmod = grp_mod;
3614
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003615 /* compile special uses status flags */
3616 uses_flags = uses_p->flags;
3617 ret = lys_compile_status(ctx, &uses_flags, "<uses>", parent ? parent->flags : 0, parent ? parent->name : NULL, 0);
3618 LY_CHECK_GOTO(ret, cleanup);
3619 uses_flags |= LYS_STATUS_USES;
3620
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003621 /* compile data nodes */
Radek Krejci01180ac2021-01-27 08:48:22 +01003622 LY_LIST_FOR(grp->child, pnode) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003623 ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003624 LY_CHECK_GOTO(ret, cleanup);
3625 }
3626
3627 if (child_set) {
3628 /* add these children to our compiled child_set as well since uses is a schema-only node */
3629 LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
3630 }
3631
3632 if (uses_p->when) {
3633 /* pass uses's when to all the data children */
3634 for (i = 0; i < uses_child_set.count; ++i) {
3635 child = uses_child_set.snodes[i];
3636
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003637 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 +02003638 LY_CHECK_GOTO(ret, cleanup);
3639 }
3640 }
3641
3642 /* compile actions */
3643 if (grp->actions) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003644 struct lysc_node_action **actions;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003645 actions = parent ? lysc_node_actions_p(parent) : &ctx->cur_mod->compiled->rpcs;
3646 if (!actions) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003647 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003648 grp->actions->name, lys_nodetype2str(grp->actions->nodetype),
3649 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003650 ret = LY_EVALID;
3651 goto cleanup;
3652 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003653 LY_LIST_FOR((struct lysp_node *)grp->actions, pnode) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003654 ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003655 LY_CHECK_GOTO(ret, cleanup);
3656 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003657
3658 if (uses_p->when) {
3659 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003660 LY_LIST_FOR((struct lysc_node *)*actions, child) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003661 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 +02003662 LY_CHECK_GOTO(ret, cleanup);
3663 }
3664 }
3665 }
3666
3667 /* compile notifications */
3668 if (grp->notifs) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003669 struct lysc_node_notif **notifs;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003670 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->cur_mod->compiled->notifs;
3671 if (!notifs) {
Radek Krejci2efc45b2020-12-22 16:25:44 +01003672 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Invalid child %s \"%s\" of uses parent %s \"%s\" node.",
Radek Krejci2a9fc652021-01-22 17:44:34 +01003673 grp->notifs->name, lys_nodetype2str(grp->notifs->nodetype),
3674 parent->name, lys_nodetype2str(parent->nodetype));
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003675 ret = LY_EVALID;
3676 goto cleanup;
3677 }
Radek Krejci2a9fc652021-01-22 17:44:34 +01003678
3679 LY_LIST_FOR((struct lysp_node *)grp->notifs, pnode) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003680 ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
Radek Krejci2a9fc652021-01-22 17:44:34 +01003681 LY_CHECK_GOTO(ret, cleanup);
3682 }
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003683
3684 if (uses_p->when) {
3685 /* inherit when */
Radek Krejci2a9fc652021-01-22 17:44:34 +01003686 LY_LIST_FOR((struct lysc_node *)*notifs, child) {
Michal Vasko10b6b1c2021-07-20 10:43:12 +02003687 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 +02003688 LY_CHECK_GOTO(ret, cleanup);
3689 }
3690 }
3691 }
3692
3693 /* check that all augments were applied */
3694 for (i = 0; i < ctx->uses_augs.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003695 if (((struct lysc_augment *)ctx->uses_augs.objs[i])->aug_p->parent != (struct lysp_node *)uses_p) {
3696 /* augment of some parent uses, irrelevant now */
3697 continue;
3698 }
3699
3700 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Augment target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003701 ((struct lysc_augment *)ctx->uses_augs.objs[i])->nodeid->expr, grp->name);
3702 ret = LY_ENOTFOUND;
3703 }
3704 LY_CHECK_GOTO(ret, cleanup);
3705
3706 /* check that all refines were applied */
3707 for (i = 0; i < ctx->uses_rfns.count; ++i) {
Michal Vaskod8655722021-01-12 15:20:36 +01003708 if (((struct lysc_refine *)ctx->uses_rfns.objs[i])->uses_p != uses_p) {
3709 /* refine of some paretn uses, irrelevant now */
3710 continue;
3711 }
3712
3713 LOGVAL(ctx->ctx, LYVE_REFERENCE, "Refine(s) target node \"%s\" in grouping \"%s\" was not found.",
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003714 ((struct lysc_refine *)ctx->uses_rfns.objs[i])->nodeid->expr, grp->name);
3715 ret = LY_ENOTFOUND;
3716 }
3717 LY_CHECK_GOTO(ret, cleanup);
3718
3719cleanup:
3720 /* reload previous context's parsed module being processed */
3721 ctx->pmod = mod_old;
3722
3723 /* remove the grouping from the stack for circular groupings dependency check */
3724 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
3725 assert(ctx->groupings.count == grp_stack_count);
3726
3727 ly_set_erase(&uses_child_set, NULL);
3728 return ret;
3729}
3730
3731static int
3732lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
3733{
3734 struct lysp_node *iter;
3735 int len = 0;
3736
3737 *path = NULL;
3738 for (iter = node; iter && len >= 0; iter = iter->parent) {
3739 char *s = *path;
3740 char *id;
3741
3742 switch (iter->nodetype) {
3743 case LYS_USES:
3744 LY_CHECK_RET(asprintf(&id, "{uses='%s'}", iter->name) == -1, -1);
3745 break;
3746 case LYS_GROUPING:
3747 LY_CHECK_RET(asprintf(&id, "{grouping='%s'}", iter->name) == -1, -1);
3748 break;
3749 case LYS_AUGMENT:
3750 LY_CHECK_RET(asprintf(&id, "{augment='%s'}", iter->name) == -1, -1);
3751 break;
3752 default:
3753 id = strdup(iter->name);
3754 break;
3755 }
3756
3757 if (!iter->parent) {
3758 /* print prefix */
3759 len = asprintf(path, "/%s:%s%s", ctx->cur_mod->name, id, s ? s : "");
3760 } else {
3761 /* prefix is the same as in parent */
3762 len = asprintf(path, "/%s%s", id, s ? s : "");
3763 }
3764 free(s);
3765 free(id);
3766 }
3767
3768 if (len < 0) {
3769 free(*path);
3770 *path = NULL;
3771 } else if (len == 0) {
3772 *path = strdup("/");
3773 len = 1;
3774 }
3775 return len;
3776}
3777
3778LY_ERR
Radek Krejci2a9fc652021-01-22 17:44:34 +01003779lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysp_node_grp *grp)
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003780{
3781 LY_ERR ret;
3782 char *path;
3783 int len;
3784
3785 struct lysp_node_uses fake_uses = {
3786 .parent = pnode,
3787 .nodetype = LYS_USES,
3788 .flags = 0, .next = NULL,
3789 .name = grp->name,
3790 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
3791 .refines = NULL, .augments = NULL
3792 };
3793 struct lysc_node_container fake_container = {
3794 .nodetype = LYS_CONTAINER,
3795 .flags = pnode ? (pnode->flags & LYS_FLAGS_COMPILED_MASK) : 0,
3796 .module = ctx->cur_mod,
Michal Vaskobd6de2b2020-10-22 14:45:03 +02003797 .parent = NULL, .next = NULL,
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003798 .prev = &fake_container.node,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003799 .name = "fake",
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003800 .dsc = NULL, .ref = NULL, .exts = NULL, .when = NULL,
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003801 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
3802 };
3803
3804 if (grp->parent) {
3805 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
3806 }
3807
3808 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
3809 if (len < 0) {
3810 LOGMEM(ctx->ctx);
3811 return LY_EMEM;
3812 }
3813 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
3814 ctx->path_len = (uint32_t)len;
3815 free(path);
3816
3817 lysc_update_path(ctx, NULL, "{grouping}");
3818 lysc_update_path(ctx, NULL, grp->name);
Michal Vasko14ed9cd2021-01-28 14:16:25 +01003819 ret = lys_compile_uses(ctx, &fake_uses, &fake_container.node, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003820 lysc_update_path(ctx, NULL, NULL);
3821 lysc_update_path(ctx, NULL, NULL);
3822
3823 ctx->path_len = 1;
3824 ctx->path[1] = '\0';
3825
3826 /* cleanup */
3827 lysc_node_container_free(ctx->ctx, &fake_container);
3828
3829 return ret;
3830}
3831
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003832LY_ERR
3833lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *pnode, struct lysc_node *parent, uint16_t uses_status,
3834 struct ly_set *child_set)
3835{
3836 LY_ERR ret = LY_SUCCESS;
3837 struct lysc_node *node = NULL;
Michal Vasko7c565922021-06-10 14:58:27 +02003838 uint32_t prev_opts = ctx->compile_opts;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003839
3840 LY_ERR (*node_compile_spec)(struct lysc_ctx *, struct lysp_node *, struct lysc_node *);
3841
3842 if (pnode->nodetype != LYS_USES) {
Radek Krejcia6016992021-03-03 10:13:41 +01003843 lysc_update_path(ctx, parent ? parent->module : NULL, pnode->name);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003844 } else {
3845 lysc_update_path(ctx, NULL, "{uses}");
3846 lysc_update_path(ctx, NULL, pnode->name);
3847 }
3848
3849 switch (pnode->nodetype) {
3850 case LYS_CONTAINER:
3851 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_container));
3852 node_compile_spec = lys_compile_node_container;
3853 break;
3854 case LYS_LEAF:
3855 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaf));
3856 node_compile_spec = lys_compile_node_leaf;
3857 break;
3858 case LYS_LIST:
3859 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_list));
3860 node_compile_spec = lys_compile_node_list;
3861 break;
3862 case LYS_LEAFLIST:
3863 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_leaflist));
3864 node_compile_spec = lys_compile_node_leaflist;
3865 break;
3866 case LYS_CHOICE:
3867 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_choice));
3868 node_compile_spec = lys_compile_node_choice;
3869 break;
3870 case LYS_CASE:
3871 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_case));
3872 node_compile_spec = lys_compile_node_case;
3873 break;
3874 case LYS_ANYXML:
3875 case LYS_ANYDATA:
3876 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_anydata));
3877 node_compile_spec = lys_compile_node_any;
3878 break;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003879 case LYS_RPC:
3880 case LYS_ACTION:
Michal Vasko7c565922021-06-10 14:58:27 +02003881 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003882 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3883 "Action \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02003884 (ctx->compile_opts & LYS_IS_NOTIF) ? "notification" : "another RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003885 return LY_EVALID;
3886 }
3887 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_action));
3888 node_compile_spec = lys_compile_node_action;
Michal Vasko7c565922021-06-10 14:58:27 +02003889 ctx->compile_opts |= LYS_COMPILE_NO_CONFIG;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003890 break;
3891 case LYS_NOTIF:
Michal Vasko7c565922021-06-10 14:58:27 +02003892 if (ctx->compile_opts & (LYS_IS_INPUT | LYS_IS_OUTPUT | LYS_IS_NOTIF)) {
Radek Krejci2a9fc652021-01-22 17:44:34 +01003893 LOGVAL(ctx->ctx, LYVE_SEMANTICS,
3894 "Notification \"%s\" is placed inside %s.", pnode->name,
Michal Vasko7c565922021-06-10 14:58:27 +02003895 (ctx->compile_opts & LYS_IS_NOTIF) ? "another notification" : "RPC/action");
Radek Krejci2a9fc652021-01-22 17:44:34 +01003896 return LY_EVALID;
3897 }
3898 node = (struct lysc_node *)calloc(1, sizeof(struct lysc_node_notif));
3899 node_compile_spec = lys_compile_node_notif;
Michal Vasko7c565922021-06-10 14:58:27 +02003900 ctx->compile_opts |= LYS_COMPILE_NOTIFICATION;
Radek Krejci2a9fc652021-01-22 17:44:34 +01003901 break;
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003902 case LYS_USES:
3903 ret = lys_compile_uses(ctx, (struct lysp_node_uses *)pnode, parent, child_set);
3904 lysc_update_path(ctx, NULL, NULL);
3905 lysc_update_path(ctx, NULL, NULL);
3906 return ret;
3907 default:
3908 LOGINT(ctx->ctx);
3909 return LY_EINT;
3910 }
3911 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
3912
Radek Krejci2a9fc652021-01-22 17:44:34 +01003913 ret = lys_compile_node_(ctx, pnode, parent, uses_status, node_compile_spec, node, child_set);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003914
Michal Vasko7c565922021-06-10 14:58:27 +02003915 ctx->compile_opts = prev_opts;
Michal Vasko7b1ad1a2020-11-02 15:41:27 +01003916 lysc_update_path(ctx, NULL, NULL);
Michal Vasko1a7a7bd2020-10-16 14:39:15 +02003917 return ret;
3918}