blob: 9d1639d4d9588e2974fa8f51f3b199f9d3e97031 [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) */
Radek Krejciacc79042019-07-25 14:14:57 +0200185 ctx->path_len = 1;
Radek Krejci327de162019-06-14 12:52:07 +0200186 }
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 }
Radek Krejciacc79042019-07-25 14:14:57 +0200212 if (len >= LYSC_CTX_BUFSIZE - ctx->path_len) {
213 /* output truncated */
214 ctx->path_len = LYSC_CTX_BUFSIZE - 1;
215 } else {
216 ctx->path_len += len;
217 }
Radek Krejci327de162019-06-14 12:52:07 +0200218 }
219}
220
221/**
Radek Krejcib56c7502019-02-13 14:19:54 +0100222 * @brief Duplicate the compiled pattern structure.
223 *
224 * Instead of duplicating memory, the reference counter in the @p orig is increased.
225 *
226 * @param[in] orig The pattern structure to duplicate.
227 * @return The duplicated structure to use.
228 */
Radek Krejci19a96102018-11-15 13:38:09 +0100229static struct lysc_pattern*
230lysc_pattern_dup(struct lysc_pattern *orig)
231{
232 ++orig->refcount;
233 return orig;
234}
235
Radek Krejcib56c7502019-02-13 14:19:54 +0100236/**
237 * @brief Duplicate the array of compiled patterns.
238 *
239 * The sized array itself is duplicated, but the pattern structures are just shadowed by increasing their reference counter.
240 *
241 * @param[in] ctx Libyang context for logging.
242 * @param[in] orig The patterns sized array to duplicate.
243 * @return New sized array as a copy of @p orig.
244 * @return NULL in case of memory allocation error.
245 */
Radek Krejci19a96102018-11-15 13:38:09 +0100246static struct lysc_pattern**
247lysc_patterns_dup(struct ly_ctx *ctx, struct lysc_pattern **orig)
248{
Radek Krejcid05cbd92018-12-05 14:26:40 +0100249 struct lysc_pattern **dup = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100250 unsigned int u;
251
Radek Krejcib56c7502019-02-13 14:19:54 +0100252 assert(orig);
253
Radek Krejci19a96102018-11-15 13:38:09 +0100254 LY_ARRAY_CREATE_RET(ctx, dup, LY_ARRAY_SIZE(orig), NULL);
255 LY_ARRAY_FOR(orig, u) {
256 dup[u] = lysc_pattern_dup(orig[u]);
257 LY_ARRAY_INCREMENT(dup);
258 }
259 return dup;
260}
261
Radek Krejcib56c7502019-02-13 14:19:54 +0100262/**
263 * @brief Duplicate compiled range structure.
264 *
265 * @param[in] ctx Libyang context for logging.
266 * @param[in] orig The range structure to be duplicated.
267 * @return New compiled range structure as a copy of @p orig.
268 * @return NULL in case of memory allocation error.
269 */
Radek Krejci19a96102018-11-15 13:38:09 +0100270struct lysc_range*
271lysc_range_dup(struct ly_ctx *ctx, const struct lysc_range *orig)
272{
273 struct lysc_range *dup;
274 LY_ERR ret;
275
Radek Krejcib56c7502019-02-13 14:19:54 +0100276 assert(orig);
277
Radek Krejci19a96102018-11-15 13:38:09 +0100278 dup = calloc(1, sizeof *dup);
279 LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
280 if (orig->parts) {
281 LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_SIZE(orig->parts), ret, cleanup);
282 LY_ARRAY_SIZE(dup->parts) = LY_ARRAY_SIZE(orig->parts);
283 memcpy(dup->parts, orig->parts, LY_ARRAY_SIZE(dup->parts) * sizeof *dup->parts);
284 }
285 DUP_STRING(ctx, orig->eapptag, dup->eapptag);
286 DUP_STRING(ctx, orig->emsg, dup->emsg);
287 dup->exts = lysc_ext_instance_dup(ctx, orig->exts);
288
289 return dup;
290cleanup:
291 free(dup);
292 (void) ret; /* set but not used due to the return type */
293 return NULL;
294}
295
Radek Krejcib56c7502019-02-13 14:19:54 +0100296/**
297 * @brief Stack for processing if-feature expressions.
298 */
Radek Krejci19a96102018-11-15 13:38:09 +0100299struct iff_stack {
Radek Krejcib56c7502019-02-13 14:19:54 +0100300 int size; /**< number of items in the stack */
301 int index; /**< first empty item */
302 uint8_t *stack;/**< stack - array of @ref ifftokens to create the if-feature expression in prefix format */
Radek Krejci19a96102018-11-15 13:38:09 +0100303};
304
Radek Krejcib56c7502019-02-13 14:19:54 +0100305/**
306 * @brief Add @ref ifftokens into the stack.
307 * @param[in] stack The if-feature stack to use.
308 * @param[in] value One of the @ref ifftokens to store in the stack.
309 * @return LY_EMEM in case of memory allocation error
310 * @return LY_ESUCCESS if the value successfully stored.
311 */
Radek Krejci19a96102018-11-15 13:38:09 +0100312static LY_ERR
313iff_stack_push(struct iff_stack *stack, uint8_t value)
314{
315 if (stack->index == stack->size) {
316 stack->size += 4;
317 stack->stack = ly_realloc(stack->stack, stack->size * sizeof *stack->stack);
318 LY_CHECK_ERR_RET(!stack->stack, LOGMEM(NULL); stack->size = 0, LY_EMEM);
319 }
320 stack->stack[stack->index++] = value;
321 return LY_SUCCESS;
322}
323
Radek Krejcib56c7502019-02-13 14:19:54 +0100324/**
325 * @brief Get (and remove) the last item form the stack.
326 * @param[in] stack The if-feature stack to use.
327 * @return The value from the top of the stack.
328 */
Radek Krejci19a96102018-11-15 13:38:09 +0100329static uint8_t
330iff_stack_pop(struct iff_stack *stack)
331{
Radek Krejcib56c7502019-02-13 14:19:54 +0100332 assert(stack && stack->index);
333
Radek Krejci19a96102018-11-15 13:38:09 +0100334 stack->index--;
335 return stack->stack[stack->index];
336}
337
Radek Krejcib56c7502019-02-13 14:19:54 +0100338/**
339 * @brief Clean up the stack.
340 * @param[in] stack The if-feature stack to use.
341 */
Radek Krejci19a96102018-11-15 13:38:09 +0100342static void
343iff_stack_clean(struct iff_stack *stack)
344{
345 stack->size = 0;
346 free(stack->stack);
347}
348
Radek Krejcib56c7502019-02-13 14:19:54 +0100349/**
350 * @brief Store the @ref ifftokens (@p op) on the given position in the 2bits array
351 * (libyang format of the if-feature expression).
352 * @param[in,out] list The 2bits array to modify.
353 * @param[in] op The operand (@ref ifftokens) to store.
354 * @param[in] pos Position (0-based) where to store the given @p op.
355 */
Radek Krejci19a96102018-11-15 13:38:09 +0100356static void
357iff_setop(uint8_t *list, uint8_t op, int pos)
358{
359 uint8_t *item;
360 uint8_t mask = 3;
361
362 assert(pos >= 0);
363 assert(op <= 3); /* max 2 bits */
364
365 item = &list[pos / 4];
366 mask = mask << 2 * (pos % 4);
367 *item = (*item) & ~mask;
368 *item = (*item) | (op << 2 * (pos % 4));
369}
370
Radek Krejcib56c7502019-02-13 14:19:54 +0100371#define LYS_IFF_LP 0x04 /**< Additional, temporary, value of @ref ifftokens: ( */
372#define LYS_IFF_RP 0x08 /**< Additional, temporary, value of @ref ifftokens: ) */
Radek Krejci19a96102018-11-15 13:38:09 +0100373
Radek Krejci0af46292019-01-11 16:02:31 +0100374/**
375 * @brief Find a feature of the given name and referenced in the given module.
376 *
377 * If the compiled schema is available (the schema is implemented), the feature from the compiled schema is
378 * returned. Otherwise, the special array of pre-compiled features is used to search for the feature. Such
379 * features are always disabled (feature from not implemented schema cannot be enabled), but in case the schema
380 * will be made implemented in future (no matter if implicitly via augmenting/deviating it or explicitly via
381 * ly_ctx_module_implement()), the compilation of these feature structure is finished, but the pointers
382 * assigned till that time will be still valid.
383 *
384 * @param[in] mod Module where the feature was referenced (used to resolve prefix of the feature).
385 * @param[in] name Name of the feature including possible prefix.
386 * @param[in] len Length of the string representing the feature identifier in the name variable (mandatory!).
387 * @return Pointer to the feature structure if found, NULL otherwise.
388 */
Radek Krejci19a96102018-11-15 13:38:09 +0100389static struct lysc_feature *
Radek Krejci0af46292019-01-11 16:02:31 +0100390lys_feature_find(struct lys_module *mod, const char *name, size_t len)
Radek Krejci19a96102018-11-15 13:38:09 +0100391{
392 size_t i;
Radek Krejci0af46292019-01-11 16:02:31 +0100393 struct lysc_feature *f, *flist;
Radek Krejci19a96102018-11-15 13:38:09 +0100394
395 for (i = 0; i < len; ++i) {
396 if (name[i] == ':') {
397 /* we have a prefixed feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100398 mod = lys_module_find_prefix(mod, name, i);
Radek Krejci19a96102018-11-15 13:38:09 +0100399 LY_CHECK_RET(!mod, NULL);
400
401 name = &name[i + 1];
402 len = len - i - 1;
403 }
404 }
405
406 /* we have the correct module, get the feature */
Radek Krejci0af46292019-01-11 16:02:31 +0100407 if (mod->implemented) {
408 /* module is implemented so there is already the compiled schema */
409 flist = mod->compiled->features;
410 } else {
411 flist = mod->off_features;
412 }
413 LY_ARRAY_FOR(flist, i) {
414 f = &flist[i];
Radek Krejci19a96102018-11-15 13:38:09 +0100415 if (!strncmp(f->name, name, len) && f->name[len] == '\0') {
416 return f;
417 }
418 }
419
420 return NULL;
421}
422
423static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200424lys_compile_ext(struct lysc_ctx *ctx, struct lysp_ext_instance *ext_p, struct lysc_ext_instance *ext)
Radek Krejci19a96102018-11-15 13:38:09 +0100425{
426 const char *name;
427 unsigned int u;
428 const struct lys_module *mod;
429 struct lysp_ext *edef = NULL;
430
431 DUP_STRING(ctx->ctx, ext_p->argument, ext->argument);
432 ext->insubstmt = ext_p->insubstmt;
433 ext->insubstmt_index = ext_p->insubstmt_index;
434
435 /* get module where the extension definition should be placed */
436 for (u = 0; ext_p->name[u] != ':'; ++u);
Radek Krejcie86bf772018-12-14 11:39:53 +0100437 mod = lys_module_find_prefix(ctx->mod_def, ext_p->name, u);
Radek Krejci19a96102018-11-15 13:38:09 +0100438 LY_CHECK_ERR_RET(!mod, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
439 "Invalid prefix \"%.*s\" used for extension instance identifier.", u, ext_p->name),
440 LY_EVALID);
441 LY_CHECK_ERR_RET(!mod->parsed->extensions,
442 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
443 "Extension instance \"%s\" refers \"%s\" module that does not contain extension definitions.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100444 ext_p->name, mod->name),
Radek Krejci19a96102018-11-15 13:38:09 +0100445 LY_EVALID);
446 name = &ext_p->name[u + 1];
447 /* find the extension definition there */
448 for (ext = NULL, u = 0; u < LY_ARRAY_SIZE(mod->parsed->extensions); ++u) {
449 if (!strcmp(name, mod->parsed->extensions[u].name)) {
450 edef = &mod->parsed->extensions[u];
451 break;
452 }
453 }
454 LY_CHECK_ERR_RET(!edef, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
455 "Extension definition of extension instance \"%s\" not found.", ext_p->name),
456 LY_EVALID);
Radek Krejcia1911222019-07-22 17:24:50 +0200457 /* TODO extension plugins */
Radek Krejci19a96102018-11-15 13:38:09 +0100458
459 return LY_SUCCESS;
460}
461
Radek Krejcib56c7502019-02-13 14:19:54 +0100462/**
463 * @brief Compile information from the if-feature statement
464 * @param[in] ctx Compile context.
465 * @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 +0100466 * @param[in,out] iff Prepared (empty) compiled if-feature structure to fill.
467 * @return LY_ERR value.
468 */
Radek Krejci19a96102018-11-15 13:38:09 +0100469static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200470lys_compile_iffeature(struct lysc_ctx *ctx, const char **value, struct lysc_iffeature *iff)
Radek Krejci19a96102018-11-15 13:38:09 +0100471{
472 const char *c = *value;
473 int r, rc = EXIT_FAILURE;
474 int i, j, last_not, checkversion = 0;
475 unsigned int f_size = 0, expr_size = 0, f_exp = 1;
476 uint8_t op;
477 struct iff_stack stack = {0, 0, NULL};
478 struct lysc_feature *f;
479
480 assert(c);
481
482 /* pre-parse the expression to get sizes for arrays, also do some syntax checks of the expression */
483 for (i = j = last_not = 0; c[i]; i++) {
484 if (c[i] == '(') {
485 j++;
486 checkversion = 1;
487 continue;
488 } else if (c[i] == ')') {
489 j--;
490 continue;
491 } else if (isspace(c[i])) {
492 checkversion = 1;
493 continue;
494 }
495
496 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 +0200497 int sp;
498 for(sp = 0; c[i + r + sp] && isspace(c[i + r + sp]); sp++);
499 if (c[i + r + sp] == '\0') {
Radek Krejci19a96102018-11-15 13:38:09 +0100500 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
501 "Invalid value \"%s\" of if-feature - unexpected end of expression.", *value);
502 return LY_EVALID;
503 } else if (!isspace(c[i + r])) {
504 /* feature name starting with the not/and/or */
505 last_not = 0;
506 f_size++;
507 } else if (c[i] == 'n') { /* not operation */
508 if (last_not) {
509 /* double not */
510 expr_size = expr_size - 2;
511 last_not = 0;
512 } else {
513 last_not = 1;
514 }
515 } else { /* and, or */
Radek Krejci6788abc2019-06-14 13:56:49 +0200516 if (f_exp != f_size) {
517 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
518 "Invalid value \"%s\" of if-feature - missing feature/expression before \"%.*s\" operation.", *value, r, &c[i]);
519 return LY_EVALID;
520 }
Radek Krejci19a96102018-11-15 13:38:09 +0100521 f_exp++;
Radek Krejci6788abc2019-06-14 13:56:49 +0200522
Radek Krejci19a96102018-11-15 13:38:09 +0100523 /* not a not operation */
524 last_not = 0;
525 }
526 i += r;
527 } else {
528 f_size++;
529 last_not = 0;
530 }
531 expr_size++;
532
533 while (!isspace(c[i])) {
Radek Krejci6788abc2019-06-14 13:56:49 +0200534 if (!c[i] || c[i] == ')' || c[i] == '(') {
Radek Krejci19a96102018-11-15 13:38:09 +0100535 i--;
536 break;
537 }
538 i++;
539 }
540 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200541 if (j) {
Radek Krejci19a96102018-11-15 13:38:09 +0100542 /* not matching count of ( and ) */
543 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
544 "Invalid value \"%s\" of if-feature - non-matching opening and closing parentheses.", *value);
545 return LY_EVALID;
546 }
Radek Krejci6788abc2019-06-14 13:56:49 +0200547 if (f_exp != f_size) {
548 /* features do not match the needed arguments for the logical operations */
549 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
550 "Invalid value \"%s\" of if-feature - number of features in expression does not match "
551 "the required number of operands for the operations.", *value);
552 return LY_EVALID;
553 }
Radek Krejci19a96102018-11-15 13:38:09 +0100554
555 if (checkversion || expr_size > 1) {
556 /* check that we have 1.1 module */
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100557 if (ctx->mod_def->version != LYS_VERSION_1_1) {
Radek Krejci19a96102018-11-15 13:38:09 +0100558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
559 "Invalid value \"%s\" of if-feature - YANG 1.1 expression in YANG 1.0 module.", *value);
560 return LY_EVALID;
561 }
562 }
563
564 /* allocate the memory */
565 LY_ARRAY_CREATE_RET(ctx->ctx, iff->features, f_size, LY_EMEM);
566 iff->expr = calloc((j = (expr_size / 4) + ((expr_size % 4) ? 1 : 0)), sizeof *iff->expr);
567 stack.stack = malloc(expr_size * sizeof *stack.stack);
568 LY_CHECK_ERR_GOTO(!stack.stack || !iff->expr, LOGMEM(ctx->ctx), error);
569
570 stack.size = expr_size;
571 f_size--; expr_size--; /* used as indexes from now */
572
573 for (i--; i >= 0; i--) {
574 if (c[i] == ')') {
575 /* push it on stack */
576 iff_stack_push(&stack, LYS_IFF_RP);
577 continue;
578 } else if (c[i] == '(') {
579 /* pop from the stack into result all operators until ) */
580 while((op = iff_stack_pop(&stack)) != LYS_IFF_RP) {
581 iff_setop(iff->expr, op, expr_size--);
582 }
583 continue;
584 } else if (isspace(c[i])) {
585 continue;
586 }
587
588 /* end of operator or operand -> find beginning and get what is it */
589 j = i + 1;
590 while (i >= 0 && !isspace(c[i]) && c[i] != '(') {
591 i--;
592 }
593 i++; /* go back by one step */
594
595 if (!strncmp(&c[i], "not", 3) && isspace(c[i + 3])) {
596 if (stack.index && stack.stack[stack.index - 1] == LYS_IFF_NOT) {
597 /* double not */
598 iff_stack_pop(&stack);
599 } else {
600 /* not has the highest priority, so do not pop from the stack
601 * as in case of AND and OR */
602 iff_stack_push(&stack, LYS_IFF_NOT);
603 }
604 } else if (!strncmp(&c[i], "and", 3) && isspace(c[i + 3])) {
605 /* as for OR - pop from the stack all operators with the same or higher
606 * priority and store them to the result, then push the AND to the stack */
607 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_AND) {
608 op = iff_stack_pop(&stack);
609 iff_setop(iff->expr, op, expr_size--);
610 }
611 iff_stack_push(&stack, LYS_IFF_AND);
612 } else if (!strncmp(&c[i], "or", 2) && isspace(c[i + 2])) {
613 while (stack.index && stack.stack[stack.index - 1] <= LYS_IFF_OR) {
614 op = iff_stack_pop(&stack);
615 iff_setop(iff->expr, op, expr_size--);
616 }
617 iff_stack_push(&stack, LYS_IFF_OR);
618 } else {
619 /* feature name, length is j - i */
620
621 /* add it to the expression */
622 iff_setop(iff->expr, LYS_IFF_F, expr_size--);
623
624 /* now get the link to the feature definition */
Radek Krejci0af46292019-01-11 16:02:31 +0100625 f = lys_feature_find(ctx->mod_def, &c[i], j - i);
626 LY_CHECK_ERR_GOTO(!f, LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
627 "Invalid value \"%s\" of if-feature - unable to find feature \"%.*s\".", *value, j - i, &c[i]);
628 rc = LY_EVALID, error)
Radek Krejci19a96102018-11-15 13:38:09 +0100629 iff->features[f_size] = f;
630 LY_ARRAY_INCREMENT(iff->features);
631 f_size--;
632 }
633 }
634 while (stack.index) {
635 op = iff_stack_pop(&stack);
636 iff_setop(iff->expr, op, expr_size--);
637 }
638
639 if (++expr_size || ++f_size) {
640 /* not all expected operators and operands found */
641 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
642 "Invalid value \"%s\" of if-feature - processing error.", *value);
643 rc = LY_EINT;
644 } else {
645 rc = LY_SUCCESS;
646 }
647
648error:
649 /* cleanup */
650 iff_stack_clean(&stack);
651
652 return rc;
653}
654
Radek Krejcib56c7502019-02-13 14:19:54 +0100655/**
656 * @brief Compile information from the when statement
657 * @param[in] ctx Compile context.
658 * @param[in] when_p The parsed when statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100659 * @param[out] when Pointer where to store pointer to the created compiled when structure.
660 * @return LY_ERR value.
661 */
Radek Krejci19a96102018-11-15 13:38:09 +0100662static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200663lys_compile_when(struct lysc_ctx *ctx, struct lysp_when *when_p, struct lysc_when **when)
Radek Krejci19a96102018-11-15 13:38:09 +0100664{
665 unsigned int u;
666 LY_ERR ret = LY_SUCCESS;
667
Radek Krejci00b874b2019-02-12 10:54:50 +0100668 *when = calloc(1, sizeof **when);
669 (*when)->refcount = 1;
670 (*when)->cond = lyxp_expr_parse(ctx->ctx, when_p->cond);
671 DUP_STRING(ctx->ctx, when_p->dsc, (*when)->dsc);
672 DUP_STRING(ctx->ctx, when_p->ref, (*when)->ref);
673 LY_CHECK_ERR_GOTO(!(*when)->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejciec4da802019-05-02 13:02:41 +0200674 COMPILE_ARRAY_GOTO(ctx, when_p->exts, (*when)->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100675
676done:
677 return ret;
678}
679
Radek Krejcib56c7502019-02-13 14:19:54 +0100680/**
681 * @brief Compile information from the must statement
682 * @param[in] ctx Compile context.
683 * @param[in] must_p The parsed must statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100684 * @param[in,out] must Prepared (empty) compiled must structure to fill.
685 * @return LY_ERR value.
686 */
Radek Krejci19a96102018-11-15 13:38:09 +0100687static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200688lys_compile_must(struct lysc_ctx *ctx, struct lysp_restr *must_p, struct lysc_must *must)
Radek Krejci19a96102018-11-15 13:38:09 +0100689{
690 unsigned int u;
691 LY_ERR ret = LY_SUCCESS;
692
693 must->cond = lyxp_expr_parse(ctx->ctx, must_p->arg);
694 LY_CHECK_ERR_GOTO(!must->cond, ret = ly_errcode(ctx->ctx), done);
Radek Krejci462cf252019-07-24 09:49:08 +0200695 must->module = ctx->mod_def;
Radek Krejci19a96102018-11-15 13:38:09 +0100696 DUP_STRING(ctx->ctx, must_p->eapptag, must->eapptag);
697 DUP_STRING(ctx->ctx, must_p->emsg, must->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +0100698 DUP_STRING(ctx->ctx, must_p->dsc, must->dsc);
699 DUP_STRING(ctx->ctx, must_p->ref, must->ref);
Radek Krejciec4da802019-05-02 13:02:41 +0200700 COMPILE_ARRAY_GOTO(ctx, must_p->exts, must->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100701
702done:
703 return ret;
704}
705
Radek Krejcib56c7502019-02-13 14:19:54 +0100706/**
707 * @brief Compile information from the import statement
708 * @param[in] ctx Compile context.
709 * @param[in] imp_p The parsed import statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100710 * @param[in,out] imp Prepared (empty) compiled import structure to fill.
711 * @return LY_ERR value.
712 */
Radek Krejci19a96102018-11-15 13:38:09 +0100713static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200714lys_compile_import(struct lysc_ctx *ctx, struct lysp_import *imp_p, struct lysc_import *imp)
Radek Krejci19a96102018-11-15 13:38:09 +0100715{
716 unsigned int u;
717 struct lys_module *mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +0100718 LY_ERR ret = LY_SUCCESS;
719
720 DUP_STRING(ctx->ctx, imp_p->prefix, imp->prefix);
Radek Krejciec4da802019-05-02 13:02:41 +0200721 COMPILE_ARRAY_GOTO(ctx, imp_p->exts, imp->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100722 imp->module = imp_p->module;
723
Radek Krejci7f2a5362018-11-28 13:05:37 +0100724 /* make sure that we have the parsed version (lysp_) of the imported module to import groupings or typedefs.
725 * The compiled version is needed only for augments, deviates and leafrefs, so they are checked (and added,
Radek Krejci0e5d8382018-11-28 16:37:53 +0100726 * if needed) when these nodes are finally being instantiated and validated at the end of schema compilation. */
Radek Krejci19a96102018-11-15 13:38:09 +0100727 if (!imp->module->parsed) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100728 /* try to use filepath if present */
729 if (imp->module->filepath) {
730 mod = (struct lys_module*)lys_parse_path(ctx->ctx, imp->module->filepath,
731 !strcmp(&imp->module->filepath[strlen(imp->module->filepath - 4)], ".yin") ? LYS_IN_YIN : LYS_IN_YANG);
Radek Krejci19a96102018-11-15 13:38:09 +0100732 if (mod != imp->module) {
733 LOGERR(ctx->ctx, LY_EINT, "Filepath \"%s\" of the module \"%s\" does not match.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100734 imp->module->filepath, imp->module->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100735 mod = NULL;
736 }
737 }
738 if (!mod) {
Radek Krejci0af46292019-01-11 16:02:31 +0100739 if (lysp_load_module(ctx->ctx, imp->module->name, imp->module->revision, 0, 1, &mod)) {
Radek Krejci19a96102018-11-15 13:38:09 +0100740 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 +0100741 imp->module->name, ctx->mod->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100742 return LY_ENOTFOUND;
743 }
744 }
Radek Krejci19a96102018-11-15 13:38:09 +0100745 }
746
747done:
748 return ret;
749}
750
Radek Krejcib56c7502019-02-13 14:19:54 +0100751/**
752 * @brief Compile information from the identity statement
753 *
754 * The backlinks to the identities derived from this one are supposed to be filled later via lys_compile_identity_bases().
755 *
756 * @param[in] ctx Compile context.
757 * @param[in] ident_p The parsed identity statement structure.
Radek Krejcib56c7502019-02-13 14:19:54 +0100758 * @param[in] idents List of so far compiled identities to check the name uniqueness.
759 * @param[in,out] ident Prepared (empty) compiled identity structure to fill.
760 * @return LY_ERR value.
761 */
Radek Krejci19a96102018-11-15 13:38:09 +0100762static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +0200763lys_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 +0100764{
765 unsigned int u;
766 LY_ERR ret = LY_SUCCESS;
767
Radek Krejci327de162019-06-14 12:52:07 +0200768 lysc_update_path(ctx, NULL, ident_p->name);
769
Radek Krejcid05cbd92018-12-05 14:26:40 +0100770 COMPILE_CHECK_UNIQUENESS(ctx, idents, name, ident, "identity", ident_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +0100771 DUP_STRING(ctx->ctx, ident_p->name, ident->name);
Radek Krejcid3ca0632019-04-16 16:54:54 +0200772 DUP_STRING(ctx->ctx, ident_p->dsc, ident->dsc);
773 DUP_STRING(ctx->ctx, ident_p->ref, ident->ref);
Radek Krejci693262f2019-04-29 15:23:20 +0200774 ident->module = ctx->mod;
Radek Krejciec4da802019-05-02 13:02:41 +0200775 COMPILE_ARRAY_GOTO(ctx, ident_p->iffeatures, ident->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100776 /* 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 +0200777 COMPILE_ARRAY_GOTO(ctx, ident_p->exts, ident->exts, u, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +0100778 ident->flags = ident_p->flags;
779
Radek Krejci327de162019-06-14 12:52:07 +0200780 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100781done:
782 return ret;
783}
784
Radek Krejcib56c7502019-02-13 14:19:54 +0100785/**
786 * @brief Check circular dependency of identities - identity MUST NOT reference itself (via their base statement).
787 *
788 * The function works in the same way as lys_compile_feature_circular_check() with different structures and error messages.
789 *
790 * @param[in] ctx Compile context for logging.
791 * @param[in] ident The base identity (its derived list is being extended by the identity being currently processed).
792 * @param[in] derived The list of derived identities of the identity being currently processed (not the one provided as @p ident)
793 * @return LY_SUCCESS if everything is ok.
794 * @return LY_EVALID if the identity is derived from itself.
795 */
Radek Krejci38222632019-02-12 16:55:05 +0100796static LY_ERR
797lys_compile_identity_circular_check(struct lysc_ctx *ctx, struct lysc_ident *ident, struct lysc_ident **derived)
798{
799 LY_ERR ret = LY_EVALID;
800 unsigned int u, v;
801 struct ly_set recursion = {0};
802 struct lysc_ident *drv;
803
804 if (!derived) {
805 return LY_SUCCESS;
806 }
807
808 for (u = 0; u < LY_ARRAY_SIZE(derived); ++u) {
809 if (ident == derived[u]) {
810 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
811 "Identity \"%s\" is indirectly derived from itself.", ident->name);
812 goto cleanup;
813 }
814 ly_set_add(&recursion, derived[u], 0);
815 }
816
817 for (v = 0; v < recursion.count; ++v) {
818 drv = recursion.objs[v];
819 if (!drv->derived) {
820 continue;
821 }
822 for (u = 0; u < LY_ARRAY_SIZE(drv->derived); ++u) {
823 if (ident == drv->derived[u]) {
824 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
825 "Identity \"%s\" is indirectly derived from itself.", ident->name);
826 goto cleanup;
827 }
828 ly_set_add(&recursion, drv->derived[u], 0);
829 }
830 }
831 ret = LY_SUCCESS;
832
833cleanup:
834 ly_set_erase(&recursion, NULL);
835 return ret;
836}
837
Radek Krejcia3045382018-11-22 14:30:31 +0100838/**
839 * @brief Find and process the referenced base identities from another identity or identityref
840 *
Radek Krejciaca74032019-06-04 08:53:06 +0200841 * For bases in identity set backlinks to them from the base identities. For identityref, store
Radek Krejcia3045382018-11-22 14:30:31 +0100842 * the array of pointers to the base identities. So one of the ident or bases parameter must be set
843 * to distinguish these two use cases.
844 *
845 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
846 * @param[in] bases_p Array of names (including prefix if necessary) of base identities.
847 * @param[in] ident Referencing identity to work with.
848 * @param[in] bases Array of bases of identityref to fill in.
849 * @return LY_ERR value.
850 */
Radek Krejci19a96102018-11-15 13:38:09 +0100851static LY_ERR
Radek Krejci555cb5b2018-11-16 14:54:33 +0100852lys_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 +0100853{
Radek Krejci555cb5b2018-11-16 14:54:33 +0100854 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +0100855 const char *s, *name;
Radek Krejcie86bf772018-12-14 11:39:53 +0100856 struct lys_module *mod;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100857 struct lysc_ident **idref;
858
859 assert(ident || bases);
860
Radek Krejci0bcdaed2019-01-10 10:21:34 +0100861 if (LY_ARRAY_SIZE(bases_p) > 1 && ctx->mod_def->version < 2) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100862 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
863 "Multiple bases in %s are allowed only in YANG 1.1 modules.", ident ? "identity" : "identityref type");
864 return LY_EVALID;
865 }
866
867 for (u = 0; u < LY_ARRAY_SIZE(bases_p); ++u) {
868 s = strchr(bases_p[u], ':');
869 if (s) {
870 /* prefixed identity */
871 name = &s[1];
Radek Krejcie86bf772018-12-14 11:39:53 +0100872 mod = lys_module_find_prefix(ctx->mod_def, bases_p[u], s - bases_p[u]);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100873 } else {
874 name = bases_p[u];
Radek Krejcie86bf772018-12-14 11:39:53 +0100875 mod = ctx->mod_def;
Radek Krejci555cb5b2018-11-16 14:54:33 +0100876 }
877 if (!mod) {
878 if (ident) {
879 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
880 "Invalid prefix used for base (%s) of identity \"%s\".", bases_p[u], ident->name);
881 } else {
882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
883 "Invalid prefix used for base (%s) of identityref.", bases_p[u]);
884 }
885 return LY_EVALID;
886 }
887 idref = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +0100888 if (mod->compiled && mod->compiled->identities) {
889 for (v = 0; v < LY_ARRAY_SIZE(mod->compiled->identities); ++v) {
890 if (!strcmp(name, mod->compiled->identities[v].name)) {
Radek Krejci555cb5b2018-11-16 14:54:33 +0100891 if (ident) {
Radek Krejci38222632019-02-12 16:55:05 +0100892 if (ident == &mod->compiled->identities[v]) {
893 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
894 "Identity \"%s\" is derived from itself.", ident->name);
895 return LY_EVALID;
896 }
897 LY_CHECK_RET(lys_compile_identity_circular_check(ctx, &mod->compiled->identities[v], ident->derived));
Radek Krejci555cb5b2018-11-16 14:54:33 +0100898 /* we have match! store the backlink */
Radek Krejcie86bf772018-12-14 11:39:53 +0100899 LY_ARRAY_NEW_RET(ctx->ctx, mod->compiled->identities[v].derived, idref, LY_EMEM);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100900 *idref = ident;
901 } else {
902 /* we have match! store the found identity */
903 LY_ARRAY_NEW_RET(ctx->ctx, *bases, idref, LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +0100904 *idref = &mod->compiled->identities[v];
Radek Krejci555cb5b2018-11-16 14:54:33 +0100905 }
906 break;
907 }
908 }
909 }
910 if (!idref || !(*idref)) {
911 if (ident) {
912 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
913 "Unable to find base (%s) of identity \"%s\".", bases_p[u], ident->name);
914 } else {
915 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
916 "Unable to find base (%s) of identityref.", bases_p[u]);
917 }
918 return LY_EVALID;
919 }
920 }
921 return LY_SUCCESS;
922}
923
Radek Krejcia3045382018-11-22 14:30:31 +0100924/**
925 * @brief For the given array of identities, set the backlinks from all their base identities.
926 * @param[in] ctx Compile context, not only for logging but also to get the current module to resolve prefixes.
927 * @param[in] idents_p Array of identities definitions from the parsed schema structure.
928 * @param[in] idents Array of referencing identities to which the backlinks are supposed to be set.
929 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
930 */
Radek Krejci555cb5b2018-11-16 14:54:33 +0100931static LY_ERR
932lys_compile_identities_derived(struct lysc_ctx *ctx, struct lysp_ident *idents_p, struct lysc_ident *idents)
933{
934 unsigned int i;
Radek Krejci19a96102018-11-15 13:38:09 +0100935
936 for (i = 0; i < LY_ARRAY_SIZE(idents_p); ++i) {
937 if (!idents_p[i].bases) {
938 continue;
939 }
Radek Krejci327de162019-06-14 12:52:07 +0200940 lysc_update_path(ctx, NULL, idents[i].name);
Radek Krejci555cb5b2018-11-16 14:54:33 +0100941 LY_CHECK_RET(lys_compile_identity_bases(ctx, idents_p[i].bases, &idents[i], NULL));
Radek Krejci327de162019-06-14 12:52:07 +0200942 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +0100943 }
944 return LY_SUCCESS;
945}
946
Radek Krejci0af46292019-01-11 16:02:31 +0100947LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +0200948lys_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 +0100949{
950 unsigned int offset = 0, u;
951 struct lysc_ctx context = {0};
952
Radek Krejci327de162019-06-14 12:52:07 +0200953 assert(ctx_sc || ctx);
954
955 if (!ctx_sc) {
956 context.ctx = ctx;
957 context.mod = module;
958 context.path_len = 1;
959 context.path[0] = '/';
960 ctx_sc = &context;
961 }
Radek Krejci0af46292019-01-11 16:02:31 +0100962
963 if (!features_p) {
964 return LY_SUCCESS;
965 }
966 if (*features) {
967 offset = LY_ARRAY_SIZE(*features);
968 }
969
Radek Krejci327de162019-06-14 12:52:07 +0200970 lysc_update_path(ctx_sc, NULL, "{feature}");
971 LY_ARRAY_CREATE_RET(ctx_sc->ctx, *features, LY_ARRAY_SIZE(features_p), LY_EMEM);
Radek Krejci0af46292019-01-11 16:02:31 +0100972 LY_ARRAY_FOR(features_p, u) {
Radek Krejci327de162019-06-14 12:52:07 +0200973 lysc_update_path(ctx_sc, NULL, features_p[u].name);
974
Radek Krejci0af46292019-01-11 16:02:31 +0100975 LY_ARRAY_INCREMENT(*features);
Radek Krejci327de162019-06-14 12:52:07 +0200976 COMPILE_CHECK_UNIQUENESS(ctx_sc, *features, name, &(*features)[offset + u], "feature", features_p[u].name);
977 DUP_STRING(ctx_sc->ctx, features_p[u].name, (*features)[offset + u].name);
978 DUP_STRING(ctx_sc->ctx, features_p[u].dsc, (*features)[offset + u].dsc);
979 DUP_STRING(ctx_sc->ctx, features_p[u].ref, (*features)[offset + u].ref);
Radek Krejci0af46292019-01-11 16:02:31 +0100980 (*features)[offset + u].flags = features_p[u].flags;
Radek Krejci327de162019-06-14 12:52:07 +0200981 (*features)[offset + u].module = ctx_sc->mod;
982
983 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +0100984 }
Radek Krejci327de162019-06-14 12:52:07 +0200985 lysc_update_path(ctx_sc, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +0100986
987 return LY_SUCCESS;
988}
989
Radek Krejcia3045382018-11-22 14:30:31 +0100990/**
Radek Krejci09a1fc52019-02-13 10:55:17 +0100991 * @brief Check circular dependency of features - feature MUST NOT reference itself (via their if-feature statement).
Radek Krejcib56c7502019-02-13 14:19:54 +0100992 *
993 * The function works in the same way as lys_compile_identity_circular_check() with different structures and error messages.
994 *
Radek Krejci09a1fc52019-02-13 10:55:17 +0100995 * @param[in] ctx Compile context for logging.
Radek Krejcib56c7502019-02-13 14:19:54 +0100996 * @param[in] feature The feature referenced in if-feature statement (its depfeatures list is being extended by the feature
997 * being currently processed).
998 * @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 +0100999 * @return LY_SUCCESS if everything is ok.
1000 * @return LY_EVALID if the feature references indirectly itself.
1001 */
1002static LY_ERR
1003lys_compile_feature_circular_check(struct lysc_ctx *ctx, struct lysc_feature *feature, struct lysc_feature **depfeatures)
1004{
1005 LY_ERR ret = LY_EVALID;
1006 unsigned int u, v;
1007 struct ly_set recursion = {0};
1008 struct lysc_feature *drv;
1009
1010 if (!depfeatures) {
1011 return LY_SUCCESS;
1012 }
1013
1014 for (u = 0; u < LY_ARRAY_SIZE(depfeatures); ++u) {
1015 if (feature == depfeatures[u]) {
1016 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1017 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1018 goto cleanup;
1019 }
1020 ly_set_add(&recursion, depfeatures[u], 0);
1021 }
1022
1023 for (v = 0; v < recursion.count; ++v) {
1024 drv = recursion.objs[v];
1025 if (!drv->depfeatures) {
1026 continue;
1027 }
1028 for (u = 0; u < LY_ARRAY_SIZE(drv->depfeatures); ++u) {
1029 if (feature == drv->depfeatures[u]) {
1030 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1031 "Feature \"%s\" is indirectly referenced from itself.", feature->name);
1032 goto cleanup;
1033 }
1034 ly_set_add(&recursion, drv->depfeatures[u], 0);
1035 }
1036 }
1037 ret = LY_SUCCESS;
1038
1039cleanup:
1040 ly_set_erase(&recursion, NULL);
1041 return ret;
1042}
1043
1044/**
Radek Krejci0af46292019-01-11 16:02:31 +01001045 * @brief Create pre-compiled features array.
1046 *
1047 * See lys_feature_precompile() for more details.
1048 *
Radek Krejcia3045382018-11-22 14:30:31 +01001049 * @param[in] ctx Compile context.
1050 * @param[in] feature_p Parsed feature definition to compile.
Radek Krejci0af46292019-01-11 16:02:31 +01001051 * @param[in,out] features List of already (pre)compiled features to find the corresponding precompiled feature structure.
Radek Krejcia3045382018-11-22 14:30:31 +01001052 * @return LY_ERR value.
1053 */
Radek Krejci19a96102018-11-15 13:38:09 +01001054static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001055lys_feature_precompile_finish(struct lysc_ctx *ctx, struct lysp_feature *feature_p, struct lysc_feature *features)
Radek Krejci19a96102018-11-15 13:38:09 +01001056{
Radek Krejci0af46292019-01-11 16:02:31 +01001057 unsigned int u, v, x;
1058 struct lysc_feature *feature, **df;
Radek Krejci19a96102018-11-15 13:38:09 +01001059 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01001060
Radek Krejci327de162019-06-14 12:52:07 +02001061
Radek Krejci0af46292019-01-11 16:02:31 +01001062 /* find the preprecompiled feature */
1063 LY_ARRAY_FOR(features, x) {
1064 if (strcmp(features[x].name, feature_p->name)) {
1065 continue;
1066 }
1067 feature = &features[x];
Radek Krejci327de162019-06-14 12:52:07 +02001068 lysc_update_path(ctx, NULL, "{feature}");
1069 lysc_update_path(ctx, NULL, feature_p->name);
Radek Krejci19a96102018-11-15 13:38:09 +01001070
Radek Krejci0af46292019-01-11 16:02:31 +01001071 /* finish compilation started in lys_feature_precompile() */
Radek Krejciec4da802019-05-02 13:02:41 +02001072 COMPILE_ARRAY_GOTO(ctx, feature_p->exts, feature->exts, u, lys_compile_ext, ret, done);
1073 COMPILE_ARRAY_GOTO(ctx, feature_p->iffeatures, feature->iffeatures, u, lys_compile_iffeature, ret, done);
Radek Krejci0af46292019-01-11 16:02:31 +01001074 if (feature->iffeatures) {
1075 for (u = 0; u < LY_ARRAY_SIZE(feature->iffeatures); ++u) {
1076 if (feature->iffeatures[u].features) {
1077 for (v = 0; v < LY_ARRAY_SIZE(feature->iffeatures[u].features); ++v) {
Radek Krejci09a1fc52019-02-13 10:55:17 +01001078 /* check for circular dependency - direct reference first,... */
1079 if (feature == feature->iffeatures[u].features[v]) {
1080 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
1081 "Feature \"%s\" is referenced from itself.", feature->name);
1082 return LY_EVALID;
1083 }
1084 /* ... and indirect circular reference */
1085 LY_CHECK_RET(lys_compile_feature_circular_check(ctx, feature->iffeatures[u].features[v], feature->depfeatures));
1086
Radek Krejci0af46292019-01-11 16:02:31 +01001087 /* add itself into the dependants list */
1088 LY_ARRAY_NEW_RET(ctx->ctx, feature->iffeatures[u].features[v]->depfeatures, df, LY_EMEM);
1089 *df = feature;
1090 }
Radek Krejci19a96102018-11-15 13:38:09 +01001091 }
Radek Krejci19a96102018-11-15 13:38:09 +01001092 }
1093 }
Radek Krejci327de162019-06-14 12:52:07 +02001094 lysc_update_path(ctx, NULL, NULL);
1095 lysc_update_path(ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01001096 done:
1097 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01001098 }
Radek Krejci0af46292019-01-11 16:02:31 +01001099
1100 LOGINT(ctx->ctx);
1101 return LY_EINT;
Radek Krejci19a96102018-11-15 13:38:09 +01001102}
1103
Radek Krejcib56c7502019-02-13 14:19:54 +01001104/**
1105 * @brief Revert compiled list of features back to the precompiled state.
1106 *
1107 * Function is needed in case the compilation failed and the schema is expected to revert back to the non-compiled status.
1108 * The features are supposed to be stored again as off_features in ::lys_module structure.
1109 *
1110 * @param[in] ctx Compilation context.
1111 * @param[in] mod The module structure still holding the compiled (but possibly not finished, only the list of compiled features is taken) schema
1112 * and supposed to hold the off_features list.
1113 */
Radek Krejci95710c92019-02-11 15:49:55 +01001114static void
1115lys_feature_precompile_revert(struct lysc_ctx *ctx, struct lys_module *mod)
1116{
1117 unsigned int u, v;
1118
1119 /* keep the off_features list until the complete lys_module is freed */
1120 mod->off_features = mod->compiled->features;
1121 mod->compiled->features = NULL;
1122
1123 /* in the off_features list, remove all the parts (from finished compiling process)
1124 * which may points into the data being freed here */
1125 LY_ARRAY_FOR(mod->off_features, u) {
1126 LY_ARRAY_FOR(mod->off_features[u].iffeatures, v) {
1127 lysc_iffeature_free(ctx->ctx, &mod->off_features[u].iffeatures[v]);
1128 }
1129 LY_ARRAY_FREE(mod->off_features[u].iffeatures);
1130 mod->off_features[u].iffeatures = NULL;
1131
1132 LY_ARRAY_FOR(mod->off_features[u].exts, v) {
1133 lysc_ext_instance_free(ctx->ctx, &(mod->off_features[u].exts)[v]);
1134 }
1135 LY_ARRAY_FREE(mod->off_features[u].exts);
1136 mod->off_features[u].exts = NULL;
1137 }
1138}
1139
Radek Krejcia3045382018-11-22 14:30:31 +01001140/**
1141 * @brief Validate and normalize numeric value from a range definition.
1142 * @param[in] ctx Compile context.
1143 * @param[in] basetype Base YANG built-in type of the node connected with the range restriction. Actually only LY_TYPE_DEC64 is important to
1144 * allow processing of the fractions. The fraction point is extracted from the value which is then normalize according to given frdigits into
1145 * valcopy to allow easy parsing and storing of the value. libyang stores decimal number without the decimal point which is always recovered from
1146 * the known fraction-digits value. So, with fraction-digits 2, number 3.14 is stored as 314 and number 1 is stored as 100.
1147 * @param[in] frdigits The fraction-digits of the type in case of LY_TYPE_DEC64.
1148 * @param[in] value String value of the range boundary.
1149 * @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.
1150 * @param[out] valcopy NULL-terminated string with the numeric value to parse and store.
1151 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID (no number) or LY_EINVAL (decimal64 not matching fraction-digits value).
1152 */
Radek Krejcie88beef2019-05-30 15:47:19 +02001153LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001154range_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 +01001155{
Radek Krejci6cba4292018-11-15 17:33:29 +01001156 size_t fraction = 0, size;
1157
Radek Krejci19a96102018-11-15 13:38:09 +01001158 *len = 0;
1159
1160 assert(value);
1161 /* parse value */
1162 if (!isdigit(value[*len]) && (value[*len] != '-') && (value[*len] != '+')) {
1163 return LY_EVALID;
1164 }
1165
1166 if ((value[*len] == '-') || (value[*len] == '+')) {
1167 ++(*len);
1168 }
1169
1170 while (isdigit(value[*len])) {
1171 ++(*len);
1172 }
1173
1174 if ((basetype != LY_TYPE_DEC64) || (value[*len] != '.') || !isdigit(value[*len + 1])) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001175 if (basetype == LY_TYPE_DEC64) {
1176 goto decimal;
1177 } else {
1178 *valcopy = strndup(value, *len);
1179 return LY_SUCCESS;
1180 }
Radek Krejci19a96102018-11-15 13:38:09 +01001181 }
1182 fraction = *len;
1183
1184 ++(*len);
1185 while (isdigit(value[*len])) {
1186 ++(*len);
1187 }
1188
Radek Krejci6cba4292018-11-15 17:33:29 +01001189 if (basetype == LY_TYPE_DEC64) {
1190decimal:
1191 assert(frdigits);
Radek Krejci943177f2019-06-14 16:32:43 +02001192 if (fraction && (*len - 1 - fraction > frdigits)) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001193 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1194 "Range boundary \"%.*s\" of decimal64 type exceeds defined number (%u) of fraction digits.",
1195 *len, value, frdigits);
1196 return LY_EINVAL;
1197 }
1198 if (fraction) {
1199 size = (*len) + (frdigits - ((*len) - 1 - fraction));
1200 } else {
1201 size = (*len) + frdigits + 1;
1202 }
1203 *valcopy = malloc(size * sizeof **valcopy);
Radek Krejci19a96102018-11-15 13:38:09 +01001204 LY_CHECK_ERR_RET(!(*valcopy), LOGMEM(ctx->ctx), LY_EMEM);
1205
Radek Krejci6cba4292018-11-15 17:33:29 +01001206 (*valcopy)[size - 1] = '\0';
1207 if (fraction) {
1208 memcpy(&(*valcopy)[0], &value[0], fraction);
1209 memcpy(&(*valcopy)[fraction], &value[fraction + 1], (*len) - 1 - (fraction));
1210 memset(&(*valcopy)[(*len) - 1], '0', frdigits - ((*len) - 1 - fraction));
1211 } else {
1212 memcpy(&(*valcopy)[0], &value[0], *len);
1213 memset(&(*valcopy)[*len], '0', frdigits);
1214 }
Radek Krejci19a96102018-11-15 13:38:09 +01001215 }
1216 return LY_SUCCESS;
1217}
1218
Radek Krejcia3045382018-11-22 14:30:31 +01001219/**
1220 * @brief Check that values in range are in ascendant order.
1221 * @param[in] unsigned_value Flag to note that we are working with unsigned values.
Radek Krejci5969f272018-11-23 10:03:58 +01001222 * @param[in] max Flag to distinguish if checking min or max value. min value must be strictly higher than previous,
1223 * max can be also equal.
Radek Krejcia3045382018-11-22 14:30:31 +01001224 * @param[in] value Current value to check.
1225 * @param[in] prev_value The last seen value.
1226 * @return LY_SUCCESS or LY_EEXIST for invalid order.
1227 */
Radek Krejci19a96102018-11-15 13:38:09 +01001228static LY_ERR
Radek Krejci5969f272018-11-23 10:03:58 +01001229range_part_check_ascendancy(int unsigned_value, int max, int64_t value, int64_t prev_value)
Radek Krejci19a96102018-11-15 13:38:09 +01001230{
1231 if (unsigned_value) {
Radek Krejci5969f272018-11-23 10:03:58 +01001232 if ((max && (uint64_t)prev_value > (uint64_t)value) || (!max && (uint64_t)prev_value >= (uint64_t)value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001233 return LY_EEXIST;
1234 }
1235 } else {
Radek Krejci5969f272018-11-23 10:03:58 +01001236 if ((max && prev_value > value) || (!max && prev_value >= value)) {
Radek Krejci19a96102018-11-15 13:38:09 +01001237 return LY_EEXIST;
1238 }
1239 }
1240 return LY_SUCCESS;
1241}
1242
Radek Krejcia3045382018-11-22 14:30:31 +01001243/**
1244 * @brief Set min/max value of the range part.
1245 * @param[in] ctx Compile context.
1246 * @param[in] part Range part structure to fill.
1247 * @param[in] max Flag to distinguish if storing min or max value.
1248 * @param[in] prev The last seen value to check that all values in range are specified in ascendant order.
1249 * @param[in] basetype Type of the value to get know implicit min/max values and other checking rules.
1250 * @param[in] first Flag for the first value of the range to avoid ascendancy order.
1251 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1252 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
Radek Krejci5969f272018-11-23 10:03:58 +01001253 * @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 +01001254 * @param[in,out] value Numeric range value to be stored, if not provided the type's min/max value is set.
1255 * @return LY_ERR value - LY_SUCCESS, LY_EDENIED (value brokes type's boundaries), LY_EVALID (not a number),
1256 * LY_EEXIST (value is smaller than the previous one), LY_EINVAL (decimal64 value does not corresponds with the
1257 * frdigits value), LY_EMEM.
1258 */
Radek Krejci19a96102018-11-15 13:38:09 +01001259static LY_ERR
1260range_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 +01001261 uint8_t frdigits, struct lysc_range *base_range, const char **value)
Radek Krejci19a96102018-11-15 13:38:09 +01001262{
1263 LY_ERR ret = LY_SUCCESS;
1264 char *valcopy = NULL;
1265 size_t len;
1266
1267 if (value) {
Radek Krejci6cba4292018-11-15 17:33:29 +01001268 ret = range_part_check_value_syntax(ctx, basetype, frdigits, *value, &len, &valcopy);
Radek Krejci5969f272018-11-23 10:03:58 +01001269 LY_CHECK_GOTO(ret, finalize);
1270 }
1271 if (!valcopy && base_range) {
1272 if (max) {
1273 part->max_64 = base_range->parts[LY_ARRAY_SIZE(base_range->parts) - 1].max_64;
1274 } else {
1275 part->min_64 = base_range->parts[0].min_64;
1276 }
1277 if (!first) {
1278 ret = range_part_check_ascendancy(basetype <= LY_TYPE_STRING ? 1 : 0, max, max ? part->max_64 : part->min_64, prev);
1279 }
1280 goto finalize;
Radek Krejci19a96102018-11-15 13:38:09 +01001281 }
1282
1283 switch (basetype) {
Radek Krejci19a96102018-11-15 13:38:09 +01001284 case LY_TYPE_INT8: /* range */
1285 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001286 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 +01001287 } else if (max) {
1288 part->max_64 = INT64_C(127);
1289 } else {
1290 part->min_64 = INT64_C(-128);
1291 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001292 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001293 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001294 }
1295 break;
1296 case LY_TYPE_INT16: /* range */
1297 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001298 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 +01001299 } else if (max) {
1300 part->max_64 = INT64_C(32767);
1301 } else {
1302 part->min_64 = INT64_C(-32768);
1303 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001304 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001305 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001306 }
1307 break;
1308 case LY_TYPE_INT32: /* range */
1309 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001310 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 +01001311 } else if (max) {
1312 part->max_64 = INT64_C(2147483647);
1313 } else {
1314 part->min_64 = INT64_C(-2147483648);
1315 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001316 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001317 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001318 }
1319 break;
1320 case LY_TYPE_INT64: /* range */
Radek Krejci25cfef72018-11-23 14:15:52 +01001321 case LY_TYPE_DEC64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001322 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001323 ret = ly_parse_int(valcopy, strlen(valcopy), INT64_C(-9223372036854775807) - INT64_C(1), INT64_C(9223372036854775807), 10,
Radek Krejci19a96102018-11-15 13:38:09 +01001324 max ? &part->max_64 : &part->min_64);
1325 } else if (max) {
1326 part->max_64 = INT64_C(9223372036854775807);
1327 } else {
1328 part->min_64 = INT64_C(-9223372036854775807) - INT64_C(1);
1329 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001330 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001331 ret = range_part_check_ascendancy(0, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001332 }
1333 break;
1334 case LY_TYPE_UINT8: /* range */
1335 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001336 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 +01001337 } else if (max) {
1338 part->max_u64 = UINT64_C(255);
1339 } else {
1340 part->min_u64 = UINT64_C(0);
1341 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001342 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001343 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001344 }
1345 break;
1346 case LY_TYPE_UINT16: /* range */
1347 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001348 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 +01001349 } else if (max) {
1350 part->max_u64 = UINT64_C(65535);
1351 } else {
1352 part->min_u64 = UINT64_C(0);
1353 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001354 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001355 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001356 }
1357 break;
1358 case LY_TYPE_UINT32: /* range */
1359 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001360 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 +01001361 } else if (max) {
1362 part->max_u64 = UINT64_C(4294967295);
1363 } else {
1364 part->min_u64 = UINT64_C(0);
1365 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001366 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001367 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001368 }
1369 break;
1370 case LY_TYPE_UINT64: /* range */
Radek Krejci19a96102018-11-15 13:38:09 +01001371 case LY_TYPE_STRING: /* length */
Radek Krejci25cfef72018-11-23 14:15:52 +01001372 case LY_TYPE_BINARY: /* length */
Radek Krejci19a96102018-11-15 13:38:09 +01001373 if (valcopy) {
Radek Krejci249973a2019-06-10 10:50:54 +02001374 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 +01001375 } else if (max) {
1376 part->max_u64 = UINT64_C(18446744073709551615);
1377 } else {
1378 part->min_u64 = UINT64_C(0);
1379 }
Radek Krejci6cba4292018-11-15 17:33:29 +01001380 if (!ret && !first) {
Radek Krejci5969f272018-11-23 10:03:58 +01001381 ret = range_part_check_ascendancy(1, max, max ? part->max_64 : part->min_64, prev);
Radek Krejci19a96102018-11-15 13:38:09 +01001382 }
1383 break;
1384 default:
1385 LOGINT(ctx->ctx);
1386 ret = LY_EINT;
1387 }
1388
Radek Krejci5969f272018-11-23 10:03:58 +01001389finalize:
Radek Krejci19a96102018-11-15 13:38:09 +01001390 if (ret == LY_EDENIED) {
1391 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1392 "Invalid %s restriction - value \"%s\" does not fit the type limitations.",
1393 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1394 } else if (ret == LY_EVALID) {
1395 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1396 "Invalid %s restriction - invalid value \"%s\".",
1397 length_restr ? "length" : "range", valcopy ? valcopy : *value);
1398 } else if (ret == LY_EEXIST) {
1399 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1400 "Invalid %s restriction - values are not in ascending order (%s).",
Radek Krejci6cba4292018-11-15 17:33:29 +01001401 length_restr ? "length" : "range",
Radek Krejci5969f272018-11-23 10:03:58 +01001402 (valcopy && basetype != LY_TYPE_DEC64) ? valcopy : value ? *value : max ? "max" : "min");
Radek Krejci19a96102018-11-15 13:38:09 +01001403 } else if (!ret && value) {
1404 *value = *value + len;
1405 }
1406 free(valcopy);
1407 return ret;
1408}
1409
Radek Krejcia3045382018-11-22 14:30:31 +01001410/**
1411 * @brief Compile the parsed range restriction.
1412 * @param[in] ctx Compile context.
1413 * @param[in] range_p Parsed range structure to compile.
1414 * @param[in] basetype Base YANG built-in type of the node with the range restriction.
1415 * @param[in] length_restr Flag to distinguish between range and length restrictions. Only for logging.
1416 * @param[in] frdigits The fraction-digits value in case of LY_TYPE_DEC64 basetype.
1417 * @param[in] base_range Range restriction of the type from which the current type is derived. The current
1418 * range restriction must be more restrictive than the base_range.
1419 * @param[in,out] range Pointer to the created current range structure.
1420 * @return LY_ERR value.
1421 */
Radek Krejci19a96102018-11-15 13:38:09 +01001422static LY_ERR
Radek Krejci6cba4292018-11-15 17:33:29 +01001423lys_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 +01001424 struct lysc_range *base_range, struct lysc_range **range)
1425{
1426 LY_ERR ret = LY_EVALID;
1427 const char *expr;
1428 struct lysc_range_part *parts = NULL, *part;
1429 int range_expected = 0, uns;
1430 unsigned int parts_done = 0, u, v;
1431
1432 assert(range);
1433 assert(range_p);
1434
1435 expr = range_p->arg;
1436 while(1) {
1437 if (isspace(*expr)) {
1438 ++expr;
1439 } else if (*expr == '\0') {
1440 if (range_expected) {
1441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1442 "Invalid %s restriction - unexpected end of the expression after \"..\" (%s).",
1443 length_restr ? "length" : "range", range_p->arg);
1444 goto cleanup;
1445 } else if (!parts || parts_done == LY_ARRAY_SIZE(parts)) {
1446 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1447 "Invalid %s restriction - unexpected end of the expression (%s).",
1448 length_restr ? "length" : "range", range_p->arg);
1449 goto cleanup;
1450 }
1451 parts_done++;
1452 break;
1453 } else if (!strncmp(expr, "min", 3)) {
1454 if (parts) {
1455 /* min cannot be used elsewhere than in the first part */
1456 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1457 "Invalid %s restriction - unexpected data before min keyword (%.*s).", length_restr ? "length" : "range",
1458 expr - range_p->arg, range_p->arg);
1459 goto cleanup;
1460 }
1461 expr += 3;
1462
1463 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
Radek Krejci5969f272018-11-23 10:03:58 +01001464 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 +01001465 part->max_64 = part->min_64;
1466 } else if (*expr == '|') {
1467 if (!parts || range_expected) {
1468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1469 "Invalid %s restriction - unexpected beginning of the expression (%s).", length_restr ? "length" : "range", expr);
1470 goto cleanup;
1471 }
1472 expr++;
1473 parts_done++;
1474 /* process next part of the expression */
1475 } else if (!strncmp(expr, "..", 2)) {
1476 expr += 2;
1477 while (isspace(*expr)) {
1478 expr++;
1479 }
1480 if (!parts || LY_ARRAY_SIZE(parts) == parts_done) {
1481 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1482 "Invalid %s restriction - unexpected \"..\" without a lower bound.", length_restr ? "length" : "range");
1483 goto cleanup;
1484 }
1485 /* continue expecting the upper boundary */
1486 range_expected = 1;
1487 } else if (isdigit(*expr) || (*expr == '-') || (*expr == '+')) {
1488 /* number */
1489 if (range_expected) {
1490 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001491 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 +01001492 range_expected = 0;
1493 } else {
1494 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1495 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 +01001496 basetype, parts_done ? 0 : 1, length_restr, frdigits, NULL, &expr), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001497 part->max_64 = part->min_64;
1498 }
1499
1500 /* continue with possible another expression part */
1501 } else if (!strncmp(expr, "max", 3)) {
1502 expr += 3;
1503 while (isspace(*expr)) {
1504 expr++;
1505 }
1506 if (*expr != '\0') {
1507 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data after max keyword (%s).",
1508 length_restr ? "length" : "range", expr);
1509 goto cleanup;
1510 }
1511 if (range_expected) {
1512 part = &parts[LY_ARRAY_SIZE(parts) - 1];
Radek Krejci5969f272018-11-23 10:03:58 +01001513 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 +01001514 range_expected = 0;
1515 } else {
1516 LY_ARRAY_NEW_GOTO(ctx->ctx, parts, part, ret, cleanup);
1517 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 +01001518 basetype, parts_done ? 0 : 1, length_restr, frdigits, base_range, NULL), cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01001519 part->min_64 = part->max_64;
1520 }
1521 } else {
1522 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid %s restriction - unexpected data (%s).",
1523 length_restr ? "length" : "range", expr);
1524 goto cleanup;
1525 }
1526 }
1527
1528 /* check with the previous range/length restriction */
1529 if (base_range) {
1530 switch (basetype) {
1531 case LY_TYPE_BINARY:
1532 case LY_TYPE_UINT8:
1533 case LY_TYPE_UINT16:
1534 case LY_TYPE_UINT32:
1535 case LY_TYPE_UINT64:
1536 case LY_TYPE_STRING:
1537 uns = 1;
1538 break;
1539 case LY_TYPE_DEC64:
1540 case LY_TYPE_INT8:
1541 case LY_TYPE_INT16:
1542 case LY_TYPE_INT32:
1543 case LY_TYPE_INT64:
1544 uns = 0;
1545 break;
1546 default:
1547 LOGINT(ctx->ctx);
1548 ret = LY_EINT;
1549 goto cleanup;
1550 }
1551 for (u = v = 0; u < parts_done && v < LY_ARRAY_SIZE(base_range->parts); ++u) {
1552 if ((uns && parts[u].min_u64 < base_range->parts[v].min_u64) || (!uns && parts[u].min_64 < base_range->parts[v].min_64)) {
1553 goto baseerror;
1554 }
1555 /* current lower bound is not lower than the base */
1556 if (base_range->parts[v].min_64 == base_range->parts[v].max_64) {
1557 /* base has single value */
1558 if (base_range->parts[v].min_64 == parts[u].min_64) {
1559 /* both lower bounds are the same */
1560 if (parts[u].min_64 != parts[u].max_64) {
1561 /* current continues with a range */
1562 goto baseerror;
1563 } else {
1564 /* equal single values, move both forward */
1565 ++v;
1566 continue;
1567 }
1568 } else {
1569 /* base is single value lower than current range, so the
1570 * value from base range is removed in the current,
1571 * move only base and repeat checking */
1572 ++v;
1573 --u;
1574 continue;
1575 }
1576 } else {
1577 /* base is the range */
1578 if (parts[u].min_64 == parts[u].max_64) {
1579 /* current is a single value */
1580 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1581 /* current is behind the base range, so base range is omitted,
1582 * move the base and keep the current for further check */
1583 ++v;
1584 --u;
1585 } /* else it is within the base range, so move the current, but keep the base */
1586 continue;
1587 } else {
1588 /* both are ranges - check the higher bound, the lower was already checked */
1589 if ((uns && parts[u].max_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].max_64 > base_range->parts[v].max_64)) {
1590 /* higher bound is higher than the current higher bound */
1591 if ((uns && parts[u].min_u64 > base_range->parts[v].max_u64) || (!uns && parts[u].min_64 > base_range->parts[v].max_64)) {
1592 /* but the current lower bound is also higher, so the base range is omitted,
1593 * continue with the same current, but move the base */
1594 --u;
1595 ++v;
1596 continue;
1597 }
1598 /* current range starts within the base range but end behind it */
1599 goto baseerror;
1600 } else {
1601 /* current range is smaller than the base,
1602 * move current, but stay with the base */
1603 continue;
1604 }
1605 }
1606 }
1607 }
1608 if (u != parts_done) {
1609baseerror:
1610 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1611 "Invalid %s restriction - the derived restriction (%s) is not equally or more limiting.",
1612 length_restr ? "length" : "range", range_p->arg);
1613 goto cleanup;
1614 }
1615 }
1616
1617 if (!(*range)) {
1618 *range = calloc(1, sizeof **range);
1619 LY_CHECK_ERR_RET(!(*range), LOGMEM(ctx->ctx), LY_EMEM);
1620 }
1621
Radek Krejcic8b31002019-01-08 10:24:45 +01001622 /* we rewrite the following values as the types chain is being processed */
Radek Krejci19a96102018-11-15 13:38:09 +01001623 if (range_p->eapptag) {
1624 lydict_remove(ctx->ctx, (*range)->eapptag);
1625 (*range)->eapptag = lydict_insert(ctx->ctx, range_p->eapptag, 0);
1626 }
1627 if (range_p->emsg) {
1628 lydict_remove(ctx->ctx, (*range)->emsg);
1629 (*range)->emsg = lydict_insert(ctx->ctx, range_p->emsg, 0);
1630 }
Radek Krejcic8b31002019-01-08 10:24:45 +01001631 if (range_p->dsc) {
1632 lydict_remove(ctx->ctx, (*range)->dsc);
1633 (*range)->dsc = lydict_insert(ctx->ctx, range_p->dsc, 0);
1634 }
1635 if (range_p->ref) {
1636 lydict_remove(ctx->ctx, (*range)->ref);
1637 (*range)->ref = lydict_insert(ctx->ctx, range_p->ref, 0);
1638 }
Radek Krejci19a96102018-11-15 13:38:09 +01001639 /* extensions are taken only from the last range by the caller */
1640
1641 (*range)->parts = parts;
1642 parts = NULL;
1643 ret = LY_SUCCESS;
1644cleanup:
Radek Krejci19a96102018-11-15 13:38:09 +01001645 LY_ARRAY_FREE(parts);
1646
1647 return ret;
1648}
1649
1650/**
1651 * @brief Checks pattern syntax.
1652 *
Radek Krejcia3045382018-11-22 14:30:31 +01001653 * @param[in] ctx Compile context.
Radek Krejci19a96102018-11-15 13:38:09 +01001654 * @param[in] pattern Pattern to check.
Radek Krejci54579462019-04-30 12:47:06 +02001655 * @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 +01001656 * @return LY_ERR value - LY_SUCCESS, LY_EMEM, LY_EVALID.
Radek Krejci19a96102018-11-15 13:38:09 +01001657 */
1658static LY_ERR
Radek Krejci54579462019-04-30 12:47:06 +02001659lys_compile_type_pattern_check(struct lysc_ctx *ctx, const char *pattern, pcre2_code **code)
Radek Krejci19a96102018-11-15 13:38:09 +01001660{
Radek Krejci54579462019-04-30 12:47:06 +02001661 int idx, idx2, start, end, count;
Radek Krejci19a96102018-11-15 13:38:09 +01001662 char *perl_regex, *ptr;
Radek Krejci54579462019-04-30 12:47:06 +02001663 int err_code;
1664 const char *orig_ptr;
1665 PCRE2_SIZE err_offset;
1666 pcre2_code *code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001667#define URANGE_LEN 19
1668 char *ublock2urange[][2] = {
1669 {"BasicLatin", "[\\x{0000}-\\x{007F}]"},
1670 {"Latin-1Supplement", "[\\x{0080}-\\x{00FF}]"},
1671 {"LatinExtended-A", "[\\x{0100}-\\x{017F}]"},
1672 {"LatinExtended-B", "[\\x{0180}-\\x{024F}]"},
1673 {"IPAExtensions", "[\\x{0250}-\\x{02AF}]"},
1674 {"SpacingModifierLetters", "[\\x{02B0}-\\x{02FF}]"},
1675 {"CombiningDiacriticalMarks", "[\\x{0300}-\\x{036F}]"},
1676 {"Greek", "[\\x{0370}-\\x{03FF}]"},
1677 {"Cyrillic", "[\\x{0400}-\\x{04FF}]"},
1678 {"Armenian", "[\\x{0530}-\\x{058F}]"},
1679 {"Hebrew", "[\\x{0590}-\\x{05FF}]"},
1680 {"Arabic", "[\\x{0600}-\\x{06FF}]"},
1681 {"Syriac", "[\\x{0700}-\\x{074F}]"},
1682 {"Thaana", "[\\x{0780}-\\x{07BF}]"},
1683 {"Devanagari", "[\\x{0900}-\\x{097F}]"},
1684 {"Bengali", "[\\x{0980}-\\x{09FF}]"},
1685 {"Gurmukhi", "[\\x{0A00}-\\x{0A7F}]"},
1686 {"Gujarati", "[\\x{0A80}-\\x{0AFF}]"},
1687 {"Oriya", "[\\x{0B00}-\\x{0B7F}]"},
1688 {"Tamil", "[\\x{0B80}-\\x{0BFF}]"},
1689 {"Telugu", "[\\x{0C00}-\\x{0C7F}]"},
1690 {"Kannada", "[\\x{0C80}-\\x{0CFF}]"},
1691 {"Malayalam", "[\\x{0D00}-\\x{0D7F}]"},
1692 {"Sinhala", "[\\x{0D80}-\\x{0DFF}]"},
1693 {"Thai", "[\\x{0E00}-\\x{0E7F}]"},
1694 {"Lao", "[\\x{0E80}-\\x{0EFF}]"},
1695 {"Tibetan", "[\\x{0F00}-\\x{0FFF}]"},
1696 {"Myanmar", "[\\x{1000}-\\x{109F}]"},
1697 {"Georgian", "[\\x{10A0}-\\x{10FF}]"},
1698 {"HangulJamo", "[\\x{1100}-\\x{11FF}]"},
1699 {"Ethiopic", "[\\x{1200}-\\x{137F}]"},
1700 {"Cherokee", "[\\x{13A0}-\\x{13FF}]"},
1701 {"UnifiedCanadianAboriginalSyllabics", "[\\x{1400}-\\x{167F}]"},
1702 {"Ogham", "[\\x{1680}-\\x{169F}]"},
1703 {"Runic", "[\\x{16A0}-\\x{16FF}]"},
1704 {"Khmer", "[\\x{1780}-\\x{17FF}]"},
1705 {"Mongolian", "[\\x{1800}-\\x{18AF}]"},
1706 {"LatinExtendedAdditional", "[\\x{1E00}-\\x{1EFF}]"},
1707 {"GreekExtended", "[\\x{1F00}-\\x{1FFF}]"},
1708 {"GeneralPunctuation", "[\\x{2000}-\\x{206F}]"},
1709 {"SuperscriptsandSubscripts", "[\\x{2070}-\\x{209F}]"},
1710 {"CurrencySymbols", "[\\x{20A0}-\\x{20CF}]"},
1711 {"CombiningMarksforSymbols", "[\\x{20D0}-\\x{20FF}]"},
1712 {"LetterlikeSymbols", "[\\x{2100}-\\x{214F}]"},
1713 {"NumberForms", "[\\x{2150}-\\x{218F}]"},
1714 {"Arrows", "[\\x{2190}-\\x{21FF}]"},
1715 {"MathematicalOperators", "[\\x{2200}-\\x{22FF}]"},
1716 {"MiscellaneousTechnical", "[\\x{2300}-\\x{23FF}]"},
1717 {"ControlPictures", "[\\x{2400}-\\x{243F}]"},
1718 {"OpticalCharacterRecognition", "[\\x{2440}-\\x{245F}]"},
1719 {"EnclosedAlphanumerics", "[\\x{2460}-\\x{24FF}]"},
1720 {"BoxDrawing", "[\\x{2500}-\\x{257F}]"},
1721 {"BlockElements", "[\\x{2580}-\\x{259F}]"},
1722 {"GeometricShapes", "[\\x{25A0}-\\x{25FF}]"},
1723 {"MiscellaneousSymbols", "[\\x{2600}-\\x{26FF}]"},
1724 {"Dingbats", "[\\x{2700}-\\x{27BF}]"},
1725 {"BraillePatterns", "[\\x{2800}-\\x{28FF}]"},
1726 {"CJKRadicalsSupplement", "[\\x{2E80}-\\x{2EFF}]"},
1727 {"KangxiRadicals", "[\\x{2F00}-\\x{2FDF}]"},
1728 {"IdeographicDescriptionCharacters", "[\\x{2FF0}-\\x{2FFF}]"},
1729 {"CJKSymbolsandPunctuation", "[\\x{3000}-\\x{303F}]"},
1730 {"Hiragana", "[\\x{3040}-\\x{309F}]"},
1731 {"Katakana", "[\\x{30A0}-\\x{30FF}]"},
1732 {"Bopomofo", "[\\x{3100}-\\x{312F}]"},
1733 {"HangulCompatibilityJamo", "[\\x{3130}-\\x{318F}]"},
1734 {"Kanbun", "[\\x{3190}-\\x{319F}]"},
1735 {"BopomofoExtended", "[\\x{31A0}-\\x{31BF}]"},
1736 {"EnclosedCJKLettersandMonths", "[\\x{3200}-\\x{32FF}]"},
1737 {"CJKCompatibility", "[\\x{3300}-\\x{33FF}]"},
1738 {"CJKUnifiedIdeographsExtensionA", "[\\x{3400}-\\x{4DB5}]"},
1739 {"CJKUnifiedIdeographs", "[\\x{4E00}-\\x{9FFF}]"},
1740 {"YiSyllables", "[\\x{A000}-\\x{A48F}]"},
1741 {"YiRadicals", "[\\x{A490}-\\x{A4CF}]"},
1742 {"HangulSyllables", "[\\x{AC00}-\\x{D7A3}]"},
1743 {"PrivateUse", "[\\x{E000}-\\x{F8FF}]"},
1744 {"CJKCompatibilityIdeographs", "[\\x{F900}-\\x{FAFF}]"},
1745 {"AlphabeticPresentationForms", "[\\x{FB00}-\\x{FB4F}]"},
1746 {"ArabicPresentationForms-A", "[\\x{FB50}-\\x{FDFF}]"},
1747 {"CombiningHalfMarks", "[\\x{FE20}-\\x{FE2F}]"},
1748 {"CJKCompatibilityForms", "[\\x{FE30}-\\x{FE4F}]"},
1749 {"SmallFormVariants", "[\\x{FE50}-\\x{FE6F}]"},
1750 {"ArabicPresentationForms-B", "[\\x{FE70}-\\x{FEFE}]"},
1751 {"HalfwidthandFullwidthForms", "[\\x{FF00}-\\x{FFEF}]"},
1752 {NULL, NULL}
1753 };
1754
1755 /* adjust the expression to a Perl equivalent
1756 * http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs */
1757
1758 /* we need to replace all "$" with "\$", count them now */
1759 for (count = 0, ptr = strpbrk(pattern, "^$"); ptr; ++count, ptr = strpbrk(ptr + 1, "^$"));
1760
1761 perl_regex = malloc((strlen(pattern) + 4 + count) * sizeof(char));
1762 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx), LY_EMEM);
1763 perl_regex[0] = '\0';
1764
1765 ptr = perl_regex;
1766
Radek Krejci19a96102018-11-15 13:38:09 +01001767 for (orig_ptr = pattern; orig_ptr[0]; ++orig_ptr) {
1768 if (orig_ptr[0] == '$') {
1769 ptr += sprintf(ptr, "\\$");
1770 } else if (orig_ptr[0] == '^') {
1771 ptr += sprintf(ptr, "\\^");
1772 } else {
1773 ptr[0] = orig_ptr[0];
1774 ++ptr;
1775 }
1776 }
Radek Krejci5819f7c2019-05-31 14:53:29 +02001777 ptr[0] = '\0';
1778 ++ptr;
Radek Krejci19a96102018-11-15 13:38:09 +01001779
1780 /* substitute Unicode Character Blocks with exact Character Ranges */
1781 while ((ptr = strstr(perl_regex, "\\p{Is"))) {
1782 start = ptr - perl_regex;
1783
1784 ptr = strchr(ptr, '}');
1785 if (!ptr) {
1786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1787 pattern, perl_regex + start + 2, "unterminated character property");
1788 free(perl_regex);
1789 return LY_EVALID;
1790 }
1791 end = (ptr - perl_regex) + 1;
1792
1793 /* need more space */
1794 if (end - start < URANGE_LEN) {
1795 perl_regex = ly_realloc(perl_regex, strlen(perl_regex) + (URANGE_LEN - (end - start)) + 1);
1796 LY_CHECK_ERR_RET(!perl_regex, LOGMEM(ctx->ctx); free(perl_regex), LY_EMEM);
1797 }
1798
1799 /* find our range */
1800 for (idx = 0; ublock2urange[idx][0]; ++idx) {
1801 if (!strncmp(perl_regex + start + 5, ublock2urange[idx][0], strlen(ublock2urange[idx][0]))) {
1802 break;
1803 }
1804 }
1805 if (!ublock2urange[idx][0]) {
1806 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP,
1807 pattern, perl_regex + start + 5, "unknown block name");
1808 free(perl_regex);
1809 return LY_EVALID;
1810 }
1811
1812 /* make the space in the string and replace the block (but we cannot include brackets if it was already enclosed in them) */
1813 for (idx2 = 0, count = 0; idx2 < start; ++idx2) {
1814 if ((perl_regex[idx2] == '[') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1815 ++count;
1816 }
1817 if ((perl_regex[idx2] == ']') && (!idx2 || (perl_regex[idx2 - 1] != '\\'))) {
1818 --count;
1819 }
1820 }
1821 if (count) {
1822 /* skip brackets */
1823 memmove(perl_regex + start + (URANGE_LEN - 2), perl_regex + end, strlen(perl_regex + end) + 1);
1824 memcpy(perl_regex + start, ublock2urange[idx][1] + 1, URANGE_LEN - 2);
1825 } else {
1826 memmove(perl_regex + start + URANGE_LEN, perl_regex + end, strlen(perl_regex + end) + 1);
1827 memcpy(perl_regex + start, ublock2urange[idx][1], URANGE_LEN);
1828 }
1829 }
1830
1831 /* must return 0, already checked during parsing */
Radek Krejci5819f7c2019-05-31 14:53:29 +02001832 code_local = pcre2_compile((PCRE2_SPTR)perl_regex, PCRE2_ZERO_TERMINATED,
1833 PCRE2_UTF | PCRE2_ANCHORED | PCRE2_ENDANCHORED | PCRE2_DOLLAR_ENDONLY | PCRE2_NO_AUTO_CAPTURE,
Radek Krejci54579462019-04-30 12:47:06 +02001834 &err_code, &err_offset, NULL);
1835 if (!code_local) {
1836 PCRE2_UCHAR err_msg[256] = {0};
1837 pcre2_get_error_message(err_code, err_msg, 256);
Radek Krejci19a96102018-11-15 13:38:09 +01001838 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_INREGEXP, pattern, perl_regex + err_offset, err_msg);
1839 free(perl_regex);
1840 return LY_EVALID;
1841 }
1842 free(perl_regex);
1843
Radek Krejci54579462019-04-30 12:47:06 +02001844 if (code) {
1845 *code = code_local;
Radek Krejci19a96102018-11-15 13:38:09 +01001846 } else {
Radek Krejci54579462019-04-30 12:47:06 +02001847 free(code_local);
Radek Krejci19a96102018-11-15 13:38:09 +01001848 }
1849
1850 return LY_SUCCESS;
1851
1852#undef URANGE_LEN
1853}
1854
Radek Krejcia3045382018-11-22 14:30:31 +01001855/**
1856 * @brief Compile parsed pattern restriction in conjunction with the patterns from base type.
1857 * @param[in] ctx Compile context.
1858 * @param[in] patterns_p Array of parsed patterns from the current type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01001859 * @param[in] base_patterns Compiled patterns from the type from which the current type is derived.
1860 * Patterns from the base type are inherited to have all the patterns that have to match at one place.
1861 * @param[out] patterns Pointer to the storage for the patterns of the current type.
1862 * @return LY_ERR LY_SUCCESS, LY_EMEM, LY_EVALID.
1863 */
Radek Krejci19a96102018-11-15 13:38:09 +01001864static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001865lys_compile_type_patterns(struct lysc_ctx *ctx, struct lysp_restr *patterns_p,
Radek Krejci19a96102018-11-15 13:38:09 +01001866 struct lysc_pattern **base_patterns, struct lysc_pattern ***patterns)
1867{
1868 struct lysc_pattern **pattern;
1869 unsigned int u, v;
Radek Krejci19a96102018-11-15 13:38:09 +01001870 LY_ERR ret = LY_SUCCESS;
1871
1872 /* first, copy the patterns from the base type */
1873 if (base_patterns) {
1874 *patterns = lysc_patterns_dup(ctx->ctx, base_patterns);
1875 LY_CHECK_ERR_RET(!(*patterns), LOGMEM(ctx->ctx), LY_EMEM);
1876 }
1877
1878 LY_ARRAY_FOR(patterns_p, u) {
1879 LY_ARRAY_NEW_RET(ctx->ctx, (*patterns), pattern, LY_EMEM);
1880 *pattern = calloc(1, sizeof **pattern);
1881 ++(*pattern)->refcount;
1882
Radek Krejci54579462019-04-30 12:47:06 +02001883 ret = lys_compile_type_pattern_check(ctx, &patterns_p[u].arg[1], &(*pattern)->code);
Radek Krejci19a96102018-11-15 13:38:09 +01001884 LY_CHECK_RET(ret);
Radek Krejci19a96102018-11-15 13:38:09 +01001885
1886 if (patterns_p[u].arg[0] == 0x15) {
1887 (*pattern)->inverted = 1;
1888 }
Radek Krejci54579462019-04-30 12:47:06 +02001889 DUP_STRING(ctx->ctx, &patterns_p[u].arg[1], (*pattern)->expr);
Radek Krejci19a96102018-11-15 13:38:09 +01001890 DUP_STRING(ctx->ctx, patterns_p[u].eapptag, (*pattern)->eapptag);
1891 DUP_STRING(ctx->ctx, patterns_p[u].emsg, (*pattern)->emsg);
Radek Krejcic8b31002019-01-08 10:24:45 +01001892 DUP_STRING(ctx->ctx, patterns_p[u].dsc, (*pattern)->dsc);
1893 DUP_STRING(ctx->ctx, patterns_p[u].ref, (*pattern)->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02001894 COMPILE_ARRAY_GOTO(ctx, patterns_p[u].exts, (*pattern)->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01001895 }
1896done:
1897 return ret;
1898}
1899
Radek Krejcia3045382018-11-22 14:30:31 +01001900/**
1901 * @brief map of the possible restrictions combination for the specific built-in type.
1902 */
Radek Krejci19a96102018-11-15 13:38:09 +01001903static uint16_t type_substmt_map[LY_DATA_TYPE_COUNT] = {
1904 0 /* LY_TYPE_UNKNOWN */,
1905 LYS_SET_LENGTH /* LY_TYPE_BINARY */,
Radek Krejci5969f272018-11-23 10:03:58 +01001906 LYS_SET_RANGE /* LY_TYPE_UINT8 */,
1907 LYS_SET_RANGE /* LY_TYPE_UINT16 */,
1908 LYS_SET_RANGE /* LY_TYPE_UINT32 */,
1909 LYS_SET_RANGE /* LY_TYPE_UINT64 */,
1910 LYS_SET_LENGTH | LYS_SET_PATTERN /* LY_TYPE_STRING */,
Radek Krejci19a96102018-11-15 13:38:09 +01001911 LYS_SET_BIT /* LY_TYPE_BITS */,
1912 0 /* LY_TYPE_BOOL */,
1913 LYS_SET_FRDIGITS | LYS_SET_RANGE /* LY_TYPE_DEC64 */,
1914 0 /* LY_TYPE_EMPTY */,
1915 LYS_SET_ENUM /* LY_TYPE_ENUM */,
1916 LYS_SET_BASE /* LY_TYPE_IDENT */,
1917 LYS_SET_REQINST /* LY_TYPE_INST */,
1918 LYS_SET_REQINST | LYS_SET_PATH /* LY_TYPE_LEAFREF */,
Radek Krejci19a96102018-11-15 13:38:09 +01001919 LYS_SET_TYPE /* LY_TYPE_UNION */,
1920 LYS_SET_RANGE /* LY_TYPE_INT8 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001921 LYS_SET_RANGE /* LY_TYPE_INT16 */,
Radek Krejci19a96102018-11-15 13:38:09 +01001922 LYS_SET_RANGE /* LY_TYPE_INT32 */,
Radek Krejci5969f272018-11-23 10:03:58 +01001923 LYS_SET_RANGE /* LY_TYPE_INT64 */
1924};
1925
1926/**
1927 * @brief stringification of the YANG built-in data types
1928 */
1929const char* ly_data_type2str[LY_DATA_TYPE_COUNT] = {"unknown", "binary", "8bit unsigned integer", "16bit unsigned integer",
1930 "32bit unsigned integer", "64bit unsigned integer", "string", "bits", "boolean", "decimal64", "empty", "enumeration",
1931 "identityref", "instance-identifier", "leafref", "union", "8bit integer", "16bit integer", "32bit integer", "64bit integer"
Radek Krejci19a96102018-11-15 13:38:09 +01001932};
1933
Radek Krejcia3045382018-11-22 14:30:31 +01001934/**
1935 * @brief Compile parsed type's enum structures (for enumeration and bits types).
1936 * @param[in] ctx Compile context.
1937 * @param[in] enums_p Array of the parsed enum structures to compile.
1938 * @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 +01001939 * @param[in] base_enums Array of the compiled enums information from the (latest) base type to check if the current enums are compatible.
1940 * @param[out] enums Newly created array of the compiled enums information for the current type.
1941 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
1942 */
Radek Krejci19a96102018-11-15 13:38:09 +01001943static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02001944lys_compile_type_enums(struct lysc_ctx *ctx, struct lysp_type_enum *enums_p, LY_DATA_TYPE basetype,
Radek Krejci693262f2019-04-29 15:23:20 +02001945 struct lysc_type_bitenum_item *base_enums, struct lysc_type_bitenum_item **enums)
Radek Krejci19a96102018-11-15 13:38:09 +01001946{
1947 LY_ERR ret = LY_SUCCESS;
Radek Krejci4ad42aa2019-07-23 16:55:58 +02001948 unsigned int u, v, match = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01001949 int32_t value = 0;
1950 uint32_t position = 0;
Radek Krejci693262f2019-04-29 15:23:20 +02001951 struct lysc_type_bitenum_item *e, storage;
Radek Krejci19a96102018-11-15 13:38:09 +01001952
Radek Krejci0bcdaed2019-01-10 10:21:34 +01001953 if (base_enums && ctx->mod_def->version < 2) {
Radek Krejci19a96102018-11-15 13:38:09 +01001954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "%s type can be subtyped only in YANG 1.1 modules.",
1955 basetype == LY_TYPE_ENUM ? "Enumeration" : "Bits");
1956 return LY_EVALID;
1957 }
1958
1959 LY_ARRAY_FOR(enums_p, u) {
1960 LY_ARRAY_NEW_RET(ctx->ctx, *enums, e, LY_EMEM);
1961 DUP_STRING(ctx->ctx, enums_p[u].name, e->name);
Radek Krejcic8b31002019-01-08 10:24:45 +01001962 DUP_STRING(ctx->ctx, enums_p[u].ref, e->dsc);
1963 DUP_STRING(ctx->ctx, enums_p[u].ref, e->ref);
Radek Krejci693262f2019-04-29 15:23:20 +02001964 e->flags = enums_p[u].flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01001965 if (base_enums) {
1966 /* check the enum/bit presence in the base type - the set of enums/bits in the derived type must be a subset */
1967 LY_ARRAY_FOR(base_enums, v) {
1968 if (!strcmp(e->name, base_enums[v].name)) {
1969 break;
1970 }
1971 }
1972 if (v == LY_ARRAY_SIZE(base_enums)) {
1973 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1974 "Invalid %s - derived type adds new item \"%s\".",
1975 basetype == LY_TYPE_ENUM ? "enumeration" : "bits", e->name);
1976 return LY_EVALID;
1977 }
1978 match = v;
1979 }
1980
1981 if (basetype == LY_TYPE_ENUM) {
Radek Krejci693262f2019-04-29 15:23:20 +02001982 e->flags |= LYS_ISENUM;
Radek Krejci19a96102018-11-15 13:38:09 +01001983 if (enums_p[u].flags & LYS_SET_VALUE) {
1984 e->value = (int32_t)enums_p[u].value;
1985 if (!u || e->value >= value) {
1986 value = e->value + 1;
1987 }
1988 /* check collision with other values */
1989 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
1990 if (e->value == (*enums)[v].value) {
1991 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
1992 "Invalid enumeration - value %d collide in items \"%s\" and \"%s\".",
1993 e->value, e->name, (*enums)[v].name);
1994 return LY_EVALID;
1995 }
1996 }
1997 } else if (base_enums) {
1998 /* inherit the assigned value */
1999 e->value = base_enums[match].value;
2000 if (!u || e->value >= value) {
2001 value = e->value + 1;
2002 }
2003 } else {
2004 /* assign value automatically */
2005 if (u && value == INT32_MIN) {
2006 /* counter overflow */
2007 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2008 "Invalid enumeration - it is not possible to auto-assign enum value for "
2009 "\"%s\" since the highest value is already 2147483647.", e->name);
2010 return LY_EVALID;
2011 }
2012 e->value = value++;
2013 }
2014 } else { /* LY_TYPE_BITS */
2015 if (enums_p[u].flags & LYS_SET_VALUE) {
2016 e->value = (int32_t)enums_p[u].value;
2017 if (!u || (uint32_t)e->value >= position) {
2018 position = (uint32_t)e->value + 1;
2019 }
2020 /* check collision with other values */
2021 for (v = 0; v < LY_ARRAY_SIZE(*enums) - 1; ++v) {
2022 if (e->value == (*enums)[v].value) {
2023 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2024 "Invalid bits - position %u collide in items \"%s\" and \"%s\".",
2025 (uint32_t)e->value, e->name, (*enums)[v].name);
2026 return LY_EVALID;
2027 }
2028 }
2029 } else if (base_enums) {
2030 /* inherit the assigned value */
2031 e->value = base_enums[match].value;
2032 if (!u || (uint32_t)e->value >= position) {
2033 position = (uint32_t)e->value + 1;
2034 }
2035 } else {
2036 /* assign value automatically */
2037 if (u && position == 0) {
2038 /* counter overflow */
2039 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2040 "Invalid bits - it is not possible to auto-assign bit position for "
2041 "\"%s\" since the highest value is already 4294967295.", e->name);
2042 return LY_EVALID;
2043 }
2044 e->value = position++;
2045 }
2046 }
2047
2048 if (base_enums) {
2049 /* the assigned values must not change from the derived type */
2050 if (e->value != base_enums[match].value) {
2051 if (basetype == LY_TYPE_ENUM) {
2052 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2053 "Invalid enumeration - value of the item \"%s\" has changed from %d to %d in the derived type.",
2054 e->name, base_enums[match].value, e->value);
2055 } else {
2056 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2057 "Invalid bits - position of the item \"%s\" has changed from %u to %u in the derived type.",
2058 e->name, (uint32_t)base_enums[match].value, (uint32_t)e->value);
2059 }
2060 return LY_EVALID;
2061 }
2062 }
2063
Radek Krejciec4da802019-05-02 13:02:41 +02002064 COMPILE_ARRAY_GOTO(ctx, enums_p[u].iffeatures, e->iffeatures, v, lys_compile_iffeature, ret, done);
2065 COMPILE_ARRAY_GOTO(ctx, enums_p[u].exts, e->exts, v, lys_compile_ext, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01002066
2067 if (basetype == LY_TYPE_BITS) {
2068 /* keep bits ordered by position */
2069 for (v = u; v && (*enums)[v - 1].value > e->value; --v);
2070 if (v != u) {
2071 memcpy(&storage, e, sizeof *e);
2072 memmove(&(*enums)[v + 1], &(*enums)[v], (u - v) * sizeof **enums);
2073 memcpy(&(*enums)[v], &storage, sizeof storage);
2074 }
2075 }
2076 }
2077
2078done:
2079 return ret;
2080}
2081
Radek Krejcia3045382018-11-22 14:30:31 +01002082#define MOVE_PATH_PARENT(NODE, LIMIT_COND, TERM, ERR_MSG, ...) \
2083 for ((NODE) = (NODE)->parent; \
2084 (NODE) && !((NODE)->nodetype & (LYS_CONTAINER | LYS_LIST | LYS_ACTION | LYS_NOTIF | LYS_ACTION)); \
2085 (NODE) = (NODE)->parent); \
2086 if (!(NODE) && (LIMIT_COND)) { /* we are going higher than top-level */ \
2087 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, ERR_MSG, ##__VA_ARGS__); \
2088 TERM; \
2089 }
2090
2091/**
2092 * @brief Validate the predicate(s) from the leafref path.
2093 * @param[in] ctx Compile context
2094 * @param[in, out] predicate Pointer to the predicate in the leafref path. The pointer is moved after the validated predicate(s).
2095 * Since there can be multiple adjacent predicates for lists with multiple keys, all such predicates are validated.
Radek Krejci4ce68932018-11-28 12:53:36 +01002096 * @param[in] start_node Path context node (where the path is instantiated).
Radek Krejcia3045382018-11-22 14:30:31 +01002097 * @param[in] context_node Predicate context node (where the predicate is placed).
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002098 * @param[in] path_context Schema where the path was defined to correct resolve of the prefixes.
Radek Krejcia3045382018-11-22 14:30:31 +01002099 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2100 */
2101static LY_ERR
Radek Krejci4ce68932018-11-28 12:53:36 +01002102lys_compile_leafref_predicate_validate(struct lysc_ctx *ctx, const char **predicate, const struct lysc_node *start_node,
Radek Krejcibade82a2018-12-05 10:13:48 +01002103 const struct lysc_node_list *context_node, const struct lys_module *path_context)
Radek Krejcia3045382018-11-22 14:30:31 +01002104{
2105 LY_ERR ret = LY_EVALID;
2106 const struct lys_module *mod;
2107 const struct lysc_node *src_node, *dst_node;
2108 const char *path_key_expr, *pke_start, *src, *src_prefix, *dst, *dst_prefix;
2109 size_t src_len, src_prefix_len, dst_len, dst_prefix_len;
Radek Krejcibade82a2018-12-05 10:13:48 +01002110 unsigned int dest_parent_times, c, u;
Radek Krejcia3045382018-11-22 14:30:31 +01002111 const char *start, *end, *pke_end;
2112 struct ly_set keys = {0};
2113 int i;
2114
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002115 assert(path_context);
2116
Radek Krejcia3045382018-11-22 14:30:31 +01002117 while (**predicate == '[') {
2118 start = (*predicate)++;
2119
2120 while (isspace(**predicate)) {
2121 ++(*predicate);
2122 }
Radek Krejcib4a4a272019-06-10 12:44:52 +02002123 LY_CHECK_GOTO(ly_parse_nodeid(predicate, &src_prefix, &src_prefix_len, &src, &src_len), cleanup);
Radek Krejcia3045382018-11-22 14:30:31 +01002124 while (isspace(**predicate)) {
2125 ++(*predicate);
2126 }
2127 if (**predicate != '=') {
2128 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002129 "Invalid leafref path predicate \"%.*s\" - missing \"=\" after node-identifier.",
2130 *predicate - start + 1, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002131 goto cleanup;
2132 }
2133 ++(*predicate);
2134 while (isspace(**predicate)) {
2135 ++(*predicate);
2136 }
2137
2138 if ((end = pke_end = strchr(*predicate, ']')) == NULL) {
2139 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2140 "Invalid leafref path predicate \"%s\" - missing predicate termination.", start);
2141 goto cleanup;
2142 }
2143 --pke_end;
2144 while (isspace(*pke_end)) {
2145 --pke_end;
2146 }
Radek Krejci7adf4ff2018-12-05 08:55:00 +01002147 ++pke_end;
Radek Krejcia3045382018-11-22 14:30:31 +01002148 /* localize path-key-expr */
2149 pke_start = path_key_expr = *predicate;
2150 /* move after the current predicate */
2151 *predicate = end + 1;
2152
2153 /* source (must be leaf or leaf-list) */
2154 if (src_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002155 mod = lys_module_find_prefix(path_context, src_prefix, src_prefix_len);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002156 if (!mod) {
2157 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2158 "Invalid leafref path predicate \"%.*s\" - prefix \"%.*s\" not defined in module \"%s\".",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002159 *predicate - start, start, src_prefix_len, src_prefix, path_context->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002160 goto cleanup;
2161 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002162 if (!mod->implemented) {
2163 /* make the module implemented */
2164 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2165 }
Radek Krejcia3045382018-11-22 14:30:31 +01002166 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002167 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002168 }
Radek Krejcibade82a2018-12-05 10:13:48 +01002169 src_node = NULL;
2170 if (context_node->keys) {
2171 for (u = 0; u < LY_ARRAY_SIZE(context_node->keys); ++u) {
2172 if (!strncmp(src, context_node->keys[u]->name, src_len) && context_node->keys[u]->name[src_len] == '\0') {
2173 src_node = (const struct lysc_node*)context_node->keys[u];
2174 break;
2175 }
2176 }
2177 }
Radek Krejcia3045382018-11-22 14:30:31 +01002178 if (!src_node) {
2179 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002180 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002181 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002182 goto cleanup;
2183 }
Radek Krejcia3045382018-11-22 14:30:31 +01002184
2185 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002186 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002187 i = ly_set_add(&keys, (void*)src_node, 0);
2188 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002189 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002190 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002191 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002192 *predicate - start, start, src_node->name);
2193 goto cleanup;
2194 }
2195
2196 /* destination */
2197 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002198 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002199
2200 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002201 if (strncmp(path_key_expr, "current", 7)) {
2202error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002203 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2204 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2205 *predicate - start, start);
2206 goto cleanup;
2207 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002208 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2209 if (*path_key_expr != '(') {
2210 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002211 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002212 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2213 if (*path_key_expr != ')') {
2214 goto error_current_function_invocation;
2215 }
2216 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002217
2218 if (*path_key_expr != '/') {
2219 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2220 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2221 *predicate - start, start);
2222 goto cleanup;
2223 }
2224 ++path_key_expr;
2225 while (isspace(*path_key_expr)) {
2226 ++path_key_expr;
2227 }
2228
2229 /* rel-path-keyexpr:
2230 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2231 while (!strncmp(path_key_expr, "..", 2)) {
2232 ++dest_parent_times;
2233 path_key_expr += 2;
2234 while (isspace(*path_key_expr)) {
2235 ++path_key_expr;
2236 }
2237 if (*path_key_expr != '/') {
2238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2239 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2240 *predicate - start, start);
2241 goto cleanup;
2242 }
2243 ++path_key_expr;
2244 while (isspace(*path_key_expr)) {
2245 ++path_key_expr;
2246 }
2247
2248 /* path is supposed to be evaluated in data tree, so we have to skip
2249 * all schema nodes that cannot be instantiated in data tree */
2250 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2251 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2252 *predicate - start, start);
2253 }
2254 if (!dest_parent_times) {
2255 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2256 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2257 *predicate - start, start);
2258 goto cleanup;
2259 }
2260 if (path_key_expr == pke_end) {
2261 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2262 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2263 *predicate - start, start);
2264 goto cleanup;
2265 }
2266
2267 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002268 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002269 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002270 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002271 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2272 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002273 goto cleanup;
2274 }
2275
2276 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002277 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002278 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002279 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002280 }
2281 if (!mod) {
2282 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002283 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002284 *predicate - start, start, dst_len, dst);
2285 goto cleanup;
2286 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002287 if (!mod->implemented) {
2288 /* make the module implemented */
2289 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2290 }
Radek Krejcia3045382018-11-22 14:30:31 +01002291
2292 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2293 if (!dst_node) {
2294 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002295 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002296 *predicate - start, start, path_key_expr - pke_start, pke_start);
2297 goto cleanup;
2298 }
2299 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002300 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 +01002301 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002302 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002303 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2304 goto cleanup;
2305 }
2306 }
2307
2308 ret = LY_SUCCESS;
2309cleanup:
2310 ly_set_erase(&keys, NULL);
2311 return ret;
2312}
2313
2314/**
2315 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2316 *
2317 * path-arg = absolute-path / relative-path
2318 * absolute-path = 1*("/" (node-identifier *path-predicate))
2319 * relative-path = 1*(".." "/") descendant-path
2320 *
2321 * @param[in,out] path Path to parse.
2322 * @param[out] prefix Prefix of the token, NULL if there is not any.
2323 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2324 * @param[out] name Name of the token.
2325 * @param[out] nam_len Length of the name.
2326 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2327 * must not be changed between consecutive calls. -1 if the
2328 * path is absolute.
2329 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2330 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2331 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002332LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002333lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2334 int *parent_times, int *has_predicate)
2335{
2336 int par_times = 0;
2337
2338 assert(path && *path);
2339 assert(parent_times);
2340 assert(prefix);
2341 assert(prefix_len);
2342 assert(name);
2343 assert(name_len);
2344 assert(has_predicate);
2345
2346 *prefix = NULL;
2347 *prefix_len = 0;
2348 *name = NULL;
2349 *name_len = 0;
2350 *has_predicate = 0;
2351
2352 if (!*parent_times) {
2353 if (!strncmp(*path, "..", 2)) {
2354 *path += 2;
2355 ++par_times;
2356 while (!strncmp(*path, "/..", 3)) {
2357 *path += 3;
2358 ++par_times;
2359 }
2360 }
2361 if (par_times) {
2362 *parent_times = par_times;
2363 } else {
2364 *parent_times = -1;
2365 }
2366 }
2367
2368 if (**path != '/') {
2369 return LY_EINVAL;
2370 }
2371 /* skip '/' */
2372 ++(*path);
2373
2374 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002375 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002376
2377 if ((**path == '/' && (*path)[1]) || !**path) {
2378 /* path continues by another token or this is the last token */
2379 return LY_SUCCESS;
2380 } else if ((*path)[0] != '[') {
2381 /* unexpected character */
2382 return LY_EINVAL;
2383 } else {
2384 /* predicate starting with [ */
2385 *has_predicate = 1;
2386 return LY_SUCCESS;
2387 }
2388}
2389
2390/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002391 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2392 *
2393 * The set of features used for target must be a subset of features used for the leafref.
2394 * This is not a perfect, we should compare the truth tables but it could require too much resources
2395 * and RFC 7950 does not require it explicitely, so we simplify that.
2396 *
2397 * @param[in] refnode The leafref node.
2398 * @param[in] target Tha target node of the leafref.
2399 * @return LY_SUCCESS or LY_EVALID;
2400 */
2401static LY_ERR
2402lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2403{
2404 LY_ERR ret = LY_EVALID;
2405 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002406 unsigned int u, v, count;
2407 struct ly_set features = {0};
2408
2409 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002410 if (iter->iffeatures) {
2411 LY_ARRAY_FOR(iter->iffeatures, u) {
2412 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2413 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002414 }
2415 }
2416 }
2417 }
2418
2419 /* we should have, in features set, a superset of features applicable to the target node.
2420 * So when adding features applicable to the target into the features set, we should not be
2421 * able to actually add any new feature, otherwise it is not a subset of features applicable
2422 * to the leafref itself. */
2423 count = features.count;
2424 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002425 if (iter->iffeatures) {
2426 LY_ARRAY_FOR(iter->iffeatures, u) {
2427 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2428 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002429 /* new feature was added (or LY_EMEM) */
2430 goto cleanup;
2431 }
2432 }
2433 }
2434 }
2435 }
2436 ret = LY_SUCCESS;
2437
2438cleanup:
2439 ly_set_erase(&features, NULL);
2440 return ret;
2441}
2442
2443/**
Radek Krejcia3045382018-11-22 14:30:31 +01002444 * @brief Validate the leafref path.
2445 * @param[in] ctx Compile context
2446 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002447 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002448 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2449 */
2450static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002451lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002452{
2453 const struct lysc_node *node = NULL, *parent = NULL;
2454 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002455 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002456 const char *id, *prefix, *name;
2457 size_t prefix_len, name_len;
2458 int parent_times = 0, has_predicate;
2459 unsigned int iter, u;
2460 LY_ERR ret = LY_SUCCESS;
2461
2462 assert(ctx);
2463 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002464 assert(leafref);
2465
Radek Krejci1c0c3442019-07-23 16:08:47 +02002466 ctx->path[0] = '\0';
2467 lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
2468 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002469
Radek Krejcia3045382018-11-22 14:30:31 +01002470 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002471 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002472 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2473 if (!iter) { /* first iteration */
2474 /* precess ".." in relative paths */
2475 if (parent_times > 0) {
2476 /* move from the context node */
2477 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2478 /* path is supposed to be evaluated in data tree, so we have to skip
2479 * all schema nodes that cannot be instantiated in data tree */
2480 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002481 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002482 }
2483 }
2484 }
2485
2486 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002487 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002488 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002489 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002490 }
2491 if (!mod) {
2492 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002493 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2494 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002495 return LY_EVALID;
2496 }
Radek Krejci3d929562019-02-21 11:25:55 +01002497 if (!mod->implemented) {
2498 /* make the module implemented */
2499 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2500 }
Radek Krejcia3045382018-11-22 14:30:31 +01002501
2502 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2503 if (!node) {
2504 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002505 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002506 return LY_EVALID;
2507 }
2508 parent = node;
2509
2510 if (has_predicate) {
2511 /* we have predicate, so the current result must be list */
2512 if (node->nodetype != LYS_LIST) {
2513 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2514 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002515 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002516 return LY_EVALID;
2517 }
2518
Radek Krejcibade82a2018-12-05 10:13:48 +01002519 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2520 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002521 }
2522
2523 ++iter;
2524 }
2525 if (ret) {
2526 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002527 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002528 return LY_EVALID;
2529 }
2530
2531 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2532 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2533 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002534 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002535 return LY_EVALID;
2536 }
2537
2538 /* check status */
2539 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2540 return LY_EVALID;
2541 }
2542
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002543 /* check config */
2544 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2545 if (node->flags & LYS_CONFIG_R) {
2546 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2547 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2548 leafref->path);
2549 return LY_EVALID;
2550 }
2551 }
2552
Radek Krejci412ddfa2018-11-23 11:44:11 +01002553 /* store the target's type and check for circular chain of leafrefs */
2554 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2555 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2556 if (type == (struct lysc_type*)leafref) {
2557 /* circular chain detected */
2558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2559 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2560 return LY_EVALID;
2561 }
2562 }
2563
Radek Krejci58d171e2018-11-23 13:50:55 +01002564 /* check if leafref and its target are under common if-features */
2565 if (lys_compile_leafref_features_validate(startnode, node)) {
2566 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2567 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2568 leafref->path);
2569 return LY_EVALID;
2570 }
2571
Radek Krejci327de162019-06-14 12:52:07 +02002572 ctx->path_len = 1;
2573 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002574 return LY_SUCCESS;
2575}
2576
Radek Krejcicdfecd92018-11-26 11:27:32 +01002577static 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 +02002578 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002579/**
2580 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2581 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002582 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2583 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2584 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2585 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002586 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002587 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002588 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002589 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2590 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2591 * @param[out] type Newly created type structure with the filled information about the type.
2592 * @return LY_ERR value.
2593 */
Radek Krejci19a96102018-11-15 13:38:09 +01002594static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002595lys_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 +02002596 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002597 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002598{
2599 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002600 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002601 struct lysc_type_bin *bin;
2602 struct lysc_type_num *num;
2603 struct lysc_type_str *str;
2604 struct lysc_type_bits *bits;
2605 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002606 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002607 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002608 struct lysc_type_union *un, *un_aux;
2609 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002610
2611 switch (basetype) {
2612 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002613 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002614
2615 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002616 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002617 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2618 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002619 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002620 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002621 }
2622 }
2623
2624 if (tpdfname) {
2625 type_p->compiled = *type;
2626 *type = calloc(1, sizeof(struct lysc_type_bin));
2627 }
2628 break;
2629 case LY_TYPE_BITS:
2630 /* RFC 7950 9.7 - bits */
2631 bits = (struct lysc_type_bits*)(*type);
2632 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002633 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002634 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2635 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002636 }
2637
Radek Krejci555cb5b2018-11-16 14:54:33 +01002638 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002639 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002640 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002641 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002642 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002643 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002644 free(*type);
2645 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002646 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002647 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002648 }
2649
2650 if (tpdfname) {
2651 type_p->compiled = *type;
2652 *type = calloc(1, sizeof(struct lysc_type_bits));
2653 }
2654 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002655 case LY_TYPE_DEC64:
2656 dec = (struct lysc_type_dec*)(*type);
2657
2658 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002659 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002660 if (!type_p->fraction_digits) {
2661 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002662 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002663 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002665 free(*type);
2666 *type = NULL;
2667 }
2668 return LY_EVALID;
2669 }
2670 } else if (type_p->fraction_digits) {
2671 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002672 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002673 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2674 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002675 tpdfname);
2676 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002677 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2678 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002679 free(*type);
2680 *type = NULL;
2681 }
2682 return LY_EVALID;
2683 }
2684 dec->fraction_digits = type_p->fraction_digits;
2685
2686 /* RFC 7950 9.2.4 - range */
2687 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002688 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2689 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002690 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002691 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002692 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002693 }
2694
2695 if (tpdfname) {
2696 type_p->compiled = *type;
2697 *type = calloc(1, sizeof(struct lysc_type_dec));
2698 }
2699 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002700 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002701 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002702
2703 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002704 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002705 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2706 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002707 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002708 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002709 }
2710 } else if (base && ((struct lysc_type_str*)base)->length) {
2711 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2712 }
2713
2714 /* RFC 7950 9.4.5 - pattern */
2715 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002716 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002717 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002718 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2719 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2720 }
2721
2722 if (tpdfname) {
2723 type_p->compiled = *type;
2724 *type = calloc(1, sizeof(struct lysc_type_str));
2725 }
2726 break;
2727 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002728 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002729
2730 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002731 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002732 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002733 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002734 }
2735
Radek Krejci555cb5b2018-11-16 14:54:33 +01002736 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002737 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002738 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002739 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002740 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002742 free(*type);
2743 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002744 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002745 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002746 }
2747
2748 if (tpdfname) {
2749 type_p->compiled = *type;
2750 *type = calloc(1, sizeof(struct lysc_type_enum));
2751 }
2752 break;
2753 case LY_TYPE_INT8:
2754 case LY_TYPE_UINT8:
2755 case LY_TYPE_INT16:
2756 case LY_TYPE_UINT16:
2757 case LY_TYPE_INT32:
2758 case LY_TYPE_UINT32:
2759 case LY_TYPE_INT64:
2760 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002761 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002762
2763 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002764 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002765 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2766 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002767 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002768 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002769 }
2770 }
2771
2772 if (tpdfname) {
2773 type_p->compiled = *type;
2774 *type = calloc(1, sizeof(struct lysc_type_num));
2775 }
2776 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002777 case LY_TYPE_IDENT:
2778 idref = (struct lysc_type_identityref*)(*type);
2779
2780 /* RFC 7950 9.10.2 - base */
2781 if (type_p->bases) {
2782 if (base) {
2783 /* only the directly derived identityrefs can contain base specification */
2784 if (tpdfname) {
2785 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002786 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002787 tpdfname);
2788 } else {
2789 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002790 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002791 free(*type);
2792 *type = NULL;
2793 }
2794 return LY_EVALID;
2795 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002796 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002797 }
2798
2799 if (!base && !type_p->flags) {
2800 /* type derived from identityref built-in type must contain at least one base */
2801 if (tpdfname) {
2802 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2803 } else {
2804 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2805 free(*type);
2806 *type = NULL;
2807 }
2808 return LY_EVALID;
2809 }
2810
2811 if (tpdfname) {
2812 type_p->compiled = *type;
2813 *type = calloc(1, sizeof(struct lysc_type_identityref));
2814 }
2815 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002816 case LY_TYPE_LEAFREF:
2817 /* RFC 7950 9.9.3 - require-instance */
2818 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002819 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002820 if (tpdfname) {
2821 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2822 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2823 } else {
2824 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2825 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2826 free(*type);
2827 *type = NULL;
2828 }
2829 return LY_EVALID;
2830 }
Radek Krejcia3045382018-11-22 14:30:31 +01002831 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002832 } else if (base) {
2833 /* inherit */
2834 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002835 } else {
2836 /* default is true */
2837 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2838 }
2839 if (type_p->path) {
2840 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002841 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002842 } else if (base) {
2843 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002844 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002845 } else if (tpdfname) {
2846 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2847 return LY_EVALID;
2848 } else {
2849 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2850 free(*type);
2851 *type = NULL;
2852 return LY_EVALID;
2853 }
2854 if (tpdfname) {
2855 type_p->compiled = *type;
2856 *type = calloc(1, sizeof(struct lysc_type_leafref));
2857 }
2858 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002859 case LY_TYPE_INST:
2860 /* RFC 7950 9.9.3 - require-instance */
2861 if (type_p->flags & LYS_SET_REQINST) {
2862 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2863 } else {
2864 /* default is true */
2865 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2866 }
2867
2868 if (tpdfname) {
2869 type_p->compiled = *type;
2870 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2871 }
2872 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002873 case LY_TYPE_UNION:
2874 un = (struct lysc_type_union*)(*type);
2875
2876 /* RFC 7950 7.4 - type */
2877 if (type_p->types) {
2878 if (base) {
2879 /* only the directly derived union can contain types specification */
2880 if (tpdfname) {
2881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2882 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2883 tpdfname);
2884 } else {
2885 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2886 "Invalid type substatement for the type not directly derived from union built-in type.");
2887 free(*type);
2888 *type = NULL;
2889 }
2890 return LY_EVALID;
2891 }
2892 /* compile the type */
2893 additional = 0;
2894 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2895 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002896 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 +01002897 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2898 /* add space for additional types from the union subtype */
2899 un_aux = (struct lysc_type_union *)un->types[u + additional];
2900 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)));
2901 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2902 un->types = (void*)((uint32_t*)(p) + 1);
2903
2904 /* copy subtypes of the subtype union */
2905 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2906 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2907 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2908 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2909 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2910 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2911 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2912 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2913 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2914 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2915 /* TODO extensions */
2916
2917 } else {
2918 un->types[u + additional] = un_aux->types[v];
2919 ++un_aux->types[v]->refcount;
2920 }
2921 ++additional;
2922 LY_ARRAY_INCREMENT(un->types);
2923 }
2924 /* compensate u increment in main loop */
2925 --additional;
2926
2927 /* free the replaced union subtype */
2928 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2929 } else {
2930 LY_ARRAY_INCREMENT(un->types);
2931 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002932 }
2933 }
2934
2935 if (!base && !type_p->flags) {
2936 /* type derived from union built-in type must contain at least one type */
2937 if (tpdfname) {
2938 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2939 } else {
2940 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2941 free(*type);
2942 *type = NULL;
2943 }
2944 return LY_EVALID;
2945 }
2946
2947 if (tpdfname) {
2948 type_p->compiled = *type;
2949 *type = calloc(1, sizeof(struct lysc_type_union));
2950 }
2951 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002952 case LY_TYPE_BOOL:
2953 case LY_TYPE_EMPTY:
2954 case LY_TYPE_UNKNOWN: /* just to complete switch */
2955 break;
2956 }
2957 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2958done:
2959 return ret;
2960}
2961
Radek Krejcia3045382018-11-22 14:30:31 +01002962/**
2963 * @brief Compile information about the leaf/leaf-list's type.
2964 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002965 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2966 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2967 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2968 * @param[in] context_name Name of the context node or referencing typedef for logging.
2969 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002970 * @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 +01002971 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002972 * @return LY_ERR value.
2973 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002974static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002975lys_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 +02002976 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002977{
2978 LY_ERR ret = LY_SUCCESS;
2979 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002980 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002981 struct type_context {
2982 const struct lysp_tpdf *tpdf;
2983 struct lysp_node *node;
2984 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002985 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002986 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002987 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002988 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002989 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002990 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002991
2992 (*type) = NULL;
2993
2994 tctx = calloc(1, sizeof *tctx);
2995 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002996 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002997 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2998 ret == LY_SUCCESS;
2999 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3000 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3001 if (basetype) {
3002 break;
3003 }
3004
3005 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003006 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3007 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003008 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3009
Radek Krejcicdfecd92018-11-26 11:27:32 +01003010 if (units && !*units) {
3011 /* inherit units */
3012 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3013 }
Radek Krejci01342af2019-01-03 15:18:08 +01003014 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003015 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003016 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003017 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003018 }
Radek Krejci01342af2019-01-03 15:18:08 +01003019 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003020 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3021 break;
3022 }
3023
Radek Krejci19a96102018-11-15 13:38:09 +01003024 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003025 /* it is not necessary to continue, the rest of the chain was already compiled,
3026 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003027 basetype = tctx->tpdf->type.compiled->basetype;
3028 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003029 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003030 dummyloops = 1;
3031 goto preparenext;
3032 } else {
3033 tctx = NULL;
3034 break;
3035 }
Radek Krejci19a96102018-11-15 13:38:09 +01003036 }
3037
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003038 /* circular typedef reference detection */
3039 for (u = 0; u < tpdf_chain.count; u++) {
3040 /* local part */
3041 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3042 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3043 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3044 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3045 free(tctx);
3046 ret = LY_EVALID;
3047 goto cleanup;
3048 }
3049 }
3050 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3051 /* global part for unions corner case */
3052 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3053 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3054 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3055 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3056 free(tctx);
3057 ret = LY_EVALID;
3058 goto cleanup;
3059 }
3060 }
3061
Radek Krejci19a96102018-11-15 13:38:09 +01003062 /* store information for the following processing */
3063 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3064
Radek Krejcicdfecd92018-11-26 11:27:32 +01003065preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003066 /* prepare next loop */
3067 tctx_prev = tctx;
3068 tctx = calloc(1, sizeof *tctx);
3069 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3070 }
3071 free(tctx);
3072
3073 /* allocate type according to the basetype */
3074 switch (basetype) {
3075 case LY_TYPE_BINARY:
3076 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003077 break;
3078 case LY_TYPE_BITS:
3079 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003080 break;
3081 case LY_TYPE_BOOL:
3082 case LY_TYPE_EMPTY:
3083 *type = calloc(1, sizeof(struct lysc_type));
3084 break;
3085 case LY_TYPE_DEC64:
3086 *type = calloc(1, sizeof(struct lysc_type_dec));
3087 break;
3088 case LY_TYPE_ENUM:
3089 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003090 break;
3091 case LY_TYPE_IDENT:
3092 *type = calloc(1, sizeof(struct lysc_type_identityref));
3093 break;
3094 case LY_TYPE_INST:
3095 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3096 break;
3097 case LY_TYPE_LEAFREF:
3098 *type = calloc(1, sizeof(struct lysc_type_leafref));
3099 break;
3100 case LY_TYPE_STRING:
3101 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003102 break;
3103 case LY_TYPE_UNION:
3104 *type = calloc(1, sizeof(struct lysc_type_union));
3105 break;
3106 case LY_TYPE_INT8:
3107 case LY_TYPE_UINT8:
3108 case LY_TYPE_INT16:
3109 case LY_TYPE_UINT16:
3110 case LY_TYPE_INT32:
3111 case LY_TYPE_UINT32:
3112 case LY_TYPE_INT64:
3113 case LY_TYPE_UINT64:
3114 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003115 break;
3116 case LY_TYPE_UNKNOWN:
3117 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3118 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3119 ret = LY_EVALID;
3120 goto cleanup;
3121 }
3122 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003123 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003124 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3125 ly_data_type2str[basetype]);
3126 free(*type);
3127 (*type) = NULL;
3128 ret = LY_EVALID;
3129 goto cleanup;
3130 }
3131
3132 /* get restrictions from the referred typedefs */
3133 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3134 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003135
3136 /* remember the typedef context for circular check */
3137 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3138
Radek Krejci43699232018-11-23 14:59:46 +01003139 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003140 base = tctx->tpdf->type.compiled;
3141 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003142 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003143 /* no change, just use the type information from the base */
3144 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3145 ++base->refcount;
3146 continue;
3147 }
3148
3149 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003150 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3151 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3152 tctx->tpdf->name, ly_data_type2str[basetype]);
3153 ret = LY_EVALID;
3154 goto cleanup;
3155 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3156 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3157 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3158 tctx->tpdf->name, tctx->tpdf->dflt);
3159 ret = LY_EVALID;
3160 goto cleanup;
3161 }
3162
Radek Krejci19a96102018-11-15 13:38:09 +01003163 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003164 /* TODO user type plugins */
3165 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003166 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003167 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 +01003168 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003169 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003170 LY_CHECK_GOTO(ret, cleanup);
3171 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003172 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003173 /* remove the processed typedef contexts from the stack for circular check */
3174 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003175
Radek Krejcic5c27e52018-11-15 14:38:11 +01003176 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003177 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003178 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003179 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003180 /* TODO user type plugins */
3181 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003182 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003183 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 +01003184 LY_CHECK_GOTO(ret, cleanup);
3185 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003186 /* no specific restriction in leaf's type definition, copy from the base */
3187 free(*type);
3188 (*type) = base;
3189 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003190 }
Radek Krejcia1911222019-07-22 17:24:50 +02003191 if (dflt && !(*type)->dflt) {
3192 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003193 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003194 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3195 (*type)->dflt->realtype = (*type);
3196 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3197 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003198 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003199 if (err) {
3200 ly_err_print(err);
3201 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3202 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3203 ly_err_free(err);
3204 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003205 if (ret == LY_EINCOMPLETE) {
3206 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003207 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003208
3209 /* but in general result is so far ok */
3210 ret = LY_SUCCESS;
3211 }
Radek Krejcia1911222019-07-22 17:24:50 +02003212 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003213 }
Radek Krejci19a96102018-11-15 13:38:09 +01003214
Radek Krejciec4da802019-05-02 13:02:41 +02003215 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003216
3217cleanup:
3218 ly_set_erase(&tpdf_chain, free);
3219 return ret;
3220}
3221
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003222/**
3223 * @brief Compile status information of the given node.
3224 *
3225 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3226 * has the status correctly set during the compilation.
3227 *
3228 * @param[in] ctx Compile context
3229 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3230 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3231 * the compatibility with the parent's status value.
3232 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3233 * @return LY_ERR value.
3234 */
3235static LY_ERR
3236lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3237{
3238 /* status - it is not inherited by specification, but it does not make sense to have
3239 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3240 if (!((*node_flags) & LYS_STATUS_MASK)) {
3241 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3242 if ((parent_flags & 0x3) != 0x3) {
3243 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3244 * combination of bits (0x3) which marks the uses_status value */
3245 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3246 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3247 }
3248 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3249 } else {
3250 (*node_flags) |= LYS_STATUS_CURR;
3251 }
3252 } else if (parent_flags & LYS_STATUS_MASK) {
3253 /* check status compatibility with the parent */
3254 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3255 if ((*node_flags) & LYS_STATUS_CURR) {
3256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3257 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3258 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3259 } else { /* LYS_STATUS_DEPRC */
3260 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3261 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3262 }
3263 return LY_EVALID;
3264 }
3265 }
3266 return LY_SUCCESS;
3267}
3268
Radek Krejci8cce8532019-03-05 11:27:45 +01003269/**
3270 * @brief Check uniqness of the node/action/notification name.
3271 *
3272 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3273 * structures, but they share the namespace so we need to check their name collisions.
3274 *
3275 * @param[in] ctx Compile context.
3276 * @param[in] children List (linked list) of data nodes to go through.
3277 * @param[in] actions List (sized array) of actions or RPCs to go through.
3278 * @param[in] notifs List (sized array) of Notifications to go through.
3279 * @param[in] name Name of the item to find in the given lists.
3280 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3281 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3282 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3283 */
3284static LY_ERR
3285lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3286 const struct lysc_action *actions, const struct lysc_notif *notifs,
3287 const char *name, void *exclude)
3288{
3289 const struct lysc_node *iter;
3290 unsigned int u;
3291
3292 LY_LIST_FOR(children, iter) {
3293 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3294 goto error;
3295 }
3296 }
3297 LY_ARRAY_FOR(actions, u) {
3298 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3299 goto error;
3300 }
3301 }
3302 LY_ARRAY_FOR(notifs, u) {
3303 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3304 goto error;
3305 }
3306 }
3307 return LY_SUCCESS;
3308error:
3309 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3310 return LY_EEXIST;
3311}
3312
Radek Krejciec4da802019-05-02 13:02:41 +02003313static 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 +01003314
Radek Krejcia3045382018-11-22 14:30:31 +01003315/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003316 * @brief Compile parsed RPC/action schema node information.
3317 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003318 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003319 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003320 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3321 * @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).
3322 * Zero means no uses, non-zero value with no status bit set mean the default status.
3323 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3324 */
3325static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003326lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003327 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3328{
3329 LY_ERR ret = LY_SUCCESS;
3330 struct lysp_node *child_p;
3331 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003332 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003333
Radek Krejci327de162019-06-14 12:52:07 +02003334 lysc_update_path(ctx, parent, action_p->name);
3335
Radek Krejci8cce8532019-03-05 11:27:45 +01003336 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3337 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3338 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3339 action_p->name, action)) {
3340 return LY_EVALID;
3341 }
3342
Radek Krejciec4da802019-05-02 13:02:41 +02003343 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003344 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003345 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003346 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003347 return LY_EVALID;
3348 }
3349
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003350 action->nodetype = LYS_ACTION;
3351 action->module = ctx->mod;
3352 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003353 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003354 action->sp = action_p;
3355 }
3356 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3357
3358 /* status - it is not inherited by specification, but it does not make sense to have
3359 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3360 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3361
3362 DUP_STRING(ctx->ctx, action_p->name, action->name);
3363 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3364 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003365 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3366 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003367
3368 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003369 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejciec4da802019-05-02 13:02:41 +02003370 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
3371 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup);
3372 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003373 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003374 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003375 }
Radek Krejci327de162019-06-14 12:52:07 +02003376 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003377 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003378
3379 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003380 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejciec4da802019-05-02 13:02:41 +02003381 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
3382 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup);
3383 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003384 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003385 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003386 }
Radek Krejci327de162019-06-14 12:52:07 +02003387 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003388
Radek Krejci327de162019-06-14 12:52:07 +02003389 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003390cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003391 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003392 return ret;
3393}
3394
3395/**
Radek Krejci43981a32019-04-12 09:44:11 +02003396 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003397 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003398 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003399 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3400 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3401 * @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 +02003402 * Zero means no uses, non-zero value with no status bit set mean the default status.
3403 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3404 */
3405static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003406lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003407 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3408{
3409 LY_ERR ret = LY_SUCCESS;
3410 struct lysp_node *child_p;
3411 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003412 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003413
Radek Krejci327de162019-06-14 12:52:07 +02003414 lysc_update_path(ctx, parent, notif_p->name);
3415
Radek Krejcifc11bd72019-04-11 16:00:05 +02003416 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3417 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3418 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3419 notif_p->name, notif)) {
3420 return LY_EVALID;
3421 }
3422
Radek Krejciec4da802019-05-02 13:02:41 +02003423 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003424 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3425 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003426 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003427 return LY_EVALID;
3428 }
3429
3430 notif->nodetype = LYS_NOTIF;
3431 notif->module = ctx->mod;
3432 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003433 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003434 notif->sp = notif_p;
3435 }
3436 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3437
3438 /* status - it is not inherited by specification, but it does not make sense to have
3439 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3440 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3441
3442 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3443 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3444 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003445 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3446 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup);
3447 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003448
Radek Krejciec4da802019-05-02 13:02:41 +02003449 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003450 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003451 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003452 }
3453
Radek Krejci327de162019-06-14 12:52:07 +02003454 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003455cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003456 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003457 return ret;
3458}
3459
3460/**
Radek Krejcia3045382018-11-22 14:30:31 +01003461 * @brief Compile parsed container node information.
3462 * @param[in] ctx Compile context
3463 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003464 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3465 * is enriched with the container-specific information.
3466 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3467 */
Radek Krejci19a96102018-11-15 13:38:09 +01003468static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003469lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003470{
3471 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3472 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3473 struct lysp_node *child_p;
3474 unsigned int u;
3475 LY_ERR ret = LY_SUCCESS;
3476
Radek Krejcife909632019-02-12 15:34:42 +01003477 if (cont_p->presence) {
3478 cont->flags |= LYS_PRESENCE;
3479 }
3480
Radek Krejci19a96102018-11-15 13:38:09 +01003481 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003482 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003483 }
3484
Radek Krejciec4da802019-05-02 13:02:41 +02003485 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3486 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3487 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003488
3489done:
3490 return ret;
3491}
3492
Radek Krejci33f72892019-02-21 10:36:58 +01003493/*
3494 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3495 * @param[in] ctx Compile context.
3496 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3497 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003498 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3499 * @return LY_ERR value.
3500 */
3501static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003502lys_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 +01003503{
Radek Krejcia1911222019-07-22 17:24:50 +02003504 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003505 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3506
Radek Krejciec4da802019-05-02 13:02:41 +02003507 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 +01003508 leaf->units ? NULL : &leaf->units));
3509 if (leaf->nodetype == LYS_LEAFLIST) {
3510 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003511 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3512 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003513 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003514 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3515 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3516 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3517 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003518 LY_ARRAY_INCREMENT(llist->dflts);
3519 }
3520 } else {
3521 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003522 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003523 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3524 leaf->dflt->realtype = leaf->type->dflt->realtype;
3525 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3526 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003527 }
3528 }
3529 if (leaf->type->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 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3533 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3534 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3535 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3536 ly_set_add(&ctx->unres, leaf, 0);
3537 }
3538 }
3539 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003540 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3541 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3542 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3543 return LY_EVALID;
3544 }
3545 }
3546
Radek Krejci33f72892019-02-21 10:36:58 +01003547 return LY_SUCCESS;
3548}
3549
Radek Krejcia3045382018-11-22 14:30:31 +01003550/**
3551 * @brief Compile parsed leaf node information.
3552 * @param[in] ctx Compile context
3553 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003554 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3555 * is enriched with the leaf-specific information.
3556 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3557 */
Radek Krejci19a96102018-11-15 13:38:09 +01003558static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003559lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003560{
3561 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3562 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3563 unsigned int u;
3564 LY_ERR ret = LY_SUCCESS;
3565
Radek Krejciec4da802019-05-02 13:02:41 +02003566 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003567 if (leaf_p->units) {
3568 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3569 leaf->flags |= LYS_SET_UNITS;
3570 }
Radek Krejcia1911222019-07-22 17:24:50 +02003571
3572 /* the dflt member is just filled to avoid getting the default value from the type */
3573 leaf->dflt = (void*)leaf_p->dflt;
3574 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3575
Radek Krejciccd20f12019-02-15 14:12:27 +01003576 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003577 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003578 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003579 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3580 leaf->dflt->realtype = leaf->type;
3581 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3582 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003583 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003584 leaf->dflt->realtype->refcount++;
3585 if (err) {
3586 ly_err_print(err);
3587 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3588 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3589 ly_err_free(err);
3590 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003591 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003592 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003593 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003594
3595 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003596 ret = LY_SUCCESS;
3597 }
Radek Krejcia1911222019-07-22 17:24:50 +02003598 LY_CHECK_GOTO(ret, done);
Radek Krejci76b3e962018-12-14 17:01:25 +01003599 leaf->flags |= LYS_SET_DFLT;
3600 }
Radek Krejci43699232018-11-23 14:59:46 +01003601
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003602
Radek Krejci19a96102018-11-15 13:38:09 +01003603done:
3604 return ret;
3605}
3606
Radek Krejcia3045382018-11-22 14:30:31 +01003607/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003608 * @brief Compile parsed leaf-list node information.
3609 * @param[in] ctx Compile context
3610 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003611 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3612 * is enriched with the leaf-list-specific information.
3613 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3614 */
3615static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003616lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003617{
3618 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3619 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003620 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003621 LY_ERR ret = LY_SUCCESS;
3622
Radek Krejciec4da802019-05-02 13:02:41 +02003623 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003624 if (llist_p->units) {
3625 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3626 llist->flags |= LYS_SET_UNITS;
3627 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003628
Radek Krejcia1911222019-07-22 17:24:50 +02003629 /* the dflts member is just filled to avoid getting the default value from the type */
3630 llist->dflts = (void*)llist_p->dflts;
3631 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003632 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003633 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003634 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003635 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3636 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003637 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003638 LY_ARRAY_INCREMENT(llist->dflts_mods);
3639 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003640 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003641 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3642 llist->dflts[u]->realtype = llist->type;
3643 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3644 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003645 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003646 llist->dflts[u]->realtype->refcount++;
3647 if (err) {
3648 ly_err_print(err);
3649 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3650 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3651 ly_err_free(err);
3652 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003653 if (ret == LY_EINCOMPLETE) {
3654 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003655 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003656
3657 /* but in general result is so far ok */
3658 ret = LY_SUCCESS;
3659 }
Radek Krejcia1911222019-07-22 17:24:50 +02003660 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003661 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003662 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003663 }
Radek Krejcia1911222019-07-22 17:24:50 +02003664 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3665 /* configuration data values must be unique - so check the default values */
3666 LY_ARRAY_FOR(llist->dflts, u) {
3667 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3668 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3669 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003670 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 +02003671 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3672 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3673 if (dynamic) {
3674 free((char*)val);
3675 }
3676 return LY_EVALID;
3677 }
3678 }
3679 }
3680 }
3681
3682 /* 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 +01003683
3684 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003685 if (llist->min) {
3686 llist->flags |= LYS_MAND_TRUE;
3687 }
Radek Krejcib7408632018-11-28 17:12:11 +01003688 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003689
Radek Krejci0e5d8382018-11-28 16:37:53 +01003690done:
3691 return ret;
3692}
3693
3694/**
Radek Krejci7af64242019-02-18 13:07:53 +01003695 * @brief Compile information about list's uniques.
3696 * @param[in] ctx Compile context.
3697 * @param[in] context_module Module where the prefixes are going to be resolved.
3698 * @param[in] uniques Sized array list of unique statements.
3699 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3700 * @return LY_ERR value.
3701 */
3702static LY_ERR
3703lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3704{
3705 LY_ERR ret = LY_SUCCESS;
3706 struct lysc_node_leaf **key, ***unique;
3707 const char *keystr, *delim;
3708 size_t len;
3709 unsigned int v;
3710 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003711 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003712
3713 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3714 config = -1;
3715 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3716 keystr = uniques[v];
3717 while (keystr) {
3718 delim = strpbrk(keystr, " \t\n");
3719 if (delim) {
3720 len = delim - keystr;
3721 while (isspace(*delim)) {
3722 ++delim;
3723 }
3724 } else {
3725 len = strlen(keystr);
3726 }
3727
3728 /* unique node must be present */
3729 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003730 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3731 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003732 if (ret != LY_SUCCESS) {
3733 if (ret == LY_EDENIED) {
3734 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003735 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003736 len, keystr, lys_nodetype2str((*key)->nodetype));
3737 }
3738 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003739 } else if (flags) {
3740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3741 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3742 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3743 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003744 }
3745
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003746
Radek Krejci7af64242019-02-18 13:07:53 +01003747 /* all referenced leafs must be of the same config type */
3748 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3749 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3750 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3751 return LY_EVALID;
3752 } else if ((*key)->flags & LYS_CONFIG_W) {
3753 config = 1;
3754 } else { /* LYS_CONFIG_R */
3755 config = 0;
3756 }
3757
3758 /* check status */
3759 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3760 (*key)->flags, (*key)->module, (*key)->name));
3761
3762 /* mark leaf as unique */
3763 (*key)->flags |= LYS_UNIQUE;
3764
3765 /* next unique value in line */
3766 keystr = delim;
3767 }
3768 /* next unique definition */
3769 }
3770
3771 return LY_SUCCESS;
3772}
3773
3774/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003775 * @brief Compile parsed list node information.
3776 * @param[in] ctx Compile context
3777 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003778 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3779 * is enriched with the list-specific information.
3780 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3781 */
3782static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003783lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003784{
3785 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3786 struct lysc_node_list *list = (struct lysc_node_list*)node;
3787 struct lysp_node *child_p;
Radek Krejci7af64242019-02-18 13:07:53 +01003788 struct lysc_node_leaf **key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003789 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003790 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003791 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003792 LY_ERR ret = LY_SUCCESS;
3793
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003794 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003795 if (list->min) {
3796 list->flags |= LYS_MAND_TRUE;
3797 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003798 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3799
3800 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003801 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003802 }
3803
Radek Krejciec4da802019-05-02 13:02:41 +02003804 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003805
3806 /* keys */
3807 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3808 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3809 return LY_EVALID;
3810 }
3811
3812 /* find all the keys (must be direct children) */
3813 keystr = list_p->key;
3814 while (keystr) {
3815 delim = strpbrk(keystr, " \t\n");
3816 if (delim) {
3817 len = delim - keystr;
3818 while (isspace(*delim)) {
3819 ++delim;
3820 }
3821 } else {
3822 len = strlen(keystr);
3823 }
3824
3825 /* key node must be present */
3826 LY_ARRAY_NEW_RET(ctx->ctx, list->keys, key, LY_EMEM);
3827 *key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3828 if (!(*key)) {
3829 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3830 "The list's key \"%.*s\" not found.", len, keystr);
3831 return LY_EVALID;
3832 }
3833 /* keys must be unique */
3834 for(u = 0; u < LY_ARRAY_SIZE(list->keys) - 1; ++u) {
3835 if (*key == list->keys[u]) {
3836 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3837 "Duplicated key identifier \"%.*s\".", len, keystr);
3838 return LY_EVALID;
3839 }
3840 }
Radek Krejci327de162019-06-14 12:52:07 +02003841 lysc_update_path(ctx, (struct lysc_node*)list, (*key)->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003842 /* key must have the same config flag as the list itself */
3843 if ((list->flags & LYS_CONFIG_MASK) != ((*key)->flags & LYS_CONFIG_MASK)) {
3844 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3845 return LY_EVALID;
3846 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003847 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003848 /* YANG 1.0 denies key to be of empty type */
3849 if ((*key)->type->basetype == LY_TYPE_EMPTY) {
3850 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003851 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003852 return LY_EVALID;
3853 }
3854 } else {
3855 /* when and if-feature are illegal on list keys */
3856 if ((*key)->when) {
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 \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003859 return LY_EVALID;
3860 }
3861 if ((*key)->iffeatures) {
3862 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003863 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003864 return LY_EVALID;
3865 }
3866 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003867
3868 /* check status */
3869 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3870 (*key)->flags, (*key)->module, (*key)->name));
3871
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003872 /* ignore default values of the key */
3873 if ((*key)->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003874 (*key)->dflt->realtype->plugin->free(ctx->ctx, (*key)->dflt);
3875 lysc_type_free(ctx->ctx, (*key)->dflt->realtype);
3876 free((*key)->dflt);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003877 (*key)->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003878 (*key)->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003879 }
3880 /* mark leaf as key */
3881 (*key)->flags |= LYS_KEY;
3882
3883 /* next key value */
3884 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003885 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003886 }
3887
3888 /* uniques */
3889 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003890 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003891 }
3892
Radek Krejciec4da802019-05-02 13:02:41 +02003893 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3894 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003895
3896done:
3897 return ret;
3898}
3899
Radek Krejcib56c7502019-02-13 14:19:54 +01003900/**
3901 * @brief Do some checks and set the default choice's case.
3902 *
3903 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3904 *
3905 * @param[in] ctx Compile context.
3906 * @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,
3907 * not the reference to the imported module.
3908 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3909 * @return LY_ERR value.
3910 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003911static LY_ERR
3912lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3913{
3914 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3915 const char *prefix = NULL, *name;
3916 size_t prefix_len = 0;
3917
3918 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3919 name = strchr(dflt, ':');
3920 if (name) {
3921 prefix = dflt;
3922 prefix_len = name - prefix;
3923 ++name;
3924 } else {
3925 name = dflt;
3926 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003927 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003928 /* prefixed default case make sense only for the prefix of the schema itself */
3929 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3930 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3931 prefix_len, prefix);
3932 return LY_EVALID;
3933 }
3934 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3935 if (!ch->dflt) {
3936 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3937 "Default case \"%s\" not found.", dflt);
3938 return LY_EVALID;
3939 }
3940 /* no mandatory nodes directly under the default case */
3941 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003942 if (iter->parent != (struct lysc_node*)ch->dflt) {
3943 break;
3944 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003945 if (iter->flags & LYS_MAND_TRUE) {
3946 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3947 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3948 return LY_EVALID;
3949 }
3950 }
Radek Krejci01342af2019-01-03 15:18:08 +01003951 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003952 return LY_SUCCESS;
3953}
3954
Radek Krejciccd20f12019-02-15 14:12:27 +01003955static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003956lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003957{
3958 struct lys_module *mod;
3959 const char *prefix = NULL, *name;
3960 size_t prefix_len = 0;
3961 struct lysc_node_case *cs;
3962 struct lysc_node *node;
3963
3964 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3965 name = strchr(dflt, ':');
3966 if (name) {
3967 prefix = dflt;
3968 prefix_len = name - prefix;
3969 ++name;
3970 } else {
3971 name = dflt;
3972 }
3973 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
3974 if (prefix) {
3975 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
3976 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003977 "Invalid deviation adding \"default\" property \"%s\" of choice. "
3978 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003979 return LY_EVALID;
3980 }
3981 } else {
3982 mod = ctx->mod;
3983 }
3984 /* get the default case */
3985 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3986 if (!cs) {
3987 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02003988 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01003989 return LY_EVALID;
3990 }
3991
3992 /* check that there is no mandatory node */
3993 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01003994 if (node->parent != (struct lysc_node*)cs) {
3995 break;
3996 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003997 if (node->flags & LYS_MAND_TRUE) {
3998 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003999 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4000 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004001 return LY_EVALID;
4002 }
4003 }
4004
4005 /* set the default case in choice */
4006 ch->dflt = cs;
4007 cs->flags |= LYS_SET_DFLT;
4008
4009 return LY_SUCCESS;
4010}
4011
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004012/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004013 * @brief Compile parsed choice node information.
4014 * @param[in] ctx Compile context
4015 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004016 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004017 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004018 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4019 */
4020static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004021lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004022{
4023 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4024 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4025 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004026 LY_ERR ret = LY_SUCCESS;
4027
Radek Krejci056d0a82018-12-06 16:57:25 +01004028 LY_LIST_FOR(ch_p->child, child_p) {
4029 if (child_p->nodetype == LYS_CASE) {
4030 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004031 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004032 }
4033 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004034 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004035 }
4036 }
4037
4038 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004039 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004040 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004041 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004042
Radek Krejci9800fb82018-12-13 14:26:23 +01004043 return ret;
4044}
4045
4046/**
4047 * @brief Compile parsed anydata or anyxml node information.
4048 * @param[in] ctx Compile context
4049 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004050 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4051 * is enriched with the any-specific information.
4052 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4053 */
4054static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004055lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004056{
4057 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4058 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4059 unsigned int u;
4060 LY_ERR ret = LY_SUCCESS;
4061
Radek Krejciec4da802019-05-02 13:02:41 +02004062 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004063
4064 if (any->flags & LYS_CONFIG_W) {
4065 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
4066 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
4067 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004068done:
4069 return ret;
4070}
4071
Radek Krejcib56c7502019-02-13 14:19:54 +01004072/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004073 * @brief Connect the node into the siblings list and check its name uniqueness.
4074 *
4075 * @param[in] ctx Compile context
4076 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4077 * the choice itself is expected instead of a specific case node.
4078 * @param[in] node Schema node to connect into the list.
4079 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4080 */
4081static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004082lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004083{
4084 struct lysc_node **children;
4085
4086 if (node->nodetype == LYS_CASE) {
4087 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4088 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004089 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004090 }
4091 if (children) {
4092 if (!(*children)) {
4093 /* first child */
4094 *children = node;
4095 } else if (*children != node) {
4096 /* by the condition in previous branch we cover the choice/case children
4097 * - the children list is shared by the choice and the the first case, in addition
4098 * the first child of each case must be referenced from the case node. So the node is
4099 * actually always already inserted in case it is the first children - so here such
4100 * a situation actually corresponds to the first branch */
4101 /* insert at the end of the parent's children list */
4102 (*children)->prev->next = node;
4103 node->prev = (*children)->prev;
4104 (*children)->prev = node;
4105
4106 /* check the name uniqueness */
4107 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4108 lysc_node_notifs(parent), node->name, node)) {
4109 return LY_EEXIST;
4110 }
4111 }
4112 }
4113 return LY_SUCCESS;
4114}
4115
Radek Krejci95710c92019-02-11 15:49:55 +01004116/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004117 * @brief Get the XPath context node for the given schema node.
4118 * @param[in] start The schema node where the XPath expression appears.
4119 * @return The context node to evaluate XPath expression in given schema node.
4120 * @return NULL in case the context node is the root node.
4121 */
4122static struct lysc_node *
4123lysc_xpath_context(struct lysc_node *start)
4124{
4125 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
4126 start = start->parent);
4127 return start;
4128}
4129
4130/**
4131 * @brief Prepare the case structure in choice node for the new data node.
4132 *
4133 * 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
4134 * created in the choice when the first child was processed.
4135 *
4136 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004137 * @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,
4138 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004139 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4140 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4141 * @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,
4142 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004143 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004144static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004145lys_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 +01004146{
4147 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004148 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004149 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004150 unsigned int u;
4151 LY_ERR ret;
4152
Radek Krejci95710c92019-02-11 15:49:55 +01004153#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004154 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004155 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004156 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4157 return NULL; \
4158 } \
4159 }
4160
Radek Krejci95710c92019-02-11 15:49:55 +01004161 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4162 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004163
4164 /* we have to add an implicit case node into the parent choice */
4165 cs = calloc(1, sizeof(struct lysc_node_case));
4166 DUP_STRING(ctx->ctx, child->name, cs->name);
4167 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004168 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004169 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4170 /* the case is already present since the child is not its first children */
4171 return (struct lysc_node_case*)ch->cases->prev;
4172 }
Radek Krejci95710c92019-02-11 15:49:55 +01004173 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004174
4175 /* explicit parent case is not present (this is its first child) */
4176 cs = calloc(1, sizeof(struct lysc_node_case));
4177 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4178 cs->flags = LYS_STATUS_MASK & node_p->flags;
4179 cs->sp = node_p;
4180
Radek Krejcib1b59152019-01-07 13:21:56 +01004181 /* 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 +02004182 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004183
4184 if (node_p->when) {
4185 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004186 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004187 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004188 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004189 }
Radek Krejciec4da802019-05-02 13:02:41 +02004190 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004191 } else {
4192 LOGINT(ctx->ctx);
4193 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004194 }
4195 cs->module = ctx->mod;
4196 cs->prev = (struct lysc_node*)cs;
4197 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004198 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004199 cs->parent = (struct lysc_node*)ch;
4200 cs->child = child;
4201
4202 return cs;
4203error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004204 if (cs) {
4205 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4206 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004207 return NULL;
4208
4209#undef UNIQUE_CHECK
4210}
4211
Radek Krejcib56c7502019-02-13 14:19:54 +01004212/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004213 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004214 *
4215 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004216 * @param[in] node Target node where the config is supposed to be changed.
4217 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004218 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4219 * 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 +01004220 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4221 * @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 +01004222 * @return LY_ERR value.
4223 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004224static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004225lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004226 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004227{
4228 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004229 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004230
4231 if (config == (node->flags & LYS_CONFIG_MASK)) {
4232 /* nothing to do */
4233 return LY_SUCCESS;
4234 }
4235
4236 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004237 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004238 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4239 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004240 "Invalid %s of config - configuration node cannot be child of any state data node.",
4241 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004242 return LY_EVALID;
4243 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004244 node->flags |= LYS_SET_CONFIG;
4245 } else {
4246 if (node->flags & LYS_SET_CONFIG) {
4247 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4248 /* setting config flags, but have node with explicit config true */
4249 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004250 "Invalid %s of config - configuration node cannot be child of any state data node.",
4251 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004252 return LY_EVALID;
4253 }
4254 /* do not change config on nodes where the config is explicitely set, this does not apply to
4255 * nodes, which are being changed explicitly (targets of refine or deviation) */
4256 return LY_SUCCESS;
4257 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004258 }
4259 node->flags &= ~LYS_CONFIG_MASK;
4260 node->flags |= config;
4261
4262 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004263 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004264 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004265 }
4266
Radek Krejci76b3e962018-12-14 17:01:25 +01004267 return LY_SUCCESS;
4268}
4269
Radek Krejcib56c7502019-02-13 14:19:54 +01004270/**
4271 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4272 *
4273 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4274 * the flag to such parents from a mandatory children.
4275 *
4276 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4277 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4278 * (mandatory children was removed).
4279 */
Radek Krejcife909632019-02-12 15:34:42 +01004280void
4281lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4282{
4283 struct lysc_node *iter;
4284
4285 if (add) { /* set flag */
4286 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4287 parent = parent->parent) {
4288 parent->flags |= LYS_MAND_TRUE;
4289 }
4290 } else { /* unset flag */
4291 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004292 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004293 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004294 /* there is another mandatory node */
4295 return;
4296 }
4297 }
4298 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4299 parent->flags &= ~LYS_MAND_TRUE;
4300 }
4301 }
4302}
4303
Radek Krejci056d0a82018-12-06 16:57:25 +01004304/**
Radek Krejci3641f562019-02-13 15:38:40 +01004305 * @brief Internal sorting process for the lys_compile_augment_sort().
4306 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4307 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4308 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4309 */
4310static void
4311lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4312{
4313 unsigned int v;
4314 size_t len;
4315
4316 len = strlen(aug_p->nodeid);
4317 LY_ARRAY_FOR(result, v) {
4318 if (strlen(result[v]->nodeid) <= len) {
4319 continue;
4320 }
4321 if (v < LY_ARRAY_SIZE(result)) {
4322 /* move the rest of array */
4323 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4324 break;
4325 }
4326 }
4327 result[v] = aug_p;
4328 LY_ARRAY_INCREMENT(result);
4329}
4330
4331/**
4332 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4333 *
4334 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4335 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4336 *
4337 * @param[in] ctx Compile context.
4338 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4339 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4340 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4341 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4342 * @return LY_ERR value.
4343 */
4344LY_ERR
4345lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4346{
4347 struct lysp_augment **result = NULL;
4348 unsigned int u, v;
4349 size_t count = 0;
4350
4351 assert(augments);
4352
4353 /* get count of the augments in module and all its submodules */
4354 if (aug_p) {
4355 count += LY_ARRAY_SIZE(aug_p);
4356 }
4357 LY_ARRAY_FOR(inc_p, u) {
4358 if (inc_p[u].submodule->augments) {
4359 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4360 }
4361 }
4362
4363 if (!count) {
4364 *augments = NULL;
4365 return LY_SUCCESS;
4366 }
4367 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4368
4369 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4370 * together, so there can be even /z/y betwwen them. */
4371 LY_ARRAY_FOR(aug_p, u) {
4372 lys_compile_augment_sort_(&aug_p[u], result);
4373 }
4374 LY_ARRAY_FOR(inc_p, u) {
4375 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4376 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4377 }
4378 }
4379
4380 *augments = result;
4381 return LY_SUCCESS;
4382}
4383
4384/**
4385 * @brief Compile the parsed augment connecting it into its target.
4386 *
4387 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4388 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4389 * are already implemented and compiled.
4390 *
4391 * @param[in] ctx Compile context.
4392 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004393 * @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
4394 * children in case of the augmenting uses data.
4395 * @return LY_SUCCESS on success.
4396 * @return LY_EVALID on failure.
4397 */
4398LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004399lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004400{
4401 LY_ERR ret = LY_SUCCESS;
4402 struct lysp_node *node_p, *case_node_p;
4403 struct lysc_node *target; /* target target of the augment */
4404 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004405 struct lysc_when **when, *when_shared;
4406 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004407 uint16_t flags = 0;
4408 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004409 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004410
Radek Krejci327de162019-06-14 12:52:07 +02004411 lysc_update_path(ctx, NULL, "{augment}");
4412 lysc_update_path(ctx, NULL, aug_p->nodeid);
4413
Radek Krejci7af64242019-02-18 13:07:53 +01004414 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004415 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004416 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004417 if (ret != LY_SUCCESS) {
4418 if (ret == LY_EDENIED) {
4419 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4420 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4421 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4422 }
4423 return LY_EVALID;
4424 }
4425
4426 /* check for mandatory nodes
4427 * - new cases augmenting some choice can have mandatory nodes
4428 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4429 */
Radek Krejci733988a2019-02-15 15:12:44 +01004430 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004431 allow_mandatory = 1;
4432 }
4433
4434 when_shared = NULL;
4435 LY_LIST_FOR(aug_p->child, node_p) {
4436 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4437 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4438 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004439 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4440 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004441 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004442 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4443 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004444 return LY_EVALID;
4445 }
4446
4447 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004448 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004449 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004450 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004451 } else {
4452 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004453 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004454 }
4455 }
Radek Krejciec4da802019-05-02 13:02:41 +02004456 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004457
Radek Krejcife13da42019-02-15 14:51:01 +01004458 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4459 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004460 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004461 /* 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 +01004462 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 +01004463 } else if (target->nodetype == LYS_CHOICE) {
4464 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4465 node = ((struct lysc_node_choice*)target)->cases->prev;
4466 } else {
4467 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004468 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004469 }
4470
Radek Krejci733988a2019-02-15 15:12:44 +01004471 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004472 node->flags &= ~LYS_MAND_TRUE;
4473 lys_compile_mandatory_parents(target, 0);
4474 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004475 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004476 return LY_EVALID;
4477 }
4478
4479 /* pass augment's when to all the children */
4480 if (aug_p->when) {
4481 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4482 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004483 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004484 LY_CHECK_GOTO(ret, error);
4485 (*when)->context = lysc_xpath_context(target);
4486 when_shared = *when;
4487 } else {
4488 ++when_shared->refcount;
4489 (*when) = when_shared;
4490 }
4491 }
4492 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004493
Radek Krejciec4da802019-05-02 13:02:41 +02004494 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004495 switch (target->nodetype) {
4496 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004497 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004498 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004499 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004500 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004501 break;
4502 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004503 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004504 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004505 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004506 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004507 break;
4508 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004509 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004510 if (aug_p->actions) {
4511 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004512 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4513 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004514 return LY_EVALID;
4515 }
4516 if (aug_p->notifs) {
4517 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004518 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4519 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004520 return LY_EVALID;
4521 }
4522 }
Radek Krejci3641f562019-02-13 15:38:40 +01004523
Radek Krejci327de162019-06-14 12:52:07 +02004524 lysc_update_path(ctx, NULL, NULL);
4525 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004526error:
Radek Krejciec4da802019-05-02 13:02:41 +02004527 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004528 return ret;
4529}
4530
4531/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004532 * @brief Apply refined or deviated mandatory flag to the target node.
4533 *
4534 * @param[in] ctx Compile context.
4535 * @param[in] node Target node where the mandatory property is supposed to be changed.
4536 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004537 * @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 +01004538 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4539 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4540 * 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 +01004541 * @return LY_ERR value.
4542 */
4543static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004544lys_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 +01004545{
4546 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4547 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004548 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4549 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004550 return LY_EVALID;
4551 }
4552
4553 if (mandatory_flag & LYS_MAND_TRUE) {
4554 /* check if node has default value */
4555 if (node->nodetype & LYS_LEAF) {
4556 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004557 if (refine_flag) {
4558 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004559 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004560 return LY_EVALID;
4561 }
Radek Krejcia1911222019-07-22 17:24:50 +02004562 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004563 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004564 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004565
4566 /* update the list of incomplete default values if needed */
4567 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4568
Radek Krejcia1911222019-07-22 17:24:50 +02004569 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4570 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4571 free(leaf->dflt);
4572 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004573 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004574 }
4575 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004576 if (refine_flag) {
4577 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004578 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004579 return LY_EVALID;
4580 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004581 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004582 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004583 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 +01004584 return LY_EVALID;
4585 }
4586
4587 node->flags &= ~LYS_MAND_FALSE;
4588 node->flags |= LYS_MAND_TRUE;
4589 lys_compile_mandatory_parents(node->parent, 1);
4590 } else {
4591 /* make mandatory false */
4592 node->flags &= ~LYS_MAND_TRUE;
4593 node->flags |= LYS_MAND_FALSE;
4594 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004595 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 +01004596 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004597 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004598 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004599 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4600 leaf->dflt->realtype = leaf->type->dflt->realtype;
4601 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4602 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004603 }
4604 }
4605 return LY_SUCCESS;
4606}
4607
4608/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004609 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4610 * If present, also apply uses's modificators.
4611 *
4612 * @param[in] ctx Compile context
4613 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004614 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4615 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4616 * the compile context.
4617 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4618 */
4619static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004620lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004621{
4622 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004623 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004624 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004625 struct lysc_node_container context_node_fake =
4626 {.nodetype = LYS_CONTAINER,
4627 .module = ctx->mod,
4628 .flags = parent ? parent->flags : 0,
4629 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004630 .prev = (struct lysc_node*)&context_node_fake,
4631 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004632 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004633 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004634 int found;
4635 const char *id, *name, *prefix;
4636 size_t prefix_len, name_len;
4637 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004638 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004639 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004640 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004641 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004642 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004643 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004644 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004645 unsigned int actions_index, notifs_index;
4646 struct lysc_notif **notifs = NULL;
4647 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004648
4649 /* search for the grouping definition */
4650 found = 0;
4651 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004652 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004653 if (prefix) {
4654 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4655 if (!mod) {
4656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004657 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004658 return LY_EVALID;
4659 }
4660 } else {
4661 mod = ctx->mod_def;
4662 }
4663 if (mod == ctx->mod_def) {
4664 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004665 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004666 LY_ARRAY_FOR(grp, u) {
4667 if (!strcmp(grp[u].name, name)) {
4668 grp = &grp[u];
4669 found = 1;
4670 break;
4671 }
4672 }
4673 }
4674 }
4675 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004676 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004677 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004678 if (grp) {
4679 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4680 if (!strcmp(grp[u].name, name)) {
4681 grp = &grp[u];
4682 found = 1;
4683 }
4684 }
4685 }
4686 if (!found && mod->parsed->includes) {
4687 /* ... and all the submodules */
4688 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4689 grp = mod->parsed->includes[u].submodule->groupings;
4690 if (grp) {
4691 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4692 if (!strcmp(grp[v].name, name)) {
4693 grp = &grp[v];
4694 found = 1;
4695 }
4696 }
4697 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004698 }
4699 }
4700 }
4701 if (!found) {
4702 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4703 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4704 return LY_EVALID;
4705 }
4706
4707 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4708 grp_stack_count = ctx->groupings.count;
4709 ly_set_add(&ctx->groupings, (void*)grp, 0);
4710 if (grp_stack_count == ctx->groupings.count) {
4711 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4712 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4713 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4714 return LY_EVALID;
4715 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004716 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4717 /* remember that the grouping is instantiated to avoid its standalone validation */
4718 grp->flags |= LYS_USED_GRP;
4719 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004720
4721 /* switch context's mod_def */
4722 mod_old = ctx->mod_def;
4723 ctx->mod_def = mod;
4724
4725 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004726 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 +01004727
Radek Krejcifc11bd72019-04-11 16:00:05 +02004728 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004729 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004730 /* 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 +02004731 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 +01004732
4733 /* some preparation for applying refines */
4734 if (grp->data == node_p) {
4735 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004736 if (parent) {
4737 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4738 } else if (ctx->mod->compiled->data) {
4739 child = ctx->mod->compiled->data;
4740 } else {
4741 child = NULL;
4742 }
4743 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004744 }
4745 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004746 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004747 LY_LIST_FOR(context_node_fake.child, child) {
4748 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004749
Radek Krejcifc11bd72019-04-11 16:00:05 +02004750 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004751 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004752 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004753 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004754 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004755 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004756 when_shared = *when;
4757 } else {
4758 ++when_shared->refcount;
4759 (*when) = when_shared;
4760 }
4761 }
Radek Krejci01342af2019-01-03 15:18:08 +01004762 }
4763 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004764 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004765 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004766 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004767 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 +01004768 }
4769
Radek Krejcifc11bd72019-04-11 16:00:05 +02004770 /* compile actions */
4771 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4772 if (actions) {
4773 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004774 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004775 if (*actions && (uses_p->augments || uses_p->refines)) {
4776 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4777 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4778 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4779 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4780 }
4781 }
4782
4783 /* compile notifications */
4784 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4785 if (notifs) {
4786 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004787 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004788 if (*notifs && (uses_p->augments || uses_p->refines)) {
4789 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4790 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4791 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4792 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4793 }
4794 }
4795
4796
Radek Krejci3641f562019-02-13 15:38:40 +01004797 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004798 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004799 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004800 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004801 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004802
Radek Krejcif0089082019-01-07 16:42:01 +01004803 /* reload previous context's mod_def */
4804 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004805 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004806
Radek Krejci76b3e962018-12-14 17:01:25 +01004807 /* apply refine */
4808 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004809 lysc_update_path(ctx, NULL, rfn->nodeid);
4810
Radek Krejci7af64242019-02-18 13:07:53 +01004811 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 +01004812 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004813 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004814 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004815
4816 /* default value */
4817 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004818 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004819 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004820 "Invalid refine of default - %s cannot hold %d default values.",
4821 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004822 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004823 }
4824 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4825 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004826 "Invalid refine of default - %s cannot hold default value(s).",
4827 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004828 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004829 }
4830 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004831 struct ly_err_item *err = NULL;
4832 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4833 if (leaf->dflt) {
4834 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004835 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004836 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4837 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4838 } else {
4839 /* prepare a new one */
4840 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4841 leaf->dflt->realtype = leaf->type;
4842 }
4843 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004844 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004845 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4846 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004847 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004848 leaf->dflt->realtype->refcount++;
4849 if (err) {
4850 ly_err_print(err);
4851 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4852 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4853 ly_err_free(err);
4854 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004855 if (rc == LY_EINCOMPLETE) {
4856 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004857 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004858
4859 /* but in general result is so far ok */
4860 rc = LY_SUCCESS;
4861 }
Radek Krejcia1911222019-07-22 17:24:50 +02004862 LY_CHECK_GOTO(rc, cleanup);
4863 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004864 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004865 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4866
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004867 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004868 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4869 "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 +02004870 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004871 }
Radek Krejcia1911222019-07-22 17:24:50 +02004872
4873 /* remove previous set of default values */
4874 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004875 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004876 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4877 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4878 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004879 }
Radek Krejcia1911222019-07-22 17:24:50 +02004880 LY_ARRAY_FREE(llist->dflts);
4881 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004882 LY_ARRAY_FREE(llist->dflts_mods);
4883 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004884
4885 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004886 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02004887 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004888 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004889 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004890 LY_ARRAY_INCREMENT(llist->dflts_mods);
4891 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004892 LY_ARRAY_INCREMENT(llist->dflts);
4893 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4894 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004895 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004896 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004897 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004898 llist->dflts[u]->realtype->refcount++;
4899 if (err) {
4900 ly_err_print(err);
4901 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4902 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4903 ly_err_free(err);
4904 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004905 if (rc == LY_EINCOMPLETE) {
4906 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004907 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004908
4909 /* but in general result is so far ok */
4910 rc = LY_SUCCESS;
4911 }
4912 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004913 }
Radek Krejcia1911222019-07-22 17:24:50 +02004914 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004915 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004916 if (((struct lysc_node_choice*)node)->dflt) {
4917 /* unset LYS_SET_DFLT from the current default case */
4918 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4919 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004920 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 +01004921 }
4922 }
4923
Radek Krejci12fb9142019-01-08 09:45:30 +01004924 /* description */
4925 if (rfn->dsc) {
4926 FREE_STRING(ctx->ctx, node->dsc);
4927 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4928 }
4929
4930 /* reference */
4931 if (rfn->ref) {
4932 FREE_STRING(ctx->ctx, node->ref);
4933 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4934 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004935
4936 /* config */
4937 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004938 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004939 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004940 } else {
4941 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4942 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4943 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004944 }
4945
4946 /* mandatory */
4947 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004948 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004949 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004950
4951 /* presence */
4952 if (rfn->presence) {
4953 if (node->nodetype != LYS_CONTAINER) {
4954 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004955 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4956 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004957 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004958 }
4959 node->flags |= LYS_PRESENCE;
4960 }
Radek Krejci9a564c92019-01-07 14:53:57 +01004961
4962 /* must */
4963 if (rfn->musts) {
4964 switch (node->nodetype) {
4965 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02004966 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 +01004967 break;
4968 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004969 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 +01004970 break;
4971 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02004972 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 +01004973 break;
4974 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02004975 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 +01004976 break;
4977 case LYS_ANYXML:
4978 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02004979 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 +01004980 break;
4981 default:
4982 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004983 "Invalid refine of must statement - %s cannot hold any must statement.",
4984 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004985 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01004986 }
4987 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01004988
4989 /* min/max-elements */
4990 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
4991 switch (node->nodetype) {
4992 case LYS_LEAFLIST:
4993 if (rfn->flags & LYS_SET_MAX) {
4994 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
4995 }
4996 if (rfn->flags & LYS_SET_MIN) {
4997 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01004998 if (rfn->min) {
4999 node->flags |= LYS_MAND_TRUE;
5000 lys_compile_mandatory_parents(node->parent, 1);
5001 } else {
5002 node->flags &= ~LYS_MAND_TRUE;
5003 lys_compile_mandatory_parents(node->parent, 0);
5004 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005005 }
5006 break;
5007 case LYS_LIST:
5008 if (rfn->flags & LYS_SET_MAX) {
5009 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5010 }
5011 if (rfn->flags & LYS_SET_MIN) {
5012 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005013 if (rfn->min) {
5014 node->flags |= LYS_MAND_TRUE;
5015 lys_compile_mandatory_parents(node->parent, 1);
5016 } else {
5017 node->flags &= ~LYS_MAND_TRUE;
5018 lys_compile_mandatory_parents(node->parent, 0);
5019 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005020 }
5021 break;
5022 default:
5023 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005024 "Invalid refine of %s statement - %s cannot hold this statement.",
5025 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005026 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005027 }
5028 }
Radek Krejcif0089082019-01-07 16:42:01 +01005029
5030 /* if-feature */
5031 if (rfn->iffeatures) {
5032 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005033 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005034 }
Radek Krejci327de162019-06-14 12:52:07 +02005035
5036 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005037 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005038
Radek Krejcif2271f12019-01-07 16:42:23 +01005039 /* do some additional checks of the changed nodes when all the refines are applied */
5040 for (u = 0; u < refined.count; ++u) {
5041 node = (struct lysc_node*)refined.objs[u];
5042 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005043 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005044
5045 /* check possible conflict with default value (default added, mandatory left true) */
5046 if ((node->flags & LYS_MAND_TRUE) &&
5047 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5048 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5049 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005050 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005051 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005052 }
5053
5054 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5055 if (node->nodetype == LYS_LIST) {
5056 min = ((struct lysc_node_list*)node)->min;
5057 max = ((struct lysc_node_list*)node)->max;
5058 } else {
5059 min = ((struct lysc_node_leaflist*)node)->min;
5060 max = ((struct lysc_node_leaflist*)node)->max;
5061 }
5062 if (min > max) {
5063 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005064 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5065 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005066 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005067 }
5068 }
5069 }
5070
Radek Krejci327de162019-06-14 12:52:07 +02005071 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005072 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005073
5074cleanup:
5075 /* fix connection of the children nodes from fake context node back into the parent */
5076 if (context_node_fake.child) {
5077 context_node_fake.child->prev = child;
5078 }
5079 LY_LIST_FOR(context_node_fake.child, child) {
5080 child->parent = parent;
5081 }
5082
5083 if (uses_p->augments || uses_p->refines) {
5084 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005085 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005086 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5087 LY_ARRAY_FREE(context_node_fake.actions);
5088 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005089 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005090 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5091 LY_ARRAY_FREE(context_node_fake.notifs);
5092 }
5093 }
5094
Radek Krejcie86bf772018-12-14 11:39:53 +01005095 /* reload previous context's mod_def */
5096 ctx->mod_def = mod_old;
5097 /* remove the grouping from the stack for circular groupings dependency check */
5098 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5099 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005100 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005101 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005102
5103 return ret;
5104}
5105
Radek Krejci327de162019-06-14 12:52:07 +02005106static int
5107lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5108{
5109 struct lysp_node *iter;
5110 int len = 0;
5111
5112 *path = NULL;
5113 for (iter = node; iter && len >= 0; iter = iter->parent) {
5114 char *s = *path;
5115 char *id;
5116
5117 switch (iter->nodetype) {
5118 case LYS_USES:
5119 asprintf(&id, "{uses='%s'}", iter->name);
5120 break;
5121 case LYS_GROUPING:
5122 asprintf(&id, "{grouping='%s'}", iter->name);
5123 break;
5124 case LYS_AUGMENT:
5125 asprintf(&id, "{augment='%s'}", iter->name);
5126 break;
5127 default:
5128 id = strdup(iter->name);
5129 break;
5130 }
5131
5132 if (!iter->parent) {
5133 /* print prefix */
5134 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5135 } else {
5136 /* prefix is the same as in parent */
5137 len = asprintf(path, "/%s%s", id, s ? s : "");
5138 }
5139 free(s);
5140 free(id);
5141 }
5142
5143 if (len < 0) {
5144 free(*path);
5145 *path = NULL;
5146 } else if (len == 0) {
5147 *path = strdup("/");
5148 len = 1;
5149 }
5150 return len;
5151}
5152
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005153/**
5154 * @brief Validate groupings that were defined but not directly used in the schema itself.
5155 *
5156 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5157 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5158 */
5159static LY_ERR
5160lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5161{
5162 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005163 char *path;
5164 int len;
5165
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005166 struct lysp_node_uses fake_uses = {
5167 .parent = node_p,
5168 .nodetype = LYS_USES,
5169 .flags = 0, .next = NULL,
5170 .name = grp->name,
5171 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5172 .refines = NULL, .augments = NULL
5173 };
5174 struct lysc_node_container fake_container = {
5175 .nodetype = LYS_CONTAINER,
5176 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5177 .module = ctx->mod,
5178 .sp = NULL, .parent = NULL, .next = NULL,
5179 .prev = (struct lysc_node*)&fake_container,
5180 .name = "fake",
5181 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5182 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5183 };
5184
5185 if (grp->parent) {
5186 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5187 }
Radek Krejci327de162019-06-14 12:52:07 +02005188
5189 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5190 if (len < 0) {
5191 LOGMEM(ctx->ctx);
5192 return LY_EMEM;
5193 }
5194 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5195 ctx->path_len = (uint16_t)len;
5196 free(path);
5197
5198 lysc_update_path(ctx, NULL, "{grouping}");
5199 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005200 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005201 lysc_update_path(ctx, NULL, NULL);
5202 lysc_update_path(ctx, NULL, NULL);
5203
5204 ctx->path_len = 1;
5205 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005206
5207 /* cleanup */
5208 lysc_node_container_free(ctx->ctx, &fake_container);
5209
5210 return ret;
5211}
Radek Krejcife909632019-02-12 15:34:42 +01005212
Radek Krejcie86bf772018-12-14 11:39:53 +01005213/**
Radek Krejcia3045382018-11-22 14:30:31 +01005214 * @brief Compile parsed schema node information.
5215 * @param[in] ctx Compile context
5216 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005217 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5218 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5219 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005220 * @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).
5221 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005222 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5223 */
Radek Krejci19a96102018-11-15 13:38:09 +01005224static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005225lys_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 +01005226{
5227 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005228 struct lysc_node *node;
5229 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005230 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005231 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005232 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005233
Radek Krejci327de162019-06-14 12:52:07 +02005234 if (node_p->nodetype != LYS_USES) {
5235 lysc_update_path(ctx, parent, node_p->name);
5236 } else {
5237 lysc_update_path(ctx, NULL, "{uses}");
5238 lysc_update_path(ctx, NULL, node_p->name);
5239 }
5240
Radek Krejci19a96102018-11-15 13:38:09 +01005241 switch (node_p->nodetype) {
5242 case LYS_CONTAINER:
5243 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5244 node_compile_spec = lys_compile_node_container;
5245 break;
5246 case LYS_LEAF:
5247 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5248 node_compile_spec = lys_compile_node_leaf;
5249 break;
5250 case LYS_LIST:
5251 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005252 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005253 break;
5254 case LYS_LEAFLIST:
5255 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005256 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005257 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005258 case LYS_CHOICE:
5259 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005260 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005261 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005262 case LYS_ANYXML:
5263 case LYS_ANYDATA:
5264 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005265 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005266 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005267 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005268 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5269 lysc_update_path(ctx, NULL, NULL);
5270 lysc_update_path(ctx, NULL, NULL);
5271 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005272 default:
5273 LOGINT(ctx->ctx);
5274 return LY_EINT;
5275 }
5276 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5277 node->nodetype = node_p->nodetype;
5278 node->module = ctx->mod;
5279 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005280 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005281
5282 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005283 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005284 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005285 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005286 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5287 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005288 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005289 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005290 node->flags |= LYS_CONFIG_R;
5291 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005292 /* config not explicitely set, inherit it from parent */
5293 if (parent) {
5294 node->flags |= parent->flags & LYS_CONFIG_MASK;
5295 } else {
5296 /* default is config true */
5297 node->flags |= LYS_CONFIG_W;
5298 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005299 } else {
5300 /* config set explicitely */
5301 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005302 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005303 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5305 "Configuration node cannot be child of any state data node.");
5306 goto error;
5307 }
Radek Krejci19a96102018-11-15 13:38:09 +01005308
Radek Krejcia6d57732018-11-29 13:40:37 +01005309 /* *list ordering */
5310 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5311 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005312 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005313 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5314 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005315 node->flags &= ~LYS_ORDBY_MASK;
5316 node->flags |= LYS_ORDBY_SYSTEM;
5317 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5318 /* default ordering is system */
5319 node->flags |= LYS_ORDBY_SYSTEM;
5320 }
5321 }
5322
Radek Krejci19a96102018-11-15 13:38:09 +01005323 /* status - it is not inherited by specification, but it does not make sense to have
5324 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005325 if (!parent || parent->nodetype != LYS_CHOICE) {
5326 /* in case of choice/case's children, postpone the check to the moment we know if
5327 * 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 +01005328 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 +01005329 }
5330
Radek Krejciec4da802019-05-02 13:02:41 +02005331 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005332 node->sp = node_p;
5333 }
5334 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005335 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5336 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005337 if (node_p->when) {
5338 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005339 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005340 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005341 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005342 }
Radek Krejciec4da802019-05-02 13:02:41 +02005343 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
5344 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005345
5346 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005347 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005348
Radek Krejcife909632019-02-12 15:34:42 +01005349 /* inherit LYS_MAND_TRUE in parent containers */
5350 if (node->flags & LYS_MAND_TRUE) {
5351 lys_compile_mandatory_parents(parent, 1);
5352 }
5353
Radek Krejci327de162019-06-14 12:52:07 +02005354 lysc_update_path(ctx, NULL, NULL);
5355
Radek Krejci19a96102018-11-15 13:38:09 +01005356 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005357 if (parent) {
5358 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005359 if (node_p->parent->nodetype == LYS_CASE) {
5360 lysc_update_path(ctx, parent, node_p->parent->name);
5361 } else {
5362 lysc_update_path(ctx, parent, node->name);
5363 }
Radek Krejciec4da802019-05-02 13:02:41 +02005364 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005365 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005366 if (uses_status) {
5367
5368 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005369 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005370 * it directly gets the same status flags as the choice;
5371 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005372 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005373 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005374 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005375 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005376 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005377 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005378 }
Radek Krejciec4da802019-05-02 13:02:41 +02005379 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 +02005380
5381 if (parent->nodetype == LYS_CHOICE) {
5382 lysc_update_path(ctx, NULL, NULL);
5383 }
Radek Krejci19a96102018-11-15 13:38:09 +01005384 } else {
5385 /* top-level element */
5386 if (!ctx->mod->compiled->data) {
5387 ctx->mod->compiled->data = node;
5388 } else {
5389 /* insert at the end of the module's top-level nodes list */
5390 ctx->mod->compiled->data->prev->next = node;
5391 node->prev = ctx->mod->compiled->data->prev;
5392 ctx->mod->compiled->data->prev = node;
5393 }
Radek Krejci327de162019-06-14 12:52:07 +02005394 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005395 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5396 ctx->mod->compiled->notifs, node->name, node)) {
5397 return LY_EVALID;
5398 }
Radek Krejci19a96102018-11-15 13:38:09 +01005399 }
Radek Krejci327de162019-06-14 12:52:07 +02005400 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005401
5402 return LY_SUCCESS;
5403
5404error:
5405 lysc_node_free(ctx->ctx, node);
5406 return ret;
5407}
5408
Radek Krejciccd20f12019-02-15 14:12:27 +01005409static void
5410lysc_disconnect(struct lysc_node *node)
5411{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005412 struct lysc_node *parent, *child, *prev = NULL, *next;
5413 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005414 int remove_cs = 0;
5415
5416 parent = node->parent;
5417
5418 /* parent's first child */
5419 if (!parent) {
5420 return;
5421 }
5422 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005423 cs = (struct lysc_node_case*)node;
5424 } else if (parent->nodetype == LYS_CASE) {
5425 /* disconnecting some node in a case */
5426 cs = (struct lysc_node_case*)parent;
5427 parent = cs->parent;
5428 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5429 if (child == node) {
5430 if (cs->child == child) {
5431 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5432 /* case with a single child -> remove also the case */
5433 child->parent = NULL;
5434 remove_cs = 1;
5435 } else {
5436 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005437 }
5438 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005439 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005440 }
5441 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005442 if (!remove_cs) {
5443 cs = NULL;
5444 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005445 } else if (lysc_node_children(parent, node->flags) == node) {
5446 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005447 }
5448
5449 if (cs) {
5450 if (remove_cs) {
5451 /* cs has only one child which is being also removed */
5452 lysc_disconnect((struct lysc_node*)cs);
5453 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5454 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005455 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5456 /* default case removed */
5457 ((struct lysc_node_choice*)parent)->dflt = NULL;
5458 }
5459 if (((struct lysc_node_choice*)parent)->cases == cs) {
5460 /* first case removed */
5461 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5462 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005463 if (cs->child) {
5464 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5465 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5466 prev = cs->child->prev;
5467 } /* else all the children are under a single case */
5468 LY_LIST_FOR_SAFE(cs->child, next, child) {
5469 if (child->parent != (struct lysc_node*)cs) {
5470 break;
5471 }
5472 lysc_node_free(node->module->ctx, child);
5473 }
5474 if (prev) {
5475 if (prev->next) {
5476 prev->next = child;
5477 }
5478 if (child) {
5479 child->prev = prev;
5480 } else {
5481 /* link from the first child under the cases */
5482 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5483 }
5484 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005485 }
5486 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005487 }
5488
5489 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005490 if (node->prev->next) {
5491 node->prev->next = node->next;
5492 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005493 if (node->next) {
5494 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005495 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005496 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005497 if (child) {
5498 child->prev = node->prev;
5499 }
5500 } else if (((struct lysc_node_choice*)parent)->cases) {
5501 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005502 }
5503}
5504
5505LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005506lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005507{
Radek Krejcia1911222019-07-22 17:24:50 +02005508 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005509 struct ly_set devs_p = {0};
5510 struct ly_set targets = {0};
5511 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005512 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005513 struct lysc_action *rpcs;
5514 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005515 struct lysp_deviation *dev;
5516 struct lysp_deviate *d, **dp_new;
5517 struct lysp_deviate_add *d_add;
5518 struct lysp_deviate_del *d_del;
5519 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005520 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005521 struct lysc_deviation {
5522 const char *nodeid;
5523 struct lysc_node *target; /* target node of the deviation */
5524 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005525 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005526 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5527 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005528 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005529 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005530 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005531 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005532 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005533 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005534
5535 /* get all deviations from the module and all its submodules ... */
5536 LY_ARRAY_FOR(mod_p->deviations, u) {
5537 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5538 }
5539 LY_ARRAY_FOR(mod_p->includes, v) {
5540 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5541 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5542 }
5543 }
5544 if (!devs_p.count) {
5545 /* nothing to do */
5546 return LY_SUCCESS;
5547 }
5548
Radek Krejci327de162019-06-14 12:52:07 +02005549 lysc_update_path(ctx, NULL, "{deviation}");
5550
Radek Krejciccd20f12019-02-15 14:12:27 +01005551 /* ... and group them by the target node */
5552 devs = calloc(devs_p.count, sizeof *devs);
5553 for (u = 0; u < devs_p.count; ++u) {
5554 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005555 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005556
5557 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005558 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5559 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005560 if (target->nodetype == LYS_ACTION) {
5561 /* move the target pointer to input/output to make them different from the action and
5562 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5563 * back to the RPC/action node due to a better compatibility and decision code in this function.
5564 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5565 if (flags & LYSC_OPT_RPC_INPUT) {
5566 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5567 flags |= LYSC_OPT_INTERNAL;
5568 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5569 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5570 flags |= LYSC_OPT_INTERNAL;
5571 }
5572 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005573 /* insert into the set of targets with duplicity detection */
5574 i = ly_set_add(&targets, target, 0);
5575 if (!devs[i]) {
5576 /* new record */
5577 devs[i] = calloc(1, sizeof **devs);
5578 devs[i]->target = target;
5579 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005580 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005581 }
5582 /* add deviates into the deviation's list of deviates */
5583 for (d = dev->deviates; d; d = d->next) {
5584 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5585 *dp_new = d;
5586 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5587 devs[i]->not_supported = 1;
5588 }
5589 }
Radek Krejci327de162019-06-14 12:52:07 +02005590
5591 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005592 }
5593
5594 /* MACROS for deviates checking */
5595#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5596 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005597 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 +01005598 goto cleanup; \
5599 }
5600
5601#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5602 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005603 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5604 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005605 goto cleanup; \
5606 }
5607
Radek Krejcia1911222019-07-22 17:24:50 +02005608
Radek Krejciccd20f12019-02-15 14:12:27 +01005609#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005610 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5611 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5612 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5613 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5614 goto cleanup; \
5615 }
5616
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005617#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005618 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005619 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005620 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5621 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005622 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005623 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5624 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005625 goto cleanup; \
5626 }
5627
Radek Krejci551b12c2019-02-19 16:11:21 +01005628#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5629 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5630 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005631 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5632 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005633 goto cleanup; \
5634 }
5635
Radek Krejciccd20f12019-02-15 14:12:27 +01005636#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5637 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005638 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005639 goto cleanup; \
5640 }
5641
Radek Krejci551b12c2019-02-19 16:11:21 +01005642#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5643 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005645 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005646 goto cleanup; \
5647 }
5648
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005649#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5650 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5651 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5652 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5653 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 +01005654 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005655 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005656 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005657 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5658 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005659 goto cleanup; \
5660 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005661 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5662 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5663 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5664 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5665 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005666 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005667 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5668 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5669 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005670 }
5671
5672 /* apply deviations */
5673 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005674 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5675 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5676 struct ly_err_item *err = NULL;
5677
5678 dflt = NULL;
5679 changed_type = 0;
5680
Radek Krejci327de162019-06-14 12:52:07 +02005681 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5682
Radek Krejcif538ce52019-03-05 10:46:14 +01005683 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5684 /* fix the target pointer in case of RPC's/action's input/output */
5685 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5686 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5687 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5688 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5689 }
5690 }
5691
Radek Krejciccd20f12019-02-15 14:12:27 +01005692 /* not-supported */
5693 if (devs[u]->not_supported) {
5694 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5695 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5696 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5697 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005698
5699#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5700 if (devs[u]->target->parent) { \
5701 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5702 } else { \
5703 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5704 } \
5705 LY_ARRAY_FOR(ARRAY, x) { \
5706 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5707 } \
5708 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5709 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5710 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5711 LY_ARRAY_DECREMENT(ARRAY); \
5712 }
5713
Radek Krejcif538ce52019-03-05 10:46:14 +01005714 if (devs[u]->target->nodetype == LYS_ACTION) {
5715 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5716 /* remove RPC's/action's input */
5717 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5718 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005719 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5720 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005721 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5722 /* remove RPC's/action's output */
5723 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5724 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005725 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5726 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005727 } else {
5728 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005729 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005730 }
5731 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005732 /* remove Notification */
5733 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005734 } else {
5735 /* remove the target node */
5736 lysc_disconnect(devs[u]->target);
5737 lysc_node_free(ctx->ctx, devs[u]->target);
5738 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005739
Radek Krejci474f9b82019-07-24 11:36:37 +02005740 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5741 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005742 continue;
5743 }
5744
5745 /* list of deviates (not-supported is not present in the list) */
5746 LY_ARRAY_FOR(devs[u]->deviates, v) {
5747 d = devs[u]->deviates[v];
5748
5749 switch (d->mod) {
5750 case LYS_DEV_ADD:
5751 d_add = (struct lysp_deviate_add*)d;
5752 /* [units-stmt] */
5753 if (d_add->units) {
5754 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5755 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5756
5757 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5758 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5759 }
5760
5761 /* *must-stmt */
5762 if (d_add->musts) {
5763 switch (devs[u]->target->nodetype) {
5764 case LYS_CONTAINER:
5765 case LYS_LIST:
5766 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005767 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005768 break;
5769 case LYS_LEAF:
5770 case LYS_LEAFLIST:
5771 case LYS_ANYDATA:
5772 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005773 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005774 break;
5775 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005776 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->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 case LYS_ACTION:
5780 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5781 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005782 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005783 break;
5784 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5785 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005786 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005787 break;
5788 }
5789 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005790 default:
5791 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005792 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005793 goto cleanup;
5794 }
5795 }
5796
5797 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005798 if (d_add->uniques) {
5799 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5800 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5801 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005802
5803 /* *default-stmt */
5804 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005805 switch (devs[u]->target->nodetype) {
5806 case LYS_LEAF:
5807 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005808 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 +02005809 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005810 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005811 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005812 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5813 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5814 } else {
5815 /* prepare new default value storage */
5816 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005817 }
Radek Krejcia1911222019-07-22 17:24:50 +02005818 dflt = d_add->dflts[0];
5819 /* parsing is done at the end after possible replace of the leaf's type */
5820
Radek Krejci551b12c2019-02-19 16:11:21 +01005821 /* mark the new default values as leaf's own */
5822 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005823 break;
5824 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005825 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005826 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005827 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005828 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005829 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5830 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5831 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005832 }
Radek Krejcia1911222019-07-22 17:24:50 +02005833 LY_ARRAY_FREE(llist->dflts);
5834 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005835 LY_ARRAY_FREE(llist->dflts_mods);
5836 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005837 }
5838 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005839 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005840 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5841 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01005842 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005843 LY_ARRAY_INCREMENT(llist->dflts_mods);
5844 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005845 LY_ARRAY_INCREMENT(llist->dflts);
5846 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5847 llist->dflts[x]->realtype = llist->type;
5848 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 +02005849 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005850 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005851 llist->dflts[x]->realtype->refcount++;
5852 if (err) {
5853 ly_err_print(err);
5854 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5855 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5856 d_add->dflts[x - y], err->msg);
5857 ly_err_free(err);
5858 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005859 if (rc == LY_EINCOMPLETE) {
5860 /* postpone default compilation when the tree is complete */
5861 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5862
5863 /* but in general result is so far ok */
5864 rc = LY_SUCCESS;
5865 }
Radek Krejcia1911222019-07-22 17:24:50 +02005866 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005867 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005868 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005869 devs[u]->target->flags |= LYS_SET_DFLT;
5870 break;
5871 case LYS_CHOICE:
5872 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5873 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5874 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5875 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005876 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 +01005877 goto cleanup;
5878 }
5879 break;
5880 default:
5881 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005882 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005883 goto cleanup;
5884 }
5885 }
5886
5887 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005888 if (d_add->flags & LYS_CONFIG_MASK) {
5889 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005890 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005891 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5892 goto cleanup;
5893 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005894 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005895 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5896 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005897 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005898 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5899 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005900 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5901 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005902 goto cleanup;
5903 }
Radek Krejci327de162019-06-14 12:52:07 +02005904 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005905 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005906
5907 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005908 if (d_add->flags & LYS_MAND_MASK) {
5909 if (devs[u]->target->flags & LYS_MAND_MASK) {
5910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005911 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5912 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005913 goto cleanup;
5914 }
Radek Krejci327de162019-06-14 12:52:07 +02005915 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005916 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005917
5918 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005919 if (d_add->flags & LYS_SET_MIN) {
5920 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5921 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5922 /* change value */
5923 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5924 } else if (devs[u]->target->nodetype == LYS_LIST) {
5925 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5926 /* change value */
5927 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5928 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005929 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005930 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5931 goto cleanup;
5932 }
5933 if (d_add->min) {
5934 devs[u]->target->flags |= LYS_MAND_TRUE;
5935 }
5936 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005937
5938 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005939 if (d_add->flags & LYS_SET_MAX) {
5940 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5941 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5942 /* change value */
5943 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5944 } else if (devs[u]->target->nodetype == LYS_LIST) {
5945 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5946 /* change value */
5947 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5948 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005949 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005950 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5951 goto cleanup;
5952 }
5953 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005954
5955 break;
5956 case LYS_DEV_DELETE:
5957 d_del = (struct lysp_deviate_del*)d;
5958
5959 /* [units-stmt] */
5960 if (d_del->units) {
5961 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02005962 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
5963 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
5964 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
5965 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
5966 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5967 goto cleanup;
5968 }
5969 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5970 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005971 }
5972
5973 /* *must-stmt */
5974 if (d_del->musts) {
5975 switch (devs[u]->target->nodetype) {
5976 case LYS_CONTAINER:
5977 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005978 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005979 break;
5980 case LYS_LEAF:
5981 case LYS_LEAFLIST:
5982 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005983 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005984 break;
5985 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005986 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005987 break;
5988 case LYS_ACTION:
5989 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5990 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5991 break;
5992 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5993 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
5994 break;
5995 }
5996 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005997 default:
5998 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005999 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006000 goto cleanup;
6001 }
6002 }
6003
6004 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006005 if (d_del->uniques) {
6006 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6007 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6008 LY_ARRAY_FOR(d_del->uniques, x) {
6009 LY_ARRAY_FOR(list->uniques, z) {
6010 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6011 nodeid = strpbrk(name, " \t\n");
6012 if (nodeid) {
6013 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
6014 || list->uniques[z][y]->name[nodeid - name] != '\0') {
6015 break;
6016 }
6017 while (isspace(*nodeid)) {
6018 ++nodeid;
6019 }
6020 } else {
6021 if (strcmp(name, list->uniques[z][y]->name)) {
6022 break;
6023 }
6024 }
6025 }
6026 if (!name) {
6027 /* complete match - remove the unique */
6028 LY_ARRAY_DECREMENT(list->uniques);
6029 LY_ARRAY_FREE(list->uniques[z]);
6030 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6031 --z;
6032 break;
6033 }
6034 }
6035 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6036 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006037 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6038 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006039 goto cleanup;
6040 }
6041 }
6042 if (!LY_ARRAY_SIZE(list->uniques)) {
6043 LY_ARRAY_FREE(list->uniques);
6044 list->uniques = NULL;
6045 }
6046 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006047
6048 /* *default-stmt */
6049 if (d_del->dflts) {
6050 switch (devs[u]->target->nodetype) {
6051 case LYS_LEAF:
6052 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6053 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6054 dflt, "deleting", "default", d_del->dflts[0]);
6055
Radek Krejcia1911222019-07-22 17:24:50 +02006056 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006057 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006058 if (strcmp(dflt, d_del->dflts[0])) {
6059 if (i) {
6060 free((char*)dflt);
6061 }
6062 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6063 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6064 d_del->dflts[0], dflt);
6065 goto cleanup;
6066 }
6067 if (i) {
6068 free((char*)dflt);
6069 }
6070 dflt = NULL;
6071
Radek Krejci474f9b82019-07-24 11:36:37 +02006072 /* update the list of incomplete default values if needed */
6073 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6074
Radek Krejcia1911222019-07-22 17:24:50 +02006075 /* remove the default specification */
6076 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6077 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6078 free(leaf->dflt);
6079 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006080 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006081 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006082 break;
6083 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006084 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6085 LY_ARRAY_FOR(d_del->dflts, x) {
6086 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006087 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 +02006088 if (!strcmp(dflt, d_del->dflts[x])) {
6089 if (i) {
6090 free((char*)dflt);
6091 }
6092 dflt = NULL;
6093 break;
6094 }
6095 if (i) {
6096 free((char*)dflt);
6097 }
6098 dflt = NULL;
6099 }
6100 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6101 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6102 "which does not match any of the target's property values.", d_del->dflts[x]);
6103 goto cleanup;
6104 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006105
6106 /* update the list of incomplete default values if needed */
6107 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6108
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006109 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006110 LY_ARRAY_DECREMENT(llist->dflts);
6111 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6112 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6113 free(llist->dflts[y]);
6114 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006115 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 +02006116 }
6117 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006118 LY_ARRAY_FREE(llist->dflts_mods);
6119 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006120 LY_ARRAY_FREE(llist->dflts);
6121 llist->dflts = NULL;
6122 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006123 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006124 break;
6125 case LYS_CHOICE:
6126 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6127 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6128 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006129 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006130 if (prefix) {
6131 /* use module prefixes from the deviation module to match the module of the default case */
6132 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6133 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006134 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6135 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006136 goto cleanup;
6137 }
6138 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6139 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006140 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6141 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006142 goto cleanup;
6143 }
6144 }
6145 /* else {
6146 * strictly, the default prefix would point to the deviation module, but the value should actually
6147 * match the default string in the original module (usually unprefixed), so in this case we do not check
6148 * the module of the default case, just matching its name */
6149 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6150 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006151 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6152 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006153 goto cleanup;
6154 }
6155 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6156 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6157 break;
6158 default:
6159 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006160 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006161 goto cleanup;
6162 }
6163 }
6164
6165 break;
6166 case LYS_DEV_REPLACE:
6167 d_rpl = (struct lysp_deviate_rpl*)d;
6168
6169 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006170 if (d_rpl->type) {
6171 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6172 /* type is mandatory, so checking for its presence is not necessary */
6173 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006174
6175 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6176 /* the target has default from the previous type - remove it */
6177 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006178 /* update the list of incomplete default values if needed */
6179 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6180
Radek Krejcia1911222019-07-22 17:24:50 +02006181 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6182 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6183 free(leaf->dflt);
6184 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006185 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006186 } else { /* LYS_LEAFLIST */
6187 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006188 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006189 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6190 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6191 free(llist->dflts[x]);
6192 }
6193 LY_ARRAY_FREE(llist->dflts);
6194 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006195 LY_ARRAY_FREE(llist->dflts_mods);
6196 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006197 }
6198 }
6199 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006200 /* there is no default value, do not set changed_type after type compilation
6201 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006202 changed_type = -1;
6203 }
Radek Krejciec4da802019-05-02 13:02:41 +02006204 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 +02006205 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006206 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006207
6208 /* [units-stmt] */
6209 if (d_rpl->units) {
6210 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6211 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6212 units, "replacing", "units", d_rpl->units);
6213
6214 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6215 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6216 }
6217
6218 /* [default-stmt] */
6219 if (d_rpl->dflt) {
6220 switch (devs[u]->target->nodetype) {
6221 case LYS_LEAF:
6222 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6223 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006224 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006225 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006226 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6227 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6228 dflt = d_rpl->dflt;
6229 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006230 break;
6231 case LYS_CHOICE:
6232 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006233 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 +01006234 goto cleanup;
6235 }
6236 break;
6237 default:
6238 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006239 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006240 goto cleanup;
6241 }
6242 }
6243
6244 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006245 if (d_rpl->flags & LYS_CONFIG_MASK) {
6246 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006247 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006248 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6249 goto cleanup;
6250 }
6251 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006252 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006253 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6254 goto cleanup;
6255 }
Radek Krejci327de162019-06-14 12:52:07 +02006256 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006257 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006258
6259 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006260 if (d_rpl->flags & LYS_MAND_MASK) {
6261 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006263 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6264 goto cleanup;
6265 }
Radek Krejci327de162019-06-14 12:52:07 +02006266 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006267 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006268
6269 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006270 if (d_rpl->flags & LYS_SET_MIN) {
6271 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6272 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6273 /* change value */
6274 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6275 } else if (devs[u]->target->nodetype == LYS_LIST) {
6276 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6277 /* change value */
6278 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6279 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006281 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6282 goto cleanup;
6283 }
6284 if (d_rpl->min) {
6285 devs[u]->target->flags |= LYS_MAND_TRUE;
6286 }
6287 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006288
6289 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006290 if (d_rpl->flags & LYS_SET_MAX) {
6291 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6292 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6293 /* change value */
6294 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6295 } else if (devs[u]->target->nodetype == LYS_LIST) {
6296 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6297 /* change value */
6298 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6299 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006300 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006301 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6302 goto cleanup;
6303 }
6304 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006305
6306 break;
6307 default:
6308 LOGINT(ctx->ctx);
6309 goto cleanup;
6310 }
6311 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006312
Radek Krejci33f72892019-02-21 10:36:58 +01006313 /* final check when all deviations of a single target node are applied */
6314
Radek Krejci551b12c2019-02-19 16:11:21 +01006315 /* check min-max compatibility */
6316 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6317 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6318 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6319 } else if (devs[u]->target->nodetype == LYS_LIST) {
6320 min = ((struct lysc_node_list*)devs[u]->target)->min;
6321 max = ((struct lysc_node_list*)devs[u]->target)->max;
6322 } else {
6323 min = max = 0;
6324 }
6325 if (min > max) {
6326 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 +02006327 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006328 goto cleanup;
6329 }
6330
Radek Krejcia1911222019-07-22 17:24:50 +02006331 if (dflt) {
6332 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006333 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006334 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006335 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6336 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006337 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006338 leaf->dflt->realtype->refcount++;
6339 if (err) {
6340 ly_err_print(err);
6341 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6342 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6343 ly_err_free(err);
6344 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006345 if (rc == LY_EINCOMPLETE) {
6346 /* postpone default compilation when the tree is complete */
6347 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6348
6349 /* but in general result is so far ok */
6350 rc = LY_SUCCESS;
6351 }
Radek Krejcia1911222019-07-22 17:24:50 +02006352 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006353 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006354 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6355 int dynamic;
6356 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006357 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006358
6359 /* update the list of incomplete default values if needed */
6360 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6361
6362 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006363 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6364 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6365 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006366 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6367 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006368 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006369 leaf->dflt->realtype->refcount++;
6370 if (err) {
6371 ly_err_print(err);
6372 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6373 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6374 ly_err_free(err);
6375 }
6376 if (dynamic) {
6377 free((void*)dflt);
6378 }
Radek Krejcia1911222019-07-22 17:24:50 +02006379 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006380 if (rc == LY_EINCOMPLETE) {
6381 /* postpone default compilation when the tree is complete */
6382 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6383
6384 /* but in general result is so far ok */
6385 rc = LY_SUCCESS;
6386 }
6387 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006388 } else { /* LYS_LEAFLIST */
6389 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006390 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 +02006391 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6392 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6393 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006394 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6395 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006396 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6397 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006398 llist->dflts[x]->realtype->refcount++;
6399 if (err) {
6400 ly_err_print(err);
6401 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6402 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6403 dflt, err->msg);
6404 ly_err_free(err);
6405 }
6406 if (dynamic) {
6407 free((void*)dflt);
6408 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006409 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006410 if (rc == LY_EINCOMPLETE) {
6411 /* postpone default compilation when the tree is complete */
6412 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6413
6414 /* but in general result is so far ok */
6415 rc = LY_SUCCESS;
6416 }
Radek Krejcia1911222019-07-22 17:24:50 +02006417 LY_CHECK_GOTO(rc, cleanup);
6418 }
6419 }
6420 }
6421
Radek Krejci551b12c2019-02-19 16:11:21 +01006422 /* check mandatory - default compatibility */
6423 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6424 && (devs[u]->target->flags & LYS_SET_DFLT)
6425 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6426 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006427 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006428 goto cleanup;
6429 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6430 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6431 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006432 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 +01006433 goto cleanup;
6434 }
6435 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6436 /* mandatory node under a default case */
6437 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006438 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6439 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006440 goto cleanup;
6441 }
Radek Krejci33f72892019-02-21 10:36:58 +01006442
Radek Krejci327de162019-06-14 12:52:07 +02006443 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006444 }
6445
Radek Krejci327de162019-06-14 12:52:07 +02006446 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006447 ret = LY_SUCCESS;
6448
6449cleanup:
6450 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6451 LY_ARRAY_FREE(devs[u]->deviates);
6452 free(devs[u]);
6453 }
6454 free(devs);
6455 ly_set_erase(&targets, NULL);
6456 ly_set_erase(&devs_p, NULL);
6457
6458 return ret;
6459}
6460
Radek Krejcib56c7502019-02-13 14:19:54 +01006461/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006462 * @brief Compile the given YANG submodule into the main module.
6463 * @param[in] ctx Compile context
6464 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006465 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6466 */
6467LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006468lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006469{
6470 unsigned int u;
6471 LY_ERR ret = LY_SUCCESS;
6472 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006473 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006474 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006475 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006476
Radek Krejci0af46292019-01-11 16:02:31 +01006477 if (!mainmod->mod->off_features) {
6478 /* features are compiled directly into the compiled module structure,
6479 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6480 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci327de162019-06-14 12:52:07 +02006481 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01006482 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
6483 LY_CHECK_GOTO(ret, error);
6484 }
6485
Radek Krejci327de162019-06-14 12:52:07 +02006486 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006487 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006488 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006489
Radek Krejci474f9b82019-07-24 11:36:37 +02006490 /* data nodes */
6491 LY_LIST_FOR(submod->data, node_p) {
6492 ret = lys_compile_node(ctx, node_p, NULL, 0);
6493 LY_CHECK_GOTO(ret, error);
6494 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006495
Radek Krejciec4da802019-05-02 13:02:41 +02006496 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6497 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006498
Radek Krejcid05cbd92018-12-05 14:26:40 +01006499error:
6500 return ret;
6501}
6502
Radek Krejci19a96102018-11-15 13:38:09 +01006503LY_ERR
6504lys_compile(struct lys_module *mod, int options)
6505{
6506 struct lysc_ctx ctx = {0};
6507 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006508 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006509 struct lysp_module *sp;
6510 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006511 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006512 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006513 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006514 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006515 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006516
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006517 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006518
6519 if (!mod->implemented) {
6520 /* just imported modules are not compiled */
6521 return LY_SUCCESS;
6522 }
6523
Radek Krejci19a96102018-11-15 13:38:09 +01006524 sp = mod->parsed;
6525
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006526 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006527 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006528 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006529 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006530 ctx.path_len = 1;
6531 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006532
6533 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006534 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6535 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006536
Radek Krejciec4da802019-05-02 13:02:41 +02006537 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006538 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006539 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006540 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6541 }
Radek Krejci0af46292019-01-11 16:02:31 +01006542 if (mod->off_features) {
6543 /* there is already precompiled array of features */
6544 mod_c->features = mod->off_features;
6545 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006546 } else {
6547 /* features are compiled directly into the compiled module structure,
6548 * 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 +02006549 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006550 LY_CHECK_GOTO(ret, error);
6551 }
6552 /* finish feature compilation, not only for the main module, but also for the submodules.
6553 * Due to possible forward references, it must be done when all the features (including submodules)
6554 * are present. */
6555 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006556 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006557 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6558 }
Radek Krejci327de162019-06-14 12:52:07 +02006559 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006560 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006561 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006562 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006563 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006564 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6565 }
Radek Krejci327de162019-06-14 12:52:07 +02006566 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006567 }
Radek Krejci327de162019-06-14 12:52:07 +02006568 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006569
Radek Krejci327de162019-06-14 12:52:07 +02006570 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006571 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006572 if (sp->identities) {
6573 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6574 }
Radek Krejci327de162019-06-14 12:52:07 +02006575 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006576
Radek Krejci95710c92019-02-11 15:49:55 +01006577 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006578 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02006579 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01006580 LY_CHECK_GOTO(ret, error);
6581 }
Radek Krejci95710c92019-02-11 15:49:55 +01006582
Radek Krejciec4da802019-05-02 13:02:41 +02006583 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6584 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006585
Radek Krejci95710c92019-02-11 15:49:55 +01006586 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01006587 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01006588 LY_CHECK_GOTO(ret, error);
6589 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006590 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006591 LY_CHECK_GOTO(ret, error);
6592 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006593
Radek Krejci474f9b82019-07-24 11:36:37 +02006594 /* deviations TODO cover deviations from submodules */
Radek Krejciec4da802019-05-02 13:02:41 +02006595 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01006596 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006597
Radek Krejciec4da802019-05-02 13:02:41 +02006598 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006599
Radek Krejcia3045382018-11-22 14:30:31 +01006600 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006601 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6602 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6603 * 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 +01006604 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006605 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006606 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6607 if (type->basetype == LY_TYPE_LEAFREF) {
6608 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006609 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 +01006610 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006611 } else if (type->basetype == LY_TYPE_UNION) {
6612 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6613 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6614 /* validate the path */
6615 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6616 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
6617 LY_CHECK_GOTO(ret, error);
6618 }
6619 }
Radek Krejcia3045382018-11-22 14:30:31 +01006620 }
6621 }
6622 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006623 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006624 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006625 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6626 if (type->basetype == LY_TYPE_LEAFREF) {
6627 /* store pointer to the real type */
6628 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6629 typeiter->basetype == LY_TYPE_LEAFREF;
6630 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6631 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006632 } else if (type->basetype == LY_TYPE_UNION) {
6633 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6634 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6635 /* store pointer to the real type */
6636 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6637 typeiter->basetype == LY_TYPE_LEAFREF;
6638 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6639 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6640 }
6641 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006642 }
6643 }
6644 }
Radek Krejciec4da802019-05-02 13:02:41 +02006645
Radek Krejci474f9b82019-07-24 11:36:37 +02006646 /* finish incomplete default values compilation */
6647 for (u = 0; u < ctx.dflts.count; ++u) {
6648 struct ly_err_item *err = NULL;
6649 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
6650 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized),
6651 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
6652 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
6653 if (err) {
6654 ly_err_print(err);
6655 ctx.path[0] = '\0';
6656 lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
6657 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
6658 "Invalid default - value does not fit the type (%s).", err->msg);
6659 ly_err_free(err);
6660 }
6661 LY_CHECK_GOTO(ret, error);
6662 }
6663
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006664 /* validate non-instantiated groupings from the parsed schema,
6665 * without it we would accept even the schemas with invalid grouping specification */
6666 ctx.options |= LYSC_OPT_GROUPING;
6667 LY_ARRAY_FOR(sp->groupings, u) {
6668 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
6669 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
6670 }
6671 }
6672 LY_LIST_FOR(sp->data, node_p) {
6673 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
6674 LY_ARRAY_FOR(grps, u) {
6675 if (!(grps[u].flags & LYS_USED_GRP)) {
6676 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
6677 }
6678 }
6679 }
6680
Radek Krejci474f9b82019-07-24 11:36:37 +02006681 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
6682 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
6683 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
6684 }
6685
Radek Krejci1c0c3442019-07-23 16:08:47 +02006686 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006687 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006688 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006689 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006690 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01006691
Radek Krejciec4da802019-05-02 13:02:41 +02006692 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01006693 lysp_module_free(mod->parsed);
6694 ((struct lys_module*)mod)->parsed = NULL;
6695 }
6696
Radek Krejciec4da802019-05-02 13:02:41 +02006697 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01006698 /* remove flag of the modules implemented by dependency */
6699 for (u = 0; u < ctx.ctx->list.count; ++u) {
6700 m = ctx.ctx->list.objs[u];
6701 if (m->implemented == 2) {
6702 m->implemented = 1;
6703 }
6704 }
6705 }
6706
Radek Krejci19a96102018-11-15 13:38:09 +01006707 ((struct lys_module*)mod)->compiled = mod_c;
6708 return LY_SUCCESS;
6709
6710error:
Radek Krejci95710c92019-02-11 15:49:55 +01006711 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02006712 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006713 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006714 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006715 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006716 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006717 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006718 mod->compiled = NULL;
6719
6720 /* revert compilation of modules implemented by dependency */
6721 for (u = 0; u < ctx.ctx->list.count; ++u) {
6722 m = ctx.ctx->list.objs[u];
6723 if (m->implemented == 2) {
6724 /* revert features list to the precompiled state */
6725 lys_feature_precompile_revert(&ctx, m);
6726 /* mark module as imported-only / not-implemented */
6727 m->implemented = 0;
6728 /* free the compiled version of the module */
6729 lysc_module_free(m->compiled, NULL);
6730 m->compiled = NULL;
6731 }
6732 }
6733
Radek Krejci19a96102018-11-15 13:38:09 +01006734 return ret;
6735}