blob: f3fba161de5109a3d95e39a4c839b7e194080867 [file] [log] [blame]
Radek Krejci19a96102018-11-15 13:38:09 +01001/**
2 * @file tree_schema.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @brief Schema tree implementation
5 *
6 * Copyright (c) 2015 - 2018 CESNET, z.s.p.o.
7 *
8 * This source code is licensed under BSD 3-Clause License (the "License").
9 * You may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * https://opensource.org/licenses/BSD-3-Clause
13 */
14
15#include "common.h"
16
Radek Krejcie7b95092019-05-15 11:03:07 +020017#include <assert.h>
Radek Krejci19a96102018-11-15 13:38:09 +010018#include <ctype.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020019#include <stddef.h>
20#include <stdint.h>
Radek Krejci19a96102018-11-15 13:38:09 +010021#include <stdio.h>
Radek Krejcie7b95092019-05-15 11:03:07 +020022#include <stdlib.h>
23#include <string.h>
Radek Krejci19a96102018-11-15 13:38:09 +010024
Radek Krejcie7b95092019-05-15 11:03:07 +020025#include "dict.h"
26#include "log.h"
Radek Krejci0935f412019-08-20 16:15:18 +020027#include "plugins_exts.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020028#include "set.h"
29#include "plugins_types.h"
Radek Krejci0935f412019-08-20 16:15:18 +020030#include "plugins_exts_internal.h"
Radek Krejcie7b95092019-05-15 11:03:07 +020031#include "tree.h"
32#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010033#include "tree_schema_internal.h"
34#include "xpath.h"
35
36/**
37 * @brief Duplicate string into dictionary
38 * @param[in] CTX libyang context of the dictionary.
39 * @param[in] ORIG String to duplicate.
40 * @param[out] DUP Where to store the result.
41 */
42#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
43
Radek Krejciec4da802019-05-02 13:02:41 +020044#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010045 if (ARRAY_P) { \
46 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010047 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010048 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
49 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020050 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010051 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
52 } \
53 }
54
Radek Krejciec4da802019-05-02 13:02:41 +020055#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010056 if (ARRAY_P) { \
57 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
58 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
59 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
60 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020061 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010062 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
63 } \
64 }
65
Radek Krejci0935f412019-08-20 16:15:18 +020066#define COMPILE_EXTS_GOTO(CTX, EXTS_P, EXT_C, PARENT, PARENT_TYPE, RET, GOTO) \
67 if (EXTS_P) { \
68 LY_ARRAY_CREATE_GOTO((CTX)->ctx, EXT_C, LY_ARRAY_SIZE(EXTS_P), RET, GOTO); \
69 for (uint32_t __exts_iter = 0, __array_offset = LY_ARRAY_SIZE(EXT_C); __exts_iter < LY_ARRAY_SIZE(EXTS_P); ++__exts_iter) { \
70 LY_ARRAY_INCREMENT(EXT_C); \
71 RET = lys_compile_ext(CTX, &(EXTS_P)[__exts_iter], &(EXT_C)[__exts_iter + __array_offset], PARENT, PARENT_TYPE); \
72 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
73 } \
74 }
75
Radek Krejciec4da802019-05-02 13:02:41 +020076#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010077 if (ARRAY_P) { \
78 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
79 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
80 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
81 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020082 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010083 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
84 } \
85 }
86
Radek Krejciec4da802019-05-02 13:02:41 +020087#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010088 if (MEMBER_P) { \
89 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
90 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020091 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010092 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
93 }
94
Radek Krejciec4da802019-05-02 13:02:41 +020095#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +010096 if (MEMBER_P) { \
97 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
98 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
99 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +0200100 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +0100101 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
102 }
103
Radek Krejcid05cbd92018-12-05 14:26:40 +0100104#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
105 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +0100106 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
107 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +0100108 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
109 return LY_EVALID; \
110 } \
111 } \
112 }
113
Radek Krejci19a96102018-11-15 13:38:09 +0100114static struct lysc_ext_instance *
115lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
116{
Radek Krejcie7b95092019-05-15 11:03:07 +0200117 /* TODO - extensions */
Radek Krejci19a96102018-11-15 13:38:09 +0100118 (void) ctx;
119 (void) orig;
120 return NULL;
121}
122
Radek Krejcib56c7502019-02-13 14:19:54 +0100123/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200124 * @brief Add record into the compile context's list of incomplete default values.
125 * @param[in] ctx Compile context with the incomplete default values list.
126 * @param[in] context_node Context schema node to store in the record.
127 * @param[in] dflt Incomplete default value to store in the record.
128 * @param[in] dflt_mod Module of the default value definition to store in the record.
129 * @return LY_EMEM in case of memory allocation failure.
130 * @return LY_SUCCESS
131 */
132static LY_ERR
133lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
134{
135 struct lysc_incomplete_dflt *r;
136 r = malloc(sizeof *r);
137 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
138 r->context_node = context_node;
139 r->dflt = dflt;
140 r->dflt_mod = dflt_mod;
141 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
142
143 return LY_SUCCESS;
144}
145
146/**
147 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
148 * @param[in] ctx Compile context with the incomplete default values list.
149 * @param[in] dflt Incomplete default values identifying the record to remove.
150 */
151static void
152lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
153{
154 unsigned int u;
155 struct lysc_incomplete_dflt *r;
156
157 for (u = 0; u < ctx->dflts.count; ++u) {
158 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
159 if (r->dflt == dflt) {
160 free(ctx->dflts.objs[u]);
161 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
162 --ctx->dflts.count;
163 return;
164 }
165 }
166}
167
Radek Krejci0e59c312019-08-15 15:34:15 +0200168void
Radek Krejci327de162019-06-14 12:52:07 +0200169lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
170{
171 int len;
172 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
173
174 if (!name) {
175 /* removing last path segment */
176 if (ctx->path[ctx->path_len - 1] == '}') {
177 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
178 if (ctx->path[ctx->path_len] == '=') {
179 ctx->path[ctx->path_len++] = '}';
180 } else {
181 /* not a top-level special tag, remove also preceiding '/' */
182 goto remove_nodelevel;
183 }
184 } else {
185remove_nodelevel:
186 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
187 if (ctx->path_len == 0) {
188 /* top-level (last segment) */
Radek Krejciacc79042019-07-25 14:14:57 +0200189 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200190 }
191 }
192 /* set new terminating NULL-byte */
193 ctx->path[ctx->path_len] = '\0';
194 } else {
195 if (ctx->path_len > 1) {
196 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
197 /* extension of the special tag */
198 nextlevel = 2;
199 --ctx->path_len;
200 } else {
201 /* there is already some path, so add next level */
202 nextlevel = 1;
203 }
204 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
205
206 if (nextlevel != 2) {
207 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
208 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200209 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200210 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200211 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s:%s", nextlevel ? "/" : "", ctx->mod->name, name);
Radek Krejci327de162019-06-14 12:52:07 +0200212 }
213 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200214 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200215 }
Radek Krejciacc79042019-07-25 14:14:57 +0200216 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
217 /* output truncated */
218 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
219 } else {
220 ctx->path_len += len;
221 }
Radek Krejci327de162019-06-14 12:52:07 +0200222 }
223}
224
225/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100226 * @brief Duplicate the compiled pattern structure.
227 *
228 * Instead of duplicating memory, the reference counter in the @p orig is increased.
229 *
230 * @param[in] orig The pattern structure to duplicate.
231 * @return The duplicated structure to use.
232 */
Radek Krejci19a96102018-11-15 13:38:09 +0100233static struct lysc_pattern*
234lysc_pattern_dup(struct lysc_pattern *orig)
235{
236 ++orig->refcount;
237 return orig;
238}
239
Radek Krejcib56c7502019-02-13 14:19:54 +0100240/**
241 * @brief Duplicate the array of compiled patterns.
242 *
243 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
244 *
245 * @param[in] ctx Libyang context for logging.
246 * @param[in] orig The patterns sized array to duplicate.
247 * @return New sized array as a copy of @p orig.
248 * @return NULL in case of memory allocation error.
249 */
Radek Krejci19a96102018-11-15 13:38:09 +0100250static struct lysc_pattern**
251lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
252{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100253 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100254 unsigned int u;
255
Radek Krejcib56c7502019-02-13 14:19:54 +0100256 assert(orig);
257
Radek Krejci19a96102018-11-15 13:38:09 +0100258 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
259 LY_ARRAY_FOR(orig, u) {
260 dup[u] = lysc_pattern_dup(orig[u]);
261 LY_ARRAY_INCREMENT(dup);
262 }
263 return dup;
264}
265
Radek Krejcib56c7502019-02-13 14:19:54 +0100266/**
267 * @brief Duplicate compiled range structure.
268 *
269 * @param[in] ctx Libyang context for logging.
270 * @param[in] orig The range structure to be duplicated.
271 * @return New compiled range structure as a copy of @p orig.
272 * @return NULL in case of memory allocation error.
273 */
Radek Krejci19a96102018-11-15 13:38:09 +0100274struct lysc_range*
275lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
276{
277 struct lysc_range *dup;
278 LY_ERR ret;
279
Radek Krejcib56c7502019-02-13 14:19:54 +0100280 assert(orig);
281
Radek Krejci19a96102018-11-15 13:38:09 +0100282 dup = calloc(1, sizeof *dup);
283 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
284 if (orig->parts) {
285 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
286 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
287 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
288 }
289 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
290 DUP_STRING(ctx, orig->emsg, dup->emsg);
291 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
292
293 return dup;
294cleanup:
295 free(dup);
296 (void) ret; /* set but not used due to the return type */
297 return NULL;
298}
299
Radek Krejcib56c7502019-02-13 14:19:54 +0100300/**
301 * @brief Stack for processing if-feature expressions.
302 */
Radek Krejci19a96102018-11-15 13:38:09 +0100303struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100304 int size; /**< number of items in the stack */
305 int index; /**< first empty item */
306 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100307};
308
Radek Krejcib56c7502019-02-13 14:19:54 +0100309/**
310 * @brief Add @ref ifftokens into the stack.
311 * @param[in] stack The if-feature stack to use.
312 * @param[in] value One of the @ref ifftokens to store in the stack.
313 * @return LY_EMEM in case of memory allocation error
314 * @return LY_ESUCCESS if the value successfully stored.
315 */
Radek Krejci19a96102018-11-15 13:38:09 +0100316static LY_ERR
317iff_stack_push(struct iff_stack *stack, uint8_t value)
318{
319 if (stack->index == stack->size) {
320 stack->size += 4;
321 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
322 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
323 }
324 stack->stack[stack->index++] = value;
325 return LY_SUCCESS;
326}
327
Radek Krejcib56c7502019-02-13 14:19:54 +0100328/**
329 * @brief Get (and remove) the last item form the stack.
330 * @param[in] stack The if-feature stack to use.
331 * @return The value from the top of the stack.
332 */
Radek Krejci19a96102018-11-15 13:38:09 +0100333static uint8_t
334iff_stack_pop(struct iff_stack *stack)
335{
Radek Krejcib56c7502019-02-13 14:19:54 +0100336 assert(stack && stack->index);
337
Radek Krejci19a96102018-11-15 13:38:09 +0100338 stack->index--;
339 return stack->stack[stack->index];
340}
341
Radek Krejcib56c7502019-02-13 14:19:54 +0100342/**
343 * @brief Clean up the stack.
344 * @param[in] stack The if-feature stack to use.
345 */
Radek Krejci19a96102018-11-15 13:38:09 +0100346static void
347iff_stack_clean(struct iff_stack *stack)
348{
349 stack->size = 0;
350 free(stack->stack);
351}
352
Radek Krejcib56c7502019-02-13 14:19:54 +0100353/**
354 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
355 * (libyang format of the if-feature expression).
356 * @param[in,out] list The 2bits array to modify.
357 * @param[in] op The operand (@ref ifftokens) to store.
358 * @param[in] pos Position (0-based) where to store the given @p op.
359 */
Radek Krejci19a96102018-11-15 13:38:09 +0100360static void
361iff_setop(uint8_t *list, uint8_t op, int pos)
362{
363 uint8_t *item;
364 uint8_t mask = 3;
365
366 assert(pos >= 0);
367 assert(op <= 3); /* max 2 bits */
368
369 item = &list[pos / 4];
370 mask = mask << 2 * (pos % 4);
371 *item = (*item) & ~mask;
372 *item = (*item) | (op << 2 * (pos % 4));
373}
374
Radek Krejcib56c7502019-02-13 14:19:54 +0100375#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
376#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100377
Radek Krejci0af46292019-01-11 16:02:31 +0100378/**
379 * @brief Find a feature of the given name and referenced in the given module.
380 *
381 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
382 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
383 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
384 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
385 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
386 * assigned till that time will be still valid.
387 *
388 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
389 * @param[in] name Name of the feature including possible prefix.
390 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
391 * @return Pointer to the feature structure if found, NULL otherwise.
392 */
Radek Krejci19a96102018-11-15 13:38:09 +0100393static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100394lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100395{
396 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100397 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100398
399 for (i = 0; i < len; ++i) {
400 if (name[i] == ':') {
401 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100402 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100403 LY_CHECK_RET(!mod, NULL);
404
405 name = &name[i + 1];
406 len = len - i - 1;
407 }
408 }
409
410 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100411 if (mod->implemented) {
412 /* module is implemented so there is already the compiled schema */
413 flist = mod->compiled->features;
414 } else {
415 flist = mod->off_features;
416 }
417 LY_ARRAY_FOR(flist, i) {
418 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100419 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
420 return f;
421 }
422 }
423
424 return NULL;
425}
426
427static LY_ERR
Radek Krejci0935f412019-08-20 16:15:18 +0200428lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext, void *parent, LYEXT_PARENT parent_type)
Radek Krejci19a96102018-11-15 13:38:09 +0100429{
430 const char *name;
431 unsigned int u;
432 const struct lys_module *mod;
Radek Krejci0935f412019-08-20 16:15:18 +0200433 struct lysc_ext *elist = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100434
435 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
436 ext->insubstmt = ext_p->insubstmt;
437 ext->insubstmt_index = ext_p->insubstmt_index;
Radek Krejci28681fa2019-09-06 13:08:45 +0200438 ext->module = ctx->mod_def;
Radek Krejci0935f412019-08-20 16:15:18 +0200439 ext->parent = parent;
440 ext->parent_type = parent_type;
Radek Krejci19a96102018-11-15 13:38:09 +0100441
Radek Krejcif56e2a42019-09-09 14:15:25 +0200442 lysc_update_path(ctx, ext->parent_type == LYEXT_PAR_NODE ? (struct lysc_node*)ext->parent : NULL, "{extension}");
443 lysc_update_path(ctx, NULL, ext_p->name );
444
Radek Krejci19a96102018-11-15 13:38:09 +0100445 /* get module where the extension definition should be placed */
446 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100447 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100448 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
449 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
450 LY_EVALID);
451 LY_CHECK_ERR_RET(!mod->parsed->extensions,
452 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
453 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100454 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100455 LY_EVALID);
456 name = &ext_p->name[u + 1];
Radek Krejci0935f412019-08-20 16:15:18 +0200457
Radek Krejci19a96102018-11-15 13:38:09 +0100458 /* find the extension definition there */
Radek Krejci0935f412019-08-20 16:15:18 +0200459 if (mod->off_extensions) {
460 elist = mod->off_extensions;
461 } else {
462 elist = mod->compiled->extensions;
463 }
464 LY_ARRAY_FOR(elist, u) {
465 if (!strcmp(name, elist[u].name)) {
466 ext->def = &elist[u];
Radek Krejci19a96102018-11-15 13:38:09 +0100467 break;
468 }
469 }
Radek Krejci0935f412019-08-20 16:15:18 +0200470 LY_CHECK_ERR_RET(!ext->def,
471 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
472 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100473 LY_EVALID);
Radek Krejci0935f412019-08-20 16:15:18 +0200474
Radek Krejcif56e2a42019-09-09 14:15:25 +0200475 /* unify the parsed extension from YIN and YANG sources. Without extension definition, it is not possible
476 * to get extension's argument from YIN source, so it is stored as one of the substatements. Here we have
477 * to find it, mark it with LYS_YIN_ARGUMENT and store it in the compiled structure. */
478 if (ext_p->yin && ext->def->argument) {
479 /* Schema was parsed from YIN and an argument is expected, ... */
480 struct lysp_stmt *stmt = NULL;
481
482 if (ext->def->flags & LYS_YINELEM_TRUE) {
483 /* ... argument was the first XML child element */
484 if (ext_p->child && !(ext_p->child->flags & LYS_YIN_ATTR)) {
485 /* TODO check namespace of the statement */
486 if (!strcmp(ext_p->child->stmt, ext->def->argument)) {
487 stmt = ext_p->child;
488 }
489 }
490 } else {
491 /* ... argument was one of the XML attributes which are represented as child stmt
492 * with LYS_YIN_ATTR flag */
493 for (stmt = ext_p->child; stmt && (stmt->flags & LYS_YIN_ATTR); stmt = stmt->next) {
494 if (!strcmp(stmt->stmt, ext->def->argument)) {
495 /* this is the extension's argument */
496 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
497 break;
498 }
499 }
500 }
501 if (!stmt) {
502 /* missing extension's argument */
503 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
504 "Extension instance \"%s\" misses argument \"%s\".", ext_p->name, ext->def->argument);
505 return LY_EVALID;
506
507 }
508 ext->argument = lydict_insert(ctx->ctx, stmt->arg, 0);
509 stmt->flags |= LYS_YIN_ARGUMENT;
510 }
511
Radek Krejci0935f412019-08-20 16:15:18 +0200512 if (ext->def->plugin && ext->def->plugin->compile) {
Radek Krejciad5963b2019-09-06 16:03:05 +0200513 if (ext->argument) {
514 lysc_update_path(ctx, (struct lysc_node*)ext, ext->argument);
515 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200516 LY_CHECK_RET(ext->def->plugin->compile(ctx, ext_p, ext), LY_EVALID);
Radek Krejciad5963b2019-09-06 16:03:05 +0200517 if (ext->argument) {
518 lysc_update_path(ctx, NULL, NULL);
519 }
Radek Krejci0935f412019-08-20 16:15:18 +0200520 }
Radek Krejcif56e2a42019-09-09 14:15:25 +0200521 ext_p->compiled = ext;
522
523 lysc_update_path(ctx, NULL, NULL);
524 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0935f412019-08-20 16:15:18 +0200525
526 return LY_SUCCESS;
527}
528
529/**
530 * @brief Fill in the prepared compiled extensions definition structure according to the parsed extension definition.
531 */
532static LY_ERR
533lys_compile_extension(struct lysc_ctx *ctx, struct lysp_ext *ext_p, struct lysc_ext *ext)
534{
535 LY_ERR ret = LY_SUCCESS;
536
537 DUP_STRING(ctx->ctx, ext_p->name, ext->name);
538 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
539 ext->module = ctx->mod_def;
540 COMPILE_EXTS_GOTO(ctx, ext_p->exts, ext->exts, ext, LYEXT_PAR_EXT, ret, done);
541
542done:
543 return ret;
544}
545
546/**
547 * @brief Link the extensions definitions with the available extension plugins.
548 *
549 * This is done only in the compiled (implemented) module. Extensions of a non-implemented modules
550 * are not connected with even available extension plugins.
551 *
552 * @param[in] extensions List of extensions to be processed ([sized array](@ref sizedarrays)).
553 */
554static void
555lys_compile_extension_plugins(struct lysc_ext *extensions)
556{
557 unsigned int u;
558
559 LY_ARRAY_FOR(extensions, u) {
560 extensions[u].plugin = lyext_get_plugin(&extensions[u]);
561 }
562}
563
564LY_ERR
565lys_extension_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lys_module *module,
566 struct lysp_ext *extensions_p, struct lysc_ext **extensions)
567{
568 unsigned int offset = 0, u;
569 struct lysc_ctx context = {0};
570
571 assert(ctx_sc || ctx);
572
573 if (!ctx_sc) {
574 context.ctx = ctx;
575 context.mod = module;
576 context.path_len = 1;
577 context.path[0] = '/';
578 ctx_sc = &context;
579 }
580
581 if (!extensions_p) {
582 return LY_SUCCESS;
583 }
584 if (*extensions) {
585 offset = LY_ARRAY_SIZE(*extensions);
586 }
587
588 lysc_update_path(ctx_sc, NULL, "{extension}");
589 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *extensions, LY_ARRAY_SIZE(extensions_p), LY_EMEM);
590 LY_ARRAY_FOR(extensions_p, u) {
591 lysc_update_path(ctx_sc, NULL, extensions_p[u].name);
592 LY_ARRAY_INCREMENT(*extensions);
593 COMPILE_CHECK_UNIQUENESS(ctx_sc, *extensions, name, &(*extensions)[offset + u], "extension", extensions_p[u].name);
594 LY_CHECK_RET(lys_compile_extension(ctx_sc, &extensions_p[u], &(*extensions)[offset + u]));
595 lysc_update_path(ctx_sc, NULL, NULL);
596 }
597 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100598
599 return LY_SUCCESS;
600}
601
Radek Krejcib56c7502019-02-13 14:19:54 +0100602/**
603 * @brief Compile information from the if-feature statement
604 * @param[in] ctx Compile context.
605 * @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 +0100606 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
607 * @return LY_ERR value.
608 */
Radek Krejci19a96102018-11-15 13:38:09 +0100609static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200610lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100611{
612 const char *c = *value;
613 int r, rc = EXIT_FAILURE;
614 int i, j, last_not, checkversion = 0;
615 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
616 uint8_t op;
617 struct iff_stack stack = {0, 0, NULL};
618 struct lysc_feature *f;
619
620 assert(c);
621
622 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
623 for (i = j = last_not = 0; c[i]; i++) {
624 if (c[i] == '(') {
625 j++;
626 checkversion = 1;
627 continue;
628 } else if (c[i] == ')') {
629 j--;
630 continue;
631 } else if (isspace(c[i])) {
632 checkversion = 1;
633 continue;
634 }
635
636 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 +0200637 int sp;
638 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
639 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100640 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
641 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
642 return LY_EVALID;
643 } else if (!isspace(c[i + r])) {
644 /* feature name starting with the not/and/or */
645 last_not = 0;
646 f_size++;
647 } else if (c[i] == 'n') { /* not operation */
648 if (last_not) {
649 /* double not */
650 expr_size = expr_size - 2;
651 last_not = 0;
652 } else {
653 last_not = 1;
654 }
655 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200656 if (f_exp != f_size) {
657 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
658 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
659 return LY_EVALID;
660 }
Radek Krejci19a96102018-11-15 13:38:09 +0100661 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200662
Radek Krejci19a96102018-11-15 13:38:09 +0100663 /* not a not operation */
664 last_not = 0;
665 }
666 i += r;
667 } else {
668 f_size++;
669 last_not = 0;
670 }
671 expr_size++;
672
673 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200674 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100675 i--;
676 break;
677 }
678 i++;
679 }
680 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200681 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100682 /* not matching count of ( and ) */
683 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
684 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
685 return LY_EVALID;
686 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200687 if (f_exp != f_size) {
688 /* features do not match the needed arguments for the logical operations */
689 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
690 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
691 "the required number of operands for the operations.", *value);
692 return LY_EVALID;
693 }
Radek Krejci19a96102018-11-15 13:38:09 +0100694
695 if (checkversion || expr_size > 1) {
696 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100697 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
699 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
700 return LY_EVALID;
701 }
702 }
703
704 /* allocate the memory */
705 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
706 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
707 stack.stack = malloc(expr_size * sizeof *stack.stack);
708 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
709
710 stack.size = expr_size;
711 f_size--; expr_size--; /* used as indexes from now */
712
713 for (i--; i >= 0; i--) {
714 if (c[i] == ')') {
715 /* push it on stack */
716 iff_stack_push(&stack, LYS_IFF_RP);
717 continue;
718 } else if (c[i] == '(') {
719 /* pop from the stack into result all operators until ) */
720 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
721 iff_setop(iff->expr, op, expr_size--);
722 }
723 continue;
724 } else if (isspace(c[i])) {
725 continue;
726 }
727
728 /* end of operator or operand -> find beginning and get what is it */
729 j = i + 1;
730 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
731 i--;
732 }
733 i++; /* go back by one step */
734
735 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
736 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
737 /* double not */
738 iff_stack_pop(&stack);
739 } else {
740 /* not has the highest priority, so do not pop from the stack
741 * as in case of AND and OR */
742 iff_stack_push(&stack, LYS_IFF_NOT);
743 }
744 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
745 /* as for OR - pop from the stack all operators with the same or higher
746 * priority and store them to the result, then push the AND to the stack */
747 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
748 op = iff_stack_pop(&stack);
749 iff_setop(iff->expr, op, expr_size--);
750 }
751 iff_stack_push(&stack, LYS_IFF_AND);
752 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
753 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
754 op = iff_stack_pop(&stack);
755 iff_setop(iff->expr, op, expr_size--);
756 }
757 iff_stack_push(&stack, LYS_IFF_OR);
758 } else {
759 /* feature name, length is j - i */
760
761 /* add it to the expression */
762 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
763
764 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100765 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
766 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
767 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
768 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100769 iff->features[f_size] = f;
770 LY_ARRAY_INCREMENT(iff->features);
771 f_size--;
772 }
773 }
774 while (stack.index) {
775 op = iff_stack_pop(&stack);
776 iff_setop(iff->expr, op, expr_size--);
777 }
778
779 if (++expr_size || ++f_size) {
780 /* not all expected operators and operands found */
781 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
782 "Invalid value \"%s\" of if-feature - processing error.", *value);
783 rc = LY_EINT;
784 } else {
785 rc = LY_SUCCESS;
786 }
787
788error:
789 /* cleanup */
790 iff_stack_clean(&stack);
791
792 return rc;
793}
794
Radek Krejcib56c7502019-02-13 14:19:54 +0100795/**
796 * @brief Compile information from the when statement
797 * @param[in] ctx Compile context.
798 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100799 * @param[out] when Pointer where to store pointer to the created compiled when structure.
800 * @return LY_ERR value.
801 */
Radek Krejci19a96102018-11-15 13:38:09 +0100802static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200803lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100804{
Radek Krejci19a96102018-11-15 13:38:09 +0100805 LY_ERR ret = LY_SUCCESS;
806
Radek Krejci00b874b2019-02-12 10:54:50 +0100807 *when = calloc(1, sizeof **when);
808 (*when)->refcount = 1;
809 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
Radek Krejcia0f704a2019-09-09 16:12:23 +0200810 (*when)->module = ctx->mod_def;
Radek Krejci00b874b2019-02-12 10:54:50 +0100811 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
812 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
813 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200814 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100815
816done:
817 return ret;
818}
819
Radek Krejcib56c7502019-02-13 14:19:54 +0100820/**
821 * @brief Compile information from the must statement
822 * @param[in] ctx Compile context.
823 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100824 * @param[in,out] must Prepared (empty) compiled must structure to fill.
825 * @return LY_ERR value.
826 */
Radek Krejci19a96102018-11-15 13:38:09 +0100827static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200828lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100829{
Radek Krejci19a96102018-11-15 13:38:09 +0100830 LY_ERR ret = LY_SUCCESS;
831
832 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
833 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200834 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100835 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
836 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100837 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
838 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200839 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100840
841done:
842 return ret;
843}
844
Radek Krejcib56c7502019-02-13 14:19:54 +0100845/**
846 * @brief Compile information from the import statement
847 * @param[in] ctx Compile context.
848 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100849 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
850 * @return LY_ERR value.
851 */
Radek Krejci19a96102018-11-15 13:38:09 +0100852static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200853lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100854{
Radek Krejci19a96102018-11-15 13:38:09 +0100855 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100856 LY_ERR ret = LY_SUCCESS;
857
858 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200859 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100860 imp->module = imp_p->module;
861
Radek Krejci7f2a5362018-11-28 13:05:37 +0100862 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
863 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100864 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100865 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100866 /* try to use filepath if present */
867 if (imp->module->filepath) {
868 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
869 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100870 if (mod != imp->module) {
871 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100872 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100873 mod = NULL;
874 }
875 }
876 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100877 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100878 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 +0100879 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100880 return LY_ENOTFOUND;
881 }
882 }
Radek Krejci19a96102018-11-15 13:38:09 +0100883 }
884
885done:
886 return ret;
887}
888
Radek Krejcib56c7502019-02-13 14:19:54 +0100889/**
890 * @brief Compile information from the identity statement
891 *
892 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
893 *
894 * @param[in] ctx Compile context.
895 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100896 * @param[in] idents List of so far compiled identities to check the name uniqueness.
897 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
898 * @return LY_ERR value.
899 */
Radek Krejci19a96102018-11-15 13:38:09 +0100900static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200901lys_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 +0100902{
903 unsigned int u;
904 LY_ERR ret = LY_SUCCESS;
905
Radek Krejci327de162019-06-14 12:52:07 +0200906 lysc_update_path(ctx, NULL, ident_p->name);
907
Radek Krejcid05cbd92018-12-05 14:26:40 +0100908 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100909 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200910 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
911 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200912 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200913 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100914 /* 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 +0200915 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100916 ident->flags = ident_p->flags;
917
Radek Krejci327de162019-06-14 12:52:07 +0200918 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100919done:
920 return ret;
921}
922
Radek Krejcib56c7502019-02-13 14:19:54 +0100923/**
924 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
925 *
926 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
927 *
928 * @param[in] ctx Compile context for logging.
929 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
930 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
931 * @return LY_SUCCESS if everything is ok.
932 * @return LY_EVALID if the identity is derived from itself.
933 */
Radek Krejci38222632019-02-12 16:55:05 +0100934static LY_ERR
935lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
936{
937 LY_ERR ret = LY_EVALID;
938 unsigned int u, v;
939 struct ly_set recursion = {0};
940 struct lysc_ident *drv;
941
942 if (!derived) {
943 return LY_SUCCESS;
944 }
945
946 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
947 if (ident == derived[u]) {
948 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
949 "Identity \"%s\" is indirectly derived from itself.", ident->name);
950 goto cleanup;
951 }
952 ly_set_add(&recursion, derived[u], 0);
953 }
954
955 for (v = 0; v < recursion.count; ++v) {
956 drv = recursion.objs[v];
957 if (!drv->derived) {
958 continue;
959 }
960 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
961 if (ident == drv->derived[u]) {
962 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
963 "Identity \"%s\" is indirectly derived from itself.", ident->name);
964 goto cleanup;
965 }
966 ly_set_add(&recursion, drv->derived[u], 0);
967 }
968 }
969 ret = LY_SUCCESS;
970
971cleanup:
972 ly_set_erase(&recursion, NULL);
973 return ret;
974}
975
Radek Krejcia3045382018-11-22 14:30:31 +0100976/**
977 * @brief Find and process the referenced base identities from another identity or identityref
978 *
Radek Krejciaca74032019-06-04 08:53:06 +0200979 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +0100980 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
981 * to distinguish these two use cases.
982 *
983 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
984 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
985 * @param[in] ident Referencing identity to work with.
986 * @param[in] bases Array of bases of identityref to fill in.
987 * @return LY_ERR value.
988 */
Radek Krejci19a96102018-11-15 13:38:09 +0100989static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100990lys_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 +0100991{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100992 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100993 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100994 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100995 struct lysc_ident **idref;
996
997 assert(ident || bases);
998
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100999 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001000 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1001 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1002 return LY_EVALID;
1003 }
1004
1005 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1006 s = strchr(bases_p[u], ':');
1007 if (s) {
1008 /* prefixed identity */
1009 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +01001010 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001011 } else {
1012 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +01001013 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001014 }
1015 if (!mod) {
1016 if (ident) {
1017 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1018 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1019 } else {
1020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1021 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1022 }
1023 return LY_EVALID;
1024 }
1025 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001026 if (mod->compiled && mod->compiled->identities) {
1027 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1028 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001029 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001030 if (ident == &mod->compiled->identities[v]) {
1031 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1032 "Identity \"%s\" is derived from itself.", ident->name);
1033 return LY_EVALID;
1034 }
1035 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001036 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001037 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001038 *idref = ident;
1039 } else {
1040 /* we have match! store the found identity */
1041 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001042 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001043 }
1044 break;
1045 }
1046 }
1047 }
1048 if (!idref || !(*idref)) {
1049 if (ident) {
1050 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1051 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1052 } else {
1053 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1054 "Unable to find base (%s) of identityref.", bases_p[u]);
1055 }
1056 return LY_EVALID;
1057 }
1058 }
1059 return LY_SUCCESS;
1060}
1061
Radek Krejcia3045382018-11-22 14:30:31 +01001062/**
1063 * @brief For the given array of identities, set the backlinks from all their base identities.
1064 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1065 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1066 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1067 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1068 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001069static LY_ERR
1070lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1071{
1072 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +01001073
1074 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
1075 if (!idents_p[i].bases) {
1076 continue;
1077 }
Radek Krejci327de162019-06-14 12:52:07 +02001078 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001079 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001080 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001081 }
1082 return LY_SUCCESS;
1083}
1084
Radek Krejci0af46292019-01-11 16:02:31 +01001085LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001086lys_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 +01001087{
1088 unsigned int offset = 0, u;
1089 struct lysc_ctx context = {0};
1090
Radek Krejci327de162019-06-14 12:52:07 +02001091 assert(ctx_sc || ctx);
1092
1093 if (!ctx_sc) {
1094 context.ctx = ctx;
1095 context.mod = module;
1096 context.path_len = 1;
1097 context.path[0] = '/';
1098 ctx_sc = &context;
1099 }
Radek Krejci0af46292019-01-11 16:02:31 +01001100
1101 if (!features_p) {
1102 return LY_SUCCESS;
1103 }
1104 if (*features) {
1105 offset = LY_ARRAY_SIZE(*features);
1106 }
1107
Radek Krejci327de162019-06-14 12:52:07 +02001108 lysc_update_path(ctx_sc, NULL, "{feature}");
1109 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001110 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001111 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1112
Radek Krejci0af46292019-01-11 16:02:31 +01001113 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +02001114 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
1115 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1116 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1117 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001118 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001119 (*features)[offset + u].module = ctx_sc->mod;
1120
1121 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001122 }
Radek Krejci327de162019-06-14 12:52:07 +02001123 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001124
1125 return LY_SUCCESS;
1126}
1127
Radek Krejcia3045382018-11-22 14:30:31 +01001128/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001129 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001130 *
1131 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1132 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001133 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001134 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1135 * being currently processed).
1136 * @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 +01001137 * @return LY_SUCCESS if everything is ok.
1138 * @return LY_EVALID if the feature references indirectly itself.
1139 */
1140static LY_ERR
1141lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1142{
1143 LY_ERR ret = LY_EVALID;
1144 unsigned int u, v;
1145 struct ly_set recursion = {0};
1146 struct lysc_feature *drv;
1147
1148 if (!depfeatures) {
1149 return LY_SUCCESS;
1150 }
1151
1152 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1153 if (feature == depfeatures[u]) {
1154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1155 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1156 goto cleanup;
1157 }
1158 ly_set_add(&recursion, depfeatures[u], 0);
1159 }
1160
1161 for (v = 0; v < recursion.count; ++v) {
1162 drv = recursion.objs[v];
1163 if (!drv->depfeatures) {
1164 continue;
1165 }
1166 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1167 if (feature == drv->depfeatures[u]) {
1168 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1169 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1170 goto cleanup;
1171 }
1172 ly_set_add(&recursion, drv->depfeatures[u], 0);
1173 }
1174 }
1175 ret = LY_SUCCESS;
1176
1177cleanup:
1178 ly_set_erase(&recursion, NULL);
1179 return ret;
1180}
1181
1182/**
Radek Krejci0af46292019-01-11 16:02:31 +01001183 * @brief Create pre-compiled features array.
1184 *
1185 * See lys_feature_precompile() for more details.
1186 *
Radek Krejcia3045382018-11-22 14:30:31 +01001187 * @param[in] ctx Compile context.
1188 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001189 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001190 * @return LY_ERR value.
1191 */
Radek Krejci19a96102018-11-15 13:38:09 +01001192static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001193lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001194{
Radek Krejci0af46292019-01-11 16:02:31 +01001195 unsigned int u, v, x;
1196 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001197 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001198
Radek Krejci327de162019-06-14 12:52:07 +02001199
Radek Krejci0af46292019-01-11 16:02:31 +01001200 /* find the preprecompiled feature */
1201 LY_ARRAY_FOR(features, x) {
1202 if (strcmp(features[x].name, feature_p->name)) {
1203 continue;
1204 }
1205 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001206 lysc_update_path(ctx, NULL, "{feature}");
1207 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001208
Radek Krejci0af46292019-01-11 16:02:31 +01001209 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001210 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001211 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001212 if (feature->iffeatures) {
1213 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1214 if (feature->iffeatures[u].features) {
1215 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001216 /* check for circular dependency - direct reference first,... */
1217 if (feature == feature->iffeatures[u].features[v]) {
1218 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1219 "Feature \"%s\" is referenced from itself.", feature->name);
1220 return LY_EVALID;
1221 }
1222 /* ... and indirect circular reference */
1223 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1224
Radek Krejci0af46292019-01-11 16:02:31 +01001225 /* add itself into the dependants list */
1226 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1227 *df = feature;
1228 }
Radek Krejci19a96102018-11-15 13:38:09 +01001229 }
Radek Krejci19a96102018-11-15 13:38:09 +01001230 }
1231 }
Radek Krejci327de162019-06-14 12:52:07 +02001232 lysc_update_path(ctx, NULL, NULL);
1233 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001234 done:
1235 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001236 }
Radek Krejci0af46292019-01-11 16:02:31 +01001237
1238 LOGINT(ctx->ctx);
1239 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001240}
1241
Radek Krejcib56c7502019-02-13 14:19:54 +01001242/**
1243 * @brief Revert compiled list of features back to the precompiled state.
1244 *
1245 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1246 * The features are supposed to be stored again as off_features in ::lys_module structure.
1247 *
1248 * @param[in] ctx Compilation context.
1249 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1250 * and supposed to hold the off_features list.
1251 */
Radek Krejci95710c92019-02-11 15:49:55 +01001252static void
1253lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1254{
1255 unsigned int u, v;
1256
1257 /* keep the off_features list until the complete lys_module is freed */
1258 mod->off_features = mod->compiled->features;
1259 mod->compiled->features = NULL;
1260
1261 /* in the off_features list, remove all the parts (from finished compiling process)
1262 * which may points into the data being freed here */
1263 LY_ARRAY_FOR(mod->off_features, u) {
1264 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1265 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1266 }
1267 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1268 mod->off_features[u].iffeatures = NULL;
1269
1270 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1271 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1272 }
1273 LY_ARRAY_FREE(mod->off_features[u].exts);
1274 mod->off_features[u].exts = NULL;
1275 }
1276}
1277
Radek Krejcia3045382018-11-22 14:30:31 +01001278/**
1279 * @brief Validate and normalize numeric value from a range definition.
1280 * @param[in] ctx Compile context.
1281 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1282 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1283 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1284 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1285 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1286 * @param[in] value String value of the range boundary.
1287 * @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.
1288 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1289 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1290 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001291LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001292range_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 +01001293{
Radek Krejci6cba4292018-11-15 17:33:29 +01001294 size_t fraction = 0, size;
1295
Radek Krejci19a96102018-11-15 13:38:09 +01001296 *len = 0;
1297
1298 assert(value);
1299 /* parse value */
1300 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1301 return LY_EVALID;
1302 }
1303
1304 if ((value[*len] == '-') || (value[*len] == '+')) {
1305 ++(*len);
1306 }
1307
1308 while (isdigit(value[*len])) {
1309 ++(*len);
1310 }
1311
1312 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001313 if (basetype == LY_TYPE_DEC64) {
1314 goto decimal;
1315 } else {
1316 *valcopy = strndup(value, *len);
1317 return LY_SUCCESS;
1318 }
Radek Krejci19a96102018-11-15 13:38:09 +01001319 }
1320 fraction = *len;
1321
1322 ++(*len);
1323 while (isdigit(value[*len])) {
1324 ++(*len);
1325 }
1326
Radek Krejci6cba4292018-11-15 17:33:29 +01001327 if (basetype == LY_TYPE_DEC64) {
1328decimal:
1329 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001330 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001331 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1332 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1333 *len, value, frdigits);
1334 return LY_EINVAL;
1335 }
1336 if (fraction) {
1337 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1338 } else {
1339 size = (*len) + frdigits + 1;
1340 }
1341 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001342 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1343
Radek Krejci6cba4292018-11-15 17:33:29 +01001344 (*valcopy)[size - 1] = '\0';
1345 if (fraction) {
1346 memcpy(&(*valcopy)[0], &value[0], fraction);
1347 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1348 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1349 } else {
1350 memcpy(&(*valcopy)[0], &value[0], *len);
1351 memset(&(*valcopy)[*len], '0', frdigits);
1352 }
Radek Krejci19a96102018-11-15 13:38:09 +01001353 }
1354 return LY_SUCCESS;
1355}
1356
Radek Krejcia3045382018-11-22 14:30:31 +01001357/**
1358 * @brief Check that values in range are in ascendant order.
1359 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001360 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1361 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001362 * @param[in] value Current value to check.
1363 * @param[in] prev_value The last seen value.
1364 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1365 */
Radek Krejci19a96102018-11-15 13:38:09 +01001366static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001367range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001368{
1369 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001370 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001371 return LY_EEXIST;
1372 }
1373 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001374 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001375 return LY_EEXIST;
1376 }
1377 }
1378 return LY_SUCCESS;
1379}
1380
Radek Krejcia3045382018-11-22 14:30:31 +01001381/**
1382 * @brief Set min/max value of the range part.
1383 * @param[in] ctx Compile context.
1384 * @param[in] part Range part structure to fill.
1385 * @param[in] max Flag to distinguish if storing min or max value.
1386 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1387 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1388 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1389 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1390 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001391 * @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 +01001392 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1393 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1394 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1395 * frdigits value), LY_EMEM.
1396 */
Radek Krejci19a96102018-11-15 13:38:09 +01001397static LY_ERR
1398range_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 +01001399 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001400{
1401 LY_ERR ret = LY_SUCCESS;
1402 char *valcopy = NULL;
1403 size_t len;
1404
1405 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001406 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001407 LY_CHECK_GOTO(ret, finalize);
1408 }
1409 if (!valcopy && base_range) {
1410 if (max) {
1411 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1412 } else {
1413 part->min_64 = base_range->parts[0].min_64;
1414 }
1415 if (!first) {
1416 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1417 }
1418 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001419 }
1420
1421 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001422 case LY_TYPE_INT8: /* range */
1423 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001424 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 +01001425 } else if (max) {
1426 part->max_64 = INT64_C(127);
1427 } else {
1428 part->min_64 = INT64_C(-128);
1429 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001430 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001431 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001432 }
1433 break;
1434 case LY_TYPE_INT16: /* range */
1435 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001436 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 +01001437 } else if (max) {
1438 part->max_64 = INT64_C(32767);
1439 } else {
1440 part->min_64 = INT64_C(-32768);
1441 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001442 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001443 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001444 }
1445 break;
1446 case LY_TYPE_INT32: /* range */
1447 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001448 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 +01001449 } else if (max) {
1450 part->max_64 = INT64_C(2147483647);
1451 } else {
1452 part->min_64 = INT64_C(-2147483648);
1453 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001454 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001455 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001456 }
1457 break;
1458 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001459 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001460 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001461 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001462 max ? &part->max_64 : &part->min_64);
1463 } else if (max) {
1464 part->max_64 = INT64_C(9223372036854775807);
1465 } else {
1466 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1467 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001468 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001469 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001470 }
1471 break;
1472 case LY_TYPE_UINT8: /* range */
1473 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001474 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 +01001475 } else if (max) {
1476 part->max_u64 = UINT64_C(255);
1477 } else {
1478 part->min_u64 = UINT64_C(0);
1479 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001480 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001481 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001482 }
1483 break;
1484 case LY_TYPE_UINT16: /* range */
1485 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001486 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 +01001487 } else if (max) {
1488 part->max_u64 = UINT64_C(65535);
1489 } else {
1490 part->min_u64 = UINT64_C(0);
1491 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001492 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001493 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001494 }
1495 break;
1496 case LY_TYPE_UINT32: /* range */
1497 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001498 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 +01001499 } else if (max) {
1500 part->max_u64 = UINT64_C(4294967295);
1501 } else {
1502 part->min_u64 = UINT64_C(0);
1503 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001504 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001505 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001506 }
1507 break;
1508 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001509 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001510 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001511 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001512 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 +01001513 } else if (max) {
1514 part->max_u64 = UINT64_C(18446744073709551615);
1515 } else {
1516 part->min_u64 = UINT64_C(0);
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(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001520 }
1521 break;
1522 default:
1523 LOGINT(ctx->ctx);
1524 ret = LY_EINT;
1525 }
1526
Radek Krejci5969f272018-11-23 10:03:58 +01001527finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001528 if (ret == LY_EDENIED) {
1529 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1530 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1531 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1532 } else if (ret == LY_EVALID) {
1533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1534 "Invalid %s restriction - invalid value \"%s\".",
1535 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1536 } else if (ret == LY_EEXIST) {
1537 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1538 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001539 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001540 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001541 } else if (!ret && value) {
1542 *value = *value + len;
1543 }
1544 free(valcopy);
1545 return ret;
1546}
1547
Radek Krejcia3045382018-11-22 14:30:31 +01001548/**
1549 * @brief Compile the parsed range restriction.
1550 * @param[in] ctx Compile context.
1551 * @param[in] range_p Parsed range structure to compile.
1552 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1553 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1554 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1555 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1556 * range restriction must be more restrictive than the base_range.
1557 * @param[in,out] range Pointer to the created current range structure.
1558 * @return LY_ERR value.
1559 */
Radek Krejci19a96102018-11-15 13:38:09 +01001560static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001561lys_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 +01001562 struct lysc_range *base_range, struct lysc_range **range)
1563{
1564 LY_ERR ret = LY_EVALID;
1565 const char *expr;
1566 struct lysc_range_part *parts = NULL, *part;
1567 int range_expected = 0, uns;
1568 unsigned int parts_done = 0, u, v;
1569
1570 assert(range);
1571 assert(range_p);
1572
1573 expr = range_p->arg;
1574 while(1) {
1575 if (isspace(*expr)) {
1576 ++expr;
1577 } else if (*expr == '\0') {
1578 if (range_expected) {
1579 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1580 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1581 length_restr ? "length" : "range", range_p->arg);
1582 goto cleanup;
1583 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1584 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1585 "Invalid %s restriction - unexpected end of the expression (%s).",
1586 length_restr ? "length" : "range", range_p->arg);
1587 goto cleanup;
1588 }
1589 parts_done++;
1590 break;
1591 } else if (!strncmp(expr, "min", 3)) {
1592 if (parts) {
1593 /* min cannot be used elsewhere than in the first part */
1594 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1595 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1596 expr - range_p->arg, range_p->arg);
1597 goto cleanup;
1598 }
1599 expr += 3;
1600
1601 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001602 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 +01001603 part->max_64 = part->min_64;
1604 } else if (*expr == '|') {
1605 if (!parts || range_expected) {
1606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1607 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1608 goto cleanup;
1609 }
1610 expr++;
1611 parts_done++;
1612 /* process next part of the expression */
1613 } else if (!strncmp(expr, "..", 2)) {
1614 expr += 2;
1615 while (isspace(*expr)) {
1616 expr++;
1617 }
1618 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1620 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1621 goto cleanup;
1622 }
1623 /* continue expecting the upper boundary */
1624 range_expected = 1;
1625 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1626 /* number */
1627 if (range_expected) {
1628 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001629 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 +01001630 range_expected = 0;
1631 } else {
1632 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1633 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 +01001634 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001635 part->max_64 = part->min_64;
1636 }
1637
1638 /* continue with possible another expression part */
1639 } else if (!strncmp(expr, "max", 3)) {
1640 expr += 3;
1641 while (isspace(*expr)) {
1642 expr++;
1643 }
1644 if (*expr != '\0') {
1645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1646 length_restr ? "length" : "range", expr);
1647 goto cleanup;
1648 }
1649 if (range_expected) {
1650 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001651 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 +01001652 range_expected = 0;
1653 } else {
1654 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1655 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 +01001656 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001657 part->min_64 = part->max_64;
1658 }
1659 } else {
1660 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1661 length_restr ? "length" : "range", expr);
1662 goto cleanup;
1663 }
1664 }
1665
1666 /* check with the previous range/length restriction */
1667 if (base_range) {
1668 switch (basetype) {
1669 case LY_TYPE_BINARY:
1670 case LY_TYPE_UINT8:
1671 case LY_TYPE_UINT16:
1672 case LY_TYPE_UINT32:
1673 case LY_TYPE_UINT64:
1674 case LY_TYPE_STRING:
1675 uns = 1;
1676 break;
1677 case LY_TYPE_DEC64:
1678 case LY_TYPE_INT8:
1679 case LY_TYPE_INT16:
1680 case LY_TYPE_INT32:
1681 case LY_TYPE_INT64:
1682 uns = 0;
1683 break;
1684 default:
1685 LOGINT(ctx->ctx);
1686 ret = LY_EINT;
1687 goto cleanup;
1688 }
1689 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1690 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1691 goto baseerror;
1692 }
1693 /* current lower bound is not lower than the base */
1694 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1695 /* base has single value */
1696 if (base_range->parts[v].min_64 == parts[u].min_64) {
1697 /* both lower bounds are the same */
1698 if (parts[u].min_64 != parts[u].max_64) {
1699 /* current continues with a range */
1700 goto baseerror;
1701 } else {
1702 /* equal single values, move both forward */
1703 ++v;
1704 continue;
1705 }
1706 } else {
1707 /* base is single value lower than current range, so the
1708 * value from base range is removed in the current,
1709 * move only base and repeat checking */
1710 ++v;
1711 --u;
1712 continue;
1713 }
1714 } else {
1715 /* base is the range */
1716 if (parts[u].min_64 == parts[u].max_64) {
1717 /* current is a single value */
1718 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1719 /* current is behind the base range, so base range is omitted,
1720 * move the base and keep the current for further check */
1721 ++v;
1722 --u;
1723 } /* else it is within the base range, so move the current, but keep the base */
1724 continue;
1725 } else {
1726 /* both are ranges - check the higher bound, the lower was already checked */
1727 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1728 /* higher bound is higher than the current higher bound */
1729 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1730 /* but the current lower bound is also higher, so the base range is omitted,
1731 * continue with the same current, but move the base */
1732 --u;
1733 ++v;
1734 continue;
1735 }
1736 /* current range starts within the base range but end behind it */
1737 goto baseerror;
1738 } else {
1739 /* current range is smaller than the base,
1740 * move current, but stay with the base */
1741 continue;
1742 }
1743 }
1744 }
1745 }
1746 if (u != parts_done) {
1747baseerror:
1748 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1749 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1750 length_restr ? "length" : "range", range_p->arg);
1751 goto cleanup;
1752 }
1753 }
1754
1755 if (!(*range)) {
1756 *range = calloc(1, sizeof **range);
1757 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1758 }
1759
Radek Krejcic8b31002019-01-08 10:24:45 +01001760 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001761 if (range_p->eapptag) {
1762 lydict_remove(ctx->ctx, (*range)->eapptag);
1763 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1764 }
1765 if (range_p->emsg) {
1766 lydict_remove(ctx->ctx, (*range)->emsg);
1767 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1768 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001769 if (range_p->dsc) {
1770 lydict_remove(ctx->ctx, (*range)->dsc);
1771 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1772 }
1773 if (range_p->ref) {
1774 lydict_remove(ctx->ctx, (*range)->ref);
1775 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1776 }
Radek Krejci19a96102018-11-15 13:38:09 +01001777 /* extensions are taken only from the last range by the caller */
1778
1779 (*range)->parts = parts;
1780 parts = NULL;
1781 ret = LY_SUCCESS;
1782cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001783 LY_ARRAY_FREE(parts);
1784
1785 return ret;
1786}
1787
1788/**
1789 * @brief Checks pattern syntax.
1790 *
Radek Krejcia3045382018-11-22 14:30:31 +01001791 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001792 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001793 * @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 +01001794 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001795 */
1796static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001797lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001798{
Radek Krejci54579462019-04-30 12:47:06 +02001799 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001800 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001801 int err_code;
1802 const char *orig_ptr;
1803 PCRE2_SIZE err_offset;
1804 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001805#define URANGE_LEN 19
1806 char *ublock2urange[][2] = {
1807 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1808 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1809 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1810 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1811 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1812 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1813 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1814 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1815 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1816 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1817 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1818 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1819 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1820 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1821 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1822 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1823 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1824 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1825 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1826 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1827 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1828 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1829 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1830 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1831 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1832 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1833 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1834 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1835 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1836 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1837 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1838 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1839 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1840 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1841 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1842 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1843 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1844 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1845 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1846 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1847 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1848 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1849 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1850 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1851 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1852 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1853 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1854 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1855 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1856 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1857 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1858 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1859 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1860 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1861 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1862 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1863 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1864 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1865 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1866 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1867 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1868 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1869 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1870 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1871 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1872 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1873 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1874 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1875 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1876 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1877 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1878 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1879 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1880 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1881 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1882 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1883 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1884 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1885 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1886 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1887 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1888 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1889 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1890 {NULL, NULL}
1891 };
1892
1893 /* adjust the expression to a Perl equivalent
1894 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1895
1896 /* we need to replace all "$" with "\$", count them now */
1897 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1898
1899 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1900 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1901 perl_regex[0] = '\0';
1902
1903 ptr = perl_regex;
1904
Radek Krejci19a96102018-11-15 13:38:09 +01001905 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1906 if (orig_ptr[0] == '$') {
1907 ptr += sprintf(ptr, "\\$");
1908 } else if (orig_ptr[0] == '^') {
1909 ptr += sprintf(ptr, "\\^");
1910 } else {
1911 ptr[0] = orig_ptr[0];
1912 ++ptr;
1913 }
1914 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001915 ptr[0] = '\0';
1916 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001917
1918 /* substitute Unicode Character Blocks with exact Character Ranges */
1919 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1920 start = ptr - perl_regex;
1921
1922 ptr = strchr(ptr, '}');
1923 if (!ptr) {
1924 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1925 pattern, perl_regex + start + 2, "unterminated character property");
1926 free(perl_regex);
1927 return LY_EVALID;
1928 }
1929 end = (ptr - perl_regex) + 1;
1930
1931 /* need more space */
1932 if (end - start < URANGE_LEN) {
1933 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1934 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1935 }
1936
1937 /* find our range */
1938 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1939 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1940 break;
1941 }
1942 }
1943 if (!ublock2urange[idx][0]) {
1944 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1945 pattern, perl_regex + start + 5, "unknown block name");
1946 free(perl_regex);
1947 return LY_EVALID;
1948 }
1949
1950 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1951 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1952 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1953 ++count;
1954 }
1955 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1956 --count;
1957 }
1958 }
1959 if (count) {
1960 /* skip brackets */
1961 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1962 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1963 } else {
1964 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1965 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1966 }
1967 }
1968
1969 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02001970 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1971 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02001972 &err_code, &err_offset, NULL);
1973 if (!code_local) {
1974 PCRE2_UCHAR err_msg[256] = {0};
1975 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001976 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1977 free(perl_regex);
1978 return LY_EVALID;
1979 }
1980 free(perl_regex);
1981
Radek Krejci54579462019-04-30 12:47:06 +02001982 if (code) {
1983 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001984 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001985 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001986 }
1987
1988 return LY_SUCCESS;
1989
1990#undef URANGE_LEN
1991}
1992
Radek Krejcia3045382018-11-22 14:30:31 +01001993/**
1994 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1995 * @param[in] ctx Compile context.
1996 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001997 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1998 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1999 * @param[out] patterns Pointer to the storage for the patterns of the current type.
2000 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2001 */
Radek Krejci19a96102018-11-15 13:38:09 +01002002static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002003lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002004 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2005{
2006 struct lysc_pattern **pattern;
Radek Krejci0935f412019-08-20 16:15:18 +02002007 unsigned int u;
Radek Krejci19a96102018-11-15 13:38:09 +01002008 LY_ERR ret = LY_SUCCESS;
2009
2010 /* first, copy the patterns from the base type */
2011 if (base_patterns) {
2012 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2013 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2014 }
2015
2016 LY_ARRAY_FOR(patterns_p, u) {
2017 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2018 *pattern = calloc(1, sizeof **pattern);
2019 ++(*pattern)->refcount;
2020
Radek Krejci54579462019-04-30 12:47:06 +02002021 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002022 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002023
2024 if (patterns_p[u].arg[0] == 0x15) {
2025 (*pattern)->inverted = 1;
2026 }
Radek Krejci54579462019-04-30 12:47:06 +02002027 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002028 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2029 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002030 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2031 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002032 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002033 }
2034done:
2035 return ret;
2036}
2037
Radek Krejcia3045382018-11-22 14:30:31 +01002038/**
2039 * @brief map of the possible restrictions combination for the specific built-in type.
2040 */
Radek Krejci19a96102018-11-15 13:38:09 +01002041static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2042 0 /* LY_TYPE_UNKNOWN */,
2043 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002044 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2045 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2046 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2047 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2048 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002049 LYS_SET_BIT /* LY_TYPE_BITS */,
2050 0 /* LY_TYPE_BOOL */,
2051 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2052 0 /* LY_TYPE_EMPTY */,
2053 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2054 LYS_SET_BASE /* LY_TYPE_IDENT */,
2055 LYS_SET_REQINST /* LY_TYPE_INST */,
2056 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002057 LYS_SET_TYPE /* LY_TYPE_UNION */,
2058 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002059 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002060 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002061 LYS_SET_RANGE /* LY_TYPE_INT64 */
2062};
2063
2064/**
2065 * @brief stringification of the YANG built-in data types
2066 */
2067const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2068 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2069 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002070};
2071
Radek Krejcia3045382018-11-22 14:30:31 +01002072/**
2073 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2074 * @param[in] ctx Compile context.
2075 * @param[in] enums_p Array of the parsed enum structures to compile.
2076 * @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 +01002077 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2078 * @param[out] enums Newly created array of the compiled enums information for the current type.
2079 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2080 */
Radek Krejci19a96102018-11-15 13:38:09 +01002081static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002082lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002083 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002084{
2085 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02002086 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002087 int32_t value = 0;
2088 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002089 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002090
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002091 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002092 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2093 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2094 return LY_EVALID;
2095 }
2096
2097 LY_ARRAY_FOR(enums_p, u) {
2098 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2099 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002100 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2101 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002102 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002103 if (base_enums) {
2104 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2105 LY_ARRAY_FOR(base_enums, v) {
2106 if (!strcmp(e->name, base_enums[v].name)) {
2107 break;
2108 }
2109 }
2110 if (v == LY_ARRAY_SIZE(base_enums)) {
2111 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2112 "Invalid %s - derived type adds new item \"%s\".",
2113 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2114 return LY_EVALID;
2115 }
2116 match = v;
2117 }
2118
2119 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002120 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002121 if (enums_p[u].flags & LYS_SET_VALUE) {
2122 e->value = (int32_t)enums_p[u].value;
2123 if (!u || e->value >= value) {
2124 value = e->value + 1;
2125 }
2126 /* check collision with other values */
2127 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2128 if (e->value == (*enums)[v].value) {
2129 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2130 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2131 e->value, e->name, (*enums)[v].name);
2132 return LY_EVALID;
2133 }
2134 }
2135 } else if (base_enums) {
2136 /* inherit the assigned value */
2137 e->value = base_enums[match].value;
2138 if (!u || e->value >= value) {
2139 value = e->value + 1;
2140 }
2141 } else {
2142 /* assign value automatically */
2143 if (u && value == INT32_MIN) {
2144 /* counter overflow */
2145 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2146 "Invalid enumeration - it is not possible to auto-assign enum value for "
2147 "\"%s\" since the highest value is already 2147483647.", e->name);
2148 return LY_EVALID;
2149 }
2150 e->value = value++;
2151 }
2152 } else { /* LY_TYPE_BITS */
2153 if (enums_p[u].flags & LYS_SET_VALUE) {
2154 e->value = (int32_t)enums_p[u].value;
2155 if (!u || (uint32_t)e->value >= position) {
2156 position = (uint32_t)e->value + 1;
2157 }
2158 /* check collision with other values */
2159 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2160 if (e->value == (*enums)[v].value) {
2161 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2162 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2163 (uint32_t)e->value, e->name, (*enums)[v].name);
2164 return LY_EVALID;
2165 }
2166 }
2167 } else if (base_enums) {
2168 /* inherit the assigned value */
2169 e->value = base_enums[match].value;
2170 if (!u || (uint32_t)e->value >= position) {
2171 position = (uint32_t)e->value + 1;
2172 }
2173 } else {
2174 /* assign value automatically */
2175 if (u && position == 0) {
2176 /* counter overflow */
2177 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2178 "Invalid bits - it is not possible to auto-assign bit position for "
2179 "\"%s\" since the highest value is already 4294967295.", e->name);
2180 return LY_EVALID;
2181 }
2182 e->value = position++;
2183 }
2184 }
2185
2186 if (base_enums) {
2187 /* the assigned values must not change from the derived type */
2188 if (e->value != base_enums[match].value) {
2189 if (basetype == LY_TYPE_ENUM) {
2190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2191 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2192 e->name, base_enums[match].value, e->value);
2193 } else {
2194 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2195 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2196 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2197 }
2198 return LY_EVALID;
2199 }
2200 }
2201
Radek Krejciec4da802019-05-02 13:02:41 +02002202 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002203 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 +01002204
2205 if (basetype == LY_TYPE_BITS) {
2206 /* keep bits ordered by position */
2207 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2208 if (v != u) {
2209 memcpy(&storage, e, sizeof *e);
2210 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2211 memcpy(&(*enums)[v], &storage, sizeof storage);
2212 }
2213 }
2214 }
2215
2216done:
2217 return ret;
2218}
2219
Radek Krejcia3045382018-11-22 14:30:31 +01002220#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2221 for ((NODE) = (NODE)->parent; \
2222 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
2223 (NODE) = (NODE)->parent); \
2224 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2225 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2226 TERM; \
2227 }
2228
2229/**
2230 * @brief Validate the predicate(s) from the leafref path.
2231 * @param[in] ctx Compile context
2232 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2233 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002234 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002235 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002236 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002237 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2238 */
2239static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002240lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002241 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002242{
2243 LY_ERR ret = LY_EVALID;
2244 const struct lys_module *mod;
2245 const struct lysc_node *src_node, *dst_node;
2246 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2247 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002248 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002249 const char *start, *end, *pke_end;
2250 struct ly_set keys = {0};
2251 int i;
2252
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002253 assert(path_context);
2254
Radek Krejcia3045382018-11-22 14:30:31 +01002255 while (**predicate == '[') {
2256 start = (*predicate)++;
2257
2258 while (isspace(**predicate)) {
2259 ++(*predicate);
2260 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002261 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002262 while (isspace(**predicate)) {
2263 ++(*predicate);
2264 }
2265 if (**predicate != '=') {
2266 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002267 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2268 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002269 goto cleanup;
2270 }
2271 ++(*predicate);
2272 while (isspace(**predicate)) {
2273 ++(*predicate);
2274 }
2275
2276 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2278 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2279 goto cleanup;
2280 }
2281 --pke_end;
2282 while (isspace(*pke_end)) {
2283 --pke_end;
2284 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002285 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002286 /* localize path-key-expr */
2287 pke_start = path_key_expr = *predicate;
2288 /* move after the current predicate */
2289 *predicate = end + 1;
2290
2291 /* source (must be leaf or leaf-list) */
2292 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002293 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002294 if (!mod) {
2295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2296 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002297 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002298 goto cleanup;
2299 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002300 if (!mod->implemented) {
2301 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002302 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002303 }
Radek Krejcia3045382018-11-22 14:30:31 +01002304 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002305 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002306 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002307 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002308 if (!(context_node->flags & LYS_KEYLESS)) {
2309 struct lysc_node *key;
2310 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
2311 if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') {
2312 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002313 break;
2314 }
2315 }
2316 }
Radek Krejcia3045382018-11-22 14:30:31 +01002317 if (!src_node) {
2318 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002319 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002320 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002321 goto cleanup;
2322 }
Radek Krejcia3045382018-11-22 14:30:31 +01002323
2324 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002325 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002326 i = ly_set_add(&keys, (void*)src_node, 0);
2327 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002328 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002329 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002330 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002331 *predicate - start, start, src_node->name);
2332 goto cleanup;
2333 }
2334
2335 /* destination */
2336 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002337 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002338
2339 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002340 if (strncmp(path_key_expr, "current", 7)) {
2341error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2343 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2344 *predicate - start, start);
2345 goto cleanup;
2346 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002347 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2348 if (*path_key_expr != '(') {
2349 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002350 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002351 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2352 if (*path_key_expr != ')') {
2353 goto error_current_function_invocation;
2354 }
2355 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002356
2357 if (*path_key_expr != '/') {
2358 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2359 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2360 *predicate - start, start);
2361 goto cleanup;
2362 }
2363 ++path_key_expr;
2364 while (isspace(*path_key_expr)) {
2365 ++path_key_expr;
2366 }
2367
2368 /* rel-path-keyexpr:
2369 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2370 while (!strncmp(path_key_expr, "..", 2)) {
2371 ++dest_parent_times;
2372 path_key_expr += 2;
2373 while (isspace(*path_key_expr)) {
2374 ++path_key_expr;
2375 }
2376 if (*path_key_expr != '/') {
2377 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2378 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2379 *predicate - start, start);
2380 goto cleanup;
2381 }
2382 ++path_key_expr;
2383 while (isspace(*path_key_expr)) {
2384 ++path_key_expr;
2385 }
2386
2387 /* path is supposed to be evaluated in data tree, so we have to skip
2388 * all schema nodes that cannot be instantiated in data tree */
2389 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2390 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2391 *predicate - start, start);
2392 }
2393 if (!dest_parent_times) {
2394 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2395 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2396 *predicate - start, start);
2397 goto cleanup;
2398 }
2399 if (path_key_expr == pke_end) {
2400 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2401 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2402 *predicate - start, start);
2403 goto cleanup;
2404 }
2405
2406 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002407 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002408 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002409 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002410 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2411 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002412 goto cleanup;
2413 }
2414
2415 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002416 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002417 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002418 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002419 }
2420 if (!mod) {
2421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002422 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002423 *predicate - start, start, dst_len, dst);
2424 goto cleanup;
2425 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002426 if (!mod->implemented) {
2427 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002428 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci92cc8512019-04-25 09:57:06 +02002429 }
Radek Krejcia3045382018-11-22 14:30:31 +01002430
2431 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2432 if (!dst_node) {
2433 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002434 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002435 *predicate - start, start, path_key_expr - pke_start, pke_start);
2436 goto cleanup;
2437 }
2438 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002439 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 +01002440 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002441 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002442 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2443 goto cleanup;
2444 }
2445 }
2446
2447 ret = LY_SUCCESS;
2448cleanup:
2449 ly_set_erase(&keys, NULL);
2450 return ret;
2451}
2452
2453/**
2454 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2455 *
2456 * path-arg = absolute-path / relative-path
2457 * absolute-path = 1*("/" (node-identifier *path-predicate))
2458 * relative-path = 1*(".." "/") descendant-path
2459 *
2460 * @param[in,out] path Path to parse.
2461 * @param[out] prefix Prefix of the token, NULL if there is not any.
2462 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2463 * @param[out] name Name of the token.
2464 * @param[out] nam_len Length of the name.
2465 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2466 * must not be changed between consecutive calls. -1 if the
2467 * path is absolute.
2468 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2469 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2470 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002471LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002472lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2473 int *parent_times, int *has_predicate)
2474{
2475 int par_times = 0;
2476
2477 assert(path && *path);
2478 assert(parent_times);
2479 assert(prefix);
2480 assert(prefix_len);
2481 assert(name);
2482 assert(name_len);
2483 assert(has_predicate);
2484
2485 *prefix = NULL;
2486 *prefix_len = 0;
2487 *name = NULL;
2488 *name_len = 0;
2489 *has_predicate = 0;
2490
2491 if (!*parent_times) {
2492 if (!strncmp(*path, "..", 2)) {
2493 *path += 2;
2494 ++par_times;
2495 while (!strncmp(*path, "/..", 3)) {
2496 *path += 3;
2497 ++par_times;
2498 }
2499 }
2500 if (par_times) {
2501 *parent_times = par_times;
2502 } else {
2503 *parent_times = -1;
2504 }
2505 }
2506
2507 if (**path != '/') {
2508 return LY_EINVAL;
2509 }
2510 /* skip '/' */
2511 ++(*path);
2512
2513 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002514 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002515
2516 if ((**path == '/' && (*path)[1]) || !**path) {
2517 /* path continues by another token or this is the last token */
2518 return LY_SUCCESS;
2519 } else if ((*path)[0] != '[') {
2520 /* unexpected character */
2521 return LY_EINVAL;
2522 } else {
2523 /* predicate starting with [ */
2524 *has_predicate = 1;
2525 return LY_SUCCESS;
2526 }
2527}
2528
2529/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002530 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2531 *
2532 * The set of features used for target must be a subset of features used for the leafref.
2533 * This is not a perfect, we should compare the truth tables but it could require too much resources
2534 * and RFC 7950 does not require it explicitely, so we simplify that.
2535 *
2536 * @param[in] refnode The leafref node.
2537 * @param[in] target Tha target node of the leafref.
2538 * @return LY_SUCCESS or LY_EVALID;
2539 */
2540static LY_ERR
2541lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2542{
2543 LY_ERR ret = LY_EVALID;
2544 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002545 unsigned int u, v, count;
2546 struct ly_set features = {0};
2547
2548 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002549 if (iter->iffeatures) {
2550 LY_ARRAY_FOR(iter->iffeatures, u) {
2551 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2552 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002553 }
2554 }
2555 }
2556 }
2557
2558 /* we should have, in features set, a superset of features applicable to the target node.
2559 * So when adding features applicable to the target into the features set, we should not be
2560 * able to actually add any new feature, otherwise it is not a subset of features applicable
2561 * to the leafref itself. */
2562 count = features.count;
2563 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002564 if (iter->iffeatures) {
2565 LY_ARRAY_FOR(iter->iffeatures, u) {
2566 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2567 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002568 /* new feature was added (or LY_EMEM) */
2569 goto cleanup;
2570 }
2571 }
2572 }
2573 }
2574 }
2575 ret = LY_SUCCESS;
2576
2577cleanup:
2578 ly_set_erase(&features, NULL);
2579 return ret;
2580}
2581
2582/**
Radek Krejcia3045382018-11-22 14:30:31 +01002583 * @brief Validate the leafref path.
2584 * @param[in] ctx Compile context
2585 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002586 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002587 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2588 */
2589static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002590lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002591{
2592 const struct lysc_node *node = NULL, *parent = NULL;
2593 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002594 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002595 const char *id, *prefix, *name;
2596 size_t prefix_len, name_len;
2597 int parent_times = 0, has_predicate;
2598 unsigned int iter, u;
2599 LY_ERR ret = LY_SUCCESS;
2600
2601 assert(ctx);
2602 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002603 assert(leafref);
2604
Radek Krejci1c0c3442019-07-23 16:08:47 +02002605 ctx->path[0] = '\0';
2606 lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
2607 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002608
Radek Krejcia3045382018-11-22 14:30:31 +01002609 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002610 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002611 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2612 if (!iter) { /* first iteration */
2613 /* precess ".." in relative paths */
2614 if (parent_times > 0) {
2615 /* move from the context node */
2616 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2617 /* path is supposed to be evaluated in data tree, so we have to skip
2618 * all schema nodes that cannot be instantiated in data tree */
2619 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002620 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002621 }
2622 }
2623 }
2624
2625 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002626 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002627 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002628 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002629 }
2630 if (!mod) {
2631 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002632 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2633 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002634 return LY_EVALID;
2635 }
Radek Krejci3d929562019-02-21 11:25:55 +01002636 if (!mod->implemented) {
2637 /* make the module implemented */
Radek Krejci77a8bcd2019-09-11 11:20:02 +02002638 lys_set_implemented_internal((struct lys_module*)mod, 2);
Radek Krejci3d929562019-02-21 11:25:55 +01002639 }
Radek Krejcia3045382018-11-22 14:30:31 +01002640
2641 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2642 if (!node) {
2643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002644 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002645 return LY_EVALID;
2646 }
2647 parent = node;
2648
2649 if (has_predicate) {
2650 /* we have predicate, so the current result must be list */
2651 if (node->nodetype != LYS_LIST) {
2652 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2653 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002654 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002655 return LY_EVALID;
2656 }
2657
Radek Krejcibade82a2018-12-05 10:13:48 +01002658 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2659 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002660 }
2661
2662 ++iter;
2663 }
2664 if (ret) {
2665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002666 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002667 return LY_EVALID;
2668 }
2669
2670 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2672 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002673 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002674 return LY_EVALID;
2675 }
2676
2677 /* check status */
2678 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2679 return LY_EVALID;
2680 }
2681
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002682 /* check config */
2683 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2684 if (node->flags & LYS_CONFIG_R) {
2685 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2686 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2687 leafref->path);
2688 return LY_EVALID;
2689 }
2690 }
2691
Radek Krejci412ddfa2018-11-23 11:44:11 +01002692 /* store the target's type and check for circular chain of leafrefs */
2693 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2694 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2695 if (type == (struct lysc_type*)leafref) {
2696 /* circular chain detected */
2697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2698 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2699 return LY_EVALID;
2700 }
2701 }
2702
Radek Krejci58d171e2018-11-23 13:50:55 +01002703 /* check if leafref and its target are under common if-features */
2704 if (lys_compile_leafref_features_validate(startnode, node)) {
2705 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2706 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2707 leafref->path);
2708 return LY_EVALID;
2709 }
2710
Radek Krejci327de162019-06-14 12:52:07 +02002711 ctx->path_len = 1;
2712 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002713 return LY_SUCCESS;
2714}
2715
Radek Krejcicdfecd92018-11-26 11:27:32 +01002716static 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 +02002717 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002718/**
2719 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2720 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002721 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2722 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2723 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2724 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002725 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002726 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002727 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002728 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2729 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2730 * @param[out] type Newly created type structure with the filled information about the type.
2731 * @return LY_ERR value.
2732 */
Radek Krejci19a96102018-11-15 13:38:09 +01002733static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002734lys_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 +02002735 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002736 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002737{
2738 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002739 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002740 struct lysc_type_bin *bin;
2741 struct lysc_type_num *num;
2742 struct lysc_type_str *str;
2743 struct lysc_type_bits *bits;
2744 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002745 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002746 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002747 struct lysc_type_union *un, *un_aux;
2748 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002749
2750 switch (basetype) {
2751 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002752 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002753
2754 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002755 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002756 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2757 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002758 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002759 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 +01002760 }
2761 }
2762
2763 if (tpdfname) {
2764 type_p->compiled = *type;
2765 *type = calloc(1, sizeof(struct lysc_type_bin));
2766 }
2767 break;
2768 case LY_TYPE_BITS:
2769 /* RFC 7950 9.7 - bits */
2770 bits = (struct lysc_type_bits*)(*type);
2771 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002772 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002773 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2774 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002775 }
2776
Radek Krejci555cb5b2018-11-16 14:54:33 +01002777 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002778 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002779 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002781 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002782 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002783 free(*type);
2784 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002785 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002786 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002787 }
2788
2789 if (tpdfname) {
2790 type_p->compiled = *type;
2791 *type = calloc(1, sizeof(struct lysc_type_bits));
2792 }
2793 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002794 case LY_TYPE_DEC64:
2795 dec = (struct lysc_type_dec*)(*type);
2796
2797 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002798 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002799 if (!type_p->fraction_digits) {
2800 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002802 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002803 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002804 free(*type);
2805 *type = NULL;
2806 }
2807 return LY_EVALID;
2808 }
2809 } else if (type_p->fraction_digits) {
2810 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002811 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002812 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2813 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002814 tpdfname);
2815 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002816 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2817 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002818 free(*type);
2819 *type = NULL;
2820 }
2821 return LY_EVALID;
2822 }
2823 dec->fraction_digits = type_p->fraction_digits;
2824
2825 /* RFC 7950 9.2.4 - range */
2826 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002827 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2828 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002829 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002830 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 +01002831 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002832 }
2833
2834 if (tpdfname) {
2835 type_p->compiled = *type;
2836 *type = calloc(1, sizeof(struct lysc_type_dec));
2837 }
2838 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002839 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002840 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002841
2842 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002843 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002844 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2845 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002846 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002847 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 +01002848 }
2849 } else if (base && ((struct lysc_type_str*)base)->length) {
2850 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2851 }
2852
2853 /* RFC 7950 9.4.5 - pattern */
2854 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002855 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002856 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002857 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2858 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2859 }
2860
2861 if (tpdfname) {
2862 type_p->compiled = *type;
2863 *type = calloc(1, sizeof(struct lysc_type_str));
2864 }
2865 break;
2866 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002867 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002868
2869 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002870 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002871 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002872 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002873 }
2874
Radek Krejci555cb5b2018-11-16 14:54:33 +01002875 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002876 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002877 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002878 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002879 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002880 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002881 free(*type);
2882 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002883 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002884 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002885 }
2886
2887 if (tpdfname) {
2888 type_p->compiled = *type;
2889 *type = calloc(1, sizeof(struct lysc_type_enum));
2890 }
2891 break;
2892 case LY_TYPE_INT8:
2893 case LY_TYPE_UINT8:
2894 case LY_TYPE_INT16:
2895 case LY_TYPE_UINT16:
2896 case LY_TYPE_INT32:
2897 case LY_TYPE_UINT32:
2898 case LY_TYPE_INT64:
2899 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002900 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002901
2902 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002903 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002904 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2905 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002906 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002907 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 +01002908 }
2909 }
2910
2911 if (tpdfname) {
2912 type_p->compiled = *type;
2913 *type = calloc(1, sizeof(struct lysc_type_num));
2914 }
2915 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002916 case LY_TYPE_IDENT:
2917 idref = (struct lysc_type_identityref*)(*type);
2918
2919 /* RFC 7950 9.10.2 - base */
2920 if (type_p->bases) {
2921 if (base) {
2922 /* only the directly derived identityrefs can contain base specification */
2923 if (tpdfname) {
2924 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002925 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002926 tpdfname);
2927 } else {
2928 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002929 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002930 free(*type);
2931 *type = NULL;
2932 }
2933 return LY_EVALID;
2934 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002935 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002936 }
2937
2938 if (!base && !type_p->flags) {
2939 /* type derived from identityref built-in type must contain at least one base */
2940 if (tpdfname) {
2941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2942 } else {
2943 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2944 free(*type);
2945 *type = NULL;
2946 }
2947 return LY_EVALID;
2948 }
2949
2950 if (tpdfname) {
2951 type_p->compiled = *type;
2952 *type = calloc(1, sizeof(struct lysc_type_identityref));
2953 }
2954 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002955 case LY_TYPE_LEAFREF:
2956 /* RFC 7950 9.9.3 - require-instance */
2957 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002958 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002959 if (tpdfname) {
2960 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2961 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2962 } else {
2963 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2964 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2965 free(*type);
2966 *type = NULL;
2967 }
2968 return LY_EVALID;
2969 }
Radek Krejcia3045382018-11-22 14:30:31 +01002970 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002971 } else if (base) {
2972 /* inherit */
2973 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002974 } else {
2975 /* default is true */
2976 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2977 }
2978 if (type_p->path) {
2979 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002980 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002981 } else if (base) {
2982 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002983 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002984 } else if (tpdfname) {
2985 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2986 return LY_EVALID;
2987 } else {
2988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2989 free(*type);
2990 *type = NULL;
2991 return LY_EVALID;
2992 }
2993 if (tpdfname) {
2994 type_p->compiled = *type;
2995 *type = calloc(1, sizeof(struct lysc_type_leafref));
2996 }
2997 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002998 case LY_TYPE_INST:
2999 /* RFC 7950 9.9.3 - require-instance */
3000 if (type_p->flags & LYS_SET_REQINST) {
3001 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
3002 } else {
3003 /* default is true */
3004 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
3005 }
3006
3007 if (tpdfname) {
3008 type_p->compiled = *type;
3009 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3010 }
3011 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003012 case LY_TYPE_UNION:
3013 un = (struct lysc_type_union*)(*type);
3014
3015 /* RFC 7950 7.4 - type */
3016 if (type_p->types) {
3017 if (base) {
3018 /* only the directly derived union can contain types specification */
3019 if (tpdfname) {
3020 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3021 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
3022 tpdfname);
3023 } else {
3024 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3025 "Invalid type substatement for the type not directly derived from union built-in type.");
3026 free(*type);
3027 *type = NULL;
3028 }
3029 return LY_EVALID;
3030 }
3031 /* compile the type */
3032 additional = 0;
3033 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
3034 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02003035 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 +01003036 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
3037 /* add space for additional types from the union subtype */
3038 un_aux = (struct lysc_type_union *)un->types[u + additional];
3039 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)));
3040 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3041 un->types = (void*)((uint32_t*)(p) + 1);
3042
3043 /* copy subtypes of the subtype union */
3044 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
3045 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
3046 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
3047 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
3048 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3049 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
3050 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
3051 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3052 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3053 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3054 /* TODO extensions */
3055
3056 } else {
3057 un->types[u + additional] = un_aux->types[v];
3058 ++un_aux->types[v]->refcount;
3059 }
3060 ++additional;
3061 LY_ARRAY_INCREMENT(un->types);
3062 }
3063 /* compensate u increment in main loop */
3064 --additional;
3065
3066 /* free the replaced union subtype */
3067 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3068 } else {
3069 LY_ARRAY_INCREMENT(un->types);
3070 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003071 }
3072 }
3073
3074 if (!base && !type_p->flags) {
3075 /* type derived from union built-in type must contain at least one type */
3076 if (tpdfname) {
3077 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3078 } else {
3079 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3080 free(*type);
3081 *type = NULL;
3082 }
3083 return LY_EVALID;
3084 }
3085
3086 if (tpdfname) {
3087 type_p->compiled = *type;
3088 *type = calloc(1, sizeof(struct lysc_type_union));
3089 }
3090 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003091 case LY_TYPE_BOOL:
3092 case LY_TYPE_EMPTY:
3093 case LY_TYPE_UNKNOWN: /* just to complete switch */
3094 break;
3095 }
3096 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3097done:
3098 return ret;
3099}
3100
Radek Krejcia3045382018-11-22 14:30:31 +01003101/**
3102 * @brief Compile information about the leaf/leaf-list's type.
3103 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003104 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3105 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3106 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3107 * @param[in] context_name Name of the context node or referencing typedef for logging.
3108 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003109 * @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 +01003110 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003111 * @return LY_ERR value.
3112 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003113static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003114lys_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 +02003115 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003116{
3117 LY_ERR ret = LY_SUCCESS;
3118 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003119 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003120 struct type_context {
3121 const struct lysp_tpdf *tpdf;
3122 struct lysp_node *node;
3123 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003124 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003125 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003126 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003127 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003128 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003129 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003130
3131 (*type) = NULL;
3132
3133 tctx = calloc(1, sizeof *tctx);
3134 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003135 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003136 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3137 ret == LY_SUCCESS;
3138 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3139 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3140 if (basetype) {
3141 break;
3142 }
3143
3144 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003145 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3146 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003147 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3148
Radek Krejcicdfecd92018-11-26 11:27:32 +01003149 if (units && !*units) {
3150 /* inherit units */
3151 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3152 }
Radek Krejci01342af2019-01-03 15:18:08 +01003153 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003154 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003155 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003156 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003157 }
Radek Krejci01342af2019-01-03 15:18:08 +01003158 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003159 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3160 break;
3161 }
3162
Radek Krejci19a96102018-11-15 13:38:09 +01003163 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003164 /* it is not necessary to continue, the rest of the chain was already compiled,
3165 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003166 basetype = tctx->tpdf->type.compiled->basetype;
3167 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003168 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003169 dummyloops = 1;
3170 goto preparenext;
3171 } else {
3172 tctx = NULL;
3173 break;
3174 }
Radek Krejci19a96102018-11-15 13:38:09 +01003175 }
3176
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003177 /* circular typedef reference detection */
3178 for (u = 0; u < tpdf_chain.count; u++) {
3179 /* local part */
3180 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3181 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3182 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3183 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3184 free(tctx);
3185 ret = LY_EVALID;
3186 goto cleanup;
3187 }
3188 }
3189 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3190 /* global part for unions corner case */
3191 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3192 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3193 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3194 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3195 free(tctx);
3196 ret = LY_EVALID;
3197 goto cleanup;
3198 }
3199 }
3200
Radek Krejci19a96102018-11-15 13:38:09 +01003201 /* store information for the following processing */
3202 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3203
Radek Krejcicdfecd92018-11-26 11:27:32 +01003204preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003205 /* prepare next loop */
3206 tctx_prev = tctx;
3207 tctx = calloc(1, sizeof *tctx);
3208 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3209 }
3210 free(tctx);
3211
3212 /* allocate type according to the basetype */
3213 switch (basetype) {
3214 case LY_TYPE_BINARY:
3215 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003216 break;
3217 case LY_TYPE_BITS:
3218 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003219 break;
3220 case LY_TYPE_BOOL:
3221 case LY_TYPE_EMPTY:
3222 *type = calloc(1, sizeof(struct lysc_type));
3223 break;
3224 case LY_TYPE_DEC64:
3225 *type = calloc(1, sizeof(struct lysc_type_dec));
3226 break;
3227 case LY_TYPE_ENUM:
3228 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003229 break;
3230 case LY_TYPE_IDENT:
3231 *type = calloc(1, sizeof(struct lysc_type_identityref));
3232 break;
3233 case LY_TYPE_INST:
3234 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3235 break;
3236 case LY_TYPE_LEAFREF:
3237 *type = calloc(1, sizeof(struct lysc_type_leafref));
3238 break;
3239 case LY_TYPE_STRING:
3240 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003241 break;
3242 case LY_TYPE_UNION:
3243 *type = calloc(1, sizeof(struct lysc_type_union));
3244 break;
3245 case LY_TYPE_INT8:
3246 case LY_TYPE_UINT8:
3247 case LY_TYPE_INT16:
3248 case LY_TYPE_UINT16:
3249 case LY_TYPE_INT32:
3250 case LY_TYPE_UINT32:
3251 case LY_TYPE_INT64:
3252 case LY_TYPE_UINT64:
3253 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003254 break;
3255 case LY_TYPE_UNKNOWN:
3256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3257 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3258 ret = LY_EVALID;
3259 goto cleanup;
3260 }
3261 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003262 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003263 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3264 ly_data_type2str[basetype]);
3265 free(*type);
3266 (*type) = NULL;
3267 ret = LY_EVALID;
3268 goto cleanup;
3269 }
3270
3271 /* get restrictions from the referred typedefs */
3272 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3273 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003274
3275 /* remember the typedef context for circular check */
3276 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3277
Radek Krejci43699232018-11-23 14:59:46 +01003278 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003279 base = tctx->tpdf->type.compiled;
3280 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003281 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003282 /* no change, just use the type information from the base */
3283 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3284 ++base->refcount;
3285 continue;
3286 }
3287
3288 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003289 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3290 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3291 tctx->tpdf->name, ly_data_type2str[basetype]);
3292 ret = LY_EVALID;
3293 goto cleanup;
3294 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3296 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3297 tctx->tpdf->name, tctx->tpdf->dflt);
3298 ret = LY_EVALID;
3299 goto cleanup;
3300 }
3301
Radek Krejci19a96102018-11-15 13:38:09 +01003302 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003303 /* TODO user type plugins */
3304 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003305 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003306 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 +01003307 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003308 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003309 LY_CHECK_GOTO(ret, cleanup);
3310 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003311 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003312 /* remove the processed typedef contexts from the stack for circular check */
3313 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003314
Radek Krejcic5c27e52018-11-15 14:38:11 +01003315 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003316 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003317 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003318 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003319 /* TODO user type plugins */
3320 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003321 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003322 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 +01003323 LY_CHECK_GOTO(ret, cleanup);
3324 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003325 /* no specific restriction in leaf's type definition, copy from the base */
3326 free(*type);
3327 (*type) = base;
3328 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003329 }
Radek Krejcia1911222019-07-22 17:24:50 +02003330 if (dflt && !(*type)->dflt) {
3331 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003332 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003333 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3334 (*type)->dflt->realtype = (*type);
3335 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3336 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003337 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003338 if (err) {
3339 ly_err_print(err);
3340 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3341 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3342 ly_err_free(err);
3343 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003344 if (ret == LY_EINCOMPLETE) {
3345 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003346 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003347
3348 /* but in general result is so far ok */
3349 ret = LY_SUCCESS;
3350 }
Radek Krejcia1911222019-07-22 17:24:50 +02003351 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003352 }
Radek Krejci19a96102018-11-15 13:38:09 +01003353
Radek Krejci0935f412019-08-20 16:15:18 +02003354 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003355
3356cleanup:
3357 ly_set_erase(&tpdf_chain, free);
3358 return ret;
3359}
3360
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003361/**
3362 * @brief Compile status information of the given node.
3363 *
3364 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3365 * has the status correctly set during the compilation.
3366 *
3367 * @param[in] ctx Compile context
3368 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3369 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3370 * the compatibility with the parent's status value.
3371 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3372 * @return LY_ERR value.
3373 */
3374static LY_ERR
3375lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3376{
3377 /* status - it is not inherited by specification, but it does not make sense to have
3378 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3379 if (!((*node_flags) & LYS_STATUS_MASK)) {
3380 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3381 if ((parent_flags & 0x3) != 0x3) {
3382 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3383 * combination of bits (0x3) which marks the uses_status value */
3384 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3385 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3386 }
3387 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3388 } else {
3389 (*node_flags) |= LYS_STATUS_CURR;
3390 }
3391 } else if (parent_flags & LYS_STATUS_MASK) {
3392 /* check status compatibility with the parent */
3393 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3394 if ((*node_flags) & LYS_STATUS_CURR) {
3395 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3396 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3397 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3398 } else { /* LYS_STATUS_DEPRC */
3399 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3400 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3401 }
3402 return LY_EVALID;
3403 }
3404 }
3405 return LY_SUCCESS;
3406}
3407
Radek Krejci8cce8532019-03-05 11:27:45 +01003408/**
3409 * @brief Check uniqness of the node/action/notification name.
3410 *
3411 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3412 * structures, but they share the namespace so we need to check their name collisions.
3413 *
3414 * @param[in] ctx Compile context.
3415 * @param[in] children List (linked list) of data nodes to go through.
3416 * @param[in] actions List (sized array) of actions or RPCs to go through.
3417 * @param[in] notifs List (sized array) of Notifications to go through.
3418 * @param[in] name Name of the item to find in the given lists.
3419 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3420 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3421 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3422 */
3423static LY_ERR
3424lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3425 const struct lysc_action *actions, const struct lysc_notif *notifs,
3426 const char *name, void *exclude)
3427{
3428 const struct lysc_node *iter;
3429 unsigned int u;
3430
3431 LY_LIST_FOR(children, iter) {
3432 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3433 goto error;
3434 }
3435 }
3436 LY_ARRAY_FOR(actions, u) {
3437 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3438 goto error;
3439 }
3440 }
3441 LY_ARRAY_FOR(notifs, u) {
3442 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3443 goto error;
3444 }
3445 }
3446 return LY_SUCCESS;
3447error:
3448 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3449 return LY_EEXIST;
3450}
3451
Radek Krejcia0f704a2019-09-09 16:12:23 +02003452/**
3453 * @brief Get the XPath context node for the given schema node.
3454 * @param[in] start The schema node where the XPath expression appears.
3455 * @return The context node to evaluate XPath expression in given schema node.
3456 * @return NULL in case the context node is the root node.
3457 */
3458static struct lysc_node *
3459lysc_xpath_context(struct lysc_node *start)
3460{
3461 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
3462 start = start->parent);
3463 return start;
3464}
3465
Radek Krejciec4da802019-05-02 13:02:41 +02003466static 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 +01003467
Radek Krejcia3045382018-11-22 14:30:31 +01003468/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003469 * @brief Compile parsed RPC/action schema node information.
3470 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003471 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003472 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003473 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3474 * @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).
3475 * Zero means no uses, non-zero value with no status bit set mean the default status.
3476 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3477 */
3478static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003479lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003480 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3481{
3482 LY_ERR ret = LY_SUCCESS;
3483 struct lysp_node *child_p;
3484 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003485 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003486
Radek Krejci327de162019-06-14 12:52:07 +02003487 lysc_update_path(ctx, parent, action_p->name);
3488
Radek Krejci8cce8532019-03-05 11:27:45 +01003489 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3490 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3491 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3492 action_p->name, action)) {
3493 return LY_EVALID;
3494 }
3495
Radek Krejciec4da802019-05-02 13:02:41 +02003496 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003497 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003498 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003499 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003500 return LY_EVALID;
3501 }
3502
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003503 action->nodetype = LYS_ACTION;
3504 action->module = ctx->mod;
3505 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003506 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003507 action->sp = action_p;
3508 }
3509 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3510
3511 /* status - it is not inherited by specification, but it does not make sense to have
3512 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3513 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3514
3515 DUP_STRING(ctx->ctx, action_p->name, action->name);
3516 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3517 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003518 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003519 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003520
3521 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003522 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003523 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003524 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 +02003525 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003526 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003527 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003528 }
Radek Krejci327de162019-06-14 12:52:07 +02003529 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003530 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003531
3532 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003533 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003534 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003535 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 +02003536 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003537 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003538 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003539 }
Radek Krejci327de162019-06-14 12:52:07 +02003540 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003541
Radek Krejci327de162019-06-14 12:52:07 +02003542 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003543cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003544 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003545 return ret;
3546}
3547
3548/**
Radek Krejci43981a32019-04-12 09:44:11 +02003549 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003550 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003551 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003552 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3553 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3554 * @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 +02003555 * Zero means no uses, non-zero value with no status bit set mean the default status.
3556 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3557 */
3558static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003559lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003560 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3561{
3562 LY_ERR ret = LY_SUCCESS;
3563 struct lysp_node *child_p;
3564 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003565 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003566
Radek Krejci327de162019-06-14 12:52:07 +02003567 lysc_update_path(ctx, parent, notif_p->name);
3568
Radek Krejcifc11bd72019-04-11 16:00:05 +02003569 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3570 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3571 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3572 notif_p->name, notif)) {
3573 return LY_EVALID;
3574 }
3575
Radek Krejciec4da802019-05-02 13:02:41 +02003576 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003577 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3578 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003579 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003580 return LY_EVALID;
3581 }
3582
3583 notif->nodetype = LYS_NOTIF;
3584 notif->module = ctx->mod;
3585 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003586 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003587 notif->sp = notif_p;
3588 }
3589 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3590
3591 /* status - it is not inherited by specification, but it does not make sense to have
3592 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3593 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3594
3595 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3596 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3597 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003598 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003599 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003600 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003601
Radek Krejciec4da802019-05-02 13:02:41 +02003602 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003603 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003604 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003605 }
3606
Radek Krejci327de162019-06-14 12:52:07 +02003607 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003608cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003609 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003610 return ret;
3611}
3612
3613/**
Radek Krejcia3045382018-11-22 14:30:31 +01003614 * @brief Compile parsed container node information.
3615 * @param[in] ctx Compile context
3616 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003617 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3618 * is enriched with the container-specific information.
3619 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3620 */
Radek Krejci19a96102018-11-15 13:38:09 +01003621static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003622lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003623{
3624 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3625 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3626 struct lysp_node *child_p;
3627 unsigned int u;
3628 LY_ERR ret = LY_SUCCESS;
3629
Radek Krejcife909632019-02-12 15:34:42 +01003630 if (cont_p->presence) {
3631 cont->flags |= LYS_PRESENCE;
3632 }
3633
Radek Krejci19a96102018-11-15 13:38:09 +01003634 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003635 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003636 }
3637
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003638 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02003639 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3640 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003641
3642done:
3643 return ret;
3644}
3645
Radek Krejci33f72892019-02-21 10:36:58 +01003646/*
3647 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3648 * @param[in] ctx Compile context.
3649 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3650 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003651 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3652 * @return LY_ERR value.
3653 */
3654static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003655lys_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 +01003656{
Radek Krejcia1911222019-07-22 17:24:50 +02003657 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003658 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3659
Radek Krejciec4da802019-05-02 13:02:41 +02003660 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 +01003661 leaf->units ? NULL : &leaf->units));
3662 if (leaf->nodetype == LYS_LEAFLIST) {
3663 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003664 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3665 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003666 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003667 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3668 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3669 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3670 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003671 LY_ARRAY_INCREMENT(llist->dflts);
3672 }
3673 } else {
3674 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003675 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003676 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3677 leaf->dflt->realtype = leaf->type->dflt->realtype;
3678 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3679 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003680 }
3681 }
3682 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3683 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3684 ly_set_add(&ctx->unres, leaf, 0);
3685 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3686 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3687 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3688 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3689 ly_set_add(&ctx->unres, leaf, 0);
3690 }
3691 }
3692 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003693 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3695 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3696 return LY_EVALID;
3697 }
3698 }
3699
Radek Krejci33f72892019-02-21 10:36:58 +01003700 return LY_SUCCESS;
3701}
3702
Radek Krejcia3045382018-11-22 14:30:31 +01003703/**
3704 * @brief Compile parsed leaf node information.
3705 * @param[in] ctx Compile context
3706 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003707 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3708 * is enriched with the leaf-specific information.
3709 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3710 */
Radek Krejci19a96102018-11-15 13:38:09 +01003711static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003712lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003713{
3714 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3715 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3716 unsigned int u;
3717 LY_ERR ret = LY_SUCCESS;
3718
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003719 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003720 if (leaf_p->units) {
3721 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3722 leaf->flags |= LYS_SET_UNITS;
3723 }
Radek Krejcia1911222019-07-22 17:24:50 +02003724
3725 /* the dflt member is just filled to avoid getting the default value from the type */
3726 leaf->dflt = (void*)leaf_p->dflt;
3727 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003728 if (ret) {
3729 leaf->dflt = NULL;
3730 return ret;
3731 }
Radek Krejcia1911222019-07-22 17:24:50 +02003732
Radek Krejciccd20f12019-02-15 14:12:27 +01003733 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003734 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003735 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003736 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3737 leaf->dflt->realtype = leaf->type;
3738 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3739 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003740 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003741 leaf->dflt->realtype->refcount++;
3742 if (err) {
3743 ly_err_print(err);
3744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3745 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3746 ly_err_free(err);
3747 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003748 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003749 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003750 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003751
3752 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003753 ret = LY_SUCCESS;
3754 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003755 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003756 leaf->flags |= LYS_SET_DFLT;
3757 }
Radek Krejci43699232018-11-23 14:59:46 +01003758
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003759
Radek Krejci19a96102018-11-15 13:38:09 +01003760done:
3761 return ret;
3762}
3763
Radek Krejcia3045382018-11-22 14:30:31 +01003764/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003765 * @brief Compile parsed leaf-list node information.
3766 * @param[in] ctx Compile context
3767 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003768 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3769 * is enriched with the leaf-list-specific information.
3770 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3771 */
3772static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003773lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003774{
3775 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3776 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003777 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003778 LY_ERR ret = LY_SUCCESS;
3779
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003780 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003781 if (llist_p->units) {
3782 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3783 llist->flags |= LYS_SET_UNITS;
3784 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003785
Radek Krejcia1911222019-07-22 17:24:50 +02003786 /* the dflts member is just filled to avoid getting the default value from the type */
3787 llist->dflts = (void*)llist_p->dflts;
3788 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003789 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003790 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003791 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003792 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3793 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003794 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003795 LY_ARRAY_INCREMENT(llist->dflts_mods);
3796 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003797 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003798 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3799 llist->dflts[u]->realtype = llist->type;
3800 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3801 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003802 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003803 llist->dflts[u]->realtype->refcount++;
3804 if (err) {
3805 ly_err_print(err);
3806 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3807 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3808 ly_err_free(err);
3809 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003810 if (ret == LY_EINCOMPLETE) {
3811 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003812 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003813
3814 /* but in general result is so far ok */
3815 ret = LY_SUCCESS;
3816 }
Radek Krejcia1911222019-07-22 17:24:50 +02003817 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003818 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003819 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003820 }
Radek Krejcia1911222019-07-22 17:24:50 +02003821 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3822 /* configuration data values must be unique - so check the default values */
3823 LY_ARRAY_FOR(llist->dflts, u) {
3824 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3825 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3826 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003827 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 +02003828 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3829 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3830 if (dynamic) {
3831 free((char*)val);
3832 }
3833 return LY_EVALID;
3834 }
3835 }
3836 }
3837 }
3838
3839 /* 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 +01003840
3841 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003842 if (llist->min) {
3843 llist->flags |= LYS_MAND_TRUE;
3844 }
Radek Krejcib7408632018-11-28 17:12:11 +01003845 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003846
Radek Krejci0e5d8382018-11-28 16:37:53 +01003847done:
3848 return ret;
3849}
3850
3851/**
Radek Krejci7af64242019-02-18 13:07:53 +01003852 * @brief Compile information about list's uniques.
3853 * @param[in] ctx Compile context.
3854 * @param[in] context_module Module where the prefixes are going to be resolved.
3855 * @param[in] uniques Sized array list of unique statements.
3856 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3857 * @return LY_ERR value.
3858 */
3859static LY_ERR
3860lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3861{
3862 LY_ERR ret = LY_SUCCESS;
3863 struct lysc_node_leaf **key, ***unique;
3864 const char *keystr, *delim;
3865 size_t len;
3866 unsigned int v;
3867 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003868 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003869
3870 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3871 config = -1;
3872 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3873 keystr = uniques[v];
3874 while (keystr) {
3875 delim = strpbrk(keystr, " \t\n");
3876 if (delim) {
3877 len = delim - keystr;
3878 while (isspace(*delim)) {
3879 ++delim;
3880 }
3881 } else {
3882 len = strlen(keystr);
3883 }
3884
3885 /* unique node must be present */
3886 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003887 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3888 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003889 if (ret != LY_SUCCESS) {
3890 if (ret == LY_EDENIED) {
3891 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003892 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003893 len, keystr, lys_nodetype2str((*key)->nodetype));
3894 }
3895 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003896 } else if (flags) {
3897 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3898 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3899 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3900 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003901 }
3902
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003903
Radek Krejci7af64242019-02-18 13:07:53 +01003904 /* all referenced leafs must be of the same config type */
3905 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3906 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3907 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3908 return LY_EVALID;
3909 } else if ((*key)->flags & LYS_CONFIG_W) {
3910 config = 1;
3911 } else { /* LYS_CONFIG_R */
3912 config = 0;
3913 }
3914
3915 /* check status */
3916 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3917 (*key)->flags, (*key)->module, (*key)->name));
3918
3919 /* mark leaf as unique */
3920 (*key)->flags |= LYS_UNIQUE;
3921
3922 /* next unique value in line */
3923 keystr = delim;
3924 }
3925 /* next unique definition */
3926 }
3927
3928 return LY_SUCCESS;
3929}
3930
3931/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003932 * @brief Compile parsed list node information.
3933 * @param[in] ctx Compile context
3934 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003935 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3936 * is enriched with the list-specific information.
3937 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3938 */
3939static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003940lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003941{
3942 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3943 struct lysc_node_list *list = (struct lysc_node_list*)node;
3944 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003945 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003946 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003947 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003948 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003949 LY_ERR ret = LY_SUCCESS;
3950
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003951 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003952 if (list->min) {
3953 list->flags |= LYS_MAND_TRUE;
3954 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003955 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3956
3957 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003958 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003959 }
3960
Radek Krejcic71ac5b2019-09-10 15:34:22 +02003961 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003962
3963 /* keys */
3964 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3965 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3966 return LY_EVALID;
3967 }
3968
3969 /* find all the keys (must be direct children) */
3970 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003971 if (!keystr) {
3972 /* keyless list */
3973 list->flags |= LYS_KEYLESS;
3974 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003975 while (keystr) {
3976 delim = strpbrk(keystr, " \t\n");
3977 if (delim) {
3978 len = delim - keystr;
3979 while (isspace(*delim)) {
3980 ++delim;
3981 }
3982 } else {
3983 len = strlen(keystr);
3984 }
3985
3986 /* key node must be present */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003987 key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3988 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003989 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3990 "The list's key \"%.*s\" not found.", len, keystr);
3991 return LY_EVALID;
3992 }
3993 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003994 if (key->flags & LYS_KEY) {
3995 /* the node was already marked as a key */
3996 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3997 "Duplicated key identifier \"%.*s\".", len, keystr);
3998 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003999 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004000
4001 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004002 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004003 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004004 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
4005 return LY_EVALID;
4006 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004007 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004008 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004009 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004010 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004011 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004012 return LY_EVALID;
4013 }
4014 } else {
4015 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004016 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004017 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004018 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004019 return LY_EVALID;
4020 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004021 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004022 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004023 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004024 return LY_EVALID;
4025 }
4026 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004027
4028 /* check status */
4029 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02004030 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01004031
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004032 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004033 if (key->dflt) {
4034 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
4035 lysc_type_free(ctx->ctx, key->dflt->realtype);
4036 free(key->dflt);
4037 key->dflt = NULL;
4038 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004039 }
4040 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004041 key->flags |= LYS_KEY;
4042
4043 /* move it to the correct position */
4044 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
4045 /* fix links in closest previous siblings of the key */
4046 if (key->next) {
4047 key->next->prev = key->prev;
4048 } else {
4049 /* last child */
4050 list->child->prev = key->prev;
4051 }
4052 if (key->prev->next) {
4053 key->prev->next = key->next;
4054 }
4055 /* fix links in the key */
4056 if (prev_key) {
4057 key->prev = (struct lysc_node*)prev_key;
4058 key->next = prev_key->next;
4059 } else {
4060 key->prev = list->child->prev;
4061 key->next = list->child;
4062 }
4063 /* fix links in closes future siblings of the key */
4064 if (prev_key) {
4065 if (prev_key->next) {
4066 prev_key->next->prev = (struct lysc_node*)key;
4067 } else {
4068 list->child->prev = (struct lysc_node*)key;
4069 }
4070 prev_key->next = (struct lysc_node*)key;
4071 } else {
4072 list->child->prev = (struct lysc_node*)key;
4073 }
4074 /* fix links in parent */
4075 if (!key->prev->next) {
4076 list->child = (struct lysc_node*)key;
4077 }
4078 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004079
4080 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004081 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004082 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004083 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004084 }
4085
4086 /* uniques */
4087 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004088 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004089 }
4090
Radek Krejciec4da802019-05-02 13:02:41 +02004091 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4092 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004093
4094done:
4095 return ret;
4096}
4097
Radek Krejcib56c7502019-02-13 14:19:54 +01004098/**
4099 * @brief Do some checks and set the default choice's case.
4100 *
4101 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4102 *
4103 * @param[in] ctx Compile context.
4104 * @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,
4105 * not the reference to the imported module.
4106 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4107 * @return LY_ERR value.
4108 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004109static LY_ERR
4110lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4111{
4112 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4113 const char *prefix = NULL, *name;
4114 size_t prefix_len = 0;
4115
4116 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4117 name = strchr(dflt, ':');
4118 if (name) {
4119 prefix = dflt;
4120 prefix_len = name - prefix;
4121 ++name;
4122 } else {
4123 name = dflt;
4124 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004125 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004126 /* prefixed default case make sense only for the prefix of the schema itself */
4127 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4128 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4129 prefix_len, prefix);
4130 return LY_EVALID;
4131 }
4132 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4133 if (!ch->dflt) {
4134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4135 "Default case \"%s\" not found.", dflt);
4136 return LY_EVALID;
4137 }
4138 /* no mandatory nodes directly under the default case */
4139 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004140 if (iter->parent != (struct lysc_node*)ch->dflt) {
4141 break;
4142 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004143 if (iter->flags & LYS_MAND_TRUE) {
4144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4145 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4146 return LY_EVALID;
4147 }
4148 }
Radek Krejci01342af2019-01-03 15:18:08 +01004149 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004150 return LY_SUCCESS;
4151}
4152
Radek Krejciccd20f12019-02-15 14:12:27 +01004153static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004154lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004155{
4156 struct lys_module *mod;
4157 const char *prefix = NULL, *name;
4158 size_t prefix_len = 0;
4159 struct lysc_node_case *cs;
4160 struct lysc_node *node;
4161
4162 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4163 name = strchr(dflt, ':');
4164 if (name) {
4165 prefix = dflt;
4166 prefix_len = name - prefix;
4167 ++name;
4168 } else {
4169 name = dflt;
4170 }
4171 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4172 if (prefix) {
4173 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4174 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004175 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4176 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004177 return LY_EVALID;
4178 }
4179 } else {
4180 mod = ctx->mod;
4181 }
4182 /* get the default case */
4183 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4184 if (!cs) {
4185 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004186 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004187 return LY_EVALID;
4188 }
4189
4190 /* check that there is no mandatory node */
4191 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004192 if (node->parent != (struct lysc_node*)cs) {
4193 break;
4194 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004195 if (node->flags & LYS_MAND_TRUE) {
4196 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004197 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4198 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004199 return LY_EVALID;
4200 }
4201 }
4202
4203 /* set the default case in choice */
4204 ch->dflt = cs;
4205 cs->flags |= LYS_SET_DFLT;
4206
4207 return LY_SUCCESS;
4208}
4209
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004210/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004211 * @brief Compile parsed choice node information.
4212 * @param[in] ctx Compile context
4213 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004214 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004215 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004216 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4217 */
4218static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004219lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004220{
4221 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4222 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4223 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004224 LY_ERR ret = LY_SUCCESS;
4225
Radek Krejci056d0a82018-12-06 16:57:25 +01004226 LY_LIST_FOR(ch_p->child, child_p) {
4227 if (child_p->nodetype == LYS_CASE) {
4228 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004229 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004230 }
4231 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004232 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004233 }
4234 }
4235
4236 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004237 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004238 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004239 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004240
Radek Krejci9800fb82018-12-13 14:26:23 +01004241 return ret;
4242}
4243
4244/**
4245 * @brief Compile parsed anydata or anyxml node information.
4246 * @param[in] ctx Compile context
4247 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004248 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4249 * is enriched with the any-specific information.
4250 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4251 */
4252static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004253lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004254{
4255 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4256 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4257 unsigned int u;
4258 LY_ERR ret = LY_SUCCESS;
4259
Radek Krejcic71ac5b2019-09-10 15:34:22 +02004260 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004261
4262 if (any->flags & LYS_CONFIG_W) {
4263 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004264 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004265 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004266done:
4267 return ret;
4268}
4269
Radek Krejcib56c7502019-02-13 14:19:54 +01004270/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004271 * @brief Connect the node into the siblings list and check its name uniqueness.
4272 *
4273 * @param[in] ctx Compile context
4274 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4275 * the choice itself is expected instead of a specific case node.
4276 * @param[in] node Schema node to connect into the list.
4277 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4278 */
4279static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004280lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004281{
4282 struct lysc_node **children;
4283
4284 if (node->nodetype == LYS_CASE) {
4285 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4286 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004287 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004288 }
4289 if (children) {
4290 if (!(*children)) {
4291 /* first child */
4292 *children = node;
4293 } else if (*children != node) {
4294 /* by the condition in previous branch we cover the choice/case children
4295 * - the children list is shared by the choice and the the first case, in addition
4296 * the first child of each case must be referenced from the case node. So the node is
4297 * actually always already inserted in case it is the first children - so here such
4298 * a situation actually corresponds to the first branch */
4299 /* insert at the end of the parent's children list */
4300 (*children)->prev->next = node;
4301 node->prev = (*children)->prev;
4302 (*children)->prev = node;
4303
4304 /* check the name uniqueness */
4305 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4306 lysc_node_notifs(parent), node->name, node)) {
4307 return LY_EEXIST;
4308 }
4309 }
4310 }
4311 return LY_SUCCESS;
4312}
4313
Radek Krejci95710c92019-02-11 15:49:55 +01004314/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004315 * @brief Prepare the case structure in choice node for the new data node.
4316 *
4317 * 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
4318 * created in the choice when the first child was processed.
4319 *
4320 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004321 * @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,
4322 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004323 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4324 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4325 * @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,
4326 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004327 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004328static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004329lys_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 +01004330{
4331 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004332 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004333 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004334 unsigned int u;
4335 LY_ERR ret;
4336
Radek Krejci95710c92019-02-11 15:49:55 +01004337#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004338 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004339 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004340 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4341 return NULL; \
4342 } \
4343 }
4344
Radek Krejci95710c92019-02-11 15:49:55 +01004345 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4346 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004347
4348 /* we have to add an implicit case node into the parent choice */
4349 cs = calloc(1, sizeof(struct lysc_node_case));
4350 DUP_STRING(ctx->ctx, child->name, cs->name);
4351 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004352 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004353 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4354 /* the case is already present since the child is not its first children */
4355 return (struct lysc_node_case*)ch->cases->prev;
4356 }
Radek Krejci95710c92019-02-11 15:49:55 +01004357 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004358
4359 /* explicit parent case is not present (this is its first child) */
4360 cs = calloc(1, sizeof(struct lysc_node_case));
4361 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4362 cs->flags = LYS_STATUS_MASK & node_p->flags;
4363 cs->sp = node_p;
4364
Radek Krejcib1b59152019-01-07 13:21:56 +01004365 /* 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 +02004366 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004367
4368 if (node_p->when) {
4369 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004370 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004371 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004372 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004373 }
Radek Krejciec4da802019-05-02 13:02:41 +02004374 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004375 } else {
4376 LOGINT(ctx->ctx);
4377 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004378 }
4379 cs->module = ctx->mod;
4380 cs->prev = (struct lysc_node*)cs;
4381 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004382 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004383 cs->parent = (struct lysc_node*)ch;
4384 cs->child = child;
4385
4386 return cs;
4387error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004388 if (cs) {
4389 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4390 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004391 return NULL;
4392
4393#undef UNIQUE_CHECK
4394}
4395
Radek Krejcib56c7502019-02-13 14:19:54 +01004396/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004397 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004398 *
4399 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004400 * @param[in] node Target node where the config is supposed to be changed.
4401 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004402 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4403 * 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 +01004404 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4405 * @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 +01004406 * @return LY_ERR value.
4407 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004408static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004409lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004410 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004411{
4412 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004413 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004414
4415 if (config == (node->flags & LYS_CONFIG_MASK)) {
4416 /* nothing to do */
4417 return LY_SUCCESS;
4418 }
4419
4420 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004421 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004422 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4423 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004424 "Invalid %s of config - configuration node cannot be child of any state data node.",
4425 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004426 return LY_EVALID;
4427 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004428 node->flags |= LYS_SET_CONFIG;
4429 } else {
4430 if (node->flags & LYS_SET_CONFIG) {
4431 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4432 /* setting config flags, but have node with explicit config true */
4433 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004434 "Invalid %s of config - configuration node cannot be child of any state data node.",
4435 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004436 return LY_EVALID;
4437 }
4438 /* do not change config on nodes where the config is explicitely set, this does not apply to
4439 * nodes, which are being changed explicitly (targets of refine or deviation) */
4440 return LY_SUCCESS;
4441 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004442 }
4443 node->flags &= ~LYS_CONFIG_MASK;
4444 node->flags |= config;
4445
4446 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004447 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004448 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004449 }
4450
Radek Krejci76b3e962018-12-14 17:01:25 +01004451 return LY_SUCCESS;
4452}
4453
Radek Krejcib56c7502019-02-13 14:19:54 +01004454/**
4455 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4456 *
4457 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4458 * the flag to such parents from a mandatory children.
4459 *
4460 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4461 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4462 * (mandatory children was removed).
4463 */
Radek Krejcife909632019-02-12 15:34:42 +01004464void
4465lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4466{
4467 struct lysc_node *iter;
4468
4469 if (add) { /* set flag */
4470 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4471 parent = parent->parent) {
4472 parent->flags |= LYS_MAND_TRUE;
4473 }
4474 } else { /* unset flag */
4475 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004476 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004477 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004478 /* there is another mandatory node */
4479 return;
4480 }
4481 }
4482 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4483 parent->flags &= ~LYS_MAND_TRUE;
4484 }
4485 }
4486}
4487
Radek Krejci056d0a82018-12-06 16:57:25 +01004488/**
Radek Krejci3641f562019-02-13 15:38:40 +01004489 * @brief Internal sorting process for the lys_compile_augment_sort().
4490 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4491 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4492 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4493 */
4494static void
4495lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4496{
4497 unsigned int v;
4498 size_t len;
4499
4500 len = strlen(aug_p->nodeid);
4501 LY_ARRAY_FOR(result, v) {
4502 if (strlen(result[v]->nodeid) <= len) {
4503 continue;
4504 }
4505 if (v < LY_ARRAY_SIZE(result)) {
4506 /* move the rest of array */
4507 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4508 break;
4509 }
4510 }
4511 result[v] = aug_p;
4512 LY_ARRAY_INCREMENT(result);
4513}
4514
4515/**
4516 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4517 *
4518 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4519 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4520 *
4521 * @param[in] ctx Compile context.
4522 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4523 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4524 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4525 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4526 * @return LY_ERR value.
4527 */
4528LY_ERR
4529lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4530{
4531 struct lysp_augment **result = NULL;
4532 unsigned int u, v;
4533 size_t count = 0;
4534
4535 assert(augments);
4536
4537 /* get count of the augments in module and all its submodules */
4538 if (aug_p) {
4539 count += LY_ARRAY_SIZE(aug_p);
4540 }
4541 LY_ARRAY_FOR(inc_p, u) {
4542 if (inc_p[u].submodule->augments) {
4543 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4544 }
4545 }
4546
4547 if (!count) {
4548 *augments = NULL;
4549 return LY_SUCCESS;
4550 }
4551 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4552
4553 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4554 * together, so there can be even /z/y betwwen them. */
4555 LY_ARRAY_FOR(aug_p, u) {
4556 lys_compile_augment_sort_(&aug_p[u], result);
4557 }
4558 LY_ARRAY_FOR(inc_p, u) {
4559 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4560 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4561 }
4562 }
4563
4564 *augments = result;
4565 return LY_SUCCESS;
4566}
4567
4568/**
4569 * @brief Compile the parsed augment connecting it into its target.
4570 *
4571 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4572 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4573 * are already implemented and compiled.
4574 *
4575 * @param[in] ctx Compile context.
4576 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004577 * @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
4578 * children in case of the augmenting uses data.
4579 * @return LY_SUCCESS on success.
4580 * @return LY_EVALID on failure.
4581 */
4582LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004583lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004584{
4585 LY_ERR ret = LY_SUCCESS;
4586 struct lysp_node *node_p, *case_node_p;
4587 struct lysc_node *target; /* target target of the augment */
4588 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004589 struct lysc_when **when, *when_shared;
4590 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004591 uint16_t flags = 0;
4592 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004593 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004594
Radek Krejci327de162019-06-14 12:52:07 +02004595 lysc_update_path(ctx, NULL, "{augment}");
4596 lysc_update_path(ctx, NULL, aug_p->nodeid);
4597
Radek Krejci7af64242019-02-18 13:07:53 +01004598 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004599 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004600 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004601 if (ret != LY_SUCCESS) {
4602 if (ret == LY_EDENIED) {
4603 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4604 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4605 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4606 }
4607 return LY_EVALID;
4608 }
4609
4610 /* check for mandatory nodes
4611 * - new cases augmenting some choice can have mandatory nodes
4612 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4613 */
Radek Krejci733988a2019-02-15 15:12:44 +01004614 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004615 allow_mandatory = 1;
4616 }
4617
4618 when_shared = NULL;
4619 LY_LIST_FOR(aug_p->child, node_p) {
4620 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4621 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4622 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004623 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4624 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004626 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4627 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004628 return LY_EVALID;
4629 }
4630
4631 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004632 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004633 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004634 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004635 } else {
4636 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004637 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004638 }
4639 }
Radek Krejciec4da802019-05-02 13:02:41 +02004640 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004641
Radek Krejcife13da42019-02-15 14:51:01 +01004642 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4643 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004644 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004645 /* 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 +01004646 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 +01004647 } else if (target->nodetype == LYS_CHOICE) {
4648 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4649 node = ((struct lysc_node_choice*)target)->cases->prev;
4650 } else {
4651 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004652 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004653 }
4654
Radek Krejci733988a2019-02-15 15:12:44 +01004655 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004656 node->flags &= ~LYS_MAND_TRUE;
4657 lys_compile_mandatory_parents(target, 0);
4658 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004659 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004660 return LY_EVALID;
4661 }
4662
4663 /* pass augment's when to all the children */
4664 if (aug_p->when) {
4665 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4666 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004667 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004668 LY_CHECK_GOTO(ret, error);
4669 (*when)->context = lysc_xpath_context(target);
4670 when_shared = *when;
4671 } else {
4672 ++when_shared->refcount;
4673 (*when) = when_shared;
4674 }
4675 }
4676 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004677
Radek Krejciec4da802019-05-02 13:02:41 +02004678 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004679 switch (target->nodetype) {
4680 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004681 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004682 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004683 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004684 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004685 break;
4686 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004687 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004688 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004689 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004690 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004691 break;
4692 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004693 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004694 if (aug_p->actions) {
4695 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004696 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4697 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004698 return LY_EVALID;
4699 }
4700 if (aug_p->notifs) {
4701 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004702 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4703 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004704 return LY_EVALID;
4705 }
4706 }
Radek Krejci3641f562019-02-13 15:38:40 +01004707
Radek Krejci327de162019-06-14 12:52:07 +02004708 lysc_update_path(ctx, NULL, NULL);
4709 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004710error:
Radek Krejciec4da802019-05-02 13:02:41 +02004711 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004712 return ret;
4713}
4714
4715/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004716 * @brief Apply refined or deviated mandatory flag to the target node.
4717 *
4718 * @param[in] ctx Compile context.
4719 * @param[in] node Target node where the mandatory property is supposed to be changed.
4720 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004721 * @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 +01004722 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4723 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4724 * 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 +01004725 * @return LY_ERR value.
4726 */
4727static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004728lys_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 +01004729{
4730 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4731 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004732 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4733 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004734 return LY_EVALID;
4735 }
4736
4737 if (mandatory_flag & LYS_MAND_TRUE) {
4738 /* check if node has default value */
4739 if (node->nodetype & LYS_LEAF) {
4740 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004741 if (refine_flag) {
4742 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004743 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004744 return LY_EVALID;
4745 }
Radek Krejcia1911222019-07-22 17:24:50 +02004746 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004747 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004748 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004749
4750 /* update the list of incomplete default values if needed */
4751 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4752
Radek Krejcia1911222019-07-22 17:24:50 +02004753 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4754 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4755 free(leaf->dflt);
4756 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004757 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004758 }
4759 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004760 if (refine_flag) {
4761 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004762 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004763 return LY_EVALID;
4764 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004765 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004766 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004767 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 +01004768 return LY_EVALID;
4769 }
4770
4771 node->flags &= ~LYS_MAND_FALSE;
4772 node->flags |= LYS_MAND_TRUE;
4773 lys_compile_mandatory_parents(node->parent, 1);
4774 } else {
4775 /* make mandatory false */
4776 node->flags &= ~LYS_MAND_TRUE;
4777 node->flags |= LYS_MAND_FALSE;
4778 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004779 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 +01004780 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004781 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004782 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004783 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4784 leaf->dflt->realtype = leaf->type->dflt->realtype;
4785 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4786 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004787 }
4788 }
4789 return LY_SUCCESS;
4790}
4791
4792/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004793 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4794 * If present, also apply uses's modificators.
4795 *
4796 * @param[in] ctx Compile context
4797 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004798 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4799 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4800 * the compile context.
4801 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4802 */
4803static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004804lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004805{
4806 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004807 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004808 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004809 struct lysc_node_container context_node_fake =
4810 {.nodetype = LYS_CONTAINER,
4811 .module = ctx->mod,
4812 .flags = parent ? parent->flags : 0,
4813 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004814 .prev = (struct lysc_node*)&context_node_fake,
4815 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004816 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004817 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004818 int found;
4819 const char *id, *name, *prefix;
4820 size_t prefix_len, name_len;
4821 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004822 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004823 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004824 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004825 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004826 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004827 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004828 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004829 unsigned int actions_index, notifs_index;
4830 struct lysc_notif **notifs = NULL;
4831 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004832
4833 /* search for the grouping definition */
4834 found = 0;
4835 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004836 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004837 if (prefix) {
4838 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4839 if (!mod) {
4840 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004841 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004842 return LY_EVALID;
4843 }
4844 } else {
4845 mod = ctx->mod_def;
4846 }
4847 if (mod == ctx->mod_def) {
4848 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004849 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004850 LY_ARRAY_FOR(grp, u) {
4851 if (!strcmp(grp[u].name, name)) {
4852 grp = &grp[u];
4853 found = 1;
4854 break;
4855 }
4856 }
4857 }
4858 }
4859 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004860 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004861 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004862 if (grp) {
4863 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4864 if (!strcmp(grp[u].name, name)) {
4865 grp = &grp[u];
4866 found = 1;
4867 }
4868 }
4869 }
4870 if (!found && mod->parsed->includes) {
4871 /* ... and all the submodules */
4872 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4873 grp = mod->parsed->includes[u].submodule->groupings;
4874 if (grp) {
4875 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4876 if (!strcmp(grp[v].name, name)) {
4877 grp = &grp[v];
4878 found = 1;
4879 }
4880 }
4881 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004882 }
4883 }
4884 }
4885 if (!found) {
4886 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4887 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4888 return LY_EVALID;
4889 }
4890
4891 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4892 grp_stack_count = ctx->groupings.count;
4893 ly_set_add(&ctx->groupings, (void*)grp, 0);
4894 if (grp_stack_count == ctx->groupings.count) {
4895 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4896 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4897 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4898 return LY_EVALID;
4899 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004900 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4901 /* remember that the grouping is instantiated to avoid its standalone validation */
4902 grp->flags |= LYS_USED_GRP;
4903 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004904
4905 /* switch context's mod_def */
4906 mod_old = ctx->mod_def;
4907 ctx->mod_def = mod;
4908
4909 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004910 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 +01004911
Radek Krejcifc11bd72019-04-11 16:00:05 +02004912 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004913 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004914 /* 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 +02004915 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 +01004916
4917 /* some preparation for applying refines */
4918 if (grp->data == node_p) {
4919 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004920 if (parent) {
4921 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4922 } else if (ctx->mod->compiled->data) {
4923 child = ctx->mod->compiled->data;
4924 } else {
4925 child = NULL;
4926 }
4927 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004928 }
4929 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004930 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004931 LY_LIST_FOR(context_node_fake.child, child) {
4932 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004933
Radek Krejcifc11bd72019-04-11 16:00:05 +02004934 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004935 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004936 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004937 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004938 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004939 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004940 when_shared = *when;
4941 } else {
4942 ++when_shared->refcount;
4943 (*when) = when_shared;
4944 }
4945 }
Radek Krejci01342af2019-01-03 15:18:08 +01004946 }
4947 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004948 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004949 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004950 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004951 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 +01004952 }
4953
Radek Krejcifc11bd72019-04-11 16:00:05 +02004954 /* compile actions */
4955 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4956 if (actions) {
4957 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004958 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004959 if (*actions && (uses_p->augments || uses_p->refines)) {
4960 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4961 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4962 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4963 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4964 }
4965 }
4966
4967 /* compile notifications */
4968 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4969 if (notifs) {
4970 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004971 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004972 if (*notifs && (uses_p->augments || uses_p->refines)) {
4973 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4974 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4975 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4976 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4977 }
4978 }
4979
4980
Radek Krejci3641f562019-02-13 15:38:40 +01004981 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004982 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004983 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004984 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004985 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004986
Radek Krejcif0089082019-01-07 16:42:01 +01004987 /* reload previous context's mod_def */
4988 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004989 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004990
Radek Krejci76b3e962018-12-14 17:01:25 +01004991 /* apply refine */
4992 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004993 lysc_update_path(ctx, NULL, rfn->nodeid);
4994
Radek Krejci7af64242019-02-18 13:07:53 +01004995 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 +01004996 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004997 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004998 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004999
5000 /* default value */
5001 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01005002 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01005003 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005004 "Invalid refine of default - %s cannot hold %d default values.",
5005 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005006 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005007 }
5008 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
5009 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005010 "Invalid refine of default - %s cannot hold default value(s).",
5011 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005012 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005013 }
5014 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02005015 struct ly_err_item *err = NULL;
5016 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
5017 if (leaf->dflt) {
5018 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02005019 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005020 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5021 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5022 } else {
5023 /* prepare a new one */
5024 leaf->dflt = calloc(1, sizeof *leaf->dflt);
5025 leaf->dflt->realtype = leaf->type;
5026 }
5027 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005028 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005029 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
5030 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005031 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005032 leaf->dflt->realtype->refcount++;
5033 if (err) {
5034 ly_err_print(err);
5035 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5036 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
5037 ly_err_free(err);
5038 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005039 if (rc == LY_EINCOMPLETE) {
5040 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005041 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005042
5043 /* but in general result is so far ok */
5044 rc = LY_SUCCESS;
5045 }
Radek Krejcia1911222019-07-22 17:24:50 +02005046 LY_CHECK_GOTO(rc, cleanup);
5047 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005048 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02005049 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
5050
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005051 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01005052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5053 "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 +02005054 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01005055 }
Radek Krejcia1911222019-07-22 17:24:50 +02005056
5057 /* remove previous set of default values */
5058 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005059 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005060 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5061 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5062 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005063 }
Radek Krejcia1911222019-07-22 17:24:50 +02005064 LY_ARRAY_FREE(llist->dflts);
5065 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005066 LY_ARRAY_FREE(llist->dflts_mods);
5067 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005068
5069 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005070 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005071 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005072 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005073 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005074 LY_ARRAY_INCREMENT(llist->dflts_mods);
5075 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005076 LY_ARRAY_INCREMENT(llist->dflts);
5077 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5078 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005079 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005080 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005081 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005082 llist->dflts[u]->realtype->refcount++;
5083 if (err) {
5084 ly_err_print(err);
5085 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5086 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5087 ly_err_free(err);
5088 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005089 if (rc == LY_EINCOMPLETE) {
5090 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005091 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005092
5093 /* but in general result is so far ok */
5094 rc = LY_SUCCESS;
5095 }
5096 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005097 }
Radek Krejcia1911222019-07-22 17:24:50 +02005098 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005099 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005100 if (((struct lysc_node_choice*)node)->dflt) {
5101 /* unset LYS_SET_DFLT from the current default case */
5102 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5103 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005104 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 +01005105 }
5106 }
5107
Radek Krejci12fb9142019-01-08 09:45:30 +01005108 /* description */
5109 if (rfn->dsc) {
5110 FREE_STRING(ctx->ctx, node->dsc);
5111 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5112 }
5113
5114 /* reference */
5115 if (rfn->ref) {
5116 FREE_STRING(ctx->ctx, node->ref);
5117 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5118 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005119
5120 /* config */
5121 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005122 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005123 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005124 } else {
5125 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
5126 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
5127 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005128 }
5129
5130 /* mandatory */
5131 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005132 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005133 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005134
5135 /* presence */
5136 if (rfn->presence) {
5137 if (node->nodetype != LYS_CONTAINER) {
5138 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005139 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5140 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005141 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005142 }
5143 node->flags |= LYS_PRESENCE;
5144 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005145
5146 /* must */
5147 if (rfn->musts) {
5148 switch (node->nodetype) {
5149 case LYS_LEAF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005150 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 +01005151 break;
5152 case LYS_LEAFLIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005153 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 +01005154 break;
5155 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005156 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 +01005157 break;
5158 case LYS_CONTAINER:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005159 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 +01005160 break;
5161 case LYS_ANYXML:
5162 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005163 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 +01005164 break;
5165 default:
5166 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005167 "Invalid refine of must statement - %s cannot hold any must statement.",
5168 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005169 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005170 }
5171 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005172
5173 /* min/max-elements */
5174 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5175 switch (node->nodetype) {
5176 case LYS_LEAFLIST:
5177 if (rfn->flags & LYS_SET_MAX) {
5178 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5179 }
5180 if (rfn->flags & LYS_SET_MIN) {
5181 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005182 if (rfn->min) {
5183 node->flags |= LYS_MAND_TRUE;
5184 lys_compile_mandatory_parents(node->parent, 1);
5185 } else {
5186 node->flags &= ~LYS_MAND_TRUE;
5187 lys_compile_mandatory_parents(node->parent, 0);
5188 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005189 }
5190 break;
5191 case LYS_LIST:
5192 if (rfn->flags & LYS_SET_MAX) {
5193 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5194 }
5195 if (rfn->flags & LYS_SET_MIN) {
5196 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005197 if (rfn->min) {
5198 node->flags |= LYS_MAND_TRUE;
5199 lys_compile_mandatory_parents(node->parent, 1);
5200 } else {
5201 node->flags &= ~LYS_MAND_TRUE;
5202 lys_compile_mandatory_parents(node->parent, 0);
5203 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005204 }
5205 break;
5206 default:
5207 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005208 "Invalid refine of %s statement - %s cannot hold this statement.",
5209 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005210 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005211 }
5212 }
Radek Krejcif0089082019-01-07 16:42:01 +01005213
5214 /* if-feature */
5215 if (rfn->iffeatures) {
5216 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005217 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005218 }
Radek Krejci327de162019-06-14 12:52:07 +02005219
5220 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005221 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005222
Radek Krejcif2271f12019-01-07 16:42:23 +01005223 /* do some additional checks of the changed nodes when all the refines are applied */
5224 for (u = 0; u < refined.count; ++u) {
5225 node = (struct lysc_node*)refined.objs[u];
5226 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005227 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005228
5229 /* check possible conflict with default value (default added, mandatory left true) */
5230 if ((node->flags & LYS_MAND_TRUE) &&
5231 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5232 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5233 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005234 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005235 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005236 }
5237
5238 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5239 if (node->nodetype == LYS_LIST) {
5240 min = ((struct lysc_node_list*)node)->min;
5241 max = ((struct lysc_node_list*)node)->max;
5242 } else {
5243 min = ((struct lysc_node_leaflist*)node)->min;
5244 max = ((struct lysc_node_leaflist*)node)->max;
5245 }
5246 if (min > max) {
5247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005248 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5249 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005250 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005251 }
5252 }
5253 }
5254
Radek Krejci327de162019-06-14 12:52:07 +02005255 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005256 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005257
5258cleanup:
5259 /* fix connection of the children nodes from fake context node back into the parent */
5260 if (context_node_fake.child) {
5261 context_node_fake.child->prev = child;
5262 }
5263 LY_LIST_FOR(context_node_fake.child, child) {
5264 child->parent = parent;
5265 }
5266
5267 if (uses_p->augments || uses_p->refines) {
5268 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005269 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005270 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5271 LY_ARRAY_FREE(context_node_fake.actions);
5272 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005273 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005274 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5275 LY_ARRAY_FREE(context_node_fake.notifs);
5276 }
5277 }
5278
Radek Krejcie86bf772018-12-14 11:39:53 +01005279 /* reload previous context's mod_def */
5280 ctx->mod_def = mod_old;
5281 /* remove the grouping from the stack for circular groupings dependency check */
5282 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5283 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005284 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005285 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005286
5287 return ret;
5288}
5289
Radek Krejci327de162019-06-14 12:52:07 +02005290static int
5291lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5292{
5293 struct lysp_node *iter;
5294 int len = 0;
5295
5296 *path = NULL;
5297 for (iter = node; iter && len >= 0; iter = iter->parent) {
5298 char *s = *path;
5299 char *id;
5300
5301 switch (iter->nodetype) {
5302 case LYS_USES:
5303 asprintf(&id, "{uses='%s'}", iter->name);
5304 break;
5305 case LYS_GROUPING:
5306 asprintf(&id, "{grouping='%s'}", iter->name);
5307 break;
5308 case LYS_AUGMENT:
5309 asprintf(&id, "{augment='%s'}", iter->name);
5310 break;
5311 default:
5312 id = strdup(iter->name);
5313 break;
5314 }
5315
5316 if (!iter->parent) {
5317 /* print prefix */
5318 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5319 } else {
5320 /* prefix is the same as in parent */
5321 len = asprintf(path, "/%s%s", id, s ? s : "");
5322 }
5323 free(s);
5324 free(id);
5325 }
5326
5327 if (len < 0) {
5328 free(*path);
5329 *path = NULL;
5330 } else if (len == 0) {
5331 *path = strdup("/");
5332 len = 1;
5333 }
5334 return len;
5335}
5336
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005337/**
5338 * @brief Validate groupings that were defined but not directly used in the schema itself.
5339 *
5340 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5341 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5342 */
5343static LY_ERR
5344lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5345{
5346 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005347 char *path;
5348 int len;
5349
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005350 struct lysp_node_uses fake_uses = {
5351 .parent = node_p,
5352 .nodetype = LYS_USES,
5353 .flags = 0, .next = NULL,
5354 .name = grp->name,
5355 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5356 .refines = NULL, .augments = NULL
5357 };
5358 struct lysc_node_container fake_container = {
5359 .nodetype = LYS_CONTAINER,
5360 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5361 .module = ctx->mod,
5362 .sp = NULL, .parent = NULL, .next = NULL,
5363 .prev = (struct lysc_node*)&fake_container,
5364 .name = "fake",
5365 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5366 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5367 };
5368
5369 if (grp->parent) {
5370 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5371 }
Radek Krejci327de162019-06-14 12:52:07 +02005372
5373 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5374 if (len < 0) {
5375 LOGMEM(ctx->ctx);
5376 return LY_EMEM;
5377 }
5378 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5379 ctx->path_len = (uint16_t)len;
5380 free(path);
5381
5382 lysc_update_path(ctx, NULL, "{grouping}");
5383 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005384 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005385 lysc_update_path(ctx, NULL, NULL);
5386 lysc_update_path(ctx, NULL, NULL);
5387
5388 ctx->path_len = 1;
5389 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005390
5391 /* cleanup */
5392 lysc_node_container_free(ctx->ctx, &fake_container);
5393
5394 return ret;
5395}
Radek Krejcife909632019-02-12 15:34:42 +01005396
Radek Krejcie86bf772018-12-14 11:39:53 +01005397/**
Radek Krejcia3045382018-11-22 14:30:31 +01005398 * @brief Compile parsed schema node information.
5399 * @param[in] ctx Compile context
5400 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005401 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5402 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5403 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005404 * @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).
5405 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005406 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5407 */
Radek Krejci19a96102018-11-15 13:38:09 +01005408static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005409lys_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 +01005410{
5411 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005412 struct lysc_node *node;
5413 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005414 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005415 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005416 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005417
Radek Krejci327de162019-06-14 12:52:07 +02005418 if (node_p->nodetype != LYS_USES) {
5419 lysc_update_path(ctx, parent, node_p->name);
5420 } else {
5421 lysc_update_path(ctx, NULL, "{uses}");
5422 lysc_update_path(ctx, NULL, node_p->name);
5423 }
5424
Radek Krejci19a96102018-11-15 13:38:09 +01005425 switch (node_p->nodetype) {
5426 case LYS_CONTAINER:
5427 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5428 node_compile_spec = lys_compile_node_container;
5429 break;
5430 case LYS_LEAF:
5431 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5432 node_compile_spec = lys_compile_node_leaf;
5433 break;
5434 case LYS_LIST:
5435 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005436 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005437 break;
5438 case LYS_LEAFLIST:
5439 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005440 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005441 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005442 case LYS_CHOICE:
5443 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005444 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005445 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005446 case LYS_ANYXML:
5447 case LYS_ANYDATA:
5448 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005449 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005450 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005451 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005452 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5453 lysc_update_path(ctx, NULL, NULL);
5454 lysc_update_path(ctx, NULL, NULL);
5455 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005456 default:
5457 LOGINT(ctx->ctx);
5458 return LY_EINT;
5459 }
5460 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5461 node->nodetype = node_p->nodetype;
5462 node->module = ctx->mod;
5463 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005464 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005465
5466 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005467 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005468 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005469 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005470 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5471 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005472 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005473 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005474 node->flags |= LYS_CONFIG_R;
5475 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005476 /* config not explicitely set, inherit it from parent */
5477 if (parent) {
5478 node->flags |= parent->flags & LYS_CONFIG_MASK;
5479 } else {
5480 /* default is config true */
5481 node->flags |= LYS_CONFIG_W;
5482 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005483 } else {
5484 /* config set explicitely */
5485 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005486 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005487 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5488 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5489 "Configuration node cannot be child of any state data node.");
5490 goto error;
5491 }
Radek Krejci19a96102018-11-15 13:38:09 +01005492
Radek Krejcia6d57732018-11-29 13:40:37 +01005493 /* *list ordering */
5494 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5495 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005496 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005497 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5498 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005499 node->flags &= ~LYS_ORDBY_MASK;
5500 node->flags |= LYS_ORDBY_SYSTEM;
5501 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5502 /* default ordering is system */
5503 node->flags |= LYS_ORDBY_SYSTEM;
5504 }
5505 }
5506
Radek Krejci19a96102018-11-15 13:38:09 +01005507 /* status - it is not inherited by specification, but it does not make sense to have
5508 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005509 if (!parent || parent->nodetype != LYS_CHOICE) {
5510 /* in case of choice/case's children, postpone the check to the moment we know if
5511 * 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 +01005512 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 +01005513 }
5514
Radek Krejciec4da802019-05-02 13:02:41 +02005515 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005516 node->sp = node_p;
5517 }
5518 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005519 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5520 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005521 if (node_p->when) {
5522 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005523 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005524 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005525 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005526 }
Radek Krejciec4da802019-05-02 13:02:41 +02005527 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005528
5529 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005530 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005531
Radek Krejci0935f412019-08-20 16:15:18 +02005532 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5533
Radek Krejcife909632019-02-12 15:34:42 +01005534 /* inherit LYS_MAND_TRUE in parent containers */
5535 if (node->flags & LYS_MAND_TRUE) {
5536 lys_compile_mandatory_parents(parent, 1);
5537 }
5538
Radek Krejci327de162019-06-14 12:52:07 +02005539 lysc_update_path(ctx, NULL, NULL);
5540
Radek Krejci19a96102018-11-15 13:38:09 +01005541 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005542 if (parent) {
5543 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005544 if (node_p->parent->nodetype == LYS_CASE) {
5545 lysc_update_path(ctx, parent, node_p->parent->name);
5546 } else {
5547 lysc_update_path(ctx, parent, node->name);
5548 }
Radek Krejciec4da802019-05-02 13:02:41 +02005549 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005550 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005551 if (uses_status) {
5552
5553 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005554 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005555 * it directly gets the same status flags as the choice;
5556 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005557 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005558 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005559 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005560 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005561 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005562 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005563 }
Radek Krejciec4da802019-05-02 13:02:41 +02005564 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 +02005565
5566 if (parent->nodetype == LYS_CHOICE) {
5567 lysc_update_path(ctx, NULL, NULL);
5568 }
Radek Krejci19a96102018-11-15 13:38:09 +01005569 } else {
5570 /* top-level element */
5571 if (!ctx->mod->compiled->data) {
5572 ctx->mod->compiled->data = node;
5573 } else {
5574 /* insert at the end of the module's top-level nodes list */
5575 ctx->mod->compiled->data->prev->next = node;
5576 node->prev = ctx->mod->compiled->data->prev;
5577 ctx->mod->compiled->data->prev = node;
5578 }
Radek Krejci327de162019-06-14 12:52:07 +02005579 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005580 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5581 ctx->mod->compiled->notifs, node->name, node)) {
5582 return LY_EVALID;
5583 }
Radek Krejci19a96102018-11-15 13:38:09 +01005584 }
Radek Krejci327de162019-06-14 12:52:07 +02005585 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005586
5587 return LY_SUCCESS;
5588
5589error:
5590 lysc_node_free(ctx->ctx, node);
5591 return ret;
5592}
5593
Radek Krejciccd20f12019-02-15 14:12:27 +01005594static void
5595lysc_disconnect(struct lysc_node *node)
5596{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005597 struct lysc_node *parent, *child, *prev = NULL, *next;
5598 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005599 int remove_cs = 0;
5600
5601 parent = node->parent;
5602
5603 /* parent's first child */
5604 if (!parent) {
5605 return;
5606 }
5607 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005608 cs = (struct lysc_node_case*)node;
5609 } else if (parent->nodetype == LYS_CASE) {
5610 /* disconnecting some node in a case */
5611 cs = (struct lysc_node_case*)parent;
5612 parent = cs->parent;
5613 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5614 if (child == node) {
5615 if (cs->child == child) {
5616 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5617 /* case with a single child -> remove also the case */
5618 child->parent = NULL;
5619 remove_cs = 1;
5620 } else {
5621 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005622 }
5623 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005624 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005625 }
5626 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005627 if (!remove_cs) {
5628 cs = NULL;
5629 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005630 } else if (lysc_node_children(parent, node->flags) == node) {
5631 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005632 }
5633
5634 if (cs) {
5635 if (remove_cs) {
5636 /* cs has only one child which is being also removed */
5637 lysc_disconnect((struct lysc_node*)cs);
5638 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5639 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005640 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5641 /* default case removed */
5642 ((struct lysc_node_choice*)parent)->dflt = NULL;
5643 }
5644 if (((struct lysc_node_choice*)parent)->cases == cs) {
5645 /* first case removed */
5646 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5647 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005648 if (cs->child) {
5649 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5650 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5651 prev = cs->child->prev;
5652 } /* else all the children are under a single case */
5653 LY_LIST_FOR_SAFE(cs->child, next, child) {
5654 if (child->parent != (struct lysc_node*)cs) {
5655 break;
5656 }
5657 lysc_node_free(node->module->ctx, child);
5658 }
5659 if (prev) {
5660 if (prev->next) {
5661 prev->next = child;
5662 }
5663 if (child) {
5664 child->prev = prev;
5665 } else {
5666 /* link from the first child under the cases */
5667 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5668 }
5669 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005670 }
5671 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005672 }
5673
5674 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005675 if (node->prev->next) {
5676 node->prev->next = node->next;
5677 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005678 if (node->next) {
5679 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005680 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005681 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005682 if (child) {
5683 child->prev = node->prev;
5684 }
5685 } else if (((struct lysc_node_choice*)parent)->cases) {
5686 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005687 }
5688}
5689
5690LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005691lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005692{
Radek Krejcia1911222019-07-22 17:24:50 +02005693 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005694 struct ly_set devs_p = {0};
5695 struct ly_set targets = {0};
5696 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005697 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005698 struct lysc_action *rpcs;
5699 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005700 struct lysp_deviation *dev;
5701 struct lysp_deviate *d, **dp_new;
5702 struct lysp_deviate_add *d_add;
5703 struct lysp_deviate_del *d_del;
5704 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005705 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005706 struct lysc_deviation {
5707 const char *nodeid;
5708 struct lysc_node *target; /* target node of the deviation */
5709 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005710 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005711 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5712 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005713 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005714 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005715 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005716 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005717 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005718 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005719
5720 /* get all deviations from the module and all its submodules ... */
5721 LY_ARRAY_FOR(mod_p->deviations, u) {
5722 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5723 }
5724 LY_ARRAY_FOR(mod_p->includes, v) {
5725 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5726 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5727 }
5728 }
5729 if (!devs_p.count) {
5730 /* nothing to do */
5731 return LY_SUCCESS;
5732 }
5733
Radek Krejci327de162019-06-14 12:52:07 +02005734 lysc_update_path(ctx, NULL, "{deviation}");
5735
Radek Krejciccd20f12019-02-15 14:12:27 +01005736 /* ... and group them by the target node */
5737 devs = calloc(devs_p.count, sizeof *devs);
5738 for (u = 0; u < devs_p.count; ++u) {
5739 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005740 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005741
5742 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005743 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5744 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005745 if (target->nodetype == LYS_ACTION) {
5746 /* move the target pointer to input/output to make them different from the action and
5747 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5748 * back to the RPC/action node due to a better compatibility and decision code in this function.
5749 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5750 if (flags & LYSC_OPT_RPC_INPUT) {
5751 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5752 flags |= LYSC_OPT_INTERNAL;
5753 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5754 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5755 flags |= LYSC_OPT_INTERNAL;
5756 }
5757 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005758 /* insert into the set of targets with duplicity detection */
5759 i = ly_set_add(&targets, target, 0);
5760 if (!devs[i]) {
5761 /* new record */
5762 devs[i] = calloc(1, sizeof **devs);
5763 devs[i]->target = target;
5764 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005765 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005766 }
5767 /* add deviates into the deviation's list of deviates */
5768 for (d = dev->deviates; d; d = d->next) {
5769 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5770 *dp_new = d;
5771 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5772 devs[i]->not_supported = 1;
5773 }
5774 }
Radek Krejci327de162019-06-14 12:52:07 +02005775
5776 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005777 }
5778
5779 /* MACROS for deviates checking */
5780#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5781 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005782 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 +01005783 goto cleanup; \
5784 }
5785
5786#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5787 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005788 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5789 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005790 goto cleanup; \
5791 }
5792
Radek Krejcia1911222019-07-22 17:24:50 +02005793
Radek Krejciccd20f12019-02-15 14:12:27 +01005794#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005795 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5796 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5797 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5798 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5799 goto cleanup; \
5800 }
5801
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005802#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005803 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005804 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005805 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5806 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005807 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005808 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5809 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005810 goto cleanup; \
5811 }
5812
Radek Krejci551b12c2019-02-19 16:11:21 +01005813#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5814 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5815 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005816 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5817 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005818 goto cleanup; \
5819 }
5820
Radek Krejciccd20f12019-02-15 14:12:27 +01005821#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5822 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005823 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005824 goto cleanup; \
5825 }
5826
Radek Krejci551b12c2019-02-19 16:11:21 +01005827#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5828 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5829 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005830 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005831 goto cleanup; \
5832 }
5833
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005834#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5835 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5836 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5837 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5838 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 +01005839 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005840 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005842 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5843 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005844 goto cleanup; \
5845 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005846 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5847 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5848 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5849 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5850 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005851 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005852 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5853 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5854 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005855 }
5856
5857 /* apply deviations */
5858 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005859 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5860 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5861 struct ly_err_item *err = NULL;
5862
5863 dflt = NULL;
5864 changed_type = 0;
5865
Radek Krejci327de162019-06-14 12:52:07 +02005866 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5867
Radek Krejcif538ce52019-03-05 10:46:14 +01005868 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5869 /* fix the target pointer in case of RPC's/action's input/output */
5870 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5871 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5872 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5873 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5874 }
5875 }
5876
Radek Krejciccd20f12019-02-15 14:12:27 +01005877 /* not-supported */
5878 if (devs[u]->not_supported) {
5879 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5880 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5881 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5882 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005883
5884#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5885 if (devs[u]->target->parent) { \
5886 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5887 } else { \
5888 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5889 } \
5890 LY_ARRAY_FOR(ARRAY, x) { \
5891 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5892 } \
5893 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5894 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5895 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5896 LY_ARRAY_DECREMENT(ARRAY); \
5897 }
5898
Radek Krejcif538ce52019-03-05 10:46:14 +01005899 if (devs[u]->target->nodetype == LYS_ACTION) {
5900 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5901 /* remove RPC's/action's input */
5902 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5903 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005904 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5905 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005906 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5907 /* remove RPC's/action's output */
5908 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5909 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005910 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5911 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005912 } else {
5913 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005914 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005915 }
5916 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005917 /* remove Notification */
5918 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005919 } else {
5920 /* remove the target node */
5921 lysc_disconnect(devs[u]->target);
5922 lysc_node_free(ctx->ctx, devs[u]->target);
5923 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005924
Radek Krejci474f9b82019-07-24 11:36:37 +02005925 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5926 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005927 continue;
5928 }
5929
5930 /* list of deviates (not-supported is not present in the list) */
5931 LY_ARRAY_FOR(devs[u]->deviates, v) {
5932 d = devs[u]->deviates[v];
5933
5934 switch (d->mod) {
5935 case LYS_DEV_ADD:
5936 d_add = (struct lysp_deviate_add*)d;
5937 /* [units-stmt] */
5938 if (d_add->units) {
5939 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5940 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5941
5942 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5943 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5944 }
5945
5946 /* *must-stmt */
5947 if (d_add->musts) {
5948 switch (devs[u]->target->nodetype) {
5949 case LYS_CONTAINER:
5950 case LYS_LIST:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005951 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
5952 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005953 break;
5954 case LYS_LEAF:
5955 case LYS_LEAFLIST:
5956 case LYS_ANYDATA:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005957 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
5958 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005959 break;
5960 case LYS_NOTIF:
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005961 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
5962 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005963 break;
5964 case LYS_ACTION:
5965 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005966 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
5967 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005968 break;
5969 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
Radek Krejcic71ac5b2019-09-10 15:34:22 +02005970 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
5971 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005972 break;
5973 }
5974 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005975 default:
5976 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005977 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005978 goto cleanup;
5979 }
5980 }
5981
5982 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005983 if (d_add->uniques) {
5984 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5985 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5986 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005987
5988 /* *default-stmt */
5989 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005990 switch (devs[u]->target->nodetype) {
5991 case LYS_LEAF:
5992 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005993 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 +02005994 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005995 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005996 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005997 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5998 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5999 } else {
6000 /* prepare new default value storage */
6001 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01006002 }
Radek Krejcia1911222019-07-22 17:24:50 +02006003 dflt = d_add->dflts[0];
6004 /* parsing is done at the end after possible replace of the leaf's type */
6005
Radek Krejci551b12c2019-02-19 16:11:21 +01006006 /* mark the new default values as leaf's own */
6007 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006008 break;
6009 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006010 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006011 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02006012 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006013 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006014 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6015 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6016 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006017 }
Radek Krejcia1911222019-07-22 17:24:50 +02006018 LY_ARRAY_FREE(llist->dflts);
6019 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006020 LY_ARRAY_FREE(llist->dflts_mods);
6021 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006022 }
6023 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006024 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006025 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
6026 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01006027 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006028 LY_ARRAY_INCREMENT(llist->dflts_mods);
6029 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006030 LY_ARRAY_INCREMENT(llist->dflts);
6031 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
6032 llist->dflts[x]->realtype = llist->type;
6033 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 +02006034 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006035 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006036 llist->dflts[x]->realtype->refcount++;
6037 if (err) {
6038 ly_err_print(err);
6039 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6040 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
6041 d_add->dflts[x - y], err->msg);
6042 ly_err_free(err);
6043 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006044 if (rc == LY_EINCOMPLETE) {
6045 /* postpone default compilation when the tree is complete */
6046 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6047
6048 /* but in general result is so far ok */
6049 rc = LY_SUCCESS;
6050 }
Radek Krejcia1911222019-07-22 17:24:50 +02006051 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006052 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006053 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01006054 devs[u]->target->flags |= LYS_SET_DFLT;
6055 break;
6056 case LYS_CHOICE:
6057 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6058 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6059 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6060 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006061 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 +01006062 goto cleanup;
6063 }
6064 break;
6065 default:
6066 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006067 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006068 goto cleanup;
6069 }
6070 }
6071
6072 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006073 if (d_add->flags & LYS_CONFIG_MASK) {
6074 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006075 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006076 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6077 goto cleanup;
6078 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006079 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006080 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
6081 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006082 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006083 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6084 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006085 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6086 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006087 goto cleanup;
6088 }
Radek Krejci327de162019-06-14 12:52:07 +02006089 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006090 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006091
6092 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006093 if (d_add->flags & LYS_MAND_MASK) {
6094 if (devs[u]->target->flags & LYS_MAND_MASK) {
6095 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006096 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6097 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006098 goto cleanup;
6099 }
Radek Krejci327de162019-06-14 12:52:07 +02006100 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006101 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006102
6103 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006104 if (d_add->flags & LYS_SET_MIN) {
6105 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6106 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6107 /* change value */
6108 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6109 } else if (devs[u]->target->nodetype == LYS_LIST) {
6110 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6111 /* change value */
6112 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6113 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006114 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006115 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6116 goto cleanup;
6117 }
6118 if (d_add->min) {
6119 devs[u]->target->flags |= LYS_MAND_TRUE;
6120 }
6121 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006122
6123 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006124 if (d_add->flags & LYS_SET_MAX) {
6125 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6126 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6127 /* change value */
6128 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6129 } else if (devs[u]->target->nodetype == LYS_LIST) {
6130 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6131 /* change value */
6132 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6133 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006135 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6136 goto cleanup;
6137 }
6138 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006139
6140 break;
6141 case LYS_DEV_DELETE:
6142 d_del = (struct lysp_deviate_del*)d;
6143
6144 /* [units-stmt] */
6145 if (d_del->units) {
6146 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006147 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6148 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6149 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6150 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6151 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6152 goto cleanup;
6153 }
6154 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6155 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006156 }
6157
6158 /* *must-stmt */
6159 if (d_del->musts) {
6160 switch (devs[u]->target->nodetype) {
6161 case LYS_CONTAINER:
6162 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006163 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006164 break;
6165 case LYS_LEAF:
6166 case LYS_LEAFLIST:
6167 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006168 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006169 break;
6170 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006171 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006172 break;
6173 case LYS_ACTION:
6174 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6175 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6176 break;
6177 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6178 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6179 break;
6180 }
6181 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006182 default:
6183 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006184 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006185 goto cleanup;
6186 }
6187 }
6188
6189 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006190 if (d_del->uniques) {
6191 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6192 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6193 LY_ARRAY_FOR(d_del->uniques, x) {
6194 LY_ARRAY_FOR(list->uniques, z) {
6195 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6196 nodeid = strpbrk(name, " \t\n");
6197 if (nodeid) {
6198 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
6199 || list->uniques[z][y]->name[nodeid - name] != '\0') {
6200 break;
6201 }
6202 while (isspace(*nodeid)) {
6203 ++nodeid;
6204 }
6205 } else {
6206 if (strcmp(name, list->uniques[z][y]->name)) {
6207 break;
6208 }
6209 }
6210 }
6211 if (!name) {
6212 /* complete match - remove the unique */
6213 LY_ARRAY_DECREMENT(list->uniques);
6214 LY_ARRAY_FREE(list->uniques[z]);
6215 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6216 --z;
6217 break;
6218 }
6219 }
6220 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6221 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006222 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6223 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006224 goto cleanup;
6225 }
6226 }
6227 if (!LY_ARRAY_SIZE(list->uniques)) {
6228 LY_ARRAY_FREE(list->uniques);
6229 list->uniques = NULL;
6230 }
6231 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006232
6233 /* *default-stmt */
6234 if (d_del->dflts) {
6235 switch (devs[u]->target->nodetype) {
6236 case LYS_LEAF:
6237 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6238 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6239 dflt, "deleting", "default", d_del->dflts[0]);
6240
Radek Krejcia1911222019-07-22 17:24:50 +02006241 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006242 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006243 if (strcmp(dflt, d_del->dflts[0])) {
6244 if (i) {
6245 free((char*)dflt);
6246 }
6247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6248 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6249 d_del->dflts[0], dflt);
6250 goto cleanup;
6251 }
6252 if (i) {
6253 free((char*)dflt);
6254 }
6255 dflt = NULL;
6256
Radek Krejci474f9b82019-07-24 11:36:37 +02006257 /* update the list of incomplete default values if needed */
6258 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6259
Radek Krejcia1911222019-07-22 17:24:50 +02006260 /* remove the default specification */
6261 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6262 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6263 free(leaf->dflt);
6264 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006265 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006266 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006267 break;
6268 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006269 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6270 LY_ARRAY_FOR(d_del->dflts, x) {
6271 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006272 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 +02006273 if (!strcmp(dflt, d_del->dflts[x])) {
6274 if (i) {
6275 free((char*)dflt);
6276 }
6277 dflt = NULL;
6278 break;
6279 }
6280 if (i) {
6281 free((char*)dflt);
6282 }
6283 dflt = NULL;
6284 }
6285 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6286 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6287 "which does not match any of the target's property values.", d_del->dflts[x]);
6288 goto cleanup;
6289 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006290
6291 /* update the list of incomplete default values if needed */
6292 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6293
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006294 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006295 LY_ARRAY_DECREMENT(llist->dflts);
6296 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6297 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6298 free(llist->dflts[y]);
6299 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006300 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 +02006301 }
6302 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006303 LY_ARRAY_FREE(llist->dflts_mods);
6304 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006305 LY_ARRAY_FREE(llist->dflts);
6306 llist->dflts = NULL;
6307 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006308 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006309 break;
6310 case LYS_CHOICE:
6311 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6312 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6313 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006314 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006315 if (prefix) {
6316 /* use module prefixes from the deviation module to match the module of the default case */
6317 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6318 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006319 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6320 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006321 goto cleanup;
6322 }
6323 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6324 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006325 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6326 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006327 goto cleanup;
6328 }
6329 }
6330 /* else {
6331 * strictly, the default prefix would point to the deviation module, but the value should actually
6332 * match the default string in the original module (usually unprefixed), so in this case we do not check
6333 * the module of the default case, just matching its name */
6334 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6335 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006336 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6337 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006338 goto cleanup;
6339 }
6340 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6341 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6342 break;
6343 default:
6344 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006345 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006346 goto cleanup;
6347 }
6348 }
6349
6350 break;
6351 case LYS_DEV_REPLACE:
6352 d_rpl = (struct lysp_deviate_rpl*)d;
6353
6354 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006355 if (d_rpl->type) {
6356 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6357 /* type is mandatory, so checking for its presence is not necessary */
6358 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006359
6360 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6361 /* the target has default from the previous type - remove it */
6362 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006363 /* update the list of incomplete default values if needed */
6364 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6365
Radek Krejcia1911222019-07-22 17:24:50 +02006366 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6367 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6368 free(leaf->dflt);
6369 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006370 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006371 } else { /* LYS_LEAFLIST */
6372 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006373 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006374 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6375 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6376 free(llist->dflts[x]);
6377 }
6378 LY_ARRAY_FREE(llist->dflts);
6379 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006380 LY_ARRAY_FREE(llist->dflts_mods);
6381 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006382 }
6383 }
6384 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006385 /* there is no default value, do not set changed_type after type compilation
6386 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006387 changed_type = -1;
6388 }
Radek Krejciec4da802019-05-02 13:02:41 +02006389 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 +02006390 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006391 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006392
6393 /* [units-stmt] */
6394 if (d_rpl->units) {
6395 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6396 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6397 units, "replacing", "units", d_rpl->units);
6398
6399 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6400 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6401 }
6402
6403 /* [default-stmt] */
6404 if (d_rpl->dflt) {
6405 switch (devs[u]->target->nodetype) {
6406 case LYS_LEAF:
6407 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6408 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006409 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006410 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006411 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6412 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6413 dflt = d_rpl->dflt;
6414 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006415 break;
6416 case LYS_CHOICE:
6417 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006418 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 +01006419 goto cleanup;
6420 }
6421 break;
6422 default:
6423 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006424 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006425 goto cleanup;
6426 }
6427 }
6428
6429 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006430 if (d_rpl->flags & LYS_CONFIG_MASK) {
6431 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006433 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6434 goto cleanup;
6435 }
6436 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006438 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6439 goto cleanup;
6440 }
Radek Krejci327de162019-06-14 12:52:07 +02006441 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006442 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006443
6444 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006445 if (d_rpl->flags & LYS_MAND_MASK) {
6446 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006447 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006448 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6449 goto cleanup;
6450 }
Radek Krejci327de162019-06-14 12:52:07 +02006451 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006452 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006453
6454 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006455 if (d_rpl->flags & LYS_SET_MIN) {
6456 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6457 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6458 /* change value */
6459 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6460 } else if (devs[u]->target->nodetype == LYS_LIST) {
6461 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6462 /* change value */
6463 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6464 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006465 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006466 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6467 goto cleanup;
6468 }
6469 if (d_rpl->min) {
6470 devs[u]->target->flags |= LYS_MAND_TRUE;
6471 }
6472 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006473
6474 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006475 if (d_rpl->flags & LYS_SET_MAX) {
6476 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6477 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6478 /* change value */
6479 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6480 } else if (devs[u]->target->nodetype == LYS_LIST) {
6481 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6482 /* change value */
6483 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6484 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006485 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006486 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6487 goto cleanup;
6488 }
6489 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006490
6491 break;
6492 default:
6493 LOGINT(ctx->ctx);
6494 goto cleanup;
6495 }
6496 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006497
Radek Krejci33f72892019-02-21 10:36:58 +01006498 /* final check when all deviations of a single target node are applied */
6499
Radek Krejci551b12c2019-02-19 16:11:21 +01006500 /* check min-max compatibility */
6501 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6502 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6503 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6504 } else if (devs[u]->target->nodetype == LYS_LIST) {
6505 min = ((struct lysc_node_list*)devs[u]->target)->min;
6506 max = ((struct lysc_node_list*)devs[u]->target)->max;
6507 } else {
6508 min = max = 0;
6509 }
6510 if (min > max) {
6511 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 +02006512 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006513 goto cleanup;
6514 }
6515
Radek Krejcia1911222019-07-22 17:24:50 +02006516 if (dflt) {
6517 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006518 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006519 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006520 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6521 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006522 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006523 leaf->dflt->realtype->refcount++;
6524 if (err) {
6525 ly_err_print(err);
6526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6527 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6528 ly_err_free(err);
6529 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006530 if (rc == LY_EINCOMPLETE) {
6531 /* postpone default compilation when the tree is complete */
6532 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6533
6534 /* but in general result is so far ok */
6535 rc = LY_SUCCESS;
6536 }
Radek Krejcia1911222019-07-22 17:24:50 +02006537 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006538 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006539 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6540 int dynamic;
6541 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006542 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006543
6544 /* update the list of incomplete default values if needed */
6545 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6546
6547 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006548 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6549 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6550 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006551 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6552 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006553 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006554 leaf->dflt->realtype->refcount++;
6555 if (err) {
6556 ly_err_print(err);
6557 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6558 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6559 ly_err_free(err);
6560 }
6561 if (dynamic) {
6562 free((void*)dflt);
6563 }
Radek Krejcia1911222019-07-22 17:24:50 +02006564 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006565 if (rc == LY_EINCOMPLETE) {
6566 /* postpone default compilation when the tree is complete */
6567 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6568
6569 /* but in general result is so far ok */
6570 rc = LY_SUCCESS;
6571 }
6572 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006573 } else { /* LYS_LEAFLIST */
6574 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006575 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 +02006576 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6577 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6578 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006579 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6580 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006581 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6582 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006583 llist->dflts[x]->realtype->refcount++;
6584 if (err) {
6585 ly_err_print(err);
6586 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6587 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6588 dflt, err->msg);
6589 ly_err_free(err);
6590 }
6591 if (dynamic) {
6592 free((void*)dflt);
6593 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006594 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006595 if (rc == LY_EINCOMPLETE) {
6596 /* postpone default compilation when the tree is complete */
6597 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6598
6599 /* but in general result is so far ok */
6600 rc = LY_SUCCESS;
6601 }
Radek Krejcia1911222019-07-22 17:24:50 +02006602 LY_CHECK_GOTO(rc, cleanup);
6603 }
6604 }
6605 }
6606
Radek Krejci551b12c2019-02-19 16:11:21 +01006607 /* check mandatory - default compatibility */
6608 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6609 && (devs[u]->target->flags & LYS_SET_DFLT)
6610 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6611 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006612 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006613 goto cleanup;
6614 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6615 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6616 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006617 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 +01006618 goto cleanup;
6619 }
6620 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6621 /* mandatory node under a default case */
6622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006623 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6624 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006625 goto cleanup;
6626 }
Radek Krejci33f72892019-02-21 10:36:58 +01006627
Radek Krejci327de162019-06-14 12:52:07 +02006628 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006629 }
6630
Radek Krejci327de162019-06-14 12:52:07 +02006631 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006632 ret = LY_SUCCESS;
6633
6634cleanup:
6635 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6636 LY_ARRAY_FREE(devs[u]->deviates);
6637 free(devs[u]);
6638 }
6639 free(devs);
6640 ly_set_erase(&targets, NULL);
6641 ly_set_erase(&devs_p, NULL);
6642
6643 return ret;
6644}
6645
Radek Krejcib56c7502019-02-13 14:19:54 +01006646/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006647 * @brief Compile the given YANG submodule into the main module.
6648 * @param[in] ctx Compile context
6649 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006650 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6651 */
6652LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006653lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006654{
6655 unsigned int u;
6656 LY_ERR ret = LY_SUCCESS;
6657 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006658 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006659 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006660 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006661
Radek Krejci0af46292019-01-11 16:02:31 +01006662 if (!mainmod->mod->off_features) {
6663 /* features are compiled directly into the compiled module structure,
6664 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6665 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006666 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6667 LY_CHECK_GOTO(ret, error);
6668 }
6669 if (!mainmod->mod->off_extensions) {
6670 /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */
6671 ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions);
Radek Krejci0af46292019-01-11 16:02:31 +01006672 LY_CHECK_GOTO(ret, error);
6673 }
6674
Radek Krejci327de162019-06-14 12:52:07 +02006675 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006676 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006677 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006678
Radek Krejci474f9b82019-07-24 11:36:37 +02006679 /* data nodes */
6680 LY_LIST_FOR(submod->data, node_p) {
6681 ret = lys_compile_node(ctx, node_p, NULL, 0);
6682 LY_CHECK_GOTO(ret, error);
6683 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006684
Radek Krejciec4da802019-05-02 13:02:41 +02006685 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6686 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006687
Radek Krejcid05cbd92018-12-05 14:26:40 +01006688error:
6689 return ret;
6690}
6691
Radek Krejci335332a2019-09-05 13:03:35 +02006692static void *
6693lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6694{
6695 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6696 if (substmts[u].stmt == stmt) {
6697 return substmts[u].storage;
6698 }
6699 }
6700 return NULL;
6701}
6702
6703LY_ERR
6704lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6705{
6706 LY_ERR ret = LY_EVALID, r;
6707 unsigned int u;
6708 struct lysp_stmt *stmt;
6709 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006710
6711 /* check for invalid substatements */
6712 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006713 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6714 continue;
6715 }
Radek Krejci335332a2019-09-05 13:03:35 +02006716 for (u = 0; substmts[u].stmt; ++u) {
6717 if (substmts[u].stmt == stmt->kw) {
6718 break;
6719 }
6720 }
6721 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006722 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6723 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006724 goto cleanup;
6725 }
Radek Krejci335332a2019-09-05 13:03:35 +02006726 }
6727
Radek Krejciad5963b2019-09-06 16:03:05 +02006728 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6729
Radek Krejci335332a2019-09-05 13:03:35 +02006730 /* keep order of the processing the same as the order in the defined substmts,
6731 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6732 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006733 int stmt_present = 0;
6734
Radek Krejci335332a2019-09-05 13:03:35 +02006735 for (stmt = ext->child; stmt; stmt = stmt->next) {
6736 if (substmts[u].stmt != stmt->kw) {
6737 continue;
6738 }
6739
Radek Krejciad5963b2019-09-06 16:03:05 +02006740 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006741 if (substmts[u].storage) {
6742 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006743 case LY_STMT_STATUS:
6744 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6745 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6746 break;
6747 case LY_STMT_UNITS: {
6748 const char **units;
6749
6750 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6751 /* single item */
6752 if (*((const char **)substmts[u].storage)) {
6753 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6754 goto cleanup;
6755 }
6756 units = (const char **)substmts[u].storage;
6757 } else {
6758 /* sized array */
6759 const char ***units_array = (const char ***)substmts[u].storage;
6760 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6761 }
6762 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6763 break;
6764 }
Radek Krejci335332a2019-09-05 13:03:35 +02006765 case LY_STMT_TYPE: {
6766 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6767 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6768
6769 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6770 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006771 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006772 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6773 goto cleanup;
6774 }
6775 compiled = substmts[u].storage;
6776 } else {
6777 /* sized array */
6778 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6779 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6780 compiled = (void*)type;
6781 }
6782
Radek Krejciad5963b2019-09-06 16:03:05 +02006783 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006784 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6785 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006786 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6787 lysp_type_free(ctx->ctx, parsed);
6788 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006789 break;
6790 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006791 case LY_STMT_IF_FEATURE: {
6792 struct lysc_iffeature *iff = NULL;
6793
6794 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6795 /* single item */
6796 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6797 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6798 goto cleanup;
6799 }
6800 iff = (struct lysc_iffeature*)substmts[u].storage;
6801 } else {
6802 /* sized array */
6803 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6804 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6805 }
6806 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6807 break;
6808 }
6809 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6810 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006811 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006812 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.",
6813 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006814 goto cleanup;
6815 }
6816 }
Radek Krejci335332a2019-09-05 13:03:35 +02006817 }
Radek Krejci335332a2019-09-05 13:03:35 +02006818
Radek Krejciad5963b2019-09-06 16:03:05 +02006819 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6820 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6821 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6822 goto cleanup;
6823 }
Radek Krejci335332a2019-09-05 13:03:35 +02006824 }
6825
6826 ret = LY_SUCCESS;
6827
6828cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006829 return ret;
6830}
6831
Radek Krejci19a96102018-11-15 13:38:09 +01006832LY_ERR
6833lys_compile(struct lys_module *mod, int options)
6834{
6835 struct lysc_ctx ctx = {0};
6836 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006837 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006838 struct lysp_module *sp;
6839 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006840 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006841 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006842 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006843 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006844 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006845
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006846 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006847
6848 if (!mod->implemented) {
6849 /* just imported modules are not compiled */
6850 return LY_SUCCESS;
6851 }
6852
Radek Krejci19a96102018-11-15 13:38:09 +01006853 sp = mod->parsed;
6854
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006855 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006856 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006857 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006858 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006859 ctx.path_len = 1;
6860 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006861
6862 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006863 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6864 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006865
Radek Krejciec4da802019-05-02 13:02:41 +02006866 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006867 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006868 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006869 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6870 }
Radek Krejci0935f412019-08-20 16:15:18 +02006871
6872 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01006873 if (mod->off_features) {
6874 /* there is already precompiled array of features */
6875 mod_c->features = mod->off_features;
6876 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006877 } else {
6878 /* features are compiled directly into the compiled module structure,
6879 * 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 +02006880 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006881 LY_CHECK_GOTO(ret, error);
6882 }
6883 /* finish feature compilation, not only for the main module, but also for the submodules.
6884 * Due to possible forward references, it must be done when all the features (including submodules)
6885 * are present. */
6886 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006887 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006888 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6889 }
Radek Krejci327de162019-06-14 12:52:07 +02006890 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006891 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006892 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006893 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006894 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006895 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6896 }
Radek Krejci327de162019-06-14 12:52:07 +02006897 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006898 }
Radek Krejci327de162019-06-14 12:52:07 +02006899 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006900
Radek Krejci0935f412019-08-20 16:15:18 +02006901 /* extensions */
6902 /* 2-steps: a) prepare compiled structures and ... */
6903 if (mod->off_extensions) {
6904 /* there is already precompiled array of extension definitions */
6905 mod_c->extensions = mod->off_extensions;
6906 mod->off_extensions = NULL;
6907 } else {
6908 /* extension definitions are compiled directly into the compiled module structure */
6909 ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions);
Radek Krejcibfb2e142019-09-09 14:47:44 +02006910 LY_CHECK_GOTO(ret, error);
Radek Krejci0935f412019-08-20 16:15:18 +02006911 }
6912 /* ... b) connect the extension definitions with the appropriate extension plugins */
6913 lys_compile_extension_plugins(mod_c->extensions);
6914
6915 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02006916 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006917 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006918 if (sp->identities) {
6919 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6920 }
Radek Krejci327de162019-06-14 12:52:07 +02006921 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006922
Radek Krejci95710c92019-02-11 15:49:55 +01006923 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006924 LY_LIST_FOR(sp->data, node_p) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02006925 LY_CHECK_GOTO(ret = lys_compile_node(&ctx, node_p, NULL, 0), error);
Radek Krejci19a96102018-11-15 13:38:09 +01006926 }
Radek Krejci95710c92019-02-11 15:49:55 +01006927
Radek Krejciec4da802019-05-02 13:02:41 +02006928 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6929 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006930
Radek Krejci95710c92019-02-11 15:49:55 +01006931 /* augments - sort first to cover augments augmenting other augments */
Radek Krejcid3acfce2019-09-09 14:48:50 +02006932 LY_CHECK_GOTO(ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments), error);
Radek Krejci95710c92019-02-11 15:49:55 +01006933 LY_ARRAY_FOR(augments, u) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02006934 LY_CHECK_GOTO(ret = lys_compile_augment(&ctx, augments[u], NULL), error);
Radek Krejci95710c92019-02-11 15:49:55 +01006935 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006936
Radek Krejci474f9b82019-07-24 11:36:37 +02006937 /* deviations TODO cover deviations from submodules */
Radek Krejcid3acfce2019-09-09 14:48:50 +02006938 LY_CHECK_GOTO(ret = lys_compile_deviations(&ctx, sp), error);
Radek Krejci19a96102018-11-15 13:38:09 +01006939
Radek Krejci0935f412019-08-20 16:15:18 +02006940 /* extension instances TODO cover extension instances from submodules */
6941 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006942
Radek Krejcia3045382018-11-22 14:30:31 +01006943 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006944 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6945 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6946 * 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 +01006947 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006948 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006949 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6950 if (type->basetype == LY_TYPE_LEAFREF) {
6951 /* validate the path */
Radek Krejcid3acfce2019-09-09 14:48:50 +02006952 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type), error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006953 } else if (type->basetype == LY_TYPE_UNION) {
6954 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6955 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6956 /* validate the path */
Radek Krejcid3acfce2019-09-09 14:48:50 +02006957 LY_CHECK_GOTO(ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6958 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]),
6959 error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006960 }
6961 }
Radek Krejcia3045382018-11-22 14:30:31 +01006962 }
6963 }
6964 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006965 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006966 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006967 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6968 if (type->basetype == LY_TYPE_LEAFREF) {
6969 /* store pointer to the real type */
6970 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6971 typeiter->basetype == LY_TYPE_LEAFREF;
6972 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6973 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006974 } else if (type->basetype == LY_TYPE_UNION) {
6975 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6976 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6977 /* store pointer to the real type */
6978 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6979 typeiter->basetype == LY_TYPE_LEAFREF;
6980 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6981 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6982 }
6983 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006984 }
6985 }
6986 }
Radek Krejciec4da802019-05-02 13:02:41 +02006987
Radek Krejci474f9b82019-07-24 11:36:37 +02006988 /* finish incomplete default values compilation */
6989 for (u = 0; u < ctx.dflts.count; ++u) {
6990 struct ly_err_item *err = NULL;
6991 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
6992 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized),
6993 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
6994 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
6995 if (err) {
6996 ly_err_print(err);
6997 ctx.path[0] = '\0';
6998 lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
6999 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
7000 "Invalid default - value does not fit the type (%s).", err->msg);
7001 ly_err_free(err);
7002 }
7003 LY_CHECK_GOTO(ret, error);
7004 }
7005
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007006 /* validate non-instantiated groupings from the parsed schema,
7007 * without it we would accept even the schemas with invalid grouping specification */
7008 ctx.options |= LYSC_OPT_GROUPING;
7009 LY_ARRAY_FOR(sp->groupings, u) {
7010 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007011 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007012 }
7013 }
7014 LY_LIST_FOR(sp->data, node_p) {
7015 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7016 LY_ARRAY_FOR(grps, u) {
7017 if (!(grps[u].flags & LYS_USED_GRP)) {
Radek Krejcid3acfce2019-09-09 14:48:50 +02007018 LY_CHECK_GOTO(ret = lys_compile_grouping(&ctx, node_p, &grps[u]), error);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007019 }
7020 }
7021 }
7022
Radek Krejci474f9b82019-07-24 11:36:37 +02007023 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7024 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7025 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7026 }
7027
Radek Krejci1c0c3442019-07-23 16:08:47 +02007028 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007029 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007030 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007031 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007032 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007033
Radek Krejciec4da802019-05-02 13:02:41 +02007034 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01007035 lysp_module_free(mod->parsed);
7036 ((struct lys_module*)mod)->parsed = NULL;
7037 }
7038
Radek Krejciec4da802019-05-02 13:02:41 +02007039 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007040 /* remove flag of the modules implemented by dependency */
7041 for (u = 0; u < ctx.ctx->list.count; ++u) {
7042 m = ctx.ctx->list.objs[u];
7043 if (m->implemented == 2) {
7044 m->implemented = 1;
7045 }
7046 }
7047 }
7048
Radek Krejci19a96102018-11-15 13:38:09 +01007049 ((struct lys_module*)mod)->compiled = mod_c;
7050 return LY_SUCCESS;
7051
7052error:
Radek Krejci95710c92019-02-11 15:49:55 +01007053 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007054 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007055 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007056 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007057 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007058 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007059 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007060 mod->compiled = NULL;
7061
7062 /* revert compilation of modules implemented by dependency */
7063 for (u = 0; u < ctx.ctx->list.count; ++u) {
7064 m = ctx.ctx->list.objs[u];
7065 if (m->implemented == 2) {
7066 /* revert features list to the precompiled state */
7067 lys_feature_precompile_revert(&ctx, m);
7068 /* mark module as imported-only / not-implemented */
7069 m->implemented = 0;
7070 /* free the compiled version of the module */
7071 lysc_module_free(m->compiled, NULL);
7072 m->compiled = NULL;
7073 }
7074 }
7075
Radek Krejci19a96102018-11-15 13:38:09 +01007076 return ret;
7077}