blob: 4dd050a2aa1dd2c6ebe11912a68773d76a48ae9b [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);
810 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
811 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
812 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci0935f412019-08-20 16:15:18 +0200813 COMPILE_EXTS_GOTO(ctx, when_p->exts, (*when)->exts, (*when), LYEXT_PAR_WHEN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100814
815done:
816 return ret;
817}
818
Radek Krejcib56c7502019-02-13 14:19:54 +0100819/**
820 * @brief Compile information from the must statement
821 * @param[in] ctx Compile context.
822 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100823 * @param[in,out] must Prepared (empty) compiled must structure to fill.
824 * @return LY_ERR value.
825 */
Radek Krejci19a96102018-11-15 13:38:09 +0100826static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200827lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100828{
Radek Krejci19a96102018-11-15 13:38:09 +0100829 LY_ERR ret = LY_SUCCESS;
830
831 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
832 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200833 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100834 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
835 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100836 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
837 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejci0935f412019-08-20 16:15:18 +0200838 COMPILE_EXTS_GOTO(ctx, must_p->exts, must->exts, must, LYEXT_PAR_MUST, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100839
840done:
841 return ret;
842}
843
Radek Krejcib56c7502019-02-13 14:19:54 +0100844/**
845 * @brief Compile information from the import statement
846 * @param[in] ctx Compile context.
847 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100848 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
849 * @return LY_ERR value.
850 */
Radek Krejci19a96102018-11-15 13:38:09 +0100851static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200852lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100853{
Radek Krejci19a96102018-11-15 13:38:09 +0100854 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100855 LY_ERR ret = LY_SUCCESS;
856
857 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejci0935f412019-08-20 16:15:18 +0200858 COMPILE_EXTS_GOTO(ctx, imp_p->exts, imp->exts, imp, LYEXT_PAR_IMPORT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100859 imp->module = imp_p->module;
860
Radek Krejci7f2a5362018-11-28 13:05:37 +0100861 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
862 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100863 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100864 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100865 /* try to use filepath if present */
866 if (imp->module->filepath) {
867 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
868 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100869 if (mod != imp->module) {
870 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100871 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100872 mod = NULL;
873 }
874 }
875 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100876 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100877 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 +0100878 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100879 return LY_ENOTFOUND;
880 }
881 }
Radek Krejci19a96102018-11-15 13:38:09 +0100882 }
883
884done:
885 return ret;
886}
887
Radek Krejcib56c7502019-02-13 14:19:54 +0100888/**
889 * @brief Compile information from the identity statement
890 *
891 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
892 *
893 * @param[in] ctx Compile context.
894 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100895 * @param[in] idents List of so far compiled identities to check the name uniqueness.
896 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
897 * @return LY_ERR value.
898 */
Radek Krejci19a96102018-11-15 13:38:09 +0100899static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200900lys_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 +0100901{
902 unsigned int u;
903 LY_ERR ret = LY_SUCCESS;
904
Radek Krejci327de162019-06-14 12:52:07 +0200905 lysc_update_path(ctx, NULL, ident_p->name);
906
Radek Krejcid05cbd92018-12-05 14:26:40 +0100907 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100908 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200909 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
910 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200911 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200912 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100913 /* 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 +0200914 COMPILE_EXTS_GOTO(ctx, ident_p->exts, ident->exts, ident, LYEXT_PAR_IDENT, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100915 ident->flags = ident_p->flags;
916
Radek Krejci327de162019-06-14 12:52:07 +0200917 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100918done:
919 return ret;
920}
921
Radek Krejcib56c7502019-02-13 14:19:54 +0100922/**
923 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
924 *
925 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
926 *
927 * @param[in] ctx Compile context for logging.
928 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
929 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
930 * @return LY_SUCCESS if everything is ok.
931 * @return LY_EVALID if the identity is derived from itself.
932 */
Radek Krejci38222632019-02-12 16:55:05 +0100933static LY_ERR
934lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
935{
936 LY_ERR ret = LY_EVALID;
937 unsigned int u, v;
938 struct ly_set recursion = {0};
939 struct lysc_ident *drv;
940
941 if (!derived) {
942 return LY_SUCCESS;
943 }
944
945 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
946 if (ident == derived[u]) {
947 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
948 "Identity \"%s\" is indirectly derived from itself.", ident->name);
949 goto cleanup;
950 }
951 ly_set_add(&recursion, derived[u], 0);
952 }
953
954 for (v = 0; v < recursion.count; ++v) {
955 drv = recursion.objs[v];
956 if (!drv->derived) {
957 continue;
958 }
959 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
960 if (ident == drv->derived[u]) {
961 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
962 "Identity \"%s\" is indirectly derived from itself.", ident->name);
963 goto cleanup;
964 }
965 ly_set_add(&recursion, drv->derived[u], 0);
966 }
967 }
968 ret = LY_SUCCESS;
969
970cleanup:
971 ly_set_erase(&recursion, NULL);
972 return ret;
973}
974
Radek Krejcia3045382018-11-22 14:30:31 +0100975/**
976 * @brief Find and process the referenced base identities from another identity or identityref
977 *
Radek Krejciaca74032019-06-04 08:53:06 +0200978 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +0100979 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
980 * to distinguish these two use cases.
981 *
982 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
983 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
984 * @param[in] ident Referencing identity to work with.
985 * @param[in] bases Array of bases of identityref to fill in.
986 * @return LY_ERR value.
987 */
Radek Krejci19a96102018-11-15 13:38:09 +0100988static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100989lys_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 +0100990{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100991 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100992 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100993 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100994 struct lysc_ident **idref;
995
996 assert(ident || bases);
997
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100998 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100999 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1000 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
1001 return LY_EVALID;
1002 }
1003
1004 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
1005 s = strchr(bases_p[u], ':');
1006 if (s) {
1007 /* prefixed identity */
1008 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +01001009 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001010 } else {
1011 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +01001012 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +01001013 }
1014 if (!mod) {
1015 if (ident) {
1016 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1017 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
1018 } else {
1019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1020 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
1021 }
1022 return LY_EVALID;
1023 }
1024 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01001025 if (mod->compiled && mod->compiled->identities) {
1026 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
1027 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01001028 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +01001029 if (ident == &mod->compiled->identities[v]) {
1030 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1031 "Identity \"%s\" is derived from itself.", ident->name);
1032 return LY_EVALID;
1033 }
1034 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +01001035 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +01001036 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001037 *idref = ident;
1038 } else {
1039 /* we have match! store the found identity */
1040 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01001041 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +01001042 }
1043 break;
1044 }
1045 }
1046 }
1047 if (!idref || !(*idref)) {
1048 if (ident) {
1049 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1050 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
1051 } else {
1052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1053 "Unable to find base (%s) of identityref.", bases_p[u]);
1054 }
1055 return LY_EVALID;
1056 }
1057 }
1058 return LY_SUCCESS;
1059}
1060
Radek Krejcia3045382018-11-22 14:30:31 +01001061/**
1062 * @brief For the given array of identities, set the backlinks from all their base identities.
1063 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
1064 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
1065 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
1066 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1067 */
Radek Krejci555cb5b2018-11-16 14:54:33 +01001068static LY_ERR
1069lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
1070{
1071 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +01001072
1073 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
1074 if (!idents_p[i].bases) {
1075 continue;
1076 }
Radek Krejci327de162019-06-14 12:52:07 +02001077 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +01001078 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +02001079 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01001080 }
1081 return LY_SUCCESS;
1082}
1083
Radek Krejci0af46292019-01-11 16:02:31 +01001084LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02001085lys_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 +01001086{
1087 unsigned int offset = 0, u;
1088 struct lysc_ctx context = {0};
1089
Radek Krejci327de162019-06-14 12:52:07 +02001090 assert(ctx_sc || ctx);
1091
1092 if (!ctx_sc) {
1093 context.ctx = ctx;
1094 context.mod = module;
1095 context.path_len = 1;
1096 context.path[0] = '/';
1097 ctx_sc = &context;
1098 }
Radek Krejci0af46292019-01-11 16:02:31 +01001099
1100 if (!features_p) {
1101 return LY_SUCCESS;
1102 }
1103 if (*features) {
1104 offset = LY_ARRAY_SIZE(*features);
1105 }
1106
Radek Krejci327de162019-06-14 12:52:07 +02001107 lysc_update_path(ctx_sc, NULL, "{feature}");
1108 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +01001109 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +02001110 lysc_update_path(ctx_sc, NULL, features_p[u].name);
1111
Radek Krejci0af46292019-01-11 16:02:31 +01001112 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +02001113 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
1114 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
1115 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
1116 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +01001117 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +02001118 (*features)[offset + u].module = ctx_sc->mod;
1119
1120 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001121 }
Radek Krejci327de162019-06-14 12:52:07 +02001122 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001123
1124 return LY_SUCCESS;
1125}
1126
Radek Krejcia3045382018-11-22 14:30:31 +01001127/**
Radek Krejci09a1fc52019-02-13 10:55:17 +01001128 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +01001129 *
1130 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
1131 *
Radek Krejci09a1fc52019-02-13 10:55:17 +01001132 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +01001133 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
1134 * being currently processed).
1135 * @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 +01001136 * @return LY_SUCCESS if everything is ok.
1137 * @return LY_EVALID if the feature references indirectly itself.
1138 */
1139static LY_ERR
1140lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1141{
1142 LY_ERR ret = LY_EVALID;
1143 unsigned int u, v;
1144 struct ly_set recursion = {0};
1145 struct lysc_feature *drv;
1146
1147 if (!depfeatures) {
1148 return LY_SUCCESS;
1149 }
1150
1151 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1152 if (feature == depfeatures[u]) {
1153 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1154 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1155 goto cleanup;
1156 }
1157 ly_set_add(&recursion, depfeatures[u], 0);
1158 }
1159
1160 for (v = 0; v < recursion.count; ++v) {
1161 drv = recursion.objs[v];
1162 if (!drv->depfeatures) {
1163 continue;
1164 }
1165 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1166 if (feature == drv->depfeatures[u]) {
1167 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1168 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1169 goto cleanup;
1170 }
1171 ly_set_add(&recursion, drv->depfeatures[u], 0);
1172 }
1173 }
1174 ret = LY_SUCCESS;
1175
1176cleanup:
1177 ly_set_erase(&recursion, NULL);
1178 return ret;
1179}
1180
1181/**
Radek Krejci0af46292019-01-11 16:02:31 +01001182 * @brief Create pre-compiled features array.
1183 *
1184 * See lys_feature_precompile() for more details.
1185 *
Radek Krejcia3045382018-11-22 14:30:31 +01001186 * @param[in] ctx Compile context.
1187 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001188 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001189 * @return LY_ERR value.
1190 */
Radek Krejci19a96102018-11-15 13:38:09 +01001191static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001192lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001193{
Radek Krejci0af46292019-01-11 16:02:31 +01001194 unsigned int u, v, x;
1195 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001196 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001197
Radek Krejci327de162019-06-14 12:52:07 +02001198
Radek Krejci0af46292019-01-11 16:02:31 +01001199 /* find the preprecompiled feature */
1200 LY_ARRAY_FOR(features, x) {
1201 if (strcmp(features[x].name, feature_p->name)) {
1202 continue;
1203 }
1204 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001205 lysc_update_path(ctx, NULL, "{feature}");
1206 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001207
Radek Krejci0af46292019-01-11 16:02:31 +01001208 /* finish compilation started in lys_feature_precompile() */
Radek Krejci0935f412019-08-20 16:15:18 +02001209 COMPILE_EXTS_GOTO(ctx, feature_p->exts, feature->exts, feature, LYEXT_PAR_FEATURE, ret, done);
Radek Krejciec4da802019-05-02 13:02:41 +02001210 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001211 if (feature->iffeatures) {
1212 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1213 if (feature->iffeatures[u].features) {
1214 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001215 /* check for circular dependency - direct reference first,... */
1216 if (feature == feature->iffeatures[u].features[v]) {
1217 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1218 "Feature \"%s\" is referenced from itself.", feature->name);
1219 return LY_EVALID;
1220 }
1221 /* ... and indirect circular reference */
1222 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1223
Radek Krejci0af46292019-01-11 16:02:31 +01001224 /* add itself into the dependants list */
1225 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1226 *df = feature;
1227 }
Radek Krejci19a96102018-11-15 13:38:09 +01001228 }
Radek Krejci19a96102018-11-15 13:38:09 +01001229 }
1230 }
Radek Krejci327de162019-06-14 12:52:07 +02001231 lysc_update_path(ctx, NULL, NULL);
1232 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001233 done:
1234 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001235 }
Radek Krejci0af46292019-01-11 16:02:31 +01001236
1237 LOGINT(ctx->ctx);
1238 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001239}
1240
Radek Krejcib56c7502019-02-13 14:19:54 +01001241/**
1242 * @brief Revert compiled list of features back to the precompiled state.
1243 *
1244 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1245 * The features are supposed to be stored again as off_features in ::lys_module structure.
1246 *
1247 * @param[in] ctx Compilation context.
1248 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1249 * and supposed to hold the off_features list.
1250 */
Radek Krejci95710c92019-02-11 15:49:55 +01001251static void
1252lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1253{
1254 unsigned int u, v;
1255
1256 /* keep the off_features list until the complete lys_module is freed */
1257 mod->off_features = mod->compiled->features;
1258 mod->compiled->features = NULL;
1259
1260 /* in the off_features list, remove all the parts (from finished compiling process)
1261 * which may points into the data being freed here */
1262 LY_ARRAY_FOR(mod->off_features, u) {
1263 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1264 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1265 }
1266 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1267 mod->off_features[u].iffeatures = NULL;
1268
1269 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1270 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1271 }
1272 LY_ARRAY_FREE(mod->off_features[u].exts);
1273 mod->off_features[u].exts = NULL;
1274 }
1275}
1276
Radek Krejcia3045382018-11-22 14:30:31 +01001277/**
1278 * @brief Validate and normalize numeric value from a range definition.
1279 * @param[in] ctx Compile context.
1280 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1281 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1282 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1283 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1284 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1285 * @param[in] value String value of the range boundary.
1286 * @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.
1287 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1288 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1289 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001290LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001291range_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 +01001292{
Radek Krejci6cba4292018-11-15 17:33:29 +01001293 size_t fraction = 0, size;
1294
Radek Krejci19a96102018-11-15 13:38:09 +01001295 *len = 0;
1296
1297 assert(value);
1298 /* parse value */
1299 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1300 return LY_EVALID;
1301 }
1302
1303 if ((value[*len] == '-') || (value[*len] == '+')) {
1304 ++(*len);
1305 }
1306
1307 while (isdigit(value[*len])) {
1308 ++(*len);
1309 }
1310
1311 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001312 if (basetype == LY_TYPE_DEC64) {
1313 goto decimal;
1314 } else {
1315 *valcopy = strndup(value, *len);
1316 return LY_SUCCESS;
1317 }
Radek Krejci19a96102018-11-15 13:38:09 +01001318 }
1319 fraction = *len;
1320
1321 ++(*len);
1322 while (isdigit(value[*len])) {
1323 ++(*len);
1324 }
1325
Radek Krejci6cba4292018-11-15 17:33:29 +01001326 if (basetype == LY_TYPE_DEC64) {
1327decimal:
1328 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001329 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001330 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1331 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1332 *len, value, frdigits);
1333 return LY_EINVAL;
1334 }
1335 if (fraction) {
1336 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1337 } else {
1338 size = (*len) + frdigits + 1;
1339 }
1340 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001341 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1342
Radek Krejci6cba4292018-11-15 17:33:29 +01001343 (*valcopy)[size - 1] = '\0';
1344 if (fraction) {
1345 memcpy(&(*valcopy)[0], &value[0], fraction);
1346 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1347 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1348 } else {
1349 memcpy(&(*valcopy)[0], &value[0], *len);
1350 memset(&(*valcopy)[*len], '0', frdigits);
1351 }
Radek Krejci19a96102018-11-15 13:38:09 +01001352 }
1353 return LY_SUCCESS;
1354}
1355
Radek Krejcia3045382018-11-22 14:30:31 +01001356/**
1357 * @brief Check that values in range are in ascendant order.
1358 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001359 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1360 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001361 * @param[in] value Current value to check.
1362 * @param[in] prev_value The last seen value.
1363 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1364 */
Radek Krejci19a96102018-11-15 13:38:09 +01001365static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001366range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001367{
1368 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001369 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001370 return LY_EEXIST;
1371 }
1372 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001373 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001374 return LY_EEXIST;
1375 }
1376 }
1377 return LY_SUCCESS;
1378}
1379
Radek Krejcia3045382018-11-22 14:30:31 +01001380/**
1381 * @brief Set min/max value of the range part.
1382 * @param[in] ctx Compile context.
1383 * @param[in] part Range part structure to fill.
1384 * @param[in] max Flag to distinguish if storing min or max value.
1385 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1386 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1387 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1388 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1389 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001390 * @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 +01001391 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1392 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1393 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1394 * frdigits value), LY_EMEM.
1395 */
Radek Krejci19a96102018-11-15 13:38:09 +01001396static LY_ERR
1397range_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 +01001398 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001399{
1400 LY_ERR ret = LY_SUCCESS;
1401 char *valcopy = NULL;
1402 size_t len;
1403
1404 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001405 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001406 LY_CHECK_GOTO(ret, finalize);
1407 }
1408 if (!valcopy && base_range) {
1409 if (max) {
1410 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1411 } else {
1412 part->min_64 = base_range->parts[0].min_64;
1413 }
1414 if (!first) {
1415 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1416 }
1417 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001418 }
1419
1420 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001421 case LY_TYPE_INT8: /* range */
1422 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001423 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 +01001424 } else if (max) {
1425 part->max_64 = INT64_C(127);
1426 } else {
1427 part->min_64 = INT64_C(-128);
1428 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001429 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001430 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001431 }
1432 break;
1433 case LY_TYPE_INT16: /* range */
1434 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001435 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 +01001436 } else if (max) {
1437 part->max_64 = INT64_C(32767);
1438 } else {
1439 part->min_64 = INT64_C(-32768);
1440 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001441 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001442 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001443 }
1444 break;
1445 case LY_TYPE_INT32: /* range */
1446 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001447 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 +01001448 } else if (max) {
1449 part->max_64 = INT64_C(2147483647);
1450 } else {
1451 part->min_64 = INT64_C(-2147483648);
1452 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001453 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001454 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001455 }
1456 break;
1457 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001458 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001459 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001460 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001461 max ? &part->max_64 : &part->min_64);
1462 } else if (max) {
1463 part->max_64 = INT64_C(9223372036854775807);
1464 } else {
1465 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1466 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001467 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001468 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001469 }
1470 break;
1471 case LY_TYPE_UINT8: /* range */
1472 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001473 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 +01001474 } else if (max) {
1475 part->max_u64 = UINT64_C(255);
1476 } else {
1477 part->min_u64 = UINT64_C(0);
1478 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001479 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001480 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001481 }
1482 break;
1483 case LY_TYPE_UINT16: /* range */
1484 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001485 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 +01001486 } else if (max) {
1487 part->max_u64 = UINT64_C(65535);
1488 } else {
1489 part->min_u64 = UINT64_C(0);
1490 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001491 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001492 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001493 }
1494 break;
1495 case LY_TYPE_UINT32: /* range */
1496 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001497 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 +01001498 } else if (max) {
1499 part->max_u64 = UINT64_C(4294967295);
1500 } else {
1501 part->min_u64 = UINT64_C(0);
1502 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001503 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001504 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001505 }
1506 break;
1507 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001508 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001509 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001510 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001511 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 +01001512 } else if (max) {
1513 part->max_u64 = UINT64_C(18446744073709551615);
1514 } else {
1515 part->min_u64 = UINT64_C(0);
1516 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001517 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001518 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001519 }
1520 break;
1521 default:
1522 LOGINT(ctx->ctx);
1523 ret = LY_EINT;
1524 }
1525
Radek Krejci5969f272018-11-23 10:03:58 +01001526finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001527 if (ret == LY_EDENIED) {
1528 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1529 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1530 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1531 } else if (ret == LY_EVALID) {
1532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1533 "Invalid %s restriction - invalid value \"%s\".",
1534 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1535 } else if (ret == LY_EEXIST) {
1536 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1537 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001538 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001539 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001540 } else if (!ret && value) {
1541 *value = *value + len;
1542 }
1543 free(valcopy);
1544 return ret;
1545}
1546
Radek Krejcia3045382018-11-22 14:30:31 +01001547/**
1548 * @brief Compile the parsed range restriction.
1549 * @param[in] ctx Compile context.
1550 * @param[in] range_p Parsed range structure to compile.
1551 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1552 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1553 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1554 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1555 * range restriction must be more restrictive than the base_range.
1556 * @param[in,out] range Pointer to the created current range structure.
1557 * @return LY_ERR value.
1558 */
Radek Krejci19a96102018-11-15 13:38:09 +01001559static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001560lys_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 +01001561 struct lysc_range *base_range, struct lysc_range **range)
1562{
1563 LY_ERR ret = LY_EVALID;
1564 const char *expr;
1565 struct lysc_range_part *parts = NULL, *part;
1566 int range_expected = 0, uns;
1567 unsigned int parts_done = 0, u, v;
1568
1569 assert(range);
1570 assert(range_p);
1571
1572 expr = range_p->arg;
1573 while(1) {
1574 if (isspace(*expr)) {
1575 ++expr;
1576 } else if (*expr == '\0') {
1577 if (range_expected) {
1578 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1579 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1580 length_restr ? "length" : "range", range_p->arg);
1581 goto cleanup;
1582 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1583 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1584 "Invalid %s restriction - unexpected end of the expression (%s).",
1585 length_restr ? "length" : "range", range_p->arg);
1586 goto cleanup;
1587 }
1588 parts_done++;
1589 break;
1590 } else if (!strncmp(expr, "min", 3)) {
1591 if (parts) {
1592 /* min cannot be used elsewhere than in the first part */
1593 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1594 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1595 expr - range_p->arg, range_p->arg);
1596 goto cleanup;
1597 }
1598 expr += 3;
1599
1600 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001601 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 +01001602 part->max_64 = part->min_64;
1603 } else if (*expr == '|') {
1604 if (!parts || range_expected) {
1605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1606 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1607 goto cleanup;
1608 }
1609 expr++;
1610 parts_done++;
1611 /* process next part of the expression */
1612 } else if (!strncmp(expr, "..", 2)) {
1613 expr += 2;
1614 while (isspace(*expr)) {
1615 expr++;
1616 }
1617 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1618 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1619 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1620 goto cleanup;
1621 }
1622 /* continue expecting the upper boundary */
1623 range_expected = 1;
1624 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1625 /* number */
1626 if (range_expected) {
1627 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001628 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 +01001629 range_expected = 0;
1630 } else {
1631 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1632 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 +01001633 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001634 part->max_64 = part->min_64;
1635 }
1636
1637 /* continue with possible another expression part */
1638 } else if (!strncmp(expr, "max", 3)) {
1639 expr += 3;
1640 while (isspace(*expr)) {
1641 expr++;
1642 }
1643 if (*expr != '\0') {
1644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1645 length_restr ? "length" : "range", expr);
1646 goto cleanup;
1647 }
1648 if (range_expected) {
1649 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001650 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 +01001651 range_expected = 0;
1652 } else {
1653 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1654 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 +01001655 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001656 part->min_64 = part->max_64;
1657 }
1658 } else {
1659 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1660 length_restr ? "length" : "range", expr);
1661 goto cleanup;
1662 }
1663 }
1664
1665 /* check with the previous range/length restriction */
1666 if (base_range) {
1667 switch (basetype) {
1668 case LY_TYPE_BINARY:
1669 case LY_TYPE_UINT8:
1670 case LY_TYPE_UINT16:
1671 case LY_TYPE_UINT32:
1672 case LY_TYPE_UINT64:
1673 case LY_TYPE_STRING:
1674 uns = 1;
1675 break;
1676 case LY_TYPE_DEC64:
1677 case LY_TYPE_INT8:
1678 case LY_TYPE_INT16:
1679 case LY_TYPE_INT32:
1680 case LY_TYPE_INT64:
1681 uns = 0;
1682 break;
1683 default:
1684 LOGINT(ctx->ctx);
1685 ret = LY_EINT;
1686 goto cleanup;
1687 }
1688 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1689 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1690 goto baseerror;
1691 }
1692 /* current lower bound is not lower than the base */
1693 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1694 /* base has single value */
1695 if (base_range->parts[v].min_64 == parts[u].min_64) {
1696 /* both lower bounds are the same */
1697 if (parts[u].min_64 != parts[u].max_64) {
1698 /* current continues with a range */
1699 goto baseerror;
1700 } else {
1701 /* equal single values, move both forward */
1702 ++v;
1703 continue;
1704 }
1705 } else {
1706 /* base is single value lower than current range, so the
1707 * value from base range is removed in the current,
1708 * move only base and repeat checking */
1709 ++v;
1710 --u;
1711 continue;
1712 }
1713 } else {
1714 /* base is the range */
1715 if (parts[u].min_64 == parts[u].max_64) {
1716 /* current is a single value */
1717 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1718 /* current is behind the base range, so base range is omitted,
1719 * move the base and keep the current for further check */
1720 ++v;
1721 --u;
1722 } /* else it is within the base range, so move the current, but keep the base */
1723 continue;
1724 } else {
1725 /* both are ranges - check the higher bound, the lower was already checked */
1726 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1727 /* higher bound is higher than the current higher bound */
1728 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1729 /* but the current lower bound is also higher, so the base range is omitted,
1730 * continue with the same current, but move the base */
1731 --u;
1732 ++v;
1733 continue;
1734 }
1735 /* current range starts within the base range but end behind it */
1736 goto baseerror;
1737 } else {
1738 /* current range is smaller than the base,
1739 * move current, but stay with the base */
1740 continue;
1741 }
1742 }
1743 }
1744 }
1745 if (u != parts_done) {
1746baseerror:
1747 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1748 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1749 length_restr ? "length" : "range", range_p->arg);
1750 goto cleanup;
1751 }
1752 }
1753
1754 if (!(*range)) {
1755 *range = calloc(1, sizeof **range);
1756 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1757 }
1758
Radek Krejcic8b31002019-01-08 10:24:45 +01001759 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001760 if (range_p->eapptag) {
1761 lydict_remove(ctx->ctx, (*range)->eapptag);
1762 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1763 }
1764 if (range_p->emsg) {
1765 lydict_remove(ctx->ctx, (*range)->emsg);
1766 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1767 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001768 if (range_p->dsc) {
1769 lydict_remove(ctx->ctx, (*range)->dsc);
1770 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1771 }
1772 if (range_p->ref) {
1773 lydict_remove(ctx->ctx, (*range)->ref);
1774 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1775 }
Radek Krejci19a96102018-11-15 13:38:09 +01001776 /* extensions are taken only from the last range by the caller */
1777
1778 (*range)->parts = parts;
1779 parts = NULL;
1780 ret = LY_SUCCESS;
1781cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001782 LY_ARRAY_FREE(parts);
1783
1784 return ret;
1785}
1786
1787/**
1788 * @brief Checks pattern syntax.
1789 *
Radek Krejcia3045382018-11-22 14:30:31 +01001790 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001791 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001792 * @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 +01001793 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001794 */
1795static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001796lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001797{
Radek Krejci54579462019-04-30 12:47:06 +02001798 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001799 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001800 int err_code;
1801 const char *orig_ptr;
1802 PCRE2_SIZE err_offset;
1803 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001804#define URANGE_LEN 19
1805 char *ublock2urange[][2] = {
1806 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1807 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1808 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1809 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1810 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1811 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1812 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1813 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1814 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1815 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1816 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1817 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1818 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1819 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1820 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1821 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1822 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1823 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1824 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1825 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1826 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1827 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1828 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1829 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1830 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1831 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1832 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1833 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1834 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1835 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1836 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1837 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1838 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1839 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1840 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1841 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1842 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1843 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1844 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1845 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1846 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1847 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1848 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1849 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1850 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1851 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1852 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1853 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1854 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1855 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1856 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1857 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1858 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1859 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1860 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1861 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1862 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1863 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1864 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1865 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1866 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1867 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1868 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1869 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1870 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1871 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1872 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1873 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1874 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1875 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1876 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1877 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1878 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1879 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1880 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1881 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1882 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1883 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1884 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1885 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1886 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1887 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1888 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1889 {NULL, NULL}
1890 };
1891
1892 /* adjust the expression to a Perl equivalent
1893 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1894
1895 /* we need to replace all "$" with "\$", count them now */
1896 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1897
1898 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1899 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1900 perl_regex[0] = '\0';
1901
1902 ptr = perl_regex;
1903
Radek Krejci19a96102018-11-15 13:38:09 +01001904 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1905 if (orig_ptr[0] == '$') {
1906 ptr += sprintf(ptr, "\\$");
1907 } else if (orig_ptr[0] == '^') {
1908 ptr += sprintf(ptr, "\\^");
1909 } else {
1910 ptr[0] = orig_ptr[0];
1911 ++ptr;
1912 }
1913 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001914 ptr[0] = '\0';
1915 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001916
1917 /* substitute Unicode Character Blocks with exact Character Ranges */
1918 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1919 start = ptr - perl_regex;
1920
1921 ptr = strchr(ptr, '}');
1922 if (!ptr) {
1923 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1924 pattern, perl_regex + start + 2, "unterminated character property");
1925 free(perl_regex);
1926 return LY_EVALID;
1927 }
1928 end = (ptr - perl_regex) + 1;
1929
1930 /* need more space */
1931 if (end - start < URANGE_LEN) {
1932 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1933 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1934 }
1935
1936 /* find our range */
1937 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1938 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1939 break;
1940 }
1941 }
1942 if (!ublock2urange[idx][0]) {
1943 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1944 pattern, perl_regex + start + 5, "unknown block name");
1945 free(perl_regex);
1946 return LY_EVALID;
1947 }
1948
1949 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1950 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1951 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1952 ++count;
1953 }
1954 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1955 --count;
1956 }
1957 }
1958 if (count) {
1959 /* skip brackets */
1960 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1961 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1962 } else {
1963 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1964 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1965 }
1966 }
1967
1968 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02001969 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1970 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02001971 &err_code, &err_offset, NULL);
1972 if (!code_local) {
1973 PCRE2_UCHAR err_msg[256] = {0};
1974 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001975 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1976 free(perl_regex);
1977 return LY_EVALID;
1978 }
1979 free(perl_regex);
1980
Radek Krejci54579462019-04-30 12:47:06 +02001981 if (code) {
1982 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001983 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001984 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001985 }
1986
1987 return LY_SUCCESS;
1988
1989#undef URANGE_LEN
1990}
1991
Radek Krejcia3045382018-11-22 14:30:31 +01001992/**
1993 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1994 * @param[in] ctx Compile context.
1995 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001996 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1997 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1998 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1999 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
2000 */
Radek Krejci19a96102018-11-15 13:38:09 +01002001static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002002lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01002003 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
2004{
2005 struct lysc_pattern **pattern;
Radek Krejci0935f412019-08-20 16:15:18 +02002006 unsigned int u;
Radek Krejci19a96102018-11-15 13:38:09 +01002007 LY_ERR ret = LY_SUCCESS;
2008
2009 /* first, copy the patterns from the base type */
2010 if (base_patterns) {
2011 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
2012 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
2013 }
2014
2015 LY_ARRAY_FOR(patterns_p, u) {
2016 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
2017 *pattern = calloc(1, sizeof **pattern);
2018 ++(*pattern)->refcount;
2019
Radek Krejci54579462019-04-30 12:47:06 +02002020 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01002021 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01002022
2023 if (patterns_p[u].arg[0] == 0x15) {
2024 (*pattern)->inverted = 1;
2025 }
Radek Krejci54579462019-04-30 12:47:06 +02002026 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01002027 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
2028 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01002029 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
2030 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejci0935f412019-08-20 16:15:18 +02002031 COMPILE_EXTS_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, (*pattern), LYEXT_PAR_PATTERN, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002032 }
2033done:
2034 return ret;
2035}
2036
Radek Krejcia3045382018-11-22 14:30:31 +01002037/**
2038 * @brief map of the possible restrictions combination for the specific built-in type.
2039 */
Radek Krejci19a96102018-11-15 13:38:09 +01002040static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
2041 0 /* LY_TYPE_UNKNOWN */,
2042 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01002043 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
2044 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
2045 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
2046 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
2047 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01002048 LYS_SET_BIT /* LY_TYPE_BITS */,
2049 0 /* LY_TYPE_BOOL */,
2050 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
2051 0 /* LY_TYPE_EMPTY */,
2052 LYS_SET_ENUM /* LY_TYPE_ENUM */,
2053 LYS_SET_BASE /* LY_TYPE_IDENT */,
2054 LYS_SET_REQINST /* LY_TYPE_INST */,
2055 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01002056 LYS_SET_TYPE /* LY_TYPE_UNION */,
2057 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002058 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01002059 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01002060 LYS_SET_RANGE /* LY_TYPE_INT64 */
2061};
2062
2063/**
2064 * @brief stringification of the YANG built-in data types
2065 */
2066const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
2067 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
2068 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01002069};
2070
Radek Krejcia3045382018-11-22 14:30:31 +01002071/**
2072 * @brief Compile parsed type's enum structures (for enumeration and bits types).
2073 * @param[in] ctx Compile context.
2074 * @param[in] enums_p Array of the parsed enum structures to compile.
2075 * @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 +01002076 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
2077 * @param[out] enums Newly created array of the compiled enums information for the current type.
2078 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2079 */
Radek Krejci19a96102018-11-15 13:38:09 +01002080static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02002081lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02002082 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01002083{
2084 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02002085 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002086 int32_t value = 0;
2087 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02002088 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01002089
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002090 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01002091 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
2092 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
2093 return LY_EVALID;
2094 }
2095
2096 LY_ARRAY_FOR(enums_p, u) {
2097 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
2098 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01002099 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
2100 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02002101 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01002102 if (base_enums) {
2103 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
2104 LY_ARRAY_FOR(base_enums, v) {
2105 if (!strcmp(e->name, base_enums[v].name)) {
2106 break;
2107 }
2108 }
2109 if (v == LY_ARRAY_SIZE(base_enums)) {
2110 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2111 "Invalid %s - derived type adds new item \"%s\".",
2112 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
2113 return LY_EVALID;
2114 }
2115 match = v;
2116 }
2117
2118 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02002119 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01002120 if (enums_p[u].flags & LYS_SET_VALUE) {
2121 e->value = (int32_t)enums_p[u].value;
2122 if (!u || e->value >= value) {
2123 value = e->value + 1;
2124 }
2125 /* check collision with other values */
2126 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2127 if (e->value == (*enums)[v].value) {
2128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2129 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
2130 e->value, e->name, (*enums)[v].name);
2131 return LY_EVALID;
2132 }
2133 }
2134 } else if (base_enums) {
2135 /* inherit the assigned value */
2136 e->value = base_enums[match].value;
2137 if (!u || e->value >= value) {
2138 value = e->value + 1;
2139 }
2140 } else {
2141 /* assign value automatically */
2142 if (u && value == INT32_MIN) {
2143 /* counter overflow */
2144 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2145 "Invalid enumeration - it is not possible to auto-assign enum value for "
2146 "\"%s\" since the highest value is already 2147483647.", e->name);
2147 return LY_EVALID;
2148 }
2149 e->value = value++;
2150 }
2151 } else { /* LY_TYPE_BITS */
2152 if (enums_p[u].flags & LYS_SET_VALUE) {
2153 e->value = (int32_t)enums_p[u].value;
2154 if (!u || (uint32_t)e->value >= position) {
2155 position = (uint32_t)e->value + 1;
2156 }
2157 /* check collision with other values */
2158 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2159 if (e->value == (*enums)[v].value) {
2160 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2161 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2162 (uint32_t)e->value, e->name, (*enums)[v].name);
2163 return LY_EVALID;
2164 }
2165 }
2166 } else if (base_enums) {
2167 /* inherit the assigned value */
2168 e->value = base_enums[match].value;
2169 if (!u || (uint32_t)e->value >= position) {
2170 position = (uint32_t)e->value + 1;
2171 }
2172 } else {
2173 /* assign value automatically */
2174 if (u && position == 0) {
2175 /* counter overflow */
2176 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2177 "Invalid bits - it is not possible to auto-assign bit position for "
2178 "\"%s\" since the highest value is already 4294967295.", e->name);
2179 return LY_EVALID;
2180 }
2181 e->value = position++;
2182 }
2183 }
2184
2185 if (base_enums) {
2186 /* the assigned values must not change from the derived type */
2187 if (e->value != base_enums[match].value) {
2188 if (basetype == LY_TYPE_ENUM) {
2189 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2190 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2191 e->name, base_enums[match].value, e->value);
2192 } else {
2193 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2194 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2195 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2196 }
2197 return LY_EVALID;
2198 }
2199 }
2200
Radek Krejciec4da802019-05-02 13:02:41 +02002201 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
Radek Krejci0935f412019-08-20 16:15:18 +02002202 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 +01002203
2204 if (basetype == LY_TYPE_BITS) {
2205 /* keep bits ordered by position */
2206 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2207 if (v != u) {
2208 memcpy(&storage, e, sizeof *e);
2209 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2210 memcpy(&(*enums)[v], &storage, sizeof storage);
2211 }
2212 }
2213 }
2214
2215done:
2216 return ret;
2217}
2218
Radek Krejcia3045382018-11-22 14:30:31 +01002219#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2220 for ((NODE) = (NODE)->parent; \
2221 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
2222 (NODE) = (NODE)->parent); \
2223 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2224 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2225 TERM; \
2226 }
2227
2228/**
2229 * @brief Validate the predicate(s) from the leafref path.
2230 * @param[in] ctx Compile context
2231 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2232 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002233 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002234 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002235 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002236 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2237 */
2238static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002239lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002240 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002241{
2242 LY_ERR ret = LY_EVALID;
2243 const struct lys_module *mod;
2244 const struct lysc_node *src_node, *dst_node;
2245 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2246 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002247 unsigned int dest_parent_times, c;
Radek Krejcia3045382018-11-22 14:30:31 +01002248 const char *start, *end, *pke_end;
2249 struct ly_set keys = {0};
2250 int i;
2251
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002252 assert(path_context);
2253
Radek Krejcia3045382018-11-22 14:30:31 +01002254 while (**predicate == '[') {
2255 start = (*predicate)++;
2256
2257 while (isspace(**predicate)) {
2258 ++(*predicate);
2259 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002260 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002261 while (isspace(**predicate)) {
2262 ++(*predicate);
2263 }
2264 if (**predicate != '=') {
2265 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002266 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2267 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002268 goto cleanup;
2269 }
2270 ++(*predicate);
2271 while (isspace(**predicate)) {
2272 ++(*predicate);
2273 }
2274
2275 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2276 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2277 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2278 goto cleanup;
2279 }
2280 --pke_end;
2281 while (isspace(*pke_end)) {
2282 --pke_end;
2283 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002284 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002285 /* localize path-key-expr */
2286 pke_start = path_key_expr = *predicate;
2287 /* move after the current predicate */
2288 *predicate = end + 1;
2289
2290 /* source (must be leaf or leaf-list) */
2291 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002292 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002293 if (!mod) {
2294 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2295 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002296 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002297 goto cleanup;
2298 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002299 if (!mod->implemented) {
2300 /* make the module implemented */
2301 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2302 }
Radek Krejcia3045382018-11-22 14:30:31 +01002303 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002304 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002305 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002306 src_node = NULL;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002307 if (!(context_node->flags & LYS_KEYLESS)) {
2308 struct lysc_node *key;
2309 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
2310 if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') {
2311 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002312 break;
2313 }
2314 }
2315 }
Radek Krejcia3045382018-11-22 14:30:31 +01002316 if (!src_node) {
2317 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002318 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002319 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002320 goto cleanup;
2321 }
Radek Krejcia3045382018-11-22 14:30:31 +01002322
2323 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002324 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002325 i = ly_set_add(&keys, (void*)src_node, 0);
2326 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002327 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002328 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002329 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002330 *predicate - start, start, src_node->name);
2331 goto cleanup;
2332 }
2333
2334 /* destination */
2335 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002336 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002337
2338 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002339 if (strncmp(path_key_expr, "current", 7)) {
2340error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2342 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2343 *predicate - start, start);
2344 goto cleanup;
2345 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002346 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2347 if (*path_key_expr != '(') {
2348 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002349 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002350 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2351 if (*path_key_expr != ')') {
2352 goto error_current_function_invocation;
2353 }
2354 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002355
2356 if (*path_key_expr != '/') {
2357 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2358 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2359 *predicate - start, start);
2360 goto cleanup;
2361 }
2362 ++path_key_expr;
2363 while (isspace(*path_key_expr)) {
2364 ++path_key_expr;
2365 }
2366
2367 /* rel-path-keyexpr:
2368 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2369 while (!strncmp(path_key_expr, "..", 2)) {
2370 ++dest_parent_times;
2371 path_key_expr += 2;
2372 while (isspace(*path_key_expr)) {
2373 ++path_key_expr;
2374 }
2375 if (*path_key_expr != '/') {
2376 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2377 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2378 *predicate - start, start);
2379 goto cleanup;
2380 }
2381 ++path_key_expr;
2382 while (isspace(*path_key_expr)) {
2383 ++path_key_expr;
2384 }
2385
2386 /* path is supposed to be evaluated in data tree, so we have to skip
2387 * all schema nodes that cannot be instantiated in data tree */
2388 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2389 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2390 *predicate - start, start);
2391 }
2392 if (!dest_parent_times) {
2393 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2394 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2395 *predicate - start, start);
2396 goto cleanup;
2397 }
2398 if (path_key_expr == pke_end) {
2399 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2400 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2401 *predicate - start, start);
2402 goto cleanup;
2403 }
2404
2405 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002406 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002407 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002408 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002409 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2410 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002411 goto cleanup;
2412 }
2413
2414 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002415 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002416 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002417 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002418 }
2419 if (!mod) {
2420 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002421 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002422 *predicate - start, start, dst_len, dst);
2423 goto cleanup;
2424 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002425 if (!mod->implemented) {
2426 /* make the module implemented */
2427 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2428 }
Radek Krejcia3045382018-11-22 14:30:31 +01002429
2430 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2431 if (!dst_node) {
2432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002433 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002434 *predicate - start, start, path_key_expr - pke_start, pke_start);
2435 goto cleanup;
2436 }
2437 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002438 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 +01002439 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002440 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002441 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2442 goto cleanup;
2443 }
2444 }
2445
2446 ret = LY_SUCCESS;
2447cleanup:
2448 ly_set_erase(&keys, NULL);
2449 return ret;
2450}
2451
2452/**
2453 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2454 *
2455 * path-arg = absolute-path / relative-path
2456 * absolute-path = 1*("/" (node-identifier *path-predicate))
2457 * relative-path = 1*(".." "/") descendant-path
2458 *
2459 * @param[in,out] path Path to parse.
2460 * @param[out] prefix Prefix of the token, NULL if there is not any.
2461 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2462 * @param[out] name Name of the token.
2463 * @param[out] nam_len Length of the name.
2464 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2465 * must not be changed between consecutive calls. -1 if the
2466 * path is absolute.
2467 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2468 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2469 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002470LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002471lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2472 int *parent_times, int *has_predicate)
2473{
2474 int par_times = 0;
2475
2476 assert(path && *path);
2477 assert(parent_times);
2478 assert(prefix);
2479 assert(prefix_len);
2480 assert(name);
2481 assert(name_len);
2482 assert(has_predicate);
2483
2484 *prefix = NULL;
2485 *prefix_len = 0;
2486 *name = NULL;
2487 *name_len = 0;
2488 *has_predicate = 0;
2489
2490 if (!*parent_times) {
2491 if (!strncmp(*path, "..", 2)) {
2492 *path += 2;
2493 ++par_times;
2494 while (!strncmp(*path, "/..", 3)) {
2495 *path += 3;
2496 ++par_times;
2497 }
2498 }
2499 if (par_times) {
2500 *parent_times = par_times;
2501 } else {
2502 *parent_times = -1;
2503 }
2504 }
2505
2506 if (**path != '/') {
2507 return LY_EINVAL;
2508 }
2509 /* skip '/' */
2510 ++(*path);
2511
2512 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002513 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002514
2515 if ((**path == '/' && (*path)[1]) || !**path) {
2516 /* path continues by another token or this is the last token */
2517 return LY_SUCCESS;
2518 } else if ((*path)[0] != '[') {
2519 /* unexpected character */
2520 return LY_EINVAL;
2521 } else {
2522 /* predicate starting with [ */
2523 *has_predicate = 1;
2524 return LY_SUCCESS;
2525 }
2526}
2527
2528/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002529 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2530 *
2531 * The set of features used for target must be a subset of features used for the leafref.
2532 * This is not a perfect, we should compare the truth tables but it could require too much resources
2533 * and RFC 7950 does not require it explicitely, so we simplify that.
2534 *
2535 * @param[in] refnode The leafref node.
2536 * @param[in] target Tha target node of the leafref.
2537 * @return LY_SUCCESS or LY_EVALID;
2538 */
2539static LY_ERR
2540lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2541{
2542 LY_ERR ret = LY_EVALID;
2543 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002544 unsigned int u, v, count;
2545 struct ly_set features = {0};
2546
2547 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002548 if (iter->iffeatures) {
2549 LY_ARRAY_FOR(iter->iffeatures, u) {
2550 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2551 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002552 }
2553 }
2554 }
2555 }
2556
2557 /* we should have, in features set, a superset of features applicable to the target node.
2558 * So when adding features applicable to the target into the features set, we should not be
2559 * able to actually add any new feature, otherwise it is not a subset of features applicable
2560 * to the leafref itself. */
2561 count = features.count;
2562 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002563 if (iter->iffeatures) {
2564 LY_ARRAY_FOR(iter->iffeatures, u) {
2565 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2566 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002567 /* new feature was added (or LY_EMEM) */
2568 goto cleanup;
2569 }
2570 }
2571 }
2572 }
2573 }
2574 ret = LY_SUCCESS;
2575
2576cleanup:
2577 ly_set_erase(&features, NULL);
2578 return ret;
2579}
2580
2581/**
Radek Krejcia3045382018-11-22 14:30:31 +01002582 * @brief Validate the leafref path.
2583 * @param[in] ctx Compile context
2584 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002585 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002586 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2587 */
2588static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002589lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002590{
2591 const struct lysc_node *node = NULL, *parent = NULL;
2592 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002593 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002594 const char *id, *prefix, *name;
2595 size_t prefix_len, name_len;
2596 int parent_times = 0, has_predicate;
2597 unsigned int iter, u;
2598 LY_ERR ret = LY_SUCCESS;
2599
2600 assert(ctx);
2601 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002602 assert(leafref);
2603
Radek Krejci1c0c3442019-07-23 16:08:47 +02002604 ctx->path[0] = '\0';
2605 lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
2606 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002607
Radek Krejcia3045382018-11-22 14:30:31 +01002608 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002609 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002610 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2611 if (!iter) { /* first iteration */
2612 /* precess ".." in relative paths */
2613 if (parent_times > 0) {
2614 /* move from the context node */
2615 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2616 /* path is supposed to be evaluated in data tree, so we have to skip
2617 * all schema nodes that cannot be instantiated in data tree */
2618 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002619 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002620 }
2621 }
2622 }
2623
2624 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002625 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002626 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002627 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002628 }
2629 if (!mod) {
2630 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002631 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2632 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002633 return LY_EVALID;
2634 }
Radek Krejci3d929562019-02-21 11:25:55 +01002635 if (!mod->implemented) {
2636 /* make the module implemented */
2637 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2638 }
Radek Krejcia3045382018-11-22 14:30:31 +01002639
2640 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2641 if (!node) {
2642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002643 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002644 return LY_EVALID;
2645 }
2646 parent = node;
2647
2648 if (has_predicate) {
2649 /* we have predicate, so the current result must be list */
2650 if (node->nodetype != LYS_LIST) {
2651 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2652 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002653 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002654 return LY_EVALID;
2655 }
2656
Radek Krejcibade82a2018-12-05 10:13:48 +01002657 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2658 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002659 }
2660
2661 ++iter;
2662 }
2663 if (ret) {
2664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002665 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002666 return LY_EVALID;
2667 }
2668
2669 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2670 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2671 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002672 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002673 return LY_EVALID;
2674 }
2675
2676 /* check status */
2677 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2678 return LY_EVALID;
2679 }
2680
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002681 /* check config */
2682 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2683 if (node->flags & LYS_CONFIG_R) {
2684 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2685 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2686 leafref->path);
2687 return LY_EVALID;
2688 }
2689 }
2690
Radek Krejci412ddfa2018-11-23 11:44:11 +01002691 /* store the target's type and check for circular chain of leafrefs */
2692 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2693 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2694 if (type == (struct lysc_type*)leafref) {
2695 /* circular chain detected */
2696 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2697 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2698 return LY_EVALID;
2699 }
2700 }
2701
Radek Krejci58d171e2018-11-23 13:50:55 +01002702 /* check if leafref and its target are under common if-features */
2703 if (lys_compile_leafref_features_validate(startnode, node)) {
2704 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2705 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2706 leafref->path);
2707 return LY_EVALID;
2708 }
2709
Radek Krejci327de162019-06-14 12:52:07 +02002710 ctx->path_len = 1;
2711 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002712 return LY_SUCCESS;
2713}
2714
Radek Krejcicdfecd92018-11-26 11:27:32 +01002715static 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 +02002716 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002717/**
2718 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2719 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002720 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2721 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2722 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2723 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002724 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002725 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002726 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002727 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2728 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2729 * @param[out] type Newly created type structure with the filled information about the type.
2730 * @return LY_ERR value.
2731 */
Radek Krejci19a96102018-11-15 13:38:09 +01002732static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002733lys_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 +02002734 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002735 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002736{
2737 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002738 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002739 struct lysc_type_bin *bin;
2740 struct lysc_type_num *num;
2741 struct lysc_type_str *str;
2742 struct lysc_type_bits *bits;
2743 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002744 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002745 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002746 struct lysc_type_union *un, *un_aux;
2747 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002748
2749 switch (basetype) {
2750 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002751 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002752
2753 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002754 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002755 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2756 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002757 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002758 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 +01002759 }
2760 }
2761
2762 if (tpdfname) {
2763 type_p->compiled = *type;
2764 *type = calloc(1, sizeof(struct lysc_type_bin));
2765 }
2766 break;
2767 case LY_TYPE_BITS:
2768 /* RFC 7950 9.7 - bits */
2769 bits = (struct lysc_type_bits*)(*type);
2770 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002771 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002772 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2773 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002774 }
2775
Radek Krejci555cb5b2018-11-16 14:54:33 +01002776 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002777 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002778 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002779 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002780 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002781 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002782 free(*type);
2783 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002784 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002785 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002786 }
2787
2788 if (tpdfname) {
2789 type_p->compiled = *type;
2790 *type = calloc(1, sizeof(struct lysc_type_bits));
2791 }
2792 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002793 case LY_TYPE_DEC64:
2794 dec = (struct lysc_type_dec*)(*type);
2795
2796 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002797 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002798 if (!type_p->fraction_digits) {
2799 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002800 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002801 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002802 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002803 free(*type);
2804 *type = NULL;
2805 }
2806 return LY_EVALID;
2807 }
2808 } else if (type_p->fraction_digits) {
2809 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002810 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002811 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2812 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002813 tpdfname);
2814 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002815 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2816 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002817 free(*type);
2818 *type = NULL;
2819 }
2820 return LY_EVALID;
2821 }
2822 dec->fraction_digits = type_p->fraction_digits;
2823
2824 /* RFC 7950 9.2.4 - range */
2825 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002826 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2827 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002828 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002829 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 +01002830 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002831 }
2832
2833 if (tpdfname) {
2834 type_p->compiled = *type;
2835 *type = calloc(1, sizeof(struct lysc_type_dec));
2836 }
2837 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002838 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002839 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002840
2841 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002842 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002843 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2844 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002845 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002846 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 +01002847 }
2848 } else if (base && ((struct lysc_type_str*)base)->length) {
2849 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2850 }
2851
2852 /* RFC 7950 9.4.5 - pattern */
2853 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002854 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002855 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002856 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2857 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2858 }
2859
2860 if (tpdfname) {
2861 type_p->compiled = *type;
2862 *type = calloc(1, sizeof(struct lysc_type_str));
2863 }
2864 break;
2865 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002866 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002867
2868 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002869 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002870 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002871 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002872 }
2873
Radek Krejci555cb5b2018-11-16 14:54:33 +01002874 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002875 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002876 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002877 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002878 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002879 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002880 free(*type);
2881 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002882 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002883 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002884 }
2885
2886 if (tpdfname) {
2887 type_p->compiled = *type;
2888 *type = calloc(1, sizeof(struct lysc_type_enum));
2889 }
2890 break;
2891 case LY_TYPE_INT8:
2892 case LY_TYPE_UINT8:
2893 case LY_TYPE_INT16:
2894 case LY_TYPE_UINT16:
2895 case LY_TYPE_INT32:
2896 case LY_TYPE_UINT32:
2897 case LY_TYPE_INT64:
2898 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002899 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002900
2901 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002902 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002903 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2904 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002905 if (!tpdfname) {
Radek Krejci0935f412019-08-20 16:15:18 +02002906 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 +01002907 }
2908 }
2909
2910 if (tpdfname) {
2911 type_p->compiled = *type;
2912 *type = calloc(1, sizeof(struct lysc_type_num));
2913 }
2914 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002915 case LY_TYPE_IDENT:
2916 idref = (struct lysc_type_identityref*)(*type);
2917
2918 /* RFC 7950 9.10.2 - base */
2919 if (type_p->bases) {
2920 if (base) {
2921 /* only the directly derived identityrefs can contain base specification */
2922 if (tpdfname) {
2923 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002924 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002925 tpdfname);
2926 } else {
2927 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002928 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002929 free(*type);
2930 *type = NULL;
2931 }
2932 return LY_EVALID;
2933 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002934 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002935 }
2936
2937 if (!base && !type_p->flags) {
2938 /* type derived from identityref built-in type must contain at least one base */
2939 if (tpdfname) {
2940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2941 } else {
2942 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2943 free(*type);
2944 *type = NULL;
2945 }
2946 return LY_EVALID;
2947 }
2948
2949 if (tpdfname) {
2950 type_p->compiled = *type;
2951 *type = calloc(1, sizeof(struct lysc_type_identityref));
2952 }
2953 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002954 case LY_TYPE_LEAFREF:
2955 /* RFC 7950 9.9.3 - require-instance */
2956 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002957 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002958 if (tpdfname) {
2959 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2960 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2961 } else {
2962 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2963 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2964 free(*type);
2965 *type = NULL;
2966 }
2967 return LY_EVALID;
2968 }
Radek Krejcia3045382018-11-22 14:30:31 +01002969 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002970 } else if (base) {
2971 /* inherit */
2972 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002973 } else {
2974 /* default is true */
2975 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2976 }
2977 if (type_p->path) {
2978 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002979 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002980 } else if (base) {
2981 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002982 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002983 } else if (tpdfname) {
2984 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2985 return LY_EVALID;
2986 } else {
2987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2988 free(*type);
2989 *type = NULL;
2990 return LY_EVALID;
2991 }
2992 if (tpdfname) {
2993 type_p->compiled = *type;
2994 *type = calloc(1, sizeof(struct lysc_type_leafref));
2995 }
2996 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002997 case LY_TYPE_INST:
2998 /* RFC 7950 9.9.3 - require-instance */
2999 if (type_p->flags & LYS_SET_REQINST) {
3000 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
3001 } else {
3002 /* default is true */
3003 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
3004 }
3005
3006 if (tpdfname) {
3007 type_p->compiled = *type;
3008 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3009 }
3010 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003011 case LY_TYPE_UNION:
3012 un = (struct lysc_type_union*)(*type);
3013
3014 /* RFC 7950 7.4 - type */
3015 if (type_p->types) {
3016 if (base) {
3017 /* only the directly derived union can contain types specification */
3018 if (tpdfname) {
3019 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3020 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
3021 tpdfname);
3022 } else {
3023 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
3024 "Invalid type substatement for the type not directly derived from union built-in type.");
3025 free(*type);
3026 *type = NULL;
3027 }
3028 return LY_EVALID;
3029 }
3030 /* compile the type */
3031 additional = 0;
3032 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
3033 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02003034 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 +01003035 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
3036 /* add space for additional types from the union subtype */
3037 un_aux = (struct lysc_type_union *)un->types[u + additional];
3038 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)));
3039 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3040 un->types = (void*)((uint32_t*)(p) + 1);
3041
3042 /* copy subtypes of the subtype union */
3043 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
3044 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
3045 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
3046 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
3047 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
3048 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
3049 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
3050 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
3051 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
3052 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
3053 /* TODO extensions */
3054
3055 } else {
3056 un->types[u + additional] = un_aux->types[v];
3057 ++un_aux->types[v]->refcount;
3058 }
3059 ++additional;
3060 LY_ARRAY_INCREMENT(un->types);
3061 }
3062 /* compensate u increment in main loop */
3063 --additional;
3064
3065 /* free the replaced union subtype */
3066 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
3067 } else {
3068 LY_ARRAY_INCREMENT(un->types);
3069 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01003070 }
3071 }
3072
3073 if (!base && !type_p->flags) {
3074 /* type derived from union built-in type must contain at least one type */
3075 if (tpdfname) {
3076 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
3077 } else {
3078 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
3079 free(*type);
3080 *type = NULL;
3081 }
3082 return LY_EVALID;
3083 }
3084
3085 if (tpdfname) {
3086 type_p->compiled = *type;
3087 *type = calloc(1, sizeof(struct lysc_type_union));
3088 }
3089 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003090 case LY_TYPE_BOOL:
3091 case LY_TYPE_EMPTY:
3092 case LY_TYPE_UNKNOWN: /* just to complete switch */
3093 break;
3094 }
3095 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
3096done:
3097 return ret;
3098}
3099
Radek Krejcia3045382018-11-22 14:30:31 +01003100/**
3101 * @brief Compile information about the leaf/leaf-list's type.
3102 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01003103 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
3104 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3105 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
3106 * @param[in] context_name Name of the context node or referencing typedef for logging.
3107 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01003108 * @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 +01003109 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01003110 * @return LY_ERR value.
3111 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01003112static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01003113lys_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 +02003114 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01003115{
3116 LY_ERR ret = LY_SUCCESS;
3117 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003118 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01003119 struct type_context {
3120 const struct lysp_tpdf *tpdf;
3121 struct lysp_node *node;
3122 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003123 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01003124 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01003125 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003126 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01003127 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02003128 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01003129
3130 (*type) = NULL;
3131
3132 tctx = calloc(1, sizeof *tctx);
3133 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01003134 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01003135 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
3136 ret == LY_SUCCESS;
3137 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3138 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3139 if (basetype) {
3140 break;
3141 }
3142
3143 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003144 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3145 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003146 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3147
Radek Krejcicdfecd92018-11-26 11:27:32 +01003148 if (units && !*units) {
3149 /* inherit units */
3150 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3151 }
Radek Krejci01342af2019-01-03 15:18:08 +01003152 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003153 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003154 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003155 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003156 }
Radek Krejci01342af2019-01-03 15:18:08 +01003157 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003158 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3159 break;
3160 }
3161
Radek Krejci19a96102018-11-15 13:38:09 +01003162 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003163 /* it is not necessary to continue, the rest of the chain was already compiled,
3164 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003165 basetype = tctx->tpdf->type.compiled->basetype;
3166 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003167 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003168 dummyloops = 1;
3169 goto preparenext;
3170 } else {
3171 tctx = NULL;
3172 break;
3173 }
Radek Krejci19a96102018-11-15 13:38:09 +01003174 }
3175
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003176 /* circular typedef reference detection */
3177 for (u = 0; u < tpdf_chain.count; u++) {
3178 /* local part */
3179 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3180 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3181 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3182 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3183 free(tctx);
3184 ret = LY_EVALID;
3185 goto cleanup;
3186 }
3187 }
3188 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3189 /* global part for unions corner case */
3190 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3191 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3192 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3193 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3194 free(tctx);
3195 ret = LY_EVALID;
3196 goto cleanup;
3197 }
3198 }
3199
Radek Krejci19a96102018-11-15 13:38:09 +01003200 /* store information for the following processing */
3201 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3202
Radek Krejcicdfecd92018-11-26 11:27:32 +01003203preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003204 /* prepare next loop */
3205 tctx_prev = tctx;
3206 tctx = calloc(1, sizeof *tctx);
3207 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3208 }
3209 free(tctx);
3210
3211 /* allocate type according to the basetype */
3212 switch (basetype) {
3213 case LY_TYPE_BINARY:
3214 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003215 break;
3216 case LY_TYPE_BITS:
3217 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003218 break;
3219 case LY_TYPE_BOOL:
3220 case LY_TYPE_EMPTY:
3221 *type = calloc(1, sizeof(struct lysc_type));
3222 break;
3223 case LY_TYPE_DEC64:
3224 *type = calloc(1, sizeof(struct lysc_type_dec));
3225 break;
3226 case LY_TYPE_ENUM:
3227 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003228 break;
3229 case LY_TYPE_IDENT:
3230 *type = calloc(1, sizeof(struct lysc_type_identityref));
3231 break;
3232 case LY_TYPE_INST:
3233 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3234 break;
3235 case LY_TYPE_LEAFREF:
3236 *type = calloc(1, sizeof(struct lysc_type_leafref));
3237 break;
3238 case LY_TYPE_STRING:
3239 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003240 break;
3241 case LY_TYPE_UNION:
3242 *type = calloc(1, sizeof(struct lysc_type_union));
3243 break;
3244 case LY_TYPE_INT8:
3245 case LY_TYPE_UINT8:
3246 case LY_TYPE_INT16:
3247 case LY_TYPE_UINT16:
3248 case LY_TYPE_INT32:
3249 case LY_TYPE_UINT32:
3250 case LY_TYPE_INT64:
3251 case LY_TYPE_UINT64:
3252 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003253 break;
3254 case LY_TYPE_UNKNOWN:
3255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3256 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3257 ret = LY_EVALID;
3258 goto cleanup;
3259 }
3260 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003261 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3263 ly_data_type2str[basetype]);
3264 free(*type);
3265 (*type) = NULL;
3266 ret = LY_EVALID;
3267 goto cleanup;
3268 }
3269
3270 /* get restrictions from the referred typedefs */
3271 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3272 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003273
3274 /* remember the typedef context for circular check */
3275 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3276
Radek Krejci43699232018-11-23 14:59:46 +01003277 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003278 base = tctx->tpdf->type.compiled;
3279 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003280 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003281 /* no change, just use the type information from the base */
3282 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3283 ++base->refcount;
3284 continue;
3285 }
3286
3287 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003288 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3290 tctx->tpdf->name, ly_data_type2str[basetype]);
3291 ret = LY_EVALID;
3292 goto cleanup;
3293 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3294 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3295 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3296 tctx->tpdf->name, tctx->tpdf->dflt);
3297 ret = LY_EVALID;
3298 goto cleanup;
3299 }
3300
Radek Krejci19a96102018-11-15 13:38:09 +01003301 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003302 /* TODO user type plugins */
3303 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003304 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003305 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 +01003306 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003307 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003308 LY_CHECK_GOTO(ret, cleanup);
3309 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003310 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003311 /* remove the processed typedef contexts from the stack for circular check */
3312 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003313
Radek Krejcic5c27e52018-11-15 14:38:11 +01003314 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003315 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003316 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003317 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003318 /* TODO user type plugins */
3319 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003320 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003321 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 +01003322 LY_CHECK_GOTO(ret, cleanup);
3323 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003324 /* no specific restriction in leaf's type definition, copy from the base */
3325 free(*type);
3326 (*type) = base;
3327 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003328 }
Radek Krejcia1911222019-07-22 17:24:50 +02003329 if (dflt && !(*type)->dflt) {
3330 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003331 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003332 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3333 (*type)->dflt->realtype = (*type);
3334 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3335 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003336 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003337 if (err) {
3338 ly_err_print(err);
3339 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3340 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3341 ly_err_free(err);
3342 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003343 if (ret == LY_EINCOMPLETE) {
3344 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003345 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003346
3347 /* but in general result is so far ok */
3348 ret = LY_SUCCESS;
3349 }
Radek Krejcia1911222019-07-22 17:24:50 +02003350 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003351 }
Radek Krejci19a96102018-11-15 13:38:09 +01003352
Radek Krejci0935f412019-08-20 16:15:18 +02003353 COMPILE_EXTS_GOTO(ctx, type_p->exts, (*type)->exts, (*type), LYEXT_PAR_TYPE, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003354
3355cleanup:
3356 ly_set_erase(&tpdf_chain, free);
3357 return ret;
3358}
3359
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003360/**
3361 * @brief Compile status information of the given node.
3362 *
3363 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3364 * has the status correctly set during the compilation.
3365 *
3366 * @param[in] ctx Compile context
3367 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3368 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3369 * the compatibility with the parent's status value.
3370 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3371 * @return LY_ERR value.
3372 */
3373static LY_ERR
3374lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3375{
3376 /* status - it is not inherited by specification, but it does not make sense to have
3377 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3378 if (!((*node_flags) & LYS_STATUS_MASK)) {
3379 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3380 if ((parent_flags & 0x3) != 0x3) {
3381 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3382 * combination of bits (0x3) which marks the uses_status value */
3383 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3384 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3385 }
3386 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3387 } else {
3388 (*node_flags) |= LYS_STATUS_CURR;
3389 }
3390 } else if (parent_flags & LYS_STATUS_MASK) {
3391 /* check status compatibility with the parent */
3392 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3393 if ((*node_flags) & LYS_STATUS_CURR) {
3394 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3395 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3396 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3397 } else { /* LYS_STATUS_DEPRC */
3398 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3399 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3400 }
3401 return LY_EVALID;
3402 }
3403 }
3404 return LY_SUCCESS;
3405}
3406
Radek Krejci8cce8532019-03-05 11:27:45 +01003407/**
3408 * @brief Check uniqness of the node/action/notification name.
3409 *
3410 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3411 * structures, but they share the namespace so we need to check their name collisions.
3412 *
3413 * @param[in] ctx Compile context.
3414 * @param[in] children List (linked list) of data nodes to go through.
3415 * @param[in] actions List (sized array) of actions or RPCs to go through.
3416 * @param[in] notifs List (sized array) of Notifications to go through.
3417 * @param[in] name Name of the item to find in the given lists.
3418 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3419 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3420 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3421 */
3422static LY_ERR
3423lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3424 const struct lysc_action *actions, const struct lysc_notif *notifs,
3425 const char *name, void *exclude)
3426{
3427 const struct lysc_node *iter;
3428 unsigned int u;
3429
3430 LY_LIST_FOR(children, iter) {
3431 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3432 goto error;
3433 }
3434 }
3435 LY_ARRAY_FOR(actions, u) {
3436 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3437 goto error;
3438 }
3439 }
3440 LY_ARRAY_FOR(notifs, u) {
3441 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3442 goto error;
3443 }
3444 }
3445 return LY_SUCCESS;
3446error:
3447 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3448 return LY_EEXIST;
3449}
3450
Radek Krejciec4da802019-05-02 13:02:41 +02003451static 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 +01003452
Radek Krejcia3045382018-11-22 14:30:31 +01003453/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003454 * @brief Compile parsed RPC/action schema node information.
3455 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003456 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003457 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003458 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3459 * @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).
3460 * Zero means no uses, non-zero value with no status bit set mean the default status.
3461 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3462 */
3463static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003464lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003465 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3466{
3467 LY_ERR ret = LY_SUCCESS;
3468 struct lysp_node *child_p;
3469 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003470 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003471
Radek Krejci327de162019-06-14 12:52:07 +02003472 lysc_update_path(ctx, parent, action_p->name);
3473
Radek Krejci8cce8532019-03-05 11:27:45 +01003474 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3475 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3476 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3477 action_p->name, action)) {
3478 return LY_EVALID;
3479 }
3480
Radek Krejciec4da802019-05-02 13:02:41 +02003481 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003482 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003483 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003484 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003485 return LY_EVALID;
3486 }
3487
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003488 action->nodetype = LYS_ACTION;
3489 action->module = ctx->mod;
3490 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003491 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003492 action->sp = action_p;
3493 }
3494 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3495
3496 /* status - it is not inherited by specification, but it does not make sense to have
3497 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3498 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3499
3500 DUP_STRING(ctx->ctx, action_p->name, action->name);
3501 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3502 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003503 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003504 COMPILE_EXTS_GOTO(ctx, action_p->exts, action->exts, action, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003505
3506 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003507 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejciec4da802019-05-02 13:02:41 +02003508 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003509 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 +02003510 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003511 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003512 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003513 }
Radek Krejci327de162019-06-14 12:52:07 +02003514 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003515 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003516
3517 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003518 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejciec4da802019-05-02 13:02:41 +02003519 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003520 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 +02003521 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003522 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003523 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003524 }
Radek Krejci327de162019-06-14 12:52:07 +02003525 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003526
Radek Krejci327de162019-06-14 12:52:07 +02003527 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003528cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003529 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003530 return ret;
3531}
3532
3533/**
Radek Krejci43981a32019-04-12 09:44:11 +02003534 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003535 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003536 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003537 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3538 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3539 * @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 +02003540 * Zero means no uses, non-zero value with no status bit set mean the default status.
3541 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3542 */
3543static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003544lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003545 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3546{
3547 LY_ERR ret = LY_SUCCESS;
3548 struct lysp_node *child_p;
3549 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003550 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003551
Radek Krejci327de162019-06-14 12:52:07 +02003552 lysc_update_path(ctx, parent, notif_p->name);
3553
Radek Krejcifc11bd72019-04-11 16:00:05 +02003554 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3555 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3556 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3557 notif_p->name, notif)) {
3558 return LY_EVALID;
3559 }
3560
Radek Krejciec4da802019-05-02 13:02:41 +02003561 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003562 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3563 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003564 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003565 return LY_EVALID;
3566 }
3567
3568 notif->nodetype = LYS_NOTIF;
3569 notif->module = ctx->mod;
3570 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003571 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003572 notif->sp = notif_p;
3573 }
3574 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3575
3576 /* status - it is not inherited by specification, but it does not make sense to have
3577 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3578 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3579
3580 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3581 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3582 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003583 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejciec4da802019-05-02 13:02:41 +02003584 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejci0935f412019-08-20 16:15:18 +02003585 COMPILE_EXTS_GOTO(ctx, notif_p->exts, notif->exts, notif, LYEXT_PAR_NODE, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003586
Radek Krejciec4da802019-05-02 13:02:41 +02003587 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003588 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003589 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003590 }
3591
Radek Krejci327de162019-06-14 12:52:07 +02003592 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003593cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003594 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003595 return ret;
3596}
3597
3598/**
Radek Krejcia3045382018-11-22 14:30:31 +01003599 * @brief Compile parsed container node information.
3600 * @param[in] ctx Compile context
3601 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003602 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3603 * is enriched with the container-specific information.
3604 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3605 */
Radek Krejci19a96102018-11-15 13:38:09 +01003606static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003607lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003608{
3609 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3610 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3611 struct lysp_node *child_p;
3612 unsigned int u;
3613 LY_ERR ret = LY_SUCCESS;
3614
Radek Krejcife909632019-02-12 15:34:42 +01003615 if (cont_p->presence) {
3616 cont->flags |= LYS_PRESENCE;
3617 }
3618
Radek Krejci19a96102018-11-15 13:38:09 +01003619 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003620 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003621 }
3622
Radek Krejciec4da802019-05-02 13:02:41 +02003623 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3624 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3625 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003626
3627done:
3628 return ret;
3629}
3630
Radek Krejci33f72892019-02-21 10:36:58 +01003631/*
3632 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3633 * @param[in] ctx Compile context.
3634 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3635 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003636 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3637 * @return LY_ERR value.
3638 */
3639static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003640lys_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 +01003641{
Radek Krejcia1911222019-07-22 17:24:50 +02003642 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003643 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3644
Radek Krejciec4da802019-05-02 13:02:41 +02003645 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 +01003646 leaf->units ? NULL : &leaf->units));
3647 if (leaf->nodetype == LYS_LEAFLIST) {
3648 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003649 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3650 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003651 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003652 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3653 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3654 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3655 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003656 LY_ARRAY_INCREMENT(llist->dflts);
3657 }
3658 } else {
3659 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003660 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003661 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3662 leaf->dflt->realtype = leaf->type->dflt->realtype;
3663 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3664 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003665 }
3666 }
3667 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3668 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3669 ly_set_add(&ctx->unres, leaf, 0);
3670 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3671 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3672 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3673 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3674 ly_set_add(&ctx->unres, leaf, 0);
3675 }
3676 }
3677 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003678 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3679 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3680 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3681 return LY_EVALID;
3682 }
3683 }
3684
Radek Krejci33f72892019-02-21 10:36:58 +01003685 return LY_SUCCESS;
3686}
3687
Radek Krejcia3045382018-11-22 14:30:31 +01003688/**
3689 * @brief Compile parsed leaf node information.
3690 * @param[in] ctx Compile context
3691 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003692 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3693 * is enriched with the leaf-specific information.
3694 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3695 */
Radek Krejci19a96102018-11-15 13:38:09 +01003696static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003697lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003698{
3699 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3700 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3701 unsigned int u;
3702 LY_ERR ret = LY_SUCCESS;
3703
Radek Krejciec4da802019-05-02 13:02:41 +02003704 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003705 if (leaf_p->units) {
3706 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3707 leaf->flags |= LYS_SET_UNITS;
3708 }
Radek Krejcia1911222019-07-22 17:24:50 +02003709
3710 /* the dflt member is just filled to avoid getting the default value from the type */
3711 leaf->dflt = (void*)leaf_p->dflt;
3712 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
Radek Krejci812a5bf2019-09-09 09:18:05 +02003713 if (ret) {
3714 leaf->dflt = NULL;
3715 return ret;
3716 }
Radek Krejcia1911222019-07-22 17:24:50 +02003717
Radek Krejciccd20f12019-02-15 14:12:27 +01003718 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003719 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003720 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003721 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3722 leaf->dflt->realtype = leaf->type;
3723 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3724 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003725 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003726 leaf->dflt->realtype->refcount++;
3727 if (err) {
3728 ly_err_print(err);
3729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3730 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3731 ly_err_free(err);
3732 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003733 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003734 /* postpone default compilation when the tree is complete */
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003735 LY_CHECK_RET(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod));
Radek Krejci1c0c3442019-07-23 16:08:47 +02003736
3737 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003738 ret = LY_SUCCESS;
3739 }
Radek Krejcib5bc93e2019-09-09 09:11:23 +02003740 LY_CHECK_RET(ret);
Radek Krejci76b3e962018-12-14 17:01:25 +01003741 leaf->flags |= LYS_SET_DFLT;
3742 }
Radek Krejci43699232018-11-23 14:59:46 +01003743
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003744
Radek Krejci19a96102018-11-15 13:38:09 +01003745done:
3746 return ret;
3747}
3748
Radek Krejcia3045382018-11-22 14:30:31 +01003749/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003750 * @brief Compile parsed leaf-list node information.
3751 * @param[in] ctx Compile context
3752 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003753 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3754 * is enriched with the leaf-list-specific information.
3755 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3756 */
3757static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003758lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003759{
3760 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3761 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003762 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003763 LY_ERR ret = LY_SUCCESS;
3764
Radek Krejciec4da802019-05-02 13:02:41 +02003765 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003766 if (llist_p->units) {
3767 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3768 llist->flags |= LYS_SET_UNITS;
3769 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003770
Radek Krejcia1911222019-07-22 17:24:50 +02003771 /* the dflts member is just filled to avoid getting the default value from the type */
3772 llist->dflts = (void*)llist_p->dflts;
3773 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003774 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003775 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003776 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003777 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3778 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003779 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003780 LY_ARRAY_INCREMENT(llist->dflts_mods);
3781 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003782 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003783 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3784 llist->dflts[u]->realtype = llist->type;
3785 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3786 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003787 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003788 llist->dflts[u]->realtype->refcount++;
3789 if (err) {
3790 ly_err_print(err);
3791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3792 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3793 ly_err_free(err);
3794 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003795 if (ret == LY_EINCOMPLETE) {
3796 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003797 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003798
3799 /* but in general result is so far ok */
3800 ret = LY_SUCCESS;
3801 }
Radek Krejcia1911222019-07-22 17:24:50 +02003802 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003803 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003804 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003805 }
Radek Krejcia1911222019-07-22 17:24:50 +02003806 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3807 /* configuration data values must be unique - so check the default values */
3808 LY_ARRAY_FOR(llist->dflts, u) {
3809 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3810 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3811 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003812 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 +02003813 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3814 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3815 if (dynamic) {
3816 free((char*)val);
3817 }
3818 return LY_EVALID;
3819 }
3820 }
3821 }
3822 }
3823
3824 /* 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 +01003825
3826 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003827 if (llist->min) {
3828 llist->flags |= LYS_MAND_TRUE;
3829 }
Radek Krejcib7408632018-11-28 17:12:11 +01003830 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003831
Radek Krejci0e5d8382018-11-28 16:37:53 +01003832done:
3833 return ret;
3834}
3835
3836/**
Radek Krejci7af64242019-02-18 13:07:53 +01003837 * @brief Compile information about list's uniques.
3838 * @param[in] ctx Compile context.
3839 * @param[in] context_module Module where the prefixes are going to be resolved.
3840 * @param[in] uniques Sized array list of unique statements.
3841 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3842 * @return LY_ERR value.
3843 */
3844static LY_ERR
3845lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3846{
3847 LY_ERR ret = LY_SUCCESS;
3848 struct lysc_node_leaf **key, ***unique;
3849 const char *keystr, *delim;
3850 size_t len;
3851 unsigned int v;
3852 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003853 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003854
3855 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3856 config = -1;
3857 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3858 keystr = uniques[v];
3859 while (keystr) {
3860 delim = strpbrk(keystr, " \t\n");
3861 if (delim) {
3862 len = delim - keystr;
3863 while (isspace(*delim)) {
3864 ++delim;
3865 }
3866 } else {
3867 len = strlen(keystr);
3868 }
3869
3870 /* unique node must be present */
3871 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003872 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3873 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003874 if (ret != LY_SUCCESS) {
3875 if (ret == LY_EDENIED) {
3876 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003877 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003878 len, keystr, lys_nodetype2str((*key)->nodetype));
3879 }
3880 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003881 } else if (flags) {
3882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3883 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3884 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3885 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003886 }
3887
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003888
Radek Krejci7af64242019-02-18 13:07:53 +01003889 /* all referenced leafs must be of the same config type */
3890 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3891 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3892 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3893 return LY_EVALID;
3894 } else if ((*key)->flags & LYS_CONFIG_W) {
3895 config = 1;
3896 } else { /* LYS_CONFIG_R */
3897 config = 0;
3898 }
3899
3900 /* check status */
3901 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3902 (*key)->flags, (*key)->module, (*key)->name));
3903
3904 /* mark leaf as unique */
3905 (*key)->flags |= LYS_UNIQUE;
3906
3907 /* next unique value in line */
3908 keystr = delim;
3909 }
3910 /* next unique definition */
3911 }
3912
3913 return LY_SUCCESS;
3914}
3915
3916/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003917 * @brief Compile parsed list node information.
3918 * @param[in] ctx Compile context
3919 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003920 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3921 * is enriched with the list-specific information.
3922 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3923 */
3924static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003925lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003926{
3927 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3928 struct lysc_node_list *list = (struct lysc_node_list*)node;
3929 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003930 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003931 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003932 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003933 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003934 LY_ERR ret = LY_SUCCESS;
3935
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003936 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003937 if (list->min) {
3938 list->flags |= LYS_MAND_TRUE;
3939 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003940 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3941
3942 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003943 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003944 }
3945
Radek Krejciec4da802019-05-02 13:02:41 +02003946 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003947
3948 /* keys */
3949 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3950 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3951 return LY_EVALID;
3952 }
3953
3954 /* find all the keys (must be direct children) */
3955 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003956 if (!keystr) {
3957 /* keyless list */
3958 list->flags |= LYS_KEYLESS;
3959 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003960 while (keystr) {
3961 delim = strpbrk(keystr, " \t\n");
3962 if (delim) {
3963 len = delim - keystr;
3964 while (isspace(*delim)) {
3965 ++delim;
3966 }
3967 } else {
3968 len = strlen(keystr);
3969 }
3970
3971 /* key node must be present */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003972 key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3973 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003974 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3975 "The list's key \"%.*s\" not found.", len, keystr);
3976 return LY_EVALID;
3977 }
3978 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003979 if (key->flags & LYS_KEY) {
3980 /* the node was already marked as a key */
3981 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3982 "Duplicated key identifier \"%.*s\".", len, keystr);
3983 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003984 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003985
3986 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003987 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003988 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003989 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3990 return LY_EVALID;
3991 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003992 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003993 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003994 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003995 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003996 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003997 return LY_EVALID;
3998 }
3999 } else {
4000 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004001 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004003 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004004 return LY_EVALID;
4005 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02004006 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004007 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004008 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004009 return LY_EVALID;
4010 }
4011 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004012
4013 /* check status */
4014 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02004015 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01004016
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004017 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004018 if (key->dflt) {
4019 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
4020 lysc_type_free(ctx->ctx, key->dflt->realtype);
4021 free(key->dflt);
4022 key->dflt = NULL;
4023 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004024 }
4025 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004026 key->flags |= LYS_KEY;
4027
4028 /* move it to the correct position */
4029 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
4030 /* fix links in closest previous siblings of the key */
4031 if (key->next) {
4032 key->next->prev = key->prev;
4033 } else {
4034 /* last child */
4035 list->child->prev = key->prev;
4036 }
4037 if (key->prev->next) {
4038 key->prev->next = key->next;
4039 }
4040 /* fix links in the key */
4041 if (prev_key) {
4042 key->prev = (struct lysc_node*)prev_key;
4043 key->next = prev_key->next;
4044 } else {
4045 key->prev = list->child->prev;
4046 key->next = list->child;
4047 }
4048 /* fix links in closes future siblings of the key */
4049 if (prev_key) {
4050 if (prev_key->next) {
4051 prev_key->next->prev = (struct lysc_node*)key;
4052 } else {
4053 list->child->prev = (struct lysc_node*)key;
4054 }
4055 prev_key->next = (struct lysc_node*)key;
4056 } else {
4057 list->child->prev = (struct lysc_node*)key;
4058 }
4059 /* fix links in parent */
4060 if (!key->prev->next) {
4061 list->child = (struct lysc_node*)key;
4062 }
4063 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004064
4065 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02004066 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004067 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02004068 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004069 }
4070
4071 /* uniques */
4072 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01004073 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004074 }
4075
Radek Krejciec4da802019-05-02 13:02:41 +02004076 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
4077 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004078
4079done:
4080 return ret;
4081}
4082
Radek Krejcib56c7502019-02-13 14:19:54 +01004083/**
4084 * @brief Do some checks and set the default choice's case.
4085 *
4086 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
4087 *
4088 * @param[in] ctx Compile context.
4089 * @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,
4090 * not the reference to the imported module.
4091 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
4092 * @return LY_ERR value.
4093 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004094static LY_ERR
4095lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
4096{
4097 struct lysc_node *iter, *node = (struct lysc_node*)ch;
4098 const char *prefix = NULL, *name;
4099 size_t prefix_len = 0;
4100
4101 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4102 name = strchr(dflt, ':');
4103 if (name) {
4104 prefix = dflt;
4105 prefix_len = name - prefix;
4106 ++name;
4107 } else {
4108 name = dflt;
4109 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004110 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004111 /* prefixed default case make sense only for the prefix of the schema itself */
4112 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4113 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
4114 prefix_len, prefix);
4115 return LY_EVALID;
4116 }
4117 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4118 if (!ch->dflt) {
4119 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4120 "Default case \"%s\" not found.", dflt);
4121 return LY_EVALID;
4122 }
4123 /* no mandatory nodes directly under the default case */
4124 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01004125 if (iter->parent != (struct lysc_node*)ch->dflt) {
4126 break;
4127 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004128 if (iter->flags & LYS_MAND_TRUE) {
4129 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4130 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
4131 return LY_EVALID;
4132 }
4133 }
Radek Krejci01342af2019-01-03 15:18:08 +01004134 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004135 return LY_SUCCESS;
4136}
4137
Radek Krejciccd20f12019-02-15 14:12:27 +01004138static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004139lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01004140{
4141 struct lys_module *mod;
4142 const char *prefix = NULL, *name;
4143 size_t prefix_len = 0;
4144 struct lysc_node_case *cs;
4145 struct lysc_node *node;
4146
4147 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4148 name = strchr(dflt, ':');
4149 if (name) {
4150 prefix = dflt;
4151 prefix_len = name - prefix;
4152 ++name;
4153 } else {
4154 name = dflt;
4155 }
4156 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4157 if (prefix) {
4158 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4159 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004160 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4161 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004162 return LY_EVALID;
4163 }
4164 } else {
4165 mod = ctx->mod;
4166 }
4167 /* get the default case */
4168 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4169 if (!cs) {
4170 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004171 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004172 return LY_EVALID;
4173 }
4174
4175 /* check that there is no mandatory node */
4176 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004177 if (node->parent != (struct lysc_node*)cs) {
4178 break;
4179 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004180 if (node->flags & LYS_MAND_TRUE) {
4181 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004182 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4183 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004184 return LY_EVALID;
4185 }
4186 }
4187
4188 /* set the default case in choice */
4189 ch->dflt = cs;
4190 cs->flags |= LYS_SET_DFLT;
4191
4192 return LY_SUCCESS;
4193}
4194
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004195/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004196 * @brief Compile parsed choice node information.
4197 * @param[in] ctx Compile context
4198 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004199 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004200 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004201 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4202 */
4203static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004204lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004205{
4206 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4207 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4208 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004209 LY_ERR ret = LY_SUCCESS;
4210
Radek Krejci056d0a82018-12-06 16:57:25 +01004211 LY_LIST_FOR(ch_p->child, child_p) {
4212 if (child_p->nodetype == LYS_CASE) {
4213 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004214 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004215 }
4216 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004217 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004218 }
4219 }
4220
4221 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004222 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004223 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004224 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004225
Radek Krejci9800fb82018-12-13 14:26:23 +01004226 return ret;
4227}
4228
4229/**
4230 * @brief Compile parsed anydata or anyxml node information.
4231 * @param[in] ctx Compile context
4232 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004233 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4234 * is enriched with the any-specific information.
4235 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4236 */
4237static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004238lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004239{
4240 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4241 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4242 unsigned int u;
4243 LY_ERR ret = LY_SUCCESS;
4244
Radek Krejciec4da802019-05-02 13:02:41 +02004245 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004246
4247 if (any->flags & LYS_CONFIG_W) {
4248 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
Radek Krejcid6b76452019-09-03 17:03:03 +02004249 ly_stmt2str(any->nodetype == LYS_ANYDATA ? LY_STMT_ANYDATA : LY_STMT_ANYXML));
Radek Krejci9800fb82018-12-13 14:26:23 +01004250 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004251done:
4252 return ret;
4253}
4254
Radek Krejcib56c7502019-02-13 14:19:54 +01004255/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004256 * @brief Connect the node into the siblings list and check its name uniqueness.
4257 *
4258 * @param[in] ctx Compile context
4259 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4260 * the choice itself is expected instead of a specific case node.
4261 * @param[in] node Schema node to connect into the list.
4262 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4263 */
4264static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004265lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004266{
4267 struct lysc_node **children;
4268
4269 if (node->nodetype == LYS_CASE) {
4270 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4271 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004272 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004273 }
4274 if (children) {
4275 if (!(*children)) {
4276 /* first child */
4277 *children = node;
4278 } else if (*children != node) {
4279 /* by the condition in previous branch we cover the choice/case children
4280 * - the children list is shared by the choice and the the first case, in addition
4281 * the first child of each case must be referenced from the case node. So the node is
4282 * actually always already inserted in case it is the first children - so here such
4283 * a situation actually corresponds to the first branch */
4284 /* insert at the end of the parent's children list */
4285 (*children)->prev->next = node;
4286 node->prev = (*children)->prev;
4287 (*children)->prev = node;
4288
4289 /* check the name uniqueness */
4290 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4291 lysc_node_notifs(parent), node->name, node)) {
4292 return LY_EEXIST;
4293 }
4294 }
4295 }
4296 return LY_SUCCESS;
4297}
4298
Radek Krejci95710c92019-02-11 15:49:55 +01004299/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004300 * @brief Get the XPath context node for the given schema node.
4301 * @param[in] start The schema node where the XPath expression appears.
4302 * @return The context node to evaluate XPath expression in given schema node.
4303 * @return NULL in case the context node is the root node.
4304 */
4305static struct lysc_node *
4306lysc_xpath_context(struct lysc_node *start)
4307{
4308 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
4309 start = start->parent);
4310 return start;
4311}
4312
4313/**
4314 * @brief Prepare the case structure in choice node for the new data node.
4315 *
4316 * 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
4317 * created in the choice when the first child was processed.
4318 *
4319 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004320 * @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,
4321 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004322 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4323 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4324 * @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,
4325 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004326 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004327static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004328lys_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 +01004329{
4330 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004331 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004332 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004333 unsigned int u;
4334 LY_ERR ret;
4335
Radek Krejci95710c92019-02-11 15:49:55 +01004336#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004337 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004338 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004339 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4340 return NULL; \
4341 } \
4342 }
4343
Radek Krejci95710c92019-02-11 15:49:55 +01004344 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4345 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004346
4347 /* we have to add an implicit case node into the parent choice */
4348 cs = calloc(1, sizeof(struct lysc_node_case));
4349 DUP_STRING(ctx->ctx, child->name, cs->name);
4350 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004351 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004352 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4353 /* the case is already present since the child is not its first children */
4354 return (struct lysc_node_case*)ch->cases->prev;
4355 }
Radek Krejci95710c92019-02-11 15:49:55 +01004356 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004357
4358 /* explicit parent case is not present (this is its first child) */
4359 cs = calloc(1, sizeof(struct lysc_node_case));
4360 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4361 cs->flags = LYS_STATUS_MASK & node_p->flags;
4362 cs->sp = node_p;
4363
Radek Krejcib1b59152019-01-07 13:21:56 +01004364 /* 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 +02004365 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004366
4367 if (node_p->when) {
4368 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004369 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004370 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004371 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004372 }
Radek Krejciec4da802019-05-02 13:02:41 +02004373 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004374 } else {
4375 LOGINT(ctx->ctx);
4376 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004377 }
4378 cs->module = ctx->mod;
4379 cs->prev = (struct lysc_node*)cs;
4380 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004381 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004382 cs->parent = (struct lysc_node*)ch;
4383 cs->child = child;
4384
4385 return cs;
4386error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004387 if (cs) {
4388 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4389 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004390 return NULL;
4391
4392#undef UNIQUE_CHECK
4393}
4394
Radek Krejcib56c7502019-02-13 14:19:54 +01004395/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004396 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004397 *
4398 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004399 * @param[in] node Target node where the config is supposed to be changed.
4400 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004401 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4402 * 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 +01004403 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4404 * @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 +01004405 * @return LY_ERR value.
4406 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004407static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004408lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004409 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004410{
4411 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004412 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004413
4414 if (config == (node->flags & LYS_CONFIG_MASK)) {
4415 /* nothing to do */
4416 return LY_SUCCESS;
4417 }
4418
4419 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004420 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004421 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004423 "Invalid %s of config - configuration node cannot be child of any state data node.",
4424 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004425 return LY_EVALID;
4426 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004427 node->flags |= LYS_SET_CONFIG;
4428 } else {
4429 if (node->flags & LYS_SET_CONFIG) {
4430 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4431 /* setting config flags, but have node with explicit config true */
4432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004433 "Invalid %s of config - configuration node cannot be child of any state data node.",
4434 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004435 return LY_EVALID;
4436 }
4437 /* do not change config on nodes where the config is explicitely set, this does not apply to
4438 * nodes, which are being changed explicitly (targets of refine or deviation) */
4439 return LY_SUCCESS;
4440 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004441 }
4442 node->flags &= ~LYS_CONFIG_MASK;
4443 node->flags |= config;
4444
4445 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004446 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004447 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004448 }
4449
Radek Krejci76b3e962018-12-14 17:01:25 +01004450 return LY_SUCCESS;
4451}
4452
Radek Krejcib56c7502019-02-13 14:19:54 +01004453/**
4454 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4455 *
4456 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4457 * the flag to such parents from a mandatory children.
4458 *
4459 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4460 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4461 * (mandatory children was removed).
4462 */
Radek Krejcife909632019-02-12 15:34:42 +01004463void
4464lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4465{
4466 struct lysc_node *iter;
4467
4468 if (add) { /* set flag */
4469 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4470 parent = parent->parent) {
4471 parent->flags |= LYS_MAND_TRUE;
4472 }
4473 } else { /* unset flag */
4474 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004475 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004476 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004477 /* there is another mandatory node */
4478 return;
4479 }
4480 }
4481 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4482 parent->flags &= ~LYS_MAND_TRUE;
4483 }
4484 }
4485}
4486
Radek Krejci056d0a82018-12-06 16:57:25 +01004487/**
Radek Krejci3641f562019-02-13 15:38:40 +01004488 * @brief Internal sorting process for the lys_compile_augment_sort().
4489 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4490 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4491 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4492 */
4493static void
4494lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4495{
4496 unsigned int v;
4497 size_t len;
4498
4499 len = strlen(aug_p->nodeid);
4500 LY_ARRAY_FOR(result, v) {
4501 if (strlen(result[v]->nodeid) <= len) {
4502 continue;
4503 }
4504 if (v < LY_ARRAY_SIZE(result)) {
4505 /* move the rest of array */
4506 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4507 break;
4508 }
4509 }
4510 result[v] = aug_p;
4511 LY_ARRAY_INCREMENT(result);
4512}
4513
4514/**
4515 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4516 *
4517 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4518 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4519 *
4520 * @param[in] ctx Compile context.
4521 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4522 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4523 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4524 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4525 * @return LY_ERR value.
4526 */
4527LY_ERR
4528lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4529{
4530 struct lysp_augment **result = NULL;
4531 unsigned int u, v;
4532 size_t count = 0;
4533
4534 assert(augments);
4535
4536 /* get count of the augments in module and all its submodules */
4537 if (aug_p) {
4538 count += LY_ARRAY_SIZE(aug_p);
4539 }
4540 LY_ARRAY_FOR(inc_p, u) {
4541 if (inc_p[u].submodule->augments) {
4542 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4543 }
4544 }
4545
4546 if (!count) {
4547 *augments = NULL;
4548 return LY_SUCCESS;
4549 }
4550 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4551
4552 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4553 * together, so there can be even /z/y betwwen them. */
4554 LY_ARRAY_FOR(aug_p, u) {
4555 lys_compile_augment_sort_(&aug_p[u], result);
4556 }
4557 LY_ARRAY_FOR(inc_p, u) {
4558 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4559 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4560 }
4561 }
4562
4563 *augments = result;
4564 return LY_SUCCESS;
4565}
4566
4567/**
4568 * @brief Compile the parsed augment connecting it into its target.
4569 *
4570 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4571 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4572 * are already implemented and compiled.
4573 *
4574 * @param[in] ctx Compile context.
4575 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004576 * @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
4577 * children in case of the augmenting uses data.
4578 * @return LY_SUCCESS on success.
4579 * @return LY_EVALID on failure.
4580 */
4581LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004582lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004583{
4584 LY_ERR ret = LY_SUCCESS;
4585 struct lysp_node *node_p, *case_node_p;
4586 struct lysc_node *target; /* target target of the augment */
4587 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004588 struct lysc_when **when, *when_shared;
4589 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004590 uint16_t flags = 0;
4591 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004592 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004593
Radek Krejci327de162019-06-14 12:52:07 +02004594 lysc_update_path(ctx, NULL, "{augment}");
4595 lysc_update_path(ctx, NULL, aug_p->nodeid);
4596
Radek Krejci7af64242019-02-18 13:07:53 +01004597 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004598 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004599 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004600 if (ret != LY_SUCCESS) {
4601 if (ret == LY_EDENIED) {
4602 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4603 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4604 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4605 }
4606 return LY_EVALID;
4607 }
4608
4609 /* check for mandatory nodes
4610 * - new cases augmenting some choice can have mandatory nodes
4611 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4612 */
Radek Krejci733988a2019-02-15 15:12:44 +01004613 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004614 allow_mandatory = 1;
4615 }
4616
4617 when_shared = NULL;
4618 LY_LIST_FOR(aug_p->child, node_p) {
4619 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4620 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4621 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004622 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4623 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004624 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004625 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4626 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004627 return LY_EVALID;
4628 }
4629
4630 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004631 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004632 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004633 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004634 } else {
4635 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004636 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004637 }
4638 }
Radek Krejciec4da802019-05-02 13:02:41 +02004639 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004640
Radek Krejcife13da42019-02-15 14:51:01 +01004641 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4642 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004643 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004644 /* 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 +01004645 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 +01004646 } else if (target->nodetype == LYS_CHOICE) {
4647 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4648 node = ((struct lysc_node_choice*)target)->cases->prev;
4649 } else {
4650 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004651 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004652 }
4653
Radek Krejci733988a2019-02-15 15:12:44 +01004654 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004655 node->flags &= ~LYS_MAND_TRUE;
4656 lys_compile_mandatory_parents(target, 0);
4657 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004658 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004659 return LY_EVALID;
4660 }
4661
4662 /* pass augment's when to all the children */
4663 if (aug_p->when) {
4664 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4665 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004666 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004667 LY_CHECK_GOTO(ret, error);
4668 (*when)->context = lysc_xpath_context(target);
4669 when_shared = *when;
4670 } else {
4671 ++when_shared->refcount;
4672 (*when) = when_shared;
4673 }
4674 }
4675 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004676
Radek Krejciec4da802019-05-02 13:02:41 +02004677 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004678 switch (target->nodetype) {
4679 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004680 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004681 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004682 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004683 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004684 break;
4685 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004686 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004687 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004688 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004689 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004690 break;
4691 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004692 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004693 if (aug_p->actions) {
4694 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004695 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4696 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004697 return LY_EVALID;
4698 }
4699 if (aug_p->notifs) {
4700 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004701 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4702 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004703 return LY_EVALID;
4704 }
4705 }
Radek Krejci3641f562019-02-13 15:38:40 +01004706
Radek Krejci327de162019-06-14 12:52:07 +02004707 lysc_update_path(ctx, NULL, NULL);
4708 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004709error:
Radek Krejciec4da802019-05-02 13:02:41 +02004710 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004711 return ret;
4712}
4713
4714/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004715 * @brief Apply refined or deviated mandatory flag to the target node.
4716 *
4717 * @param[in] ctx Compile context.
4718 * @param[in] node Target node where the mandatory property is supposed to be changed.
4719 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004720 * @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 +01004721 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4722 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4723 * 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 +01004724 * @return LY_ERR value.
4725 */
4726static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004727lys_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 +01004728{
4729 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4730 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004731 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4732 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004733 return LY_EVALID;
4734 }
4735
4736 if (mandatory_flag & LYS_MAND_TRUE) {
4737 /* check if node has default value */
4738 if (node->nodetype & LYS_LEAF) {
4739 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004740 if (refine_flag) {
4741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004742 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004743 return LY_EVALID;
4744 }
Radek Krejcia1911222019-07-22 17:24:50 +02004745 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004746 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004747 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004748
4749 /* update the list of incomplete default values if needed */
4750 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4751
Radek Krejcia1911222019-07-22 17:24:50 +02004752 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4753 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4754 free(leaf->dflt);
4755 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004756 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004757 }
4758 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004759 if (refine_flag) {
4760 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004761 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004762 return LY_EVALID;
4763 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004764 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004765 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004766 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 +01004767 return LY_EVALID;
4768 }
4769
4770 node->flags &= ~LYS_MAND_FALSE;
4771 node->flags |= LYS_MAND_TRUE;
4772 lys_compile_mandatory_parents(node->parent, 1);
4773 } else {
4774 /* make mandatory false */
4775 node->flags &= ~LYS_MAND_TRUE;
4776 node->flags |= LYS_MAND_FALSE;
4777 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004778 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 +01004779 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004780 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004781 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004782 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4783 leaf->dflt->realtype = leaf->type->dflt->realtype;
4784 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4785 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004786 }
4787 }
4788 return LY_SUCCESS;
4789}
4790
4791/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004792 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4793 * If present, also apply uses's modificators.
4794 *
4795 * @param[in] ctx Compile context
4796 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004797 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4798 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4799 * the compile context.
4800 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4801 */
4802static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004803lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004804{
4805 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004806 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004807 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004808 struct lysc_node_container context_node_fake =
4809 {.nodetype = LYS_CONTAINER,
4810 .module = ctx->mod,
4811 .flags = parent ? parent->flags : 0,
4812 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004813 .prev = (struct lysc_node*)&context_node_fake,
4814 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004815 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004816 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004817 int found;
4818 const char *id, *name, *prefix;
4819 size_t prefix_len, name_len;
4820 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004821 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004822 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004823 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004824 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004825 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004826 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004827 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004828 unsigned int actions_index, notifs_index;
4829 struct lysc_notif **notifs = NULL;
4830 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004831
4832 /* search for the grouping definition */
4833 found = 0;
4834 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004835 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004836 if (prefix) {
4837 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4838 if (!mod) {
4839 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004840 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004841 return LY_EVALID;
4842 }
4843 } else {
4844 mod = ctx->mod_def;
4845 }
4846 if (mod == ctx->mod_def) {
4847 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004848 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004849 LY_ARRAY_FOR(grp, u) {
4850 if (!strcmp(grp[u].name, name)) {
4851 grp = &grp[u];
4852 found = 1;
4853 break;
4854 }
4855 }
4856 }
4857 }
4858 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004859 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004860 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004861 if (grp) {
4862 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4863 if (!strcmp(grp[u].name, name)) {
4864 grp = &grp[u];
4865 found = 1;
4866 }
4867 }
4868 }
4869 if (!found && mod->parsed->includes) {
4870 /* ... and all the submodules */
4871 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4872 grp = mod->parsed->includes[u].submodule->groupings;
4873 if (grp) {
4874 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4875 if (!strcmp(grp[v].name, name)) {
4876 grp = &grp[v];
4877 found = 1;
4878 }
4879 }
4880 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004881 }
4882 }
4883 }
4884 if (!found) {
4885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4886 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4887 return LY_EVALID;
4888 }
4889
4890 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4891 grp_stack_count = ctx->groupings.count;
4892 ly_set_add(&ctx->groupings, (void*)grp, 0);
4893 if (grp_stack_count == ctx->groupings.count) {
4894 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4895 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4896 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4897 return LY_EVALID;
4898 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004899 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4900 /* remember that the grouping is instantiated to avoid its standalone validation */
4901 grp->flags |= LYS_USED_GRP;
4902 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004903
4904 /* switch context's mod_def */
4905 mod_old = ctx->mod_def;
4906 ctx->mod_def = mod;
4907
4908 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004909 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 +01004910
Radek Krejcifc11bd72019-04-11 16:00:05 +02004911 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004912 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004913 /* 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 +02004914 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 +01004915
4916 /* some preparation for applying refines */
4917 if (grp->data == node_p) {
4918 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004919 if (parent) {
4920 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4921 } else if (ctx->mod->compiled->data) {
4922 child = ctx->mod->compiled->data;
4923 } else {
4924 child = NULL;
4925 }
4926 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004927 }
4928 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004929 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004930 LY_LIST_FOR(context_node_fake.child, child) {
4931 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004932
Radek Krejcifc11bd72019-04-11 16:00:05 +02004933 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004934 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004935 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004936 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004937 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004938 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004939 when_shared = *when;
4940 } else {
4941 ++when_shared->refcount;
4942 (*when) = when_shared;
4943 }
4944 }
Radek Krejci01342af2019-01-03 15:18:08 +01004945 }
4946 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004947 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004948 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004949 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004950 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 +01004951 }
4952
Radek Krejcifc11bd72019-04-11 16:00:05 +02004953 /* compile actions */
4954 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4955 if (actions) {
4956 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004957 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004958 if (*actions && (uses_p->augments || uses_p->refines)) {
4959 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4960 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4961 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4962 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4963 }
4964 }
4965
4966 /* compile notifications */
4967 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4968 if (notifs) {
4969 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004970 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004971 if (*notifs && (uses_p->augments || uses_p->refines)) {
4972 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4973 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4974 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4975 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4976 }
4977 }
4978
4979
Radek Krejci3641f562019-02-13 15:38:40 +01004980 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004981 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004982 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004983 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004984 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004985
Radek Krejcif0089082019-01-07 16:42:01 +01004986 /* reload previous context's mod_def */
4987 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004988 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004989
Radek Krejci76b3e962018-12-14 17:01:25 +01004990 /* apply refine */
4991 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004992 lysc_update_path(ctx, NULL, rfn->nodeid);
4993
Radek Krejci7af64242019-02-18 13:07:53 +01004994 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 +01004995 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004996 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004997 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004998
4999 /* default value */
5000 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01005001 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01005002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005003 "Invalid refine of default - %s cannot hold %d default values.",
5004 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005005 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005006 }
5007 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
5008 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005009 "Invalid refine of default - %s cannot hold default value(s).",
5010 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005011 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01005012 }
5013 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02005014 struct ly_err_item *err = NULL;
5015 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
5016 if (leaf->dflt) {
5017 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02005018 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005019 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5020 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5021 } else {
5022 /* prepare a new one */
5023 leaf->dflt = calloc(1, sizeof *leaf->dflt);
5024 leaf->dflt->realtype = leaf->type;
5025 }
5026 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005027 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005028 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
5029 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005030 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005031 leaf->dflt->realtype->refcount++;
5032 if (err) {
5033 ly_err_print(err);
5034 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5035 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
5036 ly_err_free(err);
5037 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005038 if (rc == LY_EINCOMPLETE) {
5039 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005040 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005041
5042 /* but in general result is so far ok */
5043 rc = LY_SUCCESS;
5044 }
Radek Krejcia1911222019-07-22 17:24:50 +02005045 LY_CHECK_GOTO(rc, cleanup);
5046 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005047 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02005048 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
5049
Radek Krejci0bcdaed2019-01-10 10:21:34 +01005050 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01005051 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5052 "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 +02005053 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01005054 }
Radek Krejcia1911222019-07-22 17:24:50 +02005055
5056 /* remove previous set of default values */
5057 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005058 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02005059 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
5060 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
5061 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01005062 }
Radek Krejcia1911222019-07-22 17:24:50 +02005063 LY_ARRAY_FREE(llist->dflts);
5064 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005065 LY_ARRAY_FREE(llist->dflts_mods);
5066 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005067
5068 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005069 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005070 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005071 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005072 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005073 LY_ARRAY_INCREMENT(llist->dflts_mods);
5074 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005075 LY_ARRAY_INCREMENT(llist->dflts);
5076 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
5077 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02005078 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02005079 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005080 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005081 llist->dflts[u]->realtype->refcount++;
5082 if (err) {
5083 ly_err_print(err);
5084 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5085 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
5086 ly_err_free(err);
5087 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02005088 if (rc == LY_EINCOMPLETE) {
5089 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02005090 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02005091
5092 /* but in general result is so far ok */
5093 rc = LY_SUCCESS;
5094 }
5095 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005096 }
Radek Krejcia1911222019-07-22 17:24:50 +02005097 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01005098 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01005099 if (((struct lysc_node_choice*)node)->dflt) {
5100 /* unset LYS_SET_DFLT from the current default case */
5101 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
5102 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005103 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 +01005104 }
5105 }
5106
Radek Krejci12fb9142019-01-08 09:45:30 +01005107 /* description */
5108 if (rfn->dsc) {
5109 FREE_STRING(ctx->ctx, node->dsc);
5110 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
5111 }
5112
5113 /* reference */
5114 if (rfn->ref) {
5115 FREE_STRING(ctx->ctx, node->ref);
5116 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
5117 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005118
5119 /* config */
5120 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005121 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005122 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005123 } else {
5124 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
5125 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
5126 }
Radek Krejci76b3e962018-12-14 17:01:25 +01005127 }
5128
5129 /* mandatory */
5130 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02005131 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01005132 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005133
5134 /* presence */
5135 if (rfn->presence) {
5136 if (node->nodetype != LYS_CONTAINER) {
5137 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005138 "Invalid refine of presence statement - %s cannot hold the presence statement.",
5139 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005140 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005141 }
5142 node->flags |= LYS_PRESENCE;
5143 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005144
5145 /* must */
5146 if (rfn->musts) {
5147 switch (node->nodetype) {
5148 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02005149 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 +01005150 break;
5151 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02005152 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 +01005153 break;
5154 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02005155 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 +01005156 break;
5157 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02005158 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 +01005159 break;
5160 case LYS_ANYXML:
5161 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02005162 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 +01005163 break;
5164 default:
5165 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005166 "Invalid refine of must statement - %s cannot hold any must statement.",
5167 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005168 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005169 }
5170 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005171
5172 /* min/max-elements */
5173 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5174 switch (node->nodetype) {
5175 case LYS_LEAFLIST:
5176 if (rfn->flags & LYS_SET_MAX) {
5177 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5178 }
5179 if (rfn->flags & LYS_SET_MIN) {
5180 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005181 if (rfn->min) {
5182 node->flags |= LYS_MAND_TRUE;
5183 lys_compile_mandatory_parents(node->parent, 1);
5184 } else {
5185 node->flags &= ~LYS_MAND_TRUE;
5186 lys_compile_mandatory_parents(node->parent, 0);
5187 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005188 }
5189 break;
5190 case LYS_LIST:
5191 if (rfn->flags & LYS_SET_MAX) {
5192 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5193 }
5194 if (rfn->flags & LYS_SET_MIN) {
5195 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005196 if (rfn->min) {
5197 node->flags |= LYS_MAND_TRUE;
5198 lys_compile_mandatory_parents(node->parent, 1);
5199 } else {
5200 node->flags &= ~LYS_MAND_TRUE;
5201 lys_compile_mandatory_parents(node->parent, 0);
5202 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005203 }
5204 break;
5205 default:
5206 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005207 "Invalid refine of %s statement - %s cannot hold this statement.",
5208 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005209 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005210 }
5211 }
Radek Krejcif0089082019-01-07 16:42:01 +01005212
5213 /* if-feature */
5214 if (rfn->iffeatures) {
5215 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005216 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005217 }
Radek Krejci327de162019-06-14 12:52:07 +02005218
5219 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005220 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005221
Radek Krejcif2271f12019-01-07 16:42:23 +01005222 /* do some additional checks of the changed nodes when all the refines are applied */
5223 for (u = 0; u < refined.count; ++u) {
5224 node = (struct lysc_node*)refined.objs[u];
5225 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005226 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005227
5228 /* check possible conflict with default value (default added, mandatory left true) */
5229 if ((node->flags & LYS_MAND_TRUE) &&
5230 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5231 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5232 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005233 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005234 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005235 }
5236
5237 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5238 if (node->nodetype == LYS_LIST) {
5239 min = ((struct lysc_node_list*)node)->min;
5240 max = ((struct lysc_node_list*)node)->max;
5241 } else {
5242 min = ((struct lysc_node_leaflist*)node)->min;
5243 max = ((struct lysc_node_leaflist*)node)->max;
5244 }
5245 if (min > max) {
5246 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005247 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5248 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005249 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005250 }
5251 }
5252 }
5253
Radek Krejci327de162019-06-14 12:52:07 +02005254 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005255 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005256
5257cleanup:
5258 /* fix connection of the children nodes from fake context node back into the parent */
5259 if (context_node_fake.child) {
5260 context_node_fake.child->prev = child;
5261 }
5262 LY_LIST_FOR(context_node_fake.child, child) {
5263 child->parent = parent;
5264 }
5265
5266 if (uses_p->augments || uses_p->refines) {
5267 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005268 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005269 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5270 LY_ARRAY_FREE(context_node_fake.actions);
5271 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005272 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005273 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5274 LY_ARRAY_FREE(context_node_fake.notifs);
5275 }
5276 }
5277
Radek Krejcie86bf772018-12-14 11:39:53 +01005278 /* reload previous context's mod_def */
5279 ctx->mod_def = mod_old;
5280 /* remove the grouping from the stack for circular groupings dependency check */
5281 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5282 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005283 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005284 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005285
5286 return ret;
5287}
5288
Radek Krejci327de162019-06-14 12:52:07 +02005289static int
5290lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5291{
5292 struct lysp_node *iter;
5293 int len = 0;
5294
5295 *path = NULL;
5296 for (iter = node; iter && len >= 0; iter = iter->parent) {
5297 char *s = *path;
5298 char *id;
5299
5300 switch (iter->nodetype) {
5301 case LYS_USES:
5302 asprintf(&id, "{uses='%s'}", iter->name);
5303 break;
5304 case LYS_GROUPING:
5305 asprintf(&id, "{grouping='%s'}", iter->name);
5306 break;
5307 case LYS_AUGMENT:
5308 asprintf(&id, "{augment='%s'}", iter->name);
5309 break;
5310 default:
5311 id = strdup(iter->name);
5312 break;
5313 }
5314
5315 if (!iter->parent) {
5316 /* print prefix */
5317 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5318 } else {
5319 /* prefix is the same as in parent */
5320 len = asprintf(path, "/%s%s", id, s ? s : "");
5321 }
5322 free(s);
5323 free(id);
5324 }
5325
5326 if (len < 0) {
5327 free(*path);
5328 *path = NULL;
5329 } else if (len == 0) {
5330 *path = strdup("/");
5331 len = 1;
5332 }
5333 return len;
5334}
5335
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005336/**
5337 * @brief Validate groupings that were defined but not directly used in the schema itself.
5338 *
5339 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5340 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5341 */
5342static LY_ERR
5343lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5344{
5345 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005346 char *path;
5347 int len;
5348
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005349 struct lysp_node_uses fake_uses = {
5350 .parent = node_p,
5351 .nodetype = LYS_USES,
5352 .flags = 0, .next = NULL,
5353 .name = grp->name,
5354 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5355 .refines = NULL, .augments = NULL
5356 };
5357 struct lysc_node_container fake_container = {
5358 .nodetype = LYS_CONTAINER,
5359 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5360 .module = ctx->mod,
5361 .sp = NULL, .parent = NULL, .next = NULL,
5362 .prev = (struct lysc_node*)&fake_container,
5363 .name = "fake",
5364 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5365 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5366 };
5367
5368 if (grp->parent) {
5369 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5370 }
Radek Krejci327de162019-06-14 12:52:07 +02005371
5372 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5373 if (len < 0) {
5374 LOGMEM(ctx->ctx);
5375 return LY_EMEM;
5376 }
5377 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5378 ctx->path_len = (uint16_t)len;
5379 free(path);
5380
5381 lysc_update_path(ctx, NULL, "{grouping}");
5382 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005383 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005384 lysc_update_path(ctx, NULL, NULL);
5385 lysc_update_path(ctx, NULL, NULL);
5386
5387 ctx->path_len = 1;
5388 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005389
5390 /* cleanup */
5391 lysc_node_container_free(ctx->ctx, &fake_container);
5392
5393 return ret;
5394}
Radek Krejcife909632019-02-12 15:34:42 +01005395
Radek Krejcie86bf772018-12-14 11:39:53 +01005396/**
Radek Krejcia3045382018-11-22 14:30:31 +01005397 * @brief Compile parsed schema node information.
5398 * @param[in] ctx Compile context
5399 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005400 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5401 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5402 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005403 * @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).
5404 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005405 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5406 */
Radek Krejci19a96102018-11-15 13:38:09 +01005407static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005408lys_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 +01005409{
5410 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005411 struct lysc_node *node;
5412 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005413 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005414 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005415 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005416
Radek Krejci327de162019-06-14 12:52:07 +02005417 if (node_p->nodetype != LYS_USES) {
5418 lysc_update_path(ctx, parent, node_p->name);
5419 } else {
5420 lysc_update_path(ctx, NULL, "{uses}");
5421 lysc_update_path(ctx, NULL, node_p->name);
5422 }
5423
Radek Krejci19a96102018-11-15 13:38:09 +01005424 switch (node_p->nodetype) {
5425 case LYS_CONTAINER:
5426 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5427 node_compile_spec = lys_compile_node_container;
5428 break;
5429 case LYS_LEAF:
5430 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5431 node_compile_spec = lys_compile_node_leaf;
5432 break;
5433 case LYS_LIST:
5434 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005435 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005436 break;
5437 case LYS_LEAFLIST:
5438 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005439 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005440 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005441 case LYS_CHOICE:
5442 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005443 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005444 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005445 case LYS_ANYXML:
5446 case LYS_ANYDATA:
5447 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005448 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005449 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005450 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005451 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5452 lysc_update_path(ctx, NULL, NULL);
5453 lysc_update_path(ctx, NULL, NULL);
5454 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005455 default:
5456 LOGINT(ctx->ctx);
5457 return LY_EINT;
5458 }
5459 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5460 node->nodetype = node_p->nodetype;
5461 node->module = ctx->mod;
5462 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005463 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005464
5465 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005466 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005467 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005468 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005469 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5470 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005471 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005472 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005473 node->flags |= LYS_CONFIG_R;
5474 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005475 /* config not explicitely set, inherit it from parent */
5476 if (parent) {
5477 node->flags |= parent->flags & LYS_CONFIG_MASK;
5478 } else {
5479 /* default is config true */
5480 node->flags |= LYS_CONFIG_W;
5481 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005482 } else {
5483 /* config set explicitely */
5484 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005485 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005486 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5487 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5488 "Configuration node cannot be child of any state data node.");
5489 goto error;
5490 }
Radek Krejci19a96102018-11-15 13:38:09 +01005491
Radek Krejcia6d57732018-11-29 13:40:37 +01005492 /* *list ordering */
5493 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5494 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005495 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005496 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5497 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005498 node->flags &= ~LYS_ORDBY_MASK;
5499 node->flags |= LYS_ORDBY_SYSTEM;
5500 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5501 /* default ordering is system */
5502 node->flags |= LYS_ORDBY_SYSTEM;
5503 }
5504 }
5505
Radek Krejci19a96102018-11-15 13:38:09 +01005506 /* status - it is not inherited by specification, but it does not make sense to have
5507 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005508 if (!parent || parent->nodetype != LYS_CHOICE) {
5509 /* in case of choice/case's children, postpone the check to the moment we know if
5510 * 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 +01005511 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 +01005512 }
5513
Radek Krejciec4da802019-05-02 13:02:41 +02005514 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005515 node->sp = node_p;
5516 }
5517 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005518 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5519 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005520 if (node_p->when) {
5521 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005522 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005523 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005524 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005525 }
Radek Krejciec4da802019-05-02 13:02:41 +02005526 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005527
5528 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005529 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005530
Radek Krejci0935f412019-08-20 16:15:18 +02005531 COMPILE_EXTS_GOTO(ctx, node_p->exts, node->exts, node, LYEXT_PAR_NODE, ret, error);
5532
Radek Krejcife909632019-02-12 15:34:42 +01005533 /* inherit LYS_MAND_TRUE in parent containers */
5534 if (node->flags & LYS_MAND_TRUE) {
5535 lys_compile_mandatory_parents(parent, 1);
5536 }
5537
Radek Krejci327de162019-06-14 12:52:07 +02005538 lysc_update_path(ctx, NULL, NULL);
5539
Radek Krejci19a96102018-11-15 13:38:09 +01005540 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005541 if (parent) {
5542 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005543 if (node_p->parent->nodetype == LYS_CASE) {
5544 lysc_update_path(ctx, parent, node_p->parent->name);
5545 } else {
5546 lysc_update_path(ctx, parent, node->name);
5547 }
Radek Krejciec4da802019-05-02 13:02:41 +02005548 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005549 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005550 if (uses_status) {
5551
5552 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005553 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005554 * it directly gets the same status flags as the choice;
5555 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005556 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005557 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005558 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005559 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005560 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005561 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005562 }
Radek Krejciec4da802019-05-02 13:02:41 +02005563 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 +02005564
5565 if (parent->nodetype == LYS_CHOICE) {
5566 lysc_update_path(ctx, NULL, NULL);
5567 }
Radek Krejci19a96102018-11-15 13:38:09 +01005568 } else {
5569 /* top-level element */
5570 if (!ctx->mod->compiled->data) {
5571 ctx->mod->compiled->data = node;
5572 } else {
5573 /* insert at the end of the module's top-level nodes list */
5574 ctx->mod->compiled->data->prev->next = node;
5575 node->prev = ctx->mod->compiled->data->prev;
5576 ctx->mod->compiled->data->prev = node;
5577 }
Radek Krejci327de162019-06-14 12:52:07 +02005578 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005579 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5580 ctx->mod->compiled->notifs, node->name, node)) {
5581 return LY_EVALID;
5582 }
Radek Krejci19a96102018-11-15 13:38:09 +01005583 }
Radek Krejci327de162019-06-14 12:52:07 +02005584 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005585
5586 return LY_SUCCESS;
5587
5588error:
5589 lysc_node_free(ctx->ctx, node);
5590 return ret;
5591}
5592
Radek Krejciccd20f12019-02-15 14:12:27 +01005593static void
5594lysc_disconnect(struct lysc_node *node)
5595{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005596 struct lysc_node *parent, *child, *prev = NULL, *next;
5597 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005598 int remove_cs = 0;
5599
5600 parent = node->parent;
5601
5602 /* parent's first child */
5603 if (!parent) {
5604 return;
5605 }
5606 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005607 cs = (struct lysc_node_case*)node;
5608 } else if (parent->nodetype == LYS_CASE) {
5609 /* disconnecting some node in a case */
5610 cs = (struct lysc_node_case*)parent;
5611 parent = cs->parent;
5612 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5613 if (child == node) {
5614 if (cs->child == child) {
5615 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5616 /* case with a single child -> remove also the case */
5617 child->parent = NULL;
5618 remove_cs = 1;
5619 } else {
5620 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005621 }
5622 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005623 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005624 }
5625 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005626 if (!remove_cs) {
5627 cs = NULL;
5628 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005629 } else if (lysc_node_children(parent, node->flags) == node) {
5630 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005631 }
5632
5633 if (cs) {
5634 if (remove_cs) {
5635 /* cs has only one child which is being also removed */
5636 lysc_disconnect((struct lysc_node*)cs);
5637 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5638 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005639 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5640 /* default case removed */
5641 ((struct lysc_node_choice*)parent)->dflt = NULL;
5642 }
5643 if (((struct lysc_node_choice*)parent)->cases == cs) {
5644 /* first case removed */
5645 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5646 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005647 if (cs->child) {
5648 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5649 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5650 prev = cs->child->prev;
5651 } /* else all the children are under a single case */
5652 LY_LIST_FOR_SAFE(cs->child, next, child) {
5653 if (child->parent != (struct lysc_node*)cs) {
5654 break;
5655 }
5656 lysc_node_free(node->module->ctx, child);
5657 }
5658 if (prev) {
5659 if (prev->next) {
5660 prev->next = child;
5661 }
5662 if (child) {
5663 child->prev = prev;
5664 } else {
5665 /* link from the first child under the cases */
5666 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5667 }
5668 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005669 }
5670 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005671 }
5672
5673 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005674 if (node->prev->next) {
5675 node->prev->next = node->next;
5676 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005677 if (node->next) {
5678 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005679 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005680 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005681 if (child) {
5682 child->prev = node->prev;
5683 }
5684 } else if (((struct lysc_node_choice*)parent)->cases) {
5685 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005686 }
5687}
5688
5689LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005690lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005691{
Radek Krejcia1911222019-07-22 17:24:50 +02005692 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005693 struct ly_set devs_p = {0};
5694 struct ly_set targets = {0};
5695 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005696 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005697 struct lysc_action *rpcs;
5698 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005699 struct lysp_deviation *dev;
5700 struct lysp_deviate *d, **dp_new;
5701 struct lysp_deviate_add *d_add;
5702 struct lysp_deviate_del *d_del;
5703 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005704 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005705 struct lysc_deviation {
5706 const char *nodeid;
5707 struct lysc_node *target; /* target node of the deviation */
5708 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005709 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005710 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5711 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005712 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005713 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005714 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005715 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005716 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005717 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005718
5719 /* get all deviations from the module and all its submodules ... */
5720 LY_ARRAY_FOR(mod_p->deviations, u) {
5721 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5722 }
5723 LY_ARRAY_FOR(mod_p->includes, v) {
5724 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5725 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5726 }
5727 }
5728 if (!devs_p.count) {
5729 /* nothing to do */
5730 return LY_SUCCESS;
5731 }
5732
Radek Krejci327de162019-06-14 12:52:07 +02005733 lysc_update_path(ctx, NULL, "{deviation}");
5734
Radek Krejciccd20f12019-02-15 14:12:27 +01005735 /* ... and group them by the target node */
5736 devs = calloc(devs_p.count, sizeof *devs);
5737 for (u = 0; u < devs_p.count; ++u) {
5738 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005739 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005740
5741 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005742 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5743 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005744 if (target->nodetype == LYS_ACTION) {
5745 /* move the target pointer to input/output to make them different from the action and
5746 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5747 * back to the RPC/action node due to a better compatibility and decision code in this function.
5748 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5749 if (flags & LYSC_OPT_RPC_INPUT) {
5750 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5751 flags |= LYSC_OPT_INTERNAL;
5752 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5753 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5754 flags |= LYSC_OPT_INTERNAL;
5755 }
5756 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005757 /* insert into the set of targets with duplicity detection */
5758 i = ly_set_add(&targets, target, 0);
5759 if (!devs[i]) {
5760 /* new record */
5761 devs[i] = calloc(1, sizeof **devs);
5762 devs[i]->target = target;
5763 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005764 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005765 }
5766 /* add deviates into the deviation's list of deviates */
5767 for (d = dev->deviates; d; d = d->next) {
5768 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5769 *dp_new = d;
5770 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5771 devs[i]->not_supported = 1;
5772 }
5773 }
Radek Krejci327de162019-06-14 12:52:07 +02005774
5775 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005776 }
5777
5778 /* MACROS for deviates checking */
5779#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5780 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005781 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 +01005782 goto cleanup; \
5783 }
5784
5785#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5786 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005787 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5788 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005789 goto cleanup; \
5790 }
5791
Radek Krejcia1911222019-07-22 17:24:50 +02005792
Radek Krejciccd20f12019-02-15 14:12:27 +01005793#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005794 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5795 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5796 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5797 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5798 goto cleanup; \
5799 }
5800
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005801#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005802 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005803 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005804 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5805 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005806 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005807 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5808 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005809 goto cleanup; \
5810 }
5811
Radek Krejci551b12c2019-02-19 16:11:21 +01005812#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5813 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005815 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5816 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005817 goto cleanup; \
5818 }
5819
Radek Krejciccd20f12019-02-15 14:12:27 +01005820#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5821 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005822 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005823 goto cleanup; \
5824 }
5825
Radek Krejci551b12c2019-02-19 16:11:21 +01005826#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5827 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5828 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005829 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005830 goto cleanup; \
5831 }
5832
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005833#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5834 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5835 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5836 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5837 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 +01005838 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005839 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005840 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005841 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5842 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005843 goto cleanup; \
5844 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005845 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5846 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5847 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5848 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5849 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005850 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005851 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5852 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5853 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005854 }
5855
5856 /* apply deviations */
5857 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005858 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5859 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5860 struct ly_err_item *err = NULL;
5861
5862 dflt = NULL;
5863 changed_type = 0;
5864
Radek Krejci327de162019-06-14 12:52:07 +02005865 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5866
Radek Krejcif538ce52019-03-05 10:46:14 +01005867 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5868 /* fix the target pointer in case of RPC's/action's input/output */
5869 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5870 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5871 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5872 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5873 }
5874 }
5875
Radek Krejciccd20f12019-02-15 14:12:27 +01005876 /* not-supported */
5877 if (devs[u]->not_supported) {
5878 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5879 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5880 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5881 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005882
5883#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5884 if (devs[u]->target->parent) { \
5885 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5886 } else { \
5887 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5888 } \
5889 LY_ARRAY_FOR(ARRAY, x) { \
5890 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5891 } \
5892 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5893 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5894 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5895 LY_ARRAY_DECREMENT(ARRAY); \
5896 }
5897
Radek Krejcif538ce52019-03-05 10:46:14 +01005898 if (devs[u]->target->nodetype == LYS_ACTION) {
5899 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5900 /* remove RPC's/action's input */
5901 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5902 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005903 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5904 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005905 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5906 /* remove RPC's/action's output */
5907 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5908 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005909 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5910 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005911 } else {
5912 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005913 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005914 }
5915 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005916 /* remove Notification */
5917 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005918 } else {
5919 /* remove the target node */
5920 lysc_disconnect(devs[u]->target);
5921 lysc_node_free(ctx->ctx, devs[u]->target);
5922 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005923
Radek Krejci474f9b82019-07-24 11:36:37 +02005924 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5925 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005926 continue;
5927 }
5928
5929 /* list of deviates (not-supported is not present in the list) */
5930 LY_ARRAY_FOR(devs[u]->deviates, v) {
5931 d = devs[u]->deviates[v];
5932
5933 switch (d->mod) {
5934 case LYS_DEV_ADD:
5935 d_add = (struct lysp_deviate_add*)d;
5936 /* [units-stmt] */
5937 if (d_add->units) {
5938 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5939 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5940
5941 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5942 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5943 }
5944
5945 /* *must-stmt */
5946 if (d_add->musts) {
5947 switch (devs[u]->target->nodetype) {
5948 case LYS_CONTAINER:
5949 case LYS_LIST:
5950 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005951 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005952 break;
5953 case LYS_LEAF:
5954 case LYS_LEAFLIST:
5955 case LYS_ANYDATA:
5956 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005957 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005958 break;
5959 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005960 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005961 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005962 break;
5963 case LYS_ACTION:
5964 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5965 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005966 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005967 break;
5968 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5969 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005970 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005971 break;
5972 }
5973 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005974 default:
5975 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005976 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005977 goto cleanup;
5978 }
5979 }
5980
5981 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005982 if (d_add->uniques) {
5983 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5984 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5985 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005986
5987 /* *default-stmt */
5988 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005989 switch (devs[u]->target->nodetype) {
5990 case LYS_LEAF:
5991 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005992 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 +02005993 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005994 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005995 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005996 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5997 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5998 } else {
5999 /* prepare new default value storage */
6000 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01006001 }
Radek Krejcia1911222019-07-22 17:24:50 +02006002 dflt = d_add->dflts[0];
6003 /* parsing is done at the end after possible replace of the leaf's type */
6004
Radek Krejci551b12c2019-02-19 16:11:21 +01006005 /* mark the new default values as leaf's own */
6006 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006007 break;
6008 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006009 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01006010 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02006011 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006012 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006013 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6014 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6015 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006016 }
Radek Krejcia1911222019-07-22 17:24:50 +02006017 LY_ARRAY_FREE(llist->dflts);
6018 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006019 LY_ARRAY_FREE(llist->dflts_mods);
6020 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006021 }
6022 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006023 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006024 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
6025 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01006026 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006027 LY_ARRAY_INCREMENT(llist->dflts_mods);
6028 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006029 LY_ARRAY_INCREMENT(llist->dflts);
6030 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
6031 llist->dflts[x]->realtype = llist->type;
6032 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 +02006033 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006034 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006035 llist->dflts[x]->realtype->refcount++;
6036 if (err) {
6037 ly_err_print(err);
6038 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6039 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
6040 d_add->dflts[x - y], err->msg);
6041 ly_err_free(err);
6042 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006043 if (rc == LY_EINCOMPLETE) {
6044 /* postpone default compilation when the tree is complete */
6045 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6046
6047 /* but in general result is so far ok */
6048 rc = LY_SUCCESS;
6049 }
Radek Krejcia1911222019-07-22 17:24:50 +02006050 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006051 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006052 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01006053 devs[u]->target->flags |= LYS_SET_DFLT;
6054 break;
6055 case LYS_CHOICE:
6056 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
6057 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
6058 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
6059 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02006060 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 +01006061 goto cleanup;
6062 }
6063 break;
6064 default:
6065 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006066 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006067 goto cleanup;
6068 }
6069 }
6070
6071 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006072 if (d_add->flags & LYS_CONFIG_MASK) {
6073 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006074 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006075 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
6076 goto cleanup;
6077 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006078 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02006079 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
6080 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006081 }
Radek Krejci93dcc392019-02-19 10:43:38 +01006082 if (devs[u]->target->flags & LYS_SET_CONFIG) {
6083 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006084 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
6085 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01006086 goto cleanup;
6087 }
Radek Krejci327de162019-06-14 12:52:07 +02006088 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006089 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006090
6091 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006092 if (d_add->flags & LYS_MAND_MASK) {
6093 if (devs[u]->target->flags & LYS_MAND_MASK) {
6094 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006095 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
6096 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01006097 goto cleanup;
6098 }
Radek Krejci327de162019-06-14 12:52:07 +02006099 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006100 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006101
6102 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006103 if (d_add->flags & LYS_SET_MIN) {
6104 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6105 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6106 /* change value */
6107 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
6108 } else if (devs[u]->target->nodetype == LYS_LIST) {
6109 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6110 /* change value */
6111 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
6112 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006113 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006114 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
6115 goto cleanup;
6116 }
6117 if (d_add->min) {
6118 devs[u]->target->flags |= LYS_MAND_TRUE;
6119 }
6120 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006121
6122 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006123 if (d_add->flags & LYS_SET_MAX) {
6124 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6125 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6126 /* change value */
6127 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6128 } else if (devs[u]->target->nodetype == LYS_LIST) {
6129 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6130 /* change value */
6131 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
6132 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006133 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006134 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
6135 goto cleanup;
6136 }
6137 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006138
6139 break;
6140 case LYS_DEV_DELETE:
6141 d_del = (struct lysp_deviate_del*)d;
6142
6143 /* [units-stmt] */
6144 if (d_del->units) {
6145 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006146 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6147 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6148 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6149 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6150 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6151 goto cleanup;
6152 }
6153 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6154 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006155 }
6156
6157 /* *must-stmt */
6158 if (d_del->musts) {
6159 switch (devs[u]->target->nodetype) {
6160 case LYS_CONTAINER:
6161 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006162 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006163 break;
6164 case LYS_LEAF:
6165 case LYS_LEAFLIST:
6166 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006167 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006168 break;
6169 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006170 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006171 break;
6172 case LYS_ACTION:
6173 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6174 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6175 break;
6176 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6177 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6178 break;
6179 }
6180 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006181 default:
6182 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006183 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006184 goto cleanup;
6185 }
6186 }
6187
6188 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006189 if (d_del->uniques) {
6190 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6191 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6192 LY_ARRAY_FOR(d_del->uniques, x) {
6193 LY_ARRAY_FOR(list->uniques, z) {
6194 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6195 nodeid = strpbrk(name, " \t\n");
6196 if (nodeid) {
6197 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
6198 || list->uniques[z][y]->name[nodeid - name] != '\0') {
6199 break;
6200 }
6201 while (isspace(*nodeid)) {
6202 ++nodeid;
6203 }
6204 } else {
6205 if (strcmp(name, list->uniques[z][y]->name)) {
6206 break;
6207 }
6208 }
6209 }
6210 if (!name) {
6211 /* complete match - remove the unique */
6212 LY_ARRAY_DECREMENT(list->uniques);
6213 LY_ARRAY_FREE(list->uniques[z]);
6214 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6215 --z;
6216 break;
6217 }
6218 }
6219 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6220 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006221 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6222 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006223 goto cleanup;
6224 }
6225 }
6226 if (!LY_ARRAY_SIZE(list->uniques)) {
6227 LY_ARRAY_FREE(list->uniques);
6228 list->uniques = NULL;
6229 }
6230 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006231
6232 /* *default-stmt */
6233 if (d_del->dflts) {
6234 switch (devs[u]->target->nodetype) {
6235 case LYS_LEAF:
6236 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6237 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6238 dflt, "deleting", "default", d_del->dflts[0]);
6239
Radek Krejcia1911222019-07-22 17:24:50 +02006240 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006241 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006242 if (strcmp(dflt, d_del->dflts[0])) {
6243 if (i) {
6244 free((char*)dflt);
6245 }
6246 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6247 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6248 d_del->dflts[0], dflt);
6249 goto cleanup;
6250 }
6251 if (i) {
6252 free((char*)dflt);
6253 }
6254 dflt = NULL;
6255
Radek Krejci474f9b82019-07-24 11:36:37 +02006256 /* update the list of incomplete default values if needed */
6257 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6258
Radek Krejcia1911222019-07-22 17:24:50 +02006259 /* remove the default specification */
6260 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6261 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6262 free(leaf->dflt);
6263 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006264 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006265 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006266 break;
6267 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006268 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6269 LY_ARRAY_FOR(d_del->dflts, x) {
6270 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006271 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 +02006272 if (!strcmp(dflt, d_del->dflts[x])) {
6273 if (i) {
6274 free((char*)dflt);
6275 }
6276 dflt = NULL;
6277 break;
6278 }
6279 if (i) {
6280 free((char*)dflt);
6281 }
6282 dflt = NULL;
6283 }
6284 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6285 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6286 "which does not match any of the target's property values.", d_del->dflts[x]);
6287 goto cleanup;
6288 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006289
6290 /* update the list of incomplete default values if needed */
6291 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6292
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006293 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006294 LY_ARRAY_DECREMENT(llist->dflts);
6295 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6296 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6297 free(llist->dflts[y]);
6298 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006299 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 +02006300 }
6301 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006302 LY_ARRAY_FREE(llist->dflts_mods);
6303 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006304 LY_ARRAY_FREE(llist->dflts);
6305 llist->dflts = NULL;
6306 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006307 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006308 break;
6309 case LYS_CHOICE:
6310 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6311 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6312 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006313 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006314 if (prefix) {
6315 /* use module prefixes from the deviation module to match the module of the default case */
6316 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6317 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006318 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6319 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006320 goto cleanup;
6321 }
6322 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6323 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006324 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6325 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006326 goto cleanup;
6327 }
6328 }
6329 /* else {
6330 * strictly, the default prefix would point to the deviation module, but the value should actually
6331 * match the default string in the original module (usually unprefixed), so in this case we do not check
6332 * the module of the default case, just matching its name */
6333 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6334 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006335 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6336 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006337 goto cleanup;
6338 }
6339 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6340 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6341 break;
6342 default:
6343 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006344 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006345 goto cleanup;
6346 }
6347 }
6348
6349 break;
6350 case LYS_DEV_REPLACE:
6351 d_rpl = (struct lysp_deviate_rpl*)d;
6352
6353 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006354 if (d_rpl->type) {
6355 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6356 /* type is mandatory, so checking for its presence is not necessary */
6357 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006358
6359 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6360 /* the target has default from the previous type - remove it */
6361 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006362 /* update the list of incomplete default values if needed */
6363 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6364
Radek Krejcia1911222019-07-22 17:24:50 +02006365 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6366 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6367 free(leaf->dflt);
6368 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006369 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006370 } else { /* LYS_LEAFLIST */
6371 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006372 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006373 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6374 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6375 free(llist->dflts[x]);
6376 }
6377 LY_ARRAY_FREE(llist->dflts);
6378 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006379 LY_ARRAY_FREE(llist->dflts_mods);
6380 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006381 }
6382 }
6383 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006384 /* there is no default value, do not set changed_type after type compilation
6385 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006386 changed_type = -1;
6387 }
Radek Krejciec4da802019-05-02 13:02:41 +02006388 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 +02006389 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006390 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006391
6392 /* [units-stmt] */
6393 if (d_rpl->units) {
6394 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6395 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6396 units, "replacing", "units", d_rpl->units);
6397
6398 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6399 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6400 }
6401
6402 /* [default-stmt] */
6403 if (d_rpl->dflt) {
6404 switch (devs[u]->target->nodetype) {
6405 case LYS_LEAF:
6406 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6407 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006408 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006409 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006410 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6411 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6412 dflt = d_rpl->dflt;
6413 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006414 break;
6415 case LYS_CHOICE:
6416 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006417 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 +01006418 goto cleanup;
6419 }
6420 break;
6421 default:
6422 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006423 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006424 goto cleanup;
6425 }
6426 }
6427
6428 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006429 if (d_rpl->flags & LYS_CONFIG_MASK) {
6430 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006431 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006432 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6433 goto cleanup;
6434 }
6435 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006436 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006437 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6438 goto cleanup;
6439 }
Radek Krejci327de162019-06-14 12:52:07 +02006440 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006441 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006442
6443 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006444 if (d_rpl->flags & LYS_MAND_MASK) {
6445 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006447 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6448 goto cleanup;
6449 }
Radek Krejci327de162019-06-14 12:52:07 +02006450 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006451 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006452
6453 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006454 if (d_rpl->flags & LYS_SET_MIN) {
6455 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6456 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6457 /* change value */
6458 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6459 } else if (devs[u]->target->nodetype == LYS_LIST) {
6460 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6461 /* change value */
6462 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6463 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006464 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006465 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6466 goto cleanup;
6467 }
6468 if (d_rpl->min) {
6469 devs[u]->target->flags |= LYS_MAND_TRUE;
6470 }
6471 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006472
6473 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006474 if (d_rpl->flags & LYS_SET_MAX) {
6475 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6476 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6477 /* change value */
6478 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6479 } else if (devs[u]->target->nodetype == LYS_LIST) {
6480 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6481 /* change value */
6482 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6483 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006484 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006485 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6486 goto cleanup;
6487 }
6488 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006489
6490 break;
6491 default:
6492 LOGINT(ctx->ctx);
6493 goto cleanup;
6494 }
6495 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006496
Radek Krejci33f72892019-02-21 10:36:58 +01006497 /* final check when all deviations of a single target node are applied */
6498
Radek Krejci551b12c2019-02-19 16:11:21 +01006499 /* check min-max compatibility */
6500 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6501 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6502 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6503 } else if (devs[u]->target->nodetype == LYS_LIST) {
6504 min = ((struct lysc_node_list*)devs[u]->target)->min;
6505 max = ((struct lysc_node_list*)devs[u]->target)->max;
6506 } else {
6507 min = max = 0;
6508 }
6509 if (min > max) {
6510 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 +02006511 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006512 goto cleanup;
6513 }
6514
Radek Krejcia1911222019-07-22 17:24:50 +02006515 if (dflt) {
6516 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006517 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006518 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006519 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6520 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006521 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006522 leaf->dflt->realtype->refcount++;
6523 if (err) {
6524 ly_err_print(err);
6525 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6526 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6527 ly_err_free(err);
6528 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006529 if (rc == LY_EINCOMPLETE) {
6530 /* postpone default compilation when the tree is complete */
6531 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6532
6533 /* but in general result is so far ok */
6534 rc = LY_SUCCESS;
6535 }
Radek Krejcia1911222019-07-22 17:24:50 +02006536 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006537 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006538 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6539 int dynamic;
6540 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006541 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006542
6543 /* update the list of incomplete default values if needed */
6544 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6545
6546 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006547 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6548 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6549 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006550 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6551 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006552 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006553 leaf->dflt->realtype->refcount++;
6554 if (err) {
6555 ly_err_print(err);
6556 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6557 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6558 ly_err_free(err);
6559 }
6560 if (dynamic) {
6561 free((void*)dflt);
6562 }
Radek Krejcia1911222019-07-22 17:24:50 +02006563 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006564 if (rc == LY_EINCOMPLETE) {
6565 /* postpone default compilation when the tree is complete */
6566 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6567
6568 /* but in general result is so far ok */
6569 rc = LY_SUCCESS;
6570 }
6571 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006572 } else { /* LYS_LEAFLIST */
6573 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006574 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 +02006575 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6576 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6577 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006578 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6579 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006580 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6581 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006582 llist->dflts[x]->realtype->refcount++;
6583 if (err) {
6584 ly_err_print(err);
6585 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6586 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6587 dflt, err->msg);
6588 ly_err_free(err);
6589 }
6590 if (dynamic) {
6591 free((void*)dflt);
6592 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006593 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006594 if (rc == LY_EINCOMPLETE) {
6595 /* postpone default compilation when the tree is complete */
6596 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6597
6598 /* but in general result is so far ok */
6599 rc = LY_SUCCESS;
6600 }
Radek Krejcia1911222019-07-22 17:24:50 +02006601 LY_CHECK_GOTO(rc, cleanup);
6602 }
6603 }
6604 }
6605
Radek Krejci551b12c2019-02-19 16:11:21 +01006606 /* check mandatory - default compatibility */
6607 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6608 && (devs[u]->target->flags & LYS_SET_DFLT)
6609 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6610 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006611 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006612 goto cleanup;
6613 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6614 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6615 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006616 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 +01006617 goto cleanup;
6618 }
6619 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6620 /* mandatory node under a default case */
6621 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006622 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6623 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006624 goto cleanup;
6625 }
Radek Krejci33f72892019-02-21 10:36:58 +01006626
Radek Krejci327de162019-06-14 12:52:07 +02006627 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006628 }
6629
Radek Krejci327de162019-06-14 12:52:07 +02006630 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006631 ret = LY_SUCCESS;
6632
6633cleanup:
6634 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6635 LY_ARRAY_FREE(devs[u]->deviates);
6636 free(devs[u]);
6637 }
6638 free(devs);
6639 ly_set_erase(&targets, NULL);
6640 ly_set_erase(&devs_p, NULL);
6641
6642 return ret;
6643}
6644
Radek Krejcib56c7502019-02-13 14:19:54 +01006645/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006646 * @brief Compile the given YANG submodule into the main module.
6647 * @param[in] ctx Compile context
6648 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006649 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6650 */
6651LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006652lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006653{
6654 unsigned int u;
6655 LY_ERR ret = LY_SUCCESS;
6656 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006657 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006658 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006659 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006660
Radek Krejci0af46292019-01-11 16:02:31 +01006661 if (!mainmod->mod->off_features) {
6662 /* features are compiled directly into the compiled module structure,
6663 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6664 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci0935f412019-08-20 16:15:18 +02006665 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features, &mainmod->features);
6666 LY_CHECK_GOTO(ret, error);
6667 }
6668 if (!mainmod->mod->off_extensions) {
6669 /* extensions are compiled directly into the compiled module structure, compilation is finished in the main module (lys_compile()). */
6670 ret = lys_extension_precompile(ctx, NULL, NULL, submod->extensions, &mainmod->extensions);
Radek Krejci0af46292019-01-11 16:02:31 +01006671 LY_CHECK_GOTO(ret, error);
6672 }
6673
Radek Krejci327de162019-06-14 12:52:07 +02006674 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006675 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006676 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006677
Radek Krejci474f9b82019-07-24 11:36:37 +02006678 /* data nodes */
6679 LY_LIST_FOR(submod->data, node_p) {
6680 ret = lys_compile_node(ctx, node_p, NULL, 0);
6681 LY_CHECK_GOTO(ret, error);
6682 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006683
Radek Krejciec4da802019-05-02 13:02:41 +02006684 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6685 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006686
Radek Krejcid05cbd92018-12-05 14:26:40 +01006687error:
6688 return ret;
6689}
6690
Radek Krejci335332a2019-09-05 13:03:35 +02006691static void *
6692lys_compile_extension_instance_storage(enum ly_stmt stmt, struct lysc_ext_substmt *substmts)
6693{
6694 for (unsigned int u = 0; substmts[u].stmt; ++u) {
6695 if (substmts[u].stmt == stmt) {
6696 return substmts[u].storage;
6697 }
6698 }
6699 return NULL;
6700}
6701
6702LY_ERR
6703lys_compile_extension_instance(struct lysc_ctx *ctx, const struct lysp_ext_instance *ext, struct lysc_ext_substmt *substmts)
6704{
6705 LY_ERR ret = LY_EVALID, r;
6706 unsigned int u;
6707 struct lysp_stmt *stmt;
6708 void *parsed = NULL, **compiled = NULL;
Radek Krejci335332a2019-09-05 13:03:35 +02006709
6710 /* check for invalid substatements */
6711 for (stmt = ext->child; stmt; stmt = stmt->next) {
Radek Krejcif56e2a42019-09-09 14:15:25 +02006712 if (stmt->flags & (LYS_YIN_ATTR | LYS_YIN_ARGUMENT)) {
6713 continue;
6714 }
Radek Krejci335332a2019-09-05 13:03:35 +02006715 for (u = 0; substmts[u].stmt; ++u) {
6716 if (substmts[u].stmt == stmt->kw) {
6717 break;
6718 }
6719 }
6720 if (!substmts[u].stmt) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006721 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid keyword \"%s\" as a child of \"%s%s%s\" extension instance.",
6722 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006723 goto cleanup;
6724 }
Radek Krejci335332a2019-09-05 13:03:35 +02006725 }
6726
Radek Krejciad5963b2019-09-06 16:03:05 +02006727 /* TODO store inherited data, e.g. status first, but mark them somehow to allow to overwrite them and not detect duplicity */
6728
Radek Krejci335332a2019-09-05 13:03:35 +02006729 /* keep order of the processing the same as the order in the defined substmts,
6730 * the order is important for some of the statements depending on others (e.g. type needs status and units) */
6731 for (u = 0; substmts[u].stmt; ++u) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006732 int stmt_present = 0;
6733
Radek Krejci335332a2019-09-05 13:03:35 +02006734 for (stmt = ext->child; stmt; stmt = stmt->next) {
6735 if (substmts[u].stmt != stmt->kw) {
6736 continue;
6737 }
6738
Radek Krejciad5963b2019-09-06 16:03:05 +02006739 stmt_present = 1;
Radek Krejci335332a2019-09-05 13:03:35 +02006740 if (substmts[u].storage) {
6741 switch (stmt->kw) {
Radek Krejciad5963b2019-09-06 16:03:05 +02006742 case LY_STMT_STATUS:
6743 assert(substmts[u].cardinality < LY_STMT_CARD_SOME);
6744 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &substmts[u].storage, /* TODO */ NULL), ret = r, cleanup);
6745 break;
6746 case LY_STMT_UNITS: {
6747 const char **units;
6748
6749 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6750 /* single item */
6751 if (*((const char **)substmts[u].storage)) {
6752 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6753 goto cleanup;
6754 }
6755 units = (const char **)substmts[u].storage;
6756 } else {
6757 /* sized array */
6758 const char ***units_array = (const char ***)substmts[u].storage;
6759 LY_ARRAY_NEW_GOTO(ctx->ctx, *units_array, units, ret, cleanup);
6760 }
6761 *units = lydict_insert(ctx->ctx, stmt->arg, 0);
6762 break;
6763 }
Radek Krejci335332a2019-09-05 13:03:35 +02006764 case LY_STMT_TYPE: {
6765 uint16_t *flags = lys_compile_extension_instance_storage(LY_STMT_STATUS, substmts);
6766 const char **units = lys_compile_extension_instance_storage(LY_STMT_UNITS, substmts);
6767
6768 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6769 /* single item */
Radek Krejciad5963b2019-09-06 16:03:05 +02006770 if (*(struct lysc_type**)substmts[u].storage) {
Radek Krejci335332a2019-09-05 13:03:35 +02006771 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6772 goto cleanup;
6773 }
6774 compiled = substmts[u].storage;
6775 } else {
6776 /* sized array */
6777 struct lysc_type ***types = (struct lysc_type***)substmts[u].storage, **type = NULL;
6778 LY_ARRAY_NEW_GOTO(ctx->ctx, *types, type, ret, cleanup);
6779 compiled = (void*)type;
6780 }
6781
Radek Krejciad5963b2019-09-06 16:03:05 +02006782 LY_CHECK_ERR_GOTO(r = lysp_stmt_parse(ctx, stmt, stmt->kw, &parsed, NULL), ret = r, cleanup);
Radek Krejci335332a2019-09-05 13:03:35 +02006783 LY_CHECK_ERR_GOTO(r = lys_compile_type(ctx, ext->parent_type == LYEXT_PAR_NODE ? ((struct lysc_node*)ext->parent)->sp : NULL,
6784 flags ? *flags : 0, ctx->mod_def->parsed, ext->name, parsed, (struct lysc_type**)compiled,
Radek Krejci38d85362019-09-05 16:26:38 +02006785 units && !*units ? units : NULL), lysp_type_free(ctx->ctx, parsed); free(parsed); ret = r, cleanup);
6786 lysp_type_free(ctx->ctx, parsed);
6787 free(parsed);
Radek Krejci335332a2019-09-05 13:03:35 +02006788 break;
6789 }
Radek Krejciad5963b2019-09-06 16:03:05 +02006790 case LY_STMT_IF_FEATURE: {
6791 struct lysc_iffeature *iff = NULL;
6792
6793 if (substmts[u].cardinality < LY_STMT_CARD_SOME) {
6794 /* single item */
6795 if (((struct lysc_iffeature*)substmts[u].storage)->features) {
6796 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPSTMT, stmt->stmt);
6797 goto cleanup;
6798 }
6799 iff = (struct lysc_iffeature*)substmts[u].storage;
6800 } else {
6801 /* sized array */
6802 struct lysc_iffeature **iffs = (struct lysc_iffeature**)substmts[u].storage;
6803 LY_ARRAY_NEW_GOTO(ctx->ctx, *iffs, iff, ret, cleanup);
6804 }
6805 LY_CHECK_ERR_GOTO(r = lys_compile_iffeature(ctx, &stmt->arg, iff), ret = r, cleanup);
6806 break;
6807 }
6808 /* TODO support other substatements (parse stmt to lysp and then compile lysp to lysc),
6809 * also note that in many statements their extensions are not taken into account */
Radek Krejci335332a2019-09-05 13:03:35 +02006810 default:
Radek Krejciad5963b2019-09-06 16:03:05 +02006811 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.",
6812 stmt->stmt, ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
Radek Krejci335332a2019-09-05 13:03:35 +02006813 goto cleanup;
6814 }
6815 }
Radek Krejci335332a2019-09-05 13:03:35 +02006816 }
Radek Krejci335332a2019-09-05 13:03:35 +02006817
Radek Krejciad5963b2019-09-06 16:03:05 +02006818 if ((substmts[u].cardinality == LY_STMT_CARD_MAND || substmts[u].cardinality == LY_STMT_CARD_SOME) && !stmt_present) {
6819 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Missing mandatory keyword \"%s\" as a child of \"%s%s%s\".",
6820 ly_stmt2str(substmts[u].stmt), ext->name, ext->argument ? " " : "", ext->argument ? ext->argument : "");
6821 goto cleanup;
6822 }
Radek Krejci335332a2019-09-05 13:03:35 +02006823 }
6824
6825 ret = LY_SUCCESS;
6826
6827cleanup:
Radek Krejci335332a2019-09-05 13:03:35 +02006828 return ret;
6829}
6830
Radek Krejci19a96102018-11-15 13:38:09 +01006831LY_ERR
6832lys_compile(struct lys_module *mod, int options)
6833{
6834 struct lysc_ctx ctx = {0};
6835 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006836 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006837 struct lysp_module *sp;
6838 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006839 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006840 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006841 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006842 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006843 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006844
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006845 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006846
6847 if (!mod->implemented) {
6848 /* just imported modules are not compiled */
6849 return LY_SUCCESS;
6850 }
6851
Radek Krejci19a96102018-11-15 13:38:09 +01006852 sp = mod->parsed;
6853
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006854 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006855 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006856 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006857 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006858 ctx.path_len = 1;
6859 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006860
6861 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006862 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6863 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006864
Radek Krejciec4da802019-05-02 13:02:41 +02006865 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006866 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006867 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006868 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6869 }
Radek Krejci0935f412019-08-20 16:15:18 +02006870
6871 /* features */
Radek Krejci0af46292019-01-11 16:02:31 +01006872 if (mod->off_features) {
6873 /* there is already precompiled array of features */
6874 mod_c->features = mod->off_features;
6875 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006876 } else {
6877 /* features are compiled directly into the compiled module structure,
6878 * 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 +02006879 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006880 LY_CHECK_GOTO(ret, error);
6881 }
6882 /* finish feature compilation, not only for the main module, but also for the submodules.
6883 * Due to possible forward references, it must be done when all the features (including submodules)
6884 * are present. */
6885 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006886 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006887 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6888 }
Radek Krejci327de162019-06-14 12:52:07 +02006889 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006890 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006891 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006892 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006893 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006894 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6895 }
Radek Krejci327de162019-06-14 12:52:07 +02006896 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006897 }
Radek Krejci327de162019-06-14 12:52:07 +02006898 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006899
Radek Krejci0935f412019-08-20 16:15:18 +02006900 /* extensions */
6901 /* 2-steps: a) prepare compiled structures and ... */
6902 if (mod->off_extensions) {
6903 /* there is already precompiled array of extension definitions */
6904 mod_c->extensions = mod->off_extensions;
6905 mod->off_extensions = NULL;
6906 } else {
6907 /* extension definitions are compiled directly into the compiled module structure */
6908 ret = lys_extension_precompile(&ctx, NULL, NULL, sp->extensions, &mod_c->extensions);
Radek Krejcibfb2e142019-09-09 14:47:44 +02006909 LY_CHECK_GOTO(ret, error);
Radek Krejci0935f412019-08-20 16:15:18 +02006910 }
6911 /* ... b) connect the extension definitions with the appropriate extension plugins */
6912 lys_compile_extension_plugins(mod_c->extensions);
6913
6914 /* identities */
Radek Krejci327de162019-06-14 12:52:07 +02006915 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006916 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006917 if (sp->identities) {
6918 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6919 }
Radek Krejci327de162019-06-14 12:52:07 +02006920 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006921
Radek Krejci95710c92019-02-11 15:49:55 +01006922 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006923 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02006924 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01006925 LY_CHECK_GOTO(ret, error);
6926 }
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 Krejci3641f562019-02-13 15:38:40 +01006932 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01006933 LY_CHECK_GOTO(ret, error);
6934 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006935 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006936 LY_CHECK_GOTO(ret, error);
6937 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006938
Radek Krejci474f9b82019-07-24 11:36:37 +02006939 /* deviations TODO cover deviations from submodules */
Radek Krejciec4da802019-05-02 13:02:41 +02006940 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01006941 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006942
Radek Krejci0935f412019-08-20 16:15:18 +02006943 /* extension instances TODO cover extension instances from submodules */
6944 COMPILE_EXTS_GOTO(&ctx, sp->exts, mod_c->exts, mod_c, LYEXT_PAR_MODULE, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006945
Radek Krejcia3045382018-11-22 14:30:31 +01006946 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006947 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6948 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6949 * 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 +01006950 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006951 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006952 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6953 if (type->basetype == LY_TYPE_LEAFREF) {
6954 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006955 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]), (struct lysc_type_leafref*)type);
Radek Krejcia3045382018-11-22 14:30:31 +01006956 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006957 } else if (type->basetype == LY_TYPE_UNION) {
6958 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6959 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6960 /* validate the path */
6961 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6962 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
6963 LY_CHECK_GOTO(ret, error);
6964 }
6965 }
Radek Krejcia3045382018-11-22 14:30:31 +01006966 }
6967 }
6968 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006969 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006970 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006971 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6972 if (type->basetype == LY_TYPE_LEAFREF) {
6973 /* store pointer to the real type */
6974 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6975 typeiter->basetype == LY_TYPE_LEAFREF;
6976 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6977 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006978 } else if (type->basetype == LY_TYPE_UNION) {
6979 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6980 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6981 /* store pointer to the real type */
6982 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6983 typeiter->basetype == LY_TYPE_LEAFREF;
6984 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6985 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6986 }
6987 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006988 }
6989 }
6990 }
Radek Krejciec4da802019-05-02 13:02:41 +02006991
Radek Krejci474f9b82019-07-24 11:36:37 +02006992 /* finish incomplete default values compilation */
6993 for (u = 0; u < ctx.dflts.count; ++u) {
6994 struct ly_err_item *err = NULL;
6995 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
6996 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized),
6997 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
6998 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
6999 if (err) {
7000 ly_err_print(err);
7001 ctx.path[0] = '\0';
7002 lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
7003 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
7004 "Invalid default - value does not fit the type (%s).", err->msg);
7005 ly_err_free(err);
7006 }
7007 LY_CHECK_GOTO(ret, error);
7008 }
7009
Radek Krejcif2de0ed2019-05-02 14:13:18 +02007010 /* validate non-instantiated groupings from the parsed schema,
7011 * without it we would accept even the schemas with invalid grouping specification */
7012 ctx.options |= LYSC_OPT_GROUPING;
7013 LY_ARRAY_FOR(sp->groupings, u) {
7014 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
7015 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
7016 }
7017 }
7018 LY_LIST_FOR(sp->data, node_p) {
7019 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
7020 LY_ARRAY_FOR(grps, u) {
7021 if (!(grps[u].flags & LYS_USED_GRP)) {
7022 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
7023 }
7024 }
7025 }
7026
Radek Krejci474f9b82019-07-24 11:36:37 +02007027 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
7028 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
7029 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
7030 }
7031
Radek Krejci1c0c3442019-07-23 16:08:47 +02007032 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007033 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007034 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007035 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007036 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01007037
Radek Krejciec4da802019-05-02 13:02:41 +02007038 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01007039 lysp_module_free(mod->parsed);
7040 ((struct lys_module*)mod)->parsed = NULL;
7041 }
7042
Radek Krejciec4da802019-05-02 13:02:41 +02007043 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01007044 /* remove flag of the modules implemented by dependency */
7045 for (u = 0; u < ctx.ctx->list.count; ++u) {
7046 m = ctx.ctx->list.objs[u];
7047 if (m->implemented == 2) {
7048 m->implemented = 1;
7049 }
7050 }
7051 }
7052
Radek Krejci19a96102018-11-15 13:38:09 +01007053 ((struct lys_module*)mod)->compiled = mod_c;
7054 return LY_SUCCESS;
7055
7056error:
Radek Krejci95710c92019-02-11 15:49:55 +01007057 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02007058 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01007059 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01007060 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02007061 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007062 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01007063 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01007064 mod->compiled = NULL;
7065
7066 /* revert compilation of modules implemented by dependency */
7067 for (u = 0; u < ctx.ctx->list.count; ++u) {
7068 m = ctx.ctx->list.objs[u];
7069 if (m->implemented == 2) {
7070 /* revert features list to the precompiled state */
7071 lys_feature_precompile_revert(&ctx, m);
7072 /* mark module as imported-only / not-implemented */
7073 m->implemented = 0;
7074 /* free the compiled version of the module */
7075 lysc_module_free(m->compiled, NULL);
7076 m->compiled = NULL;
7077 }
7078 }
7079
Radek Krejci19a96102018-11-15 13:38:09 +01007080 return ret;
7081}