blob: e827ca33f841b6a6f135aae15ac585dbade85832 [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 Krejcia0f704a2019-09-09 16:12:23 +020087#define COMPILE_ARRAY_MUST_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, PARENT, RET, GOTO) \
88 if (ARRAY_P) { \
89 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
90 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
91 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
92 LY_ARRAY_INCREMENT(ARRAY_C); \
93 RET = lys_compile_must(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
94 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
95 (ARRAY_C)[ITER + __array_offset].context = lysc_xpath_context((struct lysc_node*)PARENT); \
96 } \
97 }
98
Radek Krejciec4da802019-05-02 13:02:41 +020099#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +0100100 if (MEMBER_P) { \
101 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
102 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +0200103 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +0100104 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
105 }
106
Radek Krejciec4da802019-05-02 13:02:41 +0200107#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +0100108 if (MEMBER_P) { \
109 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
110 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
111 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200112 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100113 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
114 }
115
Radek Krejcid05cbd92018-12-05 14:26:40 +0100116#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
117 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100118 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
119 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100120 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
121 return LY_EVALID; \
122 } \
123 } \
124 }
125
Radek Krejci19a96102018-11-15 13:38:09 +0100126static struct lysc_ext_instance *
127lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
128{
Radek Krejcie7b95092019-05-15 11:03:07 +0200129 /* TODO - extensions */
Radek Krejci19a96102018-11-15 13:38:09 +0100130 (void) ctx;
131 (void) orig;
132 return NULL;
133}
134
Radek Krejcib56c7502019-02-13 14:19:54 +0100135/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200136 * @brief Add record into the compile context's list of incomplete default values.
137 * @param[in] ctx Compile context with the incomplete default values list.
138 * @param[in] context_node Context schema node to store in the record.
139 * @param[in] dflt Incomplete default value to store in the record.
140 * @param[in] dflt_mod Module of the default value definition to store in the record.
141 * @return LY_EMEM in case of memory allocation failure.
142 * @return LY_SUCCESS
143 */
144static LY_ERR
145lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
146{
147 struct lysc_incomplete_dflt *r;
148 r = malloc(sizeof *r);
149 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
150 r->context_node = context_node;
151 r->dflt = dflt;
152 r->dflt_mod = dflt_mod;
153 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
154
155 return LY_SUCCESS;
156}
157
158/**
159 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
160 * @param[in] ctx Compile context with the incomplete default values list.
161 * @param[in] dflt Incomplete default values identifying the record to remove.
162 */
163static void
164lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
165{
166 unsigned int u;
167 struct lysc_incomplete_dflt *r;
168
169 for (u = 0; u < ctx->dflts.count; ++u) {
170 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
171 if (r->dflt == dflt) {
172 free(ctx->dflts.objs[u]);
173 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
174 --ctx->dflts.count;
175 return;
176 }
177 }
178}
179
Radek Krejci0e59c312019-08-15 15:34:15 +0200180void
Radek Krejci327de162019-06-14 12:52:07 +0200181lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
182{
183 int len;
184 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
185
186 if (!name) {
187 /* removing last path segment */
188 if (ctx->path[ctx->path_len - 1] == '}') {
189 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
190 if (ctx->path[ctx->path_len] == '=') {
191 ctx->path[ctx->path_len++] = '}';
192 } else {
193 /* not a top-level special tag, remove also preceiding '/' */
194 goto remove_nodelevel;
195 }
196 } else {
197remove_nodelevel:
198 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
199 if (ctx->path_len == 0) {
200 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200201 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200202 }
203 }
204 /* set new terminating NULL-byte */
205 ctx->path[ctx->path_len] = '\0';
206 } else {
207 if (ctx->path_len > 1) {
208 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
209 /* extension of the special tag */
210 nextlevel = 2;
211 --ctx->path_len;
212 } else {
213 /* there is already some path, so add next level */
214 nextlevel = 1;
215 }
216 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
217
218 if (nextlevel != 2) {
219 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
220 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200221 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200222 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200223 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 +0200224 }
225 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200226 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200227 }
Radek Krejciacc79042019-07-25 14:14:57 +0200228 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
229 /* output truncated */
230 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
231 } else {
232 ctx->path_len += len;
233 }
Radek Krejci327de162019-06-14 12:52:07 +0200234 }
235}
236
237/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100238 * @brief Duplicate the compiled pattern structure.
239 *
240 * Instead of duplicating memory, the reference counter in the @p orig is increased.
241 *
242 * @param[in] orig The pattern structure to duplicate.
243 * @return The duplicated structure to use.
244 */
Radek Krejci19a96102018-11-15 13:38:09 +0100245static struct lysc_pattern*
246lysc_pattern_dup(struct lysc_pattern *orig)
247{
248 ++orig->refcount;
249 return orig;
250}
251
Radek Krejcib56c7502019-02-13 14:19:54 +0100252/**
253 * @brief Duplicate the array of compiled patterns.
254 *
255 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
256 *
257 * @param[in] ctx Libyang context for logging.
258 * @param[in] orig The patterns sized array to duplicate.
259 * @return New sized array as a copy of @p orig.
260 * @return NULL in case of memory allocation error.
261 */
Radek Krejci19a96102018-11-15 13:38:09 +0100262static struct lysc_pattern**
263lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
264{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100265 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100266 unsigned int u;
267
Radek Krejcib56c7502019-02-13 14:19:54 +0100268 assert(orig);
269
Radek Krejci19a96102018-11-15 13:38:09 +0100270 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
271 LY_ARRAY_FOR(orig, u) {
272 dup[u] = lysc_pattern_dup(orig[u]);
273 LY_ARRAY_INCREMENT(dup);
274 }
275 return dup;
276}
277
Radek Krejcib56c7502019-02-13 14:19:54 +0100278/**
279 * @brief Duplicate compiled range structure.
280 *
281 * @param[in] ctx Libyang context for logging.
282 * @param[in] orig The range structure to be duplicated.
283 * @return New compiled range structure as a copy of @p orig.
284 * @return NULL in case of memory allocation error.
285 */
Radek Krejci19a96102018-11-15 13:38:09 +0100286struct lysc_range*
287lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
288{
289 struct lysc_range *dup;
290 LY_ERR ret;
291
Radek Krejcib56c7502019-02-13 14:19:54 +0100292 assert(orig);
293
Radek Krejci19a96102018-11-15 13:38:09 +0100294 dup = calloc(1, sizeof *dup);
295 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
296 if (orig->parts) {
297 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
298 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
299 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
300 }
301 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
302 DUP_STRING(ctx, orig->emsg, dup->emsg);
303 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
304
305 return dup;
306cleanup:
307 free(dup);
308 (void) ret; /* set but not used due to the return type */
309 return NULL;
310}
311
Radek Krejcib56c7502019-02-13 14:19:54 +0100312/**
313 * @brief Stack for processing if-feature expressions.
314 */
Radek Krejci19a96102018-11-15 13:38:09 +0100315struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100316 int size; /**< number of items in the stack */
317 int index; /**< first empty item */
318 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100319};
320
Radek Krejcib56c7502019-02-13 14:19:54 +0100321/**
322 * @brief Add @ref ifftokens into the stack.
323 * @param[in] stack The if-feature stack to use.
324 * @param[in] value One of the @ref ifftokens to store in the stack.
325 * @return LY_EMEM in case of memory allocation error
326 * @return LY_ESUCCESS if the value successfully stored.
327 */
Radek Krejci19a96102018-11-15 13:38:09 +0100328static LY_ERR
329iff_stack_push(struct iff_stack *stack, uint8_t value)
330{
331 if (stack->index == stack->size) {
332 stack->size += 4;
333 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
334 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
335 }
336 stack->stack[stack->index++] = value;
337 return LY_SUCCESS;
338}
339
Radek Krejcib56c7502019-02-13 14:19:54 +0100340/**
341 * @brief Get (and remove) the last item form the stack.
342 * @param[in] stack The if-feature stack to use.
343 * @return The value from the top of the stack.
344 */
Radek Krejci19a96102018-11-15 13:38:09 +0100345static uint8_t
346iff_stack_pop(struct iff_stack *stack)
347{
Radek Krejcib56c7502019-02-13 14:19:54 +0100348 assert(stack && stack->index);
349
Radek Krejci19a96102018-11-15 13:38:09 +0100350 stack->index--;
351 return stack->stack[stack->index];
352}
353
Radek Krejcib56c7502019-02-13 14:19:54 +0100354/**
355 * @brief Clean up the stack.
356 * @param[in] stack The if-feature stack to use.
357 */
Radek Krejci19a96102018-11-15 13:38:09 +0100358static void
359iff_stack_clean(struct iff_stack *stack)
360{
361 stack->size = 0;
362 free(stack->stack);
363}
364
Radek Krejcib56c7502019-02-13 14:19:54 +0100365/**
366 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
367 * (libyang format of the if-feature expression).
368 * @param[in,out] list The 2bits array to modify.
369 * @param[in] op The operand (@ref ifftokens) to store.
370 * @param[in] pos Position (0-based) where to store the given @p op.
371 */
Radek Krejci19a96102018-11-15 13:38:09 +0100372static void
373iff_setop(uint8_t *list, uint8_t op, int pos)
374{
375 uint8_t *item;
376 uint8_t mask = 3;
377
378 assert(pos >= 0);
379 assert(op <= 3); /* max 2 bits */
380
381 item = &list[pos / 4];
382 mask = mask << 2 * (pos % 4);
383 *item = (*item) & ~mask;
384 *item = (*item) | (op << 2 * (pos % 4));
385}
386
Radek Krejcib56c7502019-02-13 14:19:54 +0100387#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
388#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100389
Radek Krejci0af46292019-01-11 16:02:31 +0100390/**
391 * @brief Find a feature of the given name and referenced in the given module.
392 *
393 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
394 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
395 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
396 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
397 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
398 * assigned till that time will be still valid.
399 *
400 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
401 * @param[in] name Name of the feature including possible prefix.
402 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
403 * @return Pointer to the feature structure if found, NULL otherwise.
404 */
Radek Krejci19a96102018-11-15 13:38:09 +0100405static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100406lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100407{
408 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100409 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100410
411 for (i = 0; i < len; ++i) {
412 if (name[i] == ':') {
413 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100414 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100415 LY_CHECK_RET(!mod, NULL);
416
417 name = &name[i + 1];
418 len = len - i - 1;
419 }
420 }
421
422 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100423 if (mod->implemented) {
424 /* module is implemented so there is already the compiled schema */
425 flist = mod->compiled->features;
426 } else {
427 flist = mod->off_features;
428 }
429 LY_ARRAY_FOR(flist, i) {
430 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100431 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
432 return f;
433 }
434 }
435
436 return NULL;
437}
438
439static LY_ERR
Radek Krejci0935f412019-08-20 16:15:18 +0200440lys_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 +0100441{
442 const char *name;
443 unsigned int u;
444 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +0200445 struct lysc_ext *elist = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100446
447 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
448 ext->insubstmt = ext_p->insubstmt;
449 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200450 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200451 ext->parent = parent;
452 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100453
Radek Krejcif56e2a42019-09-09 14:15:25 +0200454 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
455 lysc_update_path(ctx, NULL, ext_p->name );
456
Radek Krejci19a96102018-11-15 13:38:09 +0100457 /* get module where the extension definition should be placed */
458 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100459 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100460 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
461 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
462 LY_EVALID);
463 LY_CHECK_ERR_RET(!mod->parsed->extensions,
464 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
465 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100466 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100467 LY_EVALID);
468 name = &ext_p->name[u + 1];
Radek Krejci0935f412019-08-20 16:15:18 +0200469
Radek Krejci19a96102018-11-15 13:38:09 +0100470 /* find the extension definition there */
Radek Krejci0935f412019-08-20 16:15:18 +0200471 if (mod->off_extensions) {
472 elist = mod->off_extensions;
473 } else {
474 elist = mod->compiled->extensions;
475 }
476 LY_ARRAY_FOR(elist, u) {
477 if (!strcmp(name, elist[u].name)) {
478 ext->def = &elist[u];
Radek Krejci19a96102018-11-15 13:38:09 +0100479 break;
480 }
481 }
Radek Krejci0935f412019-08-20 16:15:18 +0200482 LY_CHECK_ERR_RET(!ext->def,
483 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
484 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100485 LY_EVALID);
Radek Krejci0935f412019-08-20 16:15:18 +0200486
Radek Krejcif56e2a42019-09-09 14:15:25 +0200487 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
488 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
489 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
490 if (ext_p->yin && ext->def->argument) {
491 /* Schema was parsed from YIN and an argument is expected, ... */
492 struct lysp_stmt *stmt = NULL;
493
494 if (ext->def->flags & LYS_YINELEM_TRUE) {
495 /* ... argument was the first XML child element */
496 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
497 /* TODO check namespace of the statement */
498 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
499 stmt = ext_p->child;
500 }
501 }
502 } else {
503 /* ... argument was one of the XML attributes which are represented as child stmt
504 * with LYS_YIN_ATTR flag */
505 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
506 if (!strcmp(stmt->stmt, ext->def->argument)) {
507 /* this is the extension's argument */
508 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
509 break;
510 }
511 }
512 }
513 if (!stmt) {
514 /* missing extension's argument */
515 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
516 "Extension instance \"%s\" misses argument \"%s\".", ext_p->name, ext->def->argument);
517 return LY_EVALID;
518
519 }
520 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
521 stmt->flags |= LYS_YIN_ARGUMENT;
522 }
523
Radek Krejci0935f412019-08-20 16:15:18 +0200524 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200525 if (ext->argument) {
526 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
527 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200528 LY_CHECK_RET(ext->def->plugin->compile(ctx, ext_p, ext), LY_EVALID);
Radek Krejciad5963b2019-09-06 16:03:05 +0200529 if (ext->argument) {
530 lysc_update_path(ctx, NULL, NULL);
531 }
Radek Krejci0935f412019-08-20 16:15:18 +0200532 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200533 ext_p->compiled = ext;
534
535 lysc_update_path(ctx, NULL, NULL);
536 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200537
538 return LY_SUCCESS;
539}
540
541/**
542 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
543 */
544static LY_ERR
545lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext)
546{
547 LY_ERR ret = LY_SUCCESS;
548
549 DUP_STRING(ctx->ctx, ext_p->name, ext->name);
550 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
551 ext->module = ctx->mod_def;
552 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done);
553
554done:
555 return ret;
556}
557
558/**
559 * @brief Link the extensions definitions with the available extension plugins.
560 *
561 * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules
562 * are not connected with even available extension plugins.
563 *
564 * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)).
565 */
566static void
567lys_compile_extension_plugins(struct lysc_ext *extensions)
568{
569 unsigned int u;
570
571 LY_ARRAY_FOR(extensions, u) {
572 extensions[u].plugin = lyext_get_plugin(&extensions[u]);
573 }
574}
575
576LY_ERR
577lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
578 struct lysp_ext *extensions_p, struct lysc_ext **extensions)
579{
580 unsigned int offset = 0, u;
581 struct lysc_ctx context = {0};
582
583 assert(ctx_sc || ctx);
584
585 if (!ctx_sc) {
586 context.ctx = ctx;
587 context.mod = module;
588 context.path_len = 1;
589 context.path[0] = '/';
590 ctx_sc = &context;
591 }
592
593 if (!extensions_p) {
594 return LY_SUCCESS;
595 }
596 if (*extensions) {
597 offset = LY_ARRAY_SIZE(*extensions);
598 }
599
600 lysc_update_path(ctx_sc, NULL, "{extension}");
601 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM);
602 LY_ARRAY_FOR(extensions_p, u) {
603 lysc_update_path(ctx_sc, NULL, extensions_p[u].name);
604 LY_ARRAY_INCREMENT(*extensions);
605 COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name);
606 LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u]));
607 lysc_update_path(ctx_sc, NULL, NULL);
608 }
609 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100610
611 return LY_SUCCESS;
612}
613
Radek Krejcib56c7502019-02-13 14:19:54 +0100614/**
615 * @brief Compile information from the if-feature statement
616 * @param[in] ctx Compile context.
617 * @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 +0100618 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
619 * @return LY_ERR value.
620 */
Radek Krejci19a96102018-11-15 13:38:09 +0100621static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200622lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100623{
624 const char *c = *value;
625 int r, rc = EXIT_FAILURE;
626 int i, j, last_not, checkversion = 0;
627 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
628 uint8_t op;
629 struct iff_stack stack = {0, 0, NULL};
630 struct lysc_feature *f;
631
632 assert(c);
633
634 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
635 for (i = j = last_not = 0; c[i]; i++) {
636 if (c[i] == '(') {
637 j++;
638 checkversion = 1;
639 continue;
640 } else if (c[i] == ')') {
641 j--;
642 continue;
643 } else if (isspace(c[i])) {
644 checkversion = 1;
645 continue;
646 }
647
648 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 +0200649 int sp;
650 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
651 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100652 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
653 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
654 return LY_EVALID;
655 } else if (!isspace(c[i + r])) {
656 /* feature name starting with the not/and/or */
657 last_not = 0;
658 f_size++;
659 } else if (c[i] == 'n') { /* not operation */
660 if (last_not) {
661 /* double not */
662 expr_size = expr_size - 2;
663 last_not = 0;
664 } else {
665 last_not = 1;
666 }
667 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200668 if (f_exp != f_size) {
669 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
670 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
671 return LY_EVALID;
672 }
Radek Krejci19a96102018-11-15 13:38:09 +0100673 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200674
Radek Krejci19a96102018-11-15 13:38:09 +0100675 /* not a not operation */
676 last_not = 0;
677 }
678 i += r;
679 } else {
680 f_size++;
681 last_not = 0;
682 }
683 expr_size++;
684
685 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200686 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100687 i--;
688 break;
689 }
690 i++;
691 }
692 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200693 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100694 /* not matching count of ( and ) */
695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
696 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
697 return LY_EVALID;
698 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200699 if (f_exp != f_size) {
700 /* features do not match the needed arguments for the logical operations */
701 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
702 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
703 "the required number of operands for the operations.", *value);
704 return LY_EVALID;
705 }
Radek Krejci19a96102018-11-15 13:38:09 +0100706
707 if (checkversion || expr_size > 1) {
708 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100709 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100710 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
711 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
712 return LY_EVALID;
713 }
714 }
715
716 /* allocate the memory */
717 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
718 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
719 stack.stack = malloc(expr_size * sizeof *stack.stack);
720 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
721
722 stack.size = expr_size;
723 f_size--; expr_size--; /* used as indexes from now */
724
725 for (i--; i >= 0; i--) {
726 if (c[i] == ')') {
727 /* push it on stack */
728 iff_stack_push(&stack, LYS_IFF_RP);
729 continue;
730 } else if (c[i] == '(') {
731 /* pop from the stack into result all operators until ) */
732 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
733 iff_setop(iff->expr, op, expr_size--);
734 }
735 continue;
736 } else if (isspace(c[i])) {
737 continue;
738 }
739
740 /* end of operator or operand -> find beginning and get what is it */
741 j = i + 1;
742 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
743 i--;
744 }
745 i++; /* go back by one step */
746
747 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
748 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
749 /* double not */
750 iff_stack_pop(&stack);
751 } else {
752 /* not has the highest priority, so do not pop from the stack
753 * as in case of AND and OR */
754 iff_stack_push(&stack, LYS_IFF_NOT);
755 }
756 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
757 /* as for OR - pop from the stack all operators with the same or higher
758 * priority and store them to the result, then push the AND to the stack */
759 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
760 op = iff_stack_pop(&stack);
761 iff_setop(iff->expr, op, expr_size--);
762 }
763 iff_stack_push(&stack, LYS_IFF_AND);
764 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
765 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
766 op = iff_stack_pop(&stack);
767 iff_setop(iff->expr, op, expr_size--);
768 }
769 iff_stack_push(&stack, LYS_IFF_OR);
770 } else {
771 /* feature name, length is j - i */
772
773 /* add it to the expression */
774 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
775
776 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100777 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
778 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
779 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
780 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100781 iff->features[f_size] = f;
782 LY_ARRAY_INCREMENT(iff->features);
783 f_size--;
784 }
785 }
786 while (stack.index) {
787 op = iff_stack_pop(&stack);
788 iff_setop(iff->expr, op, expr_size--);
789 }
790
791 if (++expr_size || ++f_size) {
792 /* not all expected operators and operands found */
793 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
794 "Invalid value \"%s\" of if-feature - processing error.", *value);
795 rc = LY_EINT;
796 } else {
797 rc = LY_SUCCESS;
798 }
799
800error:
801 /* cleanup */
802 iff_stack_clean(&stack);
803
804 return rc;
805}
806
Radek Krejcib56c7502019-02-13 14:19:54 +0100807/**
808 * @brief Compile information from the when statement
809 * @param[in] ctx Compile context.
810 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100811 * @param[out] when Pointer where to store pointer to the created compiled when structure.
812 * @return LY_ERR value.
813 */
Radek Krejci19a96102018-11-15 13:38:09 +0100814static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200815lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100816{
Radek Krejci19a96102018-11-15 13:38:09 +0100817 LY_ERR ret = LY_SUCCESS;
818
Radek Krejci00b874b2019-02-12 10:54:50 +0100819 *when = calloc(1, sizeof **when);
820 (*when)->refcount = 1;
821 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200822 (*when)->module = ctx->mod_def;
Radek Krejci00b874b2019-02-12 10:54:50 +0100823 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
824 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
825 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200826 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100827
828done:
829 return ret;
830}
831
Radek Krejcib56c7502019-02-13 14:19:54 +0100832/**
833 * @brief Compile information from the must statement
834 * @param[in] ctx Compile context.
835 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100836 * @param[in,out] must Prepared (empty) compiled must structure to fill.
837 * @return LY_ERR value.
838 */
Radek Krejci19a96102018-11-15 13:38:09 +0100839static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200840lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100841{
Radek Krejci19a96102018-11-15 13:38:09 +0100842 LY_ERR ret = LY_SUCCESS;
843
844 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
845 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200846 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100847 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
848 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100849 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
850 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200851 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100852
853done:
854 return ret;
855}
856
Radek Krejcib56c7502019-02-13 14:19:54 +0100857/**
858 * @brief Compile information from the import statement
859 * @param[in] ctx Compile context.
860 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100861 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
862 * @return LY_ERR value.
863 */
Radek Krejci19a96102018-11-15 13:38:09 +0100864static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200865lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100866{
Radek Krejci19a96102018-11-15 13:38:09 +0100867 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100868 LY_ERR ret = LY_SUCCESS;
869
870 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200871 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100872 imp->module = imp_p->module;
873
Radek Krejci7f2a5362018-11-28 13:05:37 +0100874 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
875 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100876 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100877 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100878 /* try to use filepath if present */
879 if (imp->module->filepath) {
880 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
881 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100882 if (mod != imp->module) {
883 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100884 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100885 mod = NULL;
886 }
887 }
888 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100889 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100890 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 +0100891 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100892 return LY_ENOTFOUND;
893 }
894 }
Radek Krejci19a96102018-11-15 13:38:09 +0100895 }
896
897done:
898 return ret;
899}
900
Radek Krejcib56c7502019-02-13 14:19:54 +0100901/**
902 * @brief Compile information from the identity statement
903 *
904 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
905 *
906 * @param[in] ctx Compile context.
907 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100908 * @param[in] idents List of so far compiled identities to check the name uniqueness.
909 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
910 * @return LY_ERR value.
911 */
Radek Krejci19a96102018-11-15 13:38:09 +0100912static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200913lys_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 +0100914{
915 unsigned int u;
916 LY_ERR ret = LY_SUCCESS;
917
Radek Krejci327de162019-06-14 12:52:07 +0200918 lysc_update_path(ctx, NULL, ident_p->name);
919
Radek Krejcid05cbd92018-12-05 14:26:40 +0100920 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100921 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200922 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
923 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200924 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200925 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100926 /* 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 +0200927 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100928 ident->flags = ident_p->flags;
929
Radek Krejci327de162019-06-14 12:52:07 +0200930 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100931done:
932 return ret;
933}
934
Radek Krejcib56c7502019-02-13 14:19:54 +0100935/**
936 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
937 *
938 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
939 *
940 * @param[in] ctx Compile context for logging.
941 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
942 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
943 * @return LY_SUCCESS if everything is ok.
944 * @return LY_EVALID if the identity is derived from itself.
945 */
Radek Krejci38222632019-02-12 16:55:05 +0100946static LY_ERR
947lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
948{
949 LY_ERR ret = LY_EVALID;
950 unsigned int u, v;
951 struct ly_set recursion = {0};
952 struct lysc_ident *drv;
953
954 if (!derived) {
955 return LY_SUCCESS;
956 }
957
958 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
959 if (ident == derived[u]) {
960 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
961 "Identity \"%s\" is indirectly derived from itself.", ident->name);
962 goto cleanup;
963 }
964 ly_set_add(&recursion, derived[u], 0);
965 }
966
967 for (v = 0; v < recursion.count; ++v) {
968 drv = recursion.objs[v];
969 if (!drv->derived) {
970 continue;
971 }
972 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
973 if (ident == drv->derived[u]) {
974 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
975 "Identity \"%s\" is indirectly derived from itself.", ident->name);
976 goto cleanup;
977 }
978 ly_set_add(&recursion, drv->derived[u], 0);
979 }
980 }
981 ret = LY_SUCCESS;
982
983cleanup:
984 ly_set_erase(&recursion, NULL);
985 return ret;
986}
987
Radek Krejcia3045382018-11-22 14:30:31 +0100988/**
989 * @brief Find and process the referenced base identities from another identity or identityref
990 *
Radek Krejciaca74032019-06-04 08:53:06 +0200991 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +0100992 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
993 * to distinguish these two use cases.
994 *
995 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
996 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
997 * @param[in] ident Referencing identity to work with.
998 * @param[in] bases Array of bases of identityref to fill in.
999 * @return LY_ERR value.
1000 */
Radek Krejci19a96102018-11-15 13:38:09 +01001001static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +01001002lys_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 +01001003{
Radek Krejci555cb5b2018-11-16 14:54:33 +01001004 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001005 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001006 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001007 struct lysc_ident **idref;
1008
1009 assert(ident || bases);
1010
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001011 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001012 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1013 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1014 return LY_EVALID;
1015 }
1016
1017 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1018 s = strchr(bases_p[u], ':');
1019 if (s) {
1020 /* prefixed identity */
1021 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +01001022 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001023 } else {
1024 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +01001025 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001026 }
1027 if (!mod) {
1028 if (ident) {
1029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1030 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1031 } else {
1032 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1033 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1034 }
1035 return LY_EVALID;
1036 }
1037 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001038 if (mod->compiled && mod->compiled->identities) {
1039 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1040 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001041 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001042 if (ident == &mod->compiled->identities[v]) {
1043 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1044 "Identity \"%s\" is derived from itself.", ident->name);
1045 return LY_EVALID;
1046 }
1047 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001048 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001049 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001050 *idref = ident;
1051 } else {
1052 /* we have match! store the found identity */
1053 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001054 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001055 }
1056 break;
1057 }
1058 }
1059 }
1060 if (!idref || !(*idref)) {
1061 if (ident) {
1062 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1063 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1064 } else {
1065 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1066 "Unable to find base (%s) of identityref.", bases_p[u]);
1067 }
1068 return LY_EVALID;
1069 }
1070 }
1071 return LY_SUCCESS;
1072}
1073
Radek Krejcia3045382018-11-22 14:30:31 +01001074/**
1075 * @brief For the given array of identities, set the backlinks from all their base identities.
1076 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1077 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1078 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1079 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1080 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001081static LY_ERR
1082lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1083{
1084 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +01001085
1086 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
1087 if (!idents_p[i].bases) {
1088 continue;
1089 }
Radek Krejci327de162019-06-14 12:52:07 +02001090 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001091 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001092 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001093 }
1094 return LY_SUCCESS;
1095}
1096
Radek Krejci0af46292019-01-11 16:02:31 +01001097LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001098lys_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 +01001099{
1100 unsigned int offset = 0, u;
1101 struct lysc_ctx context = {0};
1102
Radek Krejci327de162019-06-14 12:52:07 +02001103 assert(ctx_sc || ctx);
1104
1105 if (!ctx_sc) {
1106 context.ctx = ctx;
1107 context.mod = module;
1108 context.path_len = 1;
1109 context.path[0] = '/';
1110 ctx_sc = &context;
1111 }
Radek Krejci0af46292019-01-11 16:02:31 +01001112
1113 if (!features_p) {
1114 return LY_SUCCESS;
1115 }
1116 if (*features) {
1117 offset = LY_ARRAY_SIZE(*features);
1118 }
1119
Radek Krejci327de162019-06-14 12:52:07 +02001120 lysc_update_path(ctx_sc, NULL, "{feature}");
1121 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001122 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001123 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1124
Radek Krejci0af46292019-01-11 16:02:31 +01001125 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +02001126 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
1127 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1128 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1129 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001130 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001131 (*features)[offset + u].module = ctx_sc->mod;
1132
1133 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001134 }
Radek Krejci327de162019-06-14 12:52:07 +02001135 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001136
1137 return LY_SUCCESS;
1138}
1139
Radek Krejcia3045382018-11-22 14:30:31 +01001140/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001141 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001142 *
1143 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1144 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001145 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001146 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1147 * being currently processed).
1148 * @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 +01001149 * @return LY_SUCCESS if everything is ok.
1150 * @return LY_EVALID if the feature references indirectly itself.
1151 */
1152static LY_ERR
1153lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1154{
1155 LY_ERR ret = LY_EVALID;
1156 unsigned int u, v;
1157 struct ly_set recursion = {0};
1158 struct lysc_feature *drv;
1159
1160 if (!depfeatures) {
1161 return LY_SUCCESS;
1162 }
1163
1164 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1165 if (feature == depfeatures[u]) {
1166 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1167 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1168 goto cleanup;
1169 }
1170 ly_set_add(&recursion, depfeatures[u], 0);
1171 }
1172
1173 for (v = 0; v < recursion.count; ++v) {
1174 drv = recursion.objs[v];
1175 if (!drv->depfeatures) {
1176 continue;
1177 }
1178 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1179 if (feature == drv->depfeatures[u]) {
1180 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1181 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1182 goto cleanup;
1183 }
1184 ly_set_add(&recursion, drv->depfeatures[u], 0);
1185 }
1186 }
1187 ret = LY_SUCCESS;
1188
1189cleanup:
1190 ly_set_erase(&recursion, NULL);
1191 return ret;
1192}
1193
1194/**
Radek Krejci0af46292019-01-11 16:02:31 +01001195 * @brief Create pre-compiled features array.
1196 *
1197 * See lys_feature_precompile() for more details.
1198 *
Radek Krejcia3045382018-11-22 14:30:31 +01001199 * @param[in] ctx Compile context.
1200 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001201 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001202 * @return LY_ERR value.
1203 */
Radek Krejci19a96102018-11-15 13:38:09 +01001204static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001205lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001206{
Radek Krejci0af46292019-01-11 16:02:31 +01001207 unsigned int u, v, x;
1208 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001209 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001210
Radek Krejci327de162019-06-14 12:52:07 +02001211
Radek Krejci0af46292019-01-11 16:02:31 +01001212 /* find the preprecompiled feature */
1213 LY_ARRAY_FOR(features, x) {
1214 if (strcmp(features[x].name, feature_p->name)) {
1215 continue;
1216 }
1217 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001218 lysc_update_path(ctx, NULL, "{feature}");
1219 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001220
Radek Krejci0af46292019-01-11 16:02:31 +01001221 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001222 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001223 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001224 if (feature->iffeatures) {
1225 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1226 if (feature->iffeatures[u].features) {
1227 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001228 /* check for circular dependency - direct reference first,... */
1229 if (feature == feature->iffeatures[u].features[v]) {
1230 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1231 "Feature \"%s\" is referenced from itself.", feature->name);
1232 return LY_EVALID;
1233 }
1234 /* ... and indirect circular reference */
1235 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1236
Radek Krejci0af46292019-01-11 16:02:31 +01001237 /* add itself into the dependants list */
1238 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1239 *df = feature;
1240 }
Radek Krejci19a96102018-11-15 13:38:09 +01001241 }
Radek Krejci19a96102018-11-15 13:38:09 +01001242 }
1243 }
Radek Krejci327de162019-06-14 12:52:07 +02001244 lysc_update_path(ctx, NULL, NULL);
1245 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001246 done:
1247 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001248 }
Radek Krejci0af46292019-01-11 16:02:31 +01001249
1250 LOGINT(ctx->ctx);
1251 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001252}
1253
Radek Krejcib56c7502019-02-13 14:19:54 +01001254/**
1255 * @brief Revert compiled list of features back to the precompiled state.
1256 *
1257 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1258 * The features are supposed to be stored again as off_features in ::lys_module structure.
1259 *
1260 * @param[in] ctx Compilation context.
1261 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1262 * and supposed to hold the off_features list.
1263 */
Radek Krejci95710c92019-02-11 15:49:55 +01001264static void
1265lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1266{
1267 unsigned int u, v;
1268
1269 /* keep the off_features list until the complete lys_module is freed */
1270 mod->off_features = mod->compiled->features;
1271 mod->compiled->features = NULL;
1272
1273 /* in the off_features list, remove all the parts (from finished compiling process)
1274 * which may points into the data being freed here */
1275 LY_ARRAY_FOR(mod->off_features, u) {
1276 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1277 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1278 }
1279 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1280 mod->off_features[u].iffeatures = NULL;
1281
1282 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1283 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1284 }
1285 LY_ARRAY_FREE(mod->off_features[u].exts);
1286 mod->off_features[u].exts = NULL;
1287 }
1288}
1289
Radek Krejcia3045382018-11-22 14:30:31 +01001290/**
1291 * @brief Validate and normalize numeric value from a range definition.
1292 * @param[in] ctx Compile context.
1293 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1294 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1295 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1296 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1297 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1298 * @param[in] value String value of the range boundary.
1299 * @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.
1300 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1301 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1302 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001303LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001304range_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 +01001305{
Radek Krejci6cba4292018-11-15 17:33:29 +01001306 size_t fraction = 0, size;
1307
Radek Krejci19a96102018-11-15 13:38:09 +01001308 *len = 0;
1309
1310 assert(value);
1311 /* parse value */
1312 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1313 return LY_EVALID;
1314 }
1315
1316 if ((value[*len] == '-') || (value[*len] == '+')) {
1317 ++(*len);
1318 }
1319
1320 while (isdigit(value[*len])) {
1321 ++(*len);
1322 }
1323
1324 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001325 if (basetype == LY_TYPE_DEC64) {
1326 goto decimal;
1327 } else {
1328 *valcopy = strndup(value, *len);
1329 return LY_SUCCESS;
1330 }
Radek Krejci19a96102018-11-15 13:38:09 +01001331 }
1332 fraction = *len;
1333
1334 ++(*len);
1335 while (isdigit(value[*len])) {
1336 ++(*len);
1337 }
1338
Radek Krejci6cba4292018-11-15 17:33:29 +01001339 if (basetype == LY_TYPE_DEC64) {
1340decimal:
1341 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001342 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1344 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1345 *len, value, frdigits);
1346 return LY_EINVAL;
1347 }
1348 if (fraction) {
1349 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1350 } else {
1351 size = (*len) + frdigits + 1;
1352 }
1353 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001354 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1355
Radek Krejci6cba4292018-11-15 17:33:29 +01001356 (*valcopy)[size - 1] = '\0';
1357 if (fraction) {
1358 memcpy(&(*valcopy)[0], &value[0], fraction);
1359 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1360 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1361 } else {
1362 memcpy(&(*valcopy)[0], &value[0], *len);
1363 memset(&(*valcopy)[*len], '0', frdigits);
1364 }
Radek Krejci19a96102018-11-15 13:38:09 +01001365 }
1366 return LY_SUCCESS;
1367}
1368
Radek Krejcia3045382018-11-22 14:30:31 +01001369/**
1370 * @brief Check that values in range are in ascendant order.
1371 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001372 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1373 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001374 * @param[in] value Current value to check.
1375 * @param[in] prev_value The last seen value.
1376 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1377 */
Radek Krejci19a96102018-11-15 13:38:09 +01001378static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001379range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001380{
1381 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001382 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001383 return LY_EEXIST;
1384 }
1385 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001386 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001387 return LY_EEXIST;
1388 }
1389 }
1390 return LY_SUCCESS;
1391}
1392
Radek Krejcia3045382018-11-22 14:30:31 +01001393/**
1394 * @brief Set min/max value of the range part.
1395 * @param[in] ctx Compile context.
1396 * @param[in] part Range part structure to fill.
1397 * @param[in] max Flag to distinguish if storing min or max value.
1398 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1399 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1400 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1401 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1402 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001403 * @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 +01001404 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1405 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1406 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1407 * frdigits value), LY_EMEM.
1408 */
Radek Krejci19a96102018-11-15 13:38:09 +01001409static LY_ERR
1410range_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 +01001411 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001412{
1413 LY_ERR ret = LY_SUCCESS;
1414 char *valcopy = NULL;
1415 size_t len;
1416
1417 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001418 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001419 LY_CHECK_GOTO(ret, finalize);
1420 }
1421 if (!valcopy && base_range) {
1422 if (max) {
1423 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1424 } else {
1425 part->min_64 = base_range->parts[0].min_64;
1426 }
1427 if (!first) {
1428 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1429 }
1430 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001431 }
1432
1433 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001434 case LY_TYPE_INT8: /* range */
1435 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001436 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 +01001437 } else if (max) {
1438 part->max_64 = INT64_C(127);
1439 } else {
1440 part->min_64 = INT64_C(-128);
1441 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001442 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001443 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001444 }
1445 break;
1446 case LY_TYPE_INT16: /* range */
1447 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001448 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 +01001449 } else if (max) {
1450 part->max_64 = INT64_C(32767);
1451 } else {
1452 part->min_64 = INT64_C(-32768);
1453 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001454 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001455 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001456 }
1457 break;
1458 case LY_TYPE_INT32: /* range */
1459 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001460 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 +01001461 } else if (max) {
1462 part->max_64 = INT64_C(2147483647);
1463 } else {
1464 part->min_64 = INT64_C(-2147483648);
1465 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001466 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001467 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001468 }
1469 break;
1470 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001471 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001472 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001473 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001474 max ? &part->max_64 : &part->min_64);
1475 } else if (max) {
1476 part->max_64 = INT64_C(9223372036854775807);
1477 } else {
1478 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1479 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001480 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001481 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001482 }
1483 break;
1484 case LY_TYPE_UINT8: /* range */
1485 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001486 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 +01001487 } else if (max) {
1488 part->max_u64 = UINT64_C(255);
1489 } else {
1490 part->min_u64 = UINT64_C(0);
1491 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001492 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001493 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001494 }
1495 break;
1496 case LY_TYPE_UINT16: /* range */
1497 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001498 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 +01001499 } else if (max) {
1500 part->max_u64 = UINT64_C(65535);
1501 } else {
1502 part->min_u64 = UINT64_C(0);
1503 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001504 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001505 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001506 }
1507 break;
1508 case LY_TYPE_UINT32: /* range */
1509 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001510 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 +01001511 } else if (max) {
1512 part->max_u64 = UINT64_C(4294967295);
1513 } else {
1514 part->min_u64 = UINT64_C(0);
1515 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001516 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001517 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001518 }
1519 break;
1520 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001521 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001522 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001523 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001524 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 +01001525 } else if (max) {
1526 part->max_u64 = UINT64_C(18446744073709551615);
1527 } else {
1528 part->min_u64 = UINT64_C(0);
1529 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001530 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001531 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001532 }
1533 break;
1534 default:
1535 LOGINT(ctx->ctx);
1536 ret = LY_EINT;
1537 }
1538
Radek Krejci5969f272018-11-23 10:03:58 +01001539finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001540 if (ret == LY_EDENIED) {
1541 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1542 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1543 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1544 } else if (ret == LY_EVALID) {
1545 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1546 "Invalid %s restriction - invalid value \"%s\".",
1547 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1548 } else if (ret == LY_EEXIST) {
1549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1550 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001551 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001552 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001553 } else if (!ret && value) {
1554 *value = *value + len;
1555 }
1556 free(valcopy);
1557 return ret;
1558}
1559
Radek Krejcia3045382018-11-22 14:30:31 +01001560/**
1561 * @brief Compile the parsed range restriction.
1562 * @param[in] ctx Compile context.
1563 * @param[in] range_p Parsed range structure to compile.
1564 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1565 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1566 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1567 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1568 * range restriction must be more restrictive than the base_range.
1569 * @param[in,out] range Pointer to the created current range structure.
1570 * @return LY_ERR value.
1571 */
Radek Krejci19a96102018-11-15 13:38:09 +01001572static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001573lys_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 +01001574 struct lysc_range *base_range, struct lysc_range **range)
1575{
1576 LY_ERR ret = LY_EVALID;
1577 const char *expr;
1578 struct lysc_range_part *parts = NULL, *part;
1579 int range_expected = 0, uns;
1580 unsigned int parts_done = 0, u, v;
1581
1582 assert(range);
1583 assert(range_p);
1584
1585 expr = range_p->arg;
1586 while(1) {
1587 if (isspace(*expr)) {
1588 ++expr;
1589 } else if (*expr == '\0') {
1590 if (range_expected) {
1591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1592 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1593 length_restr ? "length" : "range", range_p->arg);
1594 goto cleanup;
1595 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1596 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1597 "Invalid %s restriction - unexpected end of the expression (%s).",
1598 length_restr ? "length" : "range", range_p->arg);
1599 goto cleanup;
1600 }
1601 parts_done++;
1602 break;
1603 } else if (!strncmp(expr, "min", 3)) {
1604 if (parts) {
1605 /* min cannot be used elsewhere than in the first part */
1606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1607 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1608 expr - range_p->arg, range_p->arg);
1609 goto cleanup;
1610 }
1611 expr += 3;
1612
1613 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001614 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 +01001615 part->max_64 = part->min_64;
1616 } else if (*expr == '|') {
1617 if (!parts || range_expected) {
1618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1619 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1620 goto cleanup;
1621 }
1622 expr++;
1623 parts_done++;
1624 /* process next part of the expression */
1625 } else if (!strncmp(expr, "..", 2)) {
1626 expr += 2;
1627 while (isspace(*expr)) {
1628 expr++;
1629 }
1630 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1631 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1632 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1633 goto cleanup;
1634 }
1635 /* continue expecting the upper boundary */
1636 range_expected = 1;
1637 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1638 /* number */
1639 if (range_expected) {
1640 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001641 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 +01001642 range_expected = 0;
1643 } else {
1644 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1645 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 +01001646 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001647 part->max_64 = part->min_64;
1648 }
1649
1650 /* continue with possible another expression part */
1651 } else if (!strncmp(expr, "max", 3)) {
1652 expr += 3;
1653 while (isspace(*expr)) {
1654 expr++;
1655 }
1656 if (*expr != '\0') {
1657 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1658 length_restr ? "length" : "range", expr);
1659 goto cleanup;
1660 }
1661 if (range_expected) {
1662 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001663 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 +01001664 range_expected = 0;
1665 } else {
1666 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1667 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 +01001668 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001669 part->min_64 = part->max_64;
1670 }
1671 } else {
1672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1673 length_restr ? "length" : "range", expr);
1674 goto cleanup;
1675 }
1676 }
1677
1678 /* check with the previous range/length restriction */
1679 if (base_range) {
1680 switch (basetype) {
1681 case LY_TYPE_BINARY:
1682 case LY_TYPE_UINT8:
1683 case LY_TYPE_UINT16:
1684 case LY_TYPE_UINT32:
1685 case LY_TYPE_UINT64:
1686 case LY_TYPE_STRING:
1687 uns = 1;
1688 break;
1689 case LY_TYPE_DEC64:
1690 case LY_TYPE_INT8:
1691 case LY_TYPE_INT16:
1692 case LY_TYPE_INT32:
1693 case LY_TYPE_INT64:
1694 uns = 0;
1695 break;
1696 default:
1697 LOGINT(ctx->ctx);
1698 ret = LY_EINT;
1699 goto cleanup;
1700 }
1701 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1702 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1703 goto baseerror;
1704 }
1705 /* current lower bound is not lower than the base */
1706 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1707 /* base has single value */
1708 if (base_range->parts[v].min_64 == parts[u].min_64) {
1709 /* both lower bounds are the same */
1710 if (parts[u].min_64 != parts[u].max_64) {
1711 /* current continues with a range */
1712 goto baseerror;
1713 } else {
1714 /* equal single values, move both forward */
1715 ++v;
1716 continue;
1717 }
1718 } else {
1719 /* base is single value lower than current range, so the
1720 * value from base range is removed in the current,
1721 * move only base and repeat checking */
1722 ++v;
1723 --u;
1724 continue;
1725 }
1726 } else {
1727 /* base is the range */
1728 if (parts[u].min_64 == parts[u].max_64) {
1729 /* current is a single value */
1730 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1731 /* current is behind the base range, so base range is omitted,
1732 * move the base and keep the current for further check */
1733 ++v;
1734 --u;
1735 } /* else it is within the base range, so move the current, but keep the base */
1736 continue;
1737 } else {
1738 /* both are ranges - check the higher bound, the lower was already checked */
1739 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1740 /* higher bound is higher than the current higher bound */
1741 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1742 /* but the current lower bound is also higher, so the base range is omitted,
1743 * continue with the same current, but move the base */
1744 --u;
1745 ++v;
1746 continue;
1747 }
1748 /* current range starts within the base range but end behind it */
1749 goto baseerror;
1750 } else {
1751 /* current range is smaller than the base,
1752 * move current, but stay with the base */
1753 continue;
1754 }
1755 }
1756 }
1757 }
1758 if (u != parts_done) {
1759baseerror:
1760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1761 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1762 length_restr ? "length" : "range", range_p->arg);
1763 goto cleanup;
1764 }
1765 }
1766
1767 if (!(*range)) {
1768 *range = calloc(1, sizeof **range);
1769 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1770 }
1771
Radek Krejcic8b31002019-01-08 10:24:45 +01001772 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001773 if (range_p->eapptag) {
1774 lydict_remove(ctx->ctx, (*range)->eapptag);
1775 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1776 }
1777 if (range_p->emsg) {
1778 lydict_remove(ctx->ctx, (*range)->emsg);
1779 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1780 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001781 if (range_p->dsc) {
1782 lydict_remove(ctx->ctx, (*range)->dsc);
1783 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1784 }
1785 if (range_p->ref) {
1786 lydict_remove(ctx->ctx, (*range)->ref);
1787 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1788 }
Radek Krejci19a96102018-11-15 13:38:09 +01001789 /* extensions are taken only from the last range by the caller */
1790
1791 (*range)->parts = parts;
1792 parts = NULL;
1793 ret = LY_SUCCESS;
1794cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001795 LY_ARRAY_FREE(parts);
1796
1797 return ret;
1798}
1799
1800/**
1801 * @brief Checks pattern syntax.
1802 *
Radek Krejcia3045382018-11-22 14:30:31 +01001803 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001804 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001805 * @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 +01001806 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001807 */
1808static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001809lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001810{
Radek Krejci54579462019-04-30 12:47:06 +02001811 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001812 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001813 int err_code;
1814 const char *orig_ptr;
1815 PCRE2_SIZE err_offset;
1816 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001817#define URANGE_LEN 19
1818 char *ublock2urange[][2] = {
1819 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1820 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1821 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1822 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1823 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1824 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1825 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1826 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1827 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1828 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1829 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1830 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1831 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1832 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1833 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1834 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1835 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1836 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1837 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1838 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1839 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1840 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1841 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1842 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1843 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1844 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1845 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1846 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1847 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1848 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1849 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1850 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1851 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1852 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1853 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1854 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1855 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1856 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1857 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1858 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1859 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1860 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1861 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1862 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1863 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1864 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1865 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1866 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1867 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1868 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1869 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1870 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1871 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1872 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1873 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1874 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1875 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1876 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1877 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1878 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1879 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1880 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1881 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1882 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1883 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1884 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1885 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1886 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1887 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1888 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1889 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1890 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1891 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1892 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1893 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1894 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1895 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1896 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1897 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1898 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1899 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1900 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1901 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1902 {NULL, NULL}
1903 };
1904
1905 /* adjust the expression to a Perl equivalent
1906 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1907
1908 /* we need to replace all "$" with "\$", count them now */
1909 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1910
1911 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1912 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1913 perl_regex[0] = '\0';
1914
1915 ptr = perl_regex;
1916
Radek Krejci19a96102018-11-15 13:38:09 +01001917 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1918 if (orig_ptr[0] == '$') {
1919 ptr += sprintf(ptr, "\\$");
1920 } else if (orig_ptr[0] == '^') {
1921 ptr += sprintf(ptr, "\\^");
1922 } else {
1923 ptr[0] = orig_ptr[0];
1924 ++ptr;
1925 }
1926 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001927 ptr[0] = '\0';
1928 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001929
1930 /* substitute Unicode Character Blocks with exact Character Ranges */
1931 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1932 start = ptr - perl_regex;
1933
1934 ptr = strchr(ptr, '}');
1935 if (!ptr) {
1936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1937 pattern, perl_regex + start + 2, "unterminated character property");
1938 free(perl_regex);
1939 return LY_EVALID;
1940 }
1941 end = (ptr - perl_regex) + 1;
1942
1943 /* need more space */
1944 if (end - start < URANGE_LEN) {
1945 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1946 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1947 }
1948
1949 /* find our range */
1950 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1951 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1952 break;
1953 }
1954 }
1955 if (!ublock2urange[idx][0]) {
1956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1957 pattern, perl_regex + start + 5, "unknown block name");
1958 free(perl_regex);
1959 return LY_EVALID;
1960 }
1961
1962 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1963 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1964 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1965 ++count;
1966 }
1967 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1968 --count;
1969 }
1970 }
1971 if (count) {
1972 /* skip brackets */
1973 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1974 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1975 } else {
1976 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1977 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1978 }
1979 }
1980
1981 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02001982 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1983 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02001984 &err_code, &err_offset, NULL);
1985 if (!code_local) {
1986 PCRE2_UCHAR err_msg[256] = {0};
1987 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1989 free(perl_regex);
1990 return LY_EVALID;
1991 }
1992 free(perl_regex);
1993
Radek Krejci54579462019-04-30 12:47:06 +02001994 if (code) {
1995 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001996 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001997 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001998 }
1999
2000 return LY_SUCCESS;
2001
2002#undef URANGE_LEN
2003}
2004
Radek Krejcia3045382018-11-22 14:30:31 +01002005/**
2006 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2007 * @param[in] ctx Compile context.
2008 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002009 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2010 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2011 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2012 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2013 */
Radek Krejci19a96102018-11-15 13:38:09 +01002014static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002015lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002016 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2017{
2018 struct lysc_pattern **pattern;
Radek Krejci0935f412019-08-20 16:15:18 +02002019 unsigned int u;
Radek Krejci19a96102018-11-15 13:38:09 +01002020 LY_ERR ret = LY_SUCCESS;
2021
2022 /* first, copy the patterns from the base type */
2023 if (base_patterns) {
2024 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2025 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2026 }
2027
2028 LY_ARRAY_FOR(patterns_p, u) {
2029 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2030 *pattern = calloc(1, sizeof **pattern);
2031 ++(*pattern)->refcount;
2032
Radek Krejci54579462019-04-30 12:47:06 +02002033 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002034 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002035
2036 if (patterns_p[u].arg[0] == 0x15) {
2037 (*pattern)->inverted = 1;
2038 }
Radek Krejci54579462019-04-30 12:47:06 +02002039 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002040 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2041 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002042 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2043 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002044 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002045 }
2046done:
2047 return ret;
2048}
2049
Radek Krejcia3045382018-11-22 14:30:31 +01002050/**
2051 * @brief map of the possible restrictions combination for the specific built-in type.
2052 */
Radek Krejci19a96102018-11-15 13:38:09 +01002053static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2054 0 /* LY_TYPE_UNKNOWN */,
2055 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002056 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2057 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2058 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2059 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2060 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002061 LYS_SET_BIT /* LY_TYPE_BITS */,
2062 0 /* LY_TYPE_BOOL */,
2063 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2064 0 /* LY_TYPE_EMPTY */,
2065 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2066 LYS_SET_BASE /* LY_TYPE_IDENT */,
2067 LYS_SET_REQINST /* LY_TYPE_INST */,
2068 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002069 LYS_SET_TYPE /* LY_TYPE_UNION */,
2070 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002071 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002072 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002073 LYS_SET_RANGE /* LY_TYPE_INT64 */
2074};
2075
2076/**
2077 * @brief stringification of the YANG built-in data types
2078 */
2079const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2080 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2081 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002082};
2083
Radek Krejcia3045382018-11-22 14:30:31 +01002084/**
2085 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2086 * @param[in] ctx Compile context.
2087 * @param[in] enums_p Array of the parsed enum structures to compile.
2088 * @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 +01002089 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2090 * @param[out] enums Newly created array of the compiled enums information for the current type.
2091 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2092 */
Radek Krejci19a96102018-11-15 13:38:09 +01002093static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002094lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002095 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002096{
2097 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02002098 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002099 int32_t value = 0;
2100 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002101 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002102
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002103 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002104 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2105 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2106 return LY_EVALID;
2107 }
2108
2109 LY_ARRAY_FOR(enums_p, u) {
2110 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2111 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002112 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2113 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002114 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002115 if (base_enums) {
2116 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2117 LY_ARRAY_FOR(base_enums, v) {
2118 if (!strcmp(e->name, base_enums[v].name)) {
2119 break;
2120 }
2121 }
2122 if (v == LY_ARRAY_SIZE(base_enums)) {
2123 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2124 "Invalid %s - derived type adds new item \"%s\".",
2125 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2126 return LY_EVALID;
2127 }
2128 match = v;
2129 }
2130
2131 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002132 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002133 if (enums_p[u].flags & LYS_SET_VALUE) {
2134 e->value = (int32_t)enums_p[u].value;
2135 if (!u || e->value >= value) {
2136 value = e->value + 1;
2137 }
2138 /* check collision with other values */
2139 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2140 if (e->value == (*enums)[v].value) {
2141 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2142 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2143 e->value, e->name, (*enums)[v].name);
2144 return LY_EVALID;
2145 }
2146 }
2147 } else if (base_enums) {
2148 /* inherit the assigned value */
2149 e->value = base_enums[match].value;
2150 if (!u || e->value >= value) {
2151 value = e->value + 1;
2152 }
2153 } else {
2154 /* assign value automatically */
2155 if (u && value == INT32_MIN) {
2156 /* counter overflow */
2157 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2158 "Invalid enumeration - it is not possible to auto-assign enum value for "
2159 "\"%s\" since the highest value is already 2147483647.", e->name);
2160 return LY_EVALID;
2161 }
2162 e->value = value++;
2163 }
2164 } else { /* LY_TYPE_BITS */
2165 if (enums_p[u].flags & LYS_SET_VALUE) {
2166 e->value = (int32_t)enums_p[u].value;
2167 if (!u || (uint32_t)e->value >= position) {
2168 position = (uint32_t)e->value + 1;
2169 }
2170 /* check collision with other values */
2171 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2172 if (e->value == (*enums)[v].value) {
2173 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2174 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2175 (uint32_t)e->value, e->name, (*enums)[v].name);
2176 return LY_EVALID;
2177 }
2178 }
2179 } else if (base_enums) {
2180 /* inherit the assigned value */
2181 e->value = base_enums[match].value;
2182 if (!u || (uint32_t)e->value >= position) {
2183 position = (uint32_t)e->value + 1;
2184 }
2185 } else {
2186 /* assign value automatically */
2187 if (u && position == 0) {
2188 /* counter overflow */
2189 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2190 "Invalid bits - it is not possible to auto-assign bit position for "
2191 "\"%s\" since the highest value is already 4294967295.", e->name);
2192 return LY_EVALID;
2193 }
2194 e->value = position++;
2195 }
2196 }
2197
2198 if (base_enums) {
2199 /* the assigned values must not change from the derived type */
2200 if (e->value != base_enums[match].value) {
2201 if (basetype == LY_TYPE_ENUM) {
2202 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2203 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2204 e->name, base_enums[match].value, e->value);
2205 } else {
2206 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2207 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2208 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2209 }
2210 return LY_EVALID;
2211 }
2212 }
2213
Radek Krejciec4da802019-05-02 13:02:41 +02002214 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002215 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 +01002216
2217 if (basetype == LY_TYPE_BITS) {
2218 /* keep bits ordered by position */
2219 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2220 if (v != u) {
2221 memcpy(&storage, e, sizeof *e);
2222 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2223 memcpy(&(*enums)[v], &storage, sizeof storage);
2224 }
2225 }
2226 }
2227
2228done:
2229 return ret;
2230}
2231
Radek Krejcia3045382018-11-22 14:30:31 +01002232#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2233 for ((NODE) = (NODE)->parent; \
2234 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
2235 (NODE) = (NODE)->parent); \
2236 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2237 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2238 TERM; \
2239 }
2240
2241/**
2242 * @brief Validate the predicate(s) from the leafref path.
2243 * @param[in] ctx Compile context
2244 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2245 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002246 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002247 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002248 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002249 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2250 */
2251static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002252lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002253 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002254{
2255 LY_ERR ret = LY_EVALID;
2256 const struct lys_module *mod;
2257 const struct lysc_node *src_node, *dst_node;
2258 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2259 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002260 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002261 const char *start, *end, *pke_end;
2262 struct ly_set keys = {0};
2263 int i;
2264
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002265 assert(path_context);
2266
Radek Krejcia3045382018-11-22 14:30:31 +01002267 while (**predicate == '[') {
2268 start = (*predicate)++;
2269
2270 while (isspace(**predicate)) {
2271 ++(*predicate);
2272 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002273 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002274 while (isspace(**predicate)) {
2275 ++(*predicate);
2276 }
2277 if (**predicate != '=') {
2278 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002279 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2280 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002281 goto cleanup;
2282 }
2283 ++(*predicate);
2284 while (isspace(**predicate)) {
2285 ++(*predicate);
2286 }
2287
2288 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2290 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2291 goto cleanup;
2292 }
2293 --pke_end;
2294 while (isspace(*pke_end)) {
2295 --pke_end;
2296 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002297 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002298 /* localize path-key-expr */
2299 pke_start = path_key_expr = *predicate;
2300 /* move after the current predicate */
2301 *predicate = end + 1;
2302
2303 /* source (must be leaf or leaf-list) */
2304 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002305 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002306 if (!mod) {
2307 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2308 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002309 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002310 goto cleanup;
2311 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002312 if (!mod->implemented) {
2313 /* make the module implemented */
2314 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2315 }
Radek Krejcia3045382018-11-22 14:30:31 +01002316 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002317 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002318 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002319 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002320 if (!(context_node->flags & LYS_KEYLESS)) {
2321 struct lysc_node *key;
2322 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
2323 if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') {
2324 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002325 break;
2326 }
2327 }
2328 }
Radek Krejcia3045382018-11-22 14:30:31 +01002329 if (!src_node) {
2330 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002331 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002332 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002333 goto cleanup;
2334 }
Radek Krejcia3045382018-11-22 14:30:31 +01002335
2336 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002337 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002338 i = ly_set_add(&keys, (void*)src_node, 0);
2339 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002340 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002342 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002343 *predicate - start, start, src_node->name);
2344 goto cleanup;
2345 }
2346
2347 /* destination */
2348 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002349 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002350
2351 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002352 if (strncmp(path_key_expr, "current", 7)) {
2353error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002354 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2355 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2356 *predicate - start, start);
2357 goto cleanup;
2358 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002359 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2360 if (*path_key_expr != '(') {
2361 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002362 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002363 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2364 if (*path_key_expr != ')') {
2365 goto error_current_function_invocation;
2366 }
2367 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002368
2369 if (*path_key_expr != '/') {
2370 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2371 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2372 *predicate - start, start);
2373 goto cleanup;
2374 }
2375 ++path_key_expr;
2376 while (isspace(*path_key_expr)) {
2377 ++path_key_expr;
2378 }
2379
2380 /* rel-path-keyexpr:
2381 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2382 while (!strncmp(path_key_expr, "..", 2)) {
2383 ++dest_parent_times;
2384 path_key_expr += 2;
2385 while (isspace(*path_key_expr)) {
2386 ++path_key_expr;
2387 }
2388 if (*path_key_expr != '/') {
2389 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2390 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2391 *predicate - start, start);
2392 goto cleanup;
2393 }
2394 ++path_key_expr;
2395 while (isspace(*path_key_expr)) {
2396 ++path_key_expr;
2397 }
2398
2399 /* path is supposed to be evaluated in data tree, so we have to skip
2400 * all schema nodes that cannot be instantiated in data tree */
2401 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2402 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2403 *predicate - start, start);
2404 }
2405 if (!dest_parent_times) {
2406 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2407 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2408 *predicate - start, start);
2409 goto cleanup;
2410 }
2411 if (path_key_expr == pke_end) {
2412 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2413 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2414 *predicate - start, start);
2415 goto cleanup;
2416 }
2417
2418 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002419 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002420 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002422 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2423 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002424 goto cleanup;
2425 }
2426
2427 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002428 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002429 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002430 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002431 }
2432 if (!mod) {
2433 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002434 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002435 *predicate - start, start, dst_len, dst);
2436 goto cleanup;
2437 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002438 if (!mod->implemented) {
2439 /* make the module implemented */
2440 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2441 }
Radek Krejcia3045382018-11-22 14:30:31 +01002442
2443 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2444 if (!dst_node) {
2445 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002446 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002447 *predicate - start, start, path_key_expr - pke_start, pke_start);
2448 goto cleanup;
2449 }
2450 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002451 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 +01002452 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002453 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002454 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2455 goto cleanup;
2456 }
2457 }
2458
2459 ret = LY_SUCCESS;
2460cleanup:
2461 ly_set_erase(&keys, NULL);
2462 return ret;
2463}
2464
2465/**
2466 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2467 *
2468 * path-arg = absolute-path / relative-path
2469 * absolute-path = 1*("/" (node-identifier *path-predicate))
2470 * relative-path = 1*(".." "/") descendant-path
2471 *
2472 * @param[in,out] path Path to parse.
2473 * @param[out] prefix Prefix of the token, NULL if there is not any.
2474 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2475 * @param[out] name Name of the token.
2476 * @param[out] nam_len Length of the name.
2477 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2478 * must not be changed between consecutive calls. -1 if the
2479 * path is absolute.
2480 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2481 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2482 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002483LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002484lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2485 int *parent_times, int *has_predicate)
2486{
2487 int par_times = 0;
2488
2489 assert(path && *path);
2490 assert(parent_times);
2491 assert(prefix);
2492 assert(prefix_len);
2493 assert(name);
2494 assert(name_len);
2495 assert(has_predicate);
2496
2497 *prefix = NULL;
2498 *prefix_len = 0;
2499 *name = NULL;
2500 *name_len = 0;
2501 *has_predicate = 0;
2502
2503 if (!*parent_times) {
2504 if (!strncmp(*path, "..", 2)) {
2505 *path += 2;
2506 ++par_times;
2507 while (!strncmp(*path, "/..", 3)) {
2508 *path += 3;
2509 ++par_times;
2510 }
2511 }
2512 if (par_times) {
2513 *parent_times = par_times;
2514 } else {
2515 *parent_times = -1;
2516 }
2517 }
2518
2519 if (**path != '/') {
2520 return LY_EINVAL;
2521 }
2522 /* skip '/' */
2523 ++(*path);
2524
2525 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002526 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002527
2528 if ((**path == '/' && (*path)[1]) || !**path) {
2529 /* path continues by another token or this is the last token */
2530 return LY_SUCCESS;
2531 } else if ((*path)[0] != '[') {
2532 /* unexpected character */
2533 return LY_EINVAL;
2534 } else {
2535 /* predicate starting with [ */
2536 *has_predicate = 1;
2537 return LY_SUCCESS;
2538 }
2539}
2540
2541/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002542 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2543 *
2544 * The set of features used for target must be a subset of features used for the leafref.
2545 * This is not a perfect, we should compare the truth tables but it could require too much resources
2546 * and RFC 7950 does not require it explicitely, so we simplify that.
2547 *
2548 * @param[in] refnode The leafref node.
2549 * @param[in] target Tha target node of the leafref.
2550 * @return LY_SUCCESS or LY_EVALID;
2551 */
2552static LY_ERR
2553lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2554{
2555 LY_ERR ret = LY_EVALID;
2556 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002557 unsigned int u, v, count;
2558 struct ly_set features = {0};
2559
2560 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002561 if (iter->iffeatures) {
2562 LY_ARRAY_FOR(iter->iffeatures, u) {
2563 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2564 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002565 }
2566 }
2567 }
2568 }
2569
2570 /* we should have, in features set, a superset of features applicable to the target node.
2571 * So when adding features applicable to the target into the features set, we should not be
2572 * able to actually add any new feature, otherwise it is not a subset of features applicable
2573 * to the leafref itself. */
2574 count = features.count;
2575 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002576 if (iter->iffeatures) {
2577 LY_ARRAY_FOR(iter->iffeatures, u) {
2578 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2579 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002580 /* new feature was added (or LY_EMEM) */
2581 goto cleanup;
2582 }
2583 }
2584 }
2585 }
2586 }
2587 ret = LY_SUCCESS;
2588
2589cleanup:
2590 ly_set_erase(&features, NULL);
2591 return ret;
2592}
2593
2594/**
Radek Krejcia3045382018-11-22 14:30:31 +01002595 * @brief Validate the leafref path.
2596 * @param[in] ctx Compile context
2597 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002598 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002599 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2600 */
2601static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002602lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002603{
2604 const struct lysc_node *node = NULL, *parent = NULL;
2605 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002606 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002607 const char *id, *prefix, *name;
2608 size_t prefix_len, name_len;
2609 int parent_times = 0, has_predicate;
2610 unsigned int iter, u;
2611 LY_ERR ret = LY_SUCCESS;
2612
2613 assert(ctx);
2614 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002615 assert(leafref);
2616
Radek Krejci1c0c3442019-07-23 16:08:47 +02002617 ctx->path[0] = '\0';
2618 lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
2619 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002620
Radek Krejcia3045382018-11-22 14:30:31 +01002621 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002622 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002623 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2624 if (!iter) { /* first iteration */
2625 /* precess ".." in relative paths */
2626 if (parent_times > 0) {
2627 /* move from the context node */
2628 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2629 /* path is supposed to be evaluated in data tree, so we have to skip
2630 * all schema nodes that cannot be instantiated in data tree */
2631 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002632 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002633 }
2634 }
2635 }
2636
2637 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002638 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002639 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002640 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002641 }
2642 if (!mod) {
2643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002644 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2645 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002646 return LY_EVALID;
2647 }
Radek Krejci3d929562019-02-21 11:25:55 +01002648 if (!mod->implemented) {
2649 /* make the module implemented */
2650 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2651 }
Radek Krejcia3045382018-11-22 14:30:31 +01002652
2653 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2654 if (!node) {
2655 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002656 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002657 return LY_EVALID;
2658 }
2659 parent = node;
2660
2661 if (has_predicate) {
2662 /* we have predicate, so the current result must be list */
2663 if (node->nodetype != LYS_LIST) {
2664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2665 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002666 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002667 return LY_EVALID;
2668 }
2669
Radek Krejcibade82a2018-12-05 10:13:48 +01002670 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2671 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002672 }
2673
2674 ++iter;
2675 }
2676 if (ret) {
2677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002678 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002679 return LY_EVALID;
2680 }
2681
2682 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2684 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002685 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002686 return LY_EVALID;
2687 }
2688
2689 /* check status */
2690 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2691 return LY_EVALID;
2692 }
2693
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002694 /* check config */
2695 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2696 if (node->flags & LYS_CONFIG_R) {
2697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2698 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2699 leafref->path);
2700 return LY_EVALID;
2701 }
2702 }
2703
Radek Krejci412ddfa2018-11-23 11:44:11 +01002704 /* store the target's type and check for circular chain of leafrefs */
2705 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2706 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2707 if (type == (struct lysc_type*)leafref) {
2708 /* circular chain detected */
2709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2710 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2711 return LY_EVALID;
2712 }
2713 }
2714
Radek Krejci58d171e2018-11-23 13:50:55 +01002715 /* check if leafref and its target are under common if-features */
2716 if (lys_compile_leafref_features_validate(startnode, node)) {
2717 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2718 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2719 leafref->path);
2720 return LY_EVALID;
2721 }
2722
Radek Krejci327de162019-06-14 12:52:07 +02002723 ctx->path_len = 1;
2724 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002725 return LY_SUCCESS;
2726}
2727
Radek Krejcicdfecd92018-11-26 11:27:32 +01002728static 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 +02002729 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002730/**
2731 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2732 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002733 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2734 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2735 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2736 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002737 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002738 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002739 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002740 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2741 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2742 * @param[out] type Newly created type structure with the filled information about the type.
2743 * @return LY_ERR value.
2744 */
Radek Krejci19a96102018-11-15 13:38:09 +01002745static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002746lys_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 +02002747 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002748 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002749{
2750 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002751 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002752 struct lysc_type_bin *bin;
2753 struct lysc_type_num *num;
2754 struct lysc_type_str *str;
2755 struct lysc_type_bits *bits;
2756 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002757 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002758 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002759 struct lysc_type_union *un, *un_aux;
2760 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002761
2762 switch (basetype) {
2763 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002764 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002765
2766 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002767 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002768 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2769 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002770 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002771 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 +01002772 }
2773 }
2774
2775 if (tpdfname) {
2776 type_p->compiled = *type;
2777 *type = calloc(1, sizeof(struct lysc_type_bin));
2778 }
2779 break;
2780 case LY_TYPE_BITS:
2781 /* RFC 7950 9.7 - bits */
2782 bits = (struct lysc_type_bits*)(*type);
2783 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002784 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002785 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2786 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002787 }
2788
Radek Krejci555cb5b2018-11-16 14:54:33 +01002789 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002790 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002791 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002792 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002793 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002794 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002795 free(*type);
2796 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002797 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002798 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002799 }
2800
2801 if (tpdfname) {
2802 type_p->compiled = *type;
2803 *type = calloc(1, sizeof(struct lysc_type_bits));
2804 }
2805 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002806 case LY_TYPE_DEC64:
2807 dec = (struct lysc_type_dec*)(*type);
2808
2809 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002810 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002811 if (!type_p->fraction_digits) {
2812 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002813 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002814 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002815 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002816 free(*type);
2817 *type = NULL;
2818 }
2819 return LY_EVALID;
2820 }
2821 } else if (type_p->fraction_digits) {
2822 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002823 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002824 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2825 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002826 tpdfname);
2827 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002828 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2829 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002830 free(*type);
2831 *type = NULL;
2832 }
2833 return LY_EVALID;
2834 }
2835 dec->fraction_digits = type_p->fraction_digits;
2836
2837 /* RFC 7950 9.2.4 - range */
2838 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002839 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2840 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002841 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002842 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 +01002843 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002844 }
2845
2846 if (tpdfname) {
2847 type_p->compiled = *type;
2848 *type = calloc(1, sizeof(struct lysc_type_dec));
2849 }
2850 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002851 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002852 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002853
2854 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002855 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002856 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2857 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002858 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002859 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 +01002860 }
2861 } else if (base && ((struct lysc_type_str*)base)->length) {
2862 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2863 }
2864
2865 /* RFC 7950 9.4.5 - pattern */
2866 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002867 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002868 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002869 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2870 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2871 }
2872
2873 if (tpdfname) {
2874 type_p->compiled = *type;
2875 *type = calloc(1, sizeof(struct lysc_type_str));
2876 }
2877 break;
2878 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002879 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002880
2881 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002882 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002883 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002884 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002885 }
2886
Radek Krejci555cb5b2018-11-16 14:54:33 +01002887 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002888 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002889 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002891 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002893 free(*type);
2894 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002895 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002896 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002897 }
2898
2899 if (tpdfname) {
2900 type_p->compiled = *type;
2901 *type = calloc(1, sizeof(struct lysc_type_enum));
2902 }
2903 break;
2904 case LY_TYPE_INT8:
2905 case LY_TYPE_UINT8:
2906 case LY_TYPE_INT16:
2907 case LY_TYPE_UINT16:
2908 case LY_TYPE_INT32:
2909 case LY_TYPE_UINT32:
2910 case LY_TYPE_INT64:
2911 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002912 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002913
2914 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002915 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002916 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2917 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002918 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002919 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 +01002920 }
2921 }
2922
2923 if (tpdfname) {
2924 type_p->compiled = *type;
2925 *type = calloc(1, sizeof(struct lysc_type_num));
2926 }
2927 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002928 case LY_TYPE_IDENT:
2929 idref = (struct lysc_type_identityref*)(*type);
2930
2931 /* RFC 7950 9.10.2 - base */
2932 if (type_p->bases) {
2933 if (base) {
2934 /* only the directly derived identityrefs can contain base specification */
2935 if (tpdfname) {
2936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002937 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002938 tpdfname);
2939 } else {
2940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002941 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002942 free(*type);
2943 *type = NULL;
2944 }
2945 return LY_EVALID;
2946 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002947 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002948 }
2949
2950 if (!base && !type_p->flags) {
2951 /* type derived from identityref built-in type must contain at least one base */
2952 if (tpdfname) {
2953 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2954 } else {
2955 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2956 free(*type);
2957 *type = NULL;
2958 }
2959 return LY_EVALID;
2960 }
2961
2962 if (tpdfname) {
2963 type_p->compiled = *type;
2964 *type = calloc(1, sizeof(struct lysc_type_identityref));
2965 }
2966 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002967 case LY_TYPE_LEAFREF:
2968 /* RFC 7950 9.9.3 - require-instance */
2969 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002970 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002971 if (tpdfname) {
2972 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2973 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2974 } else {
2975 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2976 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2977 free(*type);
2978 *type = NULL;
2979 }
2980 return LY_EVALID;
2981 }
Radek Krejcia3045382018-11-22 14:30:31 +01002982 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002983 } else if (base) {
2984 /* inherit */
2985 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002986 } else {
2987 /* default is true */
2988 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2989 }
2990 if (type_p->path) {
2991 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002992 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002993 } else if (base) {
2994 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002995 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002996 } else if (tpdfname) {
2997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2998 return LY_EVALID;
2999 } else {
3000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
3001 free(*type);
3002 *type = NULL;
3003 return LY_EVALID;
3004 }
3005 if (tpdfname) {
3006 type_p->compiled = *type;
3007 *type = calloc(1, sizeof(struct lysc_type_leafref));
3008 }
3009 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01003010 case LY_TYPE_INST:
3011 /* RFC 7950 9.9.3 - require-instance */
3012 if (type_p->flags & LYS_SET_REQINST) {
3013 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
3014 } else {
3015 /* default is true */
3016 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
3017 }
3018
3019 if (tpdfname) {
3020 type_p->compiled = *type;
3021 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3022 }
3023 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003024 case LY_TYPE_UNION:
3025 un = (struct lysc_type_union*)(*type);
3026
3027 /* RFC 7950 7.4 - type */
3028 if (type_p->types) {
3029 if (base) {
3030 /* only the directly derived union can contain types specification */
3031 if (tpdfname) {
3032 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3033 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
3034 tpdfname);
3035 } else {
3036 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3037 "Invalid type substatement for the type not directly derived from union built-in type.");
3038 free(*type);
3039 *type = NULL;
3040 }
3041 return LY_EVALID;
3042 }
3043 /* compile the type */
3044 additional = 0;
3045 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
3046 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02003047 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 +01003048 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
3049 /* add space for additional types from the union subtype */
3050 un_aux = (struct lysc_type_union *)un->types[u + additional];
3051 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)));
3052 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3053 un->types = (void*)((uint32_t*)(p) + 1);
3054
3055 /* copy subtypes of the subtype union */
3056 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
3057 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
3058 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
3059 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
3060 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3061 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
3062 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
3063 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3064 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3065 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3066 /* TODO extensions */
3067
3068 } else {
3069 un->types[u + additional] = un_aux->types[v];
3070 ++un_aux->types[v]->refcount;
3071 }
3072 ++additional;
3073 LY_ARRAY_INCREMENT(un->types);
3074 }
3075 /* compensate u increment in main loop */
3076 --additional;
3077
3078 /* free the replaced union subtype */
3079 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3080 } else {
3081 LY_ARRAY_INCREMENT(un->types);
3082 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003083 }
3084 }
3085
3086 if (!base && !type_p->flags) {
3087 /* type derived from union built-in type must contain at least one type */
3088 if (tpdfname) {
3089 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3090 } else {
3091 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3092 free(*type);
3093 *type = NULL;
3094 }
3095 return LY_EVALID;
3096 }
3097
3098 if (tpdfname) {
3099 type_p->compiled = *type;
3100 *type = calloc(1, sizeof(struct lysc_type_union));
3101 }
3102 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003103 case LY_TYPE_BOOL:
3104 case LY_TYPE_EMPTY:
3105 case LY_TYPE_UNKNOWN: /* just to complete switch */
3106 break;
3107 }
3108 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3109done:
3110 return ret;
3111}
3112
Radek Krejcia3045382018-11-22 14:30:31 +01003113/**
3114 * @brief Compile information about the leaf/leaf-list's type.
3115 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003116 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3117 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3118 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3119 * @param[in] context_name Name of the context node or referencing typedef for logging.
3120 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003121 * @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 +01003122 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003123 * @return LY_ERR value.
3124 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003125static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003126lys_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 +02003127 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003128{
3129 LY_ERR ret = LY_SUCCESS;
3130 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003131 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003132 struct type_context {
3133 const struct lysp_tpdf *tpdf;
3134 struct lysp_node *node;
3135 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003136 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003137 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003138 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003139 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003140 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003141 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003142
3143 (*type) = NULL;
3144
3145 tctx = calloc(1, sizeof *tctx);
3146 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003147 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003148 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3149 ret == LY_SUCCESS;
3150 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3151 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3152 if (basetype) {
3153 break;
3154 }
3155
3156 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003157 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3158 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003159 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3160
Radek Krejcicdfecd92018-11-26 11:27:32 +01003161 if (units && !*units) {
3162 /* inherit units */
3163 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3164 }
Radek Krejci01342af2019-01-03 15:18:08 +01003165 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003166 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003167 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003168 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003169 }
Radek Krejci01342af2019-01-03 15:18:08 +01003170 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003171 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3172 break;
3173 }
3174
Radek Krejci19a96102018-11-15 13:38:09 +01003175 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003176 /* it is not necessary to continue, the rest of the chain was already compiled,
3177 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003178 basetype = tctx->tpdf->type.compiled->basetype;
3179 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003180 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003181 dummyloops = 1;
3182 goto preparenext;
3183 } else {
3184 tctx = NULL;
3185 break;
3186 }
Radek Krejci19a96102018-11-15 13:38:09 +01003187 }
3188
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003189 /* circular typedef reference detection */
3190 for (u = 0; u < tpdf_chain.count; u++) {
3191 /* local part */
3192 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3193 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3194 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3195 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3196 free(tctx);
3197 ret = LY_EVALID;
3198 goto cleanup;
3199 }
3200 }
3201 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3202 /* global part for unions corner case */
3203 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3204 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3205 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3206 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3207 free(tctx);
3208 ret = LY_EVALID;
3209 goto cleanup;
3210 }
3211 }
3212
Radek Krejci19a96102018-11-15 13:38:09 +01003213 /* store information for the following processing */
3214 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3215
Radek Krejcicdfecd92018-11-26 11:27:32 +01003216preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003217 /* prepare next loop */
3218 tctx_prev = tctx;
3219 tctx = calloc(1, sizeof *tctx);
3220 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3221 }
3222 free(tctx);
3223
3224 /* allocate type according to the basetype */
3225 switch (basetype) {
3226 case LY_TYPE_BINARY:
3227 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003228 break;
3229 case LY_TYPE_BITS:
3230 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003231 break;
3232 case LY_TYPE_BOOL:
3233 case LY_TYPE_EMPTY:
3234 *type = calloc(1, sizeof(struct lysc_type));
3235 break;
3236 case LY_TYPE_DEC64:
3237 *type = calloc(1, sizeof(struct lysc_type_dec));
3238 break;
3239 case LY_TYPE_ENUM:
3240 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003241 break;
3242 case LY_TYPE_IDENT:
3243 *type = calloc(1, sizeof(struct lysc_type_identityref));
3244 break;
3245 case LY_TYPE_INST:
3246 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3247 break;
3248 case LY_TYPE_LEAFREF:
3249 *type = calloc(1, sizeof(struct lysc_type_leafref));
3250 break;
3251 case LY_TYPE_STRING:
3252 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003253 break;
3254 case LY_TYPE_UNION:
3255 *type = calloc(1, sizeof(struct lysc_type_union));
3256 break;
3257 case LY_TYPE_INT8:
3258 case LY_TYPE_UINT8:
3259 case LY_TYPE_INT16:
3260 case LY_TYPE_UINT16:
3261 case LY_TYPE_INT32:
3262 case LY_TYPE_UINT32:
3263 case LY_TYPE_INT64:
3264 case LY_TYPE_UINT64:
3265 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003266 break;
3267 case LY_TYPE_UNKNOWN:
3268 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3269 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3270 ret = LY_EVALID;
3271 goto cleanup;
3272 }
3273 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003274 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003275 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3276 ly_data_type2str[basetype]);
3277 free(*type);
3278 (*type) = NULL;
3279 ret = LY_EVALID;
3280 goto cleanup;
3281 }
3282
3283 /* get restrictions from the referred typedefs */
3284 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3285 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003286
3287 /* remember the typedef context for circular check */
3288 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3289
Radek Krejci43699232018-11-23 14:59:46 +01003290 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003291 base = tctx->tpdf->type.compiled;
3292 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003293 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003294 /* no change, just use the type information from the base */
3295 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3296 ++base->refcount;
3297 continue;
3298 }
3299
3300 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003301 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3302 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3303 tctx->tpdf->name, ly_data_type2str[basetype]);
3304 ret = LY_EVALID;
3305 goto cleanup;
3306 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3307 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3308 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3309 tctx->tpdf->name, tctx->tpdf->dflt);
3310 ret = LY_EVALID;
3311 goto cleanup;
3312 }
3313
Radek Krejci19a96102018-11-15 13:38:09 +01003314 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003315 /* TODO user type plugins */
3316 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003317 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003318 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 +01003319 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003320 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003321 LY_CHECK_GOTO(ret, cleanup);
3322 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003323 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003324 /* remove the processed typedef contexts from the stack for circular check */
3325 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003326
Radek Krejcic5c27e52018-11-15 14:38:11 +01003327 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003328 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003329 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003330 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003331 /* TODO user type plugins */
3332 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003333 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003334 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 +01003335 LY_CHECK_GOTO(ret, cleanup);
3336 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003337 /* no specific restriction in leaf's type definition, copy from the base */
3338 free(*type);
3339 (*type) = base;
3340 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003341 }
Radek Krejcia1911222019-07-22 17:24:50 +02003342 if (dflt && !(*type)->dflt) {
3343 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003344 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003345 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3346 (*type)->dflt->realtype = (*type);
3347 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3348 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003349 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003350 if (err) {
3351 ly_err_print(err);
3352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3353 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3354 ly_err_free(err);
3355 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003356 if (ret == LY_EINCOMPLETE) {
3357 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003358 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003359
3360 /* but in general result is so far ok */
3361 ret = LY_SUCCESS;
3362 }
Radek Krejcia1911222019-07-22 17:24:50 +02003363 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003364 }
Radek Krejci19a96102018-11-15 13:38:09 +01003365
Radek Krejci0935f412019-08-20 16:15:18 +02003366 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003367
3368cleanup:
3369 ly_set_erase(&tpdf_chain, free);
3370 return ret;
3371}
3372
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003373/**
3374 * @brief Compile status information of the given node.
3375 *
3376 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3377 * has the status correctly set during the compilation.
3378 *
3379 * @param[in] ctx Compile context
3380 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3381 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3382 * the compatibility with the parent's status value.
3383 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3384 * @return LY_ERR value.
3385 */
3386static LY_ERR
3387lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3388{
3389 /* status - it is not inherited by specification, but it does not make sense to have
3390 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3391 if (!((*node_flags) & LYS_STATUS_MASK)) {
3392 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3393 if ((parent_flags & 0x3) != 0x3) {
3394 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3395 * combination of bits (0x3) which marks the uses_status value */
3396 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3397 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3398 }
3399 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3400 } else {
3401 (*node_flags) |= LYS_STATUS_CURR;
3402 }
3403 } else if (parent_flags & LYS_STATUS_MASK) {
3404 /* check status compatibility with the parent */
3405 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3406 if ((*node_flags) & LYS_STATUS_CURR) {
3407 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3408 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3409 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3410 } else { /* LYS_STATUS_DEPRC */
3411 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3412 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3413 }
3414 return LY_EVALID;
3415 }
3416 }
3417 return LY_SUCCESS;
3418}
3419
Radek Krejci8cce8532019-03-05 11:27:45 +01003420/**
3421 * @brief Check uniqness of the node/action/notification name.
3422 *
3423 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3424 * structures, but they share the namespace so we need to check their name collisions.
3425 *
3426 * @param[in] ctx Compile context.
3427 * @param[in] children List (linked list) of data nodes to go through.
3428 * @param[in] actions List (sized array) of actions or RPCs to go through.
3429 * @param[in] notifs List (sized array) of Notifications to go through.
3430 * @param[in] name Name of the item to find in the given lists.
3431 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3432 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3433 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3434 */
3435static LY_ERR
3436lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3437 const struct lysc_action *actions, const struct lysc_notif *notifs,
3438 const char *name, void *exclude)
3439{
3440 const struct lysc_node *iter;
3441 unsigned int u;
3442
3443 LY_LIST_FOR(children, iter) {
3444 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3445 goto error;
3446 }
3447 }
3448 LY_ARRAY_FOR(actions, u) {
3449 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3450 goto error;
3451 }
3452 }
3453 LY_ARRAY_FOR(notifs, u) {
3454 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3455 goto error;
3456 }
3457 }
3458 return LY_SUCCESS;
3459error:
3460 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3461 return LY_EEXIST;
3462}
3463
Radek Krejcia0f704a2019-09-09 16:12:23 +02003464/**
3465 * @brief Get the XPath context node for the given schema node.
3466 * @param[in] start The schema node where the XPath expression appears.
3467 * @return The context node to evaluate XPath expression in given schema node.
3468 * @return NULL in case the context node is the root node.
3469 */
3470static struct lysc_node *
3471lysc_xpath_context(struct lysc_node *start)
3472{
3473 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3474 start = start->parent);
3475 return start;
3476}
3477
Radek Krejciec4da802019-05-02 13:02:41 +02003478static 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 +01003479
Radek Krejcia3045382018-11-22 14:30:31 +01003480/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003481 * @brief Compile parsed RPC/action schema node information.
3482 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003483 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003484 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003485 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3486 * @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).
3487 * Zero means no uses, non-zero value with no status bit set mean the default status.
3488 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3489 */
3490static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003491lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003492 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3493{
3494 LY_ERR ret = LY_SUCCESS;
3495 struct lysp_node *child_p;
3496 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003497 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003498
Radek Krejci327de162019-06-14 12:52:07 +02003499 lysc_update_path(ctx, parent, action_p->name);
3500
Radek Krejci8cce8532019-03-05 11:27:45 +01003501 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3502 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3503 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3504 action_p->name, action)) {
3505 return LY_EVALID;
3506 }
3507
Radek Krejciec4da802019-05-02 13:02:41 +02003508 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003509 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003510 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003511 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003512 return LY_EVALID;
3513 }
3514
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003515 action->nodetype = LYS_ACTION;
3516 action->module = ctx->mod;
3517 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003518 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003519 action->sp = action_p;
3520 }
3521 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3522
3523 /* status - it is not inherited by specification, but it does not make sense to have
3524 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3525 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3526
3527 DUP_STRING(ctx->ctx, action_p->name, action->name);
3528 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3529 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003530 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003531 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003532
3533 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003534 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcia0f704a2019-09-09 16:12:23 +02003535 COMPILE_ARRAY_MUST_GOTO(ctx, action_p->input.musts, action->input.musts, u, action, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003536 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 +02003537 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003538 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003539 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003540 }
Radek Krejci327de162019-06-14 12:52:07 +02003541 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003542 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003543
3544 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003545 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcia0f704a2019-09-09 16:12:23 +02003546 COMPILE_ARRAY_MUST_GOTO(ctx, action_p->output.musts, action->output.musts, u, action, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003547 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 +02003548 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003549 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003550 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003551 }
Radek Krejci327de162019-06-14 12:52:07 +02003552 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003553
Radek Krejci327de162019-06-14 12:52:07 +02003554 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003555cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003556 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003557 return ret;
3558}
3559
3560/**
Radek Krejci43981a32019-04-12 09:44:11 +02003561 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003562 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003563 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003564 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3565 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3566 * @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 +02003567 * Zero means no uses, non-zero value with no status bit set mean the default status.
3568 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3569 */
3570static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003571lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003572 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3573{
3574 LY_ERR ret = LY_SUCCESS;
3575 struct lysp_node *child_p;
3576 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003577 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003578
Radek Krejci327de162019-06-14 12:52:07 +02003579 lysc_update_path(ctx, parent, notif_p->name);
3580
Radek Krejcifc11bd72019-04-11 16:00:05 +02003581 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3582 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3583 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3584 notif_p->name, notif)) {
3585 return LY_EVALID;
3586 }
3587
Radek Krejciec4da802019-05-02 13:02:41 +02003588 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3590 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003591 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003592 return LY_EVALID;
3593 }
3594
3595 notif->nodetype = LYS_NOTIF;
3596 notif->module = ctx->mod;
3597 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003598 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003599 notif->sp = notif_p;
3600 }
3601 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3602
3603 /* status - it is not inherited by specification, but it does not make sense to have
3604 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3605 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3606
3607 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3608 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3609 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003610 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcia0f704a2019-09-09 16:12:23 +02003611 COMPILE_ARRAY_MUST_GOTO(ctx, notif_p->musts, notif->musts, u, notif, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003612 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003613
Radek Krejciec4da802019-05-02 13:02:41 +02003614 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003615 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003616 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003617 }
3618
Radek Krejci327de162019-06-14 12:52:07 +02003619 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003620cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003621 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003622 return ret;
3623}
3624
3625/**
Radek Krejcia3045382018-11-22 14:30:31 +01003626 * @brief Compile parsed container node information.
3627 * @param[in] ctx Compile context
3628 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003629 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3630 * is enriched with the container-specific information.
3631 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3632 */
Radek Krejci19a96102018-11-15 13:38:09 +01003633static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003634lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003635{
3636 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3637 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3638 struct lysp_node *child_p;
3639 unsigned int u;
3640 LY_ERR ret = LY_SUCCESS;
3641
Radek Krejcife909632019-02-12 15:34:42 +01003642 if (cont_p->presence) {
3643 cont->flags |= LYS_PRESENCE;
3644 }
3645
Radek Krejci19a96102018-11-15 13:38:09 +01003646 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003647 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003648 }
3649
Radek Krejcia0f704a2019-09-09 16:12:23 +02003650 COMPILE_ARRAY_MUST_GOTO(ctx, cont_p->musts, cont->musts, u, node, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02003651 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3652 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003653
3654done:
3655 return ret;
3656}
3657
Radek Krejci33f72892019-02-21 10:36:58 +01003658/*
3659 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3660 * @param[in] ctx Compile context.
3661 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3662 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003663 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3664 * @return LY_ERR value.
3665 */
3666static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003667lys_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 +01003668{
Radek Krejcia1911222019-07-22 17:24:50 +02003669 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003670 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3671
Radek Krejciec4da802019-05-02 13:02:41 +02003672 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 +01003673 leaf->units ? NULL : &leaf->units));
3674 if (leaf->nodetype == LYS_LEAFLIST) {
3675 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003676 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3677 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003678 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003679 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3680 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3681 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3682 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003683 LY_ARRAY_INCREMENT(llist->dflts);
3684 }
3685 } else {
3686 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003687 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003688 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3689 leaf->dflt->realtype = leaf->type->dflt->realtype;
3690 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3691 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003692 }
3693 }
3694 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3695 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3696 ly_set_add(&ctx->unres, leaf, 0);
3697 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3698 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3699 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3700 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3701 ly_set_add(&ctx->unres, leaf, 0);
3702 }
3703 }
3704 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003705 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3707 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3708 return LY_EVALID;
3709 }
3710 }
3711
Radek Krejci33f72892019-02-21 10:36:58 +01003712 return LY_SUCCESS;
3713}
3714
Radek Krejcia3045382018-11-22 14:30:31 +01003715/**
3716 * @brief Compile parsed leaf node information.
3717 * @param[in] ctx Compile context
3718 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003719 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3720 * is enriched with the leaf-specific information.
3721 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3722 */
Radek Krejci19a96102018-11-15 13:38:09 +01003723static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003724lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003725{
3726 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3727 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3728 unsigned int u;
3729 LY_ERR ret = LY_SUCCESS;
3730
Radek Krejcia0f704a2019-09-09 16:12:23 +02003731 COMPILE_ARRAY_MUST_GOTO(ctx, leaf_p->musts, leaf->musts, u, node, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003732 if (leaf_p->units) {
3733 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3734 leaf->flags |= LYS_SET_UNITS;
3735 }
Radek Krejcia1911222019-07-22 17:24:50 +02003736
3737 /* the dflt member is just filled to avoid getting the default value from the type */
3738 leaf->dflt = (void*)leaf_p->dflt;
3739 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003740 if (ret) {
3741 leaf->dflt = NULL;
3742 return ret;
3743 }
Radek Krejcia1911222019-07-22 17:24:50 +02003744
Radek Krejciccd20f12019-02-15 14:12:27 +01003745 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003746 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003747 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003748 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3749 leaf->dflt->realtype = leaf->type;
3750 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3751 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003752 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003753 leaf->dflt->realtype->refcount++;
3754 if (err) {
3755 ly_err_print(err);
3756 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3757 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3758 ly_err_free(err);
3759 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003760 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003761 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003762 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003763
3764 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003765 ret = LY_SUCCESS;
3766 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003767 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003768 leaf->flags |= LYS_SET_DFLT;
3769 }
Radek Krejci43699232018-11-23 14:59:46 +01003770
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003771
Radek Krejci19a96102018-11-15 13:38:09 +01003772done:
3773 return ret;
3774}
3775
Radek Krejcia3045382018-11-22 14:30:31 +01003776/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003777 * @brief Compile parsed leaf-list node information.
3778 * @param[in] ctx Compile context
3779 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003780 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3781 * is enriched with the leaf-list-specific information.
3782 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3783 */
3784static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003785lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003786{
3787 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3788 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003789 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003790 LY_ERR ret = LY_SUCCESS;
3791
Radek Krejcia0f704a2019-09-09 16:12:23 +02003792 COMPILE_ARRAY_MUST_GOTO(ctx, llist_p->musts, llist->musts, u, node, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003793 if (llist_p->units) {
3794 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3795 llist->flags |= LYS_SET_UNITS;
3796 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003797
Radek Krejcia1911222019-07-22 17:24:50 +02003798 /* the dflts member is just filled to avoid getting the default value from the type */
3799 llist->dflts = (void*)llist_p->dflts;
3800 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003801 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003802 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003803 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003804 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3805 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003806 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003807 LY_ARRAY_INCREMENT(llist->dflts_mods);
3808 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003809 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003810 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3811 llist->dflts[u]->realtype = llist->type;
3812 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3813 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003814 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003815 llist->dflts[u]->realtype->refcount++;
3816 if (err) {
3817 ly_err_print(err);
3818 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3819 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3820 ly_err_free(err);
3821 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003822 if (ret == LY_EINCOMPLETE) {
3823 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003824 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003825
3826 /* but in general result is so far ok */
3827 ret = LY_SUCCESS;
3828 }
Radek Krejcia1911222019-07-22 17:24:50 +02003829 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003830 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003831 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003832 }
Radek Krejcia1911222019-07-22 17:24:50 +02003833 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3834 /* configuration data values must be unique - so check the default values */
3835 LY_ARRAY_FOR(llist->dflts, u) {
3836 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3837 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3838 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003839 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 +02003840 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3841 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3842 if (dynamic) {
3843 free((char*)val);
3844 }
3845 return LY_EVALID;
3846 }
3847 }
3848 }
3849 }
3850
3851 /* 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 +01003852
3853 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003854 if (llist->min) {
3855 llist->flags |= LYS_MAND_TRUE;
3856 }
Radek Krejcib7408632018-11-28 17:12:11 +01003857 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003858
Radek Krejci0e5d8382018-11-28 16:37:53 +01003859done:
3860 return ret;
3861}
3862
3863/**
Radek Krejci7af64242019-02-18 13:07:53 +01003864 * @brief Compile information about list's uniques.
3865 * @param[in] ctx Compile context.
3866 * @param[in] context_module Module where the prefixes are going to be resolved.
3867 * @param[in] uniques Sized array list of unique statements.
3868 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3869 * @return LY_ERR value.
3870 */
3871static LY_ERR
3872lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3873{
3874 LY_ERR ret = LY_SUCCESS;
3875 struct lysc_node_leaf **key, ***unique;
3876 const char *keystr, *delim;
3877 size_t len;
3878 unsigned int v;
3879 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003880 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003881
3882 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3883 config = -1;
3884 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3885 keystr = uniques[v];
3886 while (keystr) {
3887 delim = strpbrk(keystr, " \t\n");
3888 if (delim) {
3889 len = delim - keystr;
3890 while (isspace(*delim)) {
3891 ++delim;
3892 }
3893 } else {
3894 len = strlen(keystr);
3895 }
3896
3897 /* unique node must be present */
3898 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003899 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3900 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003901 if (ret != LY_SUCCESS) {
3902 if (ret == LY_EDENIED) {
3903 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003904 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003905 len, keystr, lys_nodetype2str((*key)->nodetype));
3906 }
3907 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003908 } else if (flags) {
3909 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3910 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3911 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3912 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003913 }
3914
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003915
Radek Krejci7af64242019-02-18 13:07:53 +01003916 /* all referenced leafs must be of the same config type */
3917 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3918 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3919 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3920 return LY_EVALID;
3921 } else if ((*key)->flags & LYS_CONFIG_W) {
3922 config = 1;
3923 } else { /* LYS_CONFIG_R */
3924 config = 0;
3925 }
3926
3927 /* check status */
3928 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3929 (*key)->flags, (*key)->module, (*key)->name));
3930
3931 /* mark leaf as unique */
3932 (*key)->flags |= LYS_UNIQUE;
3933
3934 /* next unique value in line */
3935 keystr = delim;
3936 }
3937 /* next unique definition */
3938 }
3939
3940 return LY_SUCCESS;
3941}
3942
3943/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003944 * @brief Compile parsed list node information.
3945 * @param[in] ctx Compile context
3946 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003947 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3948 * is enriched with the list-specific information.
3949 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3950 */
3951static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003952lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003953{
3954 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3955 struct lysc_node_list *list = (struct lysc_node_list*)node;
3956 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003957 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003958 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003959 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003960 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003961 LY_ERR ret = LY_SUCCESS;
3962
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003963 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003964 if (list->min) {
3965 list->flags |= LYS_MAND_TRUE;
3966 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003967 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3968
3969 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003970 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003971 }
3972
Radek Krejcia0f704a2019-09-09 16:12:23 +02003973 COMPILE_ARRAY_MUST_GOTO(ctx, list_p->musts, list->musts, u, node, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003974
3975 /* keys */
3976 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3977 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3978 return LY_EVALID;
3979 }
3980
3981 /* find all the keys (must be direct children) */
3982 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003983 if (!keystr) {
3984 /* keyless list */
3985 list->flags |= LYS_KEYLESS;
3986 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003987 while (keystr) {
3988 delim = strpbrk(keystr, " \t\n");
3989 if (delim) {
3990 len = delim - keystr;
3991 while (isspace(*delim)) {
3992 ++delim;
3993 }
3994 } else {
3995 len = strlen(keystr);
3996 }
3997
3998 /* key node must be present */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003999 key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
4000 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004001 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4002 "The list's key \"%.*s\" not found.", len, keystr);
4003 return LY_EVALID;
4004 }
4005 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004006 if (key->flags & LYS_KEY) {
4007 /* the node was already marked as a key */
4008 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4009 "Duplicated key identifier \"%.*s\".", len, keystr);
4010 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004011 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004012
4013 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004014 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004015 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004016 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
4017 return LY_EVALID;
4018 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004019 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004020 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004021 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004022 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004023 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004024 return LY_EVALID;
4025 }
4026 } else {
4027 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004028 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004030 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004031 return LY_EVALID;
4032 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004033 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004034 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004035 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004036 return LY_EVALID;
4037 }
4038 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004039
4040 /* check status */
4041 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02004042 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01004043
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004044 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004045 if (key->dflt) {
4046 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
4047 lysc_type_free(ctx->ctx, key->dflt->realtype);
4048 free(key->dflt);
4049 key->dflt = NULL;
4050 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004051 }
4052 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004053 key->flags |= LYS_KEY;
4054
4055 /* move it to the correct position */
4056 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
4057 /* fix links in closest previous siblings of the key */
4058 if (key->next) {
4059 key->next->prev = key->prev;
4060 } else {
4061 /* last child */
4062 list->child->prev = key->prev;
4063 }
4064 if (key->prev->next) {
4065 key->prev->next = key->next;
4066 }
4067 /* fix links in the key */
4068 if (prev_key) {
4069 key->prev = (struct lysc_node*)prev_key;
4070 key->next = prev_key->next;
4071 } else {
4072 key->prev = list->child->prev;
4073 key->next = list->child;
4074 }
4075 /* fix links in closes future siblings of the key */
4076 if (prev_key) {
4077 if (prev_key->next) {
4078 prev_key->next->prev = (struct lysc_node*)key;
4079 } else {
4080 list->child->prev = (struct lysc_node*)key;
4081 }
4082 prev_key->next = (struct lysc_node*)key;
4083 } else {
4084 list->child->prev = (struct lysc_node*)key;
4085 }
4086 /* fix links in parent */
4087 if (!key->prev->next) {
4088 list->child = (struct lysc_node*)key;
4089 }
4090 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004091
4092 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004093 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004094 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004095 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004096 }
4097
4098 /* uniques */
4099 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004100 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004101 }
4102
Radek Krejciec4da802019-05-02 13:02:41 +02004103 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4104 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004105
4106done:
4107 return ret;
4108}
4109
Radek Krejcib56c7502019-02-13 14:19:54 +01004110/**
4111 * @brief Do some checks and set the default choice's case.
4112 *
4113 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4114 *
4115 * @param[in] ctx Compile context.
4116 * @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,
4117 * not the reference to the imported module.
4118 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4119 * @return LY_ERR value.
4120 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004121static LY_ERR
4122lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4123{
4124 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4125 const char *prefix = NULL, *name;
4126 size_t prefix_len = 0;
4127
4128 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4129 name = strchr(dflt, ':');
4130 if (name) {
4131 prefix = dflt;
4132 prefix_len = name - prefix;
4133 ++name;
4134 } else {
4135 name = dflt;
4136 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004137 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004138 /* prefixed default case make sense only for the prefix of the schema itself */
4139 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4140 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4141 prefix_len, prefix);
4142 return LY_EVALID;
4143 }
4144 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4145 if (!ch->dflt) {
4146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4147 "Default case \"%s\" not found.", dflt);
4148 return LY_EVALID;
4149 }
4150 /* no mandatory nodes directly under the default case */
4151 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004152 if (iter->parent != (struct lysc_node*)ch->dflt) {
4153 break;
4154 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004155 if (iter->flags & LYS_MAND_TRUE) {
4156 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4157 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4158 return LY_EVALID;
4159 }
4160 }
Radek Krejci01342af2019-01-03 15:18:08 +01004161 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004162 return LY_SUCCESS;
4163}
4164
Radek Krejciccd20f12019-02-15 14:12:27 +01004165static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004166lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004167{
4168 struct lys_module *mod;
4169 const char *prefix = NULL, *name;
4170 size_t prefix_len = 0;
4171 struct lysc_node_case *cs;
4172 struct lysc_node *node;
4173
4174 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4175 name = strchr(dflt, ':');
4176 if (name) {
4177 prefix = dflt;
4178 prefix_len = name - prefix;
4179 ++name;
4180 } else {
4181 name = dflt;
4182 }
4183 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4184 if (prefix) {
4185 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4186 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004187 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4188 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004189 return LY_EVALID;
4190 }
4191 } else {
4192 mod = ctx->mod;
4193 }
4194 /* get the default case */
4195 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4196 if (!cs) {
4197 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004198 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004199 return LY_EVALID;
4200 }
4201
4202 /* check that there is no mandatory node */
4203 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004204 if (node->parent != (struct lysc_node*)cs) {
4205 break;
4206 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004207 if (node->flags & LYS_MAND_TRUE) {
4208 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004209 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4210 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004211 return LY_EVALID;
4212 }
4213 }
4214
4215 /* set the default case in choice */
4216 ch->dflt = cs;
4217 cs->flags |= LYS_SET_DFLT;
4218
4219 return LY_SUCCESS;
4220}
4221
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004222/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004223 * @brief Compile parsed choice node information.
4224 * @param[in] ctx Compile context
4225 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004226 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004227 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004228 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4229 */
4230static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004231lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004232{
4233 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4234 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4235 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004236 LY_ERR ret = LY_SUCCESS;
4237
Radek Krejci056d0a82018-12-06 16:57:25 +01004238 LY_LIST_FOR(ch_p->child, child_p) {
4239 if (child_p->nodetype == LYS_CASE) {
4240 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004241 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004242 }
4243 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004244 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004245 }
4246 }
4247
4248 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004249 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004250 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004251 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004252
Radek Krejci9800fb82018-12-13 14:26:23 +01004253 return ret;
4254}
4255
4256/**
4257 * @brief Compile parsed anydata or anyxml node information.
4258 * @param[in] ctx Compile context
4259 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004260 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4261 * is enriched with the any-specific information.
4262 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4263 */
4264static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004265lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004266{
4267 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4268 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4269 unsigned int u;
4270 LY_ERR ret = LY_SUCCESS;
4271
Radek Krejcia0f704a2019-09-09 16:12:23 +02004272 COMPILE_ARRAY_MUST_GOTO(ctx, any_p->musts, any->musts, u, node, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004273
4274 if (any->flags & LYS_CONFIG_W) {
4275 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004276 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004277 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004278done:
4279 return ret;
4280}
4281
Radek Krejcib56c7502019-02-13 14:19:54 +01004282/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004283 * @brief Connect the node into the siblings list and check its name uniqueness.
4284 *
4285 * @param[in] ctx Compile context
4286 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4287 * the choice itself is expected instead of a specific case node.
4288 * @param[in] node Schema node to connect into the list.
4289 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4290 */
4291static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004292lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004293{
4294 struct lysc_node **children;
4295
4296 if (node->nodetype == LYS_CASE) {
4297 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4298 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004299 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004300 }
4301 if (children) {
4302 if (!(*children)) {
4303 /* first child */
4304 *children = node;
4305 } else if (*children != node) {
4306 /* by the condition in previous branch we cover the choice/case children
4307 * - the children list is shared by the choice and the the first case, in addition
4308 * the first child of each case must be referenced from the case node. So the node is
4309 * actually always already inserted in case it is the first children - so here such
4310 * a situation actually corresponds to the first branch */
4311 /* insert at the end of the parent's children list */
4312 (*children)->prev->next = node;
4313 node->prev = (*children)->prev;
4314 (*children)->prev = node;
4315
4316 /* check the name uniqueness */
4317 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4318 lysc_node_notifs(parent), node->name, node)) {
4319 return LY_EEXIST;
4320 }
4321 }
4322 }
4323 return LY_SUCCESS;
4324}
4325
Radek Krejci95710c92019-02-11 15:49:55 +01004326/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004327 * @brief Prepare the case structure in choice node for the new data node.
4328 *
4329 * 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
4330 * created in the choice when the first child was processed.
4331 *
4332 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004333 * @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,
4334 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004335 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4336 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4337 * @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,
4338 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004339 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004340static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004341lys_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 +01004342{
4343 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004344 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004345 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004346 unsigned int u;
4347 LY_ERR ret;
4348
Radek Krejci95710c92019-02-11 15:49:55 +01004349#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004350 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004351 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004352 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4353 return NULL; \
4354 } \
4355 }
4356
Radek Krejci95710c92019-02-11 15:49:55 +01004357 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4358 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004359
4360 /* we have to add an implicit case node into the parent choice */
4361 cs = calloc(1, sizeof(struct lysc_node_case));
4362 DUP_STRING(ctx->ctx, child->name, cs->name);
4363 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004364 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004365 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4366 /* the case is already present since the child is not its first children */
4367 return (struct lysc_node_case*)ch->cases->prev;
4368 }
Radek Krejci95710c92019-02-11 15:49:55 +01004369 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004370
4371 /* explicit parent case is not present (this is its first child) */
4372 cs = calloc(1, sizeof(struct lysc_node_case));
4373 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4374 cs->flags = LYS_STATUS_MASK & node_p->flags;
4375 cs->sp = node_p;
4376
Radek Krejcib1b59152019-01-07 13:21:56 +01004377 /* 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 +02004378 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004379
4380 if (node_p->when) {
4381 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004382 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004383 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004384 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004385 }
Radek Krejciec4da802019-05-02 13:02:41 +02004386 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004387 } else {
4388 LOGINT(ctx->ctx);
4389 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004390 }
4391 cs->module = ctx->mod;
4392 cs->prev = (struct lysc_node*)cs;
4393 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004394 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004395 cs->parent = (struct lysc_node*)ch;
4396 cs->child = child;
4397
4398 return cs;
4399error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004400 if (cs) {
4401 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4402 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004403 return NULL;
4404
4405#undef UNIQUE_CHECK
4406}
4407
Radek Krejcib56c7502019-02-13 14:19:54 +01004408/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004409 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004410 *
4411 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004412 * @param[in] node Target node where the config is supposed to be changed.
4413 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004414 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4415 * 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 +01004416 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4417 * @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 +01004418 * @return LY_ERR value.
4419 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004420static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004421lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004422 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004423{
4424 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004425 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004426
4427 if (config == (node->flags & LYS_CONFIG_MASK)) {
4428 /* nothing to do */
4429 return LY_SUCCESS;
4430 }
4431
4432 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004433 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004434 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4435 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004436 "Invalid %s of config - configuration node cannot be child of any state data node.",
4437 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004438 return LY_EVALID;
4439 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004440 node->flags |= LYS_SET_CONFIG;
4441 } else {
4442 if (node->flags & LYS_SET_CONFIG) {
4443 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4444 /* setting config flags, but have node with explicit config true */
4445 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004446 "Invalid %s of config - configuration node cannot be child of any state data node.",
4447 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004448 return LY_EVALID;
4449 }
4450 /* do not change config on nodes where the config is explicitely set, this does not apply to
4451 * nodes, which are being changed explicitly (targets of refine or deviation) */
4452 return LY_SUCCESS;
4453 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004454 }
4455 node->flags &= ~LYS_CONFIG_MASK;
4456 node->flags |= config;
4457
4458 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004459 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004460 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004461 }
4462
Radek Krejci76b3e962018-12-14 17:01:25 +01004463 return LY_SUCCESS;
4464}
4465
Radek Krejcib56c7502019-02-13 14:19:54 +01004466/**
4467 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4468 *
4469 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4470 * the flag to such parents from a mandatory children.
4471 *
4472 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4473 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4474 * (mandatory children was removed).
4475 */
Radek Krejcife909632019-02-12 15:34:42 +01004476void
4477lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4478{
4479 struct lysc_node *iter;
4480
4481 if (add) { /* set flag */
4482 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4483 parent = parent->parent) {
4484 parent->flags |= LYS_MAND_TRUE;
4485 }
4486 } else { /* unset flag */
4487 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004488 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004489 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004490 /* there is another mandatory node */
4491 return;
4492 }
4493 }
4494 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4495 parent->flags &= ~LYS_MAND_TRUE;
4496 }
4497 }
4498}
4499
Radek Krejci056d0a82018-12-06 16:57:25 +01004500/**
Radek Krejci3641f562019-02-13 15:38:40 +01004501 * @brief Internal sorting process for the lys_compile_augment_sort().
4502 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4503 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4504 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4505 */
4506static void
4507lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4508{
4509 unsigned int v;
4510 size_t len;
4511
4512 len = strlen(aug_p->nodeid);
4513 LY_ARRAY_FOR(result, v) {
4514 if (strlen(result[v]->nodeid) <= len) {
4515 continue;
4516 }
4517 if (v < LY_ARRAY_SIZE(result)) {
4518 /* move the rest of array */
4519 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4520 break;
4521 }
4522 }
4523 result[v] = aug_p;
4524 LY_ARRAY_INCREMENT(result);
4525}
4526
4527/**
4528 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4529 *
4530 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4531 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4532 *
4533 * @param[in] ctx Compile context.
4534 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4535 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4536 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4537 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4538 * @return LY_ERR value.
4539 */
4540LY_ERR
4541lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4542{
4543 struct lysp_augment **result = NULL;
4544 unsigned int u, v;
4545 size_t count = 0;
4546
4547 assert(augments);
4548
4549 /* get count of the augments in module and all its submodules */
4550 if (aug_p) {
4551 count += LY_ARRAY_SIZE(aug_p);
4552 }
4553 LY_ARRAY_FOR(inc_p, u) {
4554 if (inc_p[u].submodule->augments) {
4555 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4556 }
4557 }
4558
4559 if (!count) {
4560 *augments = NULL;
4561 return LY_SUCCESS;
4562 }
4563 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4564
4565 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4566 * together, so there can be even /z/y betwwen them. */
4567 LY_ARRAY_FOR(aug_p, u) {
4568 lys_compile_augment_sort_(&aug_p[u], result);
4569 }
4570 LY_ARRAY_FOR(inc_p, u) {
4571 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4572 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4573 }
4574 }
4575
4576 *augments = result;
4577 return LY_SUCCESS;
4578}
4579
4580/**
4581 * @brief Compile the parsed augment connecting it into its target.
4582 *
4583 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4584 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4585 * are already implemented and compiled.
4586 *
4587 * @param[in] ctx Compile context.
4588 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004589 * @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
4590 * children in case of the augmenting uses data.
4591 * @return LY_SUCCESS on success.
4592 * @return LY_EVALID on failure.
4593 */
4594LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004595lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004596{
4597 LY_ERR ret = LY_SUCCESS;
4598 struct lysp_node *node_p, *case_node_p;
4599 struct lysc_node *target; /* target target of the augment */
4600 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004601 struct lysc_when **when, *when_shared;
4602 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004603 uint16_t flags = 0;
4604 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004605 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004606
Radek Krejci327de162019-06-14 12:52:07 +02004607 lysc_update_path(ctx, NULL, "{augment}");
4608 lysc_update_path(ctx, NULL, aug_p->nodeid);
4609
Radek Krejci7af64242019-02-18 13:07:53 +01004610 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004611 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004612 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004613 if (ret != LY_SUCCESS) {
4614 if (ret == LY_EDENIED) {
4615 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4616 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4617 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4618 }
4619 return LY_EVALID;
4620 }
4621
4622 /* check for mandatory nodes
4623 * - new cases augmenting some choice can have mandatory nodes
4624 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4625 */
Radek Krejci733988a2019-02-15 15:12:44 +01004626 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004627 allow_mandatory = 1;
4628 }
4629
4630 when_shared = NULL;
4631 LY_LIST_FOR(aug_p->child, node_p) {
4632 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4633 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4634 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004635 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4636 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004637 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004638 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4639 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004640 return LY_EVALID;
4641 }
4642
4643 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004644 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004645 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004646 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004647 } else {
4648 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004649 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004650 }
4651 }
Radek Krejciec4da802019-05-02 13:02:41 +02004652 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004653
Radek Krejcife13da42019-02-15 14:51:01 +01004654 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4655 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004656 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004657 /* 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 +01004658 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 +01004659 } else if (target->nodetype == LYS_CHOICE) {
4660 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4661 node = ((struct lysc_node_choice*)target)->cases->prev;
4662 } else {
4663 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004664 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004665 }
4666
Radek Krejci733988a2019-02-15 15:12:44 +01004667 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004668 node->flags &= ~LYS_MAND_TRUE;
4669 lys_compile_mandatory_parents(target, 0);
4670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004671 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004672 return LY_EVALID;
4673 }
4674
4675 /* pass augment's when to all the children */
4676 if (aug_p->when) {
4677 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4678 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004679 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004680 LY_CHECK_GOTO(ret, error);
4681 (*when)->context = lysc_xpath_context(target);
4682 when_shared = *when;
4683 } else {
4684 ++when_shared->refcount;
4685 (*when) = when_shared;
4686 }
4687 }
4688 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004689
Radek Krejciec4da802019-05-02 13:02:41 +02004690 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004691 switch (target->nodetype) {
4692 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004693 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004694 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004695 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004696 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004697 break;
4698 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004699 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004700 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004701 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004702 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004703 break;
4704 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004705 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004706 if (aug_p->actions) {
4707 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004708 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4709 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004710 return LY_EVALID;
4711 }
4712 if (aug_p->notifs) {
4713 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004714 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4715 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004716 return LY_EVALID;
4717 }
4718 }
Radek Krejci3641f562019-02-13 15:38:40 +01004719
Radek Krejci327de162019-06-14 12:52:07 +02004720 lysc_update_path(ctx, NULL, NULL);
4721 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004722error:
Radek Krejciec4da802019-05-02 13:02:41 +02004723 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004724 return ret;
4725}
4726
4727/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004728 * @brief Apply refined or deviated mandatory flag to the target node.
4729 *
4730 * @param[in] ctx Compile context.
4731 * @param[in] node Target node where the mandatory property is supposed to be changed.
4732 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004733 * @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 +01004734 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4735 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4736 * 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 +01004737 * @return LY_ERR value.
4738 */
4739static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004740lys_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 +01004741{
4742 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4743 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004744 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4745 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004746 return LY_EVALID;
4747 }
4748
4749 if (mandatory_flag & LYS_MAND_TRUE) {
4750 /* check if node has default value */
4751 if (node->nodetype & LYS_LEAF) {
4752 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004753 if (refine_flag) {
4754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004755 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004756 return LY_EVALID;
4757 }
Radek Krejcia1911222019-07-22 17:24:50 +02004758 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004759 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004760 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004761
4762 /* update the list of incomplete default values if needed */
4763 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4764
Radek Krejcia1911222019-07-22 17:24:50 +02004765 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4766 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4767 free(leaf->dflt);
4768 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004769 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004770 }
4771 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004772 if (refine_flag) {
4773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004774 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004775 return LY_EVALID;
4776 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004777 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004778 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004779 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 +01004780 return LY_EVALID;
4781 }
4782
4783 node->flags &= ~LYS_MAND_FALSE;
4784 node->flags |= LYS_MAND_TRUE;
4785 lys_compile_mandatory_parents(node->parent, 1);
4786 } else {
4787 /* make mandatory false */
4788 node->flags &= ~LYS_MAND_TRUE;
4789 node->flags |= LYS_MAND_FALSE;
4790 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004791 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 +01004792 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004793 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004794 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004795 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4796 leaf->dflt->realtype = leaf->type->dflt->realtype;
4797 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4798 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004799 }
4800 }
4801 return LY_SUCCESS;
4802}
4803
4804/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004805 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4806 * If present, also apply uses's modificators.
4807 *
4808 * @param[in] ctx Compile context
4809 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004810 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4811 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4812 * the compile context.
4813 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4814 */
4815static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004816lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004817{
4818 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004819 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004820 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004821 struct lysc_node_container context_node_fake =
4822 {.nodetype = LYS_CONTAINER,
4823 .module = ctx->mod,
4824 .flags = parent ? parent->flags : 0,
4825 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004826 .prev = (struct lysc_node*)&context_node_fake,
4827 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004828 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004829 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004830 int found;
4831 const char *id, *name, *prefix;
4832 size_t prefix_len, name_len;
4833 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004834 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004835 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004836 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004837 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004838 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004839 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004840 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004841 unsigned int actions_index, notifs_index;
4842 struct lysc_notif **notifs = NULL;
4843 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004844
4845 /* search for the grouping definition */
4846 found = 0;
4847 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004848 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004849 if (prefix) {
4850 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4851 if (!mod) {
4852 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004853 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004854 return LY_EVALID;
4855 }
4856 } else {
4857 mod = ctx->mod_def;
4858 }
4859 if (mod == ctx->mod_def) {
4860 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004861 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004862 LY_ARRAY_FOR(grp, u) {
4863 if (!strcmp(grp[u].name, name)) {
4864 grp = &grp[u];
4865 found = 1;
4866 break;
4867 }
4868 }
4869 }
4870 }
4871 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004872 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004873 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004874 if (grp) {
4875 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4876 if (!strcmp(grp[u].name, name)) {
4877 grp = &grp[u];
4878 found = 1;
4879 }
4880 }
4881 }
4882 if (!found && mod->parsed->includes) {
4883 /* ... and all the submodules */
4884 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4885 grp = mod->parsed->includes[u].submodule->groupings;
4886 if (grp) {
4887 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4888 if (!strcmp(grp[v].name, name)) {
4889 grp = &grp[v];
4890 found = 1;
4891 }
4892 }
4893 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004894 }
4895 }
4896 }
4897 if (!found) {
4898 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4899 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4900 return LY_EVALID;
4901 }
4902
4903 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4904 grp_stack_count = ctx->groupings.count;
4905 ly_set_add(&ctx->groupings, (void*)grp, 0);
4906 if (grp_stack_count == ctx->groupings.count) {
4907 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4908 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4909 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4910 return LY_EVALID;
4911 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004912 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4913 /* remember that the grouping is instantiated to avoid its standalone validation */
4914 grp->flags |= LYS_USED_GRP;
4915 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004916
4917 /* switch context's mod_def */
4918 mod_old = ctx->mod_def;
4919 ctx->mod_def = mod;
4920
4921 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004922 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 +01004923
Radek Krejcifc11bd72019-04-11 16:00:05 +02004924 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004925 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004926 /* 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 +02004927 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 +01004928
4929 /* some preparation for applying refines */
4930 if (grp->data == node_p) {
4931 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004932 if (parent) {
4933 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4934 } else if (ctx->mod->compiled->data) {
4935 child = ctx->mod->compiled->data;
4936 } else {
4937 child = NULL;
4938 }
4939 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004940 }
4941 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004942 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004943 LY_LIST_FOR(context_node_fake.child, child) {
4944 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004945
Radek Krejcifc11bd72019-04-11 16:00:05 +02004946 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004947 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004948 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004949 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004950 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004951 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004952 when_shared = *when;
4953 } else {
4954 ++when_shared->refcount;
4955 (*when) = when_shared;
4956 }
4957 }
Radek Krejci01342af2019-01-03 15:18:08 +01004958 }
4959 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004960 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004961 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004962 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004963 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 +01004964 }
4965
Radek Krejcifc11bd72019-04-11 16:00:05 +02004966 /* compile actions */
4967 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4968 if (actions) {
4969 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004970 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004971 if (*actions && (uses_p->augments || uses_p->refines)) {
4972 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4973 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4974 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4975 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4976 }
4977 }
4978
4979 /* compile notifications */
4980 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4981 if (notifs) {
4982 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004983 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004984 if (*notifs && (uses_p->augments || uses_p->refines)) {
4985 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4986 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4987 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4988 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4989 }
4990 }
4991
4992
Radek Krejci3641f562019-02-13 15:38:40 +01004993 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004994 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004995 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004996 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004997 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004998
Radek Krejcif0089082019-01-07 16:42:01 +01004999 /* reload previous context's mod_def */
5000 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02005001 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01005002
Radek Krejci76b3e962018-12-14 17:01:25 +01005003 /* apply refine */
5004 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02005005 lysc_update_path(ctx, NULL, rfn->nodeid);
5006
Radek Krejci7af64242019-02-18 13:07:53 +01005007 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 +01005008 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02005009 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01005010 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01005011
5012 /* default value */
5013 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01005014 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01005015 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005016 "Invalid refine of default - %s cannot hold %d default values.",
5017 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005018 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005019 }
5020 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
5021 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005022 "Invalid refine of default - %s cannot hold default value(s).",
5023 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005024 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005025 }
5026 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02005027 struct ly_err_item *err = NULL;
5028 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
5029 if (leaf->dflt) {
5030 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02005031 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005032 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5033 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5034 } else {
5035 /* prepare a new one */
5036 leaf->dflt = calloc(1, sizeof *leaf->dflt);
5037 leaf->dflt->realtype = leaf->type;
5038 }
5039 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005040 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005041 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
5042 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005043 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005044 leaf->dflt->realtype->refcount++;
5045 if (err) {
5046 ly_err_print(err);
5047 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5048 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
5049 ly_err_free(err);
5050 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005051 if (rc == LY_EINCOMPLETE) {
5052 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005053 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005054
5055 /* but in general result is so far ok */
5056 rc = LY_SUCCESS;
5057 }
Radek Krejcia1911222019-07-22 17:24:50 +02005058 LY_CHECK_GOTO(rc, cleanup);
5059 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005060 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02005061 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
5062
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005063 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01005064 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5065 "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 +02005066 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01005067 }
Radek Krejcia1911222019-07-22 17:24:50 +02005068
5069 /* remove previous set of default values */
5070 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005071 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005072 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5073 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5074 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005075 }
Radek Krejcia1911222019-07-22 17:24:50 +02005076 LY_ARRAY_FREE(llist->dflts);
5077 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005078 LY_ARRAY_FREE(llist->dflts_mods);
5079 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005080
5081 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005082 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005083 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005084 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005085 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005086 LY_ARRAY_INCREMENT(llist->dflts_mods);
5087 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005088 LY_ARRAY_INCREMENT(llist->dflts);
5089 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5090 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005091 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005092 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005093 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005094 llist->dflts[u]->realtype->refcount++;
5095 if (err) {
5096 ly_err_print(err);
5097 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5098 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5099 ly_err_free(err);
5100 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005101 if (rc == LY_EINCOMPLETE) {
5102 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005103 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005104
5105 /* but in general result is so far ok */
5106 rc = LY_SUCCESS;
5107 }
5108 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005109 }
Radek Krejcia1911222019-07-22 17:24:50 +02005110 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005111 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005112 if (((struct lysc_node_choice*)node)->dflt) {
5113 /* unset LYS_SET_DFLT from the current default case */
5114 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5115 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005116 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 +01005117 }
5118 }
5119
Radek Krejci12fb9142019-01-08 09:45:30 +01005120 /* description */
5121 if (rfn->dsc) {
5122 FREE_STRING(ctx->ctx, node->dsc);
5123 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5124 }
5125
5126 /* reference */
5127 if (rfn->ref) {
5128 FREE_STRING(ctx->ctx, node->ref);
5129 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5130 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005131
5132 /* config */
5133 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005134 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005135 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005136 } else {
5137 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
5138 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
5139 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005140 }
5141
5142 /* mandatory */
5143 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005144 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005145 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005146
5147 /* presence */
5148 if (rfn->presence) {
5149 if (node->nodetype != LYS_CONTAINER) {
5150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005151 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5152 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005153 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005154 }
5155 node->flags |= LYS_PRESENCE;
5156 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005157
5158 /* must */
5159 if (rfn->musts) {
5160 switch (node->nodetype) {
5161 case LYS_LEAF:
Radek Krejcia0f704a2019-09-09 16:12:23 +02005162 COMPILE_ARRAY_MUST_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, u, node, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005163 break;
5164 case LYS_LEAFLIST:
Radek Krejcia0f704a2019-09-09 16:12:23 +02005165 COMPILE_ARRAY_MUST_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, u, node, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005166 break;
5167 case LYS_LIST:
Radek Krejcia0f704a2019-09-09 16:12:23 +02005168 COMPILE_ARRAY_MUST_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, u, node, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005169 break;
5170 case LYS_CONTAINER:
Radek Krejcia0f704a2019-09-09 16:12:23 +02005171 COMPILE_ARRAY_MUST_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, u, node, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005172 break;
5173 case LYS_ANYXML:
5174 case LYS_ANYDATA:
Radek Krejcia0f704a2019-09-09 16:12:23 +02005175 COMPILE_ARRAY_MUST_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, u, node, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005176 break;
5177 default:
5178 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005179 "Invalid refine of must statement - %s cannot hold any must statement.",
5180 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005181 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005182 }
5183 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005184
5185 /* min/max-elements */
5186 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5187 switch (node->nodetype) {
5188 case LYS_LEAFLIST:
5189 if (rfn->flags & LYS_SET_MAX) {
5190 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5191 }
5192 if (rfn->flags & LYS_SET_MIN) {
5193 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005194 if (rfn->min) {
5195 node->flags |= LYS_MAND_TRUE;
5196 lys_compile_mandatory_parents(node->parent, 1);
5197 } else {
5198 node->flags &= ~LYS_MAND_TRUE;
5199 lys_compile_mandatory_parents(node->parent, 0);
5200 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005201 }
5202 break;
5203 case LYS_LIST:
5204 if (rfn->flags & LYS_SET_MAX) {
5205 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5206 }
5207 if (rfn->flags & LYS_SET_MIN) {
5208 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005209 if (rfn->min) {
5210 node->flags |= LYS_MAND_TRUE;
5211 lys_compile_mandatory_parents(node->parent, 1);
5212 } else {
5213 node->flags &= ~LYS_MAND_TRUE;
5214 lys_compile_mandatory_parents(node->parent, 0);
5215 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005216 }
5217 break;
5218 default:
5219 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005220 "Invalid refine of %s statement - %s cannot hold this statement.",
5221 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005222 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005223 }
5224 }
Radek Krejcif0089082019-01-07 16:42:01 +01005225
5226 /* if-feature */
5227 if (rfn->iffeatures) {
5228 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005229 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005230 }
Radek Krejci327de162019-06-14 12:52:07 +02005231
5232 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005233 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005234
Radek Krejcif2271f12019-01-07 16:42:23 +01005235 /* do some additional checks of the changed nodes when all the refines are applied */
5236 for (u = 0; u < refined.count; ++u) {
5237 node = (struct lysc_node*)refined.objs[u];
5238 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005239 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005240
5241 /* check possible conflict with default value (default added, mandatory left true) */
5242 if ((node->flags & LYS_MAND_TRUE) &&
5243 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5244 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5245 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005246 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005247 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005248 }
5249
5250 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5251 if (node->nodetype == LYS_LIST) {
5252 min = ((struct lysc_node_list*)node)->min;
5253 max = ((struct lysc_node_list*)node)->max;
5254 } else {
5255 min = ((struct lysc_node_leaflist*)node)->min;
5256 max = ((struct lysc_node_leaflist*)node)->max;
5257 }
5258 if (min > max) {
5259 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005260 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5261 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005262 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005263 }
5264 }
5265 }
5266
Radek Krejci327de162019-06-14 12:52:07 +02005267 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005268 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005269
5270cleanup:
5271 /* fix connection of the children nodes from fake context node back into the parent */
5272 if (context_node_fake.child) {
5273 context_node_fake.child->prev = child;
5274 }
5275 LY_LIST_FOR(context_node_fake.child, child) {
5276 child->parent = parent;
5277 }
5278
5279 if (uses_p->augments || uses_p->refines) {
5280 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005281 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005282 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5283 LY_ARRAY_FREE(context_node_fake.actions);
5284 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005285 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005286 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5287 LY_ARRAY_FREE(context_node_fake.notifs);
5288 }
5289 }
5290
Radek Krejcie86bf772018-12-14 11:39:53 +01005291 /* reload previous context's mod_def */
5292 ctx->mod_def = mod_old;
5293 /* remove the grouping from the stack for circular groupings dependency check */
5294 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5295 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005296 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005297 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005298
5299 return ret;
5300}
5301
Radek Krejci327de162019-06-14 12:52:07 +02005302static int
5303lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5304{
5305 struct lysp_node *iter;
5306 int len = 0;
5307
5308 *path = NULL;
5309 for (iter = node; iter && len >= 0; iter = iter->parent) {
5310 char *s = *path;
5311 char *id;
5312
5313 switch (iter->nodetype) {
5314 case LYS_USES:
5315 asprintf(&id, "{uses='%s'}", iter->name);
5316 break;
5317 case LYS_GROUPING:
5318 asprintf(&id, "{grouping='%s'}", iter->name);
5319 break;
5320 case LYS_AUGMENT:
5321 asprintf(&id, "{augment='%s'}", iter->name);
5322 break;
5323 default:
5324 id = strdup(iter->name);
5325 break;
5326 }
5327
5328 if (!iter->parent) {
5329 /* print prefix */
5330 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5331 } else {
5332 /* prefix is the same as in parent */
5333 len = asprintf(path, "/%s%s", id, s ? s : "");
5334 }
5335 free(s);
5336 free(id);
5337 }
5338
5339 if (len < 0) {
5340 free(*path);
5341 *path = NULL;
5342 } else if (len == 0) {
5343 *path = strdup("/");
5344 len = 1;
5345 }
5346 return len;
5347}
5348
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005349/**
5350 * @brief Validate groupings that were defined but not directly used in the schema itself.
5351 *
5352 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5353 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5354 */
5355static LY_ERR
5356lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5357{
5358 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005359 char *path;
5360 int len;
5361
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005362 struct lysp_node_uses fake_uses = {
5363 .parent = node_p,
5364 .nodetype = LYS_USES,
5365 .flags = 0, .next = NULL,
5366 .name = grp->name,
5367 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5368 .refines = NULL, .augments = NULL
5369 };
5370 struct lysc_node_container fake_container = {
5371 .nodetype = LYS_CONTAINER,
5372 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5373 .module = ctx->mod,
5374 .sp = NULL, .parent = NULL, .next = NULL,
5375 .prev = (struct lysc_node*)&fake_container,
5376 .name = "fake",
5377 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5378 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5379 };
5380
5381 if (grp->parent) {
5382 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5383 }
Radek Krejci327de162019-06-14 12:52:07 +02005384
5385 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5386 if (len < 0) {
5387 LOGMEM(ctx->ctx);
5388 return LY_EMEM;
5389 }
5390 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5391 ctx->path_len = (uint16_t)len;
5392 free(path);
5393
5394 lysc_update_path(ctx, NULL, "{grouping}");
5395 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005396 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005397 lysc_update_path(ctx, NULL, NULL);
5398 lysc_update_path(ctx, NULL, NULL);
5399
5400 ctx->path_len = 1;
5401 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005402
5403 /* cleanup */
5404 lysc_node_container_free(ctx->ctx, &fake_container);
5405
5406 return ret;
5407}
Radek Krejcife909632019-02-12 15:34:42 +01005408
Radek Krejcie86bf772018-12-14 11:39:53 +01005409/**
Radek Krejcia3045382018-11-22 14:30:31 +01005410 * @brief Compile parsed schema node information.
5411 * @param[in] ctx Compile context
5412 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005413 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5414 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5415 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005416 * @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).
5417 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005418 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5419 */
Radek Krejci19a96102018-11-15 13:38:09 +01005420static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005421lys_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 +01005422{
5423 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005424 struct lysc_node *node;
5425 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005426 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005427 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005428 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005429
Radek Krejci327de162019-06-14 12:52:07 +02005430 if (node_p->nodetype != LYS_USES) {
5431 lysc_update_path(ctx, parent, node_p->name);
5432 } else {
5433 lysc_update_path(ctx, NULL, "{uses}");
5434 lysc_update_path(ctx, NULL, node_p->name);
5435 }
5436
Radek Krejci19a96102018-11-15 13:38:09 +01005437 switch (node_p->nodetype) {
5438 case LYS_CONTAINER:
5439 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5440 node_compile_spec = lys_compile_node_container;
5441 break;
5442 case LYS_LEAF:
5443 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5444 node_compile_spec = lys_compile_node_leaf;
5445 break;
5446 case LYS_LIST:
5447 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005448 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005449 break;
5450 case LYS_LEAFLIST:
5451 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005452 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005453 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005454 case LYS_CHOICE:
5455 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005456 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005457 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005458 case LYS_ANYXML:
5459 case LYS_ANYDATA:
5460 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005461 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005462 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005463 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005464 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5465 lysc_update_path(ctx, NULL, NULL);
5466 lysc_update_path(ctx, NULL, NULL);
5467 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005468 default:
5469 LOGINT(ctx->ctx);
5470 return LY_EINT;
5471 }
5472 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5473 node->nodetype = node_p->nodetype;
5474 node->module = ctx->mod;
5475 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005476 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005477
5478 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005479 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005480 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005481 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005482 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5483 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005484 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005485 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005486 node->flags |= LYS_CONFIG_R;
5487 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005488 /* config not explicitely set, inherit it from parent */
5489 if (parent) {
5490 node->flags |= parent->flags & LYS_CONFIG_MASK;
5491 } else {
5492 /* default is config true */
5493 node->flags |= LYS_CONFIG_W;
5494 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005495 } else {
5496 /* config set explicitely */
5497 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005498 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005499 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5500 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5501 "Configuration node cannot be child of any state data node.");
5502 goto error;
5503 }
Radek Krejci19a96102018-11-15 13:38:09 +01005504
Radek Krejcia6d57732018-11-29 13:40:37 +01005505 /* *list ordering */
5506 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5507 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005508 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005509 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5510 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005511 node->flags &= ~LYS_ORDBY_MASK;
5512 node->flags |= LYS_ORDBY_SYSTEM;
5513 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5514 /* default ordering is system */
5515 node->flags |= LYS_ORDBY_SYSTEM;
5516 }
5517 }
5518
Radek Krejci19a96102018-11-15 13:38:09 +01005519 /* status - it is not inherited by specification, but it does not make sense to have
5520 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005521 if (!parent || parent->nodetype != LYS_CHOICE) {
5522 /* in case of choice/case's children, postpone the check to the moment we know if
5523 * 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 +01005524 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 +01005525 }
5526
Radek Krejciec4da802019-05-02 13:02:41 +02005527 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005528 node->sp = node_p;
5529 }
5530 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005531 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5532 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005533 if (node_p->when) {
5534 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005535 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005536 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005537 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005538 }
Radek Krejciec4da802019-05-02 13:02:41 +02005539 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005540
5541 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005542 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005543
Radek Krejci0935f412019-08-20 16:15:18 +02005544 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5545
Radek Krejcife909632019-02-12 15:34:42 +01005546 /* inherit LYS_MAND_TRUE in parent containers */
5547 if (node->flags & LYS_MAND_TRUE) {
5548 lys_compile_mandatory_parents(parent, 1);
5549 }
5550
Radek Krejci327de162019-06-14 12:52:07 +02005551 lysc_update_path(ctx, NULL, NULL);
5552
Radek Krejci19a96102018-11-15 13:38:09 +01005553 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005554 if (parent) {
5555 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005556 if (node_p->parent->nodetype == LYS_CASE) {
5557 lysc_update_path(ctx, parent, node_p->parent->name);
5558 } else {
5559 lysc_update_path(ctx, parent, node->name);
5560 }
Radek Krejciec4da802019-05-02 13:02:41 +02005561 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005562 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005563 if (uses_status) {
5564
5565 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005566 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005567 * it directly gets the same status flags as the choice;
5568 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005569 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005570 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005571 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005572 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005573 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005574 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005575 }
Radek Krejciec4da802019-05-02 13:02:41 +02005576 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 +02005577
5578 if (parent->nodetype == LYS_CHOICE) {
5579 lysc_update_path(ctx, NULL, NULL);
5580 }
Radek Krejci19a96102018-11-15 13:38:09 +01005581 } else {
5582 /* top-level element */
5583 if (!ctx->mod->compiled->data) {
5584 ctx->mod->compiled->data = node;
5585 } else {
5586 /* insert at the end of the module's top-level nodes list */
5587 ctx->mod->compiled->data->prev->next = node;
5588 node->prev = ctx->mod->compiled->data->prev;
5589 ctx->mod->compiled->data->prev = node;
5590 }
Radek Krejci327de162019-06-14 12:52:07 +02005591 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005592 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5593 ctx->mod->compiled->notifs, node->name, node)) {
5594 return LY_EVALID;
5595 }
Radek Krejci19a96102018-11-15 13:38:09 +01005596 }
Radek Krejci327de162019-06-14 12:52:07 +02005597 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005598
5599 return LY_SUCCESS;
5600
5601error:
5602 lysc_node_free(ctx->ctx, node);
5603 return ret;
5604}
5605
Radek Krejciccd20f12019-02-15 14:12:27 +01005606static void
5607lysc_disconnect(struct lysc_node *node)
5608{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005609 struct lysc_node *parent, *child, *prev = NULL, *next;
5610 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005611 int remove_cs = 0;
5612
5613 parent = node->parent;
5614
5615 /* parent's first child */
5616 if (!parent) {
5617 return;
5618 }
5619 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005620 cs = (struct lysc_node_case*)node;
5621 } else if (parent->nodetype == LYS_CASE) {
5622 /* disconnecting some node in a case */
5623 cs = (struct lysc_node_case*)parent;
5624 parent = cs->parent;
5625 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5626 if (child == node) {
5627 if (cs->child == child) {
5628 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5629 /* case with a single child -> remove also the case */
5630 child->parent = NULL;
5631 remove_cs = 1;
5632 } else {
5633 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005634 }
5635 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005636 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005637 }
5638 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005639 if (!remove_cs) {
5640 cs = NULL;
5641 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005642 } else if (lysc_node_children(parent, node->flags) == node) {
5643 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005644 }
5645
5646 if (cs) {
5647 if (remove_cs) {
5648 /* cs has only one child which is being also removed */
5649 lysc_disconnect((struct lysc_node*)cs);
5650 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5651 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005652 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5653 /* default case removed */
5654 ((struct lysc_node_choice*)parent)->dflt = NULL;
5655 }
5656 if (((struct lysc_node_choice*)parent)->cases == cs) {
5657 /* first case removed */
5658 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5659 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005660 if (cs->child) {
5661 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5662 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5663 prev = cs->child->prev;
5664 } /* else all the children are under a single case */
5665 LY_LIST_FOR_SAFE(cs->child, next, child) {
5666 if (child->parent != (struct lysc_node*)cs) {
5667 break;
5668 }
5669 lysc_node_free(node->module->ctx, child);
5670 }
5671 if (prev) {
5672 if (prev->next) {
5673 prev->next = child;
5674 }
5675 if (child) {
5676 child->prev = prev;
5677 } else {
5678 /* link from the first child under the cases */
5679 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5680 }
5681 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005682 }
5683 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005684 }
5685
5686 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005687 if (node->prev->next) {
5688 node->prev->next = node->next;
5689 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005690 if (node->next) {
5691 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005692 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005693 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005694 if (child) {
5695 child->prev = node->prev;
5696 }
5697 } else if (((struct lysc_node_choice*)parent)->cases) {
5698 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005699 }
5700}
5701
5702LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005703lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005704{
Radek Krejcia1911222019-07-22 17:24:50 +02005705 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005706 struct ly_set devs_p = {0};
5707 struct ly_set targets = {0};
5708 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005709 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005710 struct lysc_action *rpcs;
5711 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005712 struct lysp_deviation *dev;
5713 struct lysp_deviate *d, **dp_new;
5714 struct lysp_deviate_add *d_add;
5715 struct lysp_deviate_del *d_del;
5716 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005717 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005718 struct lysc_deviation {
5719 const char *nodeid;
5720 struct lysc_node *target; /* target node of the deviation */
5721 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005722 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005723 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5724 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005725 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005726 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005727 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005728 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005729 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005730 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005731
5732 /* get all deviations from the module and all its submodules ... */
5733 LY_ARRAY_FOR(mod_p->deviations, u) {
5734 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5735 }
5736 LY_ARRAY_FOR(mod_p->includes, v) {
5737 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5738 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5739 }
5740 }
5741 if (!devs_p.count) {
5742 /* nothing to do */
5743 return LY_SUCCESS;
5744 }
5745
Radek Krejci327de162019-06-14 12:52:07 +02005746 lysc_update_path(ctx, NULL, "{deviation}");
5747
Radek Krejciccd20f12019-02-15 14:12:27 +01005748 /* ... and group them by the target node */
5749 devs = calloc(devs_p.count, sizeof *devs);
5750 for (u = 0; u < devs_p.count; ++u) {
5751 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005752 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005753
5754 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005755 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5756 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005757 if (target->nodetype == LYS_ACTION) {
5758 /* move the target pointer to input/output to make them different from the action and
5759 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5760 * back to the RPC/action node due to a better compatibility and decision code in this function.
5761 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5762 if (flags & LYSC_OPT_RPC_INPUT) {
5763 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5764 flags |= LYSC_OPT_INTERNAL;
5765 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5766 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5767 flags |= LYSC_OPT_INTERNAL;
5768 }
5769 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005770 /* insert into the set of targets with duplicity detection */
5771 i = ly_set_add(&targets, target, 0);
5772 if (!devs[i]) {
5773 /* new record */
5774 devs[i] = calloc(1, sizeof **devs);
5775 devs[i]->target = target;
5776 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005777 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005778 }
5779 /* add deviates into the deviation's list of deviates */
5780 for (d = dev->deviates; d; d = d->next) {
5781 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5782 *dp_new = d;
5783 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5784 devs[i]->not_supported = 1;
5785 }
5786 }
Radek Krejci327de162019-06-14 12:52:07 +02005787
5788 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005789 }
5790
5791 /* MACROS for deviates checking */
5792#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5793 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005794 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 +01005795 goto cleanup; \
5796 }
5797
5798#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5799 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005800 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5801 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005802 goto cleanup; \
5803 }
5804
Radek Krejcia1911222019-07-22 17:24:50 +02005805
Radek Krejciccd20f12019-02-15 14:12:27 +01005806#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005807 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5808 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5809 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5810 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5811 goto cleanup; \
5812 }
5813
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005814#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005815 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005816 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005817 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5818 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005819 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005820 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5821 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005822 goto cleanup; \
5823 }
5824
Radek Krejci551b12c2019-02-19 16:11:21 +01005825#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5826 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5827 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005828 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5829 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005830 goto cleanup; \
5831 }
5832
Radek Krejciccd20f12019-02-15 14:12:27 +01005833#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5834 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005835 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005836 goto cleanup; \
5837 }
5838
Radek Krejci551b12c2019-02-19 16:11:21 +01005839#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5840 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005842 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005843 goto cleanup; \
5844 }
5845
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005846#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5847 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5848 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5849 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5850 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 +01005851 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005852 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005853 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005854 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5855 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005856 goto cleanup; \
5857 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005858 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5859 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5860 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5861 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5862 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005863 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005864 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5865 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5866 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005867 }
5868
5869 /* apply deviations */
5870 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005871 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5872 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5873 struct ly_err_item *err = NULL;
5874
5875 dflt = NULL;
5876 changed_type = 0;
5877
Radek Krejci327de162019-06-14 12:52:07 +02005878 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5879
Radek Krejcif538ce52019-03-05 10:46:14 +01005880 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5881 /* fix the target pointer in case of RPC's/action's input/output */
5882 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5883 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5884 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5885 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5886 }
5887 }
5888
Radek Krejciccd20f12019-02-15 14:12:27 +01005889 /* not-supported */
5890 if (devs[u]->not_supported) {
5891 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5892 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5893 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5894 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005895
5896#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5897 if (devs[u]->target->parent) { \
5898 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5899 } else { \
5900 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5901 } \
5902 LY_ARRAY_FOR(ARRAY, x) { \
5903 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5904 } \
5905 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5906 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5907 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5908 LY_ARRAY_DECREMENT(ARRAY); \
5909 }
5910
Radek Krejcif538ce52019-03-05 10:46:14 +01005911 if (devs[u]->target->nodetype == LYS_ACTION) {
5912 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5913 /* remove RPC's/action's input */
5914 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5915 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005916 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5917 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005918 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5919 /* remove RPC's/action's output */
5920 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5921 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005922 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5923 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005924 } else {
5925 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005926 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005927 }
5928 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005929 /* remove Notification */
5930 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005931 } else {
5932 /* remove the target node */
5933 lysc_disconnect(devs[u]->target);
5934 lysc_node_free(ctx->ctx, devs[u]->target);
5935 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005936
Radek Krejci474f9b82019-07-24 11:36:37 +02005937 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5938 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005939 continue;
5940 }
5941
5942 /* list of deviates (not-supported is not present in the list) */
5943 LY_ARRAY_FOR(devs[u]->deviates, v) {
5944 d = devs[u]->deviates[v];
5945
5946 switch (d->mod) {
5947 case LYS_DEV_ADD:
5948 d_add = (struct lysp_deviate_add*)d;
5949 /* [units-stmt] */
5950 if (d_add->units) {
5951 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5952 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5953
5954 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5955 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5956 }
5957
5958 /* *must-stmt */
5959 if (d_add->musts) {
5960 switch (devs[u]->target->nodetype) {
5961 case LYS_CONTAINER:
5962 case LYS_LIST:
Radek Krejcia0f704a2019-09-09 16:12:23 +02005963 COMPILE_ARRAY_MUST_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5964 x, devs[u]->target, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005965 break;
5966 case LYS_LEAF:
5967 case LYS_LEAFLIST:
5968 case LYS_ANYDATA:
Radek Krejcia0f704a2019-09-09 16:12:23 +02005969 COMPILE_ARRAY_MUST_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5970 x, devs[u]->target, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005971 break;
5972 case LYS_NOTIF:
Radek Krejcia0f704a2019-09-09 16:12:23 +02005973 COMPILE_ARRAY_MUST_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5974 x, devs[u]->target, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005975 break;
5976 case LYS_ACTION:
5977 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcia0f704a2019-09-09 16:12:23 +02005978 COMPILE_ARRAY_MUST_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5979 x, devs[u]->target, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005980 break;
5981 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcia0f704a2019-09-09 16:12:23 +02005982 COMPILE_ARRAY_MUST_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5983 x, devs[u]->target, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005984 break;
5985 }
5986 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005987 default:
5988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005989 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005990 goto cleanup;
5991 }
5992 }
5993
5994 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005995 if (d_add->uniques) {
5996 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5997 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5998 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005999
6000 /* *default-stmt */
6001 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006002 switch (devs[u]->target->nodetype) {
6003 case LYS_LEAF:
6004 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006005 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 +02006006 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006007 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006008 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006009 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6010 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6011 } else {
6012 /* prepare new default value storage */
6013 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01006014 }
Radek Krejcia1911222019-07-22 17:24:50 +02006015 dflt = d_add->dflts[0];
6016 /* parsing is done at the end after possible replace of the leaf's type */
6017
Radek Krejci551b12c2019-02-19 16:11:21 +01006018 /* mark the new default values as leaf's own */
6019 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006020 break;
6021 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006022 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006023 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02006024 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006025 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006026 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6027 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6028 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006029 }
Radek Krejcia1911222019-07-22 17:24:50 +02006030 LY_ARRAY_FREE(llist->dflts);
6031 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006032 LY_ARRAY_FREE(llist->dflts_mods);
6033 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006034 }
6035 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006036 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006037 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
6038 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01006039 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006040 LY_ARRAY_INCREMENT(llist->dflts_mods);
6041 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006042 LY_ARRAY_INCREMENT(llist->dflts);
6043 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
6044 llist->dflts[x]->realtype = llist->type;
6045 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 +02006046 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006047 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006048 llist->dflts[x]->realtype->refcount++;
6049 if (err) {
6050 ly_err_print(err);
6051 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6052 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
6053 d_add->dflts[x - y], err->msg);
6054 ly_err_free(err);
6055 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006056 if (rc == LY_EINCOMPLETE) {
6057 /* postpone default compilation when the tree is complete */
6058 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6059
6060 /* but in general result is so far ok */
6061 rc = LY_SUCCESS;
6062 }
Radek Krejcia1911222019-07-22 17:24:50 +02006063 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006064 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006065 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01006066 devs[u]->target->flags |= LYS_SET_DFLT;
6067 break;
6068 case LYS_CHOICE:
6069 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6070 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6071 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6072 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006073 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 +01006074 goto cleanup;
6075 }
6076 break;
6077 default:
6078 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006079 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006080 goto cleanup;
6081 }
6082 }
6083
6084 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006085 if (d_add->flags & LYS_CONFIG_MASK) {
6086 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006087 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006088 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6089 goto cleanup;
6090 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006091 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006092 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
6093 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006094 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006095 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6096 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006097 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6098 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006099 goto cleanup;
6100 }
Radek Krejci327de162019-06-14 12:52:07 +02006101 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006102 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006103
6104 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006105 if (d_add->flags & LYS_MAND_MASK) {
6106 if (devs[u]->target->flags & LYS_MAND_MASK) {
6107 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006108 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6109 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006110 goto cleanup;
6111 }
Radek Krejci327de162019-06-14 12:52:07 +02006112 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006113 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006114
6115 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006116 if (d_add->flags & LYS_SET_MIN) {
6117 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6118 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6119 /* change value */
6120 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6121 } else if (devs[u]->target->nodetype == LYS_LIST) {
6122 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6123 /* change value */
6124 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6125 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006127 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6128 goto cleanup;
6129 }
6130 if (d_add->min) {
6131 devs[u]->target->flags |= LYS_MAND_TRUE;
6132 }
6133 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006134
6135 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006136 if (d_add->flags & LYS_SET_MAX) {
6137 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6138 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6139 /* change value */
6140 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6141 } else if (devs[u]->target->nodetype == LYS_LIST) {
6142 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6143 /* change value */
6144 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6145 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006147 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6148 goto cleanup;
6149 }
6150 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006151
6152 break;
6153 case LYS_DEV_DELETE:
6154 d_del = (struct lysp_deviate_del*)d;
6155
6156 /* [units-stmt] */
6157 if (d_del->units) {
6158 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006159 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6160 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6161 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6162 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6163 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6164 goto cleanup;
6165 }
6166 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6167 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006168 }
6169
6170 /* *must-stmt */
6171 if (d_del->musts) {
6172 switch (devs[u]->target->nodetype) {
6173 case LYS_CONTAINER:
6174 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006175 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006176 break;
6177 case LYS_LEAF:
6178 case LYS_LEAFLIST:
6179 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006180 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006181 break;
6182 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006183 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006184 break;
6185 case LYS_ACTION:
6186 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6187 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6188 break;
6189 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6190 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6191 break;
6192 }
6193 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006194 default:
6195 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006196 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006197 goto cleanup;
6198 }
6199 }
6200
6201 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006202 if (d_del->uniques) {
6203 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6204 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6205 LY_ARRAY_FOR(d_del->uniques, x) {
6206 LY_ARRAY_FOR(list->uniques, z) {
6207 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6208 nodeid = strpbrk(name, " \t\n");
6209 if (nodeid) {
6210 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
6211 || list->uniques[z][y]->name[nodeid - name] != '\0') {
6212 break;
6213 }
6214 while (isspace(*nodeid)) {
6215 ++nodeid;
6216 }
6217 } else {
6218 if (strcmp(name, list->uniques[z][y]->name)) {
6219 break;
6220 }
6221 }
6222 }
6223 if (!name) {
6224 /* complete match - remove the unique */
6225 LY_ARRAY_DECREMENT(list->uniques);
6226 LY_ARRAY_FREE(list->uniques[z]);
6227 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6228 --z;
6229 break;
6230 }
6231 }
6232 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6233 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006234 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6235 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006236 goto cleanup;
6237 }
6238 }
6239 if (!LY_ARRAY_SIZE(list->uniques)) {
6240 LY_ARRAY_FREE(list->uniques);
6241 list->uniques = NULL;
6242 }
6243 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006244
6245 /* *default-stmt */
6246 if (d_del->dflts) {
6247 switch (devs[u]->target->nodetype) {
6248 case LYS_LEAF:
6249 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6250 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6251 dflt, "deleting", "default", d_del->dflts[0]);
6252
Radek Krejcia1911222019-07-22 17:24:50 +02006253 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006254 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006255 if (strcmp(dflt, d_del->dflts[0])) {
6256 if (i) {
6257 free((char*)dflt);
6258 }
6259 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6260 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6261 d_del->dflts[0], dflt);
6262 goto cleanup;
6263 }
6264 if (i) {
6265 free((char*)dflt);
6266 }
6267 dflt = NULL;
6268
Radek Krejci474f9b82019-07-24 11:36:37 +02006269 /* update the list of incomplete default values if needed */
6270 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6271
Radek Krejcia1911222019-07-22 17:24:50 +02006272 /* remove the default specification */
6273 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6274 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6275 free(leaf->dflt);
6276 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006277 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006278 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006279 break;
6280 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006281 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6282 LY_ARRAY_FOR(d_del->dflts, x) {
6283 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006284 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 +02006285 if (!strcmp(dflt, d_del->dflts[x])) {
6286 if (i) {
6287 free((char*)dflt);
6288 }
6289 dflt = NULL;
6290 break;
6291 }
6292 if (i) {
6293 free((char*)dflt);
6294 }
6295 dflt = NULL;
6296 }
6297 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6298 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6299 "which does not match any of the target's property values.", d_del->dflts[x]);
6300 goto cleanup;
6301 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006302
6303 /* update the list of incomplete default values if needed */
6304 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6305
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006306 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006307 LY_ARRAY_DECREMENT(llist->dflts);
6308 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6309 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6310 free(llist->dflts[y]);
6311 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006312 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 +02006313 }
6314 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006315 LY_ARRAY_FREE(llist->dflts_mods);
6316 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006317 LY_ARRAY_FREE(llist->dflts);
6318 llist->dflts = NULL;
6319 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006320 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006321 break;
6322 case LYS_CHOICE:
6323 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6324 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6325 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006326 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006327 if (prefix) {
6328 /* use module prefixes from the deviation module to match the module of the default case */
6329 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6330 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006331 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6332 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006333 goto cleanup;
6334 }
6335 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6336 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006337 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6338 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006339 goto cleanup;
6340 }
6341 }
6342 /* else {
6343 * strictly, the default prefix would point to the deviation module, but the value should actually
6344 * match the default string in the original module (usually unprefixed), so in this case we do not check
6345 * the module of the default case, just matching its name */
6346 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6347 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006348 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6349 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006350 goto cleanup;
6351 }
6352 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6353 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6354 break;
6355 default:
6356 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006357 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006358 goto cleanup;
6359 }
6360 }
6361
6362 break;
6363 case LYS_DEV_REPLACE:
6364 d_rpl = (struct lysp_deviate_rpl*)d;
6365
6366 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006367 if (d_rpl->type) {
6368 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6369 /* type is mandatory, so checking for its presence is not necessary */
6370 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006371
6372 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6373 /* the target has default from the previous type - remove it */
6374 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006375 /* update the list of incomplete default values if needed */
6376 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6377
Radek Krejcia1911222019-07-22 17:24:50 +02006378 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6379 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6380 free(leaf->dflt);
6381 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006382 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006383 } else { /* LYS_LEAFLIST */
6384 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006385 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006386 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6387 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6388 free(llist->dflts[x]);
6389 }
6390 LY_ARRAY_FREE(llist->dflts);
6391 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006392 LY_ARRAY_FREE(llist->dflts_mods);
6393 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006394 }
6395 }
6396 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006397 /* there is no default value, do not set changed_type after type compilation
6398 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006399 changed_type = -1;
6400 }
Radek Krejciec4da802019-05-02 13:02:41 +02006401 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 +02006402 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006403 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006404
6405 /* [units-stmt] */
6406 if (d_rpl->units) {
6407 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6408 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6409 units, "replacing", "units", d_rpl->units);
6410
6411 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6412 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6413 }
6414
6415 /* [default-stmt] */
6416 if (d_rpl->dflt) {
6417 switch (devs[u]->target->nodetype) {
6418 case LYS_LEAF:
6419 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6420 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006421 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006422 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006423 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6424 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6425 dflt = d_rpl->dflt;
6426 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006427 break;
6428 case LYS_CHOICE:
6429 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006430 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 +01006431 goto cleanup;
6432 }
6433 break;
6434 default:
6435 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006436 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006437 goto cleanup;
6438 }
6439 }
6440
6441 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006442 if (d_rpl->flags & LYS_CONFIG_MASK) {
6443 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006444 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006445 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6446 goto cleanup;
6447 }
6448 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006449 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006450 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6451 goto cleanup;
6452 }
Radek Krejci327de162019-06-14 12:52:07 +02006453 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006454 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006455
6456 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006457 if (d_rpl->flags & LYS_MAND_MASK) {
6458 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006460 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6461 goto cleanup;
6462 }
Radek Krejci327de162019-06-14 12:52:07 +02006463 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006464 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006465
6466 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006467 if (d_rpl->flags & LYS_SET_MIN) {
6468 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6469 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6470 /* change value */
6471 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6472 } else if (devs[u]->target->nodetype == LYS_LIST) {
6473 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6474 /* change value */
6475 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6476 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006477 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006478 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6479 goto cleanup;
6480 }
6481 if (d_rpl->min) {
6482 devs[u]->target->flags |= LYS_MAND_TRUE;
6483 }
6484 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006485
6486 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006487 if (d_rpl->flags & LYS_SET_MAX) {
6488 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6489 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6490 /* change value */
6491 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6492 } else if (devs[u]->target->nodetype == LYS_LIST) {
6493 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6494 /* change value */
6495 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6496 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006497 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006498 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6499 goto cleanup;
6500 }
6501 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006502
6503 break;
6504 default:
6505 LOGINT(ctx->ctx);
6506 goto cleanup;
6507 }
6508 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006509
Radek Krejci33f72892019-02-21 10:36:58 +01006510 /* final check when all deviations of a single target node are applied */
6511
Radek Krejci551b12c2019-02-19 16:11:21 +01006512 /* check min-max compatibility */
6513 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6514 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6515 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6516 } else if (devs[u]->target->nodetype == LYS_LIST) {
6517 min = ((struct lysc_node_list*)devs[u]->target)->min;
6518 max = ((struct lysc_node_list*)devs[u]->target)->max;
6519 } else {
6520 min = max = 0;
6521 }
6522 if (min > max) {
6523 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 +02006524 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006525 goto cleanup;
6526 }
6527
Radek Krejcia1911222019-07-22 17:24:50 +02006528 if (dflt) {
6529 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006530 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006531 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006532 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6533 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006534 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006535 leaf->dflt->realtype->refcount++;
6536 if (err) {
6537 ly_err_print(err);
6538 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6539 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6540 ly_err_free(err);
6541 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006542 if (rc == LY_EINCOMPLETE) {
6543 /* postpone default compilation when the tree is complete */
6544 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6545
6546 /* but in general result is so far ok */
6547 rc = LY_SUCCESS;
6548 }
Radek Krejcia1911222019-07-22 17:24:50 +02006549 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006550 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006551 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6552 int dynamic;
6553 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006554 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006555
6556 /* update the list of incomplete default values if needed */
6557 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6558
6559 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006560 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6561 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6562 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006563 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6564 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006565 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006566 leaf->dflt->realtype->refcount++;
6567 if (err) {
6568 ly_err_print(err);
6569 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6570 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6571 ly_err_free(err);
6572 }
6573 if (dynamic) {
6574 free((void*)dflt);
6575 }
Radek Krejcia1911222019-07-22 17:24:50 +02006576 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006577 if (rc == LY_EINCOMPLETE) {
6578 /* postpone default compilation when the tree is complete */
6579 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6580
6581 /* but in general result is so far ok */
6582 rc = LY_SUCCESS;
6583 }
6584 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006585 } else { /* LYS_LEAFLIST */
6586 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006587 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 +02006588 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6589 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6590 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006591 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6592 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006593 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6594 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006595 llist->dflts[x]->realtype->refcount++;
6596 if (err) {
6597 ly_err_print(err);
6598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6599 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6600 dflt, err->msg);
6601 ly_err_free(err);
6602 }
6603 if (dynamic) {
6604 free((void*)dflt);
6605 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006606 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006607 if (rc == LY_EINCOMPLETE) {
6608 /* postpone default compilation when the tree is complete */
6609 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6610
6611 /* but in general result is so far ok */
6612 rc = LY_SUCCESS;
6613 }
Radek Krejcia1911222019-07-22 17:24:50 +02006614 LY_CHECK_GOTO(rc, cleanup);
6615 }
6616 }
6617 }
6618
Radek Krejci551b12c2019-02-19 16:11:21 +01006619 /* check mandatory - default compatibility */
6620 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6621 && (devs[u]->target->flags & LYS_SET_DFLT)
6622 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6623 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006624 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006625 goto cleanup;
6626 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6627 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6628 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006629 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 +01006630 goto cleanup;
6631 }
6632 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6633 /* mandatory node under a default case */
6634 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006635 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6636 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006637 goto cleanup;
6638 }
Radek Krejci33f72892019-02-21 10:36:58 +01006639
Radek Krejci327de162019-06-14 12:52:07 +02006640 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006641 }
6642
Radek Krejci327de162019-06-14 12:52:07 +02006643 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006644 ret = LY_SUCCESS;
6645
6646cleanup:
6647 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6648 LY_ARRAY_FREE(devs[u]->deviates);
6649 free(devs[u]);
6650 }
6651 free(devs);
6652 ly_set_erase(&targets, NULL);
6653 ly_set_erase(&devs_p, NULL);
6654
6655 return ret;
6656}
6657
Radek Krejcib56c7502019-02-13 14:19:54 +01006658/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006659 * @brief Compile the given YANG submodule into the main module.
6660 * @param[in] ctx Compile context
6661 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006662 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6663 */
6664LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006665lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006666{
6667 unsigned int u;
6668 LY_ERR ret = LY_SUCCESS;
6669 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006670 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006671 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006672 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006673
Radek Krejci0af46292019-01-11 16:02:31 +01006674 if (!mainmod->mod->off_features) {
6675 /* features are compiled directly into the compiled module structure,
6676 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6677 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006678 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6679 LY_CHECK_GOTO(ret, error);
6680 }
6681 if (!mainmod->mod->off_extensions) {
6682 /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */
6683 ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions);
Radek Krejci0af46292019-01-11 16:02:31 +01006684 LY_CHECK_GOTO(ret, error);
6685 }
6686
Radek Krejci327de162019-06-14 12:52:07 +02006687 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006688 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006689 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006690
Radek Krejci474f9b82019-07-24 11:36:37 +02006691 /* data nodes */
6692 LY_LIST_FOR(submod->data, node_p) {
6693 ret = lys_compile_node(ctx, node_p, NULL, 0);
6694 LY_CHECK_GOTO(ret, error);
6695 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006696
Radek Krejciec4da802019-05-02 13:02:41 +02006697 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6698 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006699
Radek Krejcid05cbd92018-12-05 14:26:40 +01006700error:
6701 return ret;
6702}
6703
Radek Krejci335332a2019-09-05 13:03:35 +02006704static void *
6705lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6706{
6707 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6708 if (substmts[u].stmt == stmt) {
6709 return substmts[u].storage;
6710 }
6711 }
6712 return NULL;
6713}
6714
6715LY_ERR
6716lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6717{
6718 LY_ERR ret = LY_EVALID, r;
6719 unsigned int u;
6720 struct lysp_stmt *stmt;
6721 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006722
6723 /* check for invalid substatements */
6724 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006725 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6726 continue;
6727 }
Radek Krejci335332a2019-09-05 13:03:35 +02006728 for (u = 0; substmts[u].stmt; ++u) {
6729 if (substmts[u].stmt == stmt->kw) {
6730 break;
6731 }
6732 }
6733 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6735 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006736 goto cleanup;
6737 }
Radek Krejci335332a2019-09-05 13:03:35 +02006738 }
6739
Radek Krejciad5963b2019-09-06 16:03:05 +02006740 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6741
Radek Krejci335332a2019-09-05 13:03:35 +02006742 /* keep order of the processing the same as the order in the defined substmts,
6743 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6744 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006745 int stmt_present = 0;
6746
Radek Krejci335332a2019-09-05 13:03:35 +02006747 for (stmt = ext->child; stmt; stmt = stmt->next) {
6748 if (substmts[u].stmt != stmt->kw) {
6749 continue;
6750 }
6751
Radek Krejciad5963b2019-09-06 16:03:05 +02006752 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006753 if (substmts[u].storage) {
6754 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006755 case LY_STMT_STATUS:
6756 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6757 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6758 break;
6759 case LY_STMT_UNITS: {
6760 const char **units;
6761
6762 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6763 /* single item */
6764 if (*((const char **)substmts[u].storage)) {
6765 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6766 goto cleanup;
6767 }
6768 units = (const char **)substmts[u].storage;
6769 } else {
6770 /* sized array */
6771 const char ***units_array = (const char ***)substmts[u].storage;
6772 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6773 }
6774 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6775 break;
6776 }
Radek Krejci335332a2019-09-05 13:03:35 +02006777 case LY_STMT_TYPE: {
6778 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6779 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6780
6781 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6782 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006783 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006784 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6785 goto cleanup;
6786 }
6787 compiled = substmts[u].storage;
6788 } else {
6789 /* sized array */
6790 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6791 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6792 compiled = (void*)type;
6793 }
6794
Radek Krejciad5963b2019-09-06 16:03:05 +02006795 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006796 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6797 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006798 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6799 lysp_type_free(ctx->ctx, parsed);
6800 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006801 break;
6802 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006803 case LY_STMT_IF_FEATURE: {
6804 struct lysc_iffeature *iff = NULL;
6805
6806 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6807 /* single item */
6808 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6810 goto cleanup;
6811 }
6812 iff = (struct lysc_iffeature*)substmts[u].storage;
6813 } else {
6814 /* sized array */
6815 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6816 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6817 }
6818 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6819 break;
6820 }
6821 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6822 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006823 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006824 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.",
6825 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006826 goto cleanup;
6827 }
6828 }
Radek Krejci335332a2019-09-05 13:03:35 +02006829 }
Radek Krejci335332a2019-09-05 13:03:35 +02006830
Radek Krejciad5963b2019-09-06 16:03:05 +02006831 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6832 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6833 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6834 goto cleanup;
6835 }
Radek Krejci335332a2019-09-05 13:03:35 +02006836 }
6837
6838 ret = LY_SUCCESS;
6839
6840cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006841 return ret;
6842}
6843
Radek Krejci19a96102018-11-15 13:38:09 +01006844LY_ERR
6845lys_compile(struct lys_module *mod, int options)
6846{
6847 struct lysc_ctx ctx = {0};
6848 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006849 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006850 struct lysp_module *sp;
6851 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006852 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006853 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006854 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006855 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006856 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006857
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006858 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006859
6860 if (!mod->implemented) {
6861 /* just imported modules are not compiled */
6862 return LY_SUCCESS;
6863 }
6864
Radek Krejci19a96102018-11-15 13:38:09 +01006865 sp = mod->parsed;
6866
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006867 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006868 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006869 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006870 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006871 ctx.path_len = 1;
6872 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006873
6874 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006875 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6876 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006877
Radek Krejciec4da802019-05-02 13:02:41 +02006878 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006879 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006880 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006881 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6882 }
Radek Krejci0935f412019-08-20 16:15:18 +02006883
6884 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01006885 if (mod->off_features) {
6886 /* there is already precompiled array of features */
6887 mod_c->features = mod->off_features;
6888 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006889 } else {
6890 /* features are compiled directly into the compiled module structure,
6891 * 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 +02006892 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006893 LY_CHECK_GOTO(ret, error);
6894 }
6895 /* finish feature compilation, not only for the main module, but also for the submodules.
6896 * Due to possible forward references, it must be done when all the features (including submodules)
6897 * are present. */
6898 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006899 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006900 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6901 }
Radek Krejci327de162019-06-14 12:52:07 +02006902 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006903 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006904 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006905 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006906 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006907 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6908 }
Radek Krejci327de162019-06-14 12:52:07 +02006909 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006910 }
Radek Krejci327de162019-06-14 12:52:07 +02006911 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006912
Radek Krejci0935f412019-08-20 16:15:18 +02006913 /* extensions */
6914 /* 2-steps: a) prepare compiled structures and ... */
6915 if (mod->off_extensions) {
6916 /* there is already precompiled array of extension definitions */
6917 mod_c->extensions = mod->off_extensions;
6918 mod->off_extensions = NULL;
6919 } else {
6920 /* extension definitions are compiled directly into the compiled module structure */
6921 ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions);
Radek Krejcibfb2e142019-09-09 14:47:44 +02006922 LY_CHECK_GOTO(ret, error);
Radek Krejci0935f412019-08-20 16:15:18 +02006923 }
6924 /* ... b) connect the extension definitions with the appropriate extension plugins */
6925 lys_compile_extension_plugins(mod_c->extensions);
6926
6927 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02006928 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006929 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006930 if (sp->identities) {
6931 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6932 }
Radek Krejci327de162019-06-14 12:52:07 +02006933 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006934
Radek Krejci95710c92019-02-11 15:49:55 +01006935 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006936 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02006937 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01006938 }
Radek Krejci95710c92019-02-11 15:49:55 +01006939
Radek Krejciec4da802019-05-02 13:02:41 +02006940 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6941 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006942
Radek Krejci95710c92019-02-11 15:49:55 +01006943 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02006944 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01006945 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02006946 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01006947 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006948
Radek Krejci474f9b82019-07-24 11:36:37 +02006949 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02006950 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01006951
Radek Krejci0935f412019-08-20 16:15:18 +02006952 /* extension instances TODO cover extension instances from submodules */
6953 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006954
Radek Krejcia3045382018-11-22 14:30:31 +01006955 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006956 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6957 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6958 * 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 +01006959 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006960 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006961 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6962 if (type->basetype == LY_TYPE_LEAFREF) {
6963 /* validate the path */
Radek Krejcid3acfce2019-09-09 14:48:50 +02006964 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type), error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006965 } else if (type->basetype == LY_TYPE_UNION) {
6966 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6967 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6968 /* validate the path */
Radek Krejcid3acfce2019-09-09 14:48:50 +02006969 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6970 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]),
6971 error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006972 }
6973 }
Radek Krejcia3045382018-11-22 14:30:31 +01006974 }
6975 }
6976 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006977 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006978 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006979 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6980 if (type->basetype == LY_TYPE_LEAFREF) {
6981 /* store pointer to the real type */
6982 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6983 typeiter->basetype == LY_TYPE_LEAFREF;
6984 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6985 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006986 } else if (type->basetype == LY_TYPE_UNION) {
6987 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6988 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6989 /* store pointer to the real type */
6990 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6991 typeiter->basetype == LY_TYPE_LEAFREF;
6992 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6993 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6994 }
6995 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006996 }
6997 }
6998 }
Radek Krejciec4da802019-05-02 13:02:41 +02006999
Radek Krejci474f9b82019-07-24 11:36:37 +02007000 /* finish incomplete default values compilation */
7001 for (u = 0; u < ctx.dflts.count; ++u) {
7002 struct ly_err_item *err = NULL;
7003 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
7004 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized),
7005 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7006 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7007 if (err) {
7008 ly_err_print(err);
7009 ctx.path[0] = '\0';
7010 lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
7011 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
7012 "Invalid default - value does not fit the type (%s).", err->msg);
7013 ly_err_free(err);
7014 }
7015 LY_CHECK_GOTO(ret, error);
7016 }
7017
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007018 /* validate non-instantiated groupings from the parsed schema,
7019 * without it we would accept even the schemas with invalid grouping specification */
7020 ctx.options |= LYSC_OPT_GROUPING;
7021 LY_ARRAY_FOR(sp->groupings, u) {
7022 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007023 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007024 }
7025 }
7026 LY_LIST_FOR(sp->data, node_p) {
7027 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7028 LY_ARRAY_FOR(grps, u) {
7029 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007030 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007031 }
7032 }
7033 }
7034
Radek Krejci474f9b82019-07-24 11:36:37 +02007035 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7036 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7037 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7038 }
7039
Radek Krejci1c0c3442019-07-23 16:08:47 +02007040 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007041 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007042 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007043 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007044 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007045
Radek Krejciec4da802019-05-02 13:02:41 +02007046 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01007047 lysp_module_free(mod->parsed);
7048 ((struct lys_module*)mod)->parsed = NULL;
7049 }
7050
Radek Krejciec4da802019-05-02 13:02:41 +02007051 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007052 /* remove flag of the modules implemented by dependency */
7053 for (u = 0; u < ctx.ctx->list.count; ++u) {
7054 m = ctx.ctx->list.objs[u];
7055 if (m->implemented == 2) {
7056 m->implemented = 1;
7057 }
7058 }
7059 }
7060
Radek Krejci19a96102018-11-15 13:38:09 +01007061 ((struct lys_module*)mod)->compiled = mod_c;
7062 return LY_SUCCESS;
7063
7064error:
Radek Krejci95710c92019-02-11 15:49:55 +01007065 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007066 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007067 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007068 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007069 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007070 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007071 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007072 mod->compiled = NULL;
7073
7074 /* revert compilation of modules implemented by dependency */
7075 for (u = 0; u < ctx.ctx->list.count; ++u) {
7076 m = ctx.ctx->list.objs[u];
7077 if (m->implemented == 2) {
7078 /* revert features list to the precompiled state */
7079 lys_feature_precompile_revert(&ctx, m);
7080 /* mark module as imported-only / not-implemented */
7081 m->implemented = 0;
7082 /* free the compiled version of the module */
7083 lysc_module_free(m->compiled, NULL);
7084 m->compiled = NULL;
7085 }
7086 }
7087
Radek Krejci19a96102018-11-15 13:38:09 +01007088 return ret;
7089}