blob: df66c423de5c95d6e47ee741091652d38b802906 [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 Krejci7f9b6512019-09-18 13:11:09 +0200419 if (!ly_strncmp(f->name, name, len)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100420 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{
Radek Krejci7c960162019-09-18 14:16:12 +0200430 LY_ERR ret = LY_EVALID;
Radek Krejci19a96102018-11-15 13:38:09 +0100431 const char *name;
432 unsigned int u;
433 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +0200434 struct lysc_ext *elist = NULL;
Radek Krejci7c960162019-09-18 14:16:12 +0200435 const char *prefixed_name = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100436
437 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
438 ext->insubstmt = ext_p->insubstmt;
439 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200440 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200441 ext->parent = parent;
442 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100443
Radek Krejcif56e2a42019-09-09 14:15:25 +0200444 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
Radek Krejcif56e2a42019-09-09 14:15:25 +0200445
Radek Krejci19a96102018-11-15 13:38:09 +0100446 /* get module where the extension definition should be placed */
Radek Krejci7c960162019-09-18 14:16:12 +0200447 for (u = strlen(ext_p->name); u && ext_p->name[u - 1] != ':'; --u);
448 if (ext_p->yin) {
449 /* YIN parser has to replace prefixes by the namespace - XML namespace/prefix pairs may differs form the YANG schema's
450 * namespace/prefix pair. YIN parser does not have the imports available, so mapping from XML namespace to the
451 * YANG (import) prefix must be done here. */
452 if (!ly_strncmp(ctx->mod_def->ns, ext_p->name, u - 1)) {
453 prefixed_name = lydict_insert(ctx->ctx, &ext_p->name[u], 0);
454 u = 0;
455 } else if (ctx->mod_def->compiled) {
456 unsigned int v;
457 LY_ARRAY_FOR(ctx->mod_def->compiled->imports, v) {
458 if (!ly_strncmp(ctx->mod_def->compiled->imports[v].module->ns, ext_p->name, u - 1)) {
459 char *s;
460 asprintf(&s, "%s:%s", ctx->mod_def->compiled->imports[v].prefix, &ext_p->name[u]);
461 prefixed_name = lydict_insert_zc(ctx->ctx, s);
462 u = strlen(ctx->mod_def->compiled->imports[v].prefix) + 1; /* add semicolon */
463 break;
464 }
465 }
466 }
467 if (!prefixed_name) {
468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
469 "Invalid XML prefix of \"%.*s\" namespace used for extension instance identifier.", u, ext_p->name);
470 goto cleanup;
471 }
472 } else {
473 prefixed_name = ext_p->name;
474 }
475 lysc_update_path(ctx, NULL, prefixed_name);
476
477 mod = lys_module_find_prefix(ctx->mod_def, prefixed_name, u - 1);
478 if (!mod) {
479 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
480 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, prefixed_name);
481 goto cleanup;
482 } else if (!mod->parsed->extensions) {
483 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
484 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
485 prefixed_name, mod->name);
486 goto cleanup;
487 }
488 name = &prefixed_name[u];
Radek Krejci0935f412019-08-20 16:15:18 +0200489
Radek Krejci19a96102018-11-15 13:38:09 +0100490 /* find the extension definition there */
Radek Krejci0935f412019-08-20 16:15:18 +0200491 if (mod->off_extensions) {
492 elist = mod->off_extensions;
493 } else {
494 elist = mod->compiled->extensions;
495 }
496 LY_ARRAY_FOR(elist, u) {
497 if (!strcmp(name, elist[u].name)) {
498 ext->def = &elist[u];
Radek Krejci19a96102018-11-15 13:38:09 +0100499 break;
500 }
501 }
Radek Krejci7c960162019-09-18 14:16:12 +0200502 if (!ext->def) {
503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
504 "Extension definition of extension instance \"%s\" not found.", prefixed_name);
505 goto cleanup;
506 }
Radek Krejci0935f412019-08-20 16:15:18 +0200507
Radek Krejcif56e2a42019-09-09 14:15:25 +0200508 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
509 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
510 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
Radek Krejci7c960162019-09-18 14:16:12 +0200511 if (ext_p->yin && ext->def->argument && !ext->argument) {
Radek Krejcif56e2a42019-09-09 14:15:25 +0200512 /* Schema was parsed from YIN and an argument is expected, ... */
513 struct lysp_stmt *stmt = NULL;
514
515 if (ext->def->flags & LYS_YINELEM_TRUE) {
516 /* ... argument was the first XML child element */
517 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
518 /* TODO check namespace of the statement */
519 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
520 stmt = ext_p->child;
521 }
522 }
523 } else {
524 /* ... argument was one of the XML attributes which are represented as child stmt
525 * with LYS_YIN_ATTR flag */
526 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
527 if (!strcmp(stmt->stmt, ext->def->argument)) {
528 /* this is the extension's argument */
Radek Krejcif56e2a42019-09-09 14:15:25 +0200529 break;
530 }
531 }
532 }
533 if (!stmt) {
534 /* missing extension's argument */
535 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci7c960162019-09-18 14:16:12 +0200536 "Extension instance \"%s\" misses argument \"%s\".", prefixed_name, ext->def->argument);
537 goto cleanup;
Radek Krejcif56e2a42019-09-09 14:15:25 +0200538
539 }
540 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
541 stmt->flags |= LYS_YIN_ARGUMENT;
542 }
Radek Krejci7c960162019-09-18 14:16:12 +0200543 if (prefixed_name != ext_p->name) {
544 lydict_remove(ctx->ctx, ext_p->name);
545 ext_p->name = prefixed_name;
546 if (!ext_p->argument && ext->argument) {
547 ext_p->argument = lydict_insert(ctx->ctx, ext->argument, 0);
548 }
549 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200550
Radek Krejci0935f412019-08-20 16:15:18 +0200551 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200552 if (ext->argument) {
553 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
554 }
Radek Krejci7c960162019-09-18 14:16:12 +0200555 LY_CHECK_GOTO(ext->def->plugin->compile(ctx, ext_p, ext), cleanup);
Radek Krejciad5963b2019-09-06 16:03:05 +0200556 if (ext->argument) {
557 lysc_update_path(ctx, NULL, NULL);
558 }
Radek Krejci0935f412019-08-20 16:15:18 +0200559 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200560 ext_p->compiled = ext;
561
Radek Krejci7c960162019-09-18 14:16:12 +0200562 ret = LY_SUCCESS;
563cleanup:
564 if (prefixed_name && prefixed_name != ext_p->name) {
565 lydict_remove(ctx->ctx, prefixed_name);
566 }
567
Radek Krejcif56e2a42019-09-09 14:15:25 +0200568 lysc_update_path(ctx, NULL, NULL);
569 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200570
Radek Krejci7c960162019-09-18 14:16:12 +0200571 return ret;
Radek Krejci0935f412019-08-20 16:15:18 +0200572}
573
574/**
575 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
576 */
577static LY_ERR
578lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext)
579{
580 LY_ERR ret = LY_SUCCESS;
581
582 DUP_STRING(ctx->ctx, ext_p->name, ext->name);
583 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
584 ext->module = ctx->mod_def;
585 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done);
586
587done:
588 return ret;
589}
590
591/**
592 * @brief Link the extensions definitions with the available extension plugins.
593 *
594 * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules
595 * are not connected with even available extension plugins.
596 *
597 * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)).
598 */
599static void
600lys_compile_extension_plugins(struct lysc_ext *extensions)
601{
602 unsigned int u;
603
604 LY_ARRAY_FOR(extensions, u) {
605 extensions[u].plugin = lyext_get_plugin(&extensions[u]);
606 }
607}
608
609LY_ERR
610lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
611 struct lysp_ext *extensions_p, struct lysc_ext **extensions)
612{
613 unsigned int offset = 0, u;
614 struct lysc_ctx context = {0};
615
616 assert(ctx_sc || ctx);
617
618 if (!ctx_sc) {
619 context.ctx = ctx;
620 context.mod = module;
Michal Vaskob541af02019-12-18 12:11:23 +0100621 context.mod_def = module;
Radek Krejci0935f412019-08-20 16:15:18 +0200622 context.path_len = 1;
623 context.path[0] = '/';
624 ctx_sc = &context;
625 }
626
627 if (!extensions_p) {
628 return LY_SUCCESS;
629 }
630 if (*extensions) {
631 offset = LY_ARRAY_SIZE(*extensions);
632 }
633
634 lysc_update_path(ctx_sc, NULL, "{extension}");
635 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM);
636 LY_ARRAY_FOR(extensions_p, u) {
637 lysc_update_path(ctx_sc, NULL, extensions_p[u].name);
638 LY_ARRAY_INCREMENT(*extensions);
639 COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name);
640 LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u]));
641 lysc_update_path(ctx_sc, NULL, NULL);
642 }
643 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100644
645 return LY_SUCCESS;
646}
647
Radek Krejcib56c7502019-02-13 14:19:54 +0100648/**
649 * @brief Compile information from the if-feature statement
650 * @param[in] ctx Compile context.
651 * @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 +0100652 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
653 * @return LY_ERR value.
654 */
Radek Krejci19a96102018-11-15 13:38:09 +0100655static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200656lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100657{
658 const char *c = *value;
659 int r, rc = EXIT_FAILURE;
660 int i, j, last_not, checkversion = 0;
661 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
662 uint8_t op;
663 struct iff_stack stack = {0, 0, NULL};
664 struct lysc_feature *f;
665
666 assert(c);
667
668 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
669 for (i = j = last_not = 0; c[i]; i++) {
670 if (c[i] == '(') {
671 j++;
672 checkversion = 1;
673 continue;
674 } else if (c[i] == ')') {
675 j--;
676 continue;
677 } else if (isspace(c[i])) {
678 checkversion = 1;
679 continue;
680 }
681
682 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 +0200683 int sp;
684 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
685 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
687 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
688 return LY_EVALID;
689 } else if (!isspace(c[i + r])) {
690 /* feature name starting with the not/and/or */
691 last_not = 0;
692 f_size++;
693 } else if (c[i] == 'n') { /* not operation */
694 if (last_not) {
695 /* double not */
696 expr_size = expr_size - 2;
697 last_not = 0;
698 } else {
699 last_not = 1;
700 }
701 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200702 if (f_exp != f_size) {
703 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
704 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
705 return LY_EVALID;
706 }
Radek Krejci19a96102018-11-15 13:38:09 +0100707 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200708
Radek Krejci19a96102018-11-15 13:38:09 +0100709 /* not a not operation */
710 last_not = 0;
711 }
712 i += r;
713 } else {
714 f_size++;
715 last_not = 0;
716 }
717 expr_size++;
718
719 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200720 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100721 i--;
722 break;
723 }
724 i++;
725 }
726 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200727 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100728 /* not matching count of ( and ) */
729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
730 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
731 return LY_EVALID;
732 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200733 if (f_exp != f_size) {
734 /* features do not match the needed arguments for the logical operations */
735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
736 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
737 "the required number of operands for the operations.", *value);
738 return LY_EVALID;
739 }
Radek Krejci19a96102018-11-15 13:38:09 +0100740
741 if (checkversion || expr_size > 1) {
742 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100743 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
745 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
746 return LY_EVALID;
747 }
748 }
749
750 /* allocate the memory */
751 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
752 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
753 stack.stack = malloc(expr_size * sizeof *stack.stack);
754 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
755
756 stack.size = expr_size;
757 f_size--; expr_size--; /* used as indexes from now */
758
759 for (i--; i >= 0; i--) {
760 if (c[i] == ')') {
761 /* push it on stack */
762 iff_stack_push(&stack, LYS_IFF_RP);
763 continue;
764 } else if (c[i] == '(') {
765 /* pop from the stack into result all operators until ) */
766 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
767 iff_setop(iff->expr, op, expr_size--);
768 }
769 continue;
770 } else if (isspace(c[i])) {
771 continue;
772 }
773
774 /* end of operator or operand -> find beginning and get what is it */
775 j = i + 1;
776 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
777 i--;
778 }
779 i++; /* go back by one step */
780
781 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
782 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
783 /* double not */
784 iff_stack_pop(&stack);
785 } else {
786 /* not has the highest priority, so do not pop from the stack
787 * as in case of AND and OR */
788 iff_stack_push(&stack, LYS_IFF_NOT);
789 }
790 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
791 /* as for OR - pop from the stack all operators with the same or higher
792 * priority and store them to the result, then push the AND to the stack */
793 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
794 op = iff_stack_pop(&stack);
795 iff_setop(iff->expr, op, expr_size--);
796 }
797 iff_stack_push(&stack, LYS_IFF_AND);
798 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
799 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
800 op = iff_stack_pop(&stack);
801 iff_setop(iff->expr, op, expr_size--);
802 }
803 iff_stack_push(&stack, LYS_IFF_OR);
804 } else {
805 /* feature name, length is j - i */
806
807 /* add it to the expression */
808 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
809
810 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100811 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
812 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
813 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
814 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100815 iff->features[f_size] = f;
816 LY_ARRAY_INCREMENT(iff->features);
817 f_size--;
818 }
819 }
820 while (stack.index) {
821 op = iff_stack_pop(&stack);
822 iff_setop(iff->expr, op, expr_size--);
823 }
824
825 if (++expr_size || ++f_size) {
826 /* not all expected operators and operands found */
827 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
828 "Invalid value \"%s\" of if-feature - processing error.", *value);
829 rc = LY_EINT;
830 } else {
831 rc = LY_SUCCESS;
832 }
833
834error:
835 /* cleanup */
836 iff_stack_clean(&stack);
837
838 return rc;
839}
840
Radek Krejcib56c7502019-02-13 14:19:54 +0100841/**
Michal Vasko175012e2019-11-06 15:49:14 +0100842 * @brief Get the XPath context node for the given schema node.
843 * @param[in] start The schema node where the XPath expression appears.
844 * @return The context node to evaluate XPath expression in given schema node.
845 * @return NULL in case the context node is the root node.
846 */
847static struct lysc_node *
848lysc_xpath_context(struct lysc_node *start)
849{
850 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
851 start = start->parent);
852 return start;
853}
854
855/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100856 * @brief Compile information from the when statement
857 * @param[in] ctx Compile context.
858 * @param[in] when_p The parsed when statement structure.
Michal Vasko175012e2019-11-06 15:49:14 +0100859 * @param[in] flags Flags of the node with the "when" defiition.
860 * @param[in] node Node that inherited the "when" definition, must be connected to parents.
Radek Krejcib56c7502019-02-13 14:19:54 +0100861 * @param[out] when Pointer where to store pointer to the created compiled when structure.
862 * @return LY_ERR value.
863 */
Radek Krejci19a96102018-11-15 13:38:09 +0100864static LY_ERR
Michal Vasko175012e2019-11-06 15:49:14 +0100865lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, uint16_t flags, struct lysc_node *node, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100866{
Radek Krejci19a96102018-11-15 13:38:09 +0100867 LY_ERR ret = LY_SUCCESS;
868
Radek Krejci00b874b2019-02-12 10:54:50 +0100869 *when = calloc(1, sizeof **when);
870 (*when)->refcount = 1;
871 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200872 (*when)->module = ctx->mod_def;
Michal Vasko175012e2019-11-06 15:49:14 +0100873 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +0100874 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
875 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
876 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200877 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Michal Vasko175012e2019-11-06 15:49:14 +0100878 (*when)->flags = flags & LYS_STATUS_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +0100879
880done:
881 return ret;
882}
883
Radek Krejcib56c7502019-02-13 14:19:54 +0100884/**
885 * @brief Compile information from the must statement
886 * @param[in] ctx Compile context.
887 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100888 * @param[in,out] must Prepared (empty) compiled must structure to fill.
889 * @return LY_ERR value.
890 */
Radek Krejci19a96102018-11-15 13:38:09 +0100891static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200892lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100893{
Radek Krejci19a96102018-11-15 13:38:09 +0100894 LY_ERR ret = LY_SUCCESS;
895
896 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
897 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200898 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100899 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
900 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100901 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
902 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200903 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100904
905done:
906 return ret;
907}
908
Radek Krejcib56c7502019-02-13 14:19:54 +0100909/**
910 * @brief Compile information from the import statement
911 * @param[in] ctx Compile context.
912 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100913 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
914 * @return LY_ERR value.
915 */
Radek Krejci19a96102018-11-15 13:38:09 +0100916static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200917lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100918{
Radek Krejci19a96102018-11-15 13:38:09 +0100919 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100920 LY_ERR ret = LY_SUCCESS;
921
922 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200923 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100924 imp->module = imp_p->module;
925
Radek Krejci7f2a5362018-11-28 13:05:37 +0100926 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
927 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100928 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100929 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100930 /* try to use filepath if present */
931 if (imp->module->filepath) {
932 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
933 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100934 if (mod != imp->module) {
935 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100936 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100937 mod = NULL;
938 }
939 }
940 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100941 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100942 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 +0100943 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100944 return LY_ENOTFOUND;
945 }
946 }
Radek Krejci19a96102018-11-15 13:38:09 +0100947 }
948
949done:
950 return ret;
951}
952
Radek Krejcib56c7502019-02-13 14:19:54 +0100953/**
954 * @brief Compile information from the identity statement
955 *
956 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
957 *
958 * @param[in] ctx Compile context.
959 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100960 * @param[in] idents List of so far compiled identities to check the name uniqueness.
961 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
962 * @return LY_ERR value.
963 */
Radek Krejci19a96102018-11-15 13:38:09 +0100964static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200965lys_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 +0100966{
967 unsigned int u;
968 LY_ERR ret = LY_SUCCESS;
969
Radek Krejci327de162019-06-14 12:52:07 +0200970 lysc_update_path(ctx, NULL, ident_p->name);
971
Radek Krejcid05cbd92018-12-05 14:26:40 +0100972 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100973 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200974 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
975 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200976 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200977 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100978 /* 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 +0200979 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100980 ident->flags = ident_p->flags;
981
Radek Krejci327de162019-06-14 12:52:07 +0200982 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100983done:
984 return ret;
985}
986
Radek Krejcib56c7502019-02-13 14:19:54 +0100987/**
988 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
989 *
990 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
991 *
992 * @param[in] ctx Compile context for logging.
993 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
994 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
995 * @return LY_SUCCESS if everything is ok.
996 * @return LY_EVALID if the identity is derived from itself.
997 */
Radek Krejci38222632019-02-12 16:55:05 +0100998static LY_ERR
999lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
1000{
1001 LY_ERR ret = LY_EVALID;
1002 unsigned int u, v;
1003 struct ly_set recursion = {0};
1004 struct lysc_ident *drv;
1005
1006 if (!derived) {
1007 return LY_SUCCESS;
1008 }
1009
1010 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
1011 if (ident == derived[u]) {
1012 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1013 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1014 goto cleanup;
1015 }
1016 ly_set_add(&recursion, derived[u], 0);
1017 }
1018
1019 for (v = 0; v < recursion.count; ++v) {
1020 drv = recursion.objs[v];
1021 if (!drv->derived) {
1022 continue;
1023 }
1024 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
1025 if (ident == drv->derived[u]) {
1026 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1027 "Identity \"%s\" is indirectly derived from itself.", ident->name);
1028 goto cleanup;
1029 }
1030 ly_set_add(&recursion, drv->derived[u], 0);
1031 }
1032 }
1033 ret = LY_SUCCESS;
1034
1035cleanup:
1036 ly_set_erase(&recursion, NULL);
1037 return ret;
1038}
1039
Radek Krejcia3045382018-11-22 14:30:31 +01001040/**
1041 * @brief Find and process the referenced base identities from another identity or identityref
1042 *
Radek Krejciaca74032019-06-04 08:53:06 +02001043 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +01001044 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
1045 * to distinguish these two use cases.
1046 *
1047 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1048 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
1049 * @param[in] ident Referencing identity to work with.
1050 * @param[in] bases Array of bases of identityref to fill in.
1051 * @return LY_ERR value.
1052 */
Radek Krejci19a96102018-11-15 13:38:09 +01001053static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +01001054lys_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 +01001055{
Radek Krejci555cb5b2018-11-16 14:54:33 +01001056 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001057 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +01001058 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001059 struct lysc_ident **idref;
1060
1061 assert(ident || bases);
1062
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001063 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001064 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1065 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1066 return LY_EVALID;
1067 }
1068
1069 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1070 s = strchr(bases_p[u], ':');
1071 if (s) {
1072 /* prefixed identity */
1073 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +01001074 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001075 } else {
1076 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +01001077 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001078 }
1079 if (!mod) {
1080 if (ident) {
1081 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1082 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1083 } else {
1084 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1085 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1086 }
1087 return LY_EVALID;
1088 }
1089 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001090 if (mod->compiled && mod->compiled->identities) {
1091 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1092 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001093 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001094 if (ident == &mod->compiled->identities[v]) {
1095 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1096 "Identity \"%s\" is derived from itself.", ident->name);
1097 return LY_EVALID;
1098 }
1099 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001100 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001101 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001102 *idref = ident;
1103 } else {
1104 /* we have match! store the found identity */
1105 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001106 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001107 }
1108 break;
1109 }
1110 }
1111 }
1112 if (!idref || !(*idref)) {
1113 if (ident) {
1114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1115 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1116 } else {
1117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1118 "Unable to find base (%s) of identityref.", bases_p[u]);
1119 }
1120 return LY_EVALID;
1121 }
1122 }
1123 return LY_SUCCESS;
1124}
1125
Radek Krejcia3045382018-11-22 14:30:31 +01001126/**
1127 * @brief For the given array of identities, set the backlinks from all their base identities.
1128 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1129 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1130 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1131 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1132 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001133static LY_ERR
1134lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1135{
1136 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +01001137
1138 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
1139 if (!idents_p[i].bases) {
1140 continue;
1141 }
Radek Krejci327de162019-06-14 12:52:07 +02001142 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001143 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001144 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001145 }
1146 return LY_SUCCESS;
1147}
1148
Radek Krejci0af46292019-01-11 16:02:31 +01001149LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001150lys_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 +01001151{
1152 unsigned int offset = 0, u;
1153 struct lysc_ctx context = {0};
1154
Radek Krejci327de162019-06-14 12:52:07 +02001155 assert(ctx_sc || ctx);
1156
1157 if (!ctx_sc) {
1158 context.ctx = ctx;
1159 context.mod = module;
1160 context.path_len = 1;
1161 context.path[0] = '/';
1162 ctx_sc = &context;
1163 }
Radek Krejci0af46292019-01-11 16:02:31 +01001164
1165 if (!features_p) {
1166 return LY_SUCCESS;
1167 }
1168 if (*features) {
1169 offset = LY_ARRAY_SIZE(*features);
1170 }
1171
Radek Krejci327de162019-06-14 12:52:07 +02001172 lysc_update_path(ctx_sc, NULL, "{feature}");
1173 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001174 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001175 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1176
Radek Krejci0af46292019-01-11 16:02:31 +01001177 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +02001178 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
1179 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1180 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1181 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001182 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001183 (*features)[offset + u].module = ctx_sc->mod;
1184
1185 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001186 }
Radek Krejci327de162019-06-14 12:52:07 +02001187 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001188
1189 return LY_SUCCESS;
1190}
1191
Radek Krejcia3045382018-11-22 14:30:31 +01001192/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001193 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001194 *
1195 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1196 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001197 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001198 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1199 * being currently processed).
1200 * @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 +01001201 * @return LY_SUCCESS if everything is ok.
1202 * @return LY_EVALID if the feature references indirectly itself.
1203 */
1204static LY_ERR
1205lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1206{
1207 LY_ERR ret = LY_EVALID;
1208 unsigned int u, v;
1209 struct ly_set recursion = {0};
1210 struct lysc_feature *drv;
1211
1212 if (!depfeatures) {
1213 return LY_SUCCESS;
1214 }
1215
1216 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1217 if (feature == depfeatures[u]) {
1218 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1219 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1220 goto cleanup;
1221 }
1222 ly_set_add(&recursion, depfeatures[u], 0);
1223 }
1224
1225 for (v = 0; v < recursion.count; ++v) {
1226 drv = recursion.objs[v];
1227 if (!drv->depfeatures) {
1228 continue;
1229 }
1230 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1231 if (feature == drv->depfeatures[u]) {
1232 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1233 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1234 goto cleanup;
1235 }
1236 ly_set_add(&recursion, drv->depfeatures[u], 0);
1237 }
1238 }
1239 ret = LY_SUCCESS;
1240
1241cleanup:
1242 ly_set_erase(&recursion, NULL);
1243 return ret;
1244}
1245
1246/**
Radek Krejci0af46292019-01-11 16:02:31 +01001247 * @brief Create pre-compiled features array.
1248 *
1249 * See lys_feature_precompile() for more details.
1250 *
Radek Krejcia3045382018-11-22 14:30:31 +01001251 * @param[in] ctx Compile context.
1252 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001253 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001254 * @return LY_ERR value.
1255 */
Radek Krejci19a96102018-11-15 13:38:09 +01001256static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001257lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001258{
Radek Krejci0af46292019-01-11 16:02:31 +01001259 unsigned int u, v, x;
1260 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001261 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001262
Radek Krejci327de162019-06-14 12:52:07 +02001263
Radek Krejci0af46292019-01-11 16:02:31 +01001264 /* find the preprecompiled feature */
1265 LY_ARRAY_FOR(features, x) {
1266 if (strcmp(features[x].name, feature_p->name)) {
1267 continue;
1268 }
1269 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001270 lysc_update_path(ctx, NULL, "{feature}");
1271 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001272
Radek Krejci0af46292019-01-11 16:02:31 +01001273 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001274 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001275 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001276 if (feature->iffeatures) {
1277 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1278 if (feature->iffeatures[u].features) {
1279 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001280 /* check for circular dependency - direct reference first,... */
1281 if (feature == feature->iffeatures[u].features[v]) {
1282 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1283 "Feature \"%s\" is referenced from itself.", feature->name);
1284 return LY_EVALID;
1285 }
1286 /* ... and indirect circular reference */
1287 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1288
Radek Krejci0af46292019-01-11 16:02:31 +01001289 /* add itself into the dependants list */
1290 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1291 *df = feature;
1292 }
Radek Krejci19a96102018-11-15 13:38:09 +01001293 }
Radek Krejci19a96102018-11-15 13:38:09 +01001294 }
1295 }
Radek Krejci327de162019-06-14 12:52:07 +02001296 lysc_update_path(ctx, NULL, NULL);
1297 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001298 done:
1299 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001300 }
Radek Krejci0af46292019-01-11 16:02:31 +01001301
1302 LOGINT(ctx->ctx);
1303 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001304}
1305
Radek Krejcib56c7502019-02-13 14:19:54 +01001306/**
1307 * @brief Revert compiled list of features back to the precompiled state.
1308 *
1309 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1310 * The features are supposed to be stored again as off_features in ::lys_module structure.
1311 *
1312 * @param[in] ctx Compilation context.
1313 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1314 * and supposed to hold the off_features list.
1315 */
Radek Krejci95710c92019-02-11 15:49:55 +01001316static void
1317lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1318{
1319 unsigned int u, v;
1320
1321 /* keep the off_features list until the complete lys_module is freed */
1322 mod->off_features = mod->compiled->features;
1323 mod->compiled->features = NULL;
1324
1325 /* in the off_features list, remove all the parts (from finished compiling process)
1326 * which may points into the data being freed here */
1327 LY_ARRAY_FOR(mod->off_features, u) {
1328 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1329 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1330 }
1331 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1332 mod->off_features[u].iffeatures = NULL;
1333
1334 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1335 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1336 }
1337 LY_ARRAY_FREE(mod->off_features[u].exts);
1338 mod->off_features[u].exts = NULL;
1339 }
1340}
1341
Radek Krejcia3045382018-11-22 14:30:31 +01001342/**
1343 * @brief Validate and normalize numeric value from a range definition.
1344 * @param[in] ctx Compile context.
1345 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1346 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1347 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1348 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1349 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1350 * @param[in] value String value of the range boundary.
1351 * @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.
1352 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1353 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1354 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001355LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001356range_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 +01001357{
Radek Krejci6cba4292018-11-15 17:33:29 +01001358 size_t fraction = 0, size;
1359
Radek Krejci19a96102018-11-15 13:38:09 +01001360 *len = 0;
1361
1362 assert(value);
1363 /* parse value */
1364 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1365 return LY_EVALID;
1366 }
1367
1368 if ((value[*len] == '-') || (value[*len] == '+')) {
1369 ++(*len);
1370 }
1371
1372 while (isdigit(value[*len])) {
1373 ++(*len);
1374 }
1375
1376 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001377 if (basetype == LY_TYPE_DEC64) {
1378 goto decimal;
1379 } else {
1380 *valcopy = strndup(value, *len);
1381 return LY_SUCCESS;
1382 }
Radek Krejci19a96102018-11-15 13:38:09 +01001383 }
1384 fraction = *len;
1385
1386 ++(*len);
1387 while (isdigit(value[*len])) {
1388 ++(*len);
1389 }
1390
Radek Krejci6cba4292018-11-15 17:33:29 +01001391 if (basetype == LY_TYPE_DEC64) {
1392decimal:
1393 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001394 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001395 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1396 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1397 *len, value, frdigits);
1398 return LY_EINVAL;
1399 }
1400 if (fraction) {
1401 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1402 } else {
1403 size = (*len) + frdigits + 1;
1404 }
1405 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001406 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1407
Radek Krejci6cba4292018-11-15 17:33:29 +01001408 (*valcopy)[size - 1] = '\0';
1409 if (fraction) {
1410 memcpy(&(*valcopy)[0], &value[0], fraction);
1411 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1412 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1413 } else {
1414 memcpy(&(*valcopy)[0], &value[0], *len);
1415 memset(&(*valcopy)[*len], '0', frdigits);
1416 }
Radek Krejci19a96102018-11-15 13:38:09 +01001417 }
1418 return LY_SUCCESS;
1419}
1420
Radek Krejcia3045382018-11-22 14:30:31 +01001421/**
1422 * @brief Check that values in range are in ascendant order.
1423 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001424 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1425 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001426 * @param[in] value Current value to check.
1427 * @param[in] prev_value The last seen value.
1428 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1429 */
Radek Krejci19a96102018-11-15 13:38:09 +01001430static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001431range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001432{
1433 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001434 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001435 return LY_EEXIST;
1436 }
1437 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001438 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001439 return LY_EEXIST;
1440 }
1441 }
1442 return LY_SUCCESS;
1443}
1444
Radek Krejcia3045382018-11-22 14:30:31 +01001445/**
1446 * @brief Set min/max value of the range part.
1447 * @param[in] ctx Compile context.
1448 * @param[in] part Range part structure to fill.
1449 * @param[in] max Flag to distinguish if storing min or max value.
1450 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1451 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1452 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1453 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1454 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001455 * @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 +01001456 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1457 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1458 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1459 * frdigits value), LY_EMEM.
1460 */
Radek Krejci19a96102018-11-15 13:38:09 +01001461static LY_ERR
1462range_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 +01001463 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001464{
1465 LY_ERR ret = LY_SUCCESS;
1466 char *valcopy = NULL;
1467 size_t len;
1468
1469 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001470 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001471 LY_CHECK_GOTO(ret, finalize);
1472 }
1473 if (!valcopy && base_range) {
1474 if (max) {
1475 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1476 } else {
1477 part->min_64 = base_range->parts[0].min_64;
1478 }
1479 if (!first) {
1480 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1481 }
1482 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001483 }
1484
1485 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001486 case LY_TYPE_INT8: /* range */
1487 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001488 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 +01001489 } else if (max) {
1490 part->max_64 = INT64_C(127);
1491 } else {
1492 part->min_64 = INT64_C(-128);
1493 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001494 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001495 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001496 }
1497 break;
1498 case LY_TYPE_INT16: /* range */
1499 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001500 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 +01001501 } else if (max) {
1502 part->max_64 = INT64_C(32767);
1503 } else {
1504 part->min_64 = INT64_C(-32768);
1505 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001506 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001507 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001508 }
1509 break;
1510 case LY_TYPE_INT32: /* range */
1511 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001512 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 +01001513 } else if (max) {
1514 part->max_64 = INT64_C(2147483647);
1515 } else {
1516 part->min_64 = INT64_C(-2147483648);
1517 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001518 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001519 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001520 }
1521 break;
1522 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001523 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001524 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001525 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001526 max ? &part->max_64 : &part->min_64);
1527 } else if (max) {
1528 part->max_64 = INT64_C(9223372036854775807);
1529 } else {
1530 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1531 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001532 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001533 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001534 }
1535 break;
1536 case LY_TYPE_UINT8: /* range */
1537 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001538 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 +01001539 } else if (max) {
1540 part->max_u64 = UINT64_C(255);
1541 } else {
1542 part->min_u64 = UINT64_C(0);
1543 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001544 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001545 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001546 }
1547 break;
1548 case LY_TYPE_UINT16: /* range */
1549 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001550 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 +01001551 } else if (max) {
1552 part->max_u64 = UINT64_C(65535);
1553 } else {
1554 part->min_u64 = UINT64_C(0);
1555 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001556 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001557 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001558 }
1559 break;
1560 case LY_TYPE_UINT32: /* range */
1561 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001562 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 +01001563 } else if (max) {
1564 part->max_u64 = UINT64_C(4294967295);
1565 } else {
1566 part->min_u64 = UINT64_C(0);
1567 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001568 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001569 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001570 }
1571 break;
1572 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001573 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001574 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001575 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001576 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 +01001577 } else if (max) {
1578 part->max_u64 = UINT64_C(18446744073709551615);
1579 } else {
1580 part->min_u64 = UINT64_C(0);
1581 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001582 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001583 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001584 }
1585 break;
1586 default:
1587 LOGINT(ctx->ctx);
1588 ret = LY_EINT;
1589 }
1590
Radek Krejci5969f272018-11-23 10:03:58 +01001591finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001592 if (ret == LY_EDENIED) {
1593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1594 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1595 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1596 } else if (ret == LY_EVALID) {
1597 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1598 "Invalid %s restriction - invalid value \"%s\".",
1599 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1600 } else if (ret == LY_EEXIST) {
1601 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1602 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001603 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001604 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001605 } else if (!ret && value) {
1606 *value = *value + len;
1607 }
1608 free(valcopy);
1609 return ret;
1610}
1611
Radek Krejcia3045382018-11-22 14:30:31 +01001612/**
1613 * @brief Compile the parsed range restriction.
1614 * @param[in] ctx Compile context.
1615 * @param[in] range_p Parsed range structure to compile.
1616 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1617 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1618 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1619 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1620 * range restriction must be more restrictive than the base_range.
1621 * @param[in,out] range Pointer to the created current range structure.
1622 * @return LY_ERR value.
1623 */
Radek Krejci19a96102018-11-15 13:38:09 +01001624static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001625lys_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 +01001626 struct lysc_range *base_range, struct lysc_range **range)
1627{
1628 LY_ERR ret = LY_EVALID;
1629 const char *expr;
1630 struct lysc_range_part *parts = NULL, *part;
1631 int range_expected = 0, uns;
1632 unsigned int parts_done = 0, u, v;
1633
1634 assert(range);
1635 assert(range_p);
1636
1637 expr = range_p->arg;
1638 while(1) {
1639 if (isspace(*expr)) {
1640 ++expr;
1641 } else if (*expr == '\0') {
1642 if (range_expected) {
1643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1644 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1645 length_restr ? "length" : "range", range_p->arg);
1646 goto cleanup;
1647 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1648 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1649 "Invalid %s restriction - unexpected end of the expression (%s).",
1650 length_restr ? "length" : "range", range_p->arg);
1651 goto cleanup;
1652 }
1653 parts_done++;
1654 break;
1655 } else if (!strncmp(expr, "min", 3)) {
1656 if (parts) {
1657 /* min cannot be used elsewhere than in the first part */
1658 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1659 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1660 expr - range_p->arg, range_p->arg);
1661 goto cleanup;
1662 }
1663 expr += 3;
1664
1665 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001666 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 +01001667 part->max_64 = part->min_64;
1668 } else if (*expr == '|') {
1669 if (!parts || range_expected) {
1670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1671 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1672 goto cleanup;
1673 }
1674 expr++;
1675 parts_done++;
1676 /* process next part of the expression */
1677 } else if (!strncmp(expr, "..", 2)) {
1678 expr += 2;
1679 while (isspace(*expr)) {
1680 expr++;
1681 }
1682 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1684 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1685 goto cleanup;
1686 }
1687 /* continue expecting the upper boundary */
1688 range_expected = 1;
1689 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1690 /* number */
1691 if (range_expected) {
1692 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001693 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 +01001694 range_expected = 0;
1695 } else {
1696 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1697 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 +01001698 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001699 part->max_64 = part->min_64;
1700 }
1701
1702 /* continue with possible another expression part */
1703 } else if (!strncmp(expr, "max", 3)) {
1704 expr += 3;
1705 while (isspace(*expr)) {
1706 expr++;
1707 }
1708 if (*expr != '\0') {
1709 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1710 length_restr ? "length" : "range", expr);
1711 goto cleanup;
1712 }
1713 if (range_expected) {
1714 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001715 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 +01001716 range_expected = 0;
1717 } else {
1718 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1719 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 +01001720 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001721 part->min_64 = part->max_64;
1722 }
1723 } else {
1724 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1725 length_restr ? "length" : "range", expr);
1726 goto cleanup;
1727 }
1728 }
1729
1730 /* check with the previous range/length restriction */
1731 if (base_range) {
1732 switch (basetype) {
1733 case LY_TYPE_BINARY:
1734 case LY_TYPE_UINT8:
1735 case LY_TYPE_UINT16:
1736 case LY_TYPE_UINT32:
1737 case LY_TYPE_UINT64:
1738 case LY_TYPE_STRING:
1739 uns = 1;
1740 break;
1741 case LY_TYPE_DEC64:
1742 case LY_TYPE_INT8:
1743 case LY_TYPE_INT16:
1744 case LY_TYPE_INT32:
1745 case LY_TYPE_INT64:
1746 uns = 0;
1747 break;
1748 default:
1749 LOGINT(ctx->ctx);
1750 ret = LY_EINT;
1751 goto cleanup;
1752 }
1753 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1754 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1755 goto baseerror;
1756 }
1757 /* current lower bound is not lower than the base */
1758 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1759 /* base has single value */
1760 if (base_range->parts[v].min_64 == parts[u].min_64) {
1761 /* both lower bounds are the same */
1762 if (parts[u].min_64 != parts[u].max_64) {
1763 /* current continues with a range */
1764 goto baseerror;
1765 } else {
1766 /* equal single values, move both forward */
1767 ++v;
1768 continue;
1769 }
1770 } else {
1771 /* base is single value lower than current range, so the
1772 * value from base range is removed in the current,
1773 * move only base and repeat checking */
1774 ++v;
1775 --u;
1776 continue;
1777 }
1778 } else {
1779 /* base is the range */
1780 if (parts[u].min_64 == parts[u].max_64) {
1781 /* current is a single value */
1782 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1783 /* current is behind the base range, so base range is omitted,
1784 * move the base and keep the current for further check */
1785 ++v;
1786 --u;
1787 } /* else it is within the base range, so move the current, but keep the base */
1788 continue;
1789 } else {
1790 /* both are ranges - check the higher bound, the lower was already checked */
1791 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1792 /* higher bound is higher than the current higher bound */
1793 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1794 /* but the current lower bound is also higher, so the base range is omitted,
1795 * continue with the same current, but move the base */
1796 --u;
1797 ++v;
1798 continue;
1799 }
1800 /* current range starts within the base range but end behind it */
1801 goto baseerror;
1802 } else {
1803 /* current range is smaller than the base,
1804 * move current, but stay with the base */
1805 continue;
1806 }
1807 }
1808 }
1809 }
1810 if (u != parts_done) {
1811baseerror:
1812 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1813 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1814 length_restr ? "length" : "range", range_p->arg);
1815 goto cleanup;
1816 }
1817 }
1818
1819 if (!(*range)) {
1820 *range = calloc(1, sizeof **range);
1821 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1822 }
1823
Radek Krejcic8b31002019-01-08 10:24:45 +01001824 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001825 if (range_p->eapptag) {
1826 lydict_remove(ctx->ctx, (*range)->eapptag);
1827 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1828 }
1829 if (range_p->emsg) {
1830 lydict_remove(ctx->ctx, (*range)->emsg);
1831 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1832 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001833 if (range_p->dsc) {
1834 lydict_remove(ctx->ctx, (*range)->dsc);
1835 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1836 }
1837 if (range_p->ref) {
1838 lydict_remove(ctx->ctx, (*range)->ref);
1839 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1840 }
Radek Krejci19a96102018-11-15 13:38:09 +01001841 /* extensions are taken only from the last range by the caller */
1842
1843 (*range)->parts = parts;
1844 parts = NULL;
1845 ret = LY_SUCCESS;
1846cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001847 LY_ARRAY_FREE(parts);
1848
1849 return ret;
1850}
1851
1852/**
1853 * @brief Checks pattern syntax.
1854 *
Michal Vasko03ff5a72019-09-11 13:49:33 +02001855 * @param[in] ctx Context.
1856 * @param[in] log_path Path for logging errors.
Radek Krejci19a96102018-11-15 13:38:09 +01001857 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001858 * @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 +01001859 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001860 */
Michal Vasko03ff5a72019-09-11 13:49:33 +02001861LY_ERR
1862lys_compile_type_pattern_check(struct ly_ctx *ctx, const char *log_path, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001863{
Radek Krejci54579462019-04-30 12:47:06 +02001864 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001865 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001866 int err_code;
1867 const char *orig_ptr;
1868 PCRE2_SIZE err_offset;
1869 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001870#define URANGE_LEN 19
1871 char *ublock2urange[][2] = {
1872 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1873 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1874 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1875 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1876 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1877 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1878 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1879 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1880 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1881 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1882 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1883 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1884 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1885 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1886 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1887 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1888 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1889 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1890 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1891 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1892 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1893 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1894 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1895 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1896 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1897 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1898 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1899 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1900 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1901 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1902 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1903 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1904 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1905 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1906 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1907 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1908 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1909 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1910 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1911 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1912 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1913 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1914 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1915 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1916 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1917 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1918 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1919 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1920 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1921 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1922 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1923 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1924 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1925 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1926 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1927 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1928 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1929 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1930 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1931 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1932 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1933 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1934 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1935 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1936 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1937 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1938 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1939 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1940 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1941 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1942 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1943 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1944 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1945 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1946 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1947 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1948 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1949 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1950 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1951 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1952 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1953 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1954 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1955 {NULL, NULL}
1956 };
1957
1958 /* adjust the expression to a Perl equivalent
1959 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1960
Michal Vasko03ff5a72019-09-11 13:49:33 +02001961 /* we need to replace all "$" and "^" with "\$" and "\^", count them now */
Radek Krejci19a96102018-11-15 13:38:09 +01001962 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1963
1964 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
Michal Vasko03ff5a72019-09-11 13:49:33 +02001965 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01001966 perl_regex[0] = '\0';
1967
1968 ptr = perl_regex;
1969
Radek Krejci19a96102018-11-15 13:38:09 +01001970 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1971 if (orig_ptr[0] == '$') {
1972 ptr += sprintf(ptr, "\\$");
1973 } else if (orig_ptr[0] == '^') {
1974 ptr += sprintf(ptr, "\\^");
1975 } else {
1976 ptr[0] = orig_ptr[0];
1977 ++ptr;
1978 }
1979 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001980 ptr[0] = '\0';
1981 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001982
1983 /* substitute Unicode Character Blocks with exact Character Ranges */
1984 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1985 start = ptr - perl_regex;
1986
1987 ptr = strchr(ptr, '}');
1988 if (!ptr) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02001989 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01001990 pattern, perl_regex + start + 2, "unterminated character property");
1991 free(perl_regex);
1992 return LY_EVALID;
1993 }
1994 end = (ptr - perl_regex) + 1;
1995
1996 /* need more space */
1997 if (end - start < URANGE_LEN) {
1998 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
Michal Vasko03ff5a72019-09-11 13:49:33 +02001999 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx); free(perl_regex), LY_EMEM);
Radek Krejci19a96102018-11-15 13:38:09 +01002000 }
2001
2002 /* find our range */
2003 for (idx = 0; ublock2urange[idx][0]; ++idx) {
2004 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
2005 break;
2006 }
2007 }
2008 if (!ublock2urange[idx][0]) {
Michal Vasko03ff5a72019-09-11 13:49:33 +02002009 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP,
Radek Krejci19a96102018-11-15 13:38:09 +01002010 pattern, perl_regex + start + 5, "unknown block name");
2011 free(perl_regex);
2012 return LY_EVALID;
2013 }
2014
2015 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
2016 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
2017 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
2018 ++count;
2019 }
2020 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
2021 --count;
2022 }
2023 }
2024 if (count) {
2025 /* skip brackets */
2026 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
2027 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
2028 } else {
2029 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
2030 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
2031 }
2032 }
2033
2034 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02002035 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
2036 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02002037 &err_code, &err_offset, NULL);
2038 if (!code_local) {
2039 PCRE2_UCHAR err_msg[256] = {0};
2040 pcre2_get_error_message(err_code, err_msg, 256);
Michal Vasko03ff5a72019-09-11 13:49:33 +02002041 LOGVAL(ctx, LY_VLOG_STR, log_path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
Radek Krejci19a96102018-11-15 13:38:09 +01002042 free(perl_regex);
2043 return LY_EVALID;
2044 }
2045 free(perl_regex);
2046
Radek Krejci54579462019-04-30 12:47:06 +02002047 if (code) {
2048 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01002049 } else {
Radek Krejci54579462019-04-30 12:47:06 +02002050 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01002051 }
2052
2053 return LY_SUCCESS;
2054
2055#undef URANGE_LEN
2056}
2057
Radek Krejcia3045382018-11-22 14:30:31 +01002058/**
2059 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
2060 * @param[in] ctx Compile context.
2061 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002062 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
2063 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
2064 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2065 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2066 */
Radek Krejci19a96102018-11-15 13:38:09 +01002067static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002068lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002069 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2070{
2071 struct lysc_pattern **pattern;
Radek Krejci0935f412019-08-20 16:15:18 +02002072 unsigned int u;
Radek Krejci19a96102018-11-15 13:38:09 +01002073 LY_ERR ret = LY_SUCCESS;
2074
2075 /* first, copy the patterns from the base type */
2076 if (base_patterns) {
2077 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2078 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2079 }
2080
2081 LY_ARRAY_FOR(patterns_p, u) {
2082 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2083 *pattern = calloc(1, sizeof **pattern);
2084 ++(*pattern)->refcount;
2085
Michal Vasko03ff5a72019-09-11 13:49:33 +02002086 ret = lys_compile_type_pattern_check(ctx->ctx, ctx->path, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002087 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002088
2089 if (patterns_p[u].arg[0] == 0x15) {
2090 (*pattern)->inverted = 1;
2091 }
Radek Krejci54579462019-04-30 12:47:06 +02002092 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002093 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2094 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002095 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2096 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002097 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002098 }
2099done:
2100 return ret;
2101}
2102
Radek Krejcia3045382018-11-22 14:30:31 +01002103/**
2104 * @brief map of the possible restrictions combination for the specific built-in type.
2105 */
Radek Krejci19a96102018-11-15 13:38:09 +01002106static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2107 0 /* LY_TYPE_UNKNOWN */,
2108 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002109 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2110 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2111 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2112 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2113 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002114 LYS_SET_BIT /* LY_TYPE_BITS */,
2115 0 /* LY_TYPE_BOOL */,
2116 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2117 0 /* LY_TYPE_EMPTY */,
2118 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2119 LYS_SET_BASE /* LY_TYPE_IDENT */,
2120 LYS_SET_REQINST /* LY_TYPE_INST */,
2121 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002122 LYS_SET_TYPE /* LY_TYPE_UNION */,
2123 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002124 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002125 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002126 LYS_SET_RANGE /* LY_TYPE_INT64 */
2127};
2128
2129/**
2130 * @brief stringification of the YANG built-in data types
2131 */
2132const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2133 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2134 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002135};
2136
Radek Krejcia3045382018-11-22 14:30:31 +01002137/**
2138 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2139 * @param[in] ctx Compile context.
2140 * @param[in] enums_p Array of the parsed enum structures to compile.
2141 * @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 +01002142 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2143 * @param[out] enums Newly created array of the compiled enums information for the current type.
2144 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2145 */
Radek Krejci19a96102018-11-15 13:38:09 +01002146static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002147lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002148 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002149{
2150 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02002151 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002152 int32_t value = 0;
2153 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002154 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002155
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002156 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002157 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2158 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2159 return LY_EVALID;
2160 }
2161
2162 LY_ARRAY_FOR(enums_p, u) {
2163 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2164 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002165 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2166 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002167 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002168 if (base_enums) {
2169 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2170 LY_ARRAY_FOR(base_enums, v) {
2171 if (!strcmp(e->name, base_enums[v].name)) {
2172 break;
2173 }
2174 }
2175 if (v == LY_ARRAY_SIZE(base_enums)) {
2176 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2177 "Invalid %s - derived type adds new item \"%s\".",
2178 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2179 return LY_EVALID;
2180 }
2181 match = v;
2182 }
2183
2184 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002185 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002186 if (enums_p[u].flags & LYS_SET_VALUE) {
2187 e->value = (int32_t)enums_p[u].value;
2188 if (!u || e->value >= value) {
2189 value = e->value + 1;
2190 }
2191 /* check collision with other values */
2192 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2193 if (e->value == (*enums)[v].value) {
2194 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2195 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2196 e->value, e->name, (*enums)[v].name);
2197 return LY_EVALID;
2198 }
2199 }
2200 } else if (base_enums) {
2201 /* inherit the assigned value */
2202 e->value = base_enums[match].value;
2203 if (!u || e->value >= value) {
2204 value = e->value + 1;
2205 }
2206 } else {
2207 /* assign value automatically */
2208 if (u && value == INT32_MIN) {
2209 /* counter overflow */
2210 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2211 "Invalid enumeration - it is not possible to auto-assign enum value for "
2212 "\"%s\" since the highest value is already 2147483647.", e->name);
2213 return LY_EVALID;
2214 }
2215 e->value = value++;
2216 }
2217 } else { /* LY_TYPE_BITS */
2218 if (enums_p[u].flags & LYS_SET_VALUE) {
2219 e->value = (int32_t)enums_p[u].value;
2220 if (!u || (uint32_t)e->value >= position) {
2221 position = (uint32_t)e->value + 1;
2222 }
2223 /* check collision with other values */
2224 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2225 if (e->value == (*enums)[v].value) {
2226 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2227 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2228 (uint32_t)e->value, e->name, (*enums)[v].name);
2229 return LY_EVALID;
2230 }
2231 }
2232 } else if (base_enums) {
2233 /* inherit the assigned value */
2234 e->value = base_enums[match].value;
2235 if (!u || (uint32_t)e->value >= position) {
2236 position = (uint32_t)e->value + 1;
2237 }
2238 } else {
2239 /* assign value automatically */
2240 if (u && position == 0) {
2241 /* counter overflow */
2242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2243 "Invalid bits - it is not possible to auto-assign bit position for "
2244 "\"%s\" since the highest value is already 4294967295.", e->name);
2245 return LY_EVALID;
2246 }
2247 e->value = position++;
2248 }
2249 }
2250
2251 if (base_enums) {
2252 /* the assigned values must not change from the derived type */
2253 if (e->value != base_enums[match].value) {
2254 if (basetype == LY_TYPE_ENUM) {
2255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2256 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2257 e->name, base_enums[match].value, e->value);
2258 } else {
2259 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2260 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2261 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2262 }
2263 return LY_EVALID;
2264 }
2265 }
2266
Radek Krejciec4da802019-05-02 13:02:41 +02002267 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002268 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 +01002269
2270 if (basetype == LY_TYPE_BITS) {
2271 /* keep bits ordered by position */
2272 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2273 if (v != u) {
2274 memcpy(&storage, e, sizeof *e);
2275 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2276 memcpy(&(*enums)[v], &storage, sizeof storage);
2277 }
2278 }
2279 }
2280
2281done:
2282 return ret;
2283}
2284
Radek Krejcia3045382018-11-22 14:30:31 +01002285#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2286 for ((NODE) = (NODE)->parent; \
Michal Vasko03ff5a72019-09-11 13:49:33 +02002287 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_NOTIF | LYS_ACTION)); \
Radek Krejcia3045382018-11-22 14:30:31 +01002288 (NODE) = (NODE)->parent); \
2289 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2291 TERM; \
2292 }
2293
2294/**
2295 * @brief Validate the predicate(s) from the leafref path.
2296 * @param[in] ctx Compile context
2297 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2298 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002299 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002300 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002301 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002302 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2303 */
2304static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002305lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002306 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002307{
2308 LY_ERR ret = LY_EVALID;
2309 const struct lys_module *mod;
2310 const struct lysc_node *src_node, *dst_node;
2311 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2312 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002313 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002314 const char *start, *end, *pke_end;
2315 struct ly_set keys = {0};
2316 int i;
2317
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002318 assert(path_context);
2319
Radek Krejcia3045382018-11-22 14:30:31 +01002320 while (**predicate == '[') {
2321 start = (*predicate)++;
2322
2323 while (isspace(**predicate)) {
2324 ++(*predicate);
2325 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002326 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002327 while (isspace(**predicate)) {
2328 ++(*predicate);
2329 }
2330 if (**predicate != '=') {
2331 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002332 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2333 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002334 goto cleanup;
2335 }
2336 ++(*predicate);
2337 while (isspace(**predicate)) {
2338 ++(*predicate);
2339 }
2340
2341 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2343 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2344 goto cleanup;
2345 }
2346 --pke_end;
2347 while (isspace(*pke_end)) {
2348 --pke_end;
2349 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002350 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002351 /* localize path-key-expr */
2352 pke_start = path_key_expr = *predicate;
2353 /* move after the current predicate */
2354 *predicate = end + 1;
2355
2356 /* source (must be leaf or leaf-list) */
2357 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002358 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002359 if (!mod) {
2360 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2361 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002362 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002363 goto cleanup;
2364 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002365 if (!mod->implemented) {
2366 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002367 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002368 }
Radek Krejcia3045382018-11-22 14:30:31 +01002369 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002370 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002371 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002372 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002373 if (!(context_node->flags & LYS_KEYLESS)) {
2374 struct lysc_node *key;
2375 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02002376 if (!ly_strncmp(key->name, src, src_len)) {
Radek Krejci0fe9b512019-07-26 17:51:05 +02002377 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002378 break;
2379 }
2380 }
2381 }
Radek Krejcia3045382018-11-22 14:30:31 +01002382 if (!src_node) {
2383 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002384 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002385 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002386 goto cleanup;
2387 }
Radek Krejcia3045382018-11-22 14:30:31 +01002388
2389 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002390 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002391 i = ly_set_add(&keys, (void*)src_node, 0);
2392 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002393 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002394 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002395 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002396 *predicate - start, start, src_node->name);
2397 goto cleanup;
2398 }
2399
2400 /* destination */
2401 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002402 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002403
2404 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002405 if (strncmp(path_key_expr, "current", 7)) {
2406error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002407 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2408 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2409 *predicate - start, start);
2410 goto cleanup;
2411 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002412 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2413 if (*path_key_expr != '(') {
2414 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002415 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002416 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2417 if (*path_key_expr != ')') {
2418 goto error_current_function_invocation;
2419 }
2420 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002421
2422 if (*path_key_expr != '/') {
2423 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2424 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2425 *predicate - start, start);
2426 goto cleanup;
2427 }
2428 ++path_key_expr;
2429 while (isspace(*path_key_expr)) {
2430 ++path_key_expr;
2431 }
2432
2433 /* rel-path-keyexpr:
2434 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2435 while (!strncmp(path_key_expr, "..", 2)) {
2436 ++dest_parent_times;
2437 path_key_expr += 2;
2438 while (isspace(*path_key_expr)) {
2439 ++path_key_expr;
2440 }
2441 if (*path_key_expr != '/') {
2442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2443 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2444 *predicate - start, start);
2445 goto cleanup;
2446 }
2447 ++path_key_expr;
2448 while (isspace(*path_key_expr)) {
2449 ++path_key_expr;
2450 }
2451
2452 /* path is supposed to be evaluated in data tree, so we have to skip
2453 * all schema nodes that cannot be instantiated in data tree */
2454 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2455 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2456 *predicate - start, start);
2457 }
2458 if (!dest_parent_times) {
2459 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2460 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2461 *predicate - start, start);
2462 goto cleanup;
2463 }
2464 if (path_key_expr == pke_end) {
2465 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2466 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2467 *predicate - start, start);
2468 goto cleanup;
2469 }
2470
2471 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002472 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002473 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002474 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002475 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2476 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002477 goto cleanup;
2478 }
2479
2480 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002481 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002482 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002483 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002484 }
2485 if (!mod) {
2486 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002487 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002488 *predicate - start, start, dst_len, dst);
2489 goto cleanup;
2490 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002491 if (!mod->implemented) {
2492 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002493 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002494 }
Radek Krejcia3045382018-11-22 14:30:31 +01002495
2496 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2497 if (!dst_node) {
2498 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002499 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002500 *predicate - start, start, path_key_expr - pke_start, pke_start);
2501 goto cleanup;
2502 }
2503 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002504 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 +01002505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002506 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002507 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2508 goto cleanup;
2509 }
2510 }
2511
2512 ret = LY_SUCCESS;
2513cleanup:
2514 ly_set_erase(&keys, NULL);
2515 return ret;
2516}
2517
2518/**
2519 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2520 *
2521 * path-arg = absolute-path / relative-path
2522 * absolute-path = 1*("/" (node-identifier *path-predicate))
2523 * relative-path = 1*(".." "/") descendant-path
2524 *
2525 * @param[in,out] path Path to parse.
2526 * @param[out] prefix Prefix of the token, NULL if there is not any.
2527 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2528 * @param[out] name Name of the token.
2529 * @param[out] nam_len Length of the name.
2530 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2531 * must not be changed between consecutive calls. -1 if the
2532 * path is absolute.
2533 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2534 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2535 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002536LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002537lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2538 int *parent_times, int *has_predicate)
2539{
2540 int par_times = 0;
2541
2542 assert(path && *path);
2543 assert(parent_times);
2544 assert(prefix);
2545 assert(prefix_len);
2546 assert(name);
2547 assert(name_len);
2548 assert(has_predicate);
2549
2550 *prefix = NULL;
2551 *prefix_len = 0;
2552 *name = NULL;
2553 *name_len = 0;
2554 *has_predicate = 0;
2555
2556 if (!*parent_times) {
2557 if (!strncmp(*path, "..", 2)) {
2558 *path += 2;
2559 ++par_times;
2560 while (!strncmp(*path, "/..", 3)) {
2561 *path += 3;
2562 ++par_times;
2563 }
2564 }
2565 if (par_times) {
2566 *parent_times = par_times;
2567 } else {
2568 *parent_times = -1;
2569 }
2570 }
2571
2572 if (**path != '/') {
2573 return LY_EINVAL;
2574 }
2575 /* skip '/' */
2576 ++(*path);
2577
2578 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002579 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002580
2581 if ((**path == '/' && (*path)[1]) || !**path) {
2582 /* path continues by another token or this is the last token */
2583 return LY_SUCCESS;
2584 } else if ((*path)[0] != '[') {
2585 /* unexpected character */
2586 return LY_EINVAL;
2587 } else {
2588 /* predicate starting with [ */
2589 *has_predicate = 1;
2590 return LY_SUCCESS;
2591 }
2592}
2593
2594/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002595 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2596 *
2597 * The set of features used for target must be a subset of features used for the leafref.
2598 * This is not a perfect, we should compare the truth tables but it could require too much resources
2599 * and RFC 7950 does not require it explicitely, so we simplify that.
2600 *
2601 * @param[in] refnode The leafref node.
2602 * @param[in] target Tha target node of the leafref.
2603 * @return LY_SUCCESS or LY_EVALID;
2604 */
2605static LY_ERR
2606lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2607{
2608 LY_ERR ret = LY_EVALID;
2609 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002610 unsigned int u, v, count;
2611 struct ly_set features = {0};
2612
2613 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002614 if (iter->iffeatures) {
2615 LY_ARRAY_FOR(iter->iffeatures, u) {
2616 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2617 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002618 }
2619 }
2620 }
2621 }
2622
2623 /* we should have, in features set, a superset of features applicable to the target node.
2624 * So when adding features applicable to the target into the features set, we should not be
2625 * able to actually add any new feature, otherwise it is not a subset of features applicable
2626 * to the leafref itself. */
2627 count = features.count;
2628 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002629 if (iter->iffeatures) {
2630 LY_ARRAY_FOR(iter->iffeatures, u) {
2631 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2632 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002633 /* new feature was added (or LY_EMEM) */
2634 goto cleanup;
2635 }
2636 }
2637 }
2638 }
2639 }
2640 ret = LY_SUCCESS;
2641
2642cleanup:
2643 ly_set_erase(&features, NULL);
2644 return ret;
2645}
2646
2647/**
Radek Krejcia3045382018-11-22 14:30:31 +01002648 * @brief Validate the leafref path.
2649 * @param[in] ctx Compile context
2650 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002651 * @param[in] leafref Leafref to validate.
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002652 * @param[out] target Optional resolved leafref target.
Radek Krejcia3045382018-11-22 14:30:31 +01002653 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2654 */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002655LY_ERR
2656lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref,
2657 const struct lysc_node **target)
Radek Krejcia3045382018-11-22 14:30:31 +01002658{
Michal Vaskoecd62de2019-11-13 12:35:11 +01002659 const struct lysc_node *node = NULL, *parent = NULL;
Radek Krejcia3045382018-11-22 14:30:31 +01002660 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002661 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002662 const char *id, *prefix, *name;
2663 size_t prefix_len, name_len;
2664 int parent_times = 0, has_predicate;
2665 unsigned int iter, u;
2666 LY_ERR ret = LY_SUCCESS;
2667
2668 assert(ctx);
2669 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002670 assert(leafref);
2671
Radek Krejci1c0c3442019-07-23 16:08:47 +02002672 ctx->path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02002673 lysc_path(startnode, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Radek Krejci1c0c3442019-07-23 16:08:47 +02002674 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002675
Radek Krejcia3045382018-11-22 14:30:31 +01002676 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002677 id = leafref->path;
Juraj Vijtiukbd0f2a62019-12-11 15:58:18 +01002678
2679 if (!*id) {
2680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Empty leafref path.");
2681 return LY_EVALID;
2682 }
2683
Radek Krejcia3045382018-11-22 14:30:31 +01002684 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2685 if (!iter) { /* first iteration */
2686 /* precess ".." in relative paths */
2687 if (parent_times > 0) {
2688 /* move from the context node */
2689 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2690 /* path is supposed to be evaluated in data tree, so we have to skip
2691 * all schema nodes that cannot be instantiated in data tree */
2692 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002693 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002694 }
2695 }
2696 }
2697
2698 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002699 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002700 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002701 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002702 }
2703 if (!mod) {
2704 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002705 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2706 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002707 return LY_EVALID;
2708 }
Radek Krejci3d929562019-02-21 11:25:55 +01002709 if (!mod->implemented) {
2710 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002711 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci3d929562019-02-21 11:25:55 +01002712 }
Radek Krejcia3045382018-11-22 14:30:31 +01002713
2714 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2715 if (!node) {
2716 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002717 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002718 return LY_EVALID;
2719 }
2720 parent = node;
2721
2722 if (has_predicate) {
2723 /* we have predicate, so the current result must be list */
2724 if (node->nodetype != LYS_LIST) {
2725 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2726 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002727 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002728 return LY_EVALID;
2729 }
2730
Radek Krejcibade82a2018-12-05 10:13:48 +01002731 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2732 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002733 }
2734
2735 ++iter;
2736 }
2737 if (ret) {
2738 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002739 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002740 return LY_EVALID;
2741 }
2742
2743 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2745 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002746 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002747 return LY_EVALID;
2748 }
2749
2750 /* check status */
2751 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2752 return LY_EVALID;
2753 }
2754
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002755 /* check config */
2756 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2757 if (node->flags & LYS_CONFIG_R) {
2758 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2759 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2760 leafref->path);
2761 return LY_EVALID;
2762 }
2763 }
2764
Radek Krejci412ddfa2018-11-23 11:44:11 +01002765 /* store the target's type and check for circular chain of leafrefs */
2766 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2767 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2768 if (type == (struct lysc_type*)leafref) {
2769 /* circular chain detected */
2770 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2771 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2772 return LY_EVALID;
2773 }
2774 }
2775
Radek Krejci58d171e2018-11-23 13:50:55 +01002776 /* check if leafref and its target are under common if-features */
2777 if (lys_compile_leafref_features_validate(startnode, node)) {
2778 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2779 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2780 leafref->path);
2781 return LY_EVALID;
2782 }
2783
Radek Krejci327de162019-06-14 12:52:07 +02002784 ctx->path_len = 1;
2785 ctx->path[1] = '\0';
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02002786 if (target) {
2787 *target = node;
2788 }
Radek Krejcia3045382018-11-22 14:30:31 +01002789 return LY_SUCCESS;
2790}
2791
Radek Krejcicdfecd92018-11-26 11:27:32 +01002792static 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 +02002793 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002794/**
2795 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2796 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002797 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2798 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2799 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2800 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002801 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002802 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002803 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002804 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2805 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2806 * @param[out] type Newly created type structure with the filled information about the type.
2807 * @return LY_ERR value.
2808 */
Radek Krejci19a96102018-11-15 13:38:09 +01002809static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002810lys_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 +02002811 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002812 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002813{
2814 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002815 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002816 struct lysc_type_bin *bin;
2817 struct lysc_type_num *num;
2818 struct lysc_type_str *str;
2819 struct lysc_type_bits *bits;
2820 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002821 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002822 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002823 struct lysc_type_union *un, *un_aux;
2824 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002825
2826 switch (basetype) {
2827 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002828 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002829
2830 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002831 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002832 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2833 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002834 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002835 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 +01002836 }
2837 }
2838
2839 if (tpdfname) {
2840 type_p->compiled = *type;
2841 *type = calloc(1, sizeof(struct lysc_type_bin));
2842 }
2843 break;
2844 case LY_TYPE_BITS:
2845 /* RFC 7950 9.7 - bits */
2846 bits = (struct lysc_type_bits*)(*type);
2847 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002848 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002849 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2850 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002851 }
2852
Radek Krejci555cb5b2018-11-16 14:54:33 +01002853 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002854 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002855 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002856 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002857 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002858 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002859 free(*type);
2860 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002861 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002862 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002863 }
2864
2865 if (tpdfname) {
2866 type_p->compiled = *type;
2867 *type = calloc(1, sizeof(struct lysc_type_bits));
2868 }
2869 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002870 case LY_TYPE_DEC64:
2871 dec = (struct lysc_type_dec*)(*type);
2872
2873 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002874 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002875 if (!type_p->fraction_digits) {
2876 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002877 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002878 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002879 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002880 free(*type);
2881 *type = NULL;
2882 }
2883 return LY_EVALID;
2884 }
2885 } else if (type_p->fraction_digits) {
2886 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002887 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002888 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2889 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002890 tpdfname);
2891 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002892 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2893 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002894 free(*type);
2895 *type = NULL;
2896 }
2897 return LY_EVALID;
2898 }
2899 dec->fraction_digits = type_p->fraction_digits;
2900
2901 /* RFC 7950 9.2.4 - range */
2902 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002903 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2904 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002905 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002906 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 +01002907 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002908 }
2909
2910 if (tpdfname) {
2911 type_p->compiled = *type;
2912 *type = calloc(1, sizeof(struct lysc_type_dec));
2913 }
2914 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002915 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002916 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002917
2918 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002919 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002920 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2921 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002922 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002923 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 +01002924 }
2925 } else if (base && ((struct lysc_type_str*)base)->length) {
2926 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2927 }
2928
2929 /* RFC 7950 9.4.5 - pattern */
2930 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002931 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002932 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002933 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2934 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2935 }
2936
2937 if (tpdfname) {
2938 type_p->compiled = *type;
2939 *type = calloc(1, sizeof(struct lysc_type_str));
2940 }
2941 break;
2942 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002943 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002944
2945 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002946 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002947 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002948 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002949 }
2950
Radek Krejci555cb5b2018-11-16 14:54:33 +01002951 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002952 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002953 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002955 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002956 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002957 free(*type);
2958 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002959 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002960 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002961 }
2962
2963 if (tpdfname) {
2964 type_p->compiled = *type;
2965 *type = calloc(1, sizeof(struct lysc_type_enum));
2966 }
2967 break;
2968 case LY_TYPE_INT8:
2969 case LY_TYPE_UINT8:
2970 case LY_TYPE_INT16:
2971 case LY_TYPE_UINT16:
2972 case LY_TYPE_INT32:
2973 case LY_TYPE_UINT32:
2974 case LY_TYPE_INT64:
2975 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002976 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002977
2978 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002979 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002980 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2981 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002982 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002983 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 +01002984 }
2985 }
2986
2987 if (tpdfname) {
2988 type_p->compiled = *type;
2989 *type = calloc(1, sizeof(struct lysc_type_num));
2990 }
2991 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002992 case LY_TYPE_IDENT:
2993 idref = (struct lysc_type_identityref*)(*type);
2994
2995 /* RFC 7950 9.10.2 - base */
2996 if (type_p->bases) {
2997 if (base) {
2998 /* only the directly derived identityrefs can contain base specification */
2999 if (tpdfname) {
3000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01003001 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01003002 tpdfname);
3003 } else {
3004 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01003005 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01003006 free(*type);
3007 *type = NULL;
3008 }
3009 return LY_EVALID;
3010 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003011 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01003012 }
3013
3014 if (!base && !type_p->flags) {
3015 /* type derived from identityref built-in type must contain at least one base */
3016 if (tpdfname) {
3017 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
3018 } else {
3019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
3020 free(*type);
3021 *type = NULL;
3022 }
3023 return LY_EVALID;
3024 }
3025
3026 if (tpdfname) {
3027 type_p->compiled = *type;
3028 *type = calloc(1, sizeof(struct lysc_type_identityref));
3029 }
3030 break;
Radek Krejcia3045382018-11-22 14:30:31 +01003031 case LY_TYPE_LEAFREF:
3032 /* RFC 7950 9.9.3 - require-instance */
3033 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003034 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003035 if (tpdfname) {
3036 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3037 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
3038 } else {
3039 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3040 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
3041 free(*type);
3042 *type = NULL;
3043 }
3044 return LY_EVALID;
3045 }
Radek Krejcia3045382018-11-22 14:30:31 +01003046 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01003047 } else if (base) {
3048 /* inherit */
3049 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01003050 } else {
3051 /* default is true */
3052 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
3053 }
3054 if (type_p->path) {
3055 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003056 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01003057 } else if (base) {
3058 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01003059 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01003060 } else if (tpdfname) {
3061 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
3062 return LY_EVALID;
3063 } else {
3064 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
3065 free(*type);
3066 *type = NULL;
3067 return LY_EVALID;
3068 }
3069 if (tpdfname) {
3070 type_p->compiled = *type;
3071 *type = calloc(1, sizeof(struct lysc_type_leafref));
3072 }
3073 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01003074 case LY_TYPE_INST:
3075 /* RFC 7950 9.9.3 - require-instance */
3076 if (type_p->flags & LYS_SET_REQINST) {
3077 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
3078 } else {
3079 /* default is true */
3080 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
3081 }
3082
3083 if (tpdfname) {
3084 type_p->compiled = *type;
3085 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3086 }
3087 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003088 case LY_TYPE_UNION:
3089 un = (struct lysc_type_union*)(*type);
3090
3091 /* RFC 7950 7.4 - type */
3092 if (type_p->types) {
3093 if (base) {
3094 /* only the directly derived union can contain types specification */
3095 if (tpdfname) {
3096 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3097 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
3098 tpdfname);
3099 } else {
3100 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3101 "Invalid type substatement for the type not directly derived from union built-in type.");
3102 free(*type);
3103 *type = NULL;
3104 }
3105 return LY_EVALID;
3106 }
3107 /* compile the type */
3108 additional = 0;
3109 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
3110 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02003111 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 +01003112 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
3113 /* add space for additional types from the union subtype */
3114 un_aux = (struct lysc_type_union *)un->types[u + additional];
3115 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)));
3116 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3117 un->types = (void*)((uint32_t*)(p) + 1);
3118
3119 /* copy subtypes of the subtype union */
3120 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
3121 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
3122 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
3123 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
3124 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3125 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
3126 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
3127 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3128 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3129 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3130 /* TODO extensions */
3131
3132 } else {
3133 un->types[u + additional] = un_aux->types[v];
3134 ++un_aux->types[v]->refcount;
3135 }
3136 ++additional;
3137 LY_ARRAY_INCREMENT(un->types);
3138 }
3139 /* compensate u increment in main loop */
3140 --additional;
3141
3142 /* free the replaced union subtype */
3143 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3144 } else {
3145 LY_ARRAY_INCREMENT(un->types);
3146 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003147 }
3148 }
3149
3150 if (!base && !type_p->flags) {
3151 /* type derived from union built-in type must contain at least one type */
3152 if (tpdfname) {
3153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3154 } else {
3155 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3156 free(*type);
3157 *type = NULL;
3158 }
3159 return LY_EVALID;
3160 }
3161
3162 if (tpdfname) {
3163 type_p->compiled = *type;
3164 *type = calloc(1, sizeof(struct lysc_type_union));
3165 }
3166 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003167 case LY_TYPE_BOOL:
3168 case LY_TYPE_EMPTY:
3169 case LY_TYPE_UNKNOWN: /* just to complete switch */
3170 break;
3171 }
3172 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3173done:
3174 return ret;
3175}
3176
Radek Krejcia3045382018-11-22 14:30:31 +01003177/**
3178 * @brief Compile information about the leaf/leaf-list's type.
3179 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003180 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3181 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3182 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3183 * @param[in] context_name Name of the context node or referencing typedef for logging.
3184 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003185 * @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 +01003186 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003187 * @return LY_ERR value.
3188 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003189static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003190lys_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 +02003191 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003192{
3193 LY_ERR ret = LY_SUCCESS;
3194 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003195 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003196 struct type_context {
3197 const struct lysp_tpdf *tpdf;
3198 struct lysp_node *node;
3199 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003200 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003201 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003202 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003203 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003204 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003205 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003206
3207 (*type) = NULL;
3208
3209 tctx = calloc(1, sizeof *tctx);
3210 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003211 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003212 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3213 ret == LY_SUCCESS;
3214 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3215 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3216 if (basetype) {
3217 break;
3218 }
3219
3220 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003221 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3222 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003223 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3224
Radek Krejcicdfecd92018-11-26 11:27:32 +01003225 if (units && !*units) {
3226 /* inherit units */
3227 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3228 }
Radek Krejci01342af2019-01-03 15:18:08 +01003229 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003230 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003231 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003232 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003233 }
Radek Krejci01342af2019-01-03 15:18:08 +01003234 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003235 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3236 break;
3237 }
3238
Radek Krejci19a96102018-11-15 13:38:09 +01003239 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003240 /* it is not necessary to continue, the rest of the chain was already compiled,
3241 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003242 basetype = tctx->tpdf->type.compiled->basetype;
3243 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003244 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003245 dummyloops = 1;
3246 goto preparenext;
3247 } else {
3248 tctx = NULL;
3249 break;
3250 }
Radek Krejci19a96102018-11-15 13:38:09 +01003251 }
3252
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003253 /* circular typedef reference detection */
3254 for (u = 0; u < tpdf_chain.count; u++) {
3255 /* local part */
3256 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3257 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3258 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3259 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3260 free(tctx);
3261 ret = LY_EVALID;
3262 goto cleanup;
3263 }
3264 }
3265 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3266 /* global part for unions corner case */
3267 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3268 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3269 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3270 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3271 free(tctx);
3272 ret = LY_EVALID;
3273 goto cleanup;
3274 }
3275 }
3276
Radek Krejci19a96102018-11-15 13:38:09 +01003277 /* store information for the following processing */
3278 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3279
Radek Krejcicdfecd92018-11-26 11:27:32 +01003280preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003281 /* prepare next loop */
3282 tctx_prev = tctx;
3283 tctx = calloc(1, sizeof *tctx);
3284 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3285 }
3286 free(tctx);
3287
3288 /* allocate type according to the basetype */
3289 switch (basetype) {
3290 case LY_TYPE_BINARY:
3291 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003292 break;
3293 case LY_TYPE_BITS:
3294 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003295 break;
3296 case LY_TYPE_BOOL:
3297 case LY_TYPE_EMPTY:
3298 *type = calloc(1, sizeof(struct lysc_type));
3299 break;
3300 case LY_TYPE_DEC64:
3301 *type = calloc(1, sizeof(struct lysc_type_dec));
3302 break;
3303 case LY_TYPE_ENUM:
3304 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003305 break;
3306 case LY_TYPE_IDENT:
3307 *type = calloc(1, sizeof(struct lysc_type_identityref));
3308 break;
3309 case LY_TYPE_INST:
3310 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3311 break;
3312 case LY_TYPE_LEAFREF:
3313 *type = calloc(1, sizeof(struct lysc_type_leafref));
3314 break;
3315 case LY_TYPE_STRING:
3316 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003317 break;
3318 case LY_TYPE_UNION:
3319 *type = calloc(1, sizeof(struct lysc_type_union));
3320 break;
3321 case LY_TYPE_INT8:
3322 case LY_TYPE_UINT8:
3323 case LY_TYPE_INT16:
3324 case LY_TYPE_UINT16:
3325 case LY_TYPE_INT32:
3326 case LY_TYPE_UINT32:
3327 case LY_TYPE_INT64:
3328 case LY_TYPE_UINT64:
3329 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003330 break;
3331 case LY_TYPE_UNKNOWN:
3332 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3333 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3334 ret = LY_EVALID;
3335 goto cleanup;
3336 }
3337 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003338 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003339 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3340 ly_data_type2str[basetype]);
3341 free(*type);
3342 (*type) = NULL;
3343 ret = LY_EVALID;
3344 goto cleanup;
3345 }
3346
3347 /* get restrictions from the referred typedefs */
3348 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3349 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003350
3351 /* remember the typedef context for circular check */
3352 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3353
Radek Krejci43699232018-11-23 14:59:46 +01003354 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003355 base = tctx->tpdf->type.compiled;
3356 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003357 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003358 /* no change, just use the type information from the base */
3359 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3360 ++base->refcount;
3361 continue;
3362 }
3363
3364 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003365 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3366 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3367 tctx->tpdf->name, ly_data_type2str[basetype]);
3368 ret = LY_EVALID;
3369 goto cleanup;
3370 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3371 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3372 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3373 tctx->tpdf->name, tctx->tpdf->dflt);
3374 ret = LY_EVALID;
3375 goto cleanup;
3376 }
3377
Radek Krejci19a96102018-11-15 13:38:09 +01003378 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003379 /* TODO user type plugins */
3380 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003381 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003382 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 +01003383 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003384 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003385 LY_CHECK_GOTO(ret, cleanup);
3386 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003387 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003388 /* remove the processed typedef contexts from the stack for circular check */
3389 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003390
Radek Krejcic5c27e52018-11-15 14:38:11 +01003391 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003392 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003393 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003394 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003395 /* TODO user type plugins */
3396 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003397 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003398 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 +01003399 LY_CHECK_GOTO(ret, cleanup);
3400 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003401 /* no specific restriction in leaf's type definition, copy from the base */
3402 free(*type);
3403 (*type) = base;
3404 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003405 }
Radek Krejcia1911222019-07-22 17:24:50 +02003406 if (dflt && !(*type)->dflt) {
3407 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003408 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003409 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3410 (*type)->dflt->realtype = (*type);
3411 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3412 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003413 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003414 if (err) {
3415 ly_err_print(err);
3416 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3417 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3418 ly_err_free(err);
3419 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003420 if (ret == LY_EINCOMPLETE) {
3421 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003422 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003423
3424 /* but in general result is so far ok */
3425 ret = LY_SUCCESS;
3426 }
Radek Krejcia1911222019-07-22 17:24:50 +02003427 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003428 }
Radek Krejci19a96102018-11-15 13:38:09 +01003429
Radek Krejci0935f412019-08-20 16:15:18 +02003430 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003431
3432cleanup:
3433 ly_set_erase(&tpdf_chain, free);
3434 return ret;
3435}
3436
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003437/**
3438 * @brief Compile status information of the given node.
3439 *
3440 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3441 * has the status correctly set during the compilation.
3442 *
3443 * @param[in] ctx Compile context
3444 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3445 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3446 * the compatibility with the parent's status value.
3447 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3448 * @return LY_ERR value.
3449 */
3450static LY_ERR
3451lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3452{
3453 /* status - it is not inherited by specification, but it does not make sense to have
3454 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3455 if (!((*node_flags) & LYS_STATUS_MASK)) {
3456 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3457 if ((parent_flags & 0x3) != 0x3) {
3458 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3459 * combination of bits (0x3) which marks the uses_status value */
3460 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3461 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3462 }
3463 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3464 } else {
3465 (*node_flags) |= LYS_STATUS_CURR;
3466 }
3467 } else if (parent_flags & LYS_STATUS_MASK) {
3468 /* check status compatibility with the parent */
3469 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3470 if ((*node_flags) & LYS_STATUS_CURR) {
3471 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3472 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3473 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3474 } else { /* LYS_STATUS_DEPRC */
3475 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3476 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3477 }
3478 return LY_EVALID;
3479 }
3480 }
3481 return LY_SUCCESS;
3482}
3483
Radek Krejci8cce8532019-03-05 11:27:45 +01003484/**
3485 * @brief Check uniqness of the node/action/notification name.
3486 *
3487 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3488 * structures, but they share the namespace so we need to check their name collisions.
3489 *
3490 * @param[in] ctx Compile context.
3491 * @param[in] children List (linked list) of data nodes to go through.
3492 * @param[in] actions List (sized array) of actions or RPCs to go through.
3493 * @param[in] notifs List (sized array) of Notifications to go through.
3494 * @param[in] name Name of the item to find in the given lists.
3495 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3496 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3497 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3498 */
3499static LY_ERR
3500lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3501 const struct lysc_action *actions, const struct lysc_notif *notifs,
3502 const char *name, void *exclude)
3503{
3504 const struct lysc_node *iter;
3505 unsigned int u;
3506
3507 LY_LIST_FOR(children, iter) {
3508 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3509 goto error;
3510 }
3511 }
3512 LY_ARRAY_FOR(actions, u) {
3513 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3514 goto error;
3515 }
3516 }
3517 LY_ARRAY_FOR(notifs, u) {
3518 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3519 goto error;
3520 }
3521 }
3522 return LY_SUCCESS;
3523error:
Michal Vaskoa3881362020-01-21 15:57:35 +01003524 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/notification");
Radek Krejci8cce8532019-03-05 11:27:45 +01003525 return LY_EEXIST;
3526}
3527
Radek Krejciec4da802019-05-02 13:02:41 +02003528static 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 +01003529
Radek Krejcia3045382018-11-22 14:30:31 +01003530/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003531 * @brief Compile parsed RPC/action schema node information.
3532 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003533 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003534 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003535 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3536 * @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).
3537 * Zero means no uses, non-zero value with no status bit set mean the default status.
3538 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3539 */
3540static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003541lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003542 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3543{
3544 LY_ERR ret = LY_SUCCESS;
3545 struct lysp_node *child_p;
3546 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003547 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003548
Radek Krejci327de162019-06-14 12:52:07 +02003549 lysc_update_path(ctx, parent, action_p->name);
3550
Radek Krejci8cce8532019-03-05 11:27:45 +01003551 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3552 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3553 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3554 action_p->name, action)) {
3555 return LY_EVALID;
3556 }
3557
Radek Krejciec4da802019-05-02 13:02:41 +02003558 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003560 "Action \"%s\" is placed inside %s.", action_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003561 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003562 return LY_EVALID;
3563 }
3564
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003565 action->nodetype = LYS_ACTION;
3566 action->module = ctx->mod;
3567 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003568 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003569 action->sp = action_p;
3570 }
3571 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3572
3573 /* status - it is not inherited by specification, but it does not make sense to have
3574 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3575 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3576
3577 DUP_STRING(ctx->ctx, action_p->name, action->name);
3578 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3579 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003580 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003581 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003582
3583 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003584 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003585 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003586 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 +02003587 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003588 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003589 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003590 }
Radek Krejci327de162019-06-14 12:52:07 +02003591 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003592 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003593
3594 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003595 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003596 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003597 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 +02003598 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003599 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003600 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003601 }
Radek Krejci327de162019-06-14 12:52:07 +02003602 lysc_update_path(ctx, NULL, NULL);
Radek Krejci327de162019-06-14 12:52:07 +02003603 lysc_update_path(ctx, NULL, NULL);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003604
3605 if ((action_p->input.musts || action_p->output.musts) && !(ctx->options & LYSC_OPT_GROUPING)) {
3606 /* do not check "must" semantics in a grouping */
3607 ly_set_add(&ctx->unres, action, 0);
3608 }
3609
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003610cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003611 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003612 return ret;
3613}
3614
3615/**
Radek Krejci43981a32019-04-12 09:44:11 +02003616 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003617 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003618 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003619 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3620 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3621 * @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 +02003622 * Zero means no uses, non-zero value with no status bit set mean the default status.
3623 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3624 */
3625static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003626lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003627 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3628{
3629 LY_ERR ret = LY_SUCCESS;
3630 struct lysp_node *child_p;
3631 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003632 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003633
Radek Krejci327de162019-06-14 12:52:07 +02003634 lysc_update_path(ctx, parent, notif_p->name);
3635
Radek Krejcifc11bd72019-04-11 16:00:05 +02003636 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3637 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3638 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3639 notif_p->name, notif)) {
3640 return LY_EVALID;
3641 }
3642
Radek Krejciec4da802019-05-02 13:02:41 +02003643 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3645 "Notification \"%s\" is placed inside %s.", notif_p->name,
Michal Vaskoa3881362020-01-21 15:57:35 +01003646 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003647 return LY_EVALID;
3648 }
3649
3650 notif->nodetype = LYS_NOTIF;
3651 notif->module = ctx->mod;
3652 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003653 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003654 notif->sp = notif_p;
3655 }
3656 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3657
3658 /* status - it is not inherited by specification, but it does not make sense to have
3659 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3660 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3661
3662 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3663 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3664 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003665 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003666 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003667 if (notif_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3668 /* do not check "must" semantics in a grouping */
3669 ly_set_add(&ctx->unres, notif, 0);
3670 }
Radek Krejci0935f412019-08-20 16:15:18 +02003671 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003672
Radek Krejciec4da802019-05-02 13:02:41 +02003673 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003674 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003675 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003676 }
3677
Radek Krejci327de162019-06-14 12:52:07 +02003678 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003679cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003680 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003681 return ret;
3682}
3683
3684/**
Radek Krejcia3045382018-11-22 14:30:31 +01003685 * @brief Compile parsed container node information.
3686 * @param[in] ctx Compile context
3687 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003688 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3689 * is enriched with the container-specific information.
3690 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3691 */
Radek Krejci19a96102018-11-15 13:38:09 +01003692static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003693lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003694{
3695 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3696 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3697 struct lysp_node *child_p;
3698 unsigned int u;
3699 LY_ERR ret = LY_SUCCESS;
3700
Radek Krejcife909632019-02-12 15:34:42 +01003701 if (cont_p->presence) {
3702 cont->flags |= LYS_PRESENCE;
3703 }
3704
Radek Krejci19a96102018-11-15 13:38:09 +01003705 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003706 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003707 }
3708
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003709 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003710 if (cont_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3711 /* do not check "must" semantics in a grouping */
3712 ly_set_add(&ctx->unres, cont, 0);
3713 }
Radek Krejciec4da802019-05-02 13:02:41 +02003714 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3715 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003716
3717done:
3718 return ret;
3719}
3720
Radek Krejci33f72892019-02-21 10:36:58 +01003721/*
3722 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3723 * @param[in] ctx Compile context.
3724 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3725 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003726 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3727 * @return LY_ERR value.
3728 */
3729static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003730lys_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 +01003731{
Radek Krejcia1911222019-07-22 17:24:50 +02003732 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003733 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3734
Radek Krejciec4da802019-05-02 13:02:41 +02003735 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 +01003736 leaf->units ? NULL : &leaf->units));
3737 if (leaf->nodetype == LYS_LEAFLIST) {
3738 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003739 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3740 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003741 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003742 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3743 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3744 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3745 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003746 LY_ARRAY_INCREMENT(llist->dflts);
3747 }
3748 } else {
3749 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003750 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003751 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3752 leaf->dflt->realtype = leaf->type->dflt->realtype;
3753 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3754 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003755 }
3756 }
3757 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3758 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3759 ly_set_add(&ctx->unres, leaf, 0);
3760 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3761 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3762 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3763 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3764 ly_set_add(&ctx->unres, leaf, 0);
3765 }
3766 }
3767 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003768 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3769 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3770 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3771 return LY_EVALID;
3772 }
3773 }
3774
Radek Krejci33f72892019-02-21 10:36:58 +01003775 return LY_SUCCESS;
3776}
3777
Radek Krejcia3045382018-11-22 14:30:31 +01003778/**
3779 * @brief Compile parsed leaf node information.
3780 * @param[in] ctx Compile context
3781 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003782 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3783 * is enriched with the leaf-specific information.
3784 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3785 */
Radek Krejci19a96102018-11-15 13:38:09 +01003786static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003787lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003788{
3789 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3790 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3791 unsigned int u;
3792 LY_ERR ret = LY_SUCCESS;
3793
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003794 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003795 if (leaf_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3796 /* do not check "must" semantics in a grouping */
3797 ly_set_add(&ctx->unres, leaf, 0);
3798 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003799 if (leaf_p->units) {
3800 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3801 leaf->flags |= LYS_SET_UNITS;
3802 }
Radek Krejcia1911222019-07-22 17:24:50 +02003803
3804 /* the dflt member is just filled to avoid getting the default value from the type */
3805 leaf->dflt = (void*)leaf_p->dflt;
3806 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003807 if (ret) {
3808 leaf->dflt = NULL;
3809 return ret;
3810 }
Radek Krejcia1911222019-07-22 17:24:50 +02003811
Radek Krejciccd20f12019-02-15 14:12:27 +01003812 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003813 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003814 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003815 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3816 leaf->dflt->realtype = leaf->type;
3817 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3818 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003819 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003820 leaf->dflt->realtype->refcount++;
3821 if (err) {
3822 ly_err_print(err);
3823 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3824 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3825 ly_err_free(err);
3826 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003827 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003828 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003829 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003830
3831 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003832 ret = LY_SUCCESS;
3833 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003834 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003835 leaf->flags |= LYS_SET_DFLT;
3836 }
Radek Krejci43699232018-11-23 14:59:46 +01003837
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003838
Radek Krejci19a96102018-11-15 13:38:09 +01003839done:
3840 return ret;
3841}
3842
Radek Krejcia3045382018-11-22 14:30:31 +01003843/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003844 * @brief Compile parsed leaf-list node information.
3845 * @param[in] ctx Compile context
3846 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003847 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3848 * is enriched with the leaf-list-specific information.
3849 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3850 */
3851static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003852lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003853{
3854 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3855 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003856 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003857 LY_ERR ret = LY_SUCCESS;
3858
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003859 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01003860 if (llist_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
3861 /* do not check "must" semantics in a grouping */
3862 ly_set_add(&ctx->unres, llist, 0);
3863 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003864 if (llist_p->units) {
3865 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3866 llist->flags |= LYS_SET_UNITS;
3867 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003868
Radek Krejcia1911222019-07-22 17:24:50 +02003869 /* the dflts member is just filled to avoid getting the default value from the type */
3870 llist->dflts = (void*)llist_p->dflts;
3871 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Michal Vasko6a044b22020-01-15 12:25:39 +01003872 if (ret != LY_SUCCESS) {
3873 /* make sure the defaults are freed correctly */
3874 if (llist_p->dflts) {
3875 llist->dflts = NULL;
3876 }
3877 return ret;
3878 }
3879
Radek Krejci0e5d8382018-11-28 16:37:53 +01003880 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003881 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003882 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003883 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3884 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003885 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003886 LY_ARRAY_INCREMENT(llist->dflts_mods);
3887 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003888 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003889 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3890 llist->dflts[u]->realtype = llist->type;
3891 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3892 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003893 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003894 llist->dflts[u]->realtype->refcount++;
3895 if (err) {
3896 ly_err_print(err);
3897 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3898 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3899 ly_err_free(err);
3900 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003901 if (ret == LY_EINCOMPLETE) {
3902 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003903 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003904
3905 /* but in general result is so far ok */
3906 ret = LY_SUCCESS;
3907 }
Radek Krejcia1911222019-07-22 17:24:50 +02003908 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003909 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003910 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003911 }
Radek Krejcia1911222019-07-22 17:24:50 +02003912 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3913 /* configuration data values must be unique - so check the default values */
3914 LY_ARRAY_FOR(llist->dflts, u) {
3915 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3916 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3917 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003918 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 +02003919 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3920 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3921 if (dynamic) {
3922 free((char*)val);
3923 }
3924 return LY_EVALID;
3925 }
3926 }
3927 }
3928 }
3929
3930 /* 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 +01003931
3932 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003933 if (llist->min) {
3934 llist->flags |= LYS_MAND_TRUE;
3935 }
Radek Krejcib7408632018-11-28 17:12:11 +01003936 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003937
Radek Krejci0e5d8382018-11-28 16:37:53 +01003938done:
3939 return ret;
3940}
3941
3942/**
Radek Krejci7af64242019-02-18 13:07:53 +01003943 * @brief Compile information about list's uniques.
3944 * @param[in] ctx Compile context.
3945 * @param[in] context_module Module where the prefixes are going to be resolved.
3946 * @param[in] uniques Sized array list of unique statements.
3947 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3948 * @return LY_ERR value.
3949 */
3950static LY_ERR
3951lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3952{
3953 LY_ERR ret = LY_SUCCESS;
3954 struct lysc_node_leaf **key, ***unique;
3955 const char *keystr, *delim;
3956 size_t len;
3957 unsigned int v;
3958 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003959 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003960
3961 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3962 config = -1;
3963 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3964 keystr = uniques[v];
3965 while (keystr) {
3966 delim = strpbrk(keystr, " \t\n");
3967 if (delim) {
3968 len = delim - keystr;
3969 while (isspace(*delim)) {
3970 ++delim;
3971 }
3972 } else {
3973 len = strlen(keystr);
3974 }
3975
3976 /* unique node must be present */
3977 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003978 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3979 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003980 if (ret != LY_SUCCESS) {
3981 if (ret == LY_EDENIED) {
3982 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003983 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003984 len, keystr, lys_nodetype2str((*key)->nodetype));
3985 }
3986 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003987 } else if (flags) {
3988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3989 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
Michal Vaskoa3881362020-01-21 15:57:35 +01003990 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003991 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003992 }
3993
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003994
Radek Krejci7af64242019-02-18 13:07:53 +01003995 /* all referenced leafs must be of the same config type */
3996 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3997 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3998 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3999 return LY_EVALID;
4000 } else if ((*key)->flags & LYS_CONFIG_W) {
4001 config = 1;
4002 } else { /* LYS_CONFIG_R */
4003 config = 0;
4004 }
4005
4006 /* check status */
4007 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
4008 (*key)->flags, (*key)->module, (*key)->name));
4009
4010 /* mark leaf as unique */
4011 (*key)->flags |= LYS_UNIQUE;
4012
4013 /* next unique value in line */
4014 keystr = delim;
4015 }
4016 /* next unique definition */
4017 }
4018
4019 return LY_SUCCESS;
4020}
4021
4022/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004023 * @brief Compile parsed list node information.
4024 * @param[in] ctx Compile context
4025 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004026 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4027 * is enriched with the list-specific information.
4028 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4029 */
4030static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004031lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004032{
4033 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
4034 struct lysc_node_list *list = (struct lysc_node_list*)node;
4035 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004036 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004037 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01004038 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004039 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004040 LY_ERR ret = LY_SUCCESS;
4041
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004042 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01004043 if (list->min) {
4044 list->flags |= LYS_MAND_TRUE;
4045 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004046 list->max = list_p->max ? list_p->max : (uint32_t)-1;
4047
4048 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004049 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004050 }
4051
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004052 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004053 if (list_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4054 /* do not check "must" semantics in a grouping */
4055 ly_set_add(&ctx->unres, list, 0);
4056 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004057
4058 /* keys */
4059 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
4060 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
4061 return LY_EVALID;
4062 }
4063
4064 /* find all the keys (must be direct children) */
4065 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02004066 if (!keystr) {
4067 /* keyless list */
4068 list->flags |= LYS_KEYLESS;
4069 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004070 while (keystr) {
4071 delim = strpbrk(keystr, " \t\n");
4072 if (delim) {
4073 len = delim - keystr;
4074 while (isspace(*delim)) {
4075 ++delim;
4076 }
4077 } else {
4078 len = strlen(keystr);
4079 }
4080
4081 /* key node must be present */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004082 key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
4083 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004084 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4085 "The list's key \"%.*s\" not found.", len, keystr);
4086 return LY_EVALID;
4087 }
4088 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004089 if (key->flags & LYS_KEY) {
4090 /* the node was already marked as a key */
4091 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4092 "Duplicated key identifier \"%.*s\".", len, keystr);
4093 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004094 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004095
4096 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004097 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004098 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004099 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
4100 return LY_EVALID;
4101 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004102 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004103 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004104 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004106 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004107 return LY_EVALID;
4108 }
4109 } else {
4110 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004111 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004112 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004113 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004114 return LY_EVALID;
4115 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004116 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004118 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004119 return LY_EVALID;
4120 }
4121 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004122
4123 /* check status */
4124 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02004125 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01004126
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004127 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004128 if (key->dflt) {
4129 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
4130 lysc_type_free(ctx->ctx, key->dflt->realtype);
4131 free(key->dflt);
4132 key->dflt = NULL;
4133 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004134 }
4135 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004136 key->flags |= LYS_KEY;
4137
4138 /* move it to the correct position */
4139 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
4140 /* fix links in closest previous siblings of the key */
4141 if (key->next) {
4142 key->next->prev = key->prev;
4143 } else {
4144 /* last child */
4145 list->child->prev = key->prev;
4146 }
4147 if (key->prev->next) {
4148 key->prev->next = key->next;
4149 }
4150 /* fix links in the key */
4151 if (prev_key) {
4152 key->prev = (struct lysc_node*)prev_key;
4153 key->next = prev_key->next;
4154 } else {
4155 key->prev = list->child->prev;
4156 key->next = list->child;
4157 }
4158 /* fix links in closes future siblings of the key */
4159 if (prev_key) {
4160 if (prev_key->next) {
4161 prev_key->next->prev = (struct lysc_node*)key;
4162 } else {
4163 list->child->prev = (struct lysc_node*)key;
4164 }
4165 prev_key->next = (struct lysc_node*)key;
4166 } else {
4167 list->child->prev = (struct lysc_node*)key;
4168 }
4169 /* fix links in parent */
4170 if (!key->prev->next) {
4171 list->child = (struct lysc_node*)key;
4172 }
4173 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004174
4175 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004176 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004177 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004178 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004179 }
4180
4181 /* uniques */
4182 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004183 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004184 }
4185
Radek Krejciec4da802019-05-02 13:02:41 +02004186 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4187 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004188
4189done:
4190 return ret;
4191}
4192
Radek Krejcib56c7502019-02-13 14:19:54 +01004193/**
4194 * @brief Do some checks and set the default choice's case.
4195 *
4196 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4197 *
4198 * @param[in] ctx Compile context.
4199 * @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,
4200 * not the reference to the imported module.
4201 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4202 * @return LY_ERR value.
4203 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004204static LY_ERR
4205lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4206{
4207 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4208 const char *prefix = NULL, *name;
4209 size_t prefix_len = 0;
4210
4211 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4212 name = strchr(dflt, ':');
4213 if (name) {
4214 prefix = dflt;
4215 prefix_len = name - prefix;
4216 ++name;
4217 } else {
4218 name = dflt;
4219 }
Radek Krejci7f9b6512019-09-18 13:11:09 +02004220 if (prefix && ly_strncmp(node->module->prefix, prefix, prefix_len)) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004221 /* prefixed default case make sense only for the prefix of the schema itself */
4222 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4223 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4224 prefix_len, prefix);
4225 return LY_EVALID;
4226 }
4227 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4228 if (!ch->dflt) {
4229 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4230 "Default case \"%s\" not found.", dflt);
4231 return LY_EVALID;
4232 }
4233 /* no mandatory nodes directly under the default case */
4234 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004235 if (iter->parent != (struct lysc_node*)ch->dflt) {
4236 break;
4237 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004238 if (iter->flags & LYS_MAND_TRUE) {
4239 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4240 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4241 return LY_EVALID;
4242 }
4243 }
Radek Krejci01342af2019-01-03 15:18:08 +01004244 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004245 return LY_SUCCESS;
4246}
4247
Radek Krejciccd20f12019-02-15 14:12:27 +01004248static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004249lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004250{
4251 struct lys_module *mod;
4252 const char *prefix = NULL, *name;
4253 size_t prefix_len = 0;
4254 struct lysc_node_case *cs;
4255 struct lysc_node *node;
4256
4257 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4258 name = strchr(dflt, ':');
4259 if (name) {
4260 prefix = dflt;
4261 prefix_len = name - prefix;
4262 ++name;
4263 } else {
4264 name = dflt;
4265 }
4266 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4267 if (prefix) {
4268 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4269 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004270 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4271 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004272 return LY_EVALID;
4273 }
4274 } else {
4275 mod = ctx->mod;
4276 }
4277 /* get the default case */
4278 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4279 if (!cs) {
4280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004281 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004282 return LY_EVALID;
4283 }
4284
4285 /* check that there is no mandatory node */
4286 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004287 if (node->parent != (struct lysc_node*)cs) {
4288 break;
4289 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004290 if (node->flags & LYS_MAND_TRUE) {
4291 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004292 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4293 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004294 return LY_EVALID;
4295 }
4296 }
4297
4298 /* set the default case in choice */
4299 ch->dflt = cs;
4300 cs->flags |= LYS_SET_DFLT;
4301
4302 return LY_SUCCESS;
4303}
4304
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004305/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004306 * @brief Compile parsed choice node information.
4307 * @param[in] ctx Compile context
4308 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004309 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004310 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004311 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4312 */
4313static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004314lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004315{
4316 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4317 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4318 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004319 LY_ERR ret = LY_SUCCESS;
4320
Radek Krejci056d0a82018-12-06 16:57:25 +01004321 LY_LIST_FOR(ch_p->child, child_p) {
4322 if (child_p->nodetype == LYS_CASE) {
4323 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004324 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004325 }
4326 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004327 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004328 }
4329 }
4330
4331 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004332 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004333 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004334 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004335
Radek Krejci9800fb82018-12-13 14:26:23 +01004336 return ret;
4337}
4338
4339/**
4340 * @brief Compile parsed anydata or anyxml node information.
4341 * @param[in] ctx Compile context
4342 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004343 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4344 * is enriched with the any-specific information.
4345 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4346 */
4347static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004348lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004349{
4350 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4351 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4352 unsigned int u;
4353 LY_ERR ret = LY_SUCCESS;
4354
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004355 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Michal Vasko5d8756a2019-11-07 15:21:00 +01004356 if (any_p->musts && !(ctx->options & LYSC_OPT_GROUPING)) {
4357 /* do not check "must" semantics in a grouping */
4358 ly_set_add(&ctx->unres, any, 0);
4359 }
Radek Krejci9800fb82018-12-13 14:26:23 +01004360
4361 if (any->flags & LYS_CONFIG_W) {
4362 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004363 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004364 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004365done:
4366 return ret;
4367}
4368
Radek Krejcib56c7502019-02-13 14:19:54 +01004369/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004370 * @brief Connect the node into the siblings list and check its name uniqueness.
4371 *
4372 * @param[in] ctx Compile context
4373 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4374 * the choice itself is expected instead of a specific case node.
4375 * @param[in] node Schema node to connect into the list.
4376 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4377 */
4378static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004379lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004380{
4381 struct lysc_node **children;
4382
4383 if (node->nodetype == LYS_CASE) {
4384 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4385 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004386 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004387 }
4388 if (children) {
4389 if (!(*children)) {
4390 /* first child */
4391 *children = node;
4392 } else if (*children != node) {
4393 /* by the condition in previous branch we cover the choice/case children
4394 * - the children list is shared by the choice and the the first case, in addition
4395 * the first child of each case must be referenced from the case node. So the node is
4396 * actually always already inserted in case it is the first children - so here such
4397 * a situation actually corresponds to the first branch */
4398 /* insert at the end of the parent's children list */
4399 (*children)->prev->next = node;
4400 node->prev = (*children)->prev;
4401 (*children)->prev = node;
4402
4403 /* check the name uniqueness */
4404 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4405 lysc_node_notifs(parent), node->name, node)) {
4406 return LY_EEXIST;
4407 }
4408 }
4409 }
4410 return LY_SUCCESS;
4411}
4412
Radek Krejci95710c92019-02-11 15:49:55 +01004413/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004414 * @brief Prepare the case structure in choice node for the new data node.
4415 *
4416 * 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
4417 * created in the choice when the first child was processed.
4418 *
4419 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004420 * @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,
4421 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004422 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4423 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4424 * @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,
4425 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004426 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004427static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004428lys_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 +01004429{
4430 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004431 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004432 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004433 unsigned int u;
4434 LY_ERR ret;
4435
Radek Krejci95710c92019-02-11 15:49:55 +01004436#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004437 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004438 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004439 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4440 return NULL; \
4441 } \
4442 }
4443
Radek Krejci95710c92019-02-11 15:49:55 +01004444 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4445 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004446
4447 /* we have to add an implicit case node into the parent choice */
4448 cs = calloc(1, sizeof(struct lysc_node_case));
4449 DUP_STRING(ctx->ctx, child->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004450 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004451 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004452 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004453 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4454 /* the case is already present since the child is not its first children */
4455 return (struct lysc_node_case*)ch->cases->prev;
4456 }
Radek Krejci95710c92019-02-11 15:49:55 +01004457 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004458
4459 /* explicit parent case is not present (this is its first child) */
4460 cs = calloc(1, sizeof(struct lysc_node_case));
4461 DUP_STRING(ctx->ctx, node_p->name, cs->name);
Michal Vasko175012e2019-11-06 15:49:14 +01004462 cs->parent = (struct lysc_node*)ch;
Radek Krejci056d0a82018-12-06 16:57:25 +01004463 cs->flags = LYS_STATUS_MASK & node_p->flags;
4464 cs->sp = node_p;
4465
Radek Krejcib1b59152019-01-07 13:21:56 +01004466 /* 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 +02004467 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004468
4469 if (node_p->when) {
4470 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004471 ret = lys_compile_when(ctx, node_p->when, node_p->flags, (struct lysc_node *)cs, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004472 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004473
4474 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4475 /* do not check "when" semantics in a grouping */
4476 ly_set_add(&ctx->unres, cs, 0);
4477 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004478 }
Radek Krejciec4da802019-05-02 13:02:41 +02004479 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004480 } else {
4481 LOGINT(ctx->ctx);
4482 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004483 }
4484 cs->module = ctx->mod;
4485 cs->prev = (struct lysc_node*)cs;
4486 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004487 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004488 cs->child = child;
4489
4490 return cs;
4491error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004492 if (cs) {
4493 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4494 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004495 return NULL;
4496
4497#undef UNIQUE_CHECK
4498}
4499
Radek Krejcib56c7502019-02-13 14:19:54 +01004500/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004501 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004502 *
4503 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004504 * @param[in] node Target node where the config is supposed to be changed.
4505 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004506 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4507 * 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 +01004508 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4509 * @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 +01004510 * @return LY_ERR value.
4511 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004512static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004513lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004514 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004515{
4516 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004517 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004518
4519 if (config == (node->flags & LYS_CONFIG_MASK)) {
4520 /* nothing to do */
4521 return LY_SUCCESS;
4522 }
4523
4524 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004525 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004526 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4527 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004528 "Invalid %s of config - configuration node cannot be child of any state data node.",
4529 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004530 return LY_EVALID;
4531 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004532 node->flags |= LYS_SET_CONFIG;
4533 } else {
4534 if (node->flags & LYS_SET_CONFIG) {
4535 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4536 /* setting config flags, but have node with explicit config true */
4537 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004538 "Invalid %s of config - configuration node cannot be child of any state data node.",
4539 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004540 return LY_EVALID;
4541 }
4542 /* do not change config on nodes where the config is explicitely set, this does not apply to
4543 * nodes, which are being changed explicitly (targets of refine or deviation) */
4544 return LY_SUCCESS;
4545 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004546 }
4547 node->flags &= ~LYS_CONFIG_MASK;
4548 node->flags |= config;
4549
4550 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004551 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004552 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004553 }
4554
Radek Krejci76b3e962018-12-14 17:01:25 +01004555 return LY_SUCCESS;
4556}
4557
Radek Krejcib56c7502019-02-13 14:19:54 +01004558/**
4559 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4560 *
4561 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4562 * the flag to such parents from a mandatory children.
4563 *
4564 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4565 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4566 * (mandatory children was removed).
4567 */
Radek Krejcife909632019-02-12 15:34:42 +01004568void
4569lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4570{
4571 struct lysc_node *iter;
4572
4573 if (add) { /* set flag */
4574 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4575 parent = parent->parent) {
4576 parent->flags |= LYS_MAND_TRUE;
4577 }
4578 } else { /* unset flag */
4579 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004580 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004581 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004582 /* there is another mandatory node */
4583 return;
4584 }
4585 }
4586 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4587 parent->flags &= ~LYS_MAND_TRUE;
4588 }
4589 }
4590}
4591
Radek Krejci056d0a82018-12-06 16:57:25 +01004592/**
Radek Krejci3641f562019-02-13 15:38:40 +01004593 * @brief Internal sorting process for the lys_compile_augment_sort().
4594 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4595 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4596 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4597 */
4598static void
4599lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4600{
4601 unsigned int v;
4602 size_t len;
4603
4604 len = strlen(aug_p->nodeid);
4605 LY_ARRAY_FOR(result, v) {
4606 if (strlen(result[v]->nodeid) <= len) {
4607 continue;
4608 }
4609 if (v < LY_ARRAY_SIZE(result)) {
4610 /* move the rest of array */
4611 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4612 break;
4613 }
4614 }
4615 result[v] = aug_p;
4616 LY_ARRAY_INCREMENT(result);
4617}
4618
4619/**
4620 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4621 *
4622 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4623 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4624 *
4625 * @param[in] ctx Compile context.
4626 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4627 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4628 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4629 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4630 * @return LY_ERR value.
4631 */
4632LY_ERR
4633lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4634{
4635 struct lysp_augment **result = NULL;
4636 unsigned int u, v;
4637 size_t count = 0;
4638
4639 assert(augments);
4640
4641 /* get count of the augments in module and all its submodules */
4642 if (aug_p) {
4643 count += LY_ARRAY_SIZE(aug_p);
4644 }
4645 LY_ARRAY_FOR(inc_p, u) {
4646 if (inc_p[u].submodule->augments) {
4647 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4648 }
4649 }
4650
4651 if (!count) {
4652 *augments = NULL;
4653 return LY_SUCCESS;
4654 }
4655 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4656
4657 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4658 * together, so there can be even /z/y betwwen them. */
4659 LY_ARRAY_FOR(aug_p, u) {
4660 lys_compile_augment_sort_(&aug_p[u], result);
4661 }
4662 LY_ARRAY_FOR(inc_p, u) {
4663 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4664 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4665 }
4666 }
4667
4668 *augments = result;
4669 return LY_SUCCESS;
4670}
4671
4672/**
4673 * @brief Compile the parsed augment connecting it into its target.
4674 *
4675 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4676 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4677 * are already implemented and compiled.
4678 *
4679 * @param[in] ctx Compile context.
4680 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004681 * @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
4682 * children in case of the augmenting uses data.
4683 * @return LY_SUCCESS on success.
4684 * @return LY_EVALID on failure.
4685 */
4686LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004687lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004688{
4689 LY_ERR ret = LY_SUCCESS;
4690 struct lysp_node *node_p, *case_node_p;
4691 struct lysc_node *target; /* target target of the augment */
4692 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004693 struct lysc_when **when, *when_shared;
4694 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004695 uint16_t flags = 0;
4696 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004697 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004698
Radek Krejci327de162019-06-14 12:52:07 +02004699 lysc_update_path(ctx, NULL, "{augment}");
4700 lysc_update_path(ctx, NULL, aug_p->nodeid);
4701
Radek Krejci7af64242019-02-18 13:07:53 +01004702 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004703 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004704 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004705 if (ret != LY_SUCCESS) {
4706 if (ret == LY_EDENIED) {
4707 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4708 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4709 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4710 }
4711 return LY_EVALID;
4712 }
4713
4714 /* check for mandatory nodes
4715 * - new cases augmenting some choice can have mandatory nodes
4716 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4717 */
Radek Krejci733988a2019-02-15 15:12:44 +01004718 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004719 allow_mandatory = 1;
4720 }
4721
4722 when_shared = NULL;
4723 LY_LIST_FOR(aug_p->child, node_p) {
4724 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4725 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4726 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004727 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4728 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004730 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4731 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004732 return LY_EVALID;
4733 }
4734
4735 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004736 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004737 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004738 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004739 } else {
4740 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004741 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004742 }
4743 }
Radek Krejciec4da802019-05-02 13:02:41 +02004744 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004745
Radek Krejcife13da42019-02-15 14:51:01 +01004746 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4747 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004748 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004749 /* 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 +01004750 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 +01004751 } else if (target->nodetype == LYS_CHOICE) {
4752 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4753 node = ((struct lysc_node_choice*)target)->cases->prev;
4754 } else {
4755 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004756 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004757 }
4758
Radek Krejci733988a2019-02-15 15:12:44 +01004759 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004760 node->flags &= ~LYS_MAND_TRUE;
4761 lys_compile_mandatory_parents(target, 0);
4762 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004763 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004764 return LY_EVALID;
4765 }
4766
4767 /* pass augment's when to all the children */
4768 if (aug_p->when) {
4769 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4770 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01004771 ret = lys_compile_when(ctx, aug_p->when, aug_p->flags, target, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004772 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01004773
4774 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4775 /* do not check "when" semantics in a grouping */
Michal Vasko5c4e5892019-11-14 12:31:38 +01004776 ly_set_add(&ctx->unres, node, 0);
Michal Vasko175012e2019-11-06 15:49:14 +01004777 }
4778
Radek Krejci3641f562019-02-13 15:38:40 +01004779 when_shared = *when;
4780 } else {
4781 ++when_shared->refcount;
4782 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01004783
4784 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4785 /* in this case check "when" again for all children because of dummy node check */
4786 ly_set_add(&ctx->unres, node, 0);
4787 }
Radek Krejci3641f562019-02-13 15:38:40 +01004788 }
4789 }
4790 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004791
Radek Krejciec4da802019-05-02 13:02:41 +02004792 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004793 switch (target->nodetype) {
4794 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004795 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004796 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004797 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004798 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004799 break;
4800 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004801 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004802 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004803 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004804 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004805 break;
4806 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004807 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004808 if (aug_p->actions) {
4809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004810 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4811 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004812 return LY_EVALID;
4813 }
4814 if (aug_p->notifs) {
4815 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Michal Vaskoa3881362020-01-21 15:57:35 +01004816 "Invalid augment of %s node which is not allowed to contain notification node \"%s\".",
Radek Krejci327de162019-06-14 12:52:07 +02004817 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004818 return LY_EVALID;
4819 }
4820 }
Radek Krejci3641f562019-02-13 15:38:40 +01004821
Radek Krejci327de162019-06-14 12:52:07 +02004822 lysc_update_path(ctx, NULL, NULL);
4823 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004824error:
Radek Krejciec4da802019-05-02 13:02:41 +02004825 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004826 return ret;
4827}
4828
4829/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004830 * @brief Apply refined or deviated mandatory flag to the target node.
4831 *
4832 * @param[in] ctx Compile context.
4833 * @param[in] node Target node where the mandatory property is supposed to be changed.
4834 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004835 * @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 +01004836 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4837 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4838 * 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 +01004839 * @return LY_ERR value.
4840 */
4841static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004842lys_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 +01004843{
4844 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4845 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004846 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4847 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004848 return LY_EVALID;
4849 }
4850
4851 if (mandatory_flag & LYS_MAND_TRUE) {
4852 /* check if node has default value */
4853 if (node->nodetype & LYS_LEAF) {
4854 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004855 if (refine_flag) {
4856 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004857 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004858 return LY_EVALID;
4859 }
Radek Krejcia1911222019-07-22 17:24:50 +02004860 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004861 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004862 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004863
4864 /* update the list of incomplete default values if needed */
4865 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4866
Radek Krejcia1911222019-07-22 17:24:50 +02004867 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4868 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4869 free(leaf->dflt);
4870 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004871 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004872 }
4873 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004874 if (refine_flag) {
4875 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004876 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004877 return LY_EVALID;
4878 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004879 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004880 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004881 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 +01004882 return LY_EVALID;
4883 }
4884
4885 node->flags &= ~LYS_MAND_FALSE;
4886 node->flags |= LYS_MAND_TRUE;
4887 lys_compile_mandatory_parents(node->parent, 1);
4888 } else {
4889 /* make mandatory false */
4890 node->flags &= ~LYS_MAND_TRUE;
4891 node->flags |= LYS_MAND_FALSE;
4892 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004893 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 +01004894 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004895 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004896 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004897 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4898 leaf->dflt->realtype = leaf->type->dflt->realtype;
4899 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4900 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004901 }
4902 }
4903 return LY_SUCCESS;
4904}
4905
4906/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004907 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4908 * If present, also apply uses's modificators.
4909 *
4910 * @param[in] ctx Compile context
4911 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004912 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4913 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4914 * the compile context.
4915 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4916 */
4917static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004918lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004919{
4920 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004921 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004922 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004923 struct lysc_node_container context_node_fake =
4924 {.nodetype = LYS_CONTAINER,
4925 .module = ctx->mod,
4926 .flags = parent ? parent->flags : 0,
4927 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004928 .prev = (struct lysc_node*)&context_node_fake,
4929 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004930 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004931 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004932 int found;
4933 const char *id, *name, *prefix;
4934 size_t prefix_len, name_len;
4935 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004936 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004937 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004938 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004939 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004940 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004941 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004942 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004943 unsigned int actions_index, notifs_index;
4944 struct lysc_notif **notifs = NULL;
4945 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004946
4947 /* search for the grouping definition */
4948 found = 0;
4949 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004950 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004951 if (prefix) {
4952 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4953 if (!mod) {
4954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004955 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004956 return LY_EVALID;
4957 }
4958 } else {
4959 mod = ctx->mod_def;
4960 }
4961 if (mod == ctx->mod_def) {
4962 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004963 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004964 LY_ARRAY_FOR(grp, u) {
4965 if (!strcmp(grp[u].name, name)) {
4966 grp = &grp[u];
4967 found = 1;
4968 break;
4969 }
4970 }
4971 }
4972 }
4973 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004974 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004975 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004976 if (grp) {
4977 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4978 if (!strcmp(grp[u].name, name)) {
4979 grp = &grp[u];
4980 found = 1;
4981 }
4982 }
4983 }
4984 if (!found && mod->parsed->includes) {
4985 /* ... and all the submodules */
4986 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4987 grp = mod->parsed->includes[u].submodule->groupings;
4988 if (grp) {
4989 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4990 if (!strcmp(grp[v].name, name)) {
4991 grp = &grp[v];
4992 found = 1;
4993 }
4994 }
4995 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004996 }
4997 }
4998 }
4999 if (!found) {
5000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5001 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
5002 return LY_EVALID;
5003 }
5004
5005 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
5006 grp_stack_count = ctx->groupings.count;
5007 ly_set_add(&ctx->groupings, (void*)grp, 0);
5008 if (grp_stack_count == ctx->groupings.count) {
5009 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
5010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5011 "Grouping \"%s\" references itself through a uses statement.", grp->name);
5012 return LY_EVALID;
5013 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005014 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5015 /* remember that the grouping is instantiated to avoid its standalone validation */
5016 grp->flags |= LYS_USED_GRP;
5017 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005018
5019 /* switch context's mod_def */
5020 mod_old = ctx->mod_def;
5021 ctx->mod_def = mod;
5022
5023 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005024 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 +01005025
Radek Krejcifc11bd72019-04-11 16:00:05 +02005026 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01005027 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01005028 /* 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 +02005029 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 +01005030
5031 /* some preparation for applying refines */
5032 if (grp->data == node_p) {
5033 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02005034 if (parent) {
5035 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
5036 } else if (ctx->mod->compiled->data) {
5037 child = ctx->mod->compiled->data;
5038 } else {
5039 child = NULL;
5040 }
5041 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01005042 }
5043 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005044 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01005045 LY_LIST_FOR(context_node_fake.child, child) {
5046 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01005047
Radek Krejcifc11bd72019-04-11 16:00:05 +02005048 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01005049 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005050 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01005051 if (!when_shared) {
Michal Vasko175012e2019-11-06 15:49:14 +01005052 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, uses_p->flags, parent, when), cleanup);
5053
5054 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5055 /* do not check "when" semantics in a grouping */
5056 ly_set_add(&ctx->unres, child, 0);
5057 }
5058
Radek Krejci00b874b2019-02-12 10:54:50 +01005059 when_shared = *when;
5060 } else {
5061 ++when_shared->refcount;
5062 (*when) = when_shared;
Michal Vasko5c4e5892019-11-14 12:31:38 +01005063
5064 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5065 /* in this case check "when" again for all children because of dummy node check */
5066 ly_set_add(&ctx->unres, child, 0);
5067 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005068 }
5069 }
Radek Krejci01342af2019-01-03 15:18:08 +01005070 }
5071 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005072 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01005073 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005074 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02005075 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 +01005076 }
5077
Radek Krejcifc11bd72019-04-11 16:00:05 +02005078 /* compile actions */
5079 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
5080 if (actions) {
5081 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005082 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005083 if (*actions && (uses_p->augments || uses_p->refines)) {
5084 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
5085 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
5086 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
5087 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5088 }
5089 }
5090
5091 /* compile notifications */
5092 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
5093 if (notifs) {
5094 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02005095 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005096 if (*notifs && (uses_p->augments || uses_p->refines)) {
5097 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
5098 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
5099 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
5100 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5101 }
5102 }
5103
5104
Radek Krejci3641f562019-02-13 15:38:40 +01005105 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005106 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005107 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02005108 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01005109 }
Radek Krejci12fb9142019-01-08 09:45:30 +01005110
Radek Krejcif0089082019-01-07 16:42:01 +01005111 /* reload previous context's mod_def */
5112 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02005113 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01005114
Radek Krejci76b3e962018-12-14 17:01:25 +01005115 /* apply refine */
5116 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02005117 lysc_update_path(ctx, NULL, rfn->nodeid);
5118
Radek Krejci7af64242019-02-18 13:07:53 +01005119 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 +01005120 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02005121 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01005122 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01005123
5124 /* default value */
5125 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01005126 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01005127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005128 "Invalid refine of default - %s cannot hold %d default values.",
5129 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005130 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005131 }
5132 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
5133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005134 "Invalid refine of default - %s cannot hold default value(s).",
5135 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005136 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005137 }
5138 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02005139 struct ly_err_item *err = NULL;
5140 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
5141 if (leaf->dflt) {
5142 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02005143 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005144 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5145 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5146 } else {
5147 /* prepare a new one */
5148 leaf->dflt = calloc(1, sizeof *leaf->dflt);
5149 leaf->dflt->realtype = leaf->type;
5150 }
5151 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005152 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005153 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
5154 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005155 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005156 leaf->dflt->realtype->refcount++;
5157 if (err) {
5158 ly_err_print(err);
5159 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5160 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
5161 ly_err_free(err);
5162 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005163 if (rc == LY_EINCOMPLETE) {
5164 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005165 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005166
5167 /* but in general result is so far ok */
5168 rc = LY_SUCCESS;
5169 }
Radek Krejcia1911222019-07-22 17:24:50 +02005170 LY_CHECK_GOTO(rc, cleanup);
5171 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005172 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02005173 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
5174
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005175 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01005176 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5177 "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 +02005178 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01005179 }
Radek Krejcia1911222019-07-22 17:24:50 +02005180
5181 /* remove previous set of default values */
5182 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005183 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005184 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5185 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5186 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005187 }
Radek Krejcia1911222019-07-22 17:24:50 +02005188 LY_ARRAY_FREE(llist->dflts);
5189 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005190 LY_ARRAY_FREE(llist->dflts_mods);
5191 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005192
5193 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005194 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005195 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005196 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005197 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005198 LY_ARRAY_INCREMENT(llist->dflts_mods);
5199 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005200 LY_ARRAY_INCREMENT(llist->dflts);
5201 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5202 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005203 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005204 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005205 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005206 llist->dflts[u]->realtype->refcount++;
5207 if (err) {
5208 ly_err_print(err);
5209 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5210 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5211 ly_err_free(err);
5212 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005213 if (rc == LY_EINCOMPLETE) {
5214 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005215 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005216
5217 /* but in general result is so far ok */
5218 rc = LY_SUCCESS;
5219 }
5220 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005221 }
Radek Krejcia1911222019-07-22 17:24:50 +02005222 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005223 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005224 if (((struct lysc_node_choice*)node)->dflt) {
5225 /* unset LYS_SET_DFLT from the current default case */
5226 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5227 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005228 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 +01005229 }
5230 }
5231
Radek Krejci12fb9142019-01-08 09:45:30 +01005232 /* description */
5233 if (rfn->dsc) {
5234 FREE_STRING(ctx->ctx, node->dsc);
5235 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5236 }
5237
5238 /* reference */
5239 if (rfn->ref) {
5240 FREE_STRING(ctx->ctx, node->ref);
5241 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5242 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005243
5244 /* config */
5245 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005246 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005247 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005248 } else {
5249 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
Michal Vaskoa3881362020-01-21 15:57:35 +01005250 flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action", ctx->path);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005251 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005252 }
5253
5254 /* mandatory */
5255 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005256 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005257 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005258
5259 /* presence */
5260 if (rfn->presence) {
5261 if (node->nodetype != LYS_CONTAINER) {
5262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005263 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5264 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005265 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005266 }
5267 node->flags |= LYS_PRESENCE;
5268 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005269
5270 /* must */
5271 if (rfn->musts) {
5272 switch (node->nodetype) {
5273 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005274 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 +01005275 break;
5276 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005277 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 +01005278 break;
5279 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005280 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 +01005281 break;
5282 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005283 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 +01005284 break;
5285 case LYS_ANYXML:
5286 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005287 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 +01005288 break;
5289 default:
5290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005291 "Invalid refine of must statement - %s cannot hold any must statement.",
5292 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005293 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005294 }
Michal Vasko5d8756a2019-11-07 15:21:00 +01005295 ly_set_add(&ctx->unres, node, 0);
Radek Krejci9a564c92019-01-07 14:53:57 +01005296 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005297
5298 /* min/max-elements */
5299 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5300 switch (node->nodetype) {
5301 case LYS_LEAFLIST:
5302 if (rfn->flags & LYS_SET_MAX) {
5303 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5304 }
5305 if (rfn->flags & LYS_SET_MIN) {
5306 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005307 if (rfn->min) {
5308 node->flags |= LYS_MAND_TRUE;
5309 lys_compile_mandatory_parents(node->parent, 1);
5310 } else {
5311 node->flags &= ~LYS_MAND_TRUE;
5312 lys_compile_mandatory_parents(node->parent, 0);
5313 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005314 }
5315 break;
5316 case LYS_LIST:
5317 if (rfn->flags & LYS_SET_MAX) {
5318 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5319 }
5320 if (rfn->flags & LYS_SET_MIN) {
5321 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005322 if (rfn->min) {
5323 node->flags |= LYS_MAND_TRUE;
5324 lys_compile_mandatory_parents(node->parent, 1);
5325 } else {
5326 node->flags &= ~LYS_MAND_TRUE;
5327 lys_compile_mandatory_parents(node->parent, 0);
5328 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005329 }
5330 break;
5331 default:
5332 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005333 "Invalid refine of %s statement - %s cannot hold this statement.",
5334 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005335 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005336 }
5337 }
Radek Krejcif0089082019-01-07 16:42:01 +01005338
5339 /* if-feature */
5340 if (rfn->iffeatures) {
5341 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005342 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005343 }
Radek Krejci327de162019-06-14 12:52:07 +02005344
5345 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005346 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005347
Radek Krejcif2271f12019-01-07 16:42:23 +01005348 /* do some additional checks of the changed nodes when all the refines are applied */
5349 for (u = 0; u < refined.count; ++u) {
5350 node = (struct lysc_node*)refined.objs[u];
5351 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005352 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005353
5354 /* check possible conflict with default value (default added, mandatory left true) */
5355 if ((node->flags & LYS_MAND_TRUE) &&
5356 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5357 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5358 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005359 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005360 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005361 }
5362
5363 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5364 if (node->nodetype == LYS_LIST) {
5365 min = ((struct lysc_node_list*)node)->min;
5366 max = ((struct lysc_node_list*)node)->max;
5367 } else {
5368 min = ((struct lysc_node_leaflist*)node)->min;
5369 max = ((struct lysc_node_leaflist*)node)->max;
5370 }
5371 if (min > max) {
5372 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005373 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5374 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005375 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005376 }
5377 }
5378 }
5379
Radek Krejci327de162019-06-14 12:52:07 +02005380 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005381 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005382
5383cleanup:
5384 /* fix connection of the children nodes from fake context node back into the parent */
5385 if (context_node_fake.child) {
5386 context_node_fake.child->prev = child;
5387 }
5388 LY_LIST_FOR(context_node_fake.child, child) {
5389 child->parent = parent;
5390 }
5391
5392 if (uses_p->augments || uses_p->refines) {
5393 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005394 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005395 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5396 LY_ARRAY_FREE(context_node_fake.actions);
5397 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005398 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005399 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5400 LY_ARRAY_FREE(context_node_fake.notifs);
5401 }
5402 }
5403
Radek Krejcie86bf772018-12-14 11:39:53 +01005404 /* reload previous context's mod_def */
5405 ctx->mod_def = mod_old;
5406 /* remove the grouping from the stack for circular groupings dependency check */
5407 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5408 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005409 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005410 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005411
5412 return ret;
5413}
5414
Radek Krejci327de162019-06-14 12:52:07 +02005415static int
5416lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5417{
5418 struct lysp_node *iter;
5419 int len = 0;
5420
5421 *path = NULL;
5422 for (iter = node; iter && len >= 0; iter = iter->parent) {
5423 char *s = *path;
5424 char *id;
5425
5426 switch (iter->nodetype) {
5427 case LYS_USES:
5428 asprintf(&id, "{uses='%s'}", iter->name);
5429 break;
5430 case LYS_GROUPING:
5431 asprintf(&id, "{grouping='%s'}", iter->name);
5432 break;
5433 case LYS_AUGMENT:
5434 asprintf(&id, "{augment='%s'}", iter->name);
5435 break;
5436 default:
5437 id = strdup(iter->name);
5438 break;
5439 }
5440
5441 if (!iter->parent) {
5442 /* print prefix */
5443 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5444 } else {
5445 /* prefix is the same as in parent */
5446 len = asprintf(path, "/%s%s", id, s ? s : "");
5447 }
5448 free(s);
5449 free(id);
5450 }
5451
5452 if (len < 0) {
5453 free(*path);
5454 *path = NULL;
5455 } else if (len == 0) {
5456 *path = strdup("/");
5457 len = 1;
5458 }
5459 return len;
5460}
5461
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005462/**
5463 * @brief Validate groupings that were defined but not directly used in the schema itself.
5464 *
5465 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5466 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5467 */
5468static LY_ERR
5469lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5470{
5471 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005472 char *path;
5473 int len;
5474
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005475 struct lysp_node_uses fake_uses = {
5476 .parent = node_p,
5477 .nodetype = LYS_USES,
5478 .flags = 0, .next = NULL,
5479 .name = grp->name,
5480 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5481 .refines = NULL, .augments = NULL
5482 };
5483 struct lysc_node_container fake_container = {
5484 .nodetype = LYS_CONTAINER,
5485 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5486 .module = ctx->mod,
5487 .sp = NULL, .parent = NULL, .next = NULL,
5488 .prev = (struct lysc_node*)&fake_container,
5489 .name = "fake",
5490 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5491 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5492 };
5493
5494 if (grp->parent) {
5495 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5496 }
Radek Krejci327de162019-06-14 12:52:07 +02005497
5498 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5499 if (len < 0) {
5500 LOGMEM(ctx->ctx);
5501 return LY_EMEM;
5502 }
5503 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5504 ctx->path_len = (uint16_t)len;
5505 free(path);
5506
5507 lysc_update_path(ctx, NULL, "{grouping}");
5508 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005509 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005510 lysc_update_path(ctx, NULL, NULL);
5511 lysc_update_path(ctx, NULL, NULL);
5512
5513 ctx->path_len = 1;
5514 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005515
5516 /* cleanup */
5517 lysc_node_container_free(ctx->ctx, &fake_container);
5518
5519 return ret;
5520}
Radek Krejcife909632019-02-12 15:34:42 +01005521
Radek Krejcie86bf772018-12-14 11:39:53 +01005522/**
Radek Krejcia3045382018-11-22 14:30:31 +01005523 * @brief Compile parsed schema node information.
5524 * @param[in] ctx Compile context
5525 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005526 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5527 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5528 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005529 * @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).
5530 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005531 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5532 */
Radek Krejci19a96102018-11-15 13:38:09 +01005533static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005534lys_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 +01005535{
5536 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005537 struct lysc_node *node;
5538 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005539 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005540 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005541 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005542
Radek Krejci327de162019-06-14 12:52:07 +02005543 if (node_p->nodetype != LYS_USES) {
5544 lysc_update_path(ctx, parent, node_p->name);
5545 } else {
5546 lysc_update_path(ctx, NULL, "{uses}");
5547 lysc_update_path(ctx, NULL, node_p->name);
5548 }
5549
Radek Krejci19a96102018-11-15 13:38:09 +01005550 switch (node_p->nodetype) {
5551 case LYS_CONTAINER:
5552 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5553 node_compile_spec = lys_compile_node_container;
5554 break;
5555 case LYS_LEAF:
5556 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5557 node_compile_spec = lys_compile_node_leaf;
5558 break;
5559 case LYS_LIST:
5560 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005561 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005562 break;
5563 case LYS_LEAFLIST:
5564 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005565 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005566 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005567 case LYS_CHOICE:
5568 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005569 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005570 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005571 case LYS_ANYXML:
5572 case LYS_ANYDATA:
5573 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005574 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005575 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005576 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005577 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5578 lysc_update_path(ctx, NULL, NULL);
5579 lysc_update_path(ctx, NULL, NULL);
5580 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005581 default:
5582 LOGINT(ctx->ctx);
5583 return LY_EINT;
5584 }
5585 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5586 node->nodetype = node_p->nodetype;
5587 node->module = ctx->mod;
5588 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005589 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005590
5591 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005592 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005593 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005594 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005595 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5596 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005597 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005598 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005599 node->flags |= LYS_CONFIG_R;
5600 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005601 /* config not explicitely set, inherit it from parent */
5602 if (parent) {
5603 node->flags |= parent->flags & LYS_CONFIG_MASK;
5604 } else {
5605 /* default is config true */
5606 node->flags |= LYS_CONFIG_W;
5607 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005608 } else {
5609 /* config set explicitely */
5610 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005611 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005612 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5613 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5614 "Configuration node cannot be child of any state data node.");
5615 goto error;
5616 }
Radek Krejci19a96102018-11-15 13:38:09 +01005617
Radek Krejcia6d57732018-11-29 13:40:37 +01005618 /* *list ordering */
5619 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5620 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005621 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005622 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5623 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005624 node->flags &= ~LYS_ORDBY_MASK;
5625 node->flags |= LYS_ORDBY_SYSTEM;
5626 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5627 /* default ordering is system */
5628 node->flags |= LYS_ORDBY_SYSTEM;
5629 }
5630 }
5631
Radek Krejci19a96102018-11-15 13:38:09 +01005632 /* status - it is not inherited by specification, but it does not make sense to have
5633 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005634 if (!parent || parent->nodetype != LYS_CHOICE) {
5635 /* in case of choice/case's children, postpone the check to the moment we know if
5636 * 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 +01005637 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 +01005638 }
5639
Radek Krejciec4da802019-05-02 13:02:41 +02005640 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005641 node->sp = node_p;
5642 }
5643 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005644 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5645 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005646 if (node_p->when) {
5647 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005648 ret = lys_compile_when(ctx, node_p->when, node_p->flags, node, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005649 LY_CHECK_GOTO(ret, error);
Michal Vasko175012e2019-11-06 15:49:14 +01005650
5651 if (!(ctx->options & LYSC_OPT_GROUPING)) {
5652 /* do not check "when" semantics in a grouping */
5653 ly_set_add(&ctx->unres, node, 0);
5654 }
Radek Krejci00b874b2019-02-12 10:54:50 +01005655 }
Radek Krejciec4da802019-05-02 13:02:41 +02005656 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005657
5658 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005659 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005660
Radek Krejci0935f412019-08-20 16:15:18 +02005661 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5662
Radek Krejcife909632019-02-12 15:34:42 +01005663 /* inherit LYS_MAND_TRUE in parent containers */
5664 if (node->flags & LYS_MAND_TRUE) {
5665 lys_compile_mandatory_parents(parent, 1);
5666 }
5667
Radek Krejci327de162019-06-14 12:52:07 +02005668 lysc_update_path(ctx, NULL, NULL);
5669
Radek Krejci19a96102018-11-15 13:38:09 +01005670 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005671 if (parent) {
5672 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005673 if (node_p->parent->nodetype == LYS_CASE) {
5674 lysc_update_path(ctx, parent, node_p->parent->name);
5675 } else {
5676 lysc_update_path(ctx, parent, node->name);
5677 }
Radek Krejciec4da802019-05-02 13:02:41 +02005678 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005679 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005680 if (uses_status) {
5681
5682 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005683 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005684 * it directly gets the same status flags as the choice;
5685 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005686 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005687 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005688 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005689 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005690 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005691 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005692 }
Radek Krejciec4da802019-05-02 13:02:41 +02005693 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 +02005694
5695 if (parent->nodetype == LYS_CHOICE) {
5696 lysc_update_path(ctx, NULL, NULL);
5697 }
Radek Krejci19a96102018-11-15 13:38:09 +01005698 } else {
5699 /* top-level element */
5700 if (!ctx->mod->compiled->data) {
5701 ctx->mod->compiled->data = node;
5702 } else {
5703 /* insert at the end of the module's top-level nodes list */
5704 ctx->mod->compiled->data->prev->next = node;
5705 node->prev = ctx->mod->compiled->data->prev;
5706 ctx->mod->compiled->data->prev = node;
5707 }
Radek Krejci327de162019-06-14 12:52:07 +02005708 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005709 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5710 ctx->mod->compiled->notifs, node->name, node)) {
5711 return LY_EVALID;
5712 }
Radek Krejci19a96102018-11-15 13:38:09 +01005713 }
Radek Krejci327de162019-06-14 12:52:07 +02005714 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005715
5716 return LY_SUCCESS;
5717
5718error:
5719 lysc_node_free(ctx->ctx, node);
5720 return ret;
5721}
5722
Radek Krejciccd20f12019-02-15 14:12:27 +01005723static void
5724lysc_disconnect(struct lysc_node *node)
5725{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005726 struct lysc_node *parent, *child, *prev = NULL, *next;
5727 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005728 int remove_cs = 0;
5729
5730 parent = node->parent;
5731
5732 /* parent's first child */
5733 if (!parent) {
5734 return;
5735 }
5736 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005737 cs = (struct lysc_node_case*)node;
5738 } else if (parent->nodetype == LYS_CASE) {
5739 /* disconnecting some node in a case */
5740 cs = (struct lysc_node_case*)parent;
5741 parent = cs->parent;
5742 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5743 if (child == node) {
5744 if (cs->child == child) {
5745 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5746 /* case with a single child -> remove also the case */
5747 child->parent = NULL;
5748 remove_cs = 1;
5749 } else {
5750 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005751 }
5752 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005753 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005754 }
5755 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005756 if (!remove_cs) {
5757 cs = NULL;
5758 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005759 } else if (lysc_node_children(parent, node->flags) == node) {
5760 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005761 }
5762
5763 if (cs) {
5764 if (remove_cs) {
5765 /* cs has only one child which is being also removed */
5766 lysc_disconnect((struct lysc_node*)cs);
5767 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5768 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005769 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5770 /* default case removed */
5771 ((struct lysc_node_choice*)parent)->dflt = NULL;
5772 }
5773 if (((struct lysc_node_choice*)parent)->cases == cs) {
5774 /* first case removed */
5775 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5776 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005777 if (cs->child) {
5778 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5779 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5780 prev = cs->child->prev;
5781 } /* else all the children are under a single case */
5782 LY_LIST_FOR_SAFE(cs->child, next, child) {
5783 if (child->parent != (struct lysc_node*)cs) {
5784 break;
5785 }
5786 lysc_node_free(node->module->ctx, child);
5787 }
5788 if (prev) {
5789 if (prev->next) {
5790 prev->next = child;
5791 }
5792 if (child) {
5793 child->prev = prev;
5794 } else {
5795 /* link from the first child under the cases */
5796 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5797 }
5798 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005799 }
5800 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005801 }
5802
5803 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005804 if (node->prev->next) {
5805 node->prev->next = node->next;
5806 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005807 if (node->next) {
5808 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005809 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005810 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005811 if (child) {
5812 child->prev = node->prev;
5813 }
5814 } else if (((struct lysc_node_choice*)parent)->cases) {
5815 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005816 }
5817}
5818
5819LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005820lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005821{
Radek Krejcia1911222019-07-22 17:24:50 +02005822 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005823 struct ly_set devs_p = {0};
5824 struct ly_set targets = {0};
5825 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005826 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005827 struct lysc_action *rpcs;
5828 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005829 struct lysp_deviation *dev;
5830 struct lysp_deviate *d, **dp_new;
5831 struct lysp_deviate_add *d_add;
5832 struct lysp_deviate_del *d_del;
5833 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005834 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005835 struct lysc_deviation {
5836 const char *nodeid;
5837 struct lysc_node *target; /* target node of the deviation */
5838 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005839 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005840 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5841 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005842 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005843 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005844 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005845 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005846 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005847 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005848
5849 /* get all deviations from the module and all its submodules ... */
5850 LY_ARRAY_FOR(mod_p->deviations, u) {
5851 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5852 }
5853 LY_ARRAY_FOR(mod_p->includes, v) {
5854 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5855 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5856 }
5857 }
5858 if (!devs_p.count) {
5859 /* nothing to do */
5860 return LY_SUCCESS;
5861 }
5862
Radek Krejci327de162019-06-14 12:52:07 +02005863 lysc_update_path(ctx, NULL, "{deviation}");
5864
Radek Krejciccd20f12019-02-15 14:12:27 +01005865 /* ... and group them by the target node */
5866 devs = calloc(devs_p.count, sizeof *devs);
5867 for (u = 0; u < devs_p.count; ++u) {
5868 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005869 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005870
5871 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005872 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5873 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005874 if (target->nodetype == LYS_ACTION) {
5875 /* move the target pointer to input/output to make them different from the action and
5876 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5877 * back to the RPC/action node due to a better compatibility and decision code in this function.
5878 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5879 if (flags & LYSC_OPT_RPC_INPUT) {
5880 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5881 flags |= LYSC_OPT_INTERNAL;
5882 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5883 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5884 flags |= LYSC_OPT_INTERNAL;
5885 }
5886 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005887 /* insert into the set of targets with duplicity detection */
5888 i = ly_set_add(&targets, target, 0);
5889 if (!devs[i]) {
5890 /* new record */
5891 devs[i] = calloc(1, sizeof **devs);
5892 devs[i]->target = target;
5893 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005894 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005895 }
5896 /* add deviates into the deviation's list of deviates */
5897 for (d = dev->deviates; d; d = d->next) {
5898 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5899 *dp_new = d;
5900 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5901 devs[i]->not_supported = 1;
5902 }
5903 }
Radek Krejci327de162019-06-14 12:52:07 +02005904
5905 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005906 }
5907
5908 /* MACROS for deviates checking */
5909#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5910 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005911 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 +01005912 goto cleanup; \
5913 }
5914
5915#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5916 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005917 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5918 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005919 goto cleanup; \
5920 }
5921
Radek Krejcia1911222019-07-22 17:24:50 +02005922
Radek Krejciccd20f12019-02-15 14:12:27 +01005923#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005924 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5925 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5926 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5927 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5928 goto cleanup; \
5929 }
5930
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005931#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005932 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005933 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005934 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5935 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005937 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5938 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005939 goto cleanup; \
5940 }
5941
Radek Krejci551b12c2019-02-19 16:11:21 +01005942#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5943 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5944 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005945 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5946 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005947 goto cleanup; \
5948 }
5949
Radek Krejciccd20f12019-02-15 14:12:27 +01005950#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5951 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005952 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005953 goto cleanup; \
5954 }
5955
Radek Krejci551b12c2019-02-19 16:11:21 +01005956#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5957 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5958 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005959 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005960 goto cleanup; \
5961 }
5962
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005963#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5964 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5965 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5966 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5967 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 +01005968 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005969 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005970 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005971 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5972 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005973 goto cleanup; \
5974 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005975 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5976 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5977 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5978 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5979 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005980 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005981 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5982 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5983 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005984 }
5985
5986 /* apply deviations */
5987 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005988 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5989 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5990 struct ly_err_item *err = NULL;
5991
5992 dflt = NULL;
5993 changed_type = 0;
5994
Radek Krejci327de162019-06-14 12:52:07 +02005995 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5996
Radek Krejcif538ce52019-03-05 10:46:14 +01005997 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5998 /* fix the target pointer in case of RPC's/action's input/output */
5999 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6000 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
6001 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6002 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
6003 }
6004 }
6005
Radek Krejciccd20f12019-02-15 14:12:27 +01006006 /* not-supported */
6007 if (devs[u]->not_supported) {
6008 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
6009 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
6010 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
6011 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02006012
6013#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
6014 if (devs[u]->target->parent) { \
6015 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
6016 } else { \
6017 ARRAY = devs[u]->target->module->compiled->ARRAY; \
6018 } \
6019 LY_ARRAY_FOR(ARRAY, x) { \
6020 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
6021 } \
6022 if (x < LY_ARRAY_SIZE(ARRAY)) { \
6023 FREEFUNC(ctx->ctx, &ARRAY[x]); \
6024 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
6025 LY_ARRAY_DECREMENT(ARRAY); \
6026 }
6027
Radek Krejcif538ce52019-03-05 10:46:14 +01006028 if (devs[u]->target->nodetype == LYS_ACTION) {
6029 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6030 /* remove RPC's/action's input */
6031 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
6032 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02006033 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
6034 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01006035 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6036 /* remove RPC's/action's output */
6037 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
6038 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02006039 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
6040 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01006041 } else {
6042 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02006043 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01006044 }
6045 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02006046 /* remove Notification */
6047 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01006048 } else {
6049 /* remove the target node */
6050 lysc_disconnect(devs[u]->target);
6051 lysc_node_free(ctx->ctx, devs[u]->target);
6052 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006053
Radek Krejci474f9b82019-07-24 11:36:37 +02006054 /* mark the context for later re-compilation of objects that could reference the curently removed node */
6055 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01006056 continue;
6057 }
6058
6059 /* list of deviates (not-supported is not present in the list) */
6060 LY_ARRAY_FOR(devs[u]->deviates, v) {
6061 d = devs[u]->deviates[v];
6062
6063 switch (d->mod) {
6064 case LYS_DEV_ADD:
6065 d_add = (struct lysp_deviate_add*)d;
6066 /* [units-stmt] */
6067 if (d_add->units) {
6068 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
6069 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
6070
6071 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6072 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6073 }
6074
6075 /* *must-stmt */
6076 if (d_add->musts) {
6077 switch (devs[u]->target->nodetype) {
6078 case LYS_CONTAINER:
6079 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006080 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
6081 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006082 break;
6083 case LYS_LEAF:
6084 case LYS_LEAFLIST:
6085 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006086 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
6087 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006088 break;
6089 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006090 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
6091 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006092 break;
6093 case LYS_ACTION:
6094 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006095 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
6096 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006097 break;
6098 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02006099 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
6100 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006101 break;
6102 }
6103 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006104 default:
6105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006106 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006107 goto cleanup;
6108 }
Michal Vasko5d8756a2019-11-07 15:21:00 +01006109 ly_set_add(&ctx->unres, devs[u]->target, 0);
Radek Krejciccd20f12019-02-15 14:12:27 +01006110 }
6111
6112 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006113 if (d_add->uniques) {
6114 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
6115 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
6116 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006117
6118 /* *default-stmt */
6119 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006120 switch (devs[u]->target->nodetype) {
6121 case LYS_LEAF:
6122 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006123 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 +02006124 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006125 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006126 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006127 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6128 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6129 } else {
6130 /* prepare new default value storage */
6131 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01006132 }
Radek Krejcia1911222019-07-22 17:24:50 +02006133 dflt = d_add->dflts[0];
6134 /* parsing is done at the end after possible replace of the leaf's type */
6135
Radek Krejci551b12c2019-02-19 16:11:21 +01006136 /* mark the new default values as leaf's own */
6137 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006138 break;
6139 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006140 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006141 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02006142 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006143 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006144 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6145 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6146 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006147 }
Radek Krejcia1911222019-07-22 17:24:50 +02006148 LY_ARRAY_FREE(llist->dflts);
6149 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006150 LY_ARRAY_FREE(llist->dflts_mods);
6151 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006152 }
6153 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006154 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006155 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
6156 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01006157 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006158 LY_ARRAY_INCREMENT(llist->dflts_mods);
6159 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006160 LY_ARRAY_INCREMENT(llist->dflts);
6161 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
6162 llist->dflts[x]->realtype = llist->type;
6163 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 +02006164 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006165 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006166 llist->dflts[x]->realtype->refcount++;
6167 if (err) {
6168 ly_err_print(err);
6169 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6170 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
6171 d_add->dflts[x - y], err->msg);
6172 ly_err_free(err);
6173 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006174 if (rc == LY_EINCOMPLETE) {
6175 /* postpone default compilation when the tree is complete */
6176 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6177
6178 /* but in general result is so far ok */
6179 rc = LY_SUCCESS;
6180 }
Radek Krejcia1911222019-07-22 17:24:50 +02006181 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006182 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006183 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01006184 devs[u]->target->flags |= LYS_SET_DFLT;
6185 break;
6186 case LYS_CHOICE:
6187 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6188 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6189 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6190 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006191 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 +01006192 goto cleanup;
6193 }
6194 break;
6195 default:
6196 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006197 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006198 goto cleanup;
6199 }
6200 }
6201
6202 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006203 if (d_add->flags & LYS_CONFIG_MASK) {
6204 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006205 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006206 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6207 goto cleanup;
6208 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006209 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006210 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
Michal Vaskoa3881362020-01-21 15:57:35 +01006211 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006212 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006213 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6214 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006215 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6216 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006217 goto cleanup;
6218 }
Radek Krejci327de162019-06-14 12:52:07 +02006219 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006220 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006221
6222 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006223 if (d_add->flags & LYS_MAND_MASK) {
6224 if (devs[u]->target->flags & LYS_MAND_MASK) {
6225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006226 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6227 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006228 goto cleanup;
6229 }
Radek Krejci327de162019-06-14 12:52:07 +02006230 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006231 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006232
6233 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006234 if (d_add->flags & LYS_SET_MIN) {
6235 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6236 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6237 /* change value */
6238 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6239 } else if (devs[u]->target->nodetype == LYS_LIST) {
6240 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6241 /* change value */
6242 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6243 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006245 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6246 goto cleanup;
6247 }
6248 if (d_add->min) {
6249 devs[u]->target->flags |= LYS_MAND_TRUE;
6250 }
6251 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006252
6253 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006254 if (d_add->flags & LYS_SET_MAX) {
6255 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6256 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6257 /* change value */
6258 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6259 } else if (devs[u]->target->nodetype == LYS_LIST) {
6260 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6261 /* change value */
6262 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6263 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006264 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006265 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6266 goto cleanup;
6267 }
6268 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006269
6270 break;
6271 case LYS_DEV_DELETE:
6272 d_del = (struct lysp_deviate_del*)d;
6273
6274 /* [units-stmt] */
6275 if (d_del->units) {
6276 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006277 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6278 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6279 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6280 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6281 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6282 goto cleanup;
6283 }
6284 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6285 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006286 }
6287
6288 /* *must-stmt */
6289 if (d_del->musts) {
6290 switch (devs[u]->target->nodetype) {
6291 case LYS_CONTAINER:
6292 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006293 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006294 break;
6295 case LYS_LEAF:
6296 case LYS_LEAFLIST:
6297 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006298 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006299 break;
6300 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006301 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006302 break;
6303 case LYS_ACTION:
6304 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6305 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6306 break;
6307 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6308 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6309 break;
6310 }
6311 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006312 default:
6313 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006314 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006315 goto cleanup;
6316 }
6317 }
6318
6319 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006320 if (d_del->uniques) {
6321 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6322 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6323 LY_ARRAY_FOR(d_del->uniques, x) {
6324 LY_ARRAY_FOR(list->uniques, z) {
6325 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6326 nodeid = strpbrk(name, " \t\n");
6327 if (nodeid) {
Radek Krejci7f9b6512019-09-18 13:11:09 +02006328 if (ly_strncmp(list->uniques[z][y]->name, name, nodeid - name)) {
Radek Krejci7af64242019-02-18 13:07:53 +01006329 break;
6330 }
6331 while (isspace(*nodeid)) {
6332 ++nodeid;
6333 }
6334 } else {
6335 if (strcmp(name, list->uniques[z][y]->name)) {
6336 break;
6337 }
6338 }
6339 }
6340 if (!name) {
6341 /* complete match - remove the unique */
6342 LY_ARRAY_DECREMENT(list->uniques);
6343 LY_ARRAY_FREE(list->uniques[z]);
6344 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6345 --z;
6346 break;
6347 }
6348 }
6349 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6350 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006351 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6352 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006353 goto cleanup;
6354 }
6355 }
6356 if (!LY_ARRAY_SIZE(list->uniques)) {
6357 LY_ARRAY_FREE(list->uniques);
6358 list->uniques = NULL;
6359 }
6360 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006361
6362 /* *default-stmt */
6363 if (d_del->dflts) {
6364 switch (devs[u]->target->nodetype) {
6365 case LYS_LEAF:
6366 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6367 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6368 dflt, "deleting", "default", d_del->dflts[0]);
6369
Radek Krejcia1911222019-07-22 17:24:50 +02006370 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006371 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006372 if (strcmp(dflt, d_del->dflts[0])) {
6373 if (i) {
6374 free((char*)dflt);
6375 }
6376 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6377 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6378 d_del->dflts[0], dflt);
6379 goto cleanup;
6380 }
6381 if (i) {
6382 free((char*)dflt);
6383 }
6384 dflt = NULL;
6385
Radek Krejci474f9b82019-07-24 11:36:37 +02006386 /* update the list of incomplete default values if needed */
6387 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6388
Radek Krejcia1911222019-07-22 17:24:50 +02006389 /* remove the default specification */
6390 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6391 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6392 free(leaf->dflt);
6393 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006394 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006395 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006396 break;
6397 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006398 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6399 LY_ARRAY_FOR(d_del->dflts, x) {
6400 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006401 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 +02006402 if (!strcmp(dflt, d_del->dflts[x])) {
6403 if (i) {
6404 free((char*)dflt);
6405 }
6406 dflt = NULL;
6407 break;
6408 }
6409 if (i) {
6410 free((char*)dflt);
6411 }
6412 dflt = NULL;
6413 }
6414 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6415 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6416 "which does not match any of the target's property values.", d_del->dflts[x]);
6417 goto cleanup;
6418 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006419
6420 /* update the list of incomplete default values if needed */
6421 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6422
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006423 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006424 LY_ARRAY_DECREMENT(llist->dflts);
6425 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6426 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6427 free(llist->dflts[y]);
6428 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006429 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 +02006430 }
6431 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006432 LY_ARRAY_FREE(llist->dflts_mods);
6433 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006434 LY_ARRAY_FREE(llist->dflts);
6435 llist->dflts = NULL;
6436 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006437 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006438 break;
6439 case LYS_CHOICE:
6440 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6441 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6442 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006443 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006444 if (prefix) {
6445 /* use module prefixes from the deviation module to match the module of the default case */
6446 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6447 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006448 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6449 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006450 goto cleanup;
6451 }
6452 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6453 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006454 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6455 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006456 goto cleanup;
6457 }
6458 }
6459 /* else {
6460 * strictly, the default prefix would point to the deviation module, but the value should actually
6461 * match the default string in the original module (usually unprefixed), so in this case we do not check
6462 * the module of the default case, just matching its name */
6463 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6464 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006465 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6466 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006467 goto cleanup;
6468 }
6469 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6470 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6471 break;
6472 default:
6473 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006474 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006475 goto cleanup;
6476 }
6477 }
6478
6479 break;
6480 case LYS_DEV_REPLACE:
6481 d_rpl = (struct lysp_deviate_rpl*)d;
6482
6483 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006484 if (d_rpl->type) {
6485 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6486 /* type is mandatory, so checking for its presence is not necessary */
6487 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006488
6489 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6490 /* the target has default from the previous type - remove it */
6491 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006492 /* update the list of incomplete default values if needed */
6493 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6494
Radek Krejcia1911222019-07-22 17:24:50 +02006495 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6496 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6497 free(leaf->dflt);
6498 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006499 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006500 } else { /* LYS_LEAFLIST */
6501 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006502 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006503 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6504 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6505 free(llist->dflts[x]);
6506 }
6507 LY_ARRAY_FREE(llist->dflts);
6508 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006509 LY_ARRAY_FREE(llist->dflts_mods);
6510 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006511 }
6512 }
6513 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006514 /* there is no default value, do not set changed_type after type compilation
6515 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006516 changed_type = -1;
6517 }
Radek Krejciec4da802019-05-02 13:02:41 +02006518 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 +02006519 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006520 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006521
6522 /* [units-stmt] */
6523 if (d_rpl->units) {
6524 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6525 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6526 units, "replacing", "units", d_rpl->units);
6527
6528 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6529 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6530 }
6531
6532 /* [default-stmt] */
6533 if (d_rpl->dflt) {
6534 switch (devs[u]->target->nodetype) {
6535 case LYS_LEAF:
6536 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6537 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006538 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006539 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006540 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6541 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6542 dflt = d_rpl->dflt;
6543 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006544 break;
6545 case LYS_CHOICE:
6546 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006547 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 +01006548 goto cleanup;
6549 }
6550 break;
6551 default:
6552 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006553 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006554 goto cleanup;
6555 }
6556 }
6557
6558 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006559 if (d_rpl->flags & LYS_CONFIG_MASK) {
6560 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006561 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006562 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6563 goto cleanup;
6564 }
6565 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006567 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6568 goto cleanup;
6569 }
Radek Krejci327de162019-06-14 12:52:07 +02006570 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006571 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006572
6573 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006574 if (d_rpl->flags & LYS_MAND_MASK) {
6575 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006576 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006577 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6578 goto cleanup;
6579 }
Radek Krejci327de162019-06-14 12:52:07 +02006580 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006581 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006582
6583 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006584 if (d_rpl->flags & LYS_SET_MIN) {
6585 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6586 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6587 /* change value */
6588 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6589 } else if (devs[u]->target->nodetype == LYS_LIST) {
6590 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6591 /* change value */
6592 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6593 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006595 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6596 goto cleanup;
6597 }
6598 if (d_rpl->min) {
6599 devs[u]->target->flags |= LYS_MAND_TRUE;
6600 }
6601 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006602
6603 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006604 if (d_rpl->flags & LYS_SET_MAX) {
6605 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6606 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6607 /* change value */
6608 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6609 } else if (devs[u]->target->nodetype == LYS_LIST) {
6610 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6611 /* change value */
6612 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6613 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006614 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006615 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6616 goto cleanup;
6617 }
6618 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006619
6620 break;
6621 default:
6622 LOGINT(ctx->ctx);
6623 goto cleanup;
6624 }
6625 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006626
Radek Krejci33f72892019-02-21 10:36:58 +01006627 /* final check when all deviations of a single target node are applied */
6628
Radek Krejci551b12c2019-02-19 16:11:21 +01006629 /* check min-max compatibility */
6630 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6631 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6632 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6633 } else if (devs[u]->target->nodetype == LYS_LIST) {
6634 min = ((struct lysc_node_list*)devs[u]->target)->min;
6635 max = ((struct lysc_node_list*)devs[u]->target)->max;
6636 } else {
6637 min = max = 0;
6638 }
6639 if (min > max) {
6640 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 +02006641 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006642 goto cleanup;
6643 }
6644
Radek Krejcia1911222019-07-22 17:24:50 +02006645 if (dflt) {
6646 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006647 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006648 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006649 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6650 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006651 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006652 leaf->dflt->realtype->refcount++;
6653 if (err) {
6654 ly_err_print(err);
6655 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6656 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6657 ly_err_free(err);
6658 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006659 if (rc == LY_EINCOMPLETE) {
6660 /* postpone default compilation when the tree is complete */
6661 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6662
6663 /* but in general result is so far ok */
6664 rc = LY_SUCCESS;
6665 }
Radek Krejcia1911222019-07-22 17:24:50 +02006666 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006667 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006668 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6669 int dynamic;
6670 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006671 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006672
6673 /* update the list of incomplete default values if needed */
6674 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6675
6676 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006677 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6678 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6679 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006680 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6681 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006682 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006683 leaf->dflt->realtype->refcount++;
6684 if (err) {
6685 ly_err_print(err);
6686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6687 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6688 ly_err_free(err);
6689 }
6690 if (dynamic) {
6691 free((void*)dflt);
6692 }
Radek Krejcia1911222019-07-22 17:24:50 +02006693 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006694 if (rc == LY_EINCOMPLETE) {
6695 /* postpone default compilation when the tree is complete */
6696 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6697
6698 /* but in general result is so far ok */
6699 rc = LY_SUCCESS;
6700 }
6701 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006702 } else { /* LYS_LEAFLIST */
6703 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006704 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 +02006705 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6706 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6707 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006708 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6709 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006710 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6711 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006712 llist->dflts[x]->realtype->refcount++;
6713 if (err) {
6714 ly_err_print(err);
6715 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6716 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6717 dflt, err->msg);
6718 ly_err_free(err);
6719 }
6720 if (dynamic) {
6721 free((void*)dflt);
6722 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006723 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006724 if (rc == LY_EINCOMPLETE) {
6725 /* postpone default compilation when the tree is complete */
6726 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6727
6728 /* but in general result is so far ok */
6729 rc = LY_SUCCESS;
6730 }
Radek Krejcia1911222019-07-22 17:24:50 +02006731 LY_CHECK_GOTO(rc, cleanup);
6732 }
6733 }
6734 }
6735
Radek Krejci551b12c2019-02-19 16:11:21 +01006736 /* check mandatory - default compatibility */
6737 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6738 && (devs[u]->target->flags & LYS_SET_DFLT)
6739 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006741 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006742 goto cleanup;
6743 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6744 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6745 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006746 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 +01006747 goto cleanup;
6748 }
6749 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6750 /* mandatory node under a default case */
6751 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006752 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6753 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006754 goto cleanup;
6755 }
Radek Krejci33f72892019-02-21 10:36:58 +01006756
Radek Krejci327de162019-06-14 12:52:07 +02006757 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006758 }
6759
Radek Krejci327de162019-06-14 12:52:07 +02006760 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006761 ret = LY_SUCCESS;
6762
6763cleanup:
6764 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6765 LY_ARRAY_FREE(devs[u]->deviates);
6766 free(devs[u]);
6767 }
6768 free(devs);
6769 ly_set_erase(&targets, NULL);
6770 ly_set_erase(&devs_p, NULL);
6771
6772 return ret;
6773}
6774
Radek Krejcib56c7502019-02-13 14:19:54 +01006775/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006776 * @brief Compile the given YANG submodule into the main module.
6777 * @param[in] ctx Compile context
6778 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006779 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6780 */
6781LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006782lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006783{
6784 unsigned int u;
6785 LY_ERR ret = LY_SUCCESS;
6786 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006787 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006788 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006789 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006790
Radek Krejci0af46292019-01-11 16:02:31 +01006791 if (!mainmod->mod->off_features) {
6792 /* features are compiled directly into the compiled module structure,
6793 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6794 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006795 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6796 LY_CHECK_GOTO(ret, error);
6797 }
6798 if (!mainmod->mod->off_extensions) {
6799 /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */
6800 ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions);
Radek Krejci0af46292019-01-11 16:02:31 +01006801 LY_CHECK_GOTO(ret, error);
6802 }
6803
Radek Krejci327de162019-06-14 12:52:07 +02006804 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006805 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006806 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006807
Radek Krejci474f9b82019-07-24 11:36:37 +02006808 /* data nodes */
6809 LY_LIST_FOR(submod->data, node_p) {
6810 ret = lys_compile_node(ctx, node_p, NULL, 0);
6811 LY_CHECK_GOTO(ret, error);
6812 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006813
Radek Krejciec4da802019-05-02 13:02:41 +02006814 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6815 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006816
Radek Krejcid05cbd92018-12-05 14:26:40 +01006817error:
6818 return ret;
6819}
6820
Radek Krejci335332a2019-09-05 13:03:35 +02006821static void *
6822lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6823{
6824 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6825 if (substmts[u].stmt == stmt) {
6826 return substmts[u].storage;
6827 }
6828 }
6829 return NULL;
6830}
6831
6832LY_ERR
6833lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6834{
6835 LY_ERR ret = LY_EVALID, r;
6836 unsigned int u;
6837 struct lysp_stmt *stmt;
6838 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006839
6840 /* check for invalid substatements */
6841 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006842 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6843 continue;
6844 }
Radek Krejci335332a2019-09-05 13:03:35 +02006845 for (u = 0; substmts[u].stmt; ++u) {
6846 if (substmts[u].stmt == stmt->kw) {
6847 break;
6848 }
6849 }
6850 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006851 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6852 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006853 goto cleanup;
6854 }
Radek Krejci335332a2019-09-05 13:03:35 +02006855 }
6856
Radek Krejciad5963b2019-09-06 16:03:05 +02006857 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6858
Radek Krejci335332a2019-09-05 13:03:35 +02006859 /* keep order of the processing the same as the order in the defined substmts,
6860 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6861 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006862 int stmt_present = 0;
6863
Radek Krejci335332a2019-09-05 13:03:35 +02006864 for (stmt = ext->child; stmt; stmt = stmt->next) {
6865 if (substmts[u].stmt != stmt->kw) {
6866 continue;
6867 }
6868
Radek Krejciad5963b2019-09-06 16:03:05 +02006869 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006870 if (substmts[u].storage) {
6871 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006872 case LY_STMT_STATUS:
6873 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6874 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6875 break;
6876 case LY_STMT_UNITS: {
6877 const char **units;
6878
6879 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6880 /* single item */
6881 if (*((const char **)substmts[u].storage)) {
6882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6883 goto cleanup;
6884 }
6885 units = (const char **)substmts[u].storage;
6886 } else {
6887 /* sized array */
6888 const char ***units_array = (const char ***)substmts[u].storage;
6889 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6890 }
6891 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6892 break;
6893 }
Radek Krejci335332a2019-09-05 13:03:35 +02006894 case LY_STMT_TYPE: {
6895 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6896 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6897
6898 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6899 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006900 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006901 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6902 goto cleanup;
6903 }
6904 compiled = substmts[u].storage;
6905 } else {
6906 /* sized array */
6907 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6908 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6909 compiled = (void*)type;
6910 }
6911
Radek Krejciad5963b2019-09-06 16:03:05 +02006912 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006913 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6914 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006915 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6916 lysp_type_free(ctx->ctx, parsed);
6917 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006918 break;
6919 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006920 case LY_STMT_IF_FEATURE: {
6921 struct lysc_iffeature *iff = NULL;
6922
6923 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6924 /* single item */
6925 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6926 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6927 goto cleanup;
6928 }
6929 iff = (struct lysc_iffeature*)substmts[u].storage;
6930 } else {
6931 /* sized array */
6932 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6933 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6934 }
6935 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6936 break;
6937 }
6938 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6939 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006940 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Statement \"%s\" is not supported as an extension (found in \"%s%s%s\") substatement.",
6942 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006943 goto cleanup;
6944 }
6945 }
Radek Krejci335332a2019-09-05 13:03:35 +02006946 }
Radek Krejci335332a2019-09-05 13:03:35 +02006947
Radek Krejciad5963b2019-09-06 16:03:05 +02006948 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6949 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6950 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6951 goto cleanup;
6952 }
Radek Krejci335332a2019-09-05 13:03:35 +02006953 }
6954
6955 ret = LY_SUCCESS;
6956
6957cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006958 return ret;
6959}
6960
Michal Vasko175012e2019-11-06 15:49:14 +01006961/**
Michal Vaskoecd62de2019-11-13 12:35:11 +01006962 * @brief Check when for cyclic dependencies.
6963 * @param[in] set Set with all the referenced nodes.
6964 * @param[in] node Node whose "when" referenced nodes are in @p set.
6965 * @return LY_ERR value
6966 */
6967static LY_ERR
Michal Vasko5c4e5892019-11-14 12:31:38 +01006968lys_compile_check_when_cyclic(struct lyxp_set *set, const struct lysc_node *node)
Michal Vaskoecd62de2019-11-13 12:35:11 +01006969{
6970 struct lyxp_set tmp_set;
6971 struct lyxp_set_scnode *xp_scnode;
Michal Vasko251f56e2019-11-14 16:06:47 +01006972 uint32_t i, j, k;
Michal Vaskoecd62de2019-11-13 12:35:11 +01006973 int idx;
6974 struct lysc_when *when;
6975 LY_ERR ret = LY_SUCCESS;
6976
6977 memset(&tmp_set, 0, sizeof tmp_set);
6978
6979 /* prepare in_ctx of the set */
6980 for (i = 0; i < set->used; ++i) {
6981 xp_scnode = &set->val.scnodes[i];
6982
Michal Vasko5c4e5892019-11-14 12:31:38 +01006983 if (xp_scnode->in_ctx != -1) {
6984 /* check node when, skip the context node (it was just checked) */
Michal Vaskoecd62de2019-11-13 12:35:11 +01006985 xp_scnode->in_ctx = 1;
6986 }
6987 }
6988
6989 for (i = 0; i < set->used; ++i) {
6990 xp_scnode = &set->val.scnodes[i];
6991 if (xp_scnode->in_ctx != 1) {
6992 /* already checked */
6993 continue;
6994 }
6995
Michal Vasko2ff7efe2019-12-10 14:50:59 +01006996 if ((xp_scnode->type != LYXP_NODE_ELEM) || (xp_scnode->scnode->nodetype & (LYS_ACTION | LYS_NOTIF))
6997 || !xp_scnode->scnode->when) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01006998 /* no when to check */
6999 xp_scnode->in_ctx = 0;
7000 continue;
7001 }
7002
7003 node = xp_scnode->scnode;
7004 do {
Michal Vasko251f56e2019-11-14 16:06:47 +01007005 LY_ARRAY_FOR(node->when, j) {
7006 when = node->when[j];
Michal Vaskoecd62de2019-11-13 12:35:11 +01007007 ret = lyxp_atomize(when->cond, LYD_UNKNOWN, when->module, when->context,
7008 when->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, LYXP_SCNODE_SCHEMA);
7009 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007010 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when->cond->expr);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007011 goto cleanup;
7012 }
7013
Michal Vasko251f56e2019-11-14 16:06:47 +01007014 for (k = 0; k < tmp_set.used; ++k) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007015 /* skip roots'n'stuff */
Michal Vasko251f56e2019-11-14 16:06:47 +01007016 if (tmp_set.val.scnodes[k].type == LYXP_NODE_ELEM) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007017 /* try to find this node in our set */
Michal Vasko251f56e2019-11-14 16:06:47 +01007018 idx = lyxp_set_scnode_dup_node_check(set, tmp_set.val.scnodes[k].scnode, LYXP_NODE_ELEM, -1);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007019 if ((idx > -1) && (set->val.scnodes[idx].in_ctx == -1)) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007020 LOGVAL(set->ctx, LY_VLOG_LYSC, node, LY_VCODE_CIRC_WHEN, node->name, set->val.scnodes[idx].scnode->name);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007021 ret = LY_EVALID;
7022 goto cleanup;
7023 }
7024
7025 /* needs to be checked, if in both sets, will be ignored */
Michal Vasko251f56e2019-11-14 16:06:47 +01007026 tmp_set.val.scnodes[k].in_ctx = 1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007027 } else {
7028 /* no when, nothing to check */
Michal Vasko251f56e2019-11-14 16:06:47 +01007029 tmp_set.val.scnodes[k].in_ctx = 0;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007030 }
7031 }
7032
7033 /* merge this set into the global when set */
7034 lyxp_set_scnode_merge(set, &tmp_set);
7035 }
7036
7037 /* check when of non-data parents as well */
7038 node = node->parent;
7039 } while (node && (node->nodetype & (LYS_CASE | LYS_CHOICE)));
7040
Michal Vasko251f56e2019-11-14 16:06:47 +01007041 /* this node when was checked (xp_scnode could have been reallocd) */
7042 set->val.scnodes[i].in_ctx = -1;
Michal Vaskoecd62de2019-11-13 12:35:11 +01007043 }
7044
7045cleanup:
7046 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7047 return ret;
7048}
7049
7050/**
Michal Vasko175012e2019-11-06 15:49:14 +01007051 * @brief Check when/must expressions of a node on a compiled schema tree.
7052 * @param[in] ctx Compile context.
7053 * @param[in] node Node to check.
7054 * @return LY_ERR value
7055 */
7056static LY_ERR
7057lys_compile_check_xpath(struct lysc_ctx *ctx, const struct lysc_node *node)
7058{
Michal Vasko175012e2019-11-06 15:49:14 +01007059 struct lyxp_set tmp_set;
7060 uint32_t i, j;
Michal Vasko5d8756a2019-11-07 15:21:00 +01007061 int opts, input_done = 0;
Michal Vasko175012e2019-11-06 15:49:14 +01007062 struct lysc_when **when = NULL;
7063 struct lysc_must *musts = NULL;
7064 LY_ERR ret = LY_SUCCESS;
7065
7066 memset(&tmp_set, 0, sizeof tmp_set);
Michal Vasko5d8756a2019-11-07 15:21:00 +01007067 opts = LYXP_SCNODE_SCHEMA;
Michal Vasko175012e2019-11-06 15:49:14 +01007068
7069 switch (node->nodetype) {
7070 case LYS_CONTAINER:
7071 when = ((struct lysc_node_container *)node)->when;
7072 musts = ((struct lysc_node_container *)node)->musts;
7073 break;
7074 case LYS_CHOICE:
7075 when = ((struct lysc_node_choice *)node)->when;
7076 break;
7077 case LYS_LEAF:
7078 when = ((struct lysc_node_leaf *)node)->when;
7079 musts = ((struct lysc_node_leaf *)node)->musts;
7080 break;
7081 case LYS_LEAFLIST:
7082 when = ((struct lysc_node_leaflist *)node)->when;
7083 musts = ((struct lysc_node_leaflist *)node)->musts;
7084 break;
7085 case LYS_LIST:
7086 when = ((struct lysc_node_list *)node)->when;
7087 musts = ((struct lysc_node_list *)node)->musts;
7088 break;
7089 case LYS_ANYXML:
7090 case LYS_ANYDATA:
7091 when = ((struct lysc_node_anydata *)node)->when;
7092 musts = ((struct lysc_node_anydata *)node)->musts;
7093 break;
7094 case LYS_CASE:
7095 when = ((struct lysc_node_case *)node)->when;
7096 break;
7097 case LYS_NOTIF:
7098 musts = ((struct lysc_notif *)node)->musts;
7099 break;
Michal Vasko5d8756a2019-11-07 15:21:00 +01007100 case LYS_ACTION:
7101 /* first process input musts */
7102 musts = ((struct lysc_action *)node)->input.musts;
7103 break;
Michal Vasko175012e2019-11-06 15:49:14 +01007104 default:
7105 /* nothing to check */
7106 break;
7107 }
7108
Michal Vasko175012e2019-11-06 15:49:14 +01007109 /* check "when" */
7110 LY_ARRAY_FOR(when, i) {
Michal Vasko175012e2019-11-06 15:49:14 +01007111 ret = lyxp_atomize(when[i]->cond, LYD_UNKNOWN, when[i]->module, when[i]->context,
7112 when[i]->context ? LYXP_NODE_ELEM : LYXP_NODE_ROOT_CONFIG, &tmp_set, opts);
7113 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007114 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid when condition \"%s\".", when[i]->cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01007115 goto cleanup;
7116 }
7117
Michal Vaskodc052f32019-11-07 11:11:38 +01007118 ctx->path[0] = '\0';
7119 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko175012e2019-11-06 15:49:14 +01007120 for (j = 0; j < tmp_set.used; ++j) {
Michal Vasko5c4e5892019-11-14 12:31:38 +01007121 /* skip roots'n'stuff */
7122 if ((tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) && (tmp_set.val.scnodes[j].in_ctx != -1)) {
Michal Vaskoecd62de2019-11-13 12:35:11 +01007123 struct lysc_node *schema = tmp_set.val.scnodes[j].scnode;
Michal Vasko175012e2019-11-06 15:49:14 +01007124
Michal Vaskoecd62de2019-11-13 12:35:11 +01007125 /* XPath expression cannot reference "lower" status than the node that has the definition */
7126 ret = lysc_check_status(ctx, when[i]->flags, when[i]->module, node->name, schema->flags, schema->module,
7127 schema->name);
7128 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007129
7130 /* check dummy node accessing */
7131 if (schema == node) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007132 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LY_VCODE_DUMMY_WHEN, node->name);
Michal Vasko5c4e5892019-11-14 12:31:38 +01007133 ret = LY_EVALID;
7134 goto cleanup;
7135 }
Michal Vasko175012e2019-11-06 15:49:14 +01007136 }
7137 }
7138
Michal Vaskoecd62de2019-11-13 12:35:11 +01007139 /* check cyclic dependencies */
Michal Vasko5c4e5892019-11-14 12:31:38 +01007140 ret = lys_compile_check_when_cyclic(&tmp_set, node);
Michal Vaskoecd62de2019-11-13 12:35:11 +01007141 LY_CHECK_GOTO(ret, cleanup);
7142
Michal Vasko175012e2019-11-06 15:49:14 +01007143 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7144 }
7145
Michal Vasko5d8756a2019-11-07 15:21:00 +01007146check_musts:
Michal Vasko175012e2019-11-06 15:49:14 +01007147 /* check "must" */
7148 LY_ARRAY_FOR(musts, i) {
Michal Vasko175012e2019-11-06 15:49:14 +01007149 ret = lyxp_atomize(musts[i].cond, LYD_UNKNOWN, musts[i].module, node, LYXP_NODE_ELEM, &tmp_set, opts);
7150 if (ret != LY_SUCCESS) {
Michal Vaskof6e51882019-12-16 09:59:45 +01007151 LOGVAL(ctx->ctx, LY_VLOG_LYSC, node, LYVE_SEMANTICS, "Invalid must restriction \"%s\".", musts[i].cond->expr);
Michal Vasko175012e2019-11-06 15:49:14 +01007152 goto cleanup;
7153 }
7154
Michal Vaskodc052f32019-11-07 11:11:38 +01007155 ctx->path[0] = '\0';
7156 lysc_path((struct lysc_node *)node, LYSC_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
Michal Vasko175012e2019-11-06 15:49:14 +01007157 for (j = 0; j < tmp_set.used; ++j) {
7158 /* skip roots'n'stuff */
7159 if (tmp_set.val.scnodes[j].type == LYXP_NODE_ELEM) {
7160 /* XPath expression cannot reference "lower" status than the node that has the definition */
Michal Vasko5d8756a2019-11-07 15:21:00 +01007161 ret = lysc_check_status(ctx, node->flags, musts[i].module, node->name, tmp_set.val.scnodes[j].scnode->flags,
Michal Vasko175012e2019-11-06 15:49:14 +01007162 tmp_set.val.scnodes[j].scnode->module, tmp_set.val.scnodes[j].scnode->name);
7163 LY_CHECK_GOTO(ret, cleanup);
Michal Vasko175012e2019-11-06 15:49:14 +01007164 }
7165 }
7166
7167 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7168 }
7169
Michal Vasko5d8756a2019-11-07 15:21:00 +01007170 if ((node->nodetype == LYS_ACTION) && !input_done) {
7171 /* now check output musts */
7172 input_done = 1;
7173 musts = ((struct lysc_action *)node)->output.musts;
7174 opts = LYXP_SCNODE_OUTPUT;
7175 goto check_musts;
7176 }
7177
Michal Vasko175012e2019-11-06 15:49:14 +01007178cleanup:
7179 lyxp_set_cast(&tmp_set, LYXP_SET_EMPTY);
7180 return ret;
7181}
7182
Radek Krejci19a96102018-11-15 13:38:09 +01007183LY_ERR
7184lys_compile(struct lys_module *mod, int options)
7185{
7186 struct lysc_ctx ctx = {0};
7187 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01007188 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01007189 struct lysp_module *sp;
7190 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01007191 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007192 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01007193 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01007194 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01007195 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01007196
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007197 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01007198
7199 if (!mod->implemented) {
7200 /* just imported modules are not compiled */
7201 return LY_SUCCESS;
7202 }
7203
Radek Krejci19a96102018-11-15 13:38:09 +01007204 sp = mod->parsed;
7205
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007206 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01007207 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01007208 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02007209 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02007210 ctx.path_len = 1;
7211 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01007212
7213 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01007214 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
7215 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01007216
Radek Krejciec4da802019-05-02 13:02:41 +02007217 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007218 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007219 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01007220 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7221 }
Radek Krejci0935f412019-08-20 16:15:18 +02007222
7223 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01007224 if (mod->off_features) {
7225 /* there is already precompiled array of features */
7226 mod_c->features = mod->off_features;
7227 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01007228 } else {
7229 /* features are compiled directly into the compiled module structure,
7230 * 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 +02007231 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007232 LY_CHECK_GOTO(ret, error);
7233 }
7234 /* finish feature compilation, not only for the main module, but also for the submodules.
7235 * Due to possible forward references, it must be done when all the features (including submodules)
7236 * are present. */
7237 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007238 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007239 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7240 }
Radek Krejci327de162019-06-14 12:52:07 +02007241 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01007242 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02007243 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01007244 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02007245 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01007246 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
7247 }
Radek Krejci327de162019-06-14 12:52:07 +02007248 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007249 }
Radek Krejci327de162019-06-14 12:52:07 +02007250 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01007251
Radek Krejci0935f412019-08-20 16:15:18 +02007252 /* extensions */
7253 /* 2-steps: a) prepare compiled structures and ... */
7254 if (mod->off_extensions) {
7255 /* there is already precompiled array of extension definitions */
7256 mod_c->extensions = mod->off_extensions;
7257 mod->off_extensions = NULL;
7258 } else {
7259 /* extension definitions are compiled directly into the compiled module structure */
7260 ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions);
Radek Krejcibfb2e142019-09-09 14:47:44 +02007261 LY_CHECK_GOTO(ret, error);
Radek Krejci0935f412019-08-20 16:15:18 +02007262 }
7263 /* ... b) connect the extension definitions with the appropriate extension plugins */
7264 lys_compile_extension_plugins(mod_c->extensions);
7265
7266 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02007267 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02007268 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007269 if (sp->identities) {
7270 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
7271 }
Radek Krejci327de162019-06-14 12:52:07 +02007272 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01007273
Radek Krejci95710c92019-02-11 15:49:55 +01007274 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01007275 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007276 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007277 }
Radek Krejci95710c92019-02-11 15:49:55 +01007278
Radek Krejciec4da802019-05-02 13:02:41 +02007279 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
7280 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01007281
Radek Krejci95710c92019-02-11 15:49:55 +01007282 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007283 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007284 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007285 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01007286 }
Radek Krejciccd20f12019-02-15 14:12:27 +01007287
Radek Krejci474f9b82019-07-24 11:36:37 +02007288 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02007289 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01007290
Radek Krejci0935f412019-08-20 16:15:18 +02007291 /* extension instances TODO cover extension instances from submodules */
7292 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01007293
Radek Krejcia3045382018-11-22 14:30:31 +01007294 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01007295 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
7296 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
7297 * 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 +01007298 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01007299 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01007300 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
7301 if (type->basetype == LY_TYPE_LEAFREF) {
7302 /* validate the path */
Michal Vaskoae9e4cb2019-09-25 08:43:05 +02007303 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type, NULL), error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007304 } else if (type->basetype == LY_TYPE_UNION) {
7305 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7306 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7307 /* validate the path */
Michal Vasko175012e2019-11-06 15:49:14 +01007308 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
7309 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v], NULL);
7310 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01007311 }
7312 }
Radek Krejcia3045382018-11-22 14:30:31 +01007313 }
7314 }
Michal Vasko175012e2019-11-06 15:49:14 +01007315
7316 /* check xpath */
7317 LY_CHECK_GOTO(ret = lys_compile_check_xpath(&ctx, ctx.unres.objs[u]), error);
Radek Krejcia3045382018-11-22 14:30:31 +01007318 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01007319 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01007320 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01007321 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
7322 if (type->basetype == LY_TYPE_LEAFREF) {
7323 /* store pointer to the real type */
7324 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
7325 typeiter->basetype == LY_TYPE_LEAFREF;
7326 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7327 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01007328 } else if (type->basetype == LY_TYPE_UNION) {
7329 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
7330 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
7331 /* store pointer to the real type */
7332 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
7333 typeiter->basetype == LY_TYPE_LEAFREF;
7334 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
7335 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
7336 }
7337 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01007338 }
7339 }
7340 }
Radek Krejciec4da802019-05-02 13:02:41 +02007341
Radek Krejci474f9b82019-07-24 11:36:37 +02007342 /* finish incomplete default values compilation */
7343 for (u = 0; u < ctx.dflts.count; ++u) {
7344 struct ly_err_item *err = NULL;
7345 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
Radek Krejci950f6a52019-09-12 17:15:32 +02007346 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->original, strlen(r->dflt->original),
Radek Krejci474f9b82019-07-24 11:36:37 +02007347 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
7348 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
7349 if (err) {
7350 ly_err_print(err);
7351 ctx.path[0] = '\0';
Michal Vasko03ff5a72019-09-11 13:49:33 +02007352 lysc_path(r->context_node, LYSC_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
Radek Krejci474f9b82019-07-24 11:36:37 +02007353 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
7354 "Invalid default - value does not fit the type (%s).", err->msg);
7355 ly_err_free(err);
7356 }
7357 LY_CHECK_GOTO(ret, error);
7358 }
7359
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007360 /* validate non-instantiated groupings from the parsed schema,
7361 * without it we would accept even the schemas with invalid grouping specification */
7362 ctx.options |= LYSC_OPT_GROUPING;
7363 LY_ARRAY_FOR(sp->groupings, u) {
7364 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007365 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007366 }
7367 }
7368 LY_LIST_FOR(sp->data, node_p) {
7369 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7370 LY_ARRAY_FOR(grps, u) {
7371 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007372 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007373 }
7374 }
7375 }
7376
Radek Krejci474f9b82019-07-24 11:36:37 +02007377 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7378 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7379 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7380 }
7381
Radek Krejci1c0c3442019-07-23 16:08:47 +02007382 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007383 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007384 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007385 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007386 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007387
Radek Krejciec4da802019-05-02 13:02:41 +02007388 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01007389 lysp_module_free(mod->parsed);
7390 ((struct lys_module*)mod)->parsed = NULL;
7391 }
7392
Radek Krejciec4da802019-05-02 13:02:41 +02007393 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007394 /* remove flag of the modules implemented by dependency */
7395 for (u = 0; u < ctx.ctx->list.count; ++u) {
7396 m = ctx.ctx->list.objs[u];
7397 if (m->implemented == 2) {
7398 m->implemented = 1;
7399 }
7400 }
7401 }
7402
Radek Krejci19a96102018-11-15 13:38:09 +01007403 ((struct lys_module*)mod)->compiled = mod_c;
7404 return LY_SUCCESS;
7405
7406error:
Radek Krejci95710c92019-02-11 15:49:55 +01007407 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007408 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007409 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007410 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007411 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007412 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007413 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007414 mod->compiled = NULL;
7415
7416 /* revert compilation of modules implemented by dependency */
7417 for (u = 0; u < ctx.ctx->list.count; ++u) {
7418 m = ctx.ctx->list.objs[u];
Michal Vasko2947ce12019-12-16 10:37:19 +01007419 if ((m->implemented == 2) && m->compiled) {
Radek Krejci95710c92019-02-11 15:49:55 +01007420 /* revert features list to the precompiled state */
7421 lys_feature_precompile_revert(&ctx, m);
7422 /* mark module as imported-only / not-implemented */
7423 m->implemented = 0;
7424 /* free the compiled version of the module */
7425 lysc_module_free(m->compiled, NULL);
7426 m->compiled = NULL;
7427 }
7428 }
7429
Radek Krejci19a96102018-11-15 13:38:09 +01007430 return ret;
7431}