blob: 25e9ffadf984854cb3f4b19e1fea0b512a44cfac [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 Krejci28681fa2019-09-06 13:08:45 +0200438 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200439 ext->parent = parent;
440 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100441
442 /* get module where the extension definition should be placed */
443 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100444 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100445 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
446 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
447 LY_EVALID);
448 LY_CHECK_ERR_RET(!mod->parsed->extensions,
449 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
450 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100451 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100452 LY_EVALID);
453 name = &ext_p->name[u + 1];
Radek Krejci0935f412019-08-20 16:15:18 +0200454
Radek Krejci19a96102018-11-15 13:38:09 +0100455 /* find the extension definition there */
Radek Krejci0935f412019-08-20 16:15:18 +0200456 if (mod->off_extensions) {
457 elist = mod->off_extensions;
458 } else {
459 elist = mod->compiled->extensions;
460 }
461 LY_ARRAY_FOR(elist, u) {
462 if (!strcmp(name, elist[u].name)) {
463 ext->def = &elist[u];
Radek Krejci19a96102018-11-15 13:38:09 +0100464 break;
465 }
466 }
Radek Krejci0935f412019-08-20 16:15:18 +0200467 LY_CHECK_ERR_RET(!ext->def,
468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
469 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100470 LY_EVALID);
Radek Krejci0935f412019-08-20 16:15:18 +0200471
472 if (ext->def->plugin && ext->def->plugin->compile) {
473 LY_CHECK_RET(ext->def->plugin->compile(ctx, ext_p, ext),LY_EVALID);
474 }
475
476 return LY_SUCCESS;
477}
478
479/**
480 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
481 */
482static LY_ERR
483lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext)
484{
485 LY_ERR ret = LY_SUCCESS;
486
487 DUP_STRING(ctx->ctx, ext_p->name, ext->name);
488 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
489 ext->module = ctx->mod_def;
490 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done);
491
492done:
493 return ret;
494}
495
496/**
497 * @brief Link the extensions definitions with the available extension plugins.
498 *
499 * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules
500 * are not connected with even available extension plugins.
501 *
502 * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)).
503 */
504static void
505lys_compile_extension_plugins(struct lysc_ext *extensions)
506{
507 unsigned int u;
508
509 LY_ARRAY_FOR(extensions, u) {
510 extensions[u].plugin = lyext_get_plugin(&extensions[u]);
511 }
512}
513
514LY_ERR
515lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
516 struct lysp_ext *extensions_p, struct lysc_ext **extensions)
517{
518 unsigned int offset = 0, u;
519 struct lysc_ctx context = {0};
520
521 assert(ctx_sc || ctx);
522
523 if (!ctx_sc) {
524 context.ctx = ctx;
525 context.mod = module;
526 context.path_len = 1;
527 context.path[0] = '/';
528 ctx_sc = &context;
529 }
530
531 if (!extensions_p) {
532 return LY_SUCCESS;
533 }
534 if (*extensions) {
535 offset = LY_ARRAY_SIZE(*extensions);
536 }
537
538 lysc_update_path(ctx_sc, NULL, "{extension}");
539 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM);
540 LY_ARRAY_FOR(extensions_p, u) {
541 lysc_update_path(ctx_sc, NULL, extensions_p[u].name);
542 LY_ARRAY_INCREMENT(*extensions);
543 COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name);
544 LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u]));
545 lysc_update_path(ctx_sc, NULL, NULL);
546 }
547 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100548
549 return LY_SUCCESS;
550}
551
Radek Krejcib56c7502019-02-13 14:19:54 +0100552/**
553 * @brief Compile information from the if-feature statement
554 * @param[in] ctx Compile context.
555 * @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 +0100556 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
557 * @return LY_ERR value.
558 */
Radek Krejci19a96102018-11-15 13:38:09 +0100559static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200560lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100561{
562 const char *c = *value;
563 int r, rc = EXIT_FAILURE;
564 int i, j, last_not, checkversion = 0;
565 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
566 uint8_t op;
567 struct iff_stack stack = {0, 0, NULL};
568 struct lysc_feature *f;
569
570 assert(c);
571
572 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
573 for (i = j = last_not = 0; c[i]; i++) {
574 if (c[i] == '(') {
575 j++;
576 checkversion = 1;
577 continue;
578 } else if (c[i] == ')') {
579 j--;
580 continue;
581 } else if (isspace(c[i])) {
582 checkversion = 1;
583 continue;
584 }
585
586 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 +0200587 int sp;
588 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
589 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100590 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
591 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
592 return LY_EVALID;
593 } else if (!isspace(c[i + r])) {
594 /* feature name starting with the not/and/or */
595 last_not = 0;
596 f_size++;
597 } else if (c[i] == 'n') { /* not operation */
598 if (last_not) {
599 /* double not */
600 expr_size = expr_size - 2;
601 last_not = 0;
602 } else {
603 last_not = 1;
604 }
605 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200606 if (f_exp != f_size) {
607 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
608 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
609 return LY_EVALID;
610 }
Radek Krejci19a96102018-11-15 13:38:09 +0100611 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200612
Radek Krejci19a96102018-11-15 13:38:09 +0100613 /* not a not operation */
614 last_not = 0;
615 }
616 i += r;
617 } else {
618 f_size++;
619 last_not = 0;
620 }
621 expr_size++;
622
623 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200624 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100625 i--;
626 break;
627 }
628 i++;
629 }
630 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200631 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100632 /* not matching count of ( and ) */
633 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
634 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
635 return LY_EVALID;
636 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200637 if (f_exp != f_size) {
638 /* features do not match the needed arguments for the logical operations */
639 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
640 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
641 "the required number of operands for the operations.", *value);
642 return LY_EVALID;
643 }
Radek Krejci19a96102018-11-15 13:38:09 +0100644
645 if (checkversion || expr_size > 1) {
646 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100647 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100648 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
649 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
650 return LY_EVALID;
651 }
652 }
653
654 /* allocate the memory */
655 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
656 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
657 stack.stack = malloc(expr_size * sizeof *stack.stack);
658 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
659
660 stack.size = expr_size;
661 f_size--; expr_size--; /* used as indexes from now */
662
663 for (i--; i >= 0; i--) {
664 if (c[i] == ')') {
665 /* push it on stack */
666 iff_stack_push(&stack, LYS_IFF_RP);
667 continue;
668 } else if (c[i] == '(') {
669 /* pop from the stack into result all operators until ) */
670 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
671 iff_setop(iff->expr, op, expr_size--);
672 }
673 continue;
674 } else if (isspace(c[i])) {
675 continue;
676 }
677
678 /* end of operator or operand -> find beginning and get what is it */
679 j = i + 1;
680 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
681 i--;
682 }
683 i++; /* go back by one step */
684
685 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
686 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
687 /* double not */
688 iff_stack_pop(&stack);
689 } else {
690 /* not has the highest priority, so do not pop from the stack
691 * as in case of AND and OR */
692 iff_stack_push(&stack, LYS_IFF_NOT);
693 }
694 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
695 /* as for OR - pop from the stack all operators with the same or higher
696 * priority and store them to the result, then push the AND to the stack */
697 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
698 op = iff_stack_pop(&stack);
699 iff_setop(iff->expr, op, expr_size--);
700 }
701 iff_stack_push(&stack, LYS_IFF_AND);
702 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
703 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
704 op = iff_stack_pop(&stack);
705 iff_setop(iff->expr, op, expr_size--);
706 }
707 iff_stack_push(&stack, LYS_IFF_OR);
708 } else {
709 /* feature name, length is j - i */
710
711 /* add it to the expression */
712 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
713
714 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100715 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
716 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
717 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
718 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100719 iff->features[f_size] = f;
720 LY_ARRAY_INCREMENT(iff->features);
721 f_size--;
722 }
723 }
724 while (stack.index) {
725 op = iff_stack_pop(&stack);
726 iff_setop(iff->expr, op, expr_size--);
727 }
728
729 if (++expr_size || ++f_size) {
730 /* not all expected operators and operands found */
731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
732 "Invalid value \"%s\" of if-feature - processing error.", *value);
733 rc = LY_EINT;
734 } else {
735 rc = LY_SUCCESS;
736 }
737
738error:
739 /* cleanup */
740 iff_stack_clean(&stack);
741
742 return rc;
743}
744
Radek Krejcib56c7502019-02-13 14:19:54 +0100745/**
746 * @brief Compile information from the when statement
747 * @param[in] ctx Compile context.
748 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100749 * @param[out] when Pointer where to store pointer to the created compiled when structure.
750 * @return LY_ERR value.
751 */
Radek Krejci19a96102018-11-15 13:38:09 +0100752static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200753lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100754{
Radek Krejci19a96102018-11-15 13:38:09 +0100755 LY_ERR ret = LY_SUCCESS;
756
Radek Krejci00b874b2019-02-12 10:54:50 +0100757 *when = calloc(1, sizeof **when);
758 (*when)->refcount = 1;
759 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
760 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
761 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
762 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200763 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100764
765done:
766 return ret;
767}
768
Radek Krejcib56c7502019-02-13 14:19:54 +0100769/**
770 * @brief Compile information from the must statement
771 * @param[in] ctx Compile context.
772 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100773 * @param[in,out] must Prepared (empty) compiled must structure to fill.
774 * @return LY_ERR value.
775 */
Radek Krejci19a96102018-11-15 13:38:09 +0100776static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200777lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100778{
Radek Krejci19a96102018-11-15 13:38:09 +0100779 LY_ERR ret = LY_SUCCESS;
780
781 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
782 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200783 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100784 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
785 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100786 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
787 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200788 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100789
790done:
791 return ret;
792}
793
Radek Krejcib56c7502019-02-13 14:19:54 +0100794/**
795 * @brief Compile information from the import statement
796 * @param[in] ctx Compile context.
797 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100798 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
799 * @return LY_ERR value.
800 */
Radek Krejci19a96102018-11-15 13:38:09 +0100801static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200802lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100803{
Radek Krejci19a96102018-11-15 13:38:09 +0100804 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100805 LY_ERR ret = LY_SUCCESS;
806
807 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200808 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100809 imp->module = imp_p->module;
810
Radek Krejci7f2a5362018-11-28 13:05:37 +0100811 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
812 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100813 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100814 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100815 /* try to use filepath if present */
816 if (imp->module->filepath) {
817 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
818 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100819 if (mod != imp->module) {
820 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100821 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100822 mod = NULL;
823 }
824 }
825 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100826 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100827 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 +0100828 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100829 return LY_ENOTFOUND;
830 }
831 }
Radek Krejci19a96102018-11-15 13:38:09 +0100832 }
833
834done:
835 return ret;
836}
837
Radek Krejcib56c7502019-02-13 14:19:54 +0100838/**
839 * @brief Compile information from the identity statement
840 *
841 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
842 *
843 * @param[in] ctx Compile context.
844 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100845 * @param[in] idents List of so far compiled identities to check the name uniqueness.
846 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
847 * @return LY_ERR value.
848 */
Radek Krejci19a96102018-11-15 13:38:09 +0100849static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200850lys_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 +0100851{
852 unsigned int u;
853 LY_ERR ret = LY_SUCCESS;
854
Radek Krejci327de162019-06-14 12:52:07 +0200855 lysc_update_path(ctx, NULL, ident_p->name);
856
Radek Krejcid05cbd92018-12-05 14:26:40 +0100857 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100858 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200859 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
860 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200861 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200862 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100863 /* 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 +0200864 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100865 ident->flags = ident_p->flags;
866
Radek Krejci327de162019-06-14 12:52:07 +0200867 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100868done:
869 return ret;
870}
871
Radek Krejcib56c7502019-02-13 14:19:54 +0100872/**
873 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
874 *
875 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
876 *
877 * @param[in] ctx Compile context for logging.
878 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
879 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
880 * @return LY_SUCCESS if everything is ok.
881 * @return LY_EVALID if the identity is derived from itself.
882 */
Radek Krejci38222632019-02-12 16:55:05 +0100883static LY_ERR
884lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
885{
886 LY_ERR ret = LY_EVALID;
887 unsigned int u, v;
888 struct ly_set recursion = {0};
889 struct lysc_ident *drv;
890
891 if (!derived) {
892 return LY_SUCCESS;
893 }
894
895 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
896 if (ident == derived[u]) {
897 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
898 "Identity \"%s\" is indirectly derived from itself.", ident->name);
899 goto cleanup;
900 }
901 ly_set_add(&recursion, derived[u], 0);
902 }
903
904 for (v = 0; v < recursion.count; ++v) {
905 drv = recursion.objs[v];
906 if (!drv->derived) {
907 continue;
908 }
909 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
910 if (ident == drv->derived[u]) {
911 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
912 "Identity \"%s\" is indirectly derived from itself.", ident->name);
913 goto cleanup;
914 }
915 ly_set_add(&recursion, drv->derived[u], 0);
916 }
917 }
918 ret = LY_SUCCESS;
919
920cleanup:
921 ly_set_erase(&recursion, NULL);
922 return ret;
923}
924
Radek Krejcia3045382018-11-22 14:30:31 +0100925/**
926 * @brief Find and process the referenced base identities from another identity or identityref
927 *
Radek Krejciaca74032019-06-04 08:53:06 +0200928 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +0100929 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
930 * to distinguish these two use cases.
931 *
932 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
933 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
934 * @param[in] ident Referencing identity to work with.
935 * @param[in] bases Array of bases of identityref to fill in.
936 * @return LY_ERR value.
937 */
Radek Krejci19a96102018-11-15 13:38:09 +0100938static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100939lys_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 +0100940{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100941 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100942 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100943 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100944 struct lysc_ident **idref;
945
946 assert(ident || bases);
947
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100948 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100949 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
950 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
951 return LY_EVALID;
952 }
953
954 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
955 s = strchr(bases_p[u], ':');
956 if (s) {
957 /* prefixed identity */
958 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100959 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100960 } else {
961 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100962 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100963 }
964 if (!mod) {
965 if (ident) {
966 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
967 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
968 } else {
969 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
970 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
971 }
972 return LY_EVALID;
973 }
974 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100975 if (mod->compiled && mod->compiled->identities) {
976 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
977 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100978 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100979 if (ident == &mod->compiled->identities[v]) {
980 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
981 "Identity \"%s\" is derived from itself.", ident->name);
982 return LY_EVALID;
983 }
984 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100985 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100986 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100987 *idref = ident;
988 } else {
989 /* we have match! store the found identity */
990 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100991 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100992 }
993 break;
994 }
995 }
996 }
997 if (!idref || !(*idref)) {
998 if (ident) {
999 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1000 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1001 } else {
1002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1003 "Unable to find base (%s) of identityref.", bases_p[u]);
1004 }
1005 return LY_EVALID;
1006 }
1007 }
1008 return LY_SUCCESS;
1009}
1010
Radek Krejcia3045382018-11-22 14:30:31 +01001011/**
1012 * @brief For the given array of identities, set the backlinks from all their base identities.
1013 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1014 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1015 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1016 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1017 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001018static LY_ERR
1019lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1020{
1021 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +01001022
1023 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
1024 if (!idents_p[i].bases) {
1025 continue;
1026 }
Radek Krejci327de162019-06-14 12:52:07 +02001027 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001028 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001029 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001030 }
1031 return LY_SUCCESS;
1032}
1033
Radek Krejci0af46292019-01-11 16:02:31 +01001034LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001035lys_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 +01001036{
1037 unsigned int offset = 0, u;
1038 struct lysc_ctx context = {0};
1039
Radek Krejci327de162019-06-14 12:52:07 +02001040 assert(ctx_sc || ctx);
1041
1042 if (!ctx_sc) {
1043 context.ctx = ctx;
1044 context.mod = module;
1045 context.path_len = 1;
1046 context.path[0] = '/';
1047 ctx_sc = &context;
1048 }
Radek Krejci0af46292019-01-11 16:02:31 +01001049
1050 if (!features_p) {
1051 return LY_SUCCESS;
1052 }
1053 if (*features) {
1054 offset = LY_ARRAY_SIZE(*features);
1055 }
1056
Radek Krejci327de162019-06-14 12:52:07 +02001057 lysc_update_path(ctx_sc, NULL, "{feature}");
1058 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001059 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001060 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1061
Radek Krejci0af46292019-01-11 16:02:31 +01001062 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +02001063 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
1064 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1065 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1066 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001067 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001068 (*features)[offset + u].module = ctx_sc->mod;
1069
1070 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001071 }
Radek Krejci327de162019-06-14 12:52:07 +02001072 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001073
1074 return LY_SUCCESS;
1075}
1076
Radek Krejcia3045382018-11-22 14:30:31 +01001077/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001078 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001079 *
1080 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1081 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001082 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001083 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1084 * being currently processed).
1085 * @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 +01001086 * @return LY_SUCCESS if everything is ok.
1087 * @return LY_EVALID if the feature references indirectly itself.
1088 */
1089static LY_ERR
1090lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1091{
1092 LY_ERR ret = LY_EVALID;
1093 unsigned int u, v;
1094 struct ly_set recursion = {0};
1095 struct lysc_feature *drv;
1096
1097 if (!depfeatures) {
1098 return LY_SUCCESS;
1099 }
1100
1101 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1102 if (feature == depfeatures[u]) {
1103 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1104 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1105 goto cleanup;
1106 }
1107 ly_set_add(&recursion, depfeatures[u], 0);
1108 }
1109
1110 for (v = 0; v < recursion.count; ++v) {
1111 drv = recursion.objs[v];
1112 if (!drv->depfeatures) {
1113 continue;
1114 }
1115 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1116 if (feature == drv->depfeatures[u]) {
1117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1118 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1119 goto cleanup;
1120 }
1121 ly_set_add(&recursion, drv->depfeatures[u], 0);
1122 }
1123 }
1124 ret = LY_SUCCESS;
1125
1126cleanup:
1127 ly_set_erase(&recursion, NULL);
1128 return ret;
1129}
1130
1131/**
Radek Krejci0af46292019-01-11 16:02:31 +01001132 * @brief Create pre-compiled features array.
1133 *
1134 * See lys_feature_precompile() for more details.
1135 *
Radek Krejcia3045382018-11-22 14:30:31 +01001136 * @param[in] ctx Compile context.
1137 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001138 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001139 * @return LY_ERR value.
1140 */
Radek Krejci19a96102018-11-15 13:38:09 +01001141static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001142lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001143{
Radek Krejci0af46292019-01-11 16:02:31 +01001144 unsigned int u, v, x;
1145 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001146 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001147
Radek Krejci327de162019-06-14 12:52:07 +02001148
Radek Krejci0af46292019-01-11 16:02:31 +01001149 /* find the preprecompiled feature */
1150 LY_ARRAY_FOR(features, x) {
1151 if (strcmp(features[x].name, feature_p->name)) {
1152 continue;
1153 }
1154 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001155 lysc_update_path(ctx, NULL, "{feature}");
1156 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001157
Radek Krejci0af46292019-01-11 16:02:31 +01001158 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001159 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001160 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001161 if (feature->iffeatures) {
1162 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1163 if (feature->iffeatures[u].features) {
1164 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001165 /* check for circular dependency - direct reference first,... */
1166 if (feature == feature->iffeatures[u].features[v]) {
1167 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1168 "Feature \"%s\" is referenced from itself.", feature->name);
1169 return LY_EVALID;
1170 }
1171 /* ... and indirect circular reference */
1172 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1173
Radek Krejci0af46292019-01-11 16:02:31 +01001174 /* add itself into the dependants list */
1175 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1176 *df = feature;
1177 }
Radek Krejci19a96102018-11-15 13:38:09 +01001178 }
Radek Krejci19a96102018-11-15 13:38:09 +01001179 }
1180 }
Radek Krejci327de162019-06-14 12:52:07 +02001181 lysc_update_path(ctx, NULL, NULL);
1182 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001183 done:
1184 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001185 }
Radek Krejci0af46292019-01-11 16:02:31 +01001186
1187 LOGINT(ctx->ctx);
1188 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001189}
1190
Radek Krejcib56c7502019-02-13 14:19:54 +01001191/**
1192 * @brief Revert compiled list of features back to the precompiled state.
1193 *
1194 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1195 * The features are supposed to be stored again as off_features in ::lys_module structure.
1196 *
1197 * @param[in] ctx Compilation context.
1198 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1199 * and supposed to hold the off_features list.
1200 */
Radek Krejci95710c92019-02-11 15:49:55 +01001201static void
1202lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1203{
1204 unsigned int u, v;
1205
1206 /* keep the off_features list until the complete lys_module is freed */
1207 mod->off_features = mod->compiled->features;
1208 mod->compiled->features = NULL;
1209
1210 /* in the off_features list, remove all the parts (from finished compiling process)
1211 * which may points into the data being freed here */
1212 LY_ARRAY_FOR(mod->off_features, u) {
1213 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1214 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1215 }
1216 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1217 mod->off_features[u].iffeatures = NULL;
1218
1219 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1220 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1221 }
1222 LY_ARRAY_FREE(mod->off_features[u].exts);
1223 mod->off_features[u].exts = NULL;
1224 }
1225}
1226
Radek Krejcia3045382018-11-22 14:30:31 +01001227/**
1228 * @brief Validate and normalize numeric value from a range definition.
1229 * @param[in] ctx Compile context.
1230 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1231 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1232 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1233 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1234 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1235 * @param[in] value String value of the range boundary.
1236 * @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.
1237 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1238 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1239 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001240LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001241range_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 +01001242{
Radek Krejci6cba4292018-11-15 17:33:29 +01001243 size_t fraction = 0, size;
1244
Radek Krejci19a96102018-11-15 13:38:09 +01001245 *len = 0;
1246
1247 assert(value);
1248 /* parse value */
1249 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1250 return LY_EVALID;
1251 }
1252
1253 if ((value[*len] == '-') || (value[*len] == '+')) {
1254 ++(*len);
1255 }
1256
1257 while (isdigit(value[*len])) {
1258 ++(*len);
1259 }
1260
1261 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001262 if (basetype == LY_TYPE_DEC64) {
1263 goto decimal;
1264 } else {
1265 *valcopy = strndup(value, *len);
1266 return LY_SUCCESS;
1267 }
Radek Krejci19a96102018-11-15 13:38:09 +01001268 }
1269 fraction = *len;
1270
1271 ++(*len);
1272 while (isdigit(value[*len])) {
1273 ++(*len);
1274 }
1275
Radek Krejci6cba4292018-11-15 17:33:29 +01001276 if (basetype == LY_TYPE_DEC64) {
1277decimal:
1278 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001279 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1281 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1282 *len, value, frdigits);
1283 return LY_EINVAL;
1284 }
1285 if (fraction) {
1286 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1287 } else {
1288 size = (*len) + frdigits + 1;
1289 }
1290 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001291 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1292
Radek Krejci6cba4292018-11-15 17:33:29 +01001293 (*valcopy)[size - 1] = '\0';
1294 if (fraction) {
1295 memcpy(&(*valcopy)[0], &value[0], fraction);
1296 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1297 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1298 } else {
1299 memcpy(&(*valcopy)[0], &value[0], *len);
1300 memset(&(*valcopy)[*len], '0', frdigits);
1301 }
Radek Krejci19a96102018-11-15 13:38:09 +01001302 }
1303 return LY_SUCCESS;
1304}
1305
Radek Krejcia3045382018-11-22 14:30:31 +01001306/**
1307 * @brief Check that values in range are in ascendant order.
1308 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001309 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1310 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001311 * @param[in] value Current value to check.
1312 * @param[in] prev_value The last seen value.
1313 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1314 */
Radek Krejci19a96102018-11-15 13:38:09 +01001315static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001316range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001317{
1318 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001319 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001320 return LY_EEXIST;
1321 }
1322 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001323 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001324 return LY_EEXIST;
1325 }
1326 }
1327 return LY_SUCCESS;
1328}
1329
Radek Krejcia3045382018-11-22 14:30:31 +01001330/**
1331 * @brief Set min/max value of the range part.
1332 * @param[in] ctx Compile context.
1333 * @param[in] part Range part structure to fill.
1334 * @param[in] max Flag to distinguish if storing min or max value.
1335 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1336 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1337 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1338 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1339 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001340 * @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 +01001341 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1342 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1343 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1344 * frdigits value), LY_EMEM.
1345 */
Radek Krejci19a96102018-11-15 13:38:09 +01001346static LY_ERR
1347range_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 +01001348 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001349{
1350 LY_ERR ret = LY_SUCCESS;
1351 char *valcopy = NULL;
1352 size_t len;
1353
1354 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001355 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001356 LY_CHECK_GOTO(ret, finalize);
1357 }
1358 if (!valcopy && base_range) {
1359 if (max) {
1360 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1361 } else {
1362 part->min_64 = base_range->parts[0].min_64;
1363 }
1364 if (!first) {
1365 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1366 }
1367 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001368 }
1369
1370 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001371 case LY_TYPE_INT8: /* range */
1372 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001373 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 +01001374 } else if (max) {
1375 part->max_64 = INT64_C(127);
1376 } else {
1377 part->min_64 = INT64_C(-128);
1378 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001379 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001380 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001381 }
1382 break;
1383 case LY_TYPE_INT16: /* range */
1384 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001385 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 +01001386 } else if (max) {
1387 part->max_64 = INT64_C(32767);
1388 } else {
1389 part->min_64 = INT64_C(-32768);
1390 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001391 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001392 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001393 }
1394 break;
1395 case LY_TYPE_INT32: /* range */
1396 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001397 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 +01001398 } else if (max) {
1399 part->max_64 = INT64_C(2147483647);
1400 } else {
1401 part->min_64 = INT64_C(-2147483648);
1402 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001403 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001404 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001405 }
1406 break;
1407 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001408 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001409 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001410 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001411 max ? &part->max_64 : &part->min_64);
1412 } else if (max) {
1413 part->max_64 = INT64_C(9223372036854775807);
1414 } else {
1415 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1416 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001417 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001418 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001419 }
1420 break;
1421 case LY_TYPE_UINT8: /* range */
1422 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001423 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 +01001424 } else if (max) {
1425 part->max_u64 = UINT64_C(255);
1426 } else {
1427 part->min_u64 = UINT64_C(0);
1428 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001429 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001430 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001431 }
1432 break;
1433 case LY_TYPE_UINT16: /* range */
1434 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001435 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 +01001436 } else if (max) {
1437 part->max_u64 = UINT64_C(65535);
1438 } else {
1439 part->min_u64 = UINT64_C(0);
1440 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001441 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001442 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001443 }
1444 break;
1445 case LY_TYPE_UINT32: /* range */
1446 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001447 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 +01001448 } else if (max) {
1449 part->max_u64 = UINT64_C(4294967295);
1450 } else {
1451 part->min_u64 = UINT64_C(0);
1452 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001453 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001454 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001455 }
1456 break;
1457 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001458 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001459 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001460 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001461 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 +01001462 } else if (max) {
1463 part->max_u64 = UINT64_C(18446744073709551615);
1464 } else {
1465 part->min_u64 = UINT64_C(0);
1466 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001467 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001468 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001469 }
1470 break;
1471 default:
1472 LOGINT(ctx->ctx);
1473 ret = LY_EINT;
1474 }
1475
Radek Krejci5969f272018-11-23 10:03:58 +01001476finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001477 if (ret == LY_EDENIED) {
1478 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1479 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1480 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1481 } else if (ret == LY_EVALID) {
1482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1483 "Invalid %s restriction - invalid value \"%s\".",
1484 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1485 } else if (ret == LY_EEXIST) {
1486 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1487 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001488 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001489 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001490 } else if (!ret && value) {
1491 *value = *value + len;
1492 }
1493 free(valcopy);
1494 return ret;
1495}
1496
Radek Krejcia3045382018-11-22 14:30:31 +01001497/**
1498 * @brief Compile the parsed range restriction.
1499 * @param[in] ctx Compile context.
1500 * @param[in] range_p Parsed range structure to compile.
1501 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1502 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1503 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1504 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1505 * range restriction must be more restrictive than the base_range.
1506 * @param[in,out] range Pointer to the created current range structure.
1507 * @return LY_ERR value.
1508 */
Radek Krejci19a96102018-11-15 13:38:09 +01001509static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001510lys_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 +01001511 struct lysc_range *base_range, struct lysc_range **range)
1512{
1513 LY_ERR ret = LY_EVALID;
1514 const char *expr;
1515 struct lysc_range_part *parts = NULL, *part;
1516 int range_expected = 0, uns;
1517 unsigned int parts_done = 0, u, v;
1518
1519 assert(range);
1520 assert(range_p);
1521
1522 expr = range_p->arg;
1523 while(1) {
1524 if (isspace(*expr)) {
1525 ++expr;
1526 } else if (*expr == '\0') {
1527 if (range_expected) {
1528 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1529 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1530 length_restr ? "length" : "range", range_p->arg);
1531 goto cleanup;
1532 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1534 "Invalid %s restriction - unexpected end of the expression (%s).",
1535 length_restr ? "length" : "range", range_p->arg);
1536 goto cleanup;
1537 }
1538 parts_done++;
1539 break;
1540 } else if (!strncmp(expr, "min", 3)) {
1541 if (parts) {
1542 /* min cannot be used elsewhere than in the first part */
1543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1544 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1545 expr - range_p->arg, range_p->arg);
1546 goto cleanup;
1547 }
1548 expr += 3;
1549
1550 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001551 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 +01001552 part->max_64 = part->min_64;
1553 } else if (*expr == '|') {
1554 if (!parts || range_expected) {
1555 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1556 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1557 goto cleanup;
1558 }
1559 expr++;
1560 parts_done++;
1561 /* process next part of the expression */
1562 } else if (!strncmp(expr, "..", 2)) {
1563 expr += 2;
1564 while (isspace(*expr)) {
1565 expr++;
1566 }
1567 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1568 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1569 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1570 goto cleanup;
1571 }
1572 /* continue expecting the upper boundary */
1573 range_expected = 1;
1574 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1575 /* number */
1576 if (range_expected) {
1577 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001578 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 +01001579 range_expected = 0;
1580 } else {
1581 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1582 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 +01001583 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001584 part->max_64 = part->min_64;
1585 }
1586
1587 /* continue with possible another expression part */
1588 } else if (!strncmp(expr, "max", 3)) {
1589 expr += 3;
1590 while (isspace(*expr)) {
1591 expr++;
1592 }
1593 if (*expr != '\0') {
1594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1595 length_restr ? "length" : "range", expr);
1596 goto cleanup;
1597 }
1598 if (range_expected) {
1599 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001600 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 +01001601 range_expected = 0;
1602 } else {
1603 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1604 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 +01001605 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001606 part->min_64 = part->max_64;
1607 }
1608 } else {
1609 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1610 length_restr ? "length" : "range", expr);
1611 goto cleanup;
1612 }
1613 }
1614
1615 /* check with the previous range/length restriction */
1616 if (base_range) {
1617 switch (basetype) {
1618 case LY_TYPE_BINARY:
1619 case LY_TYPE_UINT8:
1620 case LY_TYPE_UINT16:
1621 case LY_TYPE_UINT32:
1622 case LY_TYPE_UINT64:
1623 case LY_TYPE_STRING:
1624 uns = 1;
1625 break;
1626 case LY_TYPE_DEC64:
1627 case LY_TYPE_INT8:
1628 case LY_TYPE_INT16:
1629 case LY_TYPE_INT32:
1630 case LY_TYPE_INT64:
1631 uns = 0;
1632 break;
1633 default:
1634 LOGINT(ctx->ctx);
1635 ret = LY_EINT;
1636 goto cleanup;
1637 }
1638 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1639 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1640 goto baseerror;
1641 }
1642 /* current lower bound is not lower than the base */
1643 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1644 /* base has single value */
1645 if (base_range->parts[v].min_64 == parts[u].min_64) {
1646 /* both lower bounds are the same */
1647 if (parts[u].min_64 != parts[u].max_64) {
1648 /* current continues with a range */
1649 goto baseerror;
1650 } else {
1651 /* equal single values, move both forward */
1652 ++v;
1653 continue;
1654 }
1655 } else {
1656 /* base is single value lower than current range, so the
1657 * value from base range is removed in the current,
1658 * move only base and repeat checking */
1659 ++v;
1660 --u;
1661 continue;
1662 }
1663 } else {
1664 /* base is the range */
1665 if (parts[u].min_64 == parts[u].max_64) {
1666 /* current is a single value */
1667 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1668 /* current is behind the base range, so base range is omitted,
1669 * move the base and keep the current for further check */
1670 ++v;
1671 --u;
1672 } /* else it is within the base range, so move the current, but keep the base */
1673 continue;
1674 } else {
1675 /* both are ranges - check the higher bound, the lower was already checked */
1676 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1677 /* higher bound is higher than the current higher bound */
1678 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1679 /* but the current lower bound is also higher, so the base range is omitted,
1680 * continue with the same current, but move the base */
1681 --u;
1682 ++v;
1683 continue;
1684 }
1685 /* current range starts within the base range but end behind it */
1686 goto baseerror;
1687 } else {
1688 /* current range is smaller than the base,
1689 * move current, but stay with the base */
1690 continue;
1691 }
1692 }
1693 }
1694 }
1695 if (u != parts_done) {
1696baseerror:
1697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1698 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1699 length_restr ? "length" : "range", range_p->arg);
1700 goto cleanup;
1701 }
1702 }
1703
1704 if (!(*range)) {
1705 *range = calloc(1, sizeof **range);
1706 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1707 }
1708
Radek Krejcic8b31002019-01-08 10:24:45 +01001709 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001710 if (range_p->eapptag) {
1711 lydict_remove(ctx->ctx, (*range)->eapptag);
1712 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1713 }
1714 if (range_p->emsg) {
1715 lydict_remove(ctx->ctx, (*range)->emsg);
1716 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1717 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001718 if (range_p->dsc) {
1719 lydict_remove(ctx->ctx, (*range)->dsc);
1720 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1721 }
1722 if (range_p->ref) {
1723 lydict_remove(ctx->ctx, (*range)->ref);
1724 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1725 }
Radek Krejci19a96102018-11-15 13:38:09 +01001726 /* extensions are taken only from the last range by the caller */
1727
1728 (*range)->parts = parts;
1729 parts = NULL;
1730 ret = LY_SUCCESS;
1731cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001732 LY_ARRAY_FREE(parts);
1733
1734 return ret;
1735}
1736
1737/**
1738 * @brief Checks pattern syntax.
1739 *
Radek Krejcia3045382018-11-22 14:30:31 +01001740 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001741 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001742 * @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 +01001743 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001744 */
1745static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001746lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001747{
Radek Krejci54579462019-04-30 12:47:06 +02001748 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001749 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001750 int err_code;
1751 const char *orig_ptr;
1752 PCRE2_SIZE err_offset;
1753 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001754#define URANGE_LEN 19
1755 char *ublock2urange[][2] = {
1756 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1757 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1758 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1759 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1760 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1761 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1762 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1763 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1764 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1765 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1766 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1767 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1768 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1769 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1770 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1771 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1772 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1773 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1774 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1775 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1776 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1777 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1778 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1779 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1780 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1781 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1782 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1783 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1784 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1785 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1786 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1787 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1788 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1789 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1790 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1791 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1792 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1793 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1794 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1795 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1796 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1797 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1798 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1799 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1800 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1801 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1802 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1803 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1804 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1805 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1806 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1807 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1808 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1809 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1810 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1811 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1812 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1813 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1814 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1815 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1816 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1817 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1818 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1819 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1820 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1821 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1822 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1823 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1824 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1825 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1826 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1827 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1828 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1829 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1830 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1831 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1832 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1833 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1834 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1835 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1836 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1837 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1838 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1839 {NULL, NULL}
1840 };
1841
1842 /* adjust the expression to a Perl equivalent
1843 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1844
1845 /* we need to replace all "$" with "\$", count them now */
1846 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1847
1848 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1849 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1850 perl_regex[0] = '\0';
1851
1852 ptr = perl_regex;
1853
Radek Krejci19a96102018-11-15 13:38:09 +01001854 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1855 if (orig_ptr[0] == '$') {
1856 ptr += sprintf(ptr, "\\$");
1857 } else if (orig_ptr[0] == '^') {
1858 ptr += sprintf(ptr, "\\^");
1859 } else {
1860 ptr[0] = orig_ptr[0];
1861 ++ptr;
1862 }
1863 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001864 ptr[0] = '\0';
1865 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001866
1867 /* substitute Unicode Character Blocks with exact Character Ranges */
1868 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1869 start = ptr - perl_regex;
1870
1871 ptr = strchr(ptr, '}');
1872 if (!ptr) {
1873 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1874 pattern, perl_regex + start + 2, "unterminated character property");
1875 free(perl_regex);
1876 return LY_EVALID;
1877 }
1878 end = (ptr - perl_regex) + 1;
1879
1880 /* need more space */
1881 if (end - start < URANGE_LEN) {
1882 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1883 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1884 }
1885
1886 /* find our range */
1887 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1888 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1889 break;
1890 }
1891 }
1892 if (!ublock2urange[idx][0]) {
1893 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1894 pattern, perl_regex + start + 5, "unknown block name");
1895 free(perl_regex);
1896 return LY_EVALID;
1897 }
1898
1899 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1900 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1901 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1902 ++count;
1903 }
1904 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1905 --count;
1906 }
1907 }
1908 if (count) {
1909 /* skip brackets */
1910 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1911 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1912 } else {
1913 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1914 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1915 }
1916 }
1917
1918 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02001919 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1920 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02001921 &err_code, &err_offset, NULL);
1922 if (!code_local) {
1923 PCRE2_UCHAR err_msg[256] = {0};
1924 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1926 free(perl_regex);
1927 return LY_EVALID;
1928 }
1929 free(perl_regex);
1930
Radek Krejci54579462019-04-30 12:47:06 +02001931 if (code) {
1932 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001933 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001934 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001935 }
1936
1937 return LY_SUCCESS;
1938
1939#undef URANGE_LEN
1940}
1941
Radek Krejcia3045382018-11-22 14:30:31 +01001942/**
1943 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1944 * @param[in] ctx Compile context.
1945 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001946 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1947 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1948 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1949 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1950 */
Radek Krejci19a96102018-11-15 13:38:09 +01001951static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001952lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01001953 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1954{
1955 struct lysc_pattern **pattern;
Radek Krejci0935f412019-08-20 16:15:18 +02001956 unsigned int u;
Radek Krejci19a96102018-11-15 13:38:09 +01001957 LY_ERR ret = LY_SUCCESS;
1958
1959 /* first, copy the patterns from the base type */
1960 if (base_patterns) {
1961 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1962 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1963 }
1964
1965 LY_ARRAY_FOR(patterns_p, u) {
1966 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1967 *pattern = calloc(1, sizeof **pattern);
1968 ++(*pattern)->refcount;
1969
Radek Krejci54579462019-04-30 12:47:06 +02001970 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01001971 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01001972
1973 if (patterns_p[u].arg[0] == 0x15) {
1974 (*pattern)->inverted = 1;
1975 }
Radek Krejci54579462019-04-30 12:47:06 +02001976 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01001977 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1978 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001979 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1980 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02001981 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01001982 }
1983done:
1984 return ret;
1985}
1986
Radek Krejcia3045382018-11-22 14:30:31 +01001987/**
1988 * @brief map of the possible restrictions combination for the specific built-in type.
1989 */
Radek Krejci19a96102018-11-15 13:38:09 +01001990static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1991 0 /* LY_TYPE_UNKNOWN */,
1992 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001993 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1994 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1995 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1996 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1997 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001998 LYS_SET_BIT /* LY_TYPE_BITS */,
1999 0 /* LY_TYPE_BOOL */,
2000 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2001 0 /* LY_TYPE_EMPTY */,
2002 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2003 LYS_SET_BASE /* LY_TYPE_IDENT */,
2004 LYS_SET_REQINST /* LY_TYPE_INST */,
2005 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002006 LYS_SET_TYPE /* LY_TYPE_UNION */,
2007 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002008 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002009 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002010 LYS_SET_RANGE /* LY_TYPE_INT64 */
2011};
2012
2013/**
2014 * @brief stringification of the YANG built-in data types
2015 */
2016const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2017 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2018 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002019};
2020
Radek Krejcia3045382018-11-22 14:30:31 +01002021/**
2022 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2023 * @param[in] ctx Compile context.
2024 * @param[in] enums_p Array of the parsed enum structures to compile.
2025 * @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 +01002026 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2027 * @param[out] enums Newly created array of the compiled enums information for the current type.
2028 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2029 */
Radek Krejci19a96102018-11-15 13:38:09 +01002030static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002031lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002032 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002033{
2034 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02002035 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002036 int32_t value = 0;
2037 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002038 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002039
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002040 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002041 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2042 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2043 return LY_EVALID;
2044 }
2045
2046 LY_ARRAY_FOR(enums_p, u) {
2047 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2048 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002049 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2050 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002051 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002052 if (base_enums) {
2053 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2054 LY_ARRAY_FOR(base_enums, v) {
2055 if (!strcmp(e->name, base_enums[v].name)) {
2056 break;
2057 }
2058 }
2059 if (v == LY_ARRAY_SIZE(base_enums)) {
2060 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2061 "Invalid %s - derived type adds new item \"%s\".",
2062 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2063 return LY_EVALID;
2064 }
2065 match = v;
2066 }
2067
2068 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002069 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002070 if (enums_p[u].flags & LYS_SET_VALUE) {
2071 e->value = (int32_t)enums_p[u].value;
2072 if (!u || e->value >= value) {
2073 value = e->value + 1;
2074 }
2075 /* check collision with other values */
2076 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2077 if (e->value == (*enums)[v].value) {
2078 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2079 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2080 e->value, e->name, (*enums)[v].name);
2081 return LY_EVALID;
2082 }
2083 }
2084 } else if (base_enums) {
2085 /* inherit the assigned value */
2086 e->value = base_enums[match].value;
2087 if (!u || e->value >= value) {
2088 value = e->value + 1;
2089 }
2090 } else {
2091 /* assign value automatically */
2092 if (u && value == INT32_MIN) {
2093 /* counter overflow */
2094 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2095 "Invalid enumeration - it is not possible to auto-assign enum value for "
2096 "\"%s\" since the highest value is already 2147483647.", e->name);
2097 return LY_EVALID;
2098 }
2099 e->value = value++;
2100 }
2101 } else { /* LY_TYPE_BITS */
2102 if (enums_p[u].flags & LYS_SET_VALUE) {
2103 e->value = (int32_t)enums_p[u].value;
2104 if (!u || (uint32_t)e->value >= position) {
2105 position = (uint32_t)e->value + 1;
2106 }
2107 /* check collision with other values */
2108 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2109 if (e->value == (*enums)[v].value) {
2110 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2111 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2112 (uint32_t)e->value, e->name, (*enums)[v].name);
2113 return LY_EVALID;
2114 }
2115 }
2116 } else if (base_enums) {
2117 /* inherit the assigned value */
2118 e->value = base_enums[match].value;
2119 if (!u || (uint32_t)e->value >= position) {
2120 position = (uint32_t)e->value + 1;
2121 }
2122 } else {
2123 /* assign value automatically */
2124 if (u && position == 0) {
2125 /* counter overflow */
2126 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2127 "Invalid bits - it is not possible to auto-assign bit position for "
2128 "\"%s\" since the highest value is already 4294967295.", e->name);
2129 return LY_EVALID;
2130 }
2131 e->value = position++;
2132 }
2133 }
2134
2135 if (base_enums) {
2136 /* the assigned values must not change from the derived type */
2137 if (e->value != base_enums[match].value) {
2138 if (basetype == LY_TYPE_ENUM) {
2139 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2140 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2141 e->name, base_enums[match].value, e->value);
2142 } else {
2143 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2144 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2145 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2146 }
2147 return LY_EVALID;
2148 }
2149 }
2150
Radek Krejciec4da802019-05-02 13:02:41 +02002151 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002152 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 +01002153
2154 if (basetype == LY_TYPE_BITS) {
2155 /* keep bits ordered by position */
2156 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2157 if (v != u) {
2158 memcpy(&storage, e, sizeof *e);
2159 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2160 memcpy(&(*enums)[v], &storage, sizeof storage);
2161 }
2162 }
2163 }
2164
2165done:
2166 return ret;
2167}
2168
Radek Krejcia3045382018-11-22 14:30:31 +01002169#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2170 for ((NODE) = (NODE)->parent; \
2171 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
2172 (NODE) = (NODE)->parent); \
2173 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2174 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2175 TERM; \
2176 }
2177
2178/**
2179 * @brief Validate the predicate(s) from the leafref path.
2180 * @param[in] ctx Compile context
2181 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2182 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002183 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002184 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002185 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002186 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2187 */
2188static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002189lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002190 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002191{
2192 LY_ERR ret = LY_EVALID;
2193 const struct lys_module *mod;
2194 const struct lysc_node *src_node, *dst_node;
2195 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2196 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002197 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002198 const char *start, *end, *pke_end;
2199 struct ly_set keys = {0};
2200 int i;
2201
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002202 assert(path_context);
2203
Radek Krejcia3045382018-11-22 14:30:31 +01002204 while (**predicate == '[') {
2205 start = (*predicate)++;
2206
2207 while (isspace(**predicate)) {
2208 ++(*predicate);
2209 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002210 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002211 while (isspace(**predicate)) {
2212 ++(*predicate);
2213 }
2214 if (**predicate != '=') {
2215 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002216 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2217 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002218 goto cleanup;
2219 }
2220 ++(*predicate);
2221 while (isspace(**predicate)) {
2222 ++(*predicate);
2223 }
2224
2225 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2226 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2227 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2228 goto cleanup;
2229 }
2230 --pke_end;
2231 while (isspace(*pke_end)) {
2232 --pke_end;
2233 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002234 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002235 /* localize path-key-expr */
2236 pke_start = path_key_expr = *predicate;
2237 /* move after the current predicate */
2238 *predicate = end + 1;
2239
2240 /* source (must be leaf or leaf-list) */
2241 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002242 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002243 if (!mod) {
2244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2245 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002246 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002247 goto cleanup;
2248 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002249 if (!mod->implemented) {
2250 /* make the module implemented */
2251 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2252 }
Radek Krejcia3045382018-11-22 14:30:31 +01002253 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002254 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002255 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002256 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002257 if (!(context_node->flags & LYS_KEYLESS)) {
2258 struct lysc_node *key;
2259 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
2260 if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') {
2261 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002262 break;
2263 }
2264 }
2265 }
Radek Krejcia3045382018-11-22 14:30:31 +01002266 if (!src_node) {
2267 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002268 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002269 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002270 goto cleanup;
2271 }
Radek Krejcia3045382018-11-22 14:30:31 +01002272
2273 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002274 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002275 i = ly_set_add(&keys, (void*)src_node, 0);
2276 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002277 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002278 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002279 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002280 *predicate - start, start, src_node->name);
2281 goto cleanup;
2282 }
2283
2284 /* destination */
2285 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002286 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002287
2288 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002289 if (strncmp(path_key_expr, "current", 7)) {
2290error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002291 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2292 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2293 *predicate - start, start);
2294 goto cleanup;
2295 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002296 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2297 if (*path_key_expr != '(') {
2298 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002299 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002300 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2301 if (*path_key_expr != ')') {
2302 goto error_current_function_invocation;
2303 }
2304 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002305
2306 if (*path_key_expr != '/') {
2307 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2308 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2309 *predicate - start, start);
2310 goto cleanup;
2311 }
2312 ++path_key_expr;
2313 while (isspace(*path_key_expr)) {
2314 ++path_key_expr;
2315 }
2316
2317 /* rel-path-keyexpr:
2318 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2319 while (!strncmp(path_key_expr, "..", 2)) {
2320 ++dest_parent_times;
2321 path_key_expr += 2;
2322 while (isspace(*path_key_expr)) {
2323 ++path_key_expr;
2324 }
2325 if (*path_key_expr != '/') {
2326 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2327 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2328 *predicate - start, start);
2329 goto cleanup;
2330 }
2331 ++path_key_expr;
2332 while (isspace(*path_key_expr)) {
2333 ++path_key_expr;
2334 }
2335
2336 /* path is supposed to be evaluated in data tree, so we have to skip
2337 * all schema nodes that cannot be instantiated in data tree */
2338 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2339 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2340 *predicate - start, start);
2341 }
2342 if (!dest_parent_times) {
2343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2344 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2345 *predicate - start, start);
2346 goto cleanup;
2347 }
2348 if (path_key_expr == pke_end) {
2349 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2350 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2351 *predicate - start, start);
2352 goto cleanup;
2353 }
2354
2355 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002356 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002357 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002358 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002359 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2360 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002361 goto cleanup;
2362 }
2363
2364 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002365 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002366 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002367 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002368 }
2369 if (!mod) {
2370 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002371 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002372 *predicate - start, start, dst_len, dst);
2373 goto cleanup;
2374 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002375 if (!mod->implemented) {
2376 /* make the module implemented */
2377 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2378 }
Radek Krejcia3045382018-11-22 14:30:31 +01002379
2380 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2381 if (!dst_node) {
2382 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002383 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002384 *predicate - start, start, path_key_expr - pke_start, pke_start);
2385 goto cleanup;
2386 }
2387 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002388 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 +01002389 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002390 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002391 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2392 goto cleanup;
2393 }
2394 }
2395
2396 ret = LY_SUCCESS;
2397cleanup:
2398 ly_set_erase(&keys, NULL);
2399 return ret;
2400}
2401
2402/**
2403 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2404 *
2405 * path-arg = absolute-path / relative-path
2406 * absolute-path = 1*("/" (node-identifier *path-predicate))
2407 * relative-path = 1*(".." "/") descendant-path
2408 *
2409 * @param[in,out] path Path to parse.
2410 * @param[out] prefix Prefix of the token, NULL if there is not any.
2411 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2412 * @param[out] name Name of the token.
2413 * @param[out] nam_len Length of the name.
2414 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2415 * must not be changed between consecutive calls. -1 if the
2416 * path is absolute.
2417 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2418 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2419 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002420LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002421lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2422 int *parent_times, int *has_predicate)
2423{
2424 int par_times = 0;
2425
2426 assert(path && *path);
2427 assert(parent_times);
2428 assert(prefix);
2429 assert(prefix_len);
2430 assert(name);
2431 assert(name_len);
2432 assert(has_predicate);
2433
2434 *prefix = NULL;
2435 *prefix_len = 0;
2436 *name = NULL;
2437 *name_len = 0;
2438 *has_predicate = 0;
2439
2440 if (!*parent_times) {
2441 if (!strncmp(*path, "..", 2)) {
2442 *path += 2;
2443 ++par_times;
2444 while (!strncmp(*path, "/..", 3)) {
2445 *path += 3;
2446 ++par_times;
2447 }
2448 }
2449 if (par_times) {
2450 *parent_times = par_times;
2451 } else {
2452 *parent_times = -1;
2453 }
2454 }
2455
2456 if (**path != '/') {
2457 return LY_EINVAL;
2458 }
2459 /* skip '/' */
2460 ++(*path);
2461
2462 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002463 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002464
2465 if ((**path == '/' && (*path)[1]) || !**path) {
2466 /* path continues by another token or this is the last token */
2467 return LY_SUCCESS;
2468 } else if ((*path)[0] != '[') {
2469 /* unexpected character */
2470 return LY_EINVAL;
2471 } else {
2472 /* predicate starting with [ */
2473 *has_predicate = 1;
2474 return LY_SUCCESS;
2475 }
2476}
2477
2478/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002479 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2480 *
2481 * The set of features used for target must be a subset of features used for the leafref.
2482 * This is not a perfect, we should compare the truth tables but it could require too much resources
2483 * and RFC 7950 does not require it explicitely, so we simplify that.
2484 *
2485 * @param[in] refnode The leafref node.
2486 * @param[in] target Tha target node of the leafref.
2487 * @return LY_SUCCESS or LY_EVALID;
2488 */
2489static LY_ERR
2490lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2491{
2492 LY_ERR ret = LY_EVALID;
2493 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002494 unsigned int u, v, count;
2495 struct ly_set features = {0};
2496
2497 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002498 if (iter->iffeatures) {
2499 LY_ARRAY_FOR(iter->iffeatures, u) {
2500 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2501 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002502 }
2503 }
2504 }
2505 }
2506
2507 /* we should have, in features set, a superset of features applicable to the target node.
2508 * So when adding features applicable to the target into the features set, we should not be
2509 * able to actually add any new feature, otherwise it is not a subset of features applicable
2510 * to the leafref itself. */
2511 count = features.count;
2512 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002513 if (iter->iffeatures) {
2514 LY_ARRAY_FOR(iter->iffeatures, u) {
2515 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2516 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002517 /* new feature was added (or LY_EMEM) */
2518 goto cleanup;
2519 }
2520 }
2521 }
2522 }
2523 }
2524 ret = LY_SUCCESS;
2525
2526cleanup:
2527 ly_set_erase(&features, NULL);
2528 return ret;
2529}
2530
2531/**
Radek Krejcia3045382018-11-22 14:30:31 +01002532 * @brief Validate the leafref path.
2533 * @param[in] ctx Compile context
2534 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002535 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002536 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2537 */
2538static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002539lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002540{
2541 const struct lysc_node *node = NULL, *parent = NULL;
2542 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002543 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002544 const char *id, *prefix, *name;
2545 size_t prefix_len, name_len;
2546 int parent_times = 0, has_predicate;
2547 unsigned int iter, u;
2548 LY_ERR ret = LY_SUCCESS;
2549
2550 assert(ctx);
2551 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002552 assert(leafref);
2553
Radek Krejci1c0c3442019-07-23 16:08:47 +02002554 ctx->path[0] = '\0';
2555 lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
2556 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002557
Radek Krejcia3045382018-11-22 14:30:31 +01002558 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002559 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002560 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2561 if (!iter) { /* first iteration */
2562 /* precess ".." in relative paths */
2563 if (parent_times > 0) {
2564 /* move from the context node */
2565 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2566 /* path is supposed to be evaluated in data tree, so we have to skip
2567 * all schema nodes that cannot be instantiated in data tree */
2568 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002569 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002570 }
2571 }
2572 }
2573
2574 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002575 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002576 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002577 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002578 }
2579 if (!mod) {
2580 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002581 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2582 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002583 return LY_EVALID;
2584 }
Radek Krejci3d929562019-02-21 11:25:55 +01002585 if (!mod->implemented) {
2586 /* make the module implemented */
2587 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2588 }
Radek Krejcia3045382018-11-22 14:30:31 +01002589
2590 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2591 if (!node) {
2592 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002593 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002594 return LY_EVALID;
2595 }
2596 parent = node;
2597
2598 if (has_predicate) {
2599 /* we have predicate, so the current result must be list */
2600 if (node->nodetype != LYS_LIST) {
2601 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2602 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002603 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002604 return LY_EVALID;
2605 }
2606
Radek Krejcibade82a2018-12-05 10:13:48 +01002607 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2608 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002609 }
2610
2611 ++iter;
2612 }
2613 if (ret) {
2614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002615 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002616 return LY_EVALID;
2617 }
2618
2619 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2620 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2621 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002622 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002623 return LY_EVALID;
2624 }
2625
2626 /* check status */
2627 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2628 return LY_EVALID;
2629 }
2630
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002631 /* check config */
2632 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2633 if (node->flags & LYS_CONFIG_R) {
2634 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2635 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2636 leafref->path);
2637 return LY_EVALID;
2638 }
2639 }
2640
Radek Krejci412ddfa2018-11-23 11:44:11 +01002641 /* store the target's type and check for circular chain of leafrefs */
2642 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2643 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2644 if (type == (struct lysc_type*)leafref) {
2645 /* circular chain detected */
2646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2647 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2648 return LY_EVALID;
2649 }
2650 }
2651
Radek Krejci58d171e2018-11-23 13:50:55 +01002652 /* check if leafref and its target are under common if-features */
2653 if (lys_compile_leafref_features_validate(startnode, node)) {
2654 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2655 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2656 leafref->path);
2657 return LY_EVALID;
2658 }
2659
Radek Krejci327de162019-06-14 12:52:07 +02002660 ctx->path_len = 1;
2661 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002662 return LY_SUCCESS;
2663}
2664
Radek Krejcicdfecd92018-11-26 11:27:32 +01002665static 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 +02002666 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002667/**
2668 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2669 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002670 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2671 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2672 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2673 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002674 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002675 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002676 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002677 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2678 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2679 * @param[out] type Newly created type structure with the filled information about the type.
2680 * @return LY_ERR value.
2681 */
Radek Krejci19a96102018-11-15 13:38:09 +01002682static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002683lys_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 +02002684 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002685 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002686{
2687 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002688 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002689 struct lysc_type_bin *bin;
2690 struct lysc_type_num *num;
2691 struct lysc_type_str *str;
2692 struct lysc_type_bits *bits;
2693 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002694 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002695 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002696 struct lysc_type_union *un, *un_aux;
2697 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002698
2699 switch (basetype) {
2700 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002701 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002702
2703 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002704 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002705 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2706 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002707 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002708 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 +01002709 }
2710 }
2711
2712 if (tpdfname) {
2713 type_p->compiled = *type;
2714 *type = calloc(1, sizeof(struct lysc_type_bin));
2715 }
2716 break;
2717 case LY_TYPE_BITS:
2718 /* RFC 7950 9.7 - bits */
2719 bits = (struct lysc_type_bits*)(*type);
2720 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002721 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002722 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2723 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002724 }
2725
Radek Krejci555cb5b2018-11-16 14:54:33 +01002726 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002727 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002728 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002730 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002732 free(*type);
2733 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002734 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002735 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002736 }
2737
2738 if (tpdfname) {
2739 type_p->compiled = *type;
2740 *type = calloc(1, sizeof(struct lysc_type_bits));
2741 }
2742 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002743 case LY_TYPE_DEC64:
2744 dec = (struct lysc_type_dec*)(*type);
2745
2746 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002747 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002748 if (!type_p->fraction_digits) {
2749 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002750 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002751 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002752 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002753 free(*type);
2754 *type = NULL;
2755 }
2756 return LY_EVALID;
2757 }
2758 } else if (type_p->fraction_digits) {
2759 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002760 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002761 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2762 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002763 tpdfname);
2764 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002765 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2766 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002767 free(*type);
2768 *type = NULL;
2769 }
2770 return LY_EVALID;
2771 }
2772 dec->fraction_digits = type_p->fraction_digits;
2773
2774 /* RFC 7950 9.2.4 - range */
2775 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002776 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2777 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002778 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002779 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 +01002780 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002781 }
2782
2783 if (tpdfname) {
2784 type_p->compiled = *type;
2785 *type = calloc(1, sizeof(struct lysc_type_dec));
2786 }
2787 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002788 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002789 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002790
2791 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002792 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002793 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2794 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002795 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002796 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 +01002797 }
2798 } else if (base && ((struct lysc_type_str*)base)->length) {
2799 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2800 }
2801
2802 /* RFC 7950 9.4.5 - pattern */
2803 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002804 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002805 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002806 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2807 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2808 }
2809
2810 if (tpdfname) {
2811 type_p->compiled = *type;
2812 *type = calloc(1, sizeof(struct lysc_type_str));
2813 }
2814 break;
2815 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002816 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002817
2818 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002819 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002820 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002821 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002822 }
2823
Radek Krejci555cb5b2018-11-16 14:54:33 +01002824 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002825 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002826 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002827 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002828 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002829 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002830 free(*type);
2831 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002832 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002833 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002834 }
2835
2836 if (tpdfname) {
2837 type_p->compiled = *type;
2838 *type = calloc(1, sizeof(struct lysc_type_enum));
2839 }
2840 break;
2841 case LY_TYPE_INT8:
2842 case LY_TYPE_UINT8:
2843 case LY_TYPE_INT16:
2844 case LY_TYPE_UINT16:
2845 case LY_TYPE_INT32:
2846 case LY_TYPE_UINT32:
2847 case LY_TYPE_INT64:
2848 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002849 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002850
2851 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002852 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002853 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2854 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002855 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002856 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 +01002857 }
2858 }
2859
2860 if (tpdfname) {
2861 type_p->compiled = *type;
2862 *type = calloc(1, sizeof(struct lysc_type_num));
2863 }
2864 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002865 case LY_TYPE_IDENT:
2866 idref = (struct lysc_type_identityref*)(*type);
2867
2868 /* RFC 7950 9.10.2 - base */
2869 if (type_p->bases) {
2870 if (base) {
2871 /* only the directly derived identityrefs can contain base specification */
2872 if (tpdfname) {
2873 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002874 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002875 tpdfname);
2876 } else {
2877 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002878 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002879 free(*type);
2880 *type = NULL;
2881 }
2882 return LY_EVALID;
2883 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002884 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002885 }
2886
2887 if (!base && !type_p->flags) {
2888 /* type derived from identityref built-in type must contain at least one base */
2889 if (tpdfname) {
2890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2891 } else {
2892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2893 free(*type);
2894 *type = NULL;
2895 }
2896 return LY_EVALID;
2897 }
2898
2899 if (tpdfname) {
2900 type_p->compiled = *type;
2901 *type = calloc(1, sizeof(struct lysc_type_identityref));
2902 }
2903 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002904 case LY_TYPE_LEAFREF:
2905 /* RFC 7950 9.9.3 - require-instance */
2906 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002907 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002908 if (tpdfname) {
2909 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2910 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2911 } else {
2912 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2913 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2914 free(*type);
2915 *type = NULL;
2916 }
2917 return LY_EVALID;
2918 }
Radek Krejcia3045382018-11-22 14:30:31 +01002919 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002920 } else if (base) {
2921 /* inherit */
2922 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002923 } else {
2924 /* default is true */
2925 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2926 }
2927 if (type_p->path) {
2928 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002929 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002930 } else if (base) {
2931 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002932 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002933 } else if (tpdfname) {
2934 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2935 return LY_EVALID;
2936 } else {
2937 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2938 free(*type);
2939 *type = NULL;
2940 return LY_EVALID;
2941 }
2942 if (tpdfname) {
2943 type_p->compiled = *type;
2944 *type = calloc(1, sizeof(struct lysc_type_leafref));
2945 }
2946 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002947 case LY_TYPE_INST:
2948 /* RFC 7950 9.9.3 - require-instance */
2949 if (type_p->flags & LYS_SET_REQINST) {
2950 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2951 } else {
2952 /* default is true */
2953 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2954 }
2955
2956 if (tpdfname) {
2957 type_p->compiled = *type;
2958 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2959 }
2960 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002961 case LY_TYPE_UNION:
2962 un = (struct lysc_type_union*)(*type);
2963
2964 /* RFC 7950 7.4 - type */
2965 if (type_p->types) {
2966 if (base) {
2967 /* only the directly derived union can contain types specification */
2968 if (tpdfname) {
2969 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2970 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2971 tpdfname);
2972 } else {
2973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2974 "Invalid type substatement for the type not directly derived from union built-in type.");
2975 free(*type);
2976 *type = NULL;
2977 }
2978 return LY_EVALID;
2979 }
2980 /* compile the type */
2981 additional = 0;
2982 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2983 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002984 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 +01002985 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2986 /* add space for additional types from the union subtype */
2987 un_aux = (struct lysc_type_union *)un->types[u + additional];
2988 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)));
2989 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2990 un->types = (void*)((uint32_t*)(p) + 1);
2991
2992 /* copy subtypes of the subtype union */
2993 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2994 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2995 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2996 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2997 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2998 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2999 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
3000 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3001 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3002 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3003 /* TODO extensions */
3004
3005 } else {
3006 un->types[u + additional] = un_aux->types[v];
3007 ++un_aux->types[v]->refcount;
3008 }
3009 ++additional;
3010 LY_ARRAY_INCREMENT(un->types);
3011 }
3012 /* compensate u increment in main loop */
3013 --additional;
3014
3015 /* free the replaced union subtype */
3016 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3017 } else {
3018 LY_ARRAY_INCREMENT(un->types);
3019 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003020 }
3021 }
3022
3023 if (!base && !type_p->flags) {
3024 /* type derived from union built-in type must contain at least one type */
3025 if (tpdfname) {
3026 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3027 } else {
3028 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3029 free(*type);
3030 *type = NULL;
3031 }
3032 return LY_EVALID;
3033 }
3034
3035 if (tpdfname) {
3036 type_p->compiled = *type;
3037 *type = calloc(1, sizeof(struct lysc_type_union));
3038 }
3039 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003040 case LY_TYPE_BOOL:
3041 case LY_TYPE_EMPTY:
3042 case LY_TYPE_UNKNOWN: /* just to complete switch */
3043 break;
3044 }
3045 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3046done:
3047 return ret;
3048}
3049
Radek Krejcia3045382018-11-22 14:30:31 +01003050/**
3051 * @brief Compile information about the leaf/leaf-list's type.
3052 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003053 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3054 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3055 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3056 * @param[in] context_name Name of the context node or referencing typedef for logging.
3057 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003058 * @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 +01003059 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003060 * @return LY_ERR value.
3061 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003062static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003063lys_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 +02003064 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003065{
3066 LY_ERR ret = LY_SUCCESS;
3067 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003068 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003069 struct type_context {
3070 const struct lysp_tpdf *tpdf;
3071 struct lysp_node *node;
3072 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003073 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003074 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003075 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003076 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003077 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003078 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003079
3080 (*type) = NULL;
3081
3082 tctx = calloc(1, sizeof *tctx);
3083 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003084 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003085 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3086 ret == LY_SUCCESS;
3087 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3088 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3089 if (basetype) {
3090 break;
3091 }
3092
3093 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003094 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3095 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003096 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3097
Radek Krejcicdfecd92018-11-26 11:27:32 +01003098 if (units && !*units) {
3099 /* inherit units */
3100 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3101 }
Radek Krejci01342af2019-01-03 15:18:08 +01003102 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003103 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003104 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003105 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003106 }
Radek Krejci01342af2019-01-03 15:18:08 +01003107 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003108 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3109 break;
3110 }
3111
Radek Krejci19a96102018-11-15 13:38:09 +01003112 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003113 /* it is not necessary to continue, the rest of the chain was already compiled,
3114 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003115 basetype = tctx->tpdf->type.compiled->basetype;
3116 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003117 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003118 dummyloops = 1;
3119 goto preparenext;
3120 } else {
3121 tctx = NULL;
3122 break;
3123 }
Radek Krejci19a96102018-11-15 13:38:09 +01003124 }
3125
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003126 /* circular typedef reference detection */
3127 for (u = 0; u < tpdf_chain.count; u++) {
3128 /* local part */
3129 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3130 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3131 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3132 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3133 free(tctx);
3134 ret = LY_EVALID;
3135 goto cleanup;
3136 }
3137 }
3138 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3139 /* global part for unions corner case */
3140 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3141 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3142 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3143 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3144 free(tctx);
3145 ret = LY_EVALID;
3146 goto cleanup;
3147 }
3148 }
3149
Radek Krejci19a96102018-11-15 13:38:09 +01003150 /* store information for the following processing */
3151 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3152
Radek Krejcicdfecd92018-11-26 11:27:32 +01003153preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003154 /* prepare next loop */
3155 tctx_prev = tctx;
3156 tctx = calloc(1, sizeof *tctx);
3157 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3158 }
3159 free(tctx);
3160
3161 /* allocate type according to the basetype */
3162 switch (basetype) {
3163 case LY_TYPE_BINARY:
3164 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003165 break;
3166 case LY_TYPE_BITS:
3167 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003168 break;
3169 case LY_TYPE_BOOL:
3170 case LY_TYPE_EMPTY:
3171 *type = calloc(1, sizeof(struct lysc_type));
3172 break;
3173 case LY_TYPE_DEC64:
3174 *type = calloc(1, sizeof(struct lysc_type_dec));
3175 break;
3176 case LY_TYPE_ENUM:
3177 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003178 break;
3179 case LY_TYPE_IDENT:
3180 *type = calloc(1, sizeof(struct lysc_type_identityref));
3181 break;
3182 case LY_TYPE_INST:
3183 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3184 break;
3185 case LY_TYPE_LEAFREF:
3186 *type = calloc(1, sizeof(struct lysc_type_leafref));
3187 break;
3188 case LY_TYPE_STRING:
3189 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003190 break;
3191 case LY_TYPE_UNION:
3192 *type = calloc(1, sizeof(struct lysc_type_union));
3193 break;
3194 case LY_TYPE_INT8:
3195 case LY_TYPE_UINT8:
3196 case LY_TYPE_INT16:
3197 case LY_TYPE_UINT16:
3198 case LY_TYPE_INT32:
3199 case LY_TYPE_UINT32:
3200 case LY_TYPE_INT64:
3201 case LY_TYPE_UINT64:
3202 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003203 break;
3204 case LY_TYPE_UNKNOWN:
3205 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3206 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3207 ret = LY_EVALID;
3208 goto cleanup;
3209 }
3210 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003211 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003212 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3213 ly_data_type2str[basetype]);
3214 free(*type);
3215 (*type) = NULL;
3216 ret = LY_EVALID;
3217 goto cleanup;
3218 }
3219
3220 /* get restrictions from the referred typedefs */
3221 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3222 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003223
3224 /* remember the typedef context for circular check */
3225 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3226
Radek Krejci43699232018-11-23 14:59:46 +01003227 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003228 base = tctx->tpdf->type.compiled;
3229 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003230 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003231 /* no change, just use the type information from the base */
3232 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3233 ++base->refcount;
3234 continue;
3235 }
3236
3237 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003238 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3239 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3240 tctx->tpdf->name, ly_data_type2str[basetype]);
3241 ret = LY_EVALID;
3242 goto cleanup;
3243 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3245 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3246 tctx->tpdf->name, tctx->tpdf->dflt);
3247 ret = LY_EVALID;
3248 goto cleanup;
3249 }
3250
Radek Krejci19a96102018-11-15 13:38:09 +01003251 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003252 /* TODO user type plugins */
3253 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003254 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003255 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 +01003256 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003257 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003258 LY_CHECK_GOTO(ret, cleanup);
3259 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003260 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003261 /* remove the processed typedef contexts from the stack for circular check */
3262 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003263
Radek Krejcic5c27e52018-11-15 14:38:11 +01003264 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003265 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003266 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003267 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003268 /* TODO user type plugins */
3269 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003270 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003271 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 +01003272 LY_CHECK_GOTO(ret, cleanup);
3273 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003274 /* no specific restriction in leaf's type definition, copy from the base */
3275 free(*type);
3276 (*type) = base;
3277 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003278 }
Radek Krejcia1911222019-07-22 17:24:50 +02003279 if (dflt && !(*type)->dflt) {
3280 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003281 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003282 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3283 (*type)->dflt->realtype = (*type);
3284 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3285 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003286 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003287 if (err) {
3288 ly_err_print(err);
3289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3290 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3291 ly_err_free(err);
3292 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003293 if (ret == LY_EINCOMPLETE) {
3294 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003295 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003296
3297 /* but in general result is so far ok */
3298 ret = LY_SUCCESS;
3299 }
Radek Krejcia1911222019-07-22 17:24:50 +02003300 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003301 }
Radek Krejci19a96102018-11-15 13:38:09 +01003302
Radek Krejci0935f412019-08-20 16:15:18 +02003303 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003304
3305cleanup:
3306 ly_set_erase(&tpdf_chain, free);
3307 return ret;
3308}
3309
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003310/**
3311 * @brief Compile status information of the given node.
3312 *
3313 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3314 * has the status correctly set during the compilation.
3315 *
3316 * @param[in] ctx Compile context
3317 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3318 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3319 * the compatibility with the parent's status value.
3320 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3321 * @return LY_ERR value.
3322 */
3323static LY_ERR
3324lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3325{
3326 /* status - it is not inherited by specification, but it does not make sense to have
3327 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3328 if (!((*node_flags) & LYS_STATUS_MASK)) {
3329 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3330 if ((parent_flags & 0x3) != 0x3) {
3331 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3332 * combination of bits (0x3) which marks the uses_status value */
3333 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3334 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3335 }
3336 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3337 } else {
3338 (*node_flags) |= LYS_STATUS_CURR;
3339 }
3340 } else if (parent_flags & LYS_STATUS_MASK) {
3341 /* check status compatibility with the parent */
3342 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3343 if ((*node_flags) & LYS_STATUS_CURR) {
3344 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3345 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3346 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3347 } else { /* LYS_STATUS_DEPRC */
3348 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3349 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3350 }
3351 return LY_EVALID;
3352 }
3353 }
3354 return LY_SUCCESS;
3355}
3356
Radek Krejci8cce8532019-03-05 11:27:45 +01003357/**
3358 * @brief Check uniqness of the node/action/notification name.
3359 *
3360 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3361 * structures, but they share the namespace so we need to check their name collisions.
3362 *
3363 * @param[in] ctx Compile context.
3364 * @param[in] children List (linked list) of data nodes to go through.
3365 * @param[in] actions List (sized array) of actions or RPCs to go through.
3366 * @param[in] notifs List (sized array) of Notifications to go through.
3367 * @param[in] name Name of the item to find in the given lists.
3368 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3369 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3370 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3371 */
3372static LY_ERR
3373lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3374 const struct lysc_action *actions, const struct lysc_notif *notifs,
3375 const char *name, void *exclude)
3376{
3377 const struct lysc_node *iter;
3378 unsigned int u;
3379
3380 LY_LIST_FOR(children, iter) {
3381 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3382 goto error;
3383 }
3384 }
3385 LY_ARRAY_FOR(actions, u) {
3386 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3387 goto error;
3388 }
3389 }
3390 LY_ARRAY_FOR(notifs, u) {
3391 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3392 goto error;
3393 }
3394 }
3395 return LY_SUCCESS;
3396error:
3397 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3398 return LY_EEXIST;
3399}
3400
Radek Krejciec4da802019-05-02 13:02:41 +02003401static 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 +01003402
Radek Krejcia3045382018-11-22 14:30:31 +01003403/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003404 * @brief Compile parsed RPC/action schema node information.
3405 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003406 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003407 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003408 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3409 * @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).
3410 * Zero means no uses, non-zero value with no status bit set mean the default status.
3411 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3412 */
3413static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003414lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003415 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3416{
3417 LY_ERR ret = LY_SUCCESS;
3418 struct lysp_node *child_p;
3419 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003420 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003421
Radek Krejci327de162019-06-14 12:52:07 +02003422 lysc_update_path(ctx, parent, action_p->name);
3423
Radek Krejci8cce8532019-03-05 11:27:45 +01003424 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3425 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3426 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3427 action_p->name, action)) {
3428 return LY_EVALID;
3429 }
3430
Radek Krejciec4da802019-05-02 13:02:41 +02003431 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003433 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003434 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003435 return LY_EVALID;
3436 }
3437
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003438 action->nodetype = LYS_ACTION;
3439 action->module = ctx->mod;
3440 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003441 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003442 action->sp = action_p;
3443 }
3444 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3445
3446 /* status - it is not inherited by specification, but it does not make sense to have
3447 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3448 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3449
3450 DUP_STRING(ctx->ctx, action_p->name, action->name);
3451 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3452 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003453 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003454 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003455
3456 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003457 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejciec4da802019-05-02 13:02:41 +02003458 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003459 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 +02003460 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003461 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003462 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003463 }
Radek Krejci327de162019-06-14 12:52:07 +02003464 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003465 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003466
3467 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003468 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejciec4da802019-05-02 13:02:41 +02003469 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003470 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 +02003471 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003472 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003473 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003474 }
Radek Krejci327de162019-06-14 12:52:07 +02003475 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003476
Radek Krejci327de162019-06-14 12:52:07 +02003477 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003478cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003479 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003480 return ret;
3481}
3482
3483/**
Radek Krejci43981a32019-04-12 09:44:11 +02003484 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003485 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003486 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003487 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3488 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3489 * @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 +02003490 * Zero means no uses, non-zero value with no status bit set mean the default status.
3491 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3492 */
3493static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003494lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003495 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3496{
3497 LY_ERR ret = LY_SUCCESS;
3498 struct lysp_node *child_p;
3499 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003500 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003501
Radek Krejci327de162019-06-14 12:52:07 +02003502 lysc_update_path(ctx, parent, notif_p->name);
3503
Radek Krejcifc11bd72019-04-11 16:00:05 +02003504 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3505 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3506 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3507 notif_p->name, notif)) {
3508 return LY_EVALID;
3509 }
3510
Radek Krejciec4da802019-05-02 13:02:41 +02003511 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3513 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003514 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003515 return LY_EVALID;
3516 }
3517
3518 notif->nodetype = LYS_NOTIF;
3519 notif->module = ctx->mod;
3520 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003521 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003522 notif->sp = notif_p;
3523 }
3524 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3525
3526 /* status - it is not inherited by specification, but it does not make sense to have
3527 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3528 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3529
3530 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3531 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3532 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003533 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003534 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003535 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003536
Radek Krejciec4da802019-05-02 13:02:41 +02003537 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003538 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003539 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003540 }
3541
Radek Krejci327de162019-06-14 12:52:07 +02003542 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003543cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003544 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003545 return ret;
3546}
3547
3548/**
Radek Krejcia3045382018-11-22 14:30:31 +01003549 * @brief Compile parsed container node information.
3550 * @param[in] ctx Compile context
3551 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003552 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3553 * is enriched with the container-specific information.
3554 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3555 */
Radek Krejci19a96102018-11-15 13:38:09 +01003556static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003557lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003558{
3559 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3560 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3561 struct lysp_node *child_p;
3562 unsigned int u;
3563 LY_ERR ret = LY_SUCCESS;
3564
Radek Krejcife909632019-02-12 15:34:42 +01003565 if (cont_p->presence) {
3566 cont->flags |= LYS_PRESENCE;
3567 }
3568
Radek Krejci19a96102018-11-15 13:38:09 +01003569 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003570 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003571 }
3572
Radek Krejciec4da802019-05-02 13:02:41 +02003573 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3574 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3575 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003576
3577done:
3578 return ret;
3579}
3580
Radek Krejci33f72892019-02-21 10:36:58 +01003581/*
3582 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3583 * @param[in] ctx Compile context.
3584 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3585 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003586 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3587 * @return LY_ERR value.
3588 */
3589static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003590lys_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 +01003591{
Radek Krejcia1911222019-07-22 17:24:50 +02003592 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003593 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3594
Radek Krejciec4da802019-05-02 13:02:41 +02003595 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 +01003596 leaf->units ? NULL : &leaf->units));
3597 if (leaf->nodetype == LYS_LEAFLIST) {
3598 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003599 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3600 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003601 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003602 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3603 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3604 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3605 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003606 LY_ARRAY_INCREMENT(llist->dflts);
3607 }
3608 } else {
3609 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003610 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003611 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3612 leaf->dflt->realtype = leaf->type->dflt->realtype;
3613 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3614 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003615 }
3616 }
3617 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3618 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3619 ly_set_add(&ctx->unres, leaf, 0);
3620 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3621 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3622 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3623 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3624 ly_set_add(&ctx->unres, leaf, 0);
3625 }
3626 }
3627 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003628 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3629 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3630 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3631 return LY_EVALID;
3632 }
3633 }
3634
Radek Krejci33f72892019-02-21 10:36:58 +01003635 return LY_SUCCESS;
3636}
3637
Radek Krejcia3045382018-11-22 14:30:31 +01003638/**
3639 * @brief Compile parsed leaf node information.
3640 * @param[in] ctx Compile context
3641 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003642 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3643 * is enriched with the leaf-specific information.
3644 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3645 */
Radek Krejci19a96102018-11-15 13:38:09 +01003646static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003647lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003648{
3649 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3650 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3651 unsigned int u;
3652 LY_ERR ret = LY_SUCCESS;
3653
Radek Krejciec4da802019-05-02 13:02:41 +02003654 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003655 if (leaf_p->units) {
3656 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3657 leaf->flags |= LYS_SET_UNITS;
3658 }
Radek Krejcia1911222019-07-22 17:24:50 +02003659
3660 /* the dflt member is just filled to avoid getting the default value from the type */
3661 leaf->dflt = (void*)leaf_p->dflt;
3662 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3663
Radek Krejciccd20f12019-02-15 14:12:27 +01003664 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003665 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003666 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003667 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3668 leaf->dflt->realtype = leaf->type;
3669 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3670 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003671 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003672 leaf->dflt->realtype->refcount++;
3673 if (err) {
3674 ly_err_print(err);
3675 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3676 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3677 ly_err_free(err);
3678 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003679 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003680 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003681 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003682
3683 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003684 ret = LY_SUCCESS;
3685 }
Radek Krejcia1911222019-07-22 17:24:50 +02003686 LY_CHECK_GOTO(ret, done);
Radek Krejci76b3e962018-12-14 17:01:25 +01003687 leaf->flags |= LYS_SET_DFLT;
3688 }
Radek Krejci43699232018-11-23 14:59:46 +01003689
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003690
Radek Krejci19a96102018-11-15 13:38:09 +01003691done:
3692 return ret;
3693}
3694
Radek Krejcia3045382018-11-22 14:30:31 +01003695/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003696 * @brief Compile parsed leaf-list node information.
3697 * @param[in] ctx Compile context
3698 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003699 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3700 * is enriched with the leaf-list-specific information.
3701 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3702 */
3703static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003704lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003705{
3706 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3707 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003708 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003709 LY_ERR ret = LY_SUCCESS;
3710
Radek Krejciec4da802019-05-02 13:02:41 +02003711 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003712 if (llist_p->units) {
3713 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3714 llist->flags |= LYS_SET_UNITS;
3715 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003716
Radek Krejcia1911222019-07-22 17:24:50 +02003717 /* the dflts member is just filled to avoid getting the default value from the type */
3718 llist->dflts = (void*)llist_p->dflts;
3719 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003720 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003721 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003722 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003723 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3724 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003725 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003726 LY_ARRAY_INCREMENT(llist->dflts_mods);
3727 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003728 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003729 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3730 llist->dflts[u]->realtype = llist->type;
3731 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3732 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003733 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003734 llist->dflts[u]->realtype->refcount++;
3735 if (err) {
3736 ly_err_print(err);
3737 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3738 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3739 ly_err_free(err);
3740 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003741 if (ret == LY_EINCOMPLETE) {
3742 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003743 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003744
3745 /* but in general result is so far ok */
3746 ret = LY_SUCCESS;
3747 }
Radek Krejcia1911222019-07-22 17:24:50 +02003748 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003749 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003750 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003751 }
Radek Krejcia1911222019-07-22 17:24:50 +02003752 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3753 /* configuration data values must be unique - so check the default values */
3754 LY_ARRAY_FOR(llist->dflts, u) {
3755 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3756 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3757 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003758 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 +02003759 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3760 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3761 if (dynamic) {
3762 free((char*)val);
3763 }
3764 return LY_EVALID;
3765 }
3766 }
3767 }
3768 }
3769
3770 /* 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 +01003771
3772 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003773 if (llist->min) {
3774 llist->flags |= LYS_MAND_TRUE;
3775 }
Radek Krejcib7408632018-11-28 17:12:11 +01003776 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003777
Radek Krejci0e5d8382018-11-28 16:37:53 +01003778done:
3779 return ret;
3780}
3781
3782/**
Radek Krejci7af64242019-02-18 13:07:53 +01003783 * @brief Compile information about list's uniques.
3784 * @param[in] ctx Compile context.
3785 * @param[in] context_module Module where the prefixes are going to be resolved.
3786 * @param[in] uniques Sized array list of unique statements.
3787 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3788 * @return LY_ERR value.
3789 */
3790static LY_ERR
3791lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3792{
3793 LY_ERR ret = LY_SUCCESS;
3794 struct lysc_node_leaf **key, ***unique;
3795 const char *keystr, *delim;
3796 size_t len;
3797 unsigned int v;
3798 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003799 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003800
3801 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3802 config = -1;
3803 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3804 keystr = uniques[v];
3805 while (keystr) {
3806 delim = strpbrk(keystr, " \t\n");
3807 if (delim) {
3808 len = delim - keystr;
3809 while (isspace(*delim)) {
3810 ++delim;
3811 }
3812 } else {
3813 len = strlen(keystr);
3814 }
3815
3816 /* unique node must be present */
3817 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003818 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3819 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003820 if (ret != LY_SUCCESS) {
3821 if (ret == LY_EDENIED) {
3822 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003823 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003824 len, keystr, lys_nodetype2str((*key)->nodetype));
3825 }
3826 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003827 } else if (flags) {
3828 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3829 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3830 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3831 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003832 }
3833
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003834
Radek Krejci7af64242019-02-18 13:07:53 +01003835 /* all referenced leafs must be of the same config type */
3836 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3837 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3838 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3839 return LY_EVALID;
3840 } else if ((*key)->flags & LYS_CONFIG_W) {
3841 config = 1;
3842 } else { /* LYS_CONFIG_R */
3843 config = 0;
3844 }
3845
3846 /* check status */
3847 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3848 (*key)->flags, (*key)->module, (*key)->name));
3849
3850 /* mark leaf as unique */
3851 (*key)->flags |= LYS_UNIQUE;
3852
3853 /* next unique value in line */
3854 keystr = delim;
3855 }
3856 /* next unique definition */
3857 }
3858
3859 return LY_SUCCESS;
3860}
3861
3862/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003863 * @brief Compile parsed list node information.
3864 * @param[in] ctx Compile context
3865 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003866 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3867 * is enriched with the list-specific information.
3868 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3869 */
3870static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003871lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003872{
3873 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3874 struct lysc_node_list *list = (struct lysc_node_list*)node;
3875 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003876 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003877 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003878 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003879 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003880 LY_ERR ret = LY_SUCCESS;
3881
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003882 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003883 if (list->min) {
3884 list->flags |= LYS_MAND_TRUE;
3885 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003886 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3887
3888 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003889 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003890 }
3891
Radek Krejciec4da802019-05-02 13:02:41 +02003892 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003893
3894 /* keys */
3895 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3896 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3897 return LY_EVALID;
3898 }
3899
3900 /* find all the keys (must be direct children) */
3901 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003902 if (!keystr) {
3903 /* keyless list */
3904 list->flags |= LYS_KEYLESS;
3905 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003906 while (keystr) {
3907 delim = strpbrk(keystr, " \t\n");
3908 if (delim) {
3909 len = delim - keystr;
3910 while (isspace(*delim)) {
3911 ++delim;
3912 }
3913 } else {
3914 len = strlen(keystr);
3915 }
3916
3917 /* key node must be present */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003918 key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3919 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003920 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3921 "The list's key \"%.*s\" not found.", len, keystr);
3922 return LY_EVALID;
3923 }
3924 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003925 if (key->flags & LYS_KEY) {
3926 /* the node was already marked as a key */
3927 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3928 "Duplicated key identifier \"%.*s\".", len, keystr);
3929 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003930 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003931
3932 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003933 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003934 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003935 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3936 return LY_EVALID;
3937 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003938 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003939 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003940 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003942 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003943 return LY_EVALID;
3944 }
3945 } else {
3946 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003947 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003949 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003950 return LY_EVALID;
3951 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003952 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003953 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003954 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003955 return LY_EVALID;
3956 }
3957 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003958
3959 /* check status */
3960 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003961 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003962
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003963 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003964 if (key->dflt) {
3965 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3966 lysc_type_free(ctx->ctx, key->dflt->realtype);
3967 free(key->dflt);
3968 key->dflt = NULL;
3969 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003970 }
3971 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003972 key->flags |= LYS_KEY;
3973
3974 /* move it to the correct position */
3975 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3976 /* fix links in closest previous siblings of the key */
3977 if (key->next) {
3978 key->next->prev = key->prev;
3979 } else {
3980 /* last child */
3981 list->child->prev = key->prev;
3982 }
3983 if (key->prev->next) {
3984 key->prev->next = key->next;
3985 }
3986 /* fix links in the key */
3987 if (prev_key) {
3988 key->prev = (struct lysc_node*)prev_key;
3989 key->next = prev_key->next;
3990 } else {
3991 key->prev = list->child->prev;
3992 key->next = list->child;
3993 }
3994 /* fix links in closes future siblings of the key */
3995 if (prev_key) {
3996 if (prev_key->next) {
3997 prev_key->next->prev = (struct lysc_node*)key;
3998 } else {
3999 list->child->prev = (struct lysc_node*)key;
4000 }
4001 prev_key->next = (struct lysc_node*)key;
4002 } else {
4003 list->child->prev = (struct lysc_node*)key;
4004 }
4005 /* fix links in parent */
4006 if (!key->prev->next) {
4007 list->child = (struct lysc_node*)key;
4008 }
4009 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004010
4011 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004012 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004013 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004014 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004015 }
4016
4017 /* uniques */
4018 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004019 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004020 }
4021
Radek Krejciec4da802019-05-02 13:02:41 +02004022 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4023 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004024
4025done:
4026 return ret;
4027}
4028
Radek Krejcib56c7502019-02-13 14:19:54 +01004029/**
4030 * @brief Do some checks and set the default choice's case.
4031 *
4032 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4033 *
4034 * @param[in] ctx Compile context.
4035 * @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,
4036 * not the reference to the imported module.
4037 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4038 * @return LY_ERR value.
4039 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004040static LY_ERR
4041lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4042{
4043 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4044 const char *prefix = NULL, *name;
4045 size_t prefix_len = 0;
4046
4047 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4048 name = strchr(dflt, ':');
4049 if (name) {
4050 prefix = dflt;
4051 prefix_len = name - prefix;
4052 ++name;
4053 } else {
4054 name = dflt;
4055 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004056 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004057 /* prefixed default case make sense only for the prefix of the schema itself */
4058 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4059 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4060 prefix_len, prefix);
4061 return LY_EVALID;
4062 }
4063 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4064 if (!ch->dflt) {
4065 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4066 "Default case \"%s\" not found.", dflt);
4067 return LY_EVALID;
4068 }
4069 /* no mandatory nodes directly under the default case */
4070 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004071 if (iter->parent != (struct lysc_node*)ch->dflt) {
4072 break;
4073 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004074 if (iter->flags & LYS_MAND_TRUE) {
4075 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4076 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4077 return LY_EVALID;
4078 }
4079 }
Radek Krejci01342af2019-01-03 15:18:08 +01004080 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004081 return LY_SUCCESS;
4082}
4083
Radek Krejciccd20f12019-02-15 14:12:27 +01004084static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004085lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004086{
4087 struct lys_module *mod;
4088 const char *prefix = NULL, *name;
4089 size_t prefix_len = 0;
4090 struct lysc_node_case *cs;
4091 struct lysc_node *node;
4092
4093 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4094 name = strchr(dflt, ':');
4095 if (name) {
4096 prefix = dflt;
4097 prefix_len = name - prefix;
4098 ++name;
4099 } else {
4100 name = dflt;
4101 }
4102 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4103 if (prefix) {
4104 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004106 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4107 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004108 return LY_EVALID;
4109 }
4110 } else {
4111 mod = ctx->mod;
4112 }
4113 /* get the default case */
4114 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4115 if (!cs) {
4116 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004117 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004118 return LY_EVALID;
4119 }
4120
4121 /* check that there is no mandatory node */
4122 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004123 if (node->parent != (struct lysc_node*)cs) {
4124 break;
4125 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004126 if (node->flags & LYS_MAND_TRUE) {
4127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004128 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4129 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004130 return LY_EVALID;
4131 }
4132 }
4133
4134 /* set the default case in choice */
4135 ch->dflt = cs;
4136 cs->flags |= LYS_SET_DFLT;
4137
4138 return LY_SUCCESS;
4139}
4140
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004141/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004142 * @brief Compile parsed choice node information.
4143 * @param[in] ctx Compile context
4144 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004145 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004146 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004147 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4148 */
4149static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004150lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004151{
4152 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4153 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4154 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004155 LY_ERR ret = LY_SUCCESS;
4156
Radek Krejci056d0a82018-12-06 16:57:25 +01004157 LY_LIST_FOR(ch_p->child, child_p) {
4158 if (child_p->nodetype == LYS_CASE) {
4159 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004160 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004161 }
4162 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004163 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004164 }
4165 }
4166
4167 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004168 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004169 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004170 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004171
Radek Krejci9800fb82018-12-13 14:26:23 +01004172 return ret;
4173}
4174
4175/**
4176 * @brief Compile parsed anydata or anyxml node information.
4177 * @param[in] ctx Compile context
4178 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004179 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4180 * is enriched with the any-specific information.
4181 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4182 */
4183static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004184lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004185{
4186 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4187 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4188 unsigned int u;
4189 LY_ERR ret = LY_SUCCESS;
4190
Radek Krejciec4da802019-05-02 13:02:41 +02004191 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004192
4193 if (any->flags & LYS_CONFIG_W) {
4194 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004195 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004196 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004197done:
4198 return ret;
4199}
4200
Radek Krejcib56c7502019-02-13 14:19:54 +01004201/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004202 * @brief Connect the node into the siblings list and check its name uniqueness.
4203 *
4204 * @param[in] ctx Compile context
4205 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4206 * the choice itself is expected instead of a specific case node.
4207 * @param[in] node Schema node to connect into the list.
4208 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4209 */
4210static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004211lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004212{
4213 struct lysc_node **children;
4214
4215 if (node->nodetype == LYS_CASE) {
4216 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4217 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004218 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004219 }
4220 if (children) {
4221 if (!(*children)) {
4222 /* first child */
4223 *children = node;
4224 } else if (*children != node) {
4225 /* by the condition in previous branch we cover the choice/case children
4226 * - the children list is shared by the choice and the the first case, in addition
4227 * the first child of each case must be referenced from the case node. So the node is
4228 * actually always already inserted in case it is the first children - so here such
4229 * a situation actually corresponds to the first branch */
4230 /* insert at the end of the parent's children list */
4231 (*children)->prev->next = node;
4232 node->prev = (*children)->prev;
4233 (*children)->prev = node;
4234
4235 /* check the name uniqueness */
4236 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4237 lysc_node_notifs(parent), node->name, node)) {
4238 return LY_EEXIST;
4239 }
4240 }
4241 }
4242 return LY_SUCCESS;
4243}
4244
Radek Krejci95710c92019-02-11 15:49:55 +01004245/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004246 * @brief Get the XPath context node for the given schema node.
4247 * @param[in] start The schema node where the XPath expression appears.
4248 * @return The context node to evaluate XPath expression in given schema node.
4249 * @return NULL in case the context node is the root node.
4250 */
4251static struct lysc_node *
4252lysc_xpath_context(struct lysc_node *start)
4253{
4254 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
4255 start = start->parent);
4256 return start;
4257}
4258
4259/**
4260 * @brief Prepare the case structure in choice node for the new data node.
4261 *
4262 * 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
4263 * created in the choice when the first child was processed.
4264 *
4265 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004266 * @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,
4267 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004268 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4269 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4270 * @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,
4271 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004272 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004273static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004274lys_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 +01004275{
4276 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004277 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004278 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004279 unsigned int u;
4280 LY_ERR ret;
4281
Radek Krejci95710c92019-02-11 15:49:55 +01004282#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004283 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004284 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4286 return NULL; \
4287 } \
4288 }
4289
Radek Krejci95710c92019-02-11 15:49:55 +01004290 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4291 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004292
4293 /* we have to add an implicit case node into the parent choice */
4294 cs = calloc(1, sizeof(struct lysc_node_case));
4295 DUP_STRING(ctx->ctx, child->name, cs->name);
4296 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004297 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004298 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4299 /* the case is already present since the child is not its first children */
4300 return (struct lysc_node_case*)ch->cases->prev;
4301 }
Radek Krejci95710c92019-02-11 15:49:55 +01004302 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004303
4304 /* explicit parent case is not present (this is its first child) */
4305 cs = calloc(1, sizeof(struct lysc_node_case));
4306 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4307 cs->flags = LYS_STATUS_MASK & node_p->flags;
4308 cs->sp = node_p;
4309
Radek Krejcib1b59152019-01-07 13:21:56 +01004310 /* 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 +02004311 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004312
4313 if (node_p->when) {
4314 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004315 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004316 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004317 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004318 }
Radek Krejciec4da802019-05-02 13:02:41 +02004319 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004320 } else {
4321 LOGINT(ctx->ctx);
4322 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004323 }
4324 cs->module = ctx->mod;
4325 cs->prev = (struct lysc_node*)cs;
4326 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004327 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004328 cs->parent = (struct lysc_node*)ch;
4329 cs->child = child;
4330
4331 return cs;
4332error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004333 if (cs) {
4334 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4335 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004336 return NULL;
4337
4338#undef UNIQUE_CHECK
4339}
4340
Radek Krejcib56c7502019-02-13 14:19:54 +01004341/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004342 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004343 *
4344 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004345 * @param[in] node Target node where the config is supposed to be changed.
4346 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004347 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4348 * 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 +01004349 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4350 * @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 +01004351 * @return LY_ERR value.
4352 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004353static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004354lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004355 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004356{
4357 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004358 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004359
4360 if (config == (node->flags & LYS_CONFIG_MASK)) {
4361 /* nothing to do */
4362 return LY_SUCCESS;
4363 }
4364
4365 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004366 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004367 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4368 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004369 "Invalid %s of config - configuration node cannot be child of any state data node.",
4370 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004371 return LY_EVALID;
4372 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004373 node->flags |= LYS_SET_CONFIG;
4374 } else {
4375 if (node->flags & LYS_SET_CONFIG) {
4376 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4377 /* setting config flags, but have node with explicit config true */
4378 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004379 "Invalid %s of config - configuration node cannot be child of any state data node.",
4380 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004381 return LY_EVALID;
4382 }
4383 /* do not change config on nodes where the config is explicitely set, this does not apply to
4384 * nodes, which are being changed explicitly (targets of refine or deviation) */
4385 return LY_SUCCESS;
4386 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004387 }
4388 node->flags &= ~LYS_CONFIG_MASK;
4389 node->flags |= config;
4390
4391 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004392 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004393 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004394 }
4395
Radek Krejci76b3e962018-12-14 17:01:25 +01004396 return LY_SUCCESS;
4397}
4398
Radek Krejcib56c7502019-02-13 14:19:54 +01004399/**
4400 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4401 *
4402 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4403 * the flag to such parents from a mandatory children.
4404 *
4405 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4406 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4407 * (mandatory children was removed).
4408 */
Radek Krejcife909632019-02-12 15:34:42 +01004409void
4410lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4411{
4412 struct lysc_node *iter;
4413
4414 if (add) { /* set flag */
4415 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4416 parent = parent->parent) {
4417 parent->flags |= LYS_MAND_TRUE;
4418 }
4419 } else { /* unset flag */
4420 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004421 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004422 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004423 /* there is another mandatory node */
4424 return;
4425 }
4426 }
4427 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4428 parent->flags &= ~LYS_MAND_TRUE;
4429 }
4430 }
4431}
4432
Radek Krejci056d0a82018-12-06 16:57:25 +01004433/**
Radek Krejci3641f562019-02-13 15:38:40 +01004434 * @brief Internal sorting process for the lys_compile_augment_sort().
4435 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4436 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4437 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4438 */
4439static void
4440lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4441{
4442 unsigned int v;
4443 size_t len;
4444
4445 len = strlen(aug_p->nodeid);
4446 LY_ARRAY_FOR(result, v) {
4447 if (strlen(result[v]->nodeid) <= len) {
4448 continue;
4449 }
4450 if (v < LY_ARRAY_SIZE(result)) {
4451 /* move the rest of array */
4452 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4453 break;
4454 }
4455 }
4456 result[v] = aug_p;
4457 LY_ARRAY_INCREMENT(result);
4458}
4459
4460/**
4461 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4462 *
4463 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4464 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4465 *
4466 * @param[in] ctx Compile context.
4467 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4468 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4469 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4470 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4471 * @return LY_ERR value.
4472 */
4473LY_ERR
4474lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4475{
4476 struct lysp_augment **result = NULL;
4477 unsigned int u, v;
4478 size_t count = 0;
4479
4480 assert(augments);
4481
4482 /* get count of the augments in module and all its submodules */
4483 if (aug_p) {
4484 count += LY_ARRAY_SIZE(aug_p);
4485 }
4486 LY_ARRAY_FOR(inc_p, u) {
4487 if (inc_p[u].submodule->augments) {
4488 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4489 }
4490 }
4491
4492 if (!count) {
4493 *augments = NULL;
4494 return LY_SUCCESS;
4495 }
4496 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4497
4498 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4499 * together, so there can be even /z/y betwwen them. */
4500 LY_ARRAY_FOR(aug_p, u) {
4501 lys_compile_augment_sort_(&aug_p[u], result);
4502 }
4503 LY_ARRAY_FOR(inc_p, u) {
4504 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4505 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4506 }
4507 }
4508
4509 *augments = result;
4510 return LY_SUCCESS;
4511}
4512
4513/**
4514 * @brief Compile the parsed augment connecting it into its target.
4515 *
4516 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4517 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4518 * are already implemented and compiled.
4519 *
4520 * @param[in] ctx Compile context.
4521 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004522 * @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
4523 * children in case of the augmenting uses data.
4524 * @return LY_SUCCESS on success.
4525 * @return LY_EVALID on failure.
4526 */
4527LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004528lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004529{
4530 LY_ERR ret = LY_SUCCESS;
4531 struct lysp_node *node_p, *case_node_p;
4532 struct lysc_node *target; /* target target of the augment */
4533 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004534 struct lysc_when **when, *when_shared;
4535 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004536 uint16_t flags = 0;
4537 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004538 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004539
Radek Krejci327de162019-06-14 12:52:07 +02004540 lysc_update_path(ctx, NULL, "{augment}");
4541 lysc_update_path(ctx, NULL, aug_p->nodeid);
4542
Radek Krejci7af64242019-02-18 13:07:53 +01004543 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004544 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004545 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004546 if (ret != LY_SUCCESS) {
4547 if (ret == LY_EDENIED) {
4548 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4549 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4550 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4551 }
4552 return LY_EVALID;
4553 }
4554
4555 /* check for mandatory nodes
4556 * - new cases augmenting some choice can have mandatory nodes
4557 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4558 */
Radek Krejci733988a2019-02-15 15:12:44 +01004559 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004560 allow_mandatory = 1;
4561 }
4562
4563 when_shared = NULL;
4564 LY_LIST_FOR(aug_p->child, node_p) {
4565 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4566 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4567 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004568 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4569 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004570 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004571 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4572 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004573 return LY_EVALID;
4574 }
4575
4576 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004577 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004578 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004579 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004580 } else {
4581 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004582 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004583 }
4584 }
Radek Krejciec4da802019-05-02 13:02:41 +02004585 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004586
Radek Krejcife13da42019-02-15 14:51:01 +01004587 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4588 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004589 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004590 /* 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 +01004591 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 +01004592 } else if (target->nodetype == LYS_CHOICE) {
4593 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4594 node = ((struct lysc_node_choice*)target)->cases->prev;
4595 } else {
4596 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004597 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004598 }
4599
Radek Krejci733988a2019-02-15 15:12:44 +01004600 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004601 node->flags &= ~LYS_MAND_TRUE;
4602 lys_compile_mandatory_parents(target, 0);
4603 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004604 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004605 return LY_EVALID;
4606 }
4607
4608 /* pass augment's when to all the children */
4609 if (aug_p->when) {
4610 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4611 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004612 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004613 LY_CHECK_GOTO(ret, error);
4614 (*when)->context = lysc_xpath_context(target);
4615 when_shared = *when;
4616 } else {
4617 ++when_shared->refcount;
4618 (*when) = when_shared;
4619 }
4620 }
4621 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004622
Radek Krejciec4da802019-05-02 13:02:41 +02004623 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004624 switch (target->nodetype) {
4625 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004626 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004627 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004628 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004629 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004630 break;
4631 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004632 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004633 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004634 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004635 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004636 break;
4637 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004638 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004639 if (aug_p->actions) {
4640 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004641 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4642 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004643 return LY_EVALID;
4644 }
4645 if (aug_p->notifs) {
4646 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004647 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4648 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004649 return LY_EVALID;
4650 }
4651 }
Radek Krejci3641f562019-02-13 15:38:40 +01004652
Radek Krejci327de162019-06-14 12:52:07 +02004653 lysc_update_path(ctx, NULL, NULL);
4654 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004655error:
Radek Krejciec4da802019-05-02 13:02:41 +02004656 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004657 return ret;
4658}
4659
4660/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004661 * @brief Apply refined or deviated mandatory flag to the target node.
4662 *
4663 * @param[in] ctx Compile context.
4664 * @param[in] node Target node where the mandatory property is supposed to be changed.
4665 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004666 * @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 +01004667 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4668 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4669 * 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 +01004670 * @return LY_ERR value.
4671 */
4672static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004673lys_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 +01004674{
4675 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4676 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004677 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4678 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004679 return LY_EVALID;
4680 }
4681
4682 if (mandatory_flag & LYS_MAND_TRUE) {
4683 /* check if node has default value */
4684 if (node->nodetype & LYS_LEAF) {
4685 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004686 if (refine_flag) {
4687 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004688 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004689 return LY_EVALID;
4690 }
Radek Krejcia1911222019-07-22 17:24:50 +02004691 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004692 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004693 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004694
4695 /* update the list of incomplete default values if needed */
4696 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4697
Radek Krejcia1911222019-07-22 17:24:50 +02004698 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4699 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4700 free(leaf->dflt);
4701 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004702 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004703 }
4704 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004705 if (refine_flag) {
4706 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004707 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004708 return LY_EVALID;
4709 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004710 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004711 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004712 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 +01004713 return LY_EVALID;
4714 }
4715
4716 node->flags &= ~LYS_MAND_FALSE;
4717 node->flags |= LYS_MAND_TRUE;
4718 lys_compile_mandatory_parents(node->parent, 1);
4719 } else {
4720 /* make mandatory false */
4721 node->flags &= ~LYS_MAND_TRUE;
4722 node->flags |= LYS_MAND_FALSE;
4723 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004724 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 +01004725 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004726 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004727 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004728 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4729 leaf->dflt->realtype = leaf->type->dflt->realtype;
4730 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4731 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004732 }
4733 }
4734 return LY_SUCCESS;
4735}
4736
4737/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004738 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4739 * If present, also apply uses's modificators.
4740 *
4741 * @param[in] ctx Compile context
4742 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004743 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4744 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4745 * the compile context.
4746 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4747 */
4748static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004749lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004750{
4751 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004752 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004753 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004754 struct lysc_node_container context_node_fake =
4755 {.nodetype = LYS_CONTAINER,
4756 .module = ctx->mod,
4757 .flags = parent ? parent->flags : 0,
4758 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004759 .prev = (struct lysc_node*)&context_node_fake,
4760 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004761 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004762 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004763 int found;
4764 const char *id, *name, *prefix;
4765 size_t prefix_len, name_len;
4766 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004767 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004768 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004769 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004770 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004771 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004772 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004773 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004774 unsigned int actions_index, notifs_index;
4775 struct lysc_notif **notifs = NULL;
4776 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004777
4778 /* search for the grouping definition */
4779 found = 0;
4780 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004781 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004782 if (prefix) {
4783 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4784 if (!mod) {
4785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004786 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004787 return LY_EVALID;
4788 }
4789 } else {
4790 mod = ctx->mod_def;
4791 }
4792 if (mod == ctx->mod_def) {
4793 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004794 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004795 LY_ARRAY_FOR(grp, u) {
4796 if (!strcmp(grp[u].name, name)) {
4797 grp = &grp[u];
4798 found = 1;
4799 break;
4800 }
4801 }
4802 }
4803 }
4804 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004805 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004806 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004807 if (grp) {
4808 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4809 if (!strcmp(grp[u].name, name)) {
4810 grp = &grp[u];
4811 found = 1;
4812 }
4813 }
4814 }
4815 if (!found && mod->parsed->includes) {
4816 /* ... and all the submodules */
4817 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4818 grp = mod->parsed->includes[u].submodule->groupings;
4819 if (grp) {
4820 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4821 if (!strcmp(grp[v].name, name)) {
4822 grp = &grp[v];
4823 found = 1;
4824 }
4825 }
4826 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004827 }
4828 }
4829 }
4830 if (!found) {
4831 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4832 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4833 return LY_EVALID;
4834 }
4835
4836 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4837 grp_stack_count = ctx->groupings.count;
4838 ly_set_add(&ctx->groupings, (void*)grp, 0);
4839 if (grp_stack_count == ctx->groupings.count) {
4840 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4842 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4843 return LY_EVALID;
4844 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004845 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4846 /* remember that the grouping is instantiated to avoid its standalone validation */
4847 grp->flags |= LYS_USED_GRP;
4848 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004849
4850 /* switch context's mod_def */
4851 mod_old = ctx->mod_def;
4852 ctx->mod_def = mod;
4853
4854 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004855 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 +01004856
Radek Krejcifc11bd72019-04-11 16:00:05 +02004857 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004858 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004859 /* 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 +02004860 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 +01004861
4862 /* some preparation for applying refines */
4863 if (grp->data == node_p) {
4864 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004865 if (parent) {
4866 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4867 } else if (ctx->mod->compiled->data) {
4868 child = ctx->mod->compiled->data;
4869 } else {
4870 child = NULL;
4871 }
4872 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004873 }
4874 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004875 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004876 LY_LIST_FOR(context_node_fake.child, child) {
4877 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004878
Radek Krejcifc11bd72019-04-11 16:00:05 +02004879 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004880 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004881 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004882 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004883 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004884 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004885 when_shared = *when;
4886 } else {
4887 ++when_shared->refcount;
4888 (*when) = when_shared;
4889 }
4890 }
Radek Krejci01342af2019-01-03 15:18:08 +01004891 }
4892 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004893 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004894 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004895 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004896 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 +01004897 }
4898
Radek Krejcifc11bd72019-04-11 16:00:05 +02004899 /* compile actions */
4900 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4901 if (actions) {
4902 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004903 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004904 if (*actions && (uses_p->augments || uses_p->refines)) {
4905 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4906 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4907 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4908 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4909 }
4910 }
4911
4912 /* compile notifications */
4913 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4914 if (notifs) {
4915 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004916 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004917 if (*notifs && (uses_p->augments || uses_p->refines)) {
4918 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4919 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4920 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4921 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4922 }
4923 }
4924
4925
Radek Krejci3641f562019-02-13 15:38:40 +01004926 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004927 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004928 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004929 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004930 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004931
Radek Krejcif0089082019-01-07 16:42:01 +01004932 /* reload previous context's mod_def */
4933 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004934 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004935
Radek Krejci76b3e962018-12-14 17:01:25 +01004936 /* apply refine */
4937 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004938 lysc_update_path(ctx, NULL, rfn->nodeid);
4939
Radek Krejci7af64242019-02-18 13:07:53 +01004940 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 +01004941 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004942 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004943 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004944
4945 /* default value */
4946 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004947 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004949 "Invalid refine of default - %s cannot hold %d default values.",
4950 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004951 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004952 }
4953 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004955 "Invalid refine of default - %s cannot hold default value(s).",
4956 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004957 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004958 }
4959 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004960 struct ly_err_item *err = NULL;
4961 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4962 if (leaf->dflt) {
4963 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004964 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004965 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4966 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4967 } else {
4968 /* prepare a new one */
4969 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4970 leaf->dflt->realtype = leaf->type;
4971 }
4972 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004973 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004974 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4975 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004976 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004977 leaf->dflt->realtype->refcount++;
4978 if (err) {
4979 ly_err_print(err);
4980 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4981 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4982 ly_err_free(err);
4983 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004984 if (rc == LY_EINCOMPLETE) {
4985 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004986 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004987
4988 /* but in general result is so far ok */
4989 rc = LY_SUCCESS;
4990 }
Radek Krejcia1911222019-07-22 17:24:50 +02004991 LY_CHECK_GOTO(rc, cleanup);
4992 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004993 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004994 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4995
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004996 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4998 "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 +02004999 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01005000 }
Radek Krejcia1911222019-07-22 17:24:50 +02005001
5002 /* remove previous set of default values */
5003 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005004 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005005 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5006 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5007 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005008 }
Radek Krejcia1911222019-07-22 17:24:50 +02005009 LY_ARRAY_FREE(llist->dflts);
5010 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005011 LY_ARRAY_FREE(llist->dflts_mods);
5012 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005013
5014 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005015 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005016 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005017 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005018 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005019 LY_ARRAY_INCREMENT(llist->dflts_mods);
5020 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005021 LY_ARRAY_INCREMENT(llist->dflts);
5022 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5023 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005024 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005025 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005026 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005027 llist->dflts[u]->realtype->refcount++;
5028 if (err) {
5029 ly_err_print(err);
5030 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5031 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5032 ly_err_free(err);
5033 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005034 if (rc == LY_EINCOMPLETE) {
5035 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005036 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005037
5038 /* but in general result is so far ok */
5039 rc = LY_SUCCESS;
5040 }
5041 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005042 }
Radek Krejcia1911222019-07-22 17:24:50 +02005043 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005044 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005045 if (((struct lysc_node_choice*)node)->dflt) {
5046 /* unset LYS_SET_DFLT from the current default case */
5047 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5048 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005049 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 +01005050 }
5051 }
5052
Radek Krejci12fb9142019-01-08 09:45:30 +01005053 /* description */
5054 if (rfn->dsc) {
5055 FREE_STRING(ctx->ctx, node->dsc);
5056 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5057 }
5058
5059 /* reference */
5060 if (rfn->ref) {
5061 FREE_STRING(ctx->ctx, node->ref);
5062 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5063 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005064
5065 /* config */
5066 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005067 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005068 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005069 } else {
5070 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
5071 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
5072 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005073 }
5074
5075 /* mandatory */
5076 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005077 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005078 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005079
5080 /* presence */
5081 if (rfn->presence) {
5082 if (node->nodetype != LYS_CONTAINER) {
5083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005084 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5085 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005086 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005087 }
5088 node->flags |= LYS_PRESENCE;
5089 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005090
5091 /* must */
5092 if (rfn->musts) {
5093 switch (node->nodetype) {
5094 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02005095 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 +01005096 break;
5097 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02005098 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 +01005099 break;
5100 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02005101 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 +01005102 break;
5103 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02005104 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 +01005105 break;
5106 case LYS_ANYXML:
5107 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02005108 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 +01005109 break;
5110 default:
5111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005112 "Invalid refine of must statement - %s cannot hold any must statement.",
5113 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005114 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005115 }
5116 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005117
5118 /* min/max-elements */
5119 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5120 switch (node->nodetype) {
5121 case LYS_LEAFLIST:
5122 if (rfn->flags & LYS_SET_MAX) {
5123 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5124 }
5125 if (rfn->flags & LYS_SET_MIN) {
5126 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005127 if (rfn->min) {
5128 node->flags |= LYS_MAND_TRUE;
5129 lys_compile_mandatory_parents(node->parent, 1);
5130 } else {
5131 node->flags &= ~LYS_MAND_TRUE;
5132 lys_compile_mandatory_parents(node->parent, 0);
5133 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005134 }
5135 break;
5136 case LYS_LIST:
5137 if (rfn->flags & LYS_SET_MAX) {
5138 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5139 }
5140 if (rfn->flags & LYS_SET_MIN) {
5141 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005142 if (rfn->min) {
5143 node->flags |= LYS_MAND_TRUE;
5144 lys_compile_mandatory_parents(node->parent, 1);
5145 } else {
5146 node->flags &= ~LYS_MAND_TRUE;
5147 lys_compile_mandatory_parents(node->parent, 0);
5148 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005149 }
5150 break;
5151 default:
5152 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005153 "Invalid refine of %s statement - %s cannot hold this statement.",
5154 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005155 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005156 }
5157 }
Radek Krejcif0089082019-01-07 16:42:01 +01005158
5159 /* if-feature */
5160 if (rfn->iffeatures) {
5161 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005162 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005163 }
Radek Krejci327de162019-06-14 12:52:07 +02005164
5165 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005166 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005167
Radek Krejcif2271f12019-01-07 16:42:23 +01005168 /* do some additional checks of the changed nodes when all the refines are applied */
5169 for (u = 0; u < refined.count; ++u) {
5170 node = (struct lysc_node*)refined.objs[u];
5171 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005172 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005173
5174 /* check possible conflict with default value (default added, mandatory left true) */
5175 if ((node->flags & LYS_MAND_TRUE) &&
5176 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5177 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5178 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005179 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005180 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005181 }
5182
5183 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5184 if (node->nodetype == LYS_LIST) {
5185 min = ((struct lysc_node_list*)node)->min;
5186 max = ((struct lysc_node_list*)node)->max;
5187 } else {
5188 min = ((struct lysc_node_leaflist*)node)->min;
5189 max = ((struct lysc_node_leaflist*)node)->max;
5190 }
5191 if (min > max) {
5192 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005193 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5194 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005195 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005196 }
5197 }
5198 }
5199
Radek Krejci327de162019-06-14 12:52:07 +02005200 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005201 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005202
5203cleanup:
5204 /* fix connection of the children nodes from fake context node back into the parent */
5205 if (context_node_fake.child) {
5206 context_node_fake.child->prev = child;
5207 }
5208 LY_LIST_FOR(context_node_fake.child, child) {
5209 child->parent = parent;
5210 }
5211
5212 if (uses_p->augments || uses_p->refines) {
5213 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005214 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005215 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5216 LY_ARRAY_FREE(context_node_fake.actions);
5217 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005218 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005219 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5220 LY_ARRAY_FREE(context_node_fake.notifs);
5221 }
5222 }
5223
Radek Krejcie86bf772018-12-14 11:39:53 +01005224 /* reload previous context's mod_def */
5225 ctx->mod_def = mod_old;
5226 /* remove the grouping from the stack for circular groupings dependency check */
5227 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5228 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005229 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005230 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005231
5232 return ret;
5233}
5234
Radek Krejci327de162019-06-14 12:52:07 +02005235static int
5236lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5237{
5238 struct lysp_node *iter;
5239 int len = 0;
5240
5241 *path = NULL;
5242 for (iter = node; iter && len >= 0; iter = iter->parent) {
5243 char *s = *path;
5244 char *id;
5245
5246 switch (iter->nodetype) {
5247 case LYS_USES:
5248 asprintf(&id, "{uses='%s'}", iter->name);
5249 break;
5250 case LYS_GROUPING:
5251 asprintf(&id, "{grouping='%s'}", iter->name);
5252 break;
5253 case LYS_AUGMENT:
5254 asprintf(&id, "{augment='%s'}", iter->name);
5255 break;
5256 default:
5257 id = strdup(iter->name);
5258 break;
5259 }
5260
5261 if (!iter->parent) {
5262 /* print prefix */
5263 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5264 } else {
5265 /* prefix is the same as in parent */
5266 len = asprintf(path, "/%s%s", id, s ? s : "");
5267 }
5268 free(s);
5269 free(id);
5270 }
5271
5272 if (len < 0) {
5273 free(*path);
5274 *path = NULL;
5275 } else if (len == 0) {
5276 *path = strdup("/");
5277 len = 1;
5278 }
5279 return len;
5280}
5281
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005282/**
5283 * @brief Validate groupings that were defined but not directly used in the schema itself.
5284 *
5285 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5286 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5287 */
5288static LY_ERR
5289lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5290{
5291 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005292 char *path;
5293 int len;
5294
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005295 struct lysp_node_uses fake_uses = {
5296 .parent = node_p,
5297 .nodetype = LYS_USES,
5298 .flags = 0, .next = NULL,
5299 .name = grp->name,
5300 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5301 .refines = NULL, .augments = NULL
5302 };
5303 struct lysc_node_container fake_container = {
5304 .nodetype = LYS_CONTAINER,
5305 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5306 .module = ctx->mod,
5307 .sp = NULL, .parent = NULL, .next = NULL,
5308 .prev = (struct lysc_node*)&fake_container,
5309 .name = "fake",
5310 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5311 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5312 };
5313
5314 if (grp->parent) {
5315 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5316 }
Radek Krejci327de162019-06-14 12:52:07 +02005317
5318 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5319 if (len < 0) {
5320 LOGMEM(ctx->ctx);
5321 return LY_EMEM;
5322 }
5323 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5324 ctx->path_len = (uint16_t)len;
5325 free(path);
5326
5327 lysc_update_path(ctx, NULL, "{grouping}");
5328 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005329 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005330 lysc_update_path(ctx, NULL, NULL);
5331 lysc_update_path(ctx, NULL, NULL);
5332
5333 ctx->path_len = 1;
5334 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005335
5336 /* cleanup */
5337 lysc_node_container_free(ctx->ctx, &fake_container);
5338
5339 return ret;
5340}
Radek Krejcife909632019-02-12 15:34:42 +01005341
Radek Krejcie86bf772018-12-14 11:39:53 +01005342/**
Radek Krejcia3045382018-11-22 14:30:31 +01005343 * @brief Compile parsed schema node information.
5344 * @param[in] ctx Compile context
5345 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005346 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5347 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5348 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005349 * @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).
5350 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005351 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5352 */
Radek Krejci19a96102018-11-15 13:38:09 +01005353static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005354lys_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 +01005355{
5356 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005357 struct lysc_node *node;
5358 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005359 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005360 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005361 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005362
Radek Krejci327de162019-06-14 12:52:07 +02005363 if (node_p->nodetype != LYS_USES) {
5364 lysc_update_path(ctx, parent, node_p->name);
5365 } else {
5366 lysc_update_path(ctx, NULL, "{uses}");
5367 lysc_update_path(ctx, NULL, node_p->name);
5368 }
5369
Radek Krejci19a96102018-11-15 13:38:09 +01005370 switch (node_p->nodetype) {
5371 case LYS_CONTAINER:
5372 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5373 node_compile_spec = lys_compile_node_container;
5374 break;
5375 case LYS_LEAF:
5376 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5377 node_compile_spec = lys_compile_node_leaf;
5378 break;
5379 case LYS_LIST:
5380 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005381 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005382 break;
5383 case LYS_LEAFLIST:
5384 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005385 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005386 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005387 case LYS_CHOICE:
5388 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005389 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005390 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005391 case LYS_ANYXML:
5392 case LYS_ANYDATA:
5393 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005394 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005395 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005396 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005397 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5398 lysc_update_path(ctx, NULL, NULL);
5399 lysc_update_path(ctx, NULL, NULL);
5400 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005401 default:
5402 LOGINT(ctx->ctx);
5403 return LY_EINT;
5404 }
5405 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5406 node->nodetype = node_p->nodetype;
5407 node->module = ctx->mod;
5408 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005409 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005410
5411 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005412 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005413 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005414 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005415 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5416 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005417 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005418 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005419 node->flags |= LYS_CONFIG_R;
5420 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005421 /* config not explicitely set, inherit it from parent */
5422 if (parent) {
5423 node->flags |= parent->flags & LYS_CONFIG_MASK;
5424 } else {
5425 /* default is config true */
5426 node->flags |= LYS_CONFIG_W;
5427 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005428 } else {
5429 /* config set explicitely */
5430 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005431 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005432 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5433 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5434 "Configuration node cannot be child of any state data node.");
5435 goto error;
5436 }
Radek Krejci19a96102018-11-15 13:38:09 +01005437
Radek Krejcia6d57732018-11-29 13:40:37 +01005438 /* *list ordering */
5439 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5440 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005441 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005442 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5443 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005444 node->flags &= ~LYS_ORDBY_MASK;
5445 node->flags |= LYS_ORDBY_SYSTEM;
5446 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5447 /* default ordering is system */
5448 node->flags |= LYS_ORDBY_SYSTEM;
5449 }
5450 }
5451
Radek Krejci19a96102018-11-15 13:38:09 +01005452 /* status - it is not inherited by specification, but it does not make sense to have
5453 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005454 if (!parent || parent->nodetype != LYS_CHOICE) {
5455 /* in case of choice/case's children, postpone the check to the moment we know if
5456 * 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 +01005457 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 +01005458 }
5459
Radek Krejciec4da802019-05-02 13:02:41 +02005460 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005461 node->sp = node_p;
5462 }
5463 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005464 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5465 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005466 if (node_p->when) {
5467 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005468 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005469 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005470 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005471 }
Radek Krejciec4da802019-05-02 13:02:41 +02005472 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005473
5474 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005475 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005476
Radek Krejci0935f412019-08-20 16:15:18 +02005477 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5478
Radek Krejcife909632019-02-12 15:34:42 +01005479 /* inherit LYS_MAND_TRUE in parent containers */
5480 if (node->flags & LYS_MAND_TRUE) {
5481 lys_compile_mandatory_parents(parent, 1);
5482 }
5483
Radek Krejci327de162019-06-14 12:52:07 +02005484 lysc_update_path(ctx, NULL, NULL);
5485
Radek Krejci19a96102018-11-15 13:38:09 +01005486 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005487 if (parent) {
5488 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005489 if (node_p->parent->nodetype == LYS_CASE) {
5490 lysc_update_path(ctx, parent, node_p->parent->name);
5491 } else {
5492 lysc_update_path(ctx, parent, node->name);
5493 }
Radek Krejciec4da802019-05-02 13:02:41 +02005494 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005495 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005496 if (uses_status) {
5497
5498 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005499 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005500 * it directly gets the same status flags as the choice;
5501 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005502 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005503 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005504 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005505 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005506 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005507 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005508 }
Radek Krejciec4da802019-05-02 13:02:41 +02005509 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 +02005510
5511 if (parent->nodetype == LYS_CHOICE) {
5512 lysc_update_path(ctx, NULL, NULL);
5513 }
Radek Krejci19a96102018-11-15 13:38:09 +01005514 } else {
5515 /* top-level element */
5516 if (!ctx->mod->compiled->data) {
5517 ctx->mod->compiled->data = node;
5518 } else {
5519 /* insert at the end of the module's top-level nodes list */
5520 ctx->mod->compiled->data->prev->next = node;
5521 node->prev = ctx->mod->compiled->data->prev;
5522 ctx->mod->compiled->data->prev = node;
5523 }
Radek Krejci327de162019-06-14 12:52:07 +02005524 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005525 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5526 ctx->mod->compiled->notifs, node->name, node)) {
5527 return LY_EVALID;
5528 }
Radek Krejci19a96102018-11-15 13:38:09 +01005529 }
Radek Krejci327de162019-06-14 12:52:07 +02005530 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005531
5532 return LY_SUCCESS;
5533
5534error:
5535 lysc_node_free(ctx->ctx, node);
5536 return ret;
5537}
5538
Radek Krejciccd20f12019-02-15 14:12:27 +01005539static void
5540lysc_disconnect(struct lysc_node *node)
5541{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005542 struct lysc_node *parent, *child, *prev = NULL, *next;
5543 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005544 int remove_cs = 0;
5545
5546 parent = node->parent;
5547
5548 /* parent's first child */
5549 if (!parent) {
5550 return;
5551 }
5552 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005553 cs = (struct lysc_node_case*)node;
5554 } else if (parent->nodetype == LYS_CASE) {
5555 /* disconnecting some node in a case */
5556 cs = (struct lysc_node_case*)parent;
5557 parent = cs->parent;
5558 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5559 if (child == node) {
5560 if (cs->child == child) {
5561 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5562 /* case with a single child -> remove also the case */
5563 child->parent = NULL;
5564 remove_cs = 1;
5565 } else {
5566 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005567 }
5568 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005569 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005570 }
5571 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005572 if (!remove_cs) {
5573 cs = NULL;
5574 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005575 } else if (lysc_node_children(parent, node->flags) == node) {
5576 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005577 }
5578
5579 if (cs) {
5580 if (remove_cs) {
5581 /* cs has only one child which is being also removed */
5582 lysc_disconnect((struct lysc_node*)cs);
5583 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5584 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005585 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5586 /* default case removed */
5587 ((struct lysc_node_choice*)parent)->dflt = NULL;
5588 }
5589 if (((struct lysc_node_choice*)parent)->cases == cs) {
5590 /* first case removed */
5591 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5592 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005593 if (cs->child) {
5594 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5595 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5596 prev = cs->child->prev;
5597 } /* else all the children are under a single case */
5598 LY_LIST_FOR_SAFE(cs->child, next, child) {
5599 if (child->parent != (struct lysc_node*)cs) {
5600 break;
5601 }
5602 lysc_node_free(node->module->ctx, child);
5603 }
5604 if (prev) {
5605 if (prev->next) {
5606 prev->next = child;
5607 }
5608 if (child) {
5609 child->prev = prev;
5610 } else {
5611 /* link from the first child under the cases */
5612 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5613 }
5614 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005615 }
5616 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005617 }
5618
5619 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005620 if (node->prev->next) {
5621 node->prev->next = node->next;
5622 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005623 if (node->next) {
5624 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005625 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005626 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005627 if (child) {
5628 child->prev = node->prev;
5629 }
5630 } else if (((struct lysc_node_choice*)parent)->cases) {
5631 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005632 }
5633}
5634
5635LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005636lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005637{
Radek Krejcia1911222019-07-22 17:24:50 +02005638 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005639 struct ly_set devs_p = {0};
5640 struct ly_set targets = {0};
5641 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005642 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005643 struct lysc_action *rpcs;
5644 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005645 struct lysp_deviation *dev;
5646 struct lysp_deviate *d, **dp_new;
5647 struct lysp_deviate_add *d_add;
5648 struct lysp_deviate_del *d_del;
5649 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005650 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005651 struct lysc_deviation {
5652 const char *nodeid;
5653 struct lysc_node *target; /* target node of the deviation */
5654 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005655 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005656 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5657 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005658 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005659 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005660 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005661 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005662 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005663 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005664
5665 /* get all deviations from the module and all its submodules ... */
5666 LY_ARRAY_FOR(mod_p->deviations, u) {
5667 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5668 }
5669 LY_ARRAY_FOR(mod_p->includes, v) {
5670 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5671 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5672 }
5673 }
5674 if (!devs_p.count) {
5675 /* nothing to do */
5676 return LY_SUCCESS;
5677 }
5678
Radek Krejci327de162019-06-14 12:52:07 +02005679 lysc_update_path(ctx, NULL, "{deviation}");
5680
Radek Krejciccd20f12019-02-15 14:12:27 +01005681 /* ... and group them by the target node */
5682 devs = calloc(devs_p.count, sizeof *devs);
5683 for (u = 0; u < devs_p.count; ++u) {
5684 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005685 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005686
5687 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005688 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5689 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005690 if (target->nodetype == LYS_ACTION) {
5691 /* move the target pointer to input/output to make them different from the action and
5692 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5693 * back to the RPC/action node due to a better compatibility and decision code in this function.
5694 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5695 if (flags & LYSC_OPT_RPC_INPUT) {
5696 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5697 flags |= LYSC_OPT_INTERNAL;
5698 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5699 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5700 flags |= LYSC_OPT_INTERNAL;
5701 }
5702 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005703 /* insert into the set of targets with duplicity detection */
5704 i = ly_set_add(&targets, target, 0);
5705 if (!devs[i]) {
5706 /* new record */
5707 devs[i] = calloc(1, sizeof **devs);
5708 devs[i]->target = target;
5709 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005710 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005711 }
5712 /* add deviates into the deviation's list of deviates */
5713 for (d = dev->deviates; d; d = d->next) {
5714 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5715 *dp_new = d;
5716 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5717 devs[i]->not_supported = 1;
5718 }
5719 }
Radek Krejci327de162019-06-14 12:52:07 +02005720
5721 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005722 }
5723
5724 /* MACROS for deviates checking */
5725#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5726 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005727 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 +01005728 goto cleanup; \
5729 }
5730
5731#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5732 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005733 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5734 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005735 goto cleanup; \
5736 }
5737
Radek Krejcia1911222019-07-22 17:24:50 +02005738
Radek Krejciccd20f12019-02-15 14:12:27 +01005739#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005740 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5742 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5743 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5744 goto cleanup; \
5745 }
5746
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005747#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005748 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005749 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005750 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5751 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005752 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005753 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5754 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005755 goto cleanup; \
5756 }
5757
Radek Krejci551b12c2019-02-19 16:11:21 +01005758#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5759 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005761 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5762 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005763 goto cleanup; \
5764 }
5765
Radek Krejciccd20f12019-02-15 14:12:27 +01005766#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5767 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005768 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005769 goto cleanup; \
5770 }
5771
Radek Krejci551b12c2019-02-19 16:11:21 +01005772#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5773 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5774 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005775 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005776 goto cleanup; \
5777 }
5778
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005779#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5780 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5781 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5782 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5783 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 +01005784 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005785 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005787 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5788 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005789 goto cleanup; \
5790 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005791 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5792 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5793 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5794 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5795 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005796 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005797 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5798 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5799 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005800 }
5801
5802 /* apply deviations */
5803 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005804 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5805 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5806 struct ly_err_item *err = NULL;
5807
5808 dflt = NULL;
5809 changed_type = 0;
5810
Radek Krejci327de162019-06-14 12:52:07 +02005811 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5812
Radek Krejcif538ce52019-03-05 10:46:14 +01005813 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5814 /* fix the target pointer in case of RPC's/action's input/output */
5815 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5816 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5817 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5818 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5819 }
5820 }
5821
Radek Krejciccd20f12019-02-15 14:12:27 +01005822 /* not-supported */
5823 if (devs[u]->not_supported) {
5824 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5825 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5826 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5827 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005828
5829#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5830 if (devs[u]->target->parent) { \
5831 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5832 } else { \
5833 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5834 } \
5835 LY_ARRAY_FOR(ARRAY, x) { \
5836 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5837 } \
5838 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5839 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5840 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5841 LY_ARRAY_DECREMENT(ARRAY); \
5842 }
5843
Radek Krejcif538ce52019-03-05 10:46:14 +01005844 if (devs[u]->target->nodetype == LYS_ACTION) {
5845 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5846 /* remove RPC's/action's input */
5847 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5848 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005849 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5850 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005851 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5852 /* remove RPC's/action's output */
5853 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5854 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005855 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5856 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005857 } else {
5858 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005859 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005860 }
5861 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005862 /* remove Notification */
5863 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005864 } else {
5865 /* remove the target node */
5866 lysc_disconnect(devs[u]->target);
5867 lysc_node_free(ctx->ctx, devs[u]->target);
5868 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005869
Radek Krejci474f9b82019-07-24 11:36:37 +02005870 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5871 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005872 continue;
5873 }
5874
5875 /* list of deviates (not-supported is not present in the list) */
5876 LY_ARRAY_FOR(devs[u]->deviates, v) {
5877 d = devs[u]->deviates[v];
5878
5879 switch (d->mod) {
5880 case LYS_DEV_ADD:
5881 d_add = (struct lysp_deviate_add*)d;
5882 /* [units-stmt] */
5883 if (d_add->units) {
5884 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5885 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5886
5887 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5888 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5889 }
5890
5891 /* *must-stmt */
5892 if (d_add->musts) {
5893 switch (devs[u]->target->nodetype) {
5894 case LYS_CONTAINER:
5895 case LYS_LIST:
5896 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005897 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005898 break;
5899 case LYS_LEAF:
5900 case LYS_LEAFLIST:
5901 case LYS_ANYDATA:
5902 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005903 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005904 break;
5905 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005906 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005907 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005908 break;
5909 case LYS_ACTION:
5910 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5911 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005912 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005913 break;
5914 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5915 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005916 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005917 break;
5918 }
5919 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005920 default:
5921 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005922 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005923 goto cleanup;
5924 }
5925 }
5926
5927 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005928 if (d_add->uniques) {
5929 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5930 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5931 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005932
5933 /* *default-stmt */
5934 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005935 switch (devs[u]->target->nodetype) {
5936 case LYS_LEAF:
5937 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005938 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 +02005939 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005940 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005941 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005942 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5943 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5944 } else {
5945 /* prepare new default value storage */
5946 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005947 }
Radek Krejcia1911222019-07-22 17:24:50 +02005948 dflt = d_add->dflts[0];
5949 /* parsing is done at the end after possible replace of the leaf's type */
5950
Radek Krejci551b12c2019-02-19 16:11:21 +01005951 /* mark the new default values as leaf's own */
5952 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005953 break;
5954 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005955 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005956 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005957 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005958 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005959 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5960 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5961 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005962 }
Radek Krejcia1911222019-07-22 17:24:50 +02005963 LY_ARRAY_FREE(llist->dflts);
5964 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005965 LY_ARRAY_FREE(llist->dflts_mods);
5966 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005967 }
5968 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005969 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005970 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5971 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01005972 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005973 LY_ARRAY_INCREMENT(llist->dflts_mods);
5974 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005975 LY_ARRAY_INCREMENT(llist->dflts);
5976 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5977 llist->dflts[x]->realtype = llist->type;
5978 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 +02005979 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005980 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005981 llist->dflts[x]->realtype->refcount++;
5982 if (err) {
5983 ly_err_print(err);
5984 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5985 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5986 d_add->dflts[x - y], err->msg);
5987 ly_err_free(err);
5988 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005989 if (rc == LY_EINCOMPLETE) {
5990 /* postpone default compilation when the tree is complete */
5991 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5992
5993 /* but in general result is so far ok */
5994 rc = LY_SUCCESS;
5995 }
Radek Krejcia1911222019-07-22 17:24:50 +02005996 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005997 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005998 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005999 devs[u]->target->flags |= LYS_SET_DFLT;
6000 break;
6001 case LYS_CHOICE:
6002 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6003 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6004 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6005 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006006 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 +01006007 goto cleanup;
6008 }
6009 break;
6010 default:
6011 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006012 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006013 goto cleanup;
6014 }
6015 }
6016
6017 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006018 if (d_add->flags & LYS_CONFIG_MASK) {
6019 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006021 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6022 goto cleanup;
6023 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006024 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006025 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
6026 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006027 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006028 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006030 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6031 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006032 goto cleanup;
6033 }
Radek Krejci327de162019-06-14 12:52:07 +02006034 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006035 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006036
6037 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006038 if (d_add->flags & LYS_MAND_MASK) {
6039 if (devs[u]->target->flags & LYS_MAND_MASK) {
6040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006041 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6042 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006043 goto cleanup;
6044 }
Radek Krejci327de162019-06-14 12:52:07 +02006045 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006046 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006047
6048 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006049 if (d_add->flags & LYS_SET_MIN) {
6050 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6051 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6052 /* change value */
6053 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6054 } else if (devs[u]->target->nodetype == LYS_LIST) {
6055 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6056 /* change value */
6057 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6058 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006059 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006060 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6061 goto cleanup;
6062 }
6063 if (d_add->min) {
6064 devs[u]->target->flags |= LYS_MAND_TRUE;
6065 }
6066 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006067
6068 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006069 if (d_add->flags & LYS_SET_MAX) {
6070 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6071 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6072 /* change value */
6073 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6074 } else if (devs[u]->target->nodetype == LYS_LIST) {
6075 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6076 /* change value */
6077 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6078 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006079 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006080 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6081 goto cleanup;
6082 }
6083 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006084
6085 break;
6086 case LYS_DEV_DELETE:
6087 d_del = (struct lysp_deviate_del*)d;
6088
6089 /* [units-stmt] */
6090 if (d_del->units) {
6091 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006092 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6093 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6094 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6095 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6096 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6097 goto cleanup;
6098 }
6099 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6100 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006101 }
6102
6103 /* *must-stmt */
6104 if (d_del->musts) {
6105 switch (devs[u]->target->nodetype) {
6106 case LYS_CONTAINER:
6107 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006108 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006109 break;
6110 case LYS_LEAF:
6111 case LYS_LEAFLIST:
6112 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006113 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006114 break;
6115 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006116 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006117 break;
6118 case LYS_ACTION:
6119 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6120 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6121 break;
6122 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6123 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6124 break;
6125 }
6126 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006127 default:
6128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006129 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006130 goto cleanup;
6131 }
6132 }
6133
6134 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006135 if (d_del->uniques) {
6136 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6137 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6138 LY_ARRAY_FOR(d_del->uniques, x) {
6139 LY_ARRAY_FOR(list->uniques, z) {
6140 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6141 nodeid = strpbrk(name, " \t\n");
6142 if (nodeid) {
6143 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
6144 || list->uniques[z][y]->name[nodeid - name] != '\0') {
6145 break;
6146 }
6147 while (isspace(*nodeid)) {
6148 ++nodeid;
6149 }
6150 } else {
6151 if (strcmp(name, list->uniques[z][y]->name)) {
6152 break;
6153 }
6154 }
6155 }
6156 if (!name) {
6157 /* complete match - remove the unique */
6158 LY_ARRAY_DECREMENT(list->uniques);
6159 LY_ARRAY_FREE(list->uniques[z]);
6160 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6161 --z;
6162 break;
6163 }
6164 }
6165 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6166 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006167 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6168 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006169 goto cleanup;
6170 }
6171 }
6172 if (!LY_ARRAY_SIZE(list->uniques)) {
6173 LY_ARRAY_FREE(list->uniques);
6174 list->uniques = NULL;
6175 }
6176 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006177
6178 /* *default-stmt */
6179 if (d_del->dflts) {
6180 switch (devs[u]->target->nodetype) {
6181 case LYS_LEAF:
6182 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6183 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6184 dflt, "deleting", "default", d_del->dflts[0]);
6185
Radek Krejcia1911222019-07-22 17:24:50 +02006186 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006187 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006188 if (strcmp(dflt, d_del->dflts[0])) {
6189 if (i) {
6190 free((char*)dflt);
6191 }
6192 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6193 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6194 d_del->dflts[0], dflt);
6195 goto cleanup;
6196 }
6197 if (i) {
6198 free((char*)dflt);
6199 }
6200 dflt = NULL;
6201
Radek Krejci474f9b82019-07-24 11:36:37 +02006202 /* update the list of incomplete default values if needed */
6203 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6204
Radek Krejcia1911222019-07-22 17:24:50 +02006205 /* remove the default specification */
6206 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6207 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6208 free(leaf->dflt);
6209 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006210 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006211 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006212 break;
6213 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006214 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6215 LY_ARRAY_FOR(d_del->dflts, x) {
6216 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006217 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 +02006218 if (!strcmp(dflt, d_del->dflts[x])) {
6219 if (i) {
6220 free((char*)dflt);
6221 }
6222 dflt = NULL;
6223 break;
6224 }
6225 if (i) {
6226 free((char*)dflt);
6227 }
6228 dflt = NULL;
6229 }
6230 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6231 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6232 "which does not match any of the target's property values.", d_del->dflts[x]);
6233 goto cleanup;
6234 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006235
6236 /* update the list of incomplete default values if needed */
6237 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6238
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006239 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006240 LY_ARRAY_DECREMENT(llist->dflts);
6241 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6242 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6243 free(llist->dflts[y]);
6244 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006245 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 +02006246 }
6247 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006248 LY_ARRAY_FREE(llist->dflts_mods);
6249 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006250 LY_ARRAY_FREE(llist->dflts);
6251 llist->dflts = NULL;
6252 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006253 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006254 break;
6255 case LYS_CHOICE:
6256 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6257 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6258 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006259 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006260 if (prefix) {
6261 /* use module prefixes from the deviation module to match the module of the default case */
6262 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6263 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006264 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6265 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006266 goto cleanup;
6267 }
6268 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6269 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006270 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6271 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006272 goto cleanup;
6273 }
6274 }
6275 /* else {
6276 * strictly, the default prefix would point to the deviation module, but the value should actually
6277 * match the default string in the original module (usually unprefixed), so in this case we do not check
6278 * the module of the default case, just matching its name */
6279 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006281 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6282 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006283 goto cleanup;
6284 }
6285 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6286 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6287 break;
6288 default:
6289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006290 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006291 goto cleanup;
6292 }
6293 }
6294
6295 break;
6296 case LYS_DEV_REPLACE:
6297 d_rpl = (struct lysp_deviate_rpl*)d;
6298
6299 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006300 if (d_rpl->type) {
6301 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6302 /* type is mandatory, so checking for its presence is not necessary */
6303 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006304
6305 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6306 /* the target has default from the previous type - remove it */
6307 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006308 /* update the list of incomplete default values if needed */
6309 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6310
Radek Krejcia1911222019-07-22 17:24:50 +02006311 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6312 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6313 free(leaf->dflt);
6314 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006315 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006316 } else { /* LYS_LEAFLIST */
6317 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006318 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006319 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6320 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6321 free(llist->dflts[x]);
6322 }
6323 LY_ARRAY_FREE(llist->dflts);
6324 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006325 LY_ARRAY_FREE(llist->dflts_mods);
6326 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006327 }
6328 }
6329 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006330 /* there is no default value, do not set changed_type after type compilation
6331 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006332 changed_type = -1;
6333 }
Radek Krejciec4da802019-05-02 13:02:41 +02006334 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 +02006335 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006336 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006337
6338 /* [units-stmt] */
6339 if (d_rpl->units) {
6340 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6341 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6342 units, "replacing", "units", d_rpl->units);
6343
6344 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6345 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6346 }
6347
6348 /* [default-stmt] */
6349 if (d_rpl->dflt) {
6350 switch (devs[u]->target->nodetype) {
6351 case LYS_LEAF:
6352 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6353 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006354 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006355 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006356 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6357 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6358 dflt = d_rpl->dflt;
6359 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006360 break;
6361 case LYS_CHOICE:
6362 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006363 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 +01006364 goto cleanup;
6365 }
6366 break;
6367 default:
6368 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006369 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006370 goto cleanup;
6371 }
6372 }
6373
6374 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006375 if (d_rpl->flags & LYS_CONFIG_MASK) {
6376 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006377 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006378 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6379 goto cleanup;
6380 }
6381 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006382 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006383 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6384 goto cleanup;
6385 }
Radek Krejci327de162019-06-14 12:52:07 +02006386 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006387 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006388
6389 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006390 if (d_rpl->flags & LYS_MAND_MASK) {
6391 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006392 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006393 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6394 goto cleanup;
6395 }
Radek Krejci327de162019-06-14 12:52:07 +02006396 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006397 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006398
6399 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006400 if (d_rpl->flags & LYS_SET_MIN) {
6401 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6402 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6403 /* change value */
6404 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6405 } else if (devs[u]->target->nodetype == LYS_LIST) {
6406 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6407 /* change value */
6408 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6409 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006410 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006411 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6412 goto cleanup;
6413 }
6414 if (d_rpl->min) {
6415 devs[u]->target->flags |= LYS_MAND_TRUE;
6416 }
6417 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006418
6419 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006420 if (d_rpl->flags & LYS_SET_MAX) {
6421 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6422 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6423 /* change value */
6424 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6425 } else if (devs[u]->target->nodetype == LYS_LIST) {
6426 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6427 /* change value */
6428 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6429 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006430 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006431 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6432 goto cleanup;
6433 }
6434 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006435
6436 break;
6437 default:
6438 LOGINT(ctx->ctx);
6439 goto cleanup;
6440 }
6441 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006442
Radek Krejci33f72892019-02-21 10:36:58 +01006443 /* final check when all deviations of a single target node are applied */
6444
Radek Krejci551b12c2019-02-19 16:11:21 +01006445 /* check min-max compatibility */
6446 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6447 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6448 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6449 } else if (devs[u]->target->nodetype == LYS_LIST) {
6450 min = ((struct lysc_node_list*)devs[u]->target)->min;
6451 max = ((struct lysc_node_list*)devs[u]->target)->max;
6452 } else {
6453 min = max = 0;
6454 }
6455 if (min > max) {
6456 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 +02006457 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006458 goto cleanup;
6459 }
6460
Radek Krejcia1911222019-07-22 17:24:50 +02006461 if (dflt) {
6462 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006463 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006464 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006465 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6466 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006467 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006468 leaf->dflt->realtype->refcount++;
6469 if (err) {
6470 ly_err_print(err);
6471 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6472 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6473 ly_err_free(err);
6474 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006475 if (rc == LY_EINCOMPLETE) {
6476 /* postpone default compilation when the tree is complete */
6477 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6478
6479 /* but in general result is so far ok */
6480 rc = LY_SUCCESS;
6481 }
Radek Krejcia1911222019-07-22 17:24:50 +02006482 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006483 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006484 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6485 int dynamic;
6486 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006487 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006488
6489 /* update the list of incomplete default values if needed */
6490 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6491
6492 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006493 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6494 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6495 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006496 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6497 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006498 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006499 leaf->dflt->realtype->refcount++;
6500 if (err) {
6501 ly_err_print(err);
6502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6503 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6504 ly_err_free(err);
6505 }
6506 if (dynamic) {
6507 free((void*)dflt);
6508 }
Radek Krejcia1911222019-07-22 17:24:50 +02006509 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006510 if (rc == LY_EINCOMPLETE) {
6511 /* postpone default compilation when the tree is complete */
6512 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6513
6514 /* but in general result is so far ok */
6515 rc = LY_SUCCESS;
6516 }
6517 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006518 } else { /* LYS_LEAFLIST */
6519 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006520 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 +02006521 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6522 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6523 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006524 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6525 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006526 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6527 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006528 llist->dflts[x]->realtype->refcount++;
6529 if (err) {
6530 ly_err_print(err);
6531 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6532 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6533 dflt, err->msg);
6534 ly_err_free(err);
6535 }
6536 if (dynamic) {
6537 free((void*)dflt);
6538 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006539 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006540 if (rc == LY_EINCOMPLETE) {
6541 /* postpone default compilation when the tree is complete */
6542 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6543
6544 /* but in general result is so far ok */
6545 rc = LY_SUCCESS;
6546 }
Radek Krejcia1911222019-07-22 17:24:50 +02006547 LY_CHECK_GOTO(rc, cleanup);
6548 }
6549 }
6550 }
6551
Radek Krejci551b12c2019-02-19 16:11:21 +01006552 /* check mandatory - default compatibility */
6553 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6554 && (devs[u]->target->flags & LYS_SET_DFLT)
6555 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6556 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006557 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006558 goto cleanup;
6559 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6560 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6561 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006562 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 +01006563 goto cleanup;
6564 }
6565 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6566 /* mandatory node under a default case */
6567 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006568 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6569 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006570 goto cleanup;
6571 }
Radek Krejci33f72892019-02-21 10:36:58 +01006572
Radek Krejci327de162019-06-14 12:52:07 +02006573 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006574 }
6575
Radek Krejci327de162019-06-14 12:52:07 +02006576 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006577 ret = LY_SUCCESS;
6578
6579cleanup:
6580 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6581 LY_ARRAY_FREE(devs[u]->deviates);
6582 free(devs[u]);
6583 }
6584 free(devs);
6585 ly_set_erase(&targets, NULL);
6586 ly_set_erase(&devs_p, NULL);
6587
6588 return ret;
6589}
6590
Radek Krejcib56c7502019-02-13 14:19:54 +01006591/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006592 * @brief Compile the given YANG submodule into the main module.
6593 * @param[in] ctx Compile context
6594 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006595 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6596 */
6597LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006598lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006599{
6600 unsigned int u;
6601 LY_ERR ret = LY_SUCCESS;
6602 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006603 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006604 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006605 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006606
Radek Krejci0af46292019-01-11 16:02:31 +01006607 if (!mainmod->mod->off_features) {
6608 /* features are compiled directly into the compiled module structure,
6609 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6610 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006611 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6612 LY_CHECK_GOTO(ret, error);
6613 }
6614 if (!mainmod->mod->off_extensions) {
6615 /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */
6616 ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions);
Radek Krejci0af46292019-01-11 16:02:31 +01006617 LY_CHECK_GOTO(ret, error);
6618 }
6619
Radek Krejci327de162019-06-14 12:52:07 +02006620 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006621 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006622 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006623
Radek Krejci474f9b82019-07-24 11:36:37 +02006624 /* data nodes */
6625 LY_LIST_FOR(submod->data, node_p) {
6626 ret = lys_compile_node(ctx, node_p, NULL, 0);
6627 LY_CHECK_GOTO(ret, error);
6628 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006629
Radek Krejciec4da802019-05-02 13:02:41 +02006630 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6631 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006632
Radek Krejcid05cbd92018-12-05 14:26:40 +01006633error:
6634 return ret;
6635}
6636
Radek Krejci335332a2019-09-05 13:03:35 +02006637static void *
6638lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6639{
6640 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6641 if (substmts[u].stmt == stmt) {
6642 return substmts[u].storage;
6643 }
6644 }
6645 return NULL;
6646}
6647
6648LY_ERR
6649lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6650{
6651 LY_ERR ret = LY_EVALID, r;
6652 unsigned int u;
6653 struct lysp_stmt *stmt;
6654 void *parsed = NULL, **compiled = NULL;
6655 struct ly_set mandatory_stmts = {0};
6656
6657 /* check for invalid substatements */
6658 for (stmt = ext->child; stmt; stmt = stmt->next) {
6659 for (u = 0; substmts[u].stmt; ++u) {
6660 if (substmts[u].stmt == stmt->kw) {
6661 break;
6662 }
6663 }
6664 if (!substmts[u].stmt) {
6665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s\" extension instance.",
6666 stmt->stmt, ext->name);
6667 goto cleanup;
6668 }
6669 if (substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) {
6670 ly_set_add(&mandatory_stmts, &substmts[u], LY_SET_OPT_USEASLIST);
6671 }
6672 }
6673
6674 /* keep order of the processing the same as the order in the defined substmts,
6675 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6676 for (u = 0; substmts[u].stmt; ++u) {
6677 for (stmt = ext->child; stmt; stmt = stmt->next) {
6678 if (substmts[u].stmt != stmt->kw) {
6679 continue;
6680 }
6681
6682 if (substmts[u].storage) {
6683 switch (stmt->kw) {
6684 case LY_STMT_TYPE: {
6685 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6686 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6687
6688 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6689 /* single item */
6690 if (*(struct lysc_type**)substmts->storage) {
6691 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6692 goto cleanup;
6693 }
6694 compiled = substmts[u].storage;
6695 } else {
6696 /* sized array */
6697 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6698 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6699 compiled = (void*)type;
6700 }
6701
6702 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed), ret = r, cleanup);
6703 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6704 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006705 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6706 lysp_type_free(ctx->ctx, parsed);
6707 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006708 break;
6709 }
6710 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc) */
6711 default:
6712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s\") substatement.",
6713 stmt->stmt, ext->name);
6714 goto cleanup;
6715 }
6716 }
6717
6718 if (substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) {
6719 int i = ly_set_contains(&mandatory_stmts, &substmts[u]);
6720 if (i != -1) {
6721 ly_set_rm_index(&mandatory_stmts, i, NULL);
6722 }
6723 }
6724 }
6725 }
6726
6727 if (mandatory_stmts.count) {
6728 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSTMT, "type", ext->name);
6729 goto cleanup;
6730 }
6731
6732 ret = LY_SUCCESS;
6733
6734cleanup:
6735 ly_set_erase(&mandatory_stmts, NULL);
6736
6737 return ret;
6738}
6739
Radek Krejci19a96102018-11-15 13:38:09 +01006740LY_ERR
6741lys_compile(struct lys_module *mod, int options)
6742{
6743 struct lysc_ctx ctx = {0};
6744 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006745 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006746 struct lysp_module *sp;
6747 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006748 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006749 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006750 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006751 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006752 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006753
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006754 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006755
6756 if (!mod->implemented) {
6757 /* just imported modules are not compiled */
6758 return LY_SUCCESS;
6759 }
6760
Radek Krejci19a96102018-11-15 13:38:09 +01006761 sp = mod->parsed;
6762
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006763 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006764 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006765 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006766 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006767 ctx.path_len = 1;
6768 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006769
6770 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006771 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6772 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006773
Radek Krejciec4da802019-05-02 13:02:41 +02006774 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006775 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006776 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006777 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6778 }
Radek Krejci0935f412019-08-20 16:15:18 +02006779
6780 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01006781 if (mod->off_features) {
6782 /* there is already precompiled array of features */
6783 mod_c->features = mod->off_features;
6784 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006785 } else {
6786 /* features are compiled directly into the compiled module structure,
6787 * 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 +02006788 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006789 LY_CHECK_GOTO(ret, error);
6790 }
6791 /* finish feature compilation, not only for the main module, but also for the submodules.
6792 * Due to possible forward references, it must be done when all the features (including submodules)
6793 * are present. */
6794 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006795 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006796 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6797 }
Radek Krejci327de162019-06-14 12:52:07 +02006798 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006799 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006800 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006801 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006802 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006803 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6804 }
Radek Krejci327de162019-06-14 12:52:07 +02006805 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006806 }
Radek Krejci327de162019-06-14 12:52:07 +02006807 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006808
Radek Krejci0935f412019-08-20 16:15:18 +02006809 /* extensions */
6810 /* 2-steps: a) prepare compiled structures and ... */
6811 if (mod->off_extensions) {
6812 /* there is already precompiled array of extension definitions */
6813 mod_c->extensions = mod->off_extensions;
6814 mod->off_extensions = NULL;
6815 } else {
6816 /* extension definitions are compiled directly into the compiled module structure */
6817 ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions);
6818 }
6819 /* ... b) connect the extension definitions with the appropriate extension plugins */
6820 lys_compile_extension_plugins(mod_c->extensions);
6821
6822 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02006823 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006824 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006825 if (sp->identities) {
6826 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6827 }
Radek Krejci327de162019-06-14 12:52:07 +02006828 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006829
Radek Krejci95710c92019-02-11 15:49:55 +01006830 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006831 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02006832 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01006833 LY_CHECK_GOTO(ret, error);
6834 }
Radek Krejci95710c92019-02-11 15:49:55 +01006835
Radek Krejciec4da802019-05-02 13:02:41 +02006836 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6837 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006838
Radek Krejci95710c92019-02-11 15:49:55 +01006839 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01006840 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01006841 LY_CHECK_GOTO(ret, error);
6842 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006843 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006844 LY_CHECK_GOTO(ret, error);
6845 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006846
Radek Krejci474f9b82019-07-24 11:36:37 +02006847 /* deviations TODO cover deviations from submodules */
Radek Krejciec4da802019-05-02 13:02:41 +02006848 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01006849 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006850
Radek Krejci0935f412019-08-20 16:15:18 +02006851 /* extension instances TODO cover extension instances from submodules */
6852 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006853
Radek Krejcia3045382018-11-22 14:30:31 +01006854 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006855 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6856 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6857 * 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 +01006858 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006859 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006860 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6861 if (type->basetype == LY_TYPE_LEAFREF) {
6862 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006863 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 +01006864 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006865 } else if (type->basetype == LY_TYPE_UNION) {
6866 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6867 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6868 /* validate the path */
6869 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6870 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
6871 LY_CHECK_GOTO(ret, error);
6872 }
6873 }
Radek Krejcia3045382018-11-22 14:30:31 +01006874 }
6875 }
6876 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006877 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006878 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006879 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6880 if (type->basetype == LY_TYPE_LEAFREF) {
6881 /* store pointer to the real type */
6882 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6883 typeiter->basetype == LY_TYPE_LEAFREF;
6884 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6885 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006886 } else if (type->basetype == LY_TYPE_UNION) {
6887 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6888 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6889 /* store pointer to the real type */
6890 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6891 typeiter->basetype == LY_TYPE_LEAFREF;
6892 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6893 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6894 }
6895 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006896 }
6897 }
6898 }
Radek Krejciec4da802019-05-02 13:02:41 +02006899
Radek Krejci474f9b82019-07-24 11:36:37 +02006900 /* finish incomplete default values compilation */
6901 for (u = 0; u < ctx.dflts.count; ++u) {
6902 struct ly_err_item *err = NULL;
6903 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
6904 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized),
6905 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
6906 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
6907 if (err) {
6908 ly_err_print(err);
6909 ctx.path[0] = '\0';
6910 lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
6911 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
6912 "Invalid default - value does not fit the type (%s).", err->msg);
6913 ly_err_free(err);
6914 }
6915 LY_CHECK_GOTO(ret, error);
6916 }
6917
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006918 /* validate non-instantiated groupings from the parsed schema,
6919 * without it we would accept even the schemas with invalid grouping specification */
6920 ctx.options |= LYSC_OPT_GROUPING;
6921 LY_ARRAY_FOR(sp->groupings, u) {
6922 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
6923 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
6924 }
6925 }
6926 LY_LIST_FOR(sp->data, node_p) {
6927 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
6928 LY_ARRAY_FOR(grps, u) {
6929 if (!(grps[u].flags & LYS_USED_GRP)) {
6930 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
6931 }
6932 }
6933 }
6934
Radek Krejci474f9b82019-07-24 11:36:37 +02006935 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
6936 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
6937 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
6938 }
6939
Radek Krejci1c0c3442019-07-23 16:08:47 +02006940 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006941 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006942 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006943 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006944 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01006945
Radek Krejciec4da802019-05-02 13:02:41 +02006946 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01006947 lysp_module_free(mod->parsed);
6948 ((struct lys_module*)mod)->parsed = NULL;
6949 }
6950
Radek Krejciec4da802019-05-02 13:02:41 +02006951 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01006952 /* remove flag of the modules implemented by dependency */
6953 for (u = 0; u < ctx.ctx->list.count; ++u) {
6954 m = ctx.ctx->list.objs[u];
6955 if (m->implemented == 2) {
6956 m->implemented = 1;
6957 }
6958 }
6959 }
6960
Radek Krejci19a96102018-11-15 13:38:09 +01006961 ((struct lys_module*)mod)->compiled = mod_c;
6962 return LY_SUCCESS;
6963
6964error:
Radek Krejci95710c92019-02-11 15:49:55 +01006965 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02006966 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006967 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006968 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006969 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006970 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006971 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006972 mod->compiled = NULL;
6973
6974 /* revert compilation of modules implemented by dependency */
6975 for (u = 0; u < ctx.ctx->list.count; ++u) {
6976 m = ctx.ctx->list.objs[u];
6977 if (m->implemented == 2) {
6978 /* revert features list to the precompiled state */
6979 lys_feature_precompile_revert(&ctx, m);
6980 /* mark module as imported-only / not-implemented */
6981 m->implemented = 0;
6982 /* free the compiled version of the module */
6983 lysc_module_free(m->compiled, NULL);
6984 m->compiled = NULL;
6985 }
6986 }
6987
Radek Krejci19a96102018-11-15 13:38:09 +01006988 return ret;
6989}