blob: 2e3e36a9e8d65cead50d43aa3c1bb7201bff6ac1 [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"
27#include "set.h"
28#include "plugins_types.h"
29#include "tree.h"
30#include "tree_schema.h"
Radek Krejci19a96102018-11-15 13:38:09 +010031#include "tree_schema_internal.h"
32#include "xpath.h"
33
34/**
35 * @brief Duplicate string into dictionary
36 * @param[in] CTX libyang context of the dictionary.
37 * @param[in] ORIG String to duplicate.
38 * @param[out] DUP Where to store the result.
39 */
40#define DUP_STRING(CTX, ORIG, DUP) if (ORIG) {DUP = lydict_insert(CTX, ORIG, 0);}
41
Radek Krejciec4da802019-05-02 13:02:41 +020042#define COMPILE_ARRAY_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010043 if (ARRAY_P) { \
44 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010045 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010046 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
47 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020048 RET = FUNC(CTX, &(ARRAY_P)[ITER], &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejcid05cbd92018-12-05 14:26:40 +010049 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
50 } \
51 }
52
Radek Krejciec4da802019-05-02 13:02:41 +020053#define COMPILE_ARRAY1_GOTO(CTX, ARRAY_P, ARRAY_C, PARENT, ITER, FUNC, USES_STATUS, RET, GOTO) \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010054 if (ARRAY_P) { \
55 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
56 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
57 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
58 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020059 RET = FUNC(CTX, &(ARRAY_P)[ITER], PARENT, &(ARRAY_C)[ITER + __array_offset], USES_STATUS); \
Radek Krejci6eeb58f2019-02-22 16:29:37 +010060 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
61 } \
62 }
63
Radek Krejciec4da802019-05-02 13:02:41 +020064#define COMPILE_ARRAY_UNIQUE_GOTO(CTX, ARRAY_P, ARRAY_C, ITER, FUNC, RET, GOTO) \
Radek Krejcid05cbd92018-12-05 14:26:40 +010065 if (ARRAY_P) { \
66 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, LY_ARRAY_SIZE(ARRAY_P), RET, GOTO); \
67 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
68 for (ITER = 0; ITER < LY_ARRAY_SIZE(ARRAY_P); ++ITER) { \
69 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020070 RET = FUNC(CTX, &(ARRAY_P)[ITER], ARRAY_C, &(ARRAY_C)[ITER + __array_offset]); \
Radek Krejci19a96102018-11-15 13:38:09 +010071 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
72 } \
73 }
74
Radek Krejciec4da802019-05-02 13:02:41 +020075#define COMPILE_MEMBER_GOTO(CTX, MEMBER_P, MEMBER_C, FUNC, RET, GOTO) \
Radek Krejci19a96102018-11-15 13:38:09 +010076 if (MEMBER_P) { \
77 MEMBER_C = calloc(1, sizeof *(MEMBER_C)); \
78 LY_CHECK_ERR_GOTO(!(MEMBER_C), LOGMEM((CTX)->ctx); RET = LY_EMEM, GOTO); \
Radek Krejciec4da802019-05-02 13:02:41 +020079 RET = FUNC(CTX, MEMBER_P, MEMBER_C); \
Radek Krejci19a96102018-11-15 13:38:09 +010080 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
81 }
82
Radek Krejciec4da802019-05-02 13:02:41 +020083#define COMPILE_MEMBER_ARRAY_GOTO(CTX, MEMBER_P, ARRAY_C, FUNC, RET, GOTO) \
Radek Krejci00b874b2019-02-12 10:54:50 +010084 if (MEMBER_P) { \
85 LY_ARRAY_CREATE_GOTO((CTX)->ctx, ARRAY_C, 1, RET, GOTO); \
86 size_t __array_offset = LY_ARRAY_SIZE(ARRAY_C); \
87 LY_ARRAY_INCREMENT(ARRAY_C); \
Radek Krejciec4da802019-05-02 13:02:41 +020088 RET = FUNC(CTX, MEMBER_P, &(ARRAY_C)[__array_offset]); \
Radek Krejci00b874b2019-02-12 10:54:50 +010089 LY_CHECK_GOTO(RET != LY_SUCCESS, GOTO); \
90 }
91
Radek Krejcid05cbd92018-12-05 14:26:40 +010092#define COMPILE_CHECK_UNIQUENESS(CTX, ARRAY, MEMBER, EXCL, STMT, IDENT) \
93 if (ARRAY) { \
Radek Krejci0af46292019-01-11 16:02:31 +010094 for (unsigned int u__ = 0; u__ < LY_ARRAY_SIZE(ARRAY); ++u__) { \
95 if (&(ARRAY)[u__] != EXCL && (void*)((ARRAY)[u__].MEMBER) == (void*)(IDENT)) { \
Radek Krejcid05cbd92018-12-05 14:26:40 +010096 LOGVAL((CTX)->ctx, LY_VLOG_STR, (CTX)->path, LY_VCODE_DUPIDENT, IDENT, STMT); \
97 return LY_EVALID; \
98 } \
99 } \
100 }
101
Radek Krejci19a96102018-11-15 13:38:09 +0100102static struct lysc_ext_instance *
103lysc_ext_instance_dup(struct ly_ctx *ctx, struct lysc_ext_instance *orig)
104{
Radek Krejcie7b95092019-05-15 11:03:07 +0200105 /* TODO - extensions */
Radek Krejci19a96102018-11-15 13:38:09 +0100106 (void) ctx;
107 (void) orig;
108 return NULL;
109}
110
Radek Krejcib56c7502019-02-13 14:19:54 +0100111/**
Radek Krejci474f9b82019-07-24 11:36:37 +0200112 * @brief Add record into the compile context's list of incomplete default values.
113 * @param[in] ctx Compile context with the incomplete default values list.
114 * @param[in] context_node Context schema node to store in the record.
115 * @param[in] dflt Incomplete default value to store in the record.
116 * @param[in] dflt_mod Module of the default value definition to store in the record.
117 * @return LY_EMEM in case of memory allocation failure.
118 * @return LY_SUCCESS
119 */
120static LY_ERR
121lysc_incomplete_dflts_add(struct lysc_ctx *ctx, struct lysc_node *context_node, struct lyd_value *dflt, struct lys_module *dflt_mod)
122{
123 struct lysc_incomplete_dflt *r;
124 r = malloc(sizeof *r);
125 LY_CHECK_ERR_RET(!r, LOGMEM(ctx->ctx), LY_EMEM);
126 r->context_node = context_node;
127 r->dflt = dflt;
128 r->dflt_mod = dflt_mod;
129 ly_set_add(&ctx->dflts, r, LY_SET_OPT_USEASLIST);
130
131 return LY_SUCCESS;
132}
133
134/**
135 * @brief Remove record of the given default value from the compile context's list of incomplete default values.
136 * @param[in] ctx Compile context with the incomplete default values list.
137 * @param[in] dflt Incomplete default values identifying the record to remove.
138 */
139static void
140lysc_incomplete_dflts_remove(struct lysc_ctx *ctx, struct lyd_value *dflt)
141{
142 unsigned int u;
143 struct lysc_incomplete_dflt *r;
144
145 for (u = 0; u < ctx->dflts.count; ++u) {
146 r = (struct lysc_incomplete_dflt*)ctx->dflts.objs[u];
147 if (r->dflt == dflt) {
148 free(ctx->dflts.objs[u]);
149 memmove(&ctx->dflts.objs[u], &ctx->dflts.objs[u + 1], (ctx->dflts.count - (u + 1)) * sizeof *ctx->dflts.objs);
150 --ctx->dflts.count;
151 return;
152 }
153 }
154}
155
156/**
Radek Krejci327de162019-06-14 12:52:07 +0200157 * @brief Update path in the compile context.
158 *
159 * @param[in] ctx Compile context with the path.
160 * @param[in] parent Parent of the current node to check difference of the node's module. The current module is taken from lysc_ctx::mod.
161 * @param[in] name Name of the node to update path with. If NULL, the last segment is removed. If the format is `{keyword}`, the following
162 * call updates the segment to the form `{keyword='name'}` (to remove this compound segment, 2 calls with NULL @p name must be used).
163 */
164static void
165lysc_update_path(struct lysc_ctx *ctx, struct lysc_node *parent, const char *name)
166{
167 int len;
168 int nextlevel = 0; /* 0 - no starttag, 1 - '/' starttag, 2 - '=' starttag + '}' endtag */
169
170 if (!name) {
171 /* removing last path segment */
172 if (ctx->path[ctx->path_len - 1] == '}') {
173 for (; ctx->path[ctx->path_len] != '=' && ctx->path[ctx->path_len] != '{'; --ctx->path_len);
174 if (ctx->path[ctx->path_len] == '=') {
175 ctx->path[ctx->path_len++] = '}';
176 } else {
177 /* not a top-level special tag, remove also preceiding '/' */
178 goto remove_nodelevel;
179 }
180 } else {
181remove_nodelevel:
182 for (; ctx->path[ctx->path_len] != '/' ; --ctx->path_len);
183 if (ctx->path_len == 0) {
184 /* top-level (last segment) */
185 ++ctx->path_len;
186 }
187 }
188 /* set new terminating NULL-byte */
189 ctx->path[ctx->path_len] = '\0';
190 } else {
191 if (ctx->path_len > 1) {
192 if (!parent && ctx->path[ctx->path_len - 1] == '}' && ctx->path[ctx->path_len - 2] != '\'') {
193 /* extension of the special tag */
194 nextlevel = 2;
195 --ctx->path_len;
196 } else {
197 /* there is already some path, so add next level */
198 nextlevel = 1;
199 }
200 } /* else the path is just initiated with '/', so do not add additional slash in case of top-level nodes */
201
202 if (nextlevel != 2) {
203 if ((parent && parent->module == ctx->mod) || (!parent && ctx->path_len > 1 && name[0] == '{')) {
204 /* module not changed, print the name unprefixed */
Radek Krejci70ee9152019-07-25 11:27:27 +0200205 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "%s%s", nextlevel ? "/" : "", name);
Radek Krejci327de162019-06-14 12:52:07 +0200206 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200207 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 +0200208 }
209 } else {
Radek Krejci70ee9152019-07-25 11:27:27 +0200210 len = snprintf(&ctx->path[ctx->path_len], LYSC_CTX_BUFSIZE - ctx->path_len, "='%s'}", name);
Radek Krejci327de162019-06-14 12:52:07 +0200211 }
212 ctx->path_len += len;
213 }
214}
215
216/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100217 * @brief Duplicate the compiled pattern structure.
218 *
219 * Instead of duplicating memory, the reference counter in the @p orig is increased.
220 *
221 * @param[in] orig The pattern structure to duplicate.
222 * @return The duplicated structure to use.
223 */
Radek Krejci19a96102018-11-15 13:38:09 +0100224static struct lysc_pattern*
225lysc_pattern_dup(struct lysc_pattern *orig)
226{
227 ++orig->refcount;
228 return orig;
229}
230
Radek Krejcib56c7502019-02-13 14:19:54 +0100231/**
232 * @brief Duplicate the array of compiled patterns.
233 *
234 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
235 *
236 * @param[in] ctx Libyang context for logging.
237 * @param[in] orig The patterns sized array to duplicate.
238 * @return New sized array as a copy of @p orig.
239 * @return NULL in case of memory allocation error.
240 */
Radek Krejci19a96102018-11-15 13:38:09 +0100241static struct lysc_pattern**
242lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
243{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100244 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100245 unsigned int u;
246
Radek Krejcib56c7502019-02-13 14:19:54 +0100247 assert(orig);
248
Radek Krejci19a96102018-11-15 13:38:09 +0100249 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
250 LY_ARRAY_FOR(orig, u) {
251 dup[u] = lysc_pattern_dup(orig[u]);
252 LY_ARRAY_INCREMENT(dup);
253 }
254 return dup;
255}
256
Radek Krejcib56c7502019-02-13 14:19:54 +0100257/**
258 * @brief Duplicate compiled range structure.
259 *
260 * @param[in] ctx Libyang context for logging.
261 * @param[in] orig The range structure to be duplicated.
262 * @return New compiled range structure as a copy of @p orig.
263 * @return NULL in case of memory allocation error.
264 */
Radek Krejci19a96102018-11-15 13:38:09 +0100265struct lysc_range*
266lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
267{
268 struct lysc_range *dup;
269 LY_ERR ret;
270
Radek Krejcib56c7502019-02-13 14:19:54 +0100271 assert(orig);
272
Radek Krejci19a96102018-11-15 13:38:09 +0100273 dup = calloc(1, sizeof *dup);
274 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
275 if (orig->parts) {
276 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
277 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
278 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
279 }
280 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
281 DUP_STRING(ctx, orig->emsg, dup->emsg);
282 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
283
284 return dup;
285cleanup:
286 free(dup);
287 (void) ret; /* set but not used due to the return type */
288 return NULL;
289}
290
Radek Krejcib56c7502019-02-13 14:19:54 +0100291/**
292 * @brief Stack for processing if-feature expressions.
293 */
Radek Krejci19a96102018-11-15 13:38:09 +0100294struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100295 int size; /**< number of items in the stack */
296 int index; /**< first empty item */
297 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100298};
299
Radek Krejcib56c7502019-02-13 14:19:54 +0100300/**
301 * @brief Add @ref ifftokens into the stack.
302 * @param[in] stack The if-feature stack to use.
303 * @param[in] value One of the @ref ifftokens to store in the stack.
304 * @return LY_EMEM in case of memory allocation error
305 * @return LY_ESUCCESS if the value successfully stored.
306 */
Radek Krejci19a96102018-11-15 13:38:09 +0100307static LY_ERR
308iff_stack_push(struct iff_stack *stack, uint8_t value)
309{
310 if (stack->index == stack->size) {
311 stack->size += 4;
312 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
313 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
314 }
315 stack->stack[stack->index++] = value;
316 return LY_SUCCESS;
317}
318
Radek Krejcib56c7502019-02-13 14:19:54 +0100319/**
320 * @brief Get (and remove) the last item form the stack.
321 * @param[in] stack The if-feature stack to use.
322 * @return The value from the top of the stack.
323 */
Radek Krejci19a96102018-11-15 13:38:09 +0100324static uint8_t
325iff_stack_pop(struct iff_stack *stack)
326{
Radek Krejcib56c7502019-02-13 14:19:54 +0100327 assert(stack && stack->index);
328
Radek Krejci19a96102018-11-15 13:38:09 +0100329 stack->index--;
330 return stack->stack[stack->index];
331}
332
Radek Krejcib56c7502019-02-13 14:19:54 +0100333/**
334 * @brief Clean up the stack.
335 * @param[in] stack The if-feature stack to use.
336 */
Radek Krejci19a96102018-11-15 13:38:09 +0100337static void
338iff_stack_clean(struct iff_stack *stack)
339{
340 stack->size = 0;
341 free(stack->stack);
342}
343
Radek Krejcib56c7502019-02-13 14:19:54 +0100344/**
345 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
346 * (libyang format of the if-feature expression).
347 * @param[in,out] list The 2bits array to modify.
348 * @param[in] op The operand (@ref ifftokens) to store.
349 * @param[in] pos Position (0-based) where to store the given @p op.
350 */
Radek Krejci19a96102018-11-15 13:38:09 +0100351static void
352iff_setop(uint8_t *list, uint8_t op, int pos)
353{
354 uint8_t *item;
355 uint8_t mask = 3;
356
357 assert(pos >= 0);
358 assert(op <= 3); /* max 2 bits */
359
360 item = &list[pos / 4];
361 mask = mask << 2 * (pos % 4);
362 *item = (*item) & ~mask;
363 *item = (*item) | (op << 2 * (pos % 4));
364}
365
Radek Krejcib56c7502019-02-13 14:19:54 +0100366#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
367#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100368
Radek Krejci0af46292019-01-11 16:02:31 +0100369/**
370 * @brief Find a feature of the given name and referenced in the given module.
371 *
372 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
373 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
374 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
375 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
376 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
377 * assigned till that time will be still valid.
378 *
379 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
380 * @param[in] name Name of the feature including possible prefix.
381 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
382 * @return Pointer to the feature structure if found, NULL otherwise.
383 */
Radek Krejci19a96102018-11-15 13:38:09 +0100384static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100385lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100386{
387 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100388 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100389
390 for (i = 0; i < len; ++i) {
391 if (name[i] == ':') {
392 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100393 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100394 LY_CHECK_RET(!mod, NULL);
395
396 name = &name[i + 1];
397 len = len - i - 1;
398 }
399 }
400
401 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100402 if (mod->implemented) {
403 /* module is implemented so there is already the compiled schema */
404 flist = mod->compiled->features;
405 } else {
406 flist = mod->off_features;
407 }
408 LY_ARRAY_FOR(flist, i) {
409 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100410 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
411 return f;
412 }
413 }
414
415 return NULL;
416}
417
418static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200419lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext)
Radek Krejci19a96102018-11-15 13:38:09 +0100420{
421 const char *name;
422 unsigned int u;
423 const struct lys_module *mod;
424 struct lysp_ext *edef = NULL;
425
426 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
427 ext->insubstmt = ext_p->insubstmt;
428 ext->insubstmt_index = ext_p->insubstmt_index;
429
430 /* get module where the extension definition should be placed */
431 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100432 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100433 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
434 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
435 LY_EVALID);
436 LY_CHECK_ERR_RET(!mod->parsed->extensions,
437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
438 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100439 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100440 LY_EVALID);
441 name = &ext_p->name[u + 1];
442 /* find the extension definition there */
443 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
444 if (!strcmp(name, mod->parsed->extensions[u].name)) {
445 edef = &mod->parsed->extensions[u];
446 break;
447 }
448 }
449 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
450 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
451 LY_EVALID);
Radek Krejcia1911222019-07-22 17:24:50 +0200452 /* TODO extension plugins */
Radek Krejci19a96102018-11-15 13:38:09 +0100453
454 return LY_SUCCESS;
455}
456
Radek Krejcib56c7502019-02-13 14:19:54 +0100457/**
458 * @brief Compile information from the if-feature statement
459 * @param[in] ctx Compile context.
460 * @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 +0100461 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
462 * @return LY_ERR value.
463 */
Radek Krejci19a96102018-11-15 13:38:09 +0100464static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200465lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100466{
467 const char *c = *value;
468 int r, rc = EXIT_FAILURE;
469 int i, j, last_not, checkversion = 0;
470 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
471 uint8_t op;
472 struct iff_stack stack = {0, 0, NULL};
473 struct lysc_feature *f;
474
475 assert(c);
476
477 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
478 for (i = j = last_not = 0; c[i]; i++) {
479 if (c[i] == '(') {
480 j++;
481 checkversion = 1;
482 continue;
483 } else if (c[i] == ')') {
484 j--;
485 continue;
486 } else if (isspace(c[i])) {
487 checkversion = 1;
488 continue;
489 }
490
491 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 +0200492 int sp;
493 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
494 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100495 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
496 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
497 return LY_EVALID;
498 } else if (!isspace(c[i + r])) {
499 /* feature name starting with the not/and/or */
500 last_not = 0;
501 f_size++;
502 } else if (c[i] == 'n') { /* not operation */
503 if (last_not) {
504 /* double not */
505 expr_size = expr_size - 2;
506 last_not = 0;
507 } else {
508 last_not = 1;
509 }
510 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200511 if (f_exp != f_size) {
512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
513 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
514 return LY_EVALID;
515 }
Radek Krejci19a96102018-11-15 13:38:09 +0100516 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200517
Radek Krejci19a96102018-11-15 13:38:09 +0100518 /* not a not operation */
519 last_not = 0;
520 }
521 i += r;
522 } else {
523 f_size++;
524 last_not = 0;
525 }
526 expr_size++;
527
528 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200529 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100530 i--;
531 break;
532 }
533 i++;
534 }
535 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200536 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100537 /* not matching count of ( and ) */
538 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
539 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
540 return LY_EVALID;
541 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200542 if (f_exp != f_size) {
543 /* features do not match the needed arguments for the logical operations */
544 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
545 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
546 "the required number of operands for the operations.", *value);
547 return LY_EVALID;
548 }
Radek Krejci19a96102018-11-15 13:38:09 +0100549
550 if (checkversion || expr_size > 1) {
551 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100552 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
554 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
555 return LY_EVALID;
556 }
557 }
558
559 /* allocate the memory */
560 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
561 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
562 stack.stack = malloc(expr_size * sizeof *stack.stack);
563 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
564
565 stack.size = expr_size;
566 f_size--; expr_size--; /* used as indexes from now */
567
568 for (i--; i >= 0; i--) {
569 if (c[i] == ')') {
570 /* push it on stack */
571 iff_stack_push(&stack, LYS_IFF_RP);
572 continue;
573 } else if (c[i] == '(') {
574 /* pop from the stack into result all operators until ) */
575 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
576 iff_setop(iff->expr, op, expr_size--);
577 }
578 continue;
579 } else if (isspace(c[i])) {
580 continue;
581 }
582
583 /* end of operator or operand -> find beginning and get what is it */
584 j = i + 1;
585 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
586 i--;
587 }
588 i++; /* go back by one step */
589
590 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
591 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
592 /* double not */
593 iff_stack_pop(&stack);
594 } else {
595 /* not has the highest priority, so do not pop from the stack
596 * as in case of AND and OR */
597 iff_stack_push(&stack, LYS_IFF_NOT);
598 }
599 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
600 /* as for OR - pop from the stack all operators with the same or higher
601 * priority and store them to the result, then push the AND to the stack */
602 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
603 op = iff_stack_pop(&stack);
604 iff_setop(iff->expr, op, expr_size--);
605 }
606 iff_stack_push(&stack, LYS_IFF_AND);
607 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
608 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
609 op = iff_stack_pop(&stack);
610 iff_setop(iff->expr, op, expr_size--);
611 }
612 iff_stack_push(&stack, LYS_IFF_OR);
613 } else {
614 /* feature name, length is j - i */
615
616 /* add it to the expression */
617 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
618
619 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100620 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
621 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
622 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
623 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100624 iff->features[f_size] = f;
625 LY_ARRAY_INCREMENT(iff->features);
626 f_size--;
627 }
628 }
629 while (stack.index) {
630 op = iff_stack_pop(&stack);
631 iff_setop(iff->expr, op, expr_size--);
632 }
633
634 if (++expr_size || ++f_size) {
635 /* not all expected operators and operands found */
636 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
637 "Invalid value \"%s\" of if-feature - processing error.", *value);
638 rc = LY_EINT;
639 } else {
640 rc = LY_SUCCESS;
641 }
642
643error:
644 /* cleanup */
645 iff_stack_clean(&stack);
646
647 return rc;
648}
649
Radek Krejcib56c7502019-02-13 14:19:54 +0100650/**
651 * @brief Compile information from the when statement
652 * @param[in] ctx Compile context.
653 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100654 * @param[out] when Pointer where to store pointer to the created compiled when structure.
655 * @return LY_ERR value.
656 */
Radek Krejci19a96102018-11-15 13:38:09 +0100657static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200658lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100659{
660 unsigned int u;
661 LY_ERR ret = LY_SUCCESS;
662
Radek Krejci00b874b2019-02-12 10:54:50 +0100663 *when = calloc(1, sizeof **when);
664 (*when)->refcount = 1;
665 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
666 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
667 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
668 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejciec4da802019-05-02 13:02:41 +0200669 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100670
671done:
672 return ret;
673}
674
Radek Krejcib56c7502019-02-13 14:19:54 +0100675/**
676 * @brief Compile information from the must statement
677 * @param[in] ctx Compile context.
678 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100679 * @param[in,out] must Prepared (empty) compiled must structure to fill.
680 * @return LY_ERR value.
681 */
Radek Krejci19a96102018-11-15 13:38:09 +0100682static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200683lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100684{
685 unsigned int u;
686 LY_ERR ret = LY_SUCCESS;
687
688 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
689 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200690 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100691 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
692 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100693 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
694 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejciec4da802019-05-02 13:02:41 +0200695 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100696
697done:
698 return ret;
699}
700
Radek Krejcib56c7502019-02-13 14:19:54 +0100701/**
702 * @brief Compile information from the import statement
703 * @param[in] ctx Compile context.
704 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100705 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
706 * @return LY_ERR value.
707 */
Radek Krejci19a96102018-11-15 13:38:09 +0100708static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200709lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100710{
711 unsigned int u;
712 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100713 LY_ERR ret = LY_SUCCESS;
714
715 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejciec4da802019-05-02 13:02:41 +0200716 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100717 imp->module = imp_p->module;
718
Radek Krejci7f2a5362018-11-28 13:05:37 +0100719 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
720 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100721 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100722 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100723 /* try to use filepath if present */
724 if (imp->module->filepath) {
725 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
726 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100727 if (mod != imp->module) {
728 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100729 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100730 mod = NULL;
731 }
732 }
733 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100734 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100735 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 +0100736 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100737 return LY_ENOTFOUND;
738 }
739 }
Radek Krejci19a96102018-11-15 13:38:09 +0100740 }
741
742done:
743 return ret;
744}
745
Radek Krejcib56c7502019-02-13 14:19:54 +0100746/**
747 * @brief Compile information from the identity statement
748 *
749 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
750 *
751 * @param[in] ctx Compile context.
752 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100753 * @param[in] idents List of so far compiled identities to check the name uniqueness.
754 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
755 * @return LY_ERR value.
756 */
Radek Krejci19a96102018-11-15 13:38:09 +0100757static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200758lys_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 +0100759{
760 unsigned int u;
761 LY_ERR ret = LY_SUCCESS;
762
Radek Krejci327de162019-06-14 12:52:07 +0200763 lysc_update_path(ctx, NULL, ident_p->name);
764
Radek Krejcid05cbd92018-12-05 14:26:40 +0100765 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100766 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200767 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
768 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200769 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200770 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100771 /* backlings (derived) can be added no sooner than when all the identities in the current module are present */
Radek Krejciec4da802019-05-02 13:02:41 +0200772 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100773 ident->flags = ident_p->flags;
774
Radek Krejci327de162019-06-14 12:52:07 +0200775 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100776done:
777 return ret;
778}
779
Radek Krejcib56c7502019-02-13 14:19:54 +0100780/**
781 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
782 *
783 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
784 *
785 * @param[in] ctx Compile context for logging.
786 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
787 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
788 * @return LY_SUCCESS if everything is ok.
789 * @return LY_EVALID if the identity is derived from itself.
790 */
Radek Krejci38222632019-02-12 16:55:05 +0100791static LY_ERR
792lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
793{
794 LY_ERR ret = LY_EVALID;
795 unsigned int u, v;
796 struct ly_set recursion = {0};
797 struct lysc_ident *drv;
798
799 if (!derived) {
800 return LY_SUCCESS;
801 }
802
803 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
804 if (ident == derived[u]) {
805 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
806 "Identity \"%s\" is indirectly derived from itself.", ident->name);
807 goto cleanup;
808 }
809 ly_set_add(&recursion, derived[u], 0);
810 }
811
812 for (v = 0; v < recursion.count; ++v) {
813 drv = recursion.objs[v];
814 if (!drv->derived) {
815 continue;
816 }
817 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
818 if (ident == drv->derived[u]) {
819 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
820 "Identity \"%s\" is indirectly derived from itself.", ident->name);
821 goto cleanup;
822 }
823 ly_set_add(&recursion, drv->derived[u], 0);
824 }
825 }
826 ret = LY_SUCCESS;
827
828cleanup:
829 ly_set_erase(&recursion, NULL);
830 return ret;
831}
832
Radek Krejcia3045382018-11-22 14:30:31 +0100833/**
834 * @brief Find and process the referenced base identities from another identity or identityref
835 *
Radek Krejciaca74032019-06-04 08:53:06 +0200836 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +0100837 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
838 * to distinguish these two use cases.
839 *
840 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
841 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
842 * @param[in] ident Referencing identity to work with.
843 * @param[in] bases Array of bases of identityref to fill in.
844 * @return LY_ERR value.
845 */
Radek Krejci19a96102018-11-15 13:38:09 +0100846static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100847lys_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 +0100848{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100849 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100850 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100851 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100852 struct lysc_ident **idref;
853
854 assert(ident || bases);
855
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100856 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100857 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
858 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
859 return LY_EVALID;
860 }
861
862 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
863 s = strchr(bases_p[u], ':');
864 if (s) {
865 /* prefixed identity */
866 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100867 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100868 } else {
869 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100870 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100871 }
872 if (!mod) {
873 if (ident) {
874 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
875 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
876 } else {
877 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
878 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
879 }
880 return LY_EVALID;
881 }
882 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100883 if (mod->compiled && mod->compiled->identities) {
884 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
885 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100886 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100887 if (ident == &mod->compiled->identities[v]) {
888 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
889 "Identity \"%s\" is derived from itself.", ident->name);
890 return LY_EVALID;
891 }
892 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100893 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100894 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100895 *idref = ident;
896 } else {
897 /* we have match! store the found identity */
898 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100899 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100900 }
901 break;
902 }
903 }
904 }
905 if (!idref || !(*idref)) {
906 if (ident) {
907 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
908 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
909 } else {
910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
911 "Unable to find base (%s) of identityref.", bases_p[u]);
912 }
913 return LY_EVALID;
914 }
915 }
916 return LY_SUCCESS;
917}
918
Radek Krejcia3045382018-11-22 14:30:31 +0100919/**
920 * @brief For the given array of identities, set the backlinks from all their base identities.
921 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
922 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
923 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
924 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
925 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100926static LY_ERR
927lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
928{
929 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100930
931 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
932 if (!idents_p[i].bases) {
933 continue;
934 }
Radek Krejci327de162019-06-14 12:52:07 +0200935 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100936 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +0200937 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100938 }
939 return LY_SUCCESS;
940}
941
Radek Krejci0af46292019-01-11 16:02:31 +0100942LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +0200943lys_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 +0100944{
945 unsigned int offset = 0, u;
946 struct lysc_ctx context = {0};
947
Radek Krejci327de162019-06-14 12:52:07 +0200948 assert(ctx_sc || ctx);
949
950 if (!ctx_sc) {
951 context.ctx = ctx;
952 context.mod = module;
953 context.path_len = 1;
954 context.path[0] = '/';
955 ctx_sc = &context;
956 }
Radek Krejci0af46292019-01-11 16:02:31 +0100957
958 if (!features_p) {
959 return LY_SUCCESS;
960 }
961 if (*features) {
962 offset = LY_ARRAY_SIZE(*features);
963 }
964
Radek Krejci327de162019-06-14 12:52:07 +0200965 lysc_update_path(ctx_sc, NULL, "{feature}");
966 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +0100967 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +0200968 lysc_update_path(ctx_sc, NULL, features_p[u].name);
969
Radek Krejci0af46292019-01-11 16:02:31 +0100970 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +0200971 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
972 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
973 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
974 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +0100975 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +0200976 (*features)[offset + u].module = ctx_sc->mod;
977
978 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +0100979 }
Radek Krejci327de162019-06-14 12:52:07 +0200980 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +0100981
982 return LY_SUCCESS;
983}
984
Radek Krejcia3045382018-11-22 14:30:31 +0100985/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100986 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100987 *
988 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
989 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100990 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100991 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
992 * being currently processed).
993 * @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 +0100994 * @return LY_SUCCESS if everything is ok.
995 * @return LY_EVALID if the feature references indirectly itself.
996 */
997static LY_ERR
998lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
999{
1000 LY_ERR ret = LY_EVALID;
1001 unsigned int u, v;
1002 struct ly_set recursion = {0};
1003 struct lysc_feature *drv;
1004
1005 if (!depfeatures) {
1006 return LY_SUCCESS;
1007 }
1008
1009 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1010 if (feature == depfeatures[u]) {
1011 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1012 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1013 goto cleanup;
1014 }
1015 ly_set_add(&recursion, depfeatures[u], 0);
1016 }
1017
1018 for (v = 0; v < recursion.count; ++v) {
1019 drv = recursion.objs[v];
1020 if (!drv->depfeatures) {
1021 continue;
1022 }
1023 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1024 if (feature == drv->depfeatures[u]) {
1025 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1026 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1027 goto cleanup;
1028 }
1029 ly_set_add(&recursion, drv->depfeatures[u], 0);
1030 }
1031 }
1032 ret = LY_SUCCESS;
1033
1034cleanup:
1035 ly_set_erase(&recursion, NULL);
1036 return ret;
1037}
1038
1039/**
Radek Krejci0af46292019-01-11 16:02:31 +01001040 * @brief Create pre-compiled features array.
1041 *
1042 * See lys_feature_precompile() for more details.
1043 *
Radek Krejcia3045382018-11-22 14:30:31 +01001044 * @param[in] ctx Compile context.
1045 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001046 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001047 * @return LY_ERR value.
1048 */
Radek Krejci19a96102018-11-15 13:38:09 +01001049static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001050lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001051{
Radek Krejci0af46292019-01-11 16:02:31 +01001052 unsigned int u, v, x;
1053 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001054 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001055
Radek Krejci327de162019-06-14 12:52:07 +02001056
Radek Krejci0af46292019-01-11 16:02:31 +01001057 /* find the preprecompiled feature */
1058 LY_ARRAY_FOR(features, x) {
1059 if (strcmp(features[x].name, feature_p->name)) {
1060 continue;
1061 }
1062 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001063 lysc_update_path(ctx, NULL, "{feature}");
1064 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001065
Radek Krejci0af46292019-01-11 16:02:31 +01001066 /* finish compilation started in lys_feature_precompile() */
Radek Krejciec4da802019-05-02 13:02:41 +02001067 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, u, lys_compile_ext, ret, done);
1068 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001069 if (feature->iffeatures) {
1070 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1071 if (feature->iffeatures[u].features) {
1072 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001073 /* check for circular dependency - direct reference first,... */
1074 if (feature == feature->iffeatures[u].features[v]) {
1075 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1076 "Feature \"%s\" is referenced from itself.", feature->name);
1077 return LY_EVALID;
1078 }
1079 /* ... and indirect circular reference */
1080 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1081
Radek Krejci0af46292019-01-11 16:02:31 +01001082 /* add itself into the dependants list */
1083 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1084 *df = feature;
1085 }
Radek Krejci19a96102018-11-15 13:38:09 +01001086 }
Radek Krejci19a96102018-11-15 13:38:09 +01001087 }
1088 }
Radek Krejci327de162019-06-14 12:52:07 +02001089 lysc_update_path(ctx, NULL, NULL);
1090 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001091 done:
1092 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001093 }
Radek Krejci0af46292019-01-11 16:02:31 +01001094
1095 LOGINT(ctx->ctx);
1096 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001097}
1098
Radek Krejcib56c7502019-02-13 14:19:54 +01001099/**
1100 * @brief Revert compiled list of features back to the precompiled state.
1101 *
1102 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1103 * The features are supposed to be stored again as off_features in ::lys_module structure.
1104 *
1105 * @param[in] ctx Compilation context.
1106 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1107 * and supposed to hold the off_features list.
1108 */
Radek Krejci95710c92019-02-11 15:49:55 +01001109static void
1110lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1111{
1112 unsigned int u, v;
1113
1114 /* keep the off_features list until the complete lys_module is freed */
1115 mod->off_features = mod->compiled->features;
1116 mod->compiled->features = NULL;
1117
1118 /* in the off_features list, remove all the parts (from finished compiling process)
1119 * which may points into the data being freed here */
1120 LY_ARRAY_FOR(mod->off_features, u) {
1121 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1122 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1123 }
1124 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1125 mod->off_features[u].iffeatures = NULL;
1126
1127 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1128 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1129 }
1130 LY_ARRAY_FREE(mod->off_features[u].exts);
1131 mod->off_features[u].exts = NULL;
1132 }
1133}
1134
Radek Krejcia3045382018-11-22 14:30:31 +01001135/**
1136 * @brief Validate and normalize numeric value from a range definition.
1137 * @param[in] ctx Compile context.
1138 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1139 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1140 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1141 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1142 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1143 * @param[in] value String value of the range boundary.
1144 * @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.
1145 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1146 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1147 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001148LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001149range_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 +01001150{
Radek Krejci6cba4292018-11-15 17:33:29 +01001151 size_t fraction = 0, size;
1152
Radek Krejci19a96102018-11-15 13:38:09 +01001153 *len = 0;
1154
1155 assert(value);
1156 /* parse value */
1157 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1158 return LY_EVALID;
1159 }
1160
1161 if ((value[*len] == '-') || (value[*len] == '+')) {
1162 ++(*len);
1163 }
1164
1165 while (isdigit(value[*len])) {
1166 ++(*len);
1167 }
1168
1169 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001170 if (basetype == LY_TYPE_DEC64) {
1171 goto decimal;
1172 } else {
1173 *valcopy = strndup(value, *len);
1174 return LY_SUCCESS;
1175 }
Radek Krejci19a96102018-11-15 13:38:09 +01001176 }
1177 fraction = *len;
1178
1179 ++(*len);
1180 while (isdigit(value[*len])) {
1181 ++(*len);
1182 }
1183
Radek Krejci6cba4292018-11-15 17:33:29 +01001184 if (basetype == LY_TYPE_DEC64) {
1185decimal:
1186 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001187 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001188 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1189 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1190 *len, value, frdigits);
1191 return LY_EINVAL;
1192 }
1193 if (fraction) {
1194 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1195 } else {
1196 size = (*len) + frdigits + 1;
1197 }
1198 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001199 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1200
Radek Krejci6cba4292018-11-15 17:33:29 +01001201 (*valcopy)[size - 1] = '\0';
1202 if (fraction) {
1203 memcpy(&(*valcopy)[0], &value[0], fraction);
1204 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1205 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1206 } else {
1207 memcpy(&(*valcopy)[0], &value[0], *len);
1208 memset(&(*valcopy)[*len], '0', frdigits);
1209 }
Radek Krejci19a96102018-11-15 13:38:09 +01001210 }
1211 return LY_SUCCESS;
1212}
1213
Radek Krejcia3045382018-11-22 14:30:31 +01001214/**
1215 * @brief Check that values in range are in ascendant order.
1216 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001217 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1218 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001219 * @param[in] value Current value to check.
1220 * @param[in] prev_value The last seen value.
1221 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1222 */
Radek Krejci19a96102018-11-15 13:38:09 +01001223static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001224range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001225{
1226 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001227 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001228 return LY_EEXIST;
1229 }
1230 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001231 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001232 return LY_EEXIST;
1233 }
1234 }
1235 return LY_SUCCESS;
1236}
1237
Radek Krejcia3045382018-11-22 14:30:31 +01001238/**
1239 * @brief Set min/max value of the range part.
1240 * @param[in] ctx Compile context.
1241 * @param[in] part Range part structure to fill.
1242 * @param[in] max Flag to distinguish if storing min or max value.
1243 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1244 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1245 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1246 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1247 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001248 * @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 +01001249 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1250 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1251 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1252 * frdigits value), LY_EMEM.
1253 */
Radek Krejci19a96102018-11-15 13:38:09 +01001254static LY_ERR
1255range_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 +01001256 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001257{
1258 LY_ERR ret = LY_SUCCESS;
1259 char *valcopy = NULL;
1260 size_t len;
1261
1262 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001263 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001264 LY_CHECK_GOTO(ret, finalize);
1265 }
1266 if (!valcopy && base_range) {
1267 if (max) {
1268 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1269 } else {
1270 part->min_64 = base_range->parts[0].min_64;
1271 }
1272 if (!first) {
1273 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1274 }
1275 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001276 }
1277
1278 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001279 case LY_TYPE_INT8: /* range */
1280 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001281 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 +01001282 } else if (max) {
1283 part->max_64 = INT64_C(127);
1284 } else {
1285 part->min_64 = INT64_C(-128);
1286 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001287 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001288 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001289 }
1290 break;
1291 case LY_TYPE_INT16: /* range */
1292 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001293 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 +01001294 } else if (max) {
1295 part->max_64 = INT64_C(32767);
1296 } else {
1297 part->min_64 = INT64_C(-32768);
1298 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001299 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001300 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001301 }
1302 break;
1303 case LY_TYPE_INT32: /* range */
1304 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001305 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 +01001306 } else if (max) {
1307 part->max_64 = INT64_C(2147483647);
1308 } else {
1309 part->min_64 = INT64_C(-2147483648);
1310 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001311 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001312 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001313 }
1314 break;
1315 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001316 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001317 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001318 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001319 max ? &part->max_64 : &part->min_64);
1320 } else if (max) {
1321 part->max_64 = INT64_C(9223372036854775807);
1322 } else {
1323 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1324 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001325 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001326 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001327 }
1328 break;
1329 case LY_TYPE_UINT8: /* range */
1330 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001331 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 +01001332 } else if (max) {
1333 part->max_u64 = UINT64_C(255);
1334 } else {
1335 part->min_u64 = UINT64_C(0);
1336 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001337 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001338 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001339 }
1340 break;
1341 case LY_TYPE_UINT16: /* range */
1342 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001343 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 +01001344 } else if (max) {
1345 part->max_u64 = UINT64_C(65535);
1346 } else {
1347 part->min_u64 = UINT64_C(0);
1348 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001349 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001350 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001351 }
1352 break;
1353 case LY_TYPE_UINT32: /* range */
1354 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001355 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 +01001356 } else if (max) {
1357 part->max_u64 = UINT64_C(4294967295);
1358 } else {
1359 part->min_u64 = UINT64_C(0);
1360 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001361 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001362 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001363 }
1364 break;
1365 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001366 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001367 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001368 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001369 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 +01001370 } else if (max) {
1371 part->max_u64 = UINT64_C(18446744073709551615);
1372 } else {
1373 part->min_u64 = UINT64_C(0);
1374 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001375 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001376 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001377 }
1378 break;
1379 default:
1380 LOGINT(ctx->ctx);
1381 ret = LY_EINT;
1382 }
1383
Radek Krejci5969f272018-11-23 10:03:58 +01001384finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001385 if (ret == LY_EDENIED) {
1386 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1387 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1388 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1389 } else if (ret == LY_EVALID) {
1390 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1391 "Invalid %s restriction - invalid value \"%s\".",
1392 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1393 } else if (ret == LY_EEXIST) {
1394 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1395 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001396 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001397 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001398 } else if (!ret && value) {
1399 *value = *value + len;
1400 }
1401 free(valcopy);
1402 return ret;
1403}
1404
Radek Krejcia3045382018-11-22 14:30:31 +01001405/**
1406 * @brief Compile the parsed range restriction.
1407 * @param[in] ctx Compile context.
1408 * @param[in] range_p Parsed range structure to compile.
1409 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1410 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1411 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1412 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1413 * range restriction must be more restrictive than the base_range.
1414 * @param[in,out] range Pointer to the created current range structure.
1415 * @return LY_ERR value.
1416 */
Radek Krejci19a96102018-11-15 13:38:09 +01001417static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001418lys_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 +01001419 struct lysc_range *base_range, struct lysc_range **range)
1420{
1421 LY_ERR ret = LY_EVALID;
1422 const char *expr;
1423 struct lysc_range_part *parts = NULL, *part;
1424 int range_expected = 0, uns;
1425 unsigned int parts_done = 0, u, v;
1426
1427 assert(range);
1428 assert(range_p);
1429
1430 expr = range_p->arg;
1431 while(1) {
1432 if (isspace(*expr)) {
1433 ++expr;
1434 } else if (*expr == '\0') {
1435 if (range_expected) {
1436 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1437 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1438 length_restr ? "length" : "range", range_p->arg);
1439 goto cleanup;
1440 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1442 "Invalid %s restriction - unexpected end of the expression (%s).",
1443 length_restr ? "length" : "range", range_p->arg);
1444 goto cleanup;
1445 }
1446 parts_done++;
1447 break;
1448 } else if (!strncmp(expr, "min", 3)) {
1449 if (parts) {
1450 /* min cannot be used elsewhere than in the first part */
1451 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1452 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1453 expr - range_p->arg, range_p->arg);
1454 goto cleanup;
1455 }
1456 expr += 3;
1457
1458 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001459 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 +01001460 part->max_64 = part->min_64;
1461 } else if (*expr == '|') {
1462 if (!parts || range_expected) {
1463 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1464 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1465 goto cleanup;
1466 }
1467 expr++;
1468 parts_done++;
1469 /* process next part of the expression */
1470 } else if (!strncmp(expr, "..", 2)) {
1471 expr += 2;
1472 while (isspace(*expr)) {
1473 expr++;
1474 }
1475 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1476 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1477 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1478 goto cleanup;
1479 }
1480 /* continue expecting the upper boundary */
1481 range_expected = 1;
1482 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1483 /* number */
1484 if (range_expected) {
1485 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001486 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 +01001487 range_expected = 0;
1488 } else {
1489 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1490 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 +01001491 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001492 part->max_64 = part->min_64;
1493 }
1494
1495 /* continue with possible another expression part */
1496 } else if (!strncmp(expr, "max", 3)) {
1497 expr += 3;
1498 while (isspace(*expr)) {
1499 expr++;
1500 }
1501 if (*expr != '\0') {
1502 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1503 length_restr ? "length" : "range", expr);
1504 goto cleanup;
1505 }
1506 if (range_expected) {
1507 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001508 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 +01001509 range_expected = 0;
1510 } else {
1511 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1512 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 +01001513 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001514 part->min_64 = part->max_64;
1515 }
1516 } else {
1517 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1518 length_restr ? "length" : "range", expr);
1519 goto cleanup;
1520 }
1521 }
1522
1523 /* check with the previous range/length restriction */
1524 if (base_range) {
1525 switch (basetype) {
1526 case LY_TYPE_BINARY:
1527 case LY_TYPE_UINT8:
1528 case LY_TYPE_UINT16:
1529 case LY_TYPE_UINT32:
1530 case LY_TYPE_UINT64:
1531 case LY_TYPE_STRING:
1532 uns = 1;
1533 break;
1534 case LY_TYPE_DEC64:
1535 case LY_TYPE_INT8:
1536 case LY_TYPE_INT16:
1537 case LY_TYPE_INT32:
1538 case LY_TYPE_INT64:
1539 uns = 0;
1540 break;
1541 default:
1542 LOGINT(ctx->ctx);
1543 ret = LY_EINT;
1544 goto cleanup;
1545 }
1546 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1547 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1548 goto baseerror;
1549 }
1550 /* current lower bound is not lower than the base */
1551 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1552 /* base has single value */
1553 if (base_range->parts[v].min_64 == parts[u].min_64) {
1554 /* both lower bounds are the same */
1555 if (parts[u].min_64 != parts[u].max_64) {
1556 /* current continues with a range */
1557 goto baseerror;
1558 } else {
1559 /* equal single values, move both forward */
1560 ++v;
1561 continue;
1562 }
1563 } else {
1564 /* base is single value lower than current range, so the
1565 * value from base range is removed in the current,
1566 * move only base and repeat checking */
1567 ++v;
1568 --u;
1569 continue;
1570 }
1571 } else {
1572 /* base is the range */
1573 if (parts[u].min_64 == parts[u].max_64) {
1574 /* current is a single value */
1575 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1576 /* current is behind the base range, so base range is omitted,
1577 * move the base and keep the current for further check */
1578 ++v;
1579 --u;
1580 } /* else it is within the base range, so move the current, but keep the base */
1581 continue;
1582 } else {
1583 /* both are ranges - check the higher bound, the lower was already checked */
1584 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1585 /* higher bound is higher than the current higher bound */
1586 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1587 /* but the current lower bound is also higher, so the base range is omitted,
1588 * continue with the same current, but move the base */
1589 --u;
1590 ++v;
1591 continue;
1592 }
1593 /* current range starts within the base range but end behind it */
1594 goto baseerror;
1595 } else {
1596 /* current range is smaller than the base,
1597 * move current, but stay with the base */
1598 continue;
1599 }
1600 }
1601 }
1602 }
1603 if (u != parts_done) {
1604baseerror:
1605 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1606 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1607 length_restr ? "length" : "range", range_p->arg);
1608 goto cleanup;
1609 }
1610 }
1611
1612 if (!(*range)) {
1613 *range = calloc(1, sizeof **range);
1614 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1615 }
1616
Radek Krejcic8b31002019-01-08 10:24:45 +01001617 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001618 if (range_p->eapptag) {
1619 lydict_remove(ctx->ctx, (*range)->eapptag);
1620 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1621 }
1622 if (range_p->emsg) {
1623 lydict_remove(ctx->ctx, (*range)->emsg);
1624 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1625 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001626 if (range_p->dsc) {
1627 lydict_remove(ctx->ctx, (*range)->dsc);
1628 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1629 }
1630 if (range_p->ref) {
1631 lydict_remove(ctx->ctx, (*range)->ref);
1632 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1633 }
Radek Krejci19a96102018-11-15 13:38:09 +01001634 /* extensions are taken only from the last range by the caller */
1635
1636 (*range)->parts = parts;
1637 parts = NULL;
1638 ret = LY_SUCCESS;
1639cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001640 LY_ARRAY_FREE(parts);
1641
1642 return ret;
1643}
1644
1645/**
1646 * @brief Checks pattern syntax.
1647 *
Radek Krejcia3045382018-11-22 14:30:31 +01001648 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001649 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001650 * @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 +01001651 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001652 */
1653static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001654lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001655{
Radek Krejci54579462019-04-30 12:47:06 +02001656 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001657 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001658 int err_code;
1659 const char *orig_ptr;
1660 PCRE2_SIZE err_offset;
1661 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001662#define URANGE_LEN 19
1663 char *ublock2urange[][2] = {
1664 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1665 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1666 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1667 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1668 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1669 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1670 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1671 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1672 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1673 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1674 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1675 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1676 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1677 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1678 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1679 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1680 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1681 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1682 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1683 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1684 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1685 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1686 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1687 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1688 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1689 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1690 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1691 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1692 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1693 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1694 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1695 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1696 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1697 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1698 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1699 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1700 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1701 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1702 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1703 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1704 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1705 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1706 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1707 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1708 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1709 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1710 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1711 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1712 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1713 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1714 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1715 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1716 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1717 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1718 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1719 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1720 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1721 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1722 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1723 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1724 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1725 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1726 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1727 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1728 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1729 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1730 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1731 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1732 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1733 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1734 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1735 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1736 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1737 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1738 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1739 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1740 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1741 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1742 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1743 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1744 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1745 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1746 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1747 {NULL, NULL}
1748 };
1749
1750 /* adjust the expression to a Perl equivalent
1751 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1752
1753 /* we need to replace all "$" with "\$", count them now */
1754 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1755
1756 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1757 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1758 perl_regex[0] = '\0';
1759
1760 ptr = perl_regex;
1761
Radek Krejci19a96102018-11-15 13:38:09 +01001762 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1763 if (orig_ptr[0] == '$') {
1764 ptr += sprintf(ptr, "\\$");
1765 } else if (orig_ptr[0] == '^') {
1766 ptr += sprintf(ptr, "\\^");
1767 } else {
1768 ptr[0] = orig_ptr[0];
1769 ++ptr;
1770 }
1771 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001772 ptr[0] = '\0';
1773 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001774
1775 /* substitute Unicode Character Blocks with exact Character Ranges */
1776 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1777 start = ptr - perl_regex;
1778
1779 ptr = strchr(ptr, '}');
1780 if (!ptr) {
1781 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1782 pattern, perl_regex + start + 2, "unterminated character property");
1783 free(perl_regex);
1784 return LY_EVALID;
1785 }
1786 end = (ptr - perl_regex) + 1;
1787
1788 /* need more space */
1789 if (end - start < URANGE_LEN) {
1790 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1791 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1792 }
1793
1794 /* find our range */
1795 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1796 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1797 break;
1798 }
1799 }
1800 if (!ublock2urange[idx][0]) {
1801 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1802 pattern, perl_regex + start + 5, "unknown block name");
1803 free(perl_regex);
1804 return LY_EVALID;
1805 }
1806
1807 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1808 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1809 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1810 ++count;
1811 }
1812 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1813 --count;
1814 }
1815 }
1816 if (count) {
1817 /* skip brackets */
1818 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1819 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1820 } else {
1821 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1822 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1823 }
1824 }
1825
1826 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02001827 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1828 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02001829 &err_code, &err_offset, NULL);
1830 if (!code_local) {
1831 PCRE2_UCHAR err_msg[256] = {0};
1832 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1834 free(perl_regex);
1835 return LY_EVALID;
1836 }
1837 free(perl_regex);
1838
Radek Krejci54579462019-04-30 12:47:06 +02001839 if (code) {
1840 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001841 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001842 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001843 }
1844
1845 return LY_SUCCESS;
1846
1847#undef URANGE_LEN
1848}
1849
Radek Krejcia3045382018-11-22 14:30:31 +01001850/**
1851 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1852 * @param[in] ctx Compile context.
1853 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001854 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1855 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1856 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1857 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1858 */
Radek Krejci19a96102018-11-15 13:38:09 +01001859static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001860lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01001861 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1862{
1863 struct lysc_pattern **pattern;
1864 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001865 LY_ERR ret = LY_SUCCESS;
1866
1867 /* first, copy the patterns from the base type */
1868 if (base_patterns) {
1869 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1870 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1871 }
1872
1873 LY_ARRAY_FOR(patterns_p, u) {
1874 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1875 *pattern = calloc(1, sizeof **pattern);
1876 ++(*pattern)->refcount;
1877
Radek Krejci54579462019-04-30 12:47:06 +02001878 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01001879 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01001880
1881 if (patterns_p[u].arg[0] == 0x15) {
1882 (*pattern)->inverted = 1;
1883 }
Radek Krejci54579462019-04-30 12:47:06 +02001884 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01001885 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1886 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001887 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1888 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02001889 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01001890 }
1891done:
1892 return ret;
1893}
1894
Radek Krejcia3045382018-11-22 14:30:31 +01001895/**
1896 * @brief map of the possible restrictions combination for the specific built-in type.
1897 */
Radek Krejci19a96102018-11-15 13:38:09 +01001898static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1899 0 /* LY_TYPE_UNKNOWN */,
1900 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001901 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1902 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1903 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1904 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1905 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001906 LYS_SET_BIT /* LY_TYPE_BITS */,
1907 0 /* LY_TYPE_BOOL */,
1908 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1909 0 /* LY_TYPE_EMPTY */,
1910 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1911 LYS_SET_BASE /* LY_TYPE_IDENT */,
1912 LYS_SET_REQINST /* LY_TYPE_INST */,
1913 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001914 LYS_SET_TYPE /* LY_TYPE_UNION */,
1915 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001916 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001917 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001918 LYS_SET_RANGE /* LY_TYPE_INT64 */
1919};
1920
1921/**
1922 * @brief stringification of the YANG built-in data types
1923 */
1924const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1925 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1926 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001927};
1928
Radek Krejcia3045382018-11-22 14:30:31 +01001929/**
1930 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1931 * @param[in] ctx Compile context.
1932 * @param[in] enums_p Array of the parsed enum structures to compile.
1933 * @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 +01001934 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1935 * @param[out] enums Newly created array of the compiled enums information for the current type.
1936 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1937 */
Radek Krejci19a96102018-11-15 13:38:09 +01001938static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001939lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02001940 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01001941{
1942 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001943 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01001944 int32_t value = 0;
1945 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001946 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01001947
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001948 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001949 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1950 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1951 return LY_EVALID;
1952 }
1953
1954 LY_ARRAY_FOR(enums_p, u) {
1955 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1956 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001957 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1958 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02001959 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01001960 if (base_enums) {
1961 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1962 LY_ARRAY_FOR(base_enums, v) {
1963 if (!strcmp(e->name, base_enums[v].name)) {
1964 break;
1965 }
1966 }
1967 if (v == LY_ARRAY_SIZE(base_enums)) {
1968 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1969 "Invalid %s - derived type adds new item \"%s\".",
1970 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1971 return LY_EVALID;
1972 }
1973 match = v;
1974 }
1975
1976 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02001977 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01001978 if (enums_p[u].flags & LYS_SET_VALUE) {
1979 e->value = (int32_t)enums_p[u].value;
1980 if (!u || e->value >= value) {
1981 value = e->value + 1;
1982 }
1983 /* check collision with other values */
1984 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1985 if (e->value == (*enums)[v].value) {
1986 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1987 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1988 e->value, e->name, (*enums)[v].name);
1989 return LY_EVALID;
1990 }
1991 }
1992 } else if (base_enums) {
1993 /* inherit the assigned value */
1994 e->value = base_enums[match].value;
1995 if (!u || e->value >= value) {
1996 value = e->value + 1;
1997 }
1998 } else {
1999 /* assign value automatically */
2000 if (u && value == INT32_MIN) {
2001 /* counter overflow */
2002 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2003 "Invalid enumeration - it is not possible to auto-assign enum value for "
2004 "\"%s\" since the highest value is already 2147483647.", e->name);
2005 return LY_EVALID;
2006 }
2007 e->value = value++;
2008 }
2009 } else { /* LY_TYPE_BITS */
2010 if (enums_p[u].flags & LYS_SET_VALUE) {
2011 e->value = (int32_t)enums_p[u].value;
2012 if (!u || (uint32_t)e->value >= position) {
2013 position = (uint32_t)e->value + 1;
2014 }
2015 /* check collision with other values */
2016 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2017 if (e->value == (*enums)[v].value) {
2018 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2019 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2020 (uint32_t)e->value, e->name, (*enums)[v].name);
2021 return LY_EVALID;
2022 }
2023 }
2024 } else if (base_enums) {
2025 /* inherit the assigned value */
2026 e->value = base_enums[match].value;
2027 if (!u || (uint32_t)e->value >= position) {
2028 position = (uint32_t)e->value + 1;
2029 }
2030 } else {
2031 /* assign value automatically */
2032 if (u && position == 0) {
2033 /* counter overflow */
2034 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2035 "Invalid bits - it is not possible to auto-assign bit position for "
2036 "\"%s\" since the highest value is already 4294967295.", e->name);
2037 return LY_EVALID;
2038 }
2039 e->value = position++;
2040 }
2041 }
2042
2043 if (base_enums) {
2044 /* the assigned values must not change from the derived type */
2045 if (e->value != base_enums[match].value) {
2046 if (basetype == LY_TYPE_ENUM) {
2047 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2048 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2049 e->name, base_enums[match].value, e->value);
2050 } else {
2051 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2052 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2053 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2054 }
2055 return LY_EVALID;
2056 }
2057 }
2058
Radek Krejciec4da802019-05-02 13:02:41 +02002059 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
2060 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002061
2062 if (basetype == LY_TYPE_BITS) {
2063 /* keep bits ordered by position */
2064 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2065 if (v != u) {
2066 memcpy(&storage, e, sizeof *e);
2067 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2068 memcpy(&(*enums)[v], &storage, sizeof storage);
2069 }
2070 }
2071 }
2072
2073done:
2074 return ret;
2075}
2076
Radek Krejcia3045382018-11-22 14:30:31 +01002077#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2078 for ((NODE) = (NODE)->parent; \
2079 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
2080 (NODE) = (NODE)->parent); \
2081 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2082 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2083 TERM; \
2084 }
2085
2086/**
2087 * @brief Validate the predicate(s) from the leafref path.
2088 * @param[in] ctx Compile context
2089 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2090 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002091 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002092 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002093 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002094 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2095 */
2096static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002097lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002098 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002099{
2100 LY_ERR ret = LY_EVALID;
2101 const struct lys_module *mod;
2102 const struct lysc_node *src_node, *dst_node;
2103 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2104 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01002105 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01002106 const char *start, *end, *pke_end;
2107 struct ly_set keys = {0};
2108 int i;
2109
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002110 assert(path_context);
2111
Radek Krejcia3045382018-11-22 14:30:31 +01002112 while (**predicate == '[') {
2113 start = (*predicate)++;
2114
2115 while (isspace(**predicate)) {
2116 ++(*predicate);
2117 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002118 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002119 while (isspace(**predicate)) {
2120 ++(*predicate);
2121 }
2122 if (**predicate != '=') {
2123 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002124 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2125 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002126 goto cleanup;
2127 }
2128 ++(*predicate);
2129 while (isspace(**predicate)) {
2130 ++(*predicate);
2131 }
2132
2133 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2134 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2135 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2136 goto cleanup;
2137 }
2138 --pke_end;
2139 while (isspace(*pke_end)) {
2140 --pke_end;
2141 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002142 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002143 /* localize path-key-expr */
2144 pke_start = path_key_expr = *predicate;
2145 /* move after the current predicate */
2146 *predicate = end + 1;
2147
2148 /* source (must be leaf or leaf-list) */
2149 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002150 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002151 if (!mod) {
2152 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2153 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002154 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002155 goto cleanup;
2156 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002157 if (!mod->implemented) {
2158 /* make the module implemented */
2159 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2160 }
Radek Krejcia3045382018-11-22 14:30:31 +01002161 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002162 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002163 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002164 src_node = NULL;
2165 if (context_node->keys) {
2166 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2167 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2168 src_node = (const struct lysc_node*)context_node->keys[u];
2169 break;
2170 }
2171 }
2172 }
Radek Krejcia3045382018-11-22 14:30:31 +01002173 if (!src_node) {
2174 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002175 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002176 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002177 goto cleanup;
2178 }
Radek Krejcia3045382018-11-22 14:30:31 +01002179
2180 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002181 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002182 i = ly_set_add(&keys, (void*)src_node, 0);
2183 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002184 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002185 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002186 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002187 *predicate - start, start, src_node->name);
2188 goto cleanup;
2189 }
2190
2191 /* destination */
2192 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002193 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002194
2195 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002196 if (strncmp(path_key_expr, "current", 7)) {
2197error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002198 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2199 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2200 *predicate - start, start);
2201 goto cleanup;
2202 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002203 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2204 if (*path_key_expr != '(') {
2205 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002206 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002207 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2208 if (*path_key_expr != ')') {
2209 goto error_current_function_invocation;
2210 }
2211 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002212
2213 if (*path_key_expr != '/') {
2214 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2215 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2216 *predicate - start, start);
2217 goto cleanup;
2218 }
2219 ++path_key_expr;
2220 while (isspace(*path_key_expr)) {
2221 ++path_key_expr;
2222 }
2223
2224 /* rel-path-keyexpr:
2225 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2226 while (!strncmp(path_key_expr, "..", 2)) {
2227 ++dest_parent_times;
2228 path_key_expr += 2;
2229 while (isspace(*path_key_expr)) {
2230 ++path_key_expr;
2231 }
2232 if (*path_key_expr != '/') {
2233 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2234 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2235 *predicate - start, start);
2236 goto cleanup;
2237 }
2238 ++path_key_expr;
2239 while (isspace(*path_key_expr)) {
2240 ++path_key_expr;
2241 }
2242
2243 /* path is supposed to be evaluated in data tree, so we have to skip
2244 * all schema nodes that cannot be instantiated in data tree */
2245 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2246 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2247 *predicate - start, start);
2248 }
2249 if (!dest_parent_times) {
2250 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2251 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2252 *predicate - start, start);
2253 goto cleanup;
2254 }
2255 if (path_key_expr == pke_end) {
2256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2257 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2258 *predicate - start, start);
2259 goto cleanup;
2260 }
2261
2262 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002263 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002264 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002265 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002266 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2267 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002268 goto cleanup;
2269 }
2270
2271 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002272 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002273 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002274 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002275 }
2276 if (!mod) {
2277 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002278 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002279 *predicate - start, start, dst_len, dst);
2280 goto cleanup;
2281 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002282 if (!mod->implemented) {
2283 /* make the module implemented */
2284 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2285 }
Radek Krejcia3045382018-11-22 14:30:31 +01002286
2287 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2288 if (!dst_node) {
2289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002290 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002291 *predicate - start, start, path_key_expr - pke_start, pke_start);
2292 goto cleanup;
2293 }
2294 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002295 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 +01002296 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002297 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002298 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2299 goto cleanup;
2300 }
2301 }
2302
2303 ret = LY_SUCCESS;
2304cleanup:
2305 ly_set_erase(&keys, NULL);
2306 return ret;
2307}
2308
2309/**
2310 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2311 *
2312 * path-arg = absolute-path / relative-path
2313 * absolute-path = 1*("/" (node-identifier *path-predicate))
2314 * relative-path = 1*(".." "/") descendant-path
2315 *
2316 * @param[in,out] path Path to parse.
2317 * @param[out] prefix Prefix of the token, NULL if there is not any.
2318 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2319 * @param[out] name Name of the token.
2320 * @param[out] nam_len Length of the name.
2321 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2322 * must not be changed between consecutive calls. -1 if the
2323 * path is absolute.
2324 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2325 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2326 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002327LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002328lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2329 int *parent_times, int *has_predicate)
2330{
2331 int par_times = 0;
2332
2333 assert(path && *path);
2334 assert(parent_times);
2335 assert(prefix);
2336 assert(prefix_len);
2337 assert(name);
2338 assert(name_len);
2339 assert(has_predicate);
2340
2341 *prefix = NULL;
2342 *prefix_len = 0;
2343 *name = NULL;
2344 *name_len = 0;
2345 *has_predicate = 0;
2346
2347 if (!*parent_times) {
2348 if (!strncmp(*path, "..", 2)) {
2349 *path += 2;
2350 ++par_times;
2351 while (!strncmp(*path, "/..", 3)) {
2352 *path += 3;
2353 ++par_times;
2354 }
2355 }
2356 if (par_times) {
2357 *parent_times = par_times;
2358 } else {
2359 *parent_times = -1;
2360 }
2361 }
2362
2363 if (**path != '/') {
2364 return LY_EINVAL;
2365 }
2366 /* skip '/' */
2367 ++(*path);
2368
2369 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002370 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002371
2372 if ((**path == '/' && (*path)[1]) || !**path) {
2373 /* path continues by another token or this is the last token */
2374 return LY_SUCCESS;
2375 } else if ((*path)[0] != '[') {
2376 /* unexpected character */
2377 return LY_EINVAL;
2378 } else {
2379 /* predicate starting with [ */
2380 *has_predicate = 1;
2381 return LY_SUCCESS;
2382 }
2383}
2384
2385/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002386 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2387 *
2388 * The set of features used for target must be a subset of features used for the leafref.
2389 * This is not a perfect, we should compare the truth tables but it could require too much resources
2390 * and RFC 7950 does not require it explicitely, so we simplify that.
2391 *
2392 * @param[in] refnode The leafref node.
2393 * @param[in] target Tha target node of the leafref.
2394 * @return LY_SUCCESS or LY_EVALID;
2395 */
2396static LY_ERR
2397lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2398{
2399 LY_ERR ret = LY_EVALID;
2400 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002401 unsigned int u, v, count;
2402 struct ly_set features = {0};
2403
2404 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002405 if (iter->iffeatures) {
2406 LY_ARRAY_FOR(iter->iffeatures, u) {
2407 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2408 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002409 }
2410 }
2411 }
2412 }
2413
2414 /* we should have, in features set, a superset of features applicable to the target node.
2415 * So when adding features applicable to the target into the features set, we should not be
2416 * able to actually add any new feature, otherwise it is not a subset of features applicable
2417 * to the leafref itself. */
2418 count = features.count;
2419 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002420 if (iter->iffeatures) {
2421 LY_ARRAY_FOR(iter->iffeatures, u) {
2422 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2423 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002424 /* new feature was added (or LY_EMEM) */
2425 goto cleanup;
2426 }
2427 }
2428 }
2429 }
2430 }
2431 ret = LY_SUCCESS;
2432
2433cleanup:
2434 ly_set_erase(&features, NULL);
2435 return ret;
2436}
2437
2438/**
Radek Krejcia3045382018-11-22 14:30:31 +01002439 * @brief Validate the leafref path.
2440 * @param[in] ctx Compile context
2441 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002442 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002443 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2444 */
2445static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002446lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002447{
2448 const struct lysc_node *node = NULL, *parent = NULL;
2449 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002450 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002451 const char *id, *prefix, *name;
2452 size_t prefix_len, name_len;
2453 int parent_times = 0, has_predicate;
2454 unsigned int iter, u;
2455 LY_ERR ret = LY_SUCCESS;
2456
2457 assert(ctx);
2458 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002459 assert(leafref);
2460
Radek Krejci1c0c3442019-07-23 16:08:47 +02002461 ctx->path[0] = '\0';
2462 lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
2463 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002464
Radek Krejcia3045382018-11-22 14:30:31 +01002465 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002466 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002467 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2468 if (!iter) { /* first iteration */
2469 /* precess ".." in relative paths */
2470 if (parent_times > 0) {
2471 /* move from the context node */
2472 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2473 /* path is supposed to be evaluated in data tree, so we have to skip
2474 * all schema nodes that cannot be instantiated in data tree */
2475 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002476 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002477 }
2478 }
2479 }
2480
2481 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002482 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002483 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002484 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002485 }
2486 if (!mod) {
2487 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002488 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2489 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002490 return LY_EVALID;
2491 }
Radek Krejci3d929562019-02-21 11:25:55 +01002492 if (!mod->implemented) {
2493 /* make the module implemented */
2494 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2495 }
Radek Krejcia3045382018-11-22 14:30:31 +01002496
2497 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2498 if (!node) {
2499 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002500 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002501 return LY_EVALID;
2502 }
2503 parent = node;
2504
2505 if (has_predicate) {
2506 /* we have predicate, so the current result must be list */
2507 if (node->nodetype != LYS_LIST) {
2508 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2509 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002510 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002511 return LY_EVALID;
2512 }
2513
Radek Krejcibade82a2018-12-05 10:13:48 +01002514 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2515 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002516 }
2517
2518 ++iter;
2519 }
2520 if (ret) {
2521 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002522 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002523 return LY_EVALID;
2524 }
2525
2526 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2527 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2528 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002529 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002530 return LY_EVALID;
2531 }
2532
2533 /* check status */
2534 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2535 return LY_EVALID;
2536 }
2537
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002538 /* check config */
2539 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2540 if (node->flags & LYS_CONFIG_R) {
2541 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2542 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2543 leafref->path);
2544 return LY_EVALID;
2545 }
2546 }
2547
Radek Krejci412ddfa2018-11-23 11:44:11 +01002548 /* store the target's type and check for circular chain of leafrefs */
2549 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2550 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2551 if (type == (struct lysc_type*)leafref) {
2552 /* circular chain detected */
2553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2554 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2555 return LY_EVALID;
2556 }
2557 }
2558
Radek Krejci58d171e2018-11-23 13:50:55 +01002559 /* check if leafref and its target are under common if-features */
2560 if (lys_compile_leafref_features_validate(startnode, node)) {
2561 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2562 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2563 leafref->path);
2564 return LY_EVALID;
2565 }
2566
Radek Krejci327de162019-06-14 12:52:07 +02002567 ctx->path_len = 1;
2568 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002569 return LY_SUCCESS;
2570}
2571
Radek Krejcicdfecd92018-11-26 11:27:32 +01002572static 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 +02002573 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002574/**
2575 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2576 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002577 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2578 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2579 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2580 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002581 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002582 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002583 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002584 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2585 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2586 * @param[out] type Newly created type structure with the filled information about the type.
2587 * @return LY_ERR value.
2588 */
Radek Krejci19a96102018-11-15 13:38:09 +01002589static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002590lys_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 +02002591 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002592 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002593{
2594 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002595 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002596 struct lysc_type_bin *bin;
2597 struct lysc_type_num *num;
2598 struct lysc_type_str *str;
2599 struct lysc_type_bits *bits;
2600 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002601 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002602 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002603 struct lysc_type_union *un, *un_aux;
2604 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002605
2606 switch (basetype) {
2607 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002608 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002609
2610 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002611 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002612 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2613 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002614 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002615 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002616 }
2617 }
2618
2619 if (tpdfname) {
2620 type_p->compiled = *type;
2621 *type = calloc(1, sizeof(struct lysc_type_bin));
2622 }
2623 break;
2624 case LY_TYPE_BITS:
2625 /* RFC 7950 9.7 - bits */
2626 bits = (struct lysc_type_bits*)(*type);
2627 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002628 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002629 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2630 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002631 }
2632
Radek Krejci555cb5b2018-11-16 14:54:33 +01002633 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002634 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002635 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002636 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002637 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002638 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002639 free(*type);
2640 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002641 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002642 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002643 }
2644
2645 if (tpdfname) {
2646 type_p->compiled = *type;
2647 *type = calloc(1, sizeof(struct lysc_type_bits));
2648 }
2649 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002650 case LY_TYPE_DEC64:
2651 dec = (struct lysc_type_dec*)(*type);
2652
2653 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002654 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002655 if (!type_p->fraction_digits) {
2656 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002657 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002658 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002659 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002660 free(*type);
2661 *type = NULL;
2662 }
2663 return LY_EVALID;
2664 }
2665 } else if (type_p->fraction_digits) {
2666 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002667 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002668 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2669 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002670 tpdfname);
2671 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2673 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002674 free(*type);
2675 *type = NULL;
2676 }
2677 return LY_EVALID;
2678 }
2679 dec->fraction_digits = type_p->fraction_digits;
2680
2681 /* RFC 7950 9.2.4 - range */
2682 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002683 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2684 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002685 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002686 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002687 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002688 }
2689
2690 if (tpdfname) {
2691 type_p->compiled = *type;
2692 *type = calloc(1, sizeof(struct lysc_type_dec));
2693 }
2694 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002695 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002696 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002697
2698 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002699 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002700 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2701 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002702 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002703 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002704 }
2705 } else if (base && ((struct lysc_type_str*)base)->length) {
2706 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2707 }
2708
2709 /* RFC 7950 9.4.5 - pattern */
2710 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002711 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002712 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002713 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2714 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2715 }
2716
2717 if (tpdfname) {
2718 type_p->compiled = *type;
2719 *type = calloc(1, sizeof(struct lysc_type_str));
2720 }
2721 break;
2722 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002723 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002724
2725 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002726 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002727 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002728 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002729 }
2730
Radek Krejci555cb5b2018-11-16 14:54:33 +01002731 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002732 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002733 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002735 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002736 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002737 free(*type);
2738 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002739 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002740 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002741 }
2742
2743 if (tpdfname) {
2744 type_p->compiled = *type;
2745 *type = calloc(1, sizeof(struct lysc_type_enum));
2746 }
2747 break;
2748 case LY_TYPE_INT8:
2749 case LY_TYPE_UINT8:
2750 case LY_TYPE_INT16:
2751 case LY_TYPE_UINT16:
2752 case LY_TYPE_INT32:
2753 case LY_TYPE_UINT32:
2754 case LY_TYPE_INT64:
2755 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002756 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002757
2758 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002759 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002760 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2761 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002762 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002763 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002764 }
2765 }
2766
2767 if (tpdfname) {
2768 type_p->compiled = *type;
2769 *type = calloc(1, sizeof(struct lysc_type_num));
2770 }
2771 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002772 case LY_TYPE_IDENT:
2773 idref = (struct lysc_type_identityref*)(*type);
2774
2775 /* RFC 7950 9.10.2 - base */
2776 if (type_p->bases) {
2777 if (base) {
2778 /* only the directly derived identityrefs can contain base specification */
2779 if (tpdfname) {
2780 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002781 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002782 tpdfname);
2783 } else {
2784 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002785 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002786 free(*type);
2787 *type = NULL;
2788 }
2789 return LY_EVALID;
2790 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002791 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002792 }
2793
2794 if (!base && !type_p->flags) {
2795 /* type derived from identityref built-in type must contain at least one base */
2796 if (tpdfname) {
2797 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2798 } else {
2799 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2800 free(*type);
2801 *type = NULL;
2802 }
2803 return LY_EVALID;
2804 }
2805
2806 if (tpdfname) {
2807 type_p->compiled = *type;
2808 *type = calloc(1, sizeof(struct lysc_type_identityref));
2809 }
2810 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002811 case LY_TYPE_LEAFREF:
2812 /* RFC 7950 9.9.3 - require-instance */
2813 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002814 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002815 if (tpdfname) {
2816 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2817 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2818 } else {
2819 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2820 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2821 free(*type);
2822 *type = NULL;
2823 }
2824 return LY_EVALID;
2825 }
Radek Krejcia3045382018-11-22 14:30:31 +01002826 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002827 } else if (base) {
2828 /* inherit */
2829 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002830 } else {
2831 /* default is true */
2832 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2833 }
2834 if (type_p->path) {
2835 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002836 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002837 } else if (base) {
2838 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002839 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002840 } else if (tpdfname) {
2841 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2842 return LY_EVALID;
2843 } else {
2844 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2845 free(*type);
2846 *type = NULL;
2847 return LY_EVALID;
2848 }
2849 if (tpdfname) {
2850 type_p->compiled = *type;
2851 *type = calloc(1, sizeof(struct lysc_type_leafref));
2852 }
2853 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002854 case LY_TYPE_INST:
2855 /* RFC 7950 9.9.3 - require-instance */
2856 if (type_p->flags & LYS_SET_REQINST) {
2857 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2858 } else {
2859 /* default is true */
2860 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2861 }
2862
2863 if (tpdfname) {
2864 type_p->compiled = *type;
2865 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2866 }
2867 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002868 case LY_TYPE_UNION:
2869 un = (struct lysc_type_union*)(*type);
2870
2871 /* RFC 7950 7.4 - type */
2872 if (type_p->types) {
2873 if (base) {
2874 /* only the directly derived union can contain types specification */
2875 if (tpdfname) {
2876 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2877 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2878 tpdfname);
2879 } else {
2880 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2881 "Invalid type substatement for the type not directly derived from union built-in type.");
2882 free(*type);
2883 *type = NULL;
2884 }
2885 return LY_EVALID;
2886 }
2887 /* compile the type */
2888 additional = 0;
2889 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2890 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002891 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 +01002892 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2893 /* add space for additional types from the union subtype */
2894 un_aux = (struct lysc_type_union *)un->types[u + additional];
2895 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)));
2896 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2897 un->types = (void*)((uint32_t*)(p) + 1);
2898
2899 /* copy subtypes of the subtype union */
2900 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2901 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2902 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2903 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2904 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2905 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2906 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2907 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2908 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2909 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2910 /* TODO extensions */
2911
2912 } else {
2913 un->types[u + additional] = un_aux->types[v];
2914 ++un_aux->types[v]->refcount;
2915 }
2916 ++additional;
2917 LY_ARRAY_INCREMENT(un->types);
2918 }
2919 /* compensate u increment in main loop */
2920 --additional;
2921
2922 /* free the replaced union subtype */
2923 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2924 } else {
2925 LY_ARRAY_INCREMENT(un->types);
2926 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002927 }
2928 }
2929
2930 if (!base && !type_p->flags) {
2931 /* type derived from union built-in type must contain at least one type */
2932 if (tpdfname) {
2933 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2934 } else {
2935 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2936 free(*type);
2937 *type = NULL;
2938 }
2939 return LY_EVALID;
2940 }
2941
2942 if (tpdfname) {
2943 type_p->compiled = *type;
2944 *type = calloc(1, sizeof(struct lysc_type_union));
2945 }
2946 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002947 case LY_TYPE_BOOL:
2948 case LY_TYPE_EMPTY:
2949 case LY_TYPE_UNKNOWN: /* just to complete switch */
2950 break;
2951 }
2952 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2953done:
2954 return ret;
2955}
2956
Radek Krejcia3045382018-11-22 14:30:31 +01002957/**
2958 * @brief Compile information about the leaf/leaf-list's type.
2959 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002960 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2961 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2962 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2963 * @param[in] context_name Name of the context node or referencing typedef for logging.
2964 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002965 * @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 +01002966 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002967 * @return LY_ERR value.
2968 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002969static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002970lys_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 +02002971 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002972{
2973 LY_ERR ret = LY_SUCCESS;
2974 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002975 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002976 struct type_context {
2977 const struct lysp_tpdf *tpdf;
2978 struct lysp_node *node;
2979 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002980 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002981 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002982 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002983 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002984 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002985 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002986
2987 (*type) = NULL;
2988
2989 tctx = calloc(1, sizeof *tctx);
2990 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002991 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002992 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2993 ret == LY_SUCCESS;
2994 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
2995 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
2996 if (basetype) {
2997 break;
2998 }
2999
3000 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003001 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3002 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003003 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3004
Radek Krejcicdfecd92018-11-26 11:27:32 +01003005 if (units && !*units) {
3006 /* inherit units */
3007 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3008 }
Radek Krejci01342af2019-01-03 15:18:08 +01003009 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003010 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003011 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003012 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003013 }
Radek Krejci01342af2019-01-03 15:18:08 +01003014 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003015 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3016 break;
3017 }
3018
Radek Krejci19a96102018-11-15 13:38:09 +01003019 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003020 /* it is not necessary to continue, the rest of the chain was already compiled,
3021 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003022 basetype = tctx->tpdf->type.compiled->basetype;
3023 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003024 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003025 dummyloops = 1;
3026 goto preparenext;
3027 } else {
3028 tctx = NULL;
3029 break;
3030 }
Radek Krejci19a96102018-11-15 13:38:09 +01003031 }
3032
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003033 /* circular typedef reference detection */
3034 for (u = 0; u < tpdf_chain.count; u++) {
3035 /* local part */
3036 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3037 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3038 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3039 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3040 free(tctx);
3041 ret = LY_EVALID;
3042 goto cleanup;
3043 }
3044 }
3045 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3046 /* global part for unions corner case */
3047 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3048 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3049 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3050 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3051 free(tctx);
3052 ret = LY_EVALID;
3053 goto cleanup;
3054 }
3055 }
3056
Radek Krejci19a96102018-11-15 13:38:09 +01003057 /* store information for the following processing */
3058 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3059
Radek Krejcicdfecd92018-11-26 11:27:32 +01003060preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003061 /* prepare next loop */
3062 tctx_prev = tctx;
3063 tctx = calloc(1, sizeof *tctx);
3064 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3065 }
3066 free(tctx);
3067
3068 /* allocate type according to the basetype */
3069 switch (basetype) {
3070 case LY_TYPE_BINARY:
3071 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003072 break;
3073 case LY_TYPE_BITS:
3074 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003075 break;
3076 case LY_TYPE_BOOL:
3077 case LY_TYPE_EMPTY:
3078 *type = calloc(1, sizeof(struct lysc_type));
3079 break;
3080 case LY_TYPE_DEC64:
3081 *type = calloc(1, sizeof(struct lysc_type_dec));
3082 break;
3083 case LY_TYPE_ENUM:
3084 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003085 break;
3086 case LY_TYPE_IDENT:
3087 *type = calloc(1, sizeof(struct lysc_type_identityref));
3088 break;
3089 case LY_TYPE_INST:
3090 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3091 break;
3092 case LY_TYPE_LEAFREF:
3093 *type = calloc(1, sizeof(struct lysc_type_leafref));
3094 break;
3095 case LY_TYPE_STRING:
3096 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003097 break;
3098 case LY_TYPE_UNION:
3099 *type = calloc(1, sizeof(struct lysc_type_union));
3100 break;
3101 case LY_TYPE_INT8:
3102 case LY_TYPE_UINT8:
3103 case LY_TYPE_INT16:
3104 case LY_TYPE_UINT16:
3105 case LY_TYPE_INT32:
3106 case LY_TYPE_UINT32:
3107 case LY_TYPE_INT64:
3108 case LY_TYPE_UINT64:
3109 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003110 break;
3111 case LY_TYPE_UNKNOWN:
3112 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3113 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3114 ret = LY_EVALID;
3115 goto cleanup;
3116 }
3117 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003118 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003119 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3120 ly_data_type2str[basetype]);
3121 free(*type);
3122 (*type) = NULL;
3123 ret = LY_EVALID;
3124 goto cleanup;
3125 }
3126
3127 /* get restrictions from the referred typedefs */
3128 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3129 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003130
3131 /* remember the typedef context for circular check */
3132 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3133
Radek Krejci43699232018-11-23 14:59:46 +01003134 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003135 base = tctx->tpdf->type.compiled;
3136 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003137 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003138 /* no change, just use the type information from the base */
3139 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3140 ++base->refcount;
3141 continue;
3142 }
3143
3144 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003145 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3146 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3147 tctx->tpdf->name, ly_data_type2str[basetype]);
3148 ret = LY_EVALID;
3149 goto cleanup;
3150 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3151 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3152 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3153 tctx->tpdf->name, tctx->tpdf->dflt);
3154 ret = LY_EVALID;
3155 goto cleanup;
3156 }
3157
Radek Krejci19a96102018-11-15 13:38:09 +01003158 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003159 /* TODO user type plugins */
3160 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003161 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003162 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 +01003163 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003164 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003165 LY_CHECK_GOTO(ret, cleanup);
3166 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003167 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003168 /* remove the processed typedef contexts from the stack for circular check */
3169 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003170
Radek Krejcic5c27e52018-11-15 14:38:11 +01003171 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003172 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003173 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003174 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003175 /* TODO user type plugins */
3176 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003177 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003178 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 +01003179 LY_CHECK_GOTO(ret, cleanup);
3180 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003181 /* no specific restriction in leaf's type definition, copy from the base */
3182 free(*type);
3183 (*type) = base;
3184 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003185 }
Radek Krejcia1911222019-07-22 17:24:50 +02003186 if (dflt && !(*type)->dflt) {
3187 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003188 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003189 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3190 (*type)->dflt->realtype = (*type);
3191 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3192 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003193 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003194 if (err) {
3195 ly_err_print(err);
3196 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3197 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3198 ly_err_free(err);
3199 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003200 if (ret == LY_EINCOMPLETE) {
3201 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003202 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003203
3204 /* but in general result is so far ok */
3205 ret = LY_SUCCESS;
3206 }
Radek Krejcia1911222019-07-22 17:24:50 +02003207 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003208 }
Radek Krejci19a96102018-11-15 13:38:09 +01003209
Radek Krejciec4da802019-05-02 13:02:41 +02003210 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003211
3212cleanup:
3213 ly_set_erase(&tpdf_chain, free);
3214 return ret;
3215}
3216
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003217/**
3218 * @brief Compile status information of the given node.
3219 *
3220 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3221 * has the status correctly set during the compilation.
3222 *
3223 * @param[in] ctx Compile context
3224 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3225 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3226 * the compatibility with the parent's status value.
3227 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3228 * @return LY_ERR value.
3229 */
3230static LY_ERR
3231lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3232{
3233 /* status - it is not inherited by specification, but it does not make sense to have
3234 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3235 if (!((*node_flags) & LYS_STATUS_MASK)) {
3236 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3237 if ((parent_flags & 0x3) != 0x3) {
3238 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3239 * combination of bits (0x3) which marks the uses_status value */
3240 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3241 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3242 }
3243 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3244 } else {
3245 (*node_flags) |= LYS_STATUS_CURR;
3246 }
3247 } else if (parent_flags & LYS_STATUS_MASK) {
3248 /* check status compatibility with the parent */
3249 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3250 if ((*node_flags) & LYS_STATUS_CURR) {
3251 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3252 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3253 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3254 } else { /* LYS_STATUS_DEPRC */
3255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3256 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3257 }
3258 return LY_EVALID;
3259 }
3260 }
3261 return LY_SUCCESS;
3262}
3263
Radek Krejci8cce8532019-03-05 11:27:45 +01003264/**
3265 * @brief Check uniqness of the node/action/notification name.
3266 *
3267 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3268 * structures, but they share the namespace so we need to check their name collisions.
3269 *
3270 * @param[in] ctx Compile context.
3271 * @param[in] children List (linked list) of data nodes to go through.
3272 * @param[in] actions List (sized array) of actions or RPCs to go through.
3273 * @param[in] notifs List (sized array) of Notifications to go through.
3274 * @param[in] name Name of the item to find in the given lists.
3275 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3276 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3277 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3278 */
3279static LY_ERR
3280lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3281 const struct lysc_action *actions, const struct lysc_notif *notifs,
3282 const char *name, void *exclude)
3283{
3284 const struct lysc_node *iter;
3285 unsigned int u;
3286
3287 LY_LIST_FOR(children, iter) {
3288 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3289 goto error;
3290 }
3291 }
3292 LY_ARRAY_FOR(actions, u) {
3293 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3294 goto error;
3295 }
3296 }
3297 LY_ARRAY_FOR(notifs, u) {
3298 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3299 goto error;
3300 }
3301 }
3302 return LY_SUCCESS;
3303error:
3304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3305 return LY_EEXIST;
3306}
3307
Radek Krejciec4da802019-05-02 13:02:41 +02003308static 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 +01003309
Radek Krejcia3045382018-11-22 14:30:31 +01003310/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003311 * @brief Compile parsed RPC/action schema node information.
3312 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003313 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003314 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003315 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3316 * @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).
3317 * Zero means no uses, non-zero value with no status bit set mean the default status.
3318 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3319 */
3320static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003321lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003322 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3323{
3324 LY_ERR ret = LY_SUCCESS;
3325 struct lysp_node *child_p;
3326 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003327 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003328
Radek Krejci327de162019-06-14 12:52:07 +02003329 lysc_update_path(ctx, parent, action_p->name);
3330
Radek Krejci8cce8532019-03-05 11:27:45 +01003331 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3332 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3333 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3334 action_p->name, action)) {
3335 return LY_EVALID;
3336 }
3337
Radek Krejciec4da802019-05-02 13:02:41 +02003338 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003339 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003340 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003341 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003342 return LY_EVALID;
3343 }
3344
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003345 action->nodetype = LYS_ACTION;
3346 action->module = ctx->mod;
3347 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003348 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003349 action->sp = action_p;
3350 }
3351 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3352
3353 /* status - it is not inherited by specification, but it does not make sense to have
3354 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3355 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3356
3357 DUP_STRING(ctx->ctx, action_p->name, action->name);
3358 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3359 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003360 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3361 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003362
3363 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003364 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejciec4da802019-05-02 13:02:41 +02003365 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
3366 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup);
3367 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003368 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003369 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003370 }
Radek Krejci327de162019-06-14 12:52:07 +02003371 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003372 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003373
3374 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003375 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejciec4da802019-05-02 13:02:41 +02003376 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
3377 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup);
3378 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003379 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003380 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003381 }
Radek Krejci327de162019-06-14 12:52:07 +02003382 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003383
Radek Krejci327de162019-06-14 12:52:07 +02003384 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003385cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003386 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003387 return ret;
3388}
3389
3390/**
Radek Krejci43981a32019-04-12 09:44:11 +02003391 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003392 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003393 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003394 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3395 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3396 * @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 +02003397 * Zero means no uses, non-zero value with no status bit set mean the default status.
3398 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3399 */
3400static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003401lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003402 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3403{
3404 LY_ERR ret = LY_SUCCESS;
3405 struct lysp_node *child_p;
3406 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003407 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003408
Radek Krejci327de162019-06-14 12:52:07 +02003409 lysc_update_path(ctx, parent, notif_p->name);
3410
Radek Krejcifc11bd72019-04-11 16:00:05 +02003411 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3412 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3413 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3414 notif_p->name, notif)) {
3415 return LY_EVALID;
3416 }
3417
Radek Krejciec4da802019-05-02 13:02:41 +02003418 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003419 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3420 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003421 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003422 return LY_EVALID;
3423 }
3424
3425 notif->nodetype = LYS_NOTIF;
3426 notif->module = ctx->mod;
3427 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003428 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003429 notif->sp = notif_p;
3430 }
3431 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3432
3433 /* status - it is not inherited by specification, but it does not make sense to have
3434 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3435 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3436
3437 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3438 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3439 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003440 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3441 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup);
3442 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003443
Radek Krejciec4da802019-05-02 13:02:41 +02003444 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003445 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003446 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003447 }
3448
Radek Krejci327de162019-06-14 12:52:07 +02003449 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003450cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003451 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003452 return ret;
3453}
3454
3455/**
Radek Krejcia3045382018-11-22 14:30:31 +01003456 * @brief Compile parsed container node information.
3457 * @param[in] ctx Compile context
3458 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003459 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3460 * is enriched with the container-specific information.
3461 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3462 */
Radek Krejci19a96102018-11-15 13:38:09 +01003463static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003464lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003465{
3466 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3467 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3468 struct lysp_node *child_p;
3469 unsigned int u;
3470 LY_ERR ret = LY_SUCCESS;
3471
Radek Krejcife909632019-02-12 15:34:42 +01003472 if (cont_p->presence) {
3473 cont->flags |= LYS_PRESENCE;
3474 }
3475
Radek Krejci19a96102018-11-15 13:38:09 +01003476 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003477 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003478 }
3479
Radek Krejciec4da802019-05-02 13:02:41 +02003480 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3481 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3482 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003483
3484done:
3485 return ret;
3486}
3487
Radek Krejci33f72892019-02-21 10:36:58 +01003488/*
3489 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3490 * @param[in] ctx Compile context.
3491 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3492 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003493 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3494 * @return LY_ERR value.
3495 */
3496static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003497lys_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 +01003498{
Radek Krejcia1911222019-07-22 17:24:50 +02003499 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003500 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3501
Radek Krejciec4da802019-05-02 13:02:41 +02003502 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 +01003503 leaf->units ? NULL : &leaf->units));
3504 if (leaf->nodetype == LYS_LEAFLIST) {
3505 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003506 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3507 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003508 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003509 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3510 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3511 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3512 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003513 LY_ARRAY_INCREMENT(llist->dflts);
3514 }
3515 } else {
3516 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003517 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003518 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3519 leaf->dflt->realtype = leaf->type->dflt->realtype;
3520 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3521 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003522 }
3523 }
3524 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3525 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3526 ly_set_add(&ctx->unres, leaf, 0);
3527 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3528 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3529 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3530 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3531 ly_set_add(&ctx->unres, leaf, 0);
3532 }
3533 }
3534 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003535 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3536 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3537 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3538 return LY_EVALID;
3539 }
3540 }
3541
Radek Krejci33f72892019-02-21 10:36:58 +01003542 return LY_SUCCESS;
3543}
3544
Radek Krejcia3045382018-11-22 14:30:31 +01003545/**
3546 * @brief Compile parsed leaf node information.
3547 * @param[in] ctx Compile context
3548 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003549 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3550 * is enriched with the leaf-specific information.
3551 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3552 */
Radek Krejci19a96102018-11-15 13:38:09 +01003553static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003554lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003555{
3556 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3557 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3558 unsigned int u;
3559 LY_ERR ret = LY_SUCCESS;
3560
Radek Krejciec4da802019-05-02 13:02:41 +02003561 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003562 if (leaf_p->units) {
3563 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3564 leaf->flags |= LYS_SET_UNITS;
3565 }
Radek Krejcia1911222019-07-22 17:24:50 +02003566
3567 /* the dflt member is just filled to avoid getting the default value from the type */
3568 leaf->dflt = (void*)leaf_p->dflt;
3569 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3570
Radek Krejciccd20f12019-02-15 14:12:27 +01003571 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003572 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003573 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003574 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3575 leaf->dflt->realtype = leaf->type;
3576 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3577 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003578 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003579 leaf->dflt->realtype->refcount++;
3580 if (err) {
3581 ly_err_print(err);
3582 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3583 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3584 ly_err_free(err);
3585 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003586 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003587 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003588 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003589
3590 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003591 ret = LY_SUCCESS;
3592 }
Radek Krejcia1911222019-07-22 17:24:50 +02003593 LY_CHECK_GOTO(ret, done);
Radek Krejci76b3e962018-12-14 17:01:25 +01003594 leaf->flags |= LYS_SET_DFLT;
3595 }
Radek Krejci43699232018-11-23 14:59:46 +01003596
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003597
Radek Krejci19a96102018-11-15 13:38:09 +01003598done:
3599 return ret;
3600}
3601
Radek Krejcia3045382018-11-22 14:30:31 +01003602/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003603 * @brief Compile parsed leaf-list node information.
3604 * @param[in] ctx Compile context
3605 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003606 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3607 * is enriched with the leaf-list-specific information.
3608 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3609 */
3610static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003611lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003612{
3613 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3614 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003615 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003616 LY_ERR ret = LY_SUCCESS;
3617
Radek Krejciec4da802019-05-02 13:02:41 +02003618 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003619 if (llist_p->units) {
3620 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3621 llist->flags |= LYS_SET_UNITS;
3622 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003623
Radek Krejcia1911222019-07-22 17:24:50 +02003624 /* the dflts member is just filled to avoid getting the default value from the type */
3625 llist->dflts = (void*)llist_p->dflts;
3626 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003627 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003628 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003629 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003630 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3631 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003632 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003633 LY_ARRAY_INCREMENT(llist->dflts_mods);
3634 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003635 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003636 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3637 llist->dflts[u]->realtype = llist->type;
3638 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3639 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003640 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003641 llist->dflts[u]->realtype->refcount++;
3642 if (err) {
3643 ly_err_print(err);
3644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3645 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3646 ly_err_free(err);
3647 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003648 if (ret == LY_EINCOMPLETE) {
3649 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003650 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003651
3652 /* but in general result is so far ok */
3653 ret = LY_SUCCESS;
3654 }
Radek Krejcia1911222019-07-22 17:24:50 +02003655 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003656 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003657 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003658 }
Radek Krejcia1911222019-07-22 17:24:50 +02003659 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3660 /* configuration data values must be unique - so check the default values */
3661 LY_ARRAY_FOR(llist->dflts, u) {
3662 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3663 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3664 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003665 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 +02003666 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3667 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3668 if (dynamic) {
3669 free((char*)val);
3670 }
3671 return LY_EVALID;
3672 }
3673 }
3674 }
3675 }
3676
3677 /* 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 +01003678
3679 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003680 if (llist->min) {
3681 llist->flags |= LYS_MAND_TRUE;
3682 }
Radek Krejcib7408632018-11-28 17:12:11 +01003683 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003684
Radek Krejci0e5d8382018-11-28 16:37:53 +01003685done:
3686 return ret;
3687}
3688
3689/**
Radek Krejci7af64242019-02-18 13:07:53 +01003690 * @brief Compile information about list's uniques.
3691 * @param[in] ctx Compile context.
3692 * @param[in] context_module Module where the prefixes are going to be resolved.
3693 * @param[in] uniques Sized array list of unique statements.
3694 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3695 * @return LY_ERR value.
3696 */
3697static LY_ERR
3698lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3699{
3700 LY_ERR ret = LY_SUCCESS;
3701 struct lysc_node_leaf **key, ***unique;
3702 const char *keystr, *delim;
3703 size_t len;
3704 unsigned int v;
3705 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003706 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003707
3708 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3709 config = -1;
3710 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3711 keystr = uniques[v];
3712 while (keystr) {
3713 delim = strpbrk(keystr, " \t\n");
3714 if (delim) {
3715 len = delim - keystr;
3716 while (isspace(*delim)) {
3717 ++delim;
3718 }
3719 } else {
3720 len = strlen(keystr);
3721 }
3722
3723 /* unique node must be present */
3724 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003725 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3726 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003727 if (ret != LY_SUCCESS) {
3728 if (ret == LY_EDENIED) {
3729 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003730 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003731 len, keystr, lys_nodetype2str((*key)->nodetype));
3732 }
3733 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003734 } else if (flags) {
3735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3736 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3737 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3738 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003739 }
3740
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003741
Radek Krejci7af64242019-02-18 13:07:53 +01003742 /* all referenced leafs must be of the same config type */
3743 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3745 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3746 return LY_EVALID;
3747 } else if ((*key)->flags & LYS_CONFIG_W) {
3748 config = 1;
3749 } else { /* LYS_CONFIG_R */
3750 config = 0;
3751 }
3752
3753 /* check status */
3754 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3755 (*key)->flags, (*key)->module, (*key)->name));
3756
3757 /* mark leaf as unique */
3758 (*key)->flags |= LYS_UNIQUE;
3759
3760 /* next unique value in line */
3761 keystr = delim;
3762 }
3763 /* next unique definition */
3764 }
3765
3766 return LY_SUCCESS;
3767}
3768
3769/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003770 * @brief Compile parsed list node information.
3771 * @param[in] ctx Compile context
3772 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003773 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3774 * is enriched with the list-specific information.
3775 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3776 */
3777static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003778lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003779{
3780 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3781 struct lysc_node_list *list = (struct lysc_node_list*)node;
3782 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003783 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003784 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003785 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003786 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003787 LY_ERR ret = LY_SUCCESS;
3788
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003789 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003790 if (list->min) {
3791 list->flags |= LYS_MAND_TRUE;
3792 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003793 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3794
3795 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003796 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003797 }
3798
Radek Krejciec4da802019-05-02 13:02:41 +02003799 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003800
3801 /* keys */
3802 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3803 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3804 return LY_EVALID;
3805 }
3806
3807 /* find all the keys (must be direct children) */
3808 keystr = list_p->key;
3809 while (keystr) {
3810 delim = strpbrk(keystr, " \t\n");
3811 if (delim) {
3812 len = delim - keystr;
3813 while (isspace(*delim)) {
3814 ++delim;
3815 }
3816 } else {
3817 len = strlen(keystr);
3818 }
3819
3820 /* key node must be present */
3821 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3822 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3823 if (!(*key)) {
3824 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3825 "The list's key \"%.*s\" not found.", len, keystr);
3826 return LY_EVALID;
3827 }
3828 /* keys must be unique */
3829 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3830 if (*key == list->keys[u]) {
3831 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3832 "Duplicated key identifier \"%.*s\".", len, keystr);
3833 return LY_EVALID;
3834 }
3835 }
Radek Krejci327de162019-06-14 12:52:07 +02003836 lysc_update_path(ctx, (struct lysc_node*)list, (*key)->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003837 /* key must have the same config flag as the list itself */
3838 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3839 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3840 return LY_EVALID;
3841 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003842 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003843 /* YANG 1.0 denies key to be of empty type */
3844 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3845 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003846 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003847 return LY_EVALID;
3848 }
3849 } else {
3850 /* when and if-feature are illegal on list keys */
3851 if ((*key)->when) {
3852 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003853 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003854 return LY_EVALID;
3855 }
3856 if ((*key)->iffeatures) {
3857 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003858 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003859 return LY_EVALID;
3860 }
3861 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003862
3863 /* check status */
3864 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3865 (*key)->flags, (*key)->module, (*key)->name));
3866
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003867 /* ignore default values of the key */
3868 if ((*key)->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003869 (*key)->dflt->realtype->plugin->free(ctx->ctx, (*key)->dflt);
3870 lysc_type_free(ctx->ctx, (*key)->dflt->realtype);
3871 free((*key)->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003872 (*key)->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003873 (*key)->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003874 }
3875 /* mark leaf as key */
3876 (*key)->flags |= LYS_KEY;
3877
3878 /* next key value */
3879 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003880 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003881 }
3882
3883 /* uniques */
3884 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003885 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003886 }
3887
Radek Krejciec4da802019-05-02 13:02:41 +02003888 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3889 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003890
3891done:
3892 return ret;
3893}
3894
Radek Krejcib56c7502019-02-13 14:19:54 +01003895/**
3896 * @brief Do some checks and set the default choice's case.
3897 *
3898 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3899 *
3900 * @param[in] ctx Compile context.
3901 * @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,
3902 * not the reference to the imported module.
3903 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3904 * @return LY_ERR value.
3905 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003906static LY_ERR
3907lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3908{
3909 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3910 const char *prefix = NULL, *name;
3911 size_t prefix_len = 0;
3912
3913 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3914 name = strchr(dflt, ':');
3915 if (name) {
3916 prefix = dflt;
3917 prefix_len = name - prefix;
3918 ++name;
3919 } else {
3920 name = dflt;
3921 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003922 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003923 /* prefixed default case make sense only for the prefix of the schema itself */
3924 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3925 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3926 prefix_len, prefix);
3927 return LY_EVALID;
3928 }
3929 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3930 if (!ch->dflt) {
3931 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3932 "Default case \"%s\" not found.", dflt);
3933 return LY_EVALID;
3934 }
3935 /* no mandatory nodes directly under the default case */
3936 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003937 if (iter->parent != (struct lysc_node*)ch->dflt) {
3938 break;
3939 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003940 if (iter->flags & LYS_MAND_TRUE) {
3941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3942 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3943 return LY_EVALID;
3944 }
3945 }
Radek Krejci01342af2019-01-03 15:18:08 +01003946 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003947 return LY_SUCCESS;
3948}
3949
Radek Krejciccd20f12019-02-15 14:12:27 +01003950static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003951lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003952{
3953 struct lys_module *mod;
3954 const char *prefix = NULL, *name;
3955 size_t prefix_len = 0;
3956 struct lysc_node_case *cs;
3957 struct lysc_node *node;
3958
3959 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3960 name = strchr(dflt, ':');
3961 if (name) {
3962 prefix = dflt;
3963 prefix_len = name - prefix;
3964 ++name;
3965 } else {
3966 name = dflt;
3967 }
3968 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3969 if (prefix) {
3970 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3971 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003972 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3973 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003974 return LY_EVALID;
3975 }
3976 } else {
3977 mod = ctx->mod;
3978 }
3979 /* get the default case */
3980 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3981 if (!cs) {
3982 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003983 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003984 return LY_EVALID;
3985 }
3986
3987 /* check that there is no mandatory node */
3988 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003989 if (node->parent != (struct lysc_node*)cs) {
3990 break;
3991 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003992 if (node->flags & LYS_MAND_TRUE) {
3993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003994 "Invalid deviation adding \"default\" property \"%s\" of choice - "
3995 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01003996 return LY_EVALID;
3997 }
3998 }
3999
4000 /* set the default case in choice */
4001 ch->dflt = cs;
4002 cs->flags |= LYS_SET_DFLT;
4003
4004 return LY_SUCCESS;
4005}
4006
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004007/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004008 * @brief Compile parsed choice node information.
4009 * @param[in] ctx Compile context
4010 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004011 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004012 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004013 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4014 */
4015static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004016lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004017{
4018 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4019 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4020 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004021 LY_ERR ret = LY_SUCCESS;
4022
Radek Krejci056d0a82018-12-06 16:57:25 +01004023 LY_LIST_FOR(ch_p->child, child_p) {
4024 if (child_p->nodetype == LYS_CASE) {
4025 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004026 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004027 }
4028 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004029 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004030 }
4031 }
4032
4033 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004034 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004035 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004036 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004037
Radek Krejci9800fb82018-12-13 14:26:23 +01004038 return ret;
4039}
4040
4041/**
4042 * @brief Compile parsed anydata or anyxml node information.
4043 * @param[in] ctx Compile context
4044 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004045 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4046 * is enriched with the any-specific information.
4047 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4048 */
4049static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004050lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004051{
4052 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4053 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4054 unsigned int u;
4055 LY_ERR ret = LY_SUCCESS;
4056
Radek Krejciec4da802019-05-02 13:02:41 +02004057 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004058
4059 if (any->flags & LYS_CONFIG_W) {
4060 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
4061 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
4062 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004063done:
4064 return ret;
4065}
4066
Radek Krejcib56c7502019-02-13 14:19:54 +01004067/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004068 * @brief Connect the node into the siblings list and check its name uniqueness.
4069 *
4070 * @param[in] ctx Compile context
4071 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4072 * the choice itself is expected instead of a specific case node.
4073 * @param[in] node Schema node to connect into the list.
4074 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4075 */
4076static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004077lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004078{
4079 struct lysc_node **children;
4080
4081 if (node->nodetype == LYS_CASE) {
4082 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4083 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004084 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004085 }
4086 if (children) {
4087 if (!(*children)) {
4088 /* first child */
4089 *children = node;
4090 } else if (*children != node) {
4091 /* by the condition in previous branch we cover the choice/case children
4092 * - the children list is shared by the choice and the the first case, in addition
4093 * the first child of each case must be referenced from the case node. So the node is
4094 * actually always already inserted in case it is the first children - so here such
4095 * a situation actually corresponds to the first branch */
4096 /* insert at the end of the parent's children list */
4097 (*children)->prev->next = node;
4098 node->prev = (*children)->prev;
4099 (*children)->prev = node;
4100
4101 /* check the name uniqueness */
4102 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4103 lysc_node_notifs(parent), node->name, node)) {
4104 return LY_EEXIST;
4105 }
4106 }
4107 }
4108 return LY_SUCCESS;
4109}
4110
Radek Krejci95710c92019-02-11 15:49:55 +01004111/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004112 * @brief Get the XPath context node for the given schema node.
4113 * @param[in] start The schema node where the XPath expression appears.
4114 * @return The context node to evaluate XPath expression in given schema node.
4115 * @return NULL in case the context node is the root node.
4116 */
4117static struct lysc_node *
4118lysc_xpath_context(struct lysc_node *start)
4119{
4120 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
4121 start = start->parent);
4122 return start;
4123}
4124
4125/**
4126 * @brief Prepare the case structure in choice node for the new data node.
4127 *
4128 * 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
4129 * created in the choice when the first child was processed.
4130 *
4131 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004132 * @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,
4133 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004134 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4135 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4136 * @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,
4137 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004138 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004139static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004140lys_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 +01004141{
4142 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004143 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004144 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004145 unsigned int u;
4146 LY_ERR ret;
4147
Radek Krejci95710c92019-02-11 15:49:55 +01004148#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004149 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004150 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004151 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4152 return NULL; \
4153 } \
4154 }
4155
Radek Krejci95710c92019-02-11 15:49:55 +01004156 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4157 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004158
4159 /* we have to add an implicit case node into the parent choice */
4160 cs = calloc(1, sizeof(struct lysc_node_case));
4161 DUP_STRING(ctx->ctx, child->name, cs->name);
4162 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004163 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004164 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4165 /* the case is already present since the child is not its first children */
4166 return (struct lysc_node_case*)ch->cases->prev;
4167 }
Radek Krejci95710c92019-02-11 15:49:55 +01004168 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004169
4170 /* explicit parent case is not present (this is its first child) */
4171 cs = calloc(1, sizeof(struct lysc_node_case));
4172 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4173 cs->flags = LYS_STATUS_MASK & node_p->flags;
4174 cs->sp = node_p;
4175
Radek Krejcib1b59152019-01-07 13:21:56 +01004176 /* 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 +02004177 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004178
4179 if (node_p->when) {
4180 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004181 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004182 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004183 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004184 }
Radek Krejciec4da802019-05-02 13:02:41 +02004185 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004186 } else {
4187 LOGINT(ctx->ctx);
4188 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004189 }
4190 cs->module = ctx->mod;
4191 cs->prev = (struct lysc_node*)cs;
4192 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004193 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004194 cs->parent = (struct lysc_node*)ch;
4195 cs->child = child;
4196
4197 return cs;
4198error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004199 if (cs) {
4200 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4201 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004202 return NULL;
4203
4204#undef UNIQUE_CHECK
4205}
4206
Radek Krejcib56c7502019-02-13 14:19:54 +01004207/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004208 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004209 *
4210 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004211 * @param[in] node Target node where the config is supposed to be changed.
4212 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004213 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4214 * 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 +01004215 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4216 * @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 +01004217 * @return LY_ERR value.
4218 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004219static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004220lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004221 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004222{
4223 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004224 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004225
4226 if (config == (node->flags & LYS_CONFIG_MASK)) {
4227 /* nothing to do */
4228 return LY_SUCCESS;
4229 }
4230
4231 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004232 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004233 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4234 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004235 "Invalid %s of config - configuration node cannot be child of any state data node.",
4236 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004237 return LY_EVALID;
4238 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004239 node->flags |= LYS_SET_CONFIG;
4240 } else {
4241 if (node->flags & LYS_SET_CONFIG) {
4242 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4243 /* setting config flags, but have node with explicit config true */
4244 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004245 "Invalid %s of config - configuration node cannot be child of any state data node.",
4246 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004247 return LY_EVALID;
4248 }
4249 /* do not change config on nodes where the config is explicitely set, this does not apply to
4250 * nodes, which are being changed explicitly (targets of refine or deviation) */
4251 return LY_SUCCESS;
4252 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004253 }
4254 node->flags &= ~LYS_CONFIG_MASK;
4255 node->flags |= config;
4256
4257 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004258 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004259 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004260 }
4261
Radek Krejci76b3e962018-12-14 17:01:25 +01004262 return LY_SUCCESS;
4263}
4264
Radek Krejcib56c7502019-02-13 14:19:54 +01004265/**
4266 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4267 *
4268 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4269 * the flag to such parents from a mandatory children.
4270 *
4271 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4272 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4273 * (mandatory children was removed).
4274 */
Radek Krejcife909632019-02-12 15:34:42 +01004275void
4276lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4277{
4278 struct lysc_node *iter;
4279
4280 if (add) { /* set flag */
4281 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4282 parent = parent->parent) {
4283 parent->flags |= LYS_MAND_TRUE;
4284 }
4285 } else { /* unset flag */
4286 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004287 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004288 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004289 /* there is another mandatory node */
4290 return;
4291 }
4292 }
4293 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4294 parent->flags &= ~LYS_MAND_TRUE;
4295 }
4296 }
4297}
4298
Radek Krejci056d0a82018-12-06 16:57:25 +01004299/**
Radek Krejci3641f562019-02-13 15:38:40 +01004300 * @brief Internal sorting process for the lys_compile_augment_sort().
4301 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4302 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4303 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4304 */
4305static void
4306lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4307{
4308 unsigned int v;
4309 size_t len;
4310
4311 len = strlen(aug_p->nodeid);
4312 LY_ARRAY_FOR(result, v) {
4313 if (strlen(result[v]->nodeid) <= len) {
4314 continue;
4315 }
4316 if (v < LY_ARRAY_SIZE(result)) {
4317 /* move the rest of array */
4318 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4319 break;
4320 }
4321 }
4322 result[v] = aug_p;
4323 LY_ARRAY_INCREMENT(result);
4324}
4325
4326/**
4327 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4328 *
4329 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4330 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4331 *
4332 * @param[in] ctx Compile context.
4333 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4334 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4335 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4336 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4337 * @return LY_ERR value.
4338 */
4339LY_ERR
4340lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4341{
4342 struct lysp_augment **result = NULL;
4343 unsigned int u, v;
4344 size_t count = 0;
4345
4346 assert(augments);
4347
4348 /* get count of the augments in module and all its submodules */
4349 if (aug_p) {
4350 count += LY_ARRAY_SIZE(aug_p);
4351 }
4352 LY_ARRAY_FOR(inc_p, u) {
4353 if (inc_p[u].submodule->augments) {
4354 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4355 }
4356 }
4357
4358 if (!count) {
4359 *augments = NULL;
4360 return LY_SUCCESS;
4361 }
4362 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4363
4364 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4365 * together, so there can be even /z/y betwwen them. */
4366 LY_ARRAY_FOR(aug_p, u) {
4367 lys_compile_augment_sort_(&aug_p[u], result);
4368 }
4369 LY_ARRAY_FOR(inc_p, u) {
4370 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4371 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4372 }
4373 }
4374
4375 *augments = result;
4376 return LY_SUCCESS;
4377}
4378
4379/**
4380 * @brief Compile the parsed augment connecting it into its target.
4381 *
4382 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4383 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4384 * are already implemented and compiled.
4385 *
4386 * @param[in] ctx Compile context.
4387 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004388 * @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
4389 * children in case of the augmenting uses data.
4390 * @return LY_SUCCESS on success.
4391 * @return LY_EVALID on failure.
4392 */
4393LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004394lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004395{
4396 LY_ERR ret = LY_SUCCESS;
4397 struct lysp_node *node_p, *case_node_p;
4398 struct lysc_node *target; /* target target of the augment */
4399 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004400 struct lysc_when **when, *when_shared;
4401 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004402 uint16_t flags = 0;
4403 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004404 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004405
Radek Krejci327de162019-06-14 12:52:07 +02004406 lysc_update_path(ctx, NULL, "{augment}");
4407 lysc_update_path(ctx, NULL, aug_p->nodeid);
4408
Radek Krejci7af64242019-02-18 13:07:53 +01004409 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004410 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004411 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004412 if (ret != LY_SUCCESS) {
4413 if (ret == LY_EDENIED) {
4414 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4415 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4416 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4417 }
4418 return LY_EVALID;
4419 }
4420
4421 /* check for mandatory nodes
4422 * - new cases augmenting some choice can have mandatory nodes
4423 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4424 */
Radek Krejci733988a2019-02-15 15:12:44 +01004425 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004426 allow_mandatory = 1;
4427 }
4428
4429 when_shared = NULL;
4430 LY_LIST_FOR(aug_p->child, node_p) {
4431 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4432 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4433 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004434 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4435 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004436 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004437 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4438 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004439 return LY_EVALID;
4440 }
4441
4442 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004443 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004444 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004445 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004446 } else {
4447 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004448 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004449 }
4450 }
Radek Krejciec4da802019-05-02 13:02:41 +02004451 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004452
Radek Krejcife13da42019-02-15 14:51:01 +01004453 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4454 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004455 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004456 /* 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 +01004457 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 +01004458 } else if (target->nodetype == LYS_CHOICE) {
4459 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4460 node = ((struct lysc_node_choice*)target)->cases->prev;
4461 } else {
4462 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004463 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004464 }
4465
Radek Krejci733988a2019-02-15 15:12:44 +01004466 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004467 node->flags &= ~LYS_MAND_TRUE;
4468 lys_compile_mandatory_parents(target, 0);
4469 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004470 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004471 return LY_EVALID;
4472 }
4473
4474 /* pass augment's when to all the children */
4475 if (aug_p->when) {
4476 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4477 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004478 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004479 LY_CHECK_GOTO(ret, error);
4480 (*when)->context = lysc_xpath_context(target);
4481 when_shared = *when;
4482 } else {
4483 ++when_shared->refcount;
4484 (*when) = when_shared;
4485 }
4486 }
4487 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004488
Radek Krejciec4da802019-05-02 13:02:41 +02004489 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004490 switch (target->nodetype) {
4491 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004492 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004493 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004494 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004495 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004496 break;
4497 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004498 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004499 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004500 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004501 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004502 break;
4503 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004504 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004505 if (aug_p->actions) {
4506 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004507 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4508 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004509 return LY_EVALID;
4510 }
4511 if (aug_p->notifs) {
4512 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004513 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4514 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004515 return LY_EVALID;
4516 }
4517 }
Radek Krejci3641f562019-02-13 15:38:40 +01004518
Radek Krejci327de162019-06-14 12:52:07 +02004519 lysc_update_path(ctx, NULL, NULL);
4520 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004521error:
Radek Krejciec4da802019-05-02 13:02:41 +02004522 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004523 return ret;
4524}
4525
4526/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004527 * @brief Apply refined or deviated mandatory flag to the target node.
4528 *
4529 * @param[in] ctx Compile context.
4530 * @param[in] node Target node where the mandatory property is supposed to be changed.
4531 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004532 * @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 +01004533 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4534 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4535 * 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 +01004536 * @return LY_ERR value.
4537 */
4538static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004539lys_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 +01004540{
4541 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4542 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004543 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4544 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004545 return LY_EVALID;
4546 }
4547
4548 if (mandatory_flag & LYS_MAND_TRUE) {
4549 /* check if node has default value */
4550 if (node->nodetype & LYS_LEAF) {
4551 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004552 if (refine_flag) {
4553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004554 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004555 return LY_EVALID;
4556 }
Radek Krejcia1911222019-07-22 17:24:50 +02004557 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004558 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004559 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004560
4561 /* update the list of incomplete default values if needed */
4562 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4563
Radek Krejcia1911222019-07-22 17:24:50 +02004564 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4565 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4566 free(leaf->dflt);
4567 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004568 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004569 }
4570 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004571 if (refine_flag) {
4572 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004573 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004574 return LY_EVALID;
4575 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004576 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004577 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004578 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 +01004579 return LY_EVALID;
4580 }
4581
4582 node->flags &= ~LYS_MAND_FALSE;
4583 node->flags |= LYS_MAND_TRUE;
4584 lys_compile_mandatory_parents(node->parent, 1);
4585 } else {
4586 /* make mandatory false */
4587 node->flags &= ~LYS_MAND_TRUE;
4588 node->flags |= LYS_MAND_FALSE;
4589 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004590 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 +01004591 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004592 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004593 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004594 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4595 leaf->dflt->realtype = leaf->type->dflt->realtype;
4596 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4597 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004598 }
4599 }
4600 return LY_SUCCESS;
4601}
4602
4603/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004604 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4605 * If present, also apply uses's modificators.
4606 *
4607 * @param[in] ctx Compile context
4608 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004609 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4610 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4611 * the compile context.
4612 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4613 */
4614static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004615lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004616{
4617 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004618 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004619 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004620 struct lysc_node_container context_node_fake =
4621 {.nodetype = LYS_CONTAINER,
4622 .module = ctx->mod,
4623 .flags = parent ? parent->flags : 0,
4624 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004625 .prev = (struct lysc_node*)&context_node_fake,
4626 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004627 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004628 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004629 int found;
4630 const char *id, *name, *prefix;
4631 size_t prefix_len, name_len;
4632 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004633 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004634 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004635 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004636 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004637 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004638 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004639 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004640 unsigned int actions_index, notifs_index;
4641 struct lysc_notif **notifs = NULL;
4642 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004643
4644 /* search for the grouping definition */
4645 found = 0;
4646 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004647 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004648 if (prefix) {
4649 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4650 if (!mod) {
4651 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004652 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004653 return LY_EVALID;
4654 }
4655 } else {
4656 mod = ctx->mod_def;
4657 }
4658 if (mod == ctx->mod_def) {
4659 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004660 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004661 LY_ARRAY_FOR(grp, u) {
4662 if (!strcmp(grp[u].name, name)) {
4663 grp = &grp[u];
4664 found = 1;
4665 break;
4666 }
4667 }
4668 }
4669 }
4670 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004671 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004672 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004673 if (grp) {
4674 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4675 if (!strcmp(grp[u].name, name)) {
4676 grp = &grp[u];
4677 found = 1;
4678 }
4679 }
4680 }
4681 if (!found && mod->parsed->includes) {
4682 /* ... and all the submodules */
4683 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4684 grp = mod->parsed->includes[u].submodule->groupings;
4685 if (grp) {
4686 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4687 if (!strcmp(grp[v].name, name)) {
4688 grp = &grp[v];
4689 found = 1;
4690 }
4691 }
4692 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004693 }
4694 }
4695 }
4696 if (!found) {
4697 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4698 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4699 return LY_EVALID;
4700 }
4701
4702 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4703 grp_stack_count = ctx->groupings.count;
4704 ly_set_add(&ctx->groupings, (void*)grp, 0);
4705 if (grp_stack_count == ctx->groupings.count) {
4706 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4707 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4708 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4709 return LY_EVALID;
4710 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004711 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4712 /* remember that the grouping is instantiated to avoid its standalone validation */
4713 grp->flags |= LYS_USED_GRP;
4714 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004715
4716 /* switch context's mod_def */
4717 mod_old = ctx->mod_def;
4718 ctx->mod_def = mod;
4719
4720 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004721 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 +01004722
Radek Krejcifc11bd72019-04-11 16:00:05 +02004723 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004724 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004725 /* 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 +02004726 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 +01004727
4728 /* some preparation for applying refines */
4729 if (grp->data == node_p) {
4730 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004731 if (parent) {
4732 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4733 } else if (ctx->mod->compiled->data) {
4734 child = ctx->mod->compiled->data;
4735 } else {
4736 child = NULL;
4737 }
4738 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004739 }
4740 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004741 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004742 LY_LIST_FOR(context_node_fake.child, child) {
4743 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004744
Radek Krejcifc11bd72019-04-11 16:00:05 +02004745 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004746 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004747 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004748 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004749 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004750 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004751 when_shared = *when;
4752 } else {
4753 ++when_shared->refcount;
4754 (*when) = when_shared;
4755 }
4756 }
Radek Krejci01342af2019-01-03 15:18:08 +01004757 }
4758 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004759 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004760 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004761 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004762 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 +01004763 }
4764
Radek Krejcifc11bd72019-04-11 16:00:05 +02004765 /* compile actions */
4766 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4767 if (actions) {
4768 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004769 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004770 if (*actions && (uses_p->augments || uses_p->refines)) {
4771 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4772 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4773 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4774 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4775 }
4776 }
4777
4778 /* compile notifications */
4779 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4780 if (notifs) {
4781 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004782 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004783 if (*notifs && (uses_p->augments || uses_p->refines)) {
4784 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4785 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4786 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4787 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4788 }
4789 }
4790
4791
Radek Krejci3641f562019-02-13 15:38:40 +01004792 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004793 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004794 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004795 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004796 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004797
Radek Krejcif0089082019-01-07 16:42:01 +01004798 /* reload previous context's mod_def */
4799 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004800 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004801
Radek Krejci76b3e962018-12-14 17:01:25 +01004802 /* apply refine */
4803 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004804 lysc_update_path(ctx, NULL, rfn->nodeid);
4805
Radek Krejci7af64242019-02-18 13:07:53 +01004806 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 +01004807 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004808 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004809 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004810
4811 /* default value */
4812 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004813 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004814 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004815 "Invalid refine of default - %s cannot hold %d default values.",
4816 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004817 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004818 }
4819 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4820 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004821 "Invalid refine of default - %s cannot hold default value(s).",
4822 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004823 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004824 }
4825 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004826 struct ly_err_item *err = NULL;
4827 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4828 if (leaf->dflt) {
4829 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004830 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004831 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4832 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4833 } else {
4834 /* prepare a new one */
4835 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4836 leaf->dflt->realtype = leaf->type;
4837 }
4838 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004839 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004840 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4841 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004842 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004843 leaf->dflt->realtype->refcount++;
4844 if (err) {
4845 ly_err_print(err);
4846 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4847 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4848 ly_err_free(err);
4849 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004850 if (rc == LY_EINCOMPLETE) {
4851 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004852 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004853
4854 /* but in general result is so far ok */
4855 rc = LY_SUCCESS;
4856 }
Radek Krejcia1911222019-07-22 17:24:50 +02004857 LY_CHECK_GOTO(rc, cleanup);
4858 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004859 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004860 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4861
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004862 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004863 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4864 "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 +02004865 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004866 }
Radek Krejcia1911222019-07-22 17:24:50 +02004867
4868 /* remove previous set of default values */
4869 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004870 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004871 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4872 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4873 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004874 }
Radek Krejcia1911222019-07-22 17:24:50 +02004875 LY_ARRAY_FREE(llist->dflts);
4876 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004877 LY_ARRAY_FREE(llist->dflts_mods);
4878 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004879
4880 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004881 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02004882 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004883 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004884 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004885 LY_ARRAY_INCREMENT(llist->dflts_mods);
4886 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004887 LY_ARRAY_INCREMENT(llist->dflts);
4888 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4889 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004890 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004891 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004892 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004893 llist->dflts[u]->realtype->refcount++;
4894 if (err) {
4895 ly_err_print(err);
4896 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4897 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4898 ly_err_free(err);
4899 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004900 if (rc == LY_EINCOMPLETE) {
4901 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004902 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004903
4904 /* but in general result is so far ok */
4905 rc = LY_SUCCESS;
4906 }
4907 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004908 }
Radek Krejcia1911222019-07-22 17:24:50 +02004909 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004910 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004911 if (((struct lysc_node_choice*)node)->dflt) {
4912 /* unset LYS_SET_DFLT from the current default case */
4913 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4914 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004915 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 +01004916 }
4917 }
4918
Radek Krejci12fb9142019-01-08 09:45:30 +01004919 /* description */
4920 if (rfn->dsc) {
4921 FREE_STRING(ctx->ctx, node->dsc);
4922 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4923 }
4924
4925 /* reference */
4926 if (rfn->ref) {
4927 FREE_STRING(ctx->ctx, node->ref);
4928 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4929 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004930
4931 /* config */
4932 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004933 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004934 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004935 } else {
4936 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4937 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4938 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004939 }
4940
4941 /* mandatory */
4942 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004943 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004944 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004945
4946 /* presence */
4947 if (rfn->presence) {
4948 if (node->nodetype != LYS_CONTAINER) {
4949 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004950 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4951 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004952 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004953 }
4954 node->flags |= LYS_PRESENCE;
4955 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004956
4957 /* must */
4958 if (rfn->musts) {
4959 switch (node->nodetype) {
4960 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02004961 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 +01004962 break;
4963 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004964 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 +01004965 break;
4966 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004967 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 +01004968 break;
4969 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02004970 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 +01004971 break;
4972 case LYS_ANYXML:
4973 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02004974 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 +01004975 break;
4976 default:
4977 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004978 "Invalid refine of must statement - %s cannot hold any must statement.",
4979 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004980 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004981 }
4982 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004983
4984 /* min/max-elements */
4985 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4986 switch (node->nodetype) {
4987 case LYS_LEAFLIST:
4988 if (rfn->flags & LYS_SET_MAX) {
4989 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4990 }
4991 if (rfn->flags & LYS_SET_MIN) {
4992 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004993 if (rfn->min) {
4994 node->flags |= LYS_MAND_TRUE;
4995 lys_compile_mandatory_parents(node->parent, 1);
4996 } else {
4997 node->flags &= ~LYS_MAND_TRUE;
4998 lys_compile_mandatory_parents(node->parent, 0);
4999 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005000 }
5001 break;
5002 case LYS_LIST:
5003 if (rfn->flags & LYS_SET_MAX) {
5004 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5005 }
5006 if (rfn->flags & LYS_SET_MIN) {
5007 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005008 if (rfn->min) {
5009 node->flags |= LYS_MAND_TRUE;
5010 lys_compile_mandatory_parents(node->parent, 1);
5011 } else {
5012 node->flags &= ~LYS_MAND_TRUE;
5013 lys_compile_mandatory_parents(node->parent, 0);
5014 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005015 }
5016 break;
5017 default:
5018 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005019 "Invalid refine of %s statement - %s cannot hold this statement.",
5020 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005021 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005022 }
5023 }
Radek Krejcif0089082019-01-07 16:42:01 +01005024
5025 /* if-feature */
5026 if (rfn->iffeatures) {
5027 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005028 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005029 }
Radek Krejci327de162019-06-14 12:52:07 +02005030
5031 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005032 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005033
Radek Krejcif2271f12019-01-07 16:42:23 +01005034 /* do some additional checks of the changed nodes when all the refines are applied */
5035 for (u = 0; u < refined.count; ++u) {
5036 node = (struct lysc_node*)refined.objs[u];
5037 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005038 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005039
5040 /* check possible conflict with default value (default added, mandatory left true) */
5041 if ((node->flags & LYS_MAND_TRUE) &&
5042 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5043 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5044 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005045 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005046 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005047 }
5048
5049 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5050 if (node->nodetype == LYS_LIST) {
5051 min = ((struct lysc_node_list*)node)->min;
5052 max = ((struct lysc_node_list*)node)->max;
5053 } else {
5054 min = ((struct lysc_node_leaflist*)node)->min;
5055 max = ((struct lysc_node_leaflist*)node)->max;
5056 }
5057 if (min > max) {
5058 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005059 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5060 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005061 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005062 }
5063 }
5064 }
5065
Radek Krejci327de162019-06-14 12:52:07 +02005066 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005067 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005068
5069cleanup:
5070 /* fix connection of the children nodes from fake context node back into the parent */
5071 if (context_node_fake.child) {
5072 context_node_fake.child->prev = child;
5073 }
5074 LY_LIST_FOR(context_node_fake.child, child) {
5075 child->parent = parent;
5076 }
5077
5078 if (uses_p->augments || uses_p->refines) {
5079 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005080 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005081 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5082 LY_ARRAY_FREE(context_node_fake.actions);
5083 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005084 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005085 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5086 LY_ARRAY_FREE(context_node_fake.notifs);
5087 }
5088 }
5089
Radek Krejcie86bf772018-12-14 11:39:53 +01005090 /* reload previous context's mod_def */
5091 ctx->mod_def = mod_old;
5092 /* remove the grouping from the stack for circular groupings dependency check */
5093 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5094 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005095 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005096 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005097
5098 return ret;
5099}
5100
Radek Krejci327de162019-06-14 12:52:07 +02005101static int
5102lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5103{
5104 struct lysp_node *iter;
5105 int len = 0;
5106
5107 *path = NULL;
5108 for (iter = node; iter && len >= 0; iter = iter->parent) {
5109 char *s = *path;
5110 char *id;
5111
5112 switch (iter->nodetype) {
5113 case LYS_USES:
5114 asprintf(&id, "{uses='%s'}", iter->name);
5115 break;
5116 case LYS_GROUPING:
5117 asprintf(&id, "{grouping='%s'}", iter->name);
5118 break;
5119 case LYS_AUGMENT:
5120 asprintf(&id, "{augment='%s'}", iter->name);
5121 break;
5122 default:
5123 id = strdup(iter->name);
5124 break;
5125 }
5126
5127 if (!iter->parent) {
5128 /* print prefix */
5129 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5130 } else {
5131 /* prefix is the same as in parent */
5132 len = asprintf(path, "/%s%s", id, s ? s : "");
5133 }
5134 free(s);
5135 free(id);
5136 }
5137
5138 if (len < 0) {
5139 free(*path);
5140 *path = NULL;
5141 } else if (len == 0) {
5142 *path = strdup("/");
5143 len = 1;
5144 }
5145 return len;
5146}
5147
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005148/**
5149 * @brief Validate groupings that were defined but not directly used in the schema itself.
5150 *
5151 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5152 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5153 */
5154static LY_ERR
5155lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5156{
5157 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005158 char *path;
5159 int len;
5160
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005161 struct lysp_node_uses fake_uses = {
5162 .parent = node_p,
5163 .nodetype = LYS_USES,
5164 .flags = 0, .next = NULL,
5165 .name = grp->name,
5166 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5167 .refines = NULL, .augments = NULL
5168 };
5169 struct lysc_node_container fake_container = {
5170 .nodetype = LYS_CONTAINER,
5171 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5172 .module = ctx->mod,
5173 .sp = NULL, .parent = NULL, .next = NULL,
5174 .prev = (struct lysc_node*)&fake_container,
5175 .name = "fake",
5176 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5177 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5178 };
5179
5180 if (grp->parent) {
5181 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5182 }
Radek Krejci327de162019-06-14 12:52:07 +02005183
5184 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5185 if (len < 0) {
5186 LOGMEM(ctx->ctx);
5187 return LY_EMEM;
5188 }
5189 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5190 ctx->path_len = (uint16_t)len;
5191 free(path);
5192
5193 lysc_update_path(ctx, NULL, "{grouping}");
5194 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005195 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005196 lysc_update_path(ctx, NULL, NULL);
5197 lysc_update_path(ctx, NULL, NULL);
5198
5199 ctx->path_len = 1;
5200 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005201
5202 /* cleanup */
5203 lysc_node_container_free(ctx->ctx, &fake_container);
5204
5205 return ret;
5206}
Radek Krejcife909632019-02-12 15:34:42 +01005207
Radek Krejcie86bf772018-12-14 11:39:53 +01005208/**
Radek Krejcia3045382018-11-22 14:30:31 +01005209 * @brief Compile parsed schema node information.
5210 * @param[in] ctx Compile context
5211 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005212 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5213 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5214 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005215 * @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).
5216 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005217 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5218 */
Radek Krejci19a96102018-11-15 13:38:09 +01005219static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005220lys_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 +01005221{
5222 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005223 struct lysc_node *node;
5224 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005225 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005226 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005227 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005228
Radek Krejci327de162019-06-14 12:52:07 +02005229 if (node_p->nodetype != LYS_USES) {
5230 lysc_update_path(ctx, parent, node_p->name);
5231 } else {
5232 lysc_update_path(ctx, NULL, "{uses}");
5233 lysc_update_path(ctx, NULL, node_p->name);
5234 }
5235
Radek Krejci19a96102018-11-15 13:38:09 +01005236 switch (node_p->nodetype) {
5237 case LYS_CONTAINER:
5238 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5239 node_compile_spec = lys_compile_node_container;
5240 break;
5241 case LYS_LEAF:
5242 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5243 node_compile_spec = lys_compile_node_leaf;
5244 break;
5245 case LYS_LIST:
5246 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005247 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005248 break;
5249 case LYS_LEAFLIST:
5250 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005251 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005252 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005253 case LYS_CHOICE:
5254 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005255 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005256 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005257 case LYS_ANYXML:
5258 case LYS_ANYDATA:
5259 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005260 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005261 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005262 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005263 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5264 lysc_update_path(ctx, NULL, NULL);
5265 lysc_update_path(ctx, NULL, NULL);
5266 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005267 default:
5268 LOGINT(ctx->ctx);
5269 return LY_EINT;
5270 }
5271 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5272 node->nodetype = node_p->nodetype;
5273 node->module = ctx->mod;
5274 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005275 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005276
5277 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005278 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005279 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005280 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005281 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5282 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005283 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005284 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005285 node->flags |= LYS_CONFIG_R;
5286 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005287 /* config not explicitely set, inherit it from parent */
5288 if (parent) {
5289 node->flags |= parent->flags & LYS_CONFIG_MASK;
5290 } else {
5291 /* default is config true */
5292 node->flags |= LYS_CONFIG_W;
5293 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005294 } else {
5295 /* config set explicitely */
5296 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005297 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005298 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5299 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5300 "Configuration node cannot be child of any state data node.");
5301 goto error;
5302 }
Radek Krejci19a96102018-11-15 13:38:09 +01005303
Radek Krejcia6d57732018-11-29 13:40:37 +01005304 /* *list ordering */
5305 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5306 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005307 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005308 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5309 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005310 node->flags &= ~LYS_ORDBY_MASK;
5311 node->flags |= LYS_ORDBY_SYSTEM;
5312 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5313 /* default ordering is system */
5314 node->flags |= LYS_ORDBY_SYSTEM;
5315 }
5316 }
5317
Radek Krejci19a96102018-11-15 13:38:09 +01005318 /* status - it is not inherited by specification, but it does not make sense to have
5319 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005320 if (!parent || parent->nodetype != LYS_CHOICE) {
5321 /* in case of choice/case's children, postpone the check to the moment we know if
5322 * 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 +01005323 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 +01005324 }
5325
Radek Krejciec4da802019-05-02 13:02:41 +02005326 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005327 node->sp = node_p;
5328 }
5329 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005330 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5331 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005332 if (node_p->when) {
5333 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005334 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005335 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005336 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005337 }
Radek Krejciec4da802019-05-02 13:02:41 +02005338 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
5339 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005340
5341 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005342 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005343
Radek Krejcife909632019-02-12 15:34:42 +01005344 /* inherit LYS_MAND_TRUE in parent containers */
5345 if (node->flags & LYS_MAND_TRUE) {
5346 lys_compile_mandatory_parents(parent, 1);
5347 }
5348
Radek Krejci327de162019-06-14 12:52:07 +02005349 lysc_update_path(ctx, NULL, NULL);
5350
Radek Krejci19a96102018-11-15 13:38:09 +01005351 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005352 if (parent) {
5353 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005354 if (node_p->parent->nodetype == LYS_CASE) {
5355 lysc_update_path(ctx, parent, node_p->parent->name);
5356 } else {
5357 lysc_update_path(ctx, parent, node->name);
5358 }
Radek Krejciec4da802019-05-02 13:02:41 +02005359 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005360 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005361 if (uses_status) {
5362
5363 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005364 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005365 * it directly gets the same status flags as the choice;
5366 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005367 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005368 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005369 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005370 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005371 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005372 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005373 }
Radek Krejciec4da802019-05-02 13:02:41 +02005374 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 +02005375
5376 if (parent->nodetype == LYS_CHOICE) {
5377 lysc_update_path(ctx, NULL, NULL);
5378 }
Radek Krejci19a96102018-11-15 13:38:09 +01005379 } else {
5380 /* top-level element */
5381 if (!ctx->mod->compiled->data) {
5382 ctx->mod->compiled->data = node;
5383 } else {
5384 /* insert at the end of the module's top-level nodes list */
5385 ctx->mod->compiled->data->prev->next = node;
5386 node->prev = ctx->mod->compiled->data->prev;
5387 ctx->mod->compiled->data->prev = node;
5388 }
Radek Krejci327de162019-06-14 12:52:07 +02005389 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005390 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5391 ctx->mod->compiled->notifs, node->name, node)) {
5392 return LY_EVALID;
5393 }
Radek Krejci19a96102018-11-15 13:38:09 +01005394 }
Radek Krejci327de162019-06-14 12:52:07 +02005395 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005396
5397 return LY_SUCCESS;
5398
5399error:
5400 lysc_node_free(ctx->ctx, node);
5401 return ret;
5402}
5403
Radek Krejciccd20f12019-02-15 14:12:27 +01005404static void
5405lysc_disconnect(struct lysc_node *node)
5406{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005407 struct lysc_node *parent, *child, *prev = NULL, *next;
5408 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005409 int remove_cs = 0;
5410
5411 parent = node->parent;
5412
5413 /* parent's first child */
5414 if (!parent) {
5415 return;
5416 }
5417 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005418 cs = (struct lysc_node_case*)node;
5419 } else if (parent->nodetype == LYS_CASE) {
5420 /* disconnecting some node in a case */
5421 cs = (struct lysc_node_case*)parent;
5422 parent = cs->parent;
5423 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5424 if (child == node) {
5425 if (cs->child == child) {
5426 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5427 /* case with a single child -> remove also the case */
5428 child->parent = NULL;
5429 remove_cs = 1;
5430 } else {
5431 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005432 }
5433 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005434 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005435 }
5436 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005437 if (!remove_cs) {
5438 cs = NULL;
5439 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005440 } else if (lysc_node_children(parent, node->flags) == node) {
5441 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005442 }
5443
5444 if (cs) {
5445 if (remove_cs) {
5446 /* cs has only one child which is being also removed */
5447 lysc_disconnect((struct lysc_node*)cs);
5448 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5449 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005450 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5451 /* default case removed */
5452 ((struct lysc_node_choice*)parent)->dflt = NULL;
5453 }
5454 if (((struct lysc_node_choice*)parent)->cases == cs) {
5455 /* first case removed */
5456 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5457 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005458 if (cs->child) {
5459 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5460 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5461 prev = cs->child->prev;
5462 } /* else all the children are under a single case */
5463 LY_LIST_FOR_SAFE(cs->child, next, child) {
5464 if (child->parent != (struct lysc_node*)cs) {
5465 break;
5466 }
5467 lysc_node_free(node->module->ctx, child);
5468 }
5469 if (prev) {
5470 if (prev->next) {
5471 prev->next = child;
5472 }
5473 if (child) {
5474 child->prev = prev;
5475 } else {
5476 /* link from the first child under the cases */
5477 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5478 }
5479 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005480 }
5481 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005482 }
5483
5484 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005485 if (node->prev->next) {
5486 node->prev->next = node->next;
5487 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005488 if (node->next) {
5489 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005490 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005491 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005492 if (child) {
5493 child->prev = node->prev;
5494 }
5495 } else if (((struct lysc_node_choice*)parent)->cases) {
5496 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005497 }
5498}
5499
5500LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005501lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005502{
Radek Krejcia1911222019-07-22 17:24:50 +02005503 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005504 struct ly_set devs_p = {0};
5505 struct ly_set targets = {0};
5506 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005507 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005508 struct lysc_action *rpcs;
5509 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005510 struct lysp_deviation *dev;
5511 struct lysp_deviate *d, **dp_new;
5512 struct lysp_deviate_add *d_add;
5513 struct lysp_deviate_del *d_del;
5514 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005515 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005516 struct lysc_deviation {
5517 const char *nodeid;
5518 struct lysc_node *target; /* target node of the deviation */
5519 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005520 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005521 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5522 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005523 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005524 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005525 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005526 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005527 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005528 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005529
5530 /* get all deviations from the module and all its submodules ... */
5531 LY_ARRAY_FOR(mod_p->deviations, u) {
5532 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5533 }
5534 LY_ARRAY_FOR(mod_p->includes, v) {
5535 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5536 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5537 }
5538 }
5539 if (!devs_p.count) {
5540 /* nothing to do */
5541 return LY_SUCCESS;
5542 }
5543
Radek Krejci327de162019-06-14 12:52:07 +02005544 lysc_update_path(ctx, NULL, "{deviation}");
5545
Radek Krejciccd20f12019-02-15 14:12:27 +01005546 /* ... and group them by the target node */
5547 devs = calloc(devs_p.count, sizeof *devs);
5548 for (u = 0; u < devs_p.count; ++u) {
5549 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005550 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005551
5552 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005553 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5554 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005555 if (target->nodetype == LYS_ACTION) {
5556 /* move the target pointer to input/output to make them different from the action and
5557 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5558 * back to the RPC/action node due to a better compatibility and decision code in this function.
5559 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5560 if (flags & LYSC_OPT_RPC_INPUT) {
5561 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5562 flags |= LYSC_OPT_INTERNAL;
5563 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5564 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5565 flags |= LYSC_OPT_INTERNAL;
5566 }
5567 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005568 /* insert into the set of targets with duplicity detection */
5569 i = ly_set_add(&targets, target, 0);
5570 if (!devs[i]) {
5571 /* new record */
5572 devs[i] = calloc(1, sizeof **devs);
5573 devs[i]->target = target;
5574 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005575 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005576 }
5577 /* add deviates into the deviation's list of deviates */
5578 for (d = dev->deviates; d; d = d->next) {
5579 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5580 *dp_new = d;
5581 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5582 devs[i]->not_supported = 1;
5583 }
5584 }
Radek Krejci327de162019-06-14 12:52:07 +02005585
5586 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005587 }
5588
5589 /* MACROS for deviates checking */
5590#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5591 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005592 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 +01005593 goto cleanup; \
5594 }
5595
5596#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5597 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005598 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5599 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005600 goto cleanup; \
5601 }
5602
Radek Krejcia1911222019-07-22 17:24:50 +02005603
Radek Krejciccd20f12019-02-15 14:12:27 +01005604#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005605 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5606 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5607 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5608 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5609 goto cleanup; \
5610 }
5611
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005612#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005613 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005614 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005615 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5616 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005617 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005618 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5619 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005620 goto cleanup; \
5621 }
5622
Radek Krejci551b12c2019-02-19 16:11:21 +01005623#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5624 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5625 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005626 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5627 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005628 goto cleanup; \
5629 }
5630
Radek Krejciccd20f12019-02-15 14:12:27 +01005631#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5632 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005633 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005634 goto cleanup; \
5635 }
5636
Radek Krejci551b12c2019-02-19 16:11:21 +01005637#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5638 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5639 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005640 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005641 goto cleanup; \
5642 }
5643
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005644#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5645 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5646 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5647 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5648 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 +01005649 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005650 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005651 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005652 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5653 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005654 goto cleanup; \
5655 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005656 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5657 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5658 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5659 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5660 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005661 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005662 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5663 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5664 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005665 }
5666
5667 /* apply deviations */
5668 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005669 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5670 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5671 struct ly_err_item *err = NULL;
5672
5673 dflt = NULL;
5674 changed_type = 0;
5675
Radek Krejci327de162019-06-14 12:52:07 +02005676 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5677
Radek Krejcif538ce52019-03-05 10:46:14 +01005678 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5679 /* fix the target pointer in case of RPC's/action's input/output */
5680 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5681 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5682 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5683 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5684 }
5685 }
5686
Radek Krejciccd20f12019-02-15 14:12:27 +01005687 /* not-supported */
5688 if (devs[u]->not_supported) {
5689 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5690 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5691 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5692 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005693
5694#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5695 if (devs[u]->target->parent) { \
5696 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5697 } else { \
5698 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5699 } \
5700 LY_ARRAY_FOR(ARRAY, x) { \
5701 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5702 } \
5703 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5704 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5705 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5706 LY_ARRAY_DECREMENT(ARRAY); \
5707 }
5708
Radek Krejcif538ce52019-03-05 10:46:14 +01005709 if (devs[u]->target->nodetype == LYS_ACTION) {
5710 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5711 /* remove RPC's/action's input */
5712 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5713 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005714 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5715 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005716 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5717 /* remove RPC's/action's output */
5718 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5719 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005720 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5721 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005722 } else {
5723 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005724 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005725 }
5726 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005727 /* remove Notification */
5728 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005729 } else {
5730 /* remove the target node */
5731 lysc_disconnect(devs[u]->target);
5732 lysc_node_free(ctx->ctx, devs[u]->target);
5733 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005734
Radek Krejci474f9b82019-07-24 11:36:37 +02005735 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5736 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005737 continue;
5738 }
5739
5740 /* list of deviates (not-supported is not present in the list) */
5741 LY_ARRAY_FOR(devs[u]->deviates, v) {
5742 d = devs[u]->deviates[v];
5743
5744 switch (d->mod) {
5745 case LYS_DEV_ADD:
5746 d_add = (struct lysp_deviate_add*)d;
5747 /* [units-stmt] */
5748 if (d_add->units) {
5749 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5750 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5751
5752 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5753 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5754 }
5755
5756 /* *must-stmt */
5757 if (d_add->musts) {
5758 switch (devs[u]->target->nodetype) {
5759 case LYS_CONTAINER:
5760 case LYS_LIST:
5761 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005762 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005763 break;
5764 case LYS_LEAF:
5765 case LYS_LEAFLIST:
5766 case LYS_ANYDATA:
5767 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005768 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005769 break;
5770 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005771 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005772 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005773 break;
5774 case LYS_ACTION:
5775 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5776 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005777 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005778 break;
5779 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5780 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005781 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005782 break;
5783 }
5784 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005785 default:
5786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005787 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005788 goto cleanup;
5789 }
5790 }
5791
5792 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005793 if (d_add->uniques) {
5794 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5795 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5796 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005797
5798 /* *default-stmt */
5799 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005800 switch (devs[u]->target->nodetype) {
5801 case LYS_LEAF:
5802 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005803 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 +02005804 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005805 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005806 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005807 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5808 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5809 } else {
5810 /* prepare new default value storage */
5811 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005812 }
Radek Krejcia1911222019-07-22 17:24:50 +02005813 dflt = d_add->dflts[0];
5814 /* parsing is done at the end after possible replace of the leaf's type */
5815
Radek Krejci551b12c2019-02-19 16:11:21 +01005816 /* mark the new default values as leaf's own */
5817 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005818 break;
5819 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005820 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005821 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005822 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005823 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005824 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5825 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5826 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005827 }
Radek Krejcia1911222019-07-22 17:24:50 +02005828 LY_ARRAY_FREE(llist->dflts);
5829 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005830 LY_ARRAY_FREE(llist->dflts_mods);
5831 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005832 }
5833 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005834 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005835 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5836 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01005837 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005838 LY_ARRAY_INCREMENT(llist->dflts_mods);
5839 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005840 LY_ARRAY_INCREMENT(llist->dflts);
5841 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5842 llist->dflts[x]->realtype = llist->type;
5843 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 +02005844 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005845 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005846 llist->dflts[x]->realtype->refcount++;
5847 if (err) {
5848 ly_err_print(err);
5849 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5850 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5851 d_add->dflts[x - y], err->msg);
5852 ly_err_free(err);
5853 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005854 if (rc == LY_EINCOMPLETE) {
5855 /* postpone default compilation when the tree is complete */
5856 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5857
5858 /* but in general result is so far ok */
5859 rc = LY_SUCCESS;
5860 }
Radek Krejcia1911222019-07-22 17:24:50 +02005861 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005862 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005863 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005864 devs[u]->target->flags |= LYS_SET_DFLT;
5865 break;
5866 case LYS_CHOICE:
5867 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5868 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5869 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5870 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005871 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 +01005872 goto cleanup;
5873 }
5874 break;
5875 default:
5876 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005877 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005878 goto cleanup;
5879 }
5880 }
5881
5882 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005883 if (d_add->flags & LYS_CONFIG_MASK) {
5884 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005886 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5887 goto cleanup;
5888 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005889 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005890 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5891 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005892 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005893 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5894 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005895 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5896 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005897 goto cleanup;
5898 }
Radek Krejci327de162019-06-14 12:52:07 +02005899 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005900 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005901
5902 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005903 if (d_add->flags & LYS_MAND_MASK) {
5904 if (devs[u]->target->flags & LYS_MAND_MASK) {
5905 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005906 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5907 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005908 goto cleanup;
5909 }
Radek Krejci327de162019-06-14 12:52:07 +02005910 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005911 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005912
5913 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005914 if (d_add->flags & LYS_SET_MIN) {
5915 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5916 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5917 /* change value */
5918 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5919 } else if (devs[u]->target->nodetype == LYS_LIST) {
5920 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5921 /* change value */
5922 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5923 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005924 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005925 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5926 goto cleanup;
5927 }
5928 if (d_add->min) {
5929 devs[u]->target->flags |= LYS_MAND_TRUE;
5930 }
5931 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005932
5933 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005934 if (d_add->flags & LYS_SET_MAX) {
5935 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5936 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5937 /* change value */
5938 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5939 } else if (devs[u]->target->nodetype == LYS_LIST) {
5940 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5941 /* change value */
5942 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5943 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005944 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005945 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5946 goto cleanup;
5947 }
5948 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005949
5950 break;
5951 case LYS_DEV_DELETE:
5952 d_del = (struct lysp_deviate_del*)d;
5953
5954 /* [units-stmt] */
5955 if (d_del->units) {
5956 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02005957 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
5958 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
5959 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5960 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5961 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5962 goto cleanup;
5963 }
5964 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5965 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005966 }
5967
5968 /* *must-stmt */
5969 if (d_del->musts) {
5970 switch (devs[u]->target->nodetype) {
5971 case LYS_CONTAINER:
5972 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005973 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005974 break;
5975 case LYS_LEAF:
5976 case LYS_LEAFLIST:
5977 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005978 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005979 break;
5980 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005981 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005982 break;
5983 case LYS_ACTION:
5984 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5985 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5986 break;
5987 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5988 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5989 break;
5990 }
5991 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005992 default:
5993 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005994 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005995 goto cleanup;
5996 }
5997 }
5998
5999 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006000 if (d_del->uniques) {
6001 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6002 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6003 LY_ARRAY_FOR(d_del->uniques, x) {
6004 LY_ARRAY_FOR(list->uniques, z) {
6005 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6006 nodeid = strpbrk(name, " \t\n");
6007 if (nodeid) {
6008 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
6009 || list->uniques[z][y]->name[nodeid - name] != '\0') {
6010 break;
6011 }
6012 while (isspace(*nodeid)) {
6013 ++nodeid;
6014 }
6015 } else {
6016 if (strcmp(name, list->uniques[z][y]->name)) {
6017 break;
6018 }
6019 }
6020 }
6021 if (!name) {
6022 /* complete match - remove the unique */
6023 LY_ARRAY_DECREMENT(list->uniques);
6024 LY_ARRAY_FREE(list->uniques[z]);
6025 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6026 --z;
6027 break;
6028 }
6029 }
6030 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6031 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006032 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6033 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006034 goto cleanup;
6035 }
6036 }
6037 if (!LY_ARRAY_SIZE(list->uniques)) {
6038 LY_ARRAY_FREE(list->uniques);
6039 list->uniques = NULL;
6040 }
6041 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006042
6043 /* *default-stmt */
6044 if (d_del->dflts) {
6045 switch (devs[u]->target->nodetype) {
6046 case LYS_LEAF:
6047 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6048 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6049 dflt, "deleting", "default", d_del->dflts[0]);
6050
Radek Krejcia1911222019-07-22 17:24:50 +02006051 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006052 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006053 if (strcmp(dflt, d_del->dflts[0])) {
6054 if (i) {
6055 free((char*)dflt);
6056 }
6057 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6058 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6059 d_del->dflts[0], dflt);
6060 goto cleanup;
6061 }
6062 if (i) {
6063 free((char*)dflt);
6064 }
6065 dflt = NULL;
6066
Radek Krejci474f9b82019-07-24 11:36:37 +02006067 /* update the list of incomplete default values if needed */
6068 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6069
Radek Krejcia1911222019-07-22 17:24:50 +02006070 /* remove the default specification */
6071 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6072 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6073 free(leaf->dflt);
6074 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006075 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006076 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006077 break;
6078 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006079 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6080 LY_ARRAY_FOR(d_del->dflts, x) {
6081 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006082 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 +02006083 if (!strcmp(dflt, d_del->dflts[x])) {
6084 if (i) {
6085 free((char*)dflt);
6086 }
6087 dflt = NULL;
6088 break;
6089 }
6090 if (i) {
6091 free((char*)dflt);
6092 }
6093 dflt = NULL;
6094 }
6095 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6096 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6097 "which does not match any of the target's property values.", d_del->dflts[x]);
6098 goto cleanup;
6099 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006100
6101 /* update the list of incomplete default values if needed */
6102 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6103
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006104 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006105 LY_ARRAY_DECREMENT(llist->dflts);
6106 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6107 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6108 free(llist->dflts[y]);
6109 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006110 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 +02006111 }
6112 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006113 LY_ARRAY_FREE(llist->dflts_mods);
6114 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006115 LY_ARRAY_FREE(llist->dflts);
6116 llist->dflts = NULL;
6117 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006118 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006119 break;
6120 case LYS_CHOICE:
6121 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6122 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6123 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006124 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006125 if (prefix) {
6126 /* use module prefixes from the deviation module to match the module of the default case */
6127 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6128 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006129 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6130 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006131 goto cleanup;
6132 }
6133 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6134 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006135 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6136 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006137 goto cleanup;
6138 }
6139 }
6140 /* else {
6141 * strictly, the default prefix would point to the deviation module, but the value should actually
6142 * match the default string in the original module (usually unprefixed), so in this case we do not check
6143 * the module of the default case, just matching its name */
6144 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6145 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006146 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6147 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006148 goto cleanup;
6149 }
6150 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6151 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6152 break;
6153 default:
6154 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006155 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006156 goto cleanup;
6157 }
6158 }
6159
6160 break;
6161 case LYS_DEV_REPLACE:
6162 d_rpl = (struct lysp_deviate_rpl*)d;
6163
6164 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006165 if (d_rpl->type) {
6166 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6167 /* type is mandatory, so checking for its presence is not necessary */
6168 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006169
6170 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6171 /* the target has default from the previous type - remove it */
6172 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006173 /* update the list of incomplete default values if needed */
6174 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6175
Radek Krejcia1911222019-07-22 17:24:50 +02006176 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6177 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6178 free(leaf->dflt);
6179 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006180 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006181 } else { /* LYS_LEAFLIST */
6182 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006183 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006184 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6185 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6186 free(llist->dflts[x]);
6187 }
6188 LY_ARRAY_FREE(llist->dflts);
6189 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006190 LY_ARRAY_FREE(llist->dflts_mods);
6191 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006192 }
6193 }
6194 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006195 /* there is no default value, do not set changed_type after type compilation
6196 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006197 changed_type = -1;
6198 }
Radek Krejciec4da802019-05-02 13:02:41 +02006199 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 +02006200 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006201 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006202
6203 /* [units-stmt] */
6204 if (d_rpl->units) {
6205 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6206 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6207 units, "replacing", "units", d_rpl->units);
6208
6209 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6210 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6211 }
6212
6213 /* [default-stmt] */
6214 if (d_rpl->dflt) {
6215 switch (devs[u]->target->nodetype) {
6216 case LYS_LEAF:
6217 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6218 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006219 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006220 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006221 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6222 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6223 dflt = d_rpl->dflt;
6224 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006225 break;
6226 case LYS_CHOICE:
6227 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006228 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 +01006229 goto cleanup;
6230 }
6231 break;
6232 default:
6233 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006234 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006235 goto cleanup;
6236 }
6237 }
6238
6239 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006240 if (d_rpl->flags & LYS_CONFIG_MASK) {
6241 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006242 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006243 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6244 goto cleanup;
6245 }
6246 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006248 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6249 goto cleanup;
6250 }
Radek Krejci327de162019-06-14 12:52:07 +02006251 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006252 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006253
6254 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006255 if (d_rpl->flags & LYS_MAND_MASK) {
6256 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006257 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006258 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6259 goto cleanup;
6260 }
Radek Krejci327de162019-06-14 12:52:07 +02006261 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006262 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006263
6264 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006265 if (d_rpl->flags & LYS_SET_MIN) {
6266 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6267 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6268 /* change value */
6269 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6270 } else if (devs[u]->target->nodetype == LYS_LIST) {
6271 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6272 /* change value */
6273 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6274 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006275 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006276 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6277 goto cleanup;
6278 }
6279 if (d_rpl->min) {
6280 devs[u]->target->flags |= LYS_MAND_TRUE;
6281 }
6282 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006283
6284 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006285 if (d_rpl->flags & LYS_SET_MAX) {
6286 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6287 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6288 /* change value */
6289 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6290 } else if (devs[u]->target->nodetype == LYS_LIST) {
6291 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6292 /* change value */
6293 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6294 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006296 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6297 goto cleanup;
6298 }
6299 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006300
6301 break;
6302 default:
6303 LOGINT(ctx->ctx);
6304 goto cleanup;
6305 }
6306 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006307
Radek Krejci33f72892019-02-21 10:36:58 +01006308 /* final check when all deviations of a single target node are applied */
6309
Radek Krejci551b12c2019-02-19 16:11:21 +01006310 /* check min-max compatibility */
6311 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6312 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6313 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6314 } else if (devs[u]->target->nodetype == LYS_LIST) {
6315 min = ((struct lysc_node_list*)devs[u]->target)->min;
6316 max = ((struct lysc_node_list*)devs[u]->target)->max;
6317 } else {
6318 min = max = 0;
6319 }
6320 if (min > max) {
6321 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 +02006322 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006323 goto cleanup;
6324 }
6325
Radek Krejcia1911222019-07-22 17:24:50 +02006326 if (dflt) {
6327 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006328 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006329 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006330 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6331 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006332 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006333 leaf->dflt->realtype->refcount++;
6334 if (err) {
6335 ly_err_print(err);
6336 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6337 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6338 ly_err_free(err);
6339 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006340 if (rc == LY_EINCOMPLETE) {
6341 /* postpone default compilation when the tree is complete */
6342 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6343
6344 /* but in general result is so far ok */
6345 rc = LY_SUCCESS;
6346 }
Radek Krejcia1911222019-07-22 17:24:50 +02006347 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006348 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006349 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6350 int dynamic;
6351 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006352 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006353
6354 /* update the list of incomplete default values if needed */
6355 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6356
6357 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006358 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6359 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6360 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006361 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6362 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006363 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006364 leaf->dflt->realtype->refcount++;
6365 if (err) {
6366 ly_err_print(err);
6367 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6368 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6369 ly_err_free(err);
6370 }
6371 if (dynamic) {
6372 free((void*)dflt);
6373 }
Radek Krejcia1911222019-07-22 17:24:50 +02006374 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006375 if (rc == LY_EINCOMPLETE) {
6376 /* postpone default compilation when the tree is complete */
6377 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6378
6379 /* but in general result is so far ok */
6380 rc = LY_SUCCESS;
6381 }
6382 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006383 } else { /* LYS_LEAFLIST */
6384 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006385 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 +02006386 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6387 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6388 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006389 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6390 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006391 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6392 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006393 llist->dflts[x]->realtype->refcount++;
6394 if (err) {
6395 ly_err_print(err);
6396 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6397 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6398 dflt, err->msg);
6399 ly_err_free(err);
6400 }
6401 if (dynamic) {
6402 free((void*)dflt);
6403 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006404 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006405 if (rc == LY_EINCOMPLETE) {
6406 /* postpone default compilation when the tree is complete */
6407 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6408
6409 /* but in general result is so far ok */
6410 rc = LY_SUCCESS;
6411 }
Radek Krejcia1911222019-07-22 17:24:50 +02006412 LY_CHECK_GOTO(rc, cleanup);
6413 }
6414 }
6415 }
6416
Radek Krejci551b12c2019-02-19 16:11:21 +01006417 /* check mandatory - default compatibility */
6418 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6419 && (devs[u]->target->flags & LYS_SET_DFLT)
6420 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6421 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006422 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006423 goto cleanup;
6424 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6425 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6426 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006427 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 +01006428 goto cleanup;
6429 }
6430 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6431 /* mandatory node under a default case */
6432 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006433 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6434 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006435 goto cleanup;
6436 }
Radek Krejci33f72892019-02-21 10:36:58 +01006437
Radek Krejci327de162019-06-14 12:52:07 +02006438 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006439 }
6440
Radek Krejci327de162019-06-14 12:52:07 +02006441 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006442 ret = LY_SUCCESS;
6443
6444cleanup:
6445 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6446 LY_ARRAY_FREE(devs[u]->deviates);
6447 free(devs[u]);
6448 }
6449 free(devs);
6450 ly_set_erase(&targets, NULL);
6451 ly_set_erase(&devs_p, NULL);
6452
6453 return ret;
6454}
6455
Radek Krejcib56c7502019-02-13 14:19:54 +01006456/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006457 * @brief Compile the given YANG submodule into the main module.
6458 * @param[in] ctx Compile context
6459 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006460 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6461 */
6462LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006463lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006464{
6465 unsigned int u;
6466 LY_ERR ret = LY_SUCCESS;
6467 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006468 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006469 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006470 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006471
Radek Krejci0af46292019-01-11 16:02:31 +01006472 if (!mainmod->mod->off_features) {
6473 /* features are compiled directly into the compiled module structure,
6474 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6475 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci327de162019-06-14 12:52:07 +02006476 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01006477 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
6478 LY_CHECK_GOTO(ret, error);
6479 }
6480
Radek Krejci327de162019-06-14 12:52:07 +02006481 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006482 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006483 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006484
Radek Krejci474f9b82019-07-24 11:36:37 +02006485 /* data nodes */
6486 LY_LIST_FOR(submod->data, node_p) {
6487 ret = lys_compile_node(ctx, node_p, NULL, 0);
6488 LY_CHECK_GOTO(ret, error);
6489 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006490
Radek Krejciec4da802019-05-02 13:02:41 +02006491 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6492 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006493
Radek Krejcid05cbd92018-12-05 14:26:40 +01006494error:
6495 return ret;
6496}
6497
Radek Krejci19a96102018-11-15 13:38:09 +01006498LY_ERR
6499lys_compile(struct lys_module *mod, int options)
6500{
6501 struct lysc_ctx ctx = {0};
6502 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006503 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006504 struct lysp_module *sp;
6505 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006506 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006507 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006508 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006509 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006510 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006511
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006512 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006513
6514 if (!mod->implemented) {
6515 /* just imported modules are not compiled */
6516 return LY_SUCCESS;
6517 }
6518
Radek Krejci19a96102018-11-15 13:38:09 +01006519 sp = mod->parsed;
6520
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006521 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006522 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006523 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006524 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006525 ctx.path_len = 1;
6526 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006527
6528 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006529 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6530 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006531
Radek Krejciec4da802019-05-02 13:02:41 +02006532 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006533 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006534 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006535 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6536 }
Radek Krejci0af46292019-01-11 16:02:31 +01006537 if (mod->off_features) {
6538 /* there is already precompiled array of features */
6539 mod_c->features = mod->off_features;
6540 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006541 } else {
6542 /* features are compiled directly into the compiled module structure,
6543 * 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 +02006544 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006545 LY_CHECK_GOTO(ret, error);
6546 }
6547 /* finish feature compilation, not only for the main module, but also for the submodules.
6548 * Due to possible forward references, it must be done when all the features (including submodules)
6549 * are present. */
6550 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006551 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006552 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6553 }
Radek Krejci327de162019-06-14 12:52:07 +02006554 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006555 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006556 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006557 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006558 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006559 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6560 }
Radek Krejci327de162019-06-14 12:52:07 +02006561 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006562 }
Radek Krejci327de162019-06-14 12:52:07 +02006563 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006564
Radek Krejci327de162019-06-14 12:52:07 +02006565 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006566 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006567 if (sp->identities) {
6568 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6569 }
Radek Krejci327de162019-06-14 12:52:07 +02006570 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006571
Radek Krejci95710c92019-02-11 15:49:55 +01006572 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006573 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02006574 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01006575 LY_CHECK_GOTO(ret, error);
6576 }
Radek Krejci95710c92019-02-11 15:49:55 +01006577
Radek Krejciec4da802019-05-02 13:02:41 +02006578 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6579 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006580
Radek Krejci95710c92019-02-11 15:49:55 +01006581 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01006582 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01006583 LY_CHECK_GOTO(ret, error);
6584 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006585 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006586 LY_CHECK_GOTO(ret, error);
6587 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006588
Radek Krejci474f9b82019-07-24 11:36:37 +02006589 /* deviations TODO cover deviations from submodules */
Radek Krejciec4da802019-05-02 13:02:41 +02006590 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01006591 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006592
Radek Krejciec4da802019-05-02 13:02:41 +02006593 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006594
Radek Krejcia3045382018-11-22 14:30:31 +01006595 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006596 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6597 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6598 * 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 +01006599 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006600 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006601 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6602 if (type->basetype == LY_TYPE_LEAFREF) {
6603 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006604 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 +01006605 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006606 } else if (type->basetype == LY_TYPE_UNION) {
6607 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6608 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6609 /* validate the path */
6610 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6611 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
6612 LY_CHECK_GOTO(ret, error);
6613 }
6614 }
Radek Krejcia3045382018-11-22 14:30:31 +01006615 }
6616 }
6617 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006618 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006619 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006620 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6621 if (type->basetype == LY_TYPE_LEAFREF) {
6622 /* store pointer to the real type */
6623 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6624 typeiter->basetype == LY_TYPE_LEAFREF;
6625 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6626 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006627 } else if (type->basetype == LY_TYPE_UNION) {
6628 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6629 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6630 /* store pointer to the real type */
6631 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6632 typeiter->basetype == LY_TYPE_LEAFREF;
6633 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6634 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6635 }
6636 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006637 }
6638 }
6639 }
Radek Krejciec4da802019-05-02 13:02:41 +02006640
Radek Krejci474f9b82019-07-24 11:36:37 +02006641 /* finish incomplete default values compilation */
6642 for (u = 0; u < ctx.dflts.count; ++u) {
6643 struct ly_err_item *err = NULL;
6644 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
6645 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized),
6646 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
6647 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
6648 if (err) {
6649 ly_err_print(err);
6650 ctx.path[0] = '\0';
6651 lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
6652 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
6653 "Invalid default - value does not fit the type (%s).", err->msg);
6654 ly_err_free(err);
6655 }
6656 LY_CHECK_GOTO(ret, error);
6657 }
6658
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006659 /* validate non-instantiated groupings from the parsed schema,
6660 * without it we would accept even the schemas with invalid grouping specification */
6661 ctx.options |= LYSC_OPT_GROUPING;
6662 LY_ARRAY_FOR(sp->groupings, u) {
6663 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
6664 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
6665 }
6666 }
6667 LY_LIST_FOR(sp->data, node_p) {
6668 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
6669 LY_ARRAY_FOR(grps, u) {
6670 if (!(grps[u].flags & LYS_USED_GRP)) {
6671 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
6672 }
6673 }
6674 }
6675
Radek Krejci474f9b82019-07-24 11:36:37 +02006676 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
6677 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
6678 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
6679 }
6680
Radek Krejci1c0c3442019-07-23 16:08:47 +02006681 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006682 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006683 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006684 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006685 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01006686
Radek Krejciec4da802019-05-02 13:02:41 +02006687 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01006688 lysp_module_free(mod->parsed);
6689 ((struct lys_module*)mod)->parsed = NULL;
6690 }
6691
Radek Krejciec4da802019-05-02 13:02:41 +02006692 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01006693 /* remove flag of the modules implemented by dependency */
6694 for (u = 0; u < ctx.ctx->list.count; ++u) {
6695 m = ctx.ctx->list.objs[u];
6696 if (m->implemented == 2) {
6697 m->implemented = 1;
6698 }
6699 }
6700 }
6701
Radek Krejci19a96102018-11-15 13:38:09 +01006702 ((struct lys_module*)mod)->compiled = mod_c;
6703 return LY_SUCCESS;
6704
6705error:
Radek Krejci95710c92019-02-11 15:49:55 +01006706 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02006707 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006708 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006709 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006710 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006711 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006712 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006713 mod->compiled = NULL;
6714
6715 /* revert compilation of modules implemented by dependency */
6716 for (u = 0; u < ctx.ctx->list.count; ++u) {
6717 m = ctx.ctx->list.objs[u];
6718 if (m->implemented == 2) {
6719 /* revert features list to the precompiled state */
6720 lys_feature_precompile_revert(&ctx, m);
6721 /* mark module as imported-only / not-implemented */
6722 m->implemented = 0;
6723 /* free the compiled version of the module */
6724 lysc_module_free(m->compiled, NULL);
6725 m->compiled = NULL;
6726 }
6727 }
6728
Radek Krejci19a96102018-11-15 13:38:09 +01006729 return ret;
6730}