blob: 25b16e43c0d5582449fac0a990cc6969a607b907 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
26#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020027#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "set.h"
29#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020030#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "tree.h"
32#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010033#include "tree_schema_internal.h"
34#include "xpath.h"
35
36/**
37 * @brief Duplicate string into dictionary
38 * @param[in] CTX libyang context of the dictionary.
39 * @param[in] ORIG String to duplicate.
40 * @param[out] DUP Where to store the result.
41 */
42#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
43
Radek Krejciec4da802019-05-02 13:02:41 +020044#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010045 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010047 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010048 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020050 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010051 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejciec4da802019-05-02 13:02:41 +020055#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010056 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020061 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
Radek Krejci0935f412019-08-20 16:15:18 +020066#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
67 if (EXTS_P) { \
68 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \
69 for (uint32_t __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \
70 LY_ARRAY_INCREMENT(EXT_C); \
71 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE); \
72 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejciec4da802019-05-02 13:02:41 +020076#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010077 if (ARRAY_P) { \
78 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
79 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
80 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
81 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020082 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010083 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
84 } \
85 }
86
Radek Krejciec4da802019-05-02 13:02:41 +020087#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010088 if (MEMBER_P) { \
89 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
90 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020091 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010092 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
93 }
94
Radek Krejciec4da802019-05-02 13:02:41 +020095#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +010096 if (MEMBER_P) { \
97 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
98 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
99 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200100 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100101 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
102 }
103
Radek Krejcid05cbd92018-12-05 14:26:40 +0100104#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
105 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100106 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
107 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100108 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
109 return LY_EVALID; \
110 } \
111 } \
112 }
113
Radek Krejci19a96102018-11-15 13:38:09 +0100114static struct lysc_ext_instance *
115lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
116{
Radek Krejcie7b95092019-05-15 11:03:07 +0200117 /* TODO - extensions */
Radek Krejci19a96102018-11-15 13:38:09 +0100118 (void) ctx;
119 (void) orig;
120 return NULL;
121}
122
Radek Krejcib56c7502019-02-13 14:19:54 +0100123/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200124 * @brief Add record into the compile context's list of incomplete default values.
125 * @param[in] ctx Compile context with the incomplete default values list.
126 * @param[in] context_node Context schema node to store in the record.
127 * @param[in] dflt Incomplete default value to store in the record.
128 * @param[in] dflt_mod Module of the default value definition to store in the record.
129 * @return LY_EMEM in case of memory allocation failure.
130 * @return LY_SUCCESS
131 */
132static LY_ERR
133lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
134{
135 struct lysc_incomplete_dflt *r;
136 r = malloc(sizeof *r);
137 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
138 r->context_node = context_node;
139 r->dflt = dflt;
140 r->dflt_mod = dflt_mod;
141 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
142
143 return LY_SUCCESS;
144}
145
146/**
147 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
148 * @param[in] ctx Compile context with the incomplete default values list.
149 * @param[in] dflt Incomplete default values identifying the record to remove.
150 */
151static void
152lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
153{
154 unsigned int u;
155 struct lysc_incomplete_dflt *r;
156
157 for (u = 0; u < ctx->dflts.count; ++u) {
158 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
159 if (r->dflt == dflt) {
160 free(ctx->dflts.objs[u]);
161 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
162 --ctx->dflts.count;
163 return;
164 }
165 }
166}
167
Radek Krejci0e59c312019-08-15 15:34:15 +0200168void
Radek Krejci327de162019-06-14 12:52:07 +0200169lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
170{
171 int len;
172 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
173
174 if (!name) {
175 /* removing last path segment */
176 if (ctx->path[ctx->path_len - 1] == '}') {
177 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
178 if (ctx->path[ctx->path_len] == '=') {
179 ctx->path[ctx->path_len++] = '}';
180 } else {
181 /* not a top-level special tag, remove also preceiding '/' */
182 goto remove_nodelevel;
183 }
184 } else {
185remove_nodelevel:
186 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
187 if (ctx->path_len == 0) {
188 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200189 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200190 }
191 }
192 /* set new terminating NULL-byte */
193 ctx->path[ctx->path_len] = '\0';
194 } else {
195 if (ctx->path_len > 1) {
196 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
197 /* extension of the special tag */
198 nextlevel = 2;
199 --ctx->path_len;
200 } else {
201 /* there is already some path, so add next level */
202 nextlevel = 1;
203 }
204 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
205
206 if (nextlevel != 2) {
207 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
208 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200209 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200210 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200211 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 +0200212 }
213 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200214 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200215 }
Radek Krejciacc79042019-07-25 14:14:57 +0200216 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
217 /* output truncated */
218 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
219 } else {
220 ctx->path_len += len;
221 }
Radek Krejci327de162019-06-14 12:52:07 +0200222 }
223}
224
225/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100226 * @brief Duplicate the compiled pattern structure.
227 *
228 * Instead of duplicating memory, the reference counter in the @p orig is increased.
229 *
230 * @param[in] orig The pattern structure to duplicate.
231 * @return The duplicated structure to use.
232 */
Radek Krejci19a96102018-11-15 13:38:09 +0100233static struct lysc_pattern*
234lysc_pattern_dup(struct lysc_pattern *orig)
235{
236 ++orig->refcount;
237 return orig;
238}
239
Radek Krejcib56c7502019-02-13 14:19:54 +0100240/**
241 * @brief Duplicate the array of compiled patterns.
242 *
243 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
244 *
245 * @param[in] ctx Libyang context for logging.
246 * @param[in] orig The patterns sized array to duplicate.
247 * @return New sized array as a copy of @p orig.
248 * @return NULL in case of memory allocation error.
249 */
Radek Krejci19a96102018-11-15 13:38:09 +0100250static struct lysc_pattern**
251lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
252{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100253 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100254 unsigned int u;
255
Radek Krejcib56c7502019-02-13 14:19:54 +0100256 assert(orig);
257
Radek Krejci19a96102018-11-15 13:38:09 +0100258 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
259 LY_ARRAY_FOR(orig, u) {
260 dup[u] = lysc_pattern_dup(orig[u]);
261 LY_ARRAY_INCREMENT(dup);
262 }
263 return dup;
264}
265
Radek Krejcib56c7502019-02-13 14:19:54 +0100266/**
267 * @brief Duplicate compiled range structure.
268 *
269 * @param[in] ctx Libyang context for logging.
270 * @param[in] orig The range structure to be duplicated.
271 * @return New compiled range structure as a copy of @p orig.
272 * @return NULL in case of memory allocation error.
273 */
Radek Krejci19a96102018-11-15 13:38:09 +0100274struct lysc_range*
275lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
276{
277 struct lysc_range *dup;
278 LY_ERR ret;
279
Radek Krejcib56c7502019-02-13 14:19:54 +0100280 assert(orig);
281
Radek Krejci19a96102018-11-15 13:38:09 +0100282 dup = calloc(1, sizeof *dup);
283 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
284 if (orig->parts) {
285 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
286 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
287 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
288 }
289 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
290 DUP_STRING(ctx, orig->emsg, dup->emsg);
291 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
292
293 return dup;
294cleanup:
295 free(dup);
296 (void) ret; /* set but not used due to the return type */
297 return NULL;
298}
299
Radek Krejcib56c7502019-02-13 14:19:54 +0100300/**
301 * @brief Stack for processing if-feature expressions.
302 */
Radek Krejci19a96102018-11-15 13:38:09 +0100303struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100304 int size; /**< number of items in the stack */
305 int index; /**< first empty item */
306 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100307};
308
Radek Krejcib56c7502019-02-13 14:19:54 +0100309/**
310 * @brief Add @ref ifftokens into the stack.
311 * @param[in] stack The if-feature stack to use.
312 * @param[in] value One of the @ref ifftokens to store in the stack.
313 * @return LY_EMEM in case of memory allocation error
314 * @return LY_ESUCCESS if the value successfully stored.
315 */
Radek Krejci19a96102018-11-15 13:38:09 +0100316static LY_ERR
317iff_stack_push(struct iff_stack *stack, uint8_t value)
318{
319 if (stack->index == stack->size) {
320 stack->size += 4;
321 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
322 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
323 }
324 stack->stack[stack->index++] = value;
325 return LY_SUCCESS;
326}
327
Radek Krejcib56c7502019-02-13 14:19:54 +0100328/**
329 * @brief Get (and remove) the last item form the stack.
330 * @param[in] stack The if-feature stack to use.
331 * @return The value from the top of the stack.
332 */
Radek Krejci19a96102018-11-15 13:38:09 +0100333static uint8_t
334iff_stack_pop(struct iff_stack *stack)
335{
Radek Krejcib56c7502019-02-13 14:19:54 +0100336 assert(stack && stack->index);
337
Radek Krejci19a96102018-11-15 13:38:09 +0100338 stack->index--;
339 return stack->stack[stack->index];
340}
341
Radek Krejcib56c7502019-02-13 14:19:54 +0100342/**
343 * @brief Clean up the stack.
344 * @param[in] stack The if-feature stack to use.
345 */
Radek Krejci19a96102018-11-15 13:38:09 +0100346static void
347iff_stack_clean(struct iff_stack *stack)
348{
349 stack->size = 0;
350 free(stack->stack);
351}
352
Radek Krejcib56c7502019-02-13 14:19:54 +0100353/**
354 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
355 * (libyang format of the if-feature expression).
356 * @param[in,out] list The 2bits array to modify.
357 * @param[in] op The operand (@ref ifftokens) to store.
358 * @param[in] pos Position (0-based) where to store the given @p op.
359 */
Radek Krejci19a96102018-11-15 13:38:09 +0100360static void
361iff_setop(uint8_t *list, uint8_t op, int pos)
362{
363 uint8_t *item;
364 uint8_t mask = 3;
365
366 assert(pos >= 0);
367 assert(op <= 3); /* max 2 bits */
368
369 item = &list[pos / 4];
370 mask = mask << 2 * (pos % 4);
371 *item = (*item) & ~mask;
372 *item = (*item) | (op << 2 * (pos % 4));
373}
374
Radek Krejcib56c7502019-02-13 14:19:54 +0100375#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
376#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100377
Radek Krejci0af46292019-01-11 16:02:31 +0100378/**
379 * @brief Find a feature of the given name and referenced in the given module.
380 *
381 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
382 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
383 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
384 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
385 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
386 * assigned till that time will be still valid.
387 *
388 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
389 * @param[in] name Name of the feature including possible prefix.
390 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
391 * @return Pointer to the feature structure if found, NULL otherwise.
392 */
Radek Krejci19a96102018-11-15 13:38:09 +0100393static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100394lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100395{
396 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100397 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100398
399 for (i = 0; i < len; ++i) {
400 if (name[i] == ':') {
401 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100402 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100403 LY_CHECK_RET(!mod, NULL);
404
405 name = &name[i + 1];
406 len = len - i - 1;
407 }
408 }
409
410 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100411 if (mod->implemented) {
412 /* module is implemented so there is already the compiled schema */
413 flist = mod->compiled->features;
414 } else {
415 flist = mod->off_features;
416 }
417 LY_ARRAY_FOR(flist, i) {
418 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100419 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
420 return f;
421 }
422 }
423
424 return NULL;
425}
426
427static LY_ERR
Radek Krejci0935f412019-08-20 16:15:18 +0200428lys_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 +0100429{
430 const char *name;
431 unsigned int u;
432 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +0200433 struct lysc_ext *elist = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100434
435 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
436 ext->insubstmt = ext_p->insubstmt;
437 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci0935f412019-08-20 16:15:18 +0200438 ext->parent = parent;
439 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100440
441 /* get module where the extension definition should be placed */
442 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100443 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100444 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
445 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
446 LY_EVALID);
447 LY_CHECK_ERR_RET(!mod->parsed->extensions,
448 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
449 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100450 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100451 LY_EVALID);
452 name = &ext_p->name[u + 1];
Radek Krejci0935f412019-08-20 16:15:18 +0200453
Radek Krejci19a96102018-11-15 13:38:09 +0100454 /* find the extension definition there */
Radek Krejci0935f412019-08-20 16:15:18 +0200455 if (mod->off_extensions) {
456 elist = mod->off_extensions;
457 } else {
458 elist = mod->compiled->extensions;
459 }
460 LY_ARRAY_FOR(elist, u) {
461 if (!strcmp(name, elist[u].name)) {
462 ext->def = &elist[u];
Radek Krejci19a96102018-11-15 13:38:09 +0100463 break;
464 }
465 }
Radek Krejci0935f412019-08-20 16:15:18 +0200466 LY_CHECK_ERR_RET(!ext->def,
467 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
468 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100469 LY_EVALID);
Radek Krejci0935f412019-08-20 16:15:18 +0200470
471 if (ext->def->plugin && ext->def->plugin->compile) {
472 LY_CHECK_RET(ext->def->plugin->compile(ctx, ext_p, ext),LY_EVALID);
473 }
474
475 return LY_SUCCESS;
476}
477
478/**
479 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
480 */
481static LY_ERR
482lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext)
483{
484 LY_ERR ret = LY_SUCCESS;
485
486 DUP_STRING(ctx->ctx, ext_p->name, ext->name);
487 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
488 ext->module = ctx->mod_def;
489 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done);
490
491done:
492 return ret;
493}
494
495/**
496 * @brief Link the extensions definitions with the available extension plugins.
497 *
498 * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules
499 * are not connected with even available extension plugins.
500 *
501 * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)).
502 */
503static void
504lys_compile_extension_plugins(struct lysc_ext *extensions)
505{
506 unsigned int u;
507
508 LY_ARRAY_FOR(extensions, u) {
509 extensions[u].plugin = lyext_get_plugin(&extensions[u]);
510 }
511}
512
513LY_ERR
514lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
515 struct lysp_ext *extensions_p, struct lysc_ext **extensions)
516{
517 unsigned int offset = 0, u;
518 struct lysc_ctx context = {0};
519
520 assert(ctx_sc || ctx);
521
522 if (!ctx_sc) {
523 context.ctx = ctx;
524 context.mod = module;
525 context.path_len = 1;
526 context.path[0] = '/';
527 ctx_sc = &context;
528 }
529
530 if (!extensions_p) {
531 return LY_SUCCESS;
532 }
533 if (*extensions) {
534 offset = LY_ARRAY_SIZE(*extensions);
535 }
536
537 lysc_update_path(ctx_sc, NULL, "{extension}");
538 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM);
539 LY_ARRAY_FOR(extensions_p, u) {
540 lysc_update_path(ctx_sc, NULL, extensions_p[u].name);
541 LY_ARRAY_INCREMENT(*extensions);
542 COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name);
543 LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u]));
544 lysc_update_path(ctx_sc, NULL, NULL);
545 }
546 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100547
548 return LY_SUCCESS;
549}
550
Radek Krejcib56c7502019-02-13 14:19:54 +0100551/**
552 * @brief Compile information from the if-feature statement
553 * @param[in] ctx Compile context.
554 * @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 +0100555 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
556 * @return LY_ERR value.
557 */
Radek Krejci19a96102018-11-15 13:38:09 +0100558static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200559lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100560{
561 const char *c = *value;
562 int r, rc = EXIT_FAILURE;
563 int i, j, last_not, checkversion = 0;
564 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
565 uint8_t op;
566 struct iff_stack stack = {0, 0, NULL};
567 struct lysc_feature *f;
568
569 assert(c);
570
571 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
572 for (i = j = last_not = 0; c[i]; i++) {
573 if (c[i] == '(') {
574 j++;
575 checkversion = 1;
576 continue;
577 } else if (c[i] == ')') {
578 j--;
579 continue;
580 } else if (isspace(c[i])) {
581 checkversion = 1;
582 continue;
583 }
584
585 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 +0200586 int sp;
587 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
588 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
590 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
591 return LY_EVALID;
592 } else if (!isspace(c[i + r])) {
593 /* feature name starting with the not/and/or */
594 last_not = 0;
595 f_size++;
596 } else if (c[i] == 'n') { /* not operation */
597 if (last_not) {
598 /* double not */
599 expr_size = expr_size - 2;
600 last_not = 0;
601 } else {
602 last_not = 1;
603 }
604 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200605 if (f_exp != f_size) {
606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
607 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
608 return LY_EVALID;
609 }
Radek Krejci19a96102018-11-15 13:38:09 +0100610 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200611
Radek Krejci19a96102018-11-15 13:38:09 +0100612 /* not a not operation */
613 last_not = 0;
614 }
615 i += r;
616 } else {
617 f_size++;
618 last_not = 0;
619 }
620 expr_size++;
621
622 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200623 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100624 i--;
625 break;
626 }
627 i++;
628 }
629 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200630 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100631 /* not matching count of ( and ) */
632 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
633 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
634 return LY_EVALID;
635 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200636 if (f_exp != f_size) {
637 /* features do not match the needed arguments for the logical operations */
638 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
639 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
640 "the required number of operands for the operations.", *value);
641 return LY_EVALID;
642 }
Radek Krejci19a96102018-11-15 13:38:09 +0100643
644 if (checkversion || expr_size > 1) {
645 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100646 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100647 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
648 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
649 return LY_EVALID;
650 }
651 }
652
653 /* allocate the memory */
654 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
655 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
656 stack.stack = malloc(expr_size * sizeof *stack.stack);
657 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
658
659 stack.size = expr_size;
660 f_size--; expr_size--; /* used as indexes from now */
661
662 for (i--; i >= 0; i--) {
663 if (c[i] == ')') {
664 /* push it on stack */
665 iff_stack_push(&stack, LYS_IFF_RP);
666 continue;
667 } else if (c[i] == '(') {
668 /* pop from the stack into result all operators until ) */
669 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
670 iff_setop(iff->expr, op, expr_size--);
671 }
672 continue;
673 } else if (isspace(c[i])) {
674 continue;
675 }
676
677 /* end of operator or operand -> find beginning and get what is it */
678 j = i + 1;
679 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
680 i--;
681 }
682 i++; /* go back by one step */
683
684 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
685 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
686 /* double not */
687 iff_stack_pop(&stack);
688 } else {
689 /* not has the highest priority, so do not pop from the stack
690 * as in case of AND and OR */
691 iff_stack_push(&stack, LYS_IFF_NOT);
692 }
693 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
694 /* as for OR - pop from the stack all operators with the same or higher
695 * priority and store them to the result, then push the AND to the stack */
696 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
697 op = iff_stack_pop(&stack);
698 iff_setop(iff->expr, op, expr_size--);
699 }
700 iff_stack_push(&stack, LYS_IFF_AND);
701 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
702 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
703 op = iff_stack_pop(&stack);
704 iff_setop(iff->expr, op, expr_size--);
705 }
706 iff_stack_push(&stack, LYS_IFF_OR);
707 } else {
708 /* feature name, length is j - i */
709
710 /* add it to the expression */
711 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
712
713 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100714 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
715 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
716 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
717 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100718 iff->features[f_size] = f;
719 LY_ARRAY_INCREMENT(iff->features);
720 f_size--;
721 }
722 }
723 while (stack.index) {
724 op = iff_stack_pop(&stack);
725 iff_setop(iff->expr, op, expr_size--);
726 }
727
728 if (++expr_size || ++f_size) {
729 /* not all expected operators and operands found */
730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
731 "Invalid value \"%s\" of if-feature - processing error.", *value);
732 rc = LY_EINT;
733 } else {
734 rc = LY_SUCCESS;
735 }
736
737error:
738 /* cleanup */
739 iff_stack_clean(&stack);
740
741 return rc;
742}
743
Radek Krejcib56c7502019-02-13 14:19:54 +0100744/**
745 * @brief Compile information from the when statement
746 * @param[in] ctx Compile context.
747 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100748 * @param[out] when Pointer where to store pointer to the created compiled when structure.
749 * @return LY_ERR value.
750 */
Radek Krejci19a96102018-11-15 13:38:09 +0100751static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200752lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100753{
Radek Krejci19a96102018-11-15 13:38:09 +0100754 LY_ERR ret = LY_SUCCESS;
755
Radek Krejci00b874b2019-02-12 10:54:50 +0100756 *when = calloc(1, sizeof **when);
757 (*when)->refcount = 1;
758 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
759 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
760 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
761 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200762 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100763
764done:
765 return ret;
766}
767
Radek Krejcib56c7502019-02-13 14:19:54 +0100768/**
769 * @brief Compile information from the must statement
770 * @param[in] ctx Compile context.
771 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100772 * @param[in,out] must Prepared (empty) compiled must structure to fill.
773 * @return LY_ERR value.
774 */
Radek Krejci19a96102018-11-15 13:38:09 +0100775static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200776lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100777{
Radek Krejci19a96102018-11-15 13:38:09 +0100778 LY_ERR ret = LY_SUCCESS;
779
780 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
781 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200782 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100783 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
784 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100785 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
786 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200787 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100788
789done:
790 return ret;
791}
792
Radek Krejcib56c7502019-02-13 14:19:54 +0100793/**
794 * @brief Compile information from the import statement
795 * @param[in] ctx Compile context.
796 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100797 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
798 * @return LY_ERR value.
799 */
Radek Krejci19a96102018-11-15 13:38:09 +0100800static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200801lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100802{
Radek Krejci19a96102018-11-15 13:38:09 +0100803 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100804 LY_ERR ret = LY_SUCCESS;
805
806 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200807 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100808 imp->module = imp_p->module;
809
Radek Krejci7f2a5362018-11-28 13:05:37 +0100810 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
811 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100812 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100813 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100814 /* try to use filepath if present */
815 if (imp->module->filepath) {
816 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
817 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100818 if (mod != imp->module) {
819 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100820 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100821 mod = NULL;
822 }
823 }
824 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100825 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100826 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 +0100827 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100828 return LY_ENOTFOUND;
829 }
830 }
Radek Krejci19a96102018-11-15 13:38:09 +0100831 }
832
833done:
834 return ret;
835}
836
Radek Krejcib56c7502019-02-13 14:19:54 +0100837/**
838 * @brief Compile information from the identity statement
839 *
840 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
841 *
842 * @param[in] ctx Compile context.
843 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100844 * @param[in] idents List of so far compiled identities to check the name uniqueness.
845 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
846 * @return LY_ERR value.
847 */
Radek Krejci19a96102018-11-15 13:38:09 +0100848static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200849lys_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 +0100850{
851 unsigned int u;
852 LY_ERR ret = LY_SUCCESS;
853
Radek Krejci327de162019-06-14 12:52:07 +0200854 lysc_update_path(ctx, NULL, ident_p->name);
855
Radek Krejcid05cbd92018-12-05 14:26:40 +0100856 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100857 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200858 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
859 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200860 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200861 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100862 /* 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 +0200863 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100864 ident->flags = ident_p->flags;
865
Radek Krejci327de162019-06-14 12:52:07 +0200866 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100867done:
868 return ret;
869}
870
Radek Krejcib56c7502019-02-13 14:19:54 +0100871/**
872 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
873 *
874 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
875 *
876 * @param[in] ctx Compile context for logging.
877 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
878 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
879 * @return LY_SUCCESS if everything is ok.
880 * @return LY_EVALID if the identity is derived from itself.
881 */
Radek Krejci38222632019-02-12 16:55:05 +0100882static LY_ERR
883lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
884{
885 LY_ERR ret = LY_EVALID;
886 unsigned int u, v;
887 struct ly_set recursion = {0};
888 struct lysc_ident *drv;
889
890 if (!derived) {
891 return LY_SUCCESS;
892 }
893
894 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
895 if (ident == derived[u]) {
896 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
897 "Identity \"%s\" is indirectly derived from itself.", ident->name);
898 goto cleanup;
899 }
900 ly_set_add(&recursion, derived[u], 0);
901 }
902
903 for (v = 0; v < recursion.count; ++v) {
904 drv = recursion.objs[v];
905 if (!drv->derived) {
906 continue;
907 }
908 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
909 if (ident == drv->derived[u]) {
910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
911 "Identity \"%s\" is indirectly derived from itself.", ident->name);
912 goto cleanup;
913 }
914 ly_set_add(&recursion, drv->derived[u], 0);
915 }
916 }
917 ret = LY_SUCCESS;
918
919cleanup:
920 ly_set_erase(&recursion, NULL);
921 return ret;
922}
923
Radek Krejcia3045382018-11-22 14:30:31 +0100924/**
925 * @brief Find and process the referenced base identities from another identity or identityref
926 *
Radek Krejciaca74032019-06-04 08:53:06 +0200927 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +0100928 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
929 * to distinguish these two use cases.
930 *
931 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
932 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
933 * @param[in] ident Referencing identity to work with.
934 * @param[in] bases Array of bases of identityref to fill in.
935 * @return LY_ERR value.
936 */
Radek Krejci19a96102018-11-15 13:38:09 +0100937static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100938lys_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 +0100939{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100940 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100941 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100942 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100943 struct lysc_ident **idref;
944
945 assert(ident || bases);
946
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100947 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
949 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
950 return LY_EVALID;
951 }
952
953 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
954 s = strchr(bases_p[u], ':');
955 if (s) {
956 /* prefixed identity */
957 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100958 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100959 } else {
960 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100961 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100962 }
963 if (!mod) {
964 if (ident) {
965 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
966 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
967 } else {
968 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
969 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
970 }
971 return LY_EVALID;
972 }
973 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100974 if (mod->compiled && mod->compiled->identities) {
975 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
976 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100977 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100978 if (ident == &mod->compiled->identities[v]) {
979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
980 "Identity \"%s\" is derived from itself.", ident->name);
981 return LY_EVALID;
982 }
983 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100984 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100985 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100986 *idref = ident;
987 } else {
988 /* we have match! store the found identity */
989 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100990 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100991 }
992 break;
993 }
994 }
995 }
996 if (!idref || !(*idref)) {
997 if (ident) {
998 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
999 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1000 } else {
1001 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1002 "Unable to find base (%s) of identityref.", bases_p[u]);
1003 }
1004 return LY_EVALID;
1005 }
1006 }
1007 return LY_SUCCESS;
1008}
1009
Radek Krejcia3045382018-11-22 14:30:31 +01001010/**
1011 * @brief For the given array of identities, set the backlinks from all their base identities.
1012 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1013 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1014 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1015 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1016 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001017static LY_ERR
1018lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1019{
1020 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +01001021
1022 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
1023 if (!idents_p[i].bases) {
1024 continue;
1025 }
Radek Krejci327de162019-06-14 12:52:07 +02001026 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001027 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001028 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001029 }
1030 return LY_SUCCESS;
1031}
1032
Radek Krejci0af46292019-01-11 16:02:31 +01001033LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001034lys_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 +01001035{
1036 unsigned int offset = 0, u;
1037 struct lysc_ctx context = {0};
1038
Radek Krejci327de162019-06-14 12:52:07 +02001039 assert(ctx_sc || ctx);
1040
1041 if (!ctx_sc) {
1042 context.ctx = ctx;
1043 context.mod = module;
1044 context.path_len = 1;
1045 context.path[0] = '/';
1046 ctx_sc = &context;
1047 }
Radek Krejci0af46292019-01-11 16:02:31 +01001048
1049 if (!features_p) {
1050 return LY_SUCCESS;
1051 }
1052 if (*features) {
1053 offset = LY_ARRAY_SIZE(*features);
1054 }
1055
Radek Krejci327de162019-06-14 12:52:07 +02001056 lysc_update_path(ctx_sc, NULL, "{feature}");
1057 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001058 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001059 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1060
Radek Krejci0af46292019-01-11 16:02:31 +01001061 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +02001062 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
1063 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1064 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1065 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001066 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001067 (*features)[offset + u].module = ctx_sc->mod;
1068
1069 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001070 }
Radek Krejci327de162019-06-14 12:52:07 +02001071 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001072
1073 return LY_SUCCESS;
1074}
1075
Radek Krejcia3045382018-11-22 14:30:31 +01001076/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001077 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001078 *
1079 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1080 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001081 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001082 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1083 * being currently processed).
1084 * @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 +01001085 * @return LY_SUCCESS if everything is ok.
1086 * @return LY_EVALID if the feature references indirectly itself.
1087 */
1088static LY_ERR
1089lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1090{
1091 LY_ERR ret = LY_EVALID;
1092 unsigned int u, v;
1093 struct ly_set recursion = {0};
1094 struct lysc_feature *drv;
1095
1096 if (!depfeatures) {
1097 return LY_SUCCESS;
1098 }
1099
1100 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1101 if (feature == depfeatures[u]) {
1102 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1103 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1104 goto cleanup;
1105 }
1106 ly_set_add(&recursion, depfeatures[u], 0);
1107 }
1108
1109 for (v = 0; v < recursion.count; ++v) {
1110 drv = recursion.objs[v];
1111 if (!drv->depfeatures) {
1112 continue;
1113 }
1114 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1115 if (feature == drv->depfeatures[u]) {
1116 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1117 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1118 goto cleanup;
1119 }
1120 ly_set_add(&recursion, drv->depfeatures[u], 0);
1121 }
1122 }
1123 ret = LY_SUCCESS;
1124
1125cleanup:
1126 ly_set_erase(&recursion, NULL);
1127 return ret;
1128}
1129
1130/**
Radek Krejci0af46292019-01-11 16:02:31 +01001131 * @brief Create pre-compiled features array.
1132 *
1133 * See lys_feature_precompile() for more details.
1134 *
Radek Krejcia3045382018-11-22 14:30:31 +01001135 * @param[in] ctx Compile context.
1136 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001137 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001138 * @return LY_ERR value.
1139 */
Radek Krejci19a96102018-11-15 13:38:09 +01001140static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001141lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001142{
Radek Krejci0af46292019-01-11 16:02:31 +01001143 unsigned int u, v, x;
1144 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001145 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001146
Radek Krejci327de162019-06-14 12:52:07 +02001147
Radek Krejci0af46292019-01-11 16:02:31 +01001148 /* find the preprecompiled feature */
1149 LY_ARRAY_FOR(features, x) {
1150 if (strcmp(features[x].name, feature_p->name)) {
1151 continue;
1152 }
1153 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001154 lysc_update_path(ctx, NULL, "{feature}");
1155 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001156
Radek Krejci0af46292019-01-11 16:02:31 +01001157 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001158 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001159 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001160 if (feature->iffeatures) {
1161 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1162 if (feature->iffeatures[u].features) {
1163 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001164 /* check for circular dependency - direct reference first,... */
1165 if (feature == feature->iffeatures[u].features[v]) {
1166 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1167 "Feature \"%s\" is referenced from itself.", feature->name);
1168 return LY_EVALID;
1169 }
1170 /* ... and indirect circular reference */
1171 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1172
Radek Krejci0af46292019-01-11 16:02:31 +01001173 /* add itself into the dependants list */
1174 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1175 *df = feature;
1176 }
Radek Krejci19a96102018-11-15 13:38:09 +01001177 }
Radek Krejci19a96102018-11-15 13:38:09 +01001178 }
1179 }
Radek Krejci327de162019-06-14 12:52:07 +02001180 lysc_update_path(ctx, NULL, NULL);
1181 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001182 done:
1183 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001184 }
Radek Krejci0af46292019-01-11 16:02:31 +01001185
1186 LOGINT(ctx->ctx);
1187 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001188}
1189
Radek Krejcib56c7502019-02-13 14:19:54 +01001190/**
1191 * @brief Revert compiled list of features back to the precompiled state.
1192 *
1193 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1194 * The features are supposed to be stored again as off_features in ::lys_module structure.
1195 *
1196 * @param[in] ctx Compilation context.
1197 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1198 * and supposed to hold the off_features list.
1199 */
Radek Krejci95710c92019-02-11 15:49:55 +01001200static void
1201lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1202{
1203 unsigned int u, v;
1204
1205 /* keep the off_features list until the complete lys_module is freed */
1206 mod->off_features = mod->compiled->features;
1207 mod->compiled->features = NULL;
1208
1209 /* in the off_features list, remove all the parts (from finished compiling process)
1210 * which may points into the data being freed here */
1211 LY_ARRAY_FOR(mod->off_features, u) {
1212 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1213 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1214 }
1215 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1216 mod->off_features[u].iffeatures = NULL;
1217
1218 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1219 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1220 }
1221 LY_ARRAY_FREE(mod->off_features[u].exts);
1222 mod->off_features[u].exts = NULL;
1223 }
1224}
1225
Radek Krejcia3045382018-11-22 14:30:31 +01001226/**
1227 * @brief Validate and normalize numeric value from a range definition.
1228 * @param[in] ctx Compile context.
1229 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1230 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1231 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1232 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1233 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1234 * @param[in] value String value of the range boundary.
1235 * @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.
1236 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1237 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1238 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001239LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001240range_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 +01001241{
Radek Krejci6cba4292018-11-15 17:33:29 +01001242 size_t fraction = 0, size;
1243
Radek Krejci19a96102018-11-15 13:38:09 +01001244 *len = 0;
1245
1246 assert(value);
1247 /* parse value */
1248 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1249 return LY_EVALID;
1250 }
1251
1252 if ((value[*len] == '-') || (value[*len] == '+')) {
1253 ++(*len);
1254 }
1255
1256 while (isdigit(value[*len])) {
1257 ++(*len);
1258 }
1259
1260 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001261 if (basetype == LY_TYPE_DEC64) {
1262 goto decimal;
1263 } else {
1264 *valcopy = strndup(value, *len);
1265 return LY_SUCCESS;
1266 }
Radek Krejci19a96102018-11-15 13:38:09 +01001267 }
1268 fraction = *len;
1269
1270 ++(*len);
1271 while (isdigit(value[*len])) {
1272 ++(*len);
1273 }
1274
Radek Krejci6cba4292018-11-15 17:33:29 +01001275 if (basetype == LY_TYPE_DEC64) {
1276decimal:
1277 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001278 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001279 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1280 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1281 *len, value, frdigits);
1282 return LY_EINVAL;
1283 }
1284 if (fraction) {
1285 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1286 } else {
1287 size = (*len) + frdigits + 1;
1288 }
1289 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001290 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1291
Radek Krejci6cba4292018-11-15 17:33:29 +01001292 (*valcopy)[size - 1] = '\0';
1293 if (fraction) {
1294 memcpy(&(*valcopy)[0], &value[0], fraction);
1295 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1296 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1297 } else {
1298 memcpy(&(*valcopy)[0], &value[0], *len);
1299 memset(&(*valcopy)[*len], '0', frdigits);
1300 }
Radek Krejci19a96102018-11-15 13:38:09 +01001301 }
1302 return LY_SUCCESS;
1303}
1304
Radek Krejcia3045382018-11-22 14:30:31 +01001305/**
1306 * @brief Check that values in range are in ascendant order.
1307 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001308 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1309 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001310 * @param[in] value Current value to check.
1311 * @param[in] prev_value The last seen value.
1312 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1313 */
Radek Krejci19a96102018-11-15 13:38:09 +01001314static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001315range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001316{
1317 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001318 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001319 return LY_EEXIST;
1320 }
1321 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001322 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001323 return LY_EEXIST;
1324 }
1325 }
1326 return LY_SUCCESS;
1327}
1328
Radek Krejcia3045382018-11-22 14:30:31 +01001329/**
1330 * @brief Set min/max value of the range part.
1331 * @param[in] ctx Compile context.
1332 * @param[in] part Range part structure to fill.
1333 * @param[in] max Flag to distinguish if storing min or max value.
1334 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1335 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1336 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1337 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1338 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001339 * @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 +01001340 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1341 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1342 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1343 * frdigits value), LY_EMEM.
1344 */
Radek Krejci19a96102018-11-15 13:38:09 +01001345static LY_ERR
1346range_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 +01001347 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001348{
1349 LY_ERR ret = LY_SUCCESS;
1350 char *valcopy = NULL;
1351 size_t len;
1352
1353 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001354 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001355 LY_CHECK_GOTO(ret, finalize);
1356 }
1357 if (!valcopy && base_range) {
1358 if (max) {
1359 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1360 } else {
1361 part->min_64 = base_range->parts[0].min_64;
1362 }
1363 if (!first) {
1364 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1365 }
1366 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001367 }
1368
1369 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001370 case LY_TYPE_INT8: /* range */
1371 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001372 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 +01001373 } else if (max) {
1374 part->max_64 = INT64_C(127);
1375 } else {
1376 part->min_64 = INT64_C(-128);
1377 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001378 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001379 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001380 }
1381 break;
1382 case LY_TYPE_INT16: /* range */
1383 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001384 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 +01001385 } else if (max) {
1386 part->max_64 = INT64_C(32767);
1387 } else {
1388 part->min_64 = INT64_C(-32768);
1389 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001390 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001391 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001392 }
1393 break;
1394 case LY_TYPE_INT32: /* range */
1395 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001396 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 +01001397 } else if (max) {
1398 part->max_64 = INT64_C(2147483647);
1399 } else {
1400 part->min_64 = INT64_C(-2147483648);
1401 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001402 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001403 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001404 }
1405 break;
1406 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001407 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001408 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001409 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001410 max ? &part->max_64 : &part->min_64);
1411 } else if (max) {
1412 part->max_64 = INT64_C(9223372036854775807);
1413 } else {
1414 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1415 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001416 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001417 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001418 }
1419 break;
1420 case LY_TYPE_UINT8: /* range */
1421 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001422 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 +01001423 } else if (max) {
1424 part->max_u64 = UINT64_C(255);
1425 } else {
1426 part->min_u64 = UINT64_C(0);
1427 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001428 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001429 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001430 }
1431 break;
1432 case LY_TYPE_UINT16: /* range */
1433 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001434 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 +01001435 } else if (max) {
1436 part->max_u64 = UINT64_C(65535);
1437 } else {
1438 part->min_u64 = UINT64_C(0);
1439 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001440 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001441 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001442 }
1443 break;
1444 case LY_TYPE_UINT32: /* range */
1445 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001446 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 +01001447 } else if (max) {
1448 part->max_u64 = UINT64_C(4294967295);
1449 } else {
1450 part->min_u64 = UINT64_C(0);
1451 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001452 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001453 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001454 }
1455 break;
1456 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001457 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001458 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001459 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001460 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 +01001461 } else if (max) {
1462 part->max_u64 = UINT64_C(18446744073709551615);
1463 } else {
1464 part->min_u64 = UINT64_C(0);
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(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001468 }
1469 break;
1470 default:
1471 LOGINT(ctx->ctx);
1472 ret = LY_EINT;
1473 }
1474
Radek Krejci5969f272018-11-23 10:03:58 +01001475finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001476 if (ret == LY_EDENIED) {
1477 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1478 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1479 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1480 } else if (ret == LY_EVALID) {
1481 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1482 "Invalid %s restriction - invalid value \"%s\".",
1483 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1484 } else if (ret == LY_EEXIST) {
1485 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1486 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001487 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001488 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001489 } else if (!ret && value) {
1490 *value = *value + len;
1491 }
1492 free(valcopy);
1493 return ret;
1494}
1495
Radek Krejcia3045382018-11-22 14:30:31 +01001496/**
1497 * @brief Compile the parsed range restriction.
1498 * @param[in] ctx Compile context.
1499 * @param[in] range_p Parsed range structure to compile.
1500 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1501 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1502 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1503 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1504 * range restriction must be more restrictive than the base_range.
1505 * @param[in,out] range Pointer to the created current range structure.
1506 * @return LY_ERR value.
1507 */
Radek Krejci19a96102018-11-15 13:38:09 +01001508static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001509lys_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 +01001510 struct lysc_range *base_range, struct lysc_range **range)
1511{
1512 LY_ERR ret = LY_EVALID;
1513 const char *expr;
1514 struct lysc_range_part *parts = NULL, *part;
1515 int range_expected = 0, uns;
1516 unsigned int parts_done = 0, u, v;
1517
1518 assert(range);
1519 assert(range_p);
1520
1521 expr = range_p->arg;
1522 while(1) {
1523 if (isspace(*expr)) {
1524 ++expr;
1525 } else if (*expr == '\0') {
1526 if (range_expected) {
1527 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1528 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1529 length_restr ? "length" : "range", range_p->arg);
1530 goto cleanup;
1531 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1533 "Invalid %s restriction - unexpected end of the expression (%s).",
1534 length_restr ? "length" : "range", range_p->arg);
1535 goto cleanup;
1536 }
1537 parts_done++;
1538 break;
1539 } else if (!strncmp(expr, "min", 3)) {
1540 if (parts) {
1541 /* min cannot be used elsewhere than in the first part */
1542 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1543 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1544 expr - range_p->arg, range_p->arg);
1545 goto cleanup;
1546 }
1547 expr += 3;
1548
1549 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001550 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 +01001551 part->max_64 = part->min_64;
1552 } else if (*expr == '|') {
1553 if (!parts || range_expected) {
1554 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1555 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1556 goto cleanup;
1557 }
1558 expr++;
1559 parts_done++;
1560 /* process next part of the expression */
1561 } else if (!strncmp(expr, "..", 2)) {
1562 expr += 2;
1563 while (isspace(*expr)) {
1564 expr++;
1565 }
1566 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1567 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1568 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1569 goto cleanup;
1570 }
1571 /* continue expecting the upper boundary */
1572 range_expected = 1;
1573 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1574 /* number */
1575 if (range_expected) {
1576 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001577 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 +01001578 range_expected = 0;
1579 } else {
1580 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1581 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 +01001582 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001583 part->max_64 = part->min_64;
1584 }
1585
1586 /* continue with possible another expression part */
1587 } else if (!strncmp(expr, "max", 3)) {
1588 expr += 3;
1589 while (isspace(*expr)) {
1590 expr++;
1591 }
1592 if (*expr != '\0') {
1593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1594 length_restr ? "length" : "range", expr);
1595 goto cleanup;
1596 }
1597 if (range_expected) {
1598 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001599 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 +01001600 range_expected = 0;
1601 } else {
1602 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1603 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 +01001604 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001605 part->min_64 = part->max_64;
1606 }
1607 } else {
1608 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1609 length_restr ? "length" : "range", expr);
1610 goto cleanup;
1611 }
1612 }
1613
1614 /* check with the previous range/length restriction */
1615 if (base_range) {
1616 switch (basetype) {
1617 case LY_TYPE_BINARY:
1618 case LY_TYPE_UINT8:
1619 case LY_TYPE_UINT16:
1620 case LY_TYPE_UINT32:
1621 case LY_TYPE_UINT64:
1622 case LY_TYPE_STRING:
1623 uns = 1;
1624 break;
1625 case LY_TYPE_DEC64:
1626 case LY_TYPE_INT8:
1627 case LY_TYPE_INT16:
1628 case LY_TYPE_INT32:
1629 case LY_TYPE_INT64:
1630 uns = 0;
1631 break;
1632 default:
1633 LOGINT(ctx->ctx);
1634 ret = LY_EINT;
1635 goto cleanup;
1636 }
1637 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1638 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1639 goto baseerror;
1640 }
1641 /* current lower bound is not lower than the base */
1642 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1643 /* base has single value */
1644 if (base_range->parts[v].min_64 == parts[u].min_64) {
1645 /* both lower bounds are the same */
1646 if (parts[u].min_64 != parts[u].max_64) {
1647 /* current continues with a range */
1648 goto baseerror;
1649 } else {
1650 /* equal single values, move both forward */
1651 ++v;
1652 continue;
1653 }
1654 } else {
1655 /* base is single value lower than current range, so the
1656 * value from base range is removed in the current,
1657 * move only base and repeat checking */
1658 ++v;
1659 --u;
1660 continue;
1661 }
1662 } else {
1663 /* base is the range */
1664 if (parts[u].min_64 == parts[u].max_64) {
1665 /* current is a single value */
1666 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1667 /* current is behind the base range, so base range is omitted,
1668 * move the base and keep the current for further check */
1669 ++v;
1670 --u;
1671 } /* else it is within the base range, so move the current, but keep the base */
1672 continue;
1673 } else {
1674 /* both are ranges - check the higher bound, the lower was already checked */
1675 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1676 /* higher bound is higher than the current higher bound */
1677 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1678 /* but the current lower bound is also higher, so the base range is omitted,
1679 * continue with the same current, but move the base */
1680 --u;
1681 ++v;
1682 continue;
1683 }
1684 /* current range starts within the base range but end behind it */
1685 goto baseerror;
1686 } else {
1687 /* current range is smaller than the base,
1688 * move current, but stay with the base */
1689 continue;
1690 }
1691 }
1692 }
1693 }
1694 if (u != parts_done) {
1695baseerror:
1696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1697 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1698 length_restr ? "length" : "range", range_p->arg);
1699 goto cleanup;
1700 }
1701 }
1702
1703 if (!(*range)) {
1704 *range = calloc(1, sizeof **range);
1705 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1706 }
1707
Radek Krejcic8b31002019-01-08 10:24:45 +01001708 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001709 if (range_p->eapptag) {
1710 lydict_remove(ctx->ctx, (*range)->eapptag);
1711 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1712 }
1713 if (range_p->emsg) {
1714 lydict_remove(ctx->ctx, (*range)->emsg);
1715 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1716 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001717 if (range_p->dsc) {
1718 lydict_remove(ctx->ctx, (*range)->dsc);
1719 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1720 }
1721 if (range_p->ref) {
1722 lydict_remove(ctx->ctx, (*range)->ref);
1723 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1724 }
Radek Krejci19a96102018-11-15 13:38:09 +01001725 /* extensions are taken only from the last range by the caller */
1726
1727 (*range)->parts = parts;
1728 parts = NULL;
1729 ret = LY_SUCCESS;
1730cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001731 LY_ARRAY_FREE(parts);
1732
1733 return ret;
1734}
1735
1736/**
1737 * @brief Checks pattern syntax.
1738 *
Radek Krejcia3045382018-11-22 14:30:31 +01001739 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001740 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001741 * @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 +01001742 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001743 */
1744static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001745lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001746{
Radek Krejci54579462019-04-30 12:47:06 +02001747 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001748 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001749 int err_code;
1750 const char *orig_ptr;
1751 PCRE2_SIZE err_offset;
1752 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001753#define URANGE_LEN 19
1754 char *ublock2urange[][2] = {
1755 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1756 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1757 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1758 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1759 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1760 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1761 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1762 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1763 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1764 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1765 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1766 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1767 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1768 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1769 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1770 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1771 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1772 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1773 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1774 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1775 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1776 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1777 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1778 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1779 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1780 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1781 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1782 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1783 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1784 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1785 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1786 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1787 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1788 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1789 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1790 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1791 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1792 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1793 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1794 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1795 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1796 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1797 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1798 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1799 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1800 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1801 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1802 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1803 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1804 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1805 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1806 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1807 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1808 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1809 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1810 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1811 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1812 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1813 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1814 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1815 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1816 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1817 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1818 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1819 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1820 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1821 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1822 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1823 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1824 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1825 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1826 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1827 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1828 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1829 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1830 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1831 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1832 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1833 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1834 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1835 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1836 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1837 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1838 {NULL, NULL}
1839 };
1840
1841 /* adjust the expression to a Perl equivalent
1842 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1843
1844 /* we need to replace all "$" with "\$", count them now */
1845 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1846
1847 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1848 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1849 perl_regex[0] = '\0';
1850
1851 ptr = perl_regex;
1852
Radek Krejci19a96102018-11-15 13:38:09 +01001853 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1854 if (orig_ptr[0] == '$') {
1855 ptr += sprintf(ptr, "\\$");
1856 } else if (orig_ptr[0] == '^') {
1857 ptr += sprintf(ptr, "\\^");
1858 } else {
1859 ptr[0] = orig_ptr[0];
1860 ++ptr;
1861 }
1862 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001863 ptr[0] = '\0';
1864 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001865
1866 /* substitute Unicode Character Blocks with exact Character Ranges */
1867 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1868 start = ptr - perl_regex;
1869
1870 ptr = strchr(ptr, '}');
1871 if (!ptr) {
1872 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1873 pattern, perl_regex + start + 2, "unterminated character property");
1874 free(perl_regex);
1875 return LY_EVALID;
1876 }
1877 end = (ptr - perl_regex) + 1;
1878
1879 /* need more space */
1880 if (end - start < URANGE_LEN) {
1881 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1882 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1883 }
1884
1885 /* find our range */
1886 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1887 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1888 break;
1889 }
1890 }
1891 if (!ublock2urange[idx][0]) {
1892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1893 pattern, perl_regex + start + 5, "unknown block name");
1894 free(perl_regex);
1895 return LY_EVALID;
1896 }
1897
1898 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1899 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1900 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1901 ++count;
1902 }
1903 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1904 --count;
1905 }
1906 }
1907 if (count) {
1908 /* skip brackets */
1909 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1910 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1911 } else {
1912 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1913 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1914 }
1915 }
1916
1917 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02001918 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1919 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02001920 &err_code, &err_offset, NULL);
1921 if (!code_local) {
1922 PCRE2_UCHAR err_msg[256] = {0};
1923 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001924 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1925 free(perl_regex);
1926 return LY_EVALID;
1927 }
1928 free(perl_regex);
1929
Radek Krejci54579462019-04-30 12:47:06 +02001930 if (code) {
1931 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001932 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001933 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001934 }
1935
1936 return LY_SUCCESS;
1937
1938#undef URANGE_LEN
1939}
1940
Radek Krejcia3045382018-11-22 14:30:31 +01001941/**
1942 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1943 * @param[in] ctx Compile context.
1944 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001945 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1946 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1947 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1948 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1949 */
Radek Krejci19a96102018-11-15 13:38:09 +01001950static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001951lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01001952 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1953{
1954 struct lysc_pattern **pattern;
Radek Krejci0935f412019-08-20 16:15:18 +02001955 unsigned int u;
Radek Krejci19a96102018-11-15 13:38:09 +01001956 LY_ERR ret = LY_SUCCESS;
1957
1958 /* first, copy the patterns from the base type */
1959 if (base_patterns) {
1960 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1961 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1962 }
1963
1964 LY_ARRAY_FOR(patterns_p, u) {
1965 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1966 *pattern = calloc(1, sizeof **pattern);
1967 ++(*pattern)->refcount;
1968
Radek Krejci54579462019-04-30 12:47:06 +02001969 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01001970 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01001971
1972 if (patterns_p[u].arg[0] == 0x15) {
1973 (*pattern)->inverted = 1;
1974 }
Radek Krejci54579462019-04-30 12:47:06 +02001975 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01001976 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1977 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001978 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1979 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02001980 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01001981 }
1982done:
1983 return ret;
1984}
1985
Radek Krejcia3045382018-11-22 14:30:31 +01001986/**
1987 * @brief map of the possible restrictions combination for the specific built-in type.
1988 */
Radek Krejci19a96102018-11-15 13:38:09 +01001989static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1990 0 /* LY_TYPE_UNKNOWN */,
1991 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001992 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1993 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1994 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1995 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1996 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001997 LYS_SET_BIT /* LY_TYPE_BITS */,
1998 0 /* LY_TYPE_BOOL */,
1999 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2000 0 /* LY_TYPE_EMPTY */,
2001 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2002 LYS_SET_BASE /* LY_TYPE_IDENT */,
2003 LYS_SET_REQINST /* LY_TYPE_INST */,
2004 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002005 LYS_SET_TYPE /* LY_TYPE_UNION */,
2006 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002007 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002008 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002009 LYS_SET_RANGE /* LY_TYPE_INT64 */
2010};
2011
2012/**
2013 * @brief stringification of the YANG built-in data types
2014 */
2015const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2016 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2017 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002018};
2019
Radek Krejcia3045382018-11-22 14:30:31 +01002020/**
2021 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2022 * @param[in] ctx Compile context.
2023 * @param[in] enums_p Array of the parsed enum structures to compile.
2024 * @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 +01002025 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2026 * @param[out] enums Newly created array of the compiled enums information for the current type.
2027 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2028 */
Radek Krejci19a96102018-11-15 13:38:09 +01002029static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002030lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002031 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002032{
2033 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02002034 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002035 int32_t value = 0;
2036 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002037 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002038
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002039 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2041 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2042 return LY_EVALID;
2043 }
2044
2045 LY_ARRAY_FOR(enums_p, u) {
2046 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2047 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002048 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2049 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002050 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002051 if (base_enums) {
2052 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2053 LY_ARRAY_FOR(base_enums, v) {
2054 if (!strcmp(e->name, base_enums[v].name)) {
2055 break;
2056 }
2057 }
2058 if (v == LY_ARRAY_SIZE(base_enums)) {
2059 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2060 "Invalid %s - derived type adds new item \"%s\".",
2061 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2062 return LY_EVALID;
2063 }
2064 match = v;
2065 }
2066
2067 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002068 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002069 if (enums_p[u].flags & LYS_SET_VALUE) {
2070 e->value = (int32_t)enums_p[u].value;
2071 if (!u || e->value >= value) {
2072 value = e->value + 1;
2073 }
2074 /* check collision with other values */
2075 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2076 if (e->value == (*enums)[v].value) {
2077 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2078 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2079 e->value, e->name, (*enums)[v].name);
2080 return LY_EVALID;
2081 }
2082 }
2083 } else if (base_enums) {
2084 /* inherit the assigned value */
2085 e->value = base_enums[match].value;
2086 if (!u || e->value >= value) {
2087 value = e->value + 1;
2088 }
2089 } else {
2090 /* assign value automatically */
2091 if (u && value == INT32_MIN) {
2092 /* counter overflow */
2093 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2094 "Invalid enumeration - it is not possible to auto-assign enum value for "
2095 "\"%s\" since the highest value is already 2147483647.", e->name);
2096 return LY_EVALID;
2097 }
2098 e->value = value++;
2099 }
2100 } else { /* LY_TYPE_BITS */
2101 if (enums_p[u].flags & LYS_SET_VALUE) {
2102 e->value = (int32_t)enums_p[u].value;
2103 if (!u || (uint32_t)e->value >= position) {
2104 position = (uint32_t)e->value + 1;
2105 }
2106 /* check collision with other values */
2107 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2108 if (e->value == (*enums)[v].value) {
2109 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2110 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2111 (uint32_t)e->value, e->name, (*enums)[v].name);
2112 return LY_EVALID;
2113 }
2114 }
2115 } else if (base_enums) {
2116 /* inherit the assigned value */
2117 e->value = base_enums[match].value;
2118 if (!u || (uint32_t)e->value >= position) {
2119 position = (uint32_t)e->value + 1;
2120 }
2121 } else {
2122 /* assign value automatically */
2123 if (u && position == 0) {
2124 /* counter overflow */
2125 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2126 "Invalid bits - it is not possible to auto-assign bit position for "
2127 "\"%s\" since the highest value is already 4294967295.", e->name);
2128 return LY_EVALID;
2129 }
2130 e->value = position++;
2131 }
2132 }
2133
2134 if (base_enums) {
2135 /* the assigned values must not change from the derived type */
2136 if (e->value != base_enums[match].value) {
2137 if (basetype == LY_TYPE_ENUM) {
2138 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2139 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2140 e->name, base_enums[match].value, e->value);
2141 } else {
2142 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2143 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2144 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2145 }
2146 return LY_EVALID;
2147 }
2148 }
2149
Radek Krejciec4da802019-05-02 13:02:41 +02002150 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002151 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 +01002152
2153 if (basetype == LY_TYPE_BITS) {
2154 /* keep bits ordered by position */
2155 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2156 if (v != u) {
2157 memcpy(&storage, e, sizeof *e);
2158 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2159 memcpy(&(*enums)[v], &storage, sizeof storage);
2160 }
2161 }
2162 }
2163
2164done:
2165 return ret;
2166}
2167
Radek Krejcia3045382018-11-22 14:30:31 +01002168#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2169 for ((NODE) = (NODE)->parent; \
2170 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
2171 (NODE) = (NODE)->parent); \
2172 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2173 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2174 TERM; \
2175 }
2176
2177/**
2178 * @brief Validate the predicate(s) from the leafref path.
2179 * @param[in] ctx Compile context
2180 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2181 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002182 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002183 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002184 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002185 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2186 */
2187static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002188lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002189 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002190{
2191 LY_ERR ret = LY_EVALID;
2192 const struct lys_module *mod;
2193 const struct lysc_node *src_node, *dst_node;
2194 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2195 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002196 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002197 const char *start, *end, *pke_end;
2198 struct ly_set keys = {0};
2199 int i;
2200
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002201 assert(path_context);
2202
Radek Krejcia3045382018-11-22 14:30:31 +01002203 while (**predicate == '[') {
2204 start = (*predicate)++;
2205
2206 while (isspace(**predicate)) {
2207 ++(*predicate);
2208 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002209 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002210 while (isspace(**predicate)) {
2211 ++(*predicate);
2212 }
2213 if (**predicate != '=') {
2214 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002215 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2216 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002217 goto cleanup;
2218 }
2219 ++(*predicate);
2220 while (isspace(**predicate)) {
2221 ++(*predicate);
2222 }
2223
2224 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2226 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2227 goto cleanup;
2228 }
2229 --pke_end;
2230 while (isspace(*pke_end)) {
2231 --pke_end;
2232 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002233 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002234 /* localize path-key-expr */
2235 pke_start = path_key_expr = *predicate;
2236 /* move after the current predicate */
2237 *predicate = end + 1;
2238
2239 /* source (must be leaf or leaf-list) */
2240 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002241 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002242 if (!mod) {
2243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2244 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002245 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002246 goto cleanup;
2247 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002248 if (!mod->implemented) {
2249 /* make the module implemented */
2250 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2251 }
Radek Krejcia3045382018-11-22 14:30:31 +01002252 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002253 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002254 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002255 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002256 if (!(context_node->flags & LYS_KEYLESS)) {
2257 struct lysc_node *key;
2258 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
2259 if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') {
2260 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002261 break;
2262 }
2263 }
2264 }
Radek Krejcia3045382018-11-22 14:30:31 +01002265 if (!src_node) {
2266 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002267 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002268 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002269 goto cleanup;
2270 }
Radek Krejcia3045382018-11-22 14:30:31 +01002271
2272 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002273 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002274 i = ly_set_add(&keys, (void*)src_node, 0);
2275 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002276 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002278 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002279 *predicate - start, start, src_node->name);
2280 goto cleanup;
2281 }
2282
2283 /* destination */
2284 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002285 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002286
2287 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002288 if (strncmp(path_key_expr, "current", 7)) {
2289error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2291 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2292 *predicate - start, start);
2293 goto cleanup;
2294 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002295 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2296 if (*path_key_expr != '(') {
2297 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002298 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002299 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2300 if (*path_key_expr != ')') {
2301 goto error_current_function_invocation;
2302 }
2303 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002304
2305 if (*path_key_expr != '/') {
2306 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2307 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2308 *predicate - start, start);
2309 goto cleanup;
2310 }
2311 ++path_key_expr;
2312 while (isspace(*path_key_expr)) {
2313 ++path_key_expr;
2314 }
2315
2316 /* rel-path-keyexpr:
2317 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2318 while (!strncmp(path_key_expr, "..", 2)) {
2319 ++dest_parent_times;
2320 path_key_expr += 2;
2321 while (isspace(*path_key_expr)) {
2322 ++path_key_expr;
2323 }
2324 if (*path_key_expr != '/') {
2325 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2326 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2327 *predicate - start, start);
2328 goto cleanup;
2329 }
2330 ++path_key_expr;
2331 while (isspace(*path_key_expr)) {
2332 ++path_key_expr;
2333 }
2334
2335 /* path is supposed to be evaluated in data tree, so we have to skip
2336 * all schema nodes that cannot be instantiated in data tree */
2337 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2338 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2339 *predicate - start, start);
2340 }
2341 if (!dest_parent_times) {
2342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2343 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2344 *predicate - start, start);
2345 goto cleanup;
2346 }
2347 if (path_key_expr == pke_end) {
2348 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2349 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2350 *predicate - start, start);
2351 goto cleanup;
2352 }
2353
2354 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002355 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002356 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002357 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002358 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2359 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002360 goto cleanup;
2361 }
2362
2363 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002364 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002365 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002366 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002367 }
2368 if (!mod) {
2369 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002370 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002371 *predicate - start, start, dst_len, dst);
2372 goto cleanup;
2373 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002374 if (!mod->implemented) {
2375 /* make the module implemented */
2376 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2377 }
Radek Krejcia3045382018-11-22 14:30:31 +01002378
2379 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2380 if (!dst_node) {
2381 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002382 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002383 *predicate - start, start, path_key_expr - pke_start, pke_start);
2384 goto cleanup;
2385 }
2386 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002387 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 +01002388 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002389 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002390 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2391 goto cleanup;
2392 }
2393 }
2394
2395 ret = LY_SUCCESS;
2396cleanup:
2397 ly_set_erase(&keys, NULL);
2398 return ret;
2399}
2400
2401/**
2402 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2403 *
2404 * path-arg = absolute-path / relative-path
2405 * absolute-path = 1*("/" (node-identifier *path-predicate))
2406 * relative-path = 1*(".." "/") descendant-path
2407 *
2408 * @param[in,out] path Path to parse.
2409 * @param[out] prefix Prefix of the token, NULL if there is not any.
2410 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2411 * @param[out] name Name of the token.
2412 * @param[out] nam_len Length of the name.
2413 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2414 * must not be changed between consecutive calls. -1 if the
2415 * path is absolute.
2416 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2417 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2418 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002419LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002420lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2421 int *parent_times, int *has_predicate)
2422{
2423 int par_times = 0;
2424
2425 assert(path && *path);
2426 assert(parent_times);
2427 assert(prefix);
2428 assert(prefix_len);
2429 assert(name);
2430 assert(name_len);
2431 assert(has_predicate);
2432
2433 *prefix = NULL;
2434 *prefix_len = 0;
2435 *name = NULL;
2436 *name_len = 0;
2437 *has_predicate = 0;
2438
2439 if (!*parent_times) {
2440 if (!strncmp(*path, "..", 2)) {
2441 *path += 2;
2442 ++par_times;
2443 while (!strncmp(*path, "/..", 3)) {
2444 *path += 3;
2445 ++par_times;
2446 }
2447 }
2448 if (par_times) {
2449 *parent_times = par_times;
2450 } else {
2451 *parent_times = -1;
2452 }
2453 }
2454
2455 if (**path != '/') {
2456 return LY_EINVAL;
2457 }
2458 /* skip '/' */
2459 ++(*path);
2460
2461 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002462 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002463
2464 if ((**path == '/' && (*path)[1]) || !**path) {
2465 /* path continues by another token or this is the last token */
2466 return LY_SUCCESS;
2467 } else if ((*path)[0] != '[') {
2468 /* unexpected character */
2469 return LY_EINVAL;
2470 } else {
2471 /* predicate starting with [ */
2472 *has_predicate = 1;
2473 return LY_SUCCESS;
2474 }
2475}
2476
2477/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002478 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2479 *
2480 * The set of features used for target must be a subset of features used for the leafref.
2481 * This is not a perfect, we should compare the truth tables but it could require too much resources
2482 * and RFC 7950 does not require it explicitely, so we simplify that.
2483 *
2484 * @param[in] refnode The leafref node.
2485 * @param[in] target Tha target node of the leafref.
2486 * @return LY_SUCCESS or LY_EVALID;
2487 */
2488static LY_ERR
2489lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2490{
2491 LY_ERR ret = LY_EVALID;
2492 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002493 unsigned int u, v, count;
2494 struct ly_set features = {0};
2495
2496 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002497 if (iter->iffeatures) {
2498 LY_ARRAY_FOR(iter->iffeatures, u) {
2499 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2500 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002501 }
2502 }
2503 }
2504 }
2505
2506 /* we should have, in features set, a superset of features applicable to the target node.
2507 * So when adding features applicable to the target into the features set, we should not be
2508 * able to actually add any new feature, otherwise it is not a subset of features applicable
2509 * to the leafref itself. */
2510 count = features.count;
2511 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002512 if (iter->iffeatures) {
2513 LY_ARRAY_FOR(iter->iffeatures, u) {
2514 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2515 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002516 /* new feature was added (or LY_EMEM) */
2517 goto cleanup;
2518 }
2519 }
2520 }
2521 }
2522 }
2523 ret = LY_SUCCESS;
2524
2525cleanup:
2526 ly_set_erase(&features, NULL);
2527 return ret;
2528}
2529
2530/**
Radek Krejcia3045382018-11-22 14:30:31 +01002531 * @brief Validate the leafref path.
2532 * @param[in] ctx Compile context
2533 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002534 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002535 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2536 */
2537static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002538lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002539{
2540 const struct lysc_node *node = NULL, *parent = NULL;
2541 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002542 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002543 const char *id, *prefix, *name;
2544 size_t prefix_len, name_len;
2545 int parent_times = 0, has_predicate;
2546 unsigned int iter, u;
2547 LY_ERR ret = LY_SUCCESS;
2548
2549 assert(ctx);
2550 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002551 assert(leafref);
2552
Radek Krejci1c0c3442019-07-23 16:08:47 +02002553 ctx->path[0] = '\0';
2554 lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
2555 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002556
Radek Krejcia3045382018-11-22 14:30:31 +01002557 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002558 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002559 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2560 if (!iter) { /* first iteration */
2561 /* precess ".." in relative paths */
2562 if (parent_times > 0) {
2563 /* move from the context node */
2564 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2565 /* path is supposed to be evaluated in data tree, so we have to skip
2566 * all schema nodes that cannot be instantiated in data tree */
2567 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002568 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002569 }
2570 }
2571 }
2572
2573 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002574 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002575 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002576 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002577 }
2578 if (!mod) {
2579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002580 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2581 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002582 return LY_EVALID;
2583 }
Radek Krejci3d929562019-02-21 11:25:55 +01002584 if (!mod->implemented) {
2585 /* make the module implemented */
2586 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2587 }
Radek Krejcia3045382018-11-22 14:30:31 +01002588
2589 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2590 if (!node) {
2591 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002592 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002593 return LY_EVALID;
2594 }
2595 parent = node;
2596
2597 if (has_predicate) {
2598 /* we have predicate, so the current result must be list */
2599 if (node->nodetype != LYS_LIST) {
2600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2601 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002602 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002603 return LY_EVALID;
2604 }
2605
Radek Krejcibade82a2018-12-05 10:13:48 +01002606 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2607 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002608 }
2609
2610 ++iter;
2611 }
2612 if (ret) {
2613 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002614 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002615 return LY_EVALID;
2616 }
2617
2618 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2620 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002621 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002622 return LY_EVALID;
2623 }
2624
2625 /* check status */
2626 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2627 return LY_EVALID;
2628 }
2629
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002630 /* check config */
2631 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2632 if (node->flags & LYS_CONFIG_R) {
2633 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2634 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2635 leafref->path);
2636 return LY_EVALID;
2637 }
2638 }
2639
Radek Krejci412ddfa2018-11-23 11:44:11 +01002640 /* store the target's type and check for circular chain of leafrefs */
2641 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2642 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2643 if (type == (struct lysc_type*)leafref) {
2644 /* circular chain detected */
2645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2646 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2647 return LY_EVALID;
2648 }
2649 }
2650
Radek Krejci58d171e2018-11-23 13:50:55 +01002651 /* check if leafref and its target are under common if-features */
2652 if (lys_compile_leafref_features_validate(startnode, node)) {
2653 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2654 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2655 leafref->path);
2656 return LY_EVALID;
2657 }
2658
Radek Krejci327de162019-06-14 12:52:07 +02002659 ctx->path_len = 1;
2660 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002661 return LY_SUCCESS;
2662}
2663
Radek Krejcicdfecd92018-11-26 11:27:32 +01002664static 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 +02002665 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002666/**
2667 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2668 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002669 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2670 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2671 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2672 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002673 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002674 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002675 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002676 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2677 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2678 * @param[out] type Newly created type structure with the filled information about the type.
2679 * @return LY_ERR value.
2680 */
Radek Krejci19a96102018-11-15 13:38:09 +01002681static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002682lys_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 +02002683 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002684 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002685{
2686 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002687 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002688 struct lysc_type_bin *bin;
2689 struct lysc_type_num *num;
2690 struct lysc_type_str *str;
2691 struct lysc_type_bits *bits;
2692 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002693 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002694 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002695 struct lysc_type_union *un, *un_aux;
2696 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002697
2698 switch (basetype) {
2699 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002700 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002701
2702 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002703 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002704 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2705 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002706 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002707 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 +01002708 }
2709 }
2710
2711 if (tpdfname) {
2712 type_p->compiled = *type;
2713 *type = calloc(1, sizeof(struct lysc_type_bin));
2714 }
2715 break;
2716 case LY_TYPE_BITS:
2717 /* RFC 7950 9.7 - bits */
2718 bits = (struct lysc_type_bits*)(*type);
2719 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002720 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002721 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2722 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002723 }
2724
Radek Krejci555cb5b2018-11-16 14:54:33 +01002725 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002726 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002727 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002729 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002731 free(*type);
2732 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002733 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002734 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002735 }
2736
2737 if (tpdfname) {
2738 type_p->compiled = *type;
2739 *type = calloc(1, sizeof(struct lysc_type_bits));
2740 }
2741 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002742 case LY_TYPE_DEC64:
2743 dec = (struct lysc_type_dec*)(*type);
2744
2745 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002746 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002747 if (!type_p->fraction_digits) {
2748 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002749 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002750 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002752 free(*type);
2753 *type = NULL;
2754 }
2755 return LY_EVALID;
2756 }
2757 } else if (type_p->fraction_digits) {
2758 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002759 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2761 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002762 tpdfname);
2763 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002764 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2765 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002766 free(*type);
2767 *type = NULL;
2768 }
2769 return LY_EVALID;
2770 }
2771 dec->fraction_digits = type_p->fraction_digits;
2772
2773 /* RFC 7950 9.2.4 - range */
2774 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002775 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2776 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002777 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002778 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 +01002779 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002780 }
2781
2782 if (tpdfname) {
2783 type_p->compiled = *type;
2784 *type = calloc(1, sizeof(struct lysc_type_dec));
2785 }
2786 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002787 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002788 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002789
2790 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002791 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002792 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2793 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002794 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002795 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 +01002796 }
2797 } else if (base && ((struct lysc_type_str*)base)->length) {
2798 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2799 }
2800
2801 /* RFC 7950 9.4.5 - pattern */
2802 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002803 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002804 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002805 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2806 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2807 }
2808
2809 if (tpdfname) {
2810 type_p->compiled = *type;
2811 *type = calloc(1, sizeof(struct lysc_type_str));
2812 }
2813 break;
2814 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002815 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002816
2817 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002818 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002819 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002820 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002821 }
2822
Radek Krejci555cb5b2018-11-16 14:54:33 +01002823 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002824 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002825 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002826 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002827 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002828 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002829 free(*type);
2830 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002831 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002832 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002833 }
2834
2835 if (tpdfname) {
2836 type_p->compiled = *type;
2837 *type = calloc(1, sizeof(struct lysc_type_enum));
2838 }
2839 break;
2840 case LY_TYPE_INT8:
2841 case LY_TYPE_UINT8:
2842 case LY_TYPE_INT16:
2843 case LY_TYPE_UINT16:
2844 case LY_TYPE_INT32:
2845 case LY_TYPE_UINT32:
2846 case LY_TYPE_INT64:
2847 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002848 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002849
2850 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002851 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002852 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2853 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002854 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002855 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 +01002856 }
2857 }
2858
2859 if (tpdfname) {
2860 type_p->compiled = *type;
2861 *type = calloc(1, sizeof(struct lysc_type_num));
2862 }
2863 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002864 case LY_TYPE_IDENT:
2865 idref = (struct lysc_type_identityref*)(*type);
2866
2867 /* RFC 7950 9.10.2 - base */
2868 if (type_p->bases) {
2869 if (base) {
2870 /* only the directly derived identityrefs can contain base specification */
2871 if (tpdfname) {
2872 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002873 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002874 tpdfname);
2875 } else {
2876 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002877 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002878 free(*type);
2879 *type = NULL;
2880 }
2881 return LY_EVALID;
2882 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002883 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002884 }
2885
2886 if (!base && !type_p->flags) {
2887 /* type derived from identityref built-in type must contain at least one base */
2888 if (tpdfname) {
2889 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2890 } else {
2891 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2892 free(*type);
2893 *type = NULL;
2894 }
2895 return LY_EVALID;
2896 }
2897
2898 if (tpdfname) {
2899 type_p->compiled = *type;
2900 *type = calloc(1, sizeof(struct lysc_type_identityref));
2901 }
2902 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002903 case LY_TYPE_LEAFREF:
2904 /* RFC 7950 9.9.3 - require-instance */
2905 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002906 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002907 if (tpdfname) {
2908 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2909 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2910 } else {
2911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2912 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2913 free(*type);
2914 *type = NULL;
2915 }
2916 return LY_EVALID;
2917 }
Radek Krejcia3045382018-11-22 14:30:31 +01002918 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002919 } else if (base) {
2920 /* inherit */
2921 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002922 } else {
2923 /* default is true */
2924 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2925 }
2926 if (type_p->path) {
2927 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002928 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002929 } else if (base) {
2930 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002931 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002932 } else if (tpdfname) {
2933 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2934 return LY_EVALID;
2935 } else {
2936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2937 free(*type);
2938 *type = NULL;
2939 return LY_EVALID;
2940 }
2941 if (tpdfname) {
2942 type_p->compiled = *type;
2943 *type = calloc(1, sizeof(struct lysc_type_leafref));
2944 }
2945 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002946 case LY_TYPE_INST:
2947 /* RFC 7950 9.9.3 - require-instance */
2948 if (type_p->flags & LYS_SET_REQINST) {
2949 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2950 } else {
2951 /* default is true */
2952 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2953 }
2954
2955 if (tpdfname) {
2956 type_p->compiled = *type;
2957 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2958 }
2959 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002960 case LY_TYPE_UNION:
2961 un = (struct lysc_type_union*)(*type);
2962
2963 /* RFC 7950 7.4 - type */
2964 if (type_p->types) {
2965 if (base) {
2966 /* only the directly derived union can contain types specification */
2967 if (tpdfname) {
2968 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2969 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2970 tpdfname);
2971 } else {
2972 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2973 "Invalid type substatement for the type not directly derived from union built-in type.");
2974 free(*type);
2975 *type = NULL;
2976 }
2977 return LY_EVALID;
2978 }
2979 /* compile the type */
2980 additional = 0;
2981 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2982 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002983 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 +01002984 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2985 /* add space for additional types from the union subtype */
2986 un_aux = (struct lysc_type_union *)un->types[u + additional];
2987 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)));
2988 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2989 un->types = (void*)((uint32_t*)(p) + 1);
2990
2991 /* copy subtypes of the subtype union */
2992 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2993 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2994 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2995 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2996 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2997 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2998 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2999 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3000 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3001 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3002 /* TODO extensions */
3003
3004 } else {
3005 un->types[u + additional] = un_aux->types[v];
3006 ++un_aux->types[v]->refcount;
3007 }
3008 ++additional;
3009 LY_ARRAY_INCREMENT(un->types);
3010 }
3011 /* compensate u increment in main loop */
3012 --additional;
3013
3014 /* free the replaced union subtype */
3015 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3016 } else {
3017 LY_ARRAY_INCREMENT(un->types);
3018 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003019 }
3020 }
3021
3022 if (!base && !type_p->flags) {
3023 /* type derived from union built-in type must contain at least one type */
3024 if (tpdfname) {
3025 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3026 } else {
3027 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3028 free(*type);
3029 *type = NULL;
3030 }
3031 return LY_EVALID;
3032 }
3033
3034 if (tpdfname) {
3035 type_p->compiled = *type;
3036 *type = calloc(1, sizeof(struct lysc_type_union));
3037 }
3038 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003039 case LY_TYPE_BOOL:
3040 case LY_TYPE_EMPTY:
3041 case LY_TYPE_UNKNOWN: /* just to complete switch */
3042 break;
3043 }
3044 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3045done:
3046 return ret;
3047}
3048
Radek Krejcia3045382018-11-22 14:30:31 +01003049/**
3050 * @brief Compile information about the leaf/leaf-list's type.
3051 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003052 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3053 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3054 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3055 * @param[in] context_name Name of the context node or referencing typedef for logging.
3056 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003057 * @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 +01003058 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003059 * @return LY_ERR value.
3060 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003061static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003062lys_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 +02003063 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003064{
3065 LY_ERR ret = LY_SUCCESS;
3066 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003067 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003068 struct type_context {
3069 const struct lysp_tpdf *tpdf;
3070 struct lysp_node *node;
3071 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003072 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003073 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003074 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003075 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003076 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003077 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003078
3079 (*type) = NULL;
3080
3081 tctx = calloc(1, sizeof *tctx);
3082 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003083 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003084 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3085 ret == LY_SUCCESS;
3086 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3087 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3088 if (basetype) {
3089 break;
3090 }
3091
3092 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003093 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3094 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003095 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3096
Radek Krejcicdfecd92018-11-26 11:27:32 +01003097 if (units && !*units) {
3098 /* inherit units */
3099 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3100 }
Radek Krejci01342af2019-01-03 15:18:08 +01003101 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003102 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003103 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003104 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003105 }
Radek Krejci01342af2019-01-03 15:18:08 +01003106 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003107 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3108 break;
3109 }
3110
Radek Krejci19a96102018-11-15 13:38:09 +01003111 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003112 /* it is not necessary to continue, the rest of the chain was already compiled,
3113 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003114 basetype = tctx->tpdf->type.compiled->basetype;
3115 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003116 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003117 dummyloops = 1;
3118 goto preparenext;
3119 } else {
3120 tctx = NULL;
3121 break;
3122 }
Radek Krejci19a96102018-11-15 13:38:09 +01003123 }
3124
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003125 /* circular typedef reference detection */
3126 for (u = 0; u < tpdf_chain.count; u++) {
3127 /* local part */
3128 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3129 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3130 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3131 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3132 free(tctx);
3133 ret = LY_EVALID;
3134 goto cleanup;
3135 }
3136 }
3137 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3138 /* global part for unions corner case */
3139 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3140 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3141 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3142 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3143 free(tctx);
3144 ret = LY_EVALID;
3145 goto cleanup;
3146 }
3147 }
3148
Radek Krejci19a96102018-11-15 13:38:09 +01003149 /* store information for the following processing */
3150 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3151
Radek Krejcicdfecd92018-11-26 11:27:32 +01003152preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003153 /* prepare next loop */
3154 tctx_prev = tctx;
3155 tctx = calloc(1, sizeof *tctx);
3156 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3157 }
3158 free(tctx);
3159
3160 /* allocate type according to the basetype */
3161 switch (basetype) {
3162 case LY_TYPE_BINARY:
3163 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003164 break;
3165 case LY_TYPE_BITS:
3166 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003167 break;
3168 case LY_TYPE_BOOL:
3169 case LY_TYPE_EMPTY:
3170 *type = calloc(1, sizeof(struct lysc_type));
3171 break;
3172 case LY_TYPE_DEC64:
3173 *type = calloc(1, sizeof(struct lysc_type_dec));
3174 break;
3175 case LY_TYPE_ENUM:
3176 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003177 break;
3178 case LY_TYPE_IDENT:
3179 *type = calloc(1, sizeof(struct lysc_type_identityref));
3180 break;
3181 case LY_TYPE_INST:
3182 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3183 break;
3184 case LY_TYPE_LEAFREF:
3185 *type = calloc(1, sizeof(struct lysc_type_leafref));
3186 break;
3187 case LY_TYPE_STRING:
3188 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003189 break;
3190 case LY_TYPE_UNION:
3191 *type = calloc(1, sizeof(struct lysc_type_union));
3192 break;
3193 case LY_TYPE_INT8:
3194 case LY_TYPE_UINT8:
3195 case LY_TYPE_INT16:
3196 case LY_TYPE_UINT16:
3197 case LY_TYPE_INT32:
3198 case LY_TYPE_UINT32:
3199 case LY_TYPE_INT64:
3200 case LY_TYPE_UINT64:
3201 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003202 break;
3203 case LY_TYPE_UNKNOWN:
3204 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3205 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3206 ret = LY_EVALID;
3207 goto cleanup;
3208 }
3209 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003210 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003211 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3212 ly_data_type2str[basetype]);
3213 free(*type);
3214 (*type) = NULL;
3215 ret = LY_EVALID;
3216 goto cleanup;
3217 }
3218
3219 /* get restrictions from the referred typedefs */
3220 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3221 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003222
3223 /* remember the typedef context for circular check */
3224 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3225
Radek Krejci43699232018-11-23 14:59:46 +01003226 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003227 base = tctx->tpdf->type.compiled;
3228 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003229 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003230 /* no change, just use the type information from the base */
3231 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3232 ++base->refcount;
3233 continue;
3234 }
3235
3236 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003237 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3239 tctx->tpdf->name, ly_data_type2str[basetype]);
3240 ret = LY_EVALID;
3241 goto cleanup;
3242 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3243 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3244 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3245 tctx->tpdf->name, tctx->tpdf->dflt);
3246 ret = LY_EVALID;
3247 goto cleanup;
3248 }
3249
Radek Krejci19a96102018-11-15 13:38:09 +01003250 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003251 /* TODO user type plugins */
3252 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003253 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003254 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 +01003255 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003256 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003257 LY_CHECK_GOTO(ret, cleanup);
3258 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003259 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003260 /* remove the processed typedef contexts from the stack for circular check */
3261 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003262
Radek Krejcic5c27e52018-11-15 14:38:11 +01003263 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003264 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003265 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003266 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003267 /* TODO user type plugins */
3268 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003269 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003270 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 +01003271 LY_CHECK_GOTO(ret, cleanup);
3272 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003273 /* no specific restriction in leaf's type definition, copy from the base */
3274 free(*type);
3275 (*type) = base;
3276 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003277 }
Radek Krejcia1911222019-07-22 17:24:50 +02003278 if (dflt && !(*type)->dflt) {
3279 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003280 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003281 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3282 (*type)->dflt->realtype = (*type);
3283 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3284 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003285 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003286 if (err) {
3287 ly_err_print(err);
3288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3289 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3290 ly_err_free(err);
3291 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003292 if (ret == LY_EINCOMPLETE) {
3293 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003294 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003295
3296 /* but in general result is so far ok */
3297 ret = LY_SUCCESS;
3298 }
Radek Krejcia1911222019-07-22 17:24:50 +02003299 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003300 }
Radek Krejci19a96102018-11-15 13:38:09 +01003301
Radek Krejci0935f412019-08-20 16:15:18 +02003302 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003303
3304cleanup:
3305 ly_set_erase(&tpdf_chain, free);
3306 return ret;
3307}
3308
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003309/**
3310 * @brief Compile status information of the given node.
3311 *
3312 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3313 * has the status correctly set during the compilation.
3314 *
3315 * @param[in] ctx Compile context
3316 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3317 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3318 * the compatibility with the parent's status value.
3319 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3320 * @return LY_ERR value.
3321 */
3322static LY_ERR
3323lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3324{
3325 /* status - it is not inherited by specification, but it does not make sense to have
3326 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3327 if (!((*node_flags) & LYS_STATUS_MASK)) {
3328 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3329 if ((parent_flags & 0x3) != 0x3) {
3330 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3331 * combination of bits (0x3) which marks the uses_status value */
3332 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3333 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3334 }
3335 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3336 } else {
3337 (*node_flags) |= LYS_STATUS_CURR;
3338 }
3339 } else if (parent_flags & LYS_STATUS_MASK) {
3340 /* check status compatibility with the parent */
3341 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3342 if ((*node_flags) & LYS_STATUS_CURR) {
3343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3344 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3345 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3346 } else { /* LYS_STATUS_DEPRC */
3347 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3348 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3349 }
3350 return LY_EVALID;
3351 }
3352 }
3353 return LY_SUCCESS;
3354}
3355
Radek Krejci8cce8532019-03-05 11:27:45 +01003356/**
3357 * @brief Check uniqness of the node/action/notification name.
3358 *
3359 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3360 * structures, but they share the namespace so we need to check their name collisions.
3361 *
3362 * @param[in] ctx Compile context.
3363 * @param[in] children List (linked list) of data nodes to go through.
3364 * @param[in] actions List (sized array) of actions or RPCs to go through.
3365 * @param[in] notifs List (sized array) of Notifications to go through.
3366 * @param[in] name Name of the item to find in the given lists.
3367 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3368 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3369 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3370 */
3371static LY_ERR
3372lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3373 const struct lysc_action *actions, const struct lysc_notif *notifs,
3374 const char *name, void *exclude)
3375{
3376 const struct lysc_node *iter;
3377 unsigned int u;
3378
3379 LY_LIST_FOR(children, iter) {
3380 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3381 goto error;
3382 }
3383 }
3384 LY_ARRAY_FOR(actions, u) {
3385 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3386 goto error;
3387 }
3388 }
3389 LY_ARRAY_FOR(notifs, u) {
3390 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3391 goto error;
3392 }
3393 }
3394 return LY_SUCCESS;
3395error:
3396 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3397 return LY_EEXIST;
3398}
3399
Radek Krejciec4da802019-05-02 13:02:41 +02003400static 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 +01003401
Radek Krejcia3045382018-11-22 14:30:31 +01003402/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003403 * @brief Compile parsed RPC/action schema node information.
3404 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003405 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003406 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003407 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3408 * @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).
3409 * Zero means no uses, non-zero value with no status bit set mean the default status.
3410 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3411 */
3412static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003413lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003414 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3415{
3416 LY_ERR ret = LY_SUCCESS;
3417 struct lysp_node *child_p;
3418 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003419 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003420
Radek Krejci327de162019-06-14 12:52:07 +02003421 lysc_update_path(ctx, parent, action_p->name);
3422
Radek Krejci8cce8532019-03-05 11:27:45 +01003423 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3424 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3425 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3426 action_p->name, action)) {
3427 return LY_EVALID;
3428 }
3429
Radek Krejciec4da802019-05-02 13:02:41 +02003430 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003431 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003432 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003433 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003434 return LY_EVALID;
3435 }
3436
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003437 action->nodetype = LYS_ACTION;
3438 action->module = ctx->mod;
3439 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003440 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003441 action->sp = action_p;
3442 }
3443 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3444
3445 /* status - it is not inherited by specification, but it does not make sense to have
3446 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3447 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3448
3449 DUP_STRING(ctx->ctx, action_p->name, action->name);
3450 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3451 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003452 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003453 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003454
3455 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003456 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejciec4da802019-05-02 13:02:41 +02003457 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003458 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 +02003459 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003460 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003461 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003462 }
Radek Krejci327de162019-06-14 12:52:07 +02003463 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003464 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003465
3466 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003467 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejciec4da802019-05-02 13:02:41 +02003468 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003469 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 +02003470 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003471 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003472 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003473 }
Radek Krejci327de162019-06-14 12:52:07 +02003474 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003475
Radek Krejci327de162019-06-14 12:52:07 +02003476 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003477cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003478 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003479 return ret;
3480}
3481
3482/**
Radek Krejci43981a32019-04-12 09:44:11 +02003483 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003484 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003485 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003486 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3487 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3488 * @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 +02003489 * Zero means no uses, non-zero value with no status bit set mean the default status.
3490 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3491 */
3492static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003493lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003494 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3495{
3496 LY_ERR ret = LY_SUCCESS;
3497 struct lysp_node *child_p;
3498 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003499 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003500
Radek Krejci327de162019-06-14 12:52:07 +02003501 lysc_update_path(ctx, parent, notif_p->name);
3502
Radek Krejcifc11bd72019-04-11 16:00:05 +02003503 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3504 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3505 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3506 notif_p->name, notif)) {
3507 return LY_EVALID;
3508 }
3509
Radek Krejciec4da802019-05-02 13:02:41 +02003510 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003511 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3512 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003513 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003514 return LY_EVALID;
3515 }
3516
3517 notif->nodetype = LYS_NOTIF;
3518 notif->module = ctx->mod;
3519 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003520 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003521 notif->sp = notif_p;
3522 }
3523 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3524
3525 /* status - it is not inherited by specification, but it does not make sense to have
3526 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3527 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3528
3529 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3530 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3531 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003532 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003533 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003534 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003535
Radek Krejciec4da802019-05-02 13:02:41 +02003536 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003537 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003538 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003539 }
3540
Radek Krejci327de162019-06-14 12:52:07 +02003541 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003542cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003543 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003544 return ret;
3545}
3546
3547/**
Radek Krejcia3045382018-11-22 14:30:31 +01003548 * @brief Compile parsed container node information.
3549 * @param[in] ctx Compile context
3550 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003551 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3552 * is enriched with the container-specific information.
3553 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3554 */
Radek Krejci19a96102018-11-15 13:38:09 +01003555static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003556lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003557{
3558 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3559 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3560 struct lysp_node *child_p;
3561 unsigned int u;
3562 LY_ERR ret = LY_SUCCESS;
3563
Radek Krejcife909632019-02-12 15:34:42 +01003564 if (cont_p->presence) {
3565 cont->flags |= LYS_PRESENCE;
3566 }
3567
Radek Krejci19a96102018-11-15 13:38:09 +01003568 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003569 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003570 }
3571
Radek Krejciec4da802019-05-02 13:02:41 +02003572 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3573 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3574 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003575
3576done:
3577 return ret;
3578}
3579
Radek Krejci33f72892019-02-21 10:36:58 +01003580/*
3581 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3582 * @param[in] ctx Compile context.
3583 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3584 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003585 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3586 * @return LY_ERR value.
3587 */
3588static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003589lys_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 +01003590{
Radek Krejcia1911222019-07-22 17:24:50 +02003591 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003592 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3593
Radek Krejciec4da802019-05-02 13:02:41 +02003594 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 +01003595 leaf->units ? NULL : &leaf->units));
3596 if (leaf->nodetype == LYS_LEAFLIST) {
3597 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003598 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3599 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003600 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003601 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3602 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3603 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3604 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003605 LY_ARRAY_INCREMENT(llist->dflts);
3606 }
3607 } else {
3608 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003609 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003610 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3611 leaf->dflt->realtype = leaf->type->dflt->realtype;
3612 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3613 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003614 }
3615 }
3616 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3617 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3618 ly_set_add(&ctx->unres, leaf, 0);
3619 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3620 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3621 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3622 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3623 ly_set_add(&ctx->unres, leaf, 0);
3624 }
3625 }
3626 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003627 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3628 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3629 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3630 return LY_EVALID;
3631 }
3632 }
3633
Radek Krejci33f72892019-02-21 10:36:58 +01003634 return LY_SUCCESS;
3635}
3636
Radek Krejcia3045382018-11-22 14:30:31 +01003637/**
3638 * @brief Compile parsed leaf node information.
3639 * @param[in] ctx Compile context
3640 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003641 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3642 * is enriched with the leaf-specific information.
3643 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3644 */
Radek Krejci19a96102018-11-15 13:38:09 +01003645static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003646lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003647{
3648 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3649 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3650 unsigned int u;
3651 LY_ERR ret = LY_SUCCESS;
3652
Radek Krejciec4da802019-05-02 13:02:41 +02003653 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003654 if (leaf_p->units) {
3655 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3656 leaf->flags |= LYS_SET_UNITS;
3657 }
Radek Krejcia1911222019-07-22 17:24:50 +02003658
3659 /* the dflt member is just filled to avoid getting the default value from the type */
3660 leaf->dflt = (void*)leaf_p->dflt;
3661 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3662
Radek Krejciccd20f12019-02-15 14:12:27 +01003663 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003664 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003665 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003666 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3667 leaf->dflt->realtype = leaf->type;
3668 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3669 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003670 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003671 leaf->dflt->realtype->refcount++;
3672 if (err) {
3673 ly_err_print(err);
3674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3675 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3676 ly_err_free(err);
3677 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003678 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003679 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003680 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003681
3682 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003683 ret = LY_SUCCESS;
3684 }
Radek Krejcia1911222019-07-22 17:24:50 +02003685 LY_CHECK_GOTO(ret, done);
Radek Krejci76b3e962018-12-14 17:01:25 +01003686 leaf->flags |= LYS_SET_DFLT;
3687 }
Radek Krejci43699232018-11-23 14:59:46 +01003688
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003689
Radek Krejci19a96102018-11-15 13:38:09 +01003690done:
3691 return ret;
3692}
3693
Radek Krejcia3045382018-11-22 14:30:31 +01003694/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003695 * @brief Compile parsed leaf-list node information.
3696 * @param[in] ctx Compile context
3697 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003698 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3699 * is enriched with the leaf-list-specific information.
3700 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3701 */
3702static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003703lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003704{
3705 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3706 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003707 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003708 LY_ERR ret = LY_SUCCESS;
3709
Radek Krejciec4da802019-05-02 13:02:41 +02003710 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003711 if (llist_p->units) {
3712 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3713 llist->flags |= LYS_SET_UNITS;
3714 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003715
Radek Krejcia1911222019-07-22 17:24:50 +02003716 /* the dflts member is just filled to avoid getting the default value from the type */
3717 llist->dflts = (void*)llist_p->dflts;
3718 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003719 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003720 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003721 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003722 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3723 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003724 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003725 LY_ARRAY_INCREMENT(llist->dflts_mods);
3726 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003727 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003728 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3729 llist->dflts[u]->realtype = llist->type;
3730 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3731 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003732 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003733 llist->dflts[u]->realtype->refcount++;
3734 if (err) {
3735 ly_err_print(err);
3736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3737 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3738 ly_err_free(err);
3739 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003740 if (ret == LY_EINCOMPLETE) {
3741 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003742 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003743
3744 /* but in general result is so far ok */
3745 ret = LY_SUCCESS;
3746 }
Radek Krejcia1911222019-07-22 17:24:50 +02003747 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003748 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003749 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003750 }
Radek Krejcia1911222019-07-22 17:24:50 +02003751 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3752 /* configuration data values must be unique - so check the default values */
3753 LY_ARRAY_FOR(llist->dflts, u) {
3754 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3755 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3756 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003757 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 +02003758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3759 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3760 if (dynamic) {
3761 free((char*)val);
3762 }
3763 return LY_EVALID;
3764 }
3765 }
3766 }
3767 }
3768
3769 /* 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 +01003770
3771 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003772 if (llist->min) {
3773 llist->flags |= LYS_MAND_TRUE;
3774 }
Radek Krejcib7408632018-11-28 17:12:11 +01003775 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003776
Radek Krejci0e5d8382018-11-28 16:37:53 +01003777done:
3778 return ret;
3779}
3780
3781/**
Radek Krejci7af64242019-02-18 13:07:53 +01003782 * @brief Compile information about list's uniques.
3783 * @param[in] ctx Compile context.
3784 * @param[in] context_module Module where the prefixes are going to be resolved.
3785 * @param[in] uniques Sized array list of unique statements.
3786 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3787 * @return LY_ERR value.
3788 */
3789static LY_ERR
3790lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3791{
3792 LY_ERR ret = LY_SUCCESS;
3793 struct lysc_node_leaf **key, ***unique;
3794 const char *keystr, *delim;
3795 size_t len;
3796 unsigned int v;
3797 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003798 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003799
3800 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3801 config = -1;
3802 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3803 keystr = uniques[v];
3804 while (keystr) {
3805 delim = strpbrk(keystr, " \t\n");
3806 if (delim) {
3807 len = delim - keystr;
3808 while (isspace(*delim)) {
3809 ++delim;
3810 }
3811 } else {
3812 len = strlen(keystr);
3813 }
3814
3815 /* unique node must be present */
3816 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003817 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3818 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003819 if (ret != LY_SUCCESS) {
3820 if (ret == LY_EDENIED) {
3821 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003822 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003823 len, keystr, lys_nodetype2str((*key)->nodetype));
3824 }
3825 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003826 } else if (flags) {
3827 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3828 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3829 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3830 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003831 }
3832
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003833
Radek Krejci7af64242019-02-18 13:07:53 +01003834 /* all referenced leafs must be of the same config type */
3835 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3837 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3838 return LY_EVALID;
3839 } else if ((*key)->flags & LYS_CONFIG_W) {
3840 config = 1;
3841 } else { /* LYS_CONFIG_R */
3842 config = 0;
3843 }
3844
3845 /* check status */
3846 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3847 (*key)->flags, (*key)->module, (*key)->name));
3848
3849 /* mark leaf as unique */
3850 (*key)->flags |= LYS_UNIQUE;
3851
3852 /* next unique value in line */
3853 keystr = delim;
3854 }
3855 /* next unique definition */
3856 }
3857
3858 return LY_SUCCESS;
3859}
3860
3861/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003862 * @brief Compile parsed list node information.
3863 * @param[in] ctx Compile context
3864 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003865 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3866 * is enriched with the list-specific information.
3867 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3868 */
3869static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003870lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003871{
3872 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3873 struct lysc_node_list *list = (struct lysc_node_list*)node;
3874 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003875 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003876 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003877 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003878 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003879 LY_ERR ret = LY_SUCCESS;
3880
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003881 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003882 if (list->min) {
3883 list->flags |= LYS_MAND_TRUE;
3884 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003885 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3886
3887 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003888 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003889 }
3890
Radek Krejciec4da802019-05-02 13:02:41 +02003891 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003892
3893 /* keys */
3894 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3895 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3896 return LY_EVALID;
3897 }
3898
3899 /* find all the keys (must be direct children) */
3900 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003901 if (!keystr) {
3902 /* keyless list */
3903 list->flags |= LYS_KEYLESS;
3904 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003905 while (keystr) {
3906 delim = strpbrk(keystr, " \t\n");
3907 if (delim) {
3908 len = delim - keystr;
3909 while (isspace(*delim)) {
3910 ++delim;
3911 }
3912 } else {
3913 len = strlen(keystr);
3914 }
3915
3916 /* key node must be present */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003917 key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3918 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003919 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3920 "The list's key \"%.*s\" not found.", len, keystr);
3921 return LY_EVALID;
3922 }
3923 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003924 if (key->flags & LYS_KEY) {
3925 /* the node was already marked as a key */
3926 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3927 "Duplicated key identifier \"%.*s\".", len, keystr);
3928 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003929 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003930
3931 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003932 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003933 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003934 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3935 return LY_EVALID;
3936 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003937 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003938 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003939 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003941 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003942 return LY_EVALID;
3943 }
3944 } else {
3945 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003946 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003948 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003949 return LY_EVALID;
3950 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003951 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003952 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003953 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003954 return LY_EVALID;
3955 }
3956 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003957
3958 /* check status */
3959 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003960 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003961
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003962 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003963 if (key->dflt) {
3964 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3965 lysc_type_free(ctx->ctx, key->dflt->realtype);
3966 free(key->dflt);
3967 key->dflt = NULL;
3968 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003969 }
3970 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003971 key->flags |= LYS_KEY;
3972
3973 /* move it to the correct position */
3974 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3975 /* fix links in closest previous siblings of the key */
3976 if (key->next) {
3977 key->next->prev = key->prev;
3978 } else {
3979 /* last child */
3980 list->child->prev = key->prev;
3981 }
3982 if (key->prev->next) {
3983 key->prev->next = key->next;
3984 }
3985 /* fix links in the key */
3986 if (prev_key) {
3987 key->prev = (struct lysc_node*)prev_key;
3988 key->next = prev_key->next;
3989 } else {
3990 key->prev = list->child->prev;
3991 key->next = list->child;
3992 }
3993 /* fix links in closes future siblings of the key */
3994 if (prev_key) {
3995 if (prev_key->next) {
3996 prev_key->next->prev = (struct lysc_node*)key;
3997 } else {
3998 list->child->prev = (struct lysc_node*)key;
3999 }
4000 prev_key->next = (struct lysc_node*)key;
4001 } else {
4002 list->child->prev = (struct lysc_node*)key;
4003 }
4004 /* fix links in parent */
4005 if (!key->prev->next) {
4006 list->child = (struct lysc_node*)key;
4007 }
4008 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004009
4010 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004011 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004012 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004013 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004014 }
4015
4016 /* uniques */
4017 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004018 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004019 }
4020
Radek Krejciec4da802019-05-02 13:02:41 +02004021 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4022 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004023
4024done:
4025 return ret;
4026}
4027
Radek Krejcib56c7502019-02-13 14:19:54 +01004028/**
4029 * @brief Do some checks and set the default choice's case.
4030 *
4031 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4032 *
4033 * @param[in] ctx Compile context.
4034 * @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,
4035 * not the reference to the imported module.
4036 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4037 * @return LY_ERR value.
4038 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004039static LY_ERR
4040lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4041{
4042 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4043 const char *prefix = NULL, *name;
4044 size_t prefix_len = 0;
4045
4046 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4047 name = strchr(dflt, ':');
4048 if (name) {
4049 prefix = dflt;
4050 prefix_len = name - prefix;
4051 ++name;
4052 } else {
4053 name = dflt;
4054 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004055 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004056 /* prefixed default case make sense only for the prefix of the schema itself */
4057 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4058 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4059 prefix_len, prefix);
4060 return LY_EVALID;
4061 }
4062 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4063 if (!ch->dflt) {
4064 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4065 "Default case \"%s\" not found.", dflt);
4066 return LY_EVALID;
4067 }
4068 /* no mandatory nodes directly under the default case */
4069 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004070 if (iter->parent != (struct lysc_node*)ch->dflt) {
4071 break;
4072 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004073 if (iter->flags & LYS_MAND_TRUE) {
4074 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4075 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4076 return LY_EVALID;
4077 }
4078 }
Radek Krejci01342af2019-01-03 15:18:08 +01004079 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004080 return LY_SUCCESS;
4081}
4082
Radek Krejciccd20f12019-02-15 14:12:27 +01004083static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004084lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004085{
4086 struct lys_module *mod;
4087 const char *prefix = NULL, *name;
4088 size_t prefix_len = 0;
4089 struct lysc_node_case *cs;
4090 struct lysc_node *node;
4091
4092 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4093 name = strchr(dflt, ':');
4094 if (name) {
4095 prefix = dflt;
4096 prefix_len = name - prefix;
4097 ++name;
4098 } else {
4099 name = dflt;
4100 }
4101 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4102 if (prefix) {
4103 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4104 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004105 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4106 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004107 return LY_EVALID;
4108 }
4109 } else {
4110 mod = ctx->mod;
4111 }
4112 /* get the default case */
4113 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4114 if (!cs) {
4115 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004116 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004117 return LY_EVALID;
4118 }
4119
4120 /* check that there is no mandatory node */
4121 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004122 if (node->parent != (struct lysc_node*)cs) {
4123 break;
4124 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004125 if (node->flags & LYS_MAND_TRUE) {
4126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004127 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4128 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004129 return LY_EVALID;
4130 }
4131 }
4132
4133 /* set the default case in choice */
4134 ch->dflt = cs;
4135 cs->flags |= LYS_SET_DFLT;
4136
4137 return LY_SUCCESS;
4138}
4139
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004140/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004141 * @brief Compile parsed choice node information.
4142 * @param[in] ctx Compile context
4143 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004144 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004145 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004146 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4147 */
4148static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004149lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004150{
4151 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4152 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4153 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004154 LY_ERR ret = LY_SUCCESS;
4155
Radek Krejci056d0a82018-12-06 16:57:25 +01004156 LY_LIST_FOR(ch_p->child, child_p) {
4157 if (child_p->nodetype == LYS_CASE) {
4158 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004159 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004160 }
4161 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004162 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004163 }
4164 }
4165
4166 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004167 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004168 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004169 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004170
Radek Krejci9800fb82018-12-13 14:26:23 +01004171 return ret;
4172}
4173
4174/**
4175 * @brief Compile parsed anydata or anyxml node information.
4176 * @param[in] ctx Compile context
4177 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004178 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4179 * is enriched with the any-specific information.
4180 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4181 */
4182static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004183lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004184{
4185 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4186 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4187 unsigned int u;
4188 LY_ERR ret = LY_SUCCESS;
4189
Radek Krejciec4da802019-05-02 13:02:41 +02004190 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004191
4192 if (any->flags & LYS_CONFIG_W) {
4193 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004194 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004195 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004196done:
4197 return ret;
4198}
4199
Radek Krejcib56c7502019-02-13 14:19:54 +01004200/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004201 * @brief Connect the node into the siblings list and check its name uniqueness.
4202 *
4203 * @param[in] ctx Compile context
4204 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4205 * the choice itself is expected instead of a specific case node.
4206 * @param[in] node Schema node to connect into the list.
4207 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4208 */
4209static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004210lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004211{
4212 struct lysc_node **children;
4213
4214 if (node->nodetype == LYS_CASE) {
4215 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4216 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004217 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004218 }
4219 if (children) {
4220 if (!(*children)) {
4221 /* first child */
4222 *children = node;
4223 } else if (*children != node) {
4224 /* by the condition in previous branch we cover the choice/case children
4225 * - the children list is shared by the choice and the the first case, in addition
4226 * the first child of each case must be referenced from the case node. So the node is
4227 * actually always already inserted in case it is the first children - so here such
4228 * a situation actually corresponds to the first branch */
4229 /* insert at the end of the parent's children list */
4230 (*children)->prev->next = node;
4231 node->prev = (*children)->prev;
4232 (*children)->prev = node;
4233
4234 /* check the name uniqueness */
4235 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4236 lysc_node_notifs(parent), node->name, node)) {
4237 return LY_EEXIST;
4238 }
4239 }
4240 }
4241 return LY_SUCCESS;
4242}
4243
Radek Krejci95710c92019-02-11 15:49:55 +01004244/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004245 * @brief Get the XPath context node for the given schema node.
4246 * @param[in] start The schema node where the XPath expression appears.
4247 * @return The context node to evaluate XPath expression in given schema node.
4248 * @return NULL in case the context node is the root node.
4249 */
4250static struct lysc_node *
4251lysc_xpath_context(struct lysc_node *start)
4252{
4253 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
4254 start = start->parent);
4255 return start;
4256}
4257
4258/**
4259 * @brief Prepare the case structure in choice node for the new data node.
4260 *
4261 * 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
4262 * created in the choice when the first child was processed.
4263 *
4264 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004265 * @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,
4266 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004267 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4268 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4269 * @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,
4270 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004271 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004272static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004273lys_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 +01004274{
4275 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004276 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004277 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004278 unsigned int u;
4279 LY_ERR ret;
4280
Radek Krejci95710c92019-02-11 15:49:55 +01004281#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004282 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004283 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004284 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4285 return NULL; \
4286 } \
4287 }
4288
Radek Krejci95710c92019-02-11 15:49:55 +01004289 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4290 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004291
4292 /* we have to add an implicit case node into the parent choice */
4293 cs = calloc(1, sizeof(struct lysc_node_case));
4294 DUP_STRING(ctx->ctx, child->name, cs->name);
4295 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004296 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004297 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4298 /* the case is already present since the child is not its first children */
4299 return (struct lysc_node_case*)ch->cases->prev;
4300 }
Radek Krejci95710c92019-02-11 15:49:55 +01004301 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004302
4303 /* explicit parent case is not present (this is its first child) */
4304 cs = calloc(1, sizeof(struct lysc_node_case));
4305 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4306 cs->flags = LYS_STATUS_MASK & node_p->flags;
4307 cs->sp = node_p;
4308
Radek Krejcib1b59152019-01-07 13:21:56 +01004309 /* 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 +02004310 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004311
4312 if (node_p->when) {
4313 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004314 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004315 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004316 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004317 }
Radek Krejciec4da802019-05-02 13:02:41 +02004318 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004319 } else {
4320 LOGINT(ctx->ctx);
4321 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004322 }
4323 cs->module = ctx->mod;
4324 cs->prev = (struct lysc_node*)cs;
4325 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004326 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004327 cs->parent = (struct lysc_node*)ch;
4328 cs->child = child;
4329
4330 return cs;
4331error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004332 if (cs) {
4333 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4334 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004335 return NULL;
4336
4337#undef UNIQUE_CHECK
4338}
4339
Radek Krejcib56c7502019-02-13 14:19:54 +01004340/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004341 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004342 *
4343 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004344 * @param[in] node Target node where the config is supposed to be changed.
4345 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004346 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4347 * 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 +01004348 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4349 * @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 +01004350 * @return LY_ERR value.
4351 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004352static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004353lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004354 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004355{
4356 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004357 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004358
4359 if (config == (node->flags & LYS_CONFIG_MASK)) {
4360 /* nothing to do */
4361 return LY_SUCCESS;
4362 }
4363
4364 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004365 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004366 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4367 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004368 "Invalid %s of config - configuration node cannot be child of any state data node.",
4369 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004370 return LY_EVALID;
4371 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004372 node->flags |= LYS_SET_CONFIG;
4373 } else {
4374 if (node->flags & LYS_SET_CONFIG) {
4375 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4376 /* setting config flags, but have node with explicit config true */
4377 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004378 "Invalid %s of config - configuration node cannot be child of any state data node.",
4379 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004380 return LY_EVALID;
4381 }
4382 /* do not change config on nodes where the config is explicitely set, this does not apply to
4383 * nodes, which are being changed explicitly (targets of refine or deviation) */
4384 return LY_SUCCESS;
4385 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004386 }
4387 node->flags &= ~LYS_CONFIG_MASK;
4388 node->flags |= config;
4389
4390 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004391 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004392 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004393 }
4394
Radek Krejci76b3e962018-12-14 17:01:25 +01004395 return LY_SUCCESS;
4396}
4397
Radek Krejcib56c7502019-02-13 14:19:54 +01004398/**
4399 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4400 *
4401 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4402 * the flag to such parents from a mandatory children.
4403 *
4404 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4405 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4406 * (mandatory children was removed).
4407 */
Radek Krejcife909632019-02-12 15:34:42 +01004408void
4409lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4410{
4411 struct lysc_node *iter;
4412
4413 if (add) { /* set flag */
4414 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4415 parent = parent->parent) {
4416 parent->flags |= LYS_MAND_TRUE;
4417 }
4418 } else { /* unset flag */
4419 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004420 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004421 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004422 /* there is another mandatory node */
4423 return;
4424 }
4425 }
4426 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4427 parent->flags &= ~LYS_MAND_TRUE;
4428 }
4429 }
4430}
4431
Radek Krejci056d0a82018-12-06 16:57:25 +01004432/**
Radek Krejci3641f562019-02-13 15:38:40 +01004433 * @brief Internal sorting process for the lys_compile_augment_sort().
4434 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4435 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4436 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4437 */
4438static void
4439lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4440{
4441 unsigned int v;
4442 size_t len;
4443
4444 len = strlen(aug_p->nodeid);
4445 LY_ARRAY_FOR(result, v) {
4446 if (strlen(result[v]->nodeid) <= len) {
4447 continue;
4448 }
4449 if (v < LY_ARRAY_SIZE(result)) {
4450 /* move the rest of array */
4451 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4452 break;
4453 }
4454 }
4455 result[v] = aug_p;
4456 LY_ARRAY_INCREMENT(result);
4457}
4458
4459/**
4460 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4461 *
4462 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4463 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4464 *
4465 * @param[in] ctx Compile context.
4466 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4467 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4468 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4469 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4470 * @return LY_ERR value.
4471 */
4472LY_ERR
4473lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4474{
4475 struct lysp_augment **result = NULL;
4476 unsigned int u, v;
4477 size_t count = 0;
4478
4479 assert(augments);
4480
4481 /* get count of the augments in module and all its submodules */
4482 if (aug_p) {
4483 count += LY_ARRAY_SIZE(aug_p);
4484 }
4485 LY_ARRAY_FOR(inc_p, u) {
4486 if (inc_p[u].submodule->augments) {
4487 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4488 }
4489 }
4490
4491 if (!count) {
4492 *augments = NULL;
4493 return LY_SUCCESS;
4494 }
4495 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4496
4497 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4498 * together, so there can be even /z/y betwwen them. */
4499 LY_ARRAY_FOR(aug_p, u) {
4500 lys_compile_augment_sort_(&aug_p[u], result);
4501 }
4502 LY_ARRAY_FOR(inc_p, u) {
4503 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4504 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4505 }
4506 }
4507
4508 *augments = result;
4509 return LY_SUCCESS;
4510}
4511
4512/**
4513 * @brief Compile the parsed augment connecting it into its target.
4514 *
4515 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4516 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4517 * are already implemented and compiled.
4518 *
4519 * @param[in] ctx Compile context.
4520 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004521 * @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
4522 * children in case of the augmenting uses data.
4523 * @return LY_SUCCESS on success.
4524 * @return LY_EVALID on failure.
4525 */
4526LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004527lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004528{
4529 LY_ERR ret = LY_SUCCESS;
4530 struct lysp_node *node_p, *case_node_p;
4531 struct lysc_node *target; /* target target of the augment */
4532 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004533 struct lysc_when **when, *when_shared;
4534 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004535 uint16_t flags = 0;
4536 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004537 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004538
Radek Krejci327de162019-06-14 12:52:07 +02004539 lysc_update_path(ctx, NULL, "{augment}");
4540 lysc_update_path(ctx, NULL, aug_p->nodeid);
4541
Radek Krejci7af64242019-02-18 13:07:53 +01004542 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004543 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004544 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004545 if (ret != LY_SUCCESS) {
4546 if (ret == LY_EDENIED) {
4547 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4548 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4549 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4550 }
4551 return LY_EVALID;
4552 }
4553
4554 /* check for mandatory nodes
4555 * - new cases augmenting some choice can have mandatory nodes
4556 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4557 */
Radek Krejci733988a2019-02-15 15:12:44 +01004558 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004559 allow_mandatory = 1;
4560 }
4561
4562 when_shared = NULL;
4563 LY_LIST_FOR(aug_p->child, node_p) {
4564 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4565 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4566 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004567 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4568 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004569 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004570 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4571 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004572 return LY_EVALID;
4573 }
4574
4575 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004576 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004577 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004578 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004579 } else {
4580 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004581 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004582 }
4583 }
Radek Krejciec4da802019-05-02 13:02:41 +02004584 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004585
Radek Krejcife13da42019-02-15 14:51:01 +01004586 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4587 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004588 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004589 /* 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 +01004590 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 +01004591 } else if (target->nodetype == LYS_CHOICE) {
4592 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4593 node = ((struct lysc_node_choice*)target)->cases->prev;
4594 } else {
4595 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004596 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004597 }
4598
Radek Krejci733988a2019-02-15 15:12:44 +01004599 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004600 node->flags &= ~LYS_MAND_TRUE;
4601 lys_compile_mandatory_parents(target, 0);
4602 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004603 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004604 return LY_EVALID;
4605 }
4606
4607 /* pass augment's when to all the children */
4608 if (aug_p->when) {
4609 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4610 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004611 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004612 LY_CHECK_GOTO(ret, error);
4613 (*when)->context = lysc_xpath_context(target);
4614 when_shared = *when;
4615 } else {
4616 ++when_shared->refcount;
4617 (*when) = when_shared;
4618 }
4619 }
4620 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004621
Radek Krejciec4da802019-05-02 13:02:41 +02004622 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004623 switch (target->nodetype) {
4624 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004625 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004626 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004627 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004628 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004629 break;
4630 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004631 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004632 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004633 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004634 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004635 break;
4636 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004637 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004638 if (aug_p->actions) {
4639 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004640 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4641 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004642 return LY_EVALID;
4643 }
4644 if (aug_p->notifs) {
4645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004646 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4647 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004648 return LY_EVALID;
4649 }
4650 }
Radek Krejci3641f562019-02-13 15:38:40 +01004651
Radek Krejci327de162019-06-14 12:52:07 +02004652 lysc_update_path(ctx, NULL, NULL);
4653 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004654error:
Radek Krejciec4da802019-05-02 13:02:41 +02004655 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004656 return ret;
4657}
4658
4659/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004660 * @brief Apply refined or deviated mandatory flag to the target node.
4661 *
4662 * @param[in] ctx Compile context.
4663 * @param[in] node Target node where the mandatory property is supposed to be changed.
4664 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004665 * @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 +01004666 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4667 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4668 * 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 +01004669 * @return LY_ERR value.
4670 */
4671static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004672lys_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 +01004673{
4674 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004676 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4677 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004678 return LY_EVALID;
4679 }
4680
4681 if (mandatory_flag & LYS_MAND_TRUE) {
4682 /* check if node has default value */
4683 if (node->nodetype & LYS_LEAF) {
4684 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004685 if (refine_flag) {
4686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004687 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004688 return LY_EVALID;
4689 }
Radek Krejcia1911222019-07-22 17:24:50 +02004690 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004691 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004692 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004693
4694 /* update the list of incomplete default values if needed */
4695 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4696
Radek Krejcia1911222019-07-22 17:24:50 +02004697 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4698 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4699 free(leaf->dflt);
4700 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004701 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004702 }
4703 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004704 if (refine_flag) {
4705 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004706 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004707 return LY_EVALID;
4708 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004709 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004710 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004711 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 +01004712 return LY_EVALID;
4713 }
4714
4715 node->flags &= ~LYS_MAND_FALSE;
4716 node->flags |= LYS_MAND_TRUE;
4717 lys_compile_mandatory_parents(node->parent, 1);
4718 } else {
4719 /* make mandatory false */
4720 node->flags &= ~LYS_MAND_TRUE;
4721 node->flags |= LYS_MAND_FALSE;
4722 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004723 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 +01004724 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004725 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004726 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004727 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4728 leaf->dflt->realtype = leaf->type->dflt->realtype;
4729 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4730 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004731 }
4732 }
4733 return LY_SUCCESS;
4734}
4735
4736/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004737 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4738 * If present, also apply uses's modificators.
4739 *
4740 * @param[in] ctx Compile context
4741 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004742 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4743 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4744 * the compile context.
4745 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4746 */
4747static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004748lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004749{
4750 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004751 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004752 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004753 struct lysc_node_container context_node_fake =
4754 {.nodetype = LYS_CONTAINER,
4755 .module = ctx->mod,
4756 .flags = parent ? parent->flags : 0,
4757 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004758 .prev = (struct lysc_node*)&context_node_fake,
4759 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004760 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004761 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004762 int found;
4763 const char *id, *name, *prefix;
4764 size_t prefix_len, name_len;
4765 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004766 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004767 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004768 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004769 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004770 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004771 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004772 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004773 unsigned int actions_index, notifs_index;
4774 struct lysc_notif **notifs = NULL;
4775 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004776
4777 /* search for the grouping definition */
4778 found = 0;
4779 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004780 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004781 if (prefix) {
4782 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4783 if (!mod) {
4784 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004785 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004786 return LY_EVALID;
4787 }
4788 } else {
4789 mod = ctx->mod_def;
4790 }
4791 if (mod == ctx->mod_def) {
4792 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004793 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004794 LY_ARRAY_FOR(grp, u) {
4795 if (!strcmp(grp[u].name, name)) {
4796 grp = &grp[u];
4797 found = 1;
4798 break;
4799 }
4800 }
4801 }
4802 }
4803 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004804 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004805 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004806 if (grp) {
4807 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4808 if (!strcmp(grp[u].name, name)) {
4809 grp = &grp[u];
4810 found = 1;
4811 }
4812 }
4813 }
4814 if (!found && mod->parsed->includes) {
4815 /* ... and all the submodules */
4816 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4817 grp = mod->parsed->includes[u].submodule->groupings;
4818 if (grp) {
4819 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4820 if (!strcmp(grp[v].name, name)) {
4821 grp = &grp[v];
4822 found = 1;
4823 }
4824 }
4825 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004826 }
4827 }
4828 }
4829 if (!found) {
4830 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4831 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4832 return LY_EVALID;
4833 }
4834
4835 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4836 grp_stack_count = ctx->groupings.count;
4837 ly_set_add(&ctx->groupings, (void*)grp, 0);
4838 if (grp_stack_count == ctx->groupings.count) {
4839 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4840 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4841 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4842 return LY_EVALID;
4843 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004844 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4845 /* remember that the grouping is instantiated to avoid its standalone validation */
4846 grp->flags |= LYS_USED_GRP;
4847 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004848
4849 /* switch context's mod_def */
4850 mod_old = ctx->mod_def;
4851 ctx->mod_def = mod;
4852
4853 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004854 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 +01004855
Radek Krejcifc11bd72019-04-11 16:00:05 +02004856 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004857 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004858 /* 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 +02004859 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 +01004860
4861 /* some preparation for applying refines */
4862 if (grp->data == node_p) {
4863 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004864 if (parent) {
4865 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4866 } else if (ctx->mod->compiled->data) {
4867 child = ctx->mod->compiled->data;
4868 } else {
4869 child = NULL;
4870 }
4871 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004872 }
4873 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004874 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004875 LY_LIST_FOR(context_node_fake.child, child) {
4876 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004877
Radek Krejcifc11bd72019-04-11 16:00:05 +02004878 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004879 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004880 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004881 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004882 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004883 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004884 when_shared = *when;
4885 } else {
4886 ++when_shared->refcount;
4887 (*when) = when_shared;
4888 }
4889 }
Radek Krejci01342af2019-01-03 15:18:08 +01004890 }
4891 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004892 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004893 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004894 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004895 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 +01004896 }
4897
Radek Krejcifc11bd72019-04-11 16:00:05 +02004898 /* compile actions */
4899 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4900 if (actions) {
4901 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004902 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004903 if (*actions && (uses_p->augments || uses_p->refines)) {
4904 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4905 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4906 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4907 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4908 }
4909 }
4910
4911 /* compile notifications */
4912 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4913 if (notifs) {
4914 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004915 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004916 if (*notifs && (uses_p->augments || uses_p->refines)) {
4917 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4918 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4919 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4920 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4921 }
4922 }
4923
4924
Radek Krejci3641f562019-02-13 15:38:40 +01004925 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004926 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004927 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004928 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004929 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004930
Radek Krejcif0089082019-01-07 16:42:01 +01004931 /* reload previous context's mod_def */
4932 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004933 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004934
Radek Krejci76b3e962018-12-14 17:01:25 +01004935 /* apply refine */
4936 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004937 lysc_update_path(ctx, NULL, rfn->nodeid);
4938
Radek Krejci7af64242019-02-18 13:07:53 +01004939 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 +01004940 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004941 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004942 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004943
4944 /* default value */
4945 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004946 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004948 "Invalid refine of default - %s cannot hold %d default values.",
4949 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004950 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004951 }
4952 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4953 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004954 "Invalid refine of default - %s cannot hold default value(s).",
4955 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004956 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004957 }
4958 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004959 struct ly_err_item *err = NULL;
4960 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4961 if (leaf->dflt) {
4962 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004963 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004964 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4965 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4966 } else {
4967 /* prepare a new one */
4968 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4969 leaf->dflt->realtype = leaf->type;
4970 }
4971 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004972 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004973 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4974 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004975 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004976 leaf->dflt->realtype->refcount++;
4977 if (err) {
4978 ly_err_print(err);
4979 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4980 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4981 ly_err_free(err);
4982 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004983 if (rc == LY_EINCOMPLETE) {
4984 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004985 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004986
4987 /* but in general result is so far ok */
4988 rc = LY_SUCCESS;
4989 }
Radek Krejcia1911222019-07-22 17:24:50 +02004990 LY_CHECK_GOTO(rc, cleanup);
4991 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004992 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004993 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4994
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004995 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004996 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4997 "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 +02004998 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004999 }
Radek Krejcia1911222019-07-22 17:24:50 +02005000
5001 /* remove previous set of default values */
5002 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005003 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005004 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5005 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5006 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005007 }
Radek Krejcia1911222019-07-22 17:24:50 +02005008 LY_ARRAY_FREE(llist->dflts);
5009 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005010 LY_ARRAY_FREE(llist->dflts_mods);
5011 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005012
5013 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005014 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005015 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005016 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005017 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005018 LY_ARRAY_INCREMENT(llist->dflts_mods);
5019 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005020 LY_ARRAY_INCREMENT(llist->dflts);
5021 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5022 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005023 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005024 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005025 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005026 llist->dflts[u]->realtype->refcount++;
5027 if (err) {
5028 ly_err_print(err);
5029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5030 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5031 ly_err_free(err);
5032 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005033 if (rc == LY_EINCOMPLETE) {
5034 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005035 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005036
5037 /* but in general result is so far ok */
5038 rc = LY_SUCCESS;
5039 }
5040 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005041 }
Radek Krejcia1911222019-07-22 17:24:50 +02005042 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005043 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005044 if (((struct lysc_node_choice*)node)->dflt) {
5045 /* unset LYS_SET_DFLT from the current default case */
5046 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5047 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005048 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 +01005049 }
5050 }
5051
Radek Krejci12fb9142019-01-08 09:45:30 +01005052 /* description */
5053 if (rfn->dsc) {
5054 FREE_STRING(ctx->ctx, node->dsc);
5055 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5056 }
5057
5058 /* reference */
5059 if (rfn->ref) {
5060 FREE_STRING(ctx->ctx, node->ref);
5061 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5062 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005063
5064 /* config */
5065 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005066 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005067 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005068 } else {
5069 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
5070 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
5071 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005072 }
5073
5074 /* mandatory */
5075 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005076 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005077 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005078
5079 /* presence */
5080 if (rfn->presence) {
5081 if (node->nodetype != LYS_CONTAINER) {
5082 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005083 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5084 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005085 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005086 }
5087 node->flags |= LYS_PRESENCE;
5088 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005089
5090 /* must */
5091 if (rfn->musts) {
5092 switch (node->nodetype) {
5093 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02005094 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaf*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005095 break;
5096 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02005097 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_leaflist*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005098 break;
5099 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02005100 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_list*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005101 break;
5102 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02005103 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_container*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005104 break;
5105 case LYS_ANYXML:
5106 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02005107 COMPILE_ARRAY_GOTO(ctx, rfn->musts, ((struct lysc_node_anydata*)node)->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci9a564c92019-01-07 14:53:57 +01005108 break;
5109 default:
5110 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005111 "Invalid refine of must statement - %s cannot hold any must statement.",
5112 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005113 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005114 }
5115 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005116
5117 /* min/max-elements */
5118 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5119 switch (node->nodetype) {
5120 case LYS_LEAFLIST:
5121 if (rfn->flags & LYS_SET_MAX) {
5122 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5123 }
5124 if (rfn->flags & LYS_SET_MIN) {
5125 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005126 if (rfn->min) {
5127 node->flags |= LYS_MAND_TRUE;
5128 lys_compile_mandatory_parents(node->parent, 1);
5129 } else {
5130 node->flags &= ~LYS_MAND_TRUE;
5131 lys_compile_mandatory_parents(node->parent, 0);
5132 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005133 }
5134 break;
5135 case LYS_LIST:
5136 if (rfn->flags & LYS_SET_MAX) {
5137 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5138 }
5139 if (rfn->flags & LYS_SET_MIN) {
5140 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005141 if (rfn->min) {
5142 node->flags |= LYS_MAND_TRUE;
5143 lys_compile_mandatory_parents(node->parent, 1);
5144 } else {
5145 node->flags &= ~LYS_MAND_TRUE;
5146 lys_compile_mandatory_parents(node->parent, 0);
5147 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005148 }
5149 break;
5150 default:
5151 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005152 "Invalid refine of %s statement - %s cannot hold this statement.",
5153 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005154 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005155 }
5156 }
Radek Krejcif0089082019-01-07 16:42:01 +01005157
5158 /* if-feature */
5159 if (rfn->iffeatures) {
5160 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005161 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005162 }
Radek Krejci327de162019-06-14 12:52:07 +02005163
5164 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005165 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005166
Radek Krejcif2271f12019-01-07 16:42:23 +01005167 /* do some additional checks of the changed nodes when all the refines are applied */
5168 for (u = 0; u < refined.count; ++u) {
5169 node = (struct lysc_node*)refined.objs[u];
5170 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005171 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005172
5173 /* check possible conflict with default value (default added, mandatory left true) */
5174 if ((node->flags & LYS_MAND_TRUE) &&
5175 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5176 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5177 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005178 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005179 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005180 }
5181
5182 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5183 if (node->nodetype == LYS_LIST) {
5184 min = ((struct lysc_node_list*)node)->min;
5185 max = ((struct lysc_node_list*)node)->max;
5186 } else {
5187 min = ((struct lysc_node_leaflist*)node)->min;
5188 max = ((struct lysc_node_leaflist*)node)->max;
5189 }
5190 if (min > max) {
5191 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005192 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5193 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005194 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005195 }
5196 }
5197 }
5198
Radek Krejci327de162019-06-14 12:52:07 +02005199 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005200 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005201
5202cleanup:
5203 /* fix connection of the children nodes from fake context node back into the parent */
5204 if (context_node_fake.child) {
5205 context_node_fake.child->prev = child;
5206 }
5207 LY_LIST_FOR(context_node_fake.child, child) {
5208 child->parent = parent;
5209 }
5210
5211 if (uses_p->augments || uses_p->refines) {
5212 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005213 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005214 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5215 LY_ARRAY_FREE(context_node_fake.actions);
5216 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005217 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005218 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5219 LY_ARRAY_FREE(context_node_fake.notifs);
5220 }
5221 }
5222
Radek Krejcie86bf772018-12-14 11:39:53 +01005223 /* reload previous context's mod_def */
5224 ctx->mod_def = mod_old;
5225 /* remove the grouping from the stack for circular groupings dependency check */
5226 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5227 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005228 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005229 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005230
5231 return ret;
5232}
5233
Radek Krejci327de162019-06-14 12:52:07 +02005234static int
5235lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5236{
5237 struct lysp_node *iter;
5238 int len = 0;
5239
5240 *path = NULL;
5241 for (iter = node; iter && len >= 0; iter = iter->parent) {
5242 char *s = *path;
5243 char *id;
5244
5245 switch (iter->nodetype) {
5246 case LYS_USES:
5247 asprintf(&id, "{uses='%s'}", iter->name);
5248 break;
5249 case LYS_GROUPING:
5250 asprintf(&id, "{grouping='%s'}", iter->name);
5251 break;
5252 case LYS_AUGMENT:
5253 asprintf(&id, "{augment='%s'}", iter->name);
5254 break;
5255 default:
5256 id = strdup(iter->name);
5257 break;
5258 }
5259
5260 if (!iter->parent) {
5261 /* print prefix */
5262 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5263 } else {
5264 /* prefix is the same as in parent */
5265 len = asprintf(path, "/%s%s", id, s ? s : "");
5266 }
5267 free(s);
5268 free(id);
5269 }
5270
5271 if (len < 0) {
5272 free(*path);
5273 *path = NULL;
5274 } else if (len == 0) {
5275 *path = strdup("/");
5276 len = 1;
5277 }
5278 return len;
5279}
5280
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005281/**
5282 * @brief Validate groupings that were defined but not directly used in the schema itself.
5283 *
5284 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5285 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5286 */
5287static LY_ERR
5288lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5289{
5290 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005291 char *path;
5292 int len;
5293
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005294 struct lysp_node_uses fake_uses = {
5295 .parent = node_p,
5296 .nodetype = LYS_USES,
5297 .flags = 0, .next = NULL,
5298 .name = grp->name,
5299 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5300 .refines = NULL, .augments = NULL
5301 };
5302 struct lysc_node_container fake_container = {
5303 .nodetype = LYS_CONTAINER,
5304 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5305 .module = ctx->mod,
5306 .sp = NULL, .parent = NULL, .next = NULL,
5307 .prev = (struct lysc_node*)&fake_container,
5308 .name = "fake",
5309 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5310 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5311 };
5312
5313 if (grp->parent) {
5314 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5315 }
Radek Krejci327de162019-06-14 12:52:07 +02005316
5317 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5318 if (len < 0) {
5319 LOGMEM(ctx->ctx);
5320 return LY_EMEM;
5321 }
5322 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5323 ctx->path_len = (uint16_t)len;
5324 free(path);
5325
5326 lysc_update_path(ctx, NULL, "{grouping}");
5327 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005328 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005329 lysc_update_path(ctx, NULL, NULL);
5330 lysc_update_path(ctx, NULL, NULL);
5331
5332 ctx->path_len = 1;
5333 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005334
5335 /* cleanup */
5336 lysc_node_container_free(ctx->ctx, &fake_container);
5337
5338 return ret;
5339}
Radek Krejcife909632019-02-12 15:34:42 +01005340
Radek Krejcie86bf772018-12-14 11:39:53 +01005341/**
Radek Krejcia3045382018-11-22 14:30:31 +01005342 * @brief Compile parsed schema node information.
5343 * @param[in] ctx Compile context
5344 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005345 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5346 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5347 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005348 * @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).
5349 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005350 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5351 */
Radek Krejci19a96102018-11-15 13:38:09 +01005352static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005353lys_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 +01005354{
5355 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005356 struct lysc_node *node;
5357 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005358 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005359 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005360 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005361
Radek Krejci327de162019-06-14 12:52:07 +02005362 if (node_p->nodetype != LYS_USES) {
5363 lysc_update_path(ctx, parent, node_p->name);
5364 } else {
5365 lysc_update_path(ctx, NULL, "{uses}");
5366 lysc_update_path(ctx, NULL, node_p->name);
5367 }
5368
Radek Krejci19a96102018-11-15 13:38:09 +01005369 switch (node_p->nodetype) {
5370 case LYS_CONTAINER:
5371 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5372 node_compile_spec = lys_compile_node_container;
5373 break;
5374 case LYS_LEAF:
5375 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5376 node_compile_spec = lys_compile_node_leaf;
5377 break;
5378 case LYS_LIST:
5379 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005380 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005381 break;
5382 case LYS_LEAFLIST:
5383 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005384 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005385 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005386 case LYS_CHOICE:
5387 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005388 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005389 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005390 case LYS_ANYXML:
5391 case LYS_ANYDATA:
5392 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005393 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005394 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005395 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005396 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5397 lysc_update_path(ctx, NULL, NULL);
5398 lysc_update_path(ctx, NULL, NULL);
5399 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005400 default:
5401 LOGINT(ctx->ctx);
5402 return LY_EINT;
5403 }
5404 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5405 node->nodetype = node_p->nodetype;
5406 node->module = ctx->mod;
5407 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005408 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005409
5410 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005411 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005412 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005413 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005414 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5415 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005416 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005417 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005418 node->flags |= LYS_CONFIG_R;
5419 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005420 /* config not explicitely set, inherit it from parent */
5421 if (parent) {
5422 node->flags |= parent->flags & LYS_CONFIG_MASK;
5423 } else {
5424 /* default is config true */
5425 node->flags |= LYS_CONFIG_W;
5426 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005427 } else {
5428 /* config set explicitely */
5429 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005430 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005431 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5433 "Configuration node cannot be child of any state data node.");
5434 goto error;
5435 }
Radek Krejci19a96102018-11-15 13:38:09 +01005436
Radek Krejcia6d57732018-11-29 13:40:37 +01005437 /* *list ordering */
5438 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5439 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005440 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005441 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5442 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005443 node->flags &= ~LYS_ORDBY_MASK;
5444 node->flags |= LYS_ORDBY_SYSTEM;
5445 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5446 /* default ordering is system */
5447 node->flags |= LYS_ORDBY_SYSTEM;
5448 }
5449 }
5450
Radek Krejci19a96102018-11-15 13:38:09 +01005451 /* status - it is not inherited by specification, but it does not make sense to have
5452 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005453 if (!parent || parent->nodetype != LYS_CHOICE) {
5454 /* in case of choice/case's children, postpone the check to the moment we know if
5455 * 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 +01005456 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 +01005457 }
5458
Radek Krejciec4da802019-05-02 13:02:41 +02005459 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005460 node->sp = node_p;
5461 }
5462 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005463 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5464 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005465 if (node_p->when) {
5466 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005467 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005468 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005469 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005470 }
Radek Krejciec4da802019-05-02 13:02:41 +02005471 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005472
5473 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005474 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005475
Radek Krejci0935f412019-08-20 16:15:18 +02005476 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5477
Radek Krejcife909632019-02-12 15:34:42 +01005478 /* inherit LYS_MAND_TRUE in parent containers */
5479 if (node->flags & LYS_MAND_TRUE) {
5480 lys_compile_mandatory_parents(parent, 1);
5481 }
5482
Radek Krejci327de162019-06-14 12:52:07 +02005483 lysc_update_path(ctx, NULL, NULL);
5484
Radek Krejci19a96102018-11-15 13:38:09 +01005485 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005486 if (parent) {
5487 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005488 if (node_p->parent->nodetype == LYS_CASE) {
5489 lysc_update_path(ctx, parent, node_p->parent->name);
5490 } else {
5491 lysc_update_path(ctx, parent, node->name);
5492 }
Radek Krejciec4da802019-05-02 13:02:41 +02005493 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005494 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005495 if (uses_status) {
5496
5497 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005498 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005499 * it directly gets the same status flags as the choice;
5500 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005501 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005502 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005503 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005504 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005505 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005506 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005507 }
Radek Krejciec4da802019-05-02 13:02:41 +02005508 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 +02005509
5510 if (parent->nodetype == LYS_CHOICE) {
5511 lysc_update_path(ctx, NULL, NULL);
5512 }
Radek Krejci19a96102018-11-15 13:38:09 +01005513 } else {
5514 /* top-level element */
5515 if (!ctx->mod->compiled->data) {
5516 ctx->mod->compiled->data = node;
5517 } else {
5518 /* insert at the end of the module's top-level nodes list */
5519 ctx->mod->compiled->data->prev->next = node;
5520 node->prev = ctx->mod->compiled->data->prev;
5521 ctx->mod->compiled->data->prev = node;
5522 }
Radek Krejci327de162019-06-14 12:52:07 +02005523 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005524 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5525 ctx->mod->compiled->notifs, node->name, node)) {
5526 return LY_EVALID;
5527 }
Radek Krejci19a96102018-11-15 13:38:09 +01005528 }
Radek Krejci327de162019-06-14 12:52:07 +02005529 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005530
5531 return LY_SUCCESS;
5532
5533error:
5534 lysc_node_free(ctx->ctx, node);
5535 return ret;
5536}
5537
Radek Krejciccd20f12019-02-15 14:12:27 +01005538static void
5539lysc_disconnect(struct lysc_node *node)
5540{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005541 struct lysc_node *parent, *child, *prev = NULL, *next;
5542 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005543 int remove_cs = 0;
5544
5545 parent = node->parent;
5546
5547 /* parent's first child */
5548 if (!parent) {
5549 return;
5550 }
5551 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005552 cs = (struct lysc_node_case*)node;
5553 } else if (parent->nodetype == LYS_CASE) {
5554 /* disconnecting some node in a case */
5555 cs = (struct lysc_node_case*)parent;
5556 parent = cs->parent;
5557 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5558 if (child == node) {
5559 if (cs->child == child) {
5560 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5561 /* case with a single child -> remove also the case */
5562 child->parent = NULL;
5563 remove_cs = 1;
5564 } else {
5565 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005566 }
5567 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005568 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005569 }
5570 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005571 if (!remove_cs) {
5572 cs = NULL;
5573 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005574 } else if (lysc_node_children(parent, node->flags) == node) {
5575 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005576 }
5577
5578 if (cs) {
5579 if (remove_cs) {
5580 /* cs has only one child which is being also removed */
5581 lysc_disconnect((struct lysc_node*)cs);
5582 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5583 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005584 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5585 /* default case removed */
5586 ((struct lysc_node_choice*)parent)->dflt = NULL;
5587 }
5588 if (((struct lysc_node_choice*)parent)->cases == cs) {
5589 /* first case removed */
5590 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5591 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005592 if (cs->child) {
5593 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5594 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5595 prev = cs->child->prev;
5596 } /* else all the children are under a single case */
5597 LY_LIST_FOR_SAFE(cs->child, next, child) {
5598 if (child->parent != (struct lysc_node*)cs) {
5599 break;
5600 }
5601 lysc_node_free(node->module->ctx, child);
5602 }
5603 if (prev) {
5604 if (prev->next) {
5605 prev->next = child;
5606 }
5607 if (child) {
5608 child->prev = prev;
5609 } else {
5610 /* link from the first child under the cases */
5611 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5612 }
5613 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005614 }
5615 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005616 }
5617
5618 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005619 if (node->prev->next) {
5620 node->prev->next = node->next;
5621 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005622 if (node->next) {
5623 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005624 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005625 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005626 if (child) {
5627 child->prev = node->prev;
5628 }
5629 } else if (((struct lysc_node_choice*)parent)->cases) {
5630 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005631 }
5632}
5633
5634LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005635lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005636{
Radek Krejcia1911222019-07-22 17:24:50 +02005637 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005638 struct ly_set devs_p = {0};
5639 struct ly_set targets = {0};
5640 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005641 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005642 struct lysc_action *rpcs;
5643 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005644 struct lysp_deviation *dev;
5645 struct lysp_deviate *d, **dp_new;
5646 struct lysp_deviate_add *d_add;
5647 struct lysp_deviate_del *d_del;
5648 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005649 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005650 struct lysc_deviation {
5651 const char *nodeid;
5652 struct lysc_node *target; /* target node of the deviation */
5653 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005654 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005655 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5656 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005657 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005658 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005659 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005660 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005661 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005662 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005663
5664 /* get all deviations from the module and all its submodules ... */
5665 LY_ARRAY_FOR(mod_p->deviations, u) {
5666 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5667 }
5668 LY_ARRAY_FOR(mod_p->includes, v) {
5669 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5670 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5671 }
5672 }
5673 if (!devs_p.count) {
5674 /* nothing to do */
5675 return LY_SUCCESS;
5676 }
5677
Radek Krejci327de162019-06-14 12:52:07 +02005678 lysc_update_path(ctx, NULL, "{deviation}");
5679
Radek Krejciccd20f12019-02-15 14:12:27 +01005680 /* ... and group them by the target node */
5681 devs = calloc(devs_p.count, sizeof *devs);
5682 for (u = 0; u < devs_p.count; ++u) {
5683 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005684 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005685
5686 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005687 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5688 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005689 if (target->nodetype == LYS_ACTION) {
5690 /* move the target pointer to input/output to make them different from the action and
5691 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5692 * back to the RPC/action node due to a better compatibility and decision code in this function.
5693 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5694 if (flags & LYSC_OPT_RPC_INPUT) {
5695 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5696 flags |= LYSC_OPT_INTERNAL;
5697 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5698 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5699 flags |= LYSC_OPT_INTERNAL;
5700 }
5701 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005702 /* insert into the set of targets with duplicity detection */
5703 i = ly_set_add(&targets, target, 0);
5704 if (!devs[i]) {
5705 /* new record */
5706 devs[i] = calloc(1, sizeof **devs);
5707 devs[i]->target = target;
5708 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005709 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005710 }
5711 /* add deviates into the deviation's list of deviates */
5712 for (d = dev->deviates; d; d = d->next) {
5713 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5714 *dp_new = d;
5715 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5716 devs[i]->not_supported = 1;
5717 }
5718 }
Radek Krejci327de162019-06-14 12:52:07 +02005719
5720 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005721 }
5722
5723 /* MACROS for deviates checking */
5724#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5725 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005726 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 +01005727 goto cleanup; \
5728 }
5729
5730#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5731 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005732 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5733 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005734 goto cleanup; \
5735 }
5736
Radek Krejcia1911222019-07-22 17:24:50 +02005737
Radek Krejciccd20f12019-02-15 14:12:27 +01005738#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005739 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5741 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5742 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5743 goto cleanup; \
5744 }
5745
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005746#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005747 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005748 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005749 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5750 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005752 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5753 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005754 goto cleanup; \
5755 }
5756
Radek Krejci551b12c2019-02-19 16:11:21 +01005757#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5758 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5759 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005760 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5761 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005762 goto cleanup; \
5763 }
5764
Radek Krejciccd20f12019-02-15 14:12:27 +01005765#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5766 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005767 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005768 goto cleanup; \
5769 }
5770
Radek Krejci551b12c2019-02-19 16:11:21 +01005771#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5772 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5773 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005774 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005775 goto cleanup; \
5776 }
5777
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005778#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5779 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5780 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5781 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5782 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 +01005783 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005784 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005786 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5787 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005788 goto cleanup; \
5789 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005790 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5791 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5792 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5793 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5794 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005795 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005796 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5797 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5798 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005799 }
5800
5801 /* apply deviations */
5802 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005803 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5804 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5805 struct ly_err_item *err = NULL;
5806
5807 dflt = NULL;
5808 changed_type = 0;
5809
Radek Krejci327de162019-06-14 12:52:07 +02005810 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5811
Radek Krejcif538ce52019-03-05 10:46:14 +01005812 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5813 /* fix the target pointer in case of RPC's/action's input/output */
5814 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5815 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5816 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5817 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5818 }
5819 }
5820
Radek Krejciccd20f12019-02-15 14:12:27 +01005821 /* not-supported */
5822 if (devs[u]->not_supported) {
5823 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5824 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5825 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5826 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005827
5828#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5829 if (devs[u]->target->parent) { \
5830 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5831 } else { \
5832 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5833 } \
5834 LY_ARRAY_FOR(ARRAY, x) { \
5835 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5836 } \
5837 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5838 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5839 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5840 LY_ARRAY_DECREMENT(ARRAY); \
5841 }
5842
Radek Krejcif538ce52019-03-05 10:46:14 +01005843 if (devs[u]->target->nodetype == LYS_ACTION) {
5844 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5845 /* remove RPC's/action's input */
5846 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5847 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005848 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5849 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005850 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5851 /* remove RPC's/action's output */
5852 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5853 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005854 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5855 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005856 } else {
5857 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005858 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005859 }
5860 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005861 /* remove Notification */
5862 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005863 } else {
5864 /* remove the target node */
5865 lysc_disconnect(devs[u]->target);
5866 lysc_node_free(ctx->ctx, devs[u]->target);
5867 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005868
Radek Krejci474f9b82019-07-24 11:36:37 +02005869 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5870 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005871 continue;
5872 }
5873
5874 /* list of deviates (not-supported is not present in the list) */
5875 LY_ARRAY_FOR(devs[u]->deviates, v) {
5876 d = devs[u]->deviates[v];
5877
5878 switch (d->mod) {
5879 case LYS_DEV_ADD:
5880 d_add = (struct lysp_deviate_add*)d;
5881 /* [units-stmt] */
5882 if (d_add->units) {
5883 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5884 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5885
5886 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5887 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5888 }
5889
5890 /* *must-stmt */
5891 if (d_add->musts) {
5892 switch (devs[u]->target->nodetype) {
5893 case LYS_CONTAINER:
5894 case LYS_LIST:
5895 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005896 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005897 break;
5898 case LYS_LEAF:
5899 case LYS_LEAFLIST:
5900 case LYS_ANYDATA:
5901 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005902 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005903 break;
5904 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005905 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005906 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005907 break;
5908 case LYS_ACTION:
5909 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5910 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005911 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005912 break;
5913 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5914 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005915 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005916 break;
5917 }
5918 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005919 default:
5920 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005921 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005922 goto cleanup;
5923 }
5924 }
5925
5926 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005927 if (d_add->uniques) {
5928 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5929 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5930 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005931
5932 /* *default-stmt */
5933 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005934 switch (devs[u]->target->nodetype) {
5935 case LYS_LEAF:
5936 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005937 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 +02005938 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005939 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005940 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005941 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5942 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5943 } else {
5944 /* prepare new default value storage */
5945 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005946 }
Radek Krejcia1911222019-07-22 17:24:50 +02005947 dflt = d_add->dflts[0];
5948 /* parsing is done at the end after possible replace of the leaf's type */
5949
Radek Krejci551b12c2019-02-19 16:11:21 +01005950 /* mark the new default values as leaf's own */
5951 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005952 break;
5953 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005954 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005955 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005956 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005957 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005958 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5959 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5960 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005961 }
Radek Krejcia1911222019-07-22 17:24:50 +02005962 LY_ARRAY_FREE(llist->dflts);
5963 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005964 LY_ARRAY_FREE(llist->dflts_mods);
5965 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005966 }
5967 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005968 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005969 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5970 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01005971 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005972 LY_ARRAY_INCREMENT(llist->dflts_mods);
5973 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005974 LY_ARRAY_INCREMENT(llist->dflts);
5975 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5976 llist->dflts[x]->realtype = llist->type;
5977 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 +02005978 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005979 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005980 llist->dflts[x]->realtype->refcount++;
5981 if (err) {
5982 ly_err_print(err);
5983 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5984 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5985 d_add->dflts[x - y], err->msg);
5986 ly_err_free(err);
5987 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005988 if (rc == LY_EINCOMPLETE) {
5989 /* postpone default compilation when the tree is complete */
5990 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5991
5992 /* but in general result is so far ok */
5993 rc = LY_SUCCESS;
5994 }
Radek Krejcia1911222019-07-22 17:24:50 +02005995 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005996 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005997 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005998 devs[u]->target->flags |= LYS_SET_DFLT;
5999 break;
6000 case LYS_CHOICE:
6001 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6002 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6003 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6004 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006005 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 +01006006 goto cleanup;
6007 }
6008 break;
6009 default:
6010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006011 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006012 goto cleanup;
6013 }
6014 }
6015
6016 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006017 if (d_add->flags & LYS_CONFIG_MASK) {
6018 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006020 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6021 goto cleanup;
6022 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006023 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006024 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
6025 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006026 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006027 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6028 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006029 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6030 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006031 goto cleanup;
6032 }
Radek Krejci327de162019-06-14 12:52:07 +02006033 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006034 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006035
6036 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006037 if (d_add->flags & LYS_MAND_MASK) {
6038 if (devs[u]->target->flags & LYS_MAND_MASK) {
6039 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006040 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6041 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006042 goto cleanup;
6043 }
Radek Krejci327de162019-06-14 12:52:07 +02006044 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006045 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006046
6047 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006048 if (d_add->flags & LYS_SET_MIN) {
6049 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6050 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6051 /* change value */
6052 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6053 } else if (devs[u]->target->nodetype == LYS_LIST) {
6054 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6055 /* change value */
6056 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6057 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006058 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006059 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6060 goto cleanup;
6061 }
6062 if (d_add->min) {
6063 devs[u]->target->flags |= LYS_MAND_TRUE;
6064 }
6065 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006066
6067 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006068 if (d_add->flags & LYS_SET_MAX) {
6069 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6070 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6071 /* change value */
6072 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6073 } else if (devs[u]->target->nodetype == LYS_LIST) {
6074 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6075 /* change value */
6076 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6077 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006078 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006079 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6080 goto cleanup;
6081 }
6082 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006083
6084 break;
6085 case LYS_DEV_DELETE:
6086 d_del = (struct lysp_deviate_del*)d;
6087
6088 /* [units-stmt] */
6089 if (d_del->units) {
6090 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006091 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6092 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6093 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6094 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6095 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6096 goto cleanup;
6097 }
6098 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6099 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006100 }
6101
6102 /* *must-stmt */
6103 if (d_del->musts) {
6104 switch (devs[u]->target->nodetype) {
6105 case LYS_CONTAINER:
6106 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006107 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006108 break;
6109 case LYS_LEAF:
6110 case LYS_LEAFLIST:
6111 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006112 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006113 break;
6114 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006115 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006116 break;
6117 case LYS_ACTION:
6118 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6119 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6120 break;
6121 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6122 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6123 break;
6124 }
6125 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006126 default:
6127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006128 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006129 goto cleanup;
6130 }
6131 }
6132
6133 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006134 if (d_del->uniques) {
6135 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6136 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6137 LY_ARRAY_FOR(d_del->uniques, x) {
6138 LY_ARRAY_FOR(list->uniques, z) {
6139 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6140 nodeid = strpbrk(name, " \t\n");
6141 if (nodeid) {
6142 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
6143 || list->uniques[z][y]->name[nodeid - name] != '\0') {
6144 break;
6145 }
6146 while (isspace(*nodeid)) {
6147 ++nodeid;
6148 }
6149 } else {
6150 if (strcmp(name, list->uniques[z][y]->name)) {
6151 break;
6152 }
6153 }
6154 }
6155 if (!name) {
6156 /* complete match - remove the unique */
6157 LY_ARRAY_DECREMENT(list->uniques);
6158 LY_ARRAY_FREE(list->uniques[z]);
6159 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6160 --z;
6161 break;
6162 }
6163 }
6164 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6165 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006166 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6167 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006168 goto cleanup;
6169 }
6170 }
6171 if (!LY_ARRAY_SIZE(list->uniques)) {
6172 LY_ARRAY_FREE(list->uniques);
6173 list->uniques = NULL;
6174 }
6175 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006176
6177 /* *default-stmt */
6178 if (d_del->dflts) {
6179 switch (devs[u]->target->nodetype) {
6180 case LYS_LEAF:
6181 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6182 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6183 dflt, "deleting", "default", d_del->dflts[0]);
6184
Radek Krejcia1911222019-07-22 17:24:50 +02006185 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006186 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006187 if (strcmp(dflt, d_del->dflts[0])) {
6188 if (i) {
6189 free((char*)dflt);
6190 }
6191 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6192 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6193 d_del->dflts[0], dflt);
6194 goto cleanup;
6195 }
6196 if (i) {
6197 free((char*)dflt);
6198 }
6199 dflt = NULL;
6200
Radek Krejci474f9b82019-07-24 11:36:37 +02006201 /* update the list of incomplete default values if needed */
6202 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6203
Radek Krejcia1911222019-07-22 17:24:50 +02006204 /* remove the default specification */
6205 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6206 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6207 free(leaf->dflt);
6208 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006209 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006210 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006211 break;
6212 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006213 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6214 LY_ARRAY_FOR(d_del->dflts, x) {
6215 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006216 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 +02006217 if (!strcmp(dflt, d_del->dflts[x])) {
6218 if (i) {
6219 free((char*)dflt);
6220 }
6221 dflt = NULL;
6222 break;
6223 }
6224 if (i) {
6225 free((char*)dflt);
6226 }
6227 dflt = NULL;
6228 }
6229 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6230 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6231 "which does not match any of the target's property values.", d_del->dflts[x]);
6232 goto cleanup;
6233 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006234
6235 /* update the list of incomplete default values if needed */
6236 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6237
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006238 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006239 LY_ARRAY_DECREMENT(llist->dflts);
6240 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6241 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6242 free(llist->dflts[y]);
6243 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006244 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 +02006245 }
6246 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006247 LY_ARRAY_FREE(llist->dflts_mods);
6248 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006249 LY_ARRAY_FREE(llist->dflts);
6250 llist->dflts = NULL;
6251 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006252 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006253 break;
6254 case LYS_CHOICE:
6255 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6256 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6257 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006258 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006259 if (prefix) {
6260 /* use module prefixes from the deviation module to match the module of the default case */
6261 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6262 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006263 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6264 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006265 goto cleanup;
6266 }
6267 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6268 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006269 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6270 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006271 goto cleanup;
6272 }
6273 }
6274 /* else {
6275 * strictly, the default prefix would point to the deviation module, but the value should actually
6276 * match the default string in the original module (usually unprefixed), so in this case we do not check
6277 * the module of the default case, just matching its name */
6278 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6279 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006280 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6281 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006282 goto cleanup;
6283 }
6284 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6285 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6286 break;
6287 default:
6288 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006289 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006290 goto cleanup;
6291 }
6292 }
6293
6294 break;
6295 case LYS_DEV_REPLACE:
6296 d_rpl = (struct lysp_deviate_rpl*)d;
6297
6298 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006299 if (d_rpl->type) {
6300 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6301 /* type is mandatory, so checking for its presence is not necessary */
6302 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006303
6304 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6305 /* the target has default from the previous type - remove it */
6306 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006307 /* update the list of incomplete default values if needed */
6308 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6309
Radek Krejcia1911222019-07-22 17:24:50 +02006310 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6311 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6312 free(leaf->dflt);
6313 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006314 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006315 } else { /* LYS_LEAFLIST */
6316 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006317 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006318 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6319 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6320 free(llist->dflts[x]);
6321 }
6322 LY_ARRAY_FREE(llist->dflts);
6323 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006324 LY_ARRAY_FREE(llist->dflts_mods);
6325 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006326 }
6327 }
6328 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006329 /* there is no default value, do not set changed_type after type compilation
6330 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006331 changed_type = -1;
6332 }
Radek Krejciec4da802019-05-02 13:02:41 +02006333 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 +02006334 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006335 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006336
6337 /* [units-stmt] */
6338 if (d_rpl->units) {
6339 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6340 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6341 units, "replacing", "units", d_rpl->units);
6342
6343 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6344 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6345 }
6346
6347 /* [default-stmt] */
6348 if (d_rpl->dflt) {
6349 switch (devs[u]->target->nodetype) {
6350 case LYS_LEAF:
6351 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6352 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006353 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006354 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006355 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6356 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6357 dflt = d_rpl->dflt;
6358 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006359 break;
6360 case LYS_CHOICE:
6361 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006362 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 +01006363 goto cleanup;
6364 }
6365 break;
6366 default:
6367 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006368 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006369 goto cleanup;
6370 }
6371 }
6372
6373 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006374 if (d_rpl->flags & LYS_CONFIG_MASK) {
6375 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006376 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006377 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6378 goto cleanup;
6379 }
6380 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006381 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006382 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6383 goto cleanup;
6384 }
Radek Krejci327de162019-06-14 12:52:07 +02006385 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006386 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006387
6388 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006389 if (d_rpl->flags & LYS_MAND_MASK) {
6390 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006391 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006392 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6393 goto cleanup;
6394 }
Radek Krejci327de162019-06-14 12:52:07 +02006395 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006396 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006397
6398 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006399 if (d_rpl->flags & LYS_SET_MIN) {
6400 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6401 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6402 /* change value */
6403 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6404 } else if (devs[u]->target->nodetype == LYS_LIST) {
6405 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6406 /* change value */
6407 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6408 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006409 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006410 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6411 goto cleanup;
6412 }
6413 if (d_rpl->min) {
6414 devs[u]->target->flags |= LYS_MAND_TRUE;
6415 }
6416 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006417
6418 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006419 if (d_rpl->flags & LYS_SET_MAX) {
6420 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6421 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6422 /* change value */
6423 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6424 } else if (devs[u]->target->nodetype == LYS_LIST) {
6425 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6426 /* change value */
6427 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6428 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006429 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006430 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6431 goto cleanup;
6432 }
6433 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006434
6435 break;
6436 default:
6437 LOGINT(ctx->ctx);
6438 goto cleanup;
6439 }
6440 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006441
Radek Krejci33f72892019-02-21 10:36:58 +01006442 /* final check when all deviations of a single target node are applied */
6443
Radek Krejci551b12c2019-02-19 16:11:21 +01006444 /* check min-max compatibility */
6445 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6446 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6447 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6448 } else if (devs[u]->target->nodetype == LYS_LIST) {
6449 min = ((struct lysc_node_list*)devs[u]->target)->min;
6450 max = ((struct lysc_node_list*)devs[u]->target)->max;
6451 } else {
6452 min = max = 0;
6453 }
6454 if (min > max) {
6455 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 +02006456 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006457 goto cleanup;
6458 }
6459
Radek Krejcia1911222019-07-22 17:24:50 +02006460 if (dflt) {
6461 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006462 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006463 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006464 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6465 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006466 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006467 leaf->dflt->realtype->refcount++;
6468 if (err) {
6469 ly_err_print(err);
6470 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6471 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6472 ly_err_free(err);
6473 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006474 if (rc == LY_EINCOMPLETE) {
6475 /* postpone default compilation when the tree is complete */
6476 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6477
6478 /* but in general result is so far ok */
6479 rc = LY_SUCCESS;
6480 }
Radek Krejcia1911222019-07-22 17:24:50 +02006481 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006482 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006483 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6484 int dynamic;
6485 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006486 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006487
6488 /* update the list of incomplete default values if needed */
6489 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6490
6491 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006492 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6493 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6494 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006495 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6496 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006497 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006498 leaf->dflt->realtype->refcount++;
6499 if (err) {
6500 ly_err_print(err);
6501 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6502 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6503 ly_err_free(err);
6504 }
6505 if (dynamic) {
6506 free((void*)dflt);
6507 }
Radek Krejcia1911222019-07-22 17:24:50 +02006508 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006509 if (rc == LY_EINCOMPLETE) {
6510 /* postpone default compilation when the tree is complete */
6511 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6512
6513 /* but in general result is so far ok */
6514 rc = LY_SUCCESS;
6515 }
6516 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006517 } else { /* LYS_LEAFLIST */
6518 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006519 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 +02006520 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6521 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6522 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006523 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6524 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006525 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6526 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006527 llist->dflts[x]->realtype->refcount++;
6528 if (err) {
6529 ly_err_print(err);
6530 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6531 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6532 dflt, err->msg);
6533 ly_err_free(err);
6534 }
6535 if (dynamic) {
6536 free((void*)dflt);
6537 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006538 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006539 if (rc == LY_EINCOMPLETE) {
6540 /* postpone default compilation when the tree is complete */
6541 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6542
6543 /* but in general result is so far ok */
6544 rc = LY_SUCCESS;
6545 }
Radek Krejcia1911222019-07-22 17:24:50 +02006546 LY_CHECK_GOTO(rc, cleanup);
6547 }
6548 }
6549 }
6550
Radek Krejci551b12c2019-02-19 16:11:21 +01006551 /* check mandatory - default compatibility */
6552 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6553 && (devs[u]->target->flags & LYS_SET_DFLT)
6554 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006556 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006557 goto cleanup;
6558 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6559 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6560 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006561 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 +01006562 goto cleanup;
6563 }
6564 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6565 /* mandatory node under a default case */
6566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006567 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6568 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006569 goto cleanup;
6570 }
Radek Krejci33f72892019-02-21 10:36:58 +01006571
Radek Krejci327de162019-06-14 12:52:07 +02006572 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006573 }
6574
Radek Krejci327de162019-06-14 12:52:07 +02006575 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006576 ret = LY_SUCCESS;
6577
6578cleanup:
6579 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6580 LY_ARRAY_FREE(devs[u]->deviates);
6581 free(devs[u]);
6582 }
6583 free(devs);
6584 ly_set_erase(&targets, NULL);
6585 ly_set_erase(&devs_p, NULL);
6586
6587 return ret;
6588}
6589
Radek Krejcib56c7502019-02-13 14:19:54 +01006590/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006591 * @brief Compile the given YANG submodule into the main module.
6592 * @param[in] ctx Compile context
6593 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006594 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6595 */
6596LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006597lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006598{
6599 unsigned int u;
6600 LY_ERR ret = LY_SUCCESS;
6601 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006602 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006603 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006604 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006605
Radek Krejci0af46292019-01-11 16:02:31 +01006606 if (!mainmod->mod->off_features) {
6607 /* features are compiled directly into the compiled module structure,
6608 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6609 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006610 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6611 LY_CHECK_GOTO(ret, error);
6612 }
6613 if (!mainmod->mod->off_extensions) {
6614 /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */
6615 ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions);
Radek Krejci0af46292019-01-11 16:02:31 +01006616 LY_CHECK_GOTO(ret, error);
6617 }
6618
Radek Krejci327de162019-06-14 12:52:07 +02006619 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006620 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006621 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006622
Radek Krejci474f9b82019-07-24 11:36:37 +02006623 /* data nodes */
6624 LY_LIST_FOR(submod->data, node_p) {
6625 ret = lys_compile_node(ctx, node_p, NULL, 0);
6626 LY_CHECK_GOTO(ret, error);
6627 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006628
Radek Krejciec4da802019-05-02 13:02:41 +02006629 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6630 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006631
Radek Krejcid05cbd92018-12-05 14:26:40 +01006632error:
6633 return ret;
6634}
6635
Radek Krejci335332a2019-09-05 13:03:35 +02006636static void *
6637lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6638{
6639 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6640 if (substmts[u].stmt == stmt) {
6641 return substmts[u].storage;
6642 }
6643 }
6644 return NULL;
6645}
6646
6647LY_ERR
6648lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6649{
6650 LY_ERR ret = LY_EVALID, r;
6651 unsigned int u;
6652 struct lysp_stmt *stmt;
6653 void *parsed = NULL, **compiled = NULL;
6654 struct ly_set mandatory_stmts = {0};
6655
6656 /* check for invalid substatements */
6657 for (stmt = ext->child; stmt; stmt = stmt->next) {
6658 for (u = 0; substmts[u].stmt; ++u) {
6659 if (substmts[u].stmt == stmt->kw) {
6660 break;
6661 }
6662 }
6663 if (!substmts[u].stmt) {
6664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s\" extension instance.",
6665 stmt->stmt, ext->name);
6666 goto cleanup;
6667 }
6668 if (substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) {
6669 ly_set_add(&mandatory_stmts, &substmts[u], LY_SET_OPT_USEASLIST);
6670 }
6671 }
6672
6673 /* keep order of the processing the same as the order in the defined substmts,
6674 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6675 for (u = 0; substmts[u].stmt; ++u) {
6676 for (stmt = ext->child; stmt; stmt = stmt->next) {
6677 if (substmts[u].stmt != stmt->kw) {
6678 continue;
6679 }
6680
6681 if (substmts[u].storage) {
6682 switch (stmt->kw) {
6683 case LY_STMT_TYPE: {
6684 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6685 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6686
6687 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6688 /* single item */
6689 if (*(struct lysc_type**)substmts->storage) {
6690 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6691 goto cleanup;
6692 }
6693 compiled = substmts[u].storage;
6694 } else {
6695 /* sized array */
6696 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6697 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6698 compiled = (void*)type;
6699 }
6700
6701 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed), ret = r, cleanup);
6702 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6703 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006704 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6705 lysp_type_free(ctx->ctx, parsed);
6706 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006707 break;
6708 }
6709 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc) */
6710 default:
6711 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s\") substatement.",
6712 stmt->stmt, ext->name);
6713 goto cleanup;
6714 }
6715 }
6716
6717 if (substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) {
6718 int i = ly_set_contains(&mandatory_stmts, &substmts[u]);
6719 if (i != -1) {
6720 ly_set_rm_index(&mandatory_stmts, i, NULL);
6721 }
6722 }
6723 }
6724 }
6725
6726 if (mandatory_stmts.count) {
6727 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSTMT, "type", ext->name);
6728 goto cleanup;
6729 }
6730
6731 ret = LY_SUCCESS;
6732
6733cleanup:
6734 ly_set_erase(&mandatory_stmts, NULL);
6735
6736 return ret;
6737}
6738
Radek Krejci19a96102018-11-15 13:38:09 +01006739LY_ERR
6740lys_compile(struct lys_module *mod, int options)
6741{
6742 struct lysc_ctx ctx = {0};
6743 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006744 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006745 struct lysp_module *sp;
6746 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006747 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006748 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006749 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006750 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006751 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006752
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006753 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006754
6755 if (!mod->implemented) {
6756 /* just imported modules are not compiled */
6757 return LY_SUCCESS;
6758 }
6759
Radek Krejci19a96102018-11-15 13:38:09 +01006760 sp = mod->parsed;
6761
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006762 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006763 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006764 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006765 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006766 ctx.path_len = 1;
6767 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006768
6769 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006770 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6771 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006772
Radek Krejciec4da802019-05-02 13:02:41 +02006773 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006774 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006775 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006776 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6777 }
Radek Krejci0935f412019-08-20 16:15:18 +02006778
6779 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01006780 if (mod->off_features) {
6781 /* there is already precompiled array of features */
6782 mod_c->features = mod->off_features;
6783 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006784 } else {
6785 /* features are compiled directly into the compiled module structure,
6786 * 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 +02006787 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006788 LY_CHECK_GOTO(ret, error);
6789 }
6790 /* finish feature compilation, not only for the main module, but also for the submodules.
6791 * Due to possible forward references, it must be done when all the features (including submodules)
6792 * are present. */
6793 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006794 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006795 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6796 }
Radek Krejci327de162019-06-14 12:52:07 +02006797 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006798 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006799 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006800 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006801 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006802 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6803 }
Radek Krejci327de162019-06-14 12:52:07 +02006804 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006805 }
Radek Krejci327de162019-06-14 12:52:07 +02006806 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006807
Radek Krejci0935f412019-08-20 16:15:18 +02006808 /* extensions */
6809 /* 2-steps: a) prepare compiled structures and ... */
6810 if (mod->off_extensions) {
6811 /* there is already precompiled array of extension definitions */
6812 mod_c->extensions = mod->off_extensions;
6813 mod->off_extensions = NULL;
6814 } else {
6815 /* extension definitions are compiled directly into the compiled module structure */
6816 ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions);
6817 }
6818 /* ... b) connect the extension definitions with the appropriate extension plugins */
6819 lys_compile_extension_plugins(mod_c->extensions);
6820
6821 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02006822 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006823 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006824 if (sp->identities) {
6825 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6826 }
Radek Krejci327de162019-06-14 12:52:07 +02006827 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006828
Radek Krejci95710c92019-02-11 15:49:55 +01006829 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006830 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02006831 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01006832 LY_CHECK_GOTO(ret, error);
6833 }
Radek Krejci95710c92019-02-11 15:49:55 +01006834
Radek Krejciec4da802019-05-02 13:02:41 +02006835 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6836 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006837
Radek Krejci95710c92019-02-11 15:49:55 +01006838 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01006839 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01006840 LY_CHECK_GOTO(ret, error);
6841 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006842 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006843 LY_CHECK_GOTO(ret, error);
6844 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006845
Radek Krejci474f9b82019-07-24 11:36:37 +02006846 /* deviations TODO cover deviations from submodules */
Radek Krejciec4da802019-05-02 13:02:41 +02006847 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01006848 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006849
Radek Krejci0935f412019-08-20 16:15:18 +02006850 /* extension instances TODO cover extension instances from submodules */
6851 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006852
Radek Krejcia3045382018-11-22 14:30:31 +01006853 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006854 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6855 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6856 * 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 +01006857 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006858 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006859 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6860 if (type->basetype == LY_TYPE_LEAFREF) {
6861 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006862 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type);
Radek Krejcia3045382018-11-22 14:30:31 +01006863 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006864 } else if (type->basetype == LY_TYPE_UNION) {
6865 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6866 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6867 /* validate the path */
6868 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6869 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
6870 LY_CHECK_GOTO(ret, error);
6871 }
6872 }
Radek Krejcia3045382018-11-22 14:30:31 +01006873 }
6874 }
6875 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006876 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006877 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006878 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6879 if (type->basetype == LY_TYPE_LEAFREF) {
6880 /* store pointer to the real type */
6881 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6882 typeiter->basetype == LY_TYPE_LEAFREF;
6883 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6884 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006885 } else if (type->basetype == LY_TYPE_UNION) {
6886 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6887 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6888 /* store pointer to the real type */
6889 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6890 typeiter->basetype == LY_TYPE_LEAFREF;
6891 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6892 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6893 }
6894 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006895 }
6896 }
6897 }
Radek Krejciec4da802019-05-02 13:02:41 +02006898
Radek Krejci474f9b82019-07-24 11:36:37 +02006899 /* finish incomplete default values compilation */
6900 for (u = 0; u < ctx.dflts.count; ++u) {
6901 struct ly_err_item *err = NULL;
6902 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
6903 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized),
6904 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
6905 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
6906 if (err) {
6907 ly_err_print(err);
6908 ctx.path[0] = '\0';
6909 lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
6910 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
6911 "Invalid default - value does not fit the type (%s).", err->msg);
6912 ly_err_free(err);
6913 }
6914 LY_CHECK_GOTO(ret, error);
6915 }
6916
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006917 /* validate non-instantiated groupings from the parsed schema,
6918 * without it we would accept even the schemas with invalid grouping specification */
6919 ctx.options |= LYSC_OPT_GROUPING;
6920 LY_ARRAY_FOR(sp->groupings, u) {
6921 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
6922 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
6923 }
6924 }
6925 LY_LIST_FOR(sp->data, node_p) {
6926 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
6927 LY_ARRAY_FOR(grps, u) {
6928 if (!(grps[u].flags & LYS_USED_GRP)) {
6929 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
6930 }
6931 }
6932 }
6933
Radek Krejci474f9b82019-07-24 11:36:37 +02006934 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
6935 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
6936 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
6937 }
6938
Radek Krejci1c0c3442019-07-23 16:08:47 +02006939 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006940 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006941 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006942 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006943 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01006944
Radek Krejciec4da802019-05-02 13:02:41 +02006945 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01006946 lysp_module_free(mod->parsed);
6947 ((struct lys_module*)mod)->parsed = NULL;
6948 }
6949
Radek Krejciec4da802019-05-02 13:02:41 +02006950 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01006951 /* remove flag of the modules implemented by dependency */
6952 for (u = 0; u < ctx.ctx->list.count; ++u) {
6953 m = ctx.ctx->list.objs[u];
6954 if (m->implemented == 2) {
6955 m->implemented = 1;
6956 }
6957 }
6958 }
6959
Radek Krejci19a96102018-11-15 13:38:09 +01006960 ((struct lys_module*)mod)->compiled = mod_c;
6961 return LY_SUCCESS;
6962
6963error:
Radek Krejci95710c92019-02-11 15:49:55 +01006964 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02006965 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006966 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006967 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006968 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006969 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006970 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006971 mod->compiled = NULL;
6972
6973 /* revert compilation of modules implemented by dependency */
6974 for (u = 0; u < ctx.ctx->list.count; ++u) {
6975 m = ctx.ctx->list.objs[u];
6976 if (m->implemented == 2) {
6977 /* revert features list to the precompiled state */
6978 lys_feature_precompile_revert(&ctx, m);
6979 /* mark module as imported-only / not-implemented */
6980 m->implemented = 0;
6981 /* free the compiled version of the module */
6982 lysc_module_free(m->compiled, NULL);
6983 m->compiled = NULL;
6984 }
6985 }
6986
Radek Krejci19a96102018-11-15 13:38:09 +01006987 return ret;
6988}