blob: bea32c1747eecf9fe23be8d5d3d2b6ec76728c4b [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 Krejci0fe9b512019-07-26 17:51:05 +02002110 unsigned int dest_parent_times, c;
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;
Radek Krejci0fe9b512019-07-26 17:51:05 +02002170 if (!(context_node->flags & LYS_KEYLESS)) {
2171 struct lysc_node *key;
2172 for (key = context_node->child; key && key->nodetype == LYS_LEAF && (key->flags & LYS_KEY); key = key->next) {
2173 if (!strncmp(src, key->name, src_len) && key->name[src_len] == '\0') {
2174 src_node = key;
Radek Krejcibade82a2018-12-05 10:13:48 +01002175 break;
2176 }
2177 }
2178 }
Radek Krejcia3045382018-11-22 14:30:31 +01002179 if (!src_node) {
2180 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002181 "Invalid leafref path predicate \"%.*s\" - predicate's key node \"%.*s\" not found.",
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002182 *predicate - start, start, src_len, src, mod->name);
Radek Krejcia3045382018-11-22 14:30:31 +01002183 goto cleanup;
2184 }
Radek Krejcia3045382018-11-22 14:30:31 +01002185
2186 /* check that there is only one predicate for the */
Radek Krejcibade82a2018-12-05 10:13:48 +01002187 c = keys.count;
Radek Krejcia3045382018-11-22 14:30:31 +01002188 i = ly_set_add(&keys, (void*)src_node, 0);
2189 LY_CHECK_GOTO(i == -1, cleanup);
Radek Krejcibade82a2018-12-05 10:13:48 +01002190 if (keys.count == c) { /* node was already present in the set */
Radek Krejcia3045382018-11-22 14:30:31 +01002191 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejcibade82a2018-12-05 10:13:48 +01002192 "Invalid leafref path predicate \"%.*s\" - multiple equality tests for the key \"%s\".",
Radek Krejcia3045382018-11-22 14:30:31 +01002193 *predicate - start, start, src_node->name);
2194 goto cleanup;
2195 }
2196
2197 /* destination */
2198 dest_parent_times = 0;
Radek Krejci4ce68932018-11-28 12:53:36 +01002199 dst_node = start_node;
Radek Krejcia3045382018-11-22 14:30:31 +01002200
2201 /* current-function-invocation *WSP "/" *WSP rel-path-keyexpr */
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002202 if (strncmp(path_key_expr, "current", 7)) {
2203error_current_function_invocation:
Radek Krejcia3045382018-11-22 14:30:31 +01002204 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2205 "Invalid leafref path predicate \"%.*s\" - missing current-function-invocation.",
2206 *predicate - start, start);
2207 goto cleanup;
2208 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002209 for (path_key_expr += 7; isspace(*path_key_expr); ++path_key_expr);
2210 if (*path_key_expr != '(') {
2211 goto error_current_function_invocation;
Radek Krejcia3045382018-11-22 14:30:31 +01002212 }
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002213 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
2214 if (*path_key_expr != ')') {
2215 goto error_current_function_invocation;
2216 }
2217 for (path_key_expr++; isspace(*path_key_expr); ++path_key_expr);
Radek Krejcia3045382018-11-22 14:30:31 +01002218
2219 if (*path_key_expr != '/') {
2220 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2221 "Invalid leafref path predicate \"%.*s\" - missing \"/\" after current-function-invocation.",
2222 *predicate - start, start);
2223 goto cleanup;
2224 }
2225 ++path_key_expr;
2226 while (isspace(*path_key_expr)) {
2227 ++path_key_expr;
2228 }
2229
2230 /* rel-path-keyexpr:
2231 * 1*(".." *WSP "/" *WSP) *(node-identifier *WSP "/" *WSP) node-identifier */
2232 while (!strncmp(path_key_expr, "..", 2)) {
2233 ++dest_parent_times;
2234 path_key_expr += 2;
2235 while (isspace(*path_key_expr)) {
2236 ++path_key_expr;
2237 }
2238 if (*path_key_expr != '/') {
2239 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2240 "Invalid leafref path predicate \"%.*s\" - missing \"/\" in \"../\" rel-path-keyexpr pattern.",
2241 *predicate - start, start);
2242 goto cleanup;
2243 }
2244 ++path_key_expr;
2245 while (isspace(*path_key_expr)) {
2246 ++path_key_expr;
2247 }
2248
2249 /* path is supposed to be evaluated in data tree, so we have to skip
2250 * all schema nodes that cannot be instantiated in data tree */
2251 MOVE_PATH_PARENT(dst_node, !strncmp(path_key_expr, "..", 2), goto cleanup,
2252 "Invalid leafref path predicate \"%.*s\" - too many \"..\" in rel-path-keyexpr.",
2253 *predicate - start, start);
2254 }
2255 if (!dest_parent_times) {
2256 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2257 "Invalid leafref path predicate \"%.*s\" - at least one \"..\" is expected in rel-path-keyexpr.",
2258 *predicate - start, start);
2259 goto cleanup;
2260 }
2261 if (path_key_expr == pke_end) {
2262 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2263 "Invalid leafref path predicate \"%.*s\" - at least one node-identifier is expected in rel-path-keyexpr.",
2264 *predicate - start, start);
2265 goto cleanup;
2266 }
2267
2268 while(path_key_expr != pke_end) {
Radek Krejci7a3f89f2019-07-12 11:17:33 +02002269 for (;*path_key_expr == '/' || isspace(*path_key_expr); ++path_key_expr);
Radek Krejcib4a4a272019-06-10 12:44:52 +02002270 if (ly_parse_nodeid(&path_key_expr, &dst_prefix, &dst_prefix_len, &dst, &dst_len)) {
Radek Krejcia3045382018-11-22 14:30:31 +01002271 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcibade82a2018-12-05 10:13:48 +01002272 "Invalid node identifier in leafref path predicate - character %d (of %.*s).",
2273 path_key_expr - start + 1, *predicate - start, start);
Radek Krejcia3045382018-11-22 14:30:31 +01002274 goto cleanup;
2275 }
2276
2277 if (dst_prefix) {
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002278 mod = lys_module_find_prefix(path_context, dst_prefix, dst_prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002279 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002280 mod = start_node->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002281 }
2282 if (!mod) {
2283 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002284 "Invalid leafref path predicate \"%.*s\" - unable to find module of the node \"%.*s\" in rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002285 *predicate - start, start, dst_len, dst);
2286 goto cleanup;
2287 }
Radek Krejci92cc8512019-04-25 09:57:06 +02002288 if (!mod->implemented) {
2289 /* make the module implemented */
2290 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2291 }
Radek Krejcia3045382018-11-22 14:30:31 +01002292
2293 dst_node = lys_child(dst_node, mod, dst, dst_len, 0, LYS_GETNEXT_NOSTATECHECK);
2294 if (!dst_node) {
2295 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002296 "Invalid leafref path predicate \"%.*s\" - unable to find node \"%.*s\" in the rel-path-keyexpr.",
Radek Krejcia3045382018-11-22 14:30:31 +01002297 *predicate - start, start, path_key_expr - pke_start, pke_start);
2298 goto cleanup;
2299 }
2300 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002301 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 +01002302 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci92cc8512019-04-25 09:57:06 +02002303 "Invalid leafref path predicate \"%.*s\" - rel-path-keyexpr \"%.*s\" refers %s instead of leaf.",
Radek Krejcia3045382018-11-22 14:30:31 +01002304 *predicate - start, start, path_key_expr - pke_start, pke_start, lys_nodetype2str(dst_node->nodetype));
2305 goto cleanup;
2306 }
2307 }
2308
2309 ret = LY_SUCCESS;
2310cleanup:
2311 ly_set_erase(&keys, NULL);
2312 return ret;
2313}
2314
2315/**
2316 * @brief Parse path-arg (leafref). Get tokens of the path by repetitive calls of the function.
2317 *
2318 * path-arg = absolute-path / relative-path
2319 * absolute-path = 1*("/" (node-identifier *path-predicate))
2320 * relative-path = 1*(".." "/") descendant-path
2321 *
2322 * @param[in,out] path Path to parse.
2323 * @param[out] prefix Prefix of the token, NULL if there is not any.
2324 * @param[out] pref_len Length of the prefix, 0 if there is not any.
2325 * @param[out] name Name of the token.
2326 * @param[out] nam_len Length of the name.
2327 * @param[out] parent_times Number of leading ".." in the path. Must be 0 on the first call,
2328 * must not be changed between consecutive calls. -1 if the
2329 * path is absolute.
2330 * @param[out] has_predicate Flag to mark whether there is a predicate specified.
2331 * @return LY_ERR value: LY_SUCCESS or LY_EINVAL in case of invalid character in the path.
2332 */
Radek Krejci2d7a47b2019-05-16 13:34:10 +02002333LY_ERR
Radek Krejcia3045382018-11-22 14:30:31 +01002334lys_path_token(const char **path, const char **prefix, size_t *prefix_len, const char **name, size_t *name_len,
2335 int *parent_times, int *has_predicate)
2336{
2337 int par_times = 0;
2338
2339 assert(path && *path);
2340 assert(parent_times);
2341 assert(prefix);
2342 assert(prefix_len);
2343 assert(name);
2344 assert(name_len);
2345 assert(has_predicate);
2346
2347 *prefix = NULL;
2348 *prefix_len = 0;
2349 *name = NULL;
2350 *name_len = 0;
2351 *has_predicate = 0;
2352
2353 if (!*parent_times) {
2354 if (!strncmp(*path, "..", 2)) {
2355 *path += 2;
2356 ++par_times;
2357 while (!strncmp(*path, "/..", 3)) {
2358 *path += 3;
2359 ++par_times;
2360 }
2361 }
2362 if (par_times) {
2363 *parent_times = par_times;
2364 } else {
2365 *parent_times = -1;
2366 }
2367 }
2368
2369 if (**path != '/') {
2370 return LY_EINVAL;
2371 }
2372 /* skip '/' */
2373 ++(*path);
2374
2375 /* node-identifier ([prefix:]name) */
Radek Krejcib4a4a272019-06-10 12:44:52 +02002376 LY_CHECK_RET(ly_parse_nodeid(path, prefix, prefix_len, name, name_len));
Radek Krejcia3045382018-11-22 14:30:31 +01002377
2378 if ((**path == '/' && (*path)[1]) || !**path) {
2379 /* path continues by another token or this is the last token */
2380 return LY_SUCCESS;
2381 } else if ((*path)[0] != '[') {
2382 /* unexpected character */
2383 return LY_EINVAL;
2384 } else {
2385 /* predicate starting with [ */
2386 *has_predicate = 1;
2387 return LY_SUCCESS;
2388 }
2389}
2390
2391/**
Radek Krejci58d171e2018-11-23 13:50:55 +01002392 * @brief Check the features used in if-feature statements applicable to the leafref and its target.
2393 *
2394 * The set of features used for target must be a subset of features used for the leafref.
2395 * This is not a perfect, we should compare the truth tables but it could require too much resources
2396 * and RFC 7950 does not require it explicitely, so we simplify that.
2397 *
2398 * @param[in] refnode The leafref node.
2399 * @param[in] target Tha target node of the leafref.
2400 * @return LY_SUCCESS or LY_EVALID;
2401 */
2402static LY_ERR
2403lys_compile_leafref_features_validate(const struct lysc_node *refnode, const struct lysc_node *target)
2404{
2405 LY_ERR ret = LY_EVALID;
2406 const struct lysc_node *iter;
Radek Krejci58d171e2018-11-23 13:50:55 +01002407 unsigned int u, v, count;
2408 struct ly_set features = {0};
2409
2410 for (iter = refnode; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002411 if (iter->iffeatures) {
2412 LY_ARRAY_FOR(iter->iffeatures, u) {
2413 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2414 LY_CHECK_GOTO(ly_set_add(&features, iter->iffeatures[u].features[v], 0) == -1, cleanup);
Radek Krejci58d171e2018-11-23 13:50:55 +01002415 }
2416 }
2417 }
2418 }
2419
2420 /* we should have, in features set, a superset of features applicable to the target node.
2421 * So when adding features applicable to the target into the features set, we should not be
2422 * able to actually add any new feature, otherwise it is not a subset of features applicable
2423 * to the leafref itself. */
2424 count = features.count;
2425 for (iter = target; iter; iter = iter->parent) {
Radek Krejci056d0a82018-12-06 16:57:25 +01002426 if (iter->iffeatures) {
2427 LY_ARRAY_FOR(iter->iffeatures, u) {
2428 LY_ARRAY_FOR(iter->iffeatures[u].features, v) {
2429 if ((unsigned int)ly_set_add(&features, iter->iffeatures[u].features[v], 0) >= count) {
Radek Krejci58d171e2018-11-23 13:50:55 +01002430 /* new feature was added (or LY_EMEM) */
2431 goto cleanup;
2432 }
2433 }
2434 }
2435 }
2436 }
2437 ret = LY_SUCCESS;
2438
2439cleanup:
2440 ly_set_erase(&features, NULL);
2441 return ret;
2442}
2443
2444/**
Radek Krejcia3045382018-11-22 14:30:31 +01002445 * @brief Validate the leafref path.
2446 * @param[in] ctx Compile context
2447 * @param[in] startnode Path context node (where the leafref path begins/is placed).
Radek Krejci412ddfa2018-11-23 11:44:11 +01002448 * @param[in] leafref Leafref to validate.
Radek Krejcia3045382018-11-22 14:30:31 +01002449 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
2450 */
2451static LY_ERR
Radek Krejci412ddfa2018-11-23 11:44:11 +01002452lys_compile_leafref_validate(struct lysc_ctx *ctx, struct lysc_node *startnode, struct lysc_type_leafref *leafref)
Radek Krejcia3045382018-11-22 14:30:31 +01002453{
2454 const struct lysc_node *node = NULL, *parent = NULL;
2455 const struct lys_module *mod;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002456 struct lysc_type *type;
Radek Krejcia3045382018-11-22 14:30:31 +01002457 const char *id, *prefix, *name;
2458 size_t prefix_len, name_len;
2459 int parent_times = 0, has_predicate;
2460 unsigned int iter, u;
2461 LY_ERR ret = LY_SUCCESS;
2462
2463 assert(ctx);
2464 assert(startnode);
Radek Krejci412ddfa2018-11-23 11:44:11 +01002465 assert(leafref);
2466
Radek Krejci1c0c3442019-07-23 16:08:47 +02002467 ctx->path[0] = '\0';
2468 lysc_path(startnode, LY_PATH_LOG, ctx->path, LYSC_CTX_BUFSIZE);
2469 ctx->path_len = strlen(ctx->path);
Radek Krejci327de162019-06-14 12:52:07 +02002470
Radek Krejcia3045382018-11-22 14:30:31 +01002471 iter = 0;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002472 id = leafref->path;
Radek Krejcia3045382018-11-22 14:30:31 +01002473 while(*id && (ret = lys_path_token(&id, &prefix, &prefix_len, &name, &name_len, &parent_times, &has_predicate)) == LY_SUCCESS) {
2474 if (!iter) { /* first iteration */
2475 /* precess ".." in relative paths */
2476 if (parent_times > 0) {
2477 /* move from the context node */
2478 for (u = 0, parent = startnode; u < (unsigned int)parent_times; u++) {
2479 /* path is supposed to be evaluated in data tree, so we have to skip
2480 * all schema nodes that cannot be instantiated in data tree */
2481 MOVE_PATH_PARENT(parent, u < (unsigned int)parent_times - 1, return LY_EVALID,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002482 "Invalid leafref path \"%s\" - too many \"..\" in the path.", leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002483 }
2484 }
2485 }
2486
2487 if (prefix) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01002488 mod = lys_module_find_prefix(leafref->path_context, prefix, prefix_len);
Radek Krejcia3045382018-11-22 14:30:31 +01002489 } else {
Radek Krejci4ce68932018-11-28 12:53:36 +01002490 mod = startnode->module;
Radek Krejcia3045382018-11-22 14:30:31 +01002491 }
2492 if (!mod) {
2493 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002494 "Invalid leafref path - unable to find module connected with the prefix of the node \"%.*s\".",
2495 id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002496 return LY_EVALID;
2497 }
Radek Krejci3d929562019-02-21 11:25:55 +01002498 if (!mod->implemented) {
2499 /* make the module implemented */
2500 ly_ctx_module_implement_internal(ctx->ctx, (struct lys_module*)mod, 2);
2501 }
Radek Krejcia3045382018-11-22 14:30:31 +01002502
2503 node = lys_child(parent, mod, name, name_len, 0, LYS_GETNEXT_NOSTATECHECK);
2504 if (!node) {
2505 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002506 "Invalid leafref path - unable to find \"%.*s\".", id - leafref->path, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002507 return LY_EVALID;
2508 }
2509 parent = node;
2510
2511 if (has_predicate) {
2512 /* we have predicate, so the current result must be list */
2513 if (node->nodetype != LYS_LIST) {
2514 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2515 "Invalid leafref path - node \"%.*s\" is expected to be a list, but it is %s.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002516 id - leafref->path, leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002517 return LY_EVALID;
2518 }
2519
Radek Krejcibade82a2018-12-05 10:13:48 +01002520 LY_CHECK_RET(lys_compile_leafref_predicate_validate(ctx, &id, startnode, (struct lysc_node_list*)node, leafref->path_context),
2521 LY_EVALID);
Radek Krejcia3045382018-11-22 14:30:31 +01002522 }
2523
2524 ++iter;
2525 }
2526 if (ret) {
2527 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejci412ddfa2018-11-23 11:44:11 +01002528 "Invalid leafref path at character %d (%s).", id - leafref->path + 1, leafref->path);
Radek Krejcia3045382018-11-22 14:30:31 +01002529 return LY_EVALID;
2530 }
2531
2532 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST))) {
2533 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2534 "Invalid leafref path \"%s\" - target node is %s instead of leaf or leaf-list.",
Radek Krejci412ddfa2018-11-23 11:44:11 +01002535 leafref->path, lys_nodetype2str(node->nodetype));
Radek Krejcia3045382018-11-22 14:30:31 +01002536 return LY_EVALID;
2537 }
2538
2539 /* check status */
2540 if (lysc_check_status(ctx, startnode->flags, startnode->module, startnode->name, node->flags, node->module, node->name)) {
2541 return LY_EVALID;
2542 }
2543
Radek Krejcib57cf1e2018-11-23 14:07:05 +01002544 /* check config */
2545 if (leafref->require_instance && (startnode->flags & LYS_CONFIG_W)) {
2546 if (node->flags & LYS_CONFIG_R) {
2547 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2548 "Invalid leafref path \"%s\" - target is supposed to represent configuration data (as the leafref does), but it does not.",
2549 leafref->path);
2550 return LY_EVALID;
2551 }
2552 }
2553
Radek Krejci412ddfa2018-11-23 11:44:11 +01002554 /* store the target's type and check for circular chain of leafrefs */
2555 leafref->realtype = ((struct lysc_node_leaf*)node)->type;
2556 for (type = leafref->realtype; type && type->basetype == LY_TYPE_LEAFREF; type = ((struct lysc_type_leafref*)type)->realtype) {
2557 if (type == (struct lysc_type*)leafref) {
2558 /* circular chain detected */
2559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2560 "Invalid leafref path \"%s\" - circular chain of leafrefs detected.", leafref->path);
2561 return LY_EVALID;
2562 }
2563 }
2564
Radek Krejci58d171e2018-11-23 13:50:55 +01002565 /* check if leafref and its target are under common if-features */
2566 if (lys_compile_leafref_features_validate(startnode, node)) {
2567 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
2568 "Invalid leafref path \"%s\" - set of features applicable to the leafref target is not a subset of features applicable to the leafref itself.",
2569 leafref->path);
2570 return LY_EVALID;
2571 }
2572
Radek Krejci327de162019-06-14 12:52:07 +02002573 ctx->path_len = 1;
2574 ctx->path[1] = '\0';
Radek Krejcia3045382018-11-22 14:30:31 +01002575 return LY_SUCCESS;
2576}
2577
Radek Krejcicdfecd92018-11-26 11:27:32 +01002578static 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 +02002579 struct lysp_type *type_p, struct lysc_type **type, const char **units);
Radek Krejcia3045382018-11-22 14:30:31 +01002580/**
2581 * @brief The core of the lys_compile_type() - compile information about the given type (from typedef or leaf/leaf-list).
2582 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002583 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2584 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2585 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2586 * @param[in] context_name Name of the context node or referencing typedef for logging.
Radek Krejcia3045382018-11-22 14:30:31 +01002587 * @param[in] type_p Parsed type to compile.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002588 * @param[in] module Context module for the leafref path (to correctly resolve prefixes in path)
Radek Krejcia3045382018-11-22 14:30:31 +01002589 * @param[in] basetype Base YANG built-in type of the type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002590 * @param[in] tpdfname Name of the type's typedef, serves as a flag - if it is leaf/leaf-list's type, it is NULL.
2591 * @param[in] base The latest base (compiled) type from which the current type is being derived.
2592 * @param[out] type Newly created type structure with the filled information about the type.
2593 * @return LY_ERR value.
2594 */
Radek Krejci19a96102018-11-15 13:38:09 +01002595static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002596lys_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 +02002597 struct lysp_type *type_p, struct lys_module *module, LY_DATA_TYPE basetype, const char *tpdfname,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002598 struct lysc_type *base, struct lysc_type **type)
Radek Krejcic5c27e52018-11-15 14:38:11 +01002599{
2600 LY_ERR ret = LY_SUCCESS;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002601 unsigned int u, v, additional;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002602 struct lysc_type_bin *bin;
2603 struct lysc_type_num *num;
2604 struct lysc_type_str *str;
2605 struct lysc_type_bits *bits;
2606 struct lysc_type_enum *enumeration;
Radek Krejci6cba4292018-11-15 17:33:29 +01002607 struct lysc_type_dec *dec;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002608 struct lysc_type_identityref *idref;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002609 struct lysc_type_union *un, *un_aux;
2610 void *p;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002611
2612 switch (basetype) {
2613 case LY_TYPE_BINARY:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002614 bin = (struct lysc_type_bin*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002615
2616 /* RFC 7950 9.8.1, 9.4.4 - length, number of octets it contains */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002617 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002618 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2619 base ? ((struct lysc_type_bin*)base)->length : NULL, &bin->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002620 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002621 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, bin->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002622 }
2623 }
2624
2625 if (tpdfname) {
2626 type_p->compiled = *type;
2627 *type = calloc(1, sizeof(struct lysc_type_bin));
2628 }
2629 break;
2630 case LY_TYPE_BITS:
2631 /* RFC 7950 9.7 - bits */
2632 bits = (struct lysc_type_bits*)(*type);
2633 if (type_p->bits) {
Radek Krejciec4da802019-05-02 13:02:41 +02002634 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->bits, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002635 base ? (struct lysc_type_bitenum_item*)((struct lysc_type_bits*)base)->bits : NULL,
2636 (struct lysc_type_bitenum_item**)&bits->bits));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002637 }
2638
Radek Krejci555cb5b2018-11-16 14:54:33 +01002639 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002640 /* type derived from bits built-in type must contain at least one bit */
Radek Krejci6cba4292018-11-15 17:33:29 +01002641 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002642 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002643 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002644 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "bit", "bits type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002645 free(*type);
2646 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002647 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002648 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002649 }
2650
2651 if (tpdfname) {
2652 type_p->compiled = *type;
2653 *type = calloc(1, sizeof(struct lysc_type_bits));
2654 }
2655 break;
Radek Krejci6cba4292018-11-15 17:33:29 +01002656 case LY_TYPE_DEC64:
2657 dec = (struct lysc_type_dec*)(*type);
2658
2659 /* RFC 7950 9.3.4 - fraction-digits */
Radek Krejci555cb5b2018-11-16 14:54:33 +01002660 if (!base) {
Radek Krejci643c8242018-11-15 17:51:11 +01002661 if (!type_p->fraction_digits) {
2662 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002663 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type ", tpdfname);
Radek Krejci643c8242018-11-15 17:51:11 +01002664 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002665 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "fraction-digits", "decimal64 type", "");
Radek Krejci643c8242018-11-15 17:51:11 +01002666 free(*type);
2667 *type = NULL;
2668 }
2669 return LY_EVALID;
2670 }
2671 } else if (type_p->fraction_digits) {
2672 /* fraction digits is prohibited in types not directly derived from built-in decimal64 */
Radek Krejci6cba4292018-11-15 17:33:29 +01002673 if (tpdfname) {
Radek Krejci643c8242018-11-15 17:51:11 +01002674 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2675 "Invalid fraction-digits substatement for type \"%s\" not directly derived from decimal64 built-in type.",
Radek Krejci6cba4292018-11-15 17:33:29 +01002676 tpdfname);
2677 } else {
Radek Krejci643c8242018-11-15 17:51:11 +01002678 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2679 "Invalid fraction-digits substatement for type not directly derived from decimal64 built-in type.");
Radek Krejci6cba4292018-11-15 17:33:29 +01002680 free(*type);
2681 *type = NULL;
2682 }
2683 return LY_EVALID;
2684 }
2685 dec->fraction_digits = type_p->fraction_digits;
2686
2687 /* RFC 7950 9.2.4 - range */
2688 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002689 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, dec->fraction_digits,
2690 base ? ((struct lysc_type_dec*)base)->range : NULL, &dec->range));
Radek Krejci6cba4292018-11-15 17:33:29 +01002691 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002692 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, dec->range->exts, u, lys_compile_ext, ret, done);
Radek Krejci6cba4292018-11-15 17:33:29 +01002693 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002694 }
2695
2696 if (tpdfname) {
2697 type_p->compiled = *type;
2698 *type = calloc(1, sizeof(struct lysc_type_dec));
2699 }
2700 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002701 case LY_TYPE_STRING:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002702 str = (struct lysc_type_str*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002703
2704 /* RFC 7950 9.4.4 - length */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002705 if (type_p->length) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002706 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->length, basetype, 1, 0,
2707 base ? ((struct lysc_type_str*)base)->length : NULL, &str->length));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002708 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002709 COMPILE_ARRAY_GOTO(ctx, type_p->length->exts, str->length->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002710 }
2711 } else if (base && ((struct lysc_type_str*)base)->length) {
2712 str->length = lysc_range_dup(ctx->ctx, ((struct lysc_type_str*)base)->length);
2713 }
2714
2715 /* RFC 7950 9.4.5 - pattern */
2716 if (type_p->patterns) {
Radek Krejciec4da802019-05-02 13:02:41 +02002717 LY_CHECK_RET(lys_compile_type_patterns(ctx, type_p->patterns,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002718 base ? ((struct lysc_type_str*)base)->patterns : NULL, &str->patterns));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002719 } else if (base && ((struct lysc_type_str*)base)->patterns) {
2720 str->patterns = lysc_patterns_dup(ctx->ctx, ((struct lysc_type_str*)base)->patterns);
2721 }
2722
2723 if (tpdfname) {
2724 type_p->compiled = *type;
2725 *type = calloc(1, sizeof(struct lysc_type_str));
2726 }
2727 break;
2728 case LY_TYPE_ENUM:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002729 enumeration = (struct lysc_type_enum*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002730
2731 /* RFC 7950 9.6 - enum */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002732 if (type_p->enums) {
Radek Krejciec4da802019-05-02 13:02:41 +02002733 LY_CHECK_RET(lys_compile_type_enums(ctx, type_p->enums, basetype,
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002734 base ? ((struct lysc_type_enum*)base)->enums : NULL, &enumeration->enums));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002735 }
2736
Radek Krejci555cb5b2018-11-16 14:54:33 +01002737 if (!base && !type_p->flags) {
Radek Krejcic5c27e52018-11-15 14:38:11 +01002738 /* type derived from enumerations built-in type must contain at least one enum */
Radek Krejci6cba4292018-11-15 17:33:29 +01002739 if (tpdfname) {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002740 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type ", tpdfname);
Radek Krejci6cba4292018-11-15 17:33:29 +01002741 } else {
Radek Krejci555cb5b2018-11-16 14:54:33 +01002742 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "enum", "enumeration type", "");
Radek Krejci6cba4292018-11-15 17:33:29 +01002743 free(*type);
2744 *type = NULL;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002745 }
Radek Krejci6cba4292018-11-15 17:33:29 +01002746 return LY_EVALID;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002747 }
2748
2749 if (tpdfname) {
2750 type_p->compiled = *type;
2751 *type = calloc(1, sizeof(struct lysc_type_enum));
2752 }
2753 break;
2754 case LY_TYPE_INT8:
2755 case LY_TYPE_UINT8:
2756 case LY_TYPE_INT16:
2757 case LY_TYPE_UINT16:
2758 case LY_TYPE_INT32:
2759 case LY_TYPE_UINT32:
2760 case LY_TYPE_INT64:
2761 case LY_TYPE_UINT64:
Radek Krejcic5c27e52018-11-15 14:38:11 +01002762 num = (struct lysc_type_num*)(*type);
Radek Krejci555cb5b2018-11-16 14:54:33 +01002763
2764 /* RFC 6020 9.2.4 - range */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002765 if (type_p->range) {
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002766 LY_CHECK_RET(lys_compile_type_range(ctx, type_p->range, basetype, 0, 0,
2767 base ? ((struct lysc_type_num*)base)->range : NULL, &num->range));
Radek Krejcic5c27e52018-11-15 14:38:11 +01002768 if (!tpdfname) {
Radek Krejciec4da802019-05-02 13:02:41 +02002769 COMPILE_ARRAY_GOTO(ctx, type_p->range->exts, num->range->exts, u, lys_compile_ext, ret, done);
Radek Krejcic5c27e52018-11-15 14:38:11 +01002770 }
2771 }
2772
2773 if (tpdfname) {
2774 type_p->compiled = *type;
2775 *type = calloc(1, sizeof(struct lysc_type_num));
2776 }
2777 break;
Radek Krejci555cb5b2018-11-16 14:54:33 +01002778 case LY_TYPE_IDENT:
2779 idref = (struct lysc_type_identityref*)(*type);
2780
2781 /* RFC 7950 9.10.2 - base */
2782 if (type_p->bases) {
2783 if (base) {
2784 /* only the directly derived identityrefs can contain base specification */
2785 if (tpdfname) {
2786 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002787 "Invalid base substatement for the type \"%s\" not directly derived from identityref built-in type.",
Radek Krejci555cb5b2018-11-16 14:54:33 +01002788 tpdfname);
2789 } else {
2790 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
Radek Krejcicdfecd92018-11-26 11:27:32 +01002791 "Invalid base substatement for the type not directly derived from identityref built-in type.");
Radek Krejci555cb5b2018-11-16 14:54:33 +01002792 free(*type);
2793 *type = NULL;
2794 }
2795 return LY_EVALID;
2796 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002797 LY_CHECK_RET(lys_compile_identity_bases(ctx, type_p->bases, NULL, &idref->bases));
Radek Krejci555cb5b2018-11-16 14:54:33 +01002798 }
2799
2800 if (!base && !type_p->flags) {
2801 /* type derived from identityref built-in type must contain at least one base */
2802 if (tpdfname) {
2803 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type ", tpdfname);
2804 } else {
2805 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "base", "identityref type", "");
2806 free(*type);
2807 *type = NULL;
2808 }
2809 return LY_EVALID;
2810 }
2811
2812 if (tpdfname) {
2813 type_p->compiled = *type;
2814 *type = calloc(1, sizeof(struct lysc_type_identityref));
2815 }
2816 break;
Radek Krejcia3045382018-11-22 14:30:31 +01002817 case LY_TYPE_LEAFREF:
2818 /* RFC 7950 9.9.3 - require-instance */
2819 if (type_p->flags & LYS_SET_REQINST) {
Radek Krejci0bcdaed2019-01-10 10:21:34 +01002820 if (context_mod->mod->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01002821 if (tpdfname) {
2822 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2823 "Leafref type \"%s\" can be restricted by require-instance statement only in YANG 1.1 modules.", tpdfname);
2824 } else {
2825 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
2826 "Leafref type can be restricted by require-instance statement only in YANG 1.1 modules.");
2827 free(*type);
2828 *type = NULL;
2829 }
2830 return LY_EVALID;
2831 }
Radek Krejcia3045382018-11-22 14:30:31 +01002832 ((struct lysc_type_leafref*)(*type))->require_instance = type_p->require_instance;
Radek Krejci412ddfa2018-11-23 11:44:11 +01002833 } else if (base) {
2834 /* inherit */
2835 ((struct lysc_type_leafref*)(*type))->require_instance = ((struct lysc_type_leafref*)base)->require_instance;
Radek Krejcia3045382018-11-22 14:30:31 +01002836 } else {
2837 /* default is true */
2838 ((struct lysc_type_leafref*)(*type))->require_instance = 1;
2839 }
2840 if (type_p->path) {
2841 DUP_STRING(ctx->ctx, (void*)type_p->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002842 ((struct lysc_type_leafref*)(*type))->path_context = module;
Radek Krejcia3045382018-11-22 14:30:31 +01002843 } else if (base) {
2844 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)base)->path, ((struct lysc_type_leafref*)(*type))->path);
Radek Krejci96a0bfd2018-11-22 15:25:06 +01002845 ((struct lysc_type_leafref*)(*type))->path_context = ((struct lysc_type_leafref*)base)->path_context;
Radek Krejcia3045382018-11-22 14:30:31 +01002846 } else if (tpdfname) {
2847 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type ", tpdfname);
2848 return LY_EVALID;
2849 } else {
2850 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
2851 free(*type);
2852 *type = NULL;
2853 return LY_EVALID;
2854 }
2855 if (tpdfname) {
2856 type_p->compiled = *type;
2857 *type = calloc(1, sizeof(struct lysc_type_leafref));
2858 }
2859 break;
Radek Krejci16c0f822018-11-16 10:46:10 +01002860 case LY_TYPE_INST:
2861 /* RFC 7950 9.9.3 - require-instance */
2862 if (type_p->flags & LYS_SET_REQINST) {
2863 ((struct lysc_type_instanceid*)(*type))->require_instance = type_p->require_instance;
2864 } else {
2865 /* default is true */
2866 ((struct lysc_type_instanceid*)(*type))->require_instance = 1;
2867 }
2868
2869 if (tpdfname) {
2870 type_p->compiled = *type;
2871 *type = calloc(1, sizeof(struct lysc_type_instanceid));
2872 }
2873 break;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002874 case LY_TYPE_UNION:
2875 un = (struct lysc_type_union*)(*type);
2876
2877 /* RFC 7950 7.4 - type */
2878 if (type_p->types) {
2879 if (base) {
2880 /* only the directly derived union can contain types specification */
2881 if (tpdfname) {
2882 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2883 "Invalid type substatement for the type \"%s\" not directly derived from union built-in type.",
2884 tpdfname);
2885 } else {
2886 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG,
2887 "Invalid type substatement for the type not directly derived from union built-in type.");
2888 free(*type);
2889 *type = NULL;
2890 }
2891 return LY_EVALID;
2892 }
2893 /* compile the type */
2894 additional = 0;
2895 LY_ARRAY_CREATE_RET(ctx->ctx, un->types, LY_ARRAY_SIZE(type_p->types), LY_EVALID);
2896 for (u = 0; u < LY_ARRAY_SIZE(type_p->types); ++u) {
Radek Krejciec4da802019-05-02 13:02:41 +02002897 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 +01002898 if (un->types[u + additional]->basetype == LY_TYPE_UNION) {
2899 /* add space for additional types from the union subtype */
2900 un_aux = (struct lysc_type_union *)un->types[u + additional];
2901 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)));
2902 LY_CHECK_ERR_RET(!p, LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2903 un->types = (void*)((uint32_t*)(p) + 1);
2904
2905 /* copy subtypes of the subtype union */
2906 for (v = 0; v < LY_ARRAY_SIZE(un_aux->types); ++v) {
2907 if (un_aux->types[v]->basetype == LY_TYPE_LEAFREF) {
2908 /* duplicate the whole structure because of the instance-specific path resolving for realtype */
2909 un->types[u + additional] = calloc(1, sizeof(struct lysc_type_leafref));
2910 LY_CHECK_ERR_RET(!un->types[u + additional], LOGMEM(ctx->ctx);lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux), LY_EMEM);
2911 ((struct lysc_type_leafref*)un->types[u + additional])->basetype = LY_TYPE_LEAFREF;
2912 DUP_STRING(ctx->ctx, ((struct lysc_type_leafref*)un_aux->types[v])->path, ((struct lysc_type_leafref*)un->types[u + additional])->path);
2913 ((struct lysc_type_leafref*)un->types[u + additional])->refcount = 1;
2914 ((struct lysc_type_leafref*)un->types[u + additional])->require_instance = ((struct lysc_type_leafref*)un_aux->types[v])->require_instance;
2915 ((struct lysc_type_leafref*)un->types[u + additional])->path_context = ((struct lysc_type_leafref*)un_aux->types[v])->path_context;
2916 /* TODO extensions */
2917
2918 } else {
2919 un->types[u + additional] = un_aux->types[v];
2920 ++un_aux->types[v]->refcount;
2921 }
2922 ++additional;
2923 LY_ARRAY_INCREMENT(un->types);
2924 }
2925 /* compensate u increment in main loop */
2926 --additional;
2927
2928 /* free the replaced union subtype */
2929 lysc_type_free(ctx->ctx, (struct lysc_type*)un_aux);
2930 } else {
2931 LY_ARRAY_INCREMENT(un->types);
2932 }
Radek Krejcicdfecd92018-11-26 11:27:32 +01002933 }
2934 }
2935
2936 if (!base && !type_p->flags) {
2937 /* type derived from union built-in type must contain at least one type */
2938 if (tpdfname) {
2939 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type ", tpdfname);
2940 } else {
2941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "type", "union type", "");
2942 free(*type);
2943 *type = NULL;
2944 }
2945 return LY_EVALID;
2946 }
2947
2948 if (tpdfname) {
2949 type_p->compiled = *type;
2950 *type = calloc(1, sizeof(struct lysc_type_union));
2951 }
2952 break;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002953 case LY_TYPE_BOOL:
2954 case LY_TYPE_EMPTY:
2955 case LY_TYPE_UNKNOWN: /* just to complete switch */
2956 break;
2957 }
2958 LY_CHECK_ERR_RET(!(*type), LOGMEM(ctx->ctx), LY_EMEM);
2959done:
2960 return ret;
2961}
2962
Radek Krejcia3045382018-11-22 14:30:31 +01002963/**
2964 * @brief Compile information about the leaf/leaf-list's type.
2965 * @param[in] ctx Compile context.
Radek Krejcicdfecd92018-11-26 11:27:32 +01002966 * @param[in] context_node_p Schema node where the type/typedef is placed to correctly find the base types.
2967 * @param[in] context_flags Flags of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2968 * @param[in] context_mod Module of the context node or the referencing typedef to correctly check status of referencing and referenced objects.
2969 * @param[in] context_name Name of the context node or referencing typedef for logging.
2970 * @param[in] type_p Parsed type to compile.
Radek Krejcia3045382018-11-22 14:30:31 +01002971 * @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 +01002972 * @param[out] units Storage for inheriting units value from the typedefs the current type derives from.
Radek Krejcia3045382018-11-22 14:30:31 +01002973 * @return LY_ERR value.
2974 */
Radek Krejcic5c27e52018-11-15 14:38:11 +01002975static LY_ERR
Radek Krejcicdfecd92018-11-26 11:27:32 +01002976lys_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 +02002977 struct lysp_type *type_p, struct lysc_type **type, const char **units)
Radek Krejci19a96102018-11-15 13:38:09 +01002978{
2979 LY_ERR ret = LY_SUCCESS;
2980 unsigned int u;
Radek Krejcicdfecd92018-11-26 11:27:32 +01002981 int dummyloops = 0;
Radek Krejci19a96102018-11-15 13:38:09 +01002982 struct type_context {
2983 const struct lysp_tpdf *tpdf;
2984 struct lysp_node *node;
2985 struct lysp_module *mod;
Radek Krejci99b5b2a2019-04-30 16:57:04 +02002986 } *tctx, *tctx_prev = NULL, *tctx_iter;
Radek Krejci19a96102018-11-15 13:38:09 +01002987 LY_DATA_TYPE basetype = LY_TYPE_UNKNOWN;
Radek Krejcic5c27e52018-11-15 14:38:11 +01002988 struct lysc_type *base = NULL, *prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01002989 struct ly_set tpdf_chain = {0};
Radek Krejci01342af2019-01-03 15:18:08 +01002990 const char *dflt = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02002991 struct lys_module *dflt_mod = NULL;
Radek Krejci19a96102018-11-15 13:38:09 +01002992
2993 (*type) = NULL;
2994
2995 tctx = calloc(1, sizeof *tctx);
2996 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
Radek Krejcie86bf772018-12-14 11:39:53 +01002997 for (ret = lysp_type_find(type_p->name, context_node_p, ctx->mod_def->parsed,
Radek Krejci19a96102018-11-15 13:38:09 +01002998 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod);
2999 ret == LY_SUCCESS;
3000 ret = lysp_type_find(tctx_prev->tpdf->type.name, tctx_prev->node, tctx_prev->mod,
3001 &basetype, &tctx->tpdf, &tctx->node, &tctx->mod)) {
3002 if (basetype) {
3003 break;
3004 }
3005
3006 /* check status */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003007 ret = lysc_check_status(ctx, context_flags, context_mod, context_name,
3008 tctx->tpdf->flags, tctx->mod, tctx->node ? tctx->node->name : tctx->tpdf->name);
Radek Krejci19a96102018-11-15 13:38:09 +01003009 LY_CHECK_ERR_GOTO(ret, free(tctx), cleanup);
3010
Radek Krejcicdfecd92018-11-26 11:27:32 +01003011 if (units && !*units) {
3012 /* inherit units */
3013 DUP_STRING(ctx->ctx, tctx->tpdf->units, *units);
3014 }
Radek Krejci01342af2019-01-03 15:18:08 +01003015 if (!dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003016 /* inherit default */
Radek Krejci01342af2019-01-03 15:18:08 +01003017 dflt = tctx->tpdf->dflt;
Radek Krejcia1911222019-07-22 17:24:50 +02003018 dflt_mod = tctx->mod->mod;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003019 }
Radek Krejci01342af2019-01-03 15:18:08 +01003020 if (dummyloops && (!units || *units) && dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003021 basetype = ((struct type_context*)tpdf_chain.objs[tpdf_chain.count - 1])->tpdf->type.compiled->basetype;
3022 break;
3023 }
3024
Radek Krejci19a96102018-11-15 13:38:09 +01003025 if (tctx->tpdf->type.compiled) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003026 /* it is not necessary to continue, the rest of the chain was already compiled,
3027 * but we still may need to inherit default and units values, so start dummy loops */
Radek Krejci19a96102018-11-15 13:38:09 +01003028 basetype = tctx->tpdf->type.compiled->basetype;
3029 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
Radek Krejci01342af2019-01-03 15:18:08 +01003030 if ((units && !*units) || !dflt) {
Radek Krejcicdfecd92018-11-26 11:27:32 +01003031 dummyloops = 1;
3032 goto preparenext;
3033 } else {
3034 tctx = NULL;
3035 break;
3036 }
Radek Krejci19a96102018-11-15 13:38:09 +01003037 }
3038
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003039 /* circular typedef reference detection */
3040 for (u = 0; u < tpdf_chain.count; u++) {
3041 /* local part */
3042 tctx_iter = (struct type_context*)tpdf_chain.objs[u];
3043 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3044 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3045 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3046 free(tctx);
3047 ret = LY_EVALID;
3048 goto cleanup;
3049 }
3050 }
3051 for (u = 0; u < ctx->tpdf_chain.count; u++) {
3052 /* global part for unions corner case */
3053 tctx_iter = (struct type_context*)ctx->tpdf_chain.objs[u];
3054 if (tctx_iter->mod == tctx->mod && tctx_iter->node == tctx->node && tctx_iter->tpdf == tctx->tpdf) {
3055 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3056 "Invalid \"%s\" type reference - circular chain of types detected.", tctx->tpdf->name);
3057 free(tctx);
3058 ret = LY_EVALID;
3059 goto cleanup;
3060 }
3061 }
3062
Radek Krejci19a96102018-11-15 13:38:09 +01003063 /* store information for the following processing */
3064 ly_set_add(&tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3065
Radek Krejcicdfecd92018-11-26 11:27:32 +01003066preparenext:
Radek Krejci19a96102018-11-15 13:38:09 +01003067 /* prepare next loop */
3068 tctx_prev = tctx;
3069 tctx = calloc(1, sizeof *tctx);
3070 LY_CHECK_ERR_RET(!tctx, LOGMEM(ctx->ctx), LY_EMEM);
3071 }
3072 free(tctx);
3073
3074 /* allocate type according to the basetype */
3075 switch (basetype) {
3076 case LY_TYPE_BINARY:
3077 *type = calloc(1, sizeof(struct lysc_type_bin));
Radek Krejci19a96102018-11-15 13:38:09 +01003078 break;
3079 case LY_TYPE_BITS:
3080 *type = calloc(1, sizeof(struct lysc_type_bits));
Radek Krejci19a96102018-11-15 13:38:09 +01003081 break;
3082 case LY_TYPE_BOOL:
3083 case LY_TYPE_EMPTY:
3084 *type = calloc(1, sizeof(struct lysc_type));
3085 break;
3086 case LY_TYPE_DEC64:
3087 *type = calloc(1, sizeof(struct lysc_type_dec));
3088 break;
3089 case LY_TYPE_ENUM:
3090 *type = calloc(1, sizeof(struct lysc_type_enum));
Radek Krejci19a96102018-11-15 13:38:09 +01003091 break;
3092 case LY_TYPE_IDENT:
3093 *type = calloc(1, sizeof(struct lysc_type_identityref));
3094 break;
3095 case LY_TYPE_INST:
3096 *type = calloc(1, sizeof(struct lysc_type_instanceid));
3097 break;
3098 case LY_TYPE_LEAFREF:
3099 *type = calloc(1, sizeof(struct lysc_type_leafref));
3100 break;
3101 case LY_TYPE_STRING:
3102 *type = calloc(1, sizeof(struct lysc_type_str));
Radek Krejci19a96102018-11-15 13:38:09 +01003103 break;
3104 case LY_TYPE_UNION:
3105 *type = calloc(1, sizeof(struct lysc_type_union));
3106 break;
3107 case LY_TYPE_INT8:
3108 case LY_TYPE_UINT8:
3109 case LY_TYPE_INT16:
3110 case LY_TYPE_UINT16:
3111 case LY_TYPE_INT32:
3112 case LY_TYPE_UINT32:
3113 case LY_TYPE_INT64:
3114 case LY_TYPE_UINT64:
3115 *type = calloc(1, sizeof(struct lysc_type_num));
Radek Krejci19a96102018-11-15 13:38:09 +01003116 break;
3117 case LY_TYPE_UNKNOWN:
3118 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3119 "Referenced type \"%s\" not found.", tctx_prev ? tctx_prev->tpdf->type.name : type_p->name);
3120 ret = LY_EVALID;
3121 goto cleanup;
3122 }
3123 LY_CHECK_ERR_GOTO(!(*type), LOGMEM(ctx->ctx), cleanup);
Radek Krejcicdfecd92018-11-26 11:27:32 +01003124 if (~type_substmt_map[basetype] & type_p->flags) {
Radek Krejci19a96102018-11-15 13:38:09 +01003125 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type restrictions for %s type.",
3126 ly_data_type2str[basetype]);
3127 free(*type);
3128 (*type) = NULL;
3129 ret = LY_EVALID;
3130 goto cleanup;
3131 }
3132
3133 /* get restrictions from the referred typedefs */
3134 for (u = tpdf_chain.count - 1; u + 1 > 0; --u) {
3135 tctx = (struct type_context*)tpdf_chain.objs[u];
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003136
3137 /* remember the typedef context for circular check */
3138 ly_set_add(&ctx->tpdf_chain, tctx, LY_SET_OPT_USEASLIST);
3139
Radek Krejci43699232018-11-23 14:59:46 +01003140 if (tctx->tpdf->type.compiled) {
Radek Krejci19a96102018-11-15 13:38:09 +01003141 base = tctx->tpdf->type.compiled;
3142 continue;
Radek Krejci43699232018-11-23 14:59:46 +01003143 } else if (basetype != LY_TYPE_LEAFREF && (u != tpdf_chain.count - 1) && !(tctx->tpdf->type.flags)) {
Radek Krejci19a96102018-11-15 13:38:09 +01003144 /* no change, just use the type information from the base */
3145 base = ((struct lysp_tpdf*)tctx->tpdf)->type.compiled = ((struct type_context*)tpdf_chain.objs[u + 1])->tpdf->type.compiled;
3146 ++base->refcount;
3147 continue;
3148 }
3149
3150 ++(*type)->refcount;
Radek Krejci43699232018-11-23 14:59:46 +01003151 if (~type_substmt_map[basetype] & tctx->tpdf->type.flags) {
3152 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SYNTAX_YANG, "Invalid type \"%s\" restriction(s) for %s type.",
3153 tctx->tpdf->name, ly_data_type2str[basetype]);
3154 ret = LY_EVALID;
3155 goto cleanup;
3156 } else if (basetype == LY_TYPE_EMPTY && tctx->tpdf->dflt) {
3157 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3158 "Invalid type \"%s\" - \"empty\" type must not have a default value (%s).",
3159 tctx->tpdf->name, tctx->tpdf->dflt);
3160 ret = LY_EVALID;
3161 goto cleanup;
3162 }
3163
Radek Krejci19a96102018-11-15 13:38:09 +01003164 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003165 /* TODO user type plugins */
3166 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejcic5c27e52018-11-15 14:38:11 +01003167 prev_type = *type;
Radek Krejcicdfecd92018-11-26 11:27:32 +01003168 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 +01003169 basetype & (LY_TYPE_LEAFREF | LY_TYPE_UNION) ? lysp_find_module(ctx->ctx, tctx->mod) : NULL,
Radek Krejciec4da802019-05-02 13:02:41 +02003170 basetype, tctx->tpdf->name, base, type);
Radek Krejcic5c27e52018-11-15 14:38:11 +01003171 LY_CHECK_GOTO(ret, cleanup);
3172 base = prev_type;
Radek Krejci19a96102018-11-15 13:38:09 +01003173 }
Radek Krejci99b5b2a2019-04-30 16:57:04 +02003174 /* remove the processed typedef contexts from the stack for circular check */
3175 ctx->tpdf_chain.count = ctx->tpdf_chain.count - tpdf_chain.count;
Radek Krejci19a96102018-11-15 13:38:09 +01003176
Radek Krejcic5c27e52018-11-15 14:38:11 +01003177 /* process the type definition in leaf */
Radek Krejcicdfecd92018-11-26 11:27:32 +01003178 if (type_p->flags || !base || basetype == LY_TYPE_LEAFREF) {
Radek Krejcia3045382018-11-22 14:30:31 +01003179 /* get restrictions from the node itself */
Radek Krejci19a96102018-11-15 13:38:09 +01003180 (*type)->basetype = basetype;
Radek Krejcie7b95092019-05-15 11:03:07 +02003181 /* TODO user type plugins */
3182 (*type)->plugin = &ly_builtin_type_plugins[basetype];
Radek Krejci19a96102018-11-15 13:38:09 +01003183 ++(*type)->refcount;
Radek Krejciec4da802019-05-02 13:02:41 +02003184 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 +01003185 LY_CHECK_GOTO(ret, cleanup);
3186 } else {
Radek Krejci19a96102018-11-15 13:38:09 +01003187 /* no specific restriction in leaf's type definition, copy from the base */
3188 free(*type);
3189 (*type) = base;
3190 ++(*type)->refcount;
Radek Krejci19a96102018-11-15 13:38:09 +01003191 }
Radek Krejcia1911222019-07-22 17:24:50 +02003192 if (dflt && !(*type)->dflt) {
3193 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003194 (*type)->dflt_mod = dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003195 (*type)->dflt = calloc(1, sizeof *(*type)->dflt);
3196 (*type)->dflt->realtype = (*type);
3197 ret = (*type)->plugin->store(ctx->ctx, (*type), dflt, strlen(dflt),
3198 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003199 lys_resolve_prefix, (void*)(*type)->dflt_mod, LYD_XML, NULL, NULL, (*type)->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003200 if (err) {
3201 ly_err_print(err);
3202 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3203 "Invalid type's default value \"%s\" which does not fit the type (%s).", dflt, err->msg);
3204 ly_err_free(err);
3205 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003206 if (ret == LY_EINCOMPLETE) {
3207 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003208 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, NULL, (*type)->dflt, (*type)->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003209
3210 /* but in general result is so far ok */
3211 ret = LY_SUCCESS;
3212 }
Radek Krejcia1911222019-07-22 17:24:50 +02003213 LY_CHECK_GOTO(ret, cleanup);
Radek Krejci01342af2019-01-03 15:18:08 +01003214 }
Radek Krejci19a96102018-11-15 13:38:09 +01003215
Radek Krejciec4da802019-05-02 13:02:41 +02003216 COMPILE_ARRAY_GOTO(ctx, type_p->exts, (*type)->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci19a96102018-11-15 13:38:09 +01003217
3218cleanup:
3219 ly_set_erase(&tpdf_chain, free);
3220 return ret;
3221}
3222
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003223/**
3224 * @brief Compile status information of the given node.
3225 *
3226 * To simplify getting status of the node, the flags are set following inheritance rules, so all the nodes
3227 * has the status correctly set during the compilation.
3228 *
3229 * @param[in] ctx Compile context
3230 * @param[in,out] node_flags Flags of the compiled node which status is supposed to be resolved.
3231 * If the status was set explicitly on the node, it is already set in the flags value and we just check
3232 * the compatibility with the parent's status value.
3233 * @param[in] parent_flags Flags of the parent node to check/inherit the status value.
3234 * @return LY_ERR value.
3235 */
3236static LY_ERR
3237lys_compile_status(struct lysc_ctx *ctx, uint16_t *node_flags, uint16_t parent_flags)
3238{
3239 /* status - it is not inherited by specification, but it does not make sense to have
3240 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3241 if (!((*node_flags) & LYS_STATUS_MASK)) {
3242 if (parent_flags & (LYS_STATUS_DEPRC | LYS_STATUS_OBSLT)) {
3243 if ((parent_flags & 0x3) != 0x3) {
3244 /* do not print the warning when inheriting status from uses - the uses_status value has a special
3245 * combination of bits (0x3) which marks the uses_status value */
3246 LOGWRN(ctx->ctx, "Missing explicit \"%s\" status that was already specified in parent, inheriting.",
3247 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3248 }
3249 (*node_flags) |= parent_flags & LYS_STATUS_MASK;
3250 } else {
3251 (*node_flags) |= LYS_STATUS_CURR;
3252 }
3253 } else if (parent_flags & LYS_STATUS_MASK) {
3254 /* check status compatibility with the parent */
3255 if ((parent_flags & LYS_STATUS_MASK) > ((*node_flags) & LYS_STATUS_MASK)) {
3256 if ((*node_flags) & LYS_STATUS_CURR) {
3257 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3258 "A \"current\" status is in conflict with the parent's \"%s\" status.",
3259 (parent_flags & LYS_STATUS_DEPRC) ? "deprecated" : "obsolete");
3260 } else { /* LYS_STATUS_DEPRC */
3261 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3262 "A \"deprecated\" status is in conflict with the parent's \"obsolete\" status.");
3263 }
3264 return LY_EVALID;
3265 }
3266 }
3267 return LY_SUCCESS;
3268}
3269
Radek Krejci8cce8532019-03-05 11:27:45 +01003270/**
3271 * @brief Check uniqness of the node/action/notification name.
3272 *
3273 * Data nodes, actions/RPCs and Notifications are stored separately (in distinguish lists) in the schema
3274 * structures, but they share the namespace so we need to check their name collisions.
3275 *
3276 * @param[in] ctx Compile context.
3277 * @param[in] children List (linked list) of data nodes to go through.
3278 * @param[in] actions List (sized array) of actions or RPCs to go through.
3279 * @param[in] notifs List (sized array) of Notifications to go through.
3280 * @param[in] name Name of the item to find in the given lists.
3281 * @param[in] exclude Pointer to an object to exclude from the name checking - for the case that the object
3282 * with the @p name being checked is already inserted into one of the list so we need to skip it when searching for duplicity.
3283 * @return LY_SUCCESS in case of unique name, LY_EEXIST otherwise.
3284 */
3285static LY_ERR
3286lys_compile_node_uniqness(struct lysc_ctx *ctx, const struct lysc_node *children,
3287 const struct lysc_action *actions, const struct lysc_notif *notifs,
3288 const char *name, void *exclude)
3289{
3290 const struct lysc_node *iter;
3291 unsigned int u;
3292
3293 LY_LIST_FOR(children, iter) {
3294 if (iter != exclude && iter->module == ctx->mod && !strcmp(name, iter->name)) {
3295 goto error;
3296 }
3297 }
3298 LY_ARRAY_FOR(actions, u) {
3299 if (&actions[u] != exclude && actions[u].module == ctx->mod && !strcmp(name, actions[u].name)) {
3300 goto error;
3301 }
3302 }
3303 LY_ARRAY_FOR(notifs, u) {
3304 if (&notifs[u] != exclude && notifs[u].module == ctx->mod && !strcmp(name, notifs[u].name)) {
3305 goto error;
3306 }
3307 }
3308 return LY_SUCCESS;
3309error:
3310 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, name, "data definition/RPC/action/Notification");
3311 return LY_EEXIST;
3312}
3313
Radek Krejciec4da802019-05-02 13:02:41 +02003314static 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 +01003315
Radek Krejcia3045382018-11-22 14:30:31 +01003316/**
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003317 * @brief Compile parsed RPC/action schema node information.
3318 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003319 * @param[in] action_p Parsed RPC/action schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003320 * @param[in] parent Parent node of the action, NULL in case of RPC (top-level action)
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003321 * @param[in,out] action Prepared (empty) compiled action structure to fill.
3322 * @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).
3323 * Zero means no uses, non-zero value with no status bit set mean the default status.
3324 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3325 */
3326static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003327lys_compile_action(struct lysc_ctx *ctx, struct lysp_action *action_p,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003328 struct lysc_node *parent, struct lysc_action *action, uint16_t uses_status)
3329{
3330 LY_ERR ret = LY_SUCCESS;
3331 struct lysp_node *child_p;
3332 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003333 int opt_prev = ctx->options;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003334
Radek Krejci327de162019-06-14 12:52:07 +02003335 lysc_update_path(ctx, parent, action_p->name);
3336
Radek Krejci8cce8532019-03-05 11:27:45 +01003337 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3338 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3339 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3340 action_p->name, action)) {
3341 return LY_EVALID;
3342 }
3343
Radek Krejciec4da802019-05-02 13:02:41 +02003344 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejci05b774b2019-02-25 13:26:18 +01003345 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003346 "Action \"%s\" is placed inside %s.", action_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003347 ctx->options & LYSC_OPT_RPC_MASK ? "another RPC/action" : "Notification");
Radek Krejci05b774b2019-02-25 13:26:18 +01003348 return LY_EVALID;
3349 }
3350
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003351 action->nodetype = LYS_ACTION;
3352 action->module = ctx->mod;
3353 action->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003354 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003355 action->sp = action_p;
3356 }
3357 action->flags = action_p->flags & LYS_FLAGS_COMPILED_MASK;
3358
3359 /* status - it is not inherited by specification, but it does not make sense to have
3360 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3361 LY_CHECK_RET(lys_compile_status(ctx, &action->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3362
3363 DUP_STRING(ctx->ctx, action_p->name, action->name);
3364 DUP_STRING(ctx->ctx, action_p->dsc, action->dsc);
3365 DUP_STRING(ctx->ctx, action_p->ref, action->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003366 COMPILE_ARRAY_GOTO(ctx, action_p->iffeatures, action->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3367 COMPILE_ARRAY_GOTO(ctx, action_p->exts, action->exts, u, lys_compile_ext, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003368
3369 /* input */
Radek Krejci327de162019-06-14 12:52:07 +02003370 lysc_update_path(ctx, (struct lysc_node*)action, "input");
Radek Krejciec4da802019-05-02 13:02:41 +02003371 COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, u, lys_compile_must, ret, cleanup);
3372 COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input_exts, u, lys_compile_ext, ret, cleanup);
3373 ctx->options |= LYSC_OPT_RPC_INPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003374 LY_LIST_FOR(action_p->input.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003375 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003376 }
Radek Krejci327de162019-06-14 12:52:07 +02003377 lysc_update_path(ctx, NULL, NULL);
Radek Krejciec4da802019-05-02 13:02:41 +02003378 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003379
3380 /* output */
Radek Krejci327de162019-06-14 12:52:07 +02003381 lysc_update_path(ctx, (struct lysc_node*)action, "output");
Radek Krejciec4da802019-05-02 13:02:41 +02003382 COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, u, lys_compile_must, ret, cleanup);
3383 COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output_exts, u, lys_compile_ext, ret, cleanup);
3384 ctx->options |= LYSC_OPT_RPC_OUTPUT;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003385 LY_LIST_FOR(action_p->output.data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003386 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)action, uses_status));
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003387 }
Radek Krejci327de162019-06-14 12:52:07 +02003388 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003389
Radek Krejci327de162019-06-14 12:52:07 +02003390 lysc_update_path(ctx, NULL, NULL);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003391cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003392 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003393 return ret;
3394}
3395
3396/**
Radek Krejci43981a32019-04-12 09:44:11 +02003397 * @brief Compile parsed Notification schema node information.
Radek Krejcifc11bd72019-04-11 16:00:05 +02003398 * @param[in] ctx Compile context
Radek Krejci43981a32019-04-12 09:44:11 +02003399 * @param[in] notif_p Parsed Notification schema node.
Radek Krejci43981a32019-04-12 09:44:11 +02003400 * @param[in] parent Parent node of the Notification, NULL in case of top-level Notification
3401 * @param[in,out] notif Prepared (empty) compiled notification structure to fill.
3402 * @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 +02003403 * Zero means no uses, non-zero value with no status bit set mean the default status.
3404 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3405 */
3406static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003407lys_compile_notif(struct lysc_ctx *ctx, struct lysp_notif *notif_p,
Radek Krejcifc11bd72019-04-11 16:00:05 +02003408 struct lysc_node *parent, struct lysc_notif *notif, uint16_t uses_status)
3409{
3410 LY_ERR ret = LY_SUCCESS;
3411 struct lysp_node *child_p;
3412 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02003413 int opt_prev = ctx->options;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003414
Radek Krejci327de162019-06-14 12:52:07 +02003415 lysc_update_path(ctx, parent, notif_p->name);
3416
Radek Krejcifc11bd72019-04-11 16:00:05 +02003417 if (lys_compile_node_uniqness(ctx, parent ? lysc_node_children(parent, 0) : ctx->mod->compiled->data,
3418 parent ? lysc_node_actions(parent) : ctx->mod->compiled->rpcs,
3419 parent ? lysc_node_notifs(parent) : ctx->mod->compiled->notifs,
3420 notif_p->name, notif)) {
3421 return LY_EVALID;
3422 }
3423
Radek Krejciec4da802019-05-02 13:02:41 +02003424 if (ctx->options & (LYSC_OPT_RPC_MASK | LYSC_OPT_NOTIFICATION)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003425 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3426 "Notification \"%s\" is placed inside %s.", notif_p->name,
Radek Krejciec4da802019-05-02 13:02:41 +02003427 ctx->options & LYSC_OPT_RPC_MASK ? "RPC/action" : "another Notification");
Radek Krejcifc11bd72019-04-11 16:00:05 +02003428 return LY_EVALID;
3429 }
3430
3431 notif->nodetype = LYS_NOTIF;
3432 notif->module = ctx->mod;
3433 notif->parent = parent;
Radek Krejciec4da802019-05-02 13:02:41 +02003434 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02003435 notif->sp = notif_p;
3436 }
3437 notif->flags = notif_p->flags & LYS_FLAGS_COMPILED_MASK;
3438
3439 /* status - it is not inherited by specification, but it does not make sense to have
3440 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
3441 LY_CHECK_RET(lys_compile_status(ctx, &notif->flags, uses_status ? uses_status : (parent ? parent->flags : 0)));
3442
3443 DUP_STRING(ctx->ctx, notif_p->name, notif->name);
3444 DUP_STRING(ctx->ctx, notif_p->dsc, notif->dsc);
3445 DUP_STRING(ctx->ctx, notif_p->ref, notif->ref);
Radek Krejciec4da802019-05-02 13:02:41 +02003446 COMPILE_ARRAY_GOTO(ctx, notif_p->iffeatures, notif->iffeatures, u, lys_compile_iffeature, ret, cleanup);
3447 COMPILE_ARRAY_GOTO(ctx, notif_p->exts, notif->exts, u, lys_compile_ext, ret, cleanup);
3448 COMPILE_ARRAY_GOTO(ctx, notif_p->musts, notif->musts, u, lys_compile_must, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003449
Radek Krejciec4da802019-05-02 13:02:41 +02003450 ctx->options |= LYSC_OPT_NOTIFICATION;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003451 LY_LIST_FOR(notif_p->data, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003452 LY_CHECK_RET(lys_compile_node(ctx, child_p, (struct lysc_node*)notif, uses_status));
Radek Krejcifc11bd72019-04-11 16:00:05 +02003453 }
3454
Radek Krejci327de162019-06-14 12:52:07 +02003455 lysc_update_path(ctx, NULL, NULL);
Radek Krejcifc11bd72019-04-11 16:00:05 +02003456cleanup:
Radek Krejciec4da802019-05-02 13:02:41 +02003457 ctx->options = opt_prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02003458 return ret;
3459}
3460
3461/**
Radek Krejcia3045382018-11-22 14:30:31 +01003462 * @brief Compile parsed container node information.
3463 * @param[in] ctx Compile context
3464 * @param[in] node_p Parsed container node.
Radek Krejcia3045382018-11-22 14:30:31 +01003465 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3466 * is enriched with the container-specific information.
3467 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3468 */
Radek Krejci19a96102018-11-15 13:38:09 +01003469static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003470lys_compile_node_container(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003471{
3472 struct lysp_node_container *cont_p = (struct lysp_node_container*)node_p;
3473 struct lysc_node_container *cont = (struct lysc_node_container*)node;
3474 struct lysp_node *child_p;
3475 unsigned int u;
3476 LY_ERR ret = LY_SUCCESS;
3477
Radek Krejcife909632019-02-12 15:34:42 +01003478 if (cont_p->presence) {
3479 cont->flags |= LYS_PRESENCE;
3480 }
3481
Radek Krejci19a96102018-11-15 13:38:09 +01003482 LY_LIST_FOR(cont_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003483 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci19a96102018-11-15 13:38:09 +01003484 }
3485
Radek Krejciec4da802019-05-02 13:02:41 +02003486 COMPILE_ARRAY_GOTO(ctx, cont_p->musts, cont->musts, u, lys_compile_must, ret, done);
3487 COMPILE_ARRAY1_GOTO(ctx, cont_p->actions, cont->actions, node, u, lys_compile_action, 0, ret, done);
3488 COMPILE_ARRAY1_GOTO(ctx, cont_p->notifs, cont->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci19a96102018-11-15 13:38:09 +01003489
3490done:
3491 return ret;
3492}
3493
Radek Krejci33f72892019-02-21 10:36:58 +01003494/*
3495 * @brief Compile type in leaf/leaf-list node and do all the necessary checks.
3496 * @param[in] ctx Compile context.
3497 * @param[in] context_node Schema node where the type/typedef is placed to correctly find the base types.
3498 * @param[in] type_p Parsed type to compile.
Radek Krejci33f72892019-02-21 10:36:58 +01003499 * @param[in,out] leaf Compiled leaf structure (possibly cast leaf-list) to provide node information and to store the compiled type information.
3500 * @return LY_ERR value.
3501 */
3502static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003503lys_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 +01003504{
Radek Krejcia1911222019-07-22 17:24:50 +02003505 unsigned int u;
Radek Krejci33f72892019-02-21 10:36:58 +01003506 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)leaf;
3507
Radek Krejciec4da802019-05-02 13:02:41 +02003508 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 +01003509 leaf->units ? NULL : &leaf->units));
3510 if (leaf->nodetype == LYS_LEAFLIST) {
3511 if (llist->type->dflt && !llist->dflts && !llist->min) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003512 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts_mods, 1, LY_EMEM);
3513 llist->dflts_mods[0] = llist->type->dflt_mod;
Radek Krejci33f72892019-02-21 10:36:58 +01003514 LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, 1, LY_EMEM);
Radek Krejcia1911222019-07-22 17:24:50 +02003515 llist->dflts[0] = calloc(1, sizeof *llist->dflts[0]);
3516 llist->dflts[0]->realtype = llist->type->dflt->realtype;
3517 llist->dflts[0]->realtype->plugin->duplicate(ctx->ctx, llist->type->dflt, llist->dflts[0]);
3518 llist->dflts[0]->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003519 LY_ARRAY_INCREMENT(llist->dflts);
3520 }
3521 } else {
3522 if (leaf->type->dflt && !leaf->dflt && !(leaf->flags & LYS_MAND_TRUE)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003523 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02003524 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3525 leaf->dflt->realtype = leaf->type->dflt->realtype;
3526 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
3527 leaf->dflt->realtype->refcount++;
Radek Krejci33f72892019-02-21 10:36:58 +01003528 }
3529 }
3530 if (leaf->type->basetype == LY_TYPE_LEAFREF) {
3531 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3532 ly_set_add(&ctx->unres, leaf, 0);
3533 } else if (leaf->type->basetype == LY_TYPE_UNION) {
3534 LY_ARRAY_FOR(((struct lysc_type_union*)leaf->type)->types, u) {
3535 if (((struct lysc_type_union*)leaf->type)->types[u]->basetype == LY_TYPE_LEAFREF) {
3536 /* store to validate the path in the current context at the end of schema compiling when all the nodes are present */
3537 ly_set_add(&ctx->unres, leaf, 0);
3538 }
3539 }
3540 } else if (leaf->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci33f72892019-02-21 10:36:58 +01003541 if (leaf->nodetype == LYS_LEAFLIST && ctx->mod_def->version < LYS_VERSION_1_1) {
3542 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3543 "Leaf-list of type \"empty\" is allowed only in YANG 1.1 modules.");
3544 return LY_EVALID;
3545 }
3546 }
3547
Radek Krejci33f72892019-02-21 10:36:58 +01003548 return LY_SUCCESS;
3549}
3550
Radek Krejcia3045382018-11-22 14:30:31 +01003551/**
3552 * @brief Compile parsed leaf node information.
3553 * @param[in] ctx Compile context
3554 * @param[in] node_p Parsed leaf node.
Radek Krejcia3045382018-11-22 14:30:31 +01003555 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3556 * is enriched with the leaf-specific information.
3557 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3558 */
Radek Krejci19a96102018-11-15 13:38:09 +01003559static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003560lys_compile_node_leaf(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci19a96102018-11-15 13:38:09 +01003561{
3562 struct lysp_node_leaf *leaf_p = (struct lysp_node_leaf*)node_p;
3563 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
3564 unsigned int u;
3565 LY_ERR ret = LY_SUCCESS;
3566
Radek Krejciec4da802019-05-02 13:02:41 +02003567 COMPILE_ARRAY_GOTO(ctx, leaf_p->musts, leaf->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003568 if (leaf_p->units) {
3569 leaf->units = lydict_insert(ctx->ctx, leaf_p->units, 0);
3570 leaf->flags |= LYS_SET_UNITS;
3571 }
Radek Krejcia1911222019-07-22 17:24:50 +02003572
3573 /* the dflt member is just filled to avoid getting the default value from the type */
3574 leaf->dflt = (void*)leaf_p->dflt;
3575 ret = lys_compile_node_type(ctx, node_p, &leaf_p->type, leaf);
3576
Radek Krejciccd20f12019-02-15 14:12:27 +01003577 if (leaf_p->dflt) {
Radek Krejcia1911222019-07-22 17:24:50 +02003578 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003579 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02003580 leaf->dflt = calloc(1, sizeof *leaf->dflt);
3581 leaf->dflt->realtype = leaf->type;
3582 ret = leaf->type->plugin->store(ctx->ctx, leaf->type, leaf_p->dflt, strlen(leaf_p->dflt),
3583 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003584 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003585 leaf->dflt->realtype->refcount++;
3586 if (err) {
3587 ly_err_print(err);
3588 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3589 "Invalid leaf's default value \"%s\" which does not fit the type (%s).", leaf_p->dflt, err->msg);
3590 ly_err_free(err);
3591 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003592 if (ret == LY_EINCOMPLETE) {
Radek Krejci1c0c3442019-07-23 16:08:47 +02003593 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003594 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003595
3596 /* but in general result is so far ok */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003597 ret = LY_SUCCESS;
3598 }
Radek Krejcia1911222019-07-22 17:24:50 +02003599 LY_CHECK_GOTO(ret, done);
Radek Krejci76b3e962018-12-14 17:01:25 +01003600 leaf->flags |= LYS_SET_DFLT;
3601 }
Radek Krejci43699232018-11-23 14:59:46 +01003602
Radek Krejcib1a5dcc2018-11-26 14:50:05 +01003603
Radek Krejci19a96102018-11-15 13:38:09 +01003604done:
3605 return ret;
3606}
3607
Radek Krejcia3045382018-11-22 14:30:31 +01003608/**
Radek Krejci0e5d8382018-11-28 16:37:53 +01003609 * @brief Compile parsed leaf-list node information.
3610 * @param[in] ctx Compile context
3611 * @param[in] node_p Parsed leaf-list node.
Radek Krejci0e5d8382018-11-28 16:37:53 +01003612 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3613 * is enriched with the leaf-list-specific information.
3614 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3615 */
3616static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003617lys_compile_node_leaflist(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci0e5d8382018-11-28 16:37:53 +01003618{
3619 struct lysp_node_leaflist *llist_p = (struct lysp_node_leaflist*)node_p;
3620 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
Radek Krejcia1911222019-07-22 17:24:50 +02003621 unsigned int u, v;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003622 LY_ERR ret = LY_SUCCESS;
3623
Radek Krejciec4da802019-05-02 13:02:41 +02003624 COMPILE_ARRAY_GOTO(ctx, llist_p->musts, llist->musts, u, lys_compile_must, ret, done);
Radek Krejciccd20f12019-02-15 14:12:27 +01003625 if (llist_p->units) {
3626 llist->units = lydict_insert(ctx->ctx, llist_p->units, 0);
3627 llist->flags |= LYS_SET_UNITS;
3628 }
Radek Krejci0e5d8382018-11-28 16:37:53 +01003629
Radek Krejcia1911222019-07-22 17:24:50 +02003630 /* the dflts member is just filled to avoid getting the default value from the type */
3631 llist->dflts = (void*)llist_p->dflts;
3632 ret = lys_compile_node_type(ctx, node_p, &llist_p->type, (struct lysc_node_leaf*)llist);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003633 if (llist_p->dflts) {
Radek Krejcia1911222019-07-22 17:24:50 +02003634 llist->dflts = NULL; /* reset the temporary llist_p->dflts */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003635 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003636 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(llist_p->dflts), ret, done);
3637 LY_ARRAY_FOR(llist_p->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02003638 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003639 LY_ARRAY_INCREMENT(llist->dflts_mods);
3640 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003641 LY_ARRAY_INCREMENT(llist->dflts);
Radek Krejcia1911222019-07-22 17:24:50 +02003642 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
3643 llist->dflts[u]->realtype = llist->type;
3644 ret = llist->type->plugin->store(ctx->ctx, llist->type, llist_p->dflts[u], strlen(llist_p->dflts[u]),
3645 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02003646 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02003647 llist->dflts[u]->realtype->refcount++;
3648 if (err) {
3649 ly_err_print(err);
3650 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3651 "Invalid leaf-lists's default value \"%s\" which does not fit the type (%s).", llist_p->dflts[u], err->msg);
3652 ly_err_free(err);
3653 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02003654 if (ret == LY_EINCOMPLETE) {
3655 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02003656 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), done);
Radek Krejci1c0c3442019-07-23 16:08:47 +02003657
3658 /* but in general result is so far ok */
3659 ret = LY_SUCCESS;
3660 }
Radek Krejcia1911222019-07-22 17:24:50 +02003661 LY_CHECK_GOTO(ret, done);
Radek Krejci0e5d8382018-11-28 16:37:53 +01003662 }
Radek Krejciccd20f12019-02-15 14:12:27 +01003663 llist->flags |= LYS_SET_DFLT;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003664 }
Radek Krejcia1911222019-07-22 17:24:50 +02003665 if ((llist->flags & LYS_CONFIG_W) && llist->dflts && LY_ARRAY_SIZE(llist->dflts)) {
3666 /* configuration data values must be unique - so check the default values */
3667 LY_ARRAY_FOR(llist->dflts, u) {
3668 for (v = u + 1; v < LY_ARRAY_SIZE(llist->dflts); ++v) {
3669 if (!llist->type->plugin->compare(llist->dflts[u], llist->dflts[v])) {
3670 int dynamic = 0;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02003671 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 +02003672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3673 "Configuration leaf-list has multiple defaults of the same value \"%s\".", val);
3674 if (dynamic) {
3675 free((char*)val);
3676 }
3677 return LY_EVALID;
3678 }
3679 }
3680 }
3681 }
3682
3683 /* 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 +01003684
3685 llist->min = llist_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003686 if (llist->min) {
3687 llist->flags |= LYS_MAND_TRUE;
3688 }
Radek Krejcib7408632018-11-28 17:12:11 +01003689 llist->max = llist_p->max ? llist_p->max : (uint32_t)-1;
Radek Krejci0e5d8382018-11-28 16:37:53 +01003690
Radek Krejci0e5d8382018-11-28 16:37:53 +01003691done:
3692 return ret;
3693}
3694
3695/**
Radek Krejci7af64242019-02-18 13:07:53 +01003696 * @brief Compile information about list's uniques.
3697 * @param[in] ctx Compile context.
3698 * @param[in] context_module Module where the prefixes are going to be resolved.
3699 * @param[in] uniques Sized array list of unique statements.
3700 * @param[in] list Compiled list where the uniques are supposed to be resolved and stored.
3701 * @return LY_ERR value.
3702 */
3703static LY_ERR
3704lys_compile_node_list_unique(struct lysc_ctx *ctx, struct lys_module *context_module, const char **uniques, struct lysc_node_list *list)
3705{
3706 LY_ERR ret = LY_SUCCESS;
3707 struct lysc_node_leaf **key, ***unique;
3708 const char *keystr, *delim;
3709 size_t len;
3710 unsigned int v;
3711 int config;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003712 uint16_t flags;
Radek Krejci7af64242019-02-18 13:07:53 +01003713
3714 for (v = 0; v < LY_ARRAY_SIZE(uniques); ++v) {
3715 config = -1;
3716 LY_ARRAY_NEW_RET(ctx->ctx, list->uniques, unique, LY_EMEM);
3717 keystr = uniques[v];
3718 while (keystr) {
3719 delim = strpbrk(keystr, " \t\n");
3720 if (delim) {
3721 len = delim - keystr;
3722 while (isspace(*delim)) {
3723 ++delim;
3724 }
3725 } else {
3726 len = strlen(keystr);
3727 }
3728
3729 /* unique node must be present */
3730 LY_ARRAY_NEW_RET(ctx->ctx, *unique, key, LY_EMEM);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003731 ret = lys_resolve_schema_nodeid(ctx, keystr, len, (struct lysc_node*)list, context_module, LYS_LEAF, 0,
3732 (const struct lysc_node**)key, &flags);
Radek Krejci7af64242019-02-18 13:07:53 +01003733 if (ret != LY_SUCCESS) {
3734 if (ret == LY_EDENIED) {
3735 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003736 "Unique's descendant-schema-nodeid \"%.*s\" refers to %s node instead of a leaf.",
Radek Krejci7af64242019-02-18 13:07:53 +01003737 len, keystr, lys_nodetype2str((*key)->nodetype));
3738 }
3739 return LY_EVALID;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003740 } else if (flags) {
3741 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3742 "Unique's descendant-schema-nodeid \"%.*s\" refers into %s node.",
3743 len, keystr, flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
3744 return LY_EVALID;
Radek Krejci7af64242019-02-18 13:07:53 +01003745 }
3746
Radek Krejci6eeb58f2019-02-22 16:29:37 +01003747
Radek Krejci7af64242019-02-18 13:07:53 +01003748 /* all referenced leafs must be of the same config type */
3749 if (config != -1 && ((((*key)->flags & LYS_CONFIG_W) && config == 0) || (((*key)->flags & LYS_CONFIG_R) && config == 1))) {
3750 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3751 "Unique statement \"%s\" refers to leafs with different config type.", uniques[v]);
3752 return LY_EVALID;
3753 } else if ((*key)->flags & LYS_CONFIG_W) {
3754 config = 1;
3755 } else { /* LYS_CONFIG_R */
3756 config = 0;
3757 }
3758
3759 /* check status */
3760 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
3761 (*key)->flags, (*key)->module, (*key)->name));
3762
3763 /* mark leaf as unique */
3764 (*key)->flags |= LYS_UNIQUE;
3765
3766 /* next unique value in line */
3767 keystr = delim;
3768 }
3769 /* next unique definition */
3770 }
3771
3772 return LY_SUCCESS;
3773}
3774
3775/**
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003776 * @brief Compile parsed list node information.
3777 * @param[in] ctx Compile context
3778 * @param[in] node_p Parsed list node.
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003779 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
3780 * is enriched with the list-specific information.
3781 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
3782 */
3783static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02003784lys_compile_node_list(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003785{
3786 struct lysp_node_list *list_p = (struct lysp_node_list*)node_p;
3787 struct lysc_node_list *list = (struct lysc_node_list*)node;
3788 struct lysp_node *child_p;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003789 struct lysc_node_leaf *key, *prev_key = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003790 size_t len;
Radek Krejci7af64242019-02-18 13:07:53 +01003791 unsigned int u;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003792 const char *keystr, *delim;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003793 LY_ERR ret = LY_SUCCESS;
3794
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003795 list->min = list_p->min;
Radek Krejcife909632019-02-12 15:34:42 +01003796 if (list->min) {
3797 list->flags |= LYS_MAND_TRUE;
3798 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003799 list->max = list_p->max ? list_p->max : (uint32_t)-1;
3800
3801 LY_LIST_FOR(list_p->child, child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02003802 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003803 }
3804
Radek Krejciec4da802019-05-02 13:02:41 +02003805 COMPILE_ARRAY_GOTO(ctx, list_p->musts, list->musts, u, lys_compile_must, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003806
3807 /* keys */
3808 if ((list->flags & LYS_CONFIG_W) && (!list_p->key || !list_p->key[0])) {
3809 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Missing key in list representing configuration data.");
3810 return LY_EVALID;
3811 }
3812
3813 /* find all the keys (must be direct children) */
3814 keystr = list_p->key;
Radek Krejci0fe9b512019-07-26 17:51:05 +02003815 if (!keystr) {
3816 /* keyless list */
3817 list->flags |= LYS_KEYLESS;
3818 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003819 while (keystr) {
3820 delim = strpbrk(keystr, " \t\n");
3821 if (delim) {
3822 len = delim - keystr;
3823 while (isspace(*delim)) {
3824 ++delim;
3825 }
3826 } else {
3827 len = strlen(keystr);
3828 }
3829
3830 /* key node must be present */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003831 key = (struct lysc_node_leaf*)lys_child(node, node->module, keystr, len, LYS_LEAF, LYS_GETNEXT_NOCHOICE | LYS_GETNEXT_NOSTATECHECK);
3832 if (!(key)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3834 "The list's key \"%.*s\" not found.", len, keystr);
3835 return LY_EVALID;
3836 }
3837 /* keys must be unique */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003838 if (key->flags & LYS_KEY) {
3839 /* the node was already marked as a key */
3840 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3841 "Duplicated key identifier \"%.*s\".", len, keystr);
3842 return LY_EVALID;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003843 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003844
3845 lysc_update_path(ctx, (struct lysc_node*)list, key->name);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003846 /* key must have the same config flag as the list itself */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003847 if ((list->flags & LYS_CONFIG_MASK) != (key->flags & LYS_CONFIG_MASK)) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003848 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Key of the configuration list must not be status leaf.");
3849 return LY_EVALID;
3850 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003851 if (ctx->mod_def->version < LYS_VERSION_1_1) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003852 /* YANG 1.0 denies key to be of empty type */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003853 if (key->type->basetype == LY_TYPE_EMPTY) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003854 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003855 "List's key cannot be of \"empty\" type until it is in YANG 1.1 module.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003856 return LY_EVALID;
3857 }
3858 } else {
3859 /* when and if-feature are illegal on list keys */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003860 if (key->when) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003861 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003862 "List's key must not have any \"when\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003863 return LY_EVALID;
3864 }
Radek Krejci0fe9b512019-07-26 17:51:05 +02003865 if (key->iffeatures) {
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003866 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02003867 "List's key must not have any \"if-feature\" statement.");
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003868 return LY_EVALID;
3869 }
3870 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003871
3872 /* check status */
3873 LY_CHECK_RET(lysc_check_status(ctx, list->flags, list->module, list->name,
Radek Krejci0fe9b512019-07-26 17:51:05 +02003874 key->flags, key->module, key->name));
Radek Krejci76b3e962018-12-14 17:01:25 +01003875
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003876 /* ignore default values of the key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003877 if (key->dflt) {
3878 key->dflt->realtype->plugin->free(ctx->ctx, key->dflt);
3879 lysc_type_free(ctx->ctx, key->dflt->realtype);
3880 free(key->dflt);
3881 key->dflt = NULL;
3882 key->dflt_mod = NULL;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003883 }
3884 /* mark leaf as key */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003885 key->flags |= LYS_KEY;
3886
3887 /* move it to the correct position */
3888 if ((prev_key && (struct lysc_node*)prev_key != key->prev) || (!prev_key && key->prev->next)) {
3889 /* fix links in closest previous siblings of the key */
3890 if (key->next) {
3891 key->next->prev = key->prev;
3892 } else {
3893 /* last child */
3894 list->child->prev = key->prev;
3895 }
3896 if (key->prev->next) {
3897 key->prev->next = key->next;
3898 }
3899 /* fix links in the key */
3900 if (prev_key) {
3901 key->prev = (struct lysc_node*)prev_key;
3902 key->next = prev_key->next;
3903 } else {
3904 key->prev = list->child->prev;
3905 key->next = list->child;
3906 }
3907 /* fix links in closes future siblings of the key */
3908 if (prev_key) {
3909 if (prev_key->next) {
3910 prev_key->next->prev = (struct lysc_node*)key;
3911 } else {
3912 list->child->prev = (struct lysc_node*)key;
3913 }
3914 prev_key->next = (struct lysc_node*)key;
3915 } else {
3916 list->child->prev = (struct lysc_node*)key;
3917 }
3918 /* fix links in parent */
3919 if (!key->prev->next) {
3920 list->child = (struct lysc_node*)key;
3921 }
3922 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003923
3924 /* next key value */
Radek Krejci0fe9b512019-07-26 17:51:05 +02003925 prev_key = key;
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003926 keystr = delim;
Radek Krejci327de162019-06-14 12:52:07 +02003927 lysc_update_path(ctx, NULL, NULL);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003928 }
3929
3930 /* uniques */
3931 if (list_p->uniques) {
Radek Krejci7af64242019-02-18 13:07:53 +01003932 LY_CHECK_RET(lys_compile_node_list_unique(ctx, list->module, list_p->uniques, list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003933 }
3934
Radek Krejciec4da802019-05-02 13:02:41 +02003935 COMPILE_ARRAY1_GOTO(ctx, list_p->actions, list->actions, node, u, lys_compile_action, 0, ret, done);
3936 COMPILE_ARRAY1_GOTO(ctx, list_p->notifs, list->notifs, node, u, lys_compile_notif, 0, ret, done);
Radek Krejci9bb94eb2018-12-04 16:48:35 +01003937
3938done:
3939 return ret;
3940}
3941
Radek Krejcib56c7502019-02-13 14:19:54 +01003942/**
3943 * @brief Do some checks and set the default choice's case.
3944 *
3945 * Selects (and stores into ::lysc_node_choice#dflt) the default case and set LYS_SET_DFLT flag on it.
3946 *
3947 * @param[in] ctx Compile context.
3948 * @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,
3949 * not the reference to the imported module.
3950 * @param[in,out] ch The compiled choice node, its dflt member is filled to point to the default case node of the choice.
3951 * @return LY_ERR value.
3952 */
Radek Krejci76b3e962018-12-14 17:01:25 +01003953static LY_ERR
3954lys_compile_node_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
3955{
3956 struct lysc_node *iter, *node = (struct lysc_node*)ch;
3957 const char *prefix = NULL, *name;
3958 size_t prefix_len = 0;
3959
3960 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
3961 name = strchr(dflt, ':');
3962 if (name) {
3963 prefix = dflt;
3964 prefix_len = name - prefix;
3965 ++name;
3966 } else {
3967 name = dflt;
3968 }
Radek Krejci0bcdaed2019-01-10 10:21:34 +01003969 if (prefix && (strncmp(prefix, node->module->prefix, prefix_len) || node->module->prefix[prefix_len] != '\0')) {
Radek Krejci76b3e962018-12-14 17:01:25 +01003970 /* prefixed default case make sense only for the prefix of the schema itself */
3971 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
3972 "Invalid default case referencing a case from different YANG module (by prefix \"%.*s\").",
3973 prefix_len, prefix);
3974 return LY_EVALID;
3975 }
3976 ch->dflt = (struct lysc_node_case*)lys_child(node, node->module, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
3977 if (!ch->dflt) {
3978 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3979 "Default case \"%s\" not found.", dflt);
3980 return LY_EVALID;
3981 }
3982 /* no mandatory nodes directly under the default case */
3983 LY_LIST_FOR(ch->dflt->child, iter) {
Radek Krejcife13da42019-02-15 14:51:01 +01003984 if (iter->parent != (struct lysc_node*)ch->dflt) {
3985 break;
3986 }
Radek Krejci76b3e962018-12-14 17:01:25 +01003987 if (iter->flags & LYS_MAND_TRUE) {
3988 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
3989 "Mandatory node \"%s\" under the default case \"%s\".", iter->name, dflt);
3990 return LY_EVALID;
3991 }
3992 }
Radek Krejci01342af2019-01-03 15:18:08 +01003993 ch->dflt->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01003994 return LY_SUCCESS;
3995}
3996
Radek Krejciccd20f12019-02-15 14:12:27 +01003997static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02003998lys_compile_deviation_set_choice_dflt(struct lysc_ctx *ctx, const char *dflt, struct lysc_node_choice *ch)
Radek Krejciccd20f12019-02-15 14:12:27 +01003999{
4000 struct lys_module *mod;
4001 const char *prefix = NULL, *name;
4002 size_t prefix_len = 0;
4003 struct lysc_node_case *cs;
4004 struct lysc_node *node;
4005
4006 /* could use lys_parse_nodeid(), but it checks syntax which is already done in this case by the parsers */
4007 name = strchr(dflt, ':');
4008 if (name) {
4009 prefix = dflt;
4010 prefix_len = name - prefix;
4011 ++name;
4012 } else {
4013 name = dflt;
4014 }
4015 /* this code is for deviation, so we allow as the default case even the cases from other modules than the choice (augments) */
4016 if (prefix) {
4017 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
4018 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004019 "Invalid deviation adding \"default\" property \"%s\" of choice. "
4020 "The prefix does not match any imported module of the deviation module.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004021 return LY_EVALID;
4022 }
4023 } else {
4024 mod = ctx->mod;
4025 }
4026 /* get the default case */
4027 cs = (struct lysc_node_case*)lys_child((struct lysc_node*)ch, mod, name, 0, LYS_CASE, LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCASE);
4028 if (!cs) {
4029 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004030 "Invalid deviation adding \"default\" property \"%s\" of choice - the specified case does not exists.", dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01004031 return LY_EVALID;
4032 }
4033
4034 /* check that there is no mandatory node */
4035 LY_LIST_FOR(cs->child, node) {
Radek Krejcife13da42019-02-15 14:51:01 +01004036 if (node->parent != (struct lysc_node*)cs) {
4037 break;
4038 }
Radek Krejciccd20f12019-02-15 14:12:27 +01004039 if (node->flags & LYS_MAND_TRUE) {
4040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004041 "Invalid deviation adding \"default\" property \"%s\" of choice - "
4042 "mandatory node \"%s\" under the default case.", dflt, node->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01004043 return LY_EVALID;
4044 }
4045 }
4046
4047 /* set the default case in choice */
4048 ch->dflt = cs;
4049 cs->flags |= LYS_SET_DFLT;
4050
4051 return LY_SUCCESS;
4052}
4053
Radek Krejci9bb94eb2018-12-04 16:48:35 +01004054/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004055 * @brief Compile parsed choice node information.
4056 * @param[in] ctx Compile context
4057 * @param[in] node_p Parsed choice node.
Radek Krejci056d0a82018-12-06 16:57:25 +01004058 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
Radek Krejci76b3e962018-12-14 17:01:25 +01004059 * is enriched with the choice-specific information.
Radek Krejci056d0a82018-12-06 16:57:25 +01004060 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4061 */
4062static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004063lys_compile_node_choice(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004064{
4065 struct lysp_node_choice *ch_p = (struct lysp_node_choice*)node_p;
4066 struct lysc_node_choice *ch = (struct lysc_node_choice*)node;
4067 struct lysp_node *child_p, *case_child_p;
Radek Krejci056d0a82018-12-06 16:57:25 +01004068 LY_ERR ret = LY_SUCCESS;
4069
Radek Krejci056d0a82018-12-06 16:57:25 +01004070 LY_LIST_FOR(ch_p->child, child_p) {
4071 if (child_p->nodetype == LYS_CASE) {
4072 LY_LIST_FOR(((struct lysp_node_case*)child_p)->child, case_child_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004073 LY_CHECK_RET(lys_compile_node(ctx, case_child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004074 }
4075 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004076 LY_CHECK_RET(lys_compile_node(ctx, child_p, node, 0));
Radek Krejci056d0a82018-12-06 16:57:25 +01004077 }
4078 }
4079
4080 /* default branch */
Radek Krejcia9026eb2018-12-12 16:04:47 +01004081 if (ch_p->dflt) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004082 LY_CHECK_RET(lys_compile_node_choice_dflt(ctx, ch_p->dflt, ch));
Radek Krejcia9026eb2018-12-12 16:04:47 +01004083 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004084
Radek Krejci9800fb82018-12-13 14:26:23 +01004085 return ret;
4086}
4087
4088/**
4089 * @brief Compile parsed anydata or anyxml node information.
4090 * @param[in] ctx Compile context
4091 * @param[in] node_p Parsed anydata or anyxml node.
Radek Krejci9800fb82018-12-13 14:26:23 +01004092 * @param[in,out] node Pre-prepared structure from lys_compile_node() with filled generic node information
4093 * is enriched with the any-specific information.
4094 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4095 */
4096static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004097lys_compile_node_any(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysc_node *node)
Radek Krejci9800fb82018-12-13 14:26:23 +01004098{
4099 struct lysp_node_anydata *any_p = (struct lysp_node_anydata*)node_p;
4100 struct lysc_node_anydata *any = (struct lysc_node_anydata*)node;
4101 unsigned int u;
4102 LY_ERR ret = LY_SUCCESS;
4103
Radek Krejciec4da802019-05-02 13:02:41 +02004104 COMPILE_ARRAY_GOTO(ctx, any_p->musts, any->musts, u, lys_compile_must, ret, done);
Radek Krejci9800fb82018-12-13 14:26:23 +01004105
4106 if (any->flags & LYS_CONFIG_W) {
4107 LOGWRN(ctx->ctx, "Use of %s to define configuration data is not recommended.",
4108 ly_stmt2str(any->nodetype == LYS_ANYDATA ? YANG_ANYDATA : YANG_ANYXML));
4109 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004110done:
4111 return ret;
4112}
4113
Radek Krejcib56c7502019-02-13 14:19:54 +01004114/**
Radek Krejci056d0a82018-12-06 16:57:25 +01004115 * @brief Connect the node into the siblings list and check its name uniqueness.
4116 *
4117 * @param[in] ctx Compile context
4118 * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
4119 * the choice itself is expected instead of a specific case node.
4120 * @param[in] node Schema node to connect into the list.
4121 * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
4122 */
4123static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004124lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
Radek Krejci056d0a82018-12-06 16:57:25 +01004125{
4126 struct lysc_node **children;
4127
4128 if (node->nodetype == LYS_CASE) {
4129 children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
4130 } else {
Radek Krejciec4da802019-05-02 13:02:41 +02004131 children = lysc_node_children_p(parent, ctx->options);
Radek Krejci056d0a82018-12-06 16:57:25 +01004132 }
4133 if (children) {
4134 if (!(*children)) {
4135 /* first child */
4136 *children = node;
4137 } else if (*children != node) {
4138 /* by the condition in previous branch we cover the choice/case children
4139 * - the children list is shared by the choice and the the first case, in addition
4140 * the first child of each case must be referenced from the case node. So the node is
4141 * actually always already inserted in case it is the first children - so here such
4142 * a situation actually corresponds to the first branch */
4143 /* insert at the end of the parent's children list */
4144 (*children)->prev->next = node;
4145 node->prev = (*children)->prev;
4146 (*children)->prev = node;
4147
4148 /* check the name uniqueness */
4149 if (lys_compile_node_uniqness(ctx, *children, lysc_node_actions(parent),
4150 lysc_node_notifs(parent), node->name, node)) {
4151 return LY_EEXIST;
4152 }
4153 }
4154 }
4155 return LY_SUCCESS;
4156}
4157
Radek Krejci95710c92019-02-11 15:49:55 +01004158/**
Radek Krejcib56c7502019-02-13 14:19:54 +01004159 * @brief Get the XPath context node for the given schema node.
4160 * @param[in] start The schema node where the XPath expression appears.
4161 * @return The context node to evaluate XPath expression in given schema node.
4162 * @return NULL in case the context node is the root node.
4163 */
4164static struct lysc_node *
4165lysc_xpath_context(struct lysc_node *start)
4166{
4167 for (; start && !(start->nodetype & (LYS_CONTAINER | LYS_LEAF | LYS_LEAFLIST | LYS_LIST | LYS_ANYDATA | LYS_ACTION | LYS_NOTIF));
4168 start = start->parent);
4169 return start;
4170}
4171
4172/**
4173 * @brief Prepare the case structure in choice node for the new data node.
4174 *
4175 * 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
4176 * created in the choice when the first child was processed.
4177 *
4178 * @param[in] ctx Compile context.
Radek Krejci95710c92019-02-11 15:49:55 +01004179 * @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,
4180 * it is the LYS_CHOICE node or LYS_AUGMENT node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004181 * @param[in] ch The compiled choice structure where the new case structures are created (if needed).
4182 * @param[in] child The new data node being part of a case (no matter if explicit or implicit).
4183 * @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,
4184 * it is linked from the case structure only in case it is its first child.
Radek Krejci95710c92019-02-11 15:49:55 +01004185 */
Radek Krejci056d0a82018-12-06 16:57:25 +01004186static struct lysc_node_case*
Radek Krejciec4da802019-05-02 13:02:41 +02004187lys_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 +01004188{
4189 struct lysc_node *iter;
Radek Krejci98b1a662019-04-23 10:25:50 +02004190 struct lysc_node_case *cs = NULL;
Radek Krejci00b874b2019-02-12 10:54:50 +01004191 struct lysc_when **when;
Radek Krejci056d0a82018-12-06 16:57:25 +01004192 unsigned int u;
4193 LY_ERR ret;
4194
Radek Krejci95710c92019-02-11 15:49:55 +01004195#define UNIQUE_CHECK(NAME, MOD) \
Radek Krejci056d0a82018-12-06 16:57:25 +01004196 LY_LIST_FOR((struct lysc_node*)ch->cases, iter) { \
Radek Krejci95710c92019-02-11 15:49:55 +01004197 if (iter->module == MOD && !strcmp(iter->name, NAME)) { \
Radek Krejci056d0a82018-12-06 16:57:25 +01004198 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DUPIDENT, NAME, "case"); \
4199 return NULL; \
4200 } \
4201 }
4202
Radek Krejci95710c92019-02-11 15:49:55 +01004203 if (node_p->nodetype == LYS_CHOICE || node_p->nodetype == LYS_AUGMENT) {
4204 UNIQUE_CHECK(child->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004205
4206 /* we have to add an implicit case node into the parent choice */
4207 cs = calloc(1, sizeof(struct lysc_node_case));
4208 DUP_STRING(ctx->ctx, child->name, cs->name);
4209 cs->flags = ch->flags & LYS_STATUS_MASK;
Radek Krejci95710c92019-02-11 15:49:55 +01004210 } else if (node_p->nodetype == LYS_CASE) {
Radek Krejci056d0a82018-12-06 16:57:25 +01004211 if (ch->cases && (node_p == ch->cases->prev->sp)) {
4212 /* the case is already present since the child is not its first children */
4213 return (struct lysc_node_case*)ch->cases->prev;
4214 }
Radek Krejci95710c92019-02-11 15:49:55 +01004215 UNIQUE_CHECK(node_p->name, ctx->mod);
Radek Krejci056d0a82018-12-06 16:57:25 +01004216
4217 /* explicit parent case is not present (this is its first child) */
4218 cs = calloc(1, sizeof(struct lysc_node_case));
4219 DUP_STRING(ctx->ctx, node_p->name, cs->name);
4220 cs->flags = LYS_STATUS_MASK & node_p->flags;
4221 cs->sp = node_p;
4222
Radek Krejcib1b59152019-01-07 13:21:56 +01004223 /* 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 +02004224 LY_CHECK_GOTO(lys_compile_status(ctx, &cs->flags, ch->flags), error);
Radek Krejci00b874b2019-02-12 10:54:50 +01004225
4226 if (node_p->when) {
4227 LY_ARRAY_NEW_GOTO(ctx->ctx, cs->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02004228 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01004229 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01004230 (*when)->context = lysc_xpath_context(ch->parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004231 }
Radek Krejciec4da802019-05-02 13:02:41 +02004232 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, cs->iffeatures, u, lys_compile_iffeature, ret, error);
Radek Krejci95710c92019-02-11 15:49:55 +01004233 } else {
4234 LOGINT(ctx->ctx);
4235 goto error;
Radek Krejci056d0a82018-12-06 16:57:25 +01004236 }
4237 cs->module = ctx->mod;
4238 cs->prev = (struct lysc_node*)cs;
4239 cs->nodetype = LYS_CASE;
Radek Krejciec4da802019-05-02 13:02:41 +02004240 lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
Radek Krejci056d0a82018-12-06 16:57:25 +01004241 cs->parent = (struct lysc_node*)ch;
4242 cs->child = child;
4243
4244 return cs;
4245error:
Radek Krejci1b1e9252019-04-24 08:45:50 +02004246 if (cs) {
4247 lysc_node_free(ctx->ctx, (struct lysc_node*)cs);
4248 }
Radek Krejci056d0a82018-12-06 16:57:25 +01004249 return NULL;
4250
4251#undef UNIQUE_CHECK
4252}
4253
Radek Krejcib56c7502019-02-13 14:19:54 +01004254/**
Radek Krejci93dcc392019-02-19 10:43:38 +01004255 * @brief Apply refined or deviated config to the target node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004256 *
4257 * @param[in] ctx Compile context.
Radek Krejci93dcc392019-02-19 10:43:38 +01004258 * @param[in] node Target node where the config is supposed to be changed.
4259 * @param[in] config_flag Node's config flag to be applied to the @p node.
Radek Krejcib56c7502019-02-13 14:19:54 +01004260 * @param[in] inheriting Flag (inverted) to check the refined config compatibility with the node's parent. This is
4261 * 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 +01004262 * to the complete subtree (except the subnodes with explicit config set) and the test is not needed for the subnodes.
4263 * @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 +01004264 * @return LY_ERR value.
4265 */
Radek Krejci76b3e962018-12-14 17:01:25 +01004266static LY_ERR
Radek Krejci93dcc392019-02-19 10:43:38 +01004267lys_compile_change_config(struct lysc_ctx *ctx, struct lysc_node *node, uint16_t config_flag,
Radek Krejci327de162019-06-14 12:52:07 +02004268 int inheriting, int refine_flag)
Radek Krejci76b3e962018-12-14 17:01:25 +01004269{
4270 struct lysc_node *child;
Radek Krejci93dcc392019-02-19 10:43:38 +01004271 uint16_t config = config_flag & LYS_CONFIG_MASK;
Radek Krejci76b3e962018-12-14 17:01:25 +01004272
4273 if (config == (node->flags & LYS_CONFIG_MASK)) {
4274 /* nothing to do */
4275 return LY_SUCCESS;
4276 }
4277
4278 if (!inheriting) {
Radek Krejci93dcc392019-02-19 10:43:38 +01004279 /* explicit change */
Radek Krejci76b3e962018-12-14 17:01:25 +01004280 if (config == LYS_CONFIG_W && node->parent && (node->parent->flags & LYS_CONFIG_R)) {
4281 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004282 "Invalid %s of config - configuration node cannot be child of any state data node.",
4283 refine_flag ? "refine" : "deviation");
Radek Krejci76b3e962018-12-14 17:01:25 +01004284 return LY_EVALID;
4285 }
Radek Krejci93dcc392019-02-19 10:43:38 +01004286 node->flags |= LYS_SET_CONFIG;
4287 } else {
4288 if (node->flags & LYS_SET_CONFIG) {
4289 if ((node->flags & LYS_CONFIG_W) && (config == LYS_CONFIG_R)) {
4290 /* setting config flags, but have node with explicit config true */
4291 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004292 "Invalid %s of config - configuration node cannot be child of any state data node.",
4293 refine_flag ? "refine" : "deviation");
Radek Krejci93dcc392019-02-19 10:43:38 +01004294 return LY_EVALID;
4295 }
4296 /* do not change config on nodes where the config is explicitely set, this does not apply to
4297 * nodes, which are being changed explicitly (targets of refine or deviation) */
4298 return LY_SUCCESS;
4299 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004300 }
4301 node->flags &= ~LYS_CONFIG_MASK;
4302 node->flags |= config;
4303
4304 /* inherit the change into the children */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004305 LY_LIST_FOR((struct lysc_node*)lysc_node_children(node, 0), child) {
Radek Krejci327de162019-06-14 12:52:07 +02004306 LY_CHECK_RET(lys_compile_change_config(ctx, child, config_flag, 1, refine_flag));
Radek Krejci76b3e962018-12-14 17:01:25 +01004307 }
4308
Radek Krejci76b3e962018-12-14 17:01:25 +01004309 return LY_SUCCESS;
4310}
4311
Radek Krejcib56c7502019-02-13 14:19:54 +01004312/**
4313 * @brief Set LYS_MAND_TRUE flag for the non-presence container parents.
4314 *
4315 * A non-presence container is mandatory in case it has at least one mandatory children. This function propagate
4316 * the flag to such parents from a mandatory children.
4317 *
4318 * @param[in] parent A schema node to be examined if the mandatory child make it also mandatory.
4319 * @param[in] add Flag to distinguish adding the mandatory flag (new mandatory children appeared) or removing the flag
4320 * (mandatory children was removed).
4321 */
Radek Krejcife909632019-02-12 15:34:42 +01004322void
4323lys_compile_mandatory_parents(struct lysc_node *parent, int add)
4324{
4325 struct lysc_node *iter;
4326
4327 if (add) { /* set flag */
4328 for (; parent && parent->nodetype == LYS_CONTAINER && !(parent->flags & LYS_MAND_TRUE) && !(parent->flags & LYS_PRESENCE);
4329 parent = parent->parent) {
4330 parent->flags |= LYS_MAND_TRUE;
4331 }
4332 } else { /* unset flag */
4333 for (; parent && parent->nodetype == LYS_CONTAINER && (parent->flags & LYS_MAND_TRUE); parent = parent->parent) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004334 for (iter = (struct lysc_node*)lysc_node_children(parent, 0); iter; iter = iter->next) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004335 if (iter->flags & LYS_MAND_TRUE) {
Radek Krejcife909632019-02-12 15:34:42 +01004336 /* there is another mandatory node */
4337 return;
4338 }
4339 }
4340 /* unset mandatory flag - there is no mandatory children in the non-presence container */
4341 parent->flags &= ~LYS_MAND_TRUE;
4342 }
4343 }
4344}
4345
Radek Krejci056d0a82018-12-06 16:57:25 +01004346/**
Radek Krejci3641f562019-02-13 15:38:40 +01004347 * @brief Internal sorting process for the lys_compile_augment_sort().
4348 * @param[in] aug_p The parsed augment structure to insert into the sorter sized array @p result.
4349 * @param[in,out] result Sized array to store the sorted list of augments. The array is expected
4350 * to be allocated to hold the complete list, its size is just incremented by adding another item.
4351 */
4352static void
4353lys_compile_augment_sort_(struct lysp_augment *aug_p, struct lysp_augment **result)
4354{
4355 unsigned int v;
4356 size_t len;
4357
4358 len = strlen(aug_p->nodeid);
4359 LY_ARRAY_FOR(result, v) {
4360 if (strlen(result[v]->nodeid) <= len) {
4361 continue;
4362 }
4363 if (v < LY_ARRAY_SIZE(result)) {
4364 /* move the rest of array */
4365 memmove(&result[v + 1], &result[v], (LY_ARRAY_SIZE(result) - v) * sizeof *result);
4366 break;
4367 }
4368 }
4369 result[v] = aug_p;
4370 LY_ARRAY_INCREMENT(result);
4371}
4372
4373/**
4374 * @brief Sort augments to apply /a/b before /a/b/c (where the /a/b/c was added by the first augment).
4375 *
4376 * The sorting is based only on the length of the augment's path since it guarantee the correct order
4377 * (it doesn't matter the /a/x is done before /a/b/c from the example above).
4378 *
4379 * @param[in] ctx Compile context.
4380 * @param[in] mod_p Parsed module with the global augments (also augments from the submodules are taken).
4381 * @param[in] aug_p Parsed sized array of augments to sort (no matter if global or uses's)
4382 * @param[in] inc_p In case of global augments, sized array of module includes (submodules) to get global augments from submodules.
4383 * @param[out] augments Resulting sorted sized array of pointers to the augments.
4384 * @return LY_ERR value.
4385 */
4386LY_ERR
4387lys_compile_augment_sort(struct lysc_ctx *ctx, struct lysp_augment *aug_p, struct lysp_include *inc_p, struct lysp_augment ***augments)
4388{
4389 struct lysp_augment **result = NULL;
4390 unsigned int u, v;
4391 size_t count = 0;
4392
4393 assert(augments);
4394
4395 /* get count of the augments in module and all its submodules */
4396 if (aug_p) {
4397 count += LY_ARRAY_SIZE(aug_p);
4398 }
4399 LY_ARRAY_FOR(inc_p, u) {
4400 if (inc_p[u].submodule->augments) {
4401 count += LY_ARRAY_SIZE(inc_p[u].submodule->augments);
4402 }
4403 }
4404
4405 if (!count) {
4406 *augments = NULL;
4407 return LY_SUCCESS;
4408 }
4409 LY_ARRAY_CREATE_RET(ctx->ctx, result, count, LY_EMEM);
4410
4411 /* sort by the length of schema-nodeid - we need to solve /x before /x/xy. It is not necessary to group them
4412 * together, so there can be even /z/y betwwen them. */
4413 LY_ARRAY_FOR(aug_p, u) {
4414 lys_compile_augment_sort_(&aug_p[u], result);
4415 }
4416 LY_ARRAY_FOR(inc_p, u) {
4417 LY_ARRAY_FOR(inc_p[u].submodule->augments, v) {
4418 lys_compile_augment_sort_(&inc_p[u].submodule->augments[v], result);
4419 }
4420 }
4421
4422 *augments = result;
4423 return LY_SUCCESS;
4424}
4425
4426/**
4427 * @brief Compile the parsed augment connecting it into its target.
4428 *
4429 * It is expected that all the data referenced in path are present - augments are ordered so that augment B
4430 * targeting data from augment A is being compiled after augment A. Also the modules referenced in the path
4431 * are already implemented and compiled.
4432 *
4433 * @param[in] ctx Compile context.
4434 * @param[in] aug_p Parsed augment to compile.
Radek Krejci3641f562019-02-13 15:38:40 +01004435 * @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
4436 * children in case of the augmenting uses data.
4437 * @return LY_SUCCESS on success.
4438 * @return LY_EVALID on failure.
4439 */
4440LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004441lys_compile_augment(struct lysc_ctx *ctx, struct lysp_augment *aug_p, const struct lysc_node *parent)
Radek Krejci3641f562019-02-13 15:38:40 +01004442{
4443 LY_ERR ret = LY_SUCCESS;
4444 struct lysp_node *node_p, *case_node_p;
4445 struct lysc_node *target; /* target target of the augment */
4446 struct lysc_node *node;
Radek Krejci3641f562019-02-13 15:38:40 +01004447 struct lysc_when **when, *when_shared;
4448 int allow_mandatory = 0;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004449 uint16_t flags = 0;
4450 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02004451 int opt_prev = ctx->options;
Radek Krejci3641f562019-02-13 15:38:40 +01004452
Radek Krejci327de162019-06-14 12:52:07 +02004453 lysc_update_path(ctx, NULL, "{augment}");
4454 lysc_update_path(ctx, NULL, aug_p->nodeid);
4455
Radek Krejci7af64242019-02-18 13:07:53 +01004456 ret = lys_resolve_schema_nodeid(ctx, aug_p->nodeid, 0, parent, parent ? parent->module : ctx->mod_def,
Radek Krejci3641f562019-02-13 15:38:40 +01004457 LYS_CONTAINER | LYS_LIST | LYS_CHOICE | LYS_CASE | LYS_INOUT | LYS_NOTIF,
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004458 1, (const struct lysc_node**)&target, &flags);
Radek Krejci3641f562019-02-13 15:38:40 +01004459 if (ret != LY_SUCCESS) {
4460 if (ret == LY_EDENIED) {
4461 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4462 "Augment's %s-schema-nodeid \"%s\" refers to a %s node which is not an allowed augment's target.",
4463 parent ? "descendant" : "absolute", aug_p->nodeid, lys_nodetype2str(target->nodetype));
4464 }
4465 return LY_EVALID;
4466 }
4467
4468 /* check for mandatory nodes
4469 * - new cases augmenting some choice can have mandatory nodes
4470 * - mandatory nodes are allowed only in case the augmentation is made conditional with a when statement
4471 */
Radek Krejci733988a2019-02-15 15:12:44 +01004472 if (aug_p->when || target->nodetype == LYS_CHOICE || ctx->mod == target->module) {
Radek Krejci3641f562019-02-13 15:38:40 +01004473 allow_mandatory = 1;
4474 }
4475
4476 when_shared = NULL;
4477 LY_LIST_FOR(aug_p->child, node_p) {
4478 /* check if the subnode can be connected to the found target (e.g. case cannot be inserted into container) */
4479 if (!(target->nodetype == LYS_CHOICE && node_p->nodetype == LYS_CASE)
4480 && !((target->nodetype & (LYS_CONTAINER | LYS_LIST)) && (node_p->nodetype & (LYS_ACTION | LYS_NOTIF)))
Radek Krejci2d56a892019-02-19 09:05:26 +01004481 && !(target->nodetype != LYS_CHOICE && node_p->nodetype == LYS_USES)
4482 && !(node_p->nodetype & (LYS_ANYDATA | LYS_CONTAINER | LYS_CHOICE | LYS_LEAF | LYS_LIST | LYS_LEAFLIST))) {
Radek Krejci3641f562019-02-13 15:38:40 +01004483 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004484 "Invalid augment of %s node which is not allowed to contain %s node \"%s\".",
4485 lys_nodetype2str(target->nodetype), lys_nodetype2str(node_p->nodetype), node_p->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004486 return LY_EVALID;
4487 }
4488
4489 /* compile the children */
Radek Krejciec4da802019-05-02 13:02:41 +02004490 ctx->options |= flags;
Radek Krejci3641f562019-02-13 15:38:40 +01004491 if (node_p->nodetype != LYS_CASE) {
Radek Krejciec4da802019-05-02 13:02:41 +02004492 LY_CHECK_RET(lys_compile_node(ctx, node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004493 } else {
4494 LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02004495 LY_CHECK_RET(lys_compile_node(ctx, case_node_p, target, 0));
Radek Krejci3641f562019-02-13 15:38:40 +01004496 }
4497 }
Radek Krejciec4da802019-05-02 13:02:41 +02004498 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004499
Radek Krejcife13da42019-02-15 14:51:01 +01004500 /* since the augment node is not present in the compiled tree, we need to pass some of its statements to all its children,
4501 * here we gets the last created node as last children of our parent */
Radek Krejci3641f562019-02-13 15:38:40 +01004502 if (target->nodetype == LYS_CASE) {
Radek Krejcife13da42019-02-15 14:51:01 +01004503 /* 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 +01004504 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 +01004505 } else if (target->nodetype == LYS_CHOICE) {
4506 /* to pass when statement, we need the last case no matter if it is explicit or implicit case */
4507 node = ((struct lysc_node_choice*)target)->cases->prev;
4508 } else {
4509 /* the compiled node is the last child of the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004510 node = lysc_node_children(target, flags)->prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004511 }
4512
Radek Krejci733988a2019-02-15 15:12:44 +01004513 if (!allow_mandatory && (node->flags & LYS_CONFIG_W) && (node->flags & LYS_MAND_TRUE)) {
Radek Krejci3641f562019-02-13 15:38:40 +01004514 node->flags &= ~LYS_MAND_TRUE;
4515 lys_compile_mandatory_parents(target, 0);
4516 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004517 "Invalid augment adding mandatory node \"%s\" without making it conditional via when statement.", node->name);
Radek Krejci3641f562019-02-13 15:38:40 +01004518 return LY_EVALID;
4519 }
4520
4521 /* pass augment's when to all the children */
4522 if (aug_p->when) {
4523 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
4524 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004525 ret = lys_compile_when(ctx, aug_p->when, when);
Radek Krejci3641f562019-02-13 15:38:40 +01004526 LY_CHECK_GOTO(ret, error);
4527 (*when)->context = lysc_xpath_context(target);
4528 when_shared = *when;
4529 } else {
4530 ++when_shared->refcount;
4531 (*when) = when_shared;
4532 }
4533 }
4534 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004535
Radek Krejciec4da802019-05-02 13:02:41 +02004536 ctx->options |= flags;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004537 switch (target->nodetype) {
4538 case LYS_CONTAINER:
Radek Krejci05b774b2019-02-25 13:26:18 +01004539 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004540 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004541 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_container*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004542 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004543 break;
4544 case LYS_LIST:
Radek Krejci05b774b2019-02-25 13:26:18 +01004545 COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004546 u, lys_compile_action, 0, ret, error);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004547 COMPILE_ARRAY1_GOTO(ctx, aug_p->notifs, ((struct lysc_node_list*)target)->notifs, target,
Radek Krejciec4da802019-05-02 13:02:41 +02004548 u, lys_compile_notif, 0, ret, error);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004549 break;
4550 default:
Radek Krejciec4da802019-05-02 13:02:41 +02004551 ctx->options = opt_prev;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004552 if (aug_p->actions) {
4553 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004554 "Invalid augment of %s node which is not allowed to contain RPC/action node \"%s\".",
4555 lys_nodetype2str(target->nodetype), aug_p->actions[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004556 return LY_EVALID;
4557 }
4558 if (aug_p->notifs) {
4559 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004560 "Invalid augment of %s node which is not allowed to contain Notification node \"%s\".",
4561 lys_nodetype2str(target->nodetype), aug_p->notifs[0].name);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004562 return LY_EVALID;
4563 }
4564 }
Radek Krejci3641f562019-02-13 15:38:40 +01004565
Radek Krejci327de162019-06-14 12:52:07 +02004566 lysc_update_path(ctx, NULL, NULL);
4567 lysc_update_path(ctx, NULL, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01004568error:
Radek Krejciec4da802019-05-02 13:02:41 +02004569 ctx->options = opt_prev;
Radek Krejci3641f562019-02-13 15:38:40 +01004570 return ret;
4571}
4572
4573/**
Radek Krejcif1421c22019-02-19 13:05:20 +01004574 * @brief Apply refined or deviated mandatory flag to the target node.
4575 *
4576 * @param[in] ctx Compile context.
4577 * @param[in] node Target node where the mandatory property is supposed to be changed.
4578 * @param[in] mandatory_flag Node's mandatory flag to be applied to the @p node.
Radek Krejcif1421c22019-02-19 13:05:20 +01004579 * @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 +01004580 * @param[in] It is also used as a flag for testing for compatibility with default statement. In case of deviations,
4581 * there can be some other deviations of the default properties that we are testing here. To avoid false positive failure,
4582 * 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 +01004583 * @return LY_ERR value.
4584 */
4585static LY_ERR
Radek Krejci327de162019-06-14 12:52:07 +02004586lys_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 +01004587{
4588 if (!(node->nodetype & (LYS_LEAF | LYS_ANYDATA | LYS_ANYXML | LYS_CHOICE))) {
4589 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004590 "Invalid %s of mandatory - %s cannot hold mandatory statement.",
4591 refine_flag ? "refine" : "deviation", lys_nodetype2str(node->nodetype));
Radek Krejcif1421c22019-02-19 13:05:20 +01004592 return LY_EVALID;
4593 }
4594
4595 if (mandatory_flag & LYS_MAND_TRUE) {
4596 /* check if node has default value */
4597 if (node->nodetype & LYS_LEAF) {
4598 if (node->flags & LYS_SET_DFLT) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004599 if (refine_flag) {
4600 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004601 "Invalid refine of mandatory - leaf already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004602 return LY_EVALID;
4603 }
Radek Krejcia1911222019-07-22 17:24:50 +02004604 } else if (((struct lysc_node_leaf*)node)->dflt) {
Radek Krejcif1421c22019-02-19 13:05:20 +01004605 /* remove the default value taken from the leaf's type */
Radek Krejcia1911222019-07-22 17:24:50 +02004606 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejci474f9b82019-07-24 11:36:37 +02004607
4608 /* update the list of incomplete default values if needed */
4609 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
4610
Radek Krejcia1911222019-07-22 17:24:50 +02004611 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4612 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4613 free(leaf->dflt);
4614 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004615 leaf->dflt_mod = NULL;
Radek Krejcif1421c22019-02-19 13:05:20 +01004616 }
4617 } else if ((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) {
Radek Krejci551b12c2019-02-19 16:11:21 +01004618 if (refine_flag) {
4619 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004620 "Invalid refine of mandatory - choice already has \"default\" statement.");
Radek Krejci551b12c2019-02-19 16:11:21 +01004621 return LY_EVALID;
4622 }
Radek Krejcif1421c22019-02-19 13:05:20 +01004623 }
Radek Krejci551b12c2019-02-19 16:11:21 +01004624 if (refine_flag && node->parent && (node->parent->flags & LYS_SET_DFLT)) {
Radek Krejci327de162019-06-14 12:52:07 +02004625 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 +01004626 return LY_EVALID;
4627 }
4628
4629 node->flags &= ~LYS_MAND_FALSE;
4630 node->flags |= LYS_MAND_TRUE;
4631 lys_compile_mandatory_parents(node->parent, 1);
4632 } else {
4633 /* make mandatory false */
4634 node->flags &= ~LYS_MAND_TRUE;
4635 node->flags |= LYS_MAND_FALSE;
4636 lys_compile_mandatory_parents(node->parent, 0);
Radek Krejcia1911222019-07-22 17:24:50 +02004637 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 +01004638 /* get the type's default value if any */
Radek Krejcia1911222019-07-22 17:24:50 +02004639 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004640 leaf->dflt_mod = leaf->type->dflt_mod;
Radek Krejcia1911222019-07-22 17:24:50 +02004641 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4642 leaf->dflt->realtype = leaf->type->dflt->realtype;
4643 leaf->dflt->realtype->plugin->duplicate(ctx->ctx, leaf->type->dflt, leaf->dflt);
4644 leaf->dflt->realtype->refcount++;
Radek Krejcif1421c22019-02-19 13:05:20 +01004645 }
4646 }
4647 return LY_SUCCESS;
4648}
4649
4650/**
Radek Krejcie86bf772018-12-14 11:39:53 +01004651 * @brief Compile parsed uses statement - resolve target grouping and connect its content into parent.
4652 * If present, also apply uses's modificators.
4653 *
4654 * @param[in] ctx Compile context
4655 * @param[in] uses_p Parsed uses schema node.
Radek Krejcie86bf772018-12-14 11:39:53 +01004656 * @param[in] parent Compiled parent node where the content of the referenced grouping is supposed to be connected. It is
4657 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
4658 * the compile context.
4659 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
4660 */
4661static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02004662lys_compile_uses(struct lysc_ctx *ctx, struct lysp_node_uses *uses_p, struct lysc_node *parent)
Radek Krejcie86bf772018-12-14 11:39:53 +01004663{
4664 struct lysp_node *node_p;
Radek Krejci01342af2019-01-03 15:18:08 +01004665 struct lysc_node *node, *child;
Radek Krejci12fb9142019-01-08 09:45:30 +01004666 /* context_node_fake allows us to temporarily isolate the nodes inserted from the grouping instead of uses */
Radek Krejci01342af2019-01-03 15:18:08 +01004667 struct lysc_node_container context_node_fake =
4668 {.nodetype = LYS_CONTAINER,
4669 .module = ctx->mod,
4670 .flags = parent ? parent->flags : 0,
4671 .child = NULL, .next = NULL,
Radek Krejcifc11bd72019-04-11 16:00:05 +02004672 .prev = (struct lysc_node*)&context_node_fake,
4673 .actions = NULL, .notifs = NULL};
Radek Krejciec4da802019-05-02 13:02:41 +02004674 struct lysp_grp *grp = NULL;
Radek Krejci76b3e962018-12-14 17:01:25 +01004675 unsigned int u, v, grp_stack_count;
Radek Krejcie86bf772018-12-14 11:39:53 +01004676 int found;
4677 const char *id, *name, *prefix;
4678 size_t prefix_len, name_len;
4679 struct lys_module *mod, *mod_old;
Radek Krejci76b3e962018-12-14 17:01:25 +01004680 struct lysp_refine *rfn;
Radek Krejcia1911222019-07-22 17:24:50 +02004681 LY_ERR ret = LY_EVALID, rc;
Radek Krejcif2271f12019-01-07 16:42:23 +01004682 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004683 uint16_t flags;
Radek Krejcif2271f12019-01-07 16:42:23 +01004684 struct ly_set refined = {0};
Radek Krejci00b874b2019-02-12 10:54:50 +01004685 struct lysc_when **when, *when_shared;
Radek Krejci3641f562019-02-13 15:38:40 +01004686 struct lysp_augment **augments = NULL;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004687 unsigned int actions_index, notifs_index;
4688 struct lysc_notif **notifs = NULL;
4689 struct lysc_action **actions = NULL;
Radek Krejcie86bf772018-12-14 11:39:53 +01004690
4691 /* search for the grouping definition */
4692 found = 0;
4693 id = uses_p->name;
Radek Krejcib4a4a272019-06-10 12:44:52 +02004694 LY_CHECK_RET(ly_parse_nodeid(&id, &prefix, &prefix_len, &name, &name_len), LY_EVALID);
Radek Krejcie86bf772018-12-14 11:39:53 +01004695 if (prefix) {
4696 mod = lys_module_find_prefix(ctx->mod_def, prefix, prefix_len);
4697 if (!mod) {
4698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02004699 "Invalid prefix used for grouping reference.", uses_p->name);
Radek Krejcie86bf772018-12-14 11:39:53 +01004700 return LY_EVALID;
4701 }
4702 } else {
4703 mod = ctx->mod_def;
4704 }
4705 if (mod == ctx->mod_def) {
4706 for (node_p = uses_p->parent; !found && node_p; node_p = node_p->parent) {
Radek Krejciec4da802019-05-02 13:02:41 +02004707 grp = (struct lysp_grp*)lysp_node_groupings(node_p);
Radek Krejcie86bf772018-12-14 11:39:53 +01004708 LY_ARRAY_FOR(grp, u) {
4709 if (!strcmp(grp[u].name, name)) {
4710 grp = &grp[u];
4711 found = 1;
4712 break;
4713 }
4714 }
4715 }
4716 }
4717 if (!found) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004718 /* search in top-level groupings of the main module ... */
Radek Krejcie86bf772018-12-14 11:39:53 +01004719 grp = mod->parsed->groupings;
Radek Krejci76b3e962018-12-14 17:01:25 +01004720 if (grp) {
4721 for (u = 0; !found && u < LY_ARRAY_SIZE(grp); ++u) {
4722 if (!strcmp(grp[u].name, name)) {
4723 grp = &grp[u];
4724 found = 1;
4725 }
4726 }
4727 }
4728 if (!found && mod->parsed->includes) {
4729 /* ... and all the submodules */
4730 for (u = 0; !found && u < LY_ARRAY_SIZE(mod->parsed->includes); ++u) {
4731 grp = mod->parsed->includes[u].submodule->groupings;
4732 if (grp) {
4733 for (v = 0; !found && v < LY_ARRAY_SIZE(grp); ++v) {
4734 if (!strcmp(grp[v].name, name)) {
4735 grp = &grp[v];
4736 found = 1;
4737 }
4738 }
4739 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004740 }
4741 }
4742 }
4743 if (!found) {
4744 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4745 "Grouping \"%s\" referenced by a uses statement not found.", uses_p->name);
4746 return LY_EVALID;
4747 }
4748
4749 /* grouping must not reference themselves - stack in ctx maintains list of groupings currently being applied */
4750 grp_stack_count = ctx->groupings.count;
4751 ly_set_add(&ctx->groupings, (void*)grp, 0);
4752 if (grp_stack_count == ctx->groupings.count) {
4753 /* the target grouping is already in the stack, so we are already inside it -> circular dependency */
4754 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
4755 "Grouping \"%s\" references itself through a uses statement.", grp->name);
4756 return LY_EVALID;
4757 }
Radek Krejcif2de0ed2019-05-02 14:13:18 +02004758 if (!(ctx->options & LYSC_OPT_GROUPING)) {
4759 /* remember that the grouping is instantiated to avoid its standalone validation */
4760 grp->flags |= LYS_USED_GRP;
4761 }
Radek Krejcie86bf772018-12-14 11:39:53 +01004762
4763 /* switch context's mod_def */
4764 mod_old = ctx->mod_def;
4765 ctx->mod_def = mod;
4766
4767 /* check status */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004768 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 +01004769
Radek Krejcifc11bd72019-04-11 16:00:05 +02004770 /* compile data nodes */
Radek Krejcie86bf772018-12-14 11:39:53 +01004771 LY_LIST_FOR(grp->data, node_p) {
Radek Krejcib1b59152019-01-07 13:21:56 +01004772 /* 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 +02004773 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 +01004774
4775 /* some preparation for applying refines */
4776 if (grp->data == node_p) {
4777 /* remember the first child */
Radek Krejci684faf22019-05-27 14:31:16 +02004778 if (parent) {
4779 child = (struct lysc_node*)lysc_node_children(parent, ctx->options & LYSC_OPT_RPC_MASK);
4780 } else if (ctx->mod->compiled->data) {
4781 child = ctx->mod->compiled->data;
4782 } else {
4783 child = NULL;
4784 }
4785 context_node_fake.child = child ? child->prev : NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004786 }
4787 }
Radek Krejci00b874b2019-02-12 10:54:50 +01004788 when_shared = NULL;
Radek Krejci01342af2019-01-03 15:18:08 +01004789 LY_LIST_FOR(context_node_fake.child, child) {
4790 child->parent = (struct lysc_node*)&context_node_fake;
Radek Krejci00b874b2019-02-12 10:54:50 +01004791
Radek Krejcifc11bd72019-04-11 16:00:05 +02004792 /* pass uses's when to all the data children, actions and notifications are ignored */
Radek Krejci00b874b2019-02-12 10:54:50 +01004793 if (uses_p->when) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004794 LY_ARRAY_NEW_GOTO(ctx->ctx, child->when, when, ret, cleanup);
Radek Krejci00b874b2019-02-12 10:54:50 +01004795 if (!when_shared) {
Radek Krejciec4da802019-05-02 13:02:41 +02004796 LY_CHECK_GOTO(lys_compile_when(ctx, uses_p->when, when), cleanup);
Radek Krejcib56c7502019-02-13 14:19:54 +01004797 (*when)->context = lysc_xpath_context(parent);
Radek Krejci00b874b2019-02-12 10:54:50 +01004798 when_shared = *when;
4799 } else {
4800 ++when_shared->refcount;
4801 (*when) = when_shared;
4802 }
4803 }
Radek Krejci01342af2019-01-03 15:18:08 +01004804 }
4805 if (context_node_fake.child) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02004806 /* child is the last data node added by grouping */
Radek Krejci01342af2019-01-03 15:18:08 +01004807 child = context_node_fake.child->prev;
Radek Krejcifc11bd72019-04-11 16:00:05 +02004808 /* fix child link of our fake container to point to the first child of the original list */
Radek Krejciec4da802019-05-02 13:02:41 +02004809 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 +01004810 }
4811
Radek Krejcifc11bd72019-04-11 16:00:05 +02004812 /* compile actions */
4813 actions = parent ? lysc_node_actions_p(parent) : &ctx->mod->compiled->rpcs;
4814 if (actions) {
4815 actions_index = *actions ? LY_ARRAY_SIZE(*actions) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004816 COMPILE_ARRAY1_GOTO(ctx, grp->actions, *actions, parent, u, lys_compile_action, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004817 if (*actions && (uses_p->augments || uses_p->refines)) {
4818 /* but for augment and refine, we need to separate the compiled grouping's actions to avoid modification of others */
4819 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.actions, LY_ARRAY_SIZE(*actions) - actions_index, ret, cleanup);
4820 LY_ARRAY_SIZE(context_node_fake.actions) = LY_ARRAY_SIZE(*actions) - actions_index;
4821 memcpy(context_node_fake.actions, &(*actions)[actions_index], LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
4822 }
4823 }
4824
4825 /* compile notifications */
4826 notifs = parent ? lysc_node_notifs_p(parent) : &ctx->mod->compiled->notifs;
4827 if (notifs) {
4828 notifs_index = *notifs ? LY_ARRAY_SIZE(*notifs) : 0;
Radek Krejciec4da802019-05-02 13:02:41 +02004829 COMPILE_ARRAY1_GOTO(ctx, grp->notifs, *notifs, parent, u, lys_compile_notif, 0, ret, cleanup);
Radek Krejcifc11bd72019-04-11 16:00:05 +02004830 if (*notifs && (uses_p->augments || uses_p->refines)) {
4831 /* but for augment and refine, we need to separate the compiled grouping's notification to avoid modification of others */
4832 LY_ARRAY_CREATE_GOTO(ctx->ctx, context_node_fake.notifs, LY_ARRAY_SIZE(*notifs) - notifs_index, ret, cleanup);
4833 LY_ARRAY_SIZE(context_node_fake.notifs) = LY_ARRAY_SIZE(*notifs) - notifs_index;
4834 memcpy(context_node_fake.notifs, &(*notifs)[notifs_index], LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
4835 }
4836 }
4837
4838
Radek Krejci3641f562019-02-13 15:38:40 +01004839 /* sort and apply augments */
Radek Krejcifc11bd72019-04-11 16:00:05 +02004840 LY_CHECK_GOTO(lys_compile_augment_sort(ctx, uses_p->augments, NULL, &augments), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004841 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02004842 LY_CHECK_GOTO(lys_compile_augment(ctx, augments[u], (struct lysc_node*)&context_node_fake), cleanup);
Radek Krejci3641f562019-02-13 15:38:40 +01004843 }
Radek Krejci12fb9142019-01-08 09:45:30 +01004844
Radek Krejcif0089082019-01-07 16:42:01 +01004845 /* reload previous context's mod_def */
4846 ctx->mod_def = mod_old;
Radek Krejci327de162019-06-14 12:52:07 +02004847 lysc_update_path(ctx, NULL, "{refine}");
Radek Krejcif0089082019-01-07 16:42:01 +01004848
Radek Krejci76b3e962018-12-14 17:01:25 +01004849 /* apply refine */
4850 LY_ARRAY_FOR(uses_p->refines, struct lysp_refine, rfn) {
Radek Krejci327de162019-06-14 12:52:07 +02004851 lysc_update_path(ctx, NULL, rfn->nodeid);
4852
Radek Krejci7af64242019-02-18 13:07:53 +01004853 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 +01004854 0, 0, (const struct lysc_node**)&node, &flags),
Radek Krejcifc11bd72019-04-11 16:00:05 +02004855 cleanup);
Radek Krejcif2271f12019-01-07 16:42:23 +01004856 ly_set_add(&refined, node, LY_SET_OPT_USEASLIST);
Radek Krejci76b3e962018-12-14 17:01:25 +01004857
4858 /* default value */
4859 if (rfn->dflts) {
Radek Krejci01342af2019-01-03 15:18:08 +01004860 if ((node->nodetype != LYS_LEAFLIST) && LY_ARRAY_SIZE(rfn->dflts) > 1) {
Radek Krejci76b3e962018-12-14 17:01:25 +01004861 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004862 "Invalid refine of default - %s cannot hold %d default values.",
4863 lys_nodetype2str(node->nodetype), LY_ARRAY_SIZE(rfn->dflts));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004864 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004865 }
4866 if (!(node->nodetype & (LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE))) {
4867 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004868 "Invalid refine of default - %s cannot hold default value(s).",
4869 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004870 goto cleanup;
Radek Krejci76b3e962018-12-14 17:01:25 +01004871 }
4872 if (node->nodetype == LYS_LEAF) {
Radek Krejcia1911222019-07-22 17:24:50 +02004873 struct ly_err_item *err = NULL;
4874 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)node;
4875 if (leaf->dflt) {
4876 /* remove the previous default value */
Radek Krejci474f9b82019-07-24 11:36:37 +02004877 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02004878 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
4879 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
4880 } else {
4881 /* prepare a new one */
4882 leaf->dflt = calloc(1, sizeof *leaf->dflt);
4883 leaf->dflt->realtype = leaf->type;
4884 }
4885 /* parse the new one */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004886 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004887 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, rfn->dflts[0], strlen(rfn->dflts[0]),
4888 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004889 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, node, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004890 leaf->dflt->realtype->refcount++;
4891 if (err) {
4892 ly_err_print(err);
4893 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4894 "Invalid refine of default - value \"%s\" does not fit the type (%s).", rfn->dflts[0], err->msg);
4895 ly_err_free(err);
4896 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004897 if (rc == LY_EINCOMPLETE) {
4898 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004899 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, leaf->dflt, leaf->dflt_mod), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004900
4901 /* but in general result is so far ok */
4902 rc = LY_SUCCESS;
4903 }
Radek Krejcia1911222019-07-22 17:24:50 +02004904 LY_CHECK_GOTO(rc, cleanup);
4905 leaf->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004906 } else if (node->nodetype == LYS_LEAFLIST) {
Radek Krejcia1911222019-07-22 17:24:50 +02004907 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)node;
4908
Radek Krejci0bcdaed2019-01-10 10:21:34 +01004909 if (ctx->mod->version < 2) {
Radek Krejci01342af2019-01-03 15:18:08 +01004910 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4911 "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 +02004912 goto cleanup;
Radek Krejci01342af2019-01-03 15:18:08 +01004913 }
Radek Krejcia1911222019-07-22 17:24:50 +02004914
4915 /* remove previous set of default values */
4916 LY_ARRAY_FOR(llist->dflts, u) {
Radek Krejci474f9b82019-07-24 11:36:37 +02004917 lysc_incomplete_dflts_remove(ctx, llist->dflts[u]);
Radek Krejcia1911222019-07-22 17:24:50 +02004918 llist->dflts[u]->realtype->plugin->free(ctx->ctx, llist->dflts[u]);
4919 lysc_type_free(ctx->ctx, llist->dflts[u]->realtype);
4920 free(llist->dflts[u]);
Radek Krejci76b3e962018-12-14 17:01:25 +01004921 }
Radek Krejcia1911222019-07-22 17:24:50 +02004922 LY_ARRAY_FREE(llist->dflts);
4923 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004924 LY_ARRAY_FREE(llist->dflts_mods);
4925 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02004926
4927 /* create the new set of the default values */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004928 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02004929 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(rfn->dflts), ret, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004930 LY_ARRAY_FOR(rfn->dflts, u) {
Radek Krejcia1911222019-07-22 17:24:50 +02004931 struct ly_err_item *err = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02004932 LY_ARRAY_INCREMENT(llist->dflts_mods);
4933 llist->dflts_mods[u] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02004934 LY_ARRAY_INCREMENT(llist->dflts);
4935 llist->dflts[u] = calloc(1, sizeof *llist->dflts[u]);
4936 llist->dflts[u]->realtype = llist->type;
Radek Krejci1c0c3442019-07-23 16:08:47 +02004937 rc = llist->type->plugin->store(ctx->ctx, llist->type, rfn->dflts[u], strlen(rfn->dflts[u]),
Radek Krejcia1911222019-07-22 17:24:50 +02004938 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02004939 lys_resolve_prefix, (void*)llist->dflts_mods[u], LYD_XML, node, NULL, llist->dflts[u], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02004940 llist->dflts[u]->realtype->refcount++;
4941 if (err) {
4942 ly_err_print(err);
4943 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
4944 "Invalid refine of default in leaf-lists - value \"%s\" does not fit the type (%s).", rfn->dflts[u], err->msg);
4945 ly_err_free(err);
4946 }
Radek Krejci1c0c3442019-07-23 16:08:47 +02004947 if (rc == LY_EINCOMPLETE) {
4948 /* postpone default compilation when the tree is complete */
Radek Krejci474f9b82019-07-24 11:36:37 +02004949 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, node, llist->dflts[u], llist->dflts_mods[u]), cleanup);
Radek Krejci1c0c3442019-07-23 16:08:47 +02004950
4951 /* but in general result is so far ok */
4952 rc = LY_SUCCESS;
4953 }
4954 LY_CHECK_GOTO(rc, cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004955 }
Radek Krejcia1911222019-07-22 17:24:50 +02004956 llist->flags |= LYS_SET_DFLT;
Radek Krejci76b3e962018-12-14 17:01:25 +01004957 } else if (node->nodetype == LYS_CHOICE) {
Radek Krejci01342af2019-01-03 15:18:08 +01004958 if (((struct lysc_node_choice*)node)->dflt) {
4959 /* unset LYS_SET_DFLT from the current default case */
4960 ((struct lysc_node_choice*)node)->dflt->flags &= ~LYS_SET_DFLT;
4961 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02004962 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 +01004963 }
4964 }
4965
Radek Krejci12fb9142019-01-08 09:45:30 +01004966 /* description */
4967 if (rfn->dsc) {
4968 FREE_STRING(ctx->ctx, node->dsc);
4969 node->dsc = lydict_insert(ctx->ctx, rfn->dsc, 0);
4970 }
4971
4972 /* reference */
4973 if (rfn->ref) {
4974 FREE_STRING(ctx->ctx, node->ref);
4975 node->ref = lydict_insert(ctx->ctx, rfn->ref, 0);
4976 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004977
4978 /* config */
4979 if (rfn->flags & LYS_CONFIG_MASK) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004980 if (!flags) {
Radek Krejci327de162019-06-14 12:52:07 +02004981 LY_CHECK_GOTO(lys_compile_change_config(ctx, node, rfn->flags, 0, 1), cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01004982 } else {
4983 LOGWRN(ctx->ctx, "Refining config inside %s has no effect (%s).",
4984 flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action", ctx->path);
4985 }
Radek Krejci76b3e962018-12-14 17:01:25 +01004986 }
4987
4988 /* mandatory */
4989 if (rfn->flags & LYS_MAND_MASK) {
Radek Krejci327de162019-06-14 12:52:07 +02004990 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, node, rfn->flags, 1), cleanup);
Radek Krejci76b3e962018-12-14 17:01:25 +01004991 }
Radek Krejci9a54f1f2019-01-07 13:47:55 +01004992
4993 /* presence */
4994 if (rfn->presence) {
4995 if (node->nodetype != LYS_CONTAINER) {
4996 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02004997 "Invalid refine of presence statement - %s cannot hold the presence statement.",
4998 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02004999 goto cleanup;
Radek Krejci9a54f1f2019-01-07 13:47:55 +01005000 }
5001 node->flags |= LYS_PRESENCE;
5002 }
Radek Krejci9a564c92019-01-07 14:53:57 +01005003
5004 /* must */
5005 if (rfn->musts) {
5006 switch (node->nodetype) {
5007 case LYS_LEAF:
Radek Krejciec4da802019-05-02 13:02:41 +02005008 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 +01005009 break;
5010 case LYS_LEAFLIST:
Radek Krejciec4da802019-05-02 13:02:41 +02005011 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 +01005012 break;
5013 case LYS_LIST:
Radek Krejciec4da802019-05-02 13:02:41 +02005014 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 +01005015 break;
5016 case LYS_CONTAINER:
Radek Krejciec4da802019-05-02 13:02:41 +02005017 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 +01005018 break;
5019 case LYS_ANYXML:
5020 case LYS_ANYDATA:
Radek Krejciec4da802019-05-02 13:02:41 +02005021 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 +01005022 break;
5023 default:
5024 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005025 "Invalid refine of must statement - %s cannot hold any must statement.",
5026 lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005027 goto cleanup;
Radek Krejci9a564c92019-01-07 14:53:57 +01005028 }
5029 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005030
5031 /* min/max-elements */
5032 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5033 switch (node->nodetype) {
5034 case LYS_LEAFLIST:
5035 if (rfn->flags & LYS_SET_MAX) {
5036 ((struct lysc_node_leaflist*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5037 }
5038 if (rfn->flags & LYS_SET_MIN) {
5039 ((struct lysc_node_leaflist*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005040 if (rfn->min) {
5041 node->flags |= LYS_MAND_TRUE;
5042 lys_compile_mandatory_parents(node->parent, 1);
5043 } else {
5044 node->flags &= ~LYS_MAND_TRUE;
5045 lys_compile_mandatory_parents(node->parent, 0);
5046 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005047 }
5048 break;
5049 case LYS_LIST:
5050 if (rfn->flags & LYS_SET_MAX) {
5051 ((struct lysc_node_list*)node)->max = rfn->max ? rfn->max : (uint32_t)-1;
5052 }
5053 if (rfn->flags & LYS_SET_MIN) {
5054 ((struct lysc_node_list*)node)->min = rfn->min;
Radek Krejcife909632019-02-12 15:34:42 +01005055 if (rfn->min) {
5056 node->flags |= LYS_MAND_TRUE;
5057 lys_compile_mandatory_parents(node->parent, 1);
5058 } else {
5059 node->flags &= ~LYS_MAND_TRUE;
5060 lys_compile_mandatory_parents(node->parent, 0);
5061 }
Radek Krejci6b22ab72019-01-07 15:39:20 +01005062 }
5063 break;
5064 default:
5065 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005066 "Invalid refine of %s statement - %s cannot hold this statement.",
5067 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements", lys_nodetype2str(node->nodetype));
Radek Krejcifc11bd72019-04-11 16:00:05 +02005068 goto cleanup;
Radek Krejci6b22ab72019-01-07 15:39:20 +01005069 }
5070 }
Radek Krejcif0089082019-01-07 16:42:01 +01005071
5072 /* if-feature */
5073 if (rfn->iffeatures) {
5074 /* any node in compiled tree can get additional if-feature, so do not check nodetype */
Radek Krejciec4da802019-05-02 13:02:41 +02005075 COMPILE_ARRAY_GOTO(ctx, rfn->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, cleanup);
Radek Krejcif0089082019-01-07 16:42:01 +01005076 }
Radek Krejci327de162019-06-14 12:52:07 +02005077
5078 lysc_update_path(ctx, NULL, NULL);
Radek Krejci01342af2019-01-03 15:18:08 +01005079 }
Radek Krejcie86bf772018-12-14 11:39:53 +01005080
Radek Krejcif2271f12019-01-07 16:42:23 +01005081 /* do some additional checks of the changed nodes when all the refines are applied */
5082 for (u = 0; u < refined.count; ++u) {
5083 node = (struct lysc_node*)refined.objs[u];
5084 rfn = &uses_p->refines[u];
Radek Krejci327de162019-06-14 12:52:07 +02005085 lysc_update_path(ctx, NULL, rfn->nodeid);
Radek Krejcif2271f12019-01-07 16:42:23 +01005086
5087 /* check possible conflict with default value (default added, mandatory left true) */
5088 if ((node->flags & LYS_MAND_TRUE) &&
5089 (((node->nodetype & LYS_CHOICE) && ((struct lysc_node_choice*)node)->dflt) ||
5090 ((node->nodetype & LYS_LEAF) && (node->flags & LYS_SET_DFLT)))) {
5091 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005092 "Invalid refine of default - the node is mandatory.");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005093 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005094 }
5095
5096 if (rfn->flags & (LYS_SET_MAX | LYS_SET_MIN)) {
5097 if (node->nodetype == LYS_LIST) {
5098 min = ((struct lysc_node_list*)node)->min;
5099 max = ((struct lysc_node_list*)node)->max;
5100 } else {
5101 min = ((struct lysc_node_leaflist*)node)->min;
5102 max = ((struct lysc_node_leaflist*)node)->max;
5103 }
5104 if (min > max) {
5105 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02005106 "Invalid refine of %s statement - \"min-elements\" is bigger than \"max-elements\".",
5107 (rfn->flags & LYS_SET_MAX) ? "max-elements" : "min-elements");
Radek Krejcifc11bd72019-04-11 16:00:05 +02005108 goto cleanup;
Radek Krejcif2271f12019-01-07 16:42:23 +01005109 }
5110 }
5111 }
5112
Radek Krejci327de162019-06-14 12:52:07 +02005113 lysc_update_path(ctx, NULL, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01005114 ret = LY_SUCCESS;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005115
5116cleanup:
5117 /* fix connection of the children nodes from fake context node back into the parent */
5118 if (context_node_fake.child) {
5119 context_node_fake.child->prev = child;
5120 }
5121 LY_LIST_FOR(context_node_fake.child, child) {
5122 child->parent = parent;
5123 }
5124
5125 if (uses_p->augments || uses_p->refines) {
5126 /* return back actions and notifications in case they were separated for augment/refine processing */
Radek Krejci65e20e22019-04-12 09:44:37 +02005127 if (context_node_fake.actions) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005128 memcpy(&(*actions)[actions_index], context_node_fake.actions, LY_ARRAY_SIZE(context_node_fake.actions) * sizeof **actions);
5129 LY_ARRAY_FREE(context_node_fake.actions);
5130 }
Radek Krejci65e20e22019-04-12 09:44:37 +02005131 if (context_node_fake.notifs) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005132 memcpy(&(*notifs)[notifs_index], context_node_fake.notifs, LY_ARRAY_SIZE(context_node_fake.notifs) * sizeof **notifs);
5133 LY_ARRAY_FREE(context_node_fake.notifs);
5134 }
5135 }
5136
Radek Krejcie86bf772018-12-14 11:39:53 +01005137 /* reload previous context's mod_def */
5138 ctx->mod_def = mod_old;
5139 /* remove the grouping from the stack for circular groupings dependency check */
5140 ly_set_rm_index(&ctx->groupings, ctx->groupings.count - 1, NULL);
5141 assert(ctx->groupings.count == grp_stack_count);
Radek Krejcif2271f12019-01-07 16:42:23 +01005142 ly_set_erase(&refined, NULL);
Radek Krejci3641f562019-02-13 15:38:40 +01005143 LY_ARRAY_FREE(augments);
Radek Krejcie86bf772018-12-14 11:39:53 +01005144
5145 return ret;
5146}
5147
Radek Krejci327de162019-06-14 12:52:07 +02005148static int
5149lys_compile_grouping_pathlog(struct lysc_ctx *ctx, struct lysp_node *node, char **path)
5150{
5151 struct lysp_node *iter;
5152 int len = 0;
5153
5154 *path = NULL;
5155 for (iter = node; iter && len >= 0; iter = iter->parent) {
5156 char *s = *path;
5157 char *id;
5158
5159 switch (iter->nodetype) {
5160 case LYS_USES:
5161 asprintf(&id, "{uses='%s'}", iter->name);
5162 break;
5163 case LYS_GROUPING:
5164 asprintf(&id, "{grouping='%s'}", iter->name);
5165 break;
5166 case LYS_AUGMENT:
5167 asprintf(&id, "{augment='%s'}", iter->name);
5168 break;
5169 default:
5170 id = strdup(iter->name);
5171 break;
5172 }
5173
5174 if (!iter->parent) {
5175 /* print prefix */
5176 len = asprintf(path, "/%s:%s%s", ctx->mod->name, id, s ? s : "");
5177 } else {
5178 /* prefix is the same as in parent */
5179 len = asprintf(path, "/%s%s", id, s ? s : "");
5180 }
5181 free(s);
5182 free(id);
5183 }
5184
5185 if (len < 0) {
5186 free(*path);
5187 *path = NULL;
5188 } else if (len == 0) {
5189 *path = strdup("/");
5190 len = 1;
5191 }
5192 return len;
5193}
5194
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005195/**
5196 * @brief Validate groupings that were defined but not directly used in the schema itself.
5197 *
5198 * The grouping does not need to be compiled (and it is compiled here, but the result is forgotten immediately),
5199 * but to have the complete result of the schema validity, even such groupings are supposed to be checked.
5200 */
5201static LY_ERR
5202lys_compile_grouping(struct lysc_ctx *ctx, struct lysp_node *node_p, struct lysp_grp *grp)
5203{
5204 LY_ERR ret;
Radek Krejci327de162019-06-14 12:52:07 +02005205 char *path;
5206 int len;
5207
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005208 struct lysp_node_uses fake_uses = {
5209 .parent = node_p,
5210 .nodetype = LYS_USES,
5211 .flags = 0, .next = NULL,
5212 .name = grp->name,
5213 .dsc = NULL, .ref = NULL, .when = NULL, .iffeatures = NULL, .exts = NULL,
5214 .refines = NULL, .augments = NULL
5215 };
5216 struct lysc_node_container fake_container = {
5217 .nodetype = LYS_CONTAINER,
5218 .flags = node_p ? (node_p->flags & LYS_FLAGS_COMPILED_MASK) : 0,
5219 .module = ctx->mod,
5220 .sp = NULL, .parent = NULL, .next = NULL,
5221 .prev = (struct lysc_node*)&fake_container,
5222 .name = "fake",
5223 .dsc = NULL, .ref = NULL, .exts = NULL, .iffeatures = NULL, .when = NULL,
5224 .child = NULL, .musts = NULL, .actions = NULL, .notifs = NULL
5225 };
5226
5227 if (grp->parent) {
5228 LOGWRN(ctx->ctx, "Locally scoped grouping \"%s\" not used.", grp->name);
5229 }
Radek Krejci327de162019-06-14 12:52:07 +02005230
5231 len = lys_compile_grouping_pathlog(ctx, grp->parent, &path);
5232 if (len < 0) {
5233 LOGMEM(ctx->ctx);
5234 return LY_EMEM;
5235 }
5236 strncpy(ctx->path, path, LYSC_CTX_BUFSIZE - 1);
5237 ctx->path_len = (uint16_t)len;
5238 free(path);
5239
5240 lysc_update_path(ctx, NULL, "{grouping}");
5241 lysc_update_path(ctx, NULL, grp->name);
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005242 ret = lys_compile_uses(ctx, &fake_uses, (struct lysc_node*)&fake_container);
Radek Krejci327de162019-06-14 12:52:07 +02005243 lysc_update_path(ctx, NULL, NULL);
5244 lysc_update_path(ctx, NULL, NULL);
5245
5246 ctx->path_len = 1;
5247 ctx->path[1] = '\0';
Radek Krejcif2de0ed2019-05-02 14:13:18 +02005248
5249 /* cleanup */
5250 lysc_node_container_free(ctx->ctx, &fake_container);
5251
5252 return ret;
5253}
Radek Krejcife909632019-02-12 15:34:42 +01005254
Radek Krejcie86bf772018-12-14 11:39:53 +01005255/**
Radek Krejcia3045382018-11-22 14:30:31 +01005256 * @brief Compile parsed schema node information.
5257 * @param[in] ctx Compile context
5258 * @param[in] node_p Parsed schema node.
Radek Krejcia3045382018-11-22 14:30:31 +01005259 * @param[in] parent Compiled parent node where the current node is supposed to be connected. It is
5260 * NULL for top-level nodes, in such a case the module where the node will be connected is taken from
5261 * the compile context.
Radek Krejcib1b59152019-01-07 13:21:56 +01005262 * @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).
5263 * Zero means no uses, non-zero value with no status bit set mean the default status.
Radek Krejcia3045382018-11-22 14:30:31 +01005264 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
5265 */
Radek Krejci19a96102018-11-15 13:38:09 +01005266static LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005267lys_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 +01005268{
5269 LY_ERR ret = LY_EVALID;
Radek Krejci056d0a82018-12-06 16:57:25 +01005270 struct lysc_node *node;
5271 struct lysc_node_case *cs;
Radek Krejci00b874b2019-02-12 10:54:50 +01005272 struct lysc_when **when;
Radek Krejci19a96102018-11-15 13:38:09 +01005273 unsigned int u;
Radek Krejciec4da802019-05-02 13:02:41 +02005274 LY_ERR (*node_compile_spec)(struct lysc_ctx*, struct lysp_node*, struct lysc_node*);
Radek Krejci19a96102018-11-15 13:38:09 +01005275
Radek Krejci327de162019-06-14 12:52:07 +02005276 if (node_p->nodetype != LYS_USES) {
5277 lysc_update_path(ctx, parent, node_p->name);
5278 } else {
5279 lysc_update_path(ctx, NULL, "{uses}");
5280 lysc_update_path(ctx, NULL, node_p->name);
5281 }
5282
Radek Krejci19a96102018-11-15 13:38:09 +01005283 switch (node_p->nodetype) {
5284 case LYS_CONTAINER:
5285 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_container));
5286 node_compile_spec = lys_compile_node_container;
5287 break;
5288 case LYS_LEAF:
5289 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaf));
5290 node_compile_spec = lys_compile_node_leaf;
5291 break;
5292 case LYS_LIST:
5293 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_list));
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005294 node_compile_spec = lys_compile_node_list;
Radek Krejci19a96102018-11-15 13:38:09 +01005295 break;
5296 case LYS_LEAFLIST:
5297 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_leaflist));
Radek Krejci0e5d8382018-11-28 16:37:53 +01005298 node_compile_spec = lys_compile_node_leaflist;
Radek Krejci19a96102018-11-15 13:38:09 +01005299 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005300 case LYS_CHOICE:
5301 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_choice));
Radek Krejci056d0a82018-12-06 16:57:25 +01005302 node_compile_spec = lys_compile_node_choice;
Radek Krejci19a96102018-11-15 13:38:09 +01005303 break;
Radek Krejci19a96102018-11-15 13:38:09 +01005304 case LYS_ANYXML:
5305 case LYS_ANYDATA:
5306 node = (struct lysc_node*)calloc(1, sizeof(struct lysc_node_anydata));
Radek Krejci9800fb82018-12-13 14:26:23 +01005307 node_compile_spec = lys_compile_node_any;
Radek Krejci19a96102018-11-15 13:38:09 +01005308 break;
Radek Krejcie86bf772018-12-14 11:39:53 +01005309 case LYS_USES:
Radek Krejci327de162019-06-14 12:52:07 +02005310 ret = lys_compile_uses(ctx, (struct lysp_node_uses*)node_p, parent);
5311 lysc_update_path(ctx, NULL, NULL);
5312 lysc_update_path(ctx, NULL, NULL);
5313 return ret;
Radek Krejci19a96102018-11-15 13:38:09 +01005314 default:
5315 LOGINT(ctx->ctx);
5316 return LY_EINT;
5317 }
5318 LY_CHECK_ERR_RET(!node, LOGMEM(ctx->ctx), LY_EMEM);
5319 node->nodetype = node_p->nodetype;
5320 node->module = ctx->mod;
5321 node->prev = node;
Radek Krejci0e5d8382018-11-28 16:37:53 +01005322 node->flags = node_p->flags & LYS_FLAGS_COMPILED_MASK;
Radek Krejci19a96102018-11-15 13:38:09 +01005323
5324 /* config */
Radek Krejciec4da802019-05-02 13:02:41 +02005325 if (ctx->options & (LYSC_OPT_RPC_INPUT | LYSC_OPT_RPC_OUTPUT)) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005326 /* ignore config statements inside RPC/action data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005327 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejciec4da802019-05-02 13:02:41 +02005328 node->flags |= (ctx->options & LYSC_OPT_RPC_INPUT) ? LYS_CONFIG_W : LYS_CONFIG_R;
5329 } else if (ctx->options & LYSC_OPT_NOTIFICATION) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005330 /* ignore config statements inside Notification data */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005331 node->flags &= ~LYS_CONFIG_MASK;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005332 node->flags |= LYS_CONFIG_R;
5333 } else if (!(node->flags & LYS_CONFIG_MASK)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005334 /* config not explicitely set, inherit it from parent */
5335 if (parent) {
5336 node->flags |= parent->flags & LYS_CONFIG_MASK;
5337 } else {
5338 /* default is config true */
5339 node->flags |= LYS_CONFIG_W;
5340 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005341 } else {
5342 /* config set explicitely */
5343 node->flags |= LYS_SET_CONFIG;
Radek Krejci19a96102018-11-15 13:38:09 +01005344 }
Radek Krejci9bb94eb2018-12-04 16:48:35 +01005345 if (parent && (parent->flags & LYS_CONFIG_R) && (node->flags & LYS_CONFIG_W)) {
5346 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5347 "Configuration node cannot be child of any state data node.");
5348 goto error;
5349 }
Radek Krejci19a96102018-11-15 13:38:09 +01005350
Radek Krejcia6d57732018-11-29 13:40:37 +01005351 /* *list ordering */
5352 if (node->nodetype & (LYS_LIST | LYS_LEAFLIST)) {
5353 if ((node->flags & LYS_CONFIG_R) && (node->flags & LYS_ORDBY_MASK)) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005354 LOGWRN(ctx->ctx, "The ordered-by statement is ignored in lists representing %s (%s).",
Radek Krejciec4da802019-05-02 13:02:41 +02005355 (ctx->options & LYSC_OPT_RPC_OUTPUT) ? "RPC/action output parameters" :
5356 (ctx->options & LYSC_OPT_NOTIFICATION) ? "notification content" : "state data", ctx->path);
Radek Krejcia6d57732018-11-29 13:40:37 +01005357 node->flags &= ~LYS_ORDBY_MASK;
5358 node->flags |= LYS_ORDBY_SYSTEM;
5359 } else if (!(node->flags & LYS_ORDBY_MASK)) {
5360 /* default ordering is system */
5361 node->flags |= LYS_ORDBY_SYSTEM;
5362 }
5363 }
5364
Radek Krejci19a96102018-11-15 13:38:09 +01005365 /* status - it is not inherited by specification, but it does not make sense to have
5366 * current in deprecated or deprecated in obsolete, so we do print warning and inherit status */
Radek Krejci056d0a82018-12-06 16:57:25 +01005367 if (!parent || parent->nodetype != LYS_CHOICE) {
5368 /* in case of choice/case's children, postpone the check to the moment we know if
5369 * 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 +01005370 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 +01005371 }
5372
Radek Krejciec4da802019-05-02 13:02:41 +02005373 if (!(ctx->options & LYSC_OPT_FREE_SP)) {
Radek Krejci19a96102018-11-15 13:38:09 +01005374 node->sp = node_p;
5375 }
5376 DUP_STRING(ctx->ctx, node_p->name, node->name);
Radek Krejci12fb9142019-01-08 09:45:30 +01005377 DUP_STRING(ctx->ctx, node_p->dsc, node->dsc);
5378 DUP_STRING(ctx->ctx, node_p->ref, node->ref);
Radek Krejci00b874b2019-02-12 10:54:50 +01005379 if (node_p->when) {
5380 LY_ARRAY_NEW_GOTO(ctx->ctx, node->when, when, ret, error);
Radek Krejciec4da802019-05-02 13:02:41 +02005381 ret = lys_compile_when(ctx, node_p->when, when);
Radek Krejci00b874b2019-02-12 10:54:50 +01005382 LY_CHECK_GOTO(ret, error);
Radek Krejcib56c7502019-02-13 14:19:54 +01005383 (*when)->context = lysc_xpath_context(node);
Radek Krejci00b874b2019-02-12 10:54:50 +01005384 }
Radek Krejciec4da802019-05-02 13:02:41 +02005385 COMPILE_ARRAY_GOTO(ctx, node_p->iffeatures, node->iffeatures, u, lys_compile_iffeature, ret, error);
5386 COMPILE_ARRAY_GOTO(ctx, node_p->exts, node->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01005387
5388 /* nodetype-specific part */
Radek Krejciec4da802019-05-02 13:02:41 +02005389 LY_CHECK_GOTO(node_compile_spec(ctx, node_p, node), error);
Radek Krejci19a96102018-11-15 13:38:09 +01005390
Radek Krejcife909632019-02-12 15:34:42 +01005391 /* inherit LYS_MAND_TRUE in parent containers */
5392 if (node->flags & LYS_MAND_TRUE) {
5393 lys_compile_mandatory_parents(parent, 1);
5394 }
5395
Radek Krejci327de162019-06-14 12:52:07 +02005396 lysc_update_path(ctx, NULL, NULL);
5397
Radek Krejci19a96102018-11-15 13:38:09 +01005398 /* insert into parent's children */
Radek Krejcia3045382018-11-22 14:30:31 +01005399 if (parent) {
5400 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci327de162019-06-14 12:52:07 +02005401 if (node_p->parent->nodetype == LYS_CASE) {
5402 lysc_update_path(ctx, parent, node_p->parent->name);
5403 } else {
5404 lysc_update_path(ctx, parent, node->name);
5405 }
Radek Krejciec4da802019-05-02 13:02:41 +02005406 cs = lys_compile_node_case(ctx, node_p->parent, (struct lysc_node_choice*)parent, node);
Radek Krejci056d0a82018-12-06 16:57:25 +01005407 LY_CHECK_ERR_GOTO(!cs, ret = LY_EVALID, error);
Radek Krejcib1b59152019-01-07 13:21:56 +01005408 if (uses_status) {
5409
5410 }
Radek Krejci056d0a82018-12-06 16:57:25 +01005411 /* the postponed status check of the node and its real parent - in case of implicit case,
Radek Krejcib1b59152019-01-07 13:21:56 +01005412 * it directly gets the same status flags as the choice;
5413 * uses_status cannot be applied here since uses cannot be child statement of choice */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005414 LY_CHECK_GOTO(lys_compile_status(ctx, &node->flags, cs->flags), error);
Radek Krejci056d0a82018-12-06 16:57:25 +01005415 node->parent = (struct lysc_node*)cs;
Radek Krejci327de162019-06-14 12:52:07 +02005416 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005417 } else { /* other than choice */
Radek Krejci327de162019-06-14 12:52:07 +02005418 lysc_update_path(ctx, parent, node->name);
Radek Krejci056d0a82018-12-06 16:57:25 +01005419 node->parent = parent;
Radek Krejci19a96102018-11-15 13:38:09 +01005420 }
Radek Krejciec4da802019-05-02 13:02:41 +02005421 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 +02005422
5423 if (parent->nodetype == LYS_CHOICE) {
5424 lysc_update_path(ctx, NULL, NULL);
5425 }
Radek Krejci19a96102018-11-15 13:38:09 +01005426 } else {
5427 /* top-level element */
5428 if (!ctx->mod->compiled->data) {
5429 ctx->mod->compiled->data = node;
5430 } else {
5431 /* insert at the end of the module's top-level nodes list */
5432 ctx->mod->compiled->data->prev->next = node;
5433 node->prev = ctx->mod->compiled->data->prev;
5434 ctx->mod->compiled->data->prev = node;
5435 }
Radek Krejci327de162019-06-14 12:52:07 +02005436 lysc_update_path(ctx, parent, node->name);
Radek Krejci76b3e962018-12-14 17:01:25 +01005437 if (lys_compile_node_uniqness(ctx, ctx->mod->compiled->data, ctx->mod->compiled->rpcs,
5438 ctx->mod->compiled->notifs, node->name, node)) {
5439 return LY_EVALID;
5440 }
Radek Krejci19a96102018-11-15 13:38:09 +01005441 }
Radek Krejci327de162019-06-14 12:52:07 +02005442 lysc_update_path(ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01005443
5444 return LY_SUCCESS;
5445
5446error:
5447 lysc_node_free(ctx->ctx, node);
5448 return ret;
5449}
5450
Radek Krejciccd20f12019-02-15 14:12:27 +01005451static void
5452lysc_disconnect(struct lysc_node *node)
5453{
Radek Krejci0a048dd2019-02-18 16:01:43 +01005454 struct lysc_node *parent, *child, *prev = NULL, *next;
5455 struct lysc_node_case *cs = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005456 int remove_cs = 0;
5457
5458 parent = node->parent;
5459
5460 /* parent's first child */
5461 if (!parent) {
5462 return;
5463 }
5464 if (parent->nodetype == LYS_CHOICE) {
Radek Krejci0a048dd2019-02-18 16:01:43 +01005465 cs = (struct lysc_node_case*)node;
5466 } else if (parent->nodetype == LYS_CASE) {
5467 /* disconnecting some node in a case */
5468 cs = (struct lysc_node_case*)parent;
5469 parent = cs->parent;
5470 for (child = cs->child; child && child->parent == (struct lysc_node*)cs; child = child->next) {
5471 if (child == node) {
5472 if (cs->child == child) {
5473 if (!child->next || child->next->parent != (struct lysc_node*)cs) {
5474 /* case with a single child -> remove also the case */
5475 child->parent = NULL;
5476 remove_cs = 1;
5477 } else {
5478 cs->child = child->next;
Radek Krejciccd20f12019-02-15 14:12:27 +01005479 }
5480 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005481 break;
Radek Krejciccd20f12019-02-15 14:12:27 +01005482 }
5483 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005484 if (!remove_cs) {
5485 cs = NULL;
5486 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005487 } else if (lysc_node_children(parent, node->flags) == node) {
5488 *lysc_node_children_p(parent, node->flags) = node->next;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005489 }
5490
5491 if (cs) {
5492 if (remove_cs) {
5493 /* cs has only one child which is being also removed */
5494 lysc_disconnect((struct lysc_node*)cs);
5495 lysc_node_free(cs->module->ctx, (struct lysc_node*)cs);
5496 } else {
Radek Krejciccd20f12019-02-15 14:12:27 +01005497 if (((struct lysc_node_choice*)parent)->dflt == cs) {
5498 /* default case removed */
5499 ((struct lysc_node_choice*)parent)->dflt = NULL;
5500 }
5501 if (((struct lysc_node_choice*)parent)->cases == cs) {
5502 /* first case removed */
5503 ((struct lysc_node_choice*)parent)->cases = (struct lysc_node_case*)cs->next;
5504 }
Radek Krejci0a048dd2019-02-18 16:01:43 +01005505 if (cs->child) {
5506 /* cs will be removed and disconnected from its siblings, but we have to take care also about its children */
5507 if (cs->child->prev->parent != (struct lysc_node*)cs) {
5508 prev = cs->child->prev;
5509 } /* else all the children are under a single case */
5510 LY_LIST_FOR_SAFE(cs->child, next, child) {
5511 if (child->parent != (struct lysc_node*)cs) {
5512 break;
5513 }
5514 lysc_node_free(node->module->ctx, child);
5515 }
5516 if (prev) {
5517 if (prev->next) {
5518 prev->next = child;
5519 }
5520 if (child) {
5521 child->prev = prev;
5522 } else {
5523 /* link from the first child under the cases */
5524 ((struct lysc_node_choice*)cs->parent)->cases->child->prev = prev;
5525 }
5526 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005527 }
5528 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005529 }
5530
5531 /* siblings */
Radek Krejci0a048dd2019-02-18 16:01:43 +01005532 if (node->prev->next) {
5533 node->prev->next = node->next;
5534 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005535 if (node->next) {
5536 node->next->prev = node->prev;
Radek Krejci0a048dd2019-02-18 16:01:43 +01005537 } else if (node->nodetype != LYS_CASE) {
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005538 child = (struct lysc_node*)lysc_node_children(parent, node->flags);
Radek Krejci0a048dd2019-02-18 16:01:43 +01005539 if (child) {
5540 child->prev = node->prev;
5541 }
5542 } else if (((struct lysc_node_choice*)parent)->cases) {
5543 ((struct lysc_node_choice*)parent)->cases->prev = node->prev;
Radek Krejciccd20f12019-02-15 14:12:27 +01005544 }
5545}
5546
5547LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02005548lys_compile_deviations(struct lysc_ctx *ctx, struct lysp_module *mod_p)
Radek Krejciccd20f12019-02-15 14:12:27 +01005549{
Radek Krejcia1911222019-07-22 17:24:50 +02005550 LY_ERR ret = LY_EVALID, rc;
Radek Krejciccd20f12019-02-15 14:12:27 +01005551 struct ly_set devs_p = {0};
5552 struct ly_set targets = {0};
5553 struct lysc_node *target; /* target target of the deviation */
Radek Krejci7af64242019-02-18 13:07:53 +01005554 struct lysc_node_list *list;
Radek Krejcifc11bd72019-04-11 16:00:05 +02005555 struct lysc_action *rpcs;
5556 struct lysc_notif *notifs;
Radek Krejciccd20f12019-02-15 14:12:27 +01005557 struct lysp_deviation *dev;
5558 struct lysp_deviate *d, **dp_new;
5559 struct lysp_deviate_add *d_add;
5560 struct lysp_deviate_del *d_del;
5561 struct lysp_deviate_rpl *d_rpl;
Radek Krejci7af64242019-02-18 13:07:53 +01005562 unsigned int u, v, x, y, z;
Radek Krejciccd20f12019-02-15 14:12:27 +01005563 struct lysc_deviation {
5564 const char *nodeid;
5565 struct lysc_node *target; /* target node of the deviation */
5566 struct lysp_deviate** deviates;/* sized array of pointers to parsed deviate statements to apply on target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005567 uint16_t flags; /* target's flags from lys_resolve_schema_nodeid() */
Radek Krejciccd20f12019-02-15 14:12:27 +01005568 uint8_t not_supported; /* flag if deviates contains not-supported deviate */
5569 } **devs = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02005570 int i, changed_type;
Radek Krejciccd20f12019-02-15 14:12:27 +01005571 size_t prefix_len, name_len;
Radek Krejcia1911222019-07-22 17:24:50 +02005572 const char *prefix, *name, *nodeid, *dflt;
Radek Krejciccd20f12019-02-15 14:12:27 +01005573 struct lys_module *mod;
Radek Krejci551b12c2019-02-19 16:11:21 +01005574 uint32_t min, max;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005575 uint16_t flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005576
5577 /* get all deviations from the module and all its submodules ... */
5578 LY_ARRAY_FOR(mod_p->deviations, u) {
5579 ly_set_add(&devs_p, &mod_p->deviations[u], LY_SET_OPT_USEASLIST);
5580 }
5581 LY_ARRAY_FOR(mod_p->includes, v) {
5582 LY_ARRAY_FOR(mod_p->includes[v].submodule->deviations, u) {
5583 ly_set_add(&devs_p, &mod_p->includes[v].submodule->deviations[u], LY_SET_OPT_USEASLIST);
5584 }
5585 }
5586 if (!devs_p.count) {
5587 /* nothing to do */
5588 return LY_SUCCESS;
5589 }
5590
Radek Krejci327de162019-06-14 12:52:07 +02005591 lysc_update_path(ctx, NULL, "{deviation}");
5592
Radek Krejciccd20f12019-02-15 14:12:27 +01005593 /* ... and group them by the target node */
5594 devs = calloc(devs_p.count, sizeof *devs);
5595 for (u = 0; u < devs_p.count; ++u) {
5596 dev = devs_p.objs[u];
Radek Krejci327de162019-06-14 12:52:07 +02005597 lysc_update_path(ctx, NULL, dev->nodeid);
Radek Krejciccd20f12019-02-15 14:12:27 +01005598
5599 /* resolve the target */
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005600 LY_CHECK_GOTO(lys_resolve_schema_nodeid(ctx, dev->nodeid, 0, NULL, ctx->mod, 0, 1,
5601 (const struct lysc_node**)&target, &flags), cleanup);
Radek Krejcif538ce52019-03-05 10:46:14 +01005602 if (target->nodetype == LYS_ACTION) {
5603 /* move the target pointer to input/output to make them different from the action and
5604 * between them. Before the devs[] item is being processed, the target pointer must be fixed
5605 * back to the RPC/action node due to a better compatibility and decision code in this function.
5606 * The LYSC_OPT_INTERNAL is used as a flag to this change. */
5607 if (flags & LYSC_OPT_RPC_INPUT) {
5608 target = (struct lysc_node*)&((struct lysc_action*)target)->input;
5609 flags |= LYSC_OPT_INTERNAL;
5610 } else if (flags & LYSC_OPT_RPC_OUTPUT) {
5611 target = (struct lysc_node*)&((struct lysc_action*)target)->output;
5612 flags |= LYSC_OPT_INTERNAL;
5613 }
5614 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005615 /* insert into the set of targets with duplicity detection */
5616 i = ly_set_add(&targets, target, 0);
5617 if (!devs[i]) {
5618 /* new record */
5619 devs[i] = calloc(1, sizeof **devs);
5620 devs[i]->target = target;
5621 devs[i]->nodeid = dev->nodeid;
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005622 devs[i]->flags = flags;
Radek Krejciccd20f12019-02-15 14:12:27 +01005623 }
5624 /* add deviates into the deviation's list of deviates */
5625 for (d = dev->deviates; d; d = d->next) {
5626 LY_ARRAY_NEW_GOTO(ctx->ctx, devs[i]->deviates, dp_new, ret, cleanup);
5627 *dp_new = d;
5628 if (d->mod == LYS_DEV_NOT_SUPPORTED) {
5629 devs[i]->not_supported = 1;
5630 }
5631 }
Radek Krejci327de162019-06-14 12:52:07 +02005632
5633 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01005634 }
5635
5636 /* MACROS for deviates checking */
5637#define DEV_CHECK_NODETYPE(NODETYPES, DEVTYPE, PROPERTY) \
5638 if (!(devs[u]->target->nodetype & (NODETYPES))) { \
Radek Krejci327de162019-06-14 12:52:07 +02005639 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 +01005640 goto cleanup; \
5641 }
5642
5643#define DEV_CHECK_CARDINALITY(ARRAY, MAX, PROPERTY) \
5644 if (LY_ARRAY_SIZE(ARRAY) > MAX) { \
Radek Krejci327de162019-06-14 12:52:07 +02005645 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS, "Invalid deviation of %s with too many (%u) %s properties.", \
5646 lys_nodetype2str(devs[u]->target->nodetype), LY_ARRAY_SIZE(ARRAY), PROPERTY); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005647 goto cleanup; \
5648 }
5649
Radek Krejcia1911222019-07-22 17:24:50 +02005650
Radek Krejciccd20f12019-02-15 14:12:27 +01005651#define DEV_CHECK_NONPRESENCE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER) \
Radek Krejcia1911222019-07-22 17:24:50 +02005652 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
5653 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
5654 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", \
5655 PROPERTY, ((TYPE)devs[u]->target)->VALUEMEMBER); \
5656 goto cleanup; \
5657 }
5658
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005659#define DEV_CHECK_NONPRESENCE_VALUE(TYPE, COND, MEMBER, PROPERTY, VALUEMEMBER, VALUEMODMEMBER) \
Radek Krejci93dcc392019-02-19 10:43:38 +01005660 if (((TYPE)devs[u]->target)->MEMBER && (COND)) { \
Radek Krejcia1911222019-07-22 17:24:50 +02005661 int dynamic_ = 0; const char *val_; \
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005662 val_ = ((TYPE)devs[u]->target)->VALUEMEMBER->realtype->plugin->print(((TYPE)devs[u]->target)->VALUEMEMBER, LYD_XML, \
5663 lys_get_prefix, ((TYPE)devs[u]->target)->VALUEMODMEMBER, &dynamic_); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005664 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejcia1911222019-07-22 17:24:50 +02005665 "Invalid deviation adding \"%s\" property which already exists (with value \"%s\").", PROPERTY, val_); \
5666 if (dynamic_) {free((void*)val_);} \
Radek Krejciccd20f12019-02-15 14:12:27 +01005667 goto cleanup; \
5668 }
5669
Radek Krejci551b12c2019-02-19 16:11:21 +01005670#define DEV_CHECK_NONPRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5671 if (((TYPE)devs[u]->target)->MEMBER COND) { \
5672 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005673 "Invalid deviation adding \"%s\" property which already exists (with value \"%u\").", \
5674 PROPERTY, ((TYPE)devs[u]->target)->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005675 goto cleanup; \
5676 }
5677
Radek Krejciccd20f12019-02-15 14:12:27 +01005678#define DEV_CHECK_PRESENCE(TYPE, COND, MEMBER, DEVTYPE, PROPERTY, VALUE) \
5679 if (!((TYPE)devs[u]->target)->MEMBER || COND) { \
Radek Krejci327de162019-06-14 12:52:07 +02005680 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT, DEVTYPE, PROPERTY, VALUE); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005681 goto cleanup; \
5682 }
5683
Radek Krejci551b12c2019-02-19 16:11:21 +01005684#define DEV_CHECK_PRESENCE_UINT(TYPE, COND, MEMBER, PROPERTY) \
5685 if (!(((TYPE)devs[u]->target)->MEMBER COND)) { \
5686 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005687 "Invalid deviation replacing with \"%s\" property \"%u\" which is not present.", PROPERTY, d_rpl->MEMBER); \
Radek Krejci551b12c2019-02-19 16:11:21 +01005688 goto cleanup; \
5689 }
5690
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005691#define DEV_DEL_ARRAY(TYPE, ARRAY_TRG, ARRAY_DEV, VALMEMBER, VALMEMBER_CMP, DELFUNC_DEREF, DELFUNC, PROPERTY) \
5692 DEV_CHECK_PRESENCE(TYPE, 0, ARRAY_TRG, "deleting", PROPERTY, d_del->ARRAY_DEV[0]VALMEMBER); \
5693 LY_ARRAY_FOR(d_del->ARRAY_DEV, x) { \
5694 LY_ARRAY_FOR(((TYPE)devs[u]->target)->ARRAY_TRG, y) { \
5695 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 +01005696 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005697 if (y == LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
Radek Krejciccd20f12019-02-15 14:12:27 +01005698 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, \
Radek Krejci327de162019-06-14 12:52:07 +02005699 "Invalid deviation deleting \"%s\" property \"%s\" which does not match any of the target's property values.", \
5700 PROPERTY, d_del->ARRAY_DEV[x]VALMEMBER); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005701 goto cleanup; \
5702 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005703 LY_ARRAY_DECREMENT(((TYPE)devs[u]->target)->ARRAY_TRG); \
5704 DELFUNC(ctx->ctx, DELFUNC_DEREF((TYPE)devs[u]->target)->ARRAY_TRG[y]); \
5705 memmove(&((TYPE)devs[u]->target)->ARRAY_TRG[y], \
5706 &((TYPE)devs[u]->target)->ARRAY_TRG[y + 1], \
5707 (LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG) - y) * (sizeof *((TYPE)devs[u]->target)->ARRAY_TRG)); \
Radek Krejciccd20f12019-02-15 14:12:27 +01005708 } \
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005709 if (!LY_ARRAY_SIZE(((TYPE)devs[u]->target)->ARRAY_TRG)) { \
5710 LY_ARRAY_FREE(((TYPE)devs[u]->target)->ARRAY_TRG); \
5711 ((TYPE)devs[u]->target)->ARRAY_TRG = NULL; \
Radek Krejciccd20f12019-02-15 14:12:27 +01005712 }
5713
5714 /* apply deviations */
5715 for (u = 0; u < devs_p.count && devs[u]; ++u) {
Radek Krejcia1911222019-07-22 17:24:50 +02005716 struct lysc_node_leaf *leaf = (struct lysc_node_leaf*)devs[u]->target;
5717 struct lysc_node_leaflist *llist = (struct lysc_node_leaflist*)devs[u]->target;
5718 struct ly_err_item *err = NULL;
5719
5720 dflt = NULL;
5721 changed_type = 0;
5722
Radek Krejci327de162019-06-14 12:52:07 +02005723 lysc_update_path(ctx, NULL, devs[u]->nodeid);
5724
Radek Krejcif538ce52019-03-05 10:46:14 +01005725 if (devs[u]->flags & LYSC_OPT_INTERNAL) {
5726 /* fix the target pointer in case of RPC's/action's input/output */
5727 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5728 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, input));
5729 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5730 devs[u]->target = (struct lysc_node*)((char*)devs[u]->target - offsetof(struct lysc_action, output));
5731 }
5732 }
5733
Radek Krejciccd20f12019-02-15 14:12:27 +01005734 /* not-supported */
5735 if (devs[u]->not_supported) {
5736 if (LY_ARRAY_SIZE(devs[u]->deviates) > 1) {
5737 LOGWRN(ctx->ctx, "Useless multiple (%u) deviates on node \"%s\" since the node is not-supported.",
5738 LY_ARRAY_SIZE(devs[u]->deviates), devs[u]->nodeid);
5739 }
Radek Krejcifc11bd72019-04-11 16:00:05 +02005740
5741#define REMOVE_NONDATA(ARRAY, TYPE, GETFUNC, FREEFUNC) \
5742 if (devs[u]->target->parent) { \
5743 ARRAY = (TYPE*)GETFUNC(devs[u]->target->parent); \
5744 } else { \
5745 ARRAY = devs[u]->target->module->compiled->ARRAY; \
5746 } \
5747 LY_ARRAY_FOR(ARRAY, x) { \
5748 if (&ARRAY[x] == (TYPE*)devs[u]->target) { break; } \
5749 } \
5750 if (x < LY_ARRAY_SIZE(ARRAY)) { \
5751 FREEFUNC(ctx->ctx, &ARRAY[x]); \
5752 memmove(&ARRAY[x], &ARRAY[x + 1], (LY_ARRAY_SIZE(ARRAY) - (x + 1)) * sizeof *ARRAY); \
5753 LY_ARRAY_DECREMENT(ARRAY); \
5754 }
5755
Radek Krejcif538ce52019-03-05 10:46:14 +01005756 if (devs[u]->target->nodetype == LYS_ACTION) {
5757 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5758 /* remove RPC's/action's input */
5759 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->input);
5760 memset(&((struct lysc_action*)devs[u]->target)->input, 0, sizeof ((struct lysc_action*)devs[u]->target)->input);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005761 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->input_exts, lysc_ext_instance_free);
5762 ((struct lysc_action*)devs[u]->target)->input_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005763 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5764 /* remove RPC's/action's output */
5765 lysc_action_inout_free(ctx->ctx, &((struct lysc_action*)devs[u]->target)->output);
5766 memset(&((struct lysc_action*)devs[u]->target)->output, 0, sizeof ((struct lysc_action*)devs[u]->target)->output);
Radek Krejcifc11bd72019-04-11 16:00:05 +02005767 FREE_ARRAY(ctx->ctx, ((struct lysc_action*)devs[u]->target)->output_exts, lysc_ext_instance_free);
5768 ((struct lysc_action*)devs[u]->target)->output_exts = NULL;
Radek Krejcif538ce52019-03-05 10:46:14 +01005769 } else {
5770 /* remove RPC/action */
Radek Krejcifc11bd72019-04-11 16:00:05 +02005771 REMOVE_NONDATA(rpcs, struct lysc_action, lysc_node_actions, lysc_action_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005772 }
5773 } else if (devs[u]->target->nodetype == LYS_NOTIF) {
Radek Krejcifc11bd72019-04-11 16:00:05 +02005774 /* remove Notification */
5775 REMOVE_NONDATA(notifs, struct lysc_notif, lysc_node_notifs, lysc_notif_free);
Radek Krejcif538ce52019-03-05 10:46:14 +01005776 } else {
5777 /* remove the target node */
5778 lysc_disconnect(devs[u]->target);
5779 lysc_node_free(ctx->ctx, devs[u]->target);
5780 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005781
Radek Krejci474f9b82019-07-24 11:36:37 +02005782 /* mark the context for later re-compilation of objects that could reference the curently removed node */
5783 ctx->ctx->flags |= LY_CTX_CHANGED_TREE;
Radek Krejciccd20f12019-02-15 14:12:27 +01005784 continue;
5785 }
5786
5787 /* list of deviates (not-supported is not present in the list) */
5788 LY_ARRAY_FOR(devs[u]->deviates, v) {
5789 d = devs[u]->deviates[v];
5790
5791 switch (d->mod) {
5792 case LYS_DEV_ADD:
5793 d_add = (struct lysp_deviate_add*)d;
5794 /* [units-stmt] */
5795 if (d_add->units) {
5796 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "add", "units");
5797 DEV_CHECK_NONPRESENCE(struct lysc_node_leaf*, (devs[u]->target->flags & LYS_SET_UNITS), units, "units", units);
5798
5799 FREE_STRING(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
5800 DUP_STRING(ctx->ctx, d_add->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
5801 }
5802
5803 /* *must-stmt */
5804 if (d_add->musts) {
5805 switch (devs[u]->target->nodetype) {
5806 case LYS_CONTAINER:
5807 case LYS_LIST:
5808 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_container*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005809 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005810 break;
5811 case LYS_LEAF:
5812 case LYS_LEAFLIST:
5813 case LYS_ANYDATA:
5814 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_node_leaf*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005815 x, lys_compile_must, ret, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005816 break;
5817 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02005818 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_notif*)devs[u]->target)->musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005819 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005820 break;
5821 case LYS_ACTION:
5822 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
5823 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->input.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005824 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005825 break;
5826 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
5827 COMPILE_ARRAY_GOTO(ctx, d_add->musts, ((struct lysc_action*)devs[u]->target)->output.musts,
Radek Krejciec4da802019-05-02 13:02:41 +02005828 x, lys_compile_must, ret, cleanup);
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005829 break;
5830 }
5831 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01005832 default:
5833 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005834 lys_nodetype2str(devs[u]->target->nodetype), "add", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01005835 goto cleanup;
5836 }
5837 }
5838
5839 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01005840 if (d_add->uniques) {
5841 DEV_CHECK_NODETYPE(LYS_LIST, "add", "unique");
5842 LY_CHECK_GOTO(lys_compile_node_list_unique(ctx, ctx->mod, d_add->uniques, (struct lysc_node_list*)devs[u]->target), cleanup);
5843 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005844
5845 /* *default-stmt */
5846 if (d_add->dflts) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005847 switch (devs[u]->target->nodetype) {
5848 case LYS_LEAF:
5849 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005850 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 +02005851 if (leaf->dflt) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005852 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02005853 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02005854 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
5855 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
5856 } else {
5857 /* prepare new default value storage */
5858 leaf->dflt = calloc(1, sizeof *leaf->dflt);
Radek Krejciccd20f12019-02-15 14:12:27 +01005859 }
Radek Krejcia1911222019-07-22 17:24:50 +02005860 dflt = d_add->dflts[0];
5861 /* parsing is done at the end after possible replace of the leaf's type */
5862
Radek Krejci551b12c2019-02-19 16:11:21 +01005863 /* mark the new default values as leaf's own */
5864 devs[u]->target->flags |= LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01005865 break;
5866 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02005867 if (llist->dflts && !(devs[u]->target->flags & LYS_SET_DFLT)) {
Radek Krejciccd20f12019-02-15 14:12:27 +01005868 /* first, remove the default value taken from the type */
Radek Krejcia1911222019-07-22 17:24:50 +02005869 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02005870 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02005871 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
5872 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
5873 free(llist->dflts[x]);
Radek Krejciccd20f12019-02-15 14:12:27 +01005874 }
Radek Krejcia1911222019-07-22 17:24:50 +02005875 LY_ARRAY_FREE(llist->dflts);
5876 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005877 LY_ARRAY_FREE(llist->dflts_mods);
5878 llist->dflts_mods = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01005879 }
5880 /* add new default value(s) */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005881 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts_mods, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02005882 LY_ARRAY_CREATE_GOTO(ctx->ctx, llist->dflts, LY_ARRAY_SIZE(d_add->dflts), ret, cleanup);
5883 for (x = y = LY_ARRAY_SIZE(llist->dflts);
Radek Krejciccd20f12019-02-15 14:12:27 +01005884 x < LY_ARRAY_SIZE(d_add->dflts) + y; ++x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02005885 LY_ARRAY_INCREMENT(llist->dflts_mods);
5886 llist->dflts_mods[x] = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02005887 LY_ARRAY_INCREMENT(llist->dflts);
5888 llist->dflts[x] = calloc(1, sizeof *llist->dflts[x]);
5889 llist->dflts[x]->realtype = llist->type;
5890 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 +02005891 LY_TYPE_OPTS_INCOMPLETE_DATA |LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE, lys_resolve_prefix,
Radek Krejci1c0c3442019-07-23 16:08:47 +02005892 (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL, llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02005893 llist->dflts[x]->realtype->refcount++;
5894 if (err) {
5895 ly_err_print(err);
5896 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
5897 "Invalid deviation adding \"default\" property \"%s\" which does not fit the type (%s).",
5898 d_add->dflts[x - y], err->msg);
5899 ly_err_free(err);
5900 }
Radek Krejci474f9b82019-07-24 11:36:37 +02005901 if (rc == LY_EINCOMPLETE) {
5902 /* postpone default compilation when the tree is complete */
5903 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
5904
5905 /* but in general result is so far ok */
5906 rc = LY_SUCCESS;
5907 }
Radek Krejcia1911222019-07-22 17:24:50 +02005908 LY_CHECK_GOTO(rc, cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01005909 }
Radek Krejci551b12c2019-02-19 16:11:21 +01005910 /* mark the new default values as leaf-list's own */
Radek Krejciccd20f12019-02-15 14:12:27 +01005911 devs[u]->target->flags |= LYS_SET_DFLT;
5912 break;
5913 case LYS_CHOICE:
5914 DEV_CHECK_CARDINALITY(d_add->dflts, 1, "default");
5915 DEV_CHECK_NONPRESENCE(struct lysc_node_choice*, 1, dflt, "default", dflt->name);
5916 /* in contrast to delete, here we strictly resolve the prefix in the module of the deviation
5917 * to allow making the default case even the augmented case from the deviating module */
Radek Krejci327de162019-06-14 12:52:07 +02005918 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 +01005919 goto cleanup;
5920 }
5921 break;
5922 default:
5923 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02005924 lys_nodetype2str(devs[u]->target->nodetype), "add", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01005925 goto cleanup;
5926 }
5927 }
5928
5929 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01005930 if (d_add->flags & LYS_CONFIG_MASK) {
5931 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02005932 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01005933 lys_nodetype2str(devs[u]->target->nodetype), "add", "config");
5934 goto cleanup;
5935 }
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005936 if (devs[u]->flags) {
Radek Krejci327de162019-06-14 12:52:07 +02005937 LOGWRN(ctx->ctx, "Deviating config inside %s has no effect.",
5938 devs[u]->flags & LYSC_OPT_NOTIFICATION ? "Notification" : "RPC/action");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01005939 }
Radek Krejci93dcc392019-02-19 10:43:38 +01005940 if (devs[u]->target->flags & LYS_SET_CONFIG) {
5941 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005942 "Invalid deviation adding \"config\" property which already exists (with value \"config %s\").",
5943 devs[u]->target->flags & LYS_CONFIG_W ? "true" : "false");
Radek Krejci93dcc392019-02-19 10:43:38 +01005944 goto cleanup;
5945 }
Radek Krejci327de162019-06-14 12:52:07 +02005946 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_add->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01005947 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005948
5949 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01005950 if (d_add->flags & LYS_MAND_MASK) {
5951 if (devs[u]->target->flags & LYS_MAND_MASK) {
5952 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02005953 "Invalid deviation adding \"mandatory\" property which already exists (with value \"mandatory %s\").",
5954 devs[u]->target->flags & LYS_MAND_TRUE ? "true" : "false");
Radek Krejcif1421c22019-02-19 13:05:20 +01005955 goto cleanup;
5956 }
Radek Krejci327de162019-06-14 12:52:07 +02005957 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_add->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01005958 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005959
5960 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005961 if (d_add->flags & LYS_SET_MIN) {
5962 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5963 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
5964 /* change value */
5965 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_add->min;
5966 } else if (devs[u]->target->nodetype == LYS_LIST) {
5967 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
5968 /* change value */
5969 ((struct lysc_node_list*)devs[u]->target)->min = d_add->min;
5970 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005971 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005972 lys_nodetype2str(devs[u]->target->nodetype), "add", "min-elements");
5973 goto cleanup;
5974 }
5975 if (d_add->min) {
5976 devs[u]->target->flags |= LYS_MAND_TRUE;
5977 }
5978 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005979
5980 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01005981 if (d_add->flags & LYS_SET_MAX) {
5982 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
5983 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
5984 /* change value */
5985 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5986 } else if (devs[u]->target->nodetype == LYS_LIST) {
5987 DEV_CHECK_NONPRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
5988 /* change value */
5989 ((struct lysc_node_list*)devs[u]->target)->max = d_add->max ? d_add->max : (uint32_t)-1;
5990 } else {
Radek Krejci327de162019-06-14 12:52:07 +02005991 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01005992 lys_nodetype2str(devs[u]->target->nodetype), "add", "max-elements");
5993 goto cleanup;
5994 }
5995 }
Radek Krejciccd20f12019-02-15 14:12:27 +01005996
5997 break;
5998 case LYS_DEV_DELETE:
5999 d_del = (struct lysp_deviate_del*)d;
6000
6001 /* [units-stmt] */
6002 if (d_del->units) {
6003 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "delete", "units");
Radek Krejcia1911222019-07-22 17:24:50 +02006004 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, 0, units, "deleting", "units", d_del->units);
6005 if (strcmp(((struct lysc_node_leaf*)devs[u]->target)->units, d_del->units)) {
6006 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6007 "Invalid deviation deleting \"units\" property \"%s\" which does not match the target's property value \"%s\".",
6008 d_del->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6009 goto cleanup;
6010 }
6011 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6012 ((struct lysc_node_leaf*)devs[u]->target)->units = NULL;
Radek Krejciccd20f12019-02-15 14:12:27 +01006013 }
6014
6015 /* *must-stmt */
6016 if (d_del->musts) {
6017 switch (devs[u]->target->nodetype) {
6018 case LYS_CONTAINER:
6019 case LYS_LIST:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006020 DEV_DEL_ARRAY(struct lysc_node_container*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006021 break;
6022 case LYS_LEAF:
6023 case LYS_LEAFLIST:
6024 case LYS_ANYDATA:
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006025 DEV_DEL_ARRAY(struct lysc_node_leaf*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006026 break;
6027 case LYS_NOTIF:
Radek Krejcifc11bd72019-04-11 16:00:05 +02006028 DEV_DEL_ARRAY(struct lysc_notif*, musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
Radek Krejci6eeb58f2019-02-22 16:29:37 +01006029 break;
6030 case LYS_ACTION:
6031 if (devs[u]->flags & LYSC_OPT_RPC_INPUT) {
6032 DEV_DEL_ARRAY(struct lysc_action*, input.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6033 break;
6034 } else if (devs[u]->flags & LYSC_OPT_RPC_OUTPUT) {
6035 DEV_DEL_ARRAY(struct lysc_action*, output.musts, musts, .arg, .cond->expr, &, lysc_must_free, "must");
6036 break;
6037 }
6038 /* fall through */
Radek Krejciccd20f12019-02-15 14:12:27 +01006039 default:
6040 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006041 lys_nodetype2str(devs[u]->target->nodetype), "delete", "must");
Radek Krejciccd20f12019-02-15 14:12:27 +01006042 goto cleanup;
6043 }
6044 }
6045
6046 /* *unique-stmt */
Radek Krejci7af64242019-02-18 13:07:53 +01006047 if (d_del->uniques) {
6048 DEV_CHECK_NODETYPE(LYS_LIST, "delete", "unique");
6049 list = (struct lysc_node_list*)devs[u]->target; /* shortcut */
6050 LY_ARRAY_FOR(d_del->uniques, x) {
6051 LY_ARRAY_FOR(list->uniques, z) {
6052 for (name = d_del->uniques[x], y = 0; name; name = nodeid, ++y) {
6053 nodeid = strpbrk(name, " \t\n");
6054 if (nodeid) {
6055 if (strncmp(name, list->uniques[z][y]->name, nodeid - name)
6056 || list->uniques[z][y]->name[nodeid - name] != '\0') {
6057 break;
6058 }
6059 while (isspace(*nodeid)) {
6060 ++nodeid;
6061 }
6062 } else {
6063 if (strcmp(name, list->uniques[z][y]->name)) {
6064 break;
6065 }
6066 }
6067 }
6068 if (!name) {
6069 /* complete match - remove the unique */
6070 LY_ARRAY_DECREMENT(list->uniques);
6071 LY_ARRAY_FREE(list->uniques[z]);
6072 memmove(&list->uniques[z], &list->uniques[z + 1], (LY_ARRAY_SIZE(list->uniques) - z) * (sizeof *list->uniques));
6073 --z;
6074 break;
6075 }
6076 }
6077 if (!list->uniques || z == LY_ARRAY_SIZE(list->uniques)) {
6078 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006079 "Invalid deviation deleting \"unique\" property \"%s\" which does not match any of the target's property values.",
6080 d_del->uniques[x]);
Radek Krejci7af64242019-02-18 13:07:53 +01006081 goto cleanup;
6082 }
6083 }
6084 if (!LY_ARRAY_SIZE(list->uniques)) {
6085 LY_ARRAY_FREE(list->uniques);
6086 list->uniques = NULL;
6087 }
6088 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006089
6090 /* *default-stmt */
6091 if (d_del->dflts) {
6092 switch (devs[u]->target->nodetype) {
6093 case LYS_LEAF:
6094 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6095 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6096 dflt, "deleting", "default", d_del->dflts[0]);
6097
Radek Krejcia1911222019-07-22 17:24:50 +02006098 /* check that the values matches */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006099 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &i);
Radek Krejcia1911222019-07-22 17:24:50 +02006100 if (strcmp(dflt, d_del->dflts[0])) {
6101 if (i) {
6102 free((char*)dflt);
6103 }
6104 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
6105 "Invalid deviation deleting \"default\" property \"%s\" which does not match the target's property value \"%s\".",
6106 d_del->dflts[0], dflt);
6107 goto cleanup;
6108 }
6109 if (i) {
6110 free((char*)dflt);
6111 }
6112 dflt = NULL;
6113
Radek Krejci474f9b82019-07-24 11:36:37 +02006114 /* update the list of incomplete default values if needed */
6115 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6116
Radek Krejcia1911222019-07-22 17:24:50 +02006117 /* remove the default specification */
6118 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6119 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6120 free(leaf->dflt);
6121 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006122 leaf->dflt_mod = NULL;
Radek Krejci551b12c2019-02-19 16:11:21 +01006123 devs[u]->target->flags &= ~LYS_SET_DFLT;
Radek Krejciccd20f12019-02-15 14:12:27 +01006124 break;
6125 case LYS_LEAFLIST:
Radek Krejcia1911222019-07-22 17:24:50 +02006126 DEV_CHECK_PRESENCE(struct lysc_node_leaflist*, 0, dflts, "deleting", "default", d_del->dflts[0]);
6127 LY_ARRAY_FOR(d_del->dflts, x) {
6128 LY_ARRAY_FOR(llist->dflts, y) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006129 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 +02006130 if (!strcmp(dflt, d_del->dflts[x])) {
6131 if (i) {
6132 free((char*)dflt);
6133 }
6134 dflt = NULL;
6135 break;
6136 }
6137 if (i) {
6138 free((char*)dflt);
6139 }
6140 dflt = NULL;
6141 }
6142 if (y == LY_ARRAY_SIZE(llist->dflts)) {
6143 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE, "Invalid deviation deleting \"default\" property \"%s\" "
6144 "which does not match any of the target's property values.", d_del->dflts[x]);
6145 goto cleanup;
6146 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006147
6148 /* update the list of incomplete default values if needed */
6149 lysc_incomplete_dflts_remove(ctx, llist->dflts[y]);
6150
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006151 LY_ARRAY_DECREMENT(llist->dflts_mods);
Radek Krejcia1911222019-07-22 17:24:50 +02006152 LY_ARRAY_DECREMENT(llist->dflts);
6153 llist->dflts[y]->realtype->plugin->free(ctx->ctx, llist->dflts[y]);
6154 lysc_type_free(ctx->ctx, llist->dflts[y]->realtype);
6155 free(llist->dflts[y]);
6156 memmove(&llist->dflts[y], &llist->dflts[y + 1], (LY_ARRAY_SIZE(llist->dflts) - y) * (sizeof *llist->dflts));
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006157 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 +02006158 }
6159 if (!LY_ARRAY_SIZE(llist->dflts)) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006160 LY_ARRAY_FREE(llist->dflts_mods);
6161 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006162 LY_ARRAY_FREE(llist->dflts);
6163 llist->dflts = NULL;
6164 llist->flags &= ~LYS_SET_DFLT;
Radek Krejci551b12c2019-02-19 16:11:21 +01006165 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006166 break;
6167 case LYS_CHOICE:
6168 DEV_CHECK_CARDINALITY(d_del->dflts, 1, "default");
6169 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "deleting", "default", d_del->dflts[0]);
6170 nodeid = d_del->dflts[0];
Radek Krejcib4a4a272019-06-10 12:44:52 +02006171 LY_CHECK_GOTO(ly_parse_nodeid(&nodeid, &prefix, &prefix_len, &name, &name_len), cleanup);
Radek Krejciccd20f12019-02-15 14:12:27 +01006172 if (prefix) {
6173 /* use module prefixes from the deviation module to match the module of the default case */
6174 if (!(mod = lys_module_find_prefix(ctx->mod, prefix, prefix_len))) {
6175 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006176 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6177 "The prefix does not match any imported module of the deviation module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006178 goto cleanup;
6179 }
6180 if (mod != ((struct lysc_node_choice*)devs[u]->target)->dflt->module) {
6181 LOGVAL(ctx->ctx,LY_VLOG_STR,ctx->path,LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006182 "Invalid deviation deleting \"default\" property \"%s\" of choice. "
6183 "The prefix does not match the default case's module.", d_del->dflts[0]);
Radek Krejciccd20f12019-02-15 14:12:27 +01006184 goto cleanup;
6185 }
6186 }
6187 /* else {
6188 * strictly, the default prefix would point to the deviation module, but the value should actually
6189 * match the default string in the original module (usually unprefixed), so in this case we do not check
6190 * the module of the default case, just matching its name */
6191 if (strcmp(name, ((struct lysc_node_choice*)devs[u]->target)->dflt->name)) {
6192 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
Radek Krejci327de162019-06-14 12:52:07 +02006193 "Invalid deviation deleting \"default\" property \"%s\" of choice does not match the default case name \"%s\".",
6194 d_del->dflts[0], ((struct lysc_node_choice*)devs[u]->target)->dflt->name);
Radek Krejciccd20f12019-02-15 14:12:27 +01006195 goto cleanup;
6196 }
6197 ((struct lysc_node_choice*)devs[u]->target)->dflt->flags &= ~LYS_SET_DFLT;
6198 ((struct lysc_node_choice*)devs[u]->target)->dflt = NULL;
6199 break;
6200 default:
6201 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006202 lys_nodetype2str(devs[u]->target->nodetype), "delete", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006203 goto cleanup;
6204 }
6205 }
6206
6207 break;
6208 case LYS_DEV_REPLACE:
6209 d_rpl = (struct lysp_deviate_rpl*)d;
6210
6211 /* [type-stmt] */
Radek Krejci33f72892019-02-21 10:36:58 +01006212 if (d_rpl->type) {
6213 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "type");
6214 /* type is mandatory, so checking for its presence is not necessary */
6215 lysc_type_free(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->type);
Radek Krejcia1911222019-07-22 17:24:50 +02006216
6217 if (leaf->dflt && !(devs[u]->target->flags & LYS_SET_DFLT)) {
6218 /* the target has default from the previous type - remove it */
6219 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006220 /* update the list of incomplete default values if needed */
6221 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6222
Radek Krejcia1911222019-07-22 17:24:50 +02006223 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6224 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6225 free(leaf->dflt);
6226 leaf->dflt = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006227 leaf->dflt_mod = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006228 } else { /* LYS_LEAFLIST */
6229 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejci474f9b82019-07-24 11:36:37 +02006230 lysc_incomplete_dflts_remove(ctx, llist->dflts[x]);
Radek Krejcia1911222019-07-22 17:24:50 +02006231 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6232 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6233 free(llist->dflts[x]);
6234 }
6235 LY_ARRAY_FREE(llist->dflts);
6236 llist->dflts = NULL;
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006237 LY_ARRAY_FREE(llist->dflts_mods);
6238 llist->dflts_mods = NULL;
Radek Krejcia1911222019-07-22 17:24:50 +02006239 }
6240 }
6241 if (!leaf->dflt) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006242 /* there is no default value, do not set changed_type after type compilation
6243 * which is used to recompile the default value */
Radek Krejcia1911222019-07-22 17:24:50 +02006244 changed_type = -1;
6245 }
Radek Krejciec4da802019-05-02 13:02:41 +02006246 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 +02006247 changed_type++;
Radek Krejci33f72892019-02-21 10:36:58 +01006248 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006249
6250 /* [units-stmt] */
6251 if (d_rpl->units) {
6252 DEV_CHECK_NODETYPE(LYS_LEAF | LYS_LEAFLIST, "replace", "units");
6253 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_UNITS),
6254 units, "replacing", "units", d_rpl->units);
6255
6256 lydict_remove(ctx->ctx, ((struct lysc_node_leaf*)devs[u]->target)->units);
6257 DUP_STRING(ctx->ctx, d_rpl->units, ((struct lysc_node_leaf*)devs[u]->target)->units);
6258 }
6259
6260 /* [default-stmt] */
6261 if (d_rpl->dflt) {
6262 switch (devs[u]->target->nodetype) {
6263 case LYS_LEAF:
6264 DEV_CHECK_PRESENCE(struct lysc_node_leaf*, !(devs[u]->target->flags & LYS_SET_DFLT),
6265 dflt, "replacing", "default", d_rpl->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006266 /* first, remove the default value taken from the type */
Radek Krejci474f9b82019-07-24 11:36:37 +02006267 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
Radek Krejcia1911222019-07-22 17:24:50 +02006268 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6269 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6270 dflt = d_rpl->dflt;
6271 /* parsing is done at the end after possible replace of the leaf's type */
Radek Krejciccd20f12019-02-15 14:12:27 +01006272 break;
6273 case LYS_CHOICE:
6274 DEV_CHECK_PRESENCE(struct lysc_node_choice*, 0, dflt, "replacing", "default", d_rpl->dflt);
Radek Krejci327de162019-06-14 12:52:07 +02006275 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 +01006276 goto cleanup;
6277 }
6278 break;
6279 default:
6280 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci327de162019-06-14 12:52:07 +02006281 lys_nodetype2str(devs[u]->target->nodetype), "replace", "default");
Radek Krejciccd20f12019-02-15 14:12:27 +01006282 goto cleanup;
6283 }
6284 }
6285
6286 /* [config-stmt] */
Radek Krejci93dcc392019-02-19 10:43:38 +01006287 if (d_rpl->flags & LYS_CONFIG_MASK) {
6288 if (devs[u]->target->nodetype & (LYS_CASE | LYS_INOUT | LYS_ACTION | LYS_NOTIF)) {
Radek Krejci327de162019-06-14 12:52:07 +02006289 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci93dcc392019-02-19 10:43:38 +01006290 lys_nodetype2str(devs[u]->target->nodetype), "replace", "config");
6291 goto cleanup;
6292 }
6293 if (!(devs[u]->target->flags & LYS_SET_CONFIG)) {
Radek Krejci327de162019-06-14 12:52:07 +02006294 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejci93dcc392019-02-19 10:43:38 +01006295 "replacing", "config", d_rpl->flags & LYS_CONFIG_W ? "config true" : "config false");
6296 goto cleanup;
6297 }
Radek Krejci327de162019-06-14 12:52:07 +02006298 LY_CHECK_GOTO(lys_compile_change_config(ctx, devs[u]->target, d_rpl->flags, 0, 0), cleanup);
Radek Krejci93dcc392019-02-19 10:43:38 +01006299 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006300
6301 /* [mandatory-stmt] */
Radek Krejcif1421c22019-02-19 13:05:20 +01006302 if (d_rpl->flags & LYS_MAND_MASK) {
6303 if (!(devs[u]->target->flags & LYS_MAND_MASK)) {
Radek Krejci327de162019-06-14 12:52:07 +02006304 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NOT_PRESENT,
Radek Krejcif1421c22019-02-19 13:05:20 +01006305 "replacing", "mandatory", d_rpl->flags & LYS_MAND_TRUE ? "mandatory true" : "mandatory false");
6306 goto cleanup;
6307 }
Radek Krejci327de162019-06-14 12:52:07 +02006308 LY_CHECK_GOTO(lys_compile_change_mandatory(ctx, devs[u]->target, d_rpl->flags, 0), cleanup);
Radek Krejcif1421c22019-02-19 13:05:20 +01006309 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006310
6311 /* [min-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006312 if (d_rpl->flags & LYS_SET_MIN) {
6313 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6314 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, > 0, min, "min-elements");
6315 /* change value */
6316 ((struct lysc_node_leaflist*)devs[u]->target)->min = d_rpl->min;
6317 } else if (devs[u]->target->nodetype == LYS_LIST) {
6318 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, > 0, min, "min-elements");
6319 /* change value */
6320 ((struct lysc_node_list*)devs[u]->target)->min = d_rpl->min;
6321 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006322 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006323 lys_nodetype2str(devs[u]->target->nodetype), "replace", "min-elements");
6324 goto cleanup;
6325 }
6326 if (d_rpl->min) {
6327 devs[u]->target->flags |= LYS_MAND_TRUE;
6328 }
6329 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006330
6331 /* [max-elements-stmt] */
Radek Krejci551b12c2019-02-19 16:11:21 +01006332 if (d_rpl->flags & LYS_SET_MAX) {
6333 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6334 DEV_CHECK_PRESENCE_UINT(struct lysc_node_leaflist*, < (uint32_t)-1, max, "max-elements");
6335 /* change value */
6336 ((struct lysc_node_leaflist*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6337 } else if (devs[u]->target->nodetype == LYS_LIST) {
6338 DEV_CHECK_PRESENCE_UINT(struct lysc_node_list*, < (uint32_t)-1, max, "max-elements");
6339 /* change value */
6340 ((struct lysc_node_list*)devs[u]->target)->max = d_rpl->max ? d_rpl->max : (uint32_t)-1;
6341 } else {
Radek Krejci327de162019-06-14 12:52:07 +02006342 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_DEV_NODETYPE,
Radek Krejci551b12c2019-02-19 16:11:21 +01006343 lys_nodetype2str(devs[u]->target->nodetype), "replace", "max-elements");
6344 goto cleanup;
6345 }
6346 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006347
6348 break;
6349 default:
6350 LOGINT(ctx->ctx);
6351 goto cleanup;
6352 }
6353 }
Radek Krejci551b12c2019-02-19 16:11:21 +01006354
Radek Krejci33f72892019-02-21 10:36:58 +01006355 /* final check when all deviations of a single target node are applied */
6356
Radek Krejci551b12c2019-02-19 16:11:21 +01006357 /* check min-max compatibility */
6358 if (devs[u]->target->nodetype == LYS_LEAFLIST) {
6359 min = ((struct lysc_node_leaflist*)devs[u]->target)->min;
6360 max = ((struct lysc_node_leaflist*)devs[u]->target)->max;
6361 } else if (devs[u]->target->nodetype == LYS_LIST) {
6362 min = ((struct lysc_node_list*)devs[u]->target)->min;
6363 max = ((struct lysc_node_list*)devs[u]->target)->max;
6364 } else {
6365 min = max = 0;
6366 }
6367 if (min > max) {
6368 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 +02006369 "after deviation: min value %u is bigger than max value %u.", min, max);
Radek Krejci551b12c2019-02-19 16:11:21 +01006370 goto cleanup;
6371 }
6372
Radek Krejcia1911222019-07-22 17:24:50 +02006373 if (dflt) {
6374 /* parse added/changed default value after possible change of the type */
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006375 leaf->dflt_mod = ctx->mod_def;
Radek Krejcia1911222019-07-22 17:24:50 +02006376 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006377 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6378 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006379 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006380 leaf->dflt->realtype->refcount++;
6381 if (err) {
6382 ly_err_print(err);
6383 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6384 "Invalid deviation setting \"default\" property \"%s\" which does not fit the type (%s).", dflt, err->msg);
6385 ly_err_free(err);
6386 }
Radek Krejci474f9b82019-07-24 11:36:37 +02006387 if (rc == LY_EINCOMPLETE) {
6388 /* postpone default compilation when the tree is complete */
6389 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6390
6391 /* but in general result is so far ok */
6392 rc = LY_SUCCESS;
6393 }
Radek Krejcia1911222019-07-22 17:24:50 +02006394 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006395 } else if (changed_type) {
Radek Krejcia1911222019-07-22 17:24:50 +02006396 /* the leaf/leaf-list's type has changed, but there is still a default value for the previous type */
6397 int dynamic;
6398 if (devs[u]->target->nodetype == LYS_LEAF) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006399 dflt = leaf->dflt->realtype->plugin->print(leaf->dflt, LYD_XML, lys_get_prefix, leaf->dflt_mod, &dynamic);
Radek Krejci474f9b82019-07-24 11:36:37 +02006400
6401 /* update the list of incomplete default values if needed */
6402 lysc_incomplete_dflts_remove(ctx, leaf->dflt);
6403
6404 /* remove the previous default */
Radek Krejcia1911222019-07-22 17:24:50 +02006405 leaf->dflt->realtype->plugin->free(ctx->ctx, leaf->dflt);
6406 lysc_type_free(ctx->ctx, leaf->dflt->realtype);
6407 leaf->dflt->realtype = leaf->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006408 rc = leaf->type->plugin->store(ctx->ctx, leaf->type, dflt, strlen(dflt),
6409 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006410 lys_resolve_prefix, (void*)leaf->dflt_mod, LYD_XML, devs[u]->target, NULL, leaf->dflt, NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006411 leaf->dflt->realtype->refcount++;
6412 if (err) {
6413 ly_err_print(err);
6414 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6415 "Invalid deviation replacing leaf's type - the leaf's default value \"%s\" does not match the type (%s).", dflt, err->msg);
6416 ly_err_free(err);
6417 }
6418 if (dynamic) {
6419 free((void*)dflt);
6420 }
Radek Krejcia1911222019-07-22 17:24:50 +02006421 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006422 if (rc == LY_EINCOMPLETE) {
6423 /* postpone default compilation when the tree is complete */
6424 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, leaf->dflt, leaf->dflt_mod), cleanup);
6425
6426 /* but in general result is so far ok */
6427 rc = LY_SUCCESS;
6428 }
6429 LY_CHECK_GOTO(rc, cleanup);
Radek Krejcia1911222019-07-22 17:24:50 +02006430 } else { /* LYS_LEAFLIST */
6431 LY_ARRAY_FOR(llist->dflts, x) {
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006432 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 +02006433 llist->dflts[x]->realtype->plugin->free(ctx->ctx, llist->dflts[x]);
6434 lysc_type_free(ctx->ctx, llist->dflts[x]->realtype);
6435 llist->dflts[x]->realtype = llist->type;
Radek Krejci474f9b82019-07-24 11:36:37 +02006436 rc = llist->type->plugin->store(ctx->ctx, llist->type, dflt, strlen(dflt),
6437 LY_TYPE_OPTS_INCOMPLETE_DATA | LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE,
Radek Krejci1c0c3442019-07-23 16:08:47 +02006438 lys_resolve_prefix, (void*)llist->dflts_mods[x], LYD_XML, devs[u]->target, NULL,
6439 llist->dflts[x], NULL, &err);
Radek Krejcia1911222019-07-22 17:24:50 +02006440 llist->dflts[x]->realtype->refcount++;
6441 if (err) {
6442 ly_err_print(err);
6443 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
6444 "Invalid deviation replacing leaf-list's type - the leaf-list's default value \"%s\" does not match the type (%s).",
6445 dflt, err->msg);
6446 ly_err_free(err);
6447 }
6448 if (dynamic) {
6449 free((void*)dflt);
6450 }
Radek Krejcid0ef1af2019-07-23 12:22:05 +02006451 dflt = NULL;
Radek Krejci474f9b82019-07-24 11:36:37 +02006452 if (rc == LY_EINCOMPLETE) {
6453 /* postpone default compilation when the tree is complete */
6454 LY_CHECK_GOTO(lysc_incomplete_dflts_add(ctx, devs[u]->target, llist->dflts[x], llist->dflts_mods[x]), cleanup);
6455
6456 /* but in general result is so far ok */
6457 rc = LY_SUCCESS;
6458 }
Radek Krejcia1911222019-07-22 17:24:50 +02006459 LY_CHECK_GOTO(rc, cleanup);
6460 }
6461 }
6462 }
6463
Radek Krejci551b12c2019-02-19 16:11:21 +01006464 /* check mandatory - default compatibility */
6465 if ((devs[u]->target->nodetype & (LYS_LEAF | LYS_LEAFLIST))
6466 && (devs[u]->target->flags & LYS_SET_DFLT)
6467 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6468 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006469 "Invalid deviation combining default value and mandatory %s.", lys_nodetype2str(devs[u]->target->nodetype));
Radek Krejci551b12c2019-02-19 16:11:21 +01006470 goto cleanup;
6471 } else if ((devs[u]->target->nodetype & LYS_CHOICE)
6472 && ((struct lysc_node_choice*)devs[u]->target)->dflt
6473 && (devs[u]->target->flags & LYS_MAND_TRUE)) {
Radek Krejci327de162019-06-14 12:52:07 +02006474 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 +01006475 goto cleanup;
6476 }
6477 if (devs[u]->target->parent && (devs[u]->target->parent->flags & LYS_SET_DFLT) && (devs[u]->target->flags & LYS_MAND_TRUE)) {
6478 /* mandatory node under a default case */
6479 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
Radek Krejci327de162019-06-14 12:52:07 +02006480 "Invalid deviation combining mandatory %s \"%s\" in a default choice's case \"%s\".",
6481 lys_nodetype2str(devs[u]->target->nodetype), devs[u]->target->name, devs[u]->target->parent->name);
Radek Krejci551b12c2019-02-19 16:11:21 +01006482 goto cleanup;
6483 }
Radek Krejci33f72892019-02-21 10:36:58 +01006484
Radek Krejci327de162019-06-14 12:52:07 +02006485 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006486 }
6487
Radek Krejci327de162019-06-14 12:52:07 +02006488 lysc_update_path(ctx, NULL, NULL);
Radek Krejciccd20f12019-02-15 14:12:27 +01006489 ret = LY_SUCCESS;
6490
6491cleanup:
6492 for (u = 0; u < devs_p.count && devs[u]; ++u) {
6493 LY_ARRAY_FREE(devs[u]->deviates);
6494 free(devs[u]);
6495 }
6496 free(devs);
6497 ly_set_erase(&targets, NULL);
6498 ly_set_erase(&devs_p, NULL);
6499
6500 return ret;
6501}
6502
Radek Krejcib56c7502019-02-13 14:19:54 +01006503/**
Radek Krejcid05cbd92018-12-05 14:26:40 +01006504 * @brief Compile the given YANG submodule into the main module.
6505 * @param[in] ctx Compile context
6506 * @param[in] inc Include structure from the main module defining the submodule.
Radek Krejcid05cbd92018-12-05 14:26:40 +01006507 * @return LY_ERR value - LY_SUCCESS or LY_EVALID.
6508 */
6509LY_ERR
Radek Krejciec4da802019-05-02 13:02:41 +02006510lys_compile_submodule(struct lysc_ctx *ctx, struct lysp_include *inc)
Radek Krejcid05cbd92018-12-05 14:26:40 +01006511{
6512 unsigned int u;
6513 LY_ERR ret = LY_SUCCESS;
6514 /* shortcuts */
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006515 struct lysp_submodule *submod = inc->submodule;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006516 struct lysc_module *mainmod = ctx->mod->compiled;
Radek Krejci474f9b82019-07-24 11:36:37 +02006517 struct lysp_node *node_p;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006518
Radek Krejci0af46292019-01-11 16:02:31 +01006519 if (!mainmod->mod->off_features) {
6520 /* features are compiled directly into the compiled module structure,
6521 * but it must be done in two steps to allow forward references (via if-feature) between the features themselves.
6522 * The features compilation is finished in the main module (lys_compile()). */
Radek Krejci327de162019-06-14 12:52:07 +02006523 ret = lys_feature_precompile(ctx, NULL, NULL, submod->features,
Radek Krejci0af46292019-01-11 16:02:31 +01006524 mainmod->mod->off_features ? &mainmod->mod->off_features : &mainmod->features);
6525 LY_CHECK_GOTO(ret, error);
6526 }
6527
Radek Krejci327de162019-06-14 12:52:07 +02006528 lysc_update_path(ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006529 COMPILE_ARRAY_UNIQUE_GOTO(ctx, submod->identities, mainmod->identities, u, lys_compile_identity, ret, error);
Radek Krejci327de162019-06-14 12:52:07 +02006530 lysc_update_path(ctx, NULL, NULL);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006531
Radek Krejci474f9b82019-07-24 11:36:37 +02006532 /* data nodes */
6533 LY_LIST_FOR(submod->data, node_p) {
6534 ret = lys_compile_node(ctx, node_p, NULL, 0);
6535 LY_CHECK_GOTO(ret, error);
6536 }
Radek Krejci8cce8532019-03-05 11:27:45 +01006537
Radek Krejciec4da802019-05-02 13:02:41 +02006538 COMPILE_ARRAY1_GOTO(ctx, submod->rpcs, mainmod->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6539 COMPILE_ARRAY1_GOTO(ctx, submod->notifs, mainmod->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejci8cce8532019-03-05 11:27:45 +01006540
Radek Krejcid05cbd92018-12-05 14:26:40 +01006541error:
6542 return ret;
6543}
6544
Radek Krejci19a96102018-11-15 13:38:09 +01006545LY_ERR
6546lys_compile(struct lys_module *mod, int options)
6547{
6548 struct lysc_ctx ctx = {0};
6549 struct lysc_module *mod_c;
Radek Krejci412ddfa2018-11-23 11:44:11 +01006550 struct lysc_type *type, *typeiter;
Radek Krejci19a96102018-11-15 13:38:09 +01006551 struct lysp_module *sp;
6552 struct lysp_node *node_p;
Radek Krejci95710c92019-02-11 15:49:55 +01006553 struct lysp_augment **augments = NULL;
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006554 struct lysp_grp *grps;
Radek Krejci95710c92019-02-11 15:49:55 +01006555 struct lys_module *m;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006556 unsigned int u, v;
Radek Krejcid05cbd92018-12-05 14:26:40 +01006557 LY_ERR ret = LY_SUCCESS;
Radek Krejci19a96102018-11-15 13:38:09 +01006558
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006559 LY_CHECK_ARG_RET(NULL, mod, mod->parsed, mod->ctx, LY_EINVAL);
Radek Krejci096235c2019-01-11 11:12:19 +01006560
6561 if (!mod->implemented) {
6562 /* just imported modules are not compiled */
6563 return LY_SUCCESS;
6564 }
6565
Radek Krejci19a96102018-11-15 13:38:09 +01006566 sp = mod->parsed;
6567
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006568 ctx.ctx = mod->ctx;
Radek Krejci19a96102018-11-15 13:38:09 +01006569 ctx.mod = mod;
Radek Krejcie86bf772018-12-14 11:39:53 +01006570 ctx.mod_def = mod;
Radek Krejciec4da802019-05-02 13:02:41 +02006571 ctx.options = options;
Radek Krejci327de162019-06-14 12:52:07 +02006572 ctx.path_len = 1;
6573 ctx.path[0] = '/';
Radek Krejci19a96102018-11-15 13:38:09 +01006574
6575 mod->compiled = mod_c = calloc(1, sizeof *mod_c);
Radek Krejci0bcdaed2019-01-10 10:21:34 +01006576 LY_CHECK_ERR_RET(!mod_c, LOGMEM(mod->ctx), LY_EMEM);
6577 mod_c->mod = mod;
Radek Krejci19a96102018-11-15 13:38:09 +01006578
Radek Krejciec4da802019-05-02 13:02:41 +02006579 COMPILE_ARRAY_GOTO(&ctx, sp->imports, mod_c->imports, u, lys_compile_import, ret, error);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006580 LY_ARRAY_FOR(sp->includes, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006581 ret = lys_compile_submodule(&ctx, &sp->includes[u]);
Radek Krejcid05cbd92018-12-05 14:26:40 +01006582 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6583 }
Radek Krejci0af46292019-01-11 16:02:31 +01006584 if (mod->off_features) {
6585 /* there is already precompiled array of features */
6586 mod_c->features = mod->off_features;
6587 mod->off_features = NULL;
Radek Krejci0af46292019-01-11 16:02:31 +01006588 } else {
6589 /* features are compiled directly into the compiled module structure,
6590 * 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 +02006591 ret = lys_feature_precompile(&ctx, NULL, NULL, sp->features, &mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006592 LY_CHECK_GOTO(ret, error);
6593 }
6594 /* finish feature compilation, not only for the main module, but also for the submodules.
6595 * Due to possible forward references, it must be done when all the features (including submodules)
6596 * are present. */
6597 LY_ARRAY_FOR(sp->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006598 ret = lys_feature_precompile_finish(&ctx, &sp->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006599 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6600 }
Radek Krejci327de162019-06-14 12:52:07 +02006601 lysc_update_path(&ctx, NULL, "{submodule}");
Radek Krejci0af46292019-01-11 16:02:31 +01006602 LY_ARRAY_FOR(sp->includes, v) {
Radek Krejci327de162019-06-14 12:52:07 +02006603 lysc_update_path(&ctx, NULL, sp->includes[v].name);
Radek Krejci0af46292019-01-11 16:02:31 +01006604 LY_ARRAY_FOR(sp->includes[v].submodule->features, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006605 ret = lys_feature_precompile_finish(&ctx, &sp->includes[v].submodule->features[u], mod_c->features);
Radek Krejci0af46292019-01-11 16:02:31 +01006606 LY_CHECK_GOTO(ret != LY_SUCCESS, error);
6607 }
Radek Krejci327de162019-06-14 12:52:07 +02006608 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006609 }
Radek Krejci327de162019-06-14 12:52:07 +02006610 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci0af46292019-01-11 16:02:31 +01006611
Radek Krejci327de162019-06-14 12:52:07 +02006612 lysc_update_path(&ctx, NULL, "{identity}");
Radek Krejciec4da802019-05-02 13:02:41 +02006613 COMPILE_ARRAY_UNIQUE_GOTO(&ctx, sp->identities, mod_c->identities, u, lys_compile_identity, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006614 if (sp->identities) {
6615 LY_CHECK_RET(lys_compile_identities_derived(&ctx, sp->identities, mod_c->identities));
6616 }
Radek Krejci327de162019-06-14 12:52:07 +02006617 lysc_update_path(&ctx, NULL, NULL);
Radek Krejci19a96102018-11-15 13:38:09 +01006618
Radek Krejci95710c92019-02-11 15:49:55 +01006619 /* data nodes */
Radek Krejci19a96102018-11-15 13:38:09 +01006620 LY_LIST_FOR(sp->data, node_p) {
Radek Krejciec4da802019-05-02 13:02:41 +02006621 ret = lys_compile_node(&ctx, node_p, NULL, 0);
Radek Krejci19a96102018-11-15 13:38:09 +01006622 LY_CHECK_GOTO(ret, error);
6623 }
Radek Krejci95710c92019-02-11 15:49:55 +01006624
Radek Krejciec4da802019-05-02 13:02:41 +02006625 COMPILE_ARRAY1_GOTO(&ctx, sp->rpcs, mod_c->rpcs, NULL, u, lys_compile_action, 0, ret, error);
6626 COMPILE_ARRAY1_GOTO(&ctx, sp->notifs, mod_c->notifs, NULL, u, lys_compile_notif, 0, ret, error);
Radek Krejciccd20f12019-02-15 14:12:27 +01006627
Radek Krejci95710c92019-02-11 15:49:55 +01006628 /* augments - sort first to cover augments augmenting other augments */
Radek Krejci3641f562019-02-13 15:38:40 +01006629 ret = lys_compile_augment_sort(&ctx, sp->augments, sp->includes, &augments);
Radek Krejci95710c92019-02-11 15:49:55 +01006630 LY_CHECK_GOTO(ret, error);
6631 LY_ARRAY_FOR(augments, u) {
Radek Krejciec4da802019-05-02 13:02:41 +02006632 ret = lys_compile_augment(&ctx, augments[u], NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006633 LY_CHECK_GOTO(ret, error);
6634 }
Radek Krejciccd20f12019-02-15 14:12:27 +01006635
Radek Krejci474f9b82019-07-24 11:36:37 +02006636 /* deviations TODO cover deviations from submodules */
Radek Krejciec4da802019-05-02 13:02:41 +02006637 ret = lys_compile_deviations(&ctx, sp);
Radek Krejciccd20f12019-02-15 14:12:27 +01006638 LY_CHECK_GOTO(ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006639
Radek Krejciec4da802019-05-02 13:02:41 +02006640 COMPILE_ARRAY_GOTO(&ctx, sp->exts, mod_c->exts, u, lys_compile_ext, ret, error);
Radek Krejci19a96102018-11-15 13:38:09 +01006641
Radek Krejcia3045382018-11-22 14:30:31 +01006642 /* validate leafref's paths and when/must xpaths */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006643 /* for leafref, we need 2 rounds - first detects circular chain by storing the first referred type (which
6644 * can be also leafref, in case it is already resolved, go through the chain and check that it does not
6645 * 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 +01006646 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006647 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejcia3045382018-11-22 14:30:31 +01006648 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6649 if (type->basetype == LY_TYPE_LEAFREF) {
6650 /* validate the path */
Radek Krejci412ddfa2018-11-23 11:44:11 +01006651 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 +01006652 LY_CHECK_GOTO(ret, error);
Radek Krejcicdfecd92018-11-26 11:27:32 +01006653 } else if (type->basetype == LY_TYPE_UNION) {
6654 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6655 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6656 /* validate the path */
6657 ret = lys_compile_leafref_validate(&ctx, ((struct lysc_node*)ctx.unres.objs[u]),
6658 (struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v]);
6659 LY_CHECK_GOTO(ret, error);
6660 }
6661 }
Radek Krejcia3045382018-11-22 14:30:31 +01006662 }
6663 }
6664 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006665 for (u = 0; u < ctx.unres.count; ++u) {
Radek Krejci0e5d8382018-11-28 16:37:53 +01006666 if (((struct lysc_node*)ctx.unres.objs[u])->nodetype & (LYS_LEAF | LYS_LEAFLIST)) {
Radek Krejci412ddfa2018-11-23 11:44:11 +01006667 type = ((struct lysc_node_leaf*)ctx.unres.objs[u])->type;
6668 if (type->basetype == LY_TYPE_LEAFREF) {
6669 /* store pointer to the real type */
6670 for (typeiter = ((struct lysc_type_leafref*)type)->realtype;
6671 typeiter->basetype == LY_TYPE_LEAFREF;
6672 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6673 ((struct lysc_type_leafref*)type)->realtype = typeiter;
Radek Krejcicdfecd92018-11-26 11:27:32 +01006674 } else if (type->basetype == LY_TYPE_UNION) {
6675 LY_ARRAY_FOR(((struct lysc_type_union*)type)->types, v) {
6676 if (((struct lysc_type_union*)type)->types[v]->basetype == LY_TYPE_LEAFREF) {
6677 /* store pointer to the real type */
6678 for (typeiter = ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype;
6679 typeiter->basetype == LY_TYPE_LEAFREF;
6680 typeiter = ((struct lysc_type_leafref*)typeiter)->realtype);
6681 ((struct lysc_type_leafref*)((struct lysc_type_union*)type)->types[v])->realtype = typeiter;
6682 }
6683 }
Radek Krejci412ddfa2018-11-23 11:44:11 +01006684 }
6685 }
6686 }
Radek Krejciec4da802019-05-02 13:02:41 +02006687
Radek Krejci474f9b82019-07-24 11:36:37 +02006688 /* finish incomplete default values compilation */
6689 for (u = 0; u < ctx.dflts.count; ++u) {
6690 struct ly_err_item *err = NULL;
6691 struct lysc_incomplete_dflt *r = ctx.dflts.objs[u];
6692 ret = r->dflt->realtype->plugin->store(ctx.ctx, r->dflt->realtype, r->dflt->canonized, strlen(r->dflt->canonized),
6693 LY_TYPE_OPTS_SCHEMA | LY_TYPE_OPTS_STORE | LY_TYPE_OPTS_SECOND_CALL, lys_resolve_prefix,
6694 (void*)r->dflt_mod, LYD_XML, r->context_node, NULL, r->dflt, NULL, &err);
6695 if (err) {
6696 ly_err_print(err);
6697 ctx.path[0] = '\0';
6698 lysc_path(r->context_node, LY_PATH_LOG, ctx.path, LYSC_CTX_BUFSIZE);
6699 LOGVAL(ctx.ctx, LY_VLOG_STR, ctx.path, LYVE_SEMANTICS,
6700 "Invalid default - value does not fit the type (%s).", err->msg);
6701 ly_err_free(err);
6702 }
6703 LY_CHECK_GOTO(ret, error);
6704 }
6705
Radek Krejcif2de0ed2019-05-02 14:13:18 +02006706 /* validate non-instantiated groupings from the parsed schema,
6707 * without it we would accept even the schemas with invalid grouping specification */
6708 ctx.options |= LYSC_OPT_GROUPING;
6709 LY_ARRAY_FOR(sp->groupings, u) {
6710 if (!(sp->groupings[u].flags & LYS_USED_GRP)) {
6711 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &sp->groupings[u])) != LY_SUCCESS, error);
6712 }
6713 }
6714 LY_LIST_FOR(sp->data, node_p) {
6715 grps = (struct lysp_grp*)lysp_node_groupings(node_p);
6716 LY_ARRAY_FOR(grps, u) {
6717 if (!(grps[u].flags & LYS_USED_GRP)) {
6718 LY_CHECK_GOTO((ret = lys_compile_grouping(&ctx, node_p, &grps[u])) != LY_SUCCESS, error);
6719 }
6720 }
6721 }
6722
Radek Krejci474f9b82019-07-24 11:36:37 +02006723 if (ctx.ctx->flags & LY_CTX_CHANGED_TREE) {
6724 /* TODO Deviation has changed tree of a module(s) in the context (by deviate-not-supported), it is necessary to recompile
6725 leafref paths, default values and must/when expressions in all schemas of the context to check that they are still valid */
6726 }
6727
Radek Krejci1c0c3442019-07-23 16:08:47 +02006728 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006729 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006730 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006731 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006732 LY_ARRAY_FREE(augments);
Radek Krejcia3045382018-11-22 14:30:31 +01006733
Radek Krejciec4da802019-05-02 13:02:41 +02006734 if (ctx.options & LYSC_OPT_FREE_SP) {
Radek Krejci19a96102018-11-15 13:38:09 +01006735 lysp_module_free(mod->parsed);
6736 ((struct lys_module*)mod)->parsed = NULL;
6737 }
6738
Radek Krejciec4da802019-05-02 13:02:41 +02006739 if (!(ctx.options & LYSC_OPT_INTERNAL)) {
Radek Krejci95710c92019-02-11 15:49:55 +01006740 /* remove flag of the modules implemented by dependency */
6741 for (u = 0; u < ctx.ctx->list.count; ++u) {
6742 m = ctx.ctx->list.objs[u];
6743 if (m->implemented == 2) {
6744 m->implemented = 1;
6745 }
6746 }
6747 }
6748
Radek Krejci19a96102018-11-15 13:38:09 +01006749 ((struct lys_module*)mod)->compiled = mod_c;
6750 return LY_SUCCESS;
6751
6752error:
Radek Krejci95710c92019-02-11 15:49:55 +01006753 lys_feature_precompile_revert(&ctx, mod);
Radek Krejci1c0c3442019-07-23 16:08:47 +02006754 ly_set_erase(&ctx.dflts, free);
Radek Krejcia3045382018-11-22 14:30:31 +01006755 ly_set_erase(&ctx.unres, NULL);
Radek Krejcie86bf772018-12-14 11:39:53 +01006756 ly_set_erase(&ctx.groupings, NULL);
Radek Krejci99b5b2a2019-04-30 16:57:04 +02006757 ly_set_erase(&ctx.tpdf_chain, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006758 LY_ARRAY_FREE(augments);
Radek Krejci19a96102018-11-15 13:38:09 +01006759 lysc_module_free(mod_c, NULL);
Radek Krejci95710c92019-02-11 15:49:55 +01006760 mod->compiled = NULL;
6761
6762 /* revert compilation of modules implemented by dependency */
6763 for (u = 0; u < ctx.ctx->list.count; ++u) {
6764 m = ctx.ctx->list.objs[u];
6765 if (m->implemented == 2) {
6766 /* revert features list to the precompiled state */
6767 lys_feature_precompile_revert(&ctx, m);
6768 /* mark module as imported-only / not-implemented */
6769 m->implemented = 0;
6770 /* free the compiled version of the module */
6771 lysc_module_free(m->compiled, NULL);
6772 m->compiled = NULL;
6773 }
6774 }
6775
Radek Krejci19a96102018-11-15 13:38:09 +01006776 return ret;
6777}