blob: 064e6cd847e34857fca82b638e016d237c5bf6c0 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 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
15#include "common.h"
16
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
26#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020027#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "set.h"
29#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020030#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "tree.h"
32#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010033#include "tree_schema_internal.h"
34#include "xpath.h"
35
36/**
37 * @brief Duplicate string into dictionary
38 * @param[in] CTX libyang context of the dictionary.
39 * @param[in] ORIG String to duplicate.
40 * @param[out] DUP Where to store the result.
41 */
42#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
43
Radek Krejciec4da802019-05-02 13:02:41 +020044#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010045 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010047 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010048 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020050 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010051 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejciec4da802019-05-02 13:02:41 +020055#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010056 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020061 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
Radek Krejci0935f412019-08-20 16:15:18 +020066#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
67 if (EXTS_P) { \
68 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \
69 for (uint32_t __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \
70 LY_ARRAY_INCREMENT(EXT_C); \
71 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE); \
72 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejciec4da802019-05-02 13:02:41 +020076#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010077 if (ARRAY_P) { \
78 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
79 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
80 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
81 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020082 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010083 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
84 } \
85 }
86
Radek Krejciec4da802019-05-02 13:02:41 +020087#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010088 if (MEMBER_P) { \
89 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
90 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020091 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010092 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
93 }
94
Radek Krejciec4da802019-05-02 13:02:41 +020095#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +010096 if (MEMBER_P) { \
97 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
98 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
99 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200100 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100101 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
102 }
103
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100104#define COMPILE_CHECK_UNIQUENESS_ARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100105 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100106 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
107 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100108 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
109 return LY_EVALID; \
110 } \
111 } \
112 }
113
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100114#define COMPILE_CHECK_UNIQUENESS_PARRAY(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
115 if (ARRAY) { \
116 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
117 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__]->MEMBER) == (void*)(IDENT)) { \
118 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
119 return LY_EVALID; \
120 } \
121 } \
122 }
123
124struct lysc_ext *
125lysc_ext_dup(struct lysc_ext *orig)
126{
127 ++orig->refcount;
128 return orig;
129}
130
Radek Krejci19a96102018-11-15 13:38:09 +0100131static struct lysc_ext_instance *
132lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
133{
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100134 /* TODO - extensions, increase refcount */
Radek Krejci19a96102018-11-15 13:38:09 +0100135 (void) ctx;
136 (void) orig;
137 return NULL;
138}
139
Radek Krejcib56c7502019-02-13 14:19:54 +0100140/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200141 * @brief Add record into the compile context's list of incomplete default values.
142 * @param[in] ctx Compile context with the incomplete default values list.
143 * @param[in] context_node Context schema node to store in the record.
144 * @param[in] dflt Incomplete default value to store in the record.
145 * @param[in] dflt_mod Module of the default value definition to store in the record.
146 * @return LY_EMEM in case of memory allocation failure.
147 * @return LY_SUCCESS
148 */
149static LY_ERR
150lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
151{
152 struct lysc_incomplete_dflt *r;
153 r = malloc(sizeof *r);
154 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
155 r->context_node = context_node;
156 r->dflt = dflt;
157 r->dflt_mod = dflt_mod;
158 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
159
160 return LY_SUCCESS;
161}
162
163/**
164 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
165 * @param[in] ctx Compile context with the incomplete default values list.
166 * @param[in] dflt Incomplete default values identifying the record to remove.
167 */
168static void
169lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
170{
171 unsigned int u;
172 struct lysc_incomplete_dflt *r;
173
174 for (u = 0; u < ctx->dflts.count; ++u) {
175 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
176 if (r->dflt == dflt) {
177 free(ctx->dflts.objs[u]);
178 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
179 --ctx->dflts.count;
180 return;
181 }
182 }
183}
184
Radek Krejci0e59c312019-08-15 15:34:15 +0200185void
Radek Krejci327de162019-06-14 12:52:07 +0200186lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
187{
188 int len;
189 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
190
191 if (!name) {
192 /* removing last path segment */
193 if (ctx->path[ctx->path_len - 1] == '}') {
194 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
195 if (ctx->path[ctx->path_len] == '=') {
196 ctx->path[ctx->path_len++] = '}';
197 } else {
198 /* not a top-level special tag, remove also preceiding '/' */
199 goto remove_nodelevel;
200 }
201 } else {
202remove_nodelevel:
203 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
204 if (ctx->path_len == 0) {
205 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200206 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200207 }
208 }
209 /* set new terminating NULL-byte */
210 ctx->path[ctx->path_len] = '\0';
211 } else {
212 if (ctx->path_len > 1) {
213 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
214 /* extension of the special tag */
215 nextlevel = 2;
216 --ctx->path_len;
217 } else {
218 /* there is already some path, so add next level */
219 nextlevel = 1;
220 }
221 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
222
223 if (nextlevel != 2) {
224 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
225 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200226 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200227 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200228 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200229 }
230 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200231 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200232 }
Radek Krejciacc79042019-07-25 14:14:57 +0200233 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
234 /* output truncated */
235 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
236 } else {
237 ctx->path_len += len;
238 }
Radek Krejci327de162019-06-14 12:52:07 +0200239 }
240}
241
242/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100243 * @brief Duplicate the compiled pattern structure.
244 *
245 * Instead of duplicating memory, the reference counter in the @p orig is increased.
246 *
247 * @param[in] orig The pattern structure to duplicate.
248 * @return The duplicated structure to use.
249 */
Radek Krejci19a96102018-11-15 13:38:09 +0100250static struct lysc_pattern*
251lysc_pattern_dup(struct lysc_pattern *orig)
252{
253 ++orig->refcount;
254 return orig;
255}
256
Radek Krejcib56c7502019-02-13 14:19:54 +0100257/**
258 * @brief Duplicate the array of compiled patterns.
259 *
260 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
261 *
262 * @param[in] ctx Libyang context for logging.
263 * @param[in] orig The patterns sized array to duplicate.
264 * @return New sized array as a copy of @p orig.
265 * @return NULL in case of memory allocation error.
266 */
Radek Krejci19a96102018-11-15 13:38:09 +0100267static struct lysc_pattern**
268lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
269{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100270 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100271 unsigned int u;
272
Radek Krejcib56c7502019-02-13 14:19:54 +0100273 assert(orig);
274
Radek Krejci19a96102018-11-15 13:38:09 +0100275 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
276 LY_ARRAY_FOR(orig, u) {
277 dup[u] = lysc_pattern_dup(orig[u]);
278 LY_ARRAY_INCREMENT(dup);
279 }
280 return dup;
281}
282
Radek Krejcib56c7502019-02-13 14:19:54 +0100283/**
284 * @brief Duplicate compiled range structure.
285 *
286 * @param[in] ctx Libyang context for logging.
287 * @param[in] orig The range structure to be duplicated.
288 * @return New compiled range structure as a copy of @p orig.
289 * @return NULL in case of memory allocation error.
290 */
Radek Krejci19a96102018-11-15 13:38:09 +0100291struct lysc_range*
292lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
293{
294 struct lysc_range *dup;
295 LY_ERR ret;
296
Radek Krejcib56c7502019-02-13 14:19:54 +0100297 assert(orig);
298
Radek Krejci19a96102018-11-15 13:38:09 +0100299 dup = calloc(1, sizeof *dup);
300 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
301 if (orig->parts) {
302 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
303 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
304 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
305 }
306 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
307 DUP_STRING(ctx, orig->emsg, dup->emsg);
308 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
309
310 return dup;
311cleanup:
312 free(dup);
313 (void) ret; /* set but not used due to the return type */
314 return NULL;
315}
316
Radek Krejcib56c7502019-02-13 14:19:54 +0100317/**
318 * @brief Stack for processing if-feature expressions.
319 */
Radek Krejci19a96102018-11-15 13:38:09 +0100320struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100321 int size; /**< number of items in the stack */
322 int index; /**< first empty item */
323 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100324};
325
Radek Krejcib56c7502019-02-13 14:19:54 +0100326/**
327 * @brief Add @ref ifftokens into the stack.
328 * @param[in] stack The if-feature stack to use.
329 * @param[in] value One of the @ref ifftokens to store in the stack.
330 * @return LY_EMEM in case of memory allocation error
331 * @return LY_ESUCCESS if the value successfully stored.
332 */
Radek Krejci19a96102018-11-15 13:38:09 +0100333static LY_ERR
334iff_stack_push(struct iff_stack *stack, uint8_t value)
335{
336 if (stack->index == stack->size) {
337 stack->size += 4;
338 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
339 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
340 }
341 stack->stack[stack->index++] = value;
342 return LY_SUCCESS;
343}
344
Radek Krejcib56c7502019-02-13 14:19:54 +0100345/**
346 * @brief Get (and remove) the last item form the stack.
347 * @param[in] stack The if-feature stack to use.
348 * @return The value from the top of the stack.
349 */
Radek Krejci19a96102018-11-15 13:38:09 +0100350static uint8_t
351iff_stack_pop(struct iff_stack *stack)
352{
Radek Krejcib56c7502019-02-13 14:19:54 +0100353 assert(stack && stack->index);
354
Radek Krejci19a96102018-11-15 13:38:09 +0100355 stack->index--;
356 return stack->stack[stack->index];
357}
358
Radek Krejcib56c7502019-02-13 14:19:54 +0100359/**
360 * @brief Clean up the stack.
361 * @param[in] stack The if-feature stack to use.
362 */
Radek Krejci19a96102018-11-15 13:38:09 +0100363static void
364iff_stack_clean(struct iff_stack *stack)
365{
366 stack->size = 0;
367 free(stack->stack);
368}
369
Radek Krejcib56c7502019-02-13 14:19:54 +0100370/**
371 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
372 * (libyang format of the if-feature expression).
373 * @param[in,out] list The 2bits array to modify.
374 * @param[in] op The operand (@ref ifftokens) to store.
375 * @param[in] pos Position (0-based) where to store the given @p op.
376 */
Radek Krejci19a96102018-11-15 13:38:09 +0100377static void
378iff_setop(uint8_t *list, uint8_t op, int pos)
379{
380 uint8_t *item;
381 uint8_t mask = 3;
382
383 assert(pos >= 0);
384 assert(op <= 3); /* max 2 bits */
385
386 item = &list[pos / 4];
387 mask = mask << 2 * (pos % 4);
388 *item = (*item) & ~mask;
389 *item = (*item) | (op << 2 * (pos % 4));
390}
391
Radek Krejcib56c7502019-02-13 14:19:54 +0100392#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
393#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100394
Radek Krejci0af46292019-01-11 16:02:31 +0100395/**
396 * @brief Find a feature of the given name and referenced in the given module.
397 *
398 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
399 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
400 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
401 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
402 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
403 * assigned till that time will be still valid.
404 *
405 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
406 * @param[in] name Name of the feature including possible prefix.
407 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
408 * @return Pointer to the feature structure if found, NULL otherwise.
409 */
Radek Krejci19a96102018-11-15 13:38:09 +0100410static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100411lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100412{
413 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100414 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100415
416 for (i = 0; i < len; ++i) {
417 if (name[i] == ':') {
418 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100419 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100420 LY_CHECK_RET(!mod, NULL);
421
422 name = &name[i + 1];
423 len = len - i - 1;
424 }
425 }
426
427 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100428 if (mod->implemented) {
429 /* module is implemented so there is already the compiled schema */
430 flist = mod->compiled->features;
431 } else {
432 flist = mod->off_features;
433 }
434 LY_ARRAY_FOR(flist, i) {
435 f = &flist[i];
Radek Krejci7f9b6512019-09-18 13:11:09 +0200436 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100437 return f;
438 }
439 }
440
441 return NULL;
442}
443
444static LY_ERR
Radek Krejci0935f412019-08-20 16:15:18 +0200445lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, LYEXT_PARENT parent_type)
Radek Krejci19a96102018-11-15 13:38:09 +0100446{
Radek Krejci7c960162019-09-18 14:16:12 +0200447 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100448 const char *name;
449 unsigned int u;
450 const struct lys_module *mod;
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100451 struct lysc_ext **elist = NULL;
Radek Krejci7c960162019-09-18 14:16:12 +0200452 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100453
454 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
455 ext->insubstmt = ext_p->insubstmt;
456 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200457 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200458 ext->parent = parent;
459 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100460
Radek Krejcif56e2a42019-09-09 14:15:25 +0200461 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200462
Radek Krejci19a96102018-11-15 13:38:09 +0100463 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200464 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
465 if (ext_p->yin) {
466 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
467 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
468 * YANG (import) prefix must be done here. */
469 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
470 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
471 u = 0;
472 } else if (ctx->mod_def->compiled) {
473 unsigned int v;
474 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
475 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
476 char *s;
477 asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]);
478 prefixed_name = lydict_insert_zc(ctx->ctx, s);
479 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
480 break;
481 }
482 }
483 }
484 if (!prefixed_name) {
485 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
486 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
487 goto cleanup;
488 }
489 } else {
490 prefixed_name = ext_p->name;
491 }
492 lysc_update_path(ctx, NULL, prefixed_name);
493
494 mod = lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1);
495 if (!mod) {
496 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
497 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
498 goto cleanup;
499 } else if (!mod->parsed->extensions) {
500 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
501 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
502 prefixed_name, mod->name);
503 goto cleanup;
504 }
505 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200506
Radek Krejci19a96102018-11-15 13:38:09 +0100507 /* find the extension definition there */
Radek Krejci0935f412019-08-20 16:15:18 +0200508 if (mod->off_extensions) {
509 elist = mod->off_extensions;
510 } else {
511 elist = mod->compiled->extensions;
512 }
513 LY_ARRAY_FOR(elist, u) {
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100514 if (!strcmp(name, elist[u]->name)) {
515 ext->def = lysc_ext_dup(elist[u]);
Radek Krejci19a96102018-11-15 13:38:09 +0100516 break;
517 }
518 }
Radek Krejci7c960162019-09-18 14:16:12 +0200519 if (!ext->def) {
520 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
521 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
522 goto cleanup;
523 }
Radek Krejci0935f412019-08-20 16:15:18 +0200524
Radek Krejcif56e2a42019-09-09 14:15:25 +0200525 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
526 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
527 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200528 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200529 /* Schema was parsed from YIN and an argument is expected, ... */
530 struct lysp_stmt *stmt = NULL;
531
532 if (ext->def->flags & LYS_YINELEM_TRUE) {
533 /* ... argument was the first XML child element */
534 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
535 /* TODO check namespace of the statement */
536 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
537 stmt = ext_p->child;
538 }
539 }
540 } else {
541 /* ... argument was one of the XML attributes which are represented as child stmt
542 * with LYS_YIN_ATTR flag */
543 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
544 if (!strcmp(stmt->stmt, ext->def->argument)) {
545 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200546 break;
547 }
548 }
549 }
550 if (!stmt) {
551 /* missing extension's argument */
552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200553 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
554 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200555
556 }
557 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
558 stmt->flags |= LYS_YIN_ARGUMENT;
559 }
Radek Krejci7c960162019-09-18 14:16:12 +0200560 if (prefixed_name != ext_p->name) {
561 lydict_remove(ctx->ctx, ext_p->name);
562 ext_p->name = prefixed_name;
563 if (!ext_p->argument && ext->argument) {
564 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
565 }
566 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200567
Radek Krejci0935f412019-08-20 16:15:18 +0200568 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200569 if (ext->argument) {
570 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
571 }
Radek Krejci7c960162019-09-18 14:16:12 +0200572 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200573 if (ext->argument) {
574 lysc_update_path(ctx, NULL, NULL);
575 }
Radek Krejci0935f412019-08-20 16:15:18 +0200576 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200577 ext_p->compiled = ext;
578
Radek Krejci7c960162019-09-18 14:16:12 +0200579 ret = LY_SUCCESS;
580cleanup:
581 if (prefixed_name && prefixed_name != ext_p->name) {
582 lydict_remove(ctx->ctx, prefixed_name);
583 }
584
Radek Krejcif56e2a42019-09-09 14:15:25 +0200585 lysc_update_path(ctx, NULL, NULL);
586 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200587
Radek Krejci7c960162019-09-18 14:16:12 +0200588 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200589}
590
591/**
592 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
593 */
594static LY_ERR
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100595lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext **ext)
Radek Krejci0935f412019-08-20 16:15:18 +0200596{
597 LY_ERR ret = LY_SUCCESS;
598
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100599 *ext = calloc(1, sizeof **ext);
600 (*ext)->refcount = 1;
601 DUP_STRING(ctx->ctx, ext_p->name, (*ext)->name);
602 DUP_STRING(ctx->ctx, ext_p->argument, (*ext)->argument);
603 (*ext)->module = ctx->mod_def;
604 COMPILE_EXTS_GOTO(ctx, ext_p->exts, (*ext)->exts, *ext, LYEXT_PAR_EXT, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +0200605
606done:
607 return ret;
608}
609
610/**
611 * @brief Link the extensions definitions with the available extension plugins.
612 *
613 * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules
614 * are not connected with even available extension plugins.
615 *
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100616 * @param[in] extensions List of pointers to extensions to be processed ([sized array](@ref sizedarrays)).
Radek Krejci0935f412019-08-20 16:15:18 +0200617 */
618static void
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100619lys_compile_extension_plugins(struct lysc_ext **extensions)
Radek Krejci0935f412019-08-20 16:15:18 +0200620{
621 unsigned int u;
622
623 LY_ARRAY_FOR(extensions, u) {
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100624 extensions[u]->plugin = lyext_get_plugin(extensions[u]);
Radek Krejci0935f412019-08-20 16:15:18 +0200625 }
626}
627
628LY_ERR
629lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100630 struct lysp_ext *extensions_p, struct lysc_ext ***extensions)
Radek Krejci0935f412019-08-20 16:15:18 +0200631{
632 unsigned int offset = 0, u;
633 struct lysc_ctx context = {0};
634
635 assert(ctx_sc || ctx);
636
637 if (!ctx_sc) {
638 context.ctx = ctx;
639 context.mod = module;
Michal Vaskob541af02019-12-18 12:11:23 +0100640 context.mod_def = module;
Radek Krejci0935f412019-08-20 16:15:18 +0200641 context.path_len = 1;
642 context.path[0] = '/';
643 ctx_sc = &context;
644 }
645
646 if (!extensions_p) {
647 return LY_SUCCESS;
648 }
649 if (*extensions) {
650 offset = LY_ARRAY_SIZE(*extensions);
651 }
652
653 lysc_update_path(ctx_sc, NULL, "{extension}");
654 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM);
655 LY_ARRAY_FOR(extensions_p, u) {
656 lysc_update_path(ctx_sc, NULL, extensions_p[u].name);
657 LY_ARRAY_INCREMENT(*extensions);
Radek Krejci0935f412019-08-20 16:15:18 +0200658 LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u]));
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100659 COMPILE_CHECK_UNIQUENESS_PARRAY(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name);
Radek Krejci0935f412019-08-20 16:15:18 +0200660 lysc_update_path(ctx_sc, NULL, NULL);
661 }
662 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100663
664 return LY_SUCCESS;
665}
666
Radek Krejcib56c7502019-02-13 14:19:54 +0100667/**
668 * @brief Compile information from the if-feature statement
669 * @param[in] ctx Compile context.
670 * @param[in] value The if-feature argument to process. It is pointer-to-pointer-to-char just to unify the compile functions.
Radek Krejcib56c7502019-02-13 14:19:54 +0100671 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
672 * @return LY_ERR value.
673 */
Radek Krejci19a96102018-11-15 13:38:09 +0100674static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200675lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100676{
677 const char *c = *value;
678 int r, rc = EXIT_FAILURE;
679 int i, j, last_not, checkversion = 0;
680 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
681 uint8_t op;
682 struct iff_stack stack = {0, 0, NULL};
683 struct lysc_feature *f;
684
685 assert(c);
686
687 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
688 for (i = j = last_not = 0; c[i]; i++) {
689 if (c[i] == '(') {
690 j++;
691 checkversion = 1;
692 continue;
693 } else if (c[i] == ')') {
694 j--;
695 continue;
696 } else if (isspace(c[i])) {
697 checkversion = 1;
698 continue;
699 }
700
701 if (!strncmp(&c[i], "not", r = 3) || !strncmp(&c[i], "and", r = 3) || !strncmp(&c[i], "or", r = 2)) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200702 int sp;
703 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
704 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100705 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
706 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
707 return LY_EVALID;
708 } else if (!isspace(c[i + r])) {
709 /* feature name starting with the not/and/or */
710 last_not = 0;
711 f_size++;
712 } else if (c[i] == 'n') { /* not operation */
713 if (last_not) {
714 /* double not */
715 expr_size = expr_size - 2;
716 last_not = 0;
717 } else {
718 last_not = 1;
719 }
720 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200721 if (f_exp != f_size) {
722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
723 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
724 return LY_EVALID;
725 }
Radek Krejci19a96102018-11-15 13:38:09 +0100726 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200727
Radek Krejci19a96102018-11-15 13:38:09 +0100728 /* not a not operation */
729 last_not = 0;
730 }
731 i += r;
732 } else {
733 f_size++;
734 last_not = 0;
735 }
736 expr_size++;
737
738 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200739 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100740 i--;
741 break;
742 }
743 i++;
744 }
745 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200746 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100747 /* not matching count of ( and ) */
748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
749 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
750 return LY_EVALID;
751 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200752 if (f_exp != f_size) {
753 /* features do not match the needed arguments for the logical operations */
754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
755 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
756 "the required number of operands for the operations.", *value);
757 return LY_EVALID;
758 }
Radek Krejci19a96102018-11-15 13:38:09 +0100759
760 if (checkversion || expr_size > 1) {
761 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100762 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100763 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
764 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
765 return LY_EVALID;
766 }
767 }
768
769 /* allocate the memory */
770 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
771 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
772 stack.stack = malloc(expr_size * sizeof *stack.stack);
773 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
774
775 stack.size = expr_size;
776 f_size--; expr_size--; /* used as indexes from now */
777
778 for (i--; i >= 0; i--) {
779 if (c[i] == ')') {
780 /* push it on stack */
781 iff_stack_push(&stack, LYS_IFF_RP);
782 continue;
783 } else if (c[i] == '(') {
784 /* pop from the stack into result all operators until ) */
785 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
786 iff_setop(iff->expr, op, expr_size--);
787 }
788 continue;
789 } else if (isspace(c[i])) {
790 continue;
791 }
792
793 /* end of operator or operand -> find beginning and get what is it */
794 j = i + 1;
795 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
796 i--;
797 }
798 i++; /* go back by one step */
799
800 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
801 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
802 /* double not */
803 iff_stack_pop(&stack);
804 } else {
805 /* not has the highest priority, so do not pop from the stack
806 * as in case of AND and OR */
807 iff_stack_push(&stack, LYS_IFF_NOT);
808 }
809 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
810 /* as for OR - pop from the stack all operators with the same or higher
811 * priority and store them to the result, then push the AND to the stack */
812 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
813 op = iff_stack_pop(&stack);
814 iff_setop(iff->expr, op, expr_size--);
815 }
816 iff_stack_push(&stack, LYS_IFF_AND);
817 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
818 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
819 op = iff_stack_pop(&stack);
820 iff_setop(iff->expr, op, expr_size--);
821 }
822 iff_stack_push(&stack, LYS_IFF_OR);
823 } else {
824 /* feature name, length is j - i */
825
826 /* add it to the expression */
827 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
828
829 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100830 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
831 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
832 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
833 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100834 iff->features[f_size] = f;
835 LY_ARRAY_INCREMENT(iff->features);
836 f_size--;
837 }
838 }
839 while (stack.index) {
840 op = iff_stack_pop(&stack);
841 iff_setop(iff->expr, op, expr_size--);
842 }
843
844 if (++expr_size || ++f_size) {
845 /* not all expected operators and operands found */
846 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
847 "Invalid value \"%s\" of if-feature - processing error.", *value);
848 rc = LY_EINT;
849 } else {
850 rc = LY_SUCCESS;
851 }
852
853error:
854 /* cleanup */
855 iff_stack_clean(&stack);
856
857 return rc;
858}
859
Radek Krejcib56c7502019-02-13 14:19:54 +0100860/**
Michal Vasko175012e2019-11-06 15:49:14 +0100861 * @brief Get the XPath context node for the given schema node.
862 * @param[in] start The schema node where the XPath expression appears.
863 * @return The context node to evaluate XPath expression in given schema node.
864 * @return NULL in case the context node is the root node.
865 */
866static struct lysc_node *
867lysc_xpath_context(struct lysc_node *start)
868{
869 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
870 start = start->parent);
871 return start;
872}
873
874/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100875 * @brief Compile information from the when statement
876 * @param[in] ctx Compile context.
877 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100878 * @param[in] flags Flags of the node with the "when" defiition.
879 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100880 * @param[out] when Pointer where to store pointer to the created compiled when structure.
881 * @return LY_ERR value.
882 */
Radek Krejci19a96102018-11-15 13:38:09 +0100883static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100884lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100885{
Radek Krejci19a96102018-11-15 13:38:09 +0100886 LY_ERR ret = LY_SUCCESS;
887
Radek Krejci00b874b2019-02-12 10:54:50 +0100888 *when = calloc(1, sizeof **when);
889 (*when)->refcount = 1;
890 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200891 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100892 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100893 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
894 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
895 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200896 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100897 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100898
899done:
900 return ret;
901}
902
Radek Krejcib56c7502019-02-13 14:19:54 +0100903/**
904 * @brief Compile information from the must statement
905 * @param[in] ctx Compile context.
906 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100907 * @param[in,out] must Prepared (empty) compiled must structure to fill.
908 * @return LY_ERR value.
909 */
Radek Krejci19a96102018-11-15 13:38:09 +0100910static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200911lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100912{
Radek Krejci19a96102018-11-15 13:38:09 +0100913 LY_ERR ret = LY_SUCCESS;
914
915 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
916 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200917 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100918 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
919 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100920 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
921 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200922 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100923
924done:
925 return ret;
926}
927
Radek Krejcib56c7502019-02-13 14:19:54 +0100928/**
929 * @brief Compile information from the import statement
930 * @param[in] ctx Compile context.
931 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100932 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
933 * @return LY_ERR value.
934 */
Radek Krejci19a96102018-11-15 13:38:09 +0100935static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200936lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100937{
Radek Krejci19a96102018-11-15 13:38:09 +0100938 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100939 LY_ERR ret = LY_SUCCESS;
940
941 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200942 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100943 imp->module = imp_p->module;
944
Radek Krejci7f2a5362018-11-28 13:05:37 +0100945 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
946 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100947 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100948 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100949 /* try to use filepath if present */
950 if (imp->module->filepath) {
951 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
952 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100953 if (mod != imp->module) {
954 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100955 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100956 mod = NULL;
957 }
958 }
959 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100960 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100961 LOGERR(ctx->ctx, LY_ENOTFOUND, "Unable to reload \"%s\" module to import it into \"%s\", source data not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100962 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100963 return LY_ENOTFOUND;
964 }
965 }
Radek Krejci19a96102018-11-15 13:38:09 +0100966 }
967
968done:
969 return ret;
970}
971
Radek Krejcib56c7502019-02-13 14:19:54 +0100972/**
973 * @brief Compile information from the identity statement
974 *
975 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
976 *
977 * @param[in] ctx Compile context.
978 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100979 * @param[in] idents List of so far compiled identities to check the name uniqueness.
980 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
981 * @return LY_ERR value.
982 */
Radek Krejci19a96102018-11-15 13:38:09 +0100983static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200984lys_compile_identity(struct lysc_ctx *ctx, struct lysp_ident *ident_p, struct lysc_ident *idents, struct lysc_ident *ident)
Radek Krejci19a96102018-11-15 13:38:09 +0100985{
986 unsigned int u;
987 LY_ERR ret = LY_SUCCESS;
988
Radek Krejci327de162019-06-14 12:52:07 +0200989 lysc_update_path(ctx, NULL, ident_p->name);
990
Michal Vasko6f4cbb62020-02-28 11:15:47 +0100991 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100992 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200993 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
994 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200995 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200996 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100997 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
Radek Krejci0935f412019-08-20 16:15:18 +0200998 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100999 ident->flags = ident_p->flags;
1000
Radek Krejci327de162019-06-14 12:52:07 +02001001 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001002done:
1003 return ret;
1004}
1005
Radek Krejcib56c7502019-02-13 14:19:54 +01001006/**
1007 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
1008 *
1009 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
1010 *
1011 * @param[in] ctx Compile context for logging.
1012 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
1013 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
1014 * @return LY_SUCCESS if everything is ok.
1015 * @return LY_EVALID if the identity is derived from itself.
1016 */
Radek Krejci38222632019-02-12 16:55:05 +01001017static LY_ERR
1018lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1019{
1020 LY_ERR ret = LY_EVALID;
1021 unsigned int u, v;
1022 struct ly_set recursion = {0};
1023 struct lysc_ident *drv;
1024
1025 if (!derived) {
1026 return LY_SUCCESS;
1027 }
1028
1029 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
1030 if (ident == derived[u]) {
1031 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1032 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1033 goto cleanup;
1034 }
1035 ly_set_add(&recursion, derived[u], 0);
1036 }
1037
1038 for (v = 0; v < recursion.count; ++v) {
1039 drv = recursion.objs[v];
1040 if (!drv->derived) {
1041 continue;
1042 }
1043 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
1044 if (ident == drv->derived[u]) {
1045 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1046 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1047 goto cleanup;
1048 }
1049 ly_set_add(&recursion, drv->derived[u], 0);
1050 }
1051 }
1052 ret = LY_SUCCESS;
1053
1054cleanup:
1055 ly_set_erase(&recursion, NULL);
1056 return ret;
1057}
1058
Radek Krejcia3045382018-11-22 14:30:31 +01001059/**
1060 * @brief Find and process the referenced base identities from another identity or identityref
1061 *
Radek Krejciaca74032019-06-04 08:53:06 +02001062 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001063 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1064 * to distinguish these two use cases.
1065 *
1066 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1067 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
1068 * @param[in] ident Referencing identity to work with.
1069 * @param[in] bases Array of bases of identityref to fill in.
1070 * @return LY_ERR value.
1071 */
Radek Krejci19a96102018-11-15 13:38:09 +01001072static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +01001073lys_compile_identity_bases(struct lysc_ctx *ctx, const char **bases_p, struct lysc_ident *ident, struct lysc_ident ***bases)
Radek Krejci19a96102018-11-15 13:38:09 +01001074{
Radek Krejci555cb5b2018-11-16 14:54:33 +01001075 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001076 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001077 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001078 struct lysc_ident **idref;
1079
1080 assert(ident || bases);
1081
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001082 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1084 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1085 return LY_EVALID;
1086 }
1087
1088 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1089 s = strchr(bases_p[u], ':');
1090 if (s) {
1091 /* prefixed identity */
1092 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +01001093 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001094 } else {
1095 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +01001096 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001097 }
1098 if (!mod) {
1099 if (ident) {
1100 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1101 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1102 } else {
1103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1104 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1105 }
1106 return LY_EVALID;
1107 }
1108 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001109 if (mod->compiled && mod->compiled->identities) {
1110 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1111 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001112 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001113 if (ident == &mod->compiled->identities[v]) {
1114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1115 "Identity \"%s\" is derived from itself.", ident->name);
1116 return LY_EVALID;
1117 }
1118 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001119 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001120 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001121 *idref = ident;
1122 } else {
1123 /* we have match! store the found identity */
1124 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001125 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001126 }
1127 break;
1128 }
1129 }
1130 }
1131 if (!idref || !(*idref)) {
1132 if (ident) {
1133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1134 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1135 } else {
1136 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1137 "Unable to find base (%s) of identityref.", bases_p[u]);
1138 }
1139 return LY_EVALID;
1140 }
1141 }
1142 return LY_SUCCESS;
1143}
1144
Radek Krejcia3045382018-11-22 14:30:31 +01001145/**
1146 * @brief For the given array of identities, set the backlinks from all their base identities.
1147 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1148 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1149 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1150 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1151 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001152static LY_ERR
1153lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1154{
1155 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +01001156
1157 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
1158 if (!idents_p[i].bases) {
1159 continue;
1160 }
Radek Krejci327de162019-06-14 12:52:07 +02001161 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001162 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001163 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001164 }
1165 return LY_SUCCESS;
1166}
1167
Radek Krejci0af46292019-01-11 16:02:31 +01001168LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001169lys_feature_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module, struct lysp_feature *features_p, struct lysc_feature **features)
Radek Krejci0af46292019-01-11 16:02:31 +01001170{
1171 unsigned int offset = 0, u;
1172 struct lysc_ctx context = {0};
1173
Radek Krejci327de162019-06-14 12:52:07 +02001174 assert(ctx_sc || ctx);
1175
1176 if (!ctx_sc) {
1177 context.ctx = ctx;
1178 context.mod = module;
1179 context.path_len = 1;
1180 context.path[0] = '/';
1181 ctx_sc = &context;
1182 }
Radek Krejci0af46292019-01-11 16:02:31 +01001183
1184 if (!features_p) {
1185 return LY_SUCCESS;
1186 }
1187 if (*features) {
1188 offset = LY_ARRAY_SIZE(*features);
1189 }
1190
Radek Krejci327de162019-06-14 12:52:07 +02001191 lysc_update_path(ctx_sc, NULL, "{feature}");
1192 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001193 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001194 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1195
Radek Krejci0af46292019-01-11 16:02:31 +01001196 LY_ARRAY_INCREMENT(*features);
Michal Vasko6f4cbb62020-02-28 11:15:47 +01001197 COMPILE_CHECK_UNIQUENESS_ARRAY(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
Radek Krejci327de162019-06-14 12:52:07 +02001198 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1199 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1200 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001201 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001202 (*features)[offset + u].module = ctx_sc->mod;
1203
1204 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001205 }
Radek Krejci327de162019-06-14 12:52:07 +02001206 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001207
1208 return LY_SUCCESS;
1209}
1210
Radek Krejcia3045382018-11-22 14:30:31 +01001211/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001212 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001213 *
1214 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1215 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001216 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001217 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1218 * being currently processed).
1219 * @param[in] depfeatures The list of depending features of the feature being currently processed (not the one provided as @p feature)
Radek Krejci09a1fc52019-02-13 10:55:17 +01001220 * @return LY_SUCCESS if everything is ok.
1221 * @return LY_EVALID if the feature references indirectly itself.
1222 */
1223static LY_ERR
1224lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1225{
1226 LY_ERR ret = LY_EVALID;
1227 unsigned int u, v;
1228 struct ly_set recursion = {0};
1229 struct lysc_feature *drv;
1230
1231 if (!depfeatures) {
1232 return LY_SUCCESS;
1233 }
1234
1235 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1236 if (feature == depfeatures[u]) {
1237 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1238 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1239 goto cleanup;
1240 }
1241 ly_set_add(&recursion, depfeatures[u], 0);
1242 }
1243
1244 for (v = 0; v < recursion.count; ++v) {
1245 drv = recursion.objs[v];
1246 if (!drv->depfeatures) {
1247 continue;
1248 }
1249 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1250 if (feature == drv->depfeatures[u]) {
1251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1252 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1253 goto cleanup;
1254 }
1255 ly_set_add(&recursion, drv->depfeatures[u], 0);
1256 }
1257 }
1258 ret = LY_SUCCESS;
1259
1260cleanup:
1261 ly_set_erase(&recursion, NULL);
1262 return ret;
1263}
1264
1265/**
Radek Krejci0af46292019-01-11 16:02:31 +01001266 * @brief Create pre-compiled features array.
1267 *
1268 * See lys_feature_precompile() for more details.
1269 *
Radek Krejcia3045382018-11-22 14:30:31 +01001270 * @param[in] ctx Compile context.
1271 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001272 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001273 * @return LY_ERR value.
1274 */
Radek Krejci19a96102018-11-15 13:38:09 +01001275static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001276lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001277{
Radek Krejci0af46292019-01-11 16:02:31 +01001278 unsigned int u, v, x;
1279 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001280 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001281
Radek Krejci327de162019-06-14 12:52:07 +02001282
Radek Krejci0af46292019-01-11 16:02:31 +01001283 /* find the preprecompiled feature */
1284 LY_ARRAY_FOR(features, x) {
1285 if (strcmp(features[x].name, feature_p->name)) {
1286 continue;
1287 }
1288 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001289 lysc_update_path(ctx, NULL, "{feature}");
1290 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001291
Radek Krejci0af46292019-01-11 16:02:31 +01001292 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001293 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001294 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001295 if (feature->iffeatures) {
1296 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1297 if (feature->iffeatures[u].features) {
1298 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001299 /* check for circular dependency - direct reference first,... */
1300 if (feature == feature->iffeatures[u].features[v]) {
1301 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1302 "Feature \"%s\" is referenced from itself.", feature->name);
1303 return LY_EVALID;
1304 }
1305 /* ... and indirect circular reference */
1306 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1307
Radek Krejci0af46292019-01-11 16:02:31 +01001308 /* add itself into the dependants list */
1309 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1310 *df = feature;
1311 }
Radek Krejci19a96102018-11-15 13:38:09 +01001312 }
Radek Krejci19a96102018-11-15 13:38:09 +01001313 }
1314 }
Radek Krejci327de162019-06-14 12:52:07 +02001315 lysc_update_path(ctx, NULL, NULL);
1316 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001317 done:
1318 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001319 }
Radek Krejci0af46292019-01-11 16:02:31 +01001320
1321 LOGINT(ctx->ctx);
1322 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001323}
1324
Radek Krejcib56c7502019-02-13 14:19:54 +01001325/**
1326 * @brief Revert compiled list of features back to the precompiled state.
1327 *
1328 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1329 * The features are supposed to be stored again as off_features in ::lys_module structure.
1330 *
1331 * @param[in] ctx Compilation context.
1332 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1333 * and supposed to hold the off_features list.
1334 */
Radek Krejci95710c92019-02-11 15:49:55 +01001335static void
1336lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1337{
1338 unsigned int u, v;
1339
1340 /* keep the off_features list until the complete lys_module is freed */
1341 mod->off_features = mod->compiled->features;
1342 mod->compiled->features = NULL;
1343
1344 /* in the off_features list, remove all the parts (from finished compiling process)
1345 * which may points into the data being freed here */
1346 LY_ARRAY_FOR(mod->off_features, u) {
1347 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1348 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1349 }
1350 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1351 mod->off_features[u].iffeatures = NULL;
1352
1353 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1354 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1355 }
1356 LY_ARRAY_FREE(mod->off_features[u].exts);
1357 mod->off_features[u].exts = NULL;
1358 }
1359}
1360
Radek Krejcia3045382018-11-22 14:30:31 +01001361/**
1362 * @brief Validate and normalize numeric value from a range definition.
1363 * @param[in] ctx Compile context.
1364 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1365 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1366 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1367 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1368 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1369 * @param[in] value String value of the range boundary.
1370 * @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.
1371 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1372 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1373 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001374LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001375range_part_check_value_syntax(struct lysc_ctx *ctx, LY_DATA_TYPE basetype, uint8_t frdigits, const char *value, size_t *len, char **valcopy)
Radek Krejci19a96102018-11-15 13:38:09 +01001376{
Radek Krejci6cba4292018-11-15 17:33:29 +01001377 size_t fraction = 0, size;
1378
Radek Krejci19a96102018-11-15 13:38:09 +01001379 *len = 0;
1380
1381 assert(value);
1382 /* parse value */
1383 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1384 return LY_EVALID;
1385 }
1386
1387 if ((value[*len] == '-') || (value[*len] == '+')) {
1388 ++(*len);
1389 }
1390
1391 while (isdigit(value[*len])) {
1392 ++(*len);
1393 }
1394
1395 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001396 if (basetype == LY_TYPE_DEC64) {
1397 goto decimal;
1398 } else {
1399 *valcopy = strndup(value, *len);
1400 return LY_SUCCESS;
1401 }
Radek Krejci19a96102018-11-15 13:38:09 +01001402 }
1403 fraction = *len;
1404
1405 ++(*len);
1406 while (isdigit(value[*len])) {
1407 ++(*len);
1408 }
1409
Radek Krejci6cba4292018-11-15 17:33:29 +01001410 if (basetype == LY_TYPE_DEC64) {
1411decimal:
1412 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001413 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001414 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1415 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1416 *len, value, frdigits);
1417 return LY_EINVAL;
1418 }
1419 if (fraction) {
1420 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1421 } else {
1422 size = (*len) + frdigits + 1;
1423 }
1424 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001425 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1426
Radek Krejci6cba4292018-11-15 17:33:29 +01001427 (*valcopy)[size - 1] = '\0';
1428 if (fraction) {
1429 memcpy(&(*valcopy)[0], &value[0], fraction);
1430 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1431 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1432 } else {
1433 memcpy(&(*valcopy)[0], &value[0], *len);
1434 memset(&(*valcopy)[*len], '0', frdigits);
1435 }
Radek Krejci19a96102018-11-15 13:38:09 +01001436 }
1437 return LY_SUCCESS;
1438}
1439
Radek Krejcia3045382018-11-22 14:30:31 +01001440/**
1441 * @brief Check that values in range are in ascendant order.
1442 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001443 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1444 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001445 * @param[in] value Current value to check.
1446 * @param[in] prev_value The last seen value.
1447 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1448 */
Radek Krejci19a96102018-11-15 13:38:09 +01001449static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001450range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001451{
1452 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001453 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001454 return LY_EEXIST;
1455 }
1456 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001457 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001458 return LY_EEXIST;
1459 }
1460 }
1461 return LY_SUCCESS;
1462}
1463
Radek Krejcia3045382018-11-22 14:30:31 +01001464/**
1465 * @brief Set min/max value of the range part.
1466 * @param[in] ctx Compile context.
1467 * @param[in] part Range part structure to fill.
1468 * @param[in] max Flag to distinguish if storing min or max value.
1469 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1470 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1471 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1472 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1473 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001474 * @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.
Radek Krejcia3045382018-11-22 14:30:31 +01001475 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1476 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1477 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1478 * frdigits value), LY_EMEM.
1479 */
Radek Krejci19a96102018-11-15 13:38:09 +01001480static LY_ERR
1481range_part_minmax(struct lysc_ctx *ctx, struct lysc_range_part *part, int max, int64_t prev, LY_DATA_TYPE basetype, int first, int length_restr,
Radek Krejci5969f272018-11-23 10:03:58 +01001482 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001483{
1484 LY_ERR ret = LY_SUCCESS;
1485 char *valcopy = NULL;
1486 size_t len;
1487
1488 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001489 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001490 LY_CHECK_GOTO(ret, finalize);
1491 }
1492 if (!valcopy && base_range) {
1493 if (max) {
1494 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1495 } else {
1496 part->min_64 = base_range->parts[0].min_64;
1497 }
1498 if (!first) {
1499 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1500 }
1501 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001502 }
1503
1504 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001505 case LY_TYPE_INT8: /* range */
1506 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001507 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-128), INT64_C(127), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001508 } else if (max) {
1509 part->max_64 = INT64_C(127);
1510 } else {
1511 part->min_64 = INT64_C(-128);
1512 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001513 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001514 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001515 }
1516 break;
1517 case LY_TYPE_INT16: /* range */
1518 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001519 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-32768), INT64_C(32767), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001520 } else if (max) {
1521 part->max_64 = INT64_C(32767);
1522 } else {
1523 part->min_64 = INT64_C(-32768);
1524 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001525 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001526 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001527 }
1528 break;
1529 case LY_TYPE_INT32: /* range */
1530 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001531 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-2147483648), INT64_C(2147483647), 10, max ? &part->max_64 : &part->min_64);
Radek Krejci19a96102018-11-15 13:38:09 +01001532 } else if (max) {
1533 part->max_64 = INT64_C(2147483647);
1534 } else {
1535 part->min_64 = INT64_C(-2147483648);
1536 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001537 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001538 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001539 }
1540 break;
1541 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001542 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001543 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001544 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001545 max ? &part->max_64 : &part->min_64);
1546 } else if (max) {
1547 part->max_64 = INT64_C(9223372036854775807);
1548 } else {
1549 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1550 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001551 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001552 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001553 }
1554 break;
1555 case LY_TYPE_UINT8: /* range */
1556 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001557 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(255), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001558 } else if (max) {
1559 part->max_u64 = UINT64_C(255);
1560 } else {
1561 part->min_u64 = UINT64_C(0);
1562 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001563 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001564 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001565 }
1566 break;
1567 case LY_TYPE_UINT16: /* range */
1568 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001569 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(65535), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001570 } else if (max) {
1571 part->max_u64 = UINT64_C(65535);
1572 } else {
1573 part->min_u64 = UINT64_C(0);
1574 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001575 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001576 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001577 }
1578 break;
1579 case LY_TYPE_UINT32: /* range */
1580 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001581 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(4294967295), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001582 } else if (max) {
1583 part->max_u64 = UINT64_C(4294967295);
1584 } else {
1585 part->min_u64 = UINT64_C(0);
1586 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001587 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001588 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001589 }
1590 break;
1591 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001592 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001593 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001594 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001595 ret = ly_parse_uint(valcopy, strlen(valcopy), UINT64_C(18446744073709551615), 10, max ? &part->max_u64 : &part->min_u64);
Radek Krejci19a96102018-11-15 13:38:09 +01001596 } else if (max) {
1597 part->max_u64 = UINT64_C(18446744073709551615);
1598 } else {
1599 part->min_u64 = UINT64_C(0);
1600 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001601 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001602 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001603 }
1604 break;
1605 default:
1606 LOGINT(ctx->ctx);
1607 ret = LY_EINT;
1608 }
1609
Radek Krejci5969f272018-11-23 10:03:58 +01001610finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001611 if (ret == LY_EDENIED) {
1612 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1613 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1614 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1615 } else if (ret == LY_EVALID) {
1616 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1617 "Invalid %s restriction - invalid value \"%s\".",
1618 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1619 } else if (ret == LY_EEXIST) {
1620 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1621 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001622 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001623 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001624 } else if (!ret && value) {
1625 *value = *value + len;
1626 }
1627 free(valcopy);
1628 return ret;
1629}
1630
Radek Krejcia3045382018-11-22 14:30:31 +01001631/**
1632 * @brief Compile the parsed range restriction.
1633 * @param[in] ctx Compile context.
1634 * @param[in] range_p Parsed range structure to compile.
1635 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1636 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1637 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1638 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1639 * range restriction must be more restrictive than the base_range.
1640 * @param[in,out] range Pointer to the created current range structure.
1641 * @return LY_ERR value.
1642 */
Radek Krejci19a96102018-11-15 13:38:09 +01001643static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001644lys_compile_type_range(struct lysc_ctx *ctx, struct lysp_restr *range_p, LY_DATA_TYPE basetype, int length_restr, uint8_t frdigits,
Radek Krejci19a96102018-11-15 13:38:09 +01001645 struct lysc_range *base_range, struct lysc_range **range)
1646{
1647 LY_ERR ret = LY_EVALID;
1648 const char *expr;
1649 struct lysc_range_part *parts = NULL, *part;
1650 int range_expected = 0, uns;
1651 unsigned int parts_done = 0, u, v;
1652
1653 assert(range);
1654 assert(range_p);
1655
1656 expr = range_p->arg;
1657 while(1) {
1658 if (isspace(*expr)) {
1659 ++expr;
1660 } else if (*expr == '\0') {
1661 if (range_expected) {
1662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1663 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1664 length_restr ? "length" : "range", range_p->arg);
1665 goto cleanup;
1666 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1667 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1668 "Invalid %s restriction - unexpected end of the expression (%s).",
1669 length_restr ? "length" : "range", range_p->arg);
1670 goto cleanup;
1671 }
1672 parts_done++;
1673 break;
1674 } else if (!strncmp(expr, "min", 3)) {
1675 if (parts) {
1676 /* min cannot be used elsewhere than in the first part */
1677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1678 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1679 expr - range_p->arg, range_p->arg);
1680 goto cleanup;
1681 }
1682 expr += 3;
1683
1684 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001685 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, 0, basetype, 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001686 part->max_64 = part->min_64;
1687 } else if (*expr == '|') {
1688 if (!parts || range_expected) {
1689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1690 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1691 goto cleanup;
1692 }
1693 expr++;
1694 parts_done++;
1695 /* process next part of the expression */
1696 } else if (!strncmp(expr, "..", 2)) {
1697 expr += 2;
1698 while (isspace(*expr)) {
1699 expr++;
1700 }
1701 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1702 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1703 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1704 goto cleanup;
1705 }
1706 /* continue expecting the upper boundary */
1707 range_expected = 1;
1708 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1709 /* number */
1710 if (range_expected) {
1711 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001712 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001713 range_expected = 0;
1714 } else {
1715 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1716 LY_CHECK_GOTO(range_part_minmax(ctx, part, 0, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001717 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001718 part->max_64 = part->min_64;
1719 }
1720
1721 /* continue with possible another expression part */
1722 } else if (!strncmp(expr, "max", 3)) {
1723 expr += 3;
1724 while (isspace(*expr)) {
1725 expr++;
1726 }
1727 if (*expr != '\0') {
1728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1729 length_restr ? "length" : "range", expr);
1730 goto cleanup;
1731 }
1732 if (range_expected) {
1733 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001734 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, part->min_64, basetype, 0, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001735 range_expected = 0;
1736 } else {
1737 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1738 LY_CHECK_GOTO(range_part_minmax(ctx, part, 1, parts_done ? parts[LY_ARRAY_SIZE(parts) - 2].max_64 : 0,
Radek Krejci5969f272018-11-23 10:03:58 +01001739 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001740 part->min_64 = part->max_64;
1741 }
1742 } else {
1743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1744 length_restr ? "length" : "range", expr);
1745 goto cleanup;
1746 }
1747 }
1748
1749 /* check with the previous range/length restriction */
1750 if (base_range) {
1751 switch (basetype) {
1752 case LY_TYPE_BINARY:
1753 case LY_TYPE_UINT8:
1754 case LY_TYPE_UINT16:
1755 case LY_TYPE_UINT32:
1756 case LY_TYPE_UINT64:
1757 case LY_TYPE_STRING:
1758 uns = 1;
1759 break;
1760 case LY_TYPE_DEC64:
1761 case LY_TYPE_INT8:
1762 case LY_TYPE_INT16:
1763 case LY_TYPE_INT32:
1764 case LY_TYPE_INT64:
1765 uns = 0;
1766 break;
1767 default:
1768 LOGINT(ctx->ctx);
1769 ret = LY_EINT;
1770 goto cleanup;
1771 }
1772 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1773 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1774 goto baseerror;
1775 }
1776 /* current lower bound is not lower than the base */
1777 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1778 /* base has single value */
1779 if (base_range->parts[v].min_64 == parts[u].min_64) {
1780 /* both lower bounds are the same */
1781 if (parts[u].min_64 != parts[u].max_64) {
1782 /* current continues with a range */
1783 goto baseerror;
1784 } else {
1785 /* equal single values, move both forward */
1786 ++v;
1787 continue;
1788 }
1789 } else {
1790 /* base is single value lower than current range, so the
1791 * value from base range is removed in the current,
1792 * move only base and repeat checking */
1793 ++v;
1794 --u;
1795 continue;
1796 }
1797 } else {
1798 /* base is the range */
1799 if (parts[u].min_64 == parts[u].max_64) {
1800 /* current is a single value */
1801 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1802 /* current is behind the base range, so base range is omitted,
1803 * move the base and keep the current for further check */
1804 ++v;
1805 --u;
1806 } /* else it is within the base range, so move the current, but keep the base */
1807 continue;
1808 } else {
1809 /* both are ranges - check the higher bound, the lower was already checked */
1810 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1811 /* higher bound is higher than the current higher bound */
1812 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1813 /* but the current lower bound is also higher, so the base range is omitted,
1814 * continue with the same current, but move the base */
1815 --u;
1816 ++v;
1817 continue;
1818 }
1819 /* current range starts within the base range but end behind it */
1820 goto baseerror;
1821 } else {
1822 /* current range is smaller than the base,
1823 * move current, but stay with the base */
1824 continue;
1825 }
1826 }
1827 }
1828 }
1829 if (u != parts_done) {
1830baseerror:
1831 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1832 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1833 length_restr ? "length" : "range", range_p->arg);
1834 goto cleanup;
1835 }
1836 }
1837
1838 if (!(*range)) {
1839 *range = calloc(1, sizeof **range);
1840 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1841 }
1842
Radek Krejcic8b31002019-01-08 10:24:45 +01001843 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001844 if (range_p->eapptag) {
1845 lydict_remove(ctx->ctx, (*range)->eapptag);
1846 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1847 }
1848 if (range_p->emsg) {
1849 lydict_remove(ctx->ctx, (*range)->emsg);
1850 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1851 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001852 if (range_p->dsc) {
1853 lydict_remove(ctx->ctx, (*range)->dsc);
1854 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1855 }
1856 if (range_p->ref) {
1857 lydict_remove(ctx->ctx, (*range)->ref);
1858 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1859 }
Radek Krejci19a96102018-11-15 13:38:09 +01001860 /* extensions are taken only from the last range by the caller */
1861
1862 (*range)->parts = parts;
1863 parts = NULL;
1864 ret = LY_SUCCESS;
1865cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001866 LY_ARRAY_FREE(parts);
1867
1868 return ret;
1869}
1870
1871/**
1872 * @brief Checks pattern syntax.
1873 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001874 * @param[in] ctx Context.
1875 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001876 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001877 * @param[in,out] pcre2_code Compiled PCRE2 pattern. If NULL, the compiled information used to validate pattern are freed.
Radek Krejcia3045382018-11-22 14:30:31 +01001878 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001879 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001880LY_ERR
1881lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001882{
Radek Krejci54579462019-04-30 12:47:06 +02001883 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001884 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001885 int err_code;
1886 const char *orig_ptr;
1887 PCRE2_SIZE err_offset;
1888 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001889#define URANGE_LEN 19
1890 char *ublock2urange[][2] = {
1891 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1892 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1893 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1894 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1895 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1896 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1897 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1898 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1899 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1900 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1901 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1902 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1903 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1904 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1905 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1906 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1907 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1908 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1909 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1910 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1911 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1912 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1913 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1914 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1915 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1916 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1917 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1918 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1919 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1920 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1921 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1922 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1923 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1924 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1925 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1926 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1927 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1928 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1929 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1930 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1931 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1932 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1933 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1934 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1935 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1936 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1937 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1938 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1939 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1940 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1941 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1942 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1943 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1944 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1945 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1946 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1947 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1948 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1949 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1950 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1951 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1952 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1953 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1954 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1955 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1956 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1957 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1958 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1959 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1960 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1961 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1962 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1963 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1964 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1965 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1966 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1967 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1968 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1969 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1970 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1971 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1972 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1973 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1974 {NULL, NULL}
1975 };
1976
1977 /* adjust the expression to a Perl equivalent
1978 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1979
Michal Vasko03ff5a72019-09-11 13:49:33 +02001980 /* we need to replace all "$" and "^" with "\$" and "\^", count them now */
Radek Krejci19a96102018-11-15 13:38:09 +01001981 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1982
1983 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001984 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001985 perl_regex[0] = '\0';
1986
1987 ptr = perl_regex;
1988
Radek Krejci19a96102018-11-15 13:38:09 +01001989 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1990 if (orig_ptr[0] == '$') {
1991 ptr += sprintf(ptr, "\\$");
1992 } else if (orig_ptr[0] == '^') {
1993 ptr += sprintf(ptr, "\\^");
1994 } else {
1995 ptr[0] = orig_ptr[0];
1996 ++ptr;
1997 }
1998 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001999 ptr[0] = '\0';
2000 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01002001
2002 /* substitute Unicode Character Blocks with exact Character Ranges */
2003 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
2004 start = ptr - perl_regex;
2005
2006 ptr = strchr(ptr, '}');
2007 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002008 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002009 pattern, perl_regex + start + 2, "unterminated character property");
2010 free(perl_regex);
2011 return LY_EVALID;
2012 }
2013 end = (ptr - perl_regex) + 1;
2014
2015 /* need more space */
2016 if (end - start < URANGE_LEN) {
2017 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002018 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002019 }
2020
2021 /* find our range */
2022 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2023 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2024 break;
2025 }
2026 }
2027 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002028 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002029 pattern, perl_regex + start + 5, "unknown block name");
2030 free(perl_regex);
2031 return LY_EVALID;
2032 }
2033
2034 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
2035 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
2036 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
2037 ++count;
2038 }
2039 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
2040 --count;
2041 }
2042 }
2043 if (count) {
2044 /* skip brackets */
2045 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2046 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2047 } else {
2048 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2049 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2050 }
2051 }
2052
2053 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002054 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2055 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002056 &err_code, &err_offset, NULL);
2057 if (!code_local) {
2058 PCRE2_UCHAR err_msg[256] = {0};
2059 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002060 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002061 free(perl_regex);
2062 return LY_EVALID;
2063 }
2064 free(perl_regex);
2065
Radek Krejci54579462019-04-30 12:47:06 +02002066 if (code) {
2067 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002068 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002069 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002070 }
2071
2072 return LY_SUCCESS;
2073
2074#undef URANGE_LEN
2075}
2076
Radek Krejcia3045382018-11-22 14:30:31 +01002077/**
2078 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2079 * @param[in] ctx Compile context.
2080 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002081 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2082 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2083 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2084 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2085 */
Radek Krejci19a96102018-11-15 13:38:09 +01002086static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002087lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002088 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2089{
2090 struct lysc_pattern **pattern;
Radek Krejci0935f412019-08-20 16:15:18 +02002091 unsigned int u;
Radek Krejci19a96102018-11-15 13:38:09 +01002092 LY_ERR ret = LY_SUCCESS;
2093
2094 /* first, copy the patterns from the base type */
2095 if (base_patterns) {
2096 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2097 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2098 }
2099
2100 LY_ARRAY_FOR(patterns_p, u) {
2101 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2102 *pattern = calloc(1, sizeof **pattern);
2103 ++(*pattern)->refcount;
2104
Michal Vasko03ff5a72019-09-11 13:49:33 +02002105 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002106 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002107
2108 if (patterns_p[u].arg[0] == 0x15) {
2109 (*pattern)->inverted = 1;
2110 }
Radek Krejci54579462019-04-30 12:47:06 +02002111 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002112 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2113 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002114 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2115 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002116 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002117 }
2118done:
2119 return ret;
2120}
2121
Radek Krejcia3045382018-11-22 14:30:31 +01002122/**
2123 * @brief map of the possible restrictions combination for the specific built-in type.
2124 */
Radek Krejci19a96102018-11-15 13:38:09 +01002125static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2126 0 /* LY_TYPE_UNKNOWN */,
2127 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002128 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2129 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2130 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2131 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2132 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002133 LYS_SET_BIT /* LY_TYPE_BITS */,
2134 0 /* LY_TYPE_BOOL */,
2135 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2136 0 /* LY_TYPE_EMPTY */,
2137 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2138 LYS_SET_BASE /* LY_TYPE_IDENT */,
2139 LYS_SET_REQINST /* LY_TYPE_INST */,
2140 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002141 LYS_SET_TYPE /* LY_TYPE_UNION */,
2142 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002143 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002144 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002145 LYS_SET_RANGE /* LY_TYPE_INT64 */
2146};
2147
2148/**
2149 * @brief stringification of the YANG built-in data types
2150 */
2151const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2152 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2153 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002154};
2155
Radek Krejcia3045382018-11-22 14:30:31 +01002156/**
2157 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2158 * @param[in] ctx Compile context.
2159 * @param[in] enums_p Array of the parsed enum structures to compile.
2160 * @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.
Radek Krejcia3045382018-11-22 14:30:31 +01002161 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2162 * @param[out] enums Newly created array of the compiled enums information for the current type.
2163 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2164 */
Radek Krejci19a96102018-11-15 13:38:09 +01002165static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002166lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002167 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002168{
2169 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02002170 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002171 int32_t value = 0;
2172 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002173 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002174
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002175 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002176 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2177 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2178 return LY_EVALID;
2179 }
2180
2181 LY_ARRAY_FOR(enums_p, u) {
2182 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2183 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002184 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2185 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002186 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002187 if (base_enums) {
2188 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2189 LY_ARRAY_FOR(base_enums, v) {
2190 if (!strcmp(e->name, base_enums[v].name)) {
2191 break;
2192 }
2193 }
2194 if (v == LY_ARRAY_SIZE(base_enums)) {
2195 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2196 "Invalid %s - derived type adds new item \"%s\".",
2197 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2198 return LY_EVALID;
2199 }
2200 match = v;
2201 }
2202
2203 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002204 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002205 if (enums_p[u].flags & LYS_SET_VALUE) {
2206 e->value = (int32_t)enums_p[u].value;
2207 if (!u || e->value >= value) {
2208 value = e->value + 1;
2209 }
2210 /* check collision with other values */
2211 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2212 if (e->value == (*enums)[v].value) {
2213 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2214 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2215 e->value, e->name, (*enums)[v].name);
2216 return LY_EVALID;
2217 }
2218 }
2219 } else if (base_enums) {
2220 /* inherit the assigned value */
2221 e->value = base_enums[match].value;
2222 if (!u || e->value >= value) {
2223 value = e->value + 1;
2224 }
2225 } else {
2226 /* assign value automatically */
2227 if (u && value == INT32_MIN) {
2228 /* counter overflow */
2229 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2230 "Invalid enumeration - it is not possible to auto-assign enum value for "
2231 "\"%s\" since the highest value is already 2147483647.", e->name);
2232 return LY_EVALID;
2233 }
2234 e->value = value++;
2235 }
2236 } else { /* LY_TYPE_BITS */
2237 if (enums_p[u].flags & LYS_SET_VALUE) {
2238 e->value = (int32_t)enums_p[u].value;
2239 if (!u || (uint32_t)e->value >= position) {
2240 position = (uint32_t)e->value + 1;
2241 }
2242 /* check collision with other values */
2243 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2244 if (e->value == (*enums)[v].value) {
2245 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2246 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2247 (uint32_t)e->value, e->name, (*enums)[v].name);
2248 return LY_EVALID;
2249 }
2250 }
2251 } else if (base_enums) {
2252 /* inherit the assigned value */
2253 e->value = base_enums[match].value;
2254 if (!u || (uint32_t)e->value >= position) {
2255 position = (uint32_t)e->value + 1;
2256 }
2257 } else {
2258 /* assign value automatically */
2259 if (u && position == 0) {
2260 /* counter overflow */
2261 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2262 "Invalid bits - it is not possible to auto-assign bit position for "
2263 "\"%s\" since the highest value is already 4294967295.", e->name);
2264 return LY_EVALID;
2265 }
2266 e->value = position++;
2267 }
2268 }
2269
2270 if (base_enums) {
2271 /* the assigned values must not change from the derived type */
2272 if (e->value != base_enums[match].value) {
2273 if (basetype == LY_TYPE_ENUM) {
2274 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2275 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2276 e->name, base_enums[match].value, e->value);
2277 } else {
2278 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2279 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2280 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2281 }
2282 return LY_EVALID;
2283 }
2284 }
2285
Radek Krejciec4da802019-05-02 13:02:41 +02002286 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002287 COMPILE_EXTS_GOTO(ctx, enums_p[u].exts, e->exts, e, basetype == LY_TYPE_ENUM ? LYEXT_PAR_TYPE_ENUM : LYEXT_PAR_TYPE_BIT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002288
2289 if (basetype == LY_TYPE_BITS) {
2290 /* keep bits ordered by position */
2291 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2292 if (v != u) {
2293 memcpy(&storage, e, sizeof *e);
2294 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2295 memcpy(&(*enums)[v], &storage, sizeof storage);
2296 }
2297 }
2298 }
2299
2300done:
2301 return ret;
2302}
2303
Radek Krejcia3045382018-11-22 14:30:31 +01002304#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2305 for ((NODE) = (NODE)->parent; \
Michal Vasko03ff5a72019-09-11 13:49:33 +02002306 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_ACTION)); \
Radek Krejcia3045382018-11-22 14:30:31 +01002307 (NODE) = (NODE)->parent); \
2308 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2309 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2310 TERM; \
2311 }
2312
2313/**
2314 * @brief Validate the predicate(s) from the leafref path.
2315 * @param[in] ctx Compile context
2316 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2317 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002318 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002319 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002320 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002321 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2322 */
2323static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002324lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002325 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002326{
2327 LY_ERR ret = LY_EVALID;
2328 const struct lys_module *mod;
2329 const struct lysc_node *src_node, *dst_node;
2330 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2331 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002332 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002333 const char *start, *end, *pke_end;
2334 struct ly_set keys = {0};
2335 int i;
2336
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002337 assert(path_context);
2338
Radek Krejcia3045382018-11-22 14:30:31 +01002339 while (**predicate == '[') {
2340 start = (*predicate)++;
2341
2342 while (isspace(**predicate)) {
2343 ++(*predicate);
2344 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002345 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002346 while (isspace(**predicate)) {
2347 ++(*predicate);
2348 }
2349 if (**predicate != '=') {
2350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002351 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2352 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002353 goto cleanup;
2354 }
2355 ++(*predicate);
2356 while (isspace(**predicate)) {
2357 ++(*predicate);
2358 }
2359
2360 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2361 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2362 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2363 goto cleanup;
2364 }
2365 --pke_end;
2366 while (isspace(*pke_end)) {
2367 --pke_end;
2368 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002369 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002370 /* localize path-key-expr */
2371 pke_start = path_key_expr = *predicate;
2372 /* move after the current predicate */
2373 *predicate = end + 1;
2374
2375 /* source (must be leaf or leaf-list) */
2376 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002377 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002378 if (!mod) {
2379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2380 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002381 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002382 goto cleanup;
2383 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002384 if (!mod->implemented) {
2385 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002386 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002387 }
Radek Krejcia3045382018-11-22 14:30:31 +01002388 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002389 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002390 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002391 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002392 if (!(context_node->flags & LYS_KEYLESS)) {
2393 struct lysc_node *key;
2394 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02002395 if (!ly_strncmp(key->name, src, src_len)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002396 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002397 break;
2398 }
2399 }
2400 }
Radek Krejcia3045382018-11-22 14:30:31 +01002401 if (!src_node) {
2402 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002403 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002404 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002405 goto cleanup;
2406 }
Radek Krejcia3045382018-11-22 14:30:31 +01002407
2408 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002409 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002410 i = ly_set_add(&keys, (void*)src_node, 0);
2411 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002412 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002413 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002414 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002415 *predicate - start, start, src_node->name);
2416 goto cleanup;
2417 }
2418
2419 /* destination */
2420 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002421 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002422
2423 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002424 if (strncmp(path_key_expr, "current", 7)) {
2425error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002426 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2427 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2428 *predicate - start, start);
2429 goto cleanup;
2430 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002431 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2432 if (*path_key_expr != '(') {
2433 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002434 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002435 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2436 if (*path_key_expr != ')') {
2437 goto error_current_function_invocation;
2438 }
2439 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002440
2441 if (*path_key_expr != '/') {
2442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2443 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2444 *predicate - start, start);
2445 goto cleanup;
2446 }
2447 ++path_key_expr;
2448 while (isspace(*path_key_expr)) {
2449 ++path_key_expr;
2450 }
2451
2452 /* rel-path-keyexpr:
2453 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2454 while (!strncmp(path_key_expr, "..", 2)) {
2455 ++dest_parent_times;
2456 path_key_expr += 2;
2457 while (isspace(*path_key_expr)) {
2458 ++path_key_expr;
2459 }
2460 if (*path_key_expr != '/') {
2461 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2462 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2463 *predicate - start, start);
2464 goto cleanup;
2465 }
2466 ++path_key_expr;
2467 while (isspace(*path_key_expr)) {
2468 ++path_key_expr;
2469 }
2470
2471 /* path is supposed to be evaluated in data tree, so we have to skip
2472 * all schema nodes that cannot be instantiated in data tree */
2473 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2474 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2475 *predicate - start, start);
2476 }
2477 if (!dest_parent_times) {
2478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2479 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2480 *predicate - start, start);
2481 goto cleanup;
2482 }
2483 if (path_key_expr == pke_end) {
2484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2485 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2486 *predicate - start, start);
2487 goto cleanup;
2488 }
2489
2490 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002491 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002492 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002494 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2495 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002496 goto cleanup;
2497 }
2498
2499 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002500 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002501 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002502 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002503 }
2504 if (!mod) {
2505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002506 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002507 *predicate - start, start, dst_len, dst);
2508 goto cleanup;
2509 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002510 if (!mod->implemented) {
2511 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002512 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002513 }
Radek Krejcia3045382018-11-22 14:30:31 +01002514
Michal Vaskoe444f752020-02-10 12:20:06 +01002515 dst_node = lys_find_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
Radek Krejcia3045382018-11-22 14:30:31 +01002516 if (!dst_node) {
2517 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002518 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002519 *predicate - start, start, path_key_expr - pke_start, pke_start);
2520 goto cleanup;
2521 }
2522 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002523 if (!(dst_node->nodetype & (dst_node->module->version < LYS_VERSION_1_1 ? LYS_LEAF : LYS_LEAF | LYS_LEAFLIST))) {
Radek Krejcia3045382018-11-22 14:30:31 +01002524 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002525 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002526 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2527 goto cleanup;
2528 }
2529 }
2530
2531 ret = LY_SUCCESS;
2532cleanup:
2533 ly_set_erase(&keys, NULL);
2534 return ret;
2535}
2536
2537/**
2538 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2539 *
2540 * path-arg = absolute-path / relative-path
2541 * absolute-path = 1*("/" (node-identifier *path-predicate))
2542 * relative-path = 1*(".." "/") descendant-path
2543 *
2544 * @param[in,out] path Path to parse.
2545 * @param[out] prefix Prefix of the token, NULL if there is not any.
2546 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2547 * @param[out] name Name of the token.
2548 * @param[out] nam_len Length of the name.
2549 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2550 * must not be changed between consecutive calls. -1 if the
2551 * path is absolute.
2552 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2553 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2554 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002555LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002556lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2557 int *parent_times, int *has_predicate)
2558{
2559 int par_times = 0;
2560
2561 assert(path && *path);
2562 assert(parent_times);
2563 assert(prefix);
2564 assert(prefix_len);
2565 assert(name);
2566 assert(name_len);
2567 assert(has_predicate);
2568
2569 *prefix = NULL;
2570 *prefix_len = 0;
2571 *name = NULL;
2572 *name_len = 0;
2573 *has_predicate = 0;
2574
2575 if (!*parent_times) {
2576 if (!strncmp(*path, "..", 2)) {
2577 *path += 2;
2578 ++par_times;
2579 while (!strncmp(*path, "/..", 3)) {
2580 *path += 3;
2581 ++par_times;
2582 }
2583 }
2584 if (par_times) {
2585 *parent_times = par_times;
2586 } else {
2587 *parent_times = -1;
2588 }
2589 }
2590
2591 if (**path != '/') {
2592 return LY_EINVAL;
2593 }
2594 /* skip '/' */
2595 ++(*path);
2596
2597 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002598 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002599
2600 if ((**path == '/' && (*path)[1]) || !**path) {
2601 /* path continues by another token or this is the last token */
2602 return LY_SUCCESS;
2603 } else if ((*path)[0] != '[') {
2604 /* unexpected character */
2605 return LY_EINVAL;
2606 } else {
2607 /* predicate starting with [ */
2608 *has_predicate = 1;
2609 return LY_SUCCESS;
2610 }
2611}
2612
2613/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002614 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2615 *
2616 * The set of features used for target must be a subset of features used for the leafref.
2617 * This is not a perfect, we should compare the truth tables but it could require too much resources
2618 * and RFC 7950 does not require it explicitely, so we simplify that.
2619 *
2620 * @param[in] refnode The leafref node.
2621 * @param[in] target Tha target node of the leafref.
2622 * @return LY_SUCCESS or LY_EVALID;
2623 */
2624static LY_ERR
2625lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2626{
2627 LY_ERR ret = LY_EVALID;
2628 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002629 unsigned int u, v, count;
2630 struct ly_set features = {0};
2631
2632 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002633 if (iter->iffeatures) {
2634 LY_ARRAY_FOR(iter->iffeatures, u) {
2635 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2636 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002637 }
2638 }
2639 }
2640 }
2641
2642 /* we should have, in features set, a superset of features applicable to the target node.
2643 * So when adding features applicable to the target into the features set, we should not be
2644 * able to actually add any new feature, otherwise it is not a subset of features applicable
2645 * to the leafref itself. */
2646 count = features.count;
2647 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002648 if (iter->iffeatures) {
2649 LY_ARRAY_FOR(iter->iffeatures, u) {
2650 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2651 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002652 /* new feature was added (or LY_EMEM) */
2653 goto cleanup;
2654 }
2655 }
2656 }
2657 }
2658 }
2659 ret = LY_SUCCESS;
2660
2661cleanup:
2662 ly_set_erase(&features, NULL);
2663 return ret;
2664}
2665
2666/**
Radek Krejcia3045382018-11-22 14:30:31 +01002667 * @brief Validate the leafref path.
2668 * @param[in] ctx Compile context
2669 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002670 * @param[in] leafref Leafref to validate.
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002671 * @param[out] target Optional resolved leafref target.
Radek Krejcia3045382018-11-22 14:30:31 +01002672 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2673 */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002674LY_ERR
2675lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref,
2676 const struct lysc_node **target)
Radek Krejcia3045382018-11-22 14:30:31 +01002677{
Michal Vaskoecd62de2019-11-13 12:35:11 +01002678 const struct lysc_node *node = NULL, *parent = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +01002679 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002680 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002681 const char *id, *prefix, *name;
2682 size_t prefix_len, name_len;
2683 int parent_times = 0, has_predicate;
2684 unsigned int iter, u;
2685 LY_ERR ret = LY_SUCCESS;
2686
2687 assert(ctx);
2688 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002689 assert(leafref);
2690
Radek Krejci1c0c3442019-07-23 16:08:47 +02002691 ctx->path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02002692 lysc_path(startnode, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci1c0c3442019-07-23 16:08:47 +02002693 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002694
Radek Krejcia3045382018-11-22 14:30:31 +01002695 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002696 id = leafref->path;
Juraj Vijtiukbd0f2a62019-12-11 15:58:18 +01002697
2698 if (!*id) {
2699 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Empty leafref path.");
2700 return LY_EVALID;
2701 }
2702
Radek Krejcia3045382018-11-22 14:30:31 +01002703 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2704 if (!iter) { /* first iteration */
2705 /* precess ".." in relative paths */
2706 if (parent_times > 0) {
2707 /* move from the context node */
2708 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2709 /* path is supposed to be evaluated in data tree, so we have to skip
2710 * all schema nodes that cannot be instantiated in data tree */
2711 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002712 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002713 }
2714 }
2715 }
2716
2717 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002718 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002719 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002720 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002721 }
2722 if (!mod) {
2723 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002724 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2725 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002726 return LY_EVALID;
2727 }
Radek Krejci3d929562019-02-21 11:25:55 +01002728 if (!mod->implemented) {
2729 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002730 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci3d929562019-02-21 11:25:55 +01002731 }
Radek Krejcia3045382018-11-22 14:30:31 +01002732
Michal Vaskoe444f752020-02-10 12:20:06 +01002733 node = lys_find_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
Radek Krejcia3045382018-11-22 14:30:31 +01002734 if (!node) {
2735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002736 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002737 return LY_EVALID;
2738 }
2739 parent = node;
2740
2741 if (has_predicate) {
2742 /* we have predicate, so the current result must be list */
2743 if (node->nodetype != LYS_LIST) {
2744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2745 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002746 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002747 return LY_EVALID;
2748 }
2749
Radek Krejcibade82a2018-12-05 10:13:48 +01002750 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2751 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002752 }
2753
2754 ++iter;
2755 }
2756 if (ret) {
2757 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002758 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002759 return LY_EVALID;
2760 }
2761
2762 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2763 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2764 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002765 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002766 return LY_EVALID;
2767 }
2768
2769 /* check status */
2770 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2771 return LY_EVALID;
2772 }
2773
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002774 /* check config */
2775 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2776 if (node->flags & LYS_CONFIG_R) {
2777 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2778 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2779 leafref->path);
2780 return LY_EVALID;
2781 }
2782 }
2783
Radek Krejci412ddfa2018-11-23 11:44:11 +01002784 /* store the target's type and check for circular chain of leafrefs */
2785 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2786 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2787 if (type == (struct lysc_type*)leafref) {
2788 /* circular chain detected */
2789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2790 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2791 return LY_EVALID;
2792 }
2793 }
2794
Radek Krejci58d171e2018-11-23 13:50:55 +01002795 /* check if leafref and its target are under common if-features */
2796 if (lys_compile_leafref_features_validate(startnode, node)) {
2797 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2798 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2799 leafref->path);
2800 return LY_EVALID;
2801 }
2802
Radek Krejci327de162019-06-14 12:52:07 +02002803 ctx->path_len = 1;
2804 ctx->path[1] = '\0';
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002805 if (target) {
2806 *target = node;
2807 }
Radek Krejcia3045382018-11-22 14:30:31 +01002808 return LY_SUCCESS;
2809}
2810
Radek Krejcicdfecd92018-11-26 11:27:32 +01002811static LY_ERR lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02002812 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002813/**
2814 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2815 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002816 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2817 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2818 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2819 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002820 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002821 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002822 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002823 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2824 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2825 * @param[out] type Newly created type structure with the filled information about the type.
2826 * @return LY_ERR value.
2827 */
Radek Krejci19a96102018-11-15 13:38:09 +01002828static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002829lys_compile_type_(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02002830 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002831 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002832{
2833 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002834 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002835 struct lysc_type_bin *bin;
2836 struct lysc_type_num *num;
2837 struct lysc_type_str *str;
2838 struct lysc_type_bits *bits;
2839 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002840 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002841 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002842 struct lysc_type_union *un, *un_aux;
2843 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002844
2845 switch (basetype) {
2846 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002847 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002848
2849 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002850 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002851 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2852 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002853 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002854 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, bin->length->exts, bin->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002855 }
2856 }
2857
2858 if (tpdfname) {
2859 type_p->compiled = *type;
2860 *type = calloc(1, sizeof(struct lysc_type_bin));
2861 }
2862 break;
2863 case LY_TYPE_BITS:
2864 /* RFC 7950 9.7 - bits */
2865 bits = (struct lysc_type_bits*)(*type);
2866 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002867 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002868 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2869 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002870 }
2871
Radek Krejci555cb5b2018-11-16 14:54:33 +01002872 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002873 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002874 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002876 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002877 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002878 free(*type);
2879 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002880 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002881 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002882 }
2883
2884 if (tpdfname) {
2885 type_p->compiled = *type;
2886 *type = calloc(1, sizeof(struct lysc_type_bits));
2887 }
2888 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002889 case LY_TYPE_DEC64:
2890 dec = (struct lysc_type_dec*)(*type);
2891
2892 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002893 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002894 if (!type_p->fraction_digits) {
2895 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002896 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002897 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002898 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002899 free(*type);
2900 *type = NULL;
2901 }
2902 return LY_EVALID;
2903 }
2904 } else if (type_p->fraction_digits) {
2905 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002906 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002907 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2908 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002909 tpdfname);
2910 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2912 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002913 free(*type);
2914 *type = NULL;
2915 }
2916 return LY_EVALID;
2917 }
2918 dec->fraction_digits = type_p->fraction_digits;
2919
2920 /* RFC 7950 9.2.4 - range */
2921 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002922 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2923 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002924 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002925 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, dec->range->exts, dec->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002926 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002927 }
2928
2929 if (tpdfname) {
2930 type_p->compiled = *type;
2931 *type = calloc(1, sizeof(struct lysc_type_dec));
2932 }
2933 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002934 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002935 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002936
2937 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002938 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002939 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2940 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002941 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002942 COMPILE_EXTS_GOTO(ctx, type_p->length->exts, str->length->exts, str->length, LYEXT_PAR_LENGTH, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002943 }
2944 } else if (base && ((struct lysc_type_str*)base)->length) {
2945 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2946 }
2947
2948 /* RFC 7950 9.4.5 - pattern */
2949 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002950 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002951 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002952 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2953 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2954 }
2955
2956 if (tpdfname) {
2957 type_p->compiled = *type;
2958 *type = calloc(1, sizeof(struct lysc_type_str));
2959 }
2960 break;
2961 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002962 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002963
2964 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002965 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002966 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002967 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002968 }
2969
Radek Krejci555cb5b2018-11-16 14:54:33 +01002970 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002971 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002972 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002974 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002975 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002976 free(*type);
2977 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002978 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002979 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002980 }
2981
2982 if (tpdfname) {
2983 type_p->compiled = *type;
2984 *type = calloc(1, sizeof(struct lysc_type_enum));
2985 }
2986 break;
2987 case LY_TYPE_INT8:
2988 case LY_TYPE_UINT8:
2989 case LY_TYPE_INT16:
2990 case LY_TYPE_UINT16:
2991 case LY_TYPE_INT32:
2992 case LY_TYPE_UINT32:
2993 case LY_TYPE_INT64:
2994 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002995 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002996
2997 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002998 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002999 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
3000 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01003001 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02003002 COMPILE_EXTS_GOTO(ctx, type_p->range->exts, num->range->exts, num->range, LYEXT_PAR_RANGE, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003003 }
3004 }
3005
3006 if (tpdfname) {
3007 type_p->compiled = *type;
3008 *type = calloc(1, sizeof(struct lysc_type_num));
3009 }
3010 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01003011 case LY_TYPE_IDENT:
3012 idref = (struct lysc_type_identityref*)(*type);
3013
3014 /* RFC 7950 9.10.2 - base */
3015 if (type_p->bases) {
3016 if (base) {
3017 /* only the directly derived identityrefs can contain base specification */
3018 if (tpdfname) {
3019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01003020 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01003021 tpdfname);
3022 } else {
3023 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01003024 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01003025 free(*type);
3026 *type = NULL;
3027 }
3028 return LY_EVALID;
3029 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003030 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01003031 }
3032
3033 if (!base && !type_p->flags) {
3034 /* type derived from identityref built-in type must contain at least one base */
3035 if (tpdfname) {
3036 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
3037 } else {
3038 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
3039 free(*type);
3040 *type = NULL;
3041 }
3042 return LY_EVALID;
3043 }
3044
3045 if (tpdfname) {
3046 type_p->compiled = *type;
3047 *type = calloc(1, sizeof(struct lysc_type_identityref));
3048 }
3049 break;
Radek Krejcia3045382018-11-22 14:30:31 +01003050 case LY_TYPE_LEAFREF:
3051 /* RFC 7950 9.9.3 - require-instance */
3052 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003053 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003054 if (tpdfname) {
3055 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3056 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
3057 } else {
3058 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3059 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
3060 free(*type);
3061 *type = NULL;
3062 }
3063 return LY_EVALID;
3064 }
Radek Krejcia3045382018-11-22 14:30:31 +01003065 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01003066 } else if (base) {
3067 /* inherit */
3068 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01003069 } else {
3070 /* default is true */
3071 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
3072 }
3073 if (type_p->path) {
3074 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003075 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01003076 } else if (base) {
3077 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003078 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01003079 } else if (tpdfname) {
3080 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
3081 return LY_EVALID;
3082 } else {
3083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
3084 free(*type);
3085 *type = NULL;
3086 return LY_EVALID;
3087 }
3088 if (tpdfname) {
3089 type_p->compiled = *type;
3090 *type = calloc(1, sizeof(struct lysc_type_leafref));
3091 }
3092 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01003093 case LY_TYPE_INST:
3094 /* RFC 7950 9.9.3 - require-instance */
3095 if (type_p->flags & LYS_SET_REQINST) {
3096 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
3097 } else {
3098 /* default is true */
3099 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
3100 }
3101
3102 if (tpdfname) {
3103 type_p->compiled = *type;
3104 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3105 }
3106 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003107 case LY_TYPE_UNION:
3108 un = (struct lysc_type_union*)(*type);
3109
3110 /* RFC 7950 7.4 - type */
3111 if (type_p->types) {
3112 if (base) {
3113 /* only the directly derived union can contain types specification */
3114 if (tpdfname) {
3115 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3116 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
3117 tpdfname);
3118 } else {
3119 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3120 "Invalid type substatement for the type not directly derived from union built-in type.");
3121 free(*type);
3122 *type = NULL;
3123 }
3124 return LY_EVALID;
3125 }
3126 /* compile the type */
3127 additional = 0;
3128 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
3129 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02003130 LY_CHECK_RET(lys_compile_type(ctx, context_node_p, context_flags, context_mod, context_name, &type_p->types[u], &un->types[u + additional], NULL));
Radek Krejcicdfecd92018-11-26 11:27:32 +01003131 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
3132 /* add space for additional types from the union subtype */
3133 un_aux = (struct lysc_type_union *)un->types[u + additional];
3134 p = ly_realloc(((uint32_t*)(un->types) - 1), sizeof(uint32_t) + ((LY_ARRAY_SIZE(type_p->types) + additional + LY_ARRAY_SIZE(un_aux->types) - 1) * sizeof *(un->types)));
3135 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3136 un->types = (void*)((uint32_t*)(p) + 1);
3137
3138 /* copy subtypes of the subtype union */
3139 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
3140 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
3141 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
3142 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
3143 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3144 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
3145 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
3146 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3147 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3148 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3149 /* TODO extensions */
3150
3151 } else {
3152 un->types[u + additional] = un_aux->types[v];
3153 ++un_aux->types[v]->refcount;
3154 }
3155 ++additional;
3156 LY_ARRAY_INCREMENT(un->types);
3157 }
3158 /* compensate u increment in main loop */
3159 --additional;
3160
3161 /* free the replaced union subtype */
3162 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3163 } else {
3164 LY_ARRAY_INCREMENT(un->types);
3165 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003166 }
3167 }
3168
3169 if (!base && !type_p->flags) {
3170 /* type derived from union built-in type must contain at least one type */
3171 if (tpdfname) {
3172 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3173 } else {
3174 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3175 free(*type);
3176 *type = NULL;
3177 }
3178 return LY_EVALID;
3179 }
3180
3181 if (tpdfname) {
3182 type_p->compiled = *type;
3183 *type = calloc(1, sizeof(struct lysc_type_union));
3184 }
3185 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003186 case LY_TYPE_BOOL:
3187 case LY_TYPE_EMPTY:
3188 case LY_TYPE_UNKNOWN: /* just to complete switch */
3189 break;
3190 }
3191 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3192done:
3193 return ret;
3194}
3195
Radek Krejcia3045382018-11-22 14:30:31 +01003196/**
3197 * @brief Compile information about the leaf/leaf-list's type.
3198 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003199 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3200 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3201 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3202 * @param[in] context_name Name of the context node or referencing typedef for logging.
3203 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003204 * @param[out] type Newly created (or reused with increased refcount) type structure with the filled information about the type.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003205 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003206 * @return LY_ERR value.
3207 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003208static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003209lys_compile_type(struct lysc_ctx *ctx, struct lysp_node *context_node_p, uint16_t context_flags, struct lysp_module *context_mod, const char *context_name,
Radek Krejciec4da802019-05-02 13:02:41 +02003210 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003211{
3212 LY_ERR ret = LY_SUCCESS;
3213 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003214 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003215 struct type_context {
3216 const struct lysp_tpdf *tpdf;
3217 struct lysp_node *node;
3218 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003219 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003220 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003221 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003222 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003223 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003224 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003225
3226 (*type) = NULL;
3227
3228 tctx = calloc(1, sizeof *tctx);
3229 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003230 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003231 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3232 ret == LY_SUCCESS;
3233 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3234 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3235 if (basetype) {
3236 break;
3237 }
3238
3239 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003240 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3241 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003242 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3243
Radek Krejcicdfecd92018-11-26 11:27:32 +01003244 if (units && !*units) {
3245 /* inherit units */
3246 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3247 }
Radek Krejci01342af2019-01-03 15:18:08 +01003248 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003249 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003250 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003251 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003252 }
Radek Krejci01342af2019-01-03 15:18:08 +01003253 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003254 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3255 break;
3256 }
3257
Radek Krejci19a96102018-11-15 13:38:09 +01003258 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003259 /* it is not necessary to continue, the rest of the chain was already compiled,
3260 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003261 basetype = tctx->tpdf->type.compiled->basetype;
3262 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003263 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003264 dummyloops = 1;
3265 goto preparenext;
3266 } else {
3267 tctx = NULL;
3268 break;
3269 }
Radek Krejci19a96102018-11-15 13:38:09 +01003270 }
3271
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003272 /* circular typedef reference detection */
3273 for (u = 0; u < tpdf_chain.count; u++) {
3274 /* local part */
3275 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3276 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3278 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3279 free(tctx);
3280 ret = LY_EVALID;
3281 goto cleanup;
3282 }
3283 }
3284 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3285 /* global part for unions corner case */
3286 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3287 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3289 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3290 free(tctx);
3291 ret = LY_EVALID;
3292 goto cleanup;
3293 }
3294 }
3295
Radek Krejci19a96102018-11-15 13:38:09 +01003296 /* store information for the following processing */
3297 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3298
Radek Krejcicdfecd92018-11-26 11:27:32 +01003299preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003300 /* prepare next loop */
3301 tctx_prev = tctx;
3302 tctx = calloc(1, sizeof *tctx);
3303 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3304 }
3305 free(tctx);
3306
3307 /* allocate type according to the basetype */
3308 switch (basetype) {
3309 case LY_TYPE_BINARY:
3310 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003311 break;
3312 case LY_TYPE_BITS:
3313 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003314 break;
3315 case LY_TYPE_BOOL:
3316 case LY_TYPE_EMPTY:
3317 *type = calloc(1, sizeof(struct lysc_type));
3318 break;
3319 case LY_TYPE_DEC64:
3320 *type = calloc(1, sizeof(struct lysc_type_dec));
3321 break;
3322 case LY_TYPE_ENUM:
3323 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003324 break;
3325 case LY_TYPE_IDENT:
3326 *type = calloc(1, sizeof(struct lysc_type_identityref));
3327 break;
3328 case LY_TYPE_INST:
3329 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3330 break;
3331 case LY_TYPE_LEAFREF:
3332 *type = calloc(1, sizeof(struct lysc_type_leafref));
3333 break;
3334 case LY_TYPE_STRING:
3335 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003336 break;
3337 case LY_TYPE_UNION:
3338 *type = calloc(1, sizeof(struct lysc_type_union));
3339 break;
3340 case LY_TYPE_INT8:
3341 case LY_TYPE_UINT8:
3342 case LY_TYPE_INT16:
3343 case LY_TYPE_UINT16:
3344 case LY_TYPE_INT32:
3345 case LY_TYPE_UINT32:
3346 case LY_TYPE_INT64:
3347 case LY_TYPE_UINT64:
3348 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003349 break;
3350 case LY_TYPE_UNKNOWN:
3351 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3352 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3353 ret = LY_EVALID;
3354 goto cleanup;
3355 }
3356 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003357 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003358 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3359 ly_data_type2str[basetype]);
3360 free(*type);
3361 (*type) = NULL;
3362 ret = LY_EVALID;
3363 goto cleanup;
3364 }
3365
3366 /* get restrictions from the referred typedefs */
3367 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3368 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003369
3370 /* remember the typedef context for circular check */
3371 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3372
Radek Krejci43699232018-11-23 14:59:46 +01003373 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003374 base = tctx->tpdf->type.compiled;
3375 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003376 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003377 /* no change, just use the type information from the base */
3378 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3379 ++base->refcount;
3380 continue;
3381 }
3382
3383 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003384 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3385 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3386 tctx->tpdf->name, ly_data_type2str[basetype]);
3387 ret = LY_EVALID;
3388 goto cleanup;
3389 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3390 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3391 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3392 tctx->tpdf->name, tctx->tpdf->dflt);
3393 ret = LY_EVALID;
3394 goto cleanup;
3395 }
3396
Radek Krejci19a96102018-11-15 13:38:09 +01003397 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003398 /* TODO user type plugins */
3399 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003400 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003401 ret = lys_compile_type_(ctx, tctx->node, tctx->tpdf->flags, tctx->mod, tctx->tpdf->name, &((struct lysp_tpdf*)tctx->tpdf)->type,
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003402 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003403 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003404 LY_CHECK_GOTO(ret, cleanup);
3405 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003406 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003407 /* remove the processed typedef contexts from the stack for circular check */
3408 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003409
Radek Krejcic5c27e52018-11-15 14:38:11 +01003410 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003411 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003412 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003413 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003414 /* TODO user type plugins */
3415 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003416 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003417 ret = lys_compile_type_(ctx, context_node_p, context_flags, context_mod, context_name, type_p, ctx->mod_def, basetype, NULL, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003418 LY_CHECK_GOTO(ret, cleanup);
3419 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003420 /* no specific restriction in leaf's type definition, copy from the base */
3421 free(*type);
3422 (*type) = base;
3423 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003424 }
Radek Krejcia1911222019-07-22 17:24:50 +02003425 if (dflt && !(*type)->dflt) {
3426 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003427 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003428 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3429 (*type)->dflt->realtype = (*type);
3430 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3431 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003432 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003433 if (err) {
3434 ly_err_print(err);
3435 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3436 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3437 ly_err_free(err);
3438 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003439 if (ret == LY_EINCOMPLETE) {
3440 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003441 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003442
3443 /* but in general result is so far ok */
3444 ret = LY_SUCCESS;
3445 }
Radek Krejcia1911222019-07-22 17:24:50 +02003446 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003447 }
Radek Krejci19a96102018-11-15 13:38:09 +01003448
Radek Krejci0935f412019-08-20 16:15:18 +02003449 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003450
3451cleanup:
3452 ly_set_erase(&tpdf_chain, free);
3453 return ret;
3454}
3455
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003456/**
3457 * @brief Compile status information of the given node.
3458 *
3459 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3460 * has the status correctly set during the compilation.
3461 *
3462 * @param[in] ctx Compile context
3463 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3464 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3465 * the compatibility with the parent's status value.
3466 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3467 * @return LY_ERR value.
3468 */
3469static LY_ERR
3470lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3471{
3472 /* status - it is not inherited by specification, but it does not make sense to have
3473 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3474 if (!((*node_flags) & LYS_STATUS_MASK)) {
3475 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3476 if ((parent_flags & 0x3) != 0x3) {
3477 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3478 * combination of bits (0x3) which marks the uses_status value */
3479 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3480 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3481 }
3482 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3483 } else {
3484 (*node_flags) |= LYS_STATUS_CURR;
3485 }
3486 } else if (parent_flags & LYS_STATUS_MASK) {
3487 /* check status compatibility with the parent */
3488 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3489 if ((*node_flags) & LYS_STATUS_CURR) {
3490 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3491 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3492 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3493 } else { /* LYS_STATUS_DEPRC */
3494 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3495 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3496 }
3497 return LY_EVALID;
3498 }
3499 }
3500 return LY_SUCCESS;
3501}
3502
Radek Krejci8cce8532019-03-05 11:27:45 +01003503/**
3504 * @brief Check uniqness of the node/action/notification name.
3505 *
3506 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3507 * structures, but they share the namespace so we need to check their name collisions.
3508 *
3509 * @param[in] ctx Compile context.
3510 * @param[in] children List (linked list) of data nodes to go through.
3511 * @param[in] actions List (sized array) of actions or RPCs to go through.
3512 * @param[in] notifs List (sized array) of Notifications to go through.
3513 * @param[in] name Name of the item to find in the given lists.
3514 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3515 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3516 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3517 */
3518static LY_ERR
3519lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3520 const struct lysc_action *actions, const struct lysc_notif *notifs,
3521 const char *name, void *exclude)
3522{
3523 const struct lysc_node *iter;
3524 unsigned int u;
3525
3526 LY_LIST_FOR(children, iter) {
3527 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3528 goto error;
3529 }
3530 }
3531 LY_ARRAY_FOR(actions, u) {
3532 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3533 goto error;
3534 }
3535 }
3536 LY_ARRAY_FOR(notifs, u) {
3537 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3538 goto error;
3539 }
3540 }
3541 return LY_SUCCESS;
3542error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003544 return LY_EEXIST;
3545}
3546
Radek Krejciec4da802019-05-02 13:02:41 +02003547static LY_ERR lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status);
Radek Krejci19a96102018-11-15 13:38:09 +01003548
Radek Krejcia3045382018-11-22 14:30:31 +01003549/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003550 * @brief Compile parsed RPC/action schema node information.
3551 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003552 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003553 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003554 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3555 * @param[in] uses_status If the RPC/action is being placed instead of uses, here we have the uses's status value (as node's flags).
3556 * Zero means no uses, non-zero value with no status bit set mean the default status.
3557 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3558 */
3559static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003560lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003561 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3562{
3563 LY_ERR ret = LY_SUCCESS;
3564 struct lysp_node *child_p;
3565 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003566 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003567
Radek Krejci327de162019-06-14 12:52:07 +02003568 lysc_update_path(ctx, parent, action_p->name);
3569
Radek Krejci8cce8532019-03-05 11:27:45 +01003570 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3571 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3572 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3573 action_p->name, action)) {
3574 return LY_EVALID;
3575 }
3576
Radek Krejciec4da802019-05-02 13:02:41 +02003577 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003578 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003579 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003580 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003581 return LY_EVALID;
3582 }
3583
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003584 action->nodetype = LYS_ACTION;
3585 action->module = ctx->mod;
3586 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003587 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003588 action->sp = action_p;
3589 }
3590 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3591
3592 /* status - it is not inherited by specification, but it does not make sense to have
3593 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3594 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3595
3596 DUP_STRING(ctx->ctx, action_p->name, action->name);
3597 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3598 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003599 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003600 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003601
3602 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003603 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003604 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003605 COMPILE_EXTS_GOTO(ctx, action_p->input.exts, action->input_exts, &action->input, LYEXT_PAR_INPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003606 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003607 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003608 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003609 }
Radek Krejci327de162019-06-14 12:52:07 +02003610 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003611 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003612
3613 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003614 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003615 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003616 COMPILE_EXTS_GOTO(ctx, action_p->output.exts, action->output_exts, &action->output, LYEXT_PAR_OUTPUT, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003617 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003618 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003619 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003620 }
Radek Krejci327de162019-06-14 12:52:07 +02003621 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003622 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003623
3624 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3625 /* do not check "must" semantics in a grouping */
3626 ly_set_add(&ctx->unres, action, 0);
3627 }
3628
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003629cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003630 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003631 return ret;
3632}
3633
3634/**
Radek Krejci43981a32019-04-12 09:44:11 +02003635 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003636 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003637 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003638 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3639 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3640 * @param[in] uses_status If the Notification is being placed instead of uses, here we have the uses's status value (as node's flags).
Radek Krejcifc11bd72019-04-11 16:00:05 +02003641 * Zero means no uses, non-zero value with no status bit set mean the default status.
3642 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3643 */
3644static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003645lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003646 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3647{
3648 LY_ERR ret = LY_SUCCESS;
3649 struct lysp_node *child_p;
3650 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003651 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003652
Radek Krejci327de162019-06-14 12:52:07 +02003653 lysc_update_path(ctx, parent, notif_p->name);
3654
Radek Krejcifc11bd72019-04-11 16:00:05 +02003655 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3656 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3657 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3658 notif_p->name, notif)) {
3659 return LY_EVALID;
3660 }
3661
Radek Krejciec4da802019-05-02 13:02:41 +02003662 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3664 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003665 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003666 return LY_EVALID;
3667 }
3668
3669 notif->nodetype = LYS_NOTIF;
3670 notif->module = ctx->mod;
3671 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003672 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003673 notif->sp = notif_p;
3674 }
3675 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3676
3677 /* status - it is not inherited by specification, but it does not make sense to have
3678 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3679 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3680
3681 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3682 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3683 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003684 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003685 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003686 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3687 /* do not check "must" semantics in a grouping */
3688 ly_set_add(&ctx->unres, notif, 0);
3689 }
Radek Krejci0935f412019-08-20 16:15:18 +02003690 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003691
Radek Krejciec4da802019-05-02 13:02:41 +02003692 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003693 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003694 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003695 }
3696
Radek Krejci327de162019-06-14 12:52:07 +02003697 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003698cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003699 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003700 return ret;
3701}
3702
3703/**
Radek Krejcia3045382018-11-22 14:30:31 +01003704 * @brief Compile parsed container node information.
3705 * @param[in] ctx Compile context
3706 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003707 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3708 * is enriched with the container-specific information.
3709 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3710 */
Radek Krejci19a96102018-11-15 13:38:09 +01003711static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003712lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003713{
3714 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3715 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3716 struct lysp_node *child_p;
3717 unsigned int u;
3718 LY_ERR ret = LY_SUCCESS;
3719
Radek Krejcife909632019-02-12 15:34:42 +01003720 if (cont_p->presence) {
3721 cont->flags |= LYS_PRESENCE;
3722 }
3723
Radek Krejci19a96102018-11-15 13:38:09 +01003724 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003725 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003726 }
3727
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003728 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003729 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3730 /* do not check "must" semantics in a grouping */
3731 ly_set_add(&ctx->unres, cont, 0);
3732 }
Radek Krejciec4da802019-05-02 13:02:41 +02003733 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3734 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003735
3736done:
3737 return ret;
3738}
3739
Radek Krejci33f72892019-02-21 10:36:58 +01003740/*
3741 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3742 * @param[in] ctx Compile context.
3743 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3744 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003745 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3746 * @return LY_ERR value.
3747 */
3748static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003749lys_compile_node_type(struct lysc_ctx *ctx, struct lysp_node *context_node, struct lysp_type *type_p, struct lysc_node_leaf *leaf)
Radek Krejci33f72892019-02-21 10:36:58 +01003750{
Radek Krejcia1911222019-07-22 17:24:50 +02003751 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003752 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3753
Radek Krejciec4da802019-05-02 13:02:41 +02003754 LY_CHECK_RET(lys_compile_type(ctx, context_node, leaf->flags, ctx->mod_def->parsed, leaf->name, type_p, &leaf->type,
Radek Krejci33f72892019-02-21 10:36:58 +01003755 leaf->units ? NULL : &leaf->units));
3756 if (leaf->nodetype == LYS_LEAFLIST) {
3757 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003758 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3759 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003760 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003761 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3762 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3763 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3764 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003765 LY_ARRAY_INCREMENT(llist->dflts);
3766 }
3767 } else {
3768 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003769 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003770 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3771 leaf->dflt->realtype = leaf->type->dflt->realtype;
3772 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3773 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003774 }
3775 }
3776 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3777 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3778 ly_set_add(&ctx->unres, leaf, 0);
3779 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3780 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3781 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3782 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3783 ly_set_add(&ctx->unres, leaf, 0);
3784 }
3785 }
3786 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003787 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3788 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3789 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3790 return LY_EVALID;
3791 }
3792 }
3793
Radek Krejci33f72892019-02-21 10:36:58 +01003794 return LY_SUCCESS;
3795}
3796
Radek Krejcia3045382018-11-22 14:30:31 +01003797/**
3798 * @brief Compile parsed leaf node information.
3799 * @param[in] ctx Compile context
3800 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003801 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3802 * is enriched with the leaf-specific information.
3803 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3804 */
Radek Krejci19a96102018-11-15 13:38:09 +01003805static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003806lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003807{
3808 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3809 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3810 unsigned int u;
3811 LY_ERR ret = LY_SUCCESS;
3812
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003813 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003814 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3815 /* do not check "must" semantics in a grouping */
3816 ly_set_add(&ctx->unres, leaf, 0);
3817 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003818 if (leaf_p->units) {
3819 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3820 leaf->flags |= LYS_SET_UNITS;
3821 }
Radek Krejcia1911222019-07-22 17:24:50 +02003822
3823 /* the dflt member is just filled to avoid getting the default value from the type */
3824 leaf->dflt = (void*)leaf_p->dflt;
3825 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003826 if (ret) {
3827 leaf->dflt = NULL;
3828 return ret;
3829 }
Radek Krejcia1911222019-07-22 17:24:50 +02003830
Radek Krejciccd20f12019-02-15 14:12:27 +01003831 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003832 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003833 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003834 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3835 leaf->dflt->realtype = leaf->type;
3836 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3837 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003838 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003839 leaf->dflt->realtype->refcount++;
3840 if (err) {
3841 ly_err_print(err);
3842 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3843 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3844 ly_err_free(err);
3845 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003846 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003847 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003848 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003849
3850 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003851 ret = LY_SUCCESS;
3852 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003853 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003854 leaf->flags |= LYS_SET_DFLT;
3855 }
Radek Krejci43699232018-11-23 14:59:46 +01003856
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003857
Radek Krejci19a96102018-11-15 13:38:09 +01003858done:
3859 return ret;
3860}
3861
Radek Krejcia3045382018-11-22 14:30:31 +01003862/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003863 * @brief Compile parsed leaf-list node information.
3864 * @param[in] ctx Compile context
3865 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003866 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3867 * is enriched with the leaf-list-specific information.
3868 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3869 */
3870static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003871lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003872{
3873 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3874 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003875 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003876 LY_ERR ret = LY_SUCCESS;
3877
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003878 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003879 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3880 /* do not check "must" semantics in a grouping */
3881 ly_set_add(&ctx->unres, llist, 0);
3882 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003883 if (llist_p->units) {
3884 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3885 llist->flags |= LYS_SET_UNITS;
3886 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003887
Radek Krejcia1911222019-07-22 17:24:50 +02003888 /* the dflts member is just filled to avoid getting the default value from the type */
3889 llist->dflts = (void*)llist_p->dflts;
3890 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003891 if (ret != LY_SUCCESS) {
3892 /* make sure the defaults are freed correctly */
3893 if (llist_p->dflts) {
3894 llist->dflts = NULL;
3895 }
3896 return ret;
3897 }
3898
Radek Krejci0e5d8382018-11-28 16:37:53 +01003899 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003900 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003901 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003902 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3903 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003904 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003905 LY_ARRAY_INCREMENT(llist->dflts_mods);
3906 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003907 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003908 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3909 llist->dflts[u]->realtype = llist->type;
3910 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3911 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003912 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003913 llist->dflts[u]->realtype->refcount++;
3914 if (err) {
3915 ly_err_print(err);
3916 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3917 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3918 ly_err_free(err);
3919 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003920 if (ret == LY_EINCOMPLETE) {
3921 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003922 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003923
3924 /* but in general result is so far ok */
3925 ret = LY_SUCCESS;
3926 }
Radek Krejcia1911222019-07-22 17:24:50 +02003927 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003928 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003929 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003930 }
Radek Krejcia1911222019-07-22 17:24:50 +02003931 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3932 /* configuration data values must be unique - so check the default values */
3933 LY_ARRAY_FOR(llist->dflts, u) {
3934 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3935 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3936 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003937 const char *val = llist->type->plugin->print(llist->dflts[v], LYD_XML, lys_get_prefix, llist->dflts_mods[v], &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02003938 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3939 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3940 if (dynamic) {
3941 free((char*)val);
3942 }
3943 return LY_EVALID;
3944 }
3945 }
3946 }
3947 }
3948
3949 /* TODO validate default value according to the type, possibly postpone the check when the leafref target is known */
Radek Krejci0e5d8382018-11-28 16:37:53 +01003950
3951 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003952 if (llist->min) {
3953 llist->flags |= LYS_MAND_TRUE;
3954 }
Radek Krejcib7408632018-11-28 17:12:11 +01003955 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003956
Radek Krejci0e5d8382018-11-28 16:37:53 +01003957done:
3958 return ret;
3959}
3960
3961/**
Radek Krejci7af64242019-02-18 13:07:53 +01003962 * @brief Compile information about list's uniques.
3963 * @param[in] ctx Compile context.
3964 * @param[in] context_module Module where the prefixes are going to be resolved.
3965 * @param[in] uniques Sized array list of unique statements.
3966 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3967 * @return LY_ERR value.
3968 */
3969static LY_ERR
3970lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3971{
3972 LY_ERR ret = LY_SUCCESS;
3973 struct lysc_node_leaf **key, ***unique;
Michal Vasko14654712020-02-06 08:35:21 +01003974 struct lysc_node *parent;
Radek Krejci7af64242019-02-18 13:07:53 +01003975 const char *keystr, *delim;
3976 size_t len;
3977 unsigned int v;
3978 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003979 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003980
3981 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3982 config = -1;
3983 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3984 keystr = uniques[v];
3985 while (keystr) {
3986 delim = strpbrk(keystr, " \t\n");
3987 if (delim) {
3988 len = delim - keystr;
3989 while (isspace(*delim)) {
3990 ++delim;
3991 }
3992 } else {
3993 len = strlen(keystr);
3994 }
3995
3996 /* unique node must be present */
3997 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003998 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3999 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01004000 if (ret != LY_SUCCESS) {
4001 if (ret == LY_EDENIED) {
4002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004003 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01004004 len, keystr, lys_nodetype2str((*key)->nodetype));
4005 }
4006 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004007 } else if (flags) {
4008 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4009 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01004010 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004011 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01004012 }
4013
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004014
Radek Krejci7af64242019-02-18 13:07:53 +01004015 /* all referenced leafs must be of the same config type */
4016 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
4017 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Michal Vasko14654712020-02-06 08:35:21 +01004018 "Unique statement \"%s\" refers to leaves with different config type.", uniques[v]);
Radek Krejci7af64242019-02-18 13:07:53 +01004019 return LY_EVALID;
4020 } else if ((*key)->flags & LYS_CONFIG_W) {
4021 config = 1;
4022 } else { /* LYS_CONFIG_R */
4023 config = 0;
4024 }
4025
Michal Vasko14654712020-02-06 08:35:21 +01004026 /* we forbid referencing nested lists because it is unspecified what instance of such a list to use */
4027 for (parent = (*key)->parent; parent != (struct lysc_node *)list; parent = parent->parent) {
4028 if (parent->nodetype == LYS_LIST) {
4029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4030 "Unique statement \"%s\" refers to a leaf in nested list \"%s\".", uniques[v], parent->name);
4031 return LY_EVALID;
4032 }
4033 }
4034
Radek Krejci7af64242019-02-18 13:07:53 +01004035 /* check status */
4036 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
4037 (*key)->flags, (*key)->module, (*key)->name));
4038
4039 /* mark leaf as unique */
4040 (*key)->flags |= LYS_UNIQUE;
4041
4042 /* next unique value in line */
4043 keystr = delim;
4044 }
4045 /* next unique definition */
4046 }
4047
4048 return LY_SUCCESS;
4049}
4050
4051/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004052 * @brief Compile parsed list node information.
4053 * @param[in] ctx Compile context
4054 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004055 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4056 * is enriched with the list-specific information.
4057 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4058 */
4059static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004060lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004061{
4062 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
4063 struct lysc_node_list *list = (struct lysc_node_list*)node;
4064 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004065 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004066 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01004067 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004068 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004069 LY_ERR ret = LY_SUCCESS;
4070
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004071 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01004072 if (list->min) {
4073 list->flags |= LYS_MAND_TRUE;
4074 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004075 list->max = list_p->max ? list_p->max : (uint32_t)-1;
4076
4077 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004078 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004079 }
4080
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004081 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004082 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4083 /* do not check "must" semantics in a grouping */
4084 ly_set_add(&ctx->unres, list, 0);
4085 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004086
4087 /* keys */
4088 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
4089 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
4090 return LY_EVALID;
4091 }
4092
4093 /* find all the keys (must be direct children) */
4094 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004095 if (!keystr) {
4096 /* keyless list */
4097 list->flags |= LYS_KEYLESS;
4098 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004099 while (keystr) {
4100 delim = strpbrk(keystr, " \t\n");
4101 if (delim) {
4102 len = delim - keystr;
4103 while (isspace(*delim)) {
4104 ++delim;
4105 }
4106 } else {
4107 len = strlen(keystr);
4108 }
4109
4110 /* key node must be present */
Michal Vaskoe444f752020-02-10 12:20:06 +01004111 key = (struct lysc_node_leaf*)lys_find_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
Radek Krejci0fe9b512019-07-26 17:51:05 +02004112 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004113 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4114 "The list's key \"%.*s\" not found.", len, keystr);
4115 return LY_EVALID;
4116 }
4117 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004118 if (key->flags & LYS_KEY) {
4119 /* the node was already marked as a key */
4120 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4121 "Duplicated key identifier \"%.*s\".", len, keystr);
4122 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004123 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004124
4125 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004126 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004127 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
4129 return LY_EVALID;
4130 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004131 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004132 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004133 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004135 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004136 return LY_EVALID;
4137 }
4138 } else {
4139 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004140 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004141 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004142 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004143 return LY_EVALID;
4144 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004145 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004147 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004148 return LY_EVALID;
4149 }
4150 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004151
4152 /* check status */
4153 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02004154 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01004155
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004156 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004157 if (key->dflt) {
4158 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
4159 lysc_type_free(ctx->ctx, key->dflt->realtype);
4160 free(key->dflt);
4161 key->dflt = NULL;
4162 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004163 }
4164 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004165 key->flags |= LYS_KEY;
4166
4167 /* move it to the correct position */
4168 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
4169 /* fix links in closest previous siblings of the key */
4170 if (key->next) {
4171 key->next->prev = key->prev;
4172 } else {
4173 /* last child */
4174 list->child->prev = key->prev;
4175 }
4176 if (key->prev->next) {
4177 key->prev->next = key->next;
4178 }
4179 /* fix links in the key */
4180 if (prev_key) {
4181 key->prev = (struct lysc_node*)prev_key;
4182 key->next = prev_key->next;
4183 } else {
4184 key->prev = list->child->prev;
4185 key->next = list->child;
4186 }
4187 /* fix links in closes future siblings of the key */
4188 if (prev_key) {
4189 if (prev_key->next) {
4190 prev_key->next->prev = (struct lysc_node*)key;
4191 } else {
4192 list->child->prev = (struct lysc_node*)key;
4193 }
4194 prev_key->next = (struct lysc_node*)key;
4195 } else {
4196 list->child->prev = (struct lysc_node*)key;
4197 }
4198 /* fix links in parent */
4199 if (!key->prev->next) {
4200 list->child = (struct lysc_node*)key;
4201 }
4202 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004203
4204 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004205 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004206 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004207 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004208 }
4209
4210 /* uniques */
4211 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004212 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004213 }
4214
Radek Krejciec4da802019-05-02 13:02:41 +02004215 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4216 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004217
4218done:
4219 return ret;
4220}
4221
Radek Krejcib56c7502019-02-13 14:19:54 +01004222/**
4223 * @brief Do some checks and set the default choice's case.
4224 *
4225 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4226 *
4227 * @param[in] ctx Compile context.
4228 * @param[in] dflt Name of the default branch. Can contain even the prefix, but it make sense only in case it is the prefix of the module itself,
4229 * not the reference to the imported module.
4230 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4231 * @return LY_ERR value.
4232 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004233static LY_ERR
4234lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4235{
4236 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4237 const char *prefix = NULL, *name;
4238 size_t prefix_len = 0;
4239
4240 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4241 name = strchr(dflt, ':');
4242 if (name) {
4243 prefix = dflt;
4244 prefix_len = name - prefix;
4245 ++name;
4246 } else {
4247 name = dflt;
4248 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02004249 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004250 /* prefixed default case make sense only for the prefix of the schema itself */
4251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4252 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4253 prefix_len, prefix);
4254 return LY_EVALID;
4255 }
Michal Vaskoe444f752020-02-10 12:20:06 +01004256 ch->dflt = (struct lysc_node_case*)lys_find_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
Radek Krejci76b3e962018-12-14 17:01:25 +01004257 if (!ch->dflt) {
4258 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4259 "Default case \"%s\" not found.", dflt);
4260 return LY_EVALID;
4261 }
4262 /* no mandatory nodes directly under the default case */
4263 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004264 if (iter->parent != (struct lysc_node*)ch->dflt) {
4265 break;
4266 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004267 if (iter->flags & LYS_MAND_TRUE) {
4268 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4269 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4270 return LY_EVALID;
4271 }
4272 }
Radek Krejci01342af2019-01-03 15:18:08 +01004273 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004274 return LY_SUCCESS;
4275}
4276
Radek Krejciccd20f12019-02-15 14:12:27 +01004277static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004278lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004279{
4280 struct lys_module *mod;
4281 const char *prefix = NULL, *name;
4282 size_t prefix_len = 0;
4283 struct lysc_node_case *cs;
4284 struct lysc_node *node;
4285
4286 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4287 name = strchr(dflt, ':');
4288 if (name) {
4289 prefix = dflt;
4290 prefix_len = name - prefix;
4291 ++name;
4292 } else {
4293 name = dflt;
4294 }
4295 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4296 if (prefix) {
4297 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4298 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004299 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4300 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004301 return LY_EVALID;
4302 }
4303 } else {
4304 mod = ctx->mod;
4305 }
4306 /* get the default case */
Michal Vaskoe444f752020-02-10 12:20:06 +01004307 cs = (struct lysc_node_case*)lys_find_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
Radek Krejciccd20f12019-02-15 14:12:27 +01004308 if (!cs) {
4309 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004310 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004311 return LY_EVALID;
4312 }
4313
4314 /* check that there is no mandatory node */
4315 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004316 if (node->parent != (struct lysc_node*)cs) {
4317 break;
4318 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004319 if (node->flags & LYS_MAND_TRUE) {
4320 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004321 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4322 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004323 return LY_EVALID;
4324 }
4325 }
4326
4327 /* set the default case in choice */
4328 ch->dflt = cs;
4329 cs->flags |= LYS_SET_DFLT;
4330
4331 return LY_SUCCESS;
4332}
4333
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004334/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004335 * @brief Compile parsed choice node information.
4336 * @param[in] ctx Compile context
4337 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004338 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004339 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004340 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4341 */
4342static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004343lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004344{
4345 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4346 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4347 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004348 LY_ERR ret = LY_SUCCESS;
4349
Radek Krejci056d0a82018-12-06 16:57:25 +01004350 LY_LIST_FOR(ch_p->child, child_p) {
4351 if (child_p->nodetype == LYS_CASE) {
4352 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004353 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004354 }
4355 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004356 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004357 }
4358 }
4359
4360 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004361 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004362 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004363 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004364
Radek Krejci9800fb82018-12-13 14:26:23 +01004365 return ret;
4366}
4367
4368/**
4369 * @brief Compile parsed anydata or anyxml node information.
4370 * @param[in] ctx Compile context
4371 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004372 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4373 * is enriched with the any-specific information.
4374 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4375 */
4376static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004377lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004378{
4379 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4380 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4381 unsigned int u;
4382 LY_ERR ret = LY_SUCCESS;
4383
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004384 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004385 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4386 /* do not check "must" semantics in a grouping */
4387 ly_set_add(&ctx->unres, any, 0);
4388 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004389
4390 if (any->flags & LYS_CONFIG_W) {
4391 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004392 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004393 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004394done:
4395 return ret;
4396}
4397
Radek Krejcib56c7502019-02-13 14:19:54 +01004398/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004399 * @brief Connect the node into the siblings list and check its name uniqueness.
4400 *
4401 * @param[in] ctx Compile context
4402 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4403 * the choice itself is expected instead of a specific case node.
4404 * @param[in] node Schema node to connect into the list.
4405 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4406 */
4407static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004408lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004409{
4410 struct lysc_node **children;
4411
4412 if (node->nodetype == LYS_CASE) {
4413 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4414 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004415 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004416 }
4417 if (children) {
4418 if (!(*children)) {
4419 /* first child */
4420 *children = node;
4421 } else if (*children != node) {
4422 /* by the condition in previous branch we cover the choice/case children
4423 * - the children list is shared by the choice and the the first case, in addition
4424 * the first child of each case must be referenced from the case node. So the node is
4425 * actually always already inserted in case it is the first children - so here such
4426 * a situation actually corresponds to the first branch */
4427 /* insert at the end of the parent's children list */
4428 (*children)->prev->next = node;
4429 node->prev = (*children)->prev;
4430 (*children)->prev = node;
4431
4432 /* check the name uniqueness */
4433 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4434 lysc_node_notifs(parent), node->name, node)) {
4435 return LY_EEXIST;
4436 }
4437 }
4438 }
4439 return LY_SUCCESS;
4440}
4441
Radek Krejci95710c92019-02-11 15:49:55 +01004442/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004443 * @brief Prepare the case structure in choice node for the new data node.
4444 *
4445 * 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
4446 * created in the choice when the first child was processed.
4447 *
4448 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004449 * @param[in] node_p Node image from the parsed tree. If the case is explicit, it is the LYS_CASE node, but in case of implicit case,
4450 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004451 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4452 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4453 * @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,
4454 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004455 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004456static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004457lys_compile_node_case(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node_choice *ch, struct lysc_node *child)
Radek Krejci056d0a82018-12-06 16:57:25 +01004458{
4459 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004460 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004461 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004462 unsigned int u;
4463 LY_ERR ret;
4464
Radek Krejci95710c92019-02-11 15:49:55 +01004465#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004466 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004467 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4469 return NULL; \
4470 } \
4471 }
4472
Radek Krejci95710c92019-02-11 15:49:55 +01004473 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4474 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004475
4476 /* we have to add an implicit case node into the parent choice */
4477 cs = calloc(1, sizeof(struct lysc_node_case));
4478 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004479 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004480 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004481 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004482 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4483 /* the case is already present since the child is not its first children */
4484 return (struct lysc_node_case*)ch->cases->prev;
4485 }
Radek Krejci95710c92019-02-11 15:49:55 +01004486 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004487
4488 /* explicit parent case is not present (this is its first child) */
4489 cs = calloc(1, sizeof(struct lysc_node_case));
4490 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004491 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004492 cs->flags = LYS_STATUS_MASK & node_p->flags;
4493 cs->sp = node_p;
4494
Radek Krejcib1b59152019-01-07 13:21:56 +01004495 /* check the case's status (don't need to solve uses_status since case statement cannot be directly in grouping statement */
Radek Krejcibae745f2019-04-09 16:28:00 +02004496 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004497
4498 if (node_p->when) {
4499 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004500 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004501 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004502
4503 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4504 /* do not check "when" semantics in a grouping */
4505 ly_set_add(&ctx->unres, cs, 0);
4506 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004507 }
Radek Krejciec4da802019-05-02 13:02:41 +02004508 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004509 } else {
4510 LOGINT(ctx->ctx);
4511 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004512 }
4513 cs->module = ctx->mod;
4514 cs->prev = (struct lysc_node*)cs;
4515 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004516 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004517 cs->child = child;
4518
4519 return cs;
4520error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004521 if (cs) {
4522 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4523 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004524 return NULL;
4525
4526#undef UNIQUE_CHECK
4527}
4528
Radek Krejcib56c7502019-02-13 14:19:54 +01004529/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004530 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004531 *
4532 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004533 * @param[in] node Target node where the config is supposed to be changed.
4534 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004535 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4536 * done only on the node for which the refine was created. The function applies also recursively to apply the config change
Radek Krejci93dcc392019-02-19 10:43:38 +01004537 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4538 * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging).
Radek Krejcib56c7502019-02-13 14:19:54 +01004539 * @return LY_ERR value.
4540 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004541static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004542lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004543 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004544{
4545 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004546 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004547
4548 if (config == (node->flags & LYS_CONFIG_MASK)) {
4549 /* nothing to do */
4550 return LY_SUCCESS;
4551 }
4552
4553 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004554 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004555 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4556 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004557 "Invalid %s of config - configuration node cannot be child of any state data node.",
4558 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004559 return LY_EVALID;
4560 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004561 node->flags |= LYS_SET_CONFIG;
4562 } else {
4563 if (node->flags & LYS_SET_CONFIG) {
4564 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4565 /* setting config flags, but have node with explicit config true */
4566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004567 "Invalid %s of config - configuration node cannot be child of any state data node.",
4568 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004569 return LY_EVALID;
4570 }
4571 /* do not change config on nodes where the config is explicitely set, this does not apply to
4572 * nodes, which are being changed explicitly (targets of refine or deviation) */
4573 return LY_SUCCESS;
4574 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004575 }
4576 node->flags &= ~LYS_CONFIG_MASK;
4577 node->flags |= config;
4578
4579 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004580 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004581 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004582 }
4583
Radek Krejci76b3e962018-12-14 17:01:25 +01004584 return LY_SUCCESS;
4585}
4586
Radek Krejcib56c7502019-02-13 14:19:54 +01004587/**
4588 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4589 *
4590 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4591 * the flag to such parents from a mandatory children.
4592 *
4593 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4594 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4595 * (mandatory children was removed).
4596 */
Radek Krejcife909632019-02-12 15:34:42 +01004597void
4598lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4599{
4600 struct lysc_node *iter;
4601
4602 if (add) { /* set flag */
4603 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4604 parent = parent->parent) {
4605 parent->flags |= LYS_MAND_TRUE;
4606 }
4607 } else { /* unset flag */
4608 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004609 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004610 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004611 /* there is another mandatory node */
4612 return;
4613 }
4614 }
4615 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4616 parent->flags &= ~LYS_MAND_TRUE;
4617 }
4618 }
4619}
4620
Radek Krejci056d0a82018-12-06 16:57:25 +01004621/**
Radek Krejci3641f562019-02-13 15:38:40 +01004622 * @brief Internal sorting process for the lys_compile_augment_sort().
4623 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4624 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4625 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4626 */
4627static void
4628lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4629{
4630 unsigned int v;
4631 size_t len;
4632
4633 len = strlen(aug_p->nodeid);
4634 LY_ARRAY_FOR(result, v) {
4635 if (strlen(result[v]->nodeid) <= len) {
4636 continue;
4637 }
4638 if (v < LY_ARRAY_SIZE(result)) {
4639 /* move the rest of array */
4640 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4641 break;
4642 }
4643 }
4644 result[v] = aug_p;
4645 LY_ARRAY_INCREMENT(result);
4646}
4647
4648/**
4649 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4650 *
4651 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4652 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4653 *
4654 * @param[in] ctx Compile context.
4655 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4656 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4657 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4658 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4659 * @return LY_ERR value.
4660 */
4661LY_ERR
4662lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4663{
4664 struct lysp_augment **result = NULL;
4665 unsigned int u, v;
4666 size_t count = 0;
4667
4668 assert(augments);
4669
4670 /* get count of the augments in module and all its submodules */
4671 if (aug_p) {
4672 count += LY_ARRAY_SIZE(aug_p);
4673 }
4674 LY_ARRAY_FOR(inc_p, u) {
4675 if (inc_p[u].submodule->augments) {
4676 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4677 }
4678 }
4679
4680 if (!count) {
4681 *augments = NULL;
4682 return LY_SUCCESS;
4683 }
4684 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4685
4686 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4687 * together, so there can be even /z/y betwwen them. */
4688 LY_ARRAY_FOR(aug_p, u) {
4689 lys_compile_augment_sort_(&aug_p[u], result);
4690 }
4691 LY_ARRAY_FOR(inc_p, u) {
4692 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4693 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4694 }
4695 }
4696
4697 *augments = result;
4698 return LY_SUCCESS;
4699}
4700
4701/**
4702 * @brief Compile the parsed augment connecting it into its target.
4703 *
4704 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4705 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4706 * are already implemented and compiled.
4707 *
4708 * @param[in] ctx Compile context.
4709 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004710 * @param[in] parent Parent node to provide the augment's context. It is NULL for the top level augments and a node holding uses's
4711 * children in case of the augmenting uses data.
4712 * @return LY_SUCCESS on success.
4713 * @return LY_EVALID on failure.
4714 */
4715LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004716lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004717{
4718 LY_ERR ret = LY_SUCCESS;
4719 struct lysp_node *node_p, *case_node_p;
4720 struct lysc_node *target; /* target target of the augment */
4721 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004722 struct lysc_when **when, *when_shared;
4723 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004724 uint16_t flags = 0;
4725 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004726 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004727
Radek Krejci327de162019-06-14 12:52:07 +02004728 lysc_update_path(ctx, NULL, "{augment}");
4729 lysc_update_path(ctx, NULL, aug_p->nodeid);
4730
Radek Krejci7af64242019-02-18 13:07:53 +01004731 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004732 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004733 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004734 if (ret != LY_SUCCESS) {
4735 if (ret == LY_EDENIED) {
4736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4737 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4738 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4739 }
4740 return LY_EVALID;
4741 }
4742
4743 /* check for mandatory nodes
4744 * - new cases augmenting some choice can have mandatory nodes
4745 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4746 */
Radek Krejci733988a2019-02-15 15:12:44 +01004747 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004748 allow_mandatory = 1;
4749 }
4750
4751 when_shared = NULL;
4752 LY_LIST_FOR(aug_p->child, node_p) {
4753 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4754 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4755 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004756 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4757 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004759 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4760 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004761 return LY_EVALID;
4762 }
4763
4764 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004765 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004766 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004767 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004768 } else {
4769 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004770 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004771 }
4772 }
Radek Krejciec4da802019-05-02 13:02:41 +02004773 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004774
Radek Krejcife13da42019-02-15 14:51:01 +01004775 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4776 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004777 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004778 /* the compiled node is the last child of the target (but it is a case, so we have to be careful and stop) */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004779 for (node = (struct lysc_node*)lysc_node_children(target, flags); node->next && node->next->parent == node->parent; node = node->next);
Radek Krejci3641f562019-02-13 15:38:40 +01004780 } else if (target->nodetype == LYS_CHOICE) {
4781 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4782 node = ((struct lysc_node_choice*)target)->cases->prev;
4783 } else {
4784 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004785 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004786 }
4787
Radek Krejci733988a2019-02-15 15:12:44 +01004788 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004789 node->flags &= ~LYS_MAND_TRUE;
4790 lys_compile_mandatory_parents(target, 0);
4791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004792 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004793 return LY_EVALID;
4794 }
4795
4796 /* pass augment's when to all the children */
4797 if (aug_p->when) {
4798 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4799 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004800 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004801 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004802
4803 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4804 /* do not check "when" semantics in a grouping */
Michal Vasko5c4e5892019-11-14 12:31:38 +01004805 ly_set_add(&ctx->unres, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004806 }
4807
Radek Krejci3641f562019-02-13 15:38:40 +01004808 when_shared = *when;
4809 } else {
4810 ++when_shared->refcount;
4811 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004812
4813 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4814 /* in this case check "when" again for all children because of dummy node check */
4815 ly_set_add(&ctx->unres, node, 0);
4816 }
Radek Krejci3641f562019-02-13 15:38:40 +01004817 }
4818 }
4819 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004820
Radek Krejciec4da802019-05-02 13:02:41 +02004821 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004822 switch (target->nodetype) {
4823 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004824 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004825 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004826 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004827 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004828 break;
4829 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004830 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004831 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004832 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004833 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004834 break;
4835 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004836 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004837 if (aug_p->actions) {
4838 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004839 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4840 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004841 return LY_EVALID;
4842 }
4843 if (aug_p->notifs) {
4844 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004845 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004846 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004847 return LY_EVALID;
4848 }
4849 }
Radek Krejci3641f562019-02-13 15:38:40 +01004850
Radek Krejci327de162019-06-14 12:52:07 +02004851 lysc_update_path(ctx, NULL, NULL);
4852 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004853error:
Radek Krejciec4da802019-05-02 13:02:41 +02004854 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004855 return ret;
4856}
4857
4858/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004859 * @brief Apply refined or deviated mandatory flag to the target node.
4860 *
4861 * @param[in] ctx Compile context.
4862 * @param[in] node Target node where the mandatory property is supposed to be changed.
4863 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004864 * @param[in] refine_flag Flag to distinguish if the change is caused by refine (flag set) or deviation (for logging).
Radek Krejci551b12c2019-02-19 16:11:21 +01004865 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4866 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4867 * the tests are skipped here, but they are supposed to be performed after all the deviations are applied.
Radek Krejcif1421c22019-02-19 13:05:20 +01004868 * @return LY_ERR value.
4869 */
4870static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004871lys_compile_change_mandatory(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t mandatory_flag, int refine_flag)
Radek Krejcif1421c22019-02-19 13:05:20 +01004872{
4873 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4874 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004875 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4876 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004877 return LY_EVALID;
4878 }
4879
4880 if (mandatory_flag & LYS_MAND_TRUE) {
4881 /* check if node has default value */
4882 if (node->nodetype & LYS_LEAF) {
4883 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004884 if (refine_flag) {
4885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004886 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004887 return LY_EVALID;
4888 }
Radek Krejcia1911222019-07-22 17:24:50 +02004889 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004890 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004891 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004892
4893 /* update the list of incomplete default values if needed */
4894 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4895
Radek Krejcia1911222019-07-22 17:24:50 +02004896 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4897 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4898 free(leaf->dflt);
4899 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004900 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004901 }
4902 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004903 if (refine_flag) {
4904 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004905 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004906 return LY_EVALID;
4907 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004908 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004909 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid refine of mandatory under the default case.");
Radek Krejcif1421c22019-02-19 13:05:20 +01004911 return LY_EVALID;
4912 }
4913
4914 node->flags &= ~LYS_MAND_FALSE;
4915 node->flags |= LYS_MAND_TRUE;
4916 lys_compile_mandatory_parents(node->parent, 1);
4917 } else {
4918 /* make mandatory false */
4919 node->flags &= ~LYS_MAND_TRUE;
4920 node->flags |= LYS_MAND_FALSE;
4921 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004922 if ((node->nodetype & LYS_LEAF) && !((struct lysc_node_leaf*)node)->dflt && ((struct lysc_node_leaf*)node)->type->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004923 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004924 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004925 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004926 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4927 leaf->dflt->realtype = leaf->type->dflt->realtype;
4928 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4929 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004930 }
4931 }
4932 return LY_SUCCESS;
4933}
4934
4935/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004936 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4937 * If present, also apply uses's modificators.
4938 *
4939 * @param[in] ctx Compile context
4940 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004941 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4942 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4943 * the compile context.
4944 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4945 */
4946static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004947lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004948{
4949 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004950 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004951 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004952 struct lysc_node_container context_node_fake =
4953 {.nodetype = LYS_CONTAINER,
4954 .module = ctx->mod,
4955 .flags = parent ? parent->flags : 0,
4956 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004957 .prev = (struct lysc_node*)&context_node_fake,
4958 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004959 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004960 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004961 int found;
4962 const char *id, *name, *prefix;
4963 size_t prefix_len, name_len;
4964 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004965 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004966 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004967 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004968 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004969 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004970 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004971 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004972 unsigned int actions_index, notifs_index;
4973 struct lysc_notif **notifs = NULL;
4974 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004975
4976 /* search for the grouping definition */
4977 found = 0;
4978 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004979 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004980 if (prefix) {
4981 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4982 if (!mod) {
4983 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004984 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004985 return LY_EVALID;
4986 }
4987 } else {
4988 mod = ctx->mod_def;
4989 }
4990 if (mod == ctx->mod_def) {
4991 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004992 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004993 LY_ARRAY_FOR(grp, u) {
4994 if (!strcmp(grp[u].name, name)) {
4995 grp = &grp[u];
4996 found = 1;
4997 break;
4998 }
4999 }
5000 }
5001 }
5002 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01005003 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01005004 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01005005 if (grp) {
5006 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
5007 if (!strcmp(grp[u].name, name)) {
5008 grp = &grp[u];
5009 found = 1;
5010 }
5011 }
5012 }
5013 if (!found && mod->parsed->includes) {
5014 /* ... and all the submodules */
5015 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
5016 grp = mod->parsed->includes[u].submodule->groupings;
5017 if (grp) {
5018 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
5019 if (!strcmp(grp[v].name, name)) {
5020 grp = &grp[v];
5021 found = 1;
5022 }
5023 }
5024 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005025 }
5026 }
5027 }
5028 if (!found) {
5029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5030 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
5031 return LY_EVALID;
5032 }
5033
5034 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
5035 grp_stack_count = ctx->groupings.count;
5036 ly_set_add(&ctx->groupings, (void*)grp, 0);
5037 if (grp_stack_count == ctx->groupings.count) {
5038 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
5039 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5040 "Grouping \"%s\" references itself through a uses statement.", grp->name);
5041 return LY_EVALID;
5042 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005043 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5044 /* remember that the grouping is instantiated to avoid its standalone validation */
5045 grp->flags |= LYS_USED_GRP;
5046 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005047
5048 /* switch context's mod_def */
5049 mod_old = ctx->mod_def;
5050 ctx->mod_def = mod;
5051
5052 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005053 LY_CHECK_GOTO(lysc_check_status(ctx, uses_p->flags, mod_old, uses_p->name, grp->flags, mod, grp->name), cleanup);
Radek Krejcie86bf772018-12-14 11:39:53 +01005054
Radek Krejcifc11bd72019-04-11 16:00:05 +02005055 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01005056 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005057 /* 0x3 in uses_status is a special bits combination to be able to detect status flags from uses */
Radek Krejciec4da802019-05-02 13:02:41 +02005058 LY_CHECK_GOTO(lys_compile_node(ctx, node_p, parent, (uses_p->flags & LYS_STATUS_MASK) | 0x3), cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01005059
5060 /* some preparation for applying refines */
5061 if (grp->data == node_p) {
5062 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02005063 if (parent) {
5064 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5065 } else if (ctx->mod->compiled->data) {
5066 child = ctx->mod->compiled->data;
5067 } else {
5068 child = NULL;
5069 }
5070 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01005071 }
5072 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005073 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01005074 LY_LIST_FOR(context_node_fake.child, child) {
5075 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01005076
Radek Krejcifc11bd72019-04-11 16:00:05 +02005077 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01005078 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005079 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01005080 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01005081 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
5082
5083 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5084 /* do not check "when" semantics in a grouping */
5085 ly_set_add(&ctx->unres, child, 0);
5086 }
5087
Radek Krejci00b874b2019-02-12 10:54:50 +01005088 when_shared = *when;
5089 } else {
5090 ++when_shared->refcount;
5091 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01005092
5093 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5094 /* in this case check "when" again for all children because of dummy node check */
5095 ly_set_add(&ctx->unres, child, 0);
5096 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005097 }
5098 }
Radek Krejci01342af2019-01-03 15:18:08 +01005099 }
5100 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005101 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01005102 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005103 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02005104 context_node_fake.child->prev = parent ? lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK)->prev : ctx->mod->compiled->data->prev;
Radek Krejci76b3e962018-12-14 17:01:25 +01005105 }
5106
Radek Krejcifc11bd72019-04-11 16:00:05 +02005107 /* compile actions */
5108 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5109 if (actions) {
5110 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005111 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005112 if (*actions && (uses_p->augments || uses_p->refines)) {
5113 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5114 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
5115 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
5116 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5117 }
5118 }
5119
5120 /* compile notifications */
5121 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5122 if (notifs) {
5123 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005124 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005125 if (*notifs && (uses_p->augments || uses_p->refines)) {
5126 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5127 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
5128 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
5129 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5130 }
5131 }
5132
5133
Radek Krejci3641f562019-02-13 15:38:40 +01005134 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005135 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005136 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005137 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005138 }
Radek Krejci12fb9142019-01-08 09:45:30 +01005139
Radek Krejcif0089082019-01-07 16:42:01 +01005140 /* reload previous context's mod_def */
5141 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02005142 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01005143
Radek Krejci76b3e962018-12-14 17:01:25 +01005144 /* apply refine */
5145 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02005146 lysc_update_path(ctx, NULL, rfn->nodeid);
5147
Radek Krejci7af64242019-02-18 13:07:53 +01005148 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, rfn->nodeid, 0, (struct lysc_node*)&context_node_fake, ctx->mod,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005149 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02005150 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01005151 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01005152
5153 /* default value */
5154 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01005155 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01005156 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005157 "Invalid refine of default - %s cannot hold %d default values.",
5158 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005159 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005160 }
5161 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
5162 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005163 "Invalid refine of default - %s cannot hold default value(s).",
5164 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005165 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005166 }
5167 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02005168 struct ly_err_item *err = NULL;
5169 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
5170 if (leaf->dflt) {
5171 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02005172 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005173 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5174 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5175 } else {
5176 /* prepare a new one */
5177 leaf->dflt = calloc(1, sizeof *leaf->dflt);
5178 leaf->dflt->realtype = leaf->type;
5179 }
5180 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005181 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005182 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
5183 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005184 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005185 leaf->dflt->realtype->refcount++;
5186 if (err) {
5187 ly_err_print(err);
5188 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5189 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
5190 ly_err_free(err);
5191 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005192 if (rc == LY_EINCOMPLETE) {
5193 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005194 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005195
5196 /* but in general result is so far ok */
5197 rc = LY_SUCCESS;
5198 }
Radek Krejcia1911222019-07-22 17:24:50 +02005199 LY_CHECK_GOTO(rc, cleanup);
5200 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005201 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02005202 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
5203
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005204 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01005205 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5206 "Invalid refine of default in leaf-list - the default statement is allowed only in YANG 1.1 modules.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005207 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01005208 }
Radek Krejcia1911222019-07-22 17:24:50 +02005209
5210 /* remove previous set of default values */
5211 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005212 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005213 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5214 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5215 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005216 }
Radek Krejcia1911222019-07-22 17:24:50 +02005217 LY_ARRAY_FREE(llist->dflts);
5218 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005219 LY_ARRAY_FREE(llist->dflts_mods);
5220 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005221
5222 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005223 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005224 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005225 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005226 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005227 LY_ARRAY_INCREMENT(llist->dflts_mods);
5228 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005229 LY_ARRAY_INCREMENT(llist->dflts);
5230 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5231 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005232 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005233 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005234 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005235 llist->dflts[u]->realtype->refcount++;
5236 if (err) {
5237 ly_err_print(err);
5238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5239 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5240 ly_err_free(err);
5241 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005242 if (rc == LY_EINCOMPLETE) {
5243 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005244 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005245
5246 /* but in general result is so far ok */
5247 rc = LY_SUCCESS;
5248 }
5249 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005250 }
Radek Krejcia1911222019-07-22 17:24:50 +02005251 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005252 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005253 if (((struct lysc_node_choice*)node)->dflt) {
5254 /* unset LYS_SET_DFLT from the current default case */
5255 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5256 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005257 LY_CHECK_GOTO(lys_compile_node_choice_dflt(ctx, rfn->dflts[0], (struct lysc_node_choice*)node), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005258 }
5259 }
5260
Radek Krejci12fb9142019-01-08 09:45:30 +01005261 /* description */
5262 if (rfn->dsc) {
5263 FREE_STRING(ctx->ctx, node->dsc);
5264 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5265 }
5266
5267 /* reference */
5268 if (rfn->ref) {
5269 FREE_STRING(ctx->ctx, node->ref);
5270 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5271 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005272
5273 /* config */
5274 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005275 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005276 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005277 } else {
5278 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01005279 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005280 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005281 }
5282
5283 /* mandatory */
5284 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005285 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005286 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005287
5288 /* presence */
5289 if (rfn->presence) {
5290 if (node->nodetype != LYS_CONTAINER) {
5291 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005292 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5293 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005294 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005295 }
5296 node->flags |= LYS_PRESENCE;
5297 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005298
5299 /* must */
5300 if (rfn->musts) {
5301 switch (node->nodetype) {
5302 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005303 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005304 break;
5305 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005306 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005307 break;
5308 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005309 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005310 break;
5311 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005312 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005313 break;
5314 case LYS_ANYXML:
5315 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005316 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005317 break;
5318 default:
5319 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005320 "Invalid refine of must statement - %s cannot hold any must statement.",
5321 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005322 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005323 }
Michal Vasko5d8756a2019-11-07 15:21:00 +01005324 ly_set_add(&ctx->unres, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01005325 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005326
5327 /* min/max-elements */
5328 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5329 switch (node->nodetype) {
5330 case LYS_LEAFLIST:
5331 if (rfn->flags & LYS_SET_MAX) {
5332 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5333 }
5334 if (rfn->flags & LYS_SET_MIN) {
5335 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005336 if (rfn->min) {
5337 node->flags |= LYS_MAND_TRUE;
5338 lys_compile_mandatory_parents(node->parent, 1);
5339 } else {
5340 node->flags &= ~LYS_MAND_TRUE;
5341 lys_compile_mandatory_parents(node->parent, 0);
5342 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005343 }
5344 break;
5345 case LYS_LIST:
5346 if (rfn->flags & LYS_SET_MAX) {
5347 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5348 }
5349 if (rfn->flags & LYS_SET_MIN) {
5350 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005351 if (rfn->min) {
5352 node->flags |= LYS_MAND_TRUE;
5353 lys_compile_mandatory_parents(node->parent, 1);
5354 } else {
5355 node->flags &= ~LYS_MAND_TRUE;
5356 lys_compile_mandatory_parents(node->parent, 0);
5357 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005358 }
5359 break;
5360 default:
5361 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005362 "Invalid refine of %s statement - %s cannot hold this statement.",
5363 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005364 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005365 }
5366 }
Radek Krejcif0089082019-01-07 16:42:01 +01005367
5368 /* if-feature */
5369 if (rfn->iffeatures) {
5370 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005371 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005372 }
Radek Krejci327de162019-06-14 12:52:07 +02005373
5374 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005375 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005376
Radek Krejcif2271f12019-01-07 16:42:23 +01005377 /* do some additional checks of the changed nodes when all the refines are applied */
5378 for (u = 0; u < refined.count; ++u) {
5379 node = (struct lysc_node*)refined.objs[u];
5380 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005381 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005382
5383 /* check possible conflict with default value (default added, mandatory left true) */
5384 if ((node->flags & LYS_MAND_TRUE) &&
5385 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5386 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5387 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005388 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005389 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005390 }
5391
5392 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5393 if (node->nodetype == LYS_LIST) {
5394 min = ((struct lysc_node_list*)node)->min;
5395 max = ((struct lysc_node_list*)node)->max;
5396 } else {
5397 min = ((struct lysc_node_leaflist*)node)->min;
5398 max = ((struct lysc_node_leaflist*)node)->max;
5399 }
5400 if (min > max) {
5401 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005402 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5403 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005404 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005405 }
5406 }
5407 }
5408
Radek Krejci327de162019-06-14 12:52:07 +02005409 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005410 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005411
5412cleanup:
5413 /* fix connection of the children nodes from fake context node back into the parent */
5414 if (context_node_fake.child) {
5415 context_node_fake.child->prev = child;
5416 }
5417 LY_LIST_FOR(context_node_fake.child, child) {
5418 child->parent = parent;
5419 }
5420
5421 if (uses_p->augments || uses_p->refines) {
5422 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005423 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005424 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5425 LY_ARRAY_FREE(context_node_fake.actions);
5426 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005427 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005428 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5429 LY_ARRAY_FREE(context_node_fake.notifs);
5430 }
5431 }
5432
Radek Krejcie86bf772018-12-14 11:39:53 +01005433 /* reload previous context's mod_def */
5434 ctx->mod_def = mod_old;
5435 /* remove the grouping from the stack for circular groupings dependency check */
5436 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5437 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005438 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005439 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005440
5441 return ret;
5442}
5443
Radek Krejci327de162019-06-14 12:52:07 +02005444static int
5445lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5446{
5447 struct lysp_node *iter;
5448 int len = 0;
5449
5450 *path = NULL;
5451 for (iter = node; iter && len >= 0; iter = iter->parent) {
5452 char *s = *path;
5453 char *id;
5454
5455 switch (iter->nodetype) {
5456 case LYS_USES:
5457 asprintf(&id, "{uses='%s'}", iter->name);
5458 break;
5459 case LYS_GROUPING:
5460 asprintf(&id, "{grouping='%s'}", iter->name);
5461 break;
5462 case LYS_AUGMENT:
5463 asprintf(&id, "{augment='%s'}", iter->name);
5464 break;
5465 default:
5466 id = strdup(iter->name);
5467 break;
5468 }
5469
5470 if (!iter->parent) {
5471 /* print prefix */
5472 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5473 } else {
5474 /* prefix is the same as in parent */
5475 len = asprintf(path, "/%s%s", id, s ? s : "");
5476 }
5477 free(s);
5478 free(id);
5479 }
5480
5481 if (len < 0) {
5482 free(*path);
5483 *path = NULL;
5484 } else if (len == 0) {
5485 *path = strdup("/");
5486 len = 1;
5487 }
5488 return len;
5489}
5490
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005491/**
5492 * @brief Validate groupings that were defined but not directly used in the schema itself.
5493 *
5494 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5495 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5496 */
5497static LY_ERR
5498lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5499{
5500 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005501 char *path;
5502 int len;
5503
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005504 struct lysp_node_uses fake_uses = {
5505 .parent = node_p,
5506 .nodetype = LYS_USES,
5507 .flags = 0, .next = NULL,
5508 .name = grp->name,
5509 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5510 .refines = NULL, .augments = NULL
5511 };
5512 struct lysc_node_container fake_container = {
5513 .nodetype = LYS_CONTAINER,
5514 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5515 .module = ctx->mod,
5516 .sp = NULL, .parent = NULL, .next = NULL,
5517 .prev = (struct lysc_node*)&fake_container,
5518 .name = "fake",
5519 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5520 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5521 };
5522
5523 if (grp->parent) {
5524 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5525 }
Radek Krejci327de162019-06-14 12:52:07 +02005526
5527 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5528 if (len < 0) {
5529 LOGMEM(ctx->ctx);
5530 return LY_EMEM;
5531 }
5532 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5533 ctx->path_len = (uint16_t)len;
5534 free(path);
5535
5536 lysc_update_path(ctx, NULL, "{grouping}");
5537 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005538 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005539 lysc_update_path(ctx, NULL, NULL);
5540 lysc_update_path(ctx, NULL, NULL);
5541
5542 ctx->path_len = 1;
5543 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005544
5545 /* cleanup */
5546 lysc_node_container_free(ctx->ctx, &fake_container);
5547
5548 return ret;
5549}
Radek Krejcife909632019-02-12 15:34:42 +01005550
Radek Krejcie86bf772018-12-14 11:39:53 +01005551/**
Radek Krejcia3045382018-11-22 14:30:31 +01005552 * @brief Compile parsed schema node information.
5553 * @param[in] ctx Compile context
5554 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005555 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5556 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5557 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005558 * @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).
5559 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005560 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5561 */
Radek Krejci19a96102018-11-15 13:38:09 +01005562static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005563lys_compile_node(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *parent, uint16_t uses_status)
Radek Krejci19a96102018-11-15 13:38:09 +01005564{
5565 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005566 struct lysc_node *node;
5567 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005568 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005569 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005570 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005571
Radek Krejci327de162019-06-14 12:52:07 +02005572 if (node_p->nodetype != LYS_USES) {
5573 lysc_update_path(ctx, parent, node_p->name);
5574 } else {
5575 lysc_update_path(ctx, NULL, "{uses}");
5576 lysc_update_path(ctx, NULL, node_p->name);
5577 }
5578
Radek Krejci19a96102018-11-15 13:38:09 +01005579 switch (node_p->nodetype) {
5580 case LYS_CONTAINER:
5581 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5582 node_compile_spec = lys_compile_node_container;
5583 break;
5584 case LYS_LEAF:
5585 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5586 node_compile_spec = lys_compile_node_leaf;
5587 break;
5588 case LYS_LIST:
5589 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005590 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005591 break;
5592 case LYS_LEAFLIST:
5593 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005594 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005595 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005596 case LYS_CHOICE:
5597 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005598 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005599 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005600 case LYS_ANYXML:
5601 case LYS_ANYDATA:
5602 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005603 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005604 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005605 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005606 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5607 lysc_update_path(ctx, NULL, NULL);
5608 lysc_update_path(ctx, NULL, NULL);
5609 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005610 default:
5611 LOGINT(ctx->ctx);
5612 return LY_EINT;
5613 }
5614 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5615 node->nodetype = node_p->nodetype;
5616 node->module = ctx->mod;
5617 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005618 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005619
5620 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005621 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005622 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005623 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005624 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5625 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005626 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005627 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005628 node->flags |= LYS_CONFIG_R;
5629 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005630 /* config not explicitely set, inherit it from parent */
5631 if (parent) {
5632 node->flags |= parent->flags & LYS_CONFIG_MASK;
5633 } else {
5634 /* default is config true */
5635 node->flags |= LYS_CONFIG_W;
5636 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005637 } else {
5638 /* config set explicitely */
5639 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005640 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005641 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5643 "Configuration node cannot be child of any state data node.");
5644 goto error;
5645 }
Radek Krejci19a96102018-11-15 13:38:09 +01005646
Radek Krejcia6d57732018-11-29 13:40:37 +01005647 /* *list ordering */
5648 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5649 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005650 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005651 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5652 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005653 node->flags &= ~LYS_ORDBY_MASK;
5654 node->flags |= LYS_ORDBY_SYSTEM;
5655 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5656 /* default ordering is system */
5657 node->flags |= LYS_ORDBY_SYSTEM;
5658 }
5659 }
5660
Radek Krejci19a96102018-11-15 13:38:09 +01005661 /* status - it is not inherited by specification, but it does not make sense to have
5662 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005663 if (!parent || parent->nodetype != LYS_CHOICE) {
5664 /* in case of choice/case's children, postpone the check to the moment we know if
5665 * the parent is choice (parent here) or some case (so we have to get its flags to check) */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005666 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, uses_status ? uses_status : (parent ? parent->flags : 0)), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005667 }
5668
Radek Krejciec4da802019-05-02 13:02:41 +02005669 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005670 node->sp = node_p;
5671 }
5672 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005673 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5674 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005675 if (node_p->when) {
5676 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005677 ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005678 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005679
5680 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5681 /* do not check "when" semantics in a grouping */
5682 ly_set_add(&ctx->unres, node, 0);
5683 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005684 }
Radek Krejciec4da802019-05-02 13:02:41 +02005685 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005686
5687 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005688 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005689
Radek Krejci0935f412019-08-20 16:15:18 +02005690 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5691
Radek Krejcife909632019-02-12 15:34:42 +01005692 /* inherit LYS_MAND_TRUE in parent containers */
5693 if (node->flags & LYS_MAND_TRUE) {
5694 lys_compile_mandatory_parents(parent, 1);
5695 }
5696
Radek Krejci327de162019-06-14 12:52:07 +02005697 lysc_update_path(ctx, NULL, NULL);
5698
Radek Krejci19a96102018-11-15 13:38:09 +01005699 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005700 if (parent) {
5701 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005702 if (node_p->parent->nodetype == LYS_CASE) {
5703 lysc_update_path(ctx, parent, node_p->parent->name);
5704 } else {
5705 lysc_update_path(ctx, parent, node->name);
5706 }
Radek Krejciec4da802019-05-02 13:02:41 +02005707 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005708 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005709 if (uses_status) {
5710
5711 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005712 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005713 * it directly gets the same status flags as the choice;
5714 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005715 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005716 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005717 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005718 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005719 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005720 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005721 }
Radek Krejciec4da802019-05-02 13:02:41 +02005722 LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID);
Radek Krejci327de162019-06-14 12:52:07 +02005723
5724 if (parent->nodetype == LYS_CHOICE) {
5725 lysc_update_path(ctx, NULL, NULL);
5726 }
Radek Krejci19a96102018-11-15 13:38:09 +01005727 } else {
5728 /* top-level element */
5729 if (!ctx->mod->compiled->data) {
5730 ctx->mod->compiled->data = node;
5731 } else {
5732 /* insert at the end of the module's top-level nodes list */
5733 ctx->mod->compiled->data->prev->next = node;
5734 node->prev = ctx->mod->compiled->data->prev;
5735 ctx->mod->compiled->data->prev = node;
5736 }
Radek Krejci327de162019-06-14 12:52:07 +02005737 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005738 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5739 ctx->mod->compiled->notifs, node->name, node)) {
5740 return LY_EVALID;
5741 }
Radek Krejci19a96102018-11-15 13:38:09 +01005742 }
Radek Krejci327de162019-06-14 12:52:07 +02005743 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005744
5745 return LY_SUCCESS;
5746
5747error:
5748 lysc_node_free(ctx->ctx, node);
5749 return ret;
5750}
5751
Radek Krejciccd20f12019-02-15 14:12:27 +01005752static void
5753lysc_disconnect(struct lysc_node *node)
5754{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005755 struct lysc_node *parent, *child, *prev = NULL, *next;
5756 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005757 int remove_cs = 0;
5758
5759 parent = node->parent;
5760
5761 /* parent's first child */
5762 if (!parent) {
5763 return;
5764 }
5765 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005766 cs = (struct lysc_node_case*)node;
5767 } else if (parent->nodetype == LYS_CASE) {
5768 /* disconnecting some node in a case */
5769 cs = (struct lysc_node_case*)parent;
5770 parent = cs->parent;
5771 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5772 if (child == node) {
5773 if (cs->child == child) {
5774 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5775 /* case with a single child -> remove also the case */
5776 child->parent = NULL;
5777 remove_cs = 1;
5778 } else {
5779 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005780 }
5781 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005782 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005783 }
5784 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005785 if (!remove_cs) {
5786 cs = NULL;
5787 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005788 } else if (lysc_node_children(parent, node->flags) == node) {
5789 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005790 }
5791
5792 if (cs) {
5793 if (remove_cs) {
5794 /* cs has only one child which is being also removed */
5795 lysc_disconnect((struct lysc_node*)cs);
5796 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5797 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005798 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5799 /* default case removed */
5800 ((struct lysc_node_choice*)parent)->dflt = NULL;
5801 }
5802 if (((struct lysc_node_choice*)parent)->cases == cs) {
5803 /* first case removed */
5804 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5805 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005806 if (cs->child) {
5807 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5808 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5809 prev = cs->child->prev;
5810 } /* else all the children are under a single case */
5811 LY_LIST_FOR_SAFE(cs->child, next, child) {
5812 if (child->parent != (struct lysc_node*)cs) {
5813 break;
5814 }
5815 lysc_node_free(node->module->ctx, child);
5816 }
5817 if (prev) {
5818 if (prev->next) {
5819 prev->next = child;
5820 }
5821 if (child) {
5822 child->prev = prev;
5823 } else {
5824 /* link from the first child under the cases */
5825 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5826 }
5827 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005828 }
5829 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005830 }
5831
5832 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005833 if (node->prev->next) {
5834 node->prev->next = node->next;
5835 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005836 if (node->next) {
5837 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005838 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005839 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005840 if (child) {
5841 child->prev = node->prev;
5842 }
5843 } else if (((struct lysc_node_choice*)parent)->cases) {
5844 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005845 }
5846}
5847
5848LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005849lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005850{
Radek Krejcia1911222019-07-22 17:24:50 +02005851 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005852 struct ly_set devs_p = {0};
5853 struct ly_set targets = {0};
5854 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005855 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005856 struct lysc_action *rpcs;
5857 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005858 struct lysp_deviation *dev;
5859 struct lysp_deviate *d, **dp_new;
5860 struct lysp_deviate_add *d_add;
5861 struct lysp_deviate_del *d_del;
5862 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005863 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005864 struct lysc_deviation {
5865 const char *nodeid;
5866 struct lysc_node *target; /* target node of the deviation */
5867 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005868 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005869 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5870 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005871 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005872 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005873 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005874 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005875 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005876 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005877
5878 /* get all deviations from the module and all its submodules ... */
5879 LY_ARRAY_FOR(mod_p->deviations, u) {
5880 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5881 }
5882 LY_ARRAY_FOR(mod_p->includes, v) {
5883 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5884 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5885 }
5886 }
5887 if (!devs_p.count) {
5888 /* nothing to do */
5889 return LY_SUCCESS;
5890 }
5891
Radek Krejci327de162019-06-14 12:52:07 +02005892 lysc_update_path(ctx, NULL, "{deviation}");
5893
Radek Krejciccd20f12019-02-15 14:12:27 +01005894 /* ... and group them by the target node */
5895 devs = calloc(devs_p.count, sizeof *devs);
5896 for (u = 0; u < devs_p.count; ++u) {
5897 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005898 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005899
5900 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005901 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5902 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005903 if (target->nodetype == LYS_ACTION) {
5904 /* move the target pointer to input/output to make them different from the action and
5905 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5906 * back to the RPC/action node due to a better compatibility and decision code in this function.
5907 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5908 if (flags & LYSC_OPT_RPC_INPUT) {
5909 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5910 flags |= LYSC_OPT_INTERNAL;
5911 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5912 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5913 flags |= LYSC_OPT_INTERNAL;
5914 }
5915 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005916 /* insert into the set of targets with duplicity detection */
5917 i = ly_set_add(&targets, target, 0);
5918 if (!devs[i]) {
5919 /* new record */
5920 devs[i] = calloc(1, sizeof **devs);
5921 devs[i]->target = target;
5922 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005923 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005924 }
5925 /* add deviates into the deviation's list of deviates */
5926 for (d = dev->deviates; d; d = d->next) {
5927 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5928 *dp_new = d;
5929 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5930 devs[i]->not_supported = 1;
5931 }
5932 }
Radek Krejci327de162019-06-14 12:52:07 +02005933
5934 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005935 }
5936
5937 /* MACROS for deviates checking */
5938#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5939 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE, lys_nodetype2str(devs[u]->target->nodetype), DEVTYPE, PROPERTY);\
Radek Krejciccd20f12019-02-15 14:12:27 +01005941 goto cleanup; \
5942 }
5943
5944#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5945 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005946 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5947 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005948 goto cleanup; \
5949 }
5950
Radek Krejcia1911222019-07-22 17:24:50 +02005951
Radek Krejciccd20f12019-02-15 14:12:27 +01005952#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005953 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5955 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5956 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5957 goto cleanup; \
5958 }
5959
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005960#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005961 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005962 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005963 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5964 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005965 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005966 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5967 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005968 goto cleanup; \
5969 }
5970
Radek Krejci551b12c2019-02-19 16:11:21 +01005971#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5972 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005974 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5975 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005976 goto cleanup; \
5977 }
5978
Radek Krejciccd20f12019-02-15 14:12:27 +01005979#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5980 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005981 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005982 goto cleanup; \
5983 }
5984
Radek Krejci551b12c2019-02-19 16:11:21 +01005985#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5986 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005988 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005989 goto cleanup; \
5990 }
5991
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005992#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5993 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5994 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5995 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5996 if (!strcmp(((TYPE)devs[u]->target)->ARRAY_TRG[y]VALMEMBER_CMP, d_del->ARRAY_DEV[x]VALMEMBER)) { break; } \
Radek Krejciccd20f12019-02-15 14:12:27 +01005997 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005998 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005999 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02006000 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
6001 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01006002 goto cleanup; \
6003 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006004 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
6005 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
6006 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
6007 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
6008 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01006009 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006010 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
6011 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
6012 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01006013 }
6014
6015 /* apply deviations */
6016 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02006017 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
6018 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
6019 struct ly_err_item *err = NULL;
6020
6021 dflt = NULL;
6022 changed_type = 0;
6023
Radek Krejci327de162019-06-14 12:52:07 +02006024 lysc_update_path(ctx, NULL, devs[u]->nodeid);
6025
Radek Krejcif538ce52019-03-05 10:46:14 +01006026 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
6027 /* fix the target pointer in case of RPC's/action's input/output */
6028 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6029 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
6030 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6031 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
6032 }
6033 }
6034
Radek Krejciccd20f12019-02-15 14:12:27 +01006035 /* not-supported */
6036 if (devs[u]->not_supported) {
6037 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
6038 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
6039 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
6040 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02006041
6042#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6043 if (devs[u]->target->parent) { \
6044 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
6045 } else { \
6046 ARRAY = devs[u]->target->module->compiled->ARRAY; \
6047 } \
6048 LY_ARRAY_FOR(ARRAY, x) { \
6049 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
6050 } \
6051 if (x < LY_ARRAY_SIZE(ARRAY)) { \
6052 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6053 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6054 LY_ARRAY_DECREMENT(ARRAY); \
6055 }
6056
Radek Krejcif538ce52019-03-05 10:46:14 +01006057 if (devs[u]->target->nodetype == LYS_ACTION) {
6058 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6059 /* remove RPC's/action's input */
6060 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
6061 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02006062 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
6063 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01006064 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6065 /* remove RPC's/action's output */
6066 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
6067 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02006068 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
6069 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01006070 } else {
6071 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02006072 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01006073 }
6074 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02006075 /* remove Notification */
6076 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01006077 } else {
6078 /* remove the target node */
6079 lysc_disconnect(devs[u]->target);
6080 lysc_node_free(ctx->ctx, devs[u]->target);
6081 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006082
Radek Krejci474f9b82019-07-24 11:36:37 +02006083 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6084 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01006085 continue;
6086 }
6087
6088 /* list of deviates (not-supported is not present in the list) */
6089 LY_ARRAY_FOR(devs[u]->deviates, v) {
6090 d = devs[u]->deviates[v];
6091
6092 switch (d->mod) {
6093 case LYS_DEV_ADD:
6094 d_add = (struct lysp_deviate_add*)d;
6095 /* [units-stmt] */
6096 if (d_add->units) {
6097 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
6098 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
6099
6100 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6101 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6102 }
6103
6104 /* *must-stmt */
6105 if (d_add->musts) {
6106 switch (devs[u]->target->nodetype) {
6107 case LYS_CONTAINER:
6108 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006109 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
6110 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006111 break;
6112 case LYS_LEAF:
6113 case LYS_LEAFLIST:
6114 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006115 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
6116 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006117 break;
6118 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006119 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
6120 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006121 break;
6122 case LYS_ACTION:
6123 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006124 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
6125 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006126 break;
6127 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006128 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
6129 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006130 break;
6131 }
6132 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006133 default:
6134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006135 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006136 goto cleanup;
6137 }
Michal Vasko5d8756a2019-11-07 15:21:00 +01006138 ly_set_add(&ctx->unres, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01006139 }
6140
6141 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006142 if (d_add->uniques) {
6143 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
6144 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
6145 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006146
6147 /* *default-stmt */
6148 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006149 switch (devs[u]->target->nodetype) {
6150 case LYS_LEAF:
6151 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006152 DEV_CHECK_NONPRESENCE_VALUE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_DFLT), dflt, "default", dflt, dflt_mod);
Radek Krejcia1911222019-07-22 17:24:50 +02006153 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006154 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006155 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006156 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6157 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6158 } else {
6159 /* prepare new default value storage */
6160 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01006161 }
Radek Krejcia1911222019-07-22 17:24:50 +02006162 dflt = d_add->dflts[0];
6163 /* parsing is done at the end after possible replace of the leaf's type */
6164
Radek Krejci551b12c2019-02-19 16:11:21 +01006165 /* mark the new default values as leaf's own */
6166 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006167 break;
6168 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006169 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006170 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02006171 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006172 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006173 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6174 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6175 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006176 }
Radek Krejcia1911222019-07-22 17:24:50 +02006177 LY_ARRAY_FREE(llist->dflts);
6178 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006179 LY_ARRAY_FREE(llist->dflts_mods);
6180 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006181 }
6182 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006183 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006184 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
6185 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01006186 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006187 LY_ARRAY_INCREMENT(llist->dflts_mods);
6188 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006189 LY_ARRAY_INCREMENT(llist->dflts);
6190 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
6191 llist->dflts[x]->realtype = llist->type;
6192 rc = llist->type->plugin->store(ctx->ctx, llist->type, d_add->dflts[x - y], strlen(d_add->dflts[x - y]),
Radek Krejci474f9b82019-07-24 11:36:37 +02006193 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006194 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006195 llist->dflts[x]->realtype->refcount++;
6196 if (err) {
6197 ly_err_print(err);
6198 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6199 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
6200 d_add->dflts[x - y], err->msg);
6201 ly_err_free(err);
6202 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006203 if (rc == LY_EINCOMPLETE) {
6204 /* postpone default compilation when the tree is complete */
6205 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6206
6207 /* but in general result is so far ok */
6208 rc = LY_SUCCESS;
6209 }
Radek Krejcia1911222019-07-22 17:24:50 +02006210 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006211 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006212 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01006213 devs[u]->target->flags |= LYS_SET_DFLT;
6214 break;
6215 case LYS_CHOICE:
6216 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6217 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6218 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6219 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006220 if (lys_compile_deviation_set_choice_dflt(ctx, d_add->dflts[0], (struct lysc_node_choice*)devs[u]->target)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006221 goto cleanup;
6222 }
6223 break;
6224 default:
6225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006226 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006227 goto cleanup;
6228 }
6229 }
6230
6231 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006232 if (d_add->flags & LYS_CONFIG_MASK) {
6233 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006234 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006235 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6236 goto cleanup;
6237 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006238 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006239 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01006240 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006241 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006242 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006244 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6245 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006246 goto cleanup;
6247 }
Radek Krejci327de162019-06-14 12:52:07 +02006248 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006249 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006250
6251 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006252 if (d_add->flags & LYS_MAND_MASK) {
6253 if (devs[u]->target->flags & LYS_MAND_MASK) {
6254 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006255 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6256 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006257 goto cleanup;
6258 }
Radek Krejci327de162019-06-14 12:52:07 +02006259 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006260 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006261
6262 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006263 if (d_add->flags & LYS_SET_MIN) {
6264 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6265 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6266 /* change value */
6267 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6268 } else if (devs[u]->target->nodetype == LYS_LIST) {
6269 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6270 /* change value */
6271 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6272 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006273 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006274 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6275 goto cleanup;
6276 }
6277 if (d_add->min) {
6278 devs[u]->target->flags |= LYS_MAND_TRUE;
6279 }
6280 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006281
6282 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006283 if (d_add->flags & LYS_SET_MAX) {
6284 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6285 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6286 /* change value */
6287 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6288 } else if (devs[u]->target->nodetype == LYS_LIST) {
6289 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6290 /* change value */
6291 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6292 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006293 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006294 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6295 goto cleanup;
6296 }
6297 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006298
6299 break;
6300 case LYS_DEV_DELETE:
6301 d_del = (struct lysp_deviate_del*)d;
6302
6303 /* [units-stmt] */
6304 if (d_del->units) {
6305 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006306 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6307 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6308 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6309 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6310 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6311 goto cleanup;
6312 }
6313 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6314 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006315 }
6316
6317 /* *must-stmt */
6318 if (d_del->musts) {
6319 switch (devs[u]->target->nodetype) {
6320 case LYS_CONTAINER:
6321 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006322 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006323 break;
6324 case LYS_LEAF:
6325 case LYS_LEAFLIST:
6326 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006327 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006328 break;
6329 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006330 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006331 break;
6332 case LYS_ACTION:
6333 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6334 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6335 break;
6336 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6337 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6338 break;
6339 }
6340 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006341 default:
6342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006343 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006344 goto cleanup;
6345 }
6346 }
6347
6348 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006349 if (d_del->uniques) {
6350 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6351 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6352 LY_ARRAY_FOR(d_del->uniques, x) {
6353 LY_ARRAY_FOR(list->uniques, z) {
6354 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6355 nodeid = strpbrk(name, " \t\n");
6356 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006357 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006358 break;
6359 }
6360 while (isspace(*nodeid)) {
6361 ++nodeid;
6362 }
6363 } else {
6364 if (strcmp(name, list->uniques[z][y]->name)) {
6365 break;
6366 }
6367 }
6368 }
6369 if (!name) {
6370 /* complete match - remove the unique */
6371 LY_ARRAY_DECREMENT(list->uniques);
6372 LY_ARRAY_FREE(list->uniques[z]);
6373 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6374 --z;
6375 break;
6376 }
6377 }
6378 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6379 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006380 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6381 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006382 goto cleanup;
6383 }
6384 }
6385 if (!LY_ARRAY_SIZE(list->uniques)) {
6386 LY_ARRAY_FREE(list->uniques);
6387 list->uniques = NULL;
6388 }
6389 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006390
6391 /* *default-stmt */
6392 if (d_del->dflts) {
6393 switch (devs[u]->target->nodetype) {
6394 case LYS_LEAF:
6395 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6396 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6397 dflt, "deleting", "default", d_del->dflts[0]);
6398
Radek Krejcia1911222019-07-22 17:24:50 +02006399 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006400 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006401 if (strcmp(dflt, d_del->dflts[0])) {
6402 if (i) {
6403 free((char*)dflt);
6404 }
6405 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6406 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6407 d_del->dflts[0], dflt);
6408 goto cleanup;
6409 }
6410 if (i) {
6411 free((char*)dflt);
6412 }
6413 dflt = NULL;
6414
Radek Krejci474f9b82019-07-24 11:36:37 +02006415 /* update the list of incomplete default values if needed */
6416 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6417
Radek Krejcia1911222019-07-22 17:24:50 +02006418 /* remove the default specification */
6419 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6420 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6421 free(leaf->dflt);
6422 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006423 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006424 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006425 break;
6426 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006427 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6428 LY_ARRAY_FOR(d_del->dflts, x) {
6429 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006430 dflt = llist->type->plugin->print(llist->dflts[y], LYD_XML, lys_get_prefix, llist->dflts_mods[y], &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006431 if (!strcmp(dflt, d_del->dflts[x])) {
6432 if (i) {
6433 free((char*)dflt);
6434 }
6435 dflt = NULL;
6436 break;
6437 }
6438 if (i) {
6439 free((char*)dflt);
6440 }
6441 dflt = NULL;
6442 }
6443 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6445 "which does not match any of the target's property values.", d_del->dflts[x]);
6446 goto cleanup;
6447 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006448
6449 /* update the list of incomplete default values if needed */
6450 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6451
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006452 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006453 LY_ARRAY_DECREMENT(llist->dflts);
6454 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6455 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6456 free(llist->dflts[y]);
6457 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006458 memmove(&llist->dflts_mods[y], &llist->dflts_mods[y + 1], (LY_ARRAY_SIZE(llist->dflts_mods) - y) * (sizeof *llist->dflts_mods));
Radek Krejcia1911222019-07-22 17:24:50 +02006459 }
6460 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006461 LY_ARRAY_FREE(llist->dflts_mods);
6462 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006463 LY_ARRAY_FREE(llist->dflts);
6464 llist->dflts = NULL;
6465 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006466 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006467 break;
6468 case LYS_CHOICE:
6469 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6470 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6471 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006472 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006473 if (prefix) {
6474 /* use module prefixes from the deviation module to match the module of the default case */
6475 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6476 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006477 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6478 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006479 goto cleanup;
6480 }
6481 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6482 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006483 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6484 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006485 goto cleanup;
6486 }
6487 }
6488 /* else {
6489 * strictly, the default prefix would point to the deviation module, but the value should actually
6490 * match the default string in the original module (usually unprefixed), so in this case we do not check
6491 * the module of the default case, just matching its name */
6492 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006494 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6495 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006496 goto cleanup;
6497 }
6498 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6499 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6500 break;
6501 default:
6502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006503 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006504 goto cleanup;
6505 }
6506 }
6507
6508 break;
6509 case LYS_DEV_REPLACE:
6510 d_rpl = (struct lysp_deviate_rpl*)d;
6511
6512 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006513 if (d_rpl->type) {
6514 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6515 /* type is mandatory, so checking for its presence is not necessary */
6516 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006517
6518 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6519 /* the target has default from the previous type - remove it */
6520 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006521 /* update the list of incomplete default values if needed */
6522 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6523
Radek Krejcia1911222019-07-22 17:24:50 +02006524 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6525 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6526 free(leaf->dflt);
6527 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006528 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006529 } else { /* LYS_LEAFLIST */
6530 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006531 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006532 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6533 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6534 free(llist->dflts[x]);
6535 }
6536 LY_ARRAY_FREE(llist->dflts);
6537 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006538 LY_ARRAY_FREE(llist->dflts_mods);
6539 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006540 }
6541 }
6542 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006543 /* there is no default value, do not set changed_type after type compilation
6544 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006545 changed_type = -1;
6546 }
Radek Krejciec4da802019-05-02 13:02:41 +02006547 LY_CHECK_GOTO(lys_compile_node_type(ctx, NULL, d_rpl->type, (struct lysc_node_leaf*)devs[u]->target), cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006548 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006549 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006550
6551 /* [units-stmt] */
6552 if (d_rpl->units) {
6553 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6554 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6555 units, "replacing", "units", d_rpl->units);
6556
6557 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6558 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6559 }
6560
6561 /* [default-stmt] */
6562 if (d_rpl->dflt) {
6563 switch (devs[u]->target->nodetype) {
6564 case LYS_LEAF:
6565 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6566 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006567 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006568 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006569 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6570 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6571 dflt = d_rpl->dflt;
6572 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006573 break;
6574 case LYS_CHOICE:
6575 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006576 if (lys_compile_deviation_set_choice_dflt(ctx, d_rpl->dflt, (struct lysc_node_choice*)devs[u]->target)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006577 goto cleanup;
6578 }
6579 break;
6580 default:
6581 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006582 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006583 goto cleanup;
6584 }
6585 }
6586
6587 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006588 if (d_rpl->flags & LYS_CONFIG_MASK) {
6589 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006590 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006591 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6592 goto cleanup;
6593 }
6594 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006595 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006596 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6597 goto cleanup;
6598 }
Radek Krejci327de162019-06-14 12:52:07 +02006599 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006600 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006601
6602 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006603 if (d_rpl->flags & LYS_MAND_MASK) {
6604 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006606 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6607 goto cleanup;
6608 }
Radek Krejci327de162019-06-14 12:52:07 +02006609 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006610 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006611
6612 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006613 if (d_rpl->flags & LYS_SET_MIN) {
6614 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6615 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6616 /* change value */
6617 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6618 } else if (devs[u]->target->nodetype == LYS_LIST) {
6619 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6620 /* change value */
6621 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6622 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006624 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6625 goto cleanup;
6626 }
6627 if (d_rpl->min) {
6628 devs[u]->target->flags |= LYS_MAND_TRUE;
6629 }
6630 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006631
6632 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006633 if (d_rpl->flags & LYS_SET_MAX) {
6634 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6635 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6636 /* change value */
6637 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6638 } else if (devs[u]->target->nodetype == LYS_LIST) {
6639 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6640 /* change value */
6641 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6642 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006644 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6645 goto cleanup;
6646 }
6647 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006648
6649 break;
6650 default:
6651 LOGINT(ctx->ctx);
6652 goto cleanup;
6653 }
6654 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006655
Radek Krejci33f72892019-02-21 10:36:58 +01006656 /* final check when all deviations of a single target node are applied */
6657
Radek Krejci551b12c2019-02-19 16:11:21 +01006658 /* check min-max compatibility */
6659 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6660 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6661 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6662 } else if (devs[u]->target->nodetype == LYS_LIST) {
6663 min = ((struct lysc_node_list*)devs[u]->target)->min;
6664 max = ((struct lysc_node_list*)devs[u]->target)->max;
6665 } else {
6666 min = max = 0;
6667 }
6668 if (min > max) {
6669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid combination of min-elements and max-elements "
Radek Krejci327de162019-06-14 12:52:07 +02006670 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006671 goto cleanup;
6672 }
6673
Radek Krejcia1911222019-07-22 17:24:50 +02006674 if (dflt) {
6675 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006676 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006677 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006678 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6679 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006680 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006681 leaf->dflt->realtype->refcount++;
6682 if (err) {
6683 ly_err_print(err);
6684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6685 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6686 ly_err_free(err);
6687 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006688 if (rc == LY_EINCOMPLETE) {
6689 /* postpone default compilation when the tree is complete */
6690 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6691
6692 /* but in general result is so far ok */
6693 rc = LY_SUCCESS;
6694 }
Radek Krejcia1911222019-07-22 17:24:50 +02006695 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006696 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006697 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6698 int dynamic;
6699 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006700 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006701
6702 /* update the list of incomplete default values if needed */
6703 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6704
6705 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006706 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6707 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6708 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006709 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6710 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006711 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006712 leaf->dflt->realtype->refcount++;
6713 if (err) {
6714 ly_err_print(err);
6715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6716 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6717 ly_err_free(err);
6718 }
6719 if (dynamic) {
6720 free((void*)dflt);
6721 }
Radek Krejcia1911222019-07-22 17:24:50 +02006722 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006723 if (rc == LY_EINCOMPLETE) {
6724 /* postpone default compilation when the tree is complete */
6725 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6726
6727 /* but in general result is so far ok */
6728 rc = LY_SUCCESS;
6729 }
6730 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006731 } else { /* LYS_LEAFLIST */
6732 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006733 dflt = llist->dflts[x]->realtype->plugin->print(llist->dflts[x], LYD_XML, lys_get_prefix, llist->dflts_mods[x], &dynamic);
Radek Krejcia1911222019-07-22 17:24:50 +02006734 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6735 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6736 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006737 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6738 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006739 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6740 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006741 llist->dflts[x]->realtype->refcount++;
6742 if (err) {
6743 ly_err_print(err);
6744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6745 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6746 dflt, err->msg);
6747 ly_err_free(err);
6748 }
6749 if (dynamic) {
6750 free((void*)dflt);
6751 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006752 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006753 if (rc == LY_EINCOMPLETE) {
6754 /* postpone default compilation when the tree is complete */
6755 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6756
6757 /* but in general result is so far ok */
6758 rc = LY_SUCCESS;
6759 }
Radek Krejcia1911222019-07-22 17:24:50 +02006760 LY_CHECK_GOTO(rc, cleanup);
6761 }
6762 }
6763 }
6764
Radek Krejci551b12c2019-02-19 16:11:21 +01006765 /* check mandatory - default compatibility */
6766 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6767 && (devs[u]->target->flags & LYS_SET_DFLT)
6768 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6769 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006770 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006771 goto cleanup;
6772 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6773 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6774 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006775 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation combining default case and mandatory choice.");
Radek Krejci551b12c2019-02-19 16:11:21 +01006776 goto cleanup;
6777 }
6778 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6779 /* mandatory node under a default case */
6780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006781 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6782 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006783 goto cleanup;
6784 }
Radek Krejci33f72892019-02-21 10:36:58 +01006785
Radek Krejci327de162019-06-14 12:52:07 +02006786 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006787 }
6788
Radek Krejci327de162019-06-14 12:52:07 +02006789 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006790 ret = LY_SUCCESS;
6791
6792cleanup:
6793 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6794 LY_ARRAY_FREE(devs[u]->deviates);
6795 free(devs[u]);
6796 }
6797 free(devs);
6798 ly_set_erase(&targets, NULL);
6799 ly_set_erase(&devs_p, NULL);
6800
6801 return ret;
6802}
6803
Radek Krejcib56c7502019-02-13 14:19:54 +01006804/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006805 * @brief Compile the given YANG submodule into the main module.
6806 * @param[in] ctx Compile context
6807 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006808 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6809 */
6810LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006811lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006812{
6813 unsigned int u;
6814 LY_ERR ret = LY_SUCCESS;
6815 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006816 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006817 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006818 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006819
Radek Krejci0af46292019-01-11 16:02:31 +01006820 if (!mainmod->mod->off_features) {
6821 /* features are compiled directly into the compiled module structure,
6822 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6823 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006824 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6825 LY_CHECK_GOTO(ret, error);
6826 }
6827 if (!mainmod->mod->off_extensions) {
6828 /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */
6829 ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions);
Radek Krejci0af46292019-01-11 16:02:31 +01006830 LY_CHECK_GOTO(ret, error);
6831 }
6832
Radek Krejci327de162019-06-14 12:52:07 +02006833 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006834 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006835 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006836
Radek Krejci474f9b82019-07-24 11:36:37 +02006837 /* data nodes */
6838 LY_LIST_FOR(submod->data, node_p) {
6839 ret = lys_compile_node(ctx, node_p, NULL, 0);
6840 LY_CHECK_GOTO(ret, error);
6841 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006842
Radek Krejciec4da802019-05-02 13:02:41 +02006843 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6844 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006845
Radek Krejcid05cbd92018-12-05 14:26:40 +01006846error:
6847 return ret;
6848}
6849
Radek Krejci335332a2019-09-05 13:03:35 +02006850static void *
6851lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6852{
6853 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6854 if (substmts[u].stmt == stmt) {
6855 return substmts[u].storage;
6856 }
6857 }
6858 return NULL;
6859}
6860
6861LY_ERR
6862lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6863{
6864 LY_ERR ret = LY_EVALID, r;
6865 unsigned int u;
6866 struct lysp_stmt *stmt;
6867 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006868
6869 /* check for invalid substatements */
6870 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006871 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6872 continue;
6873 }
Radek Krejci335332a2019-09-05 13:03:35 +02006874 for (u = 0; substmts[u].stmt; ++u) {
6875 if (substmts[u].stmt == stmt->kw) {
6876 break;
6877 }
6878 }
6879 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006880 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6881 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006882 goto cleanup;
6883 }
Radek Krejci335332a2019-09-05 13:03:35 +02006884 }
6885
Radek Krejciad5963b2019-09-06 16:03:05 +02006886 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6887
Radek Krejci335332a2019-09-05 13:03:35 +02006888 /* keep order of the processing the same as the order in the defined substmts,
6889 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6890 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006891 int stmt_present = 0;
6892
Radek Krejci335332a2019-09-05 13:03:35 +02006893 for (stmt = ext->child; stmt; stmt = stmt->next) {
6894 if (substmts[u].stmt != stmt->kw) {
6895 continue;
6896 }
6897
Radek Krejciad5963b2019-09-06 16:03:05 +02006898 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006899 if (substmts[u].storage) {
6900 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006901 case LY_STMT_STATUS:
6902 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6903 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6904 break;
6905 case LY_STMT_UNITS: {
6906 const char **units;
6907
6908 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6909 /* single item */
6910 if (*((const char **)substmts[u].storage)) {
6911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6912 goto cleanup;
6913 }
6914 units = (const char **)substmts[u].storage;
6915 } else {
6916 /* sized array */
6917 const char ***units_array = (const char ***)substmts[u].storage;
6918 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6919 }
6920 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6921 break;
6922 }
Radek Krejci335332a2019-09-05 13:03:35 +02006923 case LY_STMT_TYPE: {
6924 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6925 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6926
6927 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6928 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006929 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006930 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6931 goto cleanup;
6932 }
6933 compiled = substmts[u].storage;
6934 } else {
6935 /* sized array */
6936 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6937 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6938 compiled = (void*)type;
6939 }
6940
Radek Krejciad5963b2019-09-06 16:03:05 +02006941 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006942 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6943 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006944 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6945 lysp_type_free(ctx->ctx, parsed);
6946 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006947 break;
6948 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006949 case LY_STMT_IF_FEATURE: {
6950 struct lysc_iffeature *iff = NULL;
6951
6952 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6953 /* single item */
6954 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6956 goto cleanup;
6957 }
6958 iff = (struct lysc_iffeature*)substmts[u].storage;
6959 } else {
6960 /* sized array */
6961 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6962 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6963 }
6964 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6965 break;
6966 }
6967 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6968 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006969 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006970 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.",
6971 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006972 goto cleanup;
6973 }
6974 }
Radek Krejci335332a2019-09-05 13:03:35 +02006975 }
Radek Krejci335332a2019-09-05 13:03:35 +02006976
Radek Krejciad5963b2019-09-06 16:03:05 +02006977 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6979 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6980 goto cleanup;
6981 }
Radek Krejci335332a2019-09-05 13:03:35 +02006982 }
6983
6984 ret = LY_SUCCESS;
6985
6986cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006987 return ret;
6988}
6989
Michal Vasko175012e2019-11-06 15:49:14 +01006990/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006991 * @brief Check when for cyclic dependencies.
6992 * @param[in] set Set with all the referenced nodes.
6993 * @param[in] node Node whose "when" referenced nodes are in @p set.
6994 * @return LY_ERR value
6995 */
6996static LY_ERR
Michal Vasko5c4e5892019-11-14 12:31:38 +01006997lys_compile_check_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006998{
6999 struct lyxp_set tmp_set;
7000 struct lyxp_set_scnode *xp_scnode;
Michal Vasko251f56e2019-11-14 16:06:47 +01007001 uint32_t i, j, k;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007002 int idx;
7003 struct lysc_when *when;
7004 LY_ERR ret = LY_SUCCESS;
7005
7006 memset(&tmp_set, 0, sizeof tmp_set);
7007
7008 /* prepare in_ctx of the set */
7009 for (i = 0; i < set->used; ++i) {
7010 xp_scnode = &set->val.scnodes[i];
7011
Michal Vasko5c4e5892019-11-14 12:31:38 +01007012 if (xp_scnode->in_ctx != -1) {
7013 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01007014 xp_scnode->in_ctx = 1;
7015 }
7016 }
7017
7018 for (i = 0; i < set->used; ++i) {
7019 xp_scnode = &set->val.scnodes[i];
7020 if (xp_scnode->in_ctx != 1) {
7021 /* already checked */
7022 continue;
7023 }
7024
Michal Vasko2ff7efe2019-12-10 14:50:59 +01007025 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_ACTION | LYS_NOTIF))
7026 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007027 /* no when to check */
7028 xp_scnode->in_ctx = 0;
7029 continue;
7030 }
7031
7032 node = xp_scnode->scnode;
7033 do {
Michal Vasko251f56e2019-11-14 16:06:47 +01007034 LY_ARRAY_FOR(node->when, j) {
7035 when = node->when[j];
Michal Vaskoecd62de2019-11-13 12:35:11 +01007036 ret = lyxp_atomize(when->cond, LYD_UNKNOWN, when->module, when->context,
7037 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
7038 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007039 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007040 goto cleanup;
7041 }
7042
Michal Vasko251f56e2019-11-14 16:06:47 +01007043 for (k = 0; k < tmp_set.used; ++k) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007044 /* skip roots'n'stuff */
Michal Vasko251f56e2019-11-14 16:06:47 +01007045 if (tmp_set.val.scnodes[k].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007046 /* try to find this node in our set */
Michal Vasko251f56e2019-11-14 16:06:47 +01007047 idx = lyxp_set_scnode_dup_node_check(set, tmp_set.val.scnodes[k].scnode, LYXP_NODE_ELEM, -1);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007048 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007049 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LY_VCODE_CIRC_WHEN, node->name, set->val.scnodes[idx].scnode->name);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007050 ret = LY_EVALID;
7051 goto cleanup;
7052 }
7053
7054 /* needs to be checked, if in both sets, will be ignored */
Michal Vasko251f56e2019-11-14 16:06:47 +01007055 tmp_set.val.scnodes[k].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007056 } else {
7057 /* no when, nothing to check */
Michal Vasko251f56e2019-11-14 16:06:47 +01007058 tmp_set.val.scnodes[k].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007059 }
7060 }
7061
7062 /* merge this set into the global when set */
7063 lyxp_set_scnode_merge(set, &tmp_set);
7064 }
7065
7066 /* check when of non-data parents as well */
7067 node = node->parent;
7068 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
7069
Michal Vasko251f56e2019-11-14 16:06:47 +01007070 /* this node when was checked (xp_scnode could have been reallocd) */
7071 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007072 }
7073
7074cleanup:
7075 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7076 return ret;
7077}
7078
7079/**
Michal Vasko175012e2019-11-06 15:49:14 +01007080 * @brief Check when/must expressions of a node on a compiled schema tree.
7081 * @param[in] ctx Compile context.
7082 * @param[in] node Node to check.
7083 * @return LY_ERR value
7084 */
7085static LY_ERR
7086lys_compile_check_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
7087{
Michal Vasko175012e2019-11-06 15:49:14 +01007088 struct lyxp_set tmp_set;
7089 uint32_t i, j;
Michal Vasko5d8756a2019-11-07 15:21:00 +01007090 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01007091 struct lysc_when **when = NULL;
7092 struct lysc_must *musts = NULL;
7093 LY_ERR ret = LY_SUCCESS;
7094
7095 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01007096 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01007097
7098 switch (node->nodetype) {
7099 case LYS_CONTAINER:
7100 when = ((struct lysc_node_container *)node)->when;
7101 musts = ((struct lysc_node_container *)node)->musts;
7102 break;
7103 case LYS_CHOICE:
7104 when = ((struct lysc_node_choice *)node)->when;
7105 break;
7106 case LYS_LEAF:
7107 when = ((struct lysc_node_leaf *)node)->when;
7108 musts = ((struct lysc_node_leaf *)node)->musts;
7109 break;
7110 case LYS_LEAFLIST:
7111 when = ((struct lysc_node_leaflist *)node)->when;
7112 musts = ((struct lysc_node_leaflist *)node)->musts;
7113 break;
7114 case LYS_LIST:
7115 when = ((struct lysc_node_list *)node)->when;
7116 musts = ((struct lysc_node_list *)node)->musts;
7117 break;
7118 case LYS_ANYXML:
7119 case LYS_ANYDATA:
7120 when = ((struct lysc_node_anydata *)node)->when;
7121 musts = ((struct lysc_node_anydata *)node)->musts;
7122 break;
7123 case LYS_CASE:
7124 when = ((struct lysc_node_case *)node)->when;
7125 break;
7126 case LYS_NOTIF:
7127 musts = ((struct lysc_notif *)node)->musts;
7128 break;
Michal Vasko5d8756a2019-11-07 15:21:00 +01007129 case LYS_ACTION:
7130 /* first process input musts */
7131 musts = ((struct lysc_action *)node)->input.musts;
7132 break;
Michal Vasko175012e2019-11-06 15:49:14 +01007133 default:
7134 /* nothing to check */
7135 break;
7136 }
7137
Michal Vasko175012e2019-11-06 15:49:14 +01007138 /* check "when" */
7139 LY_ARRAY_FOR(when, i) {
Michal Vasko175012e2019-11-06 15:49:14 +01007140 ret = lyxp_atomize(when[i]->cond, LYD_UNKNOWN, when[i]->module, when[i]->context,
7141 when[i]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
7142 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007143 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[i]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01007144 goto cleanup;
7145 }
7146
Michal Vaskodc052f32019-11-07 11:11:38 +01007147 ctx->path[0] = '\0';
7148 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko175012e2019-11-06 15:49:14 +01007149 for (j = 0; j < tmp_set.used; ++j) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01007150 /* skip roots'n'stuff */
7151 if ((tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[j].in_ctx != -1)) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007152 struct lysc_node *schema = tmp_set.val.scnodes[j].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01007153
Michal Vaskoecd62de2019-11-13 12:35:11 +01007154 /* XPath expression cannot reference "lower" status than the node that has the definition */
7155 ret = lysc_check_status(ctx, when[i]->flags, when[i]->module, node->name, schema->flags, schema->module,
7156 schema->name);
7157 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007158
7159 /* check dummy node accessing */
7160 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007161 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007162 ret = LY_EVALID;
7163 goto cleanup;
7164 }
Michal Vasko175012e2019-11-06 15:49:14 +01007165 }
7166 }
7167
Michal Vaskoecd62de2019-11-13 12:35:11 +01007168 /* check cyclic dependencies */
Michal Vasko5c4e5892019-11-14 12:31:38 +01007169 ret = lys_compile_check_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007170 LY_CHECK_GOTO(ret, cleanup);
7171
Michal Vasko175012e2019-11-06 15:49:14 +01007172 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7173 }
7174
Michal Vasko5d8756a2019-11-07 15:21:00 +01007175check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01007176 /* check "must" */
7177 LY_ARRAY_FOR(musts, i) {
Michal Vasko175012e2019-11-06 15:49:14 +01007178 ret = lyxp_atomize(musts[i].cond, LYD_UNKNOWN, musts[i].module, node, LYXP_NODE_ELEM, &tmp_set, opts);
7179 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007180 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[i].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01007181 goto cleanup;
7182 }
7183
Michal Vaskodc052f32019-11-07 11:11:38 +01007184 ctx->path[0] = '\0';
7185 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko175012e2019-11-06 15:49:14 +01007186 for (j = 0; j < tmp_set.used; ++j) {
7187 /* skip roots'n'stuff */
7188 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
7189 /* XPath expression cannot reference "lower" status than the node that has the definition */
Michal Vasko5d8756a2019-11-07 15:21:00 +01007190 ret = lysc_check_status(ctx, node->flags, musts[i].module, node->name, tmp_set.val.scnodes[j].scnode->flags,
Michal Vasko175012e2019-11-06 15:49:14 +01007191 tmp_set.val.scnodes[j].scnode->module, tmp_set.val.scnodes[j].scnode->name);
7192 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01007193 }
7194 }
7195
7196 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7197 }
7198
Michal Vasko5d8756a2019-11-07 15:21:00 +01007199 if ((node->nodetype == LYS_ACTION) && !input_done) {
7200 /* now check output musts */
7201 input_done = 1;
7202 musts = ((struct lysc_action *)node)->output.musts;
7203 opts = LYXP_SCNODE_OUTPUT;
7204 goto check_musts;
7205 }
7206
Michal Vasko175012e2019-11-06 15:49:14 +01007207cleanup:
7208 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7209 return ret;
7210}
7211
Radek Krejci19a96102018-11-15 13:38:09 +01007212LY_ERR
7213lys_compile(struct lys_module *mod, int options)
7214{
7215 struct lysc_ctx ctx = {0};
7216 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01007217 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01007218 struct lysp_module *sp;
7219 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007220 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007221 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007222 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01007223 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007224 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007225
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007226 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007227
7228 if (!mod->implemented) {
7229 /* just imported modules are not compiled */
7230 return LY_SUCCESS;
7231 }
7232
Radek Krejci19a96102018-11-15 13:38:09 +01007233 sp = mod->parsed;
7234
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007235 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01007236 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01007237 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007238 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007239 ctx.path_len = 1;
7240 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007241
7242 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007243 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
7244 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007245
Radek Krejciec4da802019-05-02 13:02:41 +02007246 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007247 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007248 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007249 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7250 }
Radek Krejci0935f412019-08-20 16:15:18 +02007251
7252 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01007253 if (mod->off_features) {
7254 /* there is already precompiled array of features */
7255 mod_c->features = mod->off_features;
7256 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007257 } else {
7258 /* features are compiled directly into the compiled module structure,
7259 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves */
Radek Krejci327de162019-06-14 12:52:07 +02007260 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007261 LY_CHECK_GOTO(ret, error);
7262 }
7263 /* finish feature compilation, not only for the main module, but also for the submodules.
7264 * Due to possible forward references, it must be done when all the features (including submodules)
7265 * are present. */
7266 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007267 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007268 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7269 }
Radek Krejci327de162019-06-14 12:52:07 +02007270 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007271 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007272 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007273 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007274 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007275 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7276 }
Radek Krejci327de162019-06-14 12:52:07 +02007277 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007278 }
Radek Krejci327de162019-06-14 12:52:07 +02007279 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007280
Radek Krejci0935f412019-08-20 16:15:18 +02007281 /* extensions */
7282 /* 2-steps: a) prepare compiled structures and ... */
7283 if (mod->off_extensions) {
7284 /* there is already precompiled array of extension definitions */
7285 mod_c->extensions = mod->off_extensions;
7286 mod->off_extensions = NULL;
7287 } else {
7288 /* extension definitions are compiled directly into the compiled module structure */
7289 ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions);
Radek Krejcibfb2e142019-09-09 14:47:44 +02007290 LY_CHECK_GOTO(ret, error);
Radek Krejci0935f412019-08-20 16:15:18 +02007291 }
7292 /* ... b) connect the extension definitions with the appropriate extension plugins */
7293 lys_compile_extension_plugins(mod_c->extensions);
7294
7295 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02007296 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02007297 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007298 if (sp->identities) {
7299 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
7300 }
Radek Krejci327de162019-06-14 12:52:07 +02007301 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007302
Radek Krejci95710c92019-02-11 15:49:55 +01007303 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007304 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007305 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007306 }
Radek Krejci95710c92019-02-11 15:49:55 +01007307
Radek Krejciec4da802019-05-02 13:02:41 +02007308 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7309 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007310
Radek Krejci95710c92019-02-11 15:49:55 +01007311 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007312 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007313 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007314 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007315 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007316
Radek Krejci474f9b82019-07-24 11:36:37 +02007317 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007318 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007319
Radek Krejci0935f412019-08-20 16:15:18 +02007320 /* extension instances TODO cover extension instances from submodules */
7321 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007322
Radek Krejcia3045382018-11-22 14:30:31 +01007323 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01007324 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7325 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7326 * point to the starting leafref type). The second round stores the first non-leafref type for later data validation. */
Radek Krejcia3045382018-11-22 14:30:31 +01007327 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01007328 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01007329 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
7330 if (type->basetype == LY_TYPE_LEAFREF) {
7331 /* validate the path */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02007332 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type, NULL), error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007333 } else if (type->basetype == LY_TYPE_UNION) {
7334 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7335 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7336 /* validate the path */
Michal Vasko175012e2019-11-06 15:49:14 +01007337 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
7338 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v], NULL);
7339 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007340 }
7341 }
Radek Krejcia3045382018-11-22 14:30:31 +01007342 }
7343 }
Michal Vasko175012e2019-11-06 15:49:14 +01007344
7345 /* check xpath */
7346 LY_CHECK_GOTO(ret = lys_compile_check_xpath(&ctx, ctx.unres.objs[u]), error);
Radek Krejcia3045382018-11-22 14:30:31 +01007347 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01007348 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01007349 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01007350 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
7351 if (type->basetype == LY_TYPE_LEAFREF) {
7352 /* store pointer to the real type */
7353 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7354 typeiter->basetype == LY_TYPE_LEAFREF;
7355 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7356 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01007357 } else if (type->basetype == LY_TYPE_UNION) {
7358 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7359 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7360 /* store pointer to the real type */
7361 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7362 typeiter->basetype == LY_TYPE_LEAFREF;
7363 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7364 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7365 }
7366 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01007367 }
7368 }
7369 }
Radek Krejciec4da802019-05-02 13:02:41 +02007370
Radek Krejci474f9b82019-07-24 11:36:37 +02007371 /* finish incomplete default values compilation */
7372 for (u = 0; u < ctx.dflts.count; ++u) {
7373 struct ly_err_item *err = NULL;
7374 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
Radek Krejci950f6a52019-09-12 17:15:32 +02007375 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
Radek Krejci474f9b82019-07-24 11:36:37 +02007376 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7377 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7378 if (err) {
7379 ly_err_print(err);
7380 ctx.path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02007381 lysc_path(r->context_node, LYSC_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
Radek Krejci474f9b82019-07-24 11:36:37 +02007382 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
7383 "Invalid default - value does not fit the type (%s).", err->msg);
7384 ly_err_free(err);
7385 }
7386 LY_CHECK_GOTO(ret, error);
7387 }
7388
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007389 /* validate non-instantiated groupings from the parsed schema,
7390 * without it we would accept even the schemas with invalid grouping specification */
7391 ctx.options |= LYSC_OPT_GROUPING;
7392 LY_ARRAY_FOR(sp->groupings, u) {
7393 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007394 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007395 }
7396 }
7397 LY_LIST_FOR(sp->data, node_p) {
7398 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7399 LY_ARRAY_FOR(grps, u) {
7400 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007401 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007402 }
7403 }
7404 }
7405
Radek Krejci474f9b82019-07-24 11:36:37 +02007406 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7407 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7408 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7409 }
7410
Radek Krejci1c0c3442019-07-23 16:08:47 +02007411 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007412 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007413 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007414 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007415 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007416
Radek Krejciec4da802019-05-02 13:02:41 +02007417 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01007418 lysp_module_free(mod->parsed);
7419 ((struct lys_module*)mod)->parsed = NULL;
7420 }
7421
Radek Krejciec4da802019-05-02 13:02:41 +02007422 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007423 /* remove flag of the modules implemented by dependency */
7424 for (u = 0; u < ctx.ctx->list.count; ++u) {
7425 m = ctx.ctx->list.objs[u];
7426 if (m->implemented == 2) {
7427 m->implemented = 1;
7428 }
7429 }
7430 }
7431
Radek Krejci19a96102018-11-15 13:38:09 +01007432 ((struct lys_module*)mod)->compiled = mod_c;
7433 return LY_SUCCESS;
7434
7435error:
Radek Krejci95710c92019-02-11 15:49:55 +01007436 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007437 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007438 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007439 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007440 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007441 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007442 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007443 mod->compiled = NULL;
7444
7445 /* revert compilation of modules implemented by dependency */
7446 for (u = 0; u < ctx.ctx->list.count; ++u) {
7447 m = ctx.ctx->list.objs[u];
Michal Vasko2947ce12019-12-16 10:37:19 +01007448 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007449 /* revert features list to the precompiled state */
7450 lys_feature_precompile_revert(&ctx, m);
7451 /* mark module as imported-only / not-implemented */
7452 m->implemented = 0;
7453 /* free the compiled version of the module */
7454 lysc_module_free(m->compiled, NULL);
7455 m->compiled = NULL;
7456 }
7457 }
7458
Radek Krejci19a96102018-11-15 13:38:09 +01007459 return ret;
7460}